wl_text_input.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * wl_text_input.c
  3. * Copyright (C) 2021 Kovid Goyal <kovid at kovidgoyal.net>
  4. *
  5. * Distributed under terms of the GPL3 license.
  6. */
  7. #include "wl_text_input.h"
  8. #include "internal.h"
  9. #include "wayland-text-input-unstable-v3-client-protocol.h"
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #define debug debug_input
  13. static struct zwp_text_input_v3* text_input;
  14. static struct zwp_text_input_manager_v3* text_input_manager;
  15. static char *pending_pre_edit = NULL;
  16. static char *current_pre_edit = NULL;
  17. static char *pending_commit = NULL;
  18. static bool ime_focused = false;
  19. static int last_cursor_left = 0, last_cursor_top = 0, last_cursor_width = 0, last_cursor_height = 0;
  20. uint32_t commit_serial = 0;
  21. static void commit(void) {
  22. if (text_input) {
  23. zwp_text_input_v3_commit (text_input);
  24. commit_serial++;
  25. }
  26. }
  27. static void
  28. text_input_enter(void *data UNUSED, struct zwp_text_input_v3 *txt_input, struct wl_surface *surface UNUSED) {
  29. debug("text-input: enter event\n");
  30. if (txt_input) {
  31. ime_focused = true;
  32. zwp_text_input_v3_enable(txt_input);
  33. zwp_text_input_v3_set_content_type(txt_input, ZWP_TEXT_INPUT_V3_CONTENT_HINT_NONE, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_TERMINAL);
  34. commit();
  35. }
  36. }
  37. static void
  38. text_input_leave(void *data UNUSED, struct zwp_text_input_v3 *txt_input, struct wl_surface *surface UNUSED) {
  39. debug("text-input: leave event\n");
  40. if (txt_input) {
  41. ime_focused = false;
  42. zwp_text_input_v3_disable(txt_input);
  43. commit();
  44. }
  45. }
  46. static void
  47. send_text(const char *text, GLFWIMEState ime_state) {
  48. _GLFWwindow *w = _glfwFocusedWindow();
  49. if (w && w->callbacks.keyboard) {
  50. GLFWkeyevent fake_ev = {.action = text ? GLFW_PRESS : GLFW_RELEASE};
  51. fake_ev.text = text;
  52. fake_ev.ime_state = ime_state;
  53. w->callbacks.keyboard((GLFWwindow*) w, &fake_ev);
  54. }
  55. }
  56. static void
  57. text_input_preedit_string(
  58. void *data UNUSED,
  59. struct zwp_text_input_v3 *txt_input UNUSED,
  60. const char *text,
  61. int32_t cursor_begin,
  62. int32_t cursor_end
  63. ) {
  64. debug("text-input: preedit_string event: text: %s cursor_begin: %d cursor_end: %d\n", text, cursor_begin, cursor_end);
  65. free(pending_pre_edit);
  66. pending_pre_edit = text ? _glfw_strdup(text) : NULL;
  67. }
  68. static void
  69. text_input_commit_string(void *data UNUSED, struct zwp_text_input_v3 *txt_input UNUSED, const char *text) {
  70. debug("text-input: commit_string event: text: %s\n", text);
  71. free(pending_commit);
  72. pending_commit = text ? _glfw_strdup(text) : NULL;
  73. }
  74. static void
  75. text_input_delete_surrounding_text(
  76. void *data UNUSED,
  77. struct zwp_text_input_v3 *txt_input UNUSED,
  78. uint32_t before_length,
  79. uint32_t after_length) {
  80. debug("text-input: delete_surrounding_text event: before_length: %u after_length: %u\n", before_length, after_length);
  81. }
  82. static void
  83. text_input_done(void *data UNUSED, struct zwp_text_input_v3 *txt_input UNUSED, uint32_t serial) {
  84. debug("text-input: done event: serial: %u current_commit_serial: %u\n", serial, commit_serial);
  85. const bool bad_event = serial != commit_serial;
  86. // See https://wayland.app/protocols/text-input-unstable-v3#zwp_text_input_v3:event:done
  87. // for handling of bad events. As best as I can tell spec says we perform all client side actions as usual
  88. // but send nothing back to the compositor, aka no cursor position update.
  89. // See https://github.com/kovidgoyal/kitty/pull/7283 for discussion
  90. if ((pending_pre_edit == NULL && current_pre_edit == NULL) ||
  91. (pending_pre_edit && current_pre_edit && strcmp(pending_pre_edit, current_pre_edit) == 0)) {
  92. free(pending_pre_edit); pending_pre_edit = NULL;
  93. } else {
  94. free(current_pre_edit);
  95. current_pre_edit = pending_pre_edit;
  96. pending_pre_edit = NULL;
  97. if (current_pre_edit) {
  98. send_text(current_pre_edit, bad_event ? GLFW_IME_WAYLAND_DONE_EVENT : GLFW_IME_PREEDIT_CHANGED);
  99. } else {
  100. // Clear pre-edit text
  101. send_text(NULL, GLFW_IME_WAYLAND_DONE_EVENT);
  102. }
  103. }
  104. if (pending_commit) {
  105. send_text(pending_commit, GLFW_IME_COMMIT_TEXT);
  106. free(pending_commit); pending_commit = NULL;
  107. }
  108. }
  109. void
  110. _glfwWaylandBindTextInput(struct wl_registry* registry, uint32_t name) {
  111. if (!text_input_manager && _glfw.hints.init.wl.ime) text_input_manager = wl_registry_bind(registry, name, &zwp_text_input_manager_v3_interface, 1);
  112. }
  113. void
  114. _glfwWaylandInitTextInput(void) {
  115. static const struct zwp_text_input_v3_listener text_input_listener = {
  116. .enter = text_input_enter,
  117. .leave = text_input_leave,
  118. .preedit_string = text_input_preedit_string,
  119. .commit_string = text_input_commit_string,
  120. .delete_surrounding_text = text_input_delete_surrounding_text,
  121. .done = text_input_done,
  122. };
  123. if (_glfw.hints.init.wl.ime && !text_input && text_input_manager && _glfw.wl.seat) {
  124. text_input = zwp_text_input_manager_v3_get_text_input(text_input_manager, _glfw.wl.seat);
  125. if (text_input) zwp_text_input_v3_add_listener(text_input, &text_input_listener, NULL);
  126. }
  127. }
  128. void
  129. _glfwWaylandDestroyTextInput(void) {
  130. if (text_input) zwp_text_input_v3_destroy(text_input);
  131. if (text_input_manager) zwp_text_input_manager_v3_destroy(text_input_manager);
  132. text_input = NULL; text_input_manager = NULL;
  133. free(pending_pre_edit); pending_pre_edit = NULL;
  134. free(current_pre_edit); current_pre_edit = NULL;
  135. free(pending_commit); pending_commit = NULL;
  136. }
  137. void
  138. _glfwPlatformUpdateIMEState(_GLFWwindow *w, const GLFWIMEUpdateEvent *ev) {
  139. if (!text_input) return;
  140. switch(ev->type) {
  141. case GLFW_IME_UPDATE_FOCUS:
  142. debug("\ntext-input: updating IME focus state, ime_focused: %d ev->focused: %d\n", ime_focused, ev->focused);
  143. if (ime_focused) {
  144. zwp_text_input_v3_enable(text_input);
  145. zwp_text_input_v3_set_content_type(text_input, ZWP_TEXT_INPUT_V3_CONTENT_HINT_NONE, ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_TERMINAL);
  146. } else {
  147. free(pending_pre_edit); pending_pre_edit = NULL;
  148. if (current_pre_edit) {
  149. // Clear pre-edit text
  150. send_text(NULL, GLFW_IME_PREEDIT_CHANGED);
  151. free(current_pre_edit); current_pre_edit = NULL;
  152. }
  153. if (pending_commit) {
  154. free(pending_commit); pending_commit = NULL;
  155. }
  156. zwp_text_input_v3_disable(text_input);
  157. }
  158. commit();
  159. break;
  160. case GLFW_IME_UPDATE_CURSOR_POSITION: {
  161. const double scale = _glfwWaylandWindowScale(w);
  162. #define s(x) (int)round((x) / scale)
  163. const int left = s(ev->cursor.left), top = s(ev->cursor.top), width = s(ev->cursor.width), height = s(ev->cursor.height);
  164. #undef s
  165. if (left != last_cursor_left || top != last_cursor_top || width != last_cursor_width || height != last_cursor_height) {
  166. last_cursor_left = left;
  167. last_cursor_top = top;
  168. last_cursor_width = width;
  169. last_cursor_height = height;
  170. debug("\ntext-input: updating cursor position: left=%d top=%d width=%d height=%d\n", left, top, width, height);
  171. zwp_text_input_v3_set_cursor_rectangle(text_input, left, top, width, height);
  172. commit();
  173. }
  174. }
  175. break;
  176. }
  177. }