2 * Copyright © 2012 Openismus GmbH
3 * Copyright © 2012 Intel Corporation
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.
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.
28 #include <linux/input.h>
32 #include "input-method-client-protocol.h"
33 #include "text-client-protocol.h"
37 struct virtual_keyboard {
38 struct input_panel *input_panel;
39 struct input_method *input_method;
40 struct input_method_context *context;
41 struct display *display;
43 uint32_t preedit_style;
45 xkb_mod_mask_t shift_mask;
48 uint32_t content_hint;
49 uint32_t content_purpose;
50 char *preferred_language;
51 char *surrounding_text;
52 struct keyboard *keyboard;
71 enum key_type key_type;
80 const struct key *keys;
87 uint32_t text_direction;
90 static const struct key normal_keys[] = {
91 { keytype_default, "q", "Q", 1},
92 { keytype_default, "w", "W", 1},
93 { keytype_default, "e", "E", 1},
94 { keytype_default, "r", "R", 1},
95 { keytype_default, "t", "T", 1},
96 { keytype_default, "y", "Y", 1},
97 { keytype_default, "u", "U", 1},
98 { keytype_default, "i", "I", 1},
99 { keytype_default, "o", "O", 1},
100 { keytype_default, "p", "P", 1},
101 { keytype_backspace, "<--", "<--", 2},
103 { keytype_tab, "->|", "->|", 1},
104 { keytype_default, "a", "A", 1},
105 { keytype_default, "s", "S", 1},
106 { keytype_default, "d", "D", 1},
107 { keytype_default, "f", "F", 1},
108 { keytype_default, "g", "G", 1},
109 { keytype_default, "h", "H", 1},
110 { keytype_default, "j", "J", 1},
111 { keytype_default, "k", "K", 1},
112 { keytype_default, "l", "L", 1},
113 { keytype_enter, "Enter", "Enter", 2},
115 { keytype_switch, "ABC", "abc", 2},
116 { keytype_default, "z", "Z", 1},
117 { keytype_default, "x", "X", 1},
118 { keytype_default, "c", "C", 1},
119 { keytype_default, "v", "V", 1},
120 { keytype_default, "b", "B", 1},
121 { keytype_default, "n", "N", 1},
122 { keytype_default, "m", "M", 1},
123 { keytype_default, ",", ",", 1},
124 { keytype_default, ".", ".", 1},
125 { keytype_switch, "ABC", "abc", 1},
127 { keytype_symbols, "?123", "?123", 1},
128 { keytype_space, "", "", 5},
129 { keytype_arrow_up, "/\\", "/\\", 1},
130 { keytype_arrow_left, "<", "<", 1},
131 { keytype_arrow_right, ">", ">", 1},
132 { keytype_arrow_down, "\\/", "\\/", 1},
133 { keytype_style, "", "", 2}
136 static const struct key numeric_keys[] = {
137 { keytype_default, "1", "1", 1},
138 { keytype_default, "2", "2", 1},
139 { keytype_default, "3", "3", 1},
140 { keytype_default, "4", "4", 1},
141 { keytype_default, "5", "5", 1},
142 { keytype_default, "6", "6", 1},
143 { keytype_default, "7", "7", 1},
144 { keytype_default, "8", "8", 1},
145 { keytype_default, "9", "9", 1},
146 { keytype_default, "0", "0", 1},
147 { keytype_backspace, "<--", "<--", 2},
149 { keytype_space, "", "", 4},
150 { keytype_enter, "Enter", "Enter", 2},
151 { keytype_arrow_up, "/\\", "/\\", 1},
152 { keytype_arrow_left, "<", "<", 1},
153 { keytype_arrow_right, ">", ">", 1},
154 { keytype_arrow_down, "\\/", "\\/", 1},
155 { keytype_style, "", "", 2}
158 static const struct key arabic_keys[] = {
159 { keytype_default, "ض", "ض", 1},
160 { keytype_default, "ص", "ص", 1},
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_backspace, "-->", "-->", 2},
172 { keytype_tab, "->|", "->|", 1},
173 { keytype_default, "ش", "ش", 1},
174 { keytype_default, "س", "س", 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_enter, "Enter", "Enter", 2},
186 { keytype_switch, "ABC", "abc", 2},
187 { keytype_default, "ئ", "ئ", 1},
188 { keytype_default, "ء", "ء", 1},
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_switch, "ABC", "abc", 2},
198 { keytype_symbols, "؟٣٢١", "؟٣٢١", 1},
199 { keytype_default, "ذ", "ذ", 1},
200 { keytype_default, "،", "،", 1},
201 { keytype_space, "", "", 6},
202 { keytype_default, ".", ".", 1},
203 { keytype_default, "ط", "ط", 1},
204 { keytype_style, "", "", 2}
208 static const struct layout normal_layout = {
210 sizeof(normal_keys) / sizeof(*normal_keys),
214 TEXT_INPUT_TEXT_DIRECTION_LTR
217 static const struct layout numeric_layout = {
219 sizeof(numeric_keys) / sizeof(*numeric_keys),
223 TEXT_INPUT_TEXT_DIRECTION_LTR
226 static const struct layout arabic_layout = {
228 sizeof(arabic_keys) / sizeof(*arabic_keys),
232 TEXT_INPUT_TEXT_DIRECTION_RTL
235 static const char *style_labels[] = {
246 static const double key_width = 60;
247 static const double key_height = 50;
249 enum keyboard_state {
250 keyboardstate_default,
251 keyboardstate_uppercase
255 struct virtual_keyboard *keyboard;
256 struct window *window;
257 struct widget *widget;
259 enum keyboard_state state;
263 label_from_key(struct keyboard *keyboard,
264 const struct key *key)
266 if (key->key_type == keytype_style)
267 return style_labels[keyboard->keyboard->preedit_style];
269 if (keyboard->state == keyboardstate_default)
276 draw_key(struct keyboard *keyboard,
277 const struct key *key,
283 cairo_text_extents_t extents;
287 col * key_width, row * key_height,
288 key->width * key_width, key_height);
293 col * key_width, row * key_height,
294 key->width * key_width, key_height);
295 cairo_set_line_width(cr, 3);
299 label = label_from_key(keyboard, key);
300 cairo_text_extents(cr, label, &extents);
306 (key->width * key_width - extents.width) / 2,
307 (key_height - extents.y_bearing) / 2);
308 cairo_show_text(cr, label);
313 static const struct layout *
314 get_current_layout(struct virtual_keyboard *keyboard)
316 switch (keyboard->content_purpose) {
317 case TEXT_INPUT_CONTENT_PURPOSE_DIGITS:
318 case TEXT_INPUT_CONTENT_PURPOSE_NUMBER:
319 return &numeric_layout;
321 if (keyboard->preferred_language &&
322 strcmp(keyboard->preferred_language, "ar") == 0)
323 return &arabic_layout;
325 return &normal_layout;
330 redraw_handler(struct widget *widget, void *data)
332 struct keyboard *keyboard = data;
333 cairo_surface_t *surface;
334 struct rectangle allocation;
337 unsigned int row = 0, col = 0;
338 const struct layout *layout;
340 layout = get_current_layout(keyboard->keyboard);
342 surface = window_get_surface(keyboard->window);
343 widget_get_allocation(keyboard->widget, &allocation);
345 cr = cairo_create(surface);
346 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
349 cairo_select_font_face(cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
350 cairo_set_font_size(cr, 16);
352 cairo_translate(cr, allocation.x, allocation.y);
354 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
355 cairo_set_source_rgba(cr, 1, 1, 1, 0.75);
356 cairo_rectangle(cr, 0, 0, layout->columns * key_width, layout->rows * key_height);
359 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
361 for (i = 0; i < layout->count; ++i) {
362 cairo_set_source_rgb(cr, 0, 0, 0);
363 draw_key(keyboard, &layout->keys[i], cr, row, col);
364 col += layout->keys[i].width;
365 if (col >= layout->columns) {
372 cairo_surface_destroy(surface);
376 resize_handler(struct widget *widget,
377 int32_t width, int32_t height, void *data)
379 /* struct keyboard *keyboard = data; */
383 virtual_keyboard_commit_preedit(struct virtual_keyboard *keyboard)
385 if (!keyboard->preedit_string ||
386 strlen(keyboard->preedit_string) == 0)
389 input_method_context_preedit_cursor(keyboard->context,
392 input_method_context_preedit_string(keyboard->context,
396 input_method_context_cursor_position(keyboard->context,
399 input_method_context_commit_string(keyboard->context,
401 keyboard->preedit_string);
402 free(keyboard->preedit_string);
403 keyboard->preedit_string = strdup("");
407 virtual_keyboard_send_preedit(struct virtual_keyboard *keyboard,
410 uint32_t index = strlen(keyboard->preedit_string);
412 if (keyboard->preedit_style)
413 input_method_context_preedit_styling(keyboard->context,
416 strlen(keyboard->preedit_string),
417 keyboard->preedit_style);
420 input_method_context_preedit_cursor(keyboard->context,
423 input_method_context_preedit_string(keyboard->context,
425 keyboard->preedit_string,
426 keyboard->preedit_string);
430 keyboard_handle_key(struct keyboard *keyboard, uint32_t time, const struct key *key, struct input *input, enum wl_pointer_button_state state)
432 const char *label = keyboard->state == keyboardstate_default ? key->label : key->alt;
433 xkb_mod_mask_t mod_mask = keyboard->state == keyboardstate_default ? 0 : keyboard->keyboard->keysym.shift_mask;
434 uint32_t key_state = (state == WL_POINTER_BUTTON_STATE_PRESSED) ? WL_KEYBOARD_KEY_STATE_PRESSED : WL_KEYBOARD_KEY_STATE_RELEASED;
436 switch (key->key_type) {
437 case keytype_default:
438 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
441 keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
443 virtual_keyboard_send_preedit(keyboard->keyboard, -1);
445 case keytype_backspace:
446 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
449 if (strlen(keyboard->keyboard->preedit_string) == 0) {
450 input_method_context_delete_surrounding_text(keyboard->keyboard->context,
451 keyboard->keyboard->serial,
454 keyboard->keyboard->preedit_string[strlen(keyboard->keyboard->preedit_string) - 1] = '\0';
455 virtual_keyboard_send_preedit(keyboard->keyboard, -1);
459 virtual_keyboard_commit_preedit(keyboard->keyboard);
460 input_method_context_keysym(keyboard->keyboard->context,
461 display_get_serial(keyboard->keyboard->display),
463 XKB_KEY_Return, key_state, mod_mask);
466 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
468 keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
470 virtual_keyboard_commit_preedit(keyboard->keyboard);
473 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
475 if (keyboard->state == keyboardstate_default)
476 keyboard->state = keyboardstate_uppercase;
478 keyboard->state = keyboardstate_default;
480 case keytype_symbols:
481 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
485 virtual_keyboard_commit_preedit(keyboard->keyboard);
486 input_method_context_keysym(keyboard->keyboard->context,
487 display_get_serial(keyboard->keyboard->display),
489 XKB_KEY_Tab, key_state, mod_mask);
491 case keytype_arrow_up:
492 virtual_keyboard_commit_preedit(keyboard->keyboard);
493 input_method_context_keysym(keyboard->keyboard->context,
494 display_get_serial(keyboard->keyboard->display),
496 XKB_KEY_Up, key_state, mod_mask);
498 case keytype_arrow_left:
499 virtual_keyboard_commit_preedit(keyboard->keyboard);
500 input_method_context_keysym(keyboard->keyboard->context,
501 display_get_serial(keyboard->keyboard->display),
503 XKB_KEY_Left, key_state, mod_mask);
505 case keytype_arrow_right:
506 virtual_keyboard_commit_preedit(keyboard->keyboard);
507 input_method_context_keysym(keyboard->keyboard->context,
508 display_get_serial(keyboard->keyboard->display),
510 XKB_KEY_Right, key_state, mod_mask);
512 case keytype_arrow_down:
513 virtual_keyboard_commit_preedit(keyboard->keyboard);
514 input_method_context_keysym(keyboard->keyboard->context,
515 display_get_serial(keyboard->keyboard->display),
517 XKB_KEY_Down, key_state, mod_mask);
520 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
522 keyboard->keyboard->preedit_style = (keyboard->keyboard->preedit_style + 1) % 8; /* TODO */
523 virtual_keyboard_send_preedit(keyboard->keyboard, -1);
529 button_handler(struct widget *widget,
530 struct input *input, uint32_t time,
532 enum wl_pointer_button_state state, void *data)
534 struct keyboard *keyboard = data;
535 struct rectangle allocation;
539 const struct layout *layout;
541 layout = get_current_layout(keyboard->keyboard);
543 if (button != BTN_LEFT) {
547 input_get_position(input, &x, &y);
549 widget_get_allocation(keyboard->widget, &allocation);
553 row = y / key_height;
554 col = x / key_width + row * layout->columns;
555 for (i = 0; i < layout->count; ++i) {
556 col -= layout->keys[i].width;
558 keyboard_handle_key(keyboard, time, &layout->keys[i], input, state);
563 widget_schedule_redraw(widget);
567 handle_surrounding_text(void *data,
568 struct input_method_context *context,
573 struct virtual_keyboard *keyboard = data;
575 free(keyboard->surrounding_text);
576 keyboard->surrounding_text = strdup(text);
580 handle_reset(void *data,
581 struct input_method_context *context,
584 struct virtual_keyboard *keyboard = data;
586 fprintf(stderr, "Reset pre-edit buffer\n");
588 if (strlen(keyboard->preedit_string)) {
589 input_method_context_preedit_cursor(context,
592 input_method_context_preedit_string(context,
596 free(keyboard->preedit_string);
597 keyboard->preedit_string = strdup("");
600 keyboard->serial = serial;
604 handle_content_type(void *data,
605 struct input_method_context *context,
609 struct virtual_keyboard *keyboard = data;
611 keyboard->content_hint = hint;
612 keyboard->content_purpose = purpose;
616 handle_invoke_action(void *data,
617 struct input_method_context *context,
621 struct virtual_keyboard *keyboard = data;
623 if (button != BTN_LEFT)
626 virtual_keyboard_send_preedit(keyboard, index);
630 handle_commit(void *data,
631 struct input_method_context *context)
633 struct virtual_keyboard *keyboard = data;
634 const struct layout *layout;
636 layout = get_current_layout(keyboard);
638 if (keyboard->surrounding_text)
639 fprintf(stderr, "Surrounding text updated: %s\n", keyboard->surrounding_text);
641 window_schedule_resize(keyboard->keyboard->window,
642 layout->columns * key_width,
643 layout->rows * key_height);
645 input_method_context_language(context, keyboard->serial, layout->language);
646 input_method_context_text_direction(context, keyboard->serial, layout->text_direction);
648 widget_schedule_redraw(keyboard->keyboard->widget);
652 handle_preferred_language(void *data,
653 struct input_method_context *context,
654 const char *language)
656 struct virtual_keyboard *keyboard = data;
658 if (keyboard->preferred_language)
659 free(keyboard->preferred_language);
661 keyboard->preferred_language = NULL;
664 keyboard->preferred_language = strdup(language);
667 static const struct input_method_context_listener input_method_context_listener = {
668 handle_surrounding_text,
671 handle_invoke_action,
673 handle_preferred_language
677 input_method_activate(void *data,
678 struct input_method *input_method,
679 struct input_method_context *context,
682 struct virtual_keyboard *keyboard = data;
683 struct wl_array modifiers_map;
684 const struct layout *layout;
686 keyboard->keyboard->state = keyboardstate_default;
688 if (keyboard->context)
689 input_method_context_destroy(keyboard->context);
691 if (keyboard->preedit_string)
692 free(keyboard->preedit_string);
694 keyboard->preedit_string = strdup("");
695 keyboard->content_hint = 0;
696 keyboard->content_purpose = 0;
697 free(keyboard->preferred_language);
698 keyboard->preferred_language = NULL;
699 free(keyboard->surrounding_text);
700 keyboard->surrounding_text = NULL;
702 keyboard->serial = serial;
704 keyboard->context = context;
705 input_method_context_add_listener(context,
706 &input_method_context_listener,
709 wl_array_init(&modifiers_map);
710 keysym_modifiers_add(&modifiers_map, "Shift");
711 keysym_modifiers_add(&modifiers_map, "Control");
712 keysym_modifiers_add(&modifiers_map, "Mod1");
713 input_method_context_modifiers_map(context, &modifiers_map);
714 keyboard->keysym.shift_mask = keysym_modifiers_get_mask(&modifiers_map, "Shift");
715 wl_array_release(&modifiers_map);
717 layout = get_current_layout(keyboard);
719 window_schedule_resize(keyboard->keyboard->window,
720 layout->columns * key_width,
721 layout->rows * key_height);
723 input_method_context_language(context, keyboard->serial, layout->language);
724 input_method_context_text_direction(context, keyboard->serial, layout->text_direction);
726 widget_schedule_redraw(keyboard->keyboard->widget);
730 input_method_deactivate(void *data,
731 struct input_method *input_method,
732 struct input_method_context *context)
734 struct virtual_keyboard *keyboard = data;
736 if (!keyboard->context)
739 input_method_context_destroy(keyboard->context);
740 keyboard->context = NULL;
743 static const struct input_method_listener input_method_listener = {
744 input_method_activate,
745 input_method_deactivate
749 global_handler(struct display *display, uint32_t name,
750 const char *interface, uint32_t version, void *data)
752 struct virtual_keyboard *keyboard = data;
754 if (!strcmp(interface, "input_panel")) {
755 keyboard->input_panel =
756 display_bind(display, name, &input_panel_interface, 1);
757 } else if (!strcmp(interface, "input_method")) {
758 keyboard->input_method =
759 display_bind(display, name,
760 &input_method_interface, 1);
761 input_method_add_listener(keyboard->input_method, &input_method_listener, keyboard);
766 keyboard_create(struct output *output, struct virtual_keyboard *virtual_keyboard)
768 struct keyboard *keyboard;
769 const struct layout *layout;
770 struct input_panel_surface *ips;
772 layout = get_current_layout(virtual_keyboard);
774 keyboard = malloc(sizeof *keyboard);
775 memset(keyboard, 0, sizeof *keyboard);
777 keyboard->keyboard = virtual_keyboard;
778 keyboard->window = window_create_custom(virtual_keyboard->display);
779 keyboard->widget = window_add_widget(keyboard->window, keyboard);
781 virtual_keyboard->keyboard = keyboard;
783 window_set_title(keyboard->window, "Virtual keyboard");
784 window_set_user_data(keyboard->window, keyboard);
786 widget_set_redraw_handler(keyboard->widget, redraw_handler);
787 widget_set_resize_handler(keyboard->widget, resize_handler);
788 widget_set_button_handler(keyboard->widget, button_handler);
790 window_schedule_resize(keyboard->window,
791 layout->columns * key_width,
792 layout->rows * key_height);
795 ips = input_panel_get_input_panel_surface(virtual_keyboard->input_panel,
796 window_get_wl_surface(keyboard->window));
798 input_panel_surface_set_toplevel(ips, INPUT_PANEL_SURFACE_POSITION_CENTER_BOTTOM);
802 handle_output_configure(struct output *output, void *data)
804 struct virtual_keyboard *virtual_keyboard = data;
806 /* skip existing outputs */
807 if (output_get_user_data(output))
810 output_set_user_data(output, virtual_keyboard);
812 keyboard_create(output, virtual_keyboard);
816 main(int argc, char *argv[])
818 struct virtual_keyboard virtual_keyboard;
820 memset(&virtual_keyboard, 0, sizeof virtual_keyboard);
822 virtual_keyboard.display = display_create(&argc, argv);
823 if (virtual_keyboard.display == NULL) {
824 fprintf(stderr, "failed to create display: %m\n");
828 display_set_user_data(virtual_keyboard.display, &virtual_keyboard);
829 display_set_global_handler(virtual_keyboard.display, global_handler);
830 display_set_output_configure_handler(virtual_keyboard.display, handle_output_configure);
832 display_run(virtual_keyboard.display);