1 /*
2 * Copyright © 2012 Openismus GmbH
3 * Copyright © 2012 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "config.h"
26
27 #include <stdbool.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33
34 #include <linux/input.h>
35 #include <cairo.h>
36
37 #include "window.h"
38 #include "input-method-unstable-v1-client-protocol.h"
39 #include "text-input-unstable-v1-client-protocol.h"
40 #include "shared/xalloc.h"
41
42 struct keyboard;
43
44 struct virtual_keyboard {
45 struct zwp_input_panel_v1 *input_panel;
46 struct zwp_input_method_v1 *input_method;
47 struct zwp_input_method_context_v1 *context;
48 struct display *display;
49 struct output *output;
50 char *preedit_string;
51 uint32_t preedit_style;
52 struct {
53 xkb_mod_mask_t shift_mask;
54 } keysym;
55 uint32_t serial;
56 uint32_t content_hint;
57 uint32_t content_purpose;
58 char *preferred_language;
59 char *surrounding_text;
60 uint32_t surrounding_cursor;
61 struct keyboard *keyboard;
62 bool toplevel;
63 };
64
65 enum key_type {
66 keytype_default,
67 keytype_backspace,
68 keytype_enter,
69 keytype_space,
70 keytype_switch,
71 keytype_symbols,
72 keytype_tab,
73 keytype_arrow_up,
74 keytype_arrow_left,
75 keytype_arrow_right,
76 keytype_arrow_down,
77 keytype_style
78 };
79
80 struct key {
81 enum key_type key_type;
82
83 char *label;
84 char *uppercase;
85 char *symbol;
86
87 unsigned int width;
88 };
89
90 struct layout {
91 const struct key *keys;
92 uint32_t count;
93
94 uint32_t columns;
95 uint32_t rows;
96
97 const char *language;
98 uint32_t text_direction;
99 };
100
101 static const struct key normal_keys[] = {
102 { keytype_default, "q", "Q", "1", 1},
103 { keytype_default, "w", "W", "2", 1},
104 { keytype_default, "e", "E", "3", 1},
105 { keytype_default, "r", "R", "4", 1},
106 { keytype_default, "t", "T", "5", 1},
107 { keytype_default, "y", "Y", "6", 1},
108 { keytype_default, "u", "U", "7", 1},
109 { keytype_default, "i", "I", "8", 1},
110 { keytype_default, "o", "O", "9", 1},
111 { keytype_default, "p", "P", "0", 1},
112 { keytype_backspace, "<--", "<--", "<--", 2},
113
114 { keytype_tab, "->|", "->|", "->|", 1},
115 { keytype_default, "a", "A", "-", 1},
116 { keytype_default, "s", "S", "@", 1},
117 { keytype_default, "d", "D", "*", 1},
118 { keytype_default, "f", "F", "^", 1},
119 { keytype_default, "g", "G", ":", 1},
120 { keytype_default, "h", "H", ";", 1},
121 { keytype_default, "j", "J", "(", 1},
122 { keytype_default, "k", "K", ")", 1},
123 { keytype_default, "l", "L", "~", 1},
124 { keytype_enter, "Enter", "Enter", "Enter", 2},
125
126 { keytype_switch, "ABC", "abc", "ABC", 2},
127 { keytype_default, "z", "Z", "/", 1},
128 { keytype_default, "x", "X", "\'", 1},
129 { keytype_default, "c", "C", "\"", 1},
130 { keytype_default, "v", "V", "+", 1},
131 { keytype_default, "b", "B", "=", 1},
132 { keytype_default, "n", "N", "?", 1},
133 { keytype_default, "m", "M", "!", 1},
134 { keytype_default, ",", ",", "\\", 1},
135 { keytype_default, ".", ".", "|", 1},
136 { keytype_switch, "ABC", "abc", "ABC", 1},
137
138 { keytype_symbols, "?123", "?123", "abc", 1},
139 { keytype_space, "", "", "", 5},
140 { keytype_arrow_up, "/\\", "/\\", "/\\", 1},
141 { keytype_arrow_left, "<", "<", "<", 1},
142 { keytype_arrow_right, ">", ">", ">", 1},
143 { keytype_arrow_down, "\\/", "\\/", "\\/", 1},
144 { keytype_style, "", "", "", 2}
145 };
146
147 static const struct key numeric_keys[] = {
148 { keytype_default, "1", "1", "1", 1},
149 { keytype_default, "2", "2", "2", 1},
150 { keytype_default, "3", "3", "3", 1},
151 { keytype_default, "4", "4", "4", 1},
152 { keytype_default, "5", "5", "5", 1},
153 { keytype_default, "6", "6", "6", 1},
154 { keytype_default, "7", "7", "7", 1},
155 { keytype_default, "8", "8", "8", 1},
156 { keytype_default, "9", "9", "9", 1},
157 { keytype_default, "0", "0", "0", 1},
158 { keytype_backspace, "<--", "<--", "<--", 2},
159
160 { keytype_space, "", "", "", 4},
161 { keytype_enter, "Enter", "Enter", "Enter", 2},
162 { keytype_arrow_up, "/\\", "/\\", "/\\", 1},
163 { keytype_arrow_left, "<", "<", "<", 1},
164 { keytype_arrow_right, ">", ">", ">", 1},
165 { keytype_arrow_down, "\\/", "\\/", "\\/", 1},
166 { keytype_style, "", "", "", 2}
167 };
168
169 static const struct key arabic_keys[] = {
170 { keytype_default, "ض", "ﹶ", "۱", 1},
171 { keytype_default, "ص", "ﹰ", "۲", 1},
172 { keytype_default, "ث", "ﹸ", "۳", 1},
173 { keytype_default, "ق", "ﹲ", "۴", 1},
174 { keytype_default, "ف", "ﻹ", "۵", 1},
175 { keytype_default, "غ", "ﺇ", "۶", 1},
176 { keytype_default, "ع", "`", "۷", 1},
177 { keytype_default, "ه", "٪", "۸", 1},
178 { keytype_default, "خ", ">", "۹", 1},
179 { keytype_default, "ح", "<", "۰", 1},
180 { keytype_backspace, "-->", "-->", "-->", 2},
181
182 { keytype_tab, "->|", "->|", "->|", 1},
183 { keytype_default, "ش", "ﹺ", "ﹼ", 1},
184 { keytype_default, "س", "ﹴ", "!", 1},
185 { keytype_default, "ي", "[", "@", 1},
186 { keytype_default, "ب", "]", "#", 1},
187 { keytype_default, "ل", "ﻷ", "$", 1},
188 { keytype_default, "ا", "أ", "%", 1},
189 { keytype_default, "ت", "-", "^", 1},
190 { keytype_default, "ن", "x", "&", 1},
191 { keytype_default, "م", "/", "*", 1},
192 { keytype_default, "ك", ":", "_", 1},
193 { keytype_default, "د", "\"", "+", 1},
194 { keytype_enter, "Enter", "Enter", "Enter", 2},
195
196 { keytype_switch, "Shift", "Base", "Shift", 2},
197 { keytype_default, "ئ", "~", ")", 1},
198 { keytype_default, "ء", "°", "(", 1},
199 { keytype_default, "ؤ", "{", "\"", 1},
200 { keytype_default, "ر", "}", "\'", 1},
201 { keytype_default, "ى", "ﺁ", "؟", 1},
202 { keytype_default, "ة", "'", "!", 1},
203 { keytype_default, "و", ",", ";", 1},
204 { keytype_default, "ﺯ", ".", "\\", 1},
205 { keytype_default, "ظ", "؟", "=", 1},
206 { keytype_switch, "Shift", "Base", "Shift", 2},
207
208 { keytype_symbols, "؟٣٢١", "؟٣٢١", "Base", 1},
209 { keytype_default, "ﻻ", "ﻵ", "|", 1},
210 { keytype_default, ",", "،", "،", 1},
211 { keytype_space, "", "", "", 6},
212 { keytype_default, ".", "ذ", "]", 1},
213 { keytype_default, "ط", "ﺝ", "[", 1},
214 { keytype_style, "", "", "", 2}
215 };
216
217
218 static const struct layout normal_layout = {
219 normal_keys,
220 sizeof(normal_keys) / sizeof(*normal_keys),
221 12,
222 4,
223 "en",
224 ZWP_TEXT_INPUT_V1_TEXT_DIRECTION_LTR
225 };
226
227 static const struct layout numeric_layout = {
228 numeric_keys,
229 sizeof(numeric_keys) / sizeof(*numeric_keys),
230 12,
231 2,
232 "en",
233 ZWP_TEXT_INPUT_V1_TEXT_DIRECTION_LTR
234 };
235
236 static const struct layout arabic_layout = {
237 arabic_keys,
238 sizeof(arabic_keys) / sizeof(*arabic_keys),
239 13,
240 4,
241 "ar",
242 ZWP_TEXT_INPUT_V1_TEXT_DIRECTION_RTL
243 };
244
245 static const char *style_labels[] = {
246 "default",
247 "none",
248 "active",
249 "inactive",
250 "highlight",
251 "underline",
252 "selection",
253 "incorrect"
254 };
255
256 static const double key_width = 60;
257 static const double key_height = 50;
258
259 enum keyboard_state {
260 KEYBOARD_STATE_DEFAULT,
261 KEYBOARD_STATE_UPPERCASE,
262 KEYBOARD_STATE_SYMBOLS
263 };
264
265 struct keyboard {
266 struct virtual_keyboard *keyboard;
267 struct window *window;
268 struct widget *widget;
269
270 enum keyboard_state state;
271 };
272
273 static void __attribute__ ((format (printf, 1, 2)))
dbg(const char * fmt,...)274 dbg(const char *fmt, ...)
275 {
276 #ifdef DEBUG
277 va_list argp;
278
279 va_start(argp, fmt);
280 vfprintf(stderr, fmt, argp);
281 va_end(argp);
282 #endif
283 }
284
285 static const char *
label_from_key(struct keyboard * keyboard,const struct key * key)286 label_from_key(struct keyboard *keyboard,
287 const struct key *key)
288 {
289 if (key->key_type == keytype_style)
290 return style_labels[keyboard->keyboard->preedit_style];
291
292 switch(keyboard->state) {
293 case KEYBOARD_STATE_DEFAULT:
294 return key->label;
295 case KEYBOARD_STATE_UPPERCASE:
296 return key->uppercase;
297 case KEYBOARD_STATE_SYMBOLS:
298 return key->symbol;
299 }
300
301 return "";
302 }
303
304 static void
draw_key(struct keyboard * keyboard,const struct key * key,cairo_t * cr,unsigned int row,unsigned int col)305 draw_key(struct keyboard *keyboard,
306 const struct key *key,
307 cairo_t *cr,
308 unsigned int row,
309 unsigned int col)
310 {
311 const char *label;
312 cairo_text_extents_t extents;
313
314 cairo_save(cr);
315 cairo_rectangle(cr,
316 col * key_width, row * key_height,
317 key->width * key_width, key_height);
318 cairo_clip(cr);
319
320 /* Paint frame */
321 cairo_rectangle(cr,
322 col * key_width, row * key_height,
323 key->width * key_width, key_height);
324 cairo_set_line_width(cr, 3);
325 cairo_stroke(cr);
326
327 /* Paint text */
328 label = label_from_key(keyboard, key);
329 cairo_text_extents(cr, label, &extents);
330
331 cairo_translate(cr,
332 col * key_width,
333 row * key_height);
334 cairo_translate(cr,
335 (key->width * key_width - extents.width) / 2,
336 (key_height - extents.y_bearing) / 2);
337 cairo_show_text(cr, label);
338
339 cairo_restore(cr);
340 }
341
342 static const struct layout *
get_current_layout(struct virtual_keyboard * keyboard)343 get_current_layout(struct virtual_keyboard *keyboard)
344 {
345 switch (keyboard->content_purpose) {
346 case ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_DIGITS:
347 case ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_NUMBER:
348 return &numeric_layout;
349 default:
350 if (keyboard->preferred_language &&
351 strcmp(keyboard->preferred_language, "ar") == 0)
352 return &arabic_layout;
353 else
354 return &normal_layout;
355 }
356 }
357
358 static void
redraw_handler(struct widget * widget,void * data)359 redraw_handler(struct widget *widget, void *data)
360 {
361 struct keyboard *keyboard = data;
362 cairo_surface_t *surface;
363 struct rectangle allocation;
364 cairo_t *cr;
365 unsigned int i;
366 unsigned int row = 0, col = 0;
367 const struct layout *layout;
368
369 layout = get_current_layout(keyboard->keyboard);
370
371 surface = window_get_surface(keyboard->window);
372 widget_get_allocation(keyboard->widget, &allocation);
373
374 cr = cairo_create(surface);
375 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
376 cairo_clip(cr);
377
378 cairo_select_font_face(cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
379 cairo_set_font_size(cr, 16);
380
381 cairo_translate(cr, allocation.x, allocation.y);
382
383 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
384 cairo_set_source_rgba(cr, 1, 1, 1, 0.75);
385 cairo_rectangle(cr, 0, 0, layout->columns * key_width, layout->rows * key_height);
386 cairo_paint(cr);
387
388 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
389
390 for (i = 0; i < layout->count; ++i) {
391 cairo_set_source_rgb(cr, 0, 0, 0);
392 draw_key(keyboard, &layout->keys[i], cr, row, col);
393 col += layout->keys[i].width;
394 if (col >= layout->columns) {
395 row += 1;
396 col = 0;
397 }
398 }
399
400 cairo_destroy(cr);
401 cairo_surface_destroy(surface);
402 }
403
404 static void
resize_handler(struct widget * widget,int32_t width,int32_t height,void * data)405 resize_handler(struct widget *widget,
406 int32_t width, int32_t height, void *data)
407 {
408 /* struct keyboard *keyboard = data; */
409 }
410
411 static char *
insert_text(const char * text,uint32_t offset,const char * insert)412 insert_text(const char *text, uint32_t offset, const char *insert)
413 {
414 int tlen = strlen(text), ilen = strlen(insert);
415 char *new_text = xmalloc(tlen + ilen + 1);
416
417 memcpy(new_text, text, offset);
418 memcpy(new_text + offset, insert, ilen);
419 memcpy(new_text + offset + ilen, text + offset, tlen - offset);
420 new_text[tlen + ilen] = '\0';
421
422 return new_text;
423 }
424
425 static void
virtual_keyboard_commit_preedit(struct virtual_keyboard * keyboard)426 virtual_keyboard_commit_preedit(struct virtual_keyboard *keyboard)
427 {
428 char *surrounding_text;
429
430 if (!keyboard->preedit_string ||
431 strlen(keyboard->preedit_string) == 0)
432 return;
433
434 zwp_input_method_context_v1_cursor_position(keyboard->context,
435 0, 0);
436 zwp_input_method_context_v1_commit_string(keyboard->context,
437 keyboard->serial,
438 keyboard->preedit_string);
439
440 if (keyboard->surrounding_text) {
441 surrounding_text = insert_text(keyboard->surrounding_text,
442 keyboard->surrounding_cursor,
443 keyboard->preedit_string);
444 free(keyboard->surrounding_text);
445 keyboard->surrounding_text = surrounding_text;
446 keyboard->surrounding_cursor += strlen(keyboard->preedit_string);
447 } else {
448 keyboard->surrounding_text = strdup(keyboard->preedit_string);
449 keyboard->surrounding_cursor = strlen(keyboard->preedit_string);
450 }
451
452 free(keyboard->preedit_string);
453 keyboard->preedit_string = strdup("");
454 }
455
456 static void
virtual_keyboard_send_preedit(struct virtual_keyboard * keyboard,int32_t cursor)457 virtual_keyboard_send_preedit(struct virtual_keyboard *keyboard,
458 int32_t cursor)
459 {
460 uint32_t index = strlen(keyboard->preedit_string);
461
462 if (keyboard->preedit_style)
463 zwp_input_method_context_v1_preedit_styling(keyboard->context,
464 0,
465 strlen(keyboard->preedit_string),
466 keyboard->preedit_style);
467 if (cursor > 0)
468 index = cursor;
469 zwp_input_method_context_v1_preedit_cursor(keyboard->context,
470 index);
471 zwp_input_method_context_v1_preedit_string(keyboard->context,
472 keyboard->serial,
473 keyboard->preedit_string,
474 keyboard->preedit_string);
475 }
476
477 static const char *
prev_utf8_char(const char * s,const char * p)478 prev_utf8_char(const char *s, const char *p)
479 {
480 for (--p; p >= s; --p) {
481 if ((*p & 0xc0) != 0x80)
482 return p;
483 }
484 return NULL;
485 }
486
487 static void
delete_before_cursor(struct virtual_keyboard * keyboard)488 delete_before_cursor(struct virtual_keyboard *keyboard)
489 {
490 const char *start, *end;
491
492 if (!keyboard->surrounding_text) {
493 dbg("delete_before_cursor: No surrounding text available\n");
494 return;
495 }
496
497 start = prev_utf8_char(keyboard->surrounding_text,
498 keyboard->surrounding_text + keyboard->surrounding_cursor);
499 if (!start) {
500 dbg("delete_before_cursor: No previous character to delete\n");
501 return;
502 }
503
504 end = keyboard->surrounding_text + keyboard->surrounding_cursor;
505
506 zwp_input_method_context_v1_delete_surrounding_text(keyboard->context,
507 (start - keyboard->surrounding_text) - keyboard->surrounding_cursor,
508 end - start);
509 zwp_input_method_context_v1_commit_string(keyboard->context,
510 keyboard->serial,
511 "");
512
513 /* Update surrounding text */
514 keyboard->surrounding_cursor = start - keyboard->surrounding_text;
515 keyboard->surrounding_text[keyboard->surrounding_cursor] = '\0';
516 if (*end)
517 memmove(keyboard->surrounding_text + keyboard->surrounding_cursor, end, strlen(end));
518 }
519
520 static char *
append(char * s1,const char * s2)521 append(char *s1, const char *s2)
522 {
523 int len1, len2;
524 char *s;
525
526 len1 = strlen(s1);
527 len2 = strlen(s2);
528 s = xrealloc(s1, len1 + len2 + 1);
529 memcpy(s + len1, s2, len2);
530 s[len1 + len2] = '\0';
531
532 return s;
533 }
534
535 static void
keyboard_handle_key(struct keyboard * keyboard,uint32_t time,const struct key * key,struct input * input,enum wl_pointer_button_state state)536 keyboard_handle_key(struct keyboard *keyboard, uint32_t time, const struct key *key, struct input *input, enum wl_pointer_button_state state)
537 {
538 const char *label = NULL;
539
540 switch(keyboard->state) {
541 case KEYBOARD_STATE_DEFAULT :
542 label = key->label;
543 break;
544 case KEYBOARD_STATE_UPPERCASE :
545 label = key->uppercase;
546 break;
547 case KEYBOARD_STATE_SYMBOLS :
548 label = key->symbol;
549 break;
550 }
551
552 xkb_mod_mask_t mod_mask = keyboard->state == KEYBOARD_STATE_DEFAULT ? 0 : keyboard->keyboard->keysym.shift_mask;
553 uint32_t key_state = (state == WL_POINTER_BUTTON_STATE_PRESSED) ? WL_KEYBOARD_KEY_STATE_PRESSED : WL_KEYBOARD_KEY_STATE_RELEASED;
554
555 switch (key->key_type) {
556 case keytype_default:
557 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
558 break;
559
560 keyboard->keyboard->preedit_string =
561 append(keyboard->keyboard->preedit_string,
562 label);
563 virtual_keyboard_send_preedit(keyboard->keyboard, -1);
564 break;
565 case keytype_backspace:
566 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
567 break;
568
569 if (strlen(keyboard->keyboard->preedit_string) == 0) {
570 delete_before_cursor(keyboard->keyboard);
571 } else {
572 keyboard->keyboard->preedit_string[strlen(keyboard->keyboard->preedit_string) - 1] = '\0';
573 virtual_keyboard_send_preedit(keyboard->keyboard, -1);
574 }
575 break;
576 case keytype_enter:
577 virtual_keyboard_commit_preedit(keyboard->keyboard);
578 zwp_input_method_context_v1_keysym(keyboard->keyboard->context,
579 display_get_serial(keyboard->keyboard->display),
580 time,
581 XKB_KEY_Return, key_state, mod_mask);
582 break;
583 case keytype_space:
584 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
585 break;
586 keyboard->keyboard->preedit_string =
587 append(keyboard->keyboard->preedit_string, " ");
588 virtual_keyboard_commit_preedit(keyboard->keyboard);
589 break;
590 case keytype_switch:
591 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
592 break;
593 switch(keyboard->state) {
594 case KEYBOARD_STATE_DEFAULT:
595 keyboard->state = KEYBOARD_STATE_UPPERCASE;
596 break;
597 case KEYBOARD_STATE_UPPERCASE:
598 keyboard->state = KEYBOARD_STATE_DEFAULT;
599 break;
600 case KEYBOARD_STATE_SYMBOLS:
601 keyboard->state = KEYBOARD_STATE_UPPERCASE;
602 break;
603 }
604 break;
605 case keytype_symbols:
606 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
607 break;
608 switch(keyboard->state) {
609 case KEYBOARD_STATE_DEFAULT:
610 keyboard->state = KEYBOARD_STATE_SYMBOLS;
611 break;
612 case KEYBOARD_STATE_UPPERCASE:
613 keyboard->state = KEYBOARD_STATE_SYMBOLS;
614 break;
615 case KEYBOARD_STATE_SYMBOLS:
616 keyboard->state = KEYBOARD_STATE_DEFAULT;
617 break;
618 }
619 break;
620 case keytype_tab:
621 virtual_keyboard_commit_preedit(keyboard->keyboard);
622 zwp_input_method_context_v1_keysym(keyboard->keyboard->context,
623 display_get_serial(keyboard->keyboard->display),
624 time,
625 XKB_KEY_Tab, key_state, mod_mask);
626 break;
627 case keytype_arrow_up:
628 virtual_keyboard_commit_preedit(keyboard->keyboard);
629 zwp_input_method_context_v1_keysym(keyboard->keyboard->context,
630 display_get_serial(keyboard->keyboard->display),
631 time,
632 XKB_KEY_Up, key_state, mod_mask);
633 break;
634 case keytype_arrow_left:
635 virtual_keyboard_commit_preedit(keyboard->keyboard);
636 zwp_input_method_context_v1_keysym(keyboard->keyboard->context,
637 display_get_serial(keyboard->keyboard->display),
638 time,
639 XKB_KEY_Left, key_state, mod_mask);
640 break;
641 case keytype_arrow_right:
642 virtual_keyboard_commit_preedit(keyboard->keyboard);
643 zwp_input_method_context_v1_keysym(keyboard->keyboard->context,
644 display_get_serial(keyboard->keyboard->display),
645 time,
646 XKB_KEY_Right, key_state, mod_mask);
647 break;
648 case keytype_arrow_down:
649 virtual_keyboard_commit_preedit(keyboard->keyboard);
650 zwp_input_method_context_v1_keysym(keyboard->keyboard->context,
651 display_get_serial(keyboard->keyboard->display),
652 time,
653 XKB_KEY_Down, key_state, mod_mask);
654 break;
655 case keytype_style:
656 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
657 break;
658 keyboard->keyboard->preedit_style = (keyboard->keyboard->preedit_style + 1) % 8; /* TODO */
659 virtual_keyboard_send_preedit(keyboard->keyboard, -1);
660 break;
661 }
662 }
663
664 static void
button_handler(struct widget * widget,struct input * input,uint32_t time,uint32_t button,enum wl_pointer_button_state state,void * data)665 button_handler(struct widget *widget,
666 struct input *input, uint32_t time,
667 uint32_t button,
668 enum wl_pointer_button_state state, void *data)
669 {
670 struct keyboard *keyboard = data;
671 struct rectangle allocation;
672 int32_t x, y;
673 int row, col;
674 unsigned int i;
675 const struct layout *layout;
676
677 layout = get_current_layout(keyboard->keyboard);
678
679 if (button != BTN_LEFT) {
680 return;
681 }
682
683 input_get_position(input, &x, &y);
684
685 widget_get_allocation(keyboard->widget, &allocation);
686 x -= allocation.x;
687 y -= allocation.y;
688
689 row = y / key_height;
690 col = x / key_width + row * layout->columns;
691 for (i = 0; i < layout->count; ++i) {
692 col -= layout->keys[i].width;
693 if (col < 0) {
694 keyboard_handle_key(keyboard, time, &layout->keys[i], input, state);
695 break;
696 }
697 }
698
699 widget_schedule_redraw(widget);
700 }
701
702 static void
touch_handler(struct input * input,uint32_t time,float x,float y,uint32_t state,void * data)703 touch_handler(struct input *input, uint32_t time,
704 float x, float y, uint32_t state, void *data)
705 {
706 struct keyboard *keyboard = data;
707 struct rectangle allocation;
708 int row, col;
709 unsigned int i;
710 const struct layout *layout;
711
712 layout = get_current_layout(keyboard->keyboard);
713
714 widget_get_allocation(keyboard->widget, &allocation);
715
716 x -= allocation.x;
717 y -= allocation.y;
718
719 row = (int)y / key_height;
720 col = (int)x / key_width + row * layout->columns;
721 for (i = 0; i < layout->count; ++i) {
722 col -= layout->keys[i].width;
723 if (col < 0) {
724 keyboard_handle_key(keyboard, time,
725 &layout->keys[i], input, state);
726 break;
727 }
728 }
729
730 widget_schedule_redraw(keyboard->widget);
731 }
732
733 static void
touch_down_handler(struct widget * widget,struct input * input,uint32_t serial,uint32_t time,int32_t id,float x,float y,void * data)734 touch_down_handler(struct widget *widget, struct input *input,
735 uint32_t serial, uint32_t time, int32_t id,
736 float x, float y, void *data)
737 {
738 touch_handler(input, time, x, y,
739 WL_POINTER_BUTTON_STATE_PRESSED, data);
740 }
741
742 static void
touch_up_handler(struct widget * widget,struct input * input,uint32_t serial,uint32_t time,int32_t id,void * data)743 touch_up_handler(struct widget *widget, struct input *input,
744 uint32_t serial, uint32_t time, int32_t id,
745 void *data)
746 {
747 float x, y;
748
749 input_get_touch(input, id, &x, &y);
750
751 touch_handler(input, time, x, y,
752 WL_POINTER_BUTTON_STATE_RELEASED, data);
753 }
754
755 static void
handle_surrounding_text(void * data,struct zwp_input_method_context_v1 * context,const char * text,uint32_t cursor,uint32_t anchor)756 handle_surrounding_text(void *data,
757 struct zwp_input_method_context_v1 *context,
758 const char *text,
759 uint32_t cursor,
760 uint32_t anchor)
761 {
762 struct virtual_keyboard *keyboard = data;
763
764 free(keyboard->surrounding_text);
765 keyboard->surrounding_text = strdup(text);
766
767 keyboard->surrounding_cursor = cursor;
768 }
769
770 static void
handle_reset(void * data,struct zwp_input_method_context_v1 * context)771 handle_reset(void *data,
772 struct zwp_input_method_context_v1 *context)
773 {
774 struct virtual_keyboard *keyboard = data;
775
776 dbg("Reset pre-edit buffer\n");
777
778 if (strlen(keyboard->preedit_string)) {
779 free(keyboard->preedit_string);
780 keyboard->preedit_string = strdup("");
781 }
782 }
783
784 static void
handle_content_type(void * data,struct zwp_input_method_context_v1 * context,uint32_t hint,uint32_t purpose)785 handle_content_type(void *data,
786 struct zwp_input_method_context_v1 *context,
787 uint32_t hint,
788 uint32_t purpose)
789 {
790 struct virtual_keyboard *keyboard = data;
791
792 keyboard->content_hint = hint;
793 keyboard->content_purpose = purpose;
794 }
795
796 static void
handle_invoke_action(void * data,struct zwp_input_method_context_v1 * context,uint32_t button,uint32_t index)797 handle_invoke_action(void *data,
798 struct zwp_input_method_context_v1 *context,
799 uint32_t button,
800 uint32_t index)
801 {
802 struct virtual_keyboard *keyboard = data;
803
804 if (button != BTN_LEFT)
805 return;
806
807 virtual_keyboard_send_preedit(keyboard, index);
808 }
809
810 static void
handle_commit_state(void * data,struct zwp_input_method_context_v1 * context,uint32_t serial)811 handle_commit_state(void *data,
812 struct zwp_input_method_context_v1 *context,
813 uint32_t serial)
814 {
815 struct virtual_keyboard *keyboard = data;
816 const struct layout *layout;
817
818 keyboard->serial = serial;
819
820 layout = get_current_layout(keyboard);
821
822 if (keyboard->surrounding_text)
823 dbg("Surrounding text updated: %s\n", keyboard->surrounding_text);
824
825 window_schedule_resize(keyboard->keyboard->window,
826 layout->columns * key_width,
827 layout->rows * key_height);
828
829 zwp_input_method_context_v1_language(context,
830 keyboard->serial,
831 layout->language);
832 zwp_input_method_context_v1_text_direction(context,
833 keyboard->serial,
834 layout->text_direction);
835
836 widget_schedule_redraw(keyboard->keyboard->widget);
837 }
838
839 static void
handle_preferred_language(void * data,struct zwp_input_method_context_v1 * context,const char * language)840 handle_preferred_language(void *data,
841 struct zwp_input_method_context_v1 *context,
842 const char *language)
843 {
844 struct virtual_keyboard *keyboard = data;
845
846 if (keyboard->preferred_language)
847 free(keyboard->preferred_language);
848
849 keyboard->preferred_language = NULL;
850
851 if (language)
852 keyboard->preferred_language = strdup(language);
853 }
854
855 static const struct zwp_input_method_context_v1_listener input_method_context_listener = {
856 handle_surrounding_text,
857 handle_reset,
858 handle_content_type,
859 handle_invoke_action,
860 handle_commit_state,
861 handle_preferred_language
862 };
863
864 static void
input_method_activate(void * data,struct zwp_input_method_v1 * input_method,struct zwp_input_method_context_v1 * context)865 input_method_activate(void *data,
866 struct zwp_input_method_v1 *input_method,
867 struct zwp_input_method_context_v1 *context)
868 {
869 struct virtual_keyboard *keyboard = data;
870 struct wl_array modifiers_map;
871 const struct layout *layout;
872
873 keyboard->keyboard->state = KEYBOARD_STATE_DEFAULT;
874
875 if (keyboard->context)
876 zwp_input_method_context_v1_destroy(keyboard->context);
877
878 if (keyboard->preedit_string)
879 free(keyboard->preedit_string);
880
881 keyboard->preedit_string = strdup("");
882 keyboard->content_hint = 0;
883 keyboard->content_purpose = 0;
884 free(keyboard->preferred_language);
885 keyboard->preferred_language = NULL;
886 free(keyboard->surrounding_text);
887 keyboard->surrounding_text = NULL;
888
889 keyboard->serial = 0;
890
891 keyboard->context = context;
892 zwp_input_method_context_v1_add_listener(context,
893 &input_method_context_listener,
894 keyboard);
895
896 wl_array_init(&modifiers_map);
897 keysym_modifiers_add(&modifiers_map, "Shift");
898 keysym_modifiers_add(&modifiers_map, "Control");
899 keysym_modifiers_add(&modifiers_map, "Mod1");
900 zwp_input_method_context_v1_modifiers_map(context, &modifiers_map);
901 keyboard->keysym.shift_mask = keysym_modifiers_get_mask(&modifiers_map, "Shift");
902 wl_array_release(&modifiers_map);
903
904 layout = get_current_layout(keyboard);
905
906 window_schedule_resize(keyboard->keyboard->window,
907 layout->columns * key_width,
908 layout->rows * key_height);
909
910 zwp_input_method_context_v1_language(context,
911 keyboard->serial,
912 layout->language);
913 zwp_input_method_context_v1_text_direction(context,
914 keyboard->serial,
915 layout->text_direction);
916
917 widget_schedule_redraw(keyboard->keyboard->widget);
918 }
919
920 static void
input_method_deactivate(void * data,struct zwp_input_method_v1 * input_method,struct zwp_input_method_context_v1 * context)921 input_method_deactivate(void *data,
922 struct zwp_input_method_v1 *input_method,
923 struct zwp_input_method_context_v1 *context)
924 {
925 struct virtual_keyboard *keyboard = data;
926
927 if (!keyboard->context)
928 return;
929
930 zwp_input_method_context_v1_destroy(keyboard->context);
931 keyboard->context = NULL;
932 }
933
934 static const struct zwp_input_method_v1_listener input_method_listener = {
935 input_method_activate,
936 input_method_deactivate
937 };
938
939 static void
global_handler(struct display * display,uint32_t name,const char * interface,uint32_t version,void * data)940 global_handler(struct display *display, uint32_t name,
941 const char *interface, uint32_t version, void *data)
942 {
943 struct virtual_keyboard *keyboard = data;
944
945 if (!strcmp(interface, "zwp_input_panel_v1")) {
946 keyboard->input_panel =
947 display_bind(display, name, &zwp_input_panel_v1_interface, 1);
948 } else if (!strcmp(interface, "zwp_input_method_v1")) {
949 keyboard->input_method =
950 display_bind(display, name,
951 &zwp_input_method_v1_interface, 1);
952 zwp_input_method_v1_add_listener(keyboard->input_method,
953 &input_method_listener,
954 keyboard);
955 }
956 }
957
958 static void
set_toplevel(struct output * output,struct virtual_keyboard * virtual_keyboard)959 set_toplevel(struct output *output, struct virtual_keyboard *virtual_keyboard)
960 {
961 struct zwp_input_panel_surface_v1 *ips;
962 struct keyboard *keyboard = virtual_keyboard->keyboard;
963
964 ips = zwp_input_panel_v1_get_input_panel_surface(virtual_keyboard->input_panel,
965 window_get_wl_surface(keyboard->window));
966
967 zwp_input_panel_surface_v1_set_toplevel(ips,
968 output_get_wl_output(output),
969 ZWP_INPUT_PANEL_SURFACE_V1_POSITION_CENTER_BOTTOM);
970
971 virtual_keyboard->toplevel = true;
972 }
973
974 static void
display_output_handler(struct output * output,void * data)975 display_output_handler(struct output *output, void *data) {
976 struct virtual_keyboard *keyboard = data;
977
978 if (!keyboard->toplevel)
979 set_toplevel(output, keyboard);
980 }
981
982 static void
keyboard_create(struct virtual_keyboard * virtual_keyboard)983 keyboard_create(struct virtual_keyboard *virtual_keyboard)
984 {
985 struct keyboard *keyboard;
986 const struct layout *layout;
987
988 layout = get_current_layout(virtual_keyboard);
989
990 keyboard = xzalloc(sizeof *keyboard);
991 keyboard->keyboard = virtual_keyboard;
992 keyboard->window = window_create_custom(virtual_keyboard->display);
993 keyboard->widget = window_add_widget(keyboard->window, keyboard);
994
995 virtual_keyboard->keyboard = keyboard;
996
997 window_set_title(keyboard->window, "Virtual keyboard");
998 window_set_user_data(keyboard->window, keyboard);
999
1000 widget_set_redraw_handler(keyboard->widget, redraw_handler);
1001 widget_set_resize_handler(keyboard->widget, resize_handler);
1002 widget_set_button_handler(keyboard->widget, button_handler);
1003 widget_set_touch_down_handler(keyboard->widget, touch_down_handler);
1004 widget_set_touch_up_handler(keyboard->widget, touch_up_handler);
1005
1006 window_schedule_resize(keyboard->window,
1007 layout->columns * key_width,
1008 layout->rows * key_height);
1009
1010 display_set_output_configure_handler(virtual_keyboard->display,
1011 display_output_handler);
1012 }
1013
1014 int
main(int argc,char * argv[])1015 main(int argc, char *argv[])
1016 {
1017 struct virtual_keyboard virtual_keyboard;
1018
1019 memset(&virtual_keyboard, 0, sizeof virtual_keyboard);
1020
1021 virtual_keyboard.display = display_create(&argc, argv);
1022 if (virtual_keyboard.display == NULL) {
1023 fprintf(stderr, "failed to create display: %s\n",
1024 strerror(errno));
1025 return -1;
1026 }
1027
1028 display_set_user_data(virtual_keyboard.display, &virtual_keyboard);
1029 display_set_global_handler(virtual_keyboard.display, global_handler);
1030
1031 if (virtual_keyboard.input_panel == NULL) {
1032 fprintf(stderr, "No input panel global\n");
1033 return -1;
1034 }
1035
1036 keyboard_create(&virtual_keyboard);
1037
1038 display_run(virtual_keyboard.display);
1039
1040 return 0;
1041 }
1042