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;
54 enum key_type key_type;
62 static const struct key keys[] = {
63 { keytype_default, "q", "Q", 1},
64 { keytype_default, "w", "W", 1},
65 { keytype_default, "e", "E", 1},
66 { keytype_default, "r", "R", 1},
67 { keytype_default, "t", "T", 1},
68 { keytype_default, "y", "Y", 1},
69 { keytype_default, "u", "U", 1},
70 { keytype_default, "i", "I", 1},
71 { keytype_default, "o", "O", 1},
72 { keytype_default, "p", "P", 1},
73 { keytype_backspace, "<--", "<--", 2},
75 { keytype_tab, "->|", "->|", 1},
76 { keytype_default, "a", "A", 1},
77 { keytype_default, "s", "S", 1},
78 { keytype_default, "d", "D", 1},
79 { keytype_default, "f", "F", 1},
80 { keytype_default, "g", "G", 1},
81 { keytype_default, "h", "H", 1},
82 { keytype_default, "j", "J", 1},
83 { keytype_default, "k", "K", 1},
84 { keytype_default, "l", "L", 1},
85 { keytype_enter, "Enter", "Enter", 2},
87 { keytype_switch, "ABC", "abc", 2},
88 { keytype_default, "z", "Z", 1},
89 { keytype_default, "x", "X", 1},
90 { keytype_default, "c", "C", 1},
91 { keytype_default, "v", "V", 1},
92 { keytype_default, "b", "B", 1},
93 { keytype_default, "n", "N", 1},
94 { keytype_default, "m", "M", 1},
95 { keytype_default, ",", ",", 1},
96 { keytype_default, ".", ".", 1},
97 { keytype_switch, "ABC", "abc", 1},
99 { keytype_symbols, "?123", "?123", 2},
100 { keytype_space, "", "", 8},
101 { keytype_symbols, "?123", "?123", 2}
104 static const unsigned int columns = 12;
105 static const unsigned int rows = 4;
107 static const double key_width = 60;
108 static const double key_height = 50;
110 enum keyboard_state {
111 keyboardstate_default,
112 keyboardstate_uppercase
116 struct virtual_keyboard *keyboard;
117 struct window *window;
118 struct widget *widget;
120 enum keyboard_state state;
124 draw_key(const struct key *key,
126 enum keyboard_state state,
131 cairo_text_extents_t extents;
135 col * key_width, row * key_height,
136 key->width * key_width, key_height);
141 col * key_width, row * key_height,
142 key->width * key_width, key_height);
143 cairo_set_line_width(cr, 3);
147 label = state == keyboardstate_default ? key->label : key->alt;
148 cairo_text_extents(cr, label, &extents);
154 (key->width * key_width - extents.width) / 2,
155 (key_height - extents.y_bearing) / 2);
156 cairo_show_text(cr, label);
162 redraw_handler(struct widget *widget, void *data)
164 struct keyboard *keyboard = data;
165 cairo_surface_t *surface;
166 struct rectangle allocation;
169 unsigned int row = 0, col = 0;
171 surface = window_get_surface(keyboard->window);
172 widget_get_allocation(keyboard->widget, &allocation);
174 cr = cairo_create(surface);
175 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
178 cairo_select_font_face(cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
179 cairo_set_font_size(cr, 16);
181 cairo_translate(cr, allocation.x, allocation.y);
183 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
184 cairo_set_source_rgba(cr, 1, 1, 1, 0.75);
185 cairo_rectangle(cr, 0, 0, columns * key_width, rows * key_height);
188 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
190 for (i = 0; i < sizeof(keys) / sizeof(*keys); ++i) {
191 cairo_set_source_rgb(cr, 0, 0, 0);
192 draw_key(&keys[i], cr, keyboard->state, row, col);
193 col += keys[i].width;
194 if (col >= columns) {
201 cairo_surface_destroy(surface);
205 resize_handler(struct widget *widget,
206 int32_t width, int32_t height, void *data)
208 /* struct keyboard *keyboard = data; */
212 keyboard_handle_key(struct keyboard *keyboard, const struct key *key)
214 const char *label = keyboard->state == keyboardstate_default ? key->label : key->alt;
216 switch (key->key_type) {
217 case keytype_default:
218 keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
220 input_method_context_preedit_string(keyboard->keyboard->context,
221 keyboard->keyboard->preedit_string,
222 strlen(keyboard->keyboard->preedit_string));
224 case keytype_backspace:
229 keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
231 input_method_context_preedit_string(keyboard->keyboard->context,
234 input_method_context_commit_string(keyboard->keyboard->context,
235 keyboard->keyboard->preedit_string,
236 strlen(keyboard->keyboard->preedit_string));
237 free(keyboard->keyboard->preedit_string);
238 keyboard->keyboard->preedit_string = strdup("");
241 if (keyboard->state == keyboardstate_default)
242 keyboard->state = keyboardstate_uppercase;
244 keyboard->state = keyboardstate_default;
246 case keytype_symbols:
254 button_handler(struct widget *widget,
255 struct input *input, uint32_t time,
257 enum wl_pointer_button_state state, void *data)
259 struct keyboard *keyboard = data;
260 struct rectangle allocation;
265 if (state != WL_POINTER_BUTTON_STATE_PRESSED || button != BTN_LEFT) {
269 input_get_position(input, &x, &y);
271 widget_get_allocation(keyboard->widget, &allocation);
275 row = y / key_height;
276 col = x / key_width + row * columns;
277 for (i = 0; i < sizeof(keys) / sizeof(*keys); ++i) {
278 col -= keys[i].width;
283 keyboard_handle_key(keyboard, &keys[i]);
285 widget_schedule_redraw(widget);
289 input_method_context_surrounding_text(void *data,
290 struct input_method_context *context,
295 fprintf(stderr, "Surrounding text updated: %s\n", text);
298 static const struct input_method_context_listener input_method_context_listener = {
299 input_method_context_surrounding_text,
303 input_method_activate(void *data,
304 struct input_method *input_method,
305 struct input_method_context *context)
307 struct virtual_keyboard *keyboard = data;
309 if (keyboard->context)
310 input_method_context_destroy(keyboard->context);
312 if (keyboard->preedit_string)
313 free(keyboard->preedit_string);
315 keyboard->preedit_string = strdup("");
317 keyboard->context = context;
318 input_method_context_add_listener(context,
319 &input_method_context_listener,
324 input_method_deactivate(void *data,
325 struct input_method *input_method,
326 struct input_method_context *context)
328 struct virtual_keyboard *keyboard = data;
330 if (!keyboard->context)
333 input_method_context_destroy(keyboard->context);
334 keyboard->context = NULL;
337 static const struct input_method_listener input_method_listener = {
338 input_method_activate,
339 input_method_deactivate
343 global_handler(struct wl_display *display, uint32_t id,
344 const char *interface, uint32_t version, void *data)
346 struct virtual_keyboard *keyboard = data;
348 if (!strcmp(interface, "input_panel")) {
349 keyboard->input_panel = wl_display_bind(display, id, &input_panel_interface);
350 } else if (!strcmp(interface, "input_method")) {
351 keyboard->input_method = wl_display_bind(display, id, &input_method_interface);
352 input_method_add_listener(keyboard->input_method, &input_method_listener, keyboard);
357 keyboard_create(struct output *output, struct virtual_keyboard *virtual_keyboard)
359 struct keyboard *keyboard;
361 keyboard = malloc(sizeof *keyboard);
362 memset(keyboard, 0, sizeof *keyboard);
364 keyboard->keyboard = virtual_keyboard;
365 keyboard->window = window_create_custom(virtual_keyboard->display);
366 keyboard->widget = window_add_widget(keyboard->window, keyboard);
368 window_set_title(keyboard->window, "Virtual keyboard");
369 window_set_user_data(keyboard->window, keyboard);
371 widget_set_redraw_handler(keyboard->widget, redraw_handler);
372 widget_set_resize_handler(keyboard->widget, resize_handler);
373 widget_set_button_handler(keyboard->widget, button_handler);
375 window_schedule_resize(keyboard->window,
379 input_panel_set_surface(virtual_keyboard->input_panel,
380 window_get_wl_surface(keyboard->window),
381 output_get_wl_output(output));
385 handle_output_configure(struct output *output, void *data)
387 struct virtual_keyboard *virtual_keyboard = data;
389 /* skip existing outputs */
390 if (output_get_user_data(output))
393 output_set_user_data(output, virtual_keyboard);
395 keyboard_create(output, virtual_keyboard);
399 main(int argc, char *argv[])
401 struct virtual_keyboard virtual_keyboard;
403 virtual_keyboard.display = display_create(argc, argv);
404 if (virtual_keyboard.display == NULL) {
405 fprintf(stderr, "failed to create display: %m\n");
409 virtual_keyboard.context = NULL;
410 virtual_keyboard.preedit_string = NULL;
412 wl_display_add_global_listener(display_get_display(virtual_keyboard.display),
413 global_handler, &virtual_keyboard);
415 display_set_user_data(virtual_keyboard.display, &virtual_keyboard);
416 display_set_output_configure_handler(virtual_keyboard.display, handle_output_configure);
418 display_run(virtual_keyboard.display);