keep track of number of keycodes pressed

This commit is contained in:
Tony Crisci 2017-12-27 18:58:43 -05:00
parent a0d2a6b445
commit e8b810ce3e
3 changed files with 31 additions and 13 deletions

View file

@ -44,6 +44,7 @@ struct wlr_keyboard {
xkb_mod_index_t mod_indexes[WLR_MODIFIER_COUNT]; xkb_mod_index_t mod_indexes[WLR_MODIFIER_COUNT];
uint32_t keycodes[WLR_KEYBOARD_KEYS_CAP]; uint32_t keycodes[WLR_KEYBOARD_KEYS_CAP];
size_t num_keycodes;
struct { struct {
xkb_mod_mask_t depressed; xkb_mod_mask_t depressed;
xkb_mod_mask_t latched; xkb_mod_mask_t latched;

View file

@ -2,6 +2,7 @@
#include <string.h> #include <string.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <unistd.h> #include <unistd.h>
#include <assert.h>
#include <wayland-server.h> #include <wayland-server.h>
#include <wlr/types/wlr_keyboard.h> #include <wlr/types/wlr_keyboard.h>
#include <wlr/interfaces/wlr_keyboard.h> #include <wlr/interfaces/wlr_keyboard.h>
@ -44,28 +45,46 @@ static void keyboard_modifier_update(struct wlr_keyboard *keyboard) {
wl_signal_emit(&keyboard->events.modifiers, keyboard); wl_signal_emit(&keyboard->events.modifiers, keyboard);
} }
// 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;
}
static void keyboard_key_update(struct wlr_keyboard *keyboard, static void keyboard_key_update(struct wlr_keyboard *keyboard,
struct wlr_event_keyboard_key *event) { struct wlr_event_keyboard_key *event) {
bool found = false; bool found = false;
size_t i = 0; size_t i = 0;
for (; i < WLR_KEYBOARD_KEYS_CAP; ++i) { for (; i < keyboard->num_keycodes; ++i) {
if (keyboard->keycodes[i] == event->keycode) { if (keyboard->keycodes[i] == event->keycode) {
found = true; found = true;
break; break;
} }
} }
if (event->state == WLR_KEY_PRESSED && !found) { if (event->state == WLR_KEY_PRESSED && !found &&
for (size_t i = 0; i < WLR_KEYBOARD_KEYS_CAP; ++i) { keyboard->num_keycodes < WLR_KEYBOARD_KEYS_CAP) {
if (keyboard->keycodes[i] == 0) { keyboard->keycodes[keyboard->num_keycodes++] = event->keycode;
keyboard->keycodes[i] = event->keycode;
break;
}
}
} }
if (event->state == WLR_KEY_RELEASED && found) { if (event->state == WLR_KEY_RELEASED && found) {
keyboard->keycodes[i] = 0; keyboard->keycodes[i] = 0;
keyboard->num_keycodes = push_zeroes_to_end(keyboard->keycodes, WLR_KEYBOARD_KEYS_CAP);
} }
assert(keyboard->num_keycodes <= WLR_KEYBOARD_KEYS_CAP);
} }
void wlr_keyboard_notify_modifiers(struct wlr_keyboard *keyboard, void wlr_keyboard_notify_modifiers(struct wlr_keyboard *keyboard,

View file

@ -874,12 +874,10 @@ void wlr_seat_keyboard_enter(struct wlr_seat *seat,
struct wl_array keys; struct wl_array keys;
wl_array_init(&keys); wl_array_init(&keys);
for (size_t i = 0; i < WLR_KEYBOARD_KEYS_CAP; ++i) { for (size_t i = 0; i < keyboard->num_keycodes; ++i) {
if (keyboard->keycodes[i] != 0) {
uint32_t *p = wl_array_add(&keys, sizeof(uint32_t)); uint32_t *p = wl_array_add(&keys, sizeof(uint32_t));
*p = keyboard->keycodes[i]; *p = keyboard->keycodes[i];
} }
}
uint32_t serial = wl_display_next_serial(seat->display); uint32_t serial = wl_display_next_serial(seat->display);
struct wl_resource *resource; struct wl_resource *resource;
wl_resource_for_each(resource, &client->keyboards) { wl_resource_for_each(resource, &client->keyboards) {