2f28a8f1cdc0592bf1ac378c955bfabad6055710
[profile/ivi/weston-ivi-shell.git] / clients / keyboard.c
1 /*
2  * Copyright © 2012 Openismus GmbH
3  * Copyright © 2012 Intel Corporation
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and
6  * its documentation for any purpose is hereby granted without fee, provided
7  * that the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of the copyright holders not be used in
10  * advertising or publicity pertaining to distribution of the software
11  * without specific, written prior permission.  The copyright holders make
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied warranty.
14  *
15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <linux/input.h>
29 #include <cairo.h>
30
31 #include "window.h"
32 #include "input-method-client-protocol.h"
33 #include "text-client-protocol.h"
34
35 struct keyboard;
36
37 struct virtual_keyboard {
38         struct input_panel *input_panel;
39         struct input_method *input_method;
40         struct input_method_context *context;
41         struct display *display;
42         char *preedit_string;
43         uint32_t preedit_style;
44         struct {
45                 xkb_mod_mask_t shift_mask;
46         } keysym;
47         uint32_t serial;
48         uint32_t content_hint;
49         uint32_t content_purpose;
50         char *preferred_language;
51         char *surrounding_text;
52         struct keyboard *keyboard;
53 };
54
55 enum key_type {
56         keytype_default,
57         keytype_backspace,
58         keytype_enter,
59         keytype_space,
60         keytype_switch,
61         keytype_symbols,
62         keytype_tab,
63         keytype_arrow_up,
64         keytype_arrow_left,
65         keytype_arrow_right,
66         keytype_arrow_down,
67         keytype_style
68 };
69
70 struct key {
71         enum key_type key_type;
72
73         char *label;
74         char *alt;
75
76         unsigned int width;
77 };
78
79 struct layout {
80         const struct key *keys;
81         uint32_t count;
82
83         uint32_t columns;
84         uint32_t rows;
85
86         const char *language;
87         uint32_t text_direction;
88 };
89
90 static const struct key normal_keys[] = {
91         { keytype_default, "q", "Q", 1},
92         { keytype_default, "w", "W", 1},
93         { keytype_default, "e", "E", 1},
94         { keytype_default, "r", "R", 1},
95         { keytype_default, "t", "T", 1},
96         { keytype_default, "y", "Y", 1},
97         { keytype_default, "u", "U", 1},
98         { keytype_default, "i", "I", 1},
99         { keytype_default, "o", "O", 1},
100         { keytype_default, "p", "P", 1},
101         { keytype_backspace, "<--", "<--", 2},
102
103         { keytype_tab, "->|", "->|", 1},
104         { keytype_default, "a", "A", 1},
105         { keytype_default, "s", "S", 1},
106         { keytype_default, "d", "D", 1},
107         { keytype_default, "f", "F", 1},
108         { keytype_default, "g", "G", 1},
109         { keytype_default, "h", "H", 1},
110         { keytype_default, "j", "J", 1},
111         { keytype_default, "k", "K", 1},
112         { keytype_default, "l", "L", 1},
113         { keytype_enter, "Enter", "Enter", 2},
114
115         { keytype_switch, "ABC", "abc", 2},
116         { keytype_default, "z", "Z", 1},
117         { keytype_default, "x", "X", 1},
118         { keytype_default, "c", "C", 1},
119         { keytype_default, "v", "V", 1},
120         { keytype_default, "b", "B", 1},
121         { keytype_default, "n", "N", 1},
122         { keytype_default, "m", "M", 1},
123         { keytype_default, ",", ",", 1},
124         { keytype_default, ".", ".", 1},
125         { keytype_switch, "ABC", "abc", 1},
126
127         { keytype_symbols, "?123", "?123", 1},
128         { keytype_space, "", "", 5},
129         { keytype_arrow_up, "/\\", "/\\", 1},
130         { keytype_arrow_left, "<", "<", 1},
131         { keytype_arrow_right, ">", ">", 1},
132         { keytype_arrow_down, "\\/", "\\/", 1},
133         { keytype_style, "", "", 2}
134 };
135
136 static const struct key numeric_keys[] = {
137         { keytype_default, "1", "1", 1},
138         { keytype_default, "2", "2", 1},
139         { keytype_default, "3", "3", 1},
140         { keytype_default, "4", "4", 1},
141         { keytype_default, "5", "5", 1},
142         { keytype_default, "6", "6", 1},
143         { keytype_default, "7", "7", 1},
144         { keytype_default, "8", "8", 1},
145         { keytype_default, "9", "9", 1},
146         { keytype_default, "0", "0", 1},
147         { keytype_backspace, "<--", "<--", 2},
148
149         { keytype_space, "", "", 4},
150         { keytype_enter, "Enter", "Enter", 2},
151         { keytype_arrow_up, "/\\", "/\\", 1},
152         { keytype_arrow_left, "<", "<", 1},
153         { keytype_arrow_right, ">", ">", 1},
154         { keytype_arrow_down, "\\/", "\\/", 1},
155         { keytype_style, "", "", 2}
156 };
157
158 static const struct key arabic_keys[] = {
159         { keytype_default, "ض", "ض", 1},
160         { keytype_default, "ص", "ص", 1},
161         { keytype_default, "ث", "ث", 1},
162         { keytype_default, "ق", "ق", 1},
163         { keytype_default, "ف", "ف", 1},
164         { keytype_default, "غ", "إ", 1},
165         { keytype_default, "ع", "ع", 1},
166         { keytype_default, "ه", "ه", 1},
167         { keytype_default, "خ", "خ", 1},
168         { keytype_default, "ح", "ح", 1},
169         { keytype_default, "ج", "ج", 1},
170         { keytype_backspace, "-->", "-->", 2},
171
172         { keytype_tab, "->|", "->|", 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_default, "ن", "ن", 1},
181         { keytype_default, "م", "م", 1},
182         { keytype_default, "ك", "ك", 1},
183         { keytype_default, "د", "د", 1},
184         { keytype_enter, "Enter", "Enter", 2},
185
186         { keytype_switch, "ABC", "abc", 2},
187         { keytype_default, "ئ", "ئ", 1},
188         { keytype_default, "ء", "ء", 1},
189         { keytype_default, "ؤ", "ؤ", 1},
190         { keytype_default, "ر", "ر", 1},
191         { keytype_default, "ى", "آ", 1},
192         { keytype_default, "ة", "ة", 1},
193         { keytype_default, "و", "و", 1},
194         { keytype_default, "ز", "ز", 1},
195         { keytype_default, "ظ", "ظ", 1},
196         { keytype_switch, "ABC", "abc", 2},
197
198         { keytype_symbols, "؟٣٢١", "؟٣٢١", 1},
199         { keytype_default, "ذ", "ذ", 1},
200         { keytype_default, "،", "،", 1},
201         { keytype_space, "", "", 6},
202         { keytype_default, ".", ".", 1},
203         { keytype_default, "ط", "ط", 1},
204         { keytype_style, "", "", 2}
205 };
206
207
208 static const struct layout normal_layout = {
209         normal_keys,
210         sizeof(normal_keys) / sizeof(*normal_keys),
211         12,
212         4,
213         "en",
214         TEXT_INPUT_TEXT_DIRECTION_LTR
215 };
216
217 static const struct layout numeric_layout = {
218         numeric_keys,
219         sizeof(numeric_keys) / sizeof(*numeric_keys),
220         12,
221         2,
222         "en",
223         TEXT_INPUT_TEXT_DIRECTION_LTR
224 };
225
226 static const struct layout arabic_layout = {
227         arabic_keys,
228         sizeof(arabic_keys) / sizeof(*arabic_keys),
229         13,
230         4,
231         "ar",
232         TEXT_INPUT_TEXT_DIRECTION_RTL
233 };
234
235 static const char *style_labels[] = {
236         "default",
237         "none",
238         "active",
239         "inactive",
240         "highlight",
241         "underline",
242         "selection",
243         "incorrect"
244 };
245
246 static const double key_width = 60;
247 static const double key_height = 50;
248
249 enum keyboard_state {
250         keyboardstate_default,
251         keyboardstate_uppercase
252 };
253
254 struct keyboard {
255         struct virtual_keyboard *keyboard;
256         struct window *window;
257         struct widget *widget;
258
259         enum keyboard_state state;
260 };
261
262 static const char *
263 label_from_key(struct keyboard *keyboard,
264                const struct key *key)
265 {
266         if (key->key_type == keytype_style)
267                 return style_labels[keyboard->keyboard->preedit_style];
268
269         if (keyboard->state == keyboardstate_default)
270                 return key->label;
271         else
272                 return key->alt;
273 }
274
275 static void
276 draw_key(struct keyboard *keyboard,
277          const struct key *key,
278          cairo_t *cr,
279          unsigned int row,
280          unsigned int col)
281 {
282         const char *label;
283         cairo_text_extents_t extents;
284
285         cairo_save(cr);
286         cairo_rectangle(cr,
287                         col * key_width, row * key_height,
288                         key->width * key_width, key_height);
289         cairo_clip(cr);
290
291         /* Paint frame */
292         cairo_rectangle(cr,
293                         col * key_width, row * key_height,
294                         key->width * key_width, key_height);
295         cairo_set_line_width(cr, 3);
296         cairo_stroke(cr);
297
298         /* Paint text */
299         label = label_from_key(keyboard, key);
300         cairo_text_extents(cr, label, &extents);
301
302         cairo_translate(cr,
303                         col * key_width,
304                         row * key_height);
305         cairo_translate(cr,
306                         (key->width * key_width - extents.width) / 2,
307                         (key_height - extents.y_bearing) / 2);
308         cairo_show_text(cr, label);
309
310         cairo_restore(cr);
311 }
312
313 static const struct layout *
314 get_current_layout(struct virtual_keyboard *keyboard)
315 {
316         switch (keyboard->content_purpose) {
317                 case TEXT_INPUT_CONTENT_PURPOSE_DIGITS:
318                 case TEXT_INPUT_CONTENT_PURPOSE_NUMBER:
319                         return &numeric_layout;
320                 default:
321                         if (keyboard->preferred_language &&
322                             strcmp(keyboard->preferred_language, "ar") == 0)
323                                 return &arabic_layout;
324                         else
325                                 return &normal_layout;
326         }
327 }
328
329 static void
330 redraw_handler(struct widget *widget, void *data)
331 {
332         struct keyboard *keyboard = data;
333         cairo_surface_t *surface;
334         struct rectangle allocation;
335         cairo_t *cr;
336         unsigned int i;
337         unsigned int row = 0, col = 0;
338         const struct layout *layout;
339
340         layout = get_current_layout(keyboard->keyboard);
341
342         surface = window_get_surface(keyboard->window);
343         widget_get_allocation(keyboard->widget, &allocation);
344
345         cr = cairo_create(surface);
346         cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
347         cairo_clip(cr);
348
349         cairo_select_font_face(cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
350         cairo_set_font_size(cr, 16);
351
352         cairo_translate(cr, allocation.x, allocation.y);
353
354         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
355         cairo_set_source_rgba(cr, 1, 1, 1, 0.75);
356         cairo_rectangle(cr, 0, 0, layout->columns * key_width, layout->rows * key_height);
357         cairo_paint(cr);
358
359         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
360
361         for (i = 0; i < layout->count; ++i) {
362                 cairo_set_source_rgb(cr, 0, 0, 0);
363                 draw_key(keyboard, &layout->keys[i], cr, row, col);
364                 col += layout->keys[i].width;
365                 if (col >= layout->columns) {
366                         row += 1;
367                         col = 0;
368                 }
369         }
370
371         cairo_destroy(cr);
372         cairo_surface_destroy(surface);
373 }
374
375 static void
376 resize_handler(struct widget *widget,
377                int32_t width, int32_t height, void *data)
378 {
379         /* struct keyboard *keyboard = data; */
380 }
381
382 static void
383 virtual_keyboard_commit_preedit(struct virtual_keyboard *keyboard)
384 {
385         if (!keyboard->preedit_string ||
386             strlen(keyboard->preedit_string) == 0)
387                 return;
388
389         input_method_context_preedit_cursor(keyboard->context,
390                                             keyboard->serial,
391                                             0);
392         input_method_context_preedit_string(keyboard->context,
393                                             keyboard->serial,
394                                             "",
395                                             "");
396         input_method_context_cursor_position(keyboard->context,
397                                              keyboard->serial,
398                                              0, 0);
399         input_method_context_commit_string(keyboard->context,
400                                            keyboard->serial,
401                                            keyboard->preedit_string);
402         free(keyboard->preedit_string);
403         keyboard->preedit_string = strdup("");
404 }
405
406 static void
407 virtual_keyboard_send_preedit(struct virtual_keyboard *keyboard,
408                               int32_t cursor)
409 {
410         uint32_t index = strlen(keyboard->preedit_string);
411
412         if (keyboard->preedit_style)
413                 input_method_context_preedit_styling(keyboard->context,
414                                                      keyboard->serial,
415                                                      0,
416                                                      strlen(keyboard->preedit_string),
417                                                      keyboard->preedit_style);
418         if (cursor > 0)
419                 index = cursor;
420         input_method_context_preedit_cursor(keyboard->context,
421                                             keyboard->serial,
422                                             index);
423         input_method_context_preedit_string(keyboard->context,
424                                             keyboard->serial,
425                                             keyboard->preedit_string,
426                                             keyboard->preedit_string);
427 }
428
429 static void
430 keyboard_handle_key(struct keyboard *keyboard, uint32_t time, const struct key *key, struct input *input, enum wl_pointer_button_state state)
431 {
432         const char *label = keyboard->state == keyboardstate_default ? key->label : key->alt;
433         xkb_mod_mask_t mod_mask = keyboard->state == keyboardstate_default ? 0 : keyboard->keyboard->keysym.shift_mask;
434         uint32_t key_state = (state == WL_POINTER_BUTTON_STATE_PRESSED) ? WL_KEYBOARD_KEY_STATE_PRESSED : WL_KEYBOARD_KEY_STATE_RELEASED;
435
436         switch (key->key_type) {
437                 case keytype_default:
438                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
439                                 break;
440
441                         keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
442                                                                     label);
443                         virtual_keyboard_send_preedit(keyboard->keyboard, -1);
444                         break;
445                 case keytype_backspace:
446                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
447                                 break;
448
449                         if (strlen(keyboard->keyboard->preedit_string) == 0) {
450                                 input_method_context_delete_surrounding_text(keyboard->keyboard->context,
451                                                                              keyboard->keyboard->serial,
452                                                                              -1, 1);
453                         } else {
454                                 keyboard->keyboard->preedit_string[strlen(keyboard->keyboard->preedit_string) - 1] = '\0';
455                                 virtual_keyboard_send_preedit(keyboard->keyboard, -1);
456                         }
457                         break;
458                 case keytype_enter:
459                         virtual_keyboard_commit_preedit(keyboard->keyboard);
460                         input_method_context_keysym(keyboard->keyboard->context,
461                                                     display_get_serial(keyboard->keyboard->display),
462                                                     time, 
463                                                     XKB_KEY_Return, key_state, mod_mask);
464                         break;
465                 case keytype_space:
466                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
467                                 break;
468                         keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
469                                                                     " ");
470                         virtual_keyboard_commit_preedit(keyboard->keyboard);
471                         break;
472                 case keytype_switch:
473                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
474                                 break;
475                         if (keyboard->state == keyboardstate_default)
476                                 keyboard->state = keyboardstate_uppercase;
477                         else
478                                 keyboard->state = keyboardstate_default;
479                         break;
480                 case keytype_symbols:
481                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
482                                 break;
483                         break;
484                 case keytype_tab:
485                         virtual_keyboard_commit_preedit(keyboard->keyboard);
486                         input_method_context_keysym(keyboard->keyboard->context,
487                                                     display_get_serial(keyboard->keyboard->display),
488                                                     time, 
489                                                     XKB_KEY_Tab, key_state, mod_mask);
490                         break;
491                 case keytype_arrow_up:
492                         virtual_keyboard_commit_preedit(keyboard->keyboard);
493                         input_method_context_keysym(keyboard->keyboard->context,
494                                                     display_get_serial(keyboard->keyboard->display),
495                                                     time, 
496                                                     XKB_KEY_Up, key_state, mod_mask);
497                         break;
498                 case keytype_arrow_left:
499                         virtual_keyboard_commit_preedit(keyboard->keyboard);
500                         input_method_context_keysym(keyboard->keyboard->context,
501                                                     display_get_serial(keyboard->keyboard->display),
502                                                     time, 
503                                                     XKB_KEY_Left, key_state, mod_mask);
504                         break;
505                 case keytype_arrow_right:
506                         virtual_keyboard_commit_preedit(keyboard->keyboard);
507                         input_method_context_keysym(keyboard->keyboard->context,
508                                                     display_get_serial(keyboard->keyboard->display),
509                                                     time, 
510                                                     XKB_KEY_Right, key_state, mod_mask);
511                         break;
512                 case keytype_arrow_down:
513                         virtual_keyboard_commit_preedit(keyboard->keyboard);
514                         input_method_context_keysym(keyboard->keyboard->context,
515                                                     display_get_serial(keyboard->keyboard->display),
516                                                     time, 
517                                                     XKB_KEY_Down, key_state, mod_mask);
518                         break;
519                 case keytype_style:
520                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
521                                 break;
522                         keyboard->keyboard->preedit_style = (keyboard->keyboard->preedit_style + 1) % 8; /* TODO */
523                         virtual_keyboard_send_preedit(keyboard->keyboard, -1);
524                         break;
525         }
526 }
527
528 static void
529 button_handler(struct widget *widget,
530                struct input *input, uint32_t time,
531                uint32_t button,
532                enum wl_pointer_button_state state, void *data)
533 {
534         struct keyboard *keyboard = data;
535         struct rectangle allocation;
536         int32_t x, y;
537         int row, col;
538         unsigned int i;
539         const struct layout *layout;
540
541         layout = get_current_layout(keyboard->keyboard);
542
543         if (button != BTN_LEFT) {
544                 return;
545         }
546
547         input_get_position(input, &x, &y);
548
549         widget_get_allocation(keyboard->widget, &allocation);
550         x -= allocation.x;
551         y -= allocation.y;
552
553         row = y / key_height;
554         col = x / key_width + row * layout->columns;
555         for (i = 0; i < layout->count; ++i) {
556                 col -= layout->keys[i].width;
557                 if (col < 0) {
558                         keyboard_handle_key(keyboard, time, &layout->keys[i], input, state);
559                         break;
560                 }
561         }
562
563         widget_schedule_redraw(widget);
564 }
565
566 static void
567 handle_surrounding_text(void *data,
568                         struct input_method_context *context,
569                         const char *text,
570                         uint32_t cursor,
571                         uint32_t anchor)
572 {
573         struct virtual_keyboard *keyboard = data;
574
575         free(keyboard->surrounding_text);
576         keyboard->surrounding_text = strdup(text);
577 }
578
579 static void
580 handle_reset(void *data,
581              struct input_method_context *context,
582              uint32_t serial)
583 {
584         struct virtual_keyboard *keyboard = data;
585
586         fprintf(stderr, "Reset pre-edit buffer\n");
587
588         if (strlen(keyboard->preedit_string)) {
589                 input_method_context_preedit_cursor(context,
590                                                     serial,
591                                                     0);
592                 input_method_context_preedit_string(context,
593                                                     serial,
594                                                     "",
595                                                     "");
596                 free(keyboard->preedit_string);
597                 keyboard->preedit_string = strdup("");
598         }
599
600         keyboard->serial = serial;
601 }
602
603 static void
604 handle_content_type(void *data,
605                     struct input_method_context *context,
606                     uint32_t hint,
607                     uint32_t purpose)
608 {
609         struct virtual_keyboard *keyboard = data;
610
611         keyboard->content_hint = hint;
612         keyboard->content_purpose = purpose;
613 }
614
615 static void
616 handle_invoke_action(void *data,
617                      struct input_method_context *context,
618                      uint32_t button,
619                      uint32_t index)
620 {
621         struct virtual_keyboard *keyboard = data;
622
623         if (button != BTN_LEFT)
624                 return;
625
626         virtual_keyboard_send_preedit(keyboard, index);
627 }
628
629 static void
630 handle_commit(void *data,
631               struct input_method_context *context)
632 {
633         struct virtual_keyboard *keyboard = data;
634         const struct layout *layout;
635
636         layout = get_current_layout(keyboard);
637
638         if (keyboard->surrounding_text)
639                 fprintf(stderr, "Surrounding text updated: %s\n", keyboard->surrounding_text);
640
641         window_schedule_resize(keyboard->keyboard->window,
642                                layout->columns * key_width,
643                                layout->rows * key_height);
644
645         input_method_context_language(context, keyboard->serial, layout->language);
646         input_method_context_text_direction(context, keyboard->serial, layout->text_direction);
647
648         widget_schedule_redraw(keyboard->keyboard->widget);
649 }
650
651 static void
652 handle_preferred_language(void *data,
653                           struct input_method_context *context,
654                           const char *language)
655 {
656         struct virtual_keyboard *keyboard = data;
657
658         if (keyboard->preferred_language)
659                 free(keyboard->preferred_language);
660
661         keyboard->preferred_language = NULL;
662
663         if (language)
664                 keyboard->preferred_language = strdup(language);
665 }
666
667 static const struct input_method_context_listener input_method_context_listener = {
668         handle_surrounding_text,
669         handle_reset,
670         handle_content_type,
671         handle_invoke_action,
672         handle_commit,
673         handle_preferred_language
674 };
675
676 static void
677 input_method_activate(void *data,
678                       struct input_method *input_method,
679                       struct input_method_context *context,
680                       uint32_t serial)
681 {
682         struct virtual_keyboard *keyboard = data;
683         struct wl_array modifiers_map;
684         const struct layout *layout;
685
686         keyboard->keyboard->state = keyboardstate_default;
687
688         if (keyboard->context)
689                 input_method_context_destroy(keyboard->context);
690
691         if (keyboard->preedit_string)
692                 free(keyboard->preedit_string);
693
694         keyboard->preedit_string = strdup("");
695         keyboard->content_hint = 0;
696         keyboard->content_purpose = 0;
697         free(keyboard->preferred_language);
698         keyboard->preferred_language = NULL;
699         free(keyboard->surrounding_text);
700         keyboard->surrounding_text = NULL;
701
702         keyboard->serial = serial;
703
704         keyboard->context = context;
705         input_method_context_add_listener(context,
706                                           &input_method_context_listener,
707                                           keyboard);
708
709         wl_array_init(&modifiers_map);
710         keysym_modifiers_add(&modifiers_map, "Shift");
711         keysym_modifiers_add(&modifiers_map, "Control");
712         keysym_modifiers_add(&modifiers_map, "Mod1");
713         input_method_context_modifiers_map(context, &modifiers_map);
714         keyboard->keysym.shift_mask = keysym_modifiers_get_mask(&modifiers_map, "Shift");
715         wl_array_release(&modifiers_map);
716
717         layout = get_current_layout(keyboard);
718
719         window_schedule_resize(keyboard->keyboard->window,
720                                layout->columns * key_width,
721                                layout->rows * key_height);
722
723         input_method_context_language(context, keyboard->serial, layout->language);
724         input_method_context_text_direction(context, keyboard->serial, layout->text_direction);
725
726         widget_schedule_redraw(keyboard->keyboard->widget);
727 }
728
729 static void
730 input_method_deactivate(void *data,
731                         struct input_method *input_method,
732                         struct input_method_context *context)
733 {
734         struct virtual_keyboard *keyboard = data;
735
736         if (!keyboard->context)
737                 return;
738
739         input_method_context_destroy(keyboard->context);
740         keyboard->context = NULL;
741 }
742
743 static const struct input_method_listener input_method_listener = {
744         input_method_activate,
745         input_method_deactivate
746 };
747
748 static void
749 global_handler(struct display *display, uint32_t name,
750                const char *interface, uint32_t version, void *data)
751 {
752         struct virtual_keyboard *keyboard = data;
753
754         if (!strcmp(interface, "input_panel")) {
755                 keyboard->input_panel =
756                         display_bind(display, name, &input_panel_interface, 1);
757         } else if (!strcmp(interface, "input_method")) {
758                 keyboard->input_method =
759                         display_bind(display, name,
760                                      &input_method_interface, 1);
761                 input_method_add_listener(keyboard->input_method, &input_method_listener, keyboard);
762         }
763 }
764
765 static void
766 keyboard_create(struct output *output, struct virtual_keyboard *virtual_keyboard)
767 {
768         struct keyboard *keyboard;
769         const struct layout *layout;
770         struct input_panel_surface *ips;
771
772         layout = get_current_layout(virtual_keyboard);
773
774         keyboard = malloc(sizeof *keyboard);
775         memset(keyboard, 0, sizeof *keyboard);
776
777         keyboard->keyboard = virtual_keyboard;
778         keyboard->window = window_create_custom(virtual_keyboard->display);
779         keyboard->widget = window_add_widget(keyboard->window, keyboard);
780
781         virtual_keyboard->keyboard = keyboard;
782
783         window_set_title(keyboard->window, "Virtual keyboard");
784         window_set_user_data(keyboard->window, keyboard);
785
786         widget_set_redraw_handler(keyboard->widget, redraw_handler);
787         widget_set_resize_handler(keyboard->widget, resize_handler);
788         widget_set_button_handler(keyboard->widget, button_handler);
789
790         window_schedule_resize(keyboard->window,
791                                layout->columns * key_width,
792                                layout->rows * key_height);
793
794
795         ips = input_panel_get_input_panel_surface(virtual_keyboard->input_panel,
796                                                   window_get_wl_surface(keyboard->window));
797
798         input_panel_surface_set_toplevel(ips, INPUT_PANEL_SURFACE_POSITION_CENTER_BOTTOM);
799 }
800
801 static void
802 handle_output_configure(struct output *output, void *data)
803 {
804         struct virtual_keyboard *virtual_keyboard = data;
805
806         /* skip existing outputs */
807         if (output_get_user_data(output))
808                 return;
809
810         output_set_user_data(output, virtual_keyboard);
811
812         keyboard_create(output, virtual_keyboard);
813 }
814
815 int
816 main(int argc, char *argv[])
817 {
818         struct virtual_keyboard virtual_keyboard;
819
820         memset(&virtual_keyboard, 0, sizeof virtual_keyboard);
821
822         virtual_keyboard.display = display_create(&argc, argv);
823         if (virtual_keyboard.display == NULL) {
824                 fprintf(stderr, "failed to create display: %m\n");
825                 return -1;
826         }
827
828         display_set_user_data(virtual_keyboard.display, &virtual_keyboard);
829         display_set_global_handler(virtual_keyboard.display, global_handler);
830         display_set_output_configure_handler(virtual_keyboard.display, handle_output_configure);
831
832         display_run(virtual_keyboard.display);
833
834         return 0;
835 }