nested: Remove the surface from the surface list when destroyed
[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 wl_input_panel *input_panel;
39         struct wl_input_method *input_method;
40         struct wl_input_method_context *context;
41         struct display *display;
42         struct output *output;
43         char *preedit_string;
44         uint32_t preedit_style;
45         struct {
46                 xkb_mod_mask_t shift_mask;
47         } keysym;
48         uint32_t serial;
49         uint32_t content_hint;
50         uint32_t content_purpose;
51         char *preferred_language;
52         char *surrounding_text;
53         uint32_t surrounding_cursor;
54         struct keyboard *keyboard;
55 };
56
57 enum key_type {
58         keytype_default,
59         keytype_backspace,
60         keytype_enter,
61         keytype_space,
62         keytype_switch,
63         keytype_symbols,
64         keytype_tab,
65         keytype_arrow_up,
66         keytype_arrow_left,
67         keytype_arrow_right,
68         keytype_arrow_down,
69         keytype_style
70 };
71
72 struct key {
73         enum key_type key_type;
74
75         char *label;
76         char *alt;
77
78         unsigned int width;
79 };
80
81 struct layout {
82         const struct key *keys;
83         uint32_t count;
84
85         uint32_t columns;
86         uint32_t rows;
87
88         const char *language;
89         uint32_t text_direction;
90 };
91
92 static const struct key normal_keys[] = {
93         { keytype_default, "q", "Q", 1},
94         { keytype_default, "w", "W", 1},
95         { keytype_default, "e", "E", 1},
96         { keytype_default, "r", "R", 1},
97         { keytype_default, "t", "T", 1},
98         { keytype_default, "y", "Y", 1},
99         { keytype_default, "u", "U", 1},
100         { keytype_default, "i", "I", 1},
101         { keytype_default, "o", "O", 1},
102         { keytype_default, "p", "P", 1},
103         { keytype_backspace, "<--", "<--", 2},
104
105         { keytype_tab, "->|", "->|", 1},
106         { keytype_default, "a", "A", 1},
107         { keytype_default, "s", "S", 1},
108         { keytype_default, "d", "D", 1},
109         { keytype_default, "f", "F", 1},
110         { keytype_default, "g", "G", 1},
111         { keytype_default, "h", "H", 1},
112         { keytype_default, "j", "J", 1},
113         { keytype_default, "k", "K", 1},
114         { keytype_default, "l", "L", 1},
115         { keytype_enter, "Enter", "Enter", 2},
116
117         { keytype_switch, "ABC", "abc", 2},
118         { keytype_default, "z", "Z", 1},
119         { keytype_default, "x", "X", 1},
120         { keytype_default, "c", "C", 1},
121         { keytype_default, "v", "V", 1},
122         { keytype_default, "b", "B", 1},
123         { keytype_default, "n", "N", 1},
124         { keytype_default, "m", "M", 1},
125         { keytype_default, ",", ",", 1},
126         { keytype_default, ".", ".", 1},
127         { keytype_switch, "ABC", "abc", 1},
128
129         { keytype_symbols, "?123", "?123", 1},
130         { keytype_space, "", "", 5},
131         { keytype_arrow_up, "/\\", "/\\", 1},
132         { keytype_arrow_left, "<", "<", 1},
133         { keytype_arrow_right, ">", ">", 1},
134         { keytype_arrow_down, "\\/", "\\/", 1},
135         { keytype_style, "", "", 2}
136 };
137
138 static const struct key numeric_keys[] = {
139         { keytype_default, "1", "1", 1},
140         { keytype_default, "2", "2", 1},
141         { keytype_default, "3", "3", 1},
142         { keytype_default, "4", "4", 1},
143         { keytype_default, "5", "5", 1},
144         { keytype_default, "6", "6", 1},
145         { keytype_default, "7", "7", 1},
146         { keytype_default, "8", "8", 1},
147         { keytype_default, "9", "9", 1},
148         { keytype_default, "0", "0", 1},
149         { keytype_backspace, "<--", "<--", 2},
150
151         { keytype_space, "", "", 4},
152         { keytype_enter, "Enter", "Enter", 2},
153         { keytype_arrow_up, "/\\", "/\\", 1},
154         { keytype_arrow_left, "<", "<", 1},
155         { keytype_arrow_right, ">", ">", 1},
156         { keytype_arrow_down, "\\/", "\\/", 1},
157         { keytype_style, "", "", 2}
158 };
159
160 static const struct key arabic_keys[] = {
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_default, "ح", "ح", 1},
171         { keytype_default, "ج", "ج", 1},
172         { keytype_backspace, "-->", "-->", 2},
173
174         { keytype_tab, "->|", "->|", 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_default, "ك", "ك", 1},
185         { keytype_default, "د", "د", 1},
186         { keytype_enter, "Enter", "Enter", 2},
187
188         { keytype_switch, "ABC", "abc", 2},
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_default, "ز", "ز", 1},
197         { keytype_default, "ظ", "ظ", 1},
198         { keytype_switch, "ABC", "abc", 2},
199
200         { keytype_symbols, "؟٣٢١", "؟٣٢١", 1},
201         { keytype_default, "ذ", "ذ", 1},
202         { keytype_default, "،", "،", 1},
203         { keytype_space, "", "", 6},
204         { keytype_default, ".", ".", 1},
205         { keytype_default, "ط", "ط", 1},
206         { keytype_style, "", "", 2}
207 };
208
209
210 static const struct layout normal_layout = {
211         normal_keys,
212         sizeof(normal_keys) / sizeof(*normal_keys),
213         12,
214         4,
215         "en",
216         WL_TEXT_INPUT_TEXT_DIRECTION_LTR
217 };
218
219 static const struct layout numeric_layout = {
220         numeric_keys,
221         sizeof(numeric_keys) / sizeof(*numeric_keys),
222         12,
223         2,
224         "en",
225         WL_TEXT_INPUT_TEXT_DIRECTION_LTR
226 };
227
228 static const struct layout arabic_layout = {
229         arabic_keys,
230         sizeof(arabic_keys) / sizeof(*arabic_keys),
231         13,
232         4,
233         "ar",
234         WL_TEXT_INPUT_TEXT_DIRECTION_RTL
235 };
236
237 static const char *style_labels[] = {
238         "default",
239         "none",
240         "active",
241         "inactive",
242         "highlight",
243         "underline",
244         "selection",
245         "incorrect"
246 };
247
248 static const double key_width = 60;
249 static const double key_height = 50;
250
251 enum keyboard_state {
252         keyboardstate_default,
253         keyboardstate_uppercase
254 };
255
256 struct keyboard {
257         struct virtual_keyboard *keyboard;
258         struct window *window;
259         struct widget *widget;
260
261         enum keyboard_state state;
262 };
263
264 static const char *
265 label_from_key(struct keyboard *keyboard,
266                const struct key *key)
267 {
268         if (key->key_type == keytype_style)
269                 return style_labels[keyboard->keyboard->preedit_style];
270
271         if (keyboard->state == keyboardstate_default)
272                 return key->label;
273         else
274                 return key->alt;
275 }
276
277 static void
278 draw_key(struct keyboard *keyboard,
279          const struct key *key,
280          cairo_t *cr,
281          unsigned int row,
282          unsigned int col)
283 {
284         const char *label;
285         cairo_text_extents_t extents;
286
287         cairo_save(cr);
288         cairo_rectangle(cr,
289                         col * key_width, row * key_height,
290                         key->width * key_width, key_height);
291         cairo_clip(cr);
292
293         /* Paint frame */
294         cairo_rectangle(cr,
295                         col * key_width, row * key_height,
296                         key->width * key_width, key_height);
297         cairo_set_line_width(cr, 3);
298         cairo_stroke(cr);
299
300         /* Paint text */
301         label = label_from_key(keyboard, key);
302         cairo_text_extents(cr, label, &extents);
303
304         cairo_translate(cr,
305                         col * key_width,
306                         row * key_height);
307         cairo_translate(cr,
308                         (key->width * key_width - extents.width) / 2,
309                         (key_height - extents.y_bearing) / 2);
310         cairo_show_text(cr, label);
311
312         cairo_restore(cr);
313 }
314
315 static const struct layout *
316 get_current_layout(struct virtual_keyboard *keyboard)
317 {
318         switch (keyboard->content_purpose) {
319                 case WL_TEXT_INPUT_CONTENT_PURPOSE_DIGITS:
320                 case WL_TEXT_INPUT_CONTENT_PURPOSE_NUMBER:
321                         return &numeric_layout;
322                 default:
323                         if (keyboard->preferred_language &&
324                             strcmp(keyboard->preferred_language, "ar") == 0)
325                                 return &arabic_layout;
326                         else
327                                 return &normal_layout;
328         }
329 }
330
331 static void
332 redraw_handler(struct widget *widget, void *data)
333 {
334         struct keyboard *keyboard = data;
335         cairo_surface_t *surface;
336         struct rectangle allocation;
337         cairo_t *cr;
338         unsigned int i;
339         unsigned int row = 0, col = 0;
340         const struct layout *layout;
341
342         layout = get_current_layout(keyboard->keyboard);
343
344         surface = window_get_surface(keyboard->window);
345         widget_get_allocation(keyboard->widget, &allocation);
346
347         cr = cairo_create(surface);
348         cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
349         cairo_clip(cr);
350
351         cairo_select_font_face(cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
352         cairo_set_font_size(cr, 16);
353
354         cairo_translate(cr, allocation.x, allocation.y);
355
356         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
357         cairo_set_source_rgba(cr, 1, 1, 1, 0.75);
358         cairo_rectangle(cr, 0, 0, layout->columns * key_width, layout->rows * key_height);
359         cairo_paint(cr);
360
361         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
362
363         for (i = 0; i < layout->count; ++i) {
364                 cairo_set_source_rgb(cr, 0, 0, 0);
365                 draw_key(keyboard, &layout->keys[i], cr, row, col);
366                 col += layout->keys[i].width;
367                 if (col >= layout->columns) {
368                         row += 1;
369                         col = 0;
370                 }
371         }
372
373         cairo_destroy(cr);
374         cairo_surface_destroy(surface);
375 }
376
377 static void
378 resize_handler(struct widget *widget,
379                int32_t width, int32_t height, void *data)
380 {
381         /* struct keyboard *keyboard = data; */
382 }
383
384 static char *
385 insert_text(const char *text, uint32_t offset, const char *insert)
386 {
387         char *new_text = xmalloc(strlen(text) + strlen(insert) + 1);
388
389         strncat(new_text, text, offset);
390         new_text[offset] = '\0';
391         strcat(new_text, insert);
392         strcat(new_text, text + offset);
393
394         return new_text;
395 }
396
397 static void
398 virtual_keyboard_commit_preedit(struct virtual_keyboard *keyboard)
399 {
400         char *surrounding_text;
401
402         if (!keyboard->preedit_string ||
403             strlen(keyboard->preedit_string) == 0)
404                 return;
405
406         wl_input_method_context_cursor_position(keyboard->context,
407                                                 0, 0);
408         wl_input_method_context_commit_string(keyboard->context,
409                                               keyboard->serial,
410                                               keyboard->preedit_string);
411
412         if (keyboard->surrounding_text) {
413                 surrounding_text = insert_text(keyboard->surrounding_text,
414                                                keyboard->surrounding_cursor,
415                                                keyboard->preedit_string);
416                 free(keyboard->surrounding_text);
417                 keyboard->surrounding_text = surrounding_text;
418                 keyboard->surrounding_cursor += strlen(keyboard->preedit_string);
419         } else {
420                 keyboard->surrounding_text = strdup(keyboard->preedit_string);
421                 keyboard->surrounding_cursor = strlen(keyboard->preedit_string);
422         }
423
424         free(keyboard->preedit_string);
425         keyboard->preedit_string = strdup("");
426 }
427
428 static void
429 virtual_keyboard_send_preedit(struct virtual_keyboard *keyboard,
430                               int32_t cursor)
431 {
432         uint32_t index = strlen(keyboard->preedit_string);
433
434         if (keyboard->preedit_style)
435                 wl_input_method_context_preedit_styling(keyboard->context,
436                                                         0,
437                                                         strlen(keyboard->preedit_string),
438                                                         keyboard->preedit_style);
439         if (cursor > 0)
440                 index = cursor;
441         wl_input_method_context_preedit_cursor(keyboard->context,
442                                                index);
443         wl_input_method_context_preedit_string(keyboard->context,
444                                                keyboard->serial,
445                                                keyboard->preedit_string,
446                                                keyboard->preedit_string);
447 }
448
449 static const char *
450 prev_utf8_char(const char *s, const char *p)
451 {
452         for (--p; p >= s; --p) {
453                 if ((*p & 0xc0) != 0x80)
454                         return p;
455         }
456         return NULL;
457 }
458
459 static void
460 delete_before_cursor(struct virtual_keyboard *keyboard)
461 {
462         const char *start, *end;
463
464         if (!keyboard->surrounding_text) {
465                 fprintf(stderr, "delete_before_cursor: No surrounding text available\n");
466                 return;
467         }
468
469         start = prev_utf8_char(keyboard->surrounding_text,
470                                keyboard->surrounding_text + keyboard->surrounding_cursor);
471         if (!start) {
472                 fprintf(stderr, "delete_before_cursor: No previous character to delete\n");
473                 return;
474         }
475
476         end = keyboard->surrounding_text + keyboard->surrounding_cursor;
477
478         wl_input_method_context_delete_surrounding_text(keyboard->context,
479                                                         (start - keyboard->surrounding_text) - keyboard->surrounding_cursor,
480                                                         end - start);
481         wl_input_method_context_commit_string(keyboard->context,
482                                               keyboard->serial,
483                                               "");
484
485         /* Update surrounding text */
486         keyboard->surrounding_cursor = start - keyboard->surrounding_text;
487         keyboard->surrounding_text[keyboard->surrounding_cursor] = '\0';
488         if (*end)
489                 memmove(keyboard->surrounding_text + keyboard->surrounding_cursor, end, strlen(end));
490 }
491
492 static void
493 keyboard_handle_key(struct keyboard *keyboard, uint32_t time, const struct key *key, struct input *input, enum wl_pointer_button_state state)
494 {
495         const char *label = keyboard->state == keyboardstate_default ? key->label : key->alt;
496         xkb_mod_mask_t mod_mask = keyboard->state == keyboardstate_default ? 0 : keyboard->keyboard->keysym.shift_mask;
497         uint32_t key_state = (state == WL_POINTER_BUTTON_STATE_PRESSED) ? WL_KEYBOARD_KEY_STATE_PRESSED : WL_KEYBOARD_KEY_STATE_RELEASED;
498
499         switch (key->key_type) {
500                 case keytype_default:
501                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
502                                 break;
503
504                         keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
505                                                                     label);
506                         virtual_keyboard_send_preedit(keyboard->keyboard, -1);
507                         break;
508                 case keytype_backspace:
509                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
510                                 break;
511
512                         if (strlen(keyboard->keyboard->preedit_string) == 0) {
513                                 delete_before_cursor(keyboard->keyboard);
514                         } else {
515                                 keyboard->keyboard->preedit_string[strlen(keyboard->keyboard->preedit_string) - 1] = '\0';
516                                 virtual_keyboard_send_preedit(keyboard->keyboard, -1);
517                         }
518                         break;
519                 case keytype_enter:
520                         virtual_keyboard_commit_preedit(keyboard->keyboard);
521                         wl_input_method_context_keysym(keyboard->keyboard->context,
522                                                        display_get_serial(keyboard->keyboard->display),
523                                                        time, 
524                                                        XKB_KEY_Return, key_state, mod_mask);
525                         break;
526                 case keytype_space:
527                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
528                                 break;
529                         keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
530                                                                     " ");
531                         virtual_keyboard_commit_preedit(keyboard->keyboard);
532                         break;
533                 case keytype_switch:
534                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
535                                 break;
536                         if (keyboard->state == keyboardstate_default)
537                                 keyboard->state = keyboardstate_uppercase;
538                         else
539                                 keyboard->state = keyboardstate_default;
540                         break;
541                 case keytype_symbols:
542                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
543                                 break;
544                         break;
545                 case keytype_tab:
546                         virtual_keyboard_commit_preedit(keyboard->keyboard);
547                         wl_input_method_context_keysym(keyboard->keyboard->context,
548                                                        display_get_serial(keyboard->keyboard->display),
549                                                        time, 
550                                                        XKB_KEY_Tab, key_state, mod_mask);
551                         break;
552                 case keytype_arrow_up:
553                         virtual_keyboard_commit_preedit(keyboard->keyboard);
554                         wl_input_method_context_keysym(keyboard->keyboard->context,
555                                                        display_get_serial(keyboard->keyboard->display),
556                                                        time, 
557                                                        XKB_KEY_Up, key_state, mod_mask);
558                         break;
559                 case keytype_arrow_left:
560                         virtual_keyboard_commit_preedit(keyboard->keyboard);
561                         wl_input_method_context_keysym(keyboard->keyboard->context,
562                                                        display_get_serial(keyboard->keyboard->display),
563                                                        time, 
564                                                        XKB_KEY_Left, key_state, mod_mask);
565                         break;
566                 case keytype_arrow_right:
567                         virtual_keyboard_commit_preedit(keyboard->keyboard);
568                         wl_input_method_context_keysym(keyboard->keyboard->context,
569                                                        display_get_serial(keyboard->keyboard->display),
570                                                        time, 
571                                                        XKB_KEY_Right, key_state, mod_mask);
572                         break;
573                 case keytype_arrow_down:
574                         virtual_keyboard_commit_preedit(keyboard->keyboard);
575                         wl_input_method_context_keysym(keyboard->keyboard->context,
576                                                        display_get_serial(keyboard->keyboard->display),
577                                                        time, 
578                                                        XKB_KEY_Down, key_state, mod_mask);
579                         break;
580                 case keytype_style:
581                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
582                                 break;
583                         keyboard->keyboard->preedit_style = (keyboard->keyboard->preedit_style + 1) % 8; /* TODO */
584                         virtual_keyboard_send_preedit(keyboard->keyboard, -1);
585                         break;
586         }
587 }
588
589 static void
590 button_handler(struct widget *widget,
591                struct input *input, uint32_t time,
592                uint32_t button,
593                enum wl_pointer_button_state state, void *data)
594 {
595         struct keyboard *keyboard = data;
596         struct rectangle allocation;
597         int32_t x, y;
598         int row, col;
599         unsigned int i;
600         const struct layout *layout;
601
602         layout = get_current_layout(keyboard->keyboard);
603
604         if (button != BTN_LEFT) {
605                 return;
606         }
607
608         input_get_position(input, &x, &y);
609
610         widget_get_allocation(keyboard->widget, &allocation);
611         x -= allocation.x;
612         y -= allocation.y;
613
614         row = y / key_height;
615         col = x / key_width + row * layout->columns;
616         for (i = 0; i < layout->count; ++i) {
617                 col -= layout->keys[i].width;
618                 if (col < 0) {
619                         keyboard_handle_key(keyboard, time, &layout->keys[i], input, state);
620                         break;
621                 }
622         }
623
624         widget_schedule_redraw(widget);
625 }
626
627 static void
628 touch_down_handler(struct widget *widget, struct input *input,
629                    uint32_t serial, uint32_t time, int32_t id,
630                    float x, float y, void *data)
631 {
632
633         struct keyboard *keyboard = data;
634         struct rectangle allocation;
635         int row, col;
636         unsigned int i;
637         const struct layout *layout;
638
639         layout = get_current_layout(keyboard->keyboard);
640
641         widget_get_allocation(keyboard->widget, &allocation);
642
643         x -= allocation.x;
644         y -= allocation.y;
645
646         row = (int)y / key_height;
647         col = (int)x / key_width + row * layout->columns;
648         for (i = 0; i < layout->count; ++i) {
649                 col -= layout->keys[i].width;
650                 if (col < 0) {
651                         keyboard_handle_key(keyboard, time, &layout->keys[i], input, WL_POINTER_BUTTON_STATE_PRESSED);
652                         break;
653                 }
654         }
655
656         widget_schedule_redraw(widget);
657 }
658
659 static void
660 touch_up_handler(struct widget *widget, struct input *input,
661                                 uint32_t serial, uint32_t time, int32_t id,
662                                 void *data)
663 {
664
665 }
666
667 static void
668 handle_surrounding_text(void *data,
669                         struct wl_input_method_context *context,
670                         const char *text,
671                         uint32_t cursor,
672                         uint32_t anchor)
673 {
674         struct virtual_keyboard *keyboard = data;
675
676         free(keyboard->surrounding_text);
677         keyboard->surrounding_text = strdup(text);
678
679         keyboard->surrounding_cursor = cursor;
680 }
681
682 static void
683 handle_reset(void *data,
684              struct wl_input_method_context *context)
685 {
686         struct virtual_keyboard *keyboard = data;
687
688         fprintf(stderr, "Reset pre-edit buffer\n");
689
690         if (strlen(keyboard->preedit_string)) {
691                 free(keyboard->preedit_string);
692                 keyboard->preedit_string = strdup("");
693         }
694 }
695
696 static void
697 handle_content_type(void *data,
698                     struct wl_input_method_context *context,
699                     uint32_t hint,
700                     uint32_t purpose)
701 {
702         struct virtual_keyboard *keyboard = data;
703
704         keyboard->content_hint = hint;
705         keyboard->content_purpose = purpose;
706 }
707
708 static void
709 handle_invoke_action(void *data,
710                      struct wl_input_method_context *context,
711                      uint32_t button,
712                      uint32_t index)
713 {
714         struct virtual_keyboard *keyboard = data;
715
716         if (button != BTN_LEFT)
717                 return;
718
719         virtual_keyboard_send_preedit(keyboard, index);
720 }
721
722 static void
723 handle_commit_state(void *data,
724                     struct wl_input_method_context *context,
725                     uint32_t serial)
726 {
727         struct virtual_keyboard *keyboard = data;
728         const struct layout *layout;
729
730         keyboard->serial = serial;
731
732         layout = get_current_layout(keyboard);
733
734         if (keyboard->surrounding_text)
735                 fprintf(stderr, "Surrounding text updated: %s\n", keyboard->surrounding_text);
736
737         window_schedule_resize(keyboard->keyboard->window,
738                                layout->columns * key_width,
739                                layout->rows * key_height);
740
741         wl_input_method_context_language(context, keyboard->serial, layout->language);
742         wl_input_method_context_text_direction(context, keyboard->serial, layout->text_direction);
743
744         widget_schedule_redraw(keyboard->keyboard->widget);
745 }
746
747 static void
748 handle_preferred_language(void *data,
749                           struct wl_input_method_context *context,
750                           const char *language)
751 {
752         struct virtual_keyboard *keyboard = data;
753
754         if (keyboard->preferred_language)
755                 free(keyboard->preferred_language);
756
757         keyboard->preferred_language = NULL;
758
759         if (language)
760                 keyboard->preferred_language = strdup(language);
761 }
762
763 static const struct wl_input_method_context_listener input_method_context_listener = {
764         handle_surrounding_text,
765         handle_reset,
766         handle_content_type,
767         handle_invoke_action,
768         handle_commit_state,
769         handle_preferred_language
770 };
771
772 static void
773 input_method_activate(void *data,
774                       struct wl_input_method *input_method,
775                       struct wl_input_method_context *context)
776 {
777         struct virtual_keyboard *keyboard = data;
778         struct wl_array modifiers_map;
779         const struct layout *layout;
780
781         keyboard->keyboard->state = keyboardstate_default;
782
783         if (keyboard->context)
784                 wl_input_method_context_destroy(keyboard->context);
785
786         if (keyboard->preedit_string)
787                 free(keyboard->preedit_string);
788
789         keyboard->preedit_string = strdup("");
790         keyboard->content_hint = 0;
791         keyboard->content_purpose = 0;
792         free(keyboard->preferred_language);
793         keyboard->preferred_language = NULL;
794         free(keyboard->surrounding_text);
795         keyboard->surrounding_text = NULL;
796
797         keyboard->serial = 0;
798
799         keyboard->context = context;
800         wl_input_method_context_add_listener(context,
801                                              &input_method_context_listener,
802                                              keyboard);
803
804         wl_array_init(&modifiers_map);
805         keysym_modifiers_add(&modifiers_map, "Shift");
806         keysym_modifiers_add(&modifiers_map, "Control");
807         keysym_modifiers_add(&modifiers_map, "Mod1");
808         wl_input_method_context_modifiers_map(context, &modifiers_map);
809         keyboard->keysym.shift_mask = keysym_modifiers_get_mask(&modifiers_map, "Shift");
810         wl_array_release(&modifiers_map);
811
812         layout = get_current_layout(keyboard);
813
814         window_schedule_resize(keyboard->keyboard->window,
815                                layout->columns * key_width,
816                                layout->rows * key_height);
817
818         wl_input_method_context_language(context, keyboard->serial, layout->language);
819         wl_input_method_context_text_direction(context, keyboard->serial, layout->text_direction);
820
821         widget_schedule_redraw(keyboard->keyboard->widget);
822 }
823
824 static void
825 input_method_deactivate(void *data,
826                         struct wl_input_method *input_method,
827                         struct wl_input_method_context *context)
828 {
829         struct virtual_keyboard *keyboard = data;
830
831         if (!keyboard->context)
832                 return;
833
834         wl_input_method_context_destroy(keyboard->context);
835         keyboard->context = NULL;
836 }
837
838 static const struct wl_input_method_listener input_method_listener = {
839         input_method_activate,
840         input_method_deactivate
841 };
842
843 static void
844 global_handler(struct display *display, uint32_t name,
845                const char *interface, uint32_t version, void *data)
846 {
847         struct virtual_keyboard *keyboard = data;
848
849         if (!strcmp(interface, "wl_input_panel")) {
850                 keyboard->input_panel =
851                         display_bind(display, name, &wl_input_panel_interface, 1);
852         } else if (!strcmp(interface, "wl_input_method")) {
853                 keyboard->input_method =
854                         display_bind(display, name,
855                                      &wl_input_method_interface, 1);
856                 wl_input_method_add_listener(keyboard->input_method, &input_method_listener, keyboard);
857         }
858 }
859
860 static void
861 keyboard_create(struct output *output, struct virtual_keyboard *virtual_keyboard)
862 {
863         struct keyboard *keyboard;
864         const struct layout *layout;
865         struct wl_input_panel_surface *ips;
866
867         layout = get_current_layout(virtual_keyboard);
868
869         keyboard = xzalloc(sizeof *keyboard);
870         keyboard->keyboard = virtual_keyboard;
871         keyboard->window = window_create_custom(virtual_keyboard->display);
872         keyboard->widget = window_add_widget(keyboard->window, keyboard);
873
874         virtual_keyboard->keyboard = keyboard;
875
876         window_set_title(keyboard->window, "Virtual keyboard");
877         window_set_user_data(keyboard->window, keyboard);
878
879         widget_set_redraw_handler(keyboard->widget, redraw_handler);
880         widget_set_resize_handler(keyboard->widget, resize_handler);
881         widget_set_button_handler(keyboard->widget, button_handler);
882         widget_set_touch_down_handler(keyboard->widget, touch_down_handler);
883         widget_set_touch_up_handler(keyboard->widget, touch_up_handler);
884
885
886         window_schedule_resize(keyboard->window,
887                                layout->columns * key_width,
888                                layout->rows * key_height);
889
890
891         ips = wl_input_panel_get_input_panel_surface(virtual_keyboard->input_panel,
892                                                      window_get_wl_surface(keyboard->window));
893
894         wl_input_panel_surface_set_toplevel(ips,
895                                             output_get_wl_output(output),
896                                             WL_INPUT_PANEL_SURFACE_POSITION_CENTER_BOTTOM);
897 }
898
899 int
900 main(int argc, char *argv[])
901 {
902         struct virtual_keyboard virtual_keyboard;
903         struct output *output;
904
905         memset(&virtual_keyboard, 0, sizeof virtual_keyboard);
906
907         virtual_keyboard.display = display_create(&argc, argv);
908         if (virtual_keyboard.display == NULL) {
909                 fprintf(stderr, "failed to create display: %m\n");
910                 return -1;
911         }
912
913         display_set_user_data(virtual_keyboard.display, &virtual_keyboard);
914         display_set_global_handler(virtual_keyboard.display, global_handler);
915
916         output = display_get_output(virtual_keyboard.display);
917         keyboard_create(output, &virtual_keyboard);
918
919         display_run(virtual_keyboard.display);
920
921         return 0;
922 }