fix(xim): honor reset and X11 keyboard state
This commit is contained in:
233
xim/xim.c
233
xim/xim.c
@@ -7,10 +7,11 @@
|
||||
#include <unistd.h>
|
||||
#include <xcb/xcb.h>
|
||||
#include <xkbcommon/xkbcommon.h>
|
||||
#include <xkbcommon/xkbcommon-x11.h>
|
||||
#include <xcb-imdkit/imdkit.h>
|
||||
#include <xcb-imdkit/encoding.h>
|
||||
|
||||
u32int keymaplookup(const u32int*, int, u16int);
|
||||
u32int keymaplookup(struct xkb_state*, uint8_t, u16int);
|
||||
char *ximcompound(const char*, size_t, size_t*);
|
||||
|
||||
typedef struct Ic Ic;
|
||||
@@ -35,6 +36,7 @@ struct Ic
|
||||
typedef struct Clientenc Clientenc;
|
||||
struct Clientenc
|
||||
{
|
||||
Clientenc *next;
|
||||
xcb_im_client_t *client;
|
||||
int encoding;
|
||||
};
|
||||
@@ -47,14 +49,13 @@ enum
|
||||
|
||||
static xcb_connection_t *conn;
|
||||
static xcb_im_t *xim;
|
||||
static xcb_keysym_t *kmap;
|
||||
static uint8_t minkc, maxkc;
|
||||
static uint8_t symsper;
|
||||
static struct xkb_state *kstate;
|
||||
static xcb_window_t rootwin;
|
||||
static int opened;
|
||||
static Ic *ics;
|
||||
static Ic *preowner;
|
||||
static Clientenc clientenc[Maxclients];
|
||||
static Ic passthrough;
|
||||
static Clientenc *clientenc;
|
||||
static Channel *replyc;
|
||||
static int wakefd[2] = {-1, -1};
|
||||
|
||||
@@ -148,48 +149,61 @@ wakedrain(void)
|
||||
static int
|
||||
getencoding(xcb_im_client_t *client)
|
||||
{
|
||||
int i;
|
||||
Clientenc *p;
|
||||
|
||||
for(i = 0; i < nelem(clientenc); i++)
|
||||
if(clientenc[i].client == client)
|
||||
return clientenc[i].encoding;
|
||||
for(p = clientenc; p != nil; p = p->next)
|
||||
if(p->client == client)
|
||||
return p->encoding;
|
||||
return Ecompound;
|
||||
}
|
||||
|
||||
static void
|
||||
static int
|
||||
setencoding(xcb_im_client_t *client, int encoding)
|
||||
{
|
||||
int empty, i;
|
||||
Clientenc *p;
|
||||
|
||||
if(client == nil)
|
||||
return;
|
||||
empty = -1;
|
||||
for(i = 0; i < nelem(clientenc); i++){
|
||||
if(clientenc[i].client == client){
|
||||
clientenc[i].encoding = encoding;
|
||||
return;
|
||||
return -1;
|
||||
for(p = clientenc; p != nil; p = p->next)
|
||||
if(p->client == client){
|
||||
p->encoding = encoding;
|
||||
return 0;
|
||||
}
|
||||
if(empty < 0 && clientenc[i].client == nil)
|
||||
empty = i;
|
||||
}
|
||||
if(empty >= 0){
|
||||
clientenc[empty].client = client;
|
||||
clientenc[empty].encoding = encoding;
|
||||
}
|
||||
p = calloc(1, sizeof *p);
|
||||
if(p == nil)
|
||||
return -1;
|
||||
p->client = client;
|
||||
p->encoding = encoding;
|
||||
p->next = clientenc;
|
||||
clientenc = p;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
clearencoding(xcb_im_client_t *client)
|
||||
{
|
||||
int i;
|
||||
Clientenc *p, **link;
|
||||
|
||||
for(i = 0; i < nelem(clientenc); i++)
|
||||
if(clientenc[i].client == client){
|
||||
memset(&clientenc[i], 0, sizeof clientenc[i]);
|
||||
for(link = &clientenc; (p = *link) != nil; link = &p->next)
|
||||
if(p->client == client){
|
||||
*link = p->next;
|
||||
free(p);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
clearencodings(void)
|
||||
{
|
||||
Clientenc *next, *p;
|
||||
|
||||
for(p = clientenc; p != nil; p = next){
|
||||
next = p->next;
|
||||
free(p);
|
||||
}
|
||||
clientenc = nil;
|
||||
}
|
||||
|
||||
static void
|
||||
ximlog(char *msg)
|
||||
{
|
||||
@@ -199,46 +213,46 @@ ximlog(char *msg)
|
||||
static int
|
||||
kinit(void)
|
||||
{
|
||||
xcb_get_keyboard_mapping_cookie_t c;
|
||||
xcb_get_keyboard_mapping_reply_t *r;
|
||||
const xcb_setup_t *setup;
|
||||
xcb_keysym_t *syms;
|
||||
uint8_t newmin, newmax, newper;
|
||||
int n;
|
||||
struct xkb_context *context;
|
||||
struct xkb_keymap *keymap;
|
||||
struct xkb_state *state;
|
||||
int32_t device;
|
||||
int ok;
|
||||
|
||||
setup = xcb_get_setup(conn);
|
||||
if(setup == nil)
|
||||
return -1;
|
||||
newmin = setup->min_keycode;
|
||||
newmax = setup->max_keycode;
|
||||
c = xcb_get_keyboard_mapping(conn, newmin, newmax - newmin + 1);
|
||||
r = xcb_get_keyboard_mapping_reply(conn, c, nil);
|
||||
if(r == nil)
|
||||
return -1;
|
||||
newper = r->keysyms_per_keycode;
|
||||
n = xcb_get_keyboard_mapping_keysyms_length(r);
|
||||
syms = malloc(n * sizeof(xcb_keysym_t));
|
||||
if(syms == nil){
|
||||
free(r);
|
||||
return -1;
|
||||
}
|
||||
memcpy(syms, xcb_get_keyboard_mapping_keysyms(r),
|
||||
n * sizeof(xcb_keysym_t));
|
||||
free(kmap);
|
||||
kmap = syms;
|
||||
minkc = newmin;
|
||||
maxkc = newmax;
|
||||
symsper = newper;
|
||||
free(r);
|
||||
return 0;
|
||||
context = nil;
|
||||
keymap = nil;
|
||||
state = nil;
|
||||
ok = -1;
|
||||
context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
||||
if(context == nil || !xkb_x11_setup_xkb_extension(conn,
|
||||
XKB_X11_MIN_MAJOR_XKB_VERSION, XKB_X11_MIN_MINOR_XKB_VERSION,
|
||||
XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS, nil, nil, nil, nil))
|
||||
goto out;
|
||||
device = xkb_x11_get_core_keyboard_device_id(conn);
|
||||
if(device < 0)
|
||||
goto out;
|
||||
keymap = xkb_x11_keymap_new_from_device(context, conn, device,
|
||||
XKB_KEYMAP_COMPILE_NO_FLAGS);
|
||||
if(keymap == nil)
|
||||
goto out;
|
||||
state = xkb_state_new(keymap);
|
||||
if(state == nil)
|
||||
goto out;
|
||||
xkb_state_unref(kstate);
|
||||
kstate = state;
|
||||
state = nil;
|
||||
ok = 0;
|
||||
out:
|
||||
xkb_state_unref(state);
|
||||
xkb_keymap_unref(keymap);
|
||||
xkb_context_unref(context);
|
||||
return ok;
|
||||
}
|
||||
|
||||
static u32int
|
||||
kget(uint8_t kc, u16int state)
|
||||
{
|
||||
if(kmap == nil || kc < minkc || kc > maxkc)
|
||||
return 0;
|
||||
return keymaplookup(kmap + (kc - minkc) * symsper, symsper, state);
|
||||
return keymaplookup(kstate, kc, state);
|
||||
}
|
||||
|
||||
static xcb_screen_t*
|
||||
@@ -257,13 +271,13 @@ getscreen(int scr)
|
||||
return nil;
|
||||
}
|
||||
|
||||
static int
|
||||
static void
|
||||
readattrs(Ic *state)
|
||||
{
|
||||
const xcb_im_preedit_attr_t *attr;
|
||||
xcb_window_t clientwin, focuswin;
|
||||
xcb_point_t spot;
|
||||
int changed, hasspot;
|
||||
int hasspot;
|
||||
|
||||
clientwin = xcb_im_input_context_get_client_window(state->xic);
|
||||
focuswin = xcb_im_input_context_get_focus_window(state->xic);
|
||||
@@ -278,14 +292,10 @@ readattrs(Ic *state)
|
||||
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
|
||||
@@ -507,6 +517,41 @@ release(Ic *state)
|
||||
state->engaged = 0;
|
||||
}
|
||||
|
||||
static void
|
||||
resetic(Ic *state, xcb_im_reset_ic_reply_fr_t *reply)
|
||||
{
|
||||
Keyres pending, res;
|
||||
char utf[Maxutf], *wire;
|
||||
size_t nwire;
|
||||
int nbyte;
|
||||
|
||||
if(state == nil)
|
||||
return;
|
||||
sclear(&pending.preedit);
|
||||
if(!state->engaged){
|
||||
clearpreedit(state);
|
||||
return;
|
||||
}
|
||||
sendrequest(state, Keycap, 0, 0, &pending);
|
||||
sendrequest(state, Keyreset, 0, 0, &res);
|
||||
clearpreedit(state);
|
||||
if(reply == nil || !pending.eaten || pending.preedit.n == 0)
|
||||
return;
|
||||
nbyte = stoutf(&pending.preedit, utf, sizeof utf);
|
||||
if(state->encoding == Ecompound)
|
||||
wire = ximcompound(utf, nbyte, &nwire);
|
||||
else{
|
||||
nwire = nbyte;
|
||||
wire = malloc(nwire);
|
||||
if(wire != nil)
|
||||
memmove(wire, utf, nwire);
|
||||
}
|
||||
if(wire == nil)
|
||||
return;
|
||||
reply->committed_string = (uchar*)wire;
|
||||
reply->byte_length_of_committed_string = nwire;
|
||||
}
|
||||
|
||||
static void
|
||||
kpress(Ic *state, xcb_key_press_event_t *ev)
|
||||
{
|
||||
@@ -574,15 +619,13 @@ setstyle(Ic *state, u32int style)
|
||||
}
|
||||
|
||||
static void
|
||||
iccreate(xcb_im_client_t *client, xcb_im_input_context_t *ic)
|
||||
icinstall(xcb_im_client_t *client, xcb_im_input_context_t *ic, Ic *state)
|
||||
{
|
||||
Ic *state;
|
||||
|
||||
if(ic == nil || xcb_im_input_context_get_data(ic) != nil)
|
||||
return;
|
||||
state = calloc(1, sizeof *state);
|
||||
if(state == nil)
|
||||
if(state == nil){
|
||||
ximlog("out of memory; passing input context through");
|
||||
xcb_im_input_context_set_data(ic, &passthrough, nil);
|
||||
return;
|
||||
}
|
||||
state->xic = ic;
|
||||
state->client = client;
|
||||
state->encoding = getencoding(client);
|
||||
@@ -593,6 +636,14 @@ iccreate(xcb_im_client_t *client, xcb_im_input_context_t *ic)
|
||||
xcb_im_input_context_set_data(ic, state, icfree);
|
||||
}
|
||||
|
||||
static void
|
||||
iccreate(xcb_im_client_t *client, xcb_im_input_context_t *ic)
|
||||
{
|
||||
if(ic == nil || xcb_im_input_context_get_data(ic) != nil)
|
||||
return;
|
||||
icinstall(client, ic, calloc(1, sizeof(Ic)));
|
||||
}
|
||||
|
||||
static void
|
||||
callback(xcb_im_t *im, xcb_im_client_t *client, xcb_im_input_context_t *ic,
|
||||
const xcb_im_packet_header_fr_t *hdr, void *frame, void *arg, void *user)
|
||||
@@ -604,9 +655,9 @@ callback(xcb_im_t *im, xcb_im_client_t *client, xcb_im_input_context_t *ic,
|
||||
USED(frame);
|
||||
USED(user);
|
||||
if(hdr->major_opcode == XCB_XIM_ENCODING_NEGOTIATION){
|
||||
if(arg != nil)
|
||||
setencoding(client,
|
||||
*(u16int*)arg == 1 ? Eutf8 : Ecompound);
|
||||
if(arg != nil && setencoding(client,
|
||||
*(u16int*)arg == 1 ? Eutf8 : Ecompound) < 0)
|
||||
die("xim: cannot remember client encoding");
|
||||
return;
|
||||
}
|
||||
if(hdr->major_opcode == XCB_XIM_DISCONNECT ||
|
||||
@@ -627,6 +678,14 @@ callback(xcb_im_t *im, xcb_im_client_t *client, xcb_im_input_context_t *ic,
|
||||
state = xcb_im_input_context_get_data(ic);
|
||||
if(state == nil)
|
||||
return;
|
||||
if(state == &passthrough){
|
||||
if(hdr->major_opcode == XCB_XIM_FORWARD_EVENT){
|
||||
ev = arg;
|
||||
if(ev != nil)
|
||||
xcb_im_forward_event(xim, ic, ev);
|
||||
}
|
||||
return;
|
||||
}
|
||||
switch(hdr->major_opcode){
|
||||
case XCB_XIM_FORWARD_EVENT:
|
||||
ev = arg;
|
||||
@@ -643,10 +702,12 @@ callback(xcb_im_t *im, xcb_im_client_t *client, xcb_im_input_context_t *ic,
|
||||
}
|
||||
break;
|
||||
case XCB_XIM_DESTROY_IC:
|
||||
case XCB_XIM_RESET_IC:
|
||||
case XCB_XIM_UNSET_IC_FOCUS:
|
||||
release(state);
|
||||
break;
|
||||
case XCB_XIM_RESET_IC:
|
||||
resetic(state, arg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -665,15 +726,15 @@ ximclose(void)
|
||||
}
|
||||
opened = 0;
|
||||
preowner = nil;
|
||||
memset(clientenc, 0, sizeof clientenc);
|
||||
clearencodings();
|
||||
for(state = ics; state != nil; state = next){
|
||||
next = state->next;
|
||||
free(state);
|
||||
}
|
||||
ics = nil;
|
||||
rootwin = XCB_NONE;
|
||||
free(kmap);
|
||||
kmap = nil;
|
||||
xkb_state_unref(kstate);
|
||||
kstate = nil;
|
||||
if(conn != nil){
|
||||
xcb_disconnect(conn);
|
||||
conn = nil;
|
||||
@@ -753,7 +814,7 @@ ximthread(void *arg)
|
||||
threadsetname("xim");
|
||||
if(ximinit() < 0){
|
||||
ximclose();
|
||||
return;
|
||||
die("xim: initialization failed");
|
||||
}
|
||||
fd = xcb_get_file_descriptor(conn);
|
||||
pfd[0].fd = fd;
|
||||
@@ -785,7 +846,9 @@ ximthread(void *arg)
|
||||
if(pfd[0].revents & (POLLERR|POLLHUP|POLLNVAL))
|
||||
break;
|
||||
}
|
||||
if(xcb_connection_has_error(conn))
|
||||
ximlog("X server disconnected");
|
||||
n = xcb_connection_has_error(conn);
|
||||
ximclose();
|
||||
if(n)
|
||||
die("xim: X server disconnected");
|
||||
die("xim: frontend stopped");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user