client: Fix shell unstable version check
[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 void __attribute__ ((format (printf, 1, 2)))
265 dbg(const char *fmt, ...)
266 {
267 #ifdef DEBUG
268         int l;
269         va_list argp;
270
271         va_start(argp, fmt);
272         l = vfprintf(stderr, fmt, argp);
273         va_end(argp);
274 #endif
275 }
276
277 static const char *
278 label_from_key(struct keyboard *keyboard,
279                const struct key *key)
280 {
281         if (key->key_type == keytype_style)
282                 return style_labels[keyboard->keyboard->preedit_style];
283
284         if (keyboard->state == keyboardstate_default)
285                 return key->label;
286         else
287                 return key->alt;
288 }
289
290 static void
291 draw_key(struct keyboard *keyboard,
292          const struct key *key,
293          cairo_t *cr,
294          unsigned int row,
295          unsigned int col)
296 {
297         const char *label;
298         cairo_text_extents_t extents;
299
300         cairo_save(cr);
301         cairo_rectangle(cr,
302                         col * key_width, row * key_height,
303                         key->width * key_width, key_height);
304         cairo_clip(cr);
305
306         /* Paint frame */
307         cairo_rectangle(cr,
308                         col * key_width, row * key_height,
309                         key->width * key_width, key_height);
310         cairo_set_line_width(cr, 3);
311         cairo_stroke(cr);
312
313         /* Paint text */
314         label = label_from_key(keyboard, key);
315         cairo_text_extents(cr, label, &extents);
316
317         cairo_translate(cr,
318                         col * key_width,
319                         row * key_height);
320         cairo_translate(cr,
321                         (key->width * key_width - extents.width) / 2,
322                         (key_height - extents.y_bearing) / 2);
323         cairo_show_text(cr, label);
324
325         cairo_restore(cr);
326 }
327
328 static const struct layout *
329 get_current_layout(struct virtual_keyboard *keyboard)
330 {
331         switch (keyboard->content_purpose) {
332                 case WL_TEXT_INPUT_CONTENT_PURPOSE_DIGITS:
333                 case WL_TEXT_INPUT_CONTENT_PURPOSE_NUMBER:
334                         return &numeric_layout;
335                 default:
336                         if (keyboard->preferred_language &&
337                             strcmp(keyboard->preferred_language, "ar") == 0)
338                                 return &arabic_layout;
339                         else
340                                 return &normal_layout;
341         }
342 }
343
344 static void
345 redraw_handler(struct widget *widget, void *data)
346 {
347         struct keyboard *keyboard = data;
348         cairo_surface_t *surface;
349         struct rectangle allocation;
350         cairo_t *cr;
351         unsigned int i;
352         unsigned int row = 0, col = 0;
353         const struct layout *layout;
354
355         layout = get_current_layout(keyboard->keyboard);
356
357         surface = window_get_surface(keyboard->window);
358         widget_get_allocation(keyboard->widget, &allocation);
359
360         cr = cairo_create(surface);
361         cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
362         cairo_clip(cr);
363
364         cairo_select_font_face(cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
365         cairo_set_font_size(cr, 16);
366
367         cairo_translate(cr, allocation.x, allocation.y);
368
369         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
370         cairo_set_source_rgba(cr, 1, 1, 1, 0.75);
371         cairo_rectangle(cr, 0, 0, layout->columns * key_width, layout->rows * key_height);
372         cairo_paint(cr);
373
374         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
375
376         for (i = 0; i < layout->count; ++i) {
377                 cairo_set_source_rgb(cr, 0, 0, 0);
378                 draw_key(keyboard, &layout->keys[i], cr, row, col);
379                 col += layout->keys[i].width;
380                 if (col >= layout->columns) {
381                         row += 1;
382                         col = 0;
383                 }
384         }
385
386         cairo_destroy(cr);
387         cairo_surface_destroy(surface);
388 }
389
390 static void
391 resize_handler(struct widget *widget,
392                int32_t width, int32_t height, void *data)
393 {
394         /* struct keyboard *keyboard = data; */
395 }
396
397 static char *
398 insert_text(const char *text, uint32_t offset, const char *insert)
399 {
400         int tlen = strlen(text), ilen = strlen(insert);
401         char *new_text = xmalloc(tlen + ilen + 1);
402
403         memcpy(new_text, text, offset);
404         memcpy(new_text + offset, insert, ilen);
405         memcpy(new_text + offset + ilen, text + offset, tlen - offset);
406         new_text[tlen + ilen] = '\0';
407
408         return new_text;
409 }
410
411 static void
412 virtual_keyboard_commit_preedit(struct virtual_keyboard *keyboard)
413 {
414         char *surrounding_text;
415
416         if (!keyboard->preedit_string ||
417             strlen(keyboard->preedit_string) == 0)
418                 return;
419
420         wl_input_method_context_cursor_position(keyboard->context,
421                                                 0, 0);
422         wl_input_method_context_commit_string(keyboard->context,
423                                               keyboard->serial,
424                                               keyboard->preedit_string);
425
426         if (keyboard->surrounding_text) {
427                 surrounding_text = insert_text(keyboard->surrounding_text,
428                                                keyboard->surrounding_cursor,
429                                                keyboard->preedit_string);
430                 free(keyboard->surrounding_text);
431                 keyboard->surrounding_text = surrounding_text;
432                 keyboard->surrounding_cursor += strlen(keyboard->preedit_string);
433         } else {
434                 keyboard->surrounding_text = strdup(keyboard->preedit_string);
435                 keyboard->surrounding_cursor = strlen(keyboard->preedit_string);
436         }
437
438         free(keyboard->preedit_string);
439         keyboard->preedit_string = strdup("");
440 }
441
442 static void
443 virtual_keyboard_send_preedit(struct virtual_keyboard *keyboard,
444                               int32_t cursor)
445 {
446         uint32_t index = strlen(keyboard->preedit_string);
447
448         if (keyboard->preedit_style)
449                 wl_input_method_context_preedit_styling(keyboard->context,
450                                                         0,
451                                                         strlen(keyboard->preedit_string),
452                                                         keyboard->preedit_style);
453         if (cursor > 0)
454                 index = cursor;
455         wl_input_method_context_preedit_cursor(keyboard->context,
456                                                index);
457         wl_input_method_context_preedit_string(keyboard->context,
458                                                keyboard->serial,
459                                                keyboard->preedit_string,
460                                                keyboard->preedit_string);
461 }
462
463 static const char *
464 prev_utf8_char(const char *s, const char *p)
465 {
466         for (--p; p >= s; --p) {
467                 if ((*p & 0xc0) != 0x80)
468                         return p;
469         }
470         return NULL;
471 }
472
473 static void
474 delete_before_cursor(struct virtual_keyboard *keyboard)
475 {
476         const char *start, *end;
477
478         if (!keyboard->surrounding_text) {
479                 dbg("delete_before_cursor: No surrounding text available\n");
480                 return;
481         }
482
483         start = prev_utf8_char(keyboard->surrounding_text,
484                                keyboard->surrounding_text + keyboard->surrounding_cursor);
485         if (!start) {
486                 dbg("delete_before_cursor: No previous character to delete\n");
487                 return;
488         }
489
490         end = keyboard->surrounding_text + keyboard->surrounding_cursor;
491
492         wl_input_method_context_delete_surrounding_text(keyboard->context,
493                                                         (start - keyboard->surrounding_text) - keyboard->surrounding_cursor,
494                                                         end - start);
495         wl_input_method_context_commit_string(keyboard->context,
496                                               keyboard->serial,
497                                               "");
498
499         /* Update surrounding text */
500         keyboard->surrounding_cursor = start - keyboard->surrounding_text;
501         keyboard->surrounding_text[keyboard->surrounding_cursor] = '\0';
502         if (*end)
503                 memmove(keyboard->surrounding_text + keyboard->surrounding_cursor, end, strlen(end));
504 }
505
506 static char *
507 append(char *s1, const char *s2)
508 {
509         int len1, len2;
510         char *s;
511
512         len1 = strlen(s1);
513         len2 = strlen(s2);
514         s = xrealloc(s1, len1 + len2 + 1);
515         memcpy(s + len1, s2, len2);
516         s[len1 + len2] = '\0';
517
518         return s;
519 }
520
521 static void
522 keyboard_handle_key(struct keyboard *keyboard, uint32_t time, const struct key *key, struct input *input, enum wl_pointer_button_state state)
523 {
524         const char *label = keyboard->state == keyboardstate_default ? key->label : key->alt;
525         xkb_mod_mask_t mod_mask = keyboard->state == keyboardstate_default ? 0 : keyboard->keyboard->keysym.shift_mask;
526         uint32_t key_state = (state == WL_POINTER_BUTTON_STATE_PRESSED) ? WL_KEYBOARD_KEY_STATE_PRESSED : WL_KEYBOARD_KEY_STATE_RELEASED;
527
528         switch (key->key_type) {
529                 case keytype_default:
530                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
531                                 break;
532
533                         keyboard->keyboard->preedit_string =
534                                 append(keyboard->keyboard->preedit_string,
535                                        label);
536                         virtual_keyboard_send_preedit(keyboard->keyboard, -1);
537                         break;
538                 case keytype_backspace:
539                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
540                                 break;
541
542                         if (strlen(keyboard->keyboard->preedit_string) == 0) {
543                                 delete_before_cursor(keyboard->keyboard);
544                         } else {
545                                 keyboard->keyboard->preedit_string[strlen(keyboard->keyboard->preedit_string) - 1] = '\0';
546                                 virtual_keyboard_send_preedit(keyboard->keyboard, -1);
547                         }
548                         break;
549                 case keytype_enter:
550                         virtual_keyboard_commit_preedit(keyboard->keyboard);
551                         wl_input_method_context_keysym(keyboard->keyboard->context,
552                                                        display_get_serial(keyboard->keyboard->display),
553                                                        time, 
554                                                        XKB_KEY_Return, key_state, mod_mask);
555                         break;
556                 case keytype_space:
557                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
558                                 break;
559                         keyboard->keyboard->preedit_string =
560                                 append(keyboard->keyboard->preedit_string, " ");
561                         virtual_keyboard_commit_preedit(keyboard->keyboard);
562                         break;
563                 case keytype_switch:
564                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
565                                 break;
566                         if (keyboard->state == keyboardstate_default)
567                                 keyboard->state = keyboardstate_uppercase;
568                         else
569                                 keyboard->state = keyboardstate_default;
570                         break;
571                 case keytype_symbols:
572                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
573                                 break;
574                         break;
575                 case keytype_tab:
576                         virtual_keyboard_commit_preedit(keyboard->keyboard);
577                         wl_input_method_context_keysym(keyboard->keyboard->context,
578                                                        display_get_serial(keyboard->keyboard->display),
579                                                        time, 
580                                                        XKB_KEY_Tab, key_state, mod_mask);
581                         break;
582                 case keytype_arrow_up:
583                         virtual_keyboard_commit_preedit(keyboard->keyboard);
584                         wl_input_method_context_keysym(keyboard->keyboard->context,
585                                                        display_get_serial(keyboard->keyboard->display),
586                                                        time, 
587                                                        XKB_KEY_Up, key_state, mod_mask);
588                         break;
589                 case keytype_arrow_left:
590                         virtual_keyboard_commit_preedit(keyboard->keyboard);
591                         wl_input_method_context_keysym(keyboard->keyboard->context,
592                                                        display_get_serial(keyboard->keyboard->display),
593                                                        time, 
594                                                        XKB_KEY_Left, key_state, mod_mask);
595                         break;
596                 case keytype_arrow_right:
597                         virtual_keyboard_commit_preedit(keyboard->keyboard);
598                         wl_input_method_context_keysym(keyboard->keyboard->context,
599                                                        display_get_serial(keyboard->keyboard->display),
600                                                        time, 
601                                                        XKB_KEY_Right, key_state, mod_mask);
602                         break;
603                 case keytype_arrow_down:
604                         virtual_keyboard_commit_preedit(keyboard->keyboard);
605                         wl_input_method_context_keysym(keyboard->keyboard->context,
606                                                        display_get_serial(keyboard->keyboard->display),
607                                                        time, 
608                                                        XKB_KEY_Down, key_state, mod_mask);
609                         break;
610                 case keytype_style:
611                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
612                                 break;
613                         keyboard->keyboard->preedit_style = (keyboard->keyboard->preedit_style + 1) % 8; /* TODO */
614                         virtual_keyboard_send_preedit(keyboard->keyboard, -1);
615                         break;
616         }
617 }
618
619 static void
620 button_handler(struct widget *widget,
621                struct input *input, uint32_t time,
622                uint32_t button,
623                enum wl_pointer_button_state state, void *data)
624 {
625         struct keyboard *keyboard = data;
626         struct rectangle allocation;
627         int32_t x, y;
628         int row, col;
629         unsigned int i;
630         const struct layout *layout;
631
632         layout = get_current_layout(keyboard->keyboard);
633
634         if (button != BTN_LEFT) {
635                 return;
636         }
637
638         input_get_position(input, &x, &y);
639
640         widget_get_allocation(keyboard->widget, &allocation);
641         x -= allocation.x;
642         y -= allocation.y;
643
644         row = y / key_height;
645         col = x / key_width + row * layout->columns;
646         for (i = 0; i < layout->count; ++i) {
647                 col -= layout->keys[i].width;
648                 if (col < 0) {
649                         keyboard_handle_key(keyboard, time, &layout->keys[i], input, state);
650                         break;
651                 }
652         }
653
654         widget_schedule_redraw(widget);
655 }
656
657 static void
658 touch_handler(struct input *input, uint32_t time,
659               float x, float y, uint32_t state, void *data)
660 {
661         struct keyboard *keyboard = data;
662         struct rectangle allocation;
663         int row, col;
664         unsigned int i;
665         const struct layout *layout;
666
667         layout = get_current_layout(keyboard->keyboard);
668
669         widget_get_allocation(keyboard->widget, &allocation);
670
671         x -= allocation.x;
672         y -= allocation.y;
673
674         row = (int)y / key_height;
675         col = (int)x / key_width + row * layout->columns;
676         for (i = 0; i < layout->count; ++i) {
677                 col -= layout->keys[i].width;
678                 if (col < 0) {
679                         keyboard_handle_key(keyboard, time,
680                                             &layout->keys[i], input, state);
681                         break;
682                 }
683         }
684
685         widget_schedule_redraw(keyboard->widget);
686 }
687
688 static void
689 touch_down_handler(struct widget *widget, struct input *input,
690                    uint32_t serial, uint32_t time, int32_t id,
691                    float x, float y, void *data)
692 {
693   touch_handler(input, time, x, y, 
694                 WL_POINTER_BUTTON_STATE_PRESSED, data);
695 }
696
697 static void
698 touch_up_handler(struct widget *widget, struct input *input,
699                  uint32_t serial, uint32_t time, int32_t id,
700                  void *data)
701 {
702   float x, y;
703
704   input_get_touch(input, id, &x, &y);
705
706   touch_handler(input, time, x, y,
707                 WL_POINTER_BUTTON_STATE_RELEASED, data);
708 }
709
710 static void
711 handle_surrounding_text(void *data,
712                         struct wl_input_method_context *context,
713                         const char *text,
714                         uint32_t cursor,
715                         uint32_t anchor)
716 {
717         struct virtual_keyboard *keyboard = data;
718
719         free(keyboard->surrounding_text);
720         keyboard->surrounding_text = strdup(text);
721
722         keyboard->surrounding_cursor = cursor;
723 }
724
725 static void
726 handle_reset(void *data,
727              struct wl_input_method_context *context)
728 {
729         struct virtual_keyboard *keyboard = data;
730
731         dbg("Reset pre-edit buffer\n");
732
733         if (strlen(keyboard->preedit_string)) {
734                 free(keyboard->preedit_string);
735                 keyboard->preedit_string = strdup("");
736         }
737 }
738
739 static void
740 handle_content_type(void *data,
741                     struct wl_input_method_context *context,
742                     uint32_t hint,
743                     uint32_t purpose)
744 {
745         struct virtual_keyboard *keyboard = data;
746
747         keyboard->content_hint = hint;
748         keyboard->content_purpose = purpose;
749 }
750
751 static void
752 handle_invoke_action(void *data,
753                      struct wl_input_method_context *context,
754                      uint32_t button,
755                      uint32_t index)
756 {
757         struct virtual_keyboard *keyboard = data;
758
759         if (button != BTN_LEFT)
760                 return;
761
762         virtual_keyboard_send_preedit(keyboard, index);
763 }
764
765 static void
766 handle_commit_state(void *data,
767                     struct wl_input_method_context *context,
768                     uint32_t serial)
769 {
770         struct virtual_keyboard *keyboard = data;
771         const struct layout *layout;
772
773         keyboard->serial = serial;
774
775         layout = get_current_layout(keyboard);
776
777         if (keyboard->surrounding_text)
778                 dbg("Surrounding text updated: %s\n", keyboard->surrounding_text);
779
780         window_schedule_resize(keyboard->keyboard->window,
781                                layout->columns * key_width,
782                                layout->rows * key_height);
783
784         wl_input_method_context_language(context, keyboard->serial, layout->language);
785         wl_input_method_context_text_direction(context, keyboard->serial, layout->text_direction);
786
787         widget_schedule_redraw(keyboard->keyboard->widget);
788 }
789
790 static void
791 handle_preferred_language(void *data,
792                           struct wl_input_method_context *context,
793                           const char *language)
794 {
795         struct virtual_keyboard *keyboard = data;
796
797         if (keyboard->preferred_language)
798                 free(keyboard->preferred_language);
799
800         keyboard->preferred_language = NULL;
801
802         if (language)
803                 keyboard->preferred_language = strdup(language);
804 }
805
806 static const struct wl_input_method_context_listener input_method_context_listener = {
807         handle_surrounding_text,
808         handle_reset,
809         handle_content_type,
810         handle_invoke_action,
811         handle_commit_state,
812         handle_preferred_language
813 };
814
815 static void
816 input_method_activate(void *data,
817                       struct wl_input_method *input_method,
818                       struct wl_input_method_context *context)
819 {
820         struct virtual_keyboard *keyboard = data;
821         struct wl_array modifiers_map;
822         const struct layout *layout;
823
824         keyboard->keyboard->state = keyboardstate_default;
825
826         if (keyboard->context)
827                 wl_input_method_context_destroy(keyboard->context);
828
829         if (keyboard->preedit_string)
830                 free(keyboard->preedit_string);
831
832         keyboard->preedit_string = strdup("");
833         keyboard->content_hint = 0;
834         keyboard->content_purpose = 0;
835         free(keyboard->preferred_language);
836         keyboard->preferred_language = NULL;
837         free(keyboard->surrounding_text);
838         keyboard->surrounding_text = NULL;
839
840         keyboard->serial = 0;
841
842         keyboard->context = context;
843         wl_input_method_context_add_listener(context,
844                                              &input_method_context_listener,
845                                              keyboard);
846
847         wl_array_init(&modifiers_map);
848         keysym_modifiers_add(&modifiers_map, "Shift");
849         keysym_modifiers_add(&modifiers_map, "Control");
850         keysym_modifiers_add(&modifiers_map, "Mod1");
851         wl_input_method_context_modifiers_map(context, &modifiers_map);
852         keyboard->keysym.shift_mask = keysym_modifiers_get_mask(&modifiers_map, "Shift");
853         wl_array_release(&modifiers_map);
854
855         layout = get_current_layout(keyboard);
856
857         window_schedule_resize(keyboard->keyboard->window,
858                                layout->columns * key_width,
859                                layout->rows * key_height);
860
861         wl_input_method_context_language(context, keyboard->serial, layout->language);
862         wl_input_method_context_text_direction(context, keyboard->serial, layout->text_direction);
863
864         widget_schedule_redraw(keyboard->keyboard->widget);
865 }
866
867 static void
868 input_method_deactivate(void *data,
869                         struct wl_input_method *input_method,
870                         struct wl_input_method_context *context)
871 {
872         struct virtual_keyboard *keyboard = data;
873
874         if (!keyboard->context)
875                 return;
876
877         wl_input_method_context_destroy(keyboard->context);
878         keyboard->context = NULL;
879 }
880
881 static const struct wl_input_method_listener input_method_listener = {
882         input_method_activate,
883         input_method_deactivate
884 };
885
886 static void
887 global_handler(struct display *display, uint32_t name,
888                const char *interface, uint32_t version, void *data)
889 {
890         struct virtual_keyboard *keyboard = data;
891
892         if (!strcmp(interface, "wl_input_panel")) {
893                 keyboard->input_panel =
894                         display_bind(display, name, &wl_input_panel_interface, 1);
895         } else if (!strcmp(interface, "wl_input_method")) {
896                 keyboard->input_method =
897                         display_bind(display, name,
898                                      &wl_input_method_interface, 1);
899                 wl_input_method_add_listener(keyboard->input_method, &input_method_listener, keyboard);
900         }
901 }
902
903 static void
904 keyboard_create(struct output *output, struct virtual_keyboard *virtual_keyboard)
905 {
906         struct keyboard *keyboard;
907         const struct layout *layout;
908         struct wl_input_panel_surface *ips;
909
910         layout = get_current_layout(virtual_keyboard);
911
912         keyboard = xzalloc(sizeof *keyboard);
913         keyboard->keyboard = virtual_keyboard;
914         keyboard->window = window_create_custom(virtual_keyboard->display);
915         keyboard->widget = window_add_widget(keyboard->window, keyboard);
916
917         virtual_keyboard->keyboard = keyboard;
918
919         window_set_title(keyboard->window, "Virtual keyboard");
920         window_set_user_data(keyboard->window, keyboard);
921
922         widget_set_redraw_handler(keyboard->widget, redraw_handler);
923         widget_set_resize_handler(keyboard->widget, resize_handler);
924         widget_set_button_handler(keyboard->widget, button_handler);
925         widget_set_touch_down_handler(keyboard->widget, touch_down_handler);
926         widget_set_touch_up_handler(keyboard->widget, touch_up_handler);
927
928         window_schedule_resize(keyboard->window,
929                                layout->columns * key_width,
930                                layout->rows * key_height);
931
932
933         ips = wl_input_panel_get_input_panel_surface(virtual_keyboard->input_panel,
934                                                      window_get_wl_surface(keyboard->window));
935
936         wl_input_panel_surface_set_toplevel(ips,
937                                             output_get_wl_output(output),
938                                             WL_INPUT_PANEL_SURFACE_POSITION_CENTER_BOTTOM);
939 }
940
941 int
942 main(int argc, char *argv[])
943 {
944         struct virtual_keyboard virtual_keyboard;
945         struct output *output;
946
947         memset(&virtual_keyboard, 0, sizeof virtual_keyboard);
948
949         virtual_keyboard.display = display_create(&argc, argv);
950         if (virtual_keyboard.display == NULL) {
951                 fprintf(stderr, "failed to create display: %m\n");
952                 return -1;
953         }
954
955         display_set_user_data(virtual_keyboard.display, &virtual_keyboard);
956         display_set_global_handler(virtual_keyboard.display, global_handler);
957
958         output = display_get_output(virtual_keyboard.display);
959         keyboard_create(output, &virtual_keyboard);
960
961         display_run(virtual_keyboard.display);
962
963         return 0;
964 }