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