2018-07-07 17:56:37 +02:00
|
|
|
#include "util/array.h"
|
2018-02-12 21:29:23 +01:00
|
|
|
#include <assert.h>
|
2017-06-09 23:31:21 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2019-07-27 10:53:54 +02:00
|
|
|
#include <wayland-server-core.h>
|
2017-06-21 16:27:45 +02:00
|
|
|
#include <wlr/interfaces/wlr_keyboard.h>
|
2018-02-12 21:29:23 +01:00
|
|
|
#include <wlr/types/wlr_keyboard.h>
|
2017-09-24 20:12:56 +02:00
|
|
|
#include <wlr/util/log.h>
|
2020-05-31 23:10:15 +02:00
|
|
|
#include "types/wlr_keyboard.h"
|
2018-02-12 19:45:58 +01:00
|
|
|
#include "util/signal.h"
|
2017-09-24 20:12:56 +02:00
|
|
|
|
2020-05-31 23:10:15 +02:00
|
|
|
void keyboard_led_update(struct wlr_keyboard *keyboard) {
|
2018-01-06 15:06:19 +01:00
|
|
|
if (keyboard->xkb_state == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-24 20:12:56 +02:00
|
|
|
uint32_t leds = 0;
|
2017-10-02 20:21:47 +02:00
|
|
|
for (uint32_t i = 0; i < WLR_LED_COUNT; ++i) {
|
2017-10-02 19:23:30 +02:00
|
|
|
if (xkb_state_led_index_is_active(keyboard->xkb_state,
|
|
|
|
keyboard->led_indexes[i])) {
|
2017-09-24 20:12:56 +02:00
|
|
|
leds |= (1 << i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wlr_keyboard_led_update(keyboard, leds);
|
|
|
|
}
|
|
|
|
|
2018-01-06 15:36:57 +01:00
|
|
|
/**
|
|
|
|
* Update the modifier state of the wlr-keyboard. Returns true if the modifier
|
|
|
|
* state changed.
|
|
|
|
*/
|
2020-05-31 23:10:15 +02:00
|
|
|
bool keyboard_modifier_update(struct wlr_keyboard *keyboard) {
|
2018-01-06 15:06:19 +01:00
|
|
|
if (keyboard->xkb_state == NULL) {
|
2018-01-06 15:36:57 +01:00
|
|
|
return false;
|
2018-01-06 15:06:19 +01:00
|
|
|
}
|
|
|
|
|
2017-10-03 14:03:26 +02:00
|
|
|
xkb_mod_mask_t depressed = xkb_state_serialize_mods(keyboard->xkb_state,
|
|
|
|
XKB_STATE_MODS_DEPRESSED);
|
|
|
|
xkb_mod_mask_t latched = xkb_state_serialize_mods(keyboard->xkb_state,
|
|
|
|
XKB_STATE_MODS_LATCHED);
|
|
|
|
xkb_mod_mask_t locked = xkb_state_serialize_mods(keyboard->xkb_state,
|
|
|
|
XKB_STATE_MODS_LOCKED);
|
|
|
|
xkb_mod_mask_t group = xkb_state_serialize_layout(keyboard->xkb_state,
|
|
|
|
XKB_STATE_LAYOUT_EFFECTIVE);
|
2018-01-17 14:31:15 +01:00
|
|
|
if (depressed == keyboard->modifiers.depressed &&
|
|
|
|
latched == keyboard->modifiers.latched &&
|
|
|
|
locked == keyboard->modifiers.locked &&
|
|
|
|
group == keyboard->modifiers.group) {
|
2018-01-06 15:36:57 +01:00
|
|
|
return false;
|
2017-10-03 09:15:48 +02:00
|
|
|
}
|
2017-10-03 14:03:26 +02:00
|
|
|
|
2018-01-17 14:31:15 +01:00
|
|
|
keyboard->modifiers.depressed = depressed;
|
|
|
|
keyboard->modifiers.latched = latched;
|
|
|
|
keyboard->modifiers.locked = locked;
|
|
|
|
keyboard->modifiers.group = group;
|
2017-10-03 14:03:26 +02:00
|
|
|
|
2018-01-06 15:36:57 +01:00
|
|
|
return true;
|
2017-10-03 08:40:10 +02:00
|
|
|
}
|
|
|
|
|
2020-05-31 23:10:15 +02:00
|
|
|
void keyboard_key_update(struct wlr_keyboard *keyboard,
|
2017-11-05 17:09:00 +01:00
|
|
|
struct wlr_event_keyboard_key *event) {
|
2019-06-15 20:08:53 +02:00
|
|
|
if (event->state == WLR_KEY_PRESSED) {
|
|
|
|
set_add(keyboard->keycodes, &keyboard->num_keycodes,
|
|
|
|
WLR_KEYBOARD_KEYS_CAP, event->keycode);
|
2017-11-05 17:09:00 +01:00
|
|
|
}
|
2019-06-15 20:08:53 +02:00
|
|
|
if (event->state == WLR_KEY_RELEASED) {
|
|
|
|
set_remove(keyboard->keycodes, &keyboard->num_keycodes,
|
|
|
|
WLR_KEYBOARD_KEYS_CAP, event->keycode);
|
2017-11-05 17:09:00 +01:00
|
|
|
}
|
2017-12-28 00:58:43 +01:00
|
|
|
|
|
|
|
assert(keyboard->num_keycodes <= WLR_KEYBOARD_KEYS_CAP);
|
2017-11-05 17:09:00 +01:00
|
|
|
}
|
|
|
|
|
2017-10-06 11:03:34 +02:00
|
|
|
void wlr_keyboard_notify_modifiers(struct wlr_keyboard *keyboard,
|
|
|
|
uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked,
|
|
|
|
uint32_t group) {
|
2018-01-06 15:06:19 +01:00
|
|
|
if (keyboard->xkb_state == NULL) {
|
2017-10-12 04:45:23 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-10-06 11:03:34 +02:00
|
|
|
xkb_state_update_mask(keyboard->xkb_state, mods_depressed, mods_latched,
|
|
|
|
mods_locked, 0, 0, group);
|
2018-01-06 15:36:57 +01:00
|
|
|
|
|
|
|
bool updated = keyboard_modifier_update(keyboard);
|
|
|
|
if (updated) {
|
2018-02-12 09:12:31 +01:00
|
|
|
wlr_signal_emit_safe(&keyboard->events.modifiers, keyboard);
|
2018-01-06 15:36:57 +01:00
|
|
|
}
|
Introduce wlr_keyboard_group
A wlr_keyboard_group allows for multiple keyboard devices to be
combined into one logical keyboard. Each keyboard device can only be
added to one keyboard group. This helps with the situation where one
physical keyboard is exposed as multiple keyboard devices. It is up to
the compositors on how they group keyboards together, if at all.
Since a wlr_keyboard_group is one logical keyboard, the keys are a set.
This means that if a key is pressed on multiple keyboard devices, the
key event will only be emitted once, but the internal state will count
the number of devices that the key is pressed on. Likewise, the key
release will not be emitted until the key is released from all devices.
If the compositor wants access to which keys are pressed and released
on each keyboard device, the events for those devices can be listened
to, as they currently are, in addition to the group keyboard's events.
Also, all keyboard devices in the group must share the same keymap. If
the keymap's differ, the keyboard device will not be able to be added
to the group. Once in the group, if the keymap or effective layout for
one keyboard device changes, it will be synced to all keyboard devices
in the group. The repeat info and keyboard modifiers are also synced
2019-11-03 20:18:41 +01:00
|
|
|
|
|
|
|
keyboard_led_update(keyboard);
|
2017-10-06 11:03:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void wlr_keyboard_notify_key(struct wlr_keyboard *keyboard,
|
2017-09-24 20:12:56 +02:00
|
|
|
struct wlr_event_keyboard_key *event) {
|
2019-11-30 12:17:34 +01:00
|
|
|
keyboard_key_update(keyboard, event);
|
|
|
|
wlr_signal_emit_safe(&keyboard->events.key, event);
|
|
|
|
|
2018-01-06 15:06:19 +01:00
|
|
|
if (keyboard->xkb_state == NULL) {
|
2017-10-12 04:45:23 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-04-18 11:54:59 +02:00
|
|
|
|
2017-10-06 11:03:34 +02:00
|
|
|
if (event->update_state) {
|
|
|
|
uint32_t keycode = event->keycode + 8;
|
|
|
|
xkb_state_update_key(keyboard->xkb_state, keycode,
|
|
|
|
event->state == WLR_KEY_PRESSED ? XKB_KEY_DOWN : XKB_KEY_UP);
|
|
|
|
}
|
2018-01-06 15:36:57 +01:00
|
|
|
|
|
|
|
bool updated = keyboard_modifier_update(keyboard);
|
|
|
|
if (updated) {
|
2018-02-12 09:12:31 +01:00
|
|
|
wlr_signal_emit_safe(&keyboard->events.modifiers, keyboard);
|
2018-01-06 15:36:57 +01:00
|
|
|
}
|
Introduce wlr_keyboard_group
A wlr_keyboard_group allows for multiple keyboard devices to be
combined into one logical keyboard. Each keyboard device can only be
added to one keyboard group. This helps with the situation where one
physical keyboard is exposed as multiple keyboard devices. It is up to
the compositors on how they group keyboards together, if at all.
Since a wlr_keyboard_group is one logical keyboard, the keys are a set.
This means that if a key is pressed on multiple keyboard devices, the
key event will only be emitted once, but the internal state will count
the number of devices that the key is pressed on. Likewise, the key
release will not be emitted until the key is released from all devices.
If the compositor wants access to which keys are pressed and released
on each keyboard device, the events for those devices can be listened
to, as they currently are, in addition to the group keyboard's events.
Also, all keyboard devices in the group must share the same keymap. If
the keymap's differ, the keyboard device will not be able to be added
to the group. Once in the group, if the keymap or effective layout for
one keyboard device changes, it will be synced to all keyboard devices
in the group. The repeat info and keyboard modifiers are also synced
2019-11-03 20:18:41 +01:00
|
|
|
|
|
|
|
keyboard_led_update(keyboard);
|
2017-09-24 20:12:56 +02:00
|
|
|
}
|
2017-06-09 23:31:21 +02:00
|
|
|
|
2017-08-14 15:41:14 +02:00
|
|
|
void wlr_keyboard_init(struct wlr_keyboard *kb,
|
2018-04-28 13:55:36 +02:00
|
|
|
const struct wlr_keyboard_impl *impl) {
|
2017-06-09 23:31:21 +02:00
|
|
|
kb->impl = impl;
|
|
|
|
wl_signal_init(&kb->events.key);
|
2017-10-03 14:03:26 +02:00
|
|
|
wl_signal_init(&kb->events.modifiers);
|
2017-09-24 20:12:56 +02:00
|
|
|
wl_signal_init(&kb->events.keymap);
|
2017-12-08 17:03:05 +01:00
|
|
|
wl_signal_init(&kb->events.repeat_info);
|
Introduce wlr_keyboard_group
A wlr_keyboard_group allows for multiple keyboard devices to be
combined into one logical keyboard. Each keyboard device can only be
added to one keyboard group. This helps with the situation where one
physical keyboard is exposed as multiple keyboard devices. It is up to
the compositors on how they group keyboards together, if at all.
Since a wlr_keyboard_group is one logical keyboard, the keys are a set.
This means that if a key is pressed on multiple keyboard devices, the
key event will only be emitted once, but the internal state will count
the number of devices that the key is pressed on. Likewise, the key
release will not be emitted until the key is released from all devices.
If the compositor wants access to which keys are pressed and released
on each keyboard device, the events for those devices can be listened
to, as they currently are, in addition to the group keyboard's events.
Also, all keyboard devices in the group must share the same keymap. If
the keymap's differ, the keyboard device will not be able to be added
to the group. Once in the group, if the keymap or effective layout for
one keyboard device changes, it will be synced to all keyboard devices
in the group. The repeat info and keyboard modifiers are also synced
2019-11-03 20:18:41 +01:00
|
|
|
wl_signal_init(&kb->events.destroy);
|
2017-12-08 17:03:05 +01:00
|
|
|
|
|
|
|
// Sane defaults
|
|
|
|
kb->repeat_info.rate = 25;
|
|
|
|
kb->repeat_info.delay = 600;
|
2017-06-09 23:31:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void wlr_keyboard_destroy(struct wlr_keyboard *kb) {
|
2017-11-16 10:30:54 +01:00
|
|
|
if (kb == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
Introduce wlr_keyboard_group
A wlr_keyboard_group allows for multiple keyboard devices to be
combined into one logical keyboard. Each keyboard device can only be
added to one keyboard group. This helps with the situation where one
physical keyboard is exposed as multiple keyboard devices. It is up to
the compositors on how they group keyboards together, if at all.
Since a wlr_keyboard_group is one logical keyboard, the keys are a set.
This means that if a key is pressed on multiple keyboard devices, the
key event will only be emitted once, but the internal state will count
the number of devices that the key is pressed on. Likewise, the key
release will not be emitted until the key is released from all devices.
If the compositor wants access to which keys are pressed and released
on each keyboard device, the events for those devices can be listened
to, as they currently are, in addition to the group keyboard's events.
Also, all keyboard devices in the group must share the same keymap. If
the keymap's differ, the keyboard device will not be able to be added
to the group. Once in the group, if the keymap or effective layout for
one keyboard device changes, it will be synced to all keyboard devices
in the group. The repeat info and keyboard modifiers are also synced
2019-11-03 20:18:41 +01:00
|
|
|
wlr_signal_emit_safe(&kb->events.destroy, kb);
|
2018-04-28 13:47:28 +02:00
|
|
|
xkb_state_unref(kb->xkb_state);
|
|
|
|
xkb_keymap_unref(kb->keymap);
|
2018-09-18 00:17:33 +02:00
|
|
|
free(kb->keymap_string);
|
2017-11-16 10:30:54 +01:00
|
|
|
if (kb->impl && kb->impl->destroy) {
|
2017-08-14 15:41:14 +02:00
|
|
|
kb->impl->destroy(kb);
|
|
|
|
} else {
|
2017-09-08 16:02:26 +02:00
|
|
|
wl_list_remove(&kb->events.key.listener_list);
|
2018-04-28 13:47:28 +02:00
|
|
|
free(kb);
|
2017-06-14 17:40:03 +02:00
|
|
|
}
|
2017-06-09 23:31:21 +02:00
|
|
|
}
|
2017-06-19 21:15:37 +02:00
|
|
|
|
|
|
|
void wlr_keyboard_led_update(struct wlr_keyboard *kb, uint32_t leds) {
|
2017-08-14 15:41:14 +02:00
|
|
|
if (kb->impl && kb->impl->led_update) {
|
|
|
|
kb->impl->led_update(kb, leds);
|
2017-06-19 21:15:37 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-24 20:12:56 +02:00
|
|
|
|
2020-03-24 00:18:18 +01:00
|
|
|
bool wlr_keyboard_set_keymap(struct wlr_keyboard *kb,
|
2017-09-24 20:12:56 +02:00
|
|
|
struct xkb_keymap *keymap) {
|
2018-01-06 15:06:19 +01:00
|
|
|
xkb_keymap_unref(kb->keymap);
|
|
|
|
kb->keymap = xkb_keymap_ref(keymap);
|
2017-12-18 20:53:24 +01:00
|
|
|
|
2018-01-06 15:06:19 +01:00
|
|
|
xkb_state_unref(kb->xkb_state);
|
2017-12-18 15:11:05 +01:00
|
|
|
kb->xkb_state = xkb_state_new(kb->keymap);
|
|
|
|
if (kb->xkb_state == NULL) {
|
2018-07-09 23:49:54 +02:00
|
|
|
wlr_log(WLR_ERROR, "Failed to create XKB state");
|
2018-01-06 15:06:19 +01:00
|
|
|
goto err;
|
2017-12-18 15:11:05 +01:00
|
|
|
}
|
2017-10-02 19:23:30 +02:00
|
|
|
|
2017-10-02 20:21:47 +02:00
|
|
|
const char *led_names[WLR_LED_COUNT] = {
|
2017-09-24 20:12:56 +02:00
|
|
|
XKB_LED_NAME_NUM,
|
|
|
|
XKB_LED_NAME_CAPS,
|
2017-10-02 20:21:47 +02:00
|
|
|
XKB_LED_NAME_SCROLL,
|
2017-09-24 20:12:56 +02:00
|
|
|
};
|
2017-10-02 20:21:47 +02:00
|
|
|
for (size_t i = 0; i < WLR_LED_COUNT; ++i) {
|
2017-10-02 19:23:30 +02:00
|
|
|
kb->led_indexes[i] = xkb_map_led_get_index(kb->keymap, led_names[i]);
|
2017-09-24 20:12:56 +02:00
|
|
|
}
|
2017-10-02 19:23:30 +02:00
|
|
|
|
2017-10-02 20:21:47 +02:00
|
|
|
const char *mod_names[WLR_MODIFIER_COUNT] = {
|
2017-10-02 19:23:30 +02:00
|
|
|
XKB_MOD_NAME_SHIFT,
|
|
|
|
XKB_MOD_NAME_CAPS,
|
|
|
|
XKB_MOD_NAME_CTRL, // "Control"
|
|
|
|
XKB_MOD_NAME_ALT, // "Mod1"
|
|
|
|
XKB_MOD_NAME_NUM, // "Mod2"
|
|
|
|
"Mod3",
|
|
|
|
XKB_MOD_NAME_LOGO, // "Mod4"
|
|
|
|
"Mod5",
|
|
|
|
};
|
|
|
|
// TODO: there's also "Ctrl", "Alt"?
|
2017-10-02 20:21:47 +02:00
|
|
|
for (size_t i = 0; i < WLR_MODIFIER_COUNT; ++i) {
|
2017-10-02 19:23:30 +02:00
|
|
|
kb->mod_indexes[i] = xkb_map_mod_get_index(kb->keymap, mod_names[i]);
|
|
|
|
}
|
|
|
|
|
2018-09-18 00:17:33 +02:00
|
|
|
char *tmp_keymap_string = xkb_keymap_get_as_string(kb->keymap,
|
2017-09-24 20:12:56 +02:00
|
|
|
XKB_KEYMAP_FORMAT_TEXT_V1);
|
2018-09-18 00:17:33 +02:00
|
|
|
if (tmp_keymap_string == NULL) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to get string version of keymap");
|
2018-01-06 15:06:19 +01:00
|
|
|
goto err;
|
2017-12-18 15:59:59 +01:00
|
|
|
}
|
2018-09-18 00:17:33 +02:00
|
|
|
free(kb->keymap_string);
|
|
|
|
kb->keymap_string = tmp_keymap_string;
|
|
|
|
kb->keymap_size = strlen(kb->keymap_string) + 1;
|
2017-10-02 19:23:30 +02:00
|
|
|
|
2018-01-06 15:36:57 +01:00
|
|
|
for (size_t i = 0; i < kb->num_keycodes; ++i) {
|
|
|
|
xkb_keycode_t keycode = kb->keycodes[i] + 8;
|
|
|
|
xkb_state_update_key(kb->xkb_state, keycode, XKB_KEY_DOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
keyboard_modifier_update(kb);
|
2018-01-06 15:06:19 +01:00
|
|
|
|
2018-02-12 09:12:31 +01:00
|
|
|
wlr_signal_emit_safe(&kb->events.keymap, kb);
|
2020-03-24 00:18:18 +01:00
|
|
|
return true;
|
2018-01-06 15:06:19 +01:00
|
|
|
|
|
|
|
err:
|
|
|
|
xkb_state_unref(kb->xkb_state);
|
|
|
|
kb->xkb_state = NULL;
|
|
|
|
xkb_keymap_unref(keymap);
|
|
|
|
kb->keymap = NULL;
|
2018-09-18 00:17:33 +02:00
|
|
|
free(kb->keymap_string);
|
|
|
|
kb->keymap_string = NULL;
|
2020-03-24 00:18:18 +01:00
|
|
|
return false;
|
2017-09-24 20:12:56 +02:00
|
|
|
}
|
2017-10-02 20:21:47 +02:00
|
|
|
|
2017-12-08 17:03:05 +01:00
|
|
|
void wlr_keyboard_set_repeat_info(struct wlr_keyboard *kb, int32_t rate,
|
|
|
|
int32_t delay) {
|
|
|
|
if (kb->repeat_info.rate == rate && kb->repeat_info.delay == delay) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
kb->repeat_info.rate = rate;
|
|
|
|
kb->repeat_info.delay = delay;
|
2018-02-12 09:12:31 +01:00
|
|
|
wlr_signal_emit_safe(&kb->events.repeat_info, kb);
|
2017-12-08 17:03:05 +01:00
|
|
|
}
|
|
|
|
|
2017-10-02 20:21:47 +02:00
|
|
|
uint32_t wlr_keyboard_get_modifiers(struct wlr_keyboard *kb) {
|
2018-01-17 14:31:15 +01:00
|
|
|
xkb_mod_mask_t mask = kb->modifiers.depressed | kb->modifiers.latched;
|
2017-10-02 20:21:47 +02:00
|
|
|
uint32_t modifiers = 0;
|
|
|
|
for (size_t i = 0; i < WLR_MODIFIER_COUNT; ++i) {
|
|
|
|
if (kb->mod_indexes[i] != XKB_MOD_INVALID &&
|
|
|
|
(mask & (1 << kb->mod_indexes[i]))) {
|
|
|
|
modifiers |= (1 << i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return modifiers;
|
|
|
|
}
|
2020-05-08 04:15:26 +02:00
|
|
|
|
|
|
|
bool wlr_keyboard_keymaps_match(struct xkb_keymap *km1,
|
|
|
|
struct xkb_keymap *km2) {
|
|
|
|
if (!km1 && !km2) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!km1 || !km2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
char *km1_str = xkb_keymap_get_as_string(km1, XKB_KEYMAP_FORMAT_TEXT_V1);
|
|
|
|
char *km2_str = xkb_keymap_get_as_string(km2, XKB_KEYMAP_FORMAT_TEXT_V1);
|
|
|
|
bool result = strcmp(km1_str, km2_str) == 0;
|
|
|
|
free(km1_str);
|
|
|
|
free(km2_str);
|
|
|
|
return result;
|
|
|
|
}
|