blob: ff356171a352da047f5a2e954c37ae208fa6b231 [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 Petersencd997062012-11-18 19:06:44 +010041 struct {
42 xkb_mod_mask_t shift_mask;
43 } keysym;
Jan Arne Petersencba9e472012-06-21 21:52:19 +020044};
45
Jan Arne Petersen892f1c32012-09-09 23:08:42 +020046enum key_type {
47 keytype_default,
48 keytype_backspace,
49 keytype_enter,
50 keytype_space,
51 keytype_switch,
52 keytype_symbols,
Jan Arne Petersen8aba11d2012-09-17 15:28:07 +020053 keytype_tab,
54 keytype_arrow_up,
55 keytype_arrow_left,
56 keytype_arrow_right,
57 keytype_arrow_down
Jan Arne Petersen892f1c32012-09-09 23:08:42 +020058};
59
60struct key {
61 enum key_type key_type;
62
63 char *label;
64 char *alt;
65
66 unsigned int width;
67};
68
69static const struct key keys[] = {
70 { keytype_default, "q", "Q", 1},
71 { keytype_default, "w", "W", 1},
72 { keytype_default, "e", "E", 1},
73 { keytype_default, "r", "R", 1},
74 { keytype_default, "t", "T", 1},
75 { keytype_default, "y", "Y", 1},
76 { keytype_default, "u", "U", 1},
77 { keytype_default, "i", "I", 1},
78 { keytype_default, "o", "O", 1},
79 { keytype_default, "p", "P", 1},
80 { keytype_backspace, "<--", "<--", 2},
81
82 { keytype_tab, "->|", "->|", 1},
83 { keytype_default, "a", "A", 1},
84 { keytype_default, "s", "S", 1},
85 { keytype_default, "d", "D", 1},
86 { keytype_default, "f", "F", 1},
87 { keytype_default, "g", "G", 1},
88 { keytype_default, "h", "H", 1},
89 { keytype_default, "j", "J", 1},
90 { keytype_default, "k", "K", 1},
91 { keytype_default, "l", "L", 1},
92 { keytype_enter, "Enter", "Enter", 2},
93
94 { keytype_switch, "ABC", "abc", 2},
95 { keytype_default, "z", "Z", 1},
96 { keytype_default, "x", "X", 1},
97 { keytype_default, "c", "C", 1},
98 { keytype_default, "v", "V", 1},
99 { keytype_default, "b", "B", 1},
100 { keytype_default, "n", "N", 1},
101 { keytype_default, "m", "M", 1},
102 { keytype_default, ",", ",", 1},
103 { keytype_default, ".", ".", 1},
104 { keytype_switch, "ABC", "abc", 1},
105
Jan Arne Petersen8aba11d2012-09-17 15:28:07 +0200106 { keytype_symbols, "?123", "?123", 1},
107 { keytype_space, "", "", 6},
108 { keytype_arrow_up, "/\\", "/\\", 1},
109 { keytype_arrow_left, "<", "<", 1},
110 { keytype_arrow_right, ">", ">", 1},
111 { keytype_arrow_down, "\\/", "\\/", 1},
112 { keytype_symbols, "?123", "?123", 1}
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200113};
114
115static const unsigned int columns = 12;
116static const unsigned int rows = 4;
117
118static const double key_width = 60;
119static const double key_height = 50;
120
121enum keyboard_state {
122 keyboardstate_default,
123 keyboardstate_uppercase
124};
125
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200126struct keyboard {
127 struct virtual_keyboard *keyboard;
128 struct window *window;
129 struct widget *widget;
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200130
131 enum keyboard_state state;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200132};
133
134static void
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200135draw_key(const struct key *key,
136 cairo_t *cr,
137 enum keyboard_state state,
138 unsigned int row,
139 unsigned int col)
140{
141 const char *label;
142 cairo_text_extents_t extents;
143
144 cairo_save(cr);
145 cairo_rectangle(cr,
146 col * key_width, row * key_height,
147 key->width * key_width, key_height);
148 cairo_clip(cr);
149
150 /* Paint frame */
151 cairo_rectangle(cr,
152 col * key_width, row * key_height,
153 key->width * key_width, key_height);
154 cairo_set_line_width(cr, 3);
155 cairo_stroke(cr);
156
157 /* Paint text */
158 label = state == keyboardstate_default ? key->label : key->alt;
159 cairo_text_extents(cr, label, &extents);
160
161 cairo_translate(cr,
162 col * key_width,
163 row * key_height);
164 cairo_translate(cr,
165 (key->width * key_width - extents.width) / 2,
166 (key_height - extents.y_bearing) / 2);
167 cairo_show_text(cr, label);
168
169 cairo_restore(cr);
170}
171
172static void
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200173redraw_handler(struct widget *widget, void *data)
174{
175 struct keyboard *keyboard = data;
176 cairo_surface_t *surface;
177 struct rectangle allocation;
178 cairo_t *cr;
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200179 unsigned int i;
180 unsigned int row = 0, col = 0;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200181
182 surface = window_get_surface(keyboard->window);
183 widget_get_allocation(keyboard->widget, &allocation);
184
185 cr = cairo_create(surface);
186 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
187 cairo_clip(cr);
188
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200189 cairo_select_font_face(cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
190 cairo_set_font_size(cr, 16);
191
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200192 cairo_translate(cr, allocation.x, allocation.y);
193
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200194 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200195 cairo_set_source_rgba(cr, 1, 1, 1, 0.75);
196 cairo_rectangle(cr, 0, 0, columns * key_width, rows * key_height);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200197 cairo_paint(cr);
198
199 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
200
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200201 for (i = 0; i < sizeof(keys) / sizeof(*keys); ++i) {
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200202 cairo_set_source_rgb(cr, 0, 0, 0);
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200203 draw_key(&keys[i], cr, keyboard->state, row, col);
204 col += keys[i].width;
205 if (col >= columns) {
206 row += 1;
207 col = 0;
208 }
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200209 }
210
211 cairo_destroy(cr);
212 cairo_surface_destroy(surface);
213}
214
215static void
216resize_handler(struct widget *widget,
217 int32_t width, int32_t height, void *data)
218{
219 /* struct keyboard *keyboard = data; */
220}
221
222static void
Jan Arne Petersen5ec05fb2012-11-05 03:26:42 +0100223virtual_keyboard_commit_preedit(struct virtual_keyboard *keyboard)
224{
225 if (!keyboard->preedit_string ||
226 strlen(keyboard->preedit_string) == 0)
227 return;
228
229 input_method_context_preedit_string(keyboard->context,
230 "",
231 0);
232 input_method_context_commit_string(keyboard->context,
233 keyboard->preedit_string,
234 strlen(keyboard->preedit_string));
235 free(keyboard->preedit_string);
236 keyboard->preedit_string = strdup("");
237}
238
239static void
Jan Arne Petersencd997062012-11-18 19:06:44 +0100240keyboard_handle_key(struct keyboard *keyboard, uint32_t time, const struct key *key, struct input *input, enum wl_pointer_button_state state)
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200241{
242 const char *label = keyboard->state == keyboardstate_default ? key->label : key->alt;
Jan Arne Petersencd997062012-11-18 19:06:44 +0100243 xkb_mod_mask_t mod_mask = keyboard->state == keyboardstate_default ? 0 : keyboard->keyboard->keysym.shift_mask;
244 uint32_t key_state = (state == WL_POINTER_BUTTON_STATE_PRESSED) ? WL_KEYBOARD_KEY_STATE_PRESSED : WL_KEYBOARD_KEY_STATE_RELEASED;
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200245
246 switch (key->key_type) {
247 case keytype_default:
Jan Arne Petersencd997062012-11-18 19:06:44 +0100248 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
249 break;
250
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200251 keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
252 label);
253 input_method_context_preedit_string(keyboard->keyboard->context,
254 keyboard->keyboard->preedit_string,
255 strlen(keyboard->keyboard->preedit_string));
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200256 break;
257 case keytype_backspace:
Jan Arne Petersencd997062012-11-18 19:06:44 +0100258 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
259 break;
260
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200261 if (strlen(keyboard->keyboard->preedit_string) == 0) {
262 input_method_context_delete_surrounding_text(keyboard->keyboard->context,
263 -1, 1);
Jan Arne Petersen633c83d2012-09-17 15:28:08 +0200264 } else {
265 keyboard->keyboard->preedit_string[strlen(keyboard->keyboard->preedit_string) - 1] = '\0';
266 input_method_context_preedit_string(keyboard->keyboard->context,
267 keyboard->keyboard->preedit_string,
268 strlen(keyboard->keyboard->preedit_string));
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200269 }
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200270 break;
271 case keytype_enter:
Jan Arne Petersen5ec05fb2012-11-05 03:26:42 +0100272 virtual_keyboard_commit_preedit(keyboard->keyboard);
Jan Arne Petersend9be93b2012-11-18 19:06:43 +0100273 input_method_context_keysym(keyboard->keyboard->context,
274 display_get_serial(keyboard->keyboard->display),
275 time,
Jan Arne Petersencd997062012-11-18 19:06:44 +0100276 XKB_KEY_Return, key_state, mod_mask);
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200277 break;
278 case keytype_space:
Jan Arne Petersencd997062012-11-18 19:06:44 +0100279 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
280 break;
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200281 keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
282 " ");
Jan Arne Petersen5ec05fb2012-11-05 03:26:42 +0100283 virtual_keyboard_commit_preedit(keyboard->keyboard);
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200284 break;
285 case keytype_switch:
Jan Arne Petersencd997062012-11-18 19:06:44 +0100286 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
287 break;
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200288 if (keyboard->state == keyboardstate_default)
289 keyboard->state = keyboardstate_uppercase;
290 else
291 keyboard->state = keyboardstate_default;
292 break;
293 case keytype_symbols:
Jan Arne Petersencd997062012-11-18 19:06:44 +0100294 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
295 break;
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200296 break;
297 case keytype_tab:
Jan Arne Petersen5ec05fb2012-11-05 03:26:42 +0100298 virtual_keyboard_commit_preedit(keyboard->keyboard);
Jan Arne Petersend9be93b2012-11-18 19:06:43 +0100299 input_method_context_keysym(keyboard->keyboard->context,
300 display_get_serial(keyboard->keyboard->display),
301 time,
Jan Arne Petersencd997062012-11-18 19:06:44 +0100302 XKB_KEY_Tab, key_state, mod_mask);
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200303 break;
Jan Arne Petersen8aba11d2012-09-17 15:28:07 +0200304 case keytype_arrow_up:
Jan Arne Petersen5ec05fb2012-11-05 03:26:42 +0100305 virtual_keyboard_commit_preedit(keyboard->keyboard);
Jan Arne Petersend9be93b2012-11-18 19:06:43 +0100306 input_method_context_keysym(keyboard->keyboard->context,
307 display_get_serial(keyboard->keyboard->display),
308 time,
Jan Arne Petersencd997062012-11-18 19:06:44 +0100309 XKB_KEY_Up, key_state, mod_mask);
Jan Arne Petersen8aba11d2012-09-17 15:28:07 +0200310 break;
311 case keytype_arrow_left:
Jan Arne Petersen5ec05fb2012-11-05 03:26:42 +0100312 virtual_keyboard_commit_preedit(keyboard->keyboard);
Jan Arne Petersend9be93b2012-11-18 19:06:43 +0100313 input_method_context_keysym(keyboard->keyboard->context,
314 display_get_serial(keyboard->keyboard->display),
315 time,
Jan Arne Petersencd997062012-11-18 19:06:44 +0100316 XKB_KEY_Left, key_state, mod_mask);
Jan Arne Petersen8aba11d2012-09-17 15:28:07 +0200317 break;
318 case keytype_arrow_right:
Jan Arne Petersen5ec05fb2012-11-05 03:26:42 +0100319 virtual_keyboard_commit_preedit(keyboard->keyboard);
Jan Arne Petersend9be93b2012-11-18 19:06:43 +0100320 input_method_context_keysym(keyboard->keyboard->context,
321 display_get_serial(keyboard->keyboard->display),
322 time,
Jan Arne Petersencd997062012-11-18 19:06:44 +0100323 XKB_KEY_Right, key_state, mod_mask);
Jan Arne Petersen8aba11d2012-09-17 15:28:07 +0200324 break;
325 case keytype_arrow_down:
Jan Arne Petersen5ec05fb2012-11-05 03:26:42 +0100326 virtual_keyboard_commit_preedit(keyboard->keyboard);
Jan Arne Petersend9be93b2012-11-18 19:06:43 +0100327 input_method_context_keysym(keyboard->keyboard->context,
328 display_get_serial(keyboard->keyboard->display),
329 time,
Jan Arne Petersencd997062012-11-18 19:06:44 +0100330 XKB_KEY_Down, key_state, mod_mask);
Jan Arne Petersen8aba11d2012-09-17 15:28:07 +0200331 break;
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200332 }
333}
334
335static void
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200336button_handler(struct widget *widget,
337 struct input *input, uint32_t time,
338 uint32_t button,
339 enum wl_pointer_button_state state, void *data)
340{
341 struct keyboard *keyboard = data;
342 struct rectangle allocation;
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200343 int32_t x, y;
344 int row, col;
345 unsigned int i;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200346
Jan Arne Petersencd997062012-11-18 19:06:44 +0100347 if (button != BTN_LEFT) {
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200348 return;
349 }
350
351 input_get_position(input, &x, &y);
352
353 widget_get_allocation(keyboard->widget, &allocation);
354 x -= allocation.x;
355 y -= allocation.y;
356
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200357 row = y / key_height;
358 col = x / key_width + row * columns;
359 for (i = 0; i < sizeof(keys) / sizeof(*keys); ++i) {
360 col -= keys[i].width;
Rob Bradford053fe762012-10-09 18:44:34 +0100361 if (col < 0) {
Jan Arne Petersencd997062012-11-18 19:06:44 +0100362 keyboard_handle_key(keyboard, time, &keys[i], input, state);
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200363 break;
Rob Bradford053fe762012-10-09 18:44:34 +0100364 }
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200365 }
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200366
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200367 widget_schedule_redraw(widget);
368}
369
370static void
Jan Arne Petersencb08f4d2012-09-09 23:08:40 +0200371input_method_context_surrounding_text(void *data,
372 struct input_method_context *context,
373 const char *text,
374 uint32_t cursor,
375 uint32_t anchor)
376{
377 fprintf(stderr, "Surrounding text updated: %s\n", text);
378}
379
Jan Arne Petersenc1e481e2012-09-09 23:08:46 +0200380static void
381input_method_context_reset(void *data,
382 struct input_method_context *context)
383{
384 struct virtual_keyboard *keyboard = data;
385
386 fprintf(stderr, "Reset pre-edit buffer\n");
387
388 if (strlen(keyboard->preedit_string)) {
389 input_method_context_preedit_string(context,
390 "",
391 0);
392 free(keyboard->preedit_string);
393 keyboard->preedit_string = strdup("");
394 }
395}
396
Jan Arne Petersencb08f4d2012-09-09 23:08:40 +0200397static const struct input_method_context_listener input_method_context_listener = {
398 input_method_context_surrounding_text,
Jan Arne Petersenc1e481e2012-09-09 23:08:46 +0200399 input_method_context_reset
Jan Arne Petersencb08f4d2012-09-09 23:08:40 +0200400};
401
402static void
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200403input_method_activate(void *data,
404 struct input_method *input_method,
405 struct input_method_context *context)
406{
407 struct virtual_keyboard *keyboard = data;
Jan Arne Petersencd997062012-11-18 19:06:44 +0100408 struct wl_array modifiers_map;
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200409
410 if (keyboard->context)
411 input_method_context_destroy(keyboard->context);
412
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200413 if (keyboard->preedit_string)
414 free(keyboard->preedit_string);
415
416 keyboard->preedit_string = strdup("");
417
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200418 keyboard->context = context;
Jan Arne Petersencb08f4d2012-09-09 23:08:40 +0200419 input_method_context_add_listener(context,
420 &input_method_context_listener,
421 keyboard);
Jan Arne Petersencd997062012-11-18 19:06:44 +0100422
423 wl_array_init(&modifiers_map);
424 keysym_modifiers_add(&modifiers_map, "Shift");
425 keysym_modifiers_add(&modifiers_map, "Control");
426 keysym_modifiers_add(&modifiers_map, "Mod1");
427 input_method_context_modifiers_map(context, &modifiers_map);
428 keyboard->keysym.shift_mask = keysym_modifiers_get_mask(&modifiers_map, "Shift");
429 wl_array_release(&modifiers_map);
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200430}
431
432static void
433input_method_deactivate(void *data,
434 struct input_method *input_method,
435 struct input_method_context *context)
436{
437 struct virtual_keyboard *keyboard = data;
438
439 if (!keyboard->context)
440 return;
441
442 input_method_context_destroy(keyboard->context);
443 keyboard->context = NULL;
444}
445
446static const struct input_method_listener input_method_listener = {
447 input_method_activate,
448 input_method_deactivate
449};
450
451static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400452global_handler(struct display *display, uint32_t name,
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200453 const char *interface, uint32_t version, void *data)
454{
455 struct virtual_keyboard *keyboard = data;
456
457 if (!strcmp(interface, "input_panel")) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400458 keyboard->input_panel =
459 display_bind(display, name, &input_panel_interface, 1);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200460 } else if (!strcmp(interface, "input_method")) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400461 keyboard->input_method =
462 display_bind(display, name,
463 &input_method_interface, 1);
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200464 input_method_add_listener(keyboard->input_method, &input_method_listener, keyboard);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200465 }
466}
467
468static void
469keyboard_create(struct output *output, struct virtual_keyboard *virtual_keyboard)
470{
471 struct keyboard *keyboard;
472
473 keyboard = malloc(sizeof *keyboard);
474 memset(keyboard, 0, sizeof *keyboard);
475
476 keyboard->keyboard = virtual_keyboard;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -0400477 keyboard->window = window_create_custom(virtual_keyboard->display);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200478 keyboard->widget = window_add_widget(keyboard->window, keyboard);
479
480 window_set_title(keyboard->window, "Virtual keyboard");
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200481 window_set_user_data(keyboard->window, keyboard);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200482
483 widget_set_redraw_handler(keyboard->widget, redraw_handler);
484 widget_set_resize_handler(keyboard->widget, resize_handler);
485 widget_set_button_handler(keyboard->widget, button_handler);
486
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200487 window_schedule_resize(keyboard->window,
488 columns * key_width,
489 rows * key_height);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200490
491 input_panel_set_surface(virtual_keyboard->input_panel,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -0400492 window_get_wl_surface(keyboard->window),
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200493 output_get_wl_output(output));
494}
495
496static void
497handle_output_configure(struct output *output, void *data)
498{
499 struct virtual_keyboard *virtual_keyboard = data;
500
501 /* skip existing outputs */
502 if (output_get_user_data(output))
503 return;
504
505 output_set_user_data(output, virtual_keyboard);
506
507 keyboard_create(output, virtual_keyboard);
508}
509
510int
511main(int argc, char *argv[])
512{
513 struct virtual_keyboard virtual_keyboard;
514
515 virtual_keyboard.display = display_create(argc, argv);
516 if (virtual_keyboard.display == NULL) {
517 fprintf(stderr, "failed to create display: %m\n");
518 return -1;
519 }
520
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200521 virtual_keyboard.context = NULL;
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200522 virtual_keyboard.preedit_string = NULL;
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200523
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200524 display_set_user_data(virtual_keyboard.display, &virtual_keyboard);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400525 display_set_global_handler(virtual_keyboard.display, global_handler);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200526 display_set_output_configure_handler(virtual_keyboard.display, handle_output_configure);
527
528 display_run(virtual_keyboard.display);
529
530 return 0;
531}