Skip to content

Commit

Permalink
Pass the high 16bit sequence number in forwarding event correctly
Browse files Browse the repository at this point in the history
The sequence number in the frame should be the high 16bit of full sequence.
  • Loading branch information
wengxt committed Apr 13, 2024
1 parent 065ef32 commit 4adb9ef
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#define ARRAY_SIZE(X) (sizeof(X) / sizeof(X[0]))

#define XIM_MESSAGE_BYTES(hdr) ((hdr)->length * 4)
#define XIM_MESSAGE_BYTES(hdr) ((hdr)->length * 4u)

/*
* categories in XIM_SERVERS
Expand Down
9 changes: 7 additions & 2 deletions src/imclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ bool _xcb_xim_filter_property_changed(xcb_xim_t *im,

bool xcb_xim_filter_event(xcb_xim_t *im, xcb_generic_event_t *event) {
im->yield_recheck = false;
im->event_sequence = event->full_sequence;
bool result = _xcb_xim_preconnect_im(im, event) ||
_xcb_xim_filter_event(im, event) ||
_xcb_xim_filter_destroy_window(im, event) ||
Expand Down Expand Up @@ -1485,7 +1486,11 @@ bool xcb_xim_unset_ic_focus(xcb_xim_t *im, xcb_xic_t ic) {
return !fail;
}

bool xcb_xim_forward_event(xcb_xim_t *im, xcb_xic_t ic,
bool xcb_xim_forward_event(xcb_xim_t *im, xcb_xic_t ic, xcb_key_press_event_t *event) {
return xcb_xim_forward_event_full(im, ic, (im->event_sequence >> 16), event);
}

bool xcb_xim_forward_event_full(xcb_xim_t *im, xcb_xic_t ic, uint16_t sequence,
xcb_key_press_event_t *event) {
xcb_xim_request_queue_t *queue =
_xcb_xim_new_request(im, XCB_XIM_FORWARD_EVENT, 0, NULL, NULL);
Expand All @@ -1496,7 +1501,7 @@ bool xcb_xim_forward_event(xcb_xim_t *im, xcb_xic_t ic,
frame.input_method_ID = im->connect_id;
frame.input_context_ID = ic;
frame.flag = XCB_XIM_SYNCHRONOUS;
frame.sequence_number = event->sequence;
frame.sequence_number = sequence;

const size_t length =
xcb_im_forward_event_fr_size(&frame) + sizeof(xcb_key_press_event_t);
Expand Down
3 changes: 3 additions & 0 deletions src/imclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ XCBIMDKIT_EXPORT bool xcb_xim_set_ic_focus(xcb_xim_t *im, xcb_xic_t ic);
XCBIMDKIT_EXPORT bool xcb_xim_unset_ic_focus(xcb_xim_t *im, xcb_xic_t ic);
XCBIMDKIT_EXPORT bool xcb_xim_forward_event(xcb_xim_t *im, xcb_xic_t ic,
xcb_key_press_event_t *event);
XCBIMDKIT_EXPORT bool xcb_xim_forward_event_full(xcb_xim_t *im, xcb_xic_t ic,
uint16_t sequence,
xcb_key_press_event_t *event);
XCBIMDKIT_EXPORT bool xcb_xim_reset_ic(xcb_xim_t *im, xcb_xic_t ic,
xcb_xim_reset_ic_callback callback,
void *user_data);
Expand Down
4 changes: 3 additions & 1 deletion src/imclient_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,15 @@ struct _xcb_xim_t {
bool use_compound_text;
bool use_utf8_string;
xcb_xim_encoding_t encoding;

uint32_t event_sequence;
};

#define _xcb_xim_read_frame(FRAME, DATA, LEN, FAIL) \
do { \
FAIL = false; \
uint8_t *_data = DATA; \
size_t len = (LEN); \
size_t len = (size_t)(LEN); \
frame_read_func(FRAME)(&FRAME, &_data, &len, false); \
if (!_data) { \
frame_free_func(FRAME)(&FRAME); \
Expand Down
8 changes: 7 additions & 1 deletion src/imdkit.c
Original file line number Diff line number Diff line change
Expand Up @@ -836,11 +836,17 @@ void xcb_im_destroy(xcb_im_t *im) {

void xcb_im_forward_event(xcb_im_t *im, xcb_im_input_context_t *ic,
xcb_key_press_event_t *event) {
xcb_im_forward_event_full(im, ic, ic->forward_event_sequence, event);
}

void xcb_im_forward_event_full(xcb_im_t *im, xcb_im_input_context_t *ic,
uint16_t sequence,
xcb_key_press_event_t *event) {
xcb_im_client_t *client = ic->client;
xcb_im_forward_event_fr_t frame;
frame.input_method_ID = ic->client->connect_id;
frame.input_context_ID = ic->id;
frame.sequence_number = event->sequence;
frame.sequence_number = sequence;
if (im->use_sync_mode) {
frame.flag = XCB_XIM_SYNCHRONOUS;
client->sync = true;
Expand Down
15 changes: 15 additions & 0 deletions src/imdkit.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,21 @@ XCBIMDKIT_EXPORT void xcb_im_forward_event(xcb_im_t *im,
xcb_im_input_context_t *ic,
xcb_key_press_event_t *event);

/**
* Send a key event to the client.
*
* @param im XIM server
* @param ic Input context.
* @param sequence sequence number, when re-inject, should be the value from
* forward_event
* @param event key event.
* @see xcb_im_forward_event_fr_t.
*/
XCBIMDKIT_EXPORT void xcb_im_forward_event_full(xcb_im_t *im,
xcb_im_input_context_t *ic,
uint16_t sequence,
xcb_key_press_event_t *event);

/**
* Commit a string to the client.
*
Expand Down
1 change: 1 addition & 0 deletions src/imdkit_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ struct _xcb_im_input_context_t {
UT_hash_handle hh;
void *data;
xcb_im_free_function free_data_function;
uint16_t forward_event_sequence;
};

struct _xcb_im_client_t {
Expand Down
1 change: 1 addition & 0 deletions src/protocolhandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ void _xcb_im_handle_forward_event(xcb_im_t *im, xcb_im_client_t *client,
if (!ic) {
break;
}
ic->forward_event_sequence = frame.sequence_number;
if (client->sync) {
_xcb_im_add_queue(im, client, ic->id, hdr, &frame, data);
} else {
Expand Down

0 comments on commit 4adb9ef

Please sign in to comment.