blob: bf0e9f03d57eea677c7ec4652a11ad6451ddfbf9 [file] [log] [blame]
Jan Arne Petersencba9e472012-06-21 21:52:19 +02001/*
2 * Copyright © 2012 Openismus GmbH
Jan Arne Petersen620cd622012-09-09 23:08:32 +02003 * Copyright © 2012 Intel Corporation
Jan Arne Petersencba9e472012-06-21 21:52:19 +02004 *
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"
Jan Arne Petersen30b66ef2012-09-09 23:08:41 +020032#include "input-method-client-protocol.h"
Jan Arne Petersencba9e472012-06-21 21:52:19 +020033#include "desktop-shell-client-protocol.h"
34
35struct virtual_keyboard {
36 struct input_panel *input_panel;
37 struct input_method *input_method;
Jan Arne Petersen620cd622012-09-09 23:08:32 +020038 struct input_method_context *context;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020039 struct display *display;
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +020040 char *preedit_string;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020041};
42
Jan Arne Petersen892f1c32012-09-09 23:08:42 +020043enum key_type {
44 keytype_default,
45 keytype_backspace,
46 keytype_enter,
47 keytype_space,
48 keytype_switch,
49 keytype_symbols,
Jan Arne Petersen8aba11d2012-09-17 15:28:07 +020050 keytype_tab,
51 keytype_arrow_up,
52 keytype_arrow_left,
53 keytype_arrow_right,
54 keytype_arrow_down
Jan Arne Petersen892f1c32012-09-09 23:08:42 +020055};
56
57struct key {
58 enum key_type key_type;
59
60 char *label;
61 char *alt;
62
63 unsigned int width;
64};
65
66static 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
Jan Arne Petersen8aba11d2012-09-17 15:28:07 +0200103 { 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}
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200110};
111
112static const unsigned int columns = 12;
113static const unsigned int rows = 4;
114
115static const double key_width = 60;
116static const double key_height = 50;
117
118enum keyboard_state {
119 keyboardstate_default,
120 keyboardstate_uppercase
121};
122
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200123struct keyboard {
124 struct virtual_keyboard *keyboard;
125 struct window *window;
126 struct widget *widget;
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200127
128 enum keyboard_state state;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200129};
130
131static void
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200132draw_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
169static void
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200170redraw_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;
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200176 unsigned int i;
177 unsigned int row = 0, col = 0;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200178
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
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200186 cairo_select_font_face(cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
187 cairo_set_font_size(cr, 16);
188
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200189 cairo_translate(cr, allocation.x, allocation.y);
190
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200191 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200192 cairo_set_source_rgba(cr, 1, 1, 1, 0.75);
193 cairo_rectangle(cr, 0, 0, columns * key_width, rows * key_height);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200194 cairo_paint(cr);
195
196 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
197
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200198 for (i = 0; i < sizeof(keys) / sizeof(*keys); ++i) {
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200199 cairo_set_source_rgb(cr, 0, 0, 0);
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200200 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 }
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200206 }
207
208 cairo_destroy(cr);
209 cairo_surface_destroy(surface);
210}
211
212static void
213resize_handler(struct widget *widget,
214 int32_t width, int32_t height, void *data)
215{
216 /* struct keyboard *keyboard = data; */
217}
218
219static void
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200220keyboard_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:
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200226 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));
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200231 break;
232 case keytype_backspace:
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200233 if (strlen(keyboard->keyboard->preedit_string) == 0) {
234 input_method_context_delete_surrounding_text(keyboard->keyboard->context,
235 -1, 1);
236 }
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200237 break;
238 case keytype_enter:
Jan Arne Petersence8a4432012-09-09 23:08:45 +0200239 input_method_context_key(keyboard->keyboard->context,
240 XKB_KEY_KP_Enter, WL_KEYBOARD_KEY_STATE_PRESSED);
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200241 break;
242 case keytype_space:
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200243 keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
244 " ");
245 input_method_context_preedit_string(keyboard->keyboard->context,
246 "",
247 0);
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200248 input_method_context_commit_string(keyboard->keyboard->context,
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200249 keyboard->keyboard->preedit_string,
250 strlen(keyboard->keyboard->preedit_string));
251 free(keyboard->keyboard->preedit_string);
252 keyboard->keyboard->preedit_string = strdup("");
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200253 break;
254 case keytype_switch:
255 if (keyboard->state == keyboardstate_default)
256 keyboard->state = keyboardstate_uppercase;
257 else
258 keyboard->state = keyboardstate_default;
259 break;
260 case keytype_symbols:
261 break;
262 case keytype_tab:
Jan Arne Petersence8a4432012-09-09 23:08:45 +0200263 input_method_context_key(keyboard->keyboard->context,
264 XKB_KEY_Tab, WL_KEYBOARD_KEY_STATE_PRESSED);
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200265 break;
Jan Arne Petersen8aba11d2012-09-17 15:28:07 +0200266 case keytype_arrow_up:
267 input_method_context_key(keyboard->keyboard->context,
268 XKB_KEY_Up, WL_KEYBOARD_KEY_STATE_PRESSED);
269 break;
270 case keytype_arrow_left:
271 input_method_context_key(keyboard->keyboard->context,
272 XKB_KEY_Left, WL_KEYBOARD_KEY_STATE_PRESSED);
273 break;
274 case keytype_arrow_right:
275 input_method_context_key(keyboard->keyboard->context,
276 XKB_KEY_Right, WL_KEYBOARD_KEY_STATE_PRESSED);
277 break;
278 case keytype_arrow_down:
279 input_method_context_key(keyboard->keyboard->context,
280 XKB_KEY_Down, WL_KEYBOARD_KEY_STATE_PRESSED);
281 break;
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200282 }
283}
284
285static void
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200286button_handler(struct widget *widget,
287 struct input *input, uint32_t time,
288 uint32_t button,
289 enum wl_pointer_button_state state, void *data)
290{
291 struct keyboard *keyboard = data;
292 struct rectangle allocation;
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200293 int32_t x, y;
294 int row, col;
295 unsigned int i;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200296
297 if (state != WL_POINTER_BUTTON_STATE_PRESSED || button != BTN_LEFT) {
298 return;
299 }
300
301 input_get_position(input, &x, &y);
302
303 widget_get_allocation(keyboard->widget, &allocation);
304 x -= allocation.x;
305 y -= allocation.y;
306
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200307 row = y / key_height;
308 col = x / key_width + row * columns;
309 for (i = 0; i < sizeof(keys) / sizeof(*keys); ++i) {
310 col -= keys[i].width;
311 if (col < 0)
312 break;
313 }
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200314
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200315 keyboard_handle_key(keyboard, &keys[i]);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200316
317 widget_schedule_redraw(widget);
318}
319
320static void
Jan Arne Petersencb08f4d2012-09-09 23:08:40 +0200321input_method_context_surrounding_text(void *data,
322 struct input_method_context *context,
323 const char *text,
324 uint32_t cursor,
325 uint32_t anchor)
326{
327 fprintf(stderr, "Surrounding text updated: %s\n", text);
328}
329
Jan Arne Petersenc1e481e2012-09-09 23:08:46 +0200330static void
331input_method_context_reset(void *data,
332 struct input_method_context *context)
333{
334 struct virtual_keyboard *keyboard = data;
335
336 fprintf(stderr, "Reset pre-edit buffer\n");
337
338 if (strlen(keyboard->preedit_string)) {
339 input_method_context_preedit_string(context,
340 "",
341 0);
342 free(keyboard->preedit_string);
343 keyboard->preedit_string = strdup("");
344 }
345}
346
Jan Arne Petersencb08f4d2012-09-09 23:08:40 +0200347static const struct input_method_context_listener input_method_context_listener = {
348 input_method_context_surrounding_text,
Jan Arne Petersenc1e481e2012-09-09 23:08:46 +0200349 input_method_context_reset
Jan Arne Petersencb08f4d2012-09-09 23:08:40 +0200350};
351
352static void
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200353input_method_activate(void *data,
354 struct input_method *input_method,
355 struct input_method_context *context)
356{
357 struct virtual_keyboard *keyboard = data;
358
359 if (keyboard->context)
360 input_method_context_destroy(keyboard->context);
361
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200362 if (keyboard->preedit_string)
363 free(keyboard->preedit_string);
364
365 keyboard->preedit_string = strdup("");
366
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200367 keyboard->context = context;
Jan Arne Petersencb08f4d2012-09-09 23:08:40 +0200368 input_method_context_add_listener(context,
369 &input_method_context_listener,
370 keyboard);
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200371}
372
373static void
374input_method_deactivate(void *data,
375 struct input_method *input_method,
376 struct input_method_context *context)
377{
378 struct virtual_keyboard *keyboard = data;
379
380 if (!keyboard->context)
381 return;
382
383 input_method_context_destroy(keyboard->context);
384 keyboard->context = NULL;
385}
386
387static const struct input_method_listener input_method_listener = {
388 input_method_activate,
389 input_method_deactivate
390};
391
392static void
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200393global_handler(struct wl_display *display, uint32_t id,
394 const char *interface, uint32_t version, void *data)
395{
396 struct virtual_keyboard *keyboard = data;
397
398 if (!strcmp(interface, "input_panel")) {
399 keyboard->input_panel = wl_display_bind(display, id, &input_panel_interface);
400 } else if (!strcmp(interface, "input_method")) {
401 keyboard->input_method = wl_display_bind(display, id, &input_method_interface);
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200402 input_method_add_listener(keyboard->input_method, &input_method_listener, keyboard);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200403 }
404}
405
406static void
407keyboard_create(struct output *output, struct virtual_keyboard *virtual_keyboard)
408{
409 struct keyboard *keyboard;
410
411 keyboard = malloc(sizeof *keyboard);
412 memset(keyboard, 0, sizeof *keyboard);
413
414 keyboard->keyboard = virtual_keyboard;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -0400415 keyboard->window = window_create_custom(virtual_keyboard->display);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200416 keyboard->widget = window_add_widget(keyboard->window, keyboard);
417
418 window_set_title(keyboard->window, "Virtual keyboard");
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200419 window_set_user_data(keyboard->window, keyboard);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200420
421 widget_set_redraw_handler(keyboard->widget, redraw_handler);
422 widget_set_resize_handler(keyboard->widget, resize_handler);
423 widget_set_button_handler(keyboard->widget, button_handler);
424
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200425 window_schedule_resize(keyboard->window,
426 columns * key_width,
427 rows * key_height);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200428
429 input_panel_set_surface(virtual_keyboard->input_panel,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -0400430 window_get_wl_surface(keyboard->window),
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200431 output_get_wl_output(output));
432}
433
434static void
435handle_output_configure(struct output *output, void *data)
436{
437 struct virtual_keyboard *virtual_keyboard = data;
438
439 /* skip existing outputs */
440 if (output_get_user_data(output))
441 return;
442
443 output_set_user_data(output, virtual_keyboard);
444
445 keyboard_create(output, virtual_keyboard);
446}
447
448int
449main(int argc, char *argv[])
450{
451 struct virtual_keyboard virtual_keyboard;
452
453 virtual_keyboard.display = display_create(argc, argv);
454 if (virtual_keyboard.display == NULL) {
455 fprintf(stderr, "failed to create display: %m\n");
456 return -1;
457 }
458
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200459 virtual_keyboard.context = NULL;
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200460 virtual_keyboard.preedit_string = NULL;
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200461
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200462 wl_display_add_global_listener(display_get_display(virtual_keyboard.display),
463 global_handler, &virtual_keyboard);
464
465 display_set_user_data(virtual_keyboard.display, &virtual_keyboard);
466 display_set_output_configure_handler(virtual_keyboard.display, handle_output_configure);
467
468 display_run(virtual_keyboard.display);
469
470 return 0;
471}