2017-09-24 20:12:56 +02:00
|
|
|
#include <assert.h>
|
2017-06-09 23:31:21 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-09-24 20:12:56 +02:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
2017-06-09 23:31:21 +02:00
|
|
|
#include <wayland-server.h>
|
2017-06-21 16:27:45 +02:00
|
|
|
#include <wlr/types/wlr_keyboard.h>
|
|
|
|
#include <wlr/interfaces/wlr_keyboard.h>
|
2017-09-24 20:12:56 +02:00
|
|
|
#include <wlr/util/log.h>
|
|
|
|
|
|
|
|
int os_create_anonymous_file(off_t size);
|
|
|
|
|
|
|
|
static void keyboard_led_update(struct wlr_keyboard *keyboard) {
|
|
|
|
uint32_t leds = 0;
|
|
|
|
for (uint32_t i = 0; i < WLR_LED_LAST; ++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);
|
|
|
|
}
|
|
|
|
|
2017-10-02 19:23:30 +02:00
|
|
|
static void keyboard_modifier_update(struct wlr_keyboard *keyboard) {
|
|
|
|
xkb_mod_mask_t depressed = xkb_state_serialize_mods(keyboard->xkb_state,
|
|
|
|
XKB_STATE_DEPRESSED);
|
|
|
|
xkb_mod_mask_t latched = xkb_state_serialize_mods(keyboard->xkb_state,
|
|
|
|
XKB_STATE_LATCHED);
|
|
|
|
xkb_mod_mask_t locked = xkb_state_serialize_mods(keyboard->xkb_state,
|
|
|
|
XKB_STATE_LOCKED);
|
|
|
|
xkb_mod_mask_t group = xkb_state_serialize_layout(keyboard->xkb_state,
|
|
|
|
XKB_STATE_LAYOUT_EFFECTIVE);
|
|
|
|
if (depressed == keyboard->modifiers.depressed &&
|
|
|
|
latched == keyboard->modifiers.latched &&
|
|
|
|
locked == keyboard->modifiers.locked &&
|
|
|
|
group == keyboard->modifiers.group) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
keyboard->modifiers.depressed = depressed;
|
|
|
|
keyboard->modifiers.latched = latched;
|
|
|
|
keyboard->modifiers.locked = locked;
|
|
|
|
keyboard->modifiers.group = group;
|
|
|
|
|
|
|
|
// TODO: wl_keyboard_send_modifiers
|
|
|
|
}
|
|
|
|
|
2017-09-24 20:12:56 +02:00
|
|
|
void wlr_keyboard_update_state(struct wlr_keyboard *keyboard,
|
|
|
|
struct wlr_event_keyboard_key *event) {
|
|
|
|
uint32_t keycode = event->keycode + 8;
|
|
|
|
xkb_state_update_key(keyboard->xkb_state, keycode,
|
2017-10-02 19:23:30 +02:00
|
|
|
event->state == WLR_KEY_PRESSED ? XKB_KEY_DOWN : XKB_KEY_UP);
|
2017-09-24 20:12:56 +02:00
|
|
|
keyboard_led_update(keyboard);
|
2017-10-02 19:23:30 +02:00
|
|
|
keyboard_modifier_update(keyboard);
|
2017-09-25 22:44:26 +02:00
|
|
|
wl_signal_emit(&keyboard->events.key, event);
|
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,
|
|
|
|
struct wlr_keyboard_impl *impl) {
|
2017-06-09 23:31:21 +02:00
|
|
|
kb->impl = impl;
|
|
|
|
wl_signal_init(&kb->events.key);
|
2017-09-24 20:12:56 +02:00
|
|
|
wl_signal_init(&kb->events.keymap);
|
2017-06-09 23:31:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void wlr_keyboard_destroy(struct wlr_keyboard *kb) {
|
2017-08-14 18:23:20 +02:00
|
|
|
if (kb && 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);
|
2017-06-14 17:40:03 +02:00
|
|
|
}
|
2017-09-24 20:12:56 +02:00
|
|
|
close(kb->keymap_fd);
|
2017-09-29 02:29:26 +02:00
|
|
|
free(kb);
|
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
|
|
|
|
|
|
|
void wlr_keyboard_set_keymap(struct wlr_keyboard *kb,
|
|
|
|
struct xkb_keymap *keymap) {
|
2017-09-28 14:54:57 +02:00
|
|
|
wlr_log(L_DEBUG, "Keymap set");
|
2017-09-24 20:12:56 +02:00
|
|
|
kb->keymap = keymap;
|
|
|
|
assert(kb->xkb_state = xkb_state_new(kb->keymap));
|
2017-10-02 19:23:30 +02:00
|
|
|
|
|
|
|
const char *led_names[WLR_LED_LAST] = {
|
2017-09-24 20:12:56 +02:00
|
|
|
XKB_LED_NAME_NUM,
|
|
|
|
XKB_LED_NAME_CAPS,
|
|
|
|
XKB_LED_NAME_SCROLL
|
|
|
|
};
|
2017-10-02 19:23:30 +02:00
|
|
|
for (size_t i = 0; i < WLR_LED_LAST; ++i) {
|
|
|
|
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
|
|
|
|
|
|
|
const char *mod_names[WLR_MODIFIER_LAST] = {
|
|
|
|
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"?
|
|
|
|
for (size_t i = 0; i < WLR_LED_LAST; ++i) {
|
|
|
|
kb->mod_indexes[i] = xkb_map_mod_get_index(kb->keymap, mod_names[i]);
|
|
|
|
}
|
|
|
|
|
2017-09-24 20:12:56 +02:00
|
|
|
char *keymap_str = xkb_keymap_get_as_string(kb->keymap,
|
|
|
|
XKB_KEYMAP_FORMAT_TEXT_V1);
|
2017-09-25 14:47:00 +02:00
|
|
|
kb->keymap_size = strlen(keymap_str) + 1;
|
2017-09-24 20:12:56 +02:00
|
|
|
kb->keymap_fd = os_create_anonymous_file(kb->keymap_size);
|
|
|
|
void *ptr = mmap(NULL, kb->keymap_size,
|
2017-10-02 19:23:30 +02:00
|
|
|
PROT_READ | PROT_WRITE, MAP_SHARED, kb->keymap_fd, 0);
|
2017-09-24 20:12:56 +02:00
|
|
|
strcpy(ptr, keymap_str);
|
|
|
|
free(keymap_str);
|
2017-10-02 19:23:30 +02:00
|
|
|
|
2017-09-24 20:12:56 +02:00
|
|
|
wl_signal_emit(&kb->events.keymap, kb);
|
|
|
|
}
|