SDL_fcitx.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2017 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. #ifdef HAVE_FCITX_FRONTEND_H
  20. #include <fcitx/frontend.h>
  21. #include <unistd.h>
  22. #include "SDL_fcitx.h"
  23. #include "SDL_keycode.h"
  24. #include "SDL_keyboard.h"
  25. #include "../../events/SDL_keyboard_c.h"
  26. #include "SDL_dbus.h"
  27. #include "SDL_syswm.h"
  28. #if SDL_VIDEO_DRIVER_X11
  29. # include "../../video/x11/SDL_x11video.h"
  30. #endif
  31. #include "SDL_hints.h"
  32. #define FCITX_DBUS_SERVICE "org.fcitx.Fcitx"
  33. #define FCITX_IM_DBUS_PATH "/inputmethod"
  34. #define FCITX_IC_DBUS_PATH "/inputcontext_%d"
  35. #define FCITX_IM_DBUS_INTERFACE "org.fcitx.Fcitx.InputMethod"
  36. #define FCITX_IC_DBUS_INTERFACE "org.fcitx.Fcitx.InputContext"
  37. #define IC_NAME_MAX 64
  38. #define DBUS_TIMEOUT 500
  39. typedef struct _FcitxClient
  40. {
  41. SDL_DBusContext *dbus;
  42. char servicename[IC_NAME_MAX];
  43. char icname[IC_NAME_MAX];
  44. int id;
  45. SDL_Rect cursor_rect;
  46. } FcitxClient;
  47. static FcitxClient fcitx_client;
  48. static int
  49. GetDisplayNumber()
  50. {
  51. const char *display = SDL_getenv("DISPLAY");
  52. const char *p = NULL;
  53. int number = 0;
  54. if (display == NULL)
  55. return 0;
  56. display = SDL_strchr(display, ':');
  57. if (display == NULL)
  58. return 0;
  59. display++;
  60. p = SDL_strchr(display, '.');
  61. if (p == NULL && display != NULL) {
  62. number = SDL_strtod(display, NULL);
  63. } else {
  64. char *buffer = SDL_strdup(display);
  65. buffer[p - display] = '\0';
  66. number = SDL_strtod(buffer, NULL);
  67. SDL_free(buffer);
  68. }
  69. return number;
  70. }
  71. static char*
  72. GetAppName()
  73. {
  74. #if defined(__LINUX__) || defined(__FREEBSD__)
  75. char *spot;
  76. char procfile[1024];
  77. char linkfile[1024];
  78. int linksize;
  79. #if defined(__LINUX__)
  80. SDL_snprintf(procfile, sizeof(procfile), "/proc/%d/exe", getpid());
  81. #elif defined(__FREEBSD__)
  82. SDL_snprintf(procfile, sizeof(procfile), "/proc/%d/file", getpid());
  83. #endif
  84. linksize = readlink(procfile, linkfile, sizeof(linkfile) - 1);
  85. if (linksize > 0) {
  86. linkfile[linksize] = '\0';
  87. spot = SDL_strrchr(linkfile, '/');
  88. if (spot) {
  89. return SDL_strdup(spot + 1);
  90. } else {
  91. return SDL_strdup(linkfile);
  92. }
  93. }
  94. #endif /* __LINUX__ || __FREEBSD__ */
  95. return SDL_strdup("SDL_App");
  96. }
  97. static DBusHandlerResult
  98. DBus_MessageFilter(DBusConnection *conn, DBusMessage *msg, void *data)
  99. {
  100. SDL_DBusContext *dbus = (SDL_DBusContext *)data;
  101. if (dbus->message_is_signal(msg, FCITX_IC_DBUS_INTERFACE, "CommitString")) {
  102. DBusMessageIter iter;
  103. const char *text = NULL;
  104. dbus->message_iter_init(msg, &iter);
  105. dbus->message_iter_get_basic(&iter, &text);
  106. if (text)
  107. SDL_SendKeyboardText(text);
  108. return DBUS_HANDLER_RESULT_HANDLED;
  109. }
  110. if (dbus->message_is_signal(msg, FCITX_IC_DBUS_INTERFACE, "UpdatePreedit")) {
  111. DBusMessageIter iter;
  112. const char *text;
  113. dbus->message_iter_init(msg, &iter);
  114. dbus->message_iter_get_basic(&iter, &text);
  115. if (text && *text) {
  116. char buf[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
  117. size_t text_bytes = SDL_strlen(text), i = 0;
  118. size_t cursor = 0;
  119. while (i < text_bytes) {
  120. const size_t sz = SDL_utf8strlcpy(buf, text + i, sizeof(buf));
  121. const size_t chars = SDL_utf8strlen(buf);
  122. SDL_SendEditingText(buf, cursor, chars);
  123. i += sz;
  124. cursor += chars;
  125. }
  126. }
  127. SDL_Fcitx_UpdateTextRect(NULL);
  128. return DBUS_HANDLER_RESULT_HANDLED;
  129. }
  130. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  131. }
  132. static void
  133. FcitxClientICCallMethod(FcitxClient *client, const char *method)
  134. {
  135. SDL_DBus_CallVoidMethod(client->servicename, client->icname, FCITX_IC_DBUS_INTERFACE, method, DBUS_TYPE_INVALID);
  136. }
  137. static void
  138. Fcitx_SetCapabilities(void *data,
  139. const char *name,
  140. const char *old_val,
  141. const char *internal_editing)
  142. {
  143. FcitxClient *client = (FcitxClient *)data;
  144. Uint32 caps = CAPACITY_NONE;
  145. if (!(internal_editing && *internal_editing == '1')) {
  146. caps |= CAPACITY_PREEDIT;
  147. }
  148. SDL_DBus_CallVoidMethod(client->servicename, client->icname, FCITX_IC_DBUS_INTERFACE, "SetCapacity", DBUS_TYPE_UINT32, &caps, DBUS_TYPE_INVALID);
  149. }
  150. static SDL_bool
  151. FcitxClientCreateIC(FcitxClient *client)
  152. {
  153. char *appname = GetAppName();
  154. pid_t pid = getpid();
  155. int id = -1;
  156. Uint32 enable, arg1, arg2, arg3, arg4;
  157. if (!SDL_DBus_CallMethod(client->servicename, FCITX_IM_DBUS_PATH, FCITX_IM_DBUS_INTERFACE, "CreateICv3",
  158. DBUS_TYPE_STRING, &appname, DBUS_TYPE_INT32, &pid, DBUS_TYPE_INVALID,
  159. DBUS_TYPE_INT32, &id, DBUS_TYPE_BOOLEAN, &enable, DBUS_TYPE_UINT32, &arg1, DBUS_TYPE_UINT32, &arg2, DBUS_TYPE_UINT32, &arg3, DBUS_TYPE_UINT32, &arg4, DBUS_TYPE_INVALID)) {
  160. id = -1; /* just in case. */
  161. }
  162. SDL_free(appname);
  163. if (id >= 0) {
  164. SDL_DBusContext *dbus = client->dbus;
  165. client->id = id;
  166. SDL_snprintf(client->icname, IC_NAME_MAX, FCITX_IC_DBUS_PATH, client->id);
  167. dbus->bus_add_match(dbus->session_conn,
  168. "type='signal', interface='org.fcitx.Fcitx.InputContext'",
  169. NULL);
  170. dbus->connection_add_filter(dbus->session_conn,
  171. &DBus_MessageFilter, dbus,
  172. NULL);
  173. dbus->connection_flush(dbus->session_conn);
  174. SDL_AddHintCallback(SDL_HINT_IME_INTERNAL_EDITING, &Fcitx_SetCapabilities, client);
  175. return SDL_TRUE;
  176. }
  177. return SDL_FALSE;
  178. }
  179. static Uint32
  180. Fcitx_ModState(void)
  181. {
  182. Uint32 fcitx_mods = 0;
  183. SDL_Keymod sdl_mods = SDL_GetModState();
  184. if (sdl_mods & KMOD_SHIFT) fcitx_mods |= FcitxKeyState_Shift;
  185. if (sdl_mods & KMOD_CAPS) fcitx_mods |= FcitxKeyState_CapsLock;
  186. if (sdl_mods & KMOD_CTRL) fcitx_mods |= FcitxKeyState_Ctrl;
  187. if (sdl_mods & KMOD_ALT) fcitx_mods |= FcitxKeyState_Alt;
  188. if (sdl_mods & KMOD_NUM) fcitx_mods |= FcitxKeyState_NumLock;
  189. if (sdl_mods & KMOD_LGUI) fcitx_mods |= FcitxKeyState_Super;
  190. if (sdl_mods & KMOD_RGUI) fcitx_mods |= FcitxKeyState_Meta;
  191. return fcitx_mods;
  192. }
  193. SDL_bool
  194. SDL_Fcitx_Init()
  195. {
  196. fcitx_client.dbus = SDL_DBus_GetContext();
  197. fcitx_client.cursor_rect.x = -1;
  198. fcitx_client.cursor_rect.y = -1;
  199. fcitx_client.cursor_rect.w = 0;
  200. fcitx_client.cursor_rect.h = 0;
  201. SDL_snprintf(fcitx_client.servicename, IC_NAME_MAX,
  202. "%s-%d",
  203. FCITX_DBUS_SERVICE, GetDisplayNumber());
  204. return FcitxClientCreateIC(&fcitx_client);
  205. }
  206. void
  207. SDL_Fcitx_Quit()
  208. {
  209. FcitxClientICCallMethod(&fcitx_client, "DestroyIC");
  210. }
  211. void
  212. SDL_Fcitx_SetFocus(SDL_bool focused)
  213. {
  214. if (focused) {
  215. FcitxClientICCallMethod(&fcitx_client, "FocusIn");
  216. } else {
  217. FcitxClientICCallMethod(&fcitx_client, "FocusOut");
  218. }
  219. }
  220. void
  221. SDL_Fcitx_Reset(void)
  222. {
  223. FcitxClientICCallMethod(&fcitx_client, "Reset");
  224. FcitxClientICCallMethod(&fcitx_client, "CloseIC");
  225. }
  226. SDL_bool
  227. SDL_Fcitx_ProcessKeyEvent(Uint32 keysym, Uint32 keycode)
  228. {
  229. Uint32 state = Fcitx_ModState();
  230. Uint32 handled = SDL_FALSE;
  231. int type = FCITX_PRESS_KEY;
  232. Uint32 event_time = 0;
  233. if (SDL_DBus_CallMethod(fcitx_client.servicename, fcitx_client.icname, FCITX_IC_DBUS_INTERFACE, "ProcessKeyEvent",
  234. DBUS_TYPE_UINT32, &keysym, DBUS_TYPE_UINT32, &keycode, DBUS_TYPE_UINT32, &state, DBUS_TYPE_INT32, &type, DBUS_TYPE_UINT32, &event_time, DBUS_TYPE_INVALID,
  235. DBUS_TYPE_INT32, &handled, DBUS_TYPE_INVALID)) {
  236. if (handled) {
  237. SDL_Fcitx_UpdateTextRect(NULL);
  238. return SDL_TRUE;
  239. }
  240. }
  241. return SDL_FALSE;
  242. }
  243. void
  244. SDL_Fcitx_UpdateTextRect(SDL_Rect *rect)
  245. {
  246. SDL_Window *focused_win = NULL;
  247. SDL_SysWMinfo info;
  248. int x = 0, y = 0;
  249. SDL_Rect *cursor = &fcitx_client.cursor_rect;
  250. if (rect) {
  251. SDL_memcpy(cursor, rect, sizeof(SDL_Rect));
  252. }
  253. focused_win = SDL_GetKeyboardFocus();
  254. if (!focused_win) {
  255. return ;
  256. }
  257. SDL_VERSION(&info.version);
  258. if (!SDL_GetWindowWMInfo(focused_win, &info)) {
  259. return;
  260. }
  261. SDL_GetWindowPosition(focused_win, &x, &y);
  262. #if SDL_VIDEO_DRIVER_X11
  263. if (info.subsystem == SDL_SYSWM_X11) {
  264. SDL_DisplayData *displaydata = (SDL_DisplayData *) SDL_GetDisplayForWindow(focused_win)->driverdata;
  265. Display *x_disp = info.info.x11.display;
  266. Window x_win = info.info.x11.window;
  267. int x_screen = displaydata->screen;
  268. Window unused;
  269. X11_XTranslateCoordinates(x_disp, x_win, RootWindow(x_disp, x_screen), 0, 0, &x, &y, &unused);
  270. }
  271. #endif
  272. if (cursor->x == -1 && cursor->y == -1 && cursor->w == 0 && cursor->h == 0) {
  273. /* move to bottom left */
  274. int w = 0, h = 0;
  275. SDL_GetWindowSize(focused_win, &w, &h);
  276. cursor->x = 0;
  277. cursor->y = h;
  278. }
  279. x += cursor->x;
  280. y += cursor->y;
  281. SDL_DBus_CallVoidMethod(fcitx_client.servicename, fcitx_client.icname, FCITX_IC_DBUS_INTERFACE, "SetCursorRect",
  282. DBUS_TYPE_INT32, &x, DBUS_TYPE_INT32, &y, DBUS_TYPE_INT32, &cursor->w, DBUS_TYPE_INT32, &cursor->h, DBUS_TYPE_INVALID);
  283. }
  284. void
  285. SDL_Fcitx_PumpEvents(void)
  286. {
  287. SDL_DBusContext *dbus = fcitx_client.dbus;
  288. DBusConnection *conn = dbus->session_conn;
  289. dbus->connection_read_write(conn, 0);
  290. while (dbus->connection_dispatch(conn) == DBUS_DISPATCH_DATA_REMAINS) {
  291. /* Do nothing, actual work happens in DBus_MessageFilter */
  292. usleep(10);
  293. }
  294. }
  295. #endif /* HAVE_FCITX_FRONTEND_H */
  296. /* vi: set ts=4 sw=4 expandtab: */