From 427735fcd9a11c3a3cdcfa5f48cffb55cf7a7b27 Mon Sep 17 00:00:00 2001 From: Dorota Czaplejewicz Date: Thu, 29 Mar 2018 19:41:33 +0200 Subject: [PATCH 1/4] wlroots: add support for zwp_text_input_unstable_v3 --- examples/meson.build | 4 + examples/text-input.c | 394 +++++++++++++++++++++++ include/wlr/types/meson.build | 1 + include/wlr/types/wlr_text_input_v3.h | 93 ++++++ protocol/meson.build | 2 + protocol/text-input-unstable-v3.xml | 441 ++++++++++++++++++++++++++ types/meson.build | 1 + types/wlr_text_input_v3.c | 331 +++++++++++++++++++ 8 files changed, 1267 insertions(+) create mode 100644 examples/text-input.c create mode 100644 include/wlr/types/wlr_text_input_v3.h create mode 100644 protocol/text-input-unstable-v3.xml create mode 100644 types/wlr_text_input_v3.c diff --git a/examples/meson.build b/examples/meson.build index 86c0ddbb..ac60e5f9 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -94,6 +94,10 @@ examples = { 'src': 'toplevel-decoration.c', 'dep': [wayland_client, wlr_protos, wlroots], }, + 'text-input': { + 'src': 'text-input.c', + 'dep': [wayland_cursor, wayland_client, wlr_protos, wlroots], + }, } foreach name, info : examples diff --git a/examples/text-input.c b/examples/text-input.c new file mode 100644 index 00000000..3ccd99a0 --- /dev/null +++ b/examples/text-input.c @@ -0,0 +1,394 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include +#include +#include +#include "text-input-unstable-v3-client-protocol.h" +#include "xdg-shell-client-protocol.h" + +const char usage[] = "Usage: text-input [seconds [width height]]\n\ +\n\ +Creates a xdg-toplevel using the text-input protocol.\n\ +It will be solid black when it has no text input focus, yellow when it\n\ +has focus, and red when it was notified that the focus moved away\n\ +but still didn't give up the text input ability.\n\ +\n\ +The \"seconds\" argument is optional and defines the delay between getting\n\ +notified of lost focus and releasing text input.\n\ +\n\ +The \"width\" and \"height\" arguments define the window shape.\n\ +\n\ +The console will print the internal state of the text field:\n\ +- the text in the 1st line\n\ +- \".\" under each preedit character\n\ +- \"_\" under each selected preedit character\n\ +- \"|\" at the cursor position if there are no selected characters in the\n\ +preedit.\n\ +\n\ +The cursor positions may be inaccurate, especially in presence of zero-width\n\ +characters or non-monospaced fonts.\n"; + +struct text_input_state { + char *commit; + struct { + char *text; + int32_t cursor_begin; + int32_t cursor_end; + } preedit; + struct { + uint32_t after_length; + uint32_t before_length; + } delete_surrounding; +}; + +static struct text_input_state pending = {0}; +static struct text_input_state current = {0}; +static bool entered = false; +static uint32_t serial; +static char *buffer; // text buffer +// cursor is not present, there's no way to move it outside of preedit + +static int sleeptime = 0; +static int width = 100, height = 200; +static int enabled = 0; + +static struct wl_display *display = NULL; +static struct wl_compositor *compositor = NULL; +static struct wl_seat *seat = NULL; +static struct xdg_wm_base *wm_base = NULL; +static struct zwp_text_input_manager_v3 *text_input_manager = NULL; +static struct zwp_text_input_v3 *text_input = NULL; + +struct wlr_egl egl; +struct wl_egl_window *egl_window; +struct wlr_egl_surface *egl_surface; + +static void draw(void) { + eglMakeCurrent(egl.display, egl_surface, egl_surface, egl.context); + + float color[] = {1.0, 1.0, 0.0, 1.0}; + color[0] = enabled * 1.0; + color[1] = entered * 1.0; + + glViewport(0, 0, width, height); + glClearColor(color[0], color[1], color[2], 1.0); + glClear(GL_COLOR_BUFFER_BIT); + + eglSwapBuffers(egl.display, egl_surface); +} + +static size_t utf8_strlen(char *str) { + size_t cp_count = 0; + for (; *str != '\0'; str++) { + if ((*str & 0xc0) != 0x80) { + cp_count++; + } + } + return cp_count; +} + +static size_t utf8_offset(char *utf8_str, size_t byte_offset) { + size_t cp_count = 0; + for (char *c = utf8_str; c < utf8_str + byte_offset; c++) { + if ((*c & 0xc0) != 0x80) { + cp_count++; + } + } + return cp_count; +} + +// TODO: would be nicer to have this text display inside the window +static void show_status() { + printf("State %d:", serial); + if (!enabled) { + printf(" disabled"); + } + + char *preedit_text = current.preedit.text; + if (!preedit_text) { + preedit_text = ""; + } + + printf("\n"); + printf("%s", buffer); + printf("%s\n", preedit_text); + + // Positioning of the cursor requires UTF8 offsets to match monospaced + // glyphs + for (unsigned i = 0; i < utf8_strlen(buffer); i++) { + printf(" "); + } + char *cursor_mark = calloc(utf8_strlen(preedit_text) + 2, sizeof(char)); + for (unsigned i = 0; i < utf8_strlen(preedit_text); i++) { + cursor_mark[i] = '.'; + } + if (current.preedit.cursor_begin == -1 + && current.preedit.cursor_end == -1) { + goto end; + } + if (current.preedit.cursor_begin == -1 + || current.preedit.cursor_end == -1) { + printf("Only one cursor side is defined: %d to %d\n", + current.preedit.cursor_begin, current.preedit.cursor_end); + goto end; + } + + if ((unsigned)current.preedit.cursor_begin > strlen(preedit_text) + || (unsigned)current.preedit.cursor_begin > strlen(preedit_text)) { + printf("Cursor out of bounds\n"); + goto end; + } + + if (current.preedit.cursor_begin == current.preedit.cursor_end) { + cursor_mark[utf8_offset(preedit_text, current.preedit.cursor_begin)] + = '|'; + goto print; + } + + if (current.preedit.cursor_begin > current.preedit.cursor_end) { + printf("End cursor is before start cursor\n"); + goto end; + } + + // negative offsets already checked before + for (unsigned i = utf8_offset(preedit_text, current.preedit.cursor_begin); + i < utf8_offset(preedit_text, current.preedit.cursor_end); i++) { + cursor_mark[i] = '_'; + } +print: + printf("%s\n", cursor_mark); +end: + free(cursor_mark); +} + +static void commit(struct zwp_text_input_v3 *text_input) { + zwp_text_input_v3_commit(text_input); + serial++; +} + +static void send_status_update(struct zwp_text_input_v3 *text_input) { + zwp_text_input_v3_set_surrounding_text(text_input, buffer, strlen(buffer), strlen(buffer)); + zwp_text_input_v3_set_text_change_cause(text_input, ZWP_TEXT_INPUT_V3_CHANGE_CAUSE_INPUT_METHOD); + commit(text_input); +} + +static void text_input_handle_enter(void *data, + struct zwp_text_input_v3 *zwp_text_input_v3, + struct wl_surface *surface) { + entered = true; + zwp_text_input_v3_enable(zwp_text_input_v3); + commit(zwp_text_input_v3); + enabled = true; + draw(); + show_status(); +} + +static void text_input_handle_leave(void *data, + struct zwp_text_input_v3 *zwp_text_input_v3, + struct wl_surface *surface) { + entered = false; + draw(); + wl_display_roundtrip(display); + sleep(sleeptime); + zwp_text_input_v3_disable(zwp_text_input_v3); + commit(zwp_text_input_v3); + enabled = false; + draw(); + show_status(); +} + +static void text_input_commit_string(void *data, + struct zwp_text_input_v3 *zwp_text_input_v3, + const char *text) { + free(pending.commit); + pending.commit = strdup(text); +} + +static void text_input_delete_surrounding_text(void *data, + struct zwp_text_input_v3 *zwp_text_input_v3, + uint32_t before_length, uint32_t after_length) { + pending.delete_surrounding.before_length = before_length; + pending.delete_surrounding.after_length = after_length; +} + +static void text_input_preedit_string(void *data, + struct zwp_text_input_v3 *zwp_text_input_v3, + const char *text, int32_t cursor_begin, int32_t cursor_end) { + free(pending.preedit.text); + pending.preedit.text = strdup(text); + pending.preedit.cursor_begin = cursor_begin; + pending.preedit.cursor_end = cursor_end; +} + +static void text_input_handle_done(void *data, + struct zwp_text_input_v3 *zwp_text_input_v3, + uint32_t incoming_serial) { + if (serial != incoming_serial) { + fprintf(stderr, "Received serial %d while expecting %d\n", incoming_serial, serial); + return; + } + free(current.preedit.text); + free(current.commit); + current = pending; + struct text_input_state empty = {0}; + pending = empty; + + if (current.delete_surrounding.after_length + current.delete_surrounding.before_length > 0) { + // cursor is always after committed text, after_length != 0 will never happen + unsigned delete_before = current.delete_surrounding.before_length; + if (delete_before > strlen(buffer)) { + delete_before = strlen(buffer); + } + buffer[strlen(buffer) - delete_before] = '\0'; + } + + char *commit_string = current.commit; + if (!commit_string) { + commit_string = ""; + } + char *old_buffer = buffer; + buffer = calloc(strlen(buffer) + strlen(commit_string) + 1, sizeof(char)); // realloc may fail anyway + strcpy(buffer, old_buffer); + free(old_buffer); + strcat(buffer, commit_string); + + send_status_update(zwp_text_input_v3); + show_status(); +} + +static const struct zwp_text_input_v3_listener text_input_listener = { + .enter = text_input_handle_enter, + .leave = text_input_handle_leave, + .commit_string = text_input_commit_string, + .delete_surrounding_text = text_input_delete_surrounding_text, + .preedit_string = text_input_preedit_string, + .done = text_input_handle_done, +}; + +static void xdg_surface_handle_configure(void *data, + struct xdg_surface *xdg_surface, uint32_t serial) { + xdg_surface_ack_configure(xdg_surface, serial); + wl_egl_window_resize(egl_window, width, height, 0, 0); + draw(); +} + +static const struct xdg_surface_listener xdg_surface_listener = { + .configure = xdg_surface_handle_configure, +}; + +static void xdg_toplevel_handle_configure(void *data, + struct xdg_toplevel *xdg_toplevel, int32_t w, int32_t h, + struct wl_array *states) { + width = w; + height = h; +} + +static void xdg_toplevel_handle_close(void *data, + struct xdg_toplevel *xdg_toplevel) { + exit(EXIT_SUCCESS); +} + +static const struct xdg_toplevel_listener xdg_toplevel_listener = { + .configure = xdg_toplevel_handle_configure, + .close = xdg_toplevel_handle_close, +}; + +static void handle_global(void *data, struct wl_registry *registry, + uint32_t name, const char *interface, uint32_t version) { + if (strcmp(interface, "wl_compositor") == 0) { + compositor = wl_registry_bind(registry, name, + &wl_compositor_interface, 1); + } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) { + wm_base = wl_registry_bind(registry, name, &xdg_wm_base_interface, 1); + } else if (strcmp(interface, zwp_text_input_manager_v3_interface.name) == 0) { + text_input_manager = wl_registry_bind(registry, name, + &zwp_text_input_manager_v3_interface, 1); + } else if (strcmp(interface, wl_seat_interface.name) == 0) { + seat = wl_registry_bind(registry, name, &wl_seat_interface, version); + } +} + +static void handle_global_remove(void *data, struct wl_registry *registry, + uint32_t name) { + // who cares +} + +static const struct wl_registry_listener registry_listener = { + .global = handle_global, + .global_remove = handle_global_remove, +}; + +int main(int argc, char **argv) { + if (argc > 1) { + if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) { + printf(usage); + return 0; + } + sleeptime = atoi(argv[1]); + if (argc > 3) { + width = atoi(argv[2]); + height = atoi(argv[3]); + } + } + + buffer = calloc(1, sizeof(char)); + + display = wl_display_connect(NULL); + if (display == NULL) { + fprintf(stderr, "Failed to create display\n"); + return EXIT_FAILURE; + } + + struct wl_registry *registry = wl_display_get_registry(display); + wl_registry_add_listener(registry, ®istry_listener, NULL); + wl_display_dispatch(display); + wl_display_roundtrip(display); + + if (compositor == NULL) { + fprintf(stderr, "wl-compositor not available\n"); + return EXIT_FAILURE; + } + if (wm_base == NULL) { + fprintf(stderr, "xdg-shell not available\n"); + return EXIT_FAILURE; + } + if (text_input_manager == NULL) { + fprintf(stderr, "text-input not available\n"); + return EXIT_FAILURE; + } + + text_input = zwp_text_input_manager_v3_get_text_input(text_input_manager, seat); + + zwp_text_input_v3_add_listener(text_input, &text_input_listener, NULL); + + + wlr_egl_init(&egl, EGL_PLATFORM_WAYLAND_EXT, display, NULL, + WL_SHM_FORMAT_ARGB8888); + + struct wl_surface *surface = wl_compositor_create_surface(compositor); + struct xdg_surface *xdg_surface = + xdg_wm_base_get_xdg_surface(wm_base, surface); + struct xdg_toplevel *xdg_toplevel = xdg_surface_get_toplevel(xdg_surface); + + xdg_surface_add_listener(xdg_surface, &xdg_surface_listener, NULL); + xdg_toplevel_add_listener(xdg_toplevel, &xdg_toplevel_listener, NULL); + + wl_surface_commit(surface); + + egl_window = wl_egl_window_create(surface, width, height); + egl_surface = wlr_egl_create_surface(&egl, egl_window); + + wl_display_roundtrip(display); + + draw(); + + while (wl_display_dispatch(display) != -1) { + // This space intentionally left blank + } + + return EXIT_SUCCESS; +} diff --git a/include/wlr/types/meson.build b/include/wlr/types/meson.build index 6a8955c3..e8de124a 100644 --- a/include/wlr/types/meson.build +++ b/include/wlr/types/meson.build @@ -30,6 +30,7 @@ install_headers( 'wlr_tablet_pad.h', 'wlr_tablet_tool.h', 'wlr_tablet_v2.h', + 'wlr_text_input_v3.h', 'wlr_touch.h', 'wlr_virtual_keyboard_v1.h', 'wlr_wl_shell.h', diff --git a/include/wlr/types/wlr_text_input_v3.h b/include/wlr/types/wlr_text_input_v3.h new file mode 100644 index 00000000..0db0cf47 --- /dev/null +++ b/include/wlr/types/wlr_text_input_v3.h @@ -0,0 +1,93 @@ +/* + * This an unstable interface of wlroots. No guarantees are made regarding the + * future consistency of this API. + */ +#ifndef WLR_USE_UNSTABLE +#error "Add -DWLR_USE_UNSTABLE to enable unstable wlroots features" +#endif + +#ifndef WLR_TYPES_WLR_TEXT_INPUT_V3_H +#define WLR_TYPES_WLR_TEXT_INPUT_V3_H + +#include +#include +#include + +struct wlr_text_input_v3_state { + struct { + char *text; // NULL is allowed and equivalent to empty string + uint32_t cursor; + uint32_t anchor; + } surrounding; + + uint32_t text_change_cause; + + struct { + uint32_t hint; + uint32_t purpose; + } content_type; + + struct { + int32_t x; + int32_t y; + int32_t width; + int32_t height; + } cursor_rectangle; +}; + +struct wlr_text_input_v3 { + struct wlr_seat *seat; // becomes null when seat destroyed + struct wl_resource *resource; + struct wlr_surface *focused_surface; + struct wlr_text_input_v3_state pending; + struct wlr_text_input_v3_state current; + uint32_t current_serial; // next in line to send + bool pending_enabled; + bool current_enabled; + + struct wl_list link; + + struct wl_listener surface_destroy; + struct wl_listener seat_destroy; + + struct { + struct wl_signal enable; // (struct wlr_text_input_v3*) + struct wl_signal commit; // (struct wlr_text_input_v3*) + struct wl_signal disable; // (struct wlr_text_input_v3*) + struct wl_signal destroy; // (struct wlr_text_input_v3*) + } events; +}; + +struct wlr_text_input_manager_v3 { + struct wl_global *global; + + struct wl_list bound_resources; // struct wl_resource*::link + struct wl_list text_inputs; // struct wlr_text_input_v3::resource::link + + struct wl_listener display_destroy; + + struct { + struct wl_signal text_input; // (struct wlr_text_input_v3*) + struct wl_signal destroy; // (struct wlr_input_method_manager_v3*) + } events; +}; + +struct wlr_text_input_manager_v3 *wlr_text_input_manager_v3_create( + struct wl_display *wl_display); +void wlr_text_input_manager_v3_destroy( + struct wlr_text_input_manager_v3 *manager); + +// Sends enter to the surface and saves it +void wlr_text_input_v3_send_enter(struct wlr_text_input_v3 *text_input, + struct wlr_surface *wlr_surface); +// Sends leave to the currently focused surface and clears it +void wlr_text_input_v3_send_leave(struct wlr_text_input_v3 *text_input); +void wlr_text_input_v3_send_preedit_string(struct wlr_text_input_v3 *text_input, + const char *text, uint32_t cursor_begin, uint32_t cursor_end); +void wlr_text_input_v3_send_commit_string(struct wlr_text_input_v3 *text_input, + const char *text); +void wlr_text_input_v3_send_delete_surrounding_text( + struct wlr_text_input_v3 *text_input, uint32_t before_length, + uint32_t after_length); +void wlr_text_input_v3_send_done(struct wlr_text_input_v3 *text_input); +#endif diff --git a/protocol/meson.build b/protocol/meson.build index e03b88e8..9c810367 100644 --- a/protocol/meson.build +++ b/protocol/meson.build @@ -24,6 +24,7 @@ protocols = [ 'idle.xml', 'screenshooter.xml', 'server-decoration.xml', + 'text-input-unstable-v3.xml', 'virtual-keyboard-unstable-v1.xml', 'wlr-export-dmabuf-unstable-v1.xml', 'wlr-gamma-control-unstable-v1.xml', @@ -40,6 +41,7 @@ client_protocols = [ [wl_protocol_dir, 'unstable/pointer-constraints/pointer-constraints-unstable-v1.xml'], 'idle.xml', 'screenshooter.xml', + 'text-input-unstable-v3.xml', 'wlr-export-dmabuf-unstable-v1.xml', 'wlr-gamma-control-unstable-v1.xml', 'wlr-input-inhibitor-unstable-v1.xml', diff --git a/protocol/text-input-unstable-v3.xml b/protocol/text-input-unstable-v3.xml new file mode 100644 index 00000000..8b710fd6 --- /dev/null +++ b/protocol/text-input-unstable-v3.xml @@ -0,0 +1,441 @@ + + + + + Copyright © 2012, 2013 Intel Corporation + Copyright © 2015, 2016 Jan Arne Petersen + Copyright © 2017, 2018 Red Hat, Inc. + Copyright © 2018 Purism SPC + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that copyright notice and this permission + notice appear in supporting documentation, and that the name of + the copyright holders not be used in advertising or publicity + pertaining to distribution of the software without specific, + written prior permission. The copyright holders make no + representations about the suitability of this software for any + purpose. It is provided "as is" without express or implied + warranty. + + THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN + AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, + ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF + THIS SOFTWARE. + + + + This protocol allows compositors to act as input methods and to send text + to applications. A text input object is used to manage state of what are + typically text entry fields in the application. + + This document adheres to the RFC 2119 when using words like "must", + "should", "may", etc. + + Warning! The protocol described in this file is experimental and + backward incompatible changes may be made. Backward compatible changes + may be added together with the corresponding interface version bump. + Backward incompatible changes are done by bumping the version number in + the protocol and interface names and resetting the interface version. + Once the protocol is to be declared stable, the 'z' prefix and the + version number in the protocol and interface names are removed and the + interface version number is reset. + + + + + The zwp_text_input_v3 interface represents text input and input methods + associated with a seat. It provides enter/leave events to follow the + text input focus for a seat. + + Requests are used to enable/disable the text-input object and set + state information like surrounding and selected text or the content type. + The information about the entered text is sent to the text-input object + via the preedit_string and commit_string events. + + Text is valid UTF-8 encoded, indices and lengths are in bytes. Indices + must not point to middle bytes inside a code point: they must either + point to the first byte of a code point or to the end of the buffer. + Lengths must be measured between two valid indices. + + Focus moving throughout surfaces will result in the emission of + zwp_text_input_v3.enter and zwp_text_input_v3.leave events. The focused + surface must commit zwp_text_input_v3.enable and + zwp_text_input_v3.disable requests as the keyboard focus moves across + editable and non-editable elements of the UI. Those two requests are not + expected to be paired with each other, the compositor must be able to + handle consecutive series of the same request. + + State is sent by the state requests (set_surrounding_text, + set_content_type and set_cursor_rectangle) and a commit request. After an + enter event or disable request all state information is invalidated and + needs to be resent by the client. + + + + + Destroy the wp_text_input object. Also disables all surfaces enabled + through this wp_text_input object. + + + + + + Requests text input on the surface previously obtained from the enter + event. + + This request must be issued every time the active text input changes + to a new one, including within the current surface. Use + zwp_text_input_v3.disable when there is no longer any input focus on + the current surface. + + This request resets all state associated with previous enable, disable, + set_surrounding_text, set_text_change_cause, set_content_type, and + set_cursor_rectangle requests, as well as the state associated with + preedit_string, commit_string, and delete_surrounding_text events. + + The set_surrounding_text, set_content_type and set_cursor_rectangle + requests must follow if the text input supports the necessary + functionality. + + State set with this request is double-buffered. It will get applied on + the next zwp_text_input_v3.commit request, and stay valid until the + next committed enable or disable request. + + The changes must be applied by the compositor after issuing a + zwp_text_input_v3.commit request. + + + + + + Explicitly disable text input on the current surface (typically when + there is no focus on any text entry inside the surface). + + State set with this request is double-buffered. It will get applied on + the next zwp_text_input_v3.commit request. + + + + + + Sets the surrounding plain text around the input, excluding the preedit + text. + + The client should notify the compositor of any changes in any of the + values carried with this request, including changes caused by handling + incoming text-input events as well as changes caused by other + mechanisms like keyboard typing. + + If the client is unaware of the text around the cursor, it should not + issue this request, to signify lack of support to the compositor. + + Text is UTF-8 encoded, and should include the cursor position, the + complete selection and additional characters before and after them. + There is a maximum length of wayland messages, so text can not be + longer than 4000 bytes. + + Cursor is the byte offset of the cursor within text buffer. + + Anchor is the byte offset of the selection anchor within text buffer. + If there is no selected text, anchor is the same as cursor. + + If any preedit text is present, it is replaced with a cursor for the + purpose of this event. + + Values set with this request are double-buffered. They will get applied + on the next zwp_text_input_v3.commit request, and stay valid until the + next committed enable or disable request. + + The initial state for affected fields is empty, meaning that the text + input does not support sending surrounding text. If the empty values + get applied, subsequent attempts to change them may have no effect. + + + + + + + + + Reason for the change of surrounding text or cursor posision. + + + + + + + + Tells the compositor why the text surrounding the cursor changed. + + Whenever the client detects an external change in text, cursor, or + anchor posision, it must issue this request to the compositor. This + request is intended to give the input method a chance to update the + preedit text in an appropriate way, e.g. by removing it when the user + starts typing with a keyboard. + + cause describes the source of the change. + + The value set with this request is double-buffered. It must be applied + and reset to initial at the next zwp_text_input_v3.commit request. + + The initial value of cause is input_method. + + + + + + + Content hint is a bitmask to allow to modify the behavior of the text + input. + + + + + + + + + + + + + + + + + The content purpose allows to specify the primary purpose of a text + input. + + This allows an input method to show special purpose input panels with + extra characters or to disallow some characters. + + + + + + + + + + + + + + + + + + + + Sets the content purpose and content hint. While the purpose is the + basic purpose of an input field, the hint flags allow to modify some of + the behavior. + + Values set with this request are double-buffered. They will get applied + on the next zwp_text_input_v3.commit request. + Subsequent attempts to update them may have no effect. The values + remain valid until the next committed enable or disable request. + + The initial value for hint is none, and the initial value for purpose + is normal. + + + + + + + + Marks an area around the cursor as a x, y, width, height rectangle in + surface local coordinates. + + Allows the compositor to put a window with word suggestions near the + cursor, without obstructing the text being input. + + If the client is unaware of the position of edited text, it should not + issue this request, to signify lack of support to the compositor. + + Values set with this request are double-buffered. They will get applied + on the next zwp_text_input_v3.commit request, and stay valid until the + next committed enable or disable request. + + The initial values describing a cursor rectangle are empty. That means + the text input does not support describing the cursor area. If the + empty values get applied, subsequent attempts to change them may have + no effect. + + + + + + + + + + Atomically applies state changes recently sent to the compositor. + + The commit request establishes and updates the state of the client, and + must be issued after any changes to apply them. + + Text input state (enabled status, content purpose, content hint, + surrounding text and change cause, cursor rectangle) is conceptually + double-buffered within the context of a text input, i.e. between a + committed enable request and the following committed enable or disable + request. + + Protocol requests modify the pending state, as opposed to the current + state in use by the input method. A commit request atomically applies + all pending state, replacing the current state. After commit, the new + pending state is as documented for each related request. + + Requests are applied in the order of arrival. + + Neither current nor pending state are modified unless noted otherwise. + + The compositor must count the number of commit requests coming from + each zwp_text_input_v3 object and use the count as the serial in done + events. + + + + + + Notification that this seat's text-input focus is on a certain surface. + + When the seat has the keyboard capability the text-input focus follows + the keyboard focus. This event sets the current surface for the + text-input object. + + + + + + + Notification that this seat's text-input focus is no longer on a + certain surface. The client should reset any preedit string previously + set. + + The leave notification clears the current surface. It is sent before + the enter notification for the new focus. + + When the seat has the keyboard capability the text-input focus follows + the keyboard focus. + + + + + + + Notify when a new composing text (pre-edit) should be set at the + current cursor position. Any previously set composing text must be + removed. Any previously existing selected text must be removed. + + The argument text contains the pre-edit string buffer. + + The parameters cursor_begin and cursor_end are counted in bytes + relative to the beginning of the submitted text buffer. Cursor should + be hidden when both are equal to -1. + + They could be represented by the client as a line if both values are + the same, or as a text highlight otherwise. + + Values set with this event are double-buffered. They must be applied + and reset to initial on the next zwp_text_input_v3.done event. + + The initial value of text is an empty string, and cursor_begin, + cursor_end and cursor_hidden are all 0. + + + + + + + + + Notify when text should be inserted into the editor widget. The text to + commit could be either just a single character after a key press or the + result of some composing (pre-edit). + + Values set with this event are double-buffered. They must be applied + and reset to initial on the next zwp_text_input_v3.done event. + + The initial value of text is an empty string. + + + + + + + Notify when the text around the current cursor position should be + deleted. + + Before_length and after_length are the number of bytes before and after + the current cursor index (excluding the selection) to delete. + + If a preedit text is present, in effect before_length is counted from + the beginning of it, and after_length from its end (see done event + sequence). + + Values set with this event are double-buffered. They must be applied + and reset to initial on the next zwp_text_input_v3.done event. + + The initial values of both before_length and after_length are 0. + + + + + + + + Instruct the application to apply changes to state requested by the + preedit_string, commit_string and delete_surrounding_text events. The + state relating to these events is double-buffered, and each one + modifies the pending state. This event replaces the current state with + the pending state. + + The application must proceed by evaluating the changes in the following + order: + + 1. Replace existing preedit string with the cursor. + 2. Delete requested surrounding text. + 3. Insert commit string with the cursor at its end. + 4. Calculate surrounding text to send. + 5. Insert new preedit text in cursor position. + 6. Place cursor inside preedit text. + + The serial number reflects the last state of the zwp_text_input_v3 + object known to the compositor. The value of the serial argument must + be equal to the number of commit requests already issued on that object. + When the client receives a done event with a serial different than the + number of past commit requests, it must proceed as normal, except it + should not change the current state of the zwp_text_input_v3 object. + + + + + + + + A factory for text-input objects. This object is a global singleton. + + + + + Destroy the wp_text_input_manager object. + + + + + + Creates a new text-input object for a given seat. + + + + + + diff --git a/types/meson.build b/types/meson.build index 038f7de4..e5b053aa 100644 --- a/types/meson.build +++ b/types/meson.build @@ -51,6 +51,7 @@ lib_wlr_types = static_library( 'tablet_v2/wlr_tablet_v2_tool.c', 'wlr_tablet_pad.c', 'wlr_tablet_tool.c', + 'wlr_text_input_v3.c', 'wlr_touch.c', 'wlr_virtual_keyboard_v1.c', 'wlr_wl_shell.c', diff --git a/types/wlr_text_input_v3.c b/types/wlr_text_input_v3.c new file mode 100644 index 00000000..eb46d207 --- /dev/null +++ b/types/wlr_text_input_v3.c @@ -0,0 +1,331 @@ +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200809L +#endif +#include +#include +#include +#include +#include +#include "text-input-unstable-v3-protocol.h" +#include "util/signal.h" + +static const struct zwp_text_input_v3_interface text_input_impl; + +static struct wlr_text_input_v3 *text_input_from_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, &zwp_text_input_v3_interface, + &text_input_impl)); + return wl_resource_get_user_data(resource); +} + +void wlr_text_input_v3_send_enter(struct wlr_text_input_v3 *text_input, + struct wlr_surface *surface) { + assert(wl_resource_get_client(text_input->resource) + == wl_resource_get_client(surface->resource)); + text_input->focused_surface = surface; + wl_signal_add(&text_input->focused_surface->events.destroy, + &text_input->surface_destroy); + zwp_text_input_v3_send_enter(text_input->resource, + text_input->focused_surface->resource); +} + +void wlr_text_input_v3_send_leave(struct wlr_text_input_v3 *text_input) { + zwp_text_input_v3_send_leave(text_input->resource, + text_input->focused_surface->resource); + wl_list_remove(&text_input->surface_destroy.link); + text_input->focused_surface = NULL; +} + +void wlr_text_input_v3_send_preedit_string(struct wlr_text_input_v3 *text_input, + const char *text, uint32_t cursor_begin, uint32_t cursor_end) { + zwp_text_input_v3_send_preedit_string(text_input->resource, text, + cursor_begin, cursor_end); +} + +void wlr_text_input_v3_send_commit_string(struct wlr_text_input_v3 *text_input, + const char *text) { + zwp_text_input_v3_send_commit_string(text_input->resource, text); +} + +void wlr_text_input_v3_send_delete_surrounding_text( + struct wlr_text_input_v3 *text_input, uint32_t before_length, + uint32_t after_length) { + zwp_text_input_v3_send_delete_surrounding_text(text_input->resource, + before_length, after_length); +} + +void wlr_text_input_v3_send_done(struct wlr_text_input_v3 *text_input) { + zwp_text_input_v3_send_done(text_input->resource, + text_input->current_serial); +} + +static void wlr_text_input_destroy(struct wlr_text_input_v3 *text_input) { + wlr_signal_emit_safe(&text_input->events.destroy, text_input); + wl_list_remove(&text_input->seat_destroy.link); + // remove from manager::text_inputs + wl_list_remove(&text_input->link); + free(text_input->current.surrounding.text); + free(text_input->pending.surrounding.text); + free(text_input); +} + +static void text_input_resource_destroy(struct wl_resource *resource) { + struct wlr_text_input_v3 *text_input = text_input_from_resource(resource); + if (!text_input) { + return; + } + wlr_text_input_destroy(text_input); +} + +static void text_input_destroy(struct wl_client *client, + struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static void text_input_enable(struct wl_client *client, + struct wl_resource *resource) { + struct wlr_text_input_v3 *text_input = text_input_from_resource(resource); + if (!text_input) { + return; + } + struct wlr_text_input_v3_state defaults = {0}; + free(text_input->pending.surrounding.text); + text_input->pending = defaults; + text_input->pending_enabled = true; +} + +static void text_input_disable(struct wl_client *client, + struct wl_resource *resource) { + struct wlr_text_input_v3 *text_input = text_input_from_resource(resource); + if (!text_input) { + return; + } + text_input->pending_enabled = false; +} + +static void text_input_set_surrounding_text(struct wl_client *client, + struct wl_resource *resource, const char *text, int32_t cursor, + int32_t anchor) { + struct wlr_text_input_v3 *text_input = text_input_from_resource(resource); + if (!text_input) { + return; + } + free(text_input->pending.surrounding.text); + text_input->pending.surrounding.text = strdup(text); + if (!text_input->pending.surrounding.text) { + wl_client_post_no_memory(client); + } + + text_input->pending.surrounding.cursor = cursor; + text_input->pending.surrounding.anchor = anchor; +} + +static void text_input_set_text_change_cause(struct wl_client *client, + struct wl_resource *resource, uint32_t cause) { + struct wlr_text_input_v3 *text_input = text_input_from_resource(resource); + if (!text_input) { + return; + } + text_input->pending.text_change_cause = cause; +} + +static void text_input_set_content_type(struct wl_client *client, + struct wl_resource *resource, uint32_t hint, uint32_t purpose) { + struct wlr_text_input_v3 *text_input = text_input_from_resource(resource); + if (!text_input) { + return; + } + text_input->pending.content_type.hint = hint; + text_input->pending.content_type.purpose = purpose; +} + +static void text_input_set_cursor_rectangle(struct wl_client *client, + struct wl_resource *resource, int32_t x, int32_t y, int32_t width, + int32_t height) { + struct wlr_text_input_v3 *text_input = text_input_from_resource(resource); + if (!text_input) { + return; + } + text_input->pending.cursor_rectangle.x = x; + text_input->pending.cursor_rectangle.y = y; + text_input->pending.cursor_rectangle.width = width; + text_input->pending.cursor_rectangle.height = height; +} + +static void text_input_commit(struct wl_client *client, + struct wl_resource *resource) { + struct wlr_text_input_v3 *text_input = text_input_from_resource(resource); + if (!text_input) { + return; + } + free(text_input->current.surrounding.text); + text_input->current = text_input->pending; + if (text_input->pending.surrounding.text) { + text_input->current.surrounding.text = + strdup(text_input->pending.surrounding.text); + } + + bool old_enabled = text_input->current_enabled; + text_input->current_enabled = text_input->pending_enabled; + text_input->current_serial++; + + if (text_input->current_enabled && text_input->focused_surface == NULL) { + wl_resource_post_error(text_input->resource, 0, "Text input was not" + "entered, and cannot be enabled\n"); + return; + } + if (!old_enabled && text_input->current_enabled) { + wlr_signal_emit_safe(&text_input->events.enable, text_input); + } else if (old_enabled && !text_input->current_enabled) { + wlr_signal_emit_safe(&text_input->events.disable, text_input); + } else { // including never enabled + wlr_signal_emit_safe(&text_input->events.commit, text_input); + } +} + +static const struct zwp_text_input_v3_interface text_input_impl = { + .destroy = text_input_destroy, + .enable = text_input_enable, + .disable = text_input_disable, + .set_surrounding_text = text_input_set_surrounding_text, + .set_text_change_cause = text_input_set_text_change_cause, + .set_content_type = text_input_set_content_type, + .set_cursor_rectangle = text_input_set_cursor_rectangle, + .commit = text_input_commit, +}; + +static const struct zwp_text_input_manager_v3_interface text_input_manager_impl; + +static struct wlr_text_input_manager_v3 *text_input_manager_from_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, + &zwp_text_input_manager_v3_interface, &text_input_manager_impl)); + return wl_resource_get_user_data(resource); +} + +static void text_input_manager_destroy(struct wl_client *client, + struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static void text_input_handle_seat_destroy(struct wl_listener *listener, + void *data) { + struct wlr_text_input_v3 *text_input = wl_container_of(listener, text_input, + seat_destroy); + struct wl_resource *resource = text_input->resource; + wlr_text_input_destroy(text_input); + wl_resource_set_user_data(resource, NULL); +} + +static void text_input_handle_focused_surface_destroy( + struct wl_listener *listener, void *data) { + struct wlr_text_input_v3 *text_input = wl_container_of(listener, text_input, + surface_destroy); + text_input->focused_surface = NULL; +} + +static void text_input_manager_get_text_input(struct wl_client *client, + struct wl_resource *resource, uint32_t id, struct wl_resource *seat) { + struct wlr_text_input_v3 *text_input = + calloc(1, sizeof(struct wlr_text_input_v3)); + if (text_input == NULL) { + wl_client_post_no_memory(client); + return; + } + + wl_signal_init(&text_input->events.enable); + wl_signal_init(&text_input->events.commit); + wl_signal_init(&text_input->events.disable); + wl_signal_init(&text_input->events.destroy); + + int version = wl_resource_get_version(resource); + struct wl_resource *text_input_resource = wl_resource_create(client, + &zwp_text_input_v3_interface, version, id); + if (text_input_resource == NULL) { + free(text_input); + wl_client_post_no_memory(client); + return; + } + text_input->resource = text_input_resource; + + wl_resource_set_implementation(text_input->resource, &text_input_impl, + text_input, text_input_resource_destroy); + + struct wlr_seat_client *seat_client = wlr_seat_client_from_resource(seat); + struct wlr_seat *wlr_seat = seat_client->seat; + text_input->seat = wlr_seat; + wl_signal_add(&seat_client->events.destroy, + &text_input->seat_destroy); + text_input->seat_destroy.notify = + text_input_handle_seat_destroy; + text_input->surface_destroy.notify = + text_input_handle_focused_surface_destroy; + + struct wlr_text_input_manager_v3 *manager = + text_input_manager_from_resource(resource); + wl_list_insert(&manager->text_inputs, &text_input->link); + + wlr_signal_emit_safe(&manager->events.text_input, text_input); +} + +static const struct zwp_text_input_manager_v3_interface + text_input_manager_impl = { + .destroy = text_input_manager_destroy, + .get_text_input = text_input_manager_get_text_input, +}; + +static void text_input_manager_unbind(struct wl_resource *resource) { + wl_list_remove(wl_resource_get_link(resource)); +} + +static void text_input_manager_bind(struct wl_client *wl_client, void *data, + uint32_t version, uint32_t id) { + struct wlr_text_input_manager_v3 *manager = data; + assert(wl_client && manager); + + struct wl_resource *resource = wl_resource_create(wl_client, + &zwp_text_input_manager_v3_interface, version, id); + if (resource == NULL) { + wl_client_post_no_memory(wl_client); + return; + } + wl_list_insert(&manager->bound_resources, wl_resource_get_link(resource)); + wl_resource_set_implementation(resource, &text_input_manager_impl, + manager, text_input_manager_unbind); +} + +struct wlr_text_input_manager_v3 *wlr_text_input_manager_v3_create( + struct wl_display *wl_display) { + struct wlr_text_input_manager_v3 *manager = + calloc(1, sizeof(struct wlr_text_input_manager_v3)); + wl_list_init(&manager->bound_resources); + wl_list_init(&manager->text_inputs); + wl_signal_init(&manager->events.text_input); + manager->global = wl_global_create(wl_display, + &zwp_text_input_manager_v3_interface, 1, manager, + text_input_manager_bind); + if (!manager->global) { + free(manager); + return NULL; + } + return manager; +} + +void wlr_text_input_manager_v3_destroy( + struct wlr_text_input_manager_v3 *manager) { + wlr_signal_emit_safe(&manager->events.destroy, manager); + wl_list_remove(&manager->display_destroy.link); + + struct wl_resource *resource, *resource_tmp; + wl_resource_for_each_safe(resource, resource_tmp, + &manager->bound_resources) { + wl_resource_destroy(resource); + } + struct wlr_text_input_v3 *text_input, *text_input_tmp; + wl_list_for_each_safe(text_input, text_input_tmp, &manager->text_inputs, + link) { + wl_resource_destroy(text_input->resource); + } + wl_global_destroy(manager->global); + free(manager); +} From 174e8a48aaa09908ff3970093e3a0c0d29afaf1b Mon Sep 17 00:00:00 2001 From: Dorota Czaplejewicz Date: Tue, 28 Aug 2018 10:19:57 +0200 Subject: [PATCH 2/4] build: bump wayland-scanner version wayland-scanner >= 1.15.0 accepts foreign struct references, necessary in protocols like zwp-input-method-v2 --- meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meson.build b/meson.build index 9bd71191..178acd9b 100644 --- a/meson.build +++ b/meson.build @@ -42,7 +42,7 @@ endif # Avoid wl_buffer deprecation warnings add_project_arguments('-DWL_HIDE_DEPRECATED', language: 'c') -wayland_server = dependency('wayland-server', version: '>=1.15') +wayland_server = dependency('wayland-server', version: '>=1.16') wayland_client = dependency('wayland-client') wayland_egl = dependency('wayland-egl') wayland_protos = dependency('wayland-protocols', version: '>=1.15') From cec747111946f1d51ae1c9ec14b683aa3d5dcfc4 Mon Sep 17 00:00:00 2001 From: Dorota Czaplejewicz Date: Thu, 23 Aug 2018 11:38:39 +0200 Subject: [PATCH 3/4] wlroots: add basic support for zwp_input_method_v2 Implemented basic input method functionality. Not included: popups, grabbing. --- examples/input-method.c | 400 +++++++++++++++++++ examples/meson.build | 4 + include/wlr/types/meson.build | 3 +- include/wlr/types/wlr_input_method_v2.h | 87 +++++ protocol/input-method-unstable-v2.xml | 490 ++++++++++++++++++++++++ protocol/meson.build | 2 + types/meson.build | 1 + types/wlr_input_method_v2.c | 297 ++++++++++++++ 8 files changed, 1283 insertions(+), 1 deletion(-) create mode 100644 examples/input-method.c create mode 100644 include/wlr/types/wlr_input_method_v2.h create mode 100644 protocol/input-method-unstable-v2.xml create mode 100644 types/wlr_input_method_v2.c diff --git a/examples/input-method.c b/examples/input-method.c new file mode 100644 index 00000000..9c2d5524 --- /dev/null +++ b/examples/input-method.c @@ -0,0 +1,400 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "input-method-unstable-v2-client-protocol.h" +#include "text-input-unstable-v3-client-protocol.h" +#include "xdg-shell-client-protocol.h" + +const char usage[] = "Usage: input-method [seconds]\n\ +\n\ +Creates an input method using the input-method protocol.\n\ +\n\ +Whenever a text input is activated, this program sends a few sequences of\n\ +commands and checks the validity of the responses, relying on returned\n\ +surrounding text.\n\ +\n\ +The \"seconds\" argument is optional and defines the maximum delay between\n\ +stages."; + +struct input_method_state { + enum zwp_text_input_v3_change_cause change_cause; + struct { + enum zwp_text_input_v3_content_hint hint; + enum zwp_text_input_v3_content_purpose purpose; + } content_type; + struct { + char *text; + uint32_t cursor; + uint32_t anchor; + } surrounding; +}; + +static int sleeptime = 0; + +static struct wl_display *display = NULL; +static struct wl_compositor *compositor = NULL; +static struct wl_seat *seat = NULL; +static struct zwp_input_method_manager_v2 *input_method_manager = NULL; +static struct zwp_input_method_v2 *input_method = NULL; + +struct input_method_state pending; +struct input_method_state current; + +static uint32_t serial = 0; +bool active = false; +bool pending_active = false; +bool unavailable = false; +bool running = false; + +uint32_t update_stage = 0; + +int timer_fd = 0; + +static void print_state_diff(struct input_method_state previous, + struct input_method_state future) { + if (previous.content_type.hint != future.content_type.hint) { + char *strs[] = { "COMPLETION", "SPELLCHECK", "AUTO_CAPITALIZATION", + "LOWERCASE", "UPPERCASE", "TITLECASE", "HIDDEN_TEXT", + "SENSITIVE_DATA", "LATIN", "MULTILINE"}; + printf("content_type.hint:"); + uint32_t hint = future.content_type.hint; + if (!hint) { + printf(" NONE"); + } + for (unsigned i = 0; i < sizeof(strs) / sizeof(*strs); i++) { + if (hint & 1 << i) { + printf(" %s", strs[i]); + } + } + printf("\n"); + } + if (previous.content_type.purpose != future.content_type.purpose) { + char *strs[] = { "NORMAL", "ALPHA", "DIGITS", "NUMBER", "PHONE", "URL", + "EMAIL", "NAME", "PASSWORD", "PIN", "DATE", "TIME", "DATETIME", + "TERMINAL" }; + printf("content_type.purpose: %s\n", strs[future.content_type.purpose]); + } + if (!!previous.surrounding.text != !!future.surrounding.text + || (previous.surrounding.text && future.surrounding.text + && strcmp(previous.surrounding.text, future.surrounding.text) != 0) + || previous.surrounding.anchor != future.surrounding.anchor + || previous.surrounding.cursor != future.surrounding.cursor) { + char *text = future.surrounding.text; + if (!text) { + printf("Removed surrounding text\n"); + } else { + printf("Surrounding text: %s\n", text); + uint32_t anchor = future.surrounding.anchor; + uint32_t cursor = future.surrounding.cursor; + if (cursor == anchor) { + char *temp = strndup(text, cursor); + printf("Cursor after %d: %s\n", cursor, temp); + free(temp); + } else { + if (cursor > anchor) { + uint32_t tmp = anchor; + anchor = cursor; + cursor = tmp; + } + char *temp = strndup(&text[cursor], anchor - cursor); + printf("Selection: %s\n", temp); + free(temp); + } + } + } + if (previous.change_cause != future.change_cause) { + char *strs[] = { "INPUT_METHOD", "OTHER" }; + printf("Change cause: %s\n", strs[future.change_cause]); + } +} + +static void handle_content_type(void *data, + struct zwp_input_method_v2 *zwp_input_method_v2, + uint32_t hint, uint32_t purpose) { + pending.content_type.hint = hint; + pending.content_type.purpose = purpose; +} + +static void handle_surrounding_text(void *data, + struct zwp_input_method_v2 *zwp_input_method_v2, + const char *text, uint32_t cursor, uint32_t anchor) { + free(pending.surrounding.text); + pending.surrounding.text = strdup(text); + pending.surrounding.cursor = cursor; + pending.surrounding.anchor = anchor; +} + +static void handle_text_change_cause(void *data, + struct zwp_input_method_v2 *zwp_input_method_v2, + uint32_t cause) { + pending.change_cause = cause; +} + +static void handle_activate(void *data, + struct zwp_input_method_v2 *zwp_input_method_v2) { + pending_active = true; +} + +static void handle_deactivate(void *data, + struct zwp_input_method_v2 *zwp_input_method_v2) { + pending_active = false; +} + +static void handle_unavailable(void *data, + struct zwp_input_method_v2 *zwp_input_method_v2) { + printf("IM disappeared\n"); + zwp_input_method_v2_destroy(zwp_input_method_v2); + input_method = NULL; + running = false; +} + +static void im_activate(void *data, + struct zwp_input_method_v2 *id) { + update_stage = 0; +} + +static void timer_arm(unsigned seconds) { + printf("Timer armed\n"); + struct itimerspec spec = { + .it_interval = {0}, + .it_value = { + .tv_sec = seconds, + .tv_nsec = 0 + } + }; + if (timerfd_settime(timer_fd, 0, &spec, NULL)) { + fprintf(stderr, "Failed to arm timer: %s\n", strerror(errno)); + } +} + +static void do_updates() { + printf("Update %d\n", update_stage); + switch (update_stage) { + case 0: + // TODO: remember initial surrounding text + zwp_input_method_v2_set_preedit_string(input_method, "Preedit", 2, 4); + zwp_input_method_v2_commit(input_method, serial); + // don't expect an answer, preedit doesn't change anything visible + timer_arm(sleeptime); + update_stage++; + return; + case 1: + zwp_input_method_v2_set_preedit_string(input_method, "Præedit2", strlen("Pr"), strlen("Præed")); + zwp_input_method_v2_commit_string(input_method, "_Commit_"); + zwp_input_method_v2_commit(input_method, serial); + update_stage++; + break; + case 2: + if (strcmp(current.surrounding.text, "_Commit_") != 0) { + return; + } + zwp_input_method_v2_commit_string(input_method, "_CommitNoPreed_"); + zwp_input_method_v2_commit(input_method, serial); + timer_arm(sleeptime); + update_stage++; + break; + case 3: + if (strcmp(current.surrounding.text, "_Commit__CommitNoPreed_") != 0) { + return; + } + zwp_input_method_v2_commit_string(input_method, "_WaitNo_"); + zwp_input_method_v2_delete_surrounding_text(input_method, strlen("_CommitNoPreed_"), 0); + zwp_input_method_v2_commit(input_method, serial); + update_stage++; + break; + case 4: + if (strcmp(current.surrounding.text, "_Commit__WaitNo_") != 0) { + return; + } + zwp_input_method_v2_set_preedit_string(input_method, "PreedWithDel", strlen("Preed"), strlen("Preed")); + zwp_input_method_v2_delete_surrounding_text(input_method, strlen("_WaitNo_"), 0); + zwp_input_method_v2_commit(input_method, serial); + update_stage++; + break; + case 5: + if (strcmp(current.surrounding.text, "_Commit_") != 0) { + return; + } + zwp_input_method_v2_delete_surrounding_text(input_method, strlen("mit_"), 0); + zwp_input_method_v2_commit(input_method, serial); + update_stage++; + break; + case 6: + if (strcmp(current.surrounding.text, "_Com") != 0) { + printf("Failed\n"); + } + update_stage++; + break; + default: + printf("Submitted everything\n"); + return; + }; +} + +static void handle_timer() { + printf("Timer dispatched at %d\n", update_stage); + do_updates(); +} + +static void im_deactivate(void *data, + struct zwp_input_method_v2 *context) { + // No special action needed +} + +static void handle_done(void *data, + struct zwp_input_method_v2 *zwp_input_method_v2) { + bool prev_active = active; + serial++; + printf("Handle serial %d\n", serial); + if (active != pending_active) { + printf("Now %s\n", pending_active ? "active" : "inactive"); + } + if (pending_active) { + print_state_diff(current, pending); + } + active = pending_active; + free(current.surrounding.text); + struct input_method_state default_state = {0}; + current = pending; + pending = default_state; + if (active && !prev_active) { + im_activate(data, zwp_input_method_v2); + } else if (!active && prev_active) { + im_deactivate(data, zwp_input_method_v2); + } + + do_updates(); +} + +static const struct zwp_input_method_v2_listener im_listener = { + .activate = handle_activate, + .deactivate = handle_deactivate, + .surrounding_text = handle_surrounding_text, + .text_change_cause = handle_text_change_cause, + .content_type = handle_content_type, + .done = handle_done, + .unavailable = handle_unavailable, +}; + +static void handle_global(void *data, struct wl_registry *registry, + uint32_t name, const char *interface, uint32_t version) { + if (strcmp(interface, "wl_compositor") == 0) { + compositor = wl_registry_bind(registry, name, + &wl_compositor_interface, 1); + } else if (strcmp(interface, zwp_input_method_manager_v2_interface.name) == 0) { + input_method_manager = wl_registry_bind(registry, name, + &zwp_input_method_manager_v2_interface, 1); + } else if (strcmp(interface, wl_seat_interface.name) == 0) { + seat = wl_registry_bind(registry, name, &wl_seat_interface, version); + } +} + +static void handle_global_remove(void *data, struct wl_registry *registry, + uint32_t name) { + // who cares +} + +static const struct wl_registry_listener registry_listener = { + .global = handle_global, + .global_remove = handle_global_remove, +}; + +int main(int argc, char **argv) { + if (argc > 1) { + if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) { + printf(usage); + return 0; + } + sleeptime = atoi(argv[1]); + } + display = wl_display_connect(NULL); + if (display == NULL) { + fprintf(stderr, "Failed to create display\n"); + return EXIT_FAILURE; + } + + struct wl_registry *registry = wl_display_get_registry(display); + wl_registry_add_listener(registry, ®istry_listener, NULL); + wl_display_dispatch(display); + wl_display_roundtrip(display); + + if (compositor == NULL) { + fprintf(stderr, "wl-compositor not available\n"); + return EXIT_FAILURE; + } + if (input_method_manager == NULL) { + fprintf(stderr, "input-method not available\n"); + return EXIT_FAILURE; + } + if (seat == NULL) { + fprintf(stderr, "seat not available\n"); + return EXIT_FAILURE; + } + + input_method = zwp_input_method_manager_v2_get_input_method( + input_method_manager, seat); + running = true; + zwp_input_method_v2_add_listener(input_method, &im_listener, NULL); + + int display_fd = wl_display_get_fd(display); + timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK); + if (timer_fd < 0) { + fprintf(stderr, "Failed to start timer\n"); + return EXIT_FAILURE; + } + int epoll = epoll_create1(EPOLL_CLOEXEC); + if (epoll < 0) { + fprintf(stderr, "Failed to start epoll\n"); + return EXIT_FAILURE; + } + + struct epoll_event epoll_display = { + .events = EPOLLIN | EPOLLOUT, + .data = {.fd = display_fd}, + }; + if (epoll_ctl(epoll, EPOLL_CTL_ADD, display_fd, &epoll_display)) { + fprintf(stderr, "Failed to epoll display\n"); + return EXIT_FAILURE; + } + + wl_display_roundtrip(display); // timer may be armed here + + struct epoll_event epoll_timer = { + .events = EPOLLIN, + .data = {.fd = timer_fd}, + }; + if (epoll_ctl(epoll, EPOLL_CTL_ADD, timer_fd, &epoll_timer)) { + fprintf(stderr, "Failed to epoll timer\n"); + return EXIT_FAILURE; + } + + timer_arm(2); + + struct epoll_event caught; + while (epoll_wait(epoll, &caught, 1, -1)) { + if (!running) { + printf("Exiting\n"); + return EXIT_SUCCESS; + } + if (caught.data.fd == display_fd) { + if (wl_display_dispatch(display) == -1) { + break; + } + } else if (caught.data.fd == timer_fd) { + uint64_t expirations; + read(timer_fd, &expirations, sizeof(expirations)); + handle_timer(); + } else { + printf("Unknown source\n"); + } + } + return EXIT_SUCCESS; +} diff --git a/examples/meson.build b/examples/meson.build index ac60e5f9..da7a5275 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -94,6 +94,10 @@ examples = { 'src': 'toplevel-decoration.c', 'dep': [wayland_client, wlr_protos, wlroots], }, + 'input-method': { + 'src': 'input-method.c', + 'dep': [wayland_client, wlr_protos, wlroots], + }, 'text-input': { 'src': 'text-input.c', 'dep': [wayland_cursor, wayland_client, wlr_protos, wlroots], diff --git a/include/wlr/types/meson.build b/include/wlr/types/meson.build index e8de124a..7bc2b50f 100644 --- a/include/wlr/types/meson.build +++ b/include/wlr/types/meson.build @@ -11,6 +11,7 @@ install_headers( 'wlr_idle_inhibit_v1.h', 'wlr_input_device.h', 'wlr_input_inhibitor.h', + 'wlr_input_method_v2.h', 'wlr_keyboard.h', 'wlr_layer_shell_v1.h', 'wlr_linux_dmabuf_v1.h', @@ -30,7 +31,7 @@ install_headers( 'wlr_tablet_pad.h', 'wlr_tablet_tool.h', 'wlr_tablet_v2.h', - 'wlr_text_input_v3.h', + 'wlr_text_input_v3.h', 'wlr_touch.h', 'wlr_virtual_keyboard_v1.h', 'wlr_wl_shell.h', diff --git a/include/wlr/types/wlr_input_method_v2.h b/include/wlr/types/wlr_input_method_v2.h new file mode 100644 index 00000000..823088b7 --- /dev/null +++ b/include/wlr/types/wlr_input_method_v2.h @@ -0,0 +1,87 @@ +/* + * This an unstable interface of wlroots. No guarantees are made regarding the + * future consistency of this API. + */ +#ifndef WLR_USE_UNSTABLE +#error "Add -DWLR_USE_UNSTABLE to enable unstable wlroots features" +#endif + +#ifndef WLR_TYPES_WLR_INPUT_METHOD_V1_H +#define WLR_TYPES_WLR_INPUT_METHOD_V1_H +#include +#include +#include +#include + +struct wlr_input_method_v2_preedit_string { + char *text; + int32_t cursor_begin; + int32_t cursor_end; +}; + +struct wlr_input_method_v2_delete_surrounding_text { + uint32_t before_length; + uint32_t after_length; +}; + +struct wlr_input_method_v2_state { + struct wlr_input_method_v2_preedit_string preedit; + char *commit_text; + struct wlr_input_method_v2_delete_surrounding_text delete; +}; + +struct wlr_input_method_v2 { + struct wl_resource *resource; + + struct wlr_seat *seat; + + struct wlr_input_method_v2_state pending; + struct wlr_input_method_v2_state current; + bool active; // pending compositor-side state + bool client_active; // state known to the client + uint32_t current_serial; // received in last commit call + + struct wl_list link; + + struct wl_listener seat_destroy; + + struct { + struct wl_signal commit; // (struct wlr_input_method_v2*) + struct wl_signal destroy; // (struct wlr_input_method_v2*) + } events; +}; + +struct wlr_input_method_manager_v2 { + struct wl_global *global; + struct wl_list bound_resources; // struct wl_resource*::link + struct wl_list input_methods; // struct wlr_input_method_v2*::link + + struct wl_listener display_destroy; + + struct { + struct wl_signal input_method; // (struct wlr_input_method_v2*) + struct wl_signal destroy; // (struct wlr_input_method_manager_v2*) + } events; +}; + +struct wlr_input_method_manager_v2 *wlr_input_method_manager_v2_create( + struct wl_display *display); +void wlr_input_method_manager_v2_destroy( + struct wlr_input_method_manager_v2 *manager); + +void wlr_input_method_v2_send_activate( + struct wlr_input_method_v2 *input_method); +void wlr_input_method_v2_send_deactivate( + struct wlr_input_method_v2 *input_method); +void wlr_input_method_v2_send_surrounding_text( + struct wlr_input_method_v2 *input_method, const char *text, + uint32_t cursor, uint32_t anchor); +void wlr_input_method_v2_send_content_type( + struct wlr_input_method_v2 *input_method, uint32_t hint, + uint32_t purpose); +void wlr_input_method_v2_send_text_change_cause( + struct wlr_input_method_v2 *input_method, uint32_t cause); +void wlr_input_method_v2_send_done(struct wlr_input_method_v2 *input_method); +void wlr_input_method_v2_send_unavailable( + struct wlr_input_method_v2 *input_method); +#endif diff --git a/protocol/input-method-unstable-v2.xml b/protocol/input-method-unstable-v2.xml new file mode 100644 index 00000000..62be9d94 --- /dev/null +++ b/protocol/input-method-unstable-v2.xml @@ -0,0 +1,490 @@ + + + + + Copyright © 2008-2011 Kristian Høgsberg + Copyright © 2010-2011 Intel Corporation + Copyright © 2012-2013 Collabora, Ltd. + Copyright © 2012, 2013 Intel Corporation + Copyright © 2015, 2016 Jan Arne Petersen + Copyright © 2017, 2018 Red Hat, Inc. + Copyright © 2018 Purism SPC + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice (including the next + paragraph) shall be included in all copies or substantial portions of the + Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. + + + + This protocol allows applications to act as input methods for compositors. + + An input method context is used to manage the state of the input method. + + Text strings are UTF-8 encoded, their indices and lengths are in bytes. + + This document adheres to the RFC 2119 when using words like "must", + "should", "may", etc. + + Warning! The protocol described in this file is experimental and + backward incompatible changes may be made. Backward compatible changes + may be added together with the corresponding interface version bump. + Backward incompatible changes are done by bumping the version number in + the protocol and interface names and resetting the interface version. + Once the protocol is to be declared stable, the 'z' prefix and the + version number in the protocol and interface names are removed and the + interface version number is reset. + + + + + An input method object allows for clients to compose text. + + The objects connects the client to a text input in an application, and + lets the client to serve as an input method for a seat. + + The zwp_input_method_v2 object can occupy two distinct states: active and + inactive. In the active state, the object is associated to and + communicates with a text input. In the inactive state, there is no + associated text input, and the only communication is with the compositor. + Initially, the input method is in the inactive state. + + Requests issued in the inactive state must be accepted by the compositor. + Because of the serial mechanism, and the state reset on activate event, + they will not have any effect on the state of the next text input. + + There must be no more than one input method object per seat. + + + + + Notification that a text input focused on this seat requested the input + method to be activated. + + This event serves the purpose of providing the compositor with an + active input method. + + This event resets all state associated with previous enable, disable, + surrounding_text, text_change_cause, and content_type events, as well + as the state associated with set_preedit_string, commit_string, and + delete_surrounding_text requests. In addition, it marks the + zwp_input_method_v2 object as active, and makes any existing + zwp_input_popup_surface_v2 objects visible. + + The surrounding_text, and content_type events must follow before the + next done event if the text input supports the respective + functionality. + + State set with this event is double-buffered. It will get applied on + the next zwp_input_method_v2.done event, and stay valid until changed. + + + + + + Notification that no focused text input currently needs an active + input method on this seat. + + This event marks the zwp_input_method_v2 object as inactive. The + compositor must make all existing zwp_input_popup_surface_v2 objects + invisible until the next activate event. + + State set with this event is double-buffered. It will get applied on + the next zwp_input_method_v2.done event, and stay valid until changed. + + + + + + Updates the surrounding plain text around the cursor, excluding the + preedit text. + + If any preedit text is present, it is replaced with the cursor for the + purpose of this event. + + The argument text is a buffer containing the preedit string, and must + include the cursor position, and the complete selection. It should + contain additional characters before and after these. There is a + maximum length of wayland messages, so text can not be longer than 4000 + bytes. + + cursor is the byte offset of the cursor within the text buffer. + + anchor is the byte offset of the selection anchor within the text + buffer. If there is no selected text, anchor must be the same as + cursor. + + If this event does not arrive before the first done event, the input + method may assume that the text input does not support this + functionality and ignore following surrounding_text events. + + Values set with this event are double-buffered. They will get applied + and set to initial values on the next zwp_input_method_v2.done + event. + + The initial state for affected fields is empty, meaning that the text + input does not support sending surrounding text. If the empty values + get applied, subsequent attempts to change them may have no effect. + + + + + + + + + Tells the input method why the text surrounding the cursor changed. + + Whenever the client detects an external change in text, cursor, or + anchor position, it must issue this request to the compositor. This + request is intended to give the input method a chance to update the + preedit text in an appropriate way, e.g. by removing it when the user + starts typing with a keyboard. + + cause describes the source of the change. + + The value set with this event is double-buffered. It will get applied + and set to its initial value on the next zwp_input_method_v2.done + event. + + The initial value of cause is input_method. + + + + + + + Indicates the content type and hint for the current + zwp_input_method_v2 instance. + + Values set with this event are double-buffered. They will get applied + on the next zwp_input_method_v2.done event. + + The initial value for hint is none, and the initial value for purpose + is normal. + + + + + + + + Atomically applies state changes recently sent to the client. + + The done event establishes and updates the state of the client, and + must be issued after any changes to apply them. + + Text input state (content purpose, content hint, surrounding text, and + change cause) is conceptually double-buffered within an input method + context. + + Events modify the pending state, as opposed to the current state in use + by the input method. A done event atomically applies all pending state, + replacing the current state. After done, the new pending state is as + documented for each related request. + + Events must be applied in the order of arrival. + + Neither current nor pending state are modified unless noted otherwise. + + + + + + Send the commit string text for insertion to the application. + + Inserts a string at current cursor position (see commit event + sequence). The string to commit could be either just a single character + after a key press or the result of some composing. + + The argument text is a buffer containing the string to insert. There is + a maximum length of wayland messages, so text can not be longer than + 4000 bytes. + + Values set with this event are double-buffered. They must be applied + and reset to initial on the next zwp_text_input_v3.commit request. + + The initial value of text is an empty string. + + + + + + + Send the pre-edit string text to the application text input. + + Place a new composing text (pre-edit) at the current cursor position. + Any previously set composing text must be removed. Any previously + existing selected text must be removed. The cursor is moved to a new + position within the preedit string. + + The argument text is a buffer containing the preedit string. There is + a maximum length of wayland messages, so text can not be longer than + 4000 bytes. + + The arguments cursor_begin and cursor_end are counted in bytes relative + to the beginning of the submitted string buffer. Cursor should be + hidden by the text input when both are equal to -1. + + cursor_begin indicates the beginning of the cursor. cursor_end + indicates the end of the cursor. It may be equal or different than + cursor_begin. + + Values set with this event are double-buffered. They must be applied on + the next zwp_input_method_v2.commit event. + + The initial value of text is an empty string. The initial value of + cursor_begin, and cursor_end are both 0. + + + + + + + + + Remove the surrounding text. + + before_length and after_length are the number of bytes before and after + the current cursor index (excluding the preedit text) to delete. + + If any preedit text is present, it is replaced with the cursor for the + purpose of this event. In effect before_length is counted from the + beginning of preedit text, and after_length from its end (see commit + event sequence). + + Values set with this event are double-buffered. They must be applied + and reset to initial on the next zwp_input_method_v2.commit request. + + The initial values of both before_length and after_length are 0. + + + + + + + + Apply state changes from commit_string, set_preedit_string and + delete_surrounding_text requests. + + The state relating to these events is double-buffered, and each one + modifies the pending state. This request replaces the current state + with the pending state. + + The connected text input is expected to proceed by evaluating the + changes in the following order: + + 1. Replace existing preedit string with the cursor. + 2. Delete requested surrounding text. + 3. Insert commit string with the cursor at its end. + 4. Calculate surrounding text to send. + 5. Insert new preedit text in cursor position. + 6. Place cursor inside preedit text. + + The serial number reflects the last state of the zwp_input_method_v2 + object known to the client. The value of the serial argument must be + equal to the number of done events already issued by that object. When + the compositor receives a commit request with a serial different than + the number of past done events, it must proceed as normal, except it + should not change the current state of the zwp_input_method_v2 object. + + + + + + + Creates a new zwp_input_popup_surface_v2 object wrapping a given + surface. + + The surface gets assigned the "input_popup" role. If the surface + already has an assigned role, the compositor must issue a protocol + error. + + + + + + + + Allow an input method to receive hardware keyboard input and process + key events to generate text events (with pre-edit) over the wire. This + allows input methods which compose multiple key events for inputting + text like it is done for CJK languages. + + The compositor should send all keyboard events on the seat to the grab + holder via the returned wl_keyboard object. Nevertheless, the + compositor may decide not to forward any particular event. The + compositor must not further process any event after it has been + forwarded to the grab holder. + + Releasing the resulting wl_keyboard object releases the grab. + + + + + + + The input method ceased to be available. + + The compositor must issue this event as the only event on the object if + there was another input_method object associated with the same seat at + the time of its creation. + + The compositor must issue this request when the object is no longer + useable, e.g. due to seat removal. + + The input method context becomes inert and should be destroyed after + deactivation is handled. Any further requests and events except for the + destroy request must be ignored. + + + + + + Destroys the zwp_text_input_v2 object and any associated child + objects, i.e. zwp_input_popup_surface_v2 and + zwp_input_method_keyboard_grab_v2. + + + + + + + This interface marks a surface as a popup for interacting with an input + method. + + The compositor should place it near the active text input area. It must + be visible if and only if the input method is in the active state. + + The client must not destroy the underlying wl_surface while the + zwp_input_popup_surface_v2 object exists. + + + + + Notify about the position of the area of the text input expressed as a + rectangle in surface local coordinates. + + This is a hint to the input method telling it the relative position of + the text being entered. + + + + + + + + + + + + + + The zwp_input_method_keyboard_grab_v2 interface represents an exclusive + grab of the wl_keyboard interface associated with the seat. + + + + + This event provides a file descriptor to the client which can be + memory-mapped to provide a keyboard mapping description. + + + + + + + + + A key was pressed or released. + The time argument is a timestamp with millisecond granularity, with an + undefined base. + + + + + + + + + + Notifies clients that the modifier and/or group state has changed, and + it should update its local state. + + + + + + + + + + + + + + + Informs the client about the keyboard's repeat rate and delay. + + This event is sent as soon as the zwp_input_method_keyboard_grab_v2 + object has been created, and is guaranteed to be received by the + client before any key press event. + + Negative values for either rate or delay are illegal. A rate of zero + will disable any repeating (regardless of the value of delay). + + This event can be sent later on as well with a new value if necessary, + so clients should continue listening for the event past the creation + of zwp_input_method_keyboard_grab_v2. + + + + + + + + + The input method manager allows the client to become the input method on + a chosen seat. + + No more than one input method must be associated with any seat at any + given time. + + + + + Request a new input zwp_input_method_v2 object associated with a given + seat. + + + + + + + + Destroys the zwp_input_method_manager_v2 object. + + The zwp_input_method_v2 objects originating from it remain valid. + + + + diff --git a/protocol/meson.build b/protocol/meson.build index 9c810367..e2b8f746 100644 --- a/protocol/meson.build +++ b/protocol/meson.build @@ -22,6 +22,7 @@ protocols = [ 'gamma-control.xml', 'gtk-primary-selection.xml', 'idle.xml', + 'input-method-unstable-v2.xml', 'screenshooter.xml', 'server-decoration.xml', 'text-input-unstable-v3.xml', @@ -40,6 +41,7 @@ client_protocols = [ [wl_protocol_dir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml'], [wl_protocol_dir, 'unstable/pointer-constraints/pointer-constraints-unstable-v1.xml'], 'idle.xml', + 'input-method-unstable-v2.xml', 'screenshooter.xml', 'text-input-unstable-v3.xml', 'wlr-export-dmabuf-unstable-v1.xml', diff --git a/types/meson.build b/types/meson.build index e5b053aa..0507df98 100644 --- a/types/meson.build +++ b/types/meson.build @@ -30,6 +30,7 @@ lib_wlr_types = static_library( 'wlr_idle.c', 'wlr_input_device.c', 'wlr_input_inhibitor.c', + 'wlr_input_method_v2.c', 'wlr_keyboard.c', 'wlr_layer_shell_v1.c', 'wlr_linux_dmabuf_v1.c', diff --git a/types/wlr_input_method_v2.c b/types/wlr_input_method_v2.c new file mode 100644 index 00000000..fc9fa62b --- /dev/null +++ b/types/wlr_input_method_v2.c @@ -0,0 +1,297 @@ +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200809L +#endif +#include +#include +#include +#include +#include +#include +#include +#include "input-method-unstable-v2-protocol.h" +#include "util/signal.h" + +static const struct zwp_input_method_v2_interface input_method_impl; + +static struct wlr_input_method_v2 *input_method_from_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, + &zwp_input_method_v2_interface, &input_method_impl)); + return wl_resource_get_user_data(resource); +} + +static void input_method_destroy(struct wlr_input_method_v2 *input_method) { + wlr_signal_emit_safe(&input_method->events.destroy, input_method); + wl_list_remove(&input_method->seat_destroy.link); + free(input_method->pending.commit_text); + free(input_method->pending.preedit.text); + free(input_method->current.commit_text); + free(input_method->current.preedit.text); + free(input_method); +} + +static void input_method_resource_destroy(struct wl_resource *resource) { + struct wlr_input_method_v2 *input_method = + input_method_from_resource(resource); + if (!input_method) { + return; + } + input_method_destroy(input_method); +} + +static void im_destroy(struct wl_client *client, struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static void im_commit(struct wl_client *client, struct wl_resource *resource, + uint32_t serial) { + struct wlr_input_method_v2 *input_method = + input_method_from_resource(resource); + if (!input_method) { + return; + } + input_method->current = input_method->pending; + input_method->current_serial = serial; + struct wlr_input_method_v2_state default_state = {0}; + input_method->pending = default_state; + wlr_signal_emit_safe(&input_method->events.commit, (void*)input_method); +} + +static void im_commit_string(struct wl_client *client, + struct wl_resource *resource, const char *text) { + struct wlr_input_method_v2 *input_method = + input_method_from_resource(resource); + if (!input_method) { + return; + } + free(input_method->pending.commit_text); + input_method->pending.commit_text = strdup(text); +} + +static void im_set_preedit_string(struct wl_client *client, + struct wl_resource *resource, const char *text, int32_t cursor_begin, + int32_t cursor_end) { + struct wlr_input_method_v2 *input_method = + input_method_from_resource(resource); + if (!input_method) { + return; + } + input_method->pending.preedit.cursor_begin = cursor_begin; + input_method->pending.preedit.cursor_end = cursor_end; + free(input_method->pending.preedit.text); + input_method->pending.preedit.text = strdup(text); +} + +static void im_delete_surrounding_text(struct wl_client *client, + struct wl_resource *resource, + uint32_t before_length, uint32_t after_length) { + struct wlr_input_method_v2 *input_method = + input_method_from_resource(resource); + if (!input_method) { + return; + } + input_method->pending.delete.before_length = before_length; + input_method->pending.delete.after_length = after_length; +} + +static void im_get_input_popup_surface(struct wl_client *client, + struct wl_resource *resource, uint32_t id, + struct wl_resource *surface) { + wlr_log(WLR_INFO, "Stub: zwp_input_method_v2::get_input_popup_surface"); +} + + +static void im_grab_keyboard(struct wl_client *client, + struct wl_resource *resource, uint32_t keyboard) { + wlr_log(WLR_INFO, "Stub: zwp_input_method_v2::grab_keyboard"); +} + +static const struct zwp_input_method_v2_interface input_method_impl = { + .destroy = im_destroy, + .commit = im_commit, + .commit_string = im_commit_string, + .set_preedit_string = im_set_preedit_string, + .delete_surrounding_text = im_delete_surrounding_text, + .get_input_popup_surface = im_get_input_popup_surface, + .grab_keyboard = im_grab_keyboard, +}; + +void wlr_input_method_v2_send_activate( + struct wlr_input_method_v2 *input_method) { + zwp_input_method_v2_send_activate(input_method->resource); + input_method->active = true; +} + +void wlr_input_method_v2_send_deactivate( + struct wlr_input_method_v2 *input_method) { + zwp_input_method_v2_send_deactivate(input_method->resource); + input_method->active = false; +} + +void wlr_input_method_v2_send_surrounding_text( + struct wlr_input_method_v2 *input_method, const char *text, + uint32_t cursor, uint32_t anchor) { + const char *send_text = text; + if (!send_text) { + send_text = ""; + } + zwp_input_method_v2_send_surrounding_text(input_method->resource, send_text, + cursor, anchor); +} + +void wlr_input_method_v2_send_text_change_cause( + struct wlr_input_method_v2 *input_method, uint32_t cause) { + zwp_input_method_v2_send_text_change_cause(input_method->resource, cause); +} + +void wlr_input_method_v2_send_content_type( + struct wlr_input_method_v2 *input_method, + uint32_t hint, uint32_t purpose) { + zwp_input_method_v2_send_content_type(input_method->resource, hint, + purpose); +} + +void wlr_input_method_v2_send_done(struct wlr_input_method_v2 *input_method) { + zwp_input_method_v2_send_done(input_method->resource); + input_method->client_active = input_method->active; + input_method->current_serial++; +} + +void wlr_input_method_v2_send_unavailable( + struct wlr_input_method_v2 *input_method) { + zwp_input_method_v2_send_unavailable(input_method->resource); + struct wl_resource *resource = input_method->resource; + input_method_destroy(input_method); + wl_resource_set_user_data(resource, NULL); +} + +static const struct zwp_input_method_manager_v2_interface + input_method_manager_impl; + +static struct wlr_input_method_manager_v2 *input_method_manager_from_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, + &zwp_input_method_manager_v2_interface, &input_method_manager_impl)); + return wl_resource_get_user_data(resource); +} + +static void input_method_handle_seat_destroy(struct wl_listener *listener, + void *data) { + struct wlr_input_method_v2 *input_method = wl_container_of(listener, + input_method, seat_destroy); + wlr_input_method_v2_send_unavailable(input_method); +} + +static void manager_get_input_method(struct wl_client *client, + struct wl_resource *resource, struct wl_resource *seat, + uint32_t input_method_id) { + struct wlr_input_method_manager_v2 *im_manager = + input_method_manager_from_resource(resource); + + struct wlr_input_method_v2 *input_method = calloc(1, + sizeof(struct wlr_input_method_v2)); + if (!input_method) { + wl_client_post_no_memory(client); + return; + } + wl_signal_init(&input_method->events.commit); + wl_signal_init(&input_method->events.destroy); + int version = wl_resource_get_version(resource); + struct wl_resource *im_resource = wl_resource_create(client, + &zwp_input_method_v2_interface, version, input_method_id); + if (im_resource == NULL) { + free(input_method); + wl_client_post_no_memory(client); + return; + } + wl_resource_set_implementation(im_resource, &input_method_impl, + input_method, input_method_resource_destroy); + + struct wlr_seat_client *seat_client = wlr_seat_client_from_resource(seat); + wl_signal_add(&seat_client->events.destroy, + &input_method->seat_destroy); + input_method->seat_destroy.notify = + input_method_handle_seat_destroy; + + input_method->resource = im_resource; + input_method->seat = seat_client->seat; + wl_list_insert(&im_manager->input_methods, + wl_resource_get_link(input_method->resource)); + wlr_signal_emit_safe(&im_manager->events.input_method, input_method); +} + +static void manager_destroy(struct wl_client *client, + struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static const struct zwp_input_method_manager_v2_interface + input_method_manager_impl = { + .get_input_method = manager_get_input_method, + .destroy = manager_destroy, +}; + +static void input_method_manager_unbind(struct wl_resource *resource) { + wl_list_remove(wl_resource_get_link(resource)); +} + +static void input_method_manager_bind(struct wl_client *wl_client, void *data, + uint32_t version, uint32_t id) { + assert(wl_client); + struct wlr_input_method_manager_v2 *im_manager = data; + + struct wl_resource *bound_resource = wl_resource_create(wl_client, + &zwp_input_method_manager_v2_interface, version, id); + if (bound_resource == NULL) { + wl_client_post_no_memory(wl_client); + return; + } + wl_resource_set_implementation(bound_resource, &input_method_manager_impl, + im_manager, input_method_manager_unbind); + wl_list_insert(&im_manager->bound_resources, + wl_resource_get_link(bound_resource)); +} + +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_input_method_manager_v2 *manager = + wl_container_of(listener, manager, display_destroy); + wlr_input_method_manager_v2_destroy(manager); +} + +struct wlr_input_method_manager_v2 *wlr_input_method_manager_v2_create( + struct wl_display *display) { + struct wlr_input_method_manager_v2 *im_manager = calloc(1, + sizeof(struct wlr_input_method_manager_v2)); + if (!im_manager) { + return NULL; + } + wl_signal_init(&im_manager->events.input_method); + wl_list_init(&im_manager->bound_resources); + wl_list_init(&im_manager->input_methods); + + im_manager->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &im_manager->display_destroy); + + im_manager->global = wl_global_create(display, + &zwp_input_method_manager_v2_interface, 1, im_manager, + input_method_manager_bind); + return im_manager; +} + +void wlr_input_method_manager_v2_destroy( + struct wlr_input_method_manager_v2 *manager) { + wlr_signal_emit_safe(&manager->events.destroy, manager); + wl_list_remove(&manager->display_destroy.link); + + struct wl_resource *resource, *resource_tmp; + wl_resource_for_each_safe(resource, resource_tmp, + &manager->bound_resources) { + wl_resource_destroy(resource); + } + struct wlr_input_method_v2 *im, *im_tmp; + wl_list_for_each_safe(im, im_tmp, &manager->input_methods, link) { + wl_resource_destroy(im->resource); + } + wl_global_destroy(manager->global); + free(manager); +} From 585757d6e6acd7c8dd15073d849278498066dac1 Mon Sep 17 00:00:00 2001 From: Dorota Czaplejewicz Date: Thu, 23 Aug 2018 11:39:49 +0200 Subject: [PATCH 4/4] rootston: add support for text input and the basic of input method The compositor acts as a relay between applications using the text-input protocol and input methods using the input-method protocol. This change implements the basic but useful support for input-method, leaving out grabs as well as popups. --- include/rootston/desktop.h | 4 + include/rootston/seat.h | 3 + include/rootston/text_input.h | 63 +++++++ rootston/desktop.c | 5 + rootston/meson.build | 1 + rootston/seat.c | 6 + rootston/text_input.c | 312 ++++++++++++++++++++++++++++++++++ 7 files changed, 394 insertions(+) create mode 100644 include/rootston/text_input.h create mode 100644 rootston/text_input.c diff --git a/include/rootston/desktop.h b/include/rootston/desktop.h index 90851a17..f855e4d5 100644 --- a/include/rootston/desktop.h +++ b/include/rootston/desktop.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -16,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -53,6 +55,8 @@ struct roots_desktop { struct wlr_idle_inhibit_manager_v1 *idle_inhibit; struct wlr_input_inhibit_manager *input_inhibit; struct wlr_layer_shell_v1 *layer_shell; + struct wlr_input_method_manager_v2 *input_method; + struct wlr_text_input_manager_v3 *text_input; struct wlr_virtual_keyboard_manager_v1 *virtual_keyboard; struct wlr_screencopy_manager_v1 *screencopy; struct wlr_tablet_manager_v2 *tablet_v2; diff --git a/include/rootston/seat.h b/include/rootston/seat.h index 7e7ee3e9..0187c6cc 100644 --- a/include/rootston/seat.h +++ b/include/rootston/seat.h @@ -5,6 +5,7 @@ #include "rootston/input.h" #include "rootston/keyboard.h" #include "rootston/layers.h" +#include "rootston/text_input.h" struct roots_seat { struct roots_input *input; @@ -19,6 +20,8 @@ struct roots_seat { // If the focused layer is set, views cannot receive keyboard focus struct wlr_layer_surface_v1 *focused_layer; + struct roots_input_method_relay im_relay; + // If non-null, only this client can receive input events struct wl_client *exclusive_client; diff --git a/include/rootston/text_input.h b/include/rootston/text_input.h new file mode 100644 index 00000000..82e45e3e --- /dev/null +++ b/include/rootston/text_input.h @@ -0,0 +1,63 @@ +#ifndef ROOTSTON_TEXT_INPUT_H +#define ROOTSTON_TEXT_INPUT_H + +#include +#include +#include +#include "rootston/seat.h" + +/** + * The relay structure manages the relationship between text-input and + * input_method interfaces on a given seat. Multiple text-input interfaces may + * be bound to a relay, but at most one will be focused (reveiving events) at + * a time. At most one input-method interface may be bound to the seat. The + * relay manages life cycle of both sides. When both sides are present and + * focused, the relay passes messages between them. + * + * Text input focus is a subset of keyboard focus - if the text-input is + * in the focused state, wl_keyboard sent an enter as well. However, having + * wl_keyboard focused doesn't mean that text-input will be focused. + */ +struct roots_input_method_relay { + struct roots_seat *seat; + + struct wl_list text_inputs; // roots_text_input::link + struct wlr_input_method_v2 *input_method; // doesn't have to be present + + struct wl_listener text_input_new; + struct wl_listener text_input_enable; + struct wl_listener text_input_commit; + struct wl_listener text_input_disable; + struct wl_listener text_input_destroy; + + struct wl_listener input_method_new; + struct wl_listener input_method_commit; + struct wl_listener input_method_destroy; +}; + +struct roots_text_input { + struct roots_input_method_relay *relay; + + struct wlr_text_input_v3 *input; + // The surface getting seat's focus. Stored for when text-input cannot + // be sent an enter event immediately after getting focus, e.g. when + // there's no input method available. Cleared once text-input is entered. + struct wlr_surface *pending_focused_surface; + + struct wl_list link; + + struct wl_listener pending_focused_surface_destroy; +}; + +void roots_input_method_relay_init(struct roots_seat *seat, + struct roots_input_method_relay *relay); + +// Updates currently focused surface. Surface must belong to the same seat. +void roots_input_method_relay_set_focus(struct roots_input_method_relay *relay, + struct wlr_surface *surface); + +struct roots_text_input *roots_text_input_create( + struct roots_input_method_relay *relay, + struct wlr_text_input_v3 *text_input); + +#endif diff --git a/rootston/desktop.c b/rootston/desktop.c index c180c839..5de916dd 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -949,6 +949,11 @@ struct roots_desktop *desktop_create(struct roots_server *server, wl_signal_add(&desktop->input_inhibit->events.deactivate, &desktop->input_inhibit_deactivate); + desktop->input_method = + wlr_input_method_manager_v2_create(server->wl_display); + + desktop->text_input = wlr_text_input_manager_v3_create(server->wl_display); + desktop->virtual_keyboard = wlr_virtual_keyboard_manager_v1_create( server->wl_display); wl_signal_add(&desktop->virtual_keyboard->events.new_virtual_keyboard, diff --git a/rootston/meson.build b/rootston/meson.build index 9d1decce..d650dc51 100644 --- a/rootston/meson.build +++ b/rootston/meson.build @@ -9,6 +9,7 @@ sources = [ 'main.c', 'output.c', 'seat.c', + 'text_input.c', 'virtual_keyboard.c', 'wl_shell.c', 'xdg_shell.c', diff --git a/rootston/seat.c b/rootston/seat.c index c41c32ed..82444dcb 100644 --- a/rootston/seat.c +++ b/rootston/seat.c @@ -16,6 +16,7 @@ #include "rootston/input.h" #include "rootston/keyboard.h" #include "rootston/seat.h" +#include "rootston/text_input.h" #include "rootston/xcursor.h" @@ -605,6 +606,8 @@ struct roots_seat *roots_seat_create(struct roots_input *input, char *name) { return NULL; } + roots_input_method_relay_init(seat, &seat->im_relay); + wl_list_insert(&input->seats, &seat->link); seat->new_drag_icon.notify = roots_seat_handle_new_drag_icon; @@ -1182,6 +1185,7 @@ void roots_seat_set_focus(struct roots_seat *seat, struct roots_view *view) { if (view == NULL) { seat->cursor->mode = ROOTS_CURSOR_PASSTHROUGH; wlr_seat_keyboard_clear_focus(seat->seat); + roots_input_method_relay_set_focus(&seat->im_relay, NULL); return; } @@ -1220,6 +1224,8 @@ void roots_seat_set_focus(struct roots_seat *seat, struct roots_view *view) { if (seat->cursor) { roots_cursor_update_focus(seat->cursor); } + + roots_input_method_relay_set_focus(&seat->im_relay, view->wlr_surface); } /** diff --git a/rootston/text_input.c b/rootston/text_input.c new file mode 100644 index 00000000..84dd253e --- /dev/null +++ b/rootston/text_input.c @@ -0,0 +1,312 @@ +#include +#include +#include +#include "rootston/seat.h" +#include "rootston/text_input.h" + +static struct roots_text_input *relay_get_focusable_text_input( + struct roots_input_method_relay *relay) { + struct roots_text_input *text_input = NULL; + wl_list_for_each(text_input, &relay->text_inputs, link) { + if (text_input->pending_focused_surface) { + return text_input; + } + } + return NULL; +} + +static struct roots_text_input *relay_get_focused_text_input( + struct roots_input_method_relay *relay) { + struct roots_text_input *text_input = NULL; + wl_list_for_each(text_input, &relay->text_inputs, link) { + if (text_input->input->focused_surface) { + return text_input; + } + } + return NULL; +} + +static void handle_im_commit(struct wl_listener *listener, void *data) { + struct roots_input_method_relay *relay = wl_container_of(listener, relay, + input_method_commit); + + struct roots_text_input *text_input = relay_get_focused_text_input(relay); + if (!text_input) { + return; + } + struct wlr_input_method_v2 *context = data; + assert(context == relay->input_method); + if (context->current.preedit.text) { + wlr_text_input_v3_send_preedit_string(text_input->input, + context->current.preedit.text, + context->current.preedit.cursor_begin, + context->current.preedit.cursor_end); + } + if (context->current.commit_text) { + wlr_text_input_v3_send_commit_string(text_input->input, + context->current.commit_text); + } + if (context->current.delete.before_length + || context->current.delete.after_length) { + wlr_text_input_v3_send_delete_surrounding_text(text_input->input, + context->current.delete.before_length, + context->current.delete.after_length); + } + wlr_text_input_v3_send_done(text_input->input); +} + +static void text_input_set_pending_focused_surface( + struct roots_text_input *text_input, struct wlr_surface *surface) { + text_input->pending_focused_surface = surface; + wl_signal_add(&surface->events.destroy, + &text_input->pending_focused_surface_destroy); +} + +static void text_input_clear_pending_focused_surface( + struct roots_text_input *text_input) { + wl_list_remove(&text_input->pending_focused_surface_destroy.link); + text_input->pending_focused_surface = NULL; +} + +static void handle_im_destroy(struct wl_listener *listener, void *data) { + struct roots_input_method_relay *relay = wl_container_of(listener, relay, + input_method_destroy); + struct wlr_input_method_v2 *context = data; + assert(context == relay->input_method); + relay->input_method = NULL; + struct roots_text_input *text_input = relay_get_focused_text_input(relay); + if (text_input) { + // keyboard focus is still there, so keep the surface at hand in case + // the input method returns + text_input_set_pending_focused_surface(text_input, + text_input->input->focused_surface); + wlr_text_input_v3_send_leave(text_input->input); + } +} + +static void relay_send_im_done(struct roots_input_method_relay *relay, + struct wlr_text_input_v3 *input) { + struct wlr_input_method_v2 *input_method = relay->input_method; + if (!input_method) { + wlr_log(WLR_INFO, "Sending IM_DONE but im is gone"); + return; + } + // TODO: only send each of those if they were modified + wlr_input_method_v2_send_surrounding_text(input_method, + input->current.surrounding.text, input->current.surrounding.cursor, + input->current.surrounding.anchor); + wlr_input_method_v2_send_text_change_cause(input_method, + input->current.text_change_cause); + wlr_input_method_v2_send_content_type(input_method, + input->current.content_type.hint, input->current.content_type.purpose); + wlr_input_method_v2_send_done(input_method); + // TODO: pass intent, display popup size +} + +static struct roots_text_input *text_input_to_roots( + struct roots_input_method_relay *relay, + struct wlr_text_input_v3 *text_input) { + struct roots_text_input *roots_text_input = NULL; + wl_list_for_each(roots_text_input, &relay->text_inputs, link) { + if (roots_text_input->input == text_input) { + return roots_text_input; + } + } + return NULL; +} + +static void handle_text_input_enable(struct wl_listener *listener, void *data) { + struct roots_input_method_relay *relay = wl_container_of(listener, relay, + text_input_enable); + if (relay->input_method == NULL) { + wlr_log(WLR_INFO, "Enabling text input when input method is gone"); + return; + } + struct roots_text_input *text_input = text_input_to_roots(relay, + (struct wlr_text_input_v3*)data); + wlr_input_method_v2_send_activate(relay->input_method); + relay_send_im_done(relay, text_input->input); +} + +static void handle_text_input_commit(struct wl_listener *listener, + void *data) { + struct roots_input_method_relay *relay = wl_container_of(listener, relay, + text_input_commit); + struct roots_text_input *text_input = text_input_to_roots(relay, + (struct wlr_text_input_v3*)data); + if (!text_input->input->current_enabled) { + wlr_log(WLR_INFO, "Inactive text input tried to commit an update"); + return; + } + wlr_log(WLR_DEBUG, "Text input committed update"); + if (relay->input_method == NULL) { + wlr_log(WLR_INFO, "Text input committed, but input method is gone"); + return; + } + relay_send_im_done(relay, text_input->input); +} + +static void relay_disable_text_input(struct roots_input_method_relay *relay, + struct roots_text_input *text_input) { + if (relay->input_method == NULL) { + wlr_log(WLR_DEBUG, "Disabling text input, but input method is gone"); + return; + } + wlr_input_method_v2_send_deactivate(relay->input_method); + relay_send_im_done(relay, text_input->input); +} + +static void handle_text_input_disable(struct wl_listener *listener, + void *data) { + struct roots_input_method_relay *relay = wl_container_of(listener, relay, + text_input_disable); + struct roots_text_input *text_input = text_input_to_roots(relay, + (struct wlr_text_input_v3*)data); + if (!text_input->input->current_enabled) { + wlr_log(WLR_DEBUG, "Inactive text input tried to disable itself"); + return; + } + relay_disable_text_input(relay, text_input); +} + +static void handle_text_input_destroy(struct wl_listener *listener, + void *data) { + struct roots_input_method_relay *relay = wl_container_of(listener, relay, + text_input_destroy); + struct roots_text_input *text_input = text_input_to_roots(relay, + (struct wlr_text_input_v3*)data); + + if (text_input->input->current_enabled) { + relay_disable_text_input(relay, text_input); + } + + wl_list_remove(&text_input->link); + text_input->input = NULL; + free(text_input); +} + +static void handle_pending_focused_surface_destroy(struct wl_listener *listener, + void *data) { + struct roots_text_input *text_input = wl_container_of(listener, text_input, + pending_focused_surface_destroy); + struct wlr_surface *surface = data; + assert(text_input->pending_focused_surface == surface); + text_input->pending_focused_surface = NULL; +} + +struct roots_text_input *roots_text_input_create( + struct roots_input_method_relay *relay, + struct wlr_text_input_v3 *text_input) { + struct roots_text_input *input = calloc(1, sizeof(struct roots_text_input)); + if (!input) { + return NULL; + } + input->input = text_input; + input->relay = relay; + + wl_signal_add(&text_input->events.enable, &relay->text_input_enable); + relay->text_input_enable.notify = handle_text_input_enable; + + wl_signal_add(&text_input->events.commit, &relay->text_input_commit); + relay->text_input_commit.notify = handle_text_input_commit; + + wl_signal_add(&text_input->events.disable, &relay->text_input_disable); + relay->text_input_disable.notify = handle_text_input_disable; + + wl_signal_add(&text_input->events.destroy, &relay->text_input_destroy); + relay->text_input_destroy.notify = handle_text_input_destroy; + + input->pending_focused_surface_destroy.notify = + handle_pending_focused_surface_destroy; + return input; +} + +static void relay_handle_text_input(struct wl_listener *listener, + void *data) { + struct roots_input_method_relay *relay = wl_container_of(listener, relay, + text_input_new); + struct wlr_text_input_v3 *wlr_text_input = data; + if (relay->seat->seat != wlr_text_input->seat) { + return; + } + + struct roots_text_input *text_input = roots_text_input_create(relay, + wlr_text_input); + if (!text_input) { + return; + } + wl_list_insert(&relay->text_inputs, &text_input->link); +} + +static void relay_handle_input_method(struct wl_listener *listener, + void *data) { + struct roots_input_method_relay *relay = wl_container_of(listener, relay, + input_method_new); + struct wlr_input_method_v2 *input_method = data; + if (relay->seat->seat != input_method->seat) { + return; + } + + if (relay->input_method != NULL) { + wlr_log(WLR_INFO, "Attempted to connect second input method to a seat"); + wlr_input_method_v2_send_unavailable(input_method); + return; + } + + relay->input_method = input_method; + wl_signal_add(&relay->input_method->events.commit, + &relay->input_method_commit); + relay->input_method_commit.notify = handle_im_commit; + wl_signal_add(&relay->input_method->events.destroy, + &relay->input_method_destroy); + relay->input_method_destroy.notify = handle_im_destroy; + + struct roots_text_input *text_input = relay_get_focusable_text_input(relay); + if (text_input) { + wlr_text_input_v3_send_enter(text_input->input, + text_input->pending_focused_surface); + text_input_clear_pending_focused_surface(text_input); + } +} + +void roots_input_method_relay_init(struct roots_seat *seat, + struct roots_input_method_relay *relay) { + relay->seat = seat; + wl_list_init(&relay->text_inputs); + + relay->text_input_new.notify = relay_handle_text_input; + wl_signal_add(&seat->input->server->desktop->text_input->events.text_input, + &relay->text_input_new); + + relay->input_method_new.notify = relay_handle_input_method; + wl_signal_add( + &seat->input->server->desktop->input_method->events.input_method, + &relay->input_method_new); +} + +void roots_input_method_relay_set_focus(struct roots_input_method_relay *relay, + struct wlr_surface *surface) { + struct roots_text_input *text_input; + wl_list_for_each(text_input, &relay->text_inputs, link) { + if (text_input->pending_focused_surface) { + assert(text_input->input->focused_surface == NULL); + if (surface != text_input->pending_focused_surface) { + text_input_clear_pending_focused_surface(text_input); + } + } else if (text_input->input->focused_surface) { + assert(text_input->pending_focused_surface == NULL); + if (surface != text_input->input->focused_surface) { + relay_disable_text_input(relay, text_input); + wlr_text_input_v3_send_leave(text_input->input); + } + } else if (surface + && wl_resource_get_client(text_input->input->resource) + == wl_resource_get_client(surface->resource)) { + if (relay->input_method) { + wlr_text_input_v3_send_enter(text_input->input, surface); + } else { + text_input_set_pending_focused_surface(text_input, surface); + } + } + } +}