keyboard: Fix insert_text() string utility
[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         int tlen = strlen(text), ilen = strlen(insert);
388         char *new_text = xmalloc(tlen + ilen + 1);
389
390         memcpy(new_text, text, offset);
391         memcpy(new_text + offset, insert, ilen);
392         memcpy(new_text + offset + ilen, text + offset, tlen - offset);
393         new_text[tlen + ilen] = '\0';
394
395         return new_text;
396 }
397
398 static void
399 virtual_keyboard_commit_preedit(struct virtual_keyboard *keyboard)
400 {
401         char *surrounding_text;
402
403         if (!keyboard->preedit_string ||
404             strlen(keyboard->preedit_string) == 0)
405                 return;
406
407         wl_input_method_context_cursor_position(keyboard->context,
408                                                 0, 0);
409         wl_input_method_context_commit_string(keyboard->context,
410                                               keyboard->serial,
411                                               keyboard->preedit_string);
412
413         if (keyboard->surrounding_text) {
414                 surrounding_text = insert_text(keyboard->surrounding_text,
415                                                keyboard->surrounding_cursor,
416                                                keyboard->preedit_string);
417                 free(keyboard->surrounding_text);
418                 keyboard->surrounding_text = surrounding_text;
419                 keyboard->surrounding_cursor += strlen(keyboard->preedit_string);
420         } else {
421                 keyboard->surrounding_text = strdup(keyboard->preedit_string);
422                 keyboard->surrounding_cursor = strlen(keyboard->preedit_string);
423         }
424
425         free(keyboard->preedit_string);
426         keyboard->preedit_string = strdup("");
427 }
428
429 static void
430 virtual_keyboard_send_preedit(struct virtual_keyboard *keyboard,
431                               int32_t cursor)
432 {
433         uint32_t index = strlen(keyboard->preedit_string);
434
435         if (keyboard->preedit_style)
436                 wl_input_method_context_preedit_styling(keyboard->context,
437                                                         0,
438                                                         strlen(keyboard->preedit_string),
439                                                         keyboard->preedit_style);
440         if (cursor > 0)
441                 index = cursor;
442         wl_input_method_context_preedit_cursor(keyboard->context,
443                                                index);
444         wl_input_method_context_preedit_string(keyboard->context,
445                                                keyboard->serial,
446                                                keyboard->preedit_string,
447                                                keyboard->preedit_string);
448 }
449
450 static const char *
451 prev_utf8_char(const char *s, const char *p)
452 {
453         for (--p; p >= s; --p) {
454                 if ((*p & 0xc0) != 0x80)
455                         return p;
456         }
457         return NULL;
458 }
459
460 static void
461 delete_before_cursor(struct virtual_keyboard *keyboard)
462 {
463         const char *start, *end;
464
465         if (!keyboard->surrounding_text) {
466                 fprintf(stderr, "delete_before_cursor: No surrounding text available\n");
467                 return;
468         }
469
470         start = prev_utf8_char(keyboard->surrounding_text,
471                                keyboard->surrounding_text + keyboard->surrounding_cursor);
472         if (!start) {
473                 fprintf(stderr, "delete_before_cursor: No previous character to delete\n");
474                 return;
475         }
476
477         end = keyboard->surrounding_text + keyboard->surrounding_cursor;
478
479         wl_input_method_context_delete_surrounding_text(keyboard->context,
480                                                         (start - keyboard->surrounding_text) - keyboard->surrounding_cursor,
481                                                         end - start);
482         wl_input_method_context_commit_string(keyboard->context,
483                                               keyboard->serial,
484                                               "");
485
486         /* Update surrounding text */
487         keyboard->surrounding_cursor = start - keyboard->surrounding_text;
488         keyboard->surrounding_text[keyboard->surrounding_cursor] = '\0';
489         if (*end)
490                 memmove(keyboard->surrounding_text + keyboard->surrounding_cursor, end, strlen(end));
491 }
492
493 static void
494 keyboard_handle_key(struct keyboard *keyboard, uint32_t time, const struct key *key, struct input *input, enum wl_pointer_button_state state)
495 {
496         const char *label = keyboard->state == keyboardstate_default ? key->label : key->alt;
497         xkb_mod_mask_t mod_mask = keyboard->state == keyboardstate_default ? 0 : keyboard->keyboard->keysym.shift_mask;
498         uint32_t key_state = (state == WL_POINTER_BUTTON_STATE_PRESSED) ? WL_KEYBOARD_KEY_STATE_PRESSED : WL_KEYBOARD_KEY_STATE_RELEASED;
499
500         switch (key->key_type) {
501                 case keytype_default:
502                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
503                                 break;
504
505                         keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
506                                                                     label);
507                         virtual_keyboard_send_preedit(keyboard->keyboard, -1);
508                         break;
509                 case keytype_backspace:
510                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
511                                 break;
512
513                         if (strlen(keyboard->keyboard->preedit_string) == 0) {
514                                 delete_before_cursor(keyboard->keyboard);
515                         } else {
516                                 keyboard->keyboard->preedit_string[strlen(keyboard->keyboard->preedit_string) - 1] = '\0';
517                                 virtual_keyboard_send_preedit(keyboard->keyboard, -1);
518                         }
519                         break;
520                 case keytype_enter:
521                         virtual_keyboard_commit_preedit(keyboard->keyboard);
522                         wl_input_method_context_keysym(keyboard->keyboard->context,
523                                                        display_get_serial(keyboard->keyboard->display),
524                                                        time, 
525                                                        XKB_KEY_Return, key_state, mod_mask);
526                         break;
527                 case keytype_space:
528                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
529                                 break;
530                         keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
531                                                                     " ");
532                         virtual_keyboard_commit_preedit(keyboard->keyboard);
533                         break;
534                 case keytype_switch:
535                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
536                                 break;
537                         if (keyboard->state == keyboardstate_default)
538                                 keyboard->state = keyboardstate_uppercase;
539                         else
540                                 keyboard->state = keyboardstate_default;
541                         break;
542                 case keytype_symbols:
543                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
544                                 break;
545                         break;
546                 case keytype_tab:
547                         virtual_keyboard_commit_preedit(keyboard->keyboard);
548                         wl_input_method_context_keysym(keyboard->keyboard->context,
549                                                        display_get_serial(keyboard->keyboard->display),
550                                                        time, 
551                                                        XKB_KEY_Tab, key_state, mod_mask);
552                         break;
553                 case keytype_arrow_up:
554                         virtual_keyboard_commit_preedit(keyboard->keyboard);
555                         wl_input_method_context_keysym(keyboard->keyboard->context,
556                                                        display_get_serial(keyboard->keyboard->display),
557                                                        time, 
558                                                        XKB_KEY_Up, key_state, mod_mask);
559                         break;
560                 case keytype_arrow_left:
561                         virtual_keyboard_commit_preedit(keyboard->keyboard);
562                         wl_input_method_context_keysym(keyboard->keyboard->context,
563                                                        display_get_serial(keyboard->keyboard->display),
564                                                        time, 
565                                                        XKB_KEY_Left, key_state, mod_mask);
566                         break;
567                 case keytype_arrow_right:
568                         virtual_keyboard_commit_preedit(keyboard->keyboard);
569                         wl_input_method_context_keysym(keyboard->keyboard->context,
570                                                        display_get_serial(keyboard->keyboard->display),
571                                                        time, 
572                                                        XKB_KEY_Right, key_state, mod_mask);
573                         break;
574                 case keytype_arrow_down:
575                         virtual_keyboard_commit_preedit(keyboard->keyboard);
576                         wl_input_method_context_keysym(keyboard->keyboard->context,
577                                                        display_get_serial(keyboard->keyboard->display),
578                                                        time, 
579                                                        XKB_KEY_Down, key_state, mod_mask);
580                         break;
581                 case keytype_style:
582                         if (state != WL_POINTER_BUTTON_STATE_PRESSED)
583                                 break;
584                         keyboard->keyboard->preedit_style = (keyboard->keyboard->preedit_style + 1) % 8; /* TODO */
585                         virtual_keyboard_send_preedit(keyboard->keyboard, -1);
586                         break;
587         }
588 }
589
590 static void
591 button_handler(struct widget *widget,
592                struct input *input, uint32_t time,
593                uint32_t button,
594                enum wl_pointer_button_state state, void *data)
595 {
596         struct keyboard *keyboard = data;
597         struct rectangle allocation;
598         int32_t x, y;
599         int row, col;
600         unsigned int i;
601         const struct layout *layout;
602
603         layout = get_current_layout(keyboard->keyboard);
604
605         if (button != BTN_LEFT) {
606                 return;
607         }
608
609         input_get_position(input, &x, &y);
610
611         widget_get_allocation(keyboard->widget, &allocation);
612         x -= allocation.x;
613         y -= allocation.y;
614
615         row = y / key_height;
616         col = x / key_width + row * layout->columns;
617         for (i = 0; i < layout->count; ++i) {
618                 col -= layout->keys[i].width;
619                 if (col < 0) {
620                         keyboard_handle_key(keyboard, time, &layout->keys[i], input, state);
621                         break;
622                 }
623         }
624
625         widget_schedule_redraw(widget);
626 }
627
628 static void
629 touch_handler(struct input *input, uint32_t time,
630               float x, float y, uint32_t state, void *data)
631 {
632         struct keyboard *keyboard = data;
633         struct rectangle allocation;
634         int row, col;
635         unsigned int i;
636         const struct layout *layout;
637
638         layout = get_current_layout(keyboard->keyboard);
639
640         widget_get_allocation(keyboard->widget, &allocation);
641
642         x -= allocation.x;
643         y -= allocation.y;
644
645         row = (int)y / key_height;
646         col = (int)x / key_width + row * layout->columns;
647         for (i = 0; i < layout->count; ++i) {
648                 col -= layout->keys[i].width;
649                 if (col < 0) {
650                         keyboard_handle_key(keyboard, time,
651                                             &layout->keys[i], input, state);
652                         break;
653                 }
654         }
655
656         widget_schedule_redraw(keyboard->widget);
657 }
658
659 static void
660 touch_down_handler(struct widget *widget, struct input *input,
661                    uint32_t serial, uint32_t time, int32_t id,
662                    float x, float y, void *data)
663 {
664   touch_handler(input, time, x, y, 
665                 WL_POINTER_BUTTON_STATE_PRESSED, data);
666 }
667
668 static void
669 touch_up_handler(struct widget *widget, struct input *input,
670                  uint32_t serial, uint32_t time, int32_t id,
671                  void *data)
672 {
673   float x, y;
674
675   input_get_touch(input, id, &x, &y);
676
677   touch_handler(input, time, x, y,
678                 WL_POINTER_BUTTON_STATE_RELEASED, data);
679 }
680
681 static void
682 handle_surrounding_text(void *data,
683                         struct wl_input_method_context *context,
684                         const char *text,
685                         uint32_t cursor,
686                         uint32_t anchor)
687 {
688         struct virtual_keyboard *keyboard = data;
689
690         free(keyboard->surrounding_text);
691         keyboard->surrounding_text = strdup(text);
692
693         keyboard->surrounding_cursor = cursor;
694 }
695
696 static void
697 handle_reset(void *data,
698              struct wl_input_method_context *context)
699 {
700         struct virtual_keyboard *keyboard = data;
701
702         fprintf(stderr, "Reset pre-edit buffer\n");
703
704         if (strlen(keyboard->preedit_string)) {
705                 free(keyboard->preedit_string);
706                 keyboard->preedit_string = strdup("");
707         }
708 }
709
710 static void
711 handle_content_type(void *data,
712                     struct wl_input_method_context *context,
713                     uint32_t hint,
714                     uint32_t purpose)
715 {
716         struct virtual_keyboard *keyboard = data;
717
718         keyboard->content_hint = hint;
719         keyboard->content_purpose = purpose;
720 }
721
722 static void
723 handle_invoke_action(void *data,
724                      struct wl_input_method_context *context,
725                      uint32_t button,
726                      uint32_t index)
727 {
728         struct virtual_keyboard *keyboard = data;
729
730         if (button != BTN_LEFT)
731                 return;
732
733         virtual_keyboard_send_preedit(keyboard, index);
734 }
735
736 static void
737 handle_commit_state(void *data,
738                     struct wl_input_method_context *context,
739                     uint32_t serial)
740 {
741         struct virtual_keyboard *keyboard = data;
742         const struct layout *layout;
743
744         keyboard->serial = serial;
745
746         layout = get_current_layout(keyboard);
747
748         if (keyboard->surrounding_text)
749                 fprintf(stderr, "Surrounding text updated: %s\n", keyboard->surrounding_text);
750
751         window_schedule_resize(keyboard->keyboard->window,
752                                layout->columns * key_width,
753                                layout->rows * key_height);
754
755         wl_input_method_context_language(context, keyboard->serial, layout->language);
756         wl_input_method_context_text_direction(context, keyboard->serial, layout->text_direction);
757
758         widget_schedule_redraw(keyboard->keyboard->widget);
759 }
760
761 static void
762 handle_preferred_language(void *data,
763                           struct wl_input_method_context *context,
764                           const char *language)
765 {
766         struct virtual_keyboard *keyboard = data;
767
768         if (keyboard->preferred_language)
769                 free(keyboard->preferred_language);
770
771         keyboard->preferred_language = NULL;
772
773         if (language)
774                 keyboard->preferred_language = strdup(language);
775 }
776
777 static const struct wl_input_method_context_listener input_method_context_listener = {
778         handle_surrounding_text,
779         handle_reset,
780         handle_content_type,
781         handle_invoke_action,
782         handle_commit_state,
783         handle_preferred_language
784 };
785
786 static void
787 input_method_activate(void *data,
788                       struct wl_input_method *input_method,
789                       struct wl_input_method_context *context)
790 {
791         struct virtual_keyboard *keyboard = data;
792         struct wl_array modifiers_map;
793         const struct layout *layout;
794
795         keyboard->keyboard->state = keyboardstate_default;
796
797         if (keyboard->context)
798                 wl_input_method_context_destroy(keyboard->context);
799
800         if (keyboard->preedit_string)
801                 free(keyboard->preedit_string);
802
803         keyboard->preedit_string = strdup("");
804         keyboard->content_hint = 0;
805         keyboard->content_purpose = 0;
806         free(keyboard->preferred_language);
807         keyboard->preferred_language = NULL;
808         free(keyboard->surrounding_text);
809         keyboard->surrounding_text = NULL;
810
811         keyboard->serial = 0;
812
813         keyboard->context = context;
814         wl_input_method_context_add_listener(context,
815                                              &input_method_context_listener,
816                                              keyboard);
817
818         wl_array_init(&modifiers_map);
819         keysym_modifiers_add(&modifiers_map, "Shift");
820         keysym_modifiers_add(&modifiers_map, "Control");
821         keysym_modifiers_add(&modifiers_map, "Mod1");
822         wl_input_method_context_modifiers_map(context, &modifiers_map);
823         keyboard->keysym.shift_mask = keysym_modifiers_get_mask(&modifiers_map, "Shift");
824         wl_array_release(&modifiers_map);
825
826         layout = get_current_layout(keyboard);
827
828         window_schedule_resize(keyboard->keyboard->window,
829                                layout->columns * key_width,
830                                layout->rows * key_height);
831
832         wl_input_method_context_language(context, keyboard->serial, layout->language);
833         wl_input_method_context_text_direction(context, keyboard->serial, layout->text_direction);
834
835         widget_schedule_redraw(keyboard->keyboard->widget);
836 }
837
838 static void
839 input_method_deactivate(void *data,
840                         struct wl_input_method *input_method,
841                         struct wl_input_method_context *context)
842 {
843         struct virtual_keyboard *keyboard = data;
844
845         if (!keyboard->context)
846                 return;
847
848         wl_input_method_context_destroy(keyboard->context);
849         keyboard->context = NULL;
850 }
851
852 static const struct wl_input_method_listener input_method_listener = {
853         input_method_activate,
854         input_method_deactivate
855 };
856
857 static void
858 global_handler(struct display *display, uint32_t name,
859                const char *interface, uint32_t version, void *data)
860 {
861         struct virtual_keyboard *keyboard = data;
862
863         if (!strcmp(interface, "wl_input_panel")) {
864                 keyboard->input_panel =
865                         display_bind(display, name, &wl_input_panel_interface, 1);
866         } else if (!strcmp(interface, "wl_input_method")) {
867                 keyboard->input_method =
868                         display_bind(display, name,
869                                      &wl_input_method_interface, 1);
870                 wl_input_method_add_listener(keyboard->input_method, &input_method_listener, keyboard);
871         }
872 }
873
874 static void
875 keyboard_create(struct output *output, struct virtual_keyboard *virtual_keyboard)
876 {
877         struct keyboard *keyboard;
878         const struct layout *layout;
879         struct wl_input_panel_surface *ips;
880
881         layout = get_current_layout(virtual_keyboard);
882
883         keyboard = xzalloc(sizeof *keyboard);
884         keyboard->keyboard = virtual_keyboard;
885         keyboard->window = window_create_custom(virtual_keyboard->display);
886         keyboard->widget = window_add_widget(keyboard->window, keyboard);
887
888         virtual_keyboard->keyboard = keyboard;
889
890         window_set_title(keyboard->window, "Virtual keyboard");
891         window_set_user_data(keyboard->window, keyboard);
892
893         widget_set_redraw_handler(keyboard->widget, redraw_handler);
894         widget_set_resize_handler(keyboard->widget, resize_handler);
895         widget_set_button_handler(keyboard->widget, button_handler);
896         widget_set_touch_down_handler(keyboard->widget, touch_down_handler);
897         widget_set_touch_up_handler(keyboard->widget, touch_up_handler);
898
899         window_schedule_resize(keyboard->window,
900                                layout->columns * key_width,
901                                layout->rows * key_height);
902
903
904         ips = wl_input_panel_get_input_panel_surface(virtual_keyboard->input_panel,
905                                                      window_get_wl_surface(keyboard->window));
906
907         wl_input_panel_surface_set_toplevel(ips,
908                                             output_get_wl_output(output),
909                                             WL_INPUT_PANEL_SURFACE_POSITION_CENTER_BOTTOM);
910 }
911
912 int
913 main(int argc, char *argv[])
914 {
915         struct virtual_keyboard virtual_keyboard;
916         struct output *output;
917
918         memset(&virtual_keyboard, 0, sizeof virtual_keyboard);
919
920         virtual_keyboard.display = display_create(&argc, argv);
921         if (virtual_keyboard.display == NULL) {
922                 fprintf(stderr, "failed to create display: %m\n");
923                 return -1;
924         }
925
926         display_set_user_data(virtual_keyboard.display, &virtual_keyboard);
927         display_set_global_handler(virtual_keyboard.display, global_handler);
928
929         output = display_get_output(virtual_keyboard.display);
930         keyboard_create(output, &virtual_keyboard);
931
932         display_run(virtual_keyboard.display);
933
934         return 0;
935 }