xim: add callback preedit and UTF-8 negotiation
This commit is contained in:
227
xim/xim.c
227
xim/xim.c
@@ -12,8 +12,28 @@ char *ximcompound(const char*, size_t, size_t*);
|
||||
typedef struct Ic Ic;
|
||||
struct Ic
|
||||
{
|
||||
Ic *next;
|
||||
int engaged;
|
||||
Ic *next;
|
||||
xcb_im_input_context_t *xic;
|
||||
xcb_im_client_t *client;
|
||||
int engaged;
|
||||
u32int style;
|
||||
int cap;
|
||||
int encoding;
|
||||
int prestarted;
|
||||
int nprerune;
|
||||
};
|
||||
|
||||
typedef struct Clientenc Clientenc;
|
||||
struct Clientenc
|
||||
{
|
||||
xcb_im_client_t *client;
|
||||
int encoding;
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
Ecompound,
|
||||
Eutf8,
|
||||
};
|
||||
|
||||
static xcb_connection_t *conn;
|
||||
@@ -23,13 +43,76 @@ static uint8_t minkc, maxkc;
|
||||
static uint8_t symsper;
|
||||
static int opened;
|
||||
static Ic *ics;
|
||||
static Ic *preowner;
|
||||
static Clientenc clientenc[Maxclients];
|
||||
static Channel *replyc;
|
||||
|
||||
static char *encs[] = {"COMPOUND_TEXT"};
|
||||
static char *encs[] = {"COMPOUND_TEXT", "UTF8_STRING"};
|
||||
static u32int styles[] = {
|
||||
XCB_IM_PreeditPosition | XCB_IM_StatusNothing,
|
||||
XCB_IM_PreeditCallbacks | XCB_IM_StatusNothing,
|
||||
XCB_IM_PreeditNothing | XCB_IM_StatusNothing,
|
||||
XCB_IM_PreeditNone | XCB_IM_StatusNone,
|
||||
};
|
||||
static int stylecaps[] = {0, Cclientpreedit, 0};
|
||||
|
||||
static int
|
||||
stylecap(u32int style, int *cap)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i = 0; i < nelem(styles); i++)
|
||||
if(styles[i] == style){
|
||||
*cap = stylecaps[i];
|
||||
return 1;
|
||||
}
|
||||
*cap = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
getencoding(xcb_im_client_t *client)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i = 0; i < nelem(clientenc); i++)
|
||||
if(clientenc[i].client == client)
|
||||
return clientenc[i].encoding;
|
||||
return Ecompound;
|
||||
}
|
||||
|
||||
static void
|
||||
setencoding(xcb_im_client_t *client, int encoding)
|
||||
{
|
||||
int empty, i;
|
||||
|
||||
if(client == nil)
|
||||
return;
|
||||
empty = -1;
|
||||
for(i = 0; i < nelem(clientenc); i++){
|
||||
if(clientenc[i].client == client){
|
||||
clientenc[i].encoding = encoding;
|
||||
return;
|
||||
}
|
||||
if(empty < 0 && clientenc[i].client == nil)
|
||||
empty = i;
|
||||
}
|
||||
if(empty >= 0){
|
||||
clientenc[empty].client = client;
|
||||
clientenc[empty].encoding = encoding;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
clearencoding(xcb_im_client_t *client)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i = 0; i < nelem(clientenc); i++)
|
||||
if(clientenc[i].client == client){
|
||||
memset(&clientenc[i], 0, sizeof clientenc[i]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
ximlog(char *msg)
|
||||
@@ -99,19 +182,86 @@ getscreen(int scr)
|
||||
}
|
||||
|
||||
static void
|
||||
commit(xcb_im_input_context_t *ic, char *s, int len)
|
||||
commit(Ic *state, char *s, int nbyte)
|
||||
{
|
||||
char *ct;
|
||||
size_t clen;
|
||||
char *wire;
|
||||
size_t nwire;
|
||||
|
||||
if(len == 0)
|
||||
if(nbyte == 0)
|
||||
return;
|
||||
ct = ximcompound(s, len, &clen);
|
||||
if(ct == nil)
|
||||
wire = s;
|
||||
nwire = nbyte;
|
||||
if(state->encoding == Ecompound){
|
||||
wire = ximcompound(s, nbyte, &nwire);
|
||||
if(wire == nil)
|
||||
return;
|
||||
}
|
||||
xcb_im_commit_string(xim, state->xic, XCB_XIM_LOOKUP_CHARS,
|
||||
wire, (u32int)nwire, 0);
|
||||
if(wire != s)
|
||||
free(wire);
|
||||
}
|
||||
|
||||
static void
|
||||
clearpreedit(Ic *state)
|
||||
{
|
||||
xcb_im_preedit_draw_fr_t frame;
|
||||
|
||||
if(state == nil || !state->prestarted)
|
||||
return;
|
||||
xcb_im_commit_string(xim, ic, XCB_XIM_LOOKUP_CHARS, ct, clen, 0);
|
||||
memset(&frame, 0, sizeof frame);
|
||||
frame.chg_length = state->nprerune;
|
||||
frame.status = 1;
|
||||
xcb_im_preedit_draw_callback(xim, state->xic, &frame);
|
||||
xcb_im_preedit_done_callback(xim, state->xic);
|
||||
state->prestarted = 0;
|
||||
state->nprerune = 0;
|
||||
if(preowner == state)
|
||||
preowner = nil;
|
||||
xcb_flush(conn);
|
||||
free(ct);
|
||||
}
|
||||
|
||||
static void
|
||||
updatepreedit(Ic *state, Str *pre)
|
||||
{
|
||||
xcb_im_preedit_draw_fr_t frame;
|
||||
u32int feedback[Maxrunes];
|
||||
char utf[Maxutf], *wire;
|
||||
size_t nwire;
|
||||
int i, nbyte;
|
||||
|
||||
if(!(state->style & XCB_IM_PreeditCallbacks))
|
||||
return;
|
||||
if(pre->n == 0){
|
||||
clearpreedit(state);
|
||||
return;
|
||||
}
|
||||
nbyte = stoutf(pre, utf, sizeof utf);
|
||||
wire = utf;
|
||||
nwire = nbyte;
|
||||
if(state->encoding == Ecompound){
|
||||
wire = ximcompound(utf, nbyte, &nwire);
|
||||
if(wire == nil)
|
||||
return;
|
||||
}
|
||||
for(i = 0; i < pre->n; i++)
|
||||
feedback[i] = XCB_XIM_UNDERLINE;
|
||||
if(!state->prestarted){
|
||||
xcb_im_preedit_start_callback(xim, state->xic);
|
||||
state->prestarted = 1;
|
||||
}
|
||||
memset(&frame, 0, sizeof frame);
|
||||
frame.caret = pre->n;
|
||||
frame.chg_length = state->nprerune;
|
||||
frame.length_of_preedit_string = (u16int)nwire;
|
||||
frame.preedit_string = (uchar*)wire;
|
||||
frame.feedback_array.size = pre->n;
|
||||
frame.feedback_array.items = feedback;
|
||||
xcb_im_preedit_draw_callback(xim, state->xic, &frame);
|
||||
state->nprerune = pre->n;
|
||||
preowner = state;
|
||||
if(wire != utf)
|
||||
free(wire);
|
||||
}
|
||||
|
||||
static int
|
||||
@@ -127,6 +277,7 @@ sendrequest(Ic *state, int op, u32int key, u32int mod, Keyres *res)
|
||||
|
||||
memset(&kr, 0, sizeof kr);
|
||||
kr.owner = state;
|
||||
kr.cap = state->cap;
|
||||
kr.op = op;
|
||||
kr.ks = key;
|
||||
kr.mod = mod & Mmask;
|
||||
@@ -138,8 +289,11 @@ sendrequest(Ic *state, int op, u32int key, u32int mod, Keyres *res)
|
||||
static void
|
||||
keypress(Ic *state, u32int key, u32int mod, Keyres *res)
|
||||
{
|
||||
if(meaningful(key))
|
||||
if(meaningful(key)){
|
||||
if(preowner != nil && preowner != state)
|
||||
clearpreedit(preowner);
|
||||
state->engaged = 1;
|
||||
}
|
||||
sendrequest(state, Keypress, key, mod, res);
|
||||
}
|
||||
|
||||
@@ -148,7 +302,10 @@ release(Ic *state)
|
||||
{
|
||||
Keyres res;
|
||||
|
||||
if(state == nil || !state->engaged)
|
||||
if(state == nil)
|
||||
return;
|
||||
clearpreedit(state);
|
||||
if(!state->engaged)
|
||||
return;
|
||||
sendrequest(state, Keyrelease, 0, 0, &res);
|
||||
state->engaged = 0;
|
||||
@@ -172,7 +329,8 @@ kpress(Ic *state, xcb_im_input_context_t *ic, xcb_key_press_event_t *ev)
|
||||
key = rune;
|
||||
keypress(state, key, ev->state, &res);
|
||||
n = stoutf(&res.commit, buf, sizeof buf);
|
||||
commit(ic, buf, n);
|
||||
commit(state, buf, n);
|
||||
updatepreedit(state, &res.preedit);
|
||||
if(!res.eaten)
|
||||
xcb_im_forward_event(xim, ic, ev);
|
||||
xcb_flush(conn);
|
||||
@@ -203,7 +361,20 @@ icfree(void *p)
|
||||
}
|
||||
|
||||
static void
|
||||
iccreate(xcb_im_input_context_t *ic)
|
||||
setstyle(Ic *state, u32int style)
|
||||
{
|
||||
int cap;
|
||||
|
||||
if(!stylecap(style, &cap))
|
||||
style = XCB_IM_PreeditNothing | XCB_IM_StatusNothing;
|
||||
if(state->prestarted && !(style & XCB_IM_PreeditCallbacks))
|
||||
clearpreedit(state);
|
||||
state->style = style;
|
||||
state->cap = cap;
|
||||
}
|
||||
|
||||
static void
|
||||
iccreate(xcb_im_client_t *client, xcb_im_input_context_t *ic)
|
||||
{
|
||||
Ic *state;
|
||||
|
||||
@@ -212,6 +383,10 @@ iccreate(xcb_im_input_context_t *ic)
|
||||
state = calloc(1, sizeof *state);
|
||||
if(state == nil)
|
||||
return;
|
||||
state->xic = ic;
|
||||
state->client = client;
|
||||
state->encoding = getencoding(client);
|
||||
setstyle(state, xcb_im_input_context_get_input_style(ic));
|
||||
state->next = ics;
|
||||
ics = state;
|
||||
xcb_im_input_context_set_data(ic, state, icfree);
|
||||
@@ -225,12 +400,25 @@ callback(xcb_im_t *im, xcb_im_client_t *client, xcb_im_input_context_t *ic,
|
||||
Ic *state;
|
||||
|
||||
USED(im);
|
||||
USED(client);
|
||||
USED(frame);
|
||||
USED(user);
|
||||
if(hdr->major_opcode == XCB_XIM_ENCODING_NEGOTIATION){
|
||||
if(arg != nil)
|
||||
setencoding(client,
|
||||
*(u16int*)arg == 1 ? Eutf8 : Ecompound);
|
||||
return;
|
||||
}
|
||||
if(hdr->major_opcode == XCB_XIM_DISCONNECT ||
|
||||
hdr->major_opcode == XCB_XIM_CLOSE){
|
||||
for(state = ics; state != nil; state = state->next)
|
||||
if(state->client == client)
|
||||
release(state);
|
||||
clearencoding(client);
|
||||
return;
|
||||
}
|
||||
|
||||
if(hdr->major_opcode == XCB_XIM_CREATE_IC){
|
||||
iccreate(ic);
|
||||
iccreate(client, ic);
|
||||
return;
|
||||
}
|
||||
if(ic == nil)
|
||||
@@ -244,6 +432,7 @@ callback(xcb_im_t *im, xcb_im_client_t *client, xcb_im_input_context_t *ic,
|
||||
if(ev != nil && (ev->response_type & ~0x80) == XCB_KEY_PRESS)
|
||||
kpress(state, ic, ev);
|
||||
break;
|
||||
case XCB_XIM_DESTROY_IC:
|
||||
case XCB_XIM_RESET_IC:
|
||||
case XCB_XIM_UNSET_IC_FOCUS:
|
||||
release(state);
|
||||
@@ -265,6 +454,8 @@ ximclose(void)
|
||||
xim = nil;
|
||||
}
|
||||
opened = 0;
|
||||
preowner = nil;
|
||||
memset(clientenc, 0, sizeof clientenc);
|
||||
for(state = ics; state != nil; state = next){
|
||||
next = state->next;
|
||||
free(state);
|
||||
|
||||
Reference in New Issue
Block a user