src/core/linux/SDL_ime.c
changeset 10496 6660aa9120d6
child 10500 c3874aa1f2d1
equal deleted inserted replaced
10495:f732781d7ddc 10496:6660aa9120d6
       
     1 /*
       
     2   Simple DirectMedia Layer
       
     3   Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
       
     4 
       
     5   This software is provided 'as-is', without any express or implied
       
     6   warranty.  In no event will the authors be held liable for any damages
       
     7   arising from the use of this software.
       
     8 
       
     9   Permission is granted to anyone to use this software for any purpose,
       
    10   including commercial applications, and to alter it and redistribute it
       
    11   freely, subject to the following restrictions:
       
    12 
       
    13   1. The origin of this software must not be misrepresented; you must not
       
    14      claim that you wrote the original software. If you use this software
       
    15      in a product, an acknowledgment in the product documentation would be
       
    16      appreciated but is not required.
       
    17   2. Altered source versions must be plainly marked as such, and must not be
       
    18      misrepresented as being the original software.
       
    19   3. This notice may not be removed or altered from any source distribution.
       
    20 */
       
    21 
       
    22 #include "SDL_ime.h"
       
    23 #include "SDL_ibus.h"
       
    24 #include "SDL_fcitx.h"
       
    25 
       
    26 typedef SDL_bool (*_SDL_IME_Init)();
       
    27 typedef void (*_SDL_IME_Quit)();
       
    28 typedef void (*_SDL_IME_SetFocus)(SDL_bool);
       
    29 typedef void (*_SDL_IME_Reset)();
       
    30 typedef SDL_bool (*_SDL_IME_ProcessKeyEvent)(Uint32, Uint32);
       
    31 typedef void (*_SDL_IME_UpdateTextRect)(SDL_Rect *);
       
    32 typedef void (*_SDL_IME_PumpEvents)();
       
    33 
       
    34 static _SDL_IME_Init SDL_IME_Init_Real = NULL;
       
    35 static _SDL_IME_Quit SDL_IME_Quit_Real = NULL;
       
    36 static _SDL_IME_SetFocus SDL_IME_SetFocus_Real = NULL;
       
    37 static _SDL_IME_Reset SDL_IME_Reset_Real = NULL;
       
    38 static _SDL_IME_ProcessKeyEvent SDL_IME_ProcessKeyEvent_Real = NULL;
       
    39 static _SDL_IME_UpdateTextRect SDL_IME_UpdateTextRect_Real = NULL;
       
    40 static _SDL_IME_PumpEvents SDL_IME_PumpEvents_Real = NULL;
       
    41 
       
    42 static void
       
    43 InitIME()
       
    44 {
       
    45     static SDL_bool inited = SDL_FALSE;
       
    46     const char *im_module = NULL;
       
    47 
       
    48     if (inited == SDL_TRUE)
       
    49         return ;
       
    50 
       
    51     inited = SDL_TRUE;
       
    52     // TODO:
       
    53     // better move every ime implenment to a shared library
       
    54 
       
    55     // default to IBus
       
    56 #ifdef HAVE_IBUS_IBUS_H
       
    57     SDL_IME_Init_Real = SDL_IBus_Init;
       
    58     SDL_IME_Quit_Real = SDL_IBus_Quit;
       
    59     SDL_IME_SetFocus_Real = SDL_IBus_SetFocus;
       
    60     SDL_IME_Reset_Real = SDL_IBus_Reset;
       
    61     SDL_IME_ProcessKeyEvent_Real = SDL_IBus_ProcessKeyEvent;
       
    62     SDL_IME_UpdateTextRect_Real = SDL_IBus_UpdateTextRect;
       
    63     SDL_IME_PumpEvents_Real = SDL_IBus_PumpEvents;
       
    64 #endif
       
    65 
       
    66     im_module = SDL_getenv("SDL_IM_MODULE");
       
    67     if (im_module) {
       
    68         if (SDL_strcmp(im_module, "fcitx") == 0) {
       
    69 #ifdef HAVE_FCITX_FRONTEND_H
       
    70             SDL_IME_Init_Real = SDL_Fcitx_Init;
       
    71             SDL_IME_Quit_Real = SDL_Fcitx_Quit;
       
    72             SDL_IME_SetFocus_Real = SDL_Fcitx_SetFocus;
       
    73             SDL_IME_Reset_Real = SDL_Fcitx_Reset;
       
    74             SDL_IME_ProcessKeyEvent_Real = SDL_Fcitx_ProcessKeyEvent;
       
    75             SDL_IME_UpdateTextRect_Real = SDL_Fcitx_UpdateTextRect;
       
    76             SDL_IME_PumpEvents_Real = SDL_Fcitx_PumpEvents;
       
    77 #endif
       
    78         }
       
    79     }
       
    80 }
       
    81 
       
    82 SDL_bool
       
    83 SDL_IME_Init(void)
       
    84 {
       
    85     InitIME();
       
    86 
       
    87     if (SDL_IME_Init_Real)
       
    88         return SDL_IME_Init_Real();
       
    89 
       
    90     return SDL_FALSE;
       
    91 }
       
    92 
       
    93 void
       
    94 SDL_IME_Quit(void)
       
    95 {
       
    96     if (SDL_IME_Quit_Real)
       
    97         SDL_IME_Quit_Real();
       
    98 }
       
    99 
       
   100 void
       
   101 SDL_IME_SetFocus(SDL_bool focused)
       
   102 {
       
   103     if (SDL_IME_SetFocus_Real)
       
   104         SDL_IME_SetFocus_Real(focused);
       
   105 }
       
   106 
       
   107 void
       
   108 SDL_IME_Reset(void)
       
   109 {
       
   110     if (SDL_IME_Reset_Real)
       
   111         SDL_IME_Reset_Real();
       
   112 }
       
   113 
       
   114 SDL_bool
       
   115 SDL_IME_ProcessKeyEvent(Uint32 keysym, Uint32 keycode)
       
   116 {
       
   117     if (SDL_IME_ProcessKeyEvent_Real)
       
   118         return SDL_IME_ProcessKeyEvent_Real(keysym, keycode);
       
   119 
       
   120     return SDL_FALSE;
       
   121 }
       
   122 
       
   123 void
       
   124 SDL_IME_UpdateTextRect(SDL_Rect *rect)
       
   125 {
       
   126     if (SDL_IME_UpdateTextRect_Real)
       
   127         SDL_IME_UpdateTextRect_Real(rect);
       
   128 }
       
   129 
       
   130 void
       
   131 SDL_IME_PumpEvents()
       
   132 {
       
   133     if (SDL_IME_PumpEvents_Real)
       
   134         SDL_IME_PumpEvents_Real();
       
   135 }