xim: place popup from input context

This commit is contained in:
2026-08-14 17:35:33 +09:00
parent 2b4c509d0d
commit e6bf32b0f6
4 changed files with 411 additions and 13 deletions

132
xim/xim.c
View File

@@ -21,6 +21,11 @@ struct Ic
int encoding;
int prestarted;
int nprerune;
xcb_window_t clientwin;
xcb_window_t focuswin;
xcb_point_t spot;
int hasspot;
Caret caret;
};
typedef struct Clientenc Clientenc;
@@ -41,6 +46,7 @@ static xcb_im_t *xim;
static xcb_keysym_t *kmap;
static uint8_t minkc, maxkc;
static uint8_t symsper;
static xcb_window_t rootwin;
static int opened;
static Ic *ics;
static Ic *preowner;
@@ -181,6 +187,108 @@ getscreen(int scr)
return nil;
}
static int
readattrs(Ic *state)
{
const xcb_im_preedit_attr_t *attr;
xcb_window_t clientwin, focuswin;
xcb_point_t spot;
int changed, hasspot;
clientwin = xcb_im_input_context_get_client_window(state->xic);
focuswin = xcb_im_input_context_get_focus_window(state->xic);
spot = state->spot;
hasspot = 0;
if(state->style == (XCB_IM_PreeditPosition | XCB_IM_StatusNothing) &&
(xcb_im_input_context_get_preedit_attr_mask(state->xic) &
XCB_XIM_XNSpotLocation_MASK)){
attr = xcb_im_input_context_get_preedit_attr(state->xic);
if(attr != nil){
spot = attr->spot_location;
hasspot = 1;
}
}
changed = clientwin != state->clientwin || focuswin != state->focuswin ||
hasspot != state->hasspot || (hasspot &&
(spot.x != state->spot.x || spot.y != state->spot.y));
state->clientwin = clientwin;
state->focuswin = focuswin;
state->spot = spot;
state->hasspot = hasspot;
return changed;
}
static int
translate(xcb_window_t win, int x, int y, Caret *caret)
{
xcb_translate_coordinates_cookie_t cookie;
xcb_translate_coordinates_reply_t *reply;
if(win == XCB_NONE)
return 0;
cookie = xcb_translate_coordinates(conn, win, rootwin, x, y);
reply = xcb_translate_coordinates_reply(conn, cookie, nil);
if(reply == nil || !reply->same_screen){
free(reply);
return 0;
}
caret->valid = 1;
caret->x = reply->dst_x;
caret->y = reply->dst_y;
caret->h = 0;
free(reply);
return 1;
}
static int
placebottom(Ic *state, xcb_window_t win)
{
xcb_get_geometry_cookie_t gcookie;
xcb_get_geometry_reply_t *geometry;
int height;
if(win == XCB_NONE)
return 0;
gcookie = xcb_get_geometry(conn, win);
geometry = xcb_get_geometry_reply(conn, gcookie, nil);
if(geometry == nil)
return 0;
height = geometry->height;
free(geometry);
if(!translate(win, 0, 0, &state->caret))
return 0;
state->caret.y += height;
return 1;
}
static void
place(Ic *state)
{
xcb_window_t win[2];
int i, nwin;
memset(&state->caret, 0, sizeof state->caret);
win[0] = state->focuswin;
nwin = 1;
if(state->clientwin != state->focuswin)
win[nwin++] = state->clientwin;
if(state->hasspot)
for(i = 0; i < nwin; i++)
if(translate(win[i], state->spot.x, state->spot.y,
&state->caret))
return;
for(i = 0; i < nwin; i++)
if(placebottom(state, win[i]))
return;
}
static void
refreshplace(Ic *state)
{
readattrs(state);
place(state);
}
static void
commit(Ic *state, char *s, int nbyte)
{
@@ -281,6 +389,7 @@ sendrequest(Ic *state, int op, u32int key, u32int mod, Keyres *res)
kr.op = op;
kr.ks = key;
kr.mod = mod & Mmask;
kr.caret = state->caret;
kr.reply = replyc;
chansend(keyc, &kr);
chanrecv(replyc, res);
@@ -317,7 +426,7 @@ kpress(Ic *state, xcb_im_input_context_t *ic, xcb_key_press_event_t *ev)
Keyres res;
char buf[Maxutf];
u32int key, rune;
int n;
int n, wasvalid;
key = kget(ev->detail, ev->state);
rune = xkb_keysym_to_utf32(key);
@@ -327,6 +436,15 @@ kpress(Ic *state, xcb_im_input_context_t *ic, xcb_key_press_event_t *ev)
key = Kspec + (key - 0xff00);
else
key = rune;
if(meaningful(key)){
wasvalid = state->caret.valid;
if(!state->engaged)
refreshplace(state);
else if(readattrs(state))
place(state);
if(state->engaged && wasvalid && !state->caret.valid)
sendrequest(state, Keycaret, 0, 0, &res);
}
keypress(state, key, ev->state, &res);
n = stoutf(&res.commit, buf, sizeof buf);
commit(state, buf, n);
@@ -387,6 +505,7 @@ iccreate(xcb_im_client_t *client, xcb_im_input_context_t *ic)
state->client = client;
state->encoding = getencoding(client);
setstyle(state, xcb_im_input_context_get_input_style(ic));
refreshplace(state);
state->next = ics;
ics = state;
xcb_im_input_context_set_data(ic, state, icfree);
@@ -432,6 +551,15 @@ 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_SET_IC_VALUES:
case XCB_XIM_SET_IC_FOCUS:
refreshplace(state);
if(state->engaged){
Keyres res;
sendrequest(state, Keycaret, 0, 0, &res);
}
break;
case XCB_XIM_DESTROY_IC:
case XCB_XIM_RESET_IC:
case XCB_XIM_UNSET_IC_FOCUS:
@@ -461,6 +589,7 @@ ximclose(void)
free(state);
}
ics = nil;
rootwin = XCB_NONE;
free(kmap);
kmap = nil;
if(conn != nil){
@@ -502,6 +631,7 @@ ximinit(void)
ximlog("cannot find X screen");
return -1;
}
rootwin = screen->root;
if(kinit() < 0){
ximlog("cannot read keyboard mapping");
return -1;