Improvements to the IBus related code:
authorAlex Baines <alex@abaines.me.uk>
Tue, 19 Aug 2014 23:17:28 +0100
changeset 90966454f71d6f15
parent 9095 ed277c1c9e7f
child 9097 56d712662a82
Improvements to the IBus related code:
+ 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.
src/core/linux/SDL_ibus.c
src/video/x11/SDL_x11events.c
src/video/x11/SDL_x11keyboard.c
     1.1 --- a/src/core/linux/SDL_ibus.c	Tue Aug 19 22:28:53 2014 +0100
     1.2 +++ b/src/core/linux/SDL_ibus.c	Tue Aug 19 23:17:28 2014 +0100
     1.3 @@ -45,7 +45,7 @@
     1.4  static SDL_Rect ibus_cursor_rect = {0};
     1.5  static DBusConnection *ibus_conn = NULL;
     1.6  static char *ibus_addr_file = NULL;
     1.7 -int inotify_fd = -1;
     1.8 +int inotify_fd = -1, inotify_wd = -1;
     1.9  
    1.10  static Uint32
    1.11  IBus_ModState(void)
    1.12 @@ -166,8 +166,6 @@
    1.13                  i += sz;
    1.14                  cursor += chars;
    1.15              }
    1.16 -        } else {
    1.17 -            SDL_SendEditingText("", 0, 0);
    1.18          }
    1.19          
    1.20          SDL_IBus_UpdateTextRect(NULL);
    1.21 @@ -175,6 +173,11 @@
    1.22          return DBUS_HANDLER_RESULT_HANDLED;
    1.23      }
    1.24      
    1.25 +    if(dbus->message_is_signal(msg, IBUS_INPUT_INTERFACE, "HidePreeditText")){
    1.26 +    	SDL_SendEditingText("", 0, 0);
    1.27 +    	return DBUS_HANDLER_RESULT_HANDLED;
    1.28 +    }
    1.29 +    
    1.30      return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    1.31  }
    1.32  
    1.33 @@ -360,7 +363,7 @@
    1.34          dbus->connection_flush(ibus_conn);
    1.35      }
    1.36  
    1.37 -    SDL_IBus_SetFocus(SDL_GetFocusWindow() != NULL);
    1.38 +    SDL_IBus_SetFocus(SDL_GetKeyboardFocus() != NULL);
    1.39      SDL_IBus_UpdateTextRect(NULL);
    1.40      
    1.41      return result;
    1.42 @@ -375,7 +378,7 @@
    1.43          return SDL_TRUE;
    1.44      }
    1.45      
    1.46 -    if(inotify_fd != -1){
    1.47 +    if(inotify_fd > 0 && inotify_wd > 0){
    1.48          char buf[1024];
    1.49          ssize_t readsize = read(inotify_fd, buf, sizeof(buf));
    1.50          if(readsize > 0){
    1.51 @@ -428,15 +431,17 @@
    1.52          
    1.53          char *addr = IBus_ReadAddressFromFile(addr_file);
    1.54          
    1.55 -        inotify_fd = inotify_init();
    1.56 -        fcntl(inotify_fd, F_SETFL, O_NONBLOCK);
    1.57 +        if(inotify_fd < 0){
    1.58 +            inotify_fd = inotify_init();
    1.59 +            fcntl(inotify_fd, F_SETFL, O_NONBLOCK);
    1.60 +        }
    1.61          
    1.62          char *addr_file_dir = SDL_strrchr(addr_file, '/');
    1.63          if(addr_file_dir){
    1.64              *addr_file_dir = 0;
    1.65          }
    1.66          
    1.67 -        inotify_add_watch(inotify_fd, addr_file, IN_CREATE | IN_MODIFY);
    1.68 +        inotify_wd = inotify_add_watch(inotify_fd, addr_file, IN_CREATE | IN_MODIFY);
    1.69          SDL_free(addr_file);
    1.70          
    1.71          result = IBus_SetupConnection(dbus, addr);
    1.72 @@ -466,6 +471,11 @@
    1.73          dbus->connection_unref(ibus_conn);
    1.74      }
    1.75      
    1.76 +    if(inotify_fd > 0 && inotify_wd > 0){
    1.77 +        inotify_rm_watch(inotify_fd, inotify_wd);
    1.78 +        inotify_wd = -1;
    1.79 +    }
    1.80 +    
    1.81      SDL_memset(&ibus_cursor_rect, 0, sizeof(ibus_cursor_rect));
    1.82  }
    1.83  
    1.84 @@ -548,7 +558,7 @@
    1.85          SDL_memcpy(&ibus_cursor_rect, rect, sizeof(ibus_cursor_rect));
    1.86      }
    1.87      
    1.88 -    SDL_Window *focused_win = SDL_GetFocusWindow();
    1.89 +    SDL_Window *focused_win = SDL_GetKeyboardFocus();
    1.90  
    1.91      if(!focused_win) return;
    1.92      
     2.1 --- a/src/video/x11/SDL_x11events.c	Tue Aug 19 22:28:53 2014 +0100
     2.2 +++ b/src/video/x11/SDL_x11events.c	Tue Aug 19 23:17:28 2014 +0100
     2.3 @@ -347,6 +347,9 @@
     2.4          X11_XSetICFocus(data->ic);
     2.5      }
     2.6  #endif
     2.7 +#ifdef SDL_USE_IBUS
     2.8 +    SDL_IBus_SetFocus(SDL_TRUE);
     2.9 +#endif
    2.10  }
    2.11  
    2.12  static void
    2.13 @@ -367,6 +370,9 @@
    2.14          X11_XUnsetICFocus(data->ic);
    2.15      }
    2.16  #endif
    2.17 +#ifdef SDL_USE_IBUS
    2.18 +    SDL_IBus_SetFocus(SDL_FALSE);
    2.19 +#endif
    2.20  }
    2.21  
    2.22  static void
    2.23 @@ -647,11 +653,6 @@
    2.24  #ifdef DEBUG_XEVENTS
    2.25              printf("window %p: FocusIn!\n", data);
    2.26  #endif
    2.27 -#ifdef SDL_USE_IBUS
    2.28 -            if(SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE){
    2.29 -                SDL_IBus_SetFocus(SDL_TRUE);
    2.30 -            }
    2.31 -#endif
    2.32              if (data->pending_focus == PENDING_FOCUS_OUT &&
    2.33                  data->window == SDL_GetKeyboardFocus()) {
    2.34                  /* We want to reset the keyboard here, because we may have
    2.35 @@ -689,11 +690,6 @@
    2.36  #ifdef DEBUG_XEVENTS
    2.37              printf("window %p: FocusOut!\n", data);
    2.38  #endif
    2.39 -#ifdef SDL_USE_IBUS
    2.40 -            if(SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE){
    2.41 -                SDL_IBus_SetFocus(SDL_FALSE);
    2.42 -            }
    2.43 -#endif
    2.44              data->pending_focus = PENDING_FOCUS_OUT;
    2.45              data->pending_focus_time = SDL_GetTicks() + PENDING_FOCUS_OUT_TIME;
    2.46          }
    2.47 @@ -725,16 +721,11 @@
    2.48              KeySym keysym = NoSymbol;
    2.49              char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
    2.50              Status status = 0;
    2.51 -#ifdef SDL_USE_IBUS
    2.52 -            Bool handled = False;
    2.53 -#endif
    2.54 +            SDL_bool handled_by_ime = SDL_FALSE;
    2.55  
    2.56  #ifdef DEBUG_XEVENTS
    2.57              printf("window %p: KeyPress (X11 keycode = 0x%X)\n", data, xevent.xkey.keycode);
    2.58  #endif
    2.59 -#ifndef SDL_USE_IBUS
    2.60 -            SDL_SendKeyboardKey(SDL_PRESSED, videodata->key_layout[keycode]);
    2.61 -#endif
    2.62  #if 1
    2.63              if (videodata->key_layout[keycode] == SDL_SCANCODE_UNKNOWN && keycode) {
    2.64                  int min_keycode, max_keycode;
    2.65 @@ -762,7 +753,7 @@
    2.66  #endif
    2.67  #ifdef SDL_USE_IBUS
    2.68              if(SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE){
    2.69 -                if(!(handled = SDL_IBus_ProcessKeyEvent(keysym, keycode))){
    2.70 +                if(!(handled_by_ime = SDL_IBus_ProcessKeyEvent(keysym, keycode))){
    2.71  #endif
    2.72                      if(*text){
    2.73                          SDL_SendKeyboardText(text);
    2.74 @@ -770,11 +761,11 @@
    2.75  #ifdef SDL_USE_IBUS
    2.76                  }
    2.77              }
    2.78 -
    2.79 -            if (!handled) {
    2.80 +#endif
    2.81 +            if (!handled_by_ime) {
    2.82                  SDL_SendKeyboardKey(SDL_PRESSED, videodata->key_layout[keycode]);
    2.83              }
    2.84 -#endif
    2.85 +
    2.86          }
    2.87          break;
    2.88  
     3.1 --- a/src/video/x11/SDL_x11keyboard.c	Tue Aug 19 22:28:53 2014 +0100
     3.2 +++ b/src/video/x11/SDL_x11keyboard.c	Tue Aug 19 23:17:28 2014 +0100
     3.3 @@ -332,9 +332,7 @@
     3.4  void
     3.5  X11_StartTextInput(_THIS)
     3.6  {
     3.7 -#ifdef SDL_USE_IBUS
     3.8 -    SDL_IBus_SetFocus(SDL_GetFocusWindow() != NULL);
     3.9 -#endif
    3.10 +
    3.11  }
    3.12  
    3.13  void