SDL_ibus.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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. #ifdef HAVE_IBUS_IBUS_H
  20. #include "SDL.h"
  21. #include "SDL_syswm.h"
  22. #include "SDL_ibus.h"
  23. #include "SDL_dbus.h"
  24. #include "../../video/SDL_sysvideo.h"
  25. #include "../../events/SDL_keyboard_c.h"
  26. #if SDL_VIDEO_DRIVER_X11
  27. #include "../../video/x11/SDL_x11video.h"
  28. #endif
  29. #include <sys/inotify.h>
  30. #include <unistd.h>
  31. #include <fcntl.h>
  32. static const char IBUS_SERVICE[] = "org.freedesktop.IBus";
  33. static const char IBUS_PATH[] = "/org/freedesktop/IBus";
  34. static const char IBUS_INTERFACE[] = "org.freedesktop.IBus";
  35. static const char IBUS_INPUT_INTERFACE[] = "org.freedesktop.IBus.InputContext";
  36. static char *input_ctx_path = NULL;
  37. static SDL_Rect ibus_cursor_rect = {0};
  38. static DBusConnection *ibus_conn = NULL;
  39. static char *ibus_addr_file = NULL;
  40. int inotify_fd = -1, inotify_wd = -1;
  41. static Uint32
  42. IBus_ModState(void)
  43. {
  44. Uint32 ibus_mods = 0;
  45. SDL_Keymod sdl_mods = SDL_GetModState();
  46. /* Not sure about MOD3, MOD4 and HYPER mappings */
  47. if(sdl_mods & KMOD_LSHIFT) ibus_mods |= IBUS_SHIFT_MASK;
  48. if(sdl_mods & KMOD_CAPS) ibus_mods |= IBUS_LOCK_MASK;
  49. if(sdl_mods & KMOD_LCTRL) ibus_mods |= IBUS_CONTROL_MASK;
  50. if(sdl_mods & KMOD_LALT) ibus_mods |= IBUS_MOD1_MASK;
  51. if(sdl_mods & KMOD_NUM) ibus_mods |= IBUS_MOD2_MASK;
  52. if(sdl_mods & KMOD_MODE) ibus_mods |= IBUS_MOD5_MASK;
  53. if(sdl_mods & KMOD_LGUI) ibus_mods |= IBUS_SUPER_MASK;
  54. if(sdl_mods & KMOD_RGUI) ibus_mods |= IBUS_META_MASK;
  55. return ibus_mods;
  56. }
  57. static const char *
  58. IBus_GetVariantText(DBusConnection *conn, DBusMessageIter *iter, SDL_DBusContext *dbus)
  59. {
  60. /* The text we need is nested weirdly, use dbus-monitor to see the structure better */
  61. const char *text = NULL;
  62. DBusMessageIter sub1, sub2;
  63. if(dbus->message_iter_get_arg_type(iter) != DBUS_TYPE_VARIANT){
  64. return NULL;
  65. }
  66. dbus->message_iter_recurse(iter, &sub1);
  67. if(dbus->message_iter_get_arg_type(&sub1) != DBUS_TYPE_STRUCT){
  68. return NULL;
  69. }
  70. dbus->message_iter_recurse(&sub1, &sub2);
  71. if(dbus->message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING){
  72. return NULL;
  73. }
  74. const char *struct_id = NULL;
  75. dbus->message_iter_get_basic(&sub2, &struct_id);
  76. if(!struct_id || SDL_strncmp(struct_id, "IBusText", sizeof("IBusText")) != 0){
  77. return NULL;
  78. }
  79. dbus->message_iter_next(&sub2);
  80. dbus->message_iter_next(&sub2);
  81. if(dbus->message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING){
  82. return NULL;
  83. }
  84. dbus->message_iter_get_basic(&sub2, &text);
  85. return text;
  86. }
  87. static size_t
  88. IBus_utf8_strlen(const char *str)
  89. {
  90. size_t utf8_len = 0;
  91. const char *p;
  92. for(p = str; *p; ++p){
  93. if(!((*p & 0x80) && !(*p & 0x40))){
  94. ++utf8_len;
  95. }
  96. }
  97. return utf8_len;
  98. }
  99. static DBusHandlerResult
  100. IBus_MessageFilter(DBusConnection *conn, DBusMessage *msg, void *user_data)
  101. {
  102. SDL_DBusContext *dbus = (SDL_DBusContext *)user_data;
  103. if(dbus->message_is_signal(msg, IBUS_INPUT_INTERFACE, "CommitText")){
  104. DBusMessageIter iter;
  105. dbus->message_iter_init(msg, &iter);
  106. const char *text = IBus_GetVariantText(conn, &iter, dbus);
  107. if(text && *text){
  108. char buf[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
  109. size_t text_bytes = SDL_strlen(text), i = 0;
  110. while(i < text_bytes){
  111. size_t sz = SDL_utf8strlcpy(buf, text+i, sizeof(buf));
  112. SDL_SendKeyboardText(buf);
  113. i += sz;
  114. }
  115. }
  116. return DBUS_HANDLER_RESULT_HANDLED;
  117. }
  118. if(dbus->message_is_signal(msg, IBUS_INPUT_INTERFACE, "UpdatePreeditText")){
  119. DBusMessageIter iter;
  120. dbus->message_iter_init(msg, &iter);
  121. const char *text = IBus_GetVariantText(conn, &iter, dbus);
  122. if(text && *text){
  123. char buf[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
  124. size_t text_bytes = SDL_strlen(text), i = 0;
  125. size_t cursor = 0;
  126. while(i < text_bytes){
  127. size_t sz = SDL_utf8strlcpy(buf, text+i, sizeof(buf));
  128. size_t chars = IBus_utf8_strlen(buf);
  129. SDL_SendEditingText(buf, cursor, chars);
  130. i += sz;
  131. cursor += chars;
  132. }
  133. }
  134. SDL_IBus_UpdateTextRect(NULL);
  135. return DBUS_HANDLER_RESULT_HANDLED;
  136. }
  137. if(dbus->message_is_signal(msg, IBUS_INPUT_INTERFACE, "HidePreeditText")){
  138. SDL_SendEditingText("", 0, 0);
  139. return DBUS_HANDLER_RESULT_HANDLED;
  140. }
  141. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  142. }
  143. static char *
  144. IBus_ReadAddressFromFile(const char *file_path)
  145. {
  146. FILE *addr_file = fopen(file_path, "r");
  147. if(!addr_file){
  148. return NULL;
  149. }
  150. char addr_buf[1024];
  151. SDL_bool success = SDL_FALSE;
  152. while(fgets(addr_buf, sizeof(addr_buf), addr_file)){
  153. if(SDL_strncmp(addr_buf, "IBUS_ADDRESS=", sizeof("IBUS_ADDRESS=")-1) == 0){
  154. size_t sz = SDL_strlen(addr_buf);
  155. if(addr_buf[sz-1] == '\n') addr_buf[sz-1] = 0;
  156. if(addr_buf[sz-2] == '\r') addr_buf[sz-2] = 0;
  157. success = SDL_TRUE;
  158. break;
  159. }
  160. }
  161. fclose(addr_file);
  162. if(success){
  163. return SDL_strdup(addr_buf + (sizeof("IBUS_ADDRESS=") - 1));
  164. } else {
  165. return NULL;
  166. }
  167. }
  168. static char *
  169. IBus_GetDBusAddressFilename(void)
  170. {
  171. if(ibus_addr_file){
  172. return SDL_strdup(ibus_addr_file);
  173. }
  174. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  175. if(!dbus){
  176. return NULL;
  177. }
  178. /* Use this environment variable if it exists. */
  179. const char *addr = SDL_getenv("IBUS_ADDRESS");
  180. if(addr && *addr){
  181. return SDL_strdup(addr);
  182. }
  183. /* Otherwise, we have to get the hostname, display, machine id, config dir
  184. and look up the address from a filepath using all those bits, eek. */
  185. const char *disp_env = SDL_getenv("DISPLAY");
  186. char *display = NULL;
  187. if(!disp_env || !*disp_env){
  188. display = SDL_strdup(":0.0");
  189. } else {
  190. display = SDL_strdup(disp_env);
  191. }
  192. const char *host = display;
  193. char *disp_num = SDL_strrchr(display, ':'),
  194. *screen_num = SDL_strrchr(display, '.');
  195. if(!disp_num){
  196. SDL_free(display);
  197. return NULL;
  198. }
  199. *disp_num = 0;
  200. disp_num++;
  201. if(screen_num){
  202. *screen_num = 0;
  203. }
  204. if(!*host){
  205. host = "unix";
  206. }
  207. char config_dir[PATH_MAX];
  208. SDL_memset(config_dir, 0, sizeof(config_dir));
  209. const char *conf_env = SDL_getenv("XDG_CONFIG_HOME");
  210. if(conf_env && *conf_env){
  211. SDL_strlcpy(config_dir, conf_env, sizeof(config_dir));
  212. } else {
  213. const char *home_env = SDL_getenv("HOME");
  214. if(!home_env || !*home_env){
  215. SDL_free(display);
  216. return NULL;
  217. }
  218. SDL_snprintf(config_dir, sizeof(config_dir), "%s/.config", home_env);
  219. }
  220. char *key = dbus->get_local_machine_id();
  221. char file_path[PATH_MAX];
  222. SDL_memset(file_path, 0, sizeof(file_path));
  223. SDL_snprintf(file_path, sizeof(file_path), "%s/ibus/bus/%s-%s-%s",
  224. config_dir, key, host, disp_num);
  225. dbus->free(key);
  226. SDL_free(display);
  227. return SDL_strdup(file_path);
  228. }
  229. static SDL_bool IBus_CheckConnection(SDL_DBusContext *dbus);
  230. static void
  231. IBus_SetCapabilities(void *data, const char *name, const char *old_val,
  232. const char *internal_editing)
  233. {
  234. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  235. if(IBus_CheckConnection(dbus)){
  236. DBusMessage *msg = dbus->message_new_method_call(IBUS_SERVICE,
  237. input_ctx_path,
  238. IBUS_INPUT_INTERFACE,
  239. "SetCapabilities");
  240. if(msg){
  241. Uint32 caps = IBUS_CAP_FOCUS;
  242. if(!(internal_editing && *internal_editing == '1')){
  243. caps |= IBUS_CAP_PREEDIT_TEXT;
  244. }
  245. dbus->message_append_args(msg,
  246. DBUS_TYPE_UINT32, &caps,
  247. DBUS_TYPE_INVALID);
  248. }
  249. if(msg){
  250. if(dbus->connection_send(ibus_conn, msg, NULL)){
  251. dbus->connection_flush(ibus_conn);
  252. }
  253. dbus->message_unref(msg);
  254. }
  255. }
  256. }
  257. static SDL_bool
  258. IBus_SetupConnection(SDL_DBusContext *dbus, const char* addr)
  259. {
  260. const char *path = NULL;
  261. SDL_bool result = SDL_FALSE;
  262. ibus_conn = dbus->connection_open_private(addr, NULL);
  263. if(!ibus_conn){
  264. return SDL_FALSE;
  265. }
  266. dbus->connection_flush(ibus_conn);
  267. if(!dbus->bus_register(ibus_conn, NULL)){
  268. ibus_conn = NULL;
  269. return SDL_FALSE;
  270. }
  271. dbus->connection_flush(ibus_conn);
  272. DBusMessage *msg = dbus->message_new_method_call(IBUS_SERVICE,
  273. IBUS_PATH,
  274. IBUS_INTERFACE,
  275. "CreateInputContext");
  276. if(msg){
  277. const char *client_name = "SDL2_Application";
  278. dbus->message_append_args(msg,
  279. DBUS_TYPE_STRING, &client_name,
  280. DBUS_TYPE_INVALID);
  281. }
  282. if(msg){
  283. DBusMessage *reply;
  284. reply = dbus->connection_send_with_reply_and_block(ibus_conn, msg, 1000, NULL);
  285. if(reply){
  286. if(dbus->message_get_args(reply, NULL,
  287. DBUS_TYPE_OBJECT_PATH, &path,
  288. DBUS_TYPE_INVALID)){
  289. if(input_ctx_path){
  290. SDL_free(input_ctx_path);
  291. }
  292. input_ctx_path = SDL_strdup(path);
  293. result = SDL_TRUE;
  294. }
  295. dbus->message_unref(reply);
  296. }
  297. dbus->message_unref(msg);
  298. }
  299. if(result){
  300. SDL_AddHintCallback(SDL_HINT_IM_INTERNAL_EDITING, &IBus_SetCapabilities, NULL);
  301. dbus->bus_add_match(ibus_conn, "type='signal',interface='org.freedesktop.IBus.InputContext'", NULL);
  302. dbus->connection_add_filter(ibus_conn, &IBus_MessageFilter, dbus, NULL);
  303. dbus->connection_flush(ibus_conn);
  304. }
  305. SDL_IBus_SetFocus(SDL_GetKeyboardFocus() != NULL);
  306. SDL_IBus_UpdateTextRect(NULL);
  307. return result;
  308. }
  309. static SDL_bool
  310. IBus_CheckConnection(SDL_DBusContext *dbus)
  311. {
  312. if(!dbus) return SDL_FALSE;
  313. if(ibus_conn && dbus->connection_get_is_connected(ibus_conn)){
  314. return SDL_TRUE;
  315. }
  316. if(inotify_fd > 0 && inotify_wd > 0){
  317. char buf[1024];
  318. ssize_t readsize = read(inotify_fd, buf, sizeof(buf));
  319. if(readsize > 0){
  320. char *p;
  321. SDL_bool file_updated = SDL_FALSE;
  322. for(p = buf; p < buf + readsize; /**/){
  323. struct inotify_event *event = (struct inotify_event*) p;
  324. if(event->len > 0){
  325. char *addr_file_no_path = SDL_strrchr(ibus_addr_file, '/');
  326. if(!addr_file_no_path) return SDL_FALSE;
  327. if(SDL_strcmp(addr_file_no_path + 1, event->name) == 0){
  328. file_updated = SDL_TRUE;
  329. break;
  330. }
  331. }
  332. p += sizeof(struct inotify_event) + event->len;
  333. }
  334. if(file_updated){
  335. char *addr = IBus_ReadAddressFromFile(ibus_addr_file);
  336. if(addr){
  337. SDL_bool result = IBus_SetupConnection(dbus, addr);
  338. SDL_free(addr);
  339. return result;
  340. }
  341. }
  342. }
  343. }
  344. return SDL_FALSE;
  345. }
  346. SDL_bool
  347. SDL_IBus_Init(void)
  348. {
  349. SDL_bool result = SDL_FALSE;
  350. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  351. if(dbus){
  352. char *addr_file = IBus_GetDBusAddressFilename();
  353. if(!addr_file){
  354. return SDL_FALSE;
  355. }
  356. ibus_addr_file = SDL_strdup(addr_file);
  357. char *addr = IBus_ReadAddressFromFile(addr_file);
  358. if(inotify_fd < 0){
  359. inotify_fd = inotify_init();
  360. fcntl(inotify_fd, F_SETFL, O_NONBLOCK);
  361. }
  362. char *addr_file_dir = SDL_strrchr(addr_file, '/');
  363. if(addr_file_dir){
  364. *addr_file_dir = 0;
  365. }
  366. inotify_wd = inotify_add_watch(inotify_fd, addr_file, IN_CREATE | IN_MODIFY);
  367. SDL_free(addr_file);
  368. result = IBus_SetupConnection(dbus, addr);
  369. SDL_free(addr);
  370. }
  371. return result;
  372. }
  373. void
  374. SDL_IBus_Quit(void)
  375. {
  376. if(input_ctx_path){
  377. SDL_free(input_ctx_path);
  378. input_ctx_path = NULL;
  379. }
  380. if(ibus_addr_file){
  381. SDL_free(ibus_addr_file);
  382. ibus_addr_file = NULL;
  383. }
  384. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  385. if(dbus && ibus_conn){
  386. dbus->connection_close(ibus_conn);
  387. dbus->connection_unref(ibus_conn);
  388. }
  389. if(inotify_fd > 0 && inotify_wd > 0){
  390. inotify_rm_watch(inotify_fd, inotify_wd);
  391. inotify_wd = -1;
  392. }
  393. SDL_DelHintCallback(SDL_HINT_IM_INTERNAL_EDITING, &IBus_SetCapabilities, NULL);
  394. SDL_memset(&ibus_cursor_rect, 0, sizeof(ibus_cursor_rect));
  395. }
  396. static void
  397. IBus_SimpleMessage(const char *method)
  398. {
  399. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  400. if(IBus_CheckConnection(dbus)){
  401. DBusMessage *msg = dbus->message_new_method_call(IBUS_SERVICE,
  402. input_ctx_path,
  403. IBUS_INPUT_INTERFACE,
  404. method);
  405. if(msg){
  406. if(dbus->connection_send(ibus_conn, msg, NULL)){
  407. dbus->connection_flush(ibus_conn);
  408. }
  409. dbus->message_unref(msg);
  410. }
  411. }
  412. }
  413. void
  414. SDL_IBus_SetFocus(SDL_bool focused)
  415. {
  416. const char *method = focused ? "FocusIn" : "FocusOut";
  417. IBus_SimpleMessage(method);
  418. }
  419. void
  420. SDL_IBus_Reset(void)
  421. {
  422. IBus_SimpleMessage("Reset");
  423. }
  424. SDL_bool
  425. SDL_IBus_ProcessKeyEvent(Uint32 keysym, Uint32 keycode)
  426. {
  427. SDL_bool result = SDL_FALSE;
  428. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  429. if(IBus_CheckConnection(dbus)){
  430. DBusMessage *msg = dbus->message_new_method_call(IBUS_SERVICE,
  431. input_ctx_path,
  432. IBUS_INPUT_INTERFACE,
  433. "ProcessKeyEvent");
  434. if(msg){
  435. Uint32 mods = IBus_ModState();
  436. dbus->message_append_args(msg,
  437. DBUS_TYPE_UINT32, &keysym,
  438. DBUS_TYPE_UINT32, &keycode,
  439. DBUS_TYPE_UINT32, &mods,
  440. DBUS_TYPE_INVALID);
  441. }
  442. if(msg){
  443. DBusMessage *reply;
  444. reply = dbus->connection_send_with_reply_and_block(ibus_conn, msg, 300, NULL);
  445. if(reply){
  446. if(!dbus->message_get_args(reply, NULL,
  447. DBUS_TYPE_BOOLEAN, &result,
  448. DBUS_TYPE_INVALID)){
  449. result = SDL_FALSE;
  450. }
  451. dbus->message_unref(reply);
  452. }
  453. dbus->message_unref(msg);
  454. }
  455. }
  456. SDL_IBus_UpdateTextRect(NULL);
  457. return result;
  458. }
  459. void
  460. SDL_IBus_UpdateTextRect(SDL_Rect *rect)
  461. {
  462. if(rect){
  463. SDL_memcpy(&ibus_cursor_rect, rect, sizeof(ibus_cursor_rect));
  464. }
  465. SDL_Window *focused_win = SDL_GetKeyboardFocus();
  466. if(!focused_win) return;
  467. SDL_SysWMinfo info;
  468. SDL_VERSION(&info.version);
  469. if(!SDL_GetWindowWMInfo(focused_win, &info)) return;
  470. int x = 0, y = 0;
  471. SDL_GetWindowPosition(focused_win, &x, &y);
  472. #if SDL_VIDEO_DRIVER_X11
  473. if(info.subsystem == SDL_SYSWM_X11){
  474. SDL_DisplayData *displaydata =
  475. (SDL_DisplayData *) SDL_GetDisplayForWindow(focused_win)->driverdata;
  476. Display *x_disp = info.info.x11.display;
  477. Window x_win = info.info.x11.window;
  478. int x_screen = displaydata->screen;
  479. Window unused;
  480. X11_XTranslateCoordinates(x_disp, x_win, RootWindow(x_disp, x_screen),
  481. 0, 0, &x, &y, &unused);
  482. }
  483. #endif
  484. x += ibus_cursor_rect.x;
  485. y += ibus_cursor_rect.y;
  486. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  487. if(IBus_CheckConnection(dbus)){
  488. DBusMessage *msg = dbus->message_new_method_call(IBUS_SERVICE,
  489. input_ctx_path,
  490. IBUS_INPUT_INTERFACE,
  491. "SetCursorLocation");
  492. if(msg){
  493. dbus->message_append_args(msg,
  494. DBUS_TYPE_INT32, &x,
  495. DBUS_TYPE_INT32, &y,
  496. DBUS_TYPE_INT32, &ibus_cursor_rect.w,
  497. DBUS_TYPE_INT32, &ibus_cursor_rect.h,
  498. DBUS_TYPE_INVALID);
  499. }
  500. if(msg){
  501. if(dbus->connection_send(ibus_conn, msg, NULL)){
  502. dbus->connection_flush(ibus_conn);
  503. }
  504. dbus->message_unref(msg);
  505. }
  506. }
  507. }
  508. void
  509. SDL_IBus_PumpEvents(void)
  510. {
  511. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  512. if(IBus_CheckConnection(dbus)){
  513. dbus->connection_read_write(ibus_conn, 0);
  514. while(dbus->connection_dispatch(ibus_conn) == DBUS_DISPATCH_DATA_REMAINS){
  515. /* Do nothing, actual work happens in IBus_MessageFilter */
  516. }
  517. }
  518. }
  519. #endif