From 427735fcd9a11c3a3cdcfa5f48cffb55cf7a7b27 Mon Sep 17 00:00:00 2001 From: Dorota Czaplejewicz Date: Thu, 29 Mar 2018 19:41:33 +0200 Subject: [PATCH] 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); +}