Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Openismus GmbH |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 3 | * Copyright © 2012 Intel Corporation |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 4 | * |
| 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 Petersen | 30b66ef | 2012-09-09 23:08:41 +0200 | [diff] [blame] | 32 | #include "input-method-client-protocol.h" |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 33 | #include "desktop-shell-client-protocol.h" |
| 34 | |
| 35 | struct virtual_keyboard { |
| 36 | struct input_panel *input_panel; |
| 37 | struct input_method *input_method; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 38 | struct input_method_context *context; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 39 | struct display *display; |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 40 | char *preedit_string; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 41 | }; |
| 42 | |
Jan Arne Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 43 | enum key_type { |
| 44 | keytype_default, |
| 45 | keytype_backspace, |
| 46 | keytype_enter, |
| 47 | keytype_space, |
| 48 | keytype_switch, |
| 49 | keytype_symbols, |
Jan Arne Petersen | 8aba11d | 2012-09-17 15:28:07 +0200 | [diff] [blame] | 50 | keytype_tab, |
| 51 | keytype_arrow_up, |
| 52 | keytype_arrow_left, |
| 53 | keytype_arrow_right, |
| 54 | keytype_arrow_down |
Jan Arne Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | struct key { |
| 58 | enum key_type key_type; |
| 59 | |
| 60 | char *label; |
| 61 | char *alt; |
| 62 | |
| 63 | unsigned int width; |
| 64 | }; |
| 65 | |
| 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}, |
| 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 Petersen | 8aba11d | 2012-09-17 15:28:07 +0200 | [diff] [blame] | 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} |
Jan Arne Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | static const unsigned int columns = 12; |
| 113 | static const unsigned int rows = 4; |
| 114 | |
| 115 | static const double key_width = 60; |
| 116 | static const double key_height = 50; |
| 117 | |
| 118 | enum keyboard_state { |
| 119 | keyboardstate_default, |
| 120 | keyboardstate_uppercase |
| 121 | }; |
| 122 | |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 123 | struct keyboard { |
| 124 | struct virtual_keyboard *keyboard; |
| 125 | struct window *window; |
| 126 | struct widget *widget; |
Jan Arne Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 127 | |
| 128 | enum keyboard_state state; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | static void |
Jan Arne Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 132 | draw_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 | |
| 169 | static void |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 170 | redraw_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 Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 176 | unsigned int i; |
| 177 | unsigned int row = 0, col = 0; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 178 | |
| 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 Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 186 | cairo_select_font_face(cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD); |
| 187 | cairo_set_font_size(cr, 16); |
| 188 | |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 189 | cairo_translate(cr, allocation.x, allocation.y); |
| 190 | |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 191 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
Jan Arne Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 192 | cairo_set_source_rgba(cr, 1, 1, 1, 0.75); |
| 193 | cairo_rectangle(cr, 0, 0, columns * key_width, rows * key_height); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 194 | cairo_paint(cr); |
| 195 | |
| 196 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 197 | |
Jan Arne Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 198 | for (i = 0; i < sizeof(keys) / sizeof(*keys); ++i) { |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 199 | cairo_set_source_rgb(cr, 0, 0, 0); |
Jan Arne Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 200 | 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 Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | cairo_destroy(cr); |
| 209 | cairo_surface_destroy(surface); |
| 210 | } |
| 211 | |
| 212 | static void |
| 213 | resize_handler(struct widget *widget, |
| 214 | int32_t width, int32_t height, void *data) |
| 215 | { |
| 216 | /* struct keyboard *keyboard = data; */ |
| 217 | } |
| 218 | |
| 219 | static void |
Jan Arne Petersen | 5ec05fb | 2012-11-05 03:26:42 +0100 | [diff] [blame^] | 220 | virtual_keyboard_commit_preedit(struct virtual_keyboard *keyboard) |
| 221 | { |
| 222 | if (!keyboard->preedit_string || |
| 223 | strlen(keyboard->preedit_string) == 0) |
| 224 | return; |
| 225 | |
| 226 | input_method_context_preedit_string(keyboard->context, |
| 227 | "", |
| 228 | 0); |
| 229 | input_method_context_commit_string(keyboard->context, |
| 230 | keyboard->preedit_string, |
| 231 | strlen(keyboard->preedit_string)); |
| 232 | free(keyboard->preedit_string); |
| 233 | keyboard->preedit_string = strdup(""); |
| 234 | } |
| 235 | |
| 236 | static void |
Jan Arne Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 237 | keyboard_handle_key(struct keyboard *keyboard, const struct key *key) |
| 238 | { |
| 239 | const char *label = keyboard->state == keyboardstate_default ? key->label : key->alt; |
| 240 | |
| 241 | switch (key->key_type) { |
| 242 | case keytype_default: |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 243 | keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string, |
| 244 | label); |
| 245 | input_method_context_preedit_string(keyboard->keyboard->context, |
| 246 | keyboard->keyboard->preedit_string, |
| 247 | strlen(keyboard->keyboard->preedit_string)); |
Jan Arne Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 248 | break; |
| 249 | case keytype_backspace: |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 250 | if (strlen(keyboard->keyboard->preedit_string) == 0) { |
| 251 | input_method_context_delete_surrounding_text(keyboard->keyboard->context, |
| 252 | -1, 1); |
Jan Arne Petersen | 633c83d | 2012-09-17 15:28:08 +0200 | [diff] [blame] | 253 | } else { |
| 254 | keyboard->keyboard->preedit_string[strlen(keyboard->keyboard->preedit_string) - 1] = '\0'; |
| 255 | input_method_context_preedit_string(keyboard->keyboard->context, |
| 256 | keyboard->keyboard->preedit_string, |
| 257 | strlen(keyboard->keyboard->preedit_string)); |
Jan Arne Petersen | e202bae | 2012-09-09 23:08:44 +0200 | [diff] [blame] | 258 | } |
Jan Arne Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 259 | break; |
| 260 | case keytype_enter: |
Jan Arne Petersen | 5ec05fb | 2012-11-05 03:26:42 +0100 | [diff] [blame^] | 261 | virtual_keyboard_commit_preedit(keyboard->keyboard); |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 262 | input_method_context_key(keyboard->keyboard->context, |
| 263 | XKB_KEY_KP_Enter, WL_KEYBOARD_KEY_STATE_PRESSED); |
Jan Arne Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 264 | break; |
| 265 | case keytype_space: |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 266 | keyboard->keyboard->preedit_string = strcat(keyboard->keyboard->preedit_string, |
| 267 | " "); |
Jan Arne Petersen | 5ec05fb | 2012-11-05 03:26:42 +0100 | [diff] [blame^] | 268 | virtual_keyboard_commit_preedit(keyboard->keyboard); |
Jan Arne Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 269 | break; |
| 270 | case keytype_switch: |
| 271 | if (keyboard->state == keyboardstate_default) |
| 272 | keyboard->state = keyboardstate_uppercase; |
| 273 | else |
| 274 | keyboard->state = keyboardstate_default; |
| 275 | break; |
| 276 | case keytype_symbols: |
| 277 | break; |
| 278 | case keytype_tab: |
Jan Arne Petersen | 5ec05fb | 2012-11-05 03:26:42 +0100 | [diff] [blame^] | 279 | virtual_keyboard_commit_preedit(keyboard->keyboard); |
Jan Arne Petersen | ce8a443 | 2012-09-09 23:08:45 +0200 | [diff] [blame] | 280 | input_method_context_key(keyboard->keyboard->context, |
| 281 | XKB_KEY_Tab, WL_KEYBOARD_KEY_STATE_PRESSED); |
Jan Arne Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 282 | break; |
Jan Arne Petersen | 8aba11d | 2012-09-17 15:28:07 +0200 | [diff] [blame] | 283 | case keytype_arrow_up: |
Jan Arne Petersen | 5ec05fb | 2012-11-05 03:26:42 +0100 | [diff] [blame^] | 284 | virtual_keyboard_commit_preedit(keyboard->keyboard); |
Jan Arne Petersen | 8aba11d | 2012-09-17 15:28:07 +0200 | [diff] [blame] | 285 | input_method_context_key(keyboard->keyboard->context, |
| 286 | XKB_KEY_Up, WL_KEYBOARD_KEY_STATE_PRESSED); |
| 287 | break; |
| 288 | case keytype_arrow_left: |
Jan Arne Petersen | 5ec05fb | 2012-11-05 03:26:42 +0100 | [diff] [blame^] | 289 | virtual_keyboard_commit_preedit(keyboard->keyboard); |
Jan Arne Petersen | 8aba11d | 2012-09-17 15:28:07 +0200 | [diff] [blame] | 290 | input_method_context_key(keyboard->keyboard->context, |
| 291 | XKB_KEY_Left, WL_KEYBOARD_KEY_STATE_PRESSED); |
| 292 | break; |
| 293 | case keytype_arrow_right: |
Jan Arne Petersen | 5ec05fb | 2012-11-05 03:26:42 +0100 | [diff] [blame^] | 294 | virtual_keyboard_commit_preedit(keyboard->keyboard); |
Jan Arne Petersen | 8aba11d | 2012-09-17 15:28:07 +0200 | [diff] [blame] | 295 | input_method_context_key(keyboard->keyboard->context, |
| 296 | XKB_KEY_Right, WL_KEYBOARD_KEY_STATE_PRESSED); |
| 297 | break; |
| 298 | case keytype_arrow_down: |
Jan Arne Petersen | 5ec05fb | 2012-11-05 03:26:42 +0100 | [diff] [blame^] | 299 | virtual_keyboard_commit_preedit(keyboard->keyboard); |
Jan Arne Petersen | 8aba11d | 2012-09-17 15:28:07 +0200 | [diff] [blame] | 300 | input_method_context_key(keyboard->keyboard->context, |
| 301 | XKB_KEY_Down, WL_KEYBOARD_KEY_STATE_PRESSED); |
| 302 | break; |
Jan Arne Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
| 306 | static void |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 307 | button_handler(struct widget *widget, |
| 308 | struct input *input, uint32_t time, |
| 309 | uint32_t button, |
| 310 | enum wl_pointer_button_state state, void *data) |
| 311 | { |
| 312 | struct keyboard *keyboard = data; |
| 313 | struct rectangle allocation; |
Jan Arne Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 314 | int32_t x, y; |
| 315 | int row, col; |
| 316 | unsigned int i; |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 317 | |
| 318 | if (state != WL_POINTER_BUTTON_STATE_PRESSED || button != BTN_LEFT) { |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | input_get_position(input, &x, &y); |
| 323 | |
| 324 | widget_get_allocation(keyboard->widget, &allocation); |
| 325 | x -= allocation.x; |
| 326 | y -= allocation.y; |
| 327 | |
Jan Arne Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 328 | row = y / key_height; |
| 329 | col = x / key_width + row * columns; |
| 330 | for (i = 0; i < sizeof(keys) / sizeof(*keys); ++i) { |
| 331 | col -= keys[i].width; |
Rob Bradford | 053fe76 | 2012-10-09 18:44:34 +0100 | [diff] [blame] | 332 | if (col < 0) { |
| 333 | keyboard_handle_key(keyboard, &keys[i]); |
Jan Arne Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 334 | break; |
Rob Bradford | 053fe76 | 2012-10-09 18:44:34 +0100 | [diff] [blame] | 335 | } |
Jan Arne Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 336 | } |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 337 | |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 338 | widget_schedule_redraw(widget); |
| 339 | } |
| 340 | |
| 341 | static void |
Jan Arne Petersen | cb08f4d | 2012-09-09 23:08:40 +0200 | [diff] [blame] | 342 | input_method_context_surrounding_text(void *data, |
| 343 | struct input_method_context *context, |
| 344 | const char *text, |
| 345 | uint32_t cursor, |
| 346 | uint32_t anchor) |
| 347 | { |
| 348 | fprintf(stderr, "Surrounding text updated: %s\n", text); |
| 349 | } |
| 350 | |
Jan Arne Petersen | c1e481e | 2012-09-09 23:08:46 +0200 | [diff] [blame] | 351 | static void |
| 352 | input_method_context_reset(void *data, |
| 353 | struct input_method_context *context) |
| 354 | { |
| 355 | struct virtual_keyboard *keyboard = data; |
| 356 | |
| 357 | fprintf(stderr, "Reset pre-edit buffer\n"); |
| 358 | |
| 359 | if (strlen(keyboard->preedit_string)) { |
| 360 | input_method_context_preedit_string(context, |
| 361 | "", |
| 362 | 0); |
| 363 | free(keyboard->preedit_string); |
| 364 | keyboard->preedit_string = strdup(""); |
| 365 | } |
| 366 | } |
| 367 | |
Jan Arne Petersen | cb08f4d | 2012-09-09 23:08:40 +0200 | [diff] [blame] | 368 | static const struct input_method_context_listener input_method_context_listener = { |
| 369 | input_method_context_surrounding_text, |
Jan Arne Petersen | c1e481e | 2012-09-09 23:08:46 +0200 | [diff] [blame] | 370 | input_method_context_reset |
Jan Arne Petersen | cb08f4d | 2012-09-09 23:08:40 +0200 | [diff] [blame] | 371 | }; |
| 372 | |
| 373 | static void |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 374 | input_method_activate(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 | input_method_context_destroy(keyboard->context); |
| 382 | |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 383 | if (keyboard->preedit_string) |
| 384 | free(keyboard->preedit_string); |
| 385 | |
| 386 | keyboard->preedit_string = strdup(""); |
| 387 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 388 | keyboard->context = context; |
Jan Arne Petersen | cb08f4d | 2012-09-09 23:08:40 +0200 | [diff] [blame] | 389 | input_method_context_add_listener(context, |
| 390 | &input_method_context_listener, |
| 391 | keyboard); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | static void |
| 395 | input_method_deactivate(void *data, |
| 396 | struct input_method *input_method, |
| 397 | struct input_method_context *context) |
| 398 | { |
| 399 | struct virtual_keyboard *keyboard = data; |
| 400 | |
| 401 | if (!keyboard->context) |
| 402 | return; |
| 403 | |
| 404 | input_method_context_destroy(keyboard->context); |
| 405 | keyboard->context = NULL; |
| 406 | } |
| 407 | |
| 408 | static const struct input_method_listener input_method_listener = { |
| 409 | input_method_activate, |
| 410 | input_method_deactivate |
| 411 | }; |
| 412 | |
| 413 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 414 | global_handler(struct display *display, uint32_t name, |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 415 | const char *interface, uint32_t version, void *data) |
| 416 | { |
| 417 | struct virtual_keyboard *keyboard = data; |
| 418 | |
| 419 | if (!strcmp(interface, "input_panel")) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 420 | keyboard->input_panel = |
| 421 | display_bind(display, name, &input_panel_interface, 1); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 422 | } else if (!strcmp(interface, "input_method")) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 423 | keyboard->input_method = |
| 424 | display_bind(display, name, |
| 425 | &input_method_interface, 1); |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 426 | input_method_add_listener(keyboard->input_method, &input_method_listener, keyboard); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
| 430 | static void |
| 431 | keyboard_create(struct output *output, struct virtual_keyboard *virtual_keyboard) |
| 432 | { |
| 433 | struct keyboard *keyboard; |
| 434 | |
| 435 | keyboard = malloc(sizeof *keyboard); |
| 436 | memset(keyboard, 0, sizeof *keyboard); |
| 437 | |
| 438 | keyboard->keyboard = virtual_keyboard; |
Kristian Høgsberg | 0636ac3 | 2012-06-27 10:22:15 -0400 | [diff] [blame] | 439 | keyboard->window = window_create_custom(virtual_keyboard->display); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 440 | keyboard->widget = window_add_widget(keyboard->window, keyboard); |
| 441 | |
| 442 | window_set_title(keyboard->window, "Virtual keyboard"); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 443 | window_set_user_data(keyboard->window, keyboard); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 444 | |
| 445 | widget_set_redraw_handler(keyboard->widget, redraw_handler); |
| 446 | widget_set_resize_handler(keyboard->widget, resize_handler); |
| 447 | widget_set_button_handler(keyboard->widget, button_handler); |
| 448 | |
Jan Arne Petersen | 892f1c3 | 2012-09-09 23:08:42 +0200 | [diff] [blame] | 449 | window_schedule_resize(keyboard->window, |
| 450 | columns * key_width, |
| 451 | rows * key_height); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 452 | |
| 453 | input_panel_set_surface(virtual_keyboard->input_panel, |
Kristian Høgsberg | 0636ac3 | 2012-06-27 10:22:15 -0400 | [diff] [blame] | 454 | window_get_wl_surface(keyboard->window), |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 455 | output_get_wl_output(output)); |
| 456 | } |
| 457 | |
| 458 | static void |
| 459 | handle_output_configure(struct output *output, void *data) |
| 460 | { |
| 461 | struct virtual_keyboard *virtual_keyboard = data; |
| 462 | |
| 463 | /* skip existing outputs */ |
| 464 | if (output_get_user_data(output)) |
| 465 | return; |
| 466 | |
| 467 | output_set_user_data(output, virtual_keyboard); |
| 468 | |
| 469 | keyboard_create(output, virtual_keyboard); |
| 470 | } |
| 471 | |
| 472 | int |
| 473 | main(int argc, char *argv[]) |
| 474 | { |
| 475 | struct virtual_keyboard virtual_keyboard; |
| 476 | |
| 477 | virtual_keyboard.display = display_create(argc, argv); |
| 478 | if (virtual_keyboard.display == NULL) { |
| 479 | fprintf(stderr, "failed to create display: %m\n"); |
| 480 | return -1; |
| 481 | } |
| 482 | |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 483 | virtual_keyboard.context = NULL; |
Jan Arne Petersen | 43f4aa8 | 2012-09-09 23:08:43 +0200 | [diff] [blame] | 484 | virtual_keyboard.preedit_string = NULL; |
Jan Arne Petersen | 620cd62 | 2012-09-09 23:08:32 +0200 | [diff] [blame] | 485 | |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 486 | display_set_user_data(virtual_keyboard.display, &virtual_keyboard); |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 487 | display_set_global_handler(virtual_keyboard.display, global_handler); |
Jan Arne Petersen | cba9e47 | 2012-06-21 21:52:19 +0200 | [diff] [blame] | 488 | display_set_output_configure_handler(virtual_keyboard.display, handle_output_configure); |
| 489 | |
| 490 | display_run(virtual_keyboard.display); |
| 491 | |
| 492 | return 0; |
| 493 | } |