• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <sys/mman.h>
33 
34 #include <linux/input.h>
35 
36 #include "window.h"
37 #include "input-method-unstable-v1-client-protocol.h"
38 #include "shared/helpers.h"
39 
40 enum compose_state {
41 	state_normal,
42 	state_compose
43 };
44 
45 struct compose_seq {
46 	uint32_t keys[4];
47 
48 	const char *text;
49 };
50 
51 struct simple_im;
52 
53 typedef 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 
58 struct simple_im {
59 	struct zwp_input_method_v1 *input_method;
60 	struct zwp_input_method_context_v1 *context;
61 	struct wl_display *display;
62 	struct wl_registry *registry;
63 	struct wl_keyboard *keyboard;
64 	enum compose_state compose_state;
65 	struct compose_seq compose_seq;
66 
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;
78 
79 	uint32_t serial;
80 };
81 
82 static 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 },  "á" },
91 	{ { 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 },  "Æ" },
96 	{ { XKB_KEY_O, XKB_KEY_C, 0 },  "©" },
97 	{ { XKB_KEY_O, XKB_KEY_R, 0 },  "®" },
98 	{ { XKB_KEY_s, XKB_KEY_s, 0 },  "ß" },
99 	{ { XKB_KEY_a, XKB_KEY_e, 0 },  "æ" },
100 	{ { XKB_KEY_a, XKB_KEY_a, 0 },  "å" },
101 };
102 
103 static const uint32_t ignore_keys_on_compose[] = {
104 	XKB_KEY_Shift_L,
105 	XKB_KEY_Shift_R
106 };
107 
108 static void
handle_surrounding_text(void * data,struct zwp_input_method_context_v1 * context,const char * text,uint32_t cursor,uint32_t anchor)109 handle_surrounding_text(void *data,
110 			struct zwp_input_method_context_v1 *context,
111 			const char *text,
112 			uint32_t cursor,
113 			uint32_t anchor)
114 {
115 	fprintf(stderr, "Surrounding text updated: %s\n", text);
116 }
117 
118 static void
handle_reset(void * data,struct zwp_input_method_context_v1 * context)119 handle_reset(void *data,
120 	     struct zwp_input_method_context_v1 *context)
121 {
122 	struct simple_im *keyboard = data;
123 
124 	fprintf(stderr, "Reset pre-edit buffer\n");
125 
126 	keyboard->compose_state = state_normal;
127 }
128 
129 static void
handle_content_type(void * data,struct zwp_input_method_context_v1 * context,uint32_t hint,uint32_t purpose)130 handle_content_type(void *data,
131 		    struct zwp_input_method_context_v1 *context,
132 		    uint32_t hint,
133 		    uint32_t purpose)
134 {
135 }
136 
137 static void
handle_invoke_action(void * data,struct zwp_input_method_context_v1 * context,uint32_t button,uint32_t index)138 handle_invoke_action(void *data,
139 		     struct zwp_input_method_context_v1 *context,
140 		     uint32_t button,
141 		     uint32_t index)
142 {
143 }
144 
145 static void
handle_commit_state(void * data,struct zwp_input_method_context_v1 * context,uint32_t serial)146 handle_commit_state(void *data,
147 		    struct zwp_input_method_context_v1 *context,
148 		    uint32_t serial)
149 {
150 	struct simple_im *keyboard = data;
151 
152 	keyboard->serial = serial;
153 }
154 
155 static void
handle_preferred_language(void * data,struct zwp_input_method_context_v1 * context,const char * language)156 handle_preferred_language(void *data,
157 			  struct zwp_input_method_context_v1 *context,
158 			  const char *language)
159 {
160 }
161 
162 static const struct zwp_input_method_context_v1_listener input_method_context_listener = {
163 	handle_surrounding_text,
164 	handle_reset,
165 	handle_content_type,
166 	handle_invoke_action,
167 	handle_commit_state,
168 	handle_preferred_language
169 };
170 
171 static void
input_method_keyboard_keymap(void * data,struct wl_keyboard * wl_keyboard,uint32_t format,int32_t fd,uint32_t size)172 input_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;
179 	char *map_str;
180 
181 	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 
192 	keyboard->keymap =
193 		xkb_keymap_new_from_string(keyboard->xkb_context,
194 					   map_str,
195 					   XKB_KEYMAP_FORMAT_TEXT_V1,
196 					   XKB_KEYMAP_COMPILE_NO_FLAGS);
197 
198 	munmap(map_str, size);
199 	close(fd);
200 
201 	if (!keyboard->keymap) {
202 		fprintf(stderr, "Failed to compile keymap\n");
203 		return;
204 	}
205 
206 	keyboard->state = xkb_state_new(keyboard->keymap);
207 	if (!keyboard->state) {
208 		fprintf(stderr, "Failed to create XKB state\n");
209 		xkb_keymap_unref(keyboard->keymap);
210 		return;
211 	}
212 
213 	keyboard->control_mask =
214 		1 << xkb_keymap_mod_get_index(keyboard->keymap, "Control");
215 	keyboard->alt_mask =
216 		1 << xkb_keymap_mod_get_index(keyboard->keymap, "Mod1");
217 	keyboard->shift_mask =
218 		1 << xkb_keymap_mod_get_index(keyboard->keymap, "Shift");
219 }
220 
221 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)222 input_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;
230 	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;
235 
236 	if (!keyboard->state)
237 		return;
238 
239 	code = key + 8;
240 	num_syms = xkb_state_key_get_syms(keyboard->state, code, &syms);
241 
242 	sym = XKB_KEY_NoSymbol;
243 	if (num_syms == 1)
244 		sym = syms[0];
245 
246 	if (keyboard->key_handler)
247 		(*keyboard->key_handler)(keyboard, serial, time, key, sym,
248 					 state);
249 }
250 
251 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)252 input_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;
261 	struct zwp_input_method_context_v1 *context = keyboard->context;
262 	xkb_mod_mask_t mask;
263 
264 	xkb_state_update_mask(keyboard->state, mods_depressed,
265 			      mods_latched, mods_locked, 0, 0, group);
266 	mask = xkb_state_serialize_mods(keyboard->state,
267 					XKB_STATE_MODS_DEPRESSED |
268 					XKB_STATE_MODS_LATCHED);
269 
270 	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;
277 
278 	zwp_input_method_context_v1_modifiers(context, serial,
279 					      mods_depressed, mods_depressed,
280 					      mods_latched, group);
281 }
282 
283 static 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 
291 static void
input_method_activate(void * data,struct zwp_input_method_v1 * input_method,struct zwp_input_method_context_v1 * context)292 input_method_activate(void *data,
293 		      struct zwp_input_method_v1 *input_method,
294 		      struct zwp_input_method_context_v1 *context)
295 {
296 	struct simple_im *keyboard = data;
297 
298 	if (keyboard->context)
299 		zwp_input_method_context_v1_destroy(keyboard->context);
300 
301 	keyboard->compose_state = state_normal;
302 
303 	keyboard->serial = 0;
304 
305 	keyboard->context = context;
306 	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);
310 	wl_keyboard_add_listener(keyboard->keyboard,
311 				 &input_method_keyboard_listener,
312 				 keyboard);
313 }
314 
315 static void
input_method_deactivate(void * data,struct zwp_input_method_v1 * input_method,struct zwp_input_method_context_v1 * context)316 input_method_deactivate(void *data,
317 			struct zwp_input_method_v1 *input_method,
318 			struct zwp_input_method_context_v1 *context)
319 {
320 	struct simple_im *keyboard = data;
321 
322 	if (!keyboard->context)
323 		return;
324 
325 	zwp_input_method_context_v1_destroy(keyboard->context);
326 	keyboard->context = NULL;
327 }
328 
329 static const struct zwp_input_method_v1_listener input_method_listener = {
330 	input_method_activate,
331 	input_method_deactivate
332 };
333 
334 static void
registry_handle_global(void * data,struct wl_registry * registry,uint32_t name,const char * interface,uint32_t version)335 registry_handle_global(void *data, struct wl_registry *registry,
336 		       uint32_t name, const char *interface, uint32_t version)
337 {
338 	struct simple_im *keyboard = data;
339 
340 	if (!strcmp(interface, "zwp_input_method_v1")) {
341 		keyboard->input_method =
342 			wl_registry_bind(registry, name,
343 					 &zwp_input_method_v1_interface, 1);
344 		zwp_input_method_v1_add_listener(keyboard->input_method,
345 						 &input_method_listener, keyboard);
346 	}
347 }
348 
349 static void
registry_handle_global_remove(void * data,struct wl_registry * registry,uint32_t name)350 registry_handle_global_remove(void *data, struct wl_registry *registry,
351 			      uint32_t name)
352 {
353 }
354 
355 static const struct wl_registry_listener registry_listener = {
356 	registry_handle_global,
357 	registry_handle_global_remove
358 };
359 
360 static int
compare_compose_keys(const void * c1,const void * c2)361 compare_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 
379 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)380 simple_im_key_handler(struct simple_im *keyboard,
381 		      uint32_t serial, uint32_t time, uint32_t key, uint32_t sym,
382 		      enum wl_keyboard_key_state state)
383 {
384 	struct zwp_input_method_context_v1 *context = keyboard->context;
385 	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 
402 		for (i = 0; i < ARRAY_LENGTH(ignore_keys_on_compose); i++) {
403 			if (sym == ignore_keys_on_compose[i]) {
404 				zwp_input_method_context_v1_key(context,
405 								keyboard->serial,
406 								time,
407 								key,
408 								state);
409 				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,
418 			      ARRAY_LENGTH(compose_seqs),
419 			      sizeof(compose_seqs[0]), compare_compose_keys);
420 
421 		if (cs) {
422 			if (cs->keys[i + 1] == 0) {
423 				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);
433 				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 
441 				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);
447 			}
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 			}
454 			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);
464 			keyboard->compose_state = state_normal;
465 		}
466 		return;
467 	}
468 
469 	if (xkb_keysym_to_utf8(sym, text, sizeof(text)) <= 0) {
470 		zwp_input_method_context_v1_key(context, serial, time, key, state);
471 		return;
472 	}
473 
474 	if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
475 		return;
476 
477 	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);
482 }
483 
484 int
main(int argc,char * argv[])485 main(int argc, char *argv[])
486 {
487 	struct simple_im simple_im;
488 	int ret = 0;
489 
490 	memset(&simple_im, 0, sizeof(simple_im));
491 
492 	simple_im.display = wl_display_connect(NULL);
493 	if (simple_im.display == NULL) {
494 		fprintf(stderr, "Failed to connect to server: %s\n",
495 			strerror(errno));
496 		return -1;
497 	}
498 
499 	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");
505 		return -1;
506 	}
507 
508 	simple_im.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
509 	if (simple_im.xkb_context == NULL) {
510 		fprintf(stderr, "Failed to create XKB context\n");
511 		return -1;
512 	}
513 
514 	simple_im.context = NULL;
515 	simple_im.key_handler =  simple_im_key_handler;
516 
517 	while (ret != -1)
518 		ret = wl_display_dispatch(simple_im.display);
519 
520 	if (ret == -1) {
521 		fprintf(stderr, "Dispatch error: %s\n", strerror(errno));
522 		return -1;
523 	}
524 
525 	return 0;
526 }
527