SDL_fcitx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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 <unistd.h>
  20. #include "SDL_fcitx.h"
  21. #include "SDL_keycode.h"
  22. #include "SDL_keyboard.h"
  23. #include "../../events/SDL_keyboard_c.h"
  24. #include "SDL_dbus.h"
  25. #include "SDL_syswm.h"
  26. #if SDL_VIDEO_DRIVER_X11
  27. # include "../../video/x11/SDL_x11video.h"
  28. #endif
  29. #include "SDL_hints.h"
  30. #define FCITX_DBUS_SERVICE "org.freedesktop.portal.Fcitx"
  31. #define FCITX_IM_DBUS_PATH "/org/freedesktop/portal/inputmethod"
  32. #define FCITX_IM_DBUS_INTERFACE "org.fcitx.Fcitx.InputMethod1"
  33. #define FCITX_IC_DBUS_INTERFACE "org.fcitx.Fcitx.InputContext1"
  34. #define DBUS_TIMEOUT 500
  35. typedef struct _FcitxClient
  36. {
  37. SDL_DBusContext *dbus;
  38. char *ic_path;
  39. int id;
  40. SDL_Rect cursor_rect;
  41. } FcitxClient;
  42. static FcitxClient fcitx_client;
  43. static char*
  44. GetAppName()
  45. {
  46. #if defined(__LINUX__) || defined(__FREEBSD__)
  47. char *spot;
  48. char procfile[1024];
  49. char linkfile[1024];
  50. int linksize;
  51. #if defined(__LINUX__)
  52. SDL_snprintf(procfile, sizeof(procfile), "/proc/%d/exe", getpid());
  53. #elif defined(__FREEBSD__)
  54. SDL_snprintf(procfile, sizeof(procfile), "/proc/%d/file", getpid());
  55. #endif
  56. linksize = readlink(procfile, linkfile, sizeof(linkfile) - 1);
  57. if (linksize > 0) {
  58. linkfile[linksize] = '\0';
  59. spot = SDL_strrchr(linkfile, '/');
  60. if (spot) {
  61. return SDL_strdup(spot + 1);
  62. } else {
  63. return SDL_strdup(linkfile);
  64. }
  65. }
  66. #endif /* __LINUX__ || __FREEBSD__ */
  67. return SDL_strdup("SDL_App");
  68. }
  69. static size_t
  70. Fcitx_GetPreeditString(SDL_DBusContext *dbus, DBusMessage *msg, char **ret) {
  71. char *text = NULL, *subtext;
  72. size_t text_bytes = 0;
  73. DBusMessageIter iter, array, sub;
  74. dbus->message_iter_init(msg, &iter);
  75. /* Message type is a(si)i, we only need string part */
  76. if (dbus->message_iter_get_arg_type(&iter) == DBUS_TYPE_ARRAY) {
  77. /* First pass: calculate string length */
  78. dbus->message_iter_recurse(&iter, &array);
  79. while (dbus->message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
  80. dbus->message_iter_recurse(&array, &sub);
  81. if (dbus->message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING) {
  82. dbus->message_iter_get_basic(&sub, &subtext);
  83. if (subtext && *subtext) {
  84. text_bytes += SDL_strlen(subtext);
  85. }
  86. }
  87. dbus->message_iter_next(&array);
  88. }
  89. if (text_bytes) {
  90. text = SDL_malloc(text_bytes + 1);
  91. }
  92. if (text) {
  93. char* pivot = text;
  94. /* Second pass: join all the sub string */
  95. dbus->message_iter_recurse(&iter, &array);
  96. while (dbus->message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
  97. dbus->message_iter_recurse(&array, &sub);
  98. if (dbus->message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING) {
  99. dbus->message_iter_get_basic(&sub, &subtext);
  100. if (subtext && *subtext) {
  101. size_t length = SDL_strlen(subtext);
  102. SDL_strlcpy(pivot, subtext, length + 1);
  103. pivot += length;
  104. }
  105. }
  106. dbus->message_iter_next(&array);
  107. }
  108. } else {
  109. text_bytes = 0;
  110. }
  111. }
  112. *ret= text;
  113. return text_bytes;
  114. }
  115. static DBusHandlerResult
  116. DBus_MessageFilter(DBusConnection *conn, DBusMessage *msg, void *data)
  117. {
  118. SDL_DBusContext *dbus = (SDL_DBusContext *)data;
  119. if (dbus->message_is_signal(msg, FCITX_IC_DBUS_INTERFACE, "CommitString")) {
  120. DBusMessageIter iter;
  121. const char *text = NULL;
  122. dbus->message_iter_init(msg, &iter);
  123. dbus->message_iter_get_basic(&iter, &text);
  124. if (text && *text) {
  125. char buf[SDL_TEXTINPUTEVENT_TEXT_SIZE];
  126. size_t text_bytes = SDL_strlen(text), i = 0;
  127. while (i < text_bytes) {
  128. size_t sz = SDL_utf8strlcpy(buf, text+i, sizeof(buf));
  129. SDL_SendKeyboardText(buf);
  130. i += sz;
  131. }
  132. }
  133. return DBUS_HANDLER_RESULT_HANDLED;
  134. }
  135. if (dbus->message_is_signal(msg, FCITX_IC_DBUS_INTERFACE, "UpdateFormattedPreedit")) {
  136. char *text = NULL;
  137. size_t text_bytes = Fcitx_GetPreeditString(dbus, msg, &text);
  138. if (text_bytes) {
  139. char buf[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
  140. size_t i = 0;
  141. size_t cursor = 0;
  142. while (i < text_bytes) {
  143. const size_t sz = SDL_utf8strlcpy(buf, text + i, sizeof(buf));
  144. const size_t chars = SDL_utf8strlen(buf);
  145. SDL_SendEditingText(buf, cursor, chars);
  146. i += sz;
  147. cursor += chars;
  148. }
  149. SDL_free(text);
  150. } else {
  151. SDL_SendEditingText("", 0, 0);
  152. }
  153. SDL_Fcitx_UpdateTextRect(NULL);
  154. return DBUS_HANDLER_RESULT_HANDLED;
  155. }
  156. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  157. }
  158. static void
  159. FcitxClientICCallMethod(FcitxClient *client, const char *method)
  160. {
  161. if (!client->ic_path) {
  162. return;
  163. }
  164. SDL_DBus_CallVoidMethod(FCITX_DBUS_SERVICE, client->ic_path, FCITX_IC_DBUS_INTERFACE, method, DBUS_TYPE_INVALID);
  165. }
  166. static void SDLCALL
  167. Fcitx_SetCapabilities(void *data,
  168. const char *name,
  169. const char *old_val,
  170. const char *internal_editing)
  171. {
  172. FcitxClient *client = (FcitxClient *)data;
  173. Uint32 caps = 0;
  174. if (!client->ic_path) {
  175. return;
  176. }
  177. if (!(internal_editing && *internal_editing == '1')) {
  178. caps |= (1 << 1); /* Preedit Flag */
  179. caps |= (1 << 4); /* Formatted Preedit Flag */
  180. }
  181. SDL_DBus_CallVoidMethod(FCITX_DBUS_SERVICE, client->ic_path, FCITX_IC_DBUS_INTERFACE, "SetCapability", DBUS_TYPE_UINT64, &caps, DBUS_TYPE_INVALID);
  182. }
  183. static SDL_bool
  184. FcitxCreateInputContext(SDL_DBusContext* dbus, const char *appname, char **ic_path) {
  185. const char *program = "program";
  186. SDL_bool retval = SDL_FALSE;
  187. if (dbus->session_conn) {
  188. DBusMessage *msg = dbus->message_new_method_call(FCITX_DBUS_SERVICE, FCITX_IM_DBUS_PATH, FCITX_IM_DBUS_INTERFACE, "CreateInputContext");
  189. if (msg) {
  190. DBusMessage *reply = NULL;
  191. DBusMessageIter args, array, sub;
  192. dbus->message_iter_init_append(msg, &args);
  193. dbus->message_iter_open_container(&args, DBUS_TYPE_ARRAY, "(ss)", &array);
  194. dbus->message_iter_open_container(&array, DBUS_TYPE_STRUCT, 0, &sub);
  195. dbus->message_iter_append_basic(&sub, DBUS_TYPE_STRING, &program);
  196. dbus->message_iter_append_basic(&sub, DBUS_TYPE_STRING, &appname);
  197. dbus->message_iter_close_container(&array, &sub);
  198. dbus->message_iter_close_container(&args, &array);
  199. reply = dbus->connection_send_with_reply_and_block(dbus->session_conn, msg, 300, NULL);
  200. if (reply) {
  201. if (dbus->message_get_args(reply, NULL, DBUS_TYPE_OBJECT_PATH, ic_path, DBUS_TYPE_INVALID)) {
  202. retval = SDL_TRUE;
  203. }
  204. dbus->message_unref(reply);
  205. }
  206. dbus->message_unref(msg);
  207. }
  208. }
  209. return retval;
  210. }
  211. static SDL_bool
  212. FcitxClientCreateIC(FcitxClient *client)
  213. {
  214. char *appname = GetAppName();
  215. char *ic_path = NULL;
  216. SDL_DBusContext *dbus = client->dbus;
  217. /* SDL_DBus_CallMethod cannot handle a(ss) type, call dbus function directly */
  218. if (!FcitxCreateInputContext(dbus, appname, &ic_path)) {
  219. ic_path = NULL; /* just in case. */
  220. }
  221. SDL_free(appname);
  222. if (ic_path) {
  223. SDL_free(client->ic_path);
  224. client->ic_path = SDL_strdup(ic_path);
  225. dbus->bus_add_match(dbus->session_conn,
  226. "type='signal', interface='org.fcitx.Fcitx.InputContext1'",
  227. NULL);
  228. dbus->connection_add_filter(dbus->session_conn,
  229. &DBus_MessageFilter, dbus,
  230. NULL);
  231. dbus->connection_flush(dbus->session_conn);
  232. SDL_AddHintCallback(SDL_HINT_IME_INTERNAL_EDITING, Fcitx_SetCapabilities, client);
  233. return SDL_TRUE;
  234. }
  235. return SDL_FALSE;
  236. }
  237. static Uint32
  238. Fcitx_ModState(void)
  239. {
  240. Uint32 fcitx_mods = 0;
  241. SDL_Keymod sdl_mods = SDL_GetModState();
  242. if (sdl_mods & KMOD_SHIFT) fcitx_mods |= (1 << 0);
  243. if (sdl_mods & KMOD_CAPS) fcitx_mods |= (1 << 1);
  244. if (sdl_mods & KMOD_CTRL) fcitx_mods |= (1 << 2);
  245. if (sdl_mods & KMOD_ALT) fcitx_mods |= (1 << 3);
  246. if (sdl_mods & KMOD_NUM) fcitx_mods |= (1 << 4);
  247. if (sdl_mods & KMOD_MODE) fcitx_mods |= (1 << 7);
  248. if (sdl_mods & KMOD_LGUI) fcitx_mods |= (1 << 6);
  249. if (sdl_mods & KMOD_RGUI) fcitx_mods |= (1 << 28);
  250. return fcitx_mods;
  251. }
  252. SDL_bool
  253. SDL_Fcitx_Init()
  254. {
  255. fcitx_client.dbus = SDL_DBus_GetContext();
  256. fcitx_client.cursor_rect.x = -1;
  257. fcitx_client.cursor_rect.y = -1;
  258. fcitx_client.cursor_rect.w = 0;
  259. fcitx_client.cursor_rect.h = 0;
  260. return FcitxClientCreateIC(&fcitx_client);
  261. }
  262. void
  263. SDL_Fcitx_Quit()
  264. {
  265. FcitxClientICCallMethod(&fcitx_client, "DestroyIC");
  266. if (fcitx_client.ic_path) {
  267. SDL_free(fcitx_client.ic_path);
  268. fcitx_client.ic_path = NULL;
  269. }
  270. }
  271. void
  272. SDL_Fcitx_SetFocus(SDL_bool focused)
  273. {
  274. if (focused) {
  275. FcitxClientICCallMethod(&fcitx_client, "FocusIn");
  276. } else {
  277. FcitxClientICCallMethod(&fcitx_client, "FocusOut");
  278. }
  279. }
  280. void
  281. SDL_Fcitx_Reset(void)
  282. {
  283. FcitxClientICCallMethod(&fcitx_client, "Reset");
  284. FcitxClientICCallMethod(&fcitx_client, "CloseIC");
  285. }
  286. SDL_bool
  287. SDL_Fcitx_ProcessKeyEvent(Uint32 keysym, Uint32 keycode)
  288. {
  289. Uint32 state = Fcitx_ModState();
  290. Uint32 handled = SDL_FALSE;
  291. Uint32 is_release = SDL_FALSE;
  292. Uint32 event_time = 0;
  293. if (!fcitx_client.ic_path) {
  294. return SDL_FALSE;
  295. }
  296. if (SDL_DBus_CallMethod(FCITX_DBUS_SERVICE, fcitx_client.ic_path, FCITX_IC_DBUS_INTERFACE, "ProcessKeyEvent",
  297. DBUS_TYPE_UINT32, &keysym, DBUS_TYPE_UINT32, &keycode, DBUS_TYPE_UINT32, &state, DBUS_TYPE_BOOLEAN, &is_release, DBUS_TYPE_UINT32, &event_time, DBUS_TYPE_INVALID,
  298. DBUS_TYPE_BOOLEAN, &handled, DBUS_TYPE_INVALID)) {
  299. if (handled) {
  300. SDL_Fcitx_UpdateTextRect(NULL);
  301. return SDL_TRUE;
  302. }
  303. }
  304. return SDL_FALSE;
  305. }
  306. void
  307. SDL_Fcitx_UpdateTextRect(SDL_Rect *rect)
  308. {
  309. SDL_Window *focused_win = NULL;
  310. SDL_SysWMinfo info;
  311. int x = 0, y = 0;
  312. SDL_Rect *cursor = &fcitx_client.cursor_rect;
  313. if (rect) {
  314. SDL_memcpy(cursor, rect, sizeof(SDL_Rect));
  315. }
  316. focused_win = SDL_GetKeyboardFocus();
  317. if (!focused_win) {
  318. return ;
  319. }
  320. SDL_VERSION(&info.version);
  321. if (!SDL_GetWindowWMInfo(focused_win, &info)) {
  322. return;
  323. }
  324. SDL_GetWindowPosition(focused_win, &x, &y);
  325. #if SDL_VIDEO_DRIVER_X11
  326. if (info.subsystem == SDL_SYSWM_X11) {
  327. SDL_DisplayData *displaydata = (SDL_DisplayData *) SDL_GetDisplayForWindow(focused_win)->driverdata;
  328. Display *x_disp = info.info.x11.display;
  329. Window x_win = info.info.x11.window;
  330. int x_screen = displaydata->screen;
  331. Window unused;
  332. X11_XTranslateCoordinates(x_disp, x_win, RootWindow(x_disp, x_screen), 0, 0, &x, &y, &unused);
  333. }
  334. #endif
  335. if (cursor->x == -1 && cursor->y == -1 && cursor->w == 0 && cursor->h == 0) {
  336. /* move to bottom left */
  337. int w = 0, h = 0;
  338. SDL_GetWindowSize(focused_win, &w, &h);
  339. cursor->x = 0;
  340. cursor->y = h;
  341. }
  342. x += cursor->x;
  343. y += cursor->y;
  344. SDL_DBus_CallVoidMethod(FCITX_DBUS_SERVICE, fcitx_client.ic_path, FCITX_IC_DBUS_INTERFACE, "SetCursorRect",
  345. DBUS_TYPE_INT32, &x, DBUS_TYPE_INT32, &y, DBUS_TYPE_INT32, &cursor->w, DBUS_TYPE_INT32, &cursor->h, DBUS_TYPE_INVALID);
  346. }
  347. void
  348. SDL_Fcitx_PumpEvents(void)
  349. {
  350. SDL_DBusContext *dbus = fcitx_client.dbus;
  351. DBusConnection *conn = dbus->session_conn;
  352. dbus->connection_read_write(conn, 0);
  353. while (dbus->connection_dispatch(conn) == DBUS_DISPATCH_DATA_REMAINS) {
  354. /* Do nothing, actual work happens in DBus_MessageFilter */
  355. usleep(10);
  356. }
  357. }
  358. /* vi: set ts=4 sw=4 expandtab: */