SDL_ime.c 4.5 KB

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