From 46bb8b1d46f0aebeee1138d4ae5fdfc502f8d7c9 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Mon, 17 Aug 2026 10:55:09 +0900 Subject: [PATCH] ibus: the InputContext interface as a table onmsg matched the member name, then handleplain matched four of the names again after checking their empty signature, and the handlers that take arguments each re-checked theirs with a message of their own. One table of member, signature, and handler does the matching and the checking once; the handlers read arguments a signature has already vouched for. --- ibus.c | 76 +++++++++++++++++++++++++--------------------------------- 1 file changed, 32 insertions(+), 44 deletions(-) diff --git a/ibus.c b/ibus.c index 2245d2e..9dd1324 100644 --- a/ibus.c +++ b/ibus.c @@ -630,13 +630,9 @@ handlekey(DBusConnection *c, DBusMessage *m, Ictx *ctx) char commit[Maxutf], preedit[Maxutf]; int restart; - if(!dbus_message_get_args(m, nil, - DBUS_TYPE_UINT32, &sym, - DBUS_TYPE_UINT32, &code, - DBUS_TYPE_UINT32, &state, - DBUS_TYPE_INVALID)) - return handleerror(c, m, DBUS_ERROR_INVALID_ARGS, - "ProcessKeyEvent expects three unsigned integers"); + dbus_message_get_args(m, nil, DBUS_TYPE_UINT32, &sym, + DBUS_TYPE_UINT32, &code, DBUS_TYPE_UINT32, &state, + DBUS_TYPE_INVALID); if(!processkey(ctx, sym, state, &res)) return replybool(c, m, 0); stoutf(&res.commit, commit, sizeof commit); @@ -722,10 +718,7 @@ handlecap(DBusConnection *c, DBusMessage *m, Ictx *ctx) char preedit[Maxutf]; u32int old; - if(!dbus_message_get_args(m, nil, DBUS_TYPE_UINT32, &cap, - DBUS_TYPE_INVALID)) - return handleerror(c, m, DBUS_ERROR_INVALID_ARGS, - "SetCapabilities expects one unsigned integer"); + dbus_message_get_args(m, nil, DBUS_TYPE_UINT32, &cap, DBUS_TYPE_INVALID); old = ctx->cap; ctx->cap = cap; if(ctx->focused){ @@ -865,32 +858,35 @@ handlecursor(DBusConnection *c, DBusMessage *m, Ictx *ctx) { dbus_int32_t x, y, w, h; - if(!dbus_message_get_args(m, nil, - DBUS_TYPE_INT32, &x, DBUS_TYPE_INT32, &y, - DBUS_TYPE_INT32, &w, DBUS_TYPE_INT32, &h, - DBUS_TYPE_INVALID)) - return handleerror(c, m, DBUS_ERROR_INVALID_ARGS, - "SetCursorLocation expects four integers"); + dbus_message_get_args(m, nil, DBUS_TYPE_INT32, &x, DBUS_TYPE_INT32, &y, + DBUS_TYPE_INT32, &w, DBUS_TYPE_INT32, &h, DBUS_TYPE_INVALID); setcursor(ctx, x, y, h); return reply(c, m, DBUS_TYPE_INVALID, nil); } -/* Argument-free InputContext calls, checked for an empty signature. */ +/* SetEngine: there is one engine, so any name is fine. */ static DBusHandlerResult -handleplain(DBusConnection *c, DBusMessage *m, Ictx *ctx, const char *member) +handleok(DBusConnection *c, DBusMessage *m, Ictx*) { - if(!dbus_message_has_signature(m, "")) - return handleerror(c, m, DBUS_ERROR_INVALID_ARGS, - "this call takes no arguments"); - if(strcmp(member, "FocusIn") == 0) - return handlefocusin(c, m, ctx); - if(strcmp(member, "FocusOut") == 0) - return handlefocusout(c, m, ctx); - if(strcmp(member, "Reset") == 0) - return handlereset(c, m, ctx); - return handledestroy(c, m, ctx); + return reply(c, m, DBUS_TYPE_INVALID, nil); } +/* The InputContext interface: member, argument signature, handler. */ +static struct { + char *member; + char *sig; + DBusHandlerResult (*fn)(DBusConnection*, DBusMessage*, Ictx*); +} ictab[] = { + {"ProcessKeyEvent", "uuu", handlekey}, + {"FocusIn", "", handlefocusin}, + {"FocusOut", "", handlefocusout}, + {"Reset", "", handlereset}, + {"Destroy", "", handledestroy}, + {"SetCursorLocation", "iiii", handlecursor}, + {"SetCapabilities", "u", handlecap}, + {"SetEngine", "s", handleok}, +}; + /* * One handler for everything a client sends: the tiny slice of the bus * that libibus needs, then the InputContext interface. Unhandled calls @@ -901,6 +897,7 @@ onmsg(DBusConnection *c, DBusMessage *m, void*) { const char *iface, *member, *path; Ictx *ctx; + int i; if(dbus_message_get_type(m) != DBUS_MESSAGE_TYPE_METHOD_CALL) return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; @@ -953,22 +950,13 @@ onmsg(DBusConnection *c, DBusMessage *m, void*) if(ctx == nil) return handleerror(c, m, DBUS_ERROR_UNKNOWN_OBJECT, "unknown input context"); - if(strcmp(member, "ProcessKeyEvent") == 0) - return handlekey(c, m, ctx); - if(strcmp(member, "FocusIn") == 0 || - strcmp(member, "FocusOut") == 0 || - strcmp(member, "Reset") == 0 || - strcmp(member, "Destroy") == 0) - return handleplain(c, m, ctx, member); - if(strcmp(member, "SetCursorLocation") == 0) - return handlecursor(c, m, ctx); - if(strcmp(member, "SetCapabilities") == 0) - return handlecap(c, m, ctx); - if(strcmp(member, "SetEngine") == 0){ - if(!dbus_message_has_signature(m, "s")) + for(i = 0; i < nelem(ictab); i++){ + if(strcmp(member, ictab[i].member) != 0) + continue; + if(!dbus_message_has_signature(m, ictab[i].sig)) return handleerror(c, m, DBUS_ERROR_INVALID_ARGS, - "SetEngine expects one engine name"); - return reply(c, m, DBUS_TYPE_INVALID, nil); + "bad arguments"); + return ictab[i].fn(c, m, ctx); } } return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;