blob: a5b834ded5c58ddf218961ef93bb7f8f7cf14a48 [file] [log] [blame]
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +01001/*
2 * Copyright © 2012 Intel Corporation
3 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07004 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +010010 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070011 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +010022 */
23
Bryce Harringtonb4dae9b2016-06-15 18:13:07 -070024#include "config.h"
Kristian Høgsbergc7d2c4c2013-08-26 14:43:17 -070025
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030026#include <stdint.h>
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +010027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
Kristian Høgsberg1199b162012-11-27 13:41:48 -050030#include <unistd.h>
Antonio Borneo39578632019-04-26 23:57:31 +020031#include <errno.h>
Kristian Høgsberg1199b162012-11-27 13:41:48 -050032#include <sys/mman.h>
33
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +010034#include <linux/input.h>
35
36#include "window.h"
Jonas Ådahlb57f4722015-11-17 16:00:30 +080037#include "input-method-unstable-v1-client-protocol.h"
Bryce Harringtonc6ae8122016-08-30 18:38:27 -070038#include "shared/helpers.h"
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +010039
40enum compose_state {
41 state_normal,
42 state_compose
43};
44
45struct compose_seq {
46 uint32_t keys[4];
47
48 const char *text;
49};
50
Kristian Høgsberg504e8f12012-11-27 14:28:19 -050051struct simple_im;
52
53typedef void (*keyboard_input_key_handler_t)(struct simple_im *keyboard,
54 uint32_t serial,
55 uint32_t time, uint32_t key, uint32_t unicode,
56 enum wl_keyboard_key_state state);
57
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +010058struct simple_im {
Jonas Ådahlb57f4722015-11-17 16:00:30 +080059 struct zwp_input_method_v1 *input_method;
60 struct zwp_input_method_context_v1 *context;
Kristian Høgsbergde318ab2012-11-27 14:07:22 -050061 struct wl_display *display;
62 struct wl_registry *registry;
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +010063 struct wl_keyboard *keyboard;
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +010064 enum compose_state compose_state;
65 struct compose_seq compose_seq;
Kristian Høgsberg504e8f12012-11-27 14:28:19 -050066
67 struct xkb_context *xkb_context;
68
69 uint32_t modifiers;
70
71 struct xkb_keymap *keymap;
72 struct xkb_state *state;
73 xkb_mod_mask_t control_mask;
74 xkb_mod_mask_t alt_mask;
75 xkb_mod_mask_t shift_mask;
76
77 keyboard_input_key_handler_t key_handler;
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +010078
79 uint32_t serial;
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +010080};
81
82static const struct compose_seq compose_seqs[] = {
83 { { XKB_KEY_quotedbl, XKB_KEY_A, 0 }, "Ä" },
84 { { XKB_KEY_quotedbl, XKB_KEY_O, 0 }, "Ö" },
85 { { XKB_KEY_quotedbl, XKB_KEY_U, 0 }, "Ü" },
86 { { XKB_KEY_quotedbl, XKB_KEY_a, 0 }, "ä" },
87 { { XKB_KEY_quotedbl, XKB_KEY_o, 0 }, "ö" },
88 { { XKB_KEY_quotedbl, XKB_KEY_u, 0 }, "ü" },
89 { { XKB_KEY_apostrophe, XKB_KEY_A, 0 }, "Á" },
90 { { XKB_KEY_apostrophe, XKB_KEY_a, 0 }, "á" },
Kristian Høgsbergb88b68f2012-11-27 15:11:04 -050091 { { XKB_KEY_slash, XKB_KEY_O, 0 }, "Ø" },
92 { { XKB_KEY_slash, XKB_KEY_o, 0 }, "ø" },
93 { { XKB_KEY_less, XKB_KEY_3, 0 }, "♥" },
94 { { XKB_KEY_A, XKB_KEY_A, 0 }, "Å" },
95 { { XKB_KEY_A, XKB_KEY_E, 0 }, "Æ" },
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +010096 { { XKB_KEY_O, XKB_KEY_C, 0 }, "©" },
97 { { XKB_KEY_O, XKB_KEY_R, 0 }, "®" },
98 { { XKB_KEY_s, XKB_KEY_s, 0 }, "ß" },
Kristian Høgsbergb88b68f2012-11-27 15:11:04 -050099 { { XKB_KEY_a, XKB_KEY_e, 0 }, "æ" },
100 { { XKB_KEY_a, XKB_KEY_a, 0 }, "å" },
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100101};
102
103static const uint32_t ignore_keys_on_compose[] = {
104 XKB_KEY_Shift_L,
105 XKB_KEY_Shift_R
106};
107
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100108static void
Jan Arne Petersenece6b5a2013-04-18 16:47:15 +0200109handle_surrounding_text(void *data,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800110 struct zwp_input_method_context_v1 *context,
Jan Arne Petersenece6b5a2013-04-18 16:47:15 +0200111 const char *text,
112 uint32_t cursor,
113 uint32_t anchor)
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100114{
115 fprintf(stderr, "Surrounding text updated: %s\n", text);
116}
117
118static void
Jan Arne Petersenece6b5a2013-04-18 16:47:15 +0200119handle_reset(void *data,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800120 struct zwp_input_method_context_v1 *context)
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100121{
122 struct simple_im *keyboard = data;
123
124 fprintf(stderr, "Reset pre-edit buffer\n");
125
126 keyboard->compose_state = state_normal;
127}
128
Jan Arne Petersenab2b0142013-01-16 21:26:49 +0100129static void
Jan Arne Petersenece6b5a2013-04-18 16:47:15 +0200130handle_content_type(void *data,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800131 struct zwp_input_method_context_v1 *context,
Jan Arne Petersenece6b5a2013-04-18 16:47:15 +0200132 uint32_t hint,
133 uint32_t purpose)
Jan Arne Petersenab2b0142013-01-16 21:26:49 +0100134{
135}
136
137static void
Jan Arne Petersenece6b5a2013-04-18 16:47:15 +0200138handle_invoke_action(void *data,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800139 struct zwp_input_method_context_v1 *context,
Jan Arne Petersenece6b5a2013-04-18 16:47:15 +0200140 uint32_t button,
141 uint32_t index)
Jan Arne Petersenab2b0142013-01-16 21:26:49 +0100142{
143}
144
Jan Arne Petersen0eabcaa2013-01-31 15:52:20 +0100145static void
Jan Arne Petersen00191c72013-04-18 16:47:33 +0200146handle_commit_state(void *data,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800147 struct zwp_input_method_context_v1 *context,
Jan Arne Petersen00191c72013-04-18 16:47:33 +0200148 uint32_t serial)
Jan Arne Petersenece6b5a2013-04-18 16:47:15 +0200149{
Jan Arne Petersen00191c72013-04-18 16:47:33 +0200150 struct simple_im *keyboard = data;
151
152 keyboard->serial = serial;
Jan Arne Petersenece6b5a2013-04-18 16:47:15 +0200153}
154
155static void
156handle_preferred_language(void *data,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800157 struct zwp_input_method_context_v1 *context,
Jan Arne Petersenece6b5a2013-04-18 16:47:15 +0200158 const char *language)
Jan Arne Petersen0eabcaa2013-01-31 15:52:20 +0100159{
160}
161
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800162static const struct zwp_input_method_context_v1_listener input_method_context_listener = {
Jan Arne Petersenece6b5a2013-04-18 16:47:15 +0200163 handle_surrounding_text,
164 handle_reset,
165 handle_content_type,
166 handle_invoke_action,
Jan Arne Petersen00191c72013-04-18 16:47:33 +0200167 handle_commit_state,
Jan Arne Petersenece6b5a2013-04-18 16:47:15 +0200168 handle_preferred_language
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100169};
170
171static void
172input_method_keyboard_keymap(void *data,
173 struct wl_keyboard *wl_keyboard,
174 uint32_t format,
175 int32_t fd,
176 uint32_t size)
177{
178 struct simple_im *keyboard = data;
Kristian Høgsbergaec12b82012-11-27 14:21:34 -0500179 char *map_str;
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100180
Kristian Høgsbergaec12b82012-11-27 14:21:34 -0500181 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
182 close(fd);
183 return;
184 }
185
186 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
187 if (map_str == MAP_FAILED) {
188 close(fd);
189 return;
190 }
191
Kristian Høgsberg504e8f12012-11-27 14:28:19 -0500192 keyboard->keymap =
Ran Benita2e1968f2014-08-19 23:59:51 +0300193 xkb_keymap_new_from_string(keyboard->xkb_context,
194 map_str,
195 XKB_KEYMAP_FORMAT_TEXT_V1,
Bryce Harrington4c66a902016-10-11 16:17:39 -0700196 XKB_KEYMAP_COMPILE_NO_FLAGS);
Kristian Høgsbergaec12b82012-11-27 14:21:34 -0500197
198 munmap(map_str, size);
199 close(fd);
200
Kristian Høgsberg504e8f12012-11-27 14:28:19 -0500201 if (!keyboard->keymap) {
Bryce Harringtoncd7df632016-06-15 17:59:17 -0700202 fprintf(stderr, "Failed to compile keymap\n");
Kristian Høgsbergaec12b82012-11-27 14:21:34 -0500203 return;
204 }
205
Kristian Høgsberg504e8f12012-11-27 14:28:19 -0500206 keyboard->state = xkb_state_new(keyboard->keymap);
207 if (!keyboard->state) {
Bryce Harringtoncd7df632016-06-15 17:59:17 -0700208 fprintf(stderr, "Failed to create XKB state\n");
Ran Benita2e1968f2014-08-19 23:59:51 +0300209 xkb_keymap_unref(keyboard->keymap);
Kristian Høgsbergaec12b82012-11-27 14:21:34 -0500210 return;
211 }
212
Kristian Høgsberg504e8f12012-11-27 14:28:19 -0500213 keyboard->control_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +0300214 1 << xkb_keymap_mod_get_index(keyboard->keymap, "Control");
Kristian Høgsberg504e8f12012-11-27 14:28:19 -0500215 keyboard->alt_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +0300216 1 << xkb_keymap_mod_get_index(keyboard->keymap, "Mod1");
Kristian Høgsberg504e8f12012-11-27 14:28:19 -0500217 keyboard->shift_mask =
Ran Benita2e1968f2014-08-19 23:59:51 +0300218 1 << xkb_keymap_mod_get_index(keyboard->keymap, "Shift");
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100219}
220
221static void
222input_method_keyboard_key(void *data,
223 struct wl_keyboard *wl_keyboard,
224 uint32_t serial,
225 uint32_t time,
226 uint32_t key,
227 uint32_t state_w)
228{
229 struct simple_im *keyboard = data;
Kristian Høgsberg6aae6142012-11-27 14:20:31 -0500230 uint32_t code;
231 uint32_t num_syms;
232 const xkb_keysym_t *syms;
233 xkb_keysym_t sym;
234 enum wl_keyboard_key_state state = state_w;
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100235
Kristian Høgsberg504e8f12012-11-27 14:28:19 -0500236 if (!keyboard->state)
Kristian Høgsberg6aae6142012-11-27 14:20:31 -0500237 return;
238
239 code = key + 8;
Ran Benita2e1968f2014-08-19 23:59:51 +0300240 num_syms = xkb_state_key_get_syms(keyboard->state, code, &syms);
Kristian Høgsberg6aae6142012-11-27 14:20:31 -0500241
242 sym = XKB_KEY_NoSymbol;
243 if (num_syms == 1)
244 sym = syms[0];
245
Kristian Høgsberg504e8f12012-11-27 14:28:19 -0500246 if (keyboard->key_handler)
247 (*keyboard->key_handler)(keyboard, serial, time, key, sym,
248 state);
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100249}
250
251static void
252input_method_keyboard_modifiers(void *data,
253 struct wl_keyboard *wl_keyboard,
254 uint32_t serial,
255 uint32_t mods_depressed,
256 uint32_t mods_latched,
257 uint32_t mods_locked,
258 uint32_t group)
259{
260 struct simple_im *keyboard = data;
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800261 struct zwp_input_method_context_v1 *context = keyboard->context;
Kristian Høgsberg3a3704d2012-11-27 14:18:40 -0500262 xkb_mod_mask_t mask;
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100263
Kristian Høgsberg504e8f12012-11-27 14:28:19 -0500264 xkb_state_update_mask(keyboard->state, mods_depressed,
Kristian Høgsberg3a3704d2012-11-27 14:18:40 -0500265 mods_latched, mods_locked, 0, 0, group);
Kristian Høgsberg504e8f12012-11-27 14:28:19 -0500266 mask = xkb_state_serialize_mods(keyboard->state,
Ran Benita2e1968f2014-08-19 23:59:51 +0300267 XKB_STATE_MODS_DEPRESSED |
268 XKB_STATE_MODS_LATCHED);
Kristian Høgsberg3a3704d2012-11-27 14:18:40 -0500269
Kristian Høgsberg504e8f12012-11-27 14:28:19 -0500270 keyboard->modifiers = 0;
271 if (mask & keyboard->control_mask)
272 keyboard->modifiers |= MOD_CONTROL_MASK;
273 if (mask & keyboard->alt_mask)
274 keyboard->modifiers |= MOD_ALT_MASK;
275 if (mask & keyboard->shift_mask)
276 keyboard->modifiers |= MOD_SHIFT_MASK;
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100277
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800278 zwp_input_method_context_v1_modifiers(context, serial,
279 mods_depressed, mods_depressed,
280 mods_latched, group);
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100281}
282
283static const struct wl_keyboard_listener input_method_keyboard_listener = {
284 input_method_keyboard_keymap,
285 NULL, /* enter */
286 NULL, /* leave */
287 input_method_keyboard_key,
288 input_method_keyboard_modifiers
289};
290
291static void
292input_method_activate(void *data,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800293 struct zwp_input_method_v1 *input_method,
294 struct zwp_input_method_context_v1 *context)
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100295{
296 struct simple_im *keyboard = data;
297
298 if (keyboard->context)
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800299 zwp_input_method_context_v1_destroy(keyboard->context);
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100300
301 keyboard->compose_state = state_normal;
302
Jan Arne Petersen00191c72013-04-18 16:47:33 +0200303 keyboard->serial = 0;
Jan Arne Petersenc7d2a982013-01-16 21:26:39 +0100304
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100305 keyboard->context = context;
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800306 zwp_input_method_context_v1_add_listener(context,
307 &input_method_context_listener,
308 keyboard);
309 keyboard->keyboard = zwp_input_method_context_v1_grab_keyboard(context);
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100310 wl_keyboard_add_listener(keyboard->keyboard,
311 &input_method_keyboard_listener,
312 keyboard);
313}
314
315static void
316input_method_deactivate(void *data,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800317 struct zwp_input_method_v1 *input_method,
318 struct zwp_input_method_context_v1 *context)
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100319{
320 struct simple_im *keyboard = data;
321
322 if (!keyboard->context)
323 return;
324
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800325 zwp_input_method_context_v1_destroy(keyboard->context);
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100326 keyboard->context = NULL;
327}
328
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800329static const struct zwp_input_method_v1_listener input_method_listener = {
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100330 input_method_activate,
331 input_method_deactivate
332};
333
334static void
Kristian Høgsbergde318ab2012-11-27 14:07:22 -0500335registry_handle_global(void *data, struct wl_registry *registry,
336 uint32_t name, const char *interface, uint32_t version)
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100337{
338 struct simple_im *keyboard = data;
339
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800340 if (!strcmp(interface, "zwp_input_method_v1")) {
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100341 keyboard->input_method =
Kristian Høgsbergde318ab2012-11-27 14:07:22 -0500342 wl_registry_bind(registry, name,
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800343 &zwp_input_method_v1_interface, 1);
344 zwp_input_method_v1_add_listener(keyboard->input_method,
345 &input_method_listener, keyboard);
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100346 }
347}
348
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200349static void
350registry_handle_global_remove(void *data, struct wl_registry *registry,
351 uint32_t name)
352{
353}
354
Kristian Høgsbergde318ab2012-11-27 14:07:22 -0500355static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200356 registry_handle_global,
357 registry_handle_global_remove
Kristian Høgsbergde318ab2012-11-27 14:07:22 -0500358};
359
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100360static int
361compare_compose_keys(const void *c1, const void *c2)
362{
363 const struct compose_seq *cs1 = c1;
364 const struct compose_seq *cs2 = c2;
365 int i;
366
367 for (i = 0; cs1->keys[i] != 0 && cs2->keys[i] != 0; i++) {
368 if (cs1->keys[i] != cs2->keys[i])
369 return cs1->keys[i] - cs2->keys[i];
370 }
371
372 if (cs1->keys[i] == cs2->keys[i]
373 || cs1->keys[i] == 0)
374 return 0;
375
376 return cs1->keys[i] - cs2->keys[i];
377}
378
379static void
Kristian Høgsberg504e8f12012-11-27 14:28:19 -0500380simple_im_key_handler(struct simple_im *keyboard,
Kristian Høgsberg79bfde22012-11-27 13:57:27 -0500381 uint32_t serial, uint32_t time, uint32_t key, uint32_t sym,
Kristian Høgsberg504e8f12012-11-27 14:28:19 -0500382 enum wl_keyboard_key_state state)
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100383{
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800384 struct zwp_input_method_context_v1 *context = keyboard->context;
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100385 char text[64];
386
387 if (sym == XKB_KEY_Multi_key &&
388 state == WL_KEYBOARD_KEY_STATE_RELEASED &&
389 keyboard->compose_state == state_normal) {
390 keyboard->compose_state = state_compose;
391 memset(&keyboard->compose_seq, 0, sizeof(struct compose_seq));
392 return;
393 }
394
395 if (keyboard->compose_state == state_compose) {
396 uint32_t i = 0;
397 struct compose_seq *cs;
398
399 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
400 return;
401
Bryce Harringtonc6ae8122016-08-30 18:38:27 -0700402 for (i = 0; i < ARRAY_LENGTH(ignore_keys_on_compose); i++) {
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100403 if (sym == ignore_keys_on_compose[i]) {
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800404 zwp_input_method_context_v1_key(context,
405 keyboard->serial,
406 time,
407 key,
408 state);
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100409 return;
410 }
411 }
412
413 for (i = 0; keyboard->compose_seq.keys[i] != 0; i++);
414
415 keyboard->compose_seq.keys[i] = sym;
416
417 cs = bsearch (&keyboard->compose_seq, compose_seqs,
Bryce Harringtonc6ae8122016-08-30 18:38:27 -0700418 ARRAY_LENGTH(compose_seqs),
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100419 sizeof(compose_seqs[0]), compare_compose_keys);
420
421 if (cs) {
422 if (cs->keys[i + 1] == 0) {
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800423 zwp_input_method_context_v1_preedit_cursor(keyboard->context,
424 0);
425 zwp_input_method_context_v1_preedit_string(keyboard->context,
426 keyboard->serial,
427 "", "");
428 zwp_input_method_context_v1_cursor_position(keyboard->context,
429 0, 0);
430 zwp_input_method_context_v1_commit_string(keyboard->context,
431 keyboard->serial,
432 cs->text);
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100433 keyboard->compose_state = state_normal;
434 } else {
435 uint32_t j = 0, idx = 0;
436
437 for (; j <= i; j++) {
438 idx += xkb_keysym_to_utf8(cs->keys[j], text + idx, sizeof(text) - idx);
439 }
440
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800441 zwp_input_method_context_v1_preedit_cursor(keyboard->context,
442 strlen(text));
443 zwp_input_method_context_v1_preedit_string(keyboard->context,
444 keyboard->serial,
445 text,
446 text);
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100447 }
448 } else {
449 uint32_t j = 0, idx = 0;
450
451 for (; j <= i; j++) {
452 idx += xkb_keysym_to_utf8(keyboard->compose_seq.keys[j], text + idx, sizeof(text) - idx);
453 }
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800454 zwp_input_method_context_v1_preedit_cursor(keyboard->context,
455 0);
456 zwp_input_method_context_v1_preedit_string(keyboard->context,
457 keyboard->serial,
458 "", "");
459 zwp_input_method_context_v1_cursor_position(keyboard->context,
460 0, 0);
461 zwp_input_method_context_v1_commit_string(keyboard->context,
462 keyboard->serial,
463 text);
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100464 keyboard->compose_state = state_normal;
465 }
466 return;
467 }
468
469 if (xkb_keysym_to_utf8(sym, text, sizeof(text)) <= 0) {
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800470 zwp_input_method_context_v1_key(context, serial, time, key, state);
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100471 return;
472 }
473
474 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
475 return;
476
Jonas Ådahlb57f4722015-11-17 16:00:30 +0800477 zwp_input_method_context_v1_cursor_position(keyboard->context,
478 0, 0);
479 zwp_input_method_context_v1_commit_string(keyboard->context,
480 keyboard->serial,
481 text);
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100482}
483
484int
485main(int argc, char *argv[])
486{
487 struct simple_im simple_im;
Kristian Høgsbergde318ab2012-11-27 14:07:22 -0500488 int ret = 0;
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100489
490 memset(&simple_im, 0, sizeof(simple_im));
491
Kristian Høgsbergde318ab2012-11-27 14:07:22 -0500492 simple_im.display = wl_display_connect(NULL);
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100493 if (simple_im.display == NULL) {
Antonio Borneo39578632019-04-26 23:57:31 +0200494 fprintf(stderr, "Failed to connect to server: %s\n",
495 strerror(errno));
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100496 return -1;
497 }
498
Kristian Høgsbergde318ab2012-11-27 14:07:22 -0500499 simple_im.registry = wl_display_get_registry(simple_im.display);
500 wl_registry_add_listener(simple_im.registry,
501 &registry_listener, &simple_im);
502 wl_display_roundtrip(simple_im.display);
503 if (simple_im.input_method == NULL) {
504 fprintf(stderr, "No input_method global\n");
Bryce Harrington82241712016-10-11 16:08:10 -0700505 return -1;
Kristian Høgsbergde318ab2012-11-27 14:07:22 -0500506 }
507
Bryce Harrington4c66a902016-10-11 16:17:39 -0700508 simple_im.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
Kristian Høgsberg8c036162012-11-27 13:47:16 -0500509 if (simple_im.xkb_context == NULL) {
510 fprintf(stderr, "Failed to create XKB context\n");
511 return -1;
512 }
513
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100514 simple_im.context = NULL;
Kristian Høgsberg504e8f12012-11-27 14:28:19 -0500515 simple_im.key_handler = simple_im_key_handler;
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100516
Kristian Høgsbergde318ab2012-11-27 14:07:22 -0500517 while (ret != -1)
518 ret = wl_display_dispatch(simple_im.display);
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100519
Kristian Høgsbergde318ab2012-11-27 14:07:22 -0500520 if (ret == -1) {
Antonio Borneo39578632019-04-26 23:57:31 +0200521 fprintf(stderr, "Dispatch error: %s\n", strerror(errno));
Bryce Harrington82241712016-10-11 16:08:10 -0700522 return -1;
Kristian Høgsbergde318ab2012-11-27 14:07:22 -0500523 }
Jan Arne Petersene9fba2b2012-11-18 19:06:50 +0100524
525 return 0;
526}