1 /*
2 * Copyright © 2012 Intel Corporation
3 *
4 * 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:
10 *
11 * 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.
22 */
23
24 #include <config.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/mman.h>
31
32
33 #include <linux/input.h>
34
35 #include "window.h"
36 #include "input-method-client-protocol.h"
37
38 enum compose_state {
39 state_normal,
40 state_compose
41 };
42
43 struct compose_seq {
44 uint32_t keys[4];
45
46 const char *text;
47 };
48
49 struct simple_im;
50
51 typedef void (*keyboard_input_key_handler_t)(struct simple_im *keyboard,
52 uint32_t serial,
53 uint32_t time, uint32_t key, uint32_t unicode,
54 enum wl_keyboard_key_state state);
55
56 struct simple_im {
57 struct wl_input_method *input_method;
58 struct wl_input_method_context *context;
59 struct wl_display *display;
60 struct wl_registry *registry;
61 struct wl_keyboard *keyboard;
62 enum compose_state compose_state;
63 struct compose_seq compose_seq;
64
65 struct xkb_context *xkb_context;
66
67 uint32_t modifiers;
68
69 struct xkb_keymap *keymap;
70 struct xkb_state *state;
71 xkb_mod_mask_t control_mask;
72 xkb_mod_mask_t alt_mask;
73 xkb_mod_mask_t shift_mask;
74
75 keyboard_input_key_handler_t key_handler;
76
77 uint32_t serial;
78 };
79
80 static const struct compose_seq compose_seqs[] = {
81 { { XKB_KEY_quotedbl, XKB_KEY_A, 0 }, "Ä" },
82 { { XKB_KEY_quotedbl, XKB_KEY_O, 0 }, "Ö" },
83 { { XKB_KEY_quotedbl, XKB_KEY_U, 0 }, "Ü" },
84 { { XKB_KEY_quotedbl, XKB_KEY_a, 0 }, "ä" },
85 { { XKB_KEY_quotedbl, XKB_KEY_o, 0 }, "ö" },
86 { { XKB_KEY_quotedbl, XKB_KEY_u, 0 }, "ü" },
87 { { XKB_KEY_apostrophe, XKB_KEY_A, 0 }, "Á" },
88 { { XKB_KEY_apostrophe, XKB_KEY_a, 0 }, "á" },
89 { { XKB_KEY_slash, XKB_KEY_O, 0 }, "Ø" },
90 { { XKB_KEY_slash, XKB_KEY_o, 0 }, "ø" },
91 { { XKB_KEY_less, XKB_KEY_3, 0 }, "♥" },
92 { { XKB_KEY_A, XKB_KEY_A, 0 }, "Å" },
93 { { XKB_KEY_A, XKB_KEY_E, 0 }, "Æ" },
94 { { XKB_KEY_O, XKB_KEY_C, 0 }, "©" },
95 { { XKB_KEY_O, XKB_KEY_R, 0 }, "®" },
96 { { XKB_KEY_s, XKB_KEY_s, 0 }, "ß" },
97 { { XKB_KEY_a, XKB_KEY_e, 0 }, "æ" },
98 { { XKB_KEY_a, XKB_KEY_a, 0 }, "å" },
99 };
100
101 static const uint32_t ignore_keys_on_compose[] = {
102 XKB_KEY_Shift_L,
103 XKB_KEY_Shift_R
104 };
105
106 static void
handle_surrounding_text(void * data,struct wl_input_method_context * context,const char * text,uint32_t cursor,uint32_t anchor)107 handle_surrounding_text(void *data,
108 struct wl_input_method_context *context,
109 const char *text,
110 uint32_t cursor,
111 uint32_t anchor)
112 {
113 fprintf(stderr, "Surrounding text updated: %s\n", text);
114 }
115
116 static void
handle_reset(void * data,struct wl_input_method_context * context)117 handle_reset(void *data,
118 struct wl_input_method_context *context)
119 {
120 struct simple_im *keyboard = data;
121
122 fprintf(stderr, "Reset pre-edit buffer\n");
123
124 keyboard->compose_state = state_normal;
125 }
126
127 static void
handle_content_type(void * data,struct wl_input_method_context * context,uint32_t hint,uint32_t purpose)128 handle_content_type(void *data,
129 struct wl_input_method_context *context,
130 uint32_t hint,
131 uint32_t purpose)
132 {
133 }
134
135 static void
handle_invoke_action(void * data,struct wl_input_method_context * context,uint32_t button,uint32_t index)136 handle_invoke_action(void *data,
137 struct wl_input_method_context *context,
138 uint32_t button,
139 uint32_t index)
140 {
141 }
142
143 static void
handle_commit_state(void * data,struct wl_input_method_context * context,uint32_t serial)144 handle_commit_state(void *data,
145 struct wl_input_method_context *context,
146 uint32_t serial)
147 {
148 struct simple_im *keyboard = data;
149
150 keyboard->serial = serial;
151 }
152
153 static void
handle_preferred_language(void * data,struct wl_input_method_context * context,const char * language)154 handle_preferred_language(void *data,
155 struct wl_input_method_context *context,
156 const char *language)
157 {
158 }
159
160 static const struct wl_input_method_context_listener input_method_context_listener = {
161 handle_surrounding_text,
162 handle_reset,
163 handle_content_type,
164 handle_invoke_action,
165 handle_commit_state,
166 handle_preferred_language
167 };
168
169 static void
input_method_keyboard_keymap(void * data,struct wl_keyboard * wl_keyboard,uint32_t format,int32_t fd,uint32_t size)170 input_method_keyboard_keymap(void *data,
171 struct wl_keyboard *wl_keyboard,
172 uint32_t format,
173 int32_t fd,
174 uint32_t size)
175 {
176 struct simple_im *keyboard = data;
177 char *map_str;
178
179 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
180 close(fd);
181 return;
182 }
183
184 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
185 if (map_str == MAP_FAILED) {
186 close(fd);
187 return;
188 }
189
190 keyboard->keymap =
191 xkb_keymap_new_from_string(keyboard->xkb_context,
192 map_str,
193 XKB_KEYMAP_FORMAT_TEXT_V1,
194 0);
195
196 munmap(map_str, size);
197 close(fd);
198
199 if (!keyboard->keymap) {
200 fprintf(stderr, "failed to compile keymap\n");
201 return;
202 }
203
204 keyboard->state = xkb_state_new(keyboard->keymap);
205 if (!keyboard->state) {
206 fprintf(stderr, "failed to create XKB state\n");
207 xkb_keymap_unref(keyboard->keymap);
208 return;
209 }
210
211 keyboard->control_mask =
212 1 << xkb_keymap_mod_get_index(keyboard->keymap, "Control");
213 keyboard->alt_mask =
214 1 << xkb_keymap_mod_get_index(keyboard->keymap, "Mod1");
215 keyboard->shift_mask =
216 1 << xkb_keymap_mod_get_index(keyboard->keymap, "Shift");
217 }
218
219 static void
input_method_keyboard_key(void * data,struct wl_keyboard * wl_keyboard,uint32_t serial,uint32_t time,uint32_t key,uint32_t state_w)220 input_method_keyboard_key(void *data,
221 struct wl_keyboard *wl_keyboard,
222 uint32_t serial,
223 uint32_t time,
224 uint32_t key,
225 uint32_t state_w)
226 {
227 struct simple_im *keyboard = data;
228 uint32_t code;
229 uint32_t num_syms;
230 const xkb_keysym_t *syms;
231 xkb_keysym_t sym;
232 enum wl_keyboard_key_state state = state_w;
233
234 if (!keyboard->state)
235 return;
236
237 code = key + 8;
238 num_syms = xkb_state_key_get_syms(keyboard->state, code, &syms);
239
240 sym = XKB_KEY_NoSymbol;
241 if (num_syms == 1)
242 sym = syms[0];
243
244 if (keyboard->key_handler)
245 (*keyboard->key_handler)(keyboard, serial, time, key, sym,
246 state);
247 }
248
249 static void
input_method_keyboard_modifiers(void * data,struct wl_keyboard * wl_keyboard,uint32_t serial,uint32_t mods_depressed,uint32_t mods_latched,uint32_t mods_locked,uint32_t group)250 input_method_keyboard_modifiers(void *data,
251 struct wl_keyboard *wl_keyboard,
252 uint32_t serial,
253 uint32_t mods_depressed,
254 uint32_t mods_latched,
255 uint32_t mods_locked,
256 uint32_t group)
257 {
258 struct simple_im *keyboard = data;
259 struct wl_input_method_context *context = keyboard->context;
260 xkb_mod_mask_t mask;
261
262 xkb_state_update_mask(keyboard->state, mods_depressed,
263 mods_latched, mods_locked, 0, 0, group);
264 mask = xkb_state_serialize_mods(keyboard->state,
265 XKB_STATE_MODS_DEPRESSED |
266 XKB_STATE_MODS_LATCHED);
267
268 keyboard->modifiers = 0;
269 if (mask & keyboard->control_mask)
270 keyboard->modifiers |= MOD_CONTROL_MASK;
271 if (mask & keyboard->alt_mask)
272 keyboard->modifiers |= MOD_ALT_MASK;
273 if (mask & keyboard->shift_mask)
274 keyboard->modifiers |= MOD_SHIFT_MASK;
275
276 wl_input_method_context_modifiers(context, serial,
277 mods_depressed, mods_depressed,
278 mods_latched, group);
279 }
280
281 static const struct wl_keyboard_listener input_method_keyboard_listener = {
282 input_method_keyboard_keymap,
283 NULL, /* enter */
284 NULL, /* leave */
285 input_method_keyboard_key,
286 input_method_keyboard_modifiers
287 };
288
289 static void
input_method_activate(void * data,struct wl_input_method * input_method,struct wl_input_method_context * context)290 input_method_activate(void *data,
291 struct wl_input_method *input_method,
292 struct wl_input_method_context *context)
293 {
294 struct simple_im *keyboard = data;
295
296 if (keyboard->context)
297 wl_input_method_context_destroy(keyboard->context);
298
299 keyboard->compose_state = state_normal;
300
301 keyboard->serial = 0;
302
303 keyboard->context = context;
304 wl_input_method_context_add_listener(context,
305 &input_method_context_listener,
306 keyboard);
307 keyboard->keyboard = wl_input_method_context_grab_keyboard(context);
308 wl_keyboard_add_listener(keyboard->keyboard,
309 &input_method_keyboard_listener,
310 keyboard);
311 }
312
313 static void
input_method_deactivate(void * data,struct wl_input_method * input_method,struct wl_input_method_context * context)314 input_method_deactivate(void *data,
315 struct wl_input_method *input_method,
316 struct wl_input_method_context *context)
317 {
318 struct simple_im *keyboard = data;
319
320 if (!keyboard->context)
321 return;
322
323 wl_input_method_context_destroy(keyboard->context);
324 keyboard->context = NULL;
325 }
326
327 static const struct wl_input_method_listener input_method_listener = {
328 input_method_activate,
329 input_method_deactivate
330 };
331
332 static void
registry_handle_global(void * data,struct wl_registry * registry,uint32_t name,const char * interface,uint32_t version)333 registry_handle_global(void *data, struct wl_registry *registry,
334 uint32_t name, const char *interface, uint32_t version)
335 {
336 struct simple_im *keyboard = data;
337
338 if (!strcmp(interface, "wl_input_method")) {
339 keyboard->input_method =
340 wl_registry_bind(registry, name,
341 &wl_input_method_interface, 1);
342 wl_input_method_add_listener(keyboard->input_method,
343 &input_method_listener, keyboard);
344 }
345 }
346
347 static void
registry_handle_global_remove(void * data,struct wl_registry * registry,uint32_t name)348 registry_handle_global_remove(void *data, struct wl_registry *registry,
349 uint32_t name)
350 {
351 }
352
353 static const struct wl_registry_listener registry_listener = {
354 registry_handle_global,
355 registry_handle_global_remove
356 };
357
358 static int
compare_compose_keys(const void * c1,const void * c2)359 compare_compose_keys(const void *c1, const void *c2)
360 {
361 const struct compose_seq *cs1 = c1;
362 const struct compose_seq *cs2 = c2;
363 int i;
364
365 for (i = 0; cs1->keys[i] != 0 && cs2->keys[i] != 0; i++) {
366 if (cs1->keys[i] != cs2->keys[i])
367 return cs1->keys[i] - cs2->keys[i];
368 }
369
370 if (cs1->keys[i] == cs2->keys[i]
371 || cs1->keys[i] == 0)
372 return 0;
373
374 return cs1->keys[i] - cs2->keys[i];
375 }
376
377 static void
simple_im_key_handler(struct simple_im * keyboard,uint32_t serial,uint32_t time,uint32_t key,uint32_t sym,enum wl_keyboard_key_state state)378 simple_im_key_handler(struct simple_im *keyboard,
379 uint32_t serial, uint32_t time, uint32_t key, uint32_t sym,
380 enum wl_keyboard_key_state state)
381 {
382 struct wl_input_method_context *context = keyboard->context;
383 char text[64];
384
385 if (sym == XKB_KEY_Multi_key &&
386 state == WL_KEYBOARD_KEY_STATE_RELEASED &&
387 keyboard->compose_state == state_normal) {
388 keyboard->compose_state = state_compose;
389 memset(&keyboard->compose_seq, 0, sizeof(struct compose_seq));
390 return;
391 }
392
393 if (keyboard->compose_state == state_compose) {
394 uint32_t i = 0;
395 struct compose_seq *cs;
396
397 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
398 return;
399
400 for (i = 0; i < sizeof(ignore_keys_on_compose) / sizeof(ignore_keys_on_compose[0]); i++) {
401 if (sym == ignore_keys_on_compose[i]) {
402 wl_input_method_context_key(context, keyboard->serial, time, key, state);
403 return;
404 }
405 }
406
407 for (i = 0; keyboard->compose_seq.keys[i] != 0; i++);
408
409 keyboard->compose_seq.keys[i] = sym;
410
411 cs = bsearch (&keyboard->compose_seq, compose_seqs,
412 sizeof(compose_seqs) / sizeof(compose_seqs[0]),
413 sizeof(compose_seqs[0]), compare_compose_keys);
414
415 if (cs) {
416 if (cs->keys[i + 1] == 0) {
417 wl_input_method_context_preedit_cursor(keyboard->context,
418 0);
419 wl_input_method_context_preedit_string(keyboard->context,
420 keyboard->serial,
421 "", "");
422 wl_input_method_context_cursor_position(keyboard->context,
423 0, 0);
424 wl_input_method_context_commit_string(keyboard->context,
425 keyboard->serial,
426 cs->text);
427 keyboard->compose_state = state_normal;
428 } else {
429 uint32_t j = 0, idx = 0;
430
431 for (; j <= i; j++) {
432 idx += xkb_keysym_to_utf8(cs->keys[j], text + idx, sizeof(text) - idx);
433 }
434
435 wl_input_method_context_preedit_cursor(keyboard->context,
436 strlen(text));
437 wl_input_method_context_preedit_string(keyboard->context,
438 keyboard->serial,
439 text,
440 text);
441 }
442 } else {
443 uint32_t j = 0, idx = 0;
444
445 for (; j <= i; j++) {
446 idx += xkb_keysym_to_utf8(keyboard->compose_seq.keys[j], text + idx, sizeof(text) - idx);
447 }
448 wl_input_method_context_preedit_cursor(keyboard->context,
449 0);
450 wl_input_method_context_preedit_string(keyboard->context,
451 keyboard->serial,
452 "", "");
453 wl_input_method_context_cursor_position(keyboard->context,
454 0, 0);
455 wl_input_method_context_commit_string(keyboard->context,
456 keyboard->serial,
457 text);
458 keyboard->compose_state = state_normal;
459 }
460 return;
461 }
462
463 if (xkb_keysym_to_utf8(sym, text, sizeof(text)) <= 0) {
464 wl_input_method_context_key(context, serial, time, key, state);
465 return;
466 }
467
468 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
469 return;
470
471 wl_input_method_context_cursor_position(keyboard->context,
472 0, 0);
473 wl_input_method_context_commit_string(keyboard->context,
474 keyboard->serial,
475 text);
476 }
477
478 int
main(int argc,char * argv[])479 main(int argc, char *argv[])
480 {
481 struct simple_im simple_im;
482 int ret = 0;
483
484 memset(&simple_im, 0, sizeof(simple_im));
485
486 simple_im.display = wl_display_connect(NULL);
487 if (simple_im.display == NULL) {
488 fprintf(stderr, "failed to connect to server: %m\n");
489 return -1;
490 }
491
492 simple_im.registry = wl_display_get_registry(simple_im.display);
493 wl_registry_add_listener(simple_im.registry,
494 ®istry_listener, &simple_im);
495 wl_display_roundtrip(simple_im.display);
496 if (simple_im.input_method == NULL) {
497 fprintf(stderr, "No input_method global\n");
498 exit(1);
499 }
500
501 simple_im.xkb_context = xkb_context_new(0);
502 if (simple_im.xkb_context == NULL) {
503 fprintf(stderr, "Failed to create XKB context\n");
504 return -1;
505 }
506
507 simple_im.context = NULL;
508 simple_im.key_handler = simple_im_key_handler;
509
510 while (ret != -1)
511 ret = wl_display_dispatch(simple_im.display);
512
513 if (ret == -1) {
514 fprintf(stderr, "Dispatch error: %m\n");
515 exit(1);
516 }
517
518 return 0;
519 }
520