src/core/linux/SDL_ime.c
changeset 10496 6660aa9120d6
child 10500 c3874aa1f2d1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/core/linux/SDL_ime.c	Fri Oct 07 18:57:40 2016 -0700
     1.3 @@ -0,0 +1,135 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +
    1.25 +#include "SDL_ime.h"
    1.26 +#include "SDL_ibus.h"
    1.27 +#include "SDL_fcitx.h"
    1.28 +
    1.29 +typedef SDL_bool (*_SDL_IME_Init)();
    1.30 +typedef void (*_SDL_IME_Quit)();
    1.31 +typedef void (*_SDL_IME_SetFocus)(SDL_bool);
    1.32 +typedef void (*_SDL_IME_Reset)();
    1.33 +typedef SDL_bool (*_SDL_IME_ProcessKeyEvent)(Uint32, Uint32);
    1.34 +typedef void (*_SDL_IME_UpdateTextRect)(SDL_Rect *);
    1.35 +typedef void (*_SDL_IME_PumpEvents)();
    1.36 +
    1.37 +static _SDL_IME_Init SDL_IME_Init_Real = NULL;
    1.38 +static _SDL_IME_Quit SDL_IME_Quit_Real = NULL;
    1.39 +static _SDL_IME_SetFocus SDL_IME_SetFocus_Real = NULL;
    1.40 +static _SDL_IME_Reset SDL_IME_Reset_Real = NULL;
    1.41 +static _SDL_IME_ProcessKeyEvent SDL_IME_ProcessKeyEvent_Real = NULL;
    1.42 +static _SDL_IME_UpdateTextRect SDL_IME_UpdateTextRect_Real = NULL;
    1.43 +static _SDL_IME_PumpEvents SDL_IME_PumpEvents_Real = NULL;
    1.44 +
    1.45 +static void
    1.46 +InitIME()
    1.47 +{
    1.48 +    static SDL_bool inited = SDL_FALSE;
    1.49 +    const char *im_module = NULL;
    1.50 +
    1.51 +    if (inited == SDL_TRUE)
    1.52 +        return ;
    1.53 +
    1.54 +    inited = SDL_TRUE;
    1.55 +    // TODO:
    1.56 +    // better move every ime implenment to a shared library
    1.57 +
    1.58 +    // default to IBus
    1.59 +#ifdef HAVE_IBUS_IBUS_H
    1.60 +    SDL_IME_Init_Real = SDL_IBus_Init;
    1.61 +    SDL_IME_Quit_Real = SDL_IBus_Quit;
    1.62 +    SDL_IME_SetFocus_Real = SDL_IBus_SetFocus;
    1.63 +    SDL_IME_Reset_Real = SDL_IBus_Reset;
    1.64 +    SDL_IME_ProcessKeyEvent_Real = SDL_IBus_ProcessKeyEvent;
    1.65 +    SDL_IME_UpdateTextRect_Real = SDL_IBus_UpdateTextRect;
    1.66 +    SDL_IME_PumpEvents_Real = SDL_IBus_PumpEvents;
    1.67 +#endif
    1.68 +
    1.69 +    im_module = SDL_getenv("SDL_IM_MODULE");
    1.70 +    if (im_module) {
    1.71 +        if (SDL_strcmp(im_module, "fcitx") == 0) {
    1.72 +#ifdef HAVE_FCITX_FRONTEND_H
    1.73 +            SDL_IME_Init_Real = SDL_Fcitx_Init;
    1.74 +            SDL_IME_Quit_Real = SDL_Fcitx_Quit;
    1.75 +            SDL_IME_SetFocus_Real = SDL_Fcitx_SetFocus;
    1.76 +            SDL_IME_Reset_Real = SDL_Fcitx_Reset;
    1.77 +            SDL_IME_ProcessKeyEvent_Real = SDL_Fcitx_ProcessKeyEvent;
    1.78 +            SDL_IME_UpdateTextRect_Real = SDL_Fcitx_UpdateTextRect;
    1.79 +            SDL_IME_PumpEvents_Real = SDL_Fcitx_PumpEvents;
    1.80 +#endif
    1.81 +        }
    1.82 +    }
    1.83 +}
    1.84 +
    1.85 +SDL_bool
    1.86 +SDL_IME_Init(void)
    1.87 +{
    1.88 +    InitIME();
    1.89 +
    1.90 +    if (SDL_IME_Init_Real)
    1.91 +        return SDL_IME_Init_Real();
    1.92 +
    1.93 +    return SDL_FALSE;
    1.94 +}
    1.95 +
    1.96 +void
    1.97 +SDL_IME_Quit(void)
    1.98 +{
    1.99 +    if (SDL_IME_Quit_Real)
   1.100 +        SDL_IME_Quit_Real();
   1.101 +}
   1.102 +
   1.103 +void
   1.104 +SDL_IME_SetFocus(SDL_bool focused)
   1.105 +{
   1.106 +    if (SDL_IME_SetFocus_Real)
   1.107 +        SDL_IME_SetFocus_Real(focused);
   1.108 +}
   1.109 +
   1.110 +void
   1.111 +SDL_IME_Reset(void)
   1.112 +{
   1.113 +    if (SDL_IME_Reset_Real)
   1.114 +        SDL_IME_Reset_Real();
   1.115 +}
   1.116 +
   1.117 +SDL_bool
   1.118 +SDL_IME_ProcessKeyEvent(Uint32 keysym, Uint32 keycode)
   1.119 +{
   1.120 +    if (SDL_IME_ProcessKeyEvent_Real)
   1.121 +        return SDL_IME_ProcessKeyEvent_Real(keysym, keycode);
   1.122 +
   1.123 +    return SDL_FALSE;
   1.124 +}
   1.125 +
   1.126 +void
   1.127 +SDL_IME_UpdateTextRect(SDL_Rect *rect)
   1.128 +{
   1.129 +    if (SDL_IME_UpdateTextRect_Real)
   1.130 +        SDL_IME_UpdateTextRect_Real(rect);
   1.131 +}
   1.132 +
   1.133 +void
   1.134 +SDL_IME_PumpEvents()
   1.135 +{
   1.136 +    if (SDL_IME_PumpEvents_Real)
   1.137 +        SDL_IME_PumpEvents_Real();
   1.138 +}