keyboard.c (18360B)
1 /* swc: libswc/keyboard.c 2 * 3 * Copyright (c) 2013-2020 Michael Forney 4 * 5 * Based in part upon input.c from weston, which is: 6 * 7 * Copyright © 2013 Intel Corporation 8 * 9 * Permission is hereby granted, free of charge, to any person obtaining a copy 10 * of this software and associated documentation files (the "Software"), to deal 11 * in the Software without restriction, including without limitation the rights 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 * copies of the Software, and to permit persons to whom the Software is 14 * furnished to do so, subject to the following conditions: 15 * 16 * The above copyright notice and this permission notice shall be included in 17 * all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 * SOFTWARE. 26 */ 27 28 #include "keyboard.h" 29 #include "compositor.h" 30 #include "internal.h" 31 #include "surface.h" 32 #include "xdg_shell.h" 33 #include "swc.h" 34 #include "util.h" 35 36 #include <assert.h> 37 #include <fcntl.h> 38 #include <limits.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <sys/mman.h> 43 #include <unistd.h> 44 #include <xkbcommon/xkbcommon.h> 45 46 /* Keyboard repeat rate (characters per second) and delay (ms) 47 * defaults, used if compositor didnt set them. 48 */ 49 __attribute__((visibility("default"))) 50 int32_t swc_repeat_rate = 40, swc_repeat_delay = 500; 51 52 static void 53 enter(struct input_focus_handler *handler, 54 struct wl_list *resources, 55 struct compositor_view *view) 56 { 57 struct keyboard *keyboard = 58 wl_container_of(handler, keyboard, focus_handler); 59 struct keyboard_modifier_state *state = &keyboard->modifier_state; 60 struct wl_resource *resource; 61 uint32_t serial; 62 63 serial = wl_display_next_serial(swc.display); 64 wl_resource_for_each(resource, resources) 65 { 66 wl_keyboard_send_modifiers(resource, 67 serial, 68 state->depressed, 69 state->latched, 70 state->locked, 71 state->group); 72 wl_keyboard_send_enter( 73 resource, serial, view->surface->resource, &keyboard->client_keys); 74 } 75 } 76 77 static void 78 leave(struct input_focus_handler *handler, 79 struct wl_list *resources, 80 struct compositor_view *view) 81 { 82 struct wl_resource *resource; 83 uint32_t serial; 84 85 serial = wl_display_next_serial(swc.display); 86 wl_resource_for_each(resource, resources) 87 wl_keyboard_send_leave(resource, serial, view->surface->resource); 88 } 89 90 static bool 91 client_handle_key(struct keyboard *keyboard, 92 uint32_t time, 93 struct key *key, 94 uint32_t state) 95 { 96 uint32_t *value; 97 struct wl_resource *resource; 98 99 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { 100 if (!(value = wl_array_add(&keyboard->client_keys, sizeof(*value)))) { 101 return false; 102 } 103 104 *value = key->press.value; 105 } else { 106 wl_array_for_each(value, &keyboard->client_keys) 107 { 108 if (*value == key->press.value) { 109 array_remove(&keyboard->client_keys, value, sizeof(*value)); 110 break; 111 } 112 } 113 } 114 115 wl_resource_for_each(resource, &keyboard->focus.active) 116 wl_keyboard_send_key( 117 resource, key->press.serial, time, key->press.value, state); 118 return true; 119 } 120 121 static bool 122 client_handle_modifiers(struct keyboard *keyboard, 123 const struct keyboard_modifier_state *state) 124 { 125 struct wl_resource *resource; 126 uint32_t serial; 127 128 if (wl_list_empty(&keyboard->focus.active)) { 129 return false; 130 } 131 132 serial = wl_display_next_serial(swc.display); 133 wl_resource_for_each(resource, &keyboard->focus.active) 134 wl_keyboard_send_modifiers(resource, 135 serial, 136 state->depressed, 137 state->latched, 138 state->locked, 139 state->group); 140 return true; 141 } 142 143 static bool 144 update_keymap(struct xkb *xkb) 145 { 146 char keymap_path[PATH_MAX]; 147 const char *keymap_directory; 148 char *keymap_string; 149 int ret; 150 151 if (!(keymap_directory = getenv("XDG_RUNTIME_DIR"))) { 152 keymap_directory = "/tmp"; 153 } 154 155 xkb->indices.ctrl = 156 xkb_keymap_mod_get_index(xkb->keymap.map, XKB_MOD_NAME_CTRL); 157 xkb->indices.alt = 158 xkb_keymap_mod_get_index(xkb->keymap.map, XKB_MOD_NAME_ALT); 159 xkb->indices.super = 160 xkb_keymap_mod_get_index(xkb->keymap.map, XKB_MOD_NAME_LOGO); 161 xkb->indices.shift = 162 xkb_keymap_mod_get_index(xkb->keymap.map, XKB_MOD_NAME_SHIFT); 163 164 /* In order to send the keymap to clients, we must first convert it to a 165 * string and then mmap it to a file. */ 166 keymap_string = 167 xkb_keymap_get_as_string(xkb->keymap.map, XKB_KEYMAP_FORMAT_TEXT_V1); 168 169 if (!keymap_string) { 170 WARNING("Could not get XKB keymap as a string\n"); 171 goto error0; 172 } 173 174 ret = snprintf(keymap_path, 175 sizeof(keymap_path), 176 "%s/swc-xkb-keymap-XXXXXX", 177 keymap_directory); 178 if (ret < 0 || (size_t)ret >= sizeof(keymap_path)) { 179 WARNING("Could not determine XKB keymap path\n"); 180 goto error1; 181 } 182 183 xkb->keymap.size = strlen(keymap_string) + 1; 184 xkb->keymap.fd = mkostemp(keymap_path, O_CLOEXEC); 185 186 if (xkb->keymap.fd == -1) { 187 WARNING("Could not create XKB keymap file\n"); 188 goto error1; 189 } 190 191 unlink(keymap_path); 192 193 #ifdef __linux__ 194 /* preallocate if available */ 195 if (posix_fallocate(xkb->keymap.fd, 0, xkb->keymap.size) != 0 && 196 ftruncate(xkb->keymap.fd, xkb->keymap.size) != 0) { 197 WARNING("Could not resize XKB keymap file\n"); 198 goto error2; 199 } 200 #else 201 /* otherwise, ftruncate() is fine */ 202 if (ftruncate(xkb->keymap.fd, xkb->keymap.size) != 0) { 203 WARNING("Could not resize XKB keymap file\n"); 204 goto error2; 205 } 206 #endif 207 208 xkb->keymap.area = mmap(NULL, 209 xkb->keymap.size, 210 PROT_READ | PROT_WRITE, 211 MAP_SHARED, 212 xkb->keymap.fd, 213 0); 214 215 if (xkb->keymap.area == MAP_FAILED) { 216 WARNING("Could not mmap XKB keymap string\n"); 217 goto error2; 218 } 219 220 strcpy(xkb->keymap.area, keymap_string); 221 free(keymap_string); 222 223 return true; 224 225 error2: 226 close(xkb->keymap.fd); 227 error1: 228 free(keymap_string); 229 error0: 230 return false; 231 } 232 233 /* The keymap in the file SWC_KEYMAP_FILE names, NULL when there is none. */ 234 static struct xkb_keymap * 235 keymap_from_file(struct xkb_context *context) 236 { 237 const char *path = getenv("SWC_KEYMAP_FILE"); 238 struct xkb_keymap *map; 239 FILE *file; 240 241 if (!path || !*path || !(file = fopen(path, "r"))) 242 return NULL; 243 map = xkb_keymap_new_from_file(context, file, XKB_KEYMAP_FORMAT_TEXT_V1, 0); 244 fclose(file); 245 if (!map) 246 WARNING("Could not compile the keymap in %s\n", path); 247 return map; 248 } 249 250 struct keyboard * 251 keyboard_create(struct xkb_rule_names *names) 252 { 253 struct keyboard *keyboard; 254 struct xkb *xkb; 255 256 keyboard = malloc(sizeof(*keyboard)); 257 if (!keyboard) { 258 goto error0; 259 } 260 261 xkb = &keyboard->xkb; 262 if (!(xkb->context = xkb_context_new(0))) { 263 ERROR("Could not create XKB context\n"); 264 goto error1; 265 } 266 267 /* a whole keymap from a file, when the session names one, needs no xkb 268 * data of the system's; names need the rules and files they name */ 269 if (!(xkb->keymap.map = keymap_from_file(xkb->context)) && 270 !(xkb->keymap.map = 271 xkb_keymap_new_from_names(xkb->context, names, 0))) { 272 ERROR("Could not create XKB keymap\n"); 273 goto error2; 274 } 275 276 if (!(xkb->state = xkb_state_new(xkb->keymap.map))) { 277 ERROR("Could not create XKB state\n"); 278 goto error3; 279 } 280 281 if (!update_keymap(xkb)) { 282 ERROR("Could not update XKB keymap\n"); 283 goto error4; 284 } 285 286 if (!input_focus_initialize(&keyboard->focus, &keyboard->focus_handler)) { 287 goto error4; 288 } 289 290 keyboard->modifier_state = (struct keyboard_modifier_state){0}; 291 keyboard->modifiers = 0; 292 keyboard->focus_handler.enter = &enter; 293 keyboard->focus_handler.leave = &leave; 294 keyboard->client_handler.key = &client_handle_key; 295 keyboard->client_handler.modifiers = &client_handle_modifiers; 296 wl_array_init(&keyboard->client_keys); 297 wl_array_init(&keyboard->keys); 298 wl_list_init(&keyboard->handlers); 299 wl_list_insert(&keyboard->handlers, &keyboard->client_handler.link); 300 301 return keyboard; 302 303 error4: 304 xkb_state_unref(keyboard->xkb.state); 305 error3: 306 xkb_keymap_unref(keyboard->xkb.keymap.map); 307 error2: 308 xkb_context_unref(keyboard->xkb.context); 309 error1: 310 free(keyboard); 311 error0: 312 return NULL; 313 } 314 315 void 316 keyboard_destroy(struct keyboard *keyboard) 317 { 318 wl_array_release(&keyboard->client_keys); 319 wl_array_release(&keyboard->keys); 320 input_focus_finalize(&keyboard->focus); 321 munmap(keyboard->xkb.keymap.area, keyboard->xkb.keymap.size); 322 close(keyboard->xkb.keymap.fd); 323 xkb_state_unref(keyboard->xkb.state); 324 xkb_keymap_unref(keyboard->xkb.keymap.map); 325 xkb_context_unref(keyboard->xkb.context); 326 free(keyboard); 327 } 328 329 static void update_modifiers(struct keyboard *keyboard); 330 331 /* A keymap made from names, put in place of the one in use. Clients are 332 * sent the new one on their keyboards and the keys held are let go, since 333 * what they meant belongs to the old layout. False, with the old keymap 334 * kept, when the names make none. */ 335 bool 336 keyboard_set_names(struct keyboard *keyboard, struct xkb_rule_names *names) 337 { 338 struct xkb next = keyboard->xkb; 339 struct wl_resource *resource; 340 341 if (!(next.keymap.map = 342 xkb_keymap_new_from_names(next.context, names, 0))) { 343 WARNING("Could not create XKB keymap\n"); 344 return false; 345 } 346 if (!(next.state = xkb_state_new(next.keymap.map))) { 347 xkb_keymap_unref(next.keymap.map); 348 return false; 349 } 350 if (!update_keymap(&next)) { 351 xkb_state_unref(next.state); 352 xkb_keymap_unref(next.keymap.map); 353 return false; 354 } 355 356 munmap(keyboard->xkb.keymap.area, keyboard->xkb.keymap.size); 357 close(keyboard->xkb.keymap.fd); 358 xkb_state_unref(keyboard->xkb.state); 359 xkb_keymap_unref(keyboard->xkb.keymap.map); 360 keyboard->xkb = next; 361 362 wl_resource_for_each(resource, &keyboard->focus.active) 363 wl_keyboard_send_keymap(resource, 364 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, 365 keyboard->xkb.keymap.fd, 366 keyboard->xkb.keymap.size); 367 wl_resource_for_each(resource, &keyboard->focus.inactive) 368 wl_keyboard_send_keymap(resource, 369 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, 370 keyboard->xkb.keymap.fd, 371 keyboard->xkb.keymap.size); 372 return keyboard_reset(keyboard); 373 } 374 375 /* The repeat rate and delay, told again to every client that can hear it. */ 376 void 377 keyboard_set_repeat(struct keyboard *keyboard, int32_t rate, int32_t delay) 378 { 379 struct wl_resource *resource; 380 381 swc_repeat_rate = rate; 382 swc_repeat_delay = delay; 383 wl_resource_for_each(resource, &keyboard->focus.active) 384 if (wl_resource_get_version(resource) >= 4) 385 wl_keyboard_send_repeat_info(resource, rate, delay); 386 wl_resource_for_each(resource, &keyboard->focus.inactive) 387 if (wl_resource_get_version(resource) >= 4) 388 wl_keyboard_send_repeat_info(resource, rate, delay); 389 } 390 391 bool 392 keyboard_reset(struct keyboard *keyboard) 393 { 394 struct key *key; 395 uint32_t time = get_time(); 396 struct xkb_state *state; 397 398 /* Send simulated key release events for all current key handlers. */ 399 wl_array_for_each(key, &keyboard->keys) 400 { 401 if (key->handler) { 402 key->press.serial = wl_display_next_serial(swc.display); 403 key->handler->key( 404 keyboard, time, key, WL_KEYBOARD_KEY_STATE_RELEASED); 405 /* Don't bother updating the XKB state because we will be resetting 406 * it later on and it is unlikely that a key handler cares about the 407 * keyboard state for release events. */ 408 } 409 } 410 411 /* We should have removed all the client keys by calling the client key 412 * handler. */ 413 assert(keyboard->client_keys.size == 0); 414 keyboard->keys.size = 0; 415 416 if (!(state = xkb_state_new(keyboard->xkb.keymap.map))) { 417 ERROR("Failed to allocate new XKB state\n"); 418 return false; 419 } 420 421 xkb_state_unref(keyboard->xkb.state); 422 keyboard->xkb.state = state; 423 /* tell clients the modifiers are released */ 424 update_modifiers(keyboard); 425 426 return true; 427 } 428 429 /** 430 * Sets the focus of the keyboard to the specified surface. 431 */ 432 void 433 keyboard_set_focus(struct keyboard *keyboard, struct compositor_view *view) 434 { 435 xdg_popups_focus_changed( 436 view && view->surface ? wl_resource_get_client(view->surface->resource) : NULL); 437 input_focus_set(&keyboard->focus, view); 438 } 439 440 static const struct wl_keyboard_interface keyboard_impl = { 441 .release = destroy_resource, 442 }; 443 444 static void 445 unbind(struct wl_resource *resource) 446 { 447 struct keyboard *keyboard = wl_resource_get_user_data(resource); 448 input_focus_remove_resource(&keyboard->focus, resource); 449 } 450 451 struct wl_resource * 452 keyboard_bind(struct keyboard *keyboard, 453 struct wl_client *client, 454 uint32_t version, 455 uint32_t id) 456 { 457 struct wl_resource *client_resource; 458 459 client_resource = 460 wl_resource_create(client, &wl_keyboard_interface, version, id); 461 if (!client_resource) { 462 return NULL; 463 } 464 wl_resource_set_implementation( 465 client_resource, &keyboard_impl, keyboard, &unbind); 466 467 wl_keyboard_send_keymap(client_resource, 468 WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, 469 keyboard->xkb.keymap.fd, 470 keyboard->xkb.keymap.size); 471 472 input_focus_add_resource(&keyboard->focus, client_resource); 473 474 if (version >= 4) { 475 wl_keyboard_send_repeat_info( 476 client_resource, swc_repeat_rate, swc_repeat_delay); 477 } 478 479 return client_resource; 480 } 481 482 /* update modifiers from xkb state, run handlers on change */ 483 static void 484 update_modifiers(struct keyboard *keyboard) 485 { 486 struct xkb *xkb = &keyboard->xkb; 487 struct keyboard_handler *handler; 488 struct keyboard_modifier_state modifier_state; 489 490 modifier_state.depressed = 491 xkb_state_serialize_mods(xkb->state, XKB_STATE_DEPRESSED); 492 modifier_state.latched = 493 xkb_state_serialize_mods(xkb->state, XKB_STATE_LATCHED); 494 modifier_state.locked = 495 xkb_state_serialize_mods(xkb->state, XKB_STATE_LOCKED); 496 modifier_state.group = 497 xkb_state_serialize_layout(xkb->state, XKB_STATE_LAYOUT_EFFECTIVE); 498 499 if (modifier_state.depressed != keyboard->modifier_state.depressed || 500 modifier_state.latched != keyboard->modifier_state.latched || 501 modifier_state.locked != keyboard->modifier_state.locked || 502 modifier_state.group != keyboard->modifier_state.group) { 503 uint32_t mods_active = 504 modifier_state.depressed | modifier_state.latched; 505 506 /* Update keyboard modifier state. */ 507 keyboard->modifier_state = modifier_state; 508 keyboard->modifiers = 0; 509 if (mods_active & (1 << keyboard->xkb.indices.ctrl)) { 510 keyboard->modifiers |= SWC_MOD_CTRL; 511 } 512 if (mods_active & (1 << keyboard->xkb.indices.alt)) { 513 keyboard->modifiers |= SWC_MOD_ALT; 514 } 515 if (mods_active & (1 << keyboard->xkb.indices.super)) { 516 keyboard->modifiers |= SWC_MOD_LOGO; 517 } 518 if (mods_active & (1 << keyboard->xkb.indices.shift)) { 519 keyboard->modifiers |= SWC_MOD_SHIFT; 520 } 521 522 /* Run any modifier handlers. */ 523 wl_list_for_each(handler, &keyboard->handlers, link) 524 { 525 if (handler->modifiers) { 526 handler->modifiers(keyboard, &modifier_state); 527 } 528 } 529 } 530 } 531 532 void 533 keyboard_handle_key(struct keyboard *keyboard, 534 uint32_t time, 535 uint32_t value, 536 uint32_t state) 537 { 538 struct key *key; 539 enum xkb_key_direction direction; 540 struct xkb *xkb = &keyboard->xkb; 541 struct keyboard_handler *handler; 542 uint32_t serial; 543 544 serial = wl_display_next_serial(swc.display); 545 546 /* First handle key release events associated with a particular handler. */ 547 wl_array_for_each(key, &keyboard->keys) 548 { 549 if (key->press.value == value) { 550 /* Ignore repeat events. */ 551 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { 552 return; 553 } 554 555 if (key->handler) { 556 key->press.serial = serial; 557 key->handler->key(keyboard, time, key, state); 558 } 559 560 array_remove(&keyboard->keys, key, sizeof(*key)); 561 goto update_xkb_state; 562 } 563 } 564 565 /* If we get a unpaired release event, just ignore it. */ 566 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) { 567 return; 568 } 569 570 if (!(key = wl_array_add(&keyboard->keys, sizeof(*key)))) { 571 goto update_xkb_state; 572 } 573 574 key->press.value = value; 575 key->press.serial = serial; 576 key->handler = NULL; 577 578 /* Go through handlers to see if any will accept this key event. */ 579 wl_list_for_each(handler, &keyboard->handlers, link) 580 { 581 if (handler->key && handler->key(keyboard, time, key, state)) { 582 key->handler = handler; 583 break; 584 } 585 } 586 587 /* Update XKB state. */ 588 update_xkb_state: 589 direction = 590 state == WL_KEYBOARD_KEY_STATE_PRESSED ? XKB_KEY_DOWN : XKB_KEY_UP; 591 xkb_state_update_key(xkb->state, XKB_KEY(value), direction); 592 593 update_modifiers(keyboard); 594 } 595 596 void 597 keyboard_handle_modifiers(struct keyboard *keyboard, uint32_t depressed, 598 uint32_t latched, uint32_t locked, uint32_t group) 599 { 600 xkb_state_update_mask(keyboard->xkb.state, depressed, latched, locked, 0, 601 0, group); 602 update_modifiers(keyboard); 603 } 604 605 bool 606 keyboard_set_keymap(struct keyboard *keyboard, struct xkb_keymap *map) 607 { 608 struct xkb old = keyboard->xkb; 609 struct wl_resource *resource; 610 611 keyboard->xkb.keymap.map = xkb_keymap_ref(map); 612 if (!update_keymap(&keyboard->xkb)) { 613 xkb_keymap_unref(map); 614 keyboard->xkb = old; 615 return false; 616 } 617 618 /* held keys belong to the old keymap */ 619 if (!keyboard_reset(keyboard)) { 620 return false; 621 } 622 munmap(old.keymap.area, old.keymap.size); 623 close(old.keymap.fd); 624 xkb_keymap_unref(old.keymap.map); 625 626 wl_resource_for_each(resource, &keyboard->focus.active) 627 wl_keyboard_send_keymap(resource, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, 628 keyboard->xkb.keymap.fd, 629 keyboard->xkb.keymap.size); 630 wl_resource_for_each(resource, &keyboard->focus.inactive) 631 wl_keyboard_send_keymap(resource, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, 632 keyboard->xkb.keymap.fd, 633 keyboard->xkb.keymap.size); 634 635 return true; 636 }