blob: 588ef785f1e327400c841fa1f0a7800c7c2595fa [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,
50 keytype_tab
51};
52
53struct key {
54 enum key_type key_type;
55
56 char *label;
57 char *alt;
58
59 unsigned int width;
60};
61
62static 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},
74
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},
86
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},
98
99 { keytype_symbols, "?123", "?123", 2},
100 { keytype_space, "", "", 8},
101 { keytype_symbols, "?123", "?123", 2}
102};
103
104static const unsigned int columns = 12;
105static const unsigned int rows = 4;
106
107static const double key_width = 60;
108static const double key_height = 50;
109
110enum keyboard_state {
111 keyboardstate_default,
112 keyboardstate_uppercase
113};
114
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200115struct keyboard {
116 struct virtual_keyboard *keyboard;
117 struct window *window;
118 struct widget *widget;
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200119
120 enum keyboard_state state;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200121};
122
123static void
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200124draw_key(const struct key *key,
125 cairo_t *cr,
126 enum keyboard_state state,
127 unsigned int row,
128 unsigned int col)
129{
130 const char *label;
131 cairo_text_extents_t extents;
132
133 cairo_save(cr);
134 cairo_rectangle(cr,
135 col * key_width, row * key_height,
136 key->width * key_width, key_height);
137 cairo_clip(cr);
138
139 /* Paint frame */
140 cairo_rectangle(cr,
141 col * key_width, row * key_height,
142 key->width * key_width, key_height);
143 cairo_set_line_width(cr, 3);
144 cairo_stroke(cr);
145
146 /* Paint text */
147 label = state == keyboardstate_default ? key->label : key->alt;
148 cairo_text_extents(cr, label, &extents);
149
150 cairo_translate(cr,
151 col * key_width,
152 row * key_height);
153 cairo_translate(cr,
154 (key->width * key_width - extents.width) / 2,
155 (key_height - extents.y_bearing) / 2);
156 cairo_show_text(cr, label);
157
158 cairo_restore(cr);
159}
160
161static void
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200162redraw_handler(struct widget *widget, void *data)
163{
164 struct keyboard *keyboard = data;
165 cairo_surface_t *surface;
166 struct rectangle allocation;
167 cairo_t *cr;
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200168 unsigned int i;
169 unsigned int row = 0, col = 0;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200170
171 surface = window_get_surface(keyboard->window);
172 widget_get_allocation(keyboard->widget, &allocation);
173
174 cr = cairo_create(surface);
175 cairo_rectangle(cr, allocation.x, allocation.y, allocation.width, allocation.height);
176 cairo_clip(cr);
177
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200178 cairo_select_font_face(cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
179 cairo_set_font_size(cr, 16);
180
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200181 cairo_translate(cr, allocation.x, allocation.y);
182
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200183 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200184 cairo_set_source_rgba(cr, 1, 1, 1, 0.75);
185 cairo_rectangle(cr, 0, 0, columns * key_width, rows * key_height);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200186 cairo_paint(cr);
187
188 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
189
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200190 for (i = 0; i < sizeof(keys) / sizeof(*keys); ++i) {
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200191 cairo_set_source_rgb(cr, 0, 0, 0);
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200192 draw_key(&keys[i], cr, keyboard->state, row, col);
193 col += keys[i].width;
194 if (col >= columns) {
195 row += 1;
196 col = 0;
197 }
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200198 }
199
200 cairo_destroy(cr);
201 cairo_surface_destroy(surface);
202}
203
204static void
205resize_handler(struct widget *widget,
206 int32_t width, int32_t height, void *data)
207{
208 /* struct keyboard *keyboard = data; */
209}
210
211static void
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200212keyboard_handle_key(struct keyboard *keyboard, const struct key *key)
213{
214 const char *label = keyboard->state == keyboardstate_default ? key->label : key->alt;
215
216 switch (key->key_type) {
217 case keytype_default:
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200218 keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
219 label);
220 input_method_context_preedit_string(keyboard->keyboard->context,
221 keyboard->keyboard->preedit_string,
222 strlen(keyboard->keyboard->preedit_string));
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200223 break;
224 case keytype_backspace:
Jan Arne Petersene202bae2012-09-09 23:08:44 +0200225 if (strlen(keyboard->keyboard->preedit_string) == 0) {
226 input_method_context_delete_surrounding_text(keyboard->keyboard->context,
227 -1, 1);
228 }
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200229 break;
230 case keytype_enter:
231 break;
232 case keytype_space:
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200233 keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string,
234 " ");
235 input_method_context_preedit_string(keyboard->keyboard->context,
236 "",
237 0);
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200238 input_method_context_commit_string(keyboard->keyboard->context,
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200239 keyboard->keyboard->preedit_string,
240 strlen(keyboard->keyboard->preedit_string));
241 free(keyboard->keyboard->preedit_string);
242 keyboard->keyboard->preedit_string = strdup("");
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200243 break;
244 case keytype_switch:
245 if (keyboard->state == keyboardstate_default)
246 keyboard->state = keyboardstate_uppercase;
247 else
248 keyboard->state = keyboardstate_default;
249 break;
250 case keytype_symbols:
251 break;
252 case keytype_tab:
253 break;
254 }
255}
256
257static void
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200258button_handler(struct widget *widget,
259 struct input *input, uint32_t time,
260 uint32_t button,
261 enum wl_pointer_button_state state, void *data)
262{
263 struct keyboard *keyboard = data;
264 struct rectangle allocation;
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200265 int32_t x, y;
266 int row, col;
267 unsigned int i;
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200268
269 if (state != WL_POINTER_BUTTON_STATE_PRESSED || button != BTN_LEFT) {
270 return;
271 }
272
273 input_get_position(input, &x, &y);
274
275 widget_get_allocation(keyboard->widget, &allocation);
276 x -= allocation.x;
277 y -= allocation.y;
278
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200279 row = y / key_height;
280 col = x / key_width + row * columns;
281 for (i = 0; i < sizeof(keys) / sizeof(*keys); ++i) {
282 col -= keys[i].width;
283 if (col < 0)
284 break;
285 }
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200286
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200287 keyboard_handle_key(keyboard, &keys[i]);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200288
289 widget_schedule_redraw(widget);
290}
291
292static void
Jan Arne Petersencb08f4d2012-09-09 23:08:40 +0200293input_method_context_surrounding_text(void *data,
294 struct input_method_context *context,
295 const char *text,
296 uint32_t cursor,
297 uint32_t anchor)
298{
299 fprintf(stderr, "Surrounding text updated: %s\n", text);
300}
301
302static const struct input_method_context_listener input_method_context_listener = {
303 input_method_context_surrounding_text,
304};
305
306static void
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200307input_method_activate(void *data,
308 struct input_method *input_method,
309 struct input_method_context *context)
310{
311 struct virtual_keyboard *keyboard = data;
312
313 if (keyboard->context)
314 input_method_context_destroy(keyboard->context);
315
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200316 if (keyboard->preedit_string)
317 free(keyboard->preedit_string);
318
319 keyboard->preedit_string = strdup("");
320
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200321 keyboard->context = context;
Jan Arne Petersencb08f4d2012-09-09 23:08:40 +0200322 input_method_context_add_listener(context,
323 &input_method_context_listener,
324 keyboard);
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200325}
326
327static void
328input_method_deactivate(void *data,
329 struct input_method *input_method,
330 struct input_method_context *context)
331{
332 struct virtual_keyboard *keyboard = data;
333
334 if (!keyboard->context)
335 return;
336
337 input_method_context_destroy(keyboard->context);
338 keyboard->context = NULL;
339}
340
341static const struct input_method_listener input_method_listener = {
342 input_method_activate,
343 input_method_deactivate
344};
345
346static void
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200347global_handler(struct wl_display *display, uint32_t id,
348 const char *interface, uint32_t version, void *data)
349{
350 struct virtual_keyboard *keyboard = data;
351
352 if (!strcmp(interface, "input_panel")) {
353 keyboard->input_panel = wl_display_bind(display, id, &input_panel_interface);
354 } else if (!strcmp(interface, "input_method")) {
355 keyboard->input_method = wl_display_bind(display, id, &input_method_interface);
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200356 input_method_add_listener(keyboard->input_method, &input_method_listener, keyboard);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200357 }
358}
359
360static void
361keyboard_create(struct output *output, struct virtual_keyboard *virtual_keyboard)
362{
363 struct keyboard *keyboard;
364
365 keyboard = malloc(sizeof *keyboard);
366 memset(keyboard, 0, sizeof *keyboard);
367
368 keyboard->keyboard = virtual_keyboard;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -0400369 keyboard->window = window_create_custom(virtual_keyboard->display);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200370 keyboard->widget = window_add_widget(keyboard->window, keyboard);
371
372 window_set_title(keyboard->window, "Virtual keyboard");
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200373 window_set_user_data(keyboard->window, keyboard);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200374
375 widget_set_redraw_handler(keyboard->widget, redraw_handler);
376 widget_set_resize_handler(keyboard->widget, resize_handler);
377 widget_set_button_handler(keyboard->widget, button_handler);
378
Jan Arne Petersen892f1c32012-09-09 23:08:42 +0200379 window_schedule_resize(keyboard->window,
380 columns * key_width,
381 rows * key_height);
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200382
383 input_panel_set_surface(virtual_keyboard->input_panel,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -0400384 window_get_wl_surface(keyboard->window),
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200385 output_get_wl_output(output));
386}
387
388static void
389handle_output_configure(struct output *output, void *data)
390{
391 struct virtual_keyboard *virtual_keyboard = data;
392
393 /* skip existing outputs */
394 if (output_get_user_data(output))
395 return;
396
397 output_set_user_data(output, virtual_keyboard);
398
399 keyboard_create(output, virtual_keyboard);
400}
401
402int
403main(int argc, char *argv[])
404{
405 struct virtual_keyboard virtual_keyboard;
406
407 virtual_keyboard.display = display_create(argc, argv);
408 if (virtual_keyboard.display == NULL) {
409 fprintf(stderr, "failed to create display: %m\n");
410 return -1;
411 }
412
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200413 virtual_keyboard.context = NULL;
Jan Arne Petersen43f4aa82012-09-09 23:08:43 +0200414 virtual_keyboard.preedit_string = NULL;
Jan Arne Petersen620cd622012-09-09 23:08:32 +0200415
Jan Arne Petersencba9e472012-06-21 21:52:19 +0200416 wl_display_add_global_listener(display_get_display(virtual_keyboard.display),
417 global_handler, &virtual_keyboard);
418
419 display_set_user_data(virtual_keyboard.display, &virtual_keyboard);
420 display_set_output_configure_handler(virtual_keyboard.display, handle_output_configure);
421
422 display_run(virtual_keyboard.display);
423
424 return 0;
425}