SDL_ime.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  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 bool (*SDL_IME_Init_t)(void);
  23. typedef void (*SDL_IME_Quit_t)(void);
  24. typedef void (*SDL_IME_SetFocus_t)(bool);
  25. typedef void (*SDL_IME_Reset_t)(void);
  26. typedef bool (*SDL_IME_ProcessKeyEvent_t)(Uint32, Uint32, bool down);
  27. typedef void (*SDL_IME_UpdateTextInputArea_t)(SDL_Window *window);
  28. typedef void (*SDL_IME_PumpEvents_t)(void);
  29. static SDL_IME_Init_t SDL_IME_Init_Real = NULL;
  30. static SDL_IME_Quit_t SDL_IME_Quit_Real = NULL;
  31. static SDL_IME_SetFocus_t SDL_IME_SetFocus_Real = NULL;
  32. static SDL_IME_Reset_t SDL_IME_Reset_Real = NULL;
  33. static SDL_IME_ProcessKeyEvent_t SDL_IME_ProcessKeyEvent_Real = NULL;
  34. static SDL_IME_UpdateTextInputArea_t SDL_IME_UpdateTextInputArea_Real = NULL;
  35. static SDL_IME_PumpEvents_t SDL_IME_PumpEvents_Real = NULL;
  36. static void InitIME(void)
  37. {
  38. static bool inited = false;
  39. #ifdef HAVE_FCITX
  40. const char *im_module = SDL_getenv("SDL_IM_MODULE");
  41. const char *xmodifiers = SDL_getenv("XMODIFIERS");
  42. #endif
  43. if (inited == true) {
  44. return;
  45. }
  46. inited = true;
  47. // See if fcitx IME support is being requested
  48. #ifdef HAVE_FCITX
  49. if (!SDL_IME_Init_Real &&
  50. ((im_module && SDL_strcmp(im_module, "fcitx") == 0) ||
  51. (!im_module && xmodifiers && SDL_strstr(xmodifiers, "@im=fcitx") != NULL))) {
  52. SDL_IME_Init_Real = SDL_Fcitx_Init;
  53. SDL_IME_Quit_Real = SDL_Fcitx_Quit;
  54. SDL_IME_SetFocus_Real = SDL_Fcitx_SetFocus;
  55. SDL_IME_Reset_Real = SDL_Fcitx_Reset;
  56. SDL_IME_ProcessKeyEvent_Real = SDL_Fcitx_ProcessKeyEvent;
  57. SDL_IME_UpdateTextInputArea_Real = SDL_Fcitx_UpdateTextInputArea;
  58. SDL_IME_PumpEvents_Real = SDL_Fcitx_PumpEvents;
  59. }
  60. #endif // HAVE_FCITX
  61. // default to IBus
  62. #ifdef HAVE_IBUS_IBUS_H
  63. if (!SDL_IME_Init_Real) {
  64. SDL_IME_Init_Real = SDL_IBus_Init;
  65. SDL_IME_Quit_Real = SDL_IBus_Quit;
  66. SDL_IME_SetFocus_Real = SDL_IBus_SetFocus;
  67. SDL_IME_Reset_Real = SDL_IBus_Reset;
  68. SDL_IME_ProcessKeyEvent_Real = SDL_IBus_ProcessKeyEvent;
  69. SDL_IME_UpdateTextInputArea_Real = SDL_IBus_UpdateTextInputArea;
  70. SDL_IME_PumpEvents_Real = SDL_IBus_PumpEvents;
  71. }
  72. #endif // HAVE_IBUS_IBUS_H
  73. }
  74. bool SDL_IME_Init(void)
  75. {
  76. InitIME();
  77. if (SDL_IME_Init_Real) {
  78. if (SDL_IME_Init_Real()) {
  79. return true;
  80. }
  81. // uhoh, the IME implementation's init failed! Disable IME support.
  82. SDL_IME_Init_Real = NULL;
  83. SDL_IME_Quit_Real = NULL;
  84. SDL_IME_SetFocus_Real = NULL;
  85. SDL_IME_Reset_Real = NULL;
  86. SDL_IME_ProcessKeyEvent_Real = NULL;
  87. SDL_IME_UpdateTextInputArea_Real = NULL;
  88. SDL_IME_PumpEvents_Real = NULL;
  89. }
  90. return false;
  91. }
  92. void SDL_IME_Quit(void)
  93. {
  94. if (SDL_IME_Quit_Real) {
  95. SDL_IME_Quit_Real();
  96. }
  97. }
  98. void SDL_IME_SetFocus(bool focused)
  99. {
  100. if (SDL_IME_SetFocus_Real) {
  101. SDL_IME_SetFocus_Real(focused);
  102. }
  103. }
  104. void SDL_IME_Reset(void)
  105. {
  106. if (SDL_IME_Reset_Real) {
  107. SDL_IME_Reset_Real();
  108. }
  109. }
  110. bool SDL_IME_ProcessKeyEvent(Uint32 keysym, Uint32 keycode, bool down)
  111. {
  112. if (SDL_IME_ProcessKeyEvent_Real) {
  113. return SDL_IME_ProcessKeyEvent_Real(keysym, keycode, down);
  114. }
  115. return false;
  116. }
  117. void SDL_IME_UpdateTextInputArea(SDL_Window *window)
  118. {
  119. if (SDL_IME_UpdateTextInputArea_Real) {
  120. SDL_IME_UpdateTextInputArea_Real(window);
  121. }
  122. }
  123. void SDL_IME_PumpEvents(void)
  124. {
  125. if (SDL_IME_PumpEvents_Real) {
  126. SDL_IME_PumpEvents_Real();
  127. }
  128. }