From 427735fcd9a11c3a3cdcfa5f48cffb55cf7a7b27 Mon Sep 17 00:00:00 2001
From: Dorota Czaplejewicz <dorota.czaplejewicz@puri.sm>
Date: Thu, 29 Mar 2018 19:41:33 +0200
Subject: wlroots: add support for zwp_text_input_unstable_v3

---
 include/wlr/types/meson.build         |  1 +
 include/wlr/types/wlr_text_input_v3.h | 93 +++++++++++++++++++++++++++++++++++
 2 files changed, 94 insertions(+)
 create mode 100644 include/wlr/types/wlr_text_input_v3.h

(limited to 'include')

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 <wayland-server.h>
+#include <wlr/types/wlr_seat.h>
+#include <wlr/types/wlr_surface.h>
+
+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
-- 
cgit v1.2.3


From cec747111946f1d51ae1c9ec14b683aa3d5dcfc4 Mon Sep 17 00:00:00 2001
From: Dorota Czaplejewicz <dorota.czaplejewicz@puri.sm>
Date: Thu, 23 Aug 2018 11:38:39 +0200
Subject: 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

(limited to 'include')

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, &registry_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);
+}
-- 
cgit v1.2.3


From 585757d6e6acd7c8dd15073d849278498066dac1 Mon Sep 17 00:00:00 2001
From: Dorota Czaplejewicz <dorota.czaplejewicz@puri.sm>
Date: Thu, 23 Aug 2018 11:39:49 +0200
Subject: 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

(limited to 'include')

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 <wlr/types/wlr_idle_inhibit_v1.h>
 #include <wlr/types/wlr_idle.h>
 #include <wlr/types/wlr_input_inhibitor.h>
+#include <wlr/types/wlr_input_method_v2.h>
 #include <wlr/types/wlr_layer_shell_v1.h>
 #include <wlr/types/wlr_list.h>
 #include <wlr/types/wlr_output_layout.h>
@@ -16,6 +17,7 @@
 #include <wlr/types/wlr_primary_selection.h>
 #include <wlr/types/wlr_screencopy_v1.h>
 #include <wlr/types/wlr_screenshooter.h>
+#include <wlr/types/wlr_text_input_v3.h>
 #include <wlr/types/wlr_virtual_keyboard_v1.h>
 #include <wlr/types/wlr_wl_shell.h>
 #include <wlr/types/wlr_xcursor_manager.h>
@@ -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 <wlr/types/wlr_text_input_v3.h>
+#include <wlr/types/wlr_input_method_v2.h>
+#include <wlr/types/wlr_surface.h>
+#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 <assert.h>
+#include <stdlib.h>
+#include <wlr/util/log.h>
+#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);
+			}
+		}
+	}
+}
-- 
cgit v1.2.3