Skip to content

Commit

Permalink
Improvements to the IBus related code:
Browse files Browse the repository at this point in the history
+ Handle HidePreeditText IBus signal.
+ Use SDL_GetKeyboardFocus instead of SDL_GetFocusWindow.
+ Move the X11 IBus SetFocus calls to the X11_DispatchFocus functions.
+ Simplify the IBus ifdefs when handling X11 KeyEvents.
+ Remove inotify watch when SDL_IBus_Quit is called.
  • Loading branch information
baines committed Aug 19, 2014
1 parent 6f84f37 commit f4ddacf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
28 changes: 19 additions & 9 deletions src/core/linux/SDL_ibus.c
Expand Up @@ -45,7 +45,7 @@ static char *input_ctx_path = NULL;
static SDL_Rect ibus_cursor_rect = {0};
static DBusConnection *ibus_conn = NULL;
static char *ibus_addr_file = NULL;
int inotify_fd = -1;
int inotify_fd = -1, inotify_wd = -1;

static Uint32
IBus_ModState(void)
Expand Down Expand Up @@ -166,15 +166,18 @@ IBus_MessageFilter(DBusConnection *conn, DBusMessage *msg, void *user_data)
i += sz;
cursor += chars;
}
} else {
SDL_SendEditingText("", 0, 0);
}

SDL_IBus_UpdateTextRect(NULL);

return DBUS_HANDLER_RESULT_HANDLED;
}

if(dbus->message_is_signal(msg, IBUS_INPUT_INTERFACE, "HidePreeditText")){
SDL_SendEditingText("", 0, 0);
return DBUS_HANDLER_RESULT_HANDLED;
}

return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

Expand Down Expand Up @@ -360,7 +363,7 @@ IBus_SetupConnection(SDL_DBusContext *dbus, const char* addr)
dbus->connection_flush(ibus_conn);
}

SDL_IBus_SetFocus(SDL_GetFocusWindow() != NULL);
SDL_IBus_SetFocus(SDL_GetKeyboardFocus() != NULL);
SDL_IBus_UpdateTextRect(NULL);

return result;
Expand All @@ -375,7 +378,7 @@ IBus_CheckConnection(SDL_DBusContext *dbus)
return SDL_TRUE;
}

if(inotify_fd != -1){
if(inotify_fd > 0 && inotify_wd > 0){
char buf[1024];
ssize_t readsize = read(inotify_fd, buf, sizeof(buf));
if(readsize > 0){
Expand Down Expand Up @@ -428,15 +431,17 @@ SDL_IBus_Init(void)

char *addr = IBus_ReadAddressFromFile(addr_file);

inotify_fd = inotify_init();
fcntl(inotify_fd, F_SETFL, O_NONBLOCK);
if(inotify_fd < 0){
inotify_fd = inotify_init();
fcntl(inotify_fd, F_SETFL, O_NONBLOCK);
}

char *addr_file_dir = SDL_strrchr(addr_file, '/');
if(addr_file_dir){
*addr_file_dir = 0;
}

inotify_add_watch(inotify_fd, addr_file, IN_CREATE | IN_MODIFY);
inotify_wd = inotify_add_watch(inotify_fd, addr_file, IN_CREATE | IN_MODIFY);
SDL_free(addr_file);

result = IBus_SetupConnection(dbus, addr);
Expand Down Expand Up @@ -466,6 +471,11 @@ SDL_IBus_Quit(void)
dbus->connection_unref(ibus_conn);
}

if(inotify_fd > 0 && inotify_wd > 0){
inotify_rm_watch(inotify_fd, inotify_wd);
inotify_wd = -1;
}

SDL_memset(&ibus_cursor_rect, 0, sizeof(ibus_cursor_rect));
}

Expand Down Expand Up @@ -548,7 +558,7 @@ SDL_IBus_UpdateTextRect(SDL_Rect *rect)
SDL_memcpy(&ibus_cursor_rect, rect, sizeof(ibus_cursor_rect));
}

SDL_Window *focused_win = SDL_GetFocusWindow();
SDL_Window *focused_win = SDL_GetKeyboardFocus();

if(!focused_win) return;

Expand Down
31 changes: 11 additions & 20 deletions src/video/x11/SDL_x11events.c
Expand Up @@ -347,6 +347,9 @@ X11_DispatchFocusIn(SDL_WindowData *data)
X11_XSetICFocus(data->ic);
}
#endif
#ifdef SDL_USE_IBUS
SDL_IBus_SetFocus(SDL_TRUE);
#endif
}

static void
Expand All @@ -367,6 +370,9 @@ X11_DispatchFocusOut(SDL_WindowData *data)
X11_XUnsetICFocus(data->ic);
}
#endif
#ifdef SDL_USE_IBUS
SDL_IBus_SetFocus(SDL_FALSE);
#endif
}

static void
Expand Down Expand Up @@ -646,11 +652,6 @@ X11_DispatchEvent(_THIS)
}
#ifdef DEBUG_XEVENTS
printf("window %p: FocusIn!\n", data);
#endif
#ifdef SDL_USE_IBUS
if(SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE){
SDL_IBus_SetFocus(SDL_TRUE);
}
#endif
if (data->pending_focus == PENDING_FOCUS_OUT &&
data->window == SDL_GetKeyboardFocus()) {
Expand Down Expand Up @@ -688,11 +689,6 @@ X11_DispatchEvent(_THIS)
}
#ifdef DEBUG_XEVENTS
printf("window %p: FocusOut!\n", data);
#endif
#ifdef SDL_USE_IBUS
if(SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE){
SDL_IBus_SetFocus(SDL_FALSE);
}
#endif
data->pending_focus = PENDING_FOCUS_OUT;
data->pending_focus_time = SDL_GetTicks() + PENDING_FOCUS_OUT_TIME;
Expand Down Expand Up @@ -725,16 +721,11 @@ X11_DispatchEvent(_THIS)
KeySym keysym = NoSymbol;
char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
Status status = 0;
#ifdef SDL_USE_IBUS
Bool handled = False;
#endif
SDL_bool handled_by_ime = SDL_FALSE;

#ifdef DEBUG_XEVENTS
printf("window %p: KeyPress (X11 keycode = 0x%X)\n", data, xevent.xkey.keycode);
#endif
#ifndef SDL_USE_IBUS
SDL_SendKeyboardKey(SDL_PRESSED, videodata->key_layout[keycode]);
#endif
#if 1
if (videodata->key_layout[keycode] == SDL_SCANCODE_UNKNOWN && keycode) {
int min_keycode, max_keycode;
Expand Down Expand Up @@ -762,19 +753,19 @@ X11_DispatchEvent(_THIS)
#endif
#ifdef SDL_USE_IBUS
if(SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE){
if(!(handled = SDL_IBus_ProcessKeyEvent(keysym, keycode))){
if(!(handled_by_ime = SDL_IBus_ProcessKeyEvent(keysym, keycode))){
#endif
if(*text){
SDL_SendKeyboardText(text);
}
#ifdef SDL_USE_IBUS
}
}

if (!handled) {
#endif
if (!handled_by_ime) {
SDL_SendKeyboardKey(SDL_PRESSED, videodata->key_layout[keycode]);
}
#endif

}
break;

Expand Down
4 changes: 1 addition & 3 deletions src/video/x11/SDL_x11keyboard.c
Expand Up @@ -332,9 +332,7 @@ X11_QuitKeyboard(_THIS)
void
X11_StartTextInput(_THIS)
{
#ifdef SDL_USE_IBUS
SDL_IBus_SetFocus(SDL_GetFocusWindow() != NULL);
#endif

}

void
Expand Down

0 comments on commit f4ddacf

Please sign in to comment.