2017-06-09 21:31:21 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-09-24 18:12:56 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
2017-12-27 23:58:43 +00:00
|
|
|
#include <assert.h>
|
2017-06-09 21:31:21 +00:00
|
|
|
#include <wayland-server.h>
|
2017-06-21 14:27:45 +00:00
|
|
|
#include <wlr/types/wlr_keyboard.h>
|
|
|
|
#include <wlr/interfaces/wlr_keyboard.h>
|
2017-09-24 18:12:56 +00:00
|
|
|
#include <wlr/util/log.h>
|
|
|
|
|
|
|
|
int os_create_anonymous_file(off_t size);
|
|
|
|
|
|
|
|
static void keyboard_led_update(struct wlr_keyboard *keyboard) {
|
2018-01-06 14:06:19 +00:00
|
|
|
if (keyboard->xkb_state == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-24 18:12:56 +00:00
|
|
|
uint32_t leds = 0;
|
2017-10-02 18:21:47 +00:00
|
|
|
for (uint32_t i = 0; i < WLR_LED_COUNT; ++i) {
|
2017-10-02 17:23:30 +00:00
|
|
|
if (xkb_state_led_index_is_active(keyboard->xkb_state,
|
|
|
|
keyboard->led_indexes[i])) {
|
2017-09-24 18:12:56 +00:00
|
|
|
leds |= (1 << i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wlr_keyboard_led_update(keyboard, leds);
|
|
|
|
}
|
|
|
|
|
2018-01-06 14:36:57 +00:00
|
|
|
/**
|
|
|
|
* Update the modifier state of the wlr-keyboard. Returns true if the modifier
|
|
|
|
* state changed.
|
|
|
|
*/
|
|
|
|
static bool keyboard_modifier_update(struct wlr_keyboard *keyboard) {
|
2018-01-06 14:06:19 +00:00
|
|
|
if (keyboard->xkb_state == NULL) {
|
2018-01-06 14:36:57 +00:00
|
|
|
return false;
|
2018-01-06 14:06:19 +00:00
|
|
|
}
|
|
|
|
|
2017-10-03 12:03:26 +00: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-05 12:00:50 +00:00
|
|
|
if (depressed == keyboard->modifiers->depressed &&
|
|
|
|
latched == keyboard->modifiers->latched &&
|
|
|
|
locked == keyboard->modifiers->locked &&
|
|
|
|
group == keyboard->modifiers->group) {
|
2018-01-06 14:36:57 +00:00
|
|
|
return false;
|
2017-10-03 07:15:48 +00:00
|
|
|
}
|
2017-10-03 12:03:26 +00:00
|
|
|
|
2018-01-05 12:00:50 +00:00
|
|
|
keyboard->modifiers->depressed = depressed;
|
|
|
|
keyboard->modifiers->latched = latched;
|
|
|
|
keyboard->modifiers->locked = locked;
|
|
|
|
keyboard->modifiers->group = group;
|
2017-10-03 12:03:26 +00:00
|
|
|
|
2018-01-06 14:36:57 +00:00
|
|
|
return true;
|
2017-10-03 06:40:10 +00:00
|
|
|
}
|
|
|
|
|
2017-12-27 23:58:43 +00:00
|
|
|
// https://www.geeksforgeeks.org/move-zeroes-end-array/
|
|
|
|
static size_t push_zeroes_to_end(uint32_t arr[], size_t n) {
|
|
|
|
size_t count = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < n; i++) {
|
|
|
|
if (arr[i] != 0) {
|
|
|
|
arr[count++] = arr[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ret = count;
|
|
|
|
|
|
|
|
while (count < n) {
|
|
|
|
arr[count++] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-11-05 16:09:00 +00:00
|
|
|
static void keyboard_key_update(struct wlr_keyboard *keyboard,
|
|
|
|
struct wlr_event_keyboard_key *event) {
|
|
|
|
bool found = false;
|
|
|
|
size_t i = 0;
|
2017-12-27 23:58:43 +00:00
|
|
|
for (; i < keyboard->num_keycodes; ++i) {
|
2017-11-05 16:09:00 +00:00
|
|
|
if (keyboard->keycodes[i] == event->keycode) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-27 23:58:43 +00:00
|
|
|
if (event->state == WLR_KEY_PRESSED && !found &&
|
|
|
|
keyboard->num_keycodes < WLR_KEYBOARD_KEYS_CAP) {
|
|
|
|
keyboard->keycodes[keyboard->num_keycodes++] = event->keycode;
|
2017-11-05 16:09:00 +00:00
|
|
|
}
|
|
|
|
if (event->state == WLR_KEY_RELEASED && found) {
|
|
|
|
keyboard->keycodes[i] = 0;
|
2017-12-27 23:58:43 +00:00
|
|
|
keyboard->num_keycodes = push_zeroes_to_end(keyboard->keycodes, WLR_KEYBOARD_KEYS_CAP);
|
2017-11-05 16:09:00 +00:00
|
|
|
}
|
2017-12-27 23:58:43 +00:00
|
|
|
|
|
|
|
assert(keyboard->num_keycodes <= WLR_KEYBOARD_KEYS_CAP);
|
2017-11-05 16:09:00 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 09:03:34 +00: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 14:06:19 +00:00
|
|
|
if (keyboard->xkb_state == NULL) {
|
2017-10-12 02:45:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-10-06 09:03:34 +00:00
|
|
|
xkb_state_update_mask(keyboard->xkb_state, mods_depressed, mods_latched,
|
|
|
|
mods_locked, 0, 0, group);
|
2018-01-06 14:36:57 +00:00
|
|
|
|
|
|
|
bool updated = keyboard_modifier_update(keyboard);
|
|
|
|
if (updated) {
|
|
|
|
wl_signal_emit(&keyboard->events.modifiers, keyboard);
|
|
|
|
}
|
2017-10-06 09:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void wlr_keyboard_notify_key(struct wlr_keyboard *keyboard,
|
2017-09-24 18:12:56 +00:00
|
|
|
struct wlr_event_keyboard_key *event) {
|
2018-01-06 14:06:19 +00:00
|
|
|
if (keyboard->xkb_state == NULL) {
|
2017-10-12 02:45:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-10-06 09:03:34 +00: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);
|
|
|
|
}
|
2017-09-24 18:12:56 +00:00
|
|
|
keyboard_led_update(keyboard);
|
2018-01-06 14:36:57 +00:00
|
|
|
|
|
|
|
bool updated = keyboard_modifier_update(keyboard);
|
|
|
|
if (updated) {
|
|
|
|
wl_signal_emit(&keyboard->events.modifiers, keyboard);
|
|
|
|
}
|
|
|
|
|
2017-11-05 16:09:00 +00:00
|
|
|
keyboard_key_update(keyboard, event);
|
2017-09-25 20:44:26 +00:00
|
|
|
wl_signal_emit(&keyboard->events.key, event);
|
2017-09-24 18:12:56 +00:00
|
|
|
}
|
2017-06-09 21:31:21 +00:00
|
|
|
|
2017-08-14 13:41:14 +00:00
|
|
|
void wlr_keyboard_init(struct wlr_keyboard *kb,
|
|
|
|
struct wlr_keyboard_impl *impl) {
|
2017-06-09 21:31:21 +00:00
|
|
|
kb->impl = impl;
|
2018-01-05 12:00:50 +00:00
|
|
|
kb->modifiers = calloc(1, sizeof(struct wlr_keyboard_modifiers));
|
2017-06-09 21:31:21 +00:00
|
|
|
wl_signal_init(&kb->events.key);
|
2017-10-03 12:03:26 +00:00
|
|
|
wl_signal_init(&kb->events.modifiers);
|
2017-09-24 18:12:56 +00:00
|
|
|
wl_signal_init(&kb->events.keymap);
|
2017-12-08 16:03:05 +00:00
|
|
|
wl_signal_init(&kb->events.repeat_info);
|
|
|
|
|
|
|
|
// Sane defaults
|
|
|
|
kb->repeat_info.rate = 25;
|
|
|
|
kb->repeat_info.delay = 600;
|
2017-06-09 21:31:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void wlr_keyboard_destroy(struct wlr_keyboard *kb) {
|
2017-11-16 09:30:54 +00:00
|
|
|
if (kb == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (kb->impl && kb->impl->destroy) {
|
2017-08-14 13:41:14 +00:00
|
|
|
kb->impl->destroy(kb);
|
|
|
|
} else {
|
2017-09-08 14:02:26 +00:00
|
|
|
wl_list_remove(&kb->events.key.listener_list);
|
2017-06-14 15:40:03 +00:00
|
|
|
}
|
2017-10-03 05:34:30 +00:00
|
|
|
xkb_state_unref(kb->xkb_state);
|
2017-12-18 19:53:24 +00:00
|
|
|
xkb_keymap_unref(kb->keymap);
|
2017-09-24 18:12:56 +00:00
|
|
|
close(kb->keymap_fd);
|
2018-01-05 12:00:50 +00:00
|
|
|
free(kb->modifiers);
|
2017-09-29 00:29:26 +00:00
|
|
|
free(kb);
|
2017-06-09 21:31:21 +00:00
|
|
|
}
|
2017-06-19 19:15:37 +00:00
|
|
|
|
|
|
|
void wlr_keyboard_led_update(struct wlr_keyboard *kb, uint32_t leds) {
|
2017-08-14 13:41:14 +00:00
|
|
|
if (kb->impl && kb->impl->led_update) {
|
|
|
|
kb->impl->led_update(kb, leds);
|
2017-06-19 19:15:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-24 18:12:56 +00:00
|
|
|
|
|
|
|
void wlr_keyboard_set_keymap(struct wlr_keyboard *kb,
|
|
|
|
struct xkb_keymap *keymap) {
|
2018-01-06 14:06:19 +00:00
|
|
|
char *keymap_str = NULL;
|
2017-12-18 19:53:24 +00:00
|
|
|
|
2018-01-06 14:06:19 +00:00
|
|
|
xkb_keymap_unref(kb->keymap);
|
|
|
|
kb->keymap = xkb_keymap_ref(keymap);
|
2017-12-18 19:53:24 +00:00
|
|
|
|
2018-01-06 14:06:19 +00:00
|
|
|
xkb_state_unref(kb->xkb_state);
|
2017-12-18 14:11:05 +00:00
|
|
|
kb->xkb_state = xkb_state_new(kb->keymap);
|
|
|
|
if (kb->xkb_state == NULL) {
|
|
|
|
wlr_log(L_ERROR, "Failed to create XKB state");
|
2018-01-06 14:06:19 +00:00
|
|
|
goto err;
|
2017-12-18 14:11:05 +00:00
|
|
|
}
|
2017-10-02 17:23:30 +00:00
|
|
|
|
2017-10-02 18:21:47 +00:00
|
|
|
const char *led_names[WLR_LED_COUNT] = {
|
2017-09-24 18:12:56 +00:00
|
|
|
XKB_LED_NAME_NUM,
|
|
|
|
XKB_LED_NAME_CAPS,
|
2017-10-02 18:21:47 +00:00
|
|
|
XKB_LED_NAME_SCROLL,
|
2017-09-24 18:12:56 +00:00
|
|
|
};
|
2017-10-02 18:21:47 +00:00
|
|
|
for (size_t i = 0; i < WLR_LED_COUNT; ++i) {
|
2017-10-02 17:23:30 +00:00
|
|
|
kb->led_indexes[i] = xkb_map_led_get_index(kb->keymap, led_names[i]);
|
2017-09-24 18:12:56 +00:00
|
|
|
}
|
2017-10-02 17:23:30 +00:00
|
|
|
|
2017-10-02 18:21:47 +00:00
|
|
|
const char *mod_names[WLR_MODIFIER_COUNT] = {
|
2017-10-02 17:23:30 +00: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 18:21:47 +00:00
|
|
|
for (size_t i = 0; i < WLR_MODIFIER_COUNT; ++i) {
|
2017-10-02 17:23:30 +00:00
|
|
|
kb->mod_indexes[i] = xkb_map_mod_get_index(kb->keymap, mod_names[i]);
|
|
|
|
}
|
|
|
|
|
2018-01-06 14:06:19 +00:00
|
|
|
keymap_str = xkb_keymap_get_as_string(kb->keymap,
|
2017-09-24 18:12:56 +00:00
|
|
|
XKB_KEYMAP_FORMAT_TEXT_V1);
|
2017-09-25 12:47:00 +00:00
|
|
|
kb->keymap_size = strlen(keymap_str) + 1;
|
2017-12-18 14:59:59 +00:00
|
|
|
if (kb->keymap_fd) {
|
|
|
|
close(kb->keymap_fd);
|
|
|
|
}
|
2017-09-24 18:12:56 +00:00
|
|
|
kb->keymap_fd = os_create_anonymous_file(kb->keymap_size);
|
2017-12-18 14:59:59 +00:00
|
|
|
if (kb->keymap_fd < 0) {
|
|
|
|
wlr_log(L_ERROR, "creating a keymap file for %lu bytes failed", kb->keymap_size);
|
2018-01-06 14:06:19 +00:00
|
|
|
goto err;
|
2017-12-18 14:59:59 +00:00
|
|
|
}
|
2017-09-24 18:12:56 +00:00
|
|
|
void *ptr = mmap(NULL, kb->keymap_size,
|
2017-10-02 17:23:30 +00:00
|
|
|
PROT_READ | PROT_WRITE, MAP_SHARED, kb->keymap_fd, 0);
|
2017-12-18 14:59:59 +00:00
|
|
|
if (ptr == (void*)-1) {
|
|
|
|
wlr_log(L_ERROR, "failed to mmap() %lu bytes", kb->keymap_size);
|
2018-01-06 14:06:19 +00:00
|
|
|
goto err;
|
2017-12-18 14:59:59 +00:00
|
|
|
}
|
2017-09-24 18:12:56 +00:00
|
|
|
strcpy(ptr, keymap_str);
|
|
|
|
free(keymap_str);
|
2017-10-02 17:23:30 +00:00
|
|
|
|
2018-01-06 14:36:57 +00: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 14:06:19 +00:00
|
|
|
|
2017-09-24 18:12:56 +00:00
|
|
|
wl_signal_emit(&kb->events.keymap, kb);
|
2018-01-06 14:06:19 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
err:
|
|
|
|
xkb_state_unref(kb->xkb_state);
|
|
|
|
kb->xkb_state = NULL;
|
|
|
|
xkb_keymap_unref(keymap);
|
|
|
|
kb->keymap = NULL;
|
|
|
|
close(kb->keymap_fd);
|
|
|
|
free(keymap_str);
|
2017-09-24 18:12:56 +00:00
|
|
|
}
|
2017-10-02 18:21:47 +00:00
|
|
|
|
2017-12-08 16:03:05 +00: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;
|
|
|
|
wl_signal_emit(&kb->events.repeat_info, kb);
|
|
|
|
}
|
|
|
|
|
2017-10-02 18:21:47 +00:00
|
|
|
uint32_t wlr_keyboard_get_modifiers(struct wlr_keyboard *kb) {
|
2018-01-05 12:00:50 +00:00
|
|
|
xkb_mod_mask_t mask = kb->modifiers->depressed | kb->modifiers->latched;
|
2017-10-02 18:21:47 +00: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;
|
|
|
|
}
|