ibus: support modern client preedit
This commit is contained in:
164
ibus.c
164
ibus.c
@@ -22,6 +22,9 @@ enum
|
||||
Ibuspurposepassword = 8,
|
||||
Ibuspurposepin = 9,
|
||||
Ibushinthidden = 1<<12,
|
||||
Ibusattrunderline = 1,
|
||||
Ibusunderlinesingle = 1,
|
||||
Ibuspreeditclear = 0,
|
||||
};
|
||||
|
||||
typedef struct Ictx Ictx;
|
||||
@@ -33,6 +36,7 @@ struct Ictx
|
||||
u32int cap;
|
||||
u32int purpose;
|
||||
u32int hints;
|
||||
int clientcommitpreedit;
|
||||
Caret caret;
|
||||
};
|
||||
|
||||
@@ -49,8 +53,11 @@ static int addrowned;
|
||||
static int icctr;
|
||||
static int busctr;
|
||||
static Channel *replyc;
|
||||
static const char ibusowner[] = ":1.0";
|
||||
|
||||
static DBusHandlerResult onmsg(DBusConnection*, DBusMessage*, void*);
|
||||
static DBusHandlerResult handleerror(DBusConnection*, DBusMessage*,
|
||||
const char*, const char*);
|
||||
|
||||
static void
|
||||
unlinkaddr(void)
|
||||
@@ -380,11 +387,14 @@ setcursor(Ictx *ctx, int x, int y, int h)
|
||||
}
|
||||
|
||||
static void
|
||||
appendibustext(DBusMessageIter *it, const char *s)
|
||||
appendibustext(DBusMessageIter *it, const char *s, int underline)
|
||||
{
|
||||
DBusMessageIter v, st, attach, alv, ali, attr, alist;
|
||||
DBusMessageIter av, ai, aattach;
|
||||
const char *name = "IBusText";
|
||||
const char *aname = "IBusAttrList";
|
||||
const char *iname = "IBusAttribute";
|
||||
dbus_uint32_t type, value, start, end;
|
||||
|
||||
dbus_message_iter_open_container(it, DBUS_TYPE_VARIANT, "(sa{sv}sv)", &v);
|
||||
dbus_message_iter_open_container(&v, DBUS_TYPE_STRUCT, nil, &st);
|
||||
@@ -398,6 +408,25 @@ appendibustext(DBusMessageIter *it, const char *s)
|
||||
dbus_message_iter_open_container(&ali, DBUS_TYPE_ARRAY, "{sv}", &attr);
|
||||
dbus_message_iter_close_container(&ali, &attr);
|
||||
dbus_message_iter_open_container(&ali, DBUS_TYPE_ARRAY, "v", &alist);
|
||||
if(underline && s[0] != '\0'){
|
||||
type = Ibusattrunderline;
|
||||
value = Ibusunderlinesingle;
|
||||
start = 0;
|
||||
end = utflen((char*)s);
|
||||
dbus_message_iter_open_container(&alist, DBUS_TYPE_VARIANT,
|
||||
"(sa{sv}uuuu)", &av);
|
||||
dbus_message_iter_open_container(&av, DBUS_TYPE_STRUCT, nil, &ai);
|
||||
dbus_message_iter_append_basic(&ai, DBUS_TYPE_STRING, &iname);
|
||||
dbus_message_iter_open_container(&ai, DBUS_TYPE_ARRAY, "{sv}",
|
||||
&aattach);
|
||||
dbus_message_iter_close_container(&ai, &aattach);
|
||||
dbus_message_iter_append_basic(&ai, DBUS_TYPE_UINT32, &type);
|
||||
dbus_message_iter_append_basic(&ai, DBUS_TYPE_UINT32, &value);
|
||||
dbus_message_iter_append_basic(&ai, DBUS_TYPE_UINT32, &start);
|
||||
dbus_message_iter_append_basic(&ai, DBUS_TYPE_UINT32, &end);
|
||||
dbus_message_iter_close_container(&av, &ai);
|
||||
dbus_message_iter_close_container(&alist, &av);
|
||||
}
|
||||
dbus_message_iter_close_container(&ali, &alist);
|
||||
dbus_message_iter_close_container(&alv, &ali);
|
||||
dbus_message_iter_close_container(&st, &alv);
|
||||
@@ -415,30 +444,39 @@ emitcommit(DBusConnection *c, const char *path, const char *text)
|
||||
if(sig == nil)
|
||||
return;
|
||||
dbus_message_iter_init_append(sig, &it);
|
||||
appendibustext(&it, text);
|
||||
appendibustext(&it, text, 0);
|
||||
dbus_message_set_sender(sig, ibusowner);
|
||||
dbus_connection_send(c, sig, nil);
|
||||
dbus_message_unref(sig);
|
||||
}
|
||||
|
||||
static void
|
||||
emitpreedit(DBusConnection *c, const char *path, const char *text)
|
||||
emitpreedit(Ictx *ctx, const char *text)
|
||||
{
|
||||
DBusMessage *sig;
|
||||
DBusMessageIter it;
|
||||
dbus_uint32_t cursor;
|
||||
dbus_uint32_t cursor, mode;
|
||||
dbus_bool_t visible;
|
||||
const char *name;
|
||||
|
||||
sig = dbus_message_new_signal(path,
|
||||
"org.freedesktop.IBus.InputContext", "UpdatePreeditText");
|
||||
name = ctx->clientcommitpreedit ?
|
||||
"UpdatePreeditTextWithMode" : "UpdatePreeditText";
|
||||
sig = dbus_message_new_signal(ctx->path,
|
||||
"org.freedesktop.IBus.InputContext", name);
|
||||
if(sig == nil)
|
||||
return;
|
||||
dbus_message_iter_init_append(sig, &it);
|
||||
appendibustext(&it, text);
|
||||
appendibustext(&it, text, 1);
|
||||
cursor = utflen((char*)text);
|
||||
visible = text[0] != '\0' ? TRUE : FALSE;
|
||||
dbus_message_iter_append_basic(&it, DBUS_TYPE_UINT32, &cursor);
|
||||
dbus_message_iter_append_basic(&it, DBUS_TYPE_BOOLEAN, &visible);
|
||||
dbus_connection_send(c, sig, nil);
|
||||
if(ctx->clientcommitpreedit){
|
||||
mode = Ibuspreeditclear;
|
||||
dbus_message_iter_append_basic(&it, DBUS_TYPE_UINT32, &mode);
|
||||
}
|
||||
dbus_message_set_sender(sig, ibusowner);
|
||||
dbus_connection_send(ctx->conn, sig, nil);
|
||||
dbus_message_unref(sig);
|
||||
}
|
||||
|
||||
@@ -459,6 +497,33 @@ handlehello(DBusConnection *c, DBusMessage *m)
|
||||
return DBUS_HANDLER_RESULT_HANDLED;
|
||||
}
|
||||
|
||||
static DBusHandlerResult
|
||||
handlenameowner(DBusConnection *c, DBusMessage *m)
|
||||
{
|
||||
DBusError err;
|
||||
DBusMessage *r;
|
||||
const char *name, *owner;
|
||||
|
||||
dbus_error_init(&err);
|
||||
if(!dbus_message_get_args(m, &err, DBUS_TYPE_STRING, &name,
|
||||
DBUS_TYPE_INVALID)){
|
||||
dbus_error_free(&err);
|
||||
return handleerror(c, m, DBUS_ERROR_INVALID_ARGS,
|
||||
"GetNameOwner expects one bus name");
|
||||
}
|
||||
if(strcmp(name, "org.freedesktop.IBus") != 0)
|
||||
return handleerror(c, m, DBUS_ERROR_NAME_HAS_NO_OWNER,
|
||||
"bus name has no owner");
|
||||
owner = ibusowner;
|
||||
r = dbus_message_new_method_return(m);
|
||||
if(r == nil)
|
||||
return DBUS_HANDLER_RESULT_NEED_MEMORY;
|
||||
dbus_message_append_args(r, DBUS_TYPE_STRING, &owner, DBUS_TYPE_INVALID);
|
||||
dbus_connection_send(c, r, nil);
|
||||
dbus_message_unref(r);
|
||||
return DBUS_HANDLER_RESULT_HANDLED;
|
||||
}
|
||||
|
||||
static DBusHandlerResult
|
||||
handleerror(DBusConnection *c, DBusMessage *m, const char *name,
|
||||
const char *text)
|
||||
@@ -539,7 +604,7 @@ handlekey(DBusConnection *c, DBusMessage *m, Ictx *ctx)
|
||||
if(commit[0] != '\0')
|
||||
emitcommit(c, dbus_message_get_path(m), commit);
|
||||
if(ctx->cap & Ibuscappreedit)
|
||||
emitpreedit(c, dbus_message_get_path(m), preedit);
|
||||
emitpreedit(ctx, preedit);
|
||||
return handlebool(c, m, res.eaten);
|
||||
}
|
||||
|
||||
@@ -560,7 +625,7 @@ handlereset(DBusConnection *c, DBusMessage *m, Ictx *ctx)
|
||||
Keyres res;
|
||||
|
||||
sendrequest(ctx, Keyreset, 0, 0, &res);
|
||||
emitpreedit(c, dbus_message_get_path(m), "");
|
||||
emitpreedit(ctx, "");
|
||||
return handlenoop(c, m);
|
||||
}
|
||||
|
||||
@@ -575,7 +640,7 @@ static DBusHandlerResult
|
||||
handlefocusout(DBusConnection *c, DBusMessage *m, Ictx *ctx)
|
||||
{
|
||||
releasecontext(ctx);
|
||||
emitpreedit(c, dbus_message_get_path(m), "");
|
||||
emitpreedit(ctx, "");
|
||||
return handlenoop(c, m);
|
||||
}
|
||||
|
||||
@@ -602,20 +667,19 @@ handlecap(DBusConnection *c, DBusMessage *m, Ictx *ctx)
|
||||
if((old & Ibuscappreedit) != (ctx->cap & Ibuscappreedit)){
|
||||
if((ctx->cap & Ibuscappreedit) && res.eaten){
|
||||
stoutf(&res.preedit, preedit, sizeof preedit);
|
||||
emitpreedit(c, ctx->path, preedit);
|
||||
emitpreedit(ctx, preedit);
|
||||
}else if(!(ctx->cap & Ibuscappreedit))
|
||||
emitpreedit(c, ctx->path, "");
|
||||
emitpreedit(ctx, "");
|
||||
}
|
||||
}
|
||||
return handlenoop(c, m);
|
||||
}
|
||||
|
||||
static int
|
||||
getcontent(DBusMessage *m, const char **iface, const char **name,
|
||||
u32int *purpose, u32int *hints)
|
||||
getproperty(DBusMessage *m, const char **iface, const char **name,
|
||||
DBusMessageIter *value)
|
||||
{
|
||||
DBusMessageIter it, v, st;
|
||||
dbus_uint32_t p, h;
|
||||
DBusMessageIter it;
|
||||
|
||||
if(!dbus_message_has_signature(m, "ssv") ||
|
||||
!dbus_message_iter_init(m, &it))
|
||||
@@ -624,10 +688,19 @@ getcontent(DBusMessage *m, const char **iface, const char **name,
|
||||
dbus_message_iter_next(&it);
|
||||
dbus_message_iter_get_basic(&it, name);
|
||||
dbus_message_iter_next(&it);
|
||||
dbus_message_iter_recurse(&it, &v);
|
||||
if(dbus_message_iter_get_arg_type(&v) != DBUS_TYPE_STRUCT)
|
||||
dbus_message_iter_recurse(&it, value);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
getcontent(DBusMessageIter *value, u32int *purpose, u32int *hints)
|
||||
{
|
||||
DBusMessageIter st;
|
||||
dbus_uint32_t p, h;
|
||||
|
||||
if(dbus_message_iter_get_arg_type(value) != DBUS_TYPE_STRUCT)
|
||||
return 0;
|
||||
dbus_message_iter_recurse(&v, &st);
|
||||
dbus_message_iter_recurse(value, &st);
|
||||
if(dbus_message_iter_get_arg_type(&st) != DBUS_TYPE_UINT32)
|
||||
return 0;
|
||||
dbus_message_iter_get_basic(&st, &p);
|
||||
@@ -646,27 +719,48 @@ static DBusHandlerResult
|
||||
handlepropertyset(DBusConnection *c, DBusMessage *m, Ictx *ctx)
|
||||
{
|
||||
const char *iface, *name;
|
||||
DBusMessageIter value, st;
|
||||
dbus_bool_t b;
|
||||
u32int purpose, hints;
|
||||
Keyres res;
|
||||
int washidden;
|
||||
|
||||
if(!getcontent(m, &iface, &name, &purpose, &hints))
|
||||
if(!getproperty(m, &iface, &name, &value))
|
||||
return handleerror(c, m, DBUS_ERROR_INVALID_ARGS,
|
||||
"Properties.Set expects ContentType as (uu)");
|
||||
"Properties.Set expects interface, property, and value");
|
||||
if(strcmp(iface, "org.freedesktop.IBus.InputContext") != 0)
|
||||
return handleerror(c, m, DBUS_ERROR_UNKNOWN_INTERFACE,
|
||||
"unknown property interface");
|
||||
if(strcmp(name, "ContentType") != 0)
|
||||
return handleerror(c, m, DBUS_ERROR_UNKNOWN_PROPERTY,
|
||||
"unknown input context property");
|
||||
washidden = hidden(ctx);
|
||||
ctx->purpose = purpose;
|
||||
ctx->hints = hints;
|
||||
if(ctx->focused && !washidden && hidden(ctx)){
|
||||
sendrequest(ctx, Keyreset, 0, 0, &res);
|
||||
emitpreedit(c, ctx->path, "");
|
||||
if(strcmp(name, "ContentType") == 0){
|
||||
if(!getcontent(&value, &purpose, &hints))
|
||||
return handleerror(c, m, DBUS_ERROR_INVALID_ARGS,
|
||||
"ContentType expects (uu)");
|
||||
washidden = hidden(ctx);
|
||||
ctx->purpose = purpose;
|
||||
ctx->hints = hints;
|
||||
if(ctx->focused && !washidden && hidden(ctx)){
|
||||
sendrequest(ctx, Keyreset, 0, 0, &res);
|
||||
emitpreedit(ctx, "");
|
||||
}
|
||||
return handlenoop(c, m);
|
||||
}
|
||||
return handlenoop(c, m);
|
||||
if(strcmp(name, "ClientCommitPreedit") == 0){
|
||||
if(dbus_message_iter_get_arg_type(&value) != DBUS_TYPE_STRUCT)
|
||||
return handleerror(c, m, DBUS_ERROR_INVALID_ARGS,
|
||||
"ClientCommitPreedit expects (b)");
|
||||
dbus_message_iter_recurse(&value, &st);
|
||||
if(dbus_message_iter_get_arg_type(&st) != DBUS_TYPE_BOOLEAN)
|
||||
return handleerror(c, m, DBUS_ERROR_INVALID_ARGS,
|
||||
"ClientCommitPreedit expects (b)");
|
||||
dbus_message_iter_get_basic(&st, &b);
|
||||
if(dbus_message_iter_next(&st))
|
||||
return handleerror(c, m, DBUS_ERROR_INVALID_ARGS,
|
||||
"ClientCommitPreedit expects (b)");
|
||||
ctx->clientcommitpreedit = b != FALSE;
|
||||
return handlenoop(c, m);
|
||||
}
|
||||
return handleerror(c, m, DBUS_ERROR_UNKNOWN_PROPERTY,
|
||||
"unknown input context property");
|
||||
}
|
||||
|
||||
static DBusHandlerResult
|
||||
@@ -778,9 +872,13 @@ static const char introspectxml[] =
|
||||
"<arg direction=\"in\" type=\"i\"/><arg direction=\"in\" type=\"i\"/></method>\n"
|
||||
" <method name=\"SetEngine\"><arg direction=\"in\" type=\"s\"/></method>\n"
|
||||
" <property name=\"ContentType\" type=\"(uu)\" access=\"write\"/>\n"
|
||||
" <property name=\"ClientCommitPreedit\" type=\"(b)\" access=\"write\"/>\n"
|
||||
" <signal name=\"CommitText\"><arg type=\"v\"/></signal>\n"
|
||||
" <signal name=\"UpdatePreeditText\">"
|
||||
"<arg type=\"v\"/><arg type=\"u\"/><arg type=\"b\"/></signal>\n"
|
||||
" <signal name=\"UpdatePreeditTextWithMode\">"
|
||||
"<arg type=\"v\"/><arg type=\"u\"/><arg type=\"b\"/>"
|
||||
"<arg type=\"u\"/></signal>\n"
|
||||
" </interface>\n"
|
||||
" <interface name=\"org.freedesktop.DBus.Properties\">\n"
|
||||
" <method name=\"Get\"><arg direction=\"in\" type=\"s\"/>"
|
||||
@@ -825,6 +923,8 @@ onmsg(DBusConnection *c, DBusMessage *m, void *_)
|
||||
if(strcmp(iface, "org.freedesktop.DBus") == 0){
|
||||
if(strcmp(member, "Hello") == 0)
|
||||
return handlehello(c, m);
|
||||
if(strcmp(member, "GetNameOwner") == 0)
|
||||
return handlenameowner(c, m);
|
||||
if(strcmp(member, "AddMatch") == 0
|
||||
|| strcmp(member, "RemoveMatch") == 0)
|
||||
return handlenoop(c, m);
|
||||
|
||||
Reference in New Issue
Block a user