summarylogtreecommitdiffstats
path: root/0001-IME-support-text-input-v1.patch
blob: b33bacd06eb38948b6dbd737ad94aff451cedeee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
From 948b3f6052b0174d983bf9003e6c53cafbc93122 Mon Sep 17 00:00:00 2001
From: tokyo4j <hrak1529@gmail.com>
Date: Fri, 23 Feb 2024 03:57:01 +0900
Subject: [PATCH] IME: support text-input-v1

---
 include/input/ime.h   |   1 +
 protocols/meson.build |   1 +
 src/input/ime.c       | 342 +++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 343 insertions(+), 1 deletion(-)

diff --git a/include/input/ime.h b/include/input/ime.h
index 189f51f..6a5e95f 100644
--- a/include/input/ime.h
+++ b/include/input/ime.h
@@ -57,6 +57,7 @@ struct input_method_popup {
 struct text_input {
 	struct input_method_relay *relay;
 	struct wlr_text_input_v3 *input;
+	bool v1;
 	struct wl_list link;
 
 	struct wl_listener enable;
diff --git a/protocols/meson.build b/protocols/meson.build
index 711a9ec..712f570 100644
--- a/protocols/meson.build
+++ b/protocols/meson.build
@@ -21,6 +21,7 @@ server_protocols = [
 	wl_protocol_dir / 'staging/drm-lease/drm-lease-v1.xml',
 	wl_protocol_dir / 'staging/xwayland-shell/xwayland-shell-v1.xml',
 	wl_protocol_dir / 'staging/tearing-control/tearing-control-v1.xml',
+	wl_protocol_dir / 'unstable/text-input/text-input-unstable-v1.xml',
 	'cosmic-workspace-unstable-v1.xml',
 	'wlr-layer-shell-unstable-v1.xml',
 	'wlr-input-inhibitor-unstable-v1.xml',
diff --git a/src/input/ime.c b/src/input/ime.c
index c09ef94..592bd9b 100644
--- a/src/input/ime.c
+++ b/src/input/ime.c
@@ -6,6 +6,7 @@
 #include "input/ime.h"
 #include "node.h"
 #include "view.h"
+#include "text-input-unstable-v1-protocol.h"
 
 #define SAME_CLIENT(wlr_obj1, wlr_obj2) \
 	(wl_resource_get_client((wlr_obj1)->resource) \
@@ -25,6 +26,276 @@ is_keyboard_emulated_by_input_method(struct wlr_keyboard *keyboard,
 	return virtual_keyboard && SAME_CLIENT(virtual_keyboard, input_method);
 }
 
+/*
+ * ----------------------------------------------------------------------------
+ *                 text-input-unstable-v1 protocol support
+ * ----------------------------------------------------------------------------
+ * To reuse code for text-input-unstable-v3, zwp_text_input_v1 interface is
+ * wrapped as wlr_text_input_v3, by converting zwp_text_input_v1-specific
+ * events and emitting them as wlr_text_input_v3's events. When
+ * zwp_text_input_manager_v1.create_text_input event is received, a wrapped
+ * wlr_text_input_v3 is created and added to
+ * wlr_text_input_manager_v3.text_inputs.
+ * ----------------------------------------------------------------------------
+ */
+
+struct text_input_manager_v1 {
+	struct wl_global *global;
+	struct wlr_text_input_manager_v3 *wlr_manager_v3;
+	struct wl_listener display_destroy;
+};
+
+static void
+text_input_v1_clear_focused_surface(struct wlr_text_input_v3 *text_input)
+{
+	if (text_input->focused_surface) {
+		wl_list_remove(&text_input->surface_destroy.link);
+	}
+	text_input->focused_surface = NULL;
+}
+
+static void
+text_input_v1_clear_seat(struct wlr_text_input_v3 *text_input)
+{
+	if (text_input->seat) {
+		wl_list_remove(&text_input->seat_destroy.link);
+	}
+	text_input->seat = NULL;
+}
+
+static void
+text_input_v1_destroy(struct wlr_text_input_v3 *text_input)
+{
+	wl_signal_emit_mutable(&text_input->events.destroy, NULL);
+
+	text_input_v1_clear_focused_surface(text_input);
+	text_input_v1_clear_seat(text_input);
+
+	wl_list_remove(&text_input->link);
+	free(text_input->current.surrounding.text);
+	free(text_input);
+}
+
+static void
+handle_text_input_v1_focused_surface_destroy(struct wl_listener *listener,
+		void *data)
+{
+	struct wlr_text_input_v3 *text_input =
+		wl_container_of(listener, text_input, surface_destroy);
+	text_input_v1_clear_focused_surface(text_input);
+}
+
+static void
+handle_text_input_v1_activate(struct wl_client *client,
+		struct wl_resource *resource, struct wl_resource *seat,
+		struct wl_resource *surface)
+{
+	struct wlr_text_input_v3 *text_input = wl_resource_get_user_data(resource);
+	struct wlr_seat_client *seat_client = wlr_seat_client_from_resource(seat);
+
+	text_input->seat = seat_client->seat;
+	wl_signal_add(&seat_client->events.destroy, &text_input->seat_destroy);
+
+	text_input->current_enabled = true;
+	text_input->active_features = text_input->current.features;
+	wl_signal_emit_mutable(&text_input->events.enable, NULL);
+}
+
+static void
+handle_text_input_v1_deactivate(struct wl_client *client,
+		struct wl_resource *resource, struct wl_resource *seat)
+{
+	struct wlr_text_input_v3 *text_input = wl_resource_get_user_data(resource);
+
+	text_input_v1_clear_seat(text_input);
+
+	text_input->current_enabled = false;
+	text_input->active_features = 0;
+	wl_signal_emit_mutable(&text_input->events.disable, NULL);
+}
+
+static void
+handle_text_input_v1_reset(struct wl_client *client, struct wl_resource *resource)
+{
+	struct wlr_text_input_v3 *text_input = wl_resource_get_user_data(resource);
+	/* Preserve cursor rectangle to prevent flickering */
+	text_input->current = (struct wlr_text_input_v3_state){
+		.cursor_rectangle = text_input->current.cursor_rectangle,
+		.features = text_input->current.features
+			& WLR_TEXT_INPUT_V3_FEATURE_CURSOR_RECTANGLE,
+	};
+	free(text_input->current.surrounding.text);
+	wl_signal_emit_mutable(&text_input->events.commit, NULL);
+}
+
+static void
+handle_text_input_v1_set_surrounding_text(struct wl_client *client,
+		struct wl_resource *resource, const char *text,
+		uint32_t cursor, uint32_t anchor)
+{
+	struct wlr_text_input_v3 *text_input = wl_resource_get_user_data(resource);
+
+	free(text_input->current.surrounding.text);
+	text_input->current.surrounding.text = xstrdup(text);
+	text_input->current.surrounding.cursor = cursor;
+	text_input->current.surrounding.anchor = anchor;
+	text_input->current.features |= WLR_TEXT_INPUT_V3_FEATURE_SURROUNDING_TEXT;
+	text_input->active_features = text_input->current.features;
+	wl_signal_emit_mutable(&text_input->events.commit, NULL);
+}
+
+static void
+handle_text_input_v1_set_cursor_rectangle(struct wl_client *client,
+		struct wl_resource *resource, int32_t x, int32_t y,
+		int32_t width, int32_t height)
+{
+	struct wlr_text_input_v3 *text_input = wl_resource_get_user_data(resource);
+	text_input->current.cursor_rectangle.x = x;
+	text_input->current.cursor_rectangle.y = y;
+	text_input->current.cursor_rectangle.width = width;
+	text_input->current.cursor_rectangle.height = height;
+	text_input->current.features |= WLR_TEXT_INPUT_V3_FEATURE_CURSOR_RECTANGLE;
+	text_input->active_features = text_input->current.features;
+	wl_signal_emit_mutable(&text_input->events.commit, NULL);
+}
+
+static void
+handle_text_input_v1_commit_state(struct wl_client *client,
+		struct wl_resource *resource, uint32_t serial)
+{
+	struct wlr_text_input_v3 *text_input = wl_resource_get_user_data(resource);
+	text_input->current_serial = serial;
+}
+
+static void noop(void){}
+static const struct zwp_text_input_v1_interface text_input_impl = {
+	.activate = handle_text_input_v1_activate,
+	.deactivate = handle_text_input_v1_deactivate,
+	.show_input_panel = (__typeof__(text_input_impl.show_input_panel))noop,
+	.hide_input_panel = (__typeof__(text_input_impl.hide_input_panel))noop,
+	.reset = handle_text_input_v1_reset,
+	.set_surrounding_text = handle_text_input_v1_set_surrounding_text,
+	.set_content_type = (__typeof__(text_input_impl.set_content_type))noop,
+	.set_cursor_rectangle = handle_text_input_v1_set_cursor_rectangle,
+	.set_preferred_language = (__typeof__(text_input_impl.set_preferred_language))noop,
+	.commit_state = handle_text_input_v1_commit_state,
+	.invoke_action = (__typeof__(text_input_impl.invoke_action))noop,
+};
+
+static void
+handle_text_input_v1_resource_destroy(struct wl_resource *resource)
+{
+	struct wlr_text_input_v3 *text_input = wl_resource_get_user_data(resource);
+	if (!text_input) {
+		return;
+	}
+	text_input_v1_destroy(text_input);
+	wl_resource_set_user_data(resource, NULL);
+}
+
+static void
+handle_text_input_v1_seat_destroy(struct wl_listener *listener, void *data)
+{
+	struct wlr_text_input_v3 *text_input =
+		wl_container_of(listener, text_input, seat_destroy);
+	if (!text_input) {
+		return;
+	}
+	struct wl_resource *resource = text_input->resource;
+	text_input_v1_destroy(text_input);
+	wl_resource_set_user_data(resource, NULL);
+}
+
+static void
+handle_manager_v1_create_text_input(struct wl_client *client,
+		struct wl_resource *resource, uint32_t id)
+{
+	int version = wl_resource_get_version(resource);
+	struct wl_resource *text_input_resource = wl_resource_create(client,
+		&zwp_text_input_v1_interface, version, id);
+	struct text_input_manager_v1 *manager_v1 = wl_resource_get_user_data(resource);
+	struct wlr_text_input_manager_v3 *wlr_manager_v3 = manager_v1->wlr_manager_v3;
+
+	struct wlr_text_input_v3 *text_input = znew(*text_input);
+	text_input->resource = text_input_resource;
+
+	wl_signal_init(&text_input->events.commit);
+	wl_signal_init(&text_input->events.enable);
+	wl_signal_init(&text_input->events.disable);
+	wl_signal_init(&text_input->events.destroy);
+
+	wl_resource_set_implementation(text_input_resource, &text_input_impl,
+		text_input, handle_text_input_v1_resource_destroy);
+
+	text_input->seat_destroy.notify =
+		handle_text_input_v1_seat_destroy;
+	text_input->surface_destroy.notify =
+		handle_text_input_v1_focused_surface_destroy;
+
+	wl_list_insert(&wlr_manager_v3->text_inputs, &text_input->link);
+
+	wl_signal_emit_mutable(&wlr_manager_v3->events.text_input, text_input);
+}
+
+static const struct zwp_text_input_manager_v1_interface text_input_manager_v1_impl = {
+	.create_text_input = handle_manager_v1_create_text_input,
+};
+
+static void
+handle_manager_v1_bind(struct wl_client *wl_client, void *data,
+		uint32_t version, uint32_t id)
+{
+	struct text_input_manager_v1 *manager_v1 = data;
+
+	struct wl_resource *resource = wl_resource_create(wl_client,
+		&zwp_text_input_manager_v1_interface, version, id);
+	wl_resource_set_implementation(resource, &text_input_manager_v1_impl,
+		manager_v1, NULL);
+}
+
+static void
+handle_display_destroy(struct wl_listener *listener, void *data)
+{
+	struct text_input_manager_v1 *manager_v1 =
+		wl_container_of(listener, manager_v1, display_destroy);
+	wl_list_remove(&manager_v1->display_destroy.link);
+	wl_global_destroy(manager_v1->global);
+	free(manager_v1);
+}
+
+static void
+text_input_manager_v1_init(struct wl_display *display,
+		struct wlr_text_input_manager_v3 *wlr_manager_v3)
+{
+	struct text_input_manager_v1 *manager_v1 = znew(*manager_v1);
+	manager_v1->wlr_manager_v3 = wlr_manager_v3;
+	manager_v1->global = wl_global_create(display,
+		&zwp_text_input_manager_v1_interface, 1, manager_v1,
+		handle_manager_v1_bind);
+	manager_v1->display_destroy.notify = handle_display_destroy;
+	wl_display_add_destroy_listener(display, &manager_v1->display_destroy);
+}
+
+static void
+text_input_v1_send_enter(struct wlr_text_input_v3 *text_input,
+		struct wlr_surface *surface)
+{
+	assert(!text_input->focused_surface);
+	text_input->focused_surface = surface;
+	wl_signal_add(&text_input->focused_surface->events.destroy,
+		&text_input->surface_destroy);
+	zwp_text_input_v1_send_enter(text_input->resource,
+		surface->resource);
+}
+
+static void
+text_input_v1_send_leave(struct wlr_text_input_v3 *text_input)
+{
+	assert(text_input->focused_surface);
+	text_input_v1_clear_focused_surface(text_input);
+	zwp_text_input_v1_send_leave(text_input->resource);
+}
+
 /*
  * Get keyboard grab of the seat from keyboard if we should forward events
  * to it.
@@ -155,9 +426,16 @@ update_text_inputs_focused_surface(struct input_method_relay *relay)
 			continue;
 		}
 		if (input->focused_surface) {
+			if (text_input->v1)
+				text_input_v1_send_leave(input);
+			else
 			wlr_text_input_v3_send_leave(input);
 		}
 		if (new_focused_surface) {
+			if (text_input->v1)
+				text_input_v1_send_enter(input,
+					new_focused_surface);
+			else
 			wlr_text_input_v3_send_enter(input, new_focused_surface);
 		}
 	}
@@ -274,6 +552,62 @@ handle_input_method_commit(struct wl_listener *listener, void *data)
 		return;
 	}
 
+	if (text_input->v1) {
+		/*
+		 * For some reason, if I send commit_string after preedit_string,
+		 * the text is doubled in the integrated terminal of VSCode.
+		 */
+		if (input_method->current.commit_text) {
+			zwp_text_input_v1_send_commit_string(
+				text_input->input->resource,
+				text_input->input->current_serial,
+				input_method->current.commit_text);
+		}
+		if (input_method->current.preedit.text) {
+			int preedit_len = strlen(input_method->current.preedit.text);
+			zwp_text_input_v1_send_preedit_cursor(
+				text_input->input->resource,
+				input_method->current.preedit.cursor_begin
+			);
+			if (0 < input_method->current.preedit.cursor_begin) {
+				zwp_text_input_v1_send_preedit_styling(
+					text_input->input->resource, 0,
+					input_method->current.preedit.cursor_begin,
+					ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_UNDERLINE);
+			}
+			if (input_method->current.preedit.cursor_begin
+					< input_method->current.preedit.cursor_end) {
+				zwp_text_input_v1_send_preedit_styling(
+					text_input->input->resource,
+					input_method->current.preedit.cursor_begin,
+					input_method->current.preedit.cursor_end
+						- input_method->current.preedit.cursor_begin,
+					ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_HIGHLIGHT);
+			}
+			if (input_method->current.preedit.cursor_end < preedit_len) {
+				zwp_text_input_v1_send_preedit_styling(
+					text_input->input->resource,
+					input_method->current.preedit.cursor_end,
+					preedit_len - input_method->current.preedit.cursor_end,
+					ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_UNDERLINE);
+			}
+			zwp_text_input_v1_send_preedit_string(
+				text_input->input->resource,
+				text_input->input->current_serial,
+				input_method->current.preedit.text,
+				input_method->current.preedit.text);
+		} else {
+			zwp_text_input_v1_send_preedit_cursor(
+				text_input->input->resource, 0);
+			zwp_text_input_v1_send_preedit_styling(
+				text_input->input->resource, 0, 0,
+				ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_NONE);
+			zwp_text_input_v1_send_preedit_string(
+				text_input->input->resource,
+				text_input->input->current_serial, "", "");
+		}
+		return;
+	}
 	if (input_method->current.preedit.text) {
 		wlr_text_input_v3_send_preedit_string(
 			text_input->input,
@@ -523,11 +857,14 @@ handle_new_text_input(struct wl_listener *listener, void *data)
 	struct input_method_relay *relay =
 		wl_container_of(listener, relay, new_text_input);
 	struct wlr_text_input_v3 *wlr_text_input = data;
-	if (relay->seat->seat != wlr_text_input->seat) {
+	bool is_v1 = !strcmp("zwp_text_input_v1",
+		wl_resource_get_class(wlr_text_input->resource));
+	if (!is_v1 && relay->seat->seat != wlr_text_input->seat) {
 		return;
 	}
 
 	struct text_input *text_input = znew(*text_input);
+	text_input->v1 = is_v1;
 	text_input->input = wlr_text_input;
 	text_input->relay = relay;
 	wl_list_insert(&relay->text_inputs, &text_input->link);
@@ -581,6 +918,9 @@ input_method_relay_create(struct seat *seat)
 
 	relay->focused_surface_destroy.notify = handle_focused_surface_destroy;
 
+	text_input_manager_v1_init(seat->server->wl_display,
+		seat->server->text_input_manager);
+
 	return relay;
 }
 
-- 
2.46.2