Update snapshot
[profile/ivi/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 <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 "desktop-shell-client-protocol.h"
34
35 struct virtual_keyboard {
36         struct input_panel *input_panel;
37         struct input_method *input_method;
38         struct input_method_context *context;
39         struct display *display;
40         char *preedit_string;
41 };
42
43 enum key_type {
44         keytype_default,
45         keytype_backspace,
46         keytype_enter,
47         keytype_space,
48         keytype_switch,
49         keytype_symbols,
50         keytype_tab,
51         keytype_arrow_up,
52         keytype_arrow_left,
53         keytype_arrow_right,
54         keytype_arrow_down
55 };
56
57 struct key {
58         enum key_type key_type;
59
60         char *label;
61         char *alt;
62
63         unsigned int width;
64 };
65
66 static const struct key keys[] = {
67         { keytype_default, "q", "Q", 1},
68         { keytype_default, "w", "W", 1},
69         { keytype_default, "e", "E", 1},
70         { keytype_default, "r", "R", 1},
71         { keytype_default, "t", "T", 1},
72         { keytype_default, "y", "Y", 1},
73         { keytype_default, "u", "U", 1},
74         { keytype_default, "i", "I", 1},
75         { keytype_default, "o", "O", 1},
76         { keytype_default, "p", "P", 1},
77         { keytype_backspace, "<--", "<--", 2},
78
79         { keytype_tab, "->|", "->|", 1},
80         { keytype_default, "a", "A", 1},
81         { keytype_default, "s", "S", 1},
82         { keytype_default, "d", "D", 1},
83         { keytype_default, "f", "F", 1},
84         { keytype_default, "g", "G", 1},
85         { keytype_default, "h", "H", 1},
86         { keytype_default, "j", "J", 1},
87         { keytype_default, "k", "K", 1},
88         { keytype_default, "l", "L", 1},
89         { keytype_enter, "Enter", "Enter", 2},
90
91         { keytype_switch, "ABC", "abc", 2},
92         { keytype_default, "z", "Z", 1},
93         { keytype_default, "x", "X", 1},
94         { keytype_default, "c", "C", 1},
95         { keytype_default, "v", "V", 1},
96         { keytype_default, "b", "B", 1},
97         { keytype_default, "n", "N", 1},
98         { keytype_default, "m", "M", 1},
99         { keytype_default, ",", ",", 1},
100         { keytype_default, ".", ".", 1},
101         { keytype_switch, "ABC", "abc", 1},
102
103         { keytype_symbols, "?123", "?123", 1},
104         { keytype_space, "", "", 6},
105         { keytype_arrow_up, "/\\", "/\\", 1},
106         { keytype_arrow_left, "<", "<", 1},
107         { keytype_arrow_right, ">", ">", 1},
108         { keytype_arrow_down, "\\/", "\\/", 1},
109         { keytype_symbols, "?123", "?123", 1}
110 };
111
112 static const unsigned int columns = 12;
113 static const unsigned int rows = 4;
114
115 static const double key_width = 60;
116 static const double key_height = 50;
117
118 enum keyboard_state {
119         keyboardstate_default,
120         keyboardstate_uppercase
121 };
122
123 struct keyboard {
124         struct virtual_keyboard *keyboard;
125         struct window *window;
126         struct widget *widget;
127
128         enum keyboard_state state;
129 };
130
131 static void
132 draw_key(const struct key *key,
133          cairo_t *cr,
134          enum keyboard_state state,
135          unsigned int row,
136          unsigned int col)
137 {
138         const char *label;
139         cairo_text_extents_t extents;
140
141         cairo_save(cr);
142         cairo_rectangle(cr,
143                         col * key_width, row * key_height,
144                         key->width * key_width, key_height);
145         cairo_clip(cr);
146
147         /* Paint frame */
148         cairo_rectangle(cr,
149                         col * key_width, row * key_height,
150                         key->width * key_width, key_height);
151         cairo_set_line_width(cr, 3);
152         cairo_stroke(cr);
153
154         /* Paint text */
155         label = state == keyboardstate_default ? key->label : key->alt;
156         cairo_text_extents(cr, label, &extents);
157
158         cairo_translate(cr,
159                         col * key_width,
160                         row * key_height);
161         cairo_translate(cr,
162                         (key->width * key_width - extents.width) / 2,
163                         (key_height - extents.y_bearing) / 2);
164         cairo_show_text(cr, label);
165
166         cairo_restore(cr);
167 }
168
169 static void
170 redraw_handler(struct widget *widget, void *data)
171 {
172         struct keyboard *keyboard = data;
173         cairo_surface_t *surface;
174         struct rectangle allocation;
175         cairo_t *cr;
176         unsigned int i;
177         unsigned int row = 0, col = 0;
178
179         surface = window_get_surface(keyboard->window);
180         widget_get_allocation(keyboard->widget, &allocation);
181
182         cr = cairo_create(surface);
183         cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
184         cairo_clip(cr);
185
186         cairo_select_font_face(cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
187         cairo_set_font_size(cr, 16);
188
189         cairo_translate(cr, allocation.x, allocation.y);
190
191         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
192         cairo_set_source_rgba(cr, 1, 1, 1, 0.75);
193         cairo_rectangle(cr, 0, 0, columns * key_width, rows * key_height);
194         cairo_paint(cr);
195
196         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
197
198         for (i = 0; i < sizeof(keys) / sizeof(*keys); ++i) {
199                 cairo_set_source_rgb(cr, 0, 0, 0);
200                 draw_key(&keys[i], cr, keyboard->state, row, col);
201                 col += keys[i].width;
202                 if (col >= columns) {
203                         row += 1;
204                         col = 0;
205                 }
206         }
207
208         cairo_destroy(cr);
209         cairo_surface_destroy(surface);
210 }
211
212 static void
213 resize_handler(struct widget *widget,
214                int32_t width, int32_t height, void *data)
215 {
216         /* struct keyboard *keyboard = data; */
217 }
218
219 static void
220 keyboard_handle_key(struct keyboard *keyboard, const struct key *key)
221 {
222         const char *label = keyboard->state == keyboardstate_default ? key->label : key->alt;
223
224         switch (key->key_type) {
225                 case keytype_default:
226                         keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
227                                                                     label);
228                         input_method_context_preedit_string(keyboard->keyboard->context,
229                                                             keyboard->keyboard->preedit_string,
230                                                             strlen(keyboard->keyboard->preedit_string));
231                         break;
232                 case keytype_backspace:
233                         if (strlen(keyboard->keyboard->preedit_string) == 0) {
234                                 input_method_context_delete_surrounding_text(keyboard->keyboard->context,
235                                                                              -1, 1);
236                         } else {
237                                 keyboard->keyboard->preedit_string[strlen(keyboard->keyboard->preedit_string) - 1] = '\0';
238                                 input_method_context_preedit_string(keyboard->keyboard->context,
239                                                                     keyboard->keyboard->preedit_string,
240                                                                     strlen(keyboard->keyboard->preedit_string));
241                         }
242                         break;
243                 case keytype_enter:
244                         input_method_context_key(keyboard->keyboard->context,
245                                                  XKB_KEY_KP_Enter, WL_KEYBOARD_KEY_STATE_PRESSED);
246                         break;
247                 case keytype_space:
248                         keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
249                                                                     " ");
250                         input_method_context_preedit_string(keyboard->keyboard->context,
251                                                             "",
252                                                             0);
253                         input_method_context_commit_string(keyboard->keyboard->context,
254                                                            keyboard->keyboard->preedit_string,
255                                                            strlen(keyboard->keyboard->preedit_string));
256                         free(keyboard->keyboard->preedit_string);
257                         keyboard->keyboard->preedit_string = strdup("");
258                         break;
259                 case keytype_switch:
260                         if (keyboard->state == keyboardstate_default)
261                                 keyboard->state = keyboardstate_uppercase;
262                         else
263                                 keyboard->state = keyboardstate_default;
264                         break;
265                 case keytype_symbols:
266                         break;
267                 case keytype_tab:
268                         input_method_context_key(keyboard->keyboard->context,
269                                                  XKB_KEY_Tab, WL_KEYBOARD_KEY_STATE_PRESSED);
270                         break;
271                 case keytype_arrow_up:
272                         input_method_context_key(keyboard->keyboard->context,
273                                                  XKB_KEY_Up, WL_KEYBOARD_KEY_STATE_PRESSED);
274                         break;
275                 case keytype_arrow_left:
276                         input_method_context_key(keyboard->keyboard->context,
277                                                  XKB_KEY_Left, WL_KEYBOARD_KEY_STATE_PRESSED);
278                         break;
279                 case keytype_arrow_right:
280                         input_method_context_key(keyboard->keyboard->context,
281                                                  XKB_KEY_Right, WL_KEYBOARD_KEY_STATE_PRESSED);
282                         break;
283                 case keytype_arrow_down:
284                         input_method_context_key(keyboard->keyboard->context,
285                                                  XKB_KEY_Down, WL_KEYBOARD_KEY_STATE_PRESSED);
286                         break;
287         }
288 }
289
290 static void
291 button_handler(struct widget *widget,
292                struct input *input, uint32_t time,
293                uint32_t button,
294                enum wl_pointer_button_state state, void *data)
295 {
296         struct keyboard *keyboard = data;
297         struct rectangle allocation;
298         int32_t x, y;
299         int row, col;
300         unsigned int i;
301
302         if (state != WL_POINTER_BUTTON_STATE_PRESSED || button != BTN_LEFT) {
303                 return;
304         }
305
306         input_get_position(input, &x, &y);
307
308         widget_get_allocation(keyboard->widget, &allocation);
309         x -= allocation.x;
310         y -= allocation.y;
311
312         row = y / key_height;
313         col = x / key_width + row * columns;
314         for (i = 0; i < sizeof(keys) / sizeof(*keys); ++i) {
315                 col -= keys[i].width;
316                 if (col < 0) {
317                         keyboard_handle_key(keyboard, &keys[i]);
318                         break;
319                 }
320         }
321
322         widget_schedule_redraw(widget);
323 }
324
325 static void
326 input_method_context_surrounding_text(void *data,
327                                       struct input_method_context *context,
328                                       const char *text,
329                                       uint32_t cursor,
330                                       uint32_t anchor)
331 {
332         fprintf(stderr, "Surrounding text updated: %s\n", text);
333 }
334
335 static void
336 input_method_context_reset(void *data,
337                            struct input_method_context *context)
338 {
339         struct virtual_keyboard *keyboard = data;
340
341         fprintf(stderr, "Reset pre-edit buffer\n");
342
343         if (strlen(keyboard->preedit_string)) {
344                 input_method_context_preedit_string(context,
345                                                     "",
346                                                     0);
347                 free(keyboard->preedit_string);
348                 keyboard->preedit_string = strdup("");
349         }
350 }
351
352 static const struct input_method_context_listener input_method_context_listener = {
353         input_method_context_surrounding_text,
354         input_method_context_reset
355 };
356
357 static void
358 input_method_activate(void *data,
359                       struct input_method *input_method,
360                       struct input_method_context *context)
361 {
362         struct virtual_keyboard *keyboard = data;
363
364         if (keyboard->context)
365                 input_method_context_destroy(keyboard->context);
366
367         if (keyboard->preedit_string)
368                 free(keyboard->preedit_string);
369
370         keyboard->preedit_string = strdup("");
371
372         keyboard->context = context;
373         input_method_context_add_listener(context,
374                                           &input_method_context_listener,
375                                           keyboard);
376 }
377
378 static void
379 input_method_deactivate(void *data,
380                         struct input_method *input_method,
381                         struct input_method_context *context)
382 {
383         struct virtual_keyboard *keyboard = data;
384
385         if (!keyboard->context)
386                 return;
387
388         input_method_context_destroy(keyboard->context);
389         keyboard->context = NULL;
390 }
391
392 static const struct input_method_listener input_method_listener = {
393         input_method_activate,
394         input_method_deactivate
395 };
396
397 static void
398 global_handler(struct display *display, uint32_t name,
399                const char *interface, uint32_t version, void *data)
400 {
401         struct virtual_keyboard *keyboard = data;
402
403         if (!strcmp(interface, "input_panel")) {
404                 keyboard->input_panel =
405                         display_bind(display, name, &input_panel_interface, 1);
406         } else if (!strcmp(interface, "input_method")) {
407                 keyboard->input_method =
408                         display_bind(display, name,
409                                      &input_method_interface, 1);
410                 input_method_add_listener(keyboard->input_method, &input_method_listener, keyboard);
411         }
412 }
413
414 static void
415 keyboard_create(struct output *output, struct virtual_keyboard *virtual_keyboard)
416 {
417         struct keyboard *keyboard;
418
419         keyboard = malloc(sizeof *keyboard);
420         memset(keyboard, 0, sizeof *keyboard);
421
422         keyboard->keyboard = virtual_keyboard;
423         keyboard->window = window_create_custom(virtual_keyboard->display);
424         keyboard->widget = window_add_widget(keyboard->window, keyboard);
425
426         window_set_title(keyboard->window, "Virtual keyboard");
427         window_set_user_data(keyboard->window, keyboard);
428
429         widget_set_redraw_handler(keyboard->widget, redraw_handler);
430         widget_set_resize_handler(keyboard->widget, resize_handler);
431         widget_set_button_handler(keyboard->widget, button_handler);
432
433         window_schedule_resize(keyboard->window,
434                                columns * key_width,
435                                rows * key_height);
436
437         input_panel_set_surface(virtual_keyboard->input_panel,
438                                 window_get_wl_surface(keyboard->window),
439                                 output_get_wl_output(output));
440 }
441
442 static void
443 handle_output_configure(struct output *output, void *data)
444 {
445         struct virtual_keyboard *virtual_keyboard = data;
446
447         /* skip existing outputs */
448         if (output_get_user_data(output))
449                 return;
450
451         output_set_user_data(output, virtual_keyboard);
452
453         keyboard_create(output, virtual_keyboard);
454 }
455
456 int
457 main(int argc, char *argv[])
458 {
459         struct virtual_keyboard virtual_keyboard;
460
461         virtual_keyboard.display = display_create(argc, argv);
462         if (virtual_keyboard.display == NULL) {
463                 fprintf(stderr, "failed to create display: %m\n");
464                 return -1;
465         }
466
467         virtual_keyboard.context = NULL;
468         virtual_keyboard.preedit_string = NULL;
469
470         display_set_user_data(virtual_keyboard.display, &virtual_keyboard);
471         display_set_global_handler(virtual_keyboard.display, global_handler);
472         display_set_output_configure_handler(virtual_keyboard.display, handle_output_configure);
473
474         display_run(virtual_keyboard.display);
475
476         return 0;
477 }