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 "desktop-shell-client-protocol.h"
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;
58 enum key_type key_type;
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},
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},
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},
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}
112 static const unsigned int columns = 12;
113 static const unsigned int rows = 4;
115 static const double key_width = 60;
116 static const double key_height = 50;
118 enum keyboard_state {
119 keyboardstate_default,
120 keyboardstate_uppercase
124 struct virtual_keyboard *keyboard;
125 struct window *window;
126 struct widget *widget;
128 enum keyboard_state state;
132 draw_key(const struct key *key,
134 enum keyboard_state state,
139 cairo_text_extents_t extents;
143 col * key_width, row * key_height,
144 key->width * key_width, key_height);
149 col * key_width, row * key_height,
150 key->width * key_width, key_height);
151 cairo_set_line_width(cr, 3);
155 label = state == keyboardstate_default ? key->label : key->alt;
156 cairo_text_extents(cr, label, &extents);
162 (key->width * key_width - extents.width) / 2,
163 (key_height - extents.y_bearing) / 2);
164 cairo_show_text(cr, label);
170 redraw_handler(struct widget *widget, void *data)
172 struct keyboard *keyboard = data;
173 cairo_surface_t *surface;
174 struct rectangle allocation;
177 unsigned int row = 0, col = 0;
179 surface = window_get_surface(keyboard->window);
180 widget_get_allocation(keyboard->widget, &allocation);
182 cr = cairo_create(surface);
183 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
186 cairo_select_font_face(cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
187 cairo_set_font_size(cr, 16);
189 cairo_translate(cr, allocation.x, allocation.y);
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);
196 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
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) {
209 cairo_surface_destroy(surface);
213 resize_handler(struct widget *widget,
214 int32_t width, int32_t height, void *data)
216 /* struct keyboard *keyboard = data; */
220 keyboard_handle_key(struct keyboard *keyboard, const struct key *key)
222 const char *label = keyboard->state == keyboardstate_default ? key->label : key->alt;
224 switch (key->key_type) {
225 case keytype_default:
226 keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
228 input_method_context_preedit_string(keyboard->keyboard->context,
229 keyboard->keyboard->preedit_string,
230 strlen(keyboard->keyboard->preedit_string));
232 case keytype_backspace:
233 if (strlen(keyboard->keyboard->preedit_string) == 0) {
234 input_method_context_delete_surrounding_text(keyboard->keyboard->context,
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));
244 input_method_context_key(keyboard->keyboard->context,
245 XKB_KEY_KP_Enter, WL_KEYBOARD_KEY_STATE_PRESSED);
248 keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
250 input_method_context_preedit_string(keyboard->keyboard->context,
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("");
260 if (keyboard->state == keyboardstate_default)
261 keyboard->state = keyboardstate_uppercase;
263 keyboard->state = keyboardstate_default;
265 case keytype_symbols:
268 input_method_context_key(keyboard->keyboard->context,
269 XKB_KEY_Tab, WL_KEYBOARD_KEY_STATE_PRESSED);
271 case keytype_arrow_up:
272 input_method_context_key(keyboard->keyboard->context,
273 XKB_KEY_Up, WL_KEYBOARD_KEY_STATE_PRESSED);
275 case keytype_arrow_left:
276 input_method_context_key(keyboard->keyboard->context,
277 XKB_KEY_Left, WL_KEYBOARD_KEY_STATE_PRESSED);
279 case keytype_arrow_right:
280 input_method_context_key(keyboard->keyboard->context,
281 XKB_KEY_Right, WL_KEYBOARD_KEY_STATE_PRESSED);
283 case keytype_arrow_down:
284 input_method_context_key(keyboard->keyboard->context,
285 XKB_KEY_Down, WL_KEYBOARD_KEY_STATE_PRESSED);
291 button_handler(struct widget *widget,
292 struct input *input, uint32_t time,
294 enum wl_pointer_button_state state, void *data)
296 struct keyboard *keyboard = data;
297 struct rectangle allocation;
302 if (state != WL_POINTER_BUTTON_STATE_PRESSED || button != BTN_LEFT) {
306 input_get_position(input, &x, &y);
308 widget_get_allocation(keyboard->widget, &allocation);
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;
317 keyboard_handle_key(keyboard, &keys[i]);
322 widget_schedule_redraw(widget);
326 input_method_context_surrounding_text(void *data,
327 struct input_method_context *context,
332 fprintf(stderr, "Surrounding text updated: %s\n", text);
336 input_method_context_reset(void *data,
337 struct input_method_context *context)
339 struct virtual_keyboard *keyboard = data;
341 fprintf(stderr, "Reset pre-edit buffer\n");
343 if (strlen(keyboard->preedit_string)) {
344 input_method_context_preedit_string(context,
347 free(keyboard->preedit_string);
348 keyboard->preedit_string = strdup("");
352 static const struct input_method_context_listener input_method_context_listener = {
353 input_method_context_surrounding_text,
354 input_method_context_reset
358 input_method_activate(void *data,
359 struct input_method *input_method,
360 struct input_method_context *context)
362 struct virtual_keyboard *keyboard = data;
364 if (keyboard->context)
365 input_method_context_destroy(keyboard->context);
367 if (keyboard->preedit_string)
368 free(keyboard->preedit_string);
370 keyboard->preedit_string = strdup("");
372 keyboard->context = context;
373 input_method_context_add_listener(context,
374 &input_method_context_listener,
379 input_method_deactivate(void *data,
380 struct input_method *input_method,
381 struct input_method_context *context)
383 struct virtual_keyboard *keyboard = data;
385 if (!keyboard->context)
388 input_method_context_destroy(keyboard->context);
389 keyboard->context = NULL;
392 static const struct input_method_listener input_method_listener = {
393 input_method_activate,
394 input_method_deactivate
398 global_handler(struct display *display, uint32_t name,
399 const char *interface, uint32_t version, void *data)
401 struct virtual_keyboard *keyboard = data;
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);
415 keyboard_create(struct output *output, struct virtual_keyboard *virtual_keyboard)
417 struct keyboard *keyboard;
419 keyboard = malloc(sizeof *keyboard);
420 memset(keyboard, 0, sizeof *keyboard);
422 keyboard->keyboard = virtual_keyboard;
423 keyboard->window = window_create_custom(virtual_keyboard->display);
424 keyboard->widget = window_add_widget(keyboard->window, keyboard);
426 window_set_title(keyboard->window, "Virtual keyboard");
427 window_set_user_data(keyboard->window, keyboard);
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);
433 window_schedule_resize(keyboard->window,
437 input_panel_set_surface(virtual_keyboard->input_panel,
438 window_get_wl_surface(keyboard->window),
439 output_get_wl_output(output));
443 handle_output_configure(struct output *output, void *data)
445 struct virtual_keyboard *virtual_keyboard = data;
447 /* skip existing outputs */
448 if (output_get_user_data(output))
451 output_set_user_data(output, virtual_keyboard);
453 keyboard_create(output, virtual_keyboard);
457 main(int argc, char *argv[])
459 struct virtual_keyboard virtual_keyboard;
461 virtual_keyboard.display = display_create(argc, argv);
462 if (virtual_keyboard.display == NULL) {
463 fprintf(stderr, "failed to create display: %m\n");
467 virtual_keyboard.context = NULL;
468 virtual_keyboard.preedit_string = NULL;
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);
474 display_run(virtual_keyboard.display);