diff options
author | Dorota Czaplejewicz <dorota.czaplejewicz@puri.sm> | 2018-08-23 11:38:39 +0200 |
---|---|---|
committer | Dorota Czaplejewicz <dorota.czaplejewicz@puri.sm> | 2018-10-09 09:56:46 +0000 |
commit | cec747111946f1d51ae1c9ec14b683aa3d5dcfc4 (patch) | |
tree | b2617b1a7a541a15e924feb167c08b48118c8f8a | |
parent | 174e8a48aaa09908ff3970093e3a0c0d29afaf1b (diff) |
wlroots: add basic support for zwp_input_method_v2
Implemented basic input method functionality. Not included: popups, grabbing.
-rw-r--r-- | examples/input-method.c | 400 | ||||
-rw-r--r-- | examples/meson.build | 4 | ||||
-rw-r--r-- | include/wlr/types/meson.build | 3 | ||||
-rw-r--r-- | include/wlr/types/wlr_input_method_v2.h | 87 | ||||
-rw-r--r-- | protocol/input-method-unstable-v2.xml | 490 | ||||
-rw-r--r-- | protocol/meson.build | 2 | ||||
-rw-r--r-- | types/meson.build | 1 | ||||
-rw-r--r-- | types/wlr_input_method_v2.c | 297 |
8 files changed, 1283 insertions, 1 deletions
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 <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/epoll.h> +#include <sys/timerfd.h> +#include <unistd.h> +#include <wayland-client.h> +#include <wayland-egl.h> +#include <wlr/render/egl.h> +#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 <stdint.h> +#include <stdlib.h> +#include <wayland-server.h> +#include <wlr/types/wlr_seat.h> + +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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<protocol name="input_method_unstable_v2"> + + <copyright> + 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. + </copyright> + + <description summary="Protocol for creating input methods"> + 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. + </description> + + <interface name="zwp_input_method_v2" version="1"> + <description summary="input method"> + 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. + </description> + + <event name="activate"> + <description summary="input method has been requested"> + 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. + </description> + </event> + + <event name="deactivate"> + <description summary="deactivate event"> + 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. + </description> + </event> + + <event name="surrounding_text"> + <description summary="surrounding text event"> + 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. + </description> + <arg name="text" type="string"/> + <arg name="cursor" type="uint"/> + <arg name="anchor" type="uint"/> + </event> + + <event name="text_change_cause"> + <description summary="indicates the cause of surrounding text change"> + 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. + </description> + <arg name="cause" type="uint" enum="zwp_text_input_v3.change_cause"/> + </event> + + <event name="content_type"> + <description summary="content purpose and hint"> + 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. + </description> + <arg name="hint" type="uint" enum="zwp_text_input_v3.content_hint"/> + <arg name="purpose" type="uint" enum="zwp_text_input_v3.content_purpose"/> + </event> + + <event name="done"> + <description summary="apply state"> + 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. + </description> + </event> + + <request name="commit_string"> + <description summary="commit string"> + 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. + </description> + <arg name="text" type="string"/> + </request> + + <request name="set_preedit_string"> + <description summary="pre-edit 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. + </description> + <arg name="text" type="string"/> + <arg name="cursor_begin" type="int"/> + <arg name="cursor_end" type="int"/> + </request> + + <request name="delete_surrounding_text"> + <description summary="delete text"> + 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. + </description> + <arg name="before_length" type="uint"/> + <arg name="after_length" type="uint"/> + </request> + + <request name="commit"> + <description summary="apply state"> + 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. + </description> + <arg name="serial" type="uint"/> + </request> + + <request name="get_input_popup_surface"> + <description summary="create popup surface"> + 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. + </description> + <arg name="id" type="new_id" interface="zwp_input_popup_surface_v2"/> + <arg name="surface" type="object" interface="wl_surface"/> + </request> + + <request name="grab_keyboard"> + <description summary="grab hardware keyboard"> + 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. + </description> + <arg name="keyboard" type="new_id" + interface="zwp_input_method_keyboard_grab_v2"/> + </request> + + <event name="unavailable"> + <description summary="input method unavailable"> + 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. + </description> + </event> + + <request name="destroy" type="destructor"> + <description summary="destroy the text input"> + 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. + </description> + </request> + </interface> + + <interface name="zwp_input_popup_surface_v2" version="1"> + <description summary="popup surface"> + 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. + </description> + + <event name="text_input_rectangle"> + <description summary="set text input area position"> + 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. + </description> + <arg name="x" type="int"/> + <arg name="y" type="int"/> + <arg name="width" type="int"/> + <arg name="height" type="int"/> + </event> + + <request name="destroy" type="destructor"/> + </interface> + + <interface name="zwp_input_method_keyboard_grab_v2" version="1"> + <!-- Closely follows wl_keyboard version 6 --> + <description summary="keyboard grab"> + The zwp_input_method_keyboard_grab_v2 interface represents an exclusive + grab of the wl_keyboard interface associated with the seat. + </description> + + <event name="keymap"> + <description summary="keyboard mapping"> + This event provides a file descriptor to the client which can be + memory-mapped to provide a keyboard mapping description. + </description> + <arg name="format" type="uint" enum="wl_keyboard.keymap_format" + summary="keymap format"/> + <arg name="fd" type="fd" summary="keymap file descriptor"/> + <arg name="size" type="uint" summary="keymap size, in bytes"/> + </event> + + <event name="key"> + <description summary="key event"> + A key was pressed or released. + The time argument is a timestamp with millisecond granularity, with an + undefined base. + </description> + <arg name="serial" type="uint" summary="serial number of the key event"/> + <arg name="time" type="uint" summary="timestamp with millisecond granularity"/> + <arg name="key" type="uint" summary="key that produced the event"/> + <arg name="state" type="uint" enum="wl_keyboard.key_state" + summary="physical state of the key"/> + </event> + + <event name="modifiers"> + <description summary="modifier and group state"> + Notifies clients that the modifier and/or group state has changed, and + it should update its local state. + </description> + <arg name="serial" type="uint" summary="serial number of the modifiers event"/> + <arg name="mods_depressed" type="uint" summary="depressed modifiers"/> + <arg name="mods_latched" type="uint" summary="latched modifiers"/> + <arg name="mods_locked" type="uint" summary="locked modifiers"/> + <arg name="group" type="uint" summary="keyboard layout"/> + </event> + + <request name="release" type="destructor"> + <description summary="release the grab object"/> + </request> + + <event name="repeat_info"> + <description summary="repeat rate and delay"> + 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. + </description> + <arg name="rate" type="int" + summary="the rate of repeating keys in characters per second"/> + <arg name="delay" type="int" + summary="delay in milliseconds since key down until repeating starts"/> + </event> + </interface> + + <interface name="zwp_input_method_manager_v2" version="1"> + <description summary="input method manager"> + 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. + </description> + + <request name="get_input_method"> + <description summary="request an input method object"> + Request a new input zwp_input_method_v2 object associated with a given + seat. + </description> + <arg name="seat" type="object" interface="wl_seat"/> + <arg name="input_method" type="new_id" interface="zwp_input_method_v2"/> + </request> + + <request name="destroy" type="destructor"> + <description summary="destroy the input method manager"> + Destroys the zwp_input_method_manager_v2 object. + + The zwp_input_method_v2 objects originating from it remain valid. + </description> + </request> + </interface> +</protocol> 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 <assert.h> +#include <string.h> +#include <wayland-util.h> +#include <wlr/types/wlr_input_method_v2.h> +#include <wlr/types/wlr_surface.h> +#include <wlr/util/log.h> +#include <xkbcommon/xkbcommon.h> +#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); +} |