Use a set to track pointer button state.

In addition to `button_count`, we keep track of the current buttons
pressed just as in `wlr_keyboard`.

Add `set_add` and `set_remove` to assist with this. These functions can
only be used with values greater than 0 (such as the button/key masks
for keyboards and pointers).

Partially addresses:
- https://github.com/swaywm/wlroots/issues/1716
- https://github.com/swaywm/wlroots/issues/1593
This commit is contained in:
Ashkan Kiani 2019-06-15 11:08:53 -07:00 committed by Simon Ser
parent b85f0cbff9
commit 06a13203dd
5 changed files with 75 additions and 31 deletions

View file

@ -8,6 +8,7 @@
#include <wlr/util/log.h>
#include "types/wlr_seat.h"
#include "util/signal.h"
#include "util/array.h"
static void default_pointer_enter(struct wlr_seat_pointer_grab *grab,
struct wlr_surface *surface, double sx, double sy) {
@ -339,26 +340,32 @@ void wlr_seat_pointer_notify_motion(struct wlr_seat *wlr_seat, uint32_t time,
uint32_t wlr_seat_pointer_notify_button(struct wlr_seat *wlr_seat,
uint32_t time, uint32_t button, enum wlr_button_state state) {
clock_gettime(CLOCK_MONOTONIC, &wlr_seat->last_event);
struct wlr_seat_pointer_state* pointer_state = &wlr_seat->pointer_state;
if (state == WLR_BUTTON_PRESSED) {
if (wlr_seat->pointer_state.button_count == 0) {
wlr_seat->pointer_state.grab_button = button;
wlr_seat->pointer_state.grab_time = time;
if (pointer_state->button_count == 0) {
pointer_state->grab_button = button;
pointer_state->grab_time = time;
}
wlr_seat->pointer_state.button_count++;
set_add(pointer_state->buttons, &pointer_state->button_count,
WLR_POINTER_BUTTONS_CAP, button);
} else {
if (wlr_seat->pointer_state.button_count == 0) {
wlr_log(WLR_ERROR, "Corrupted seat button count");
} else {
wlr_seat->pointer_state.button_count--;
}
set_remove(pointer_state->buttons, &pointer_state->button_count,
WLR_POINTER_BUTTONS_CAP, button);
}
struct wlr_seat_pointer_grab *grab = wlr_seat->pointer_state.grab;
struct wlr_seat_pointer_grab *grab = pointer_state->grab;
uint32_t serial = grab->interface->button(grab, time, button, state);
if (serial && wlr_seat->pointer_state.button_count == 1 &&
wlr_log(WLR_DEBUG, "button_count=%zu grab_serial=%"PRIu32" serial=%"PRIu32"",
pointer_state->button_count,
pointer_state->grab_serial, serial);
if (serial && pointer_state->button_count == 1 &&
state == WLR_BUTTON_PRESSED) {
wlr_seat->pointer_state.grab_serial = serial;
pointer_state->grab_serial = serial;
}
return serial;
@ -413,7 +420,7 @@ bool wlr_seat_validate_pointer_grab_serial(struct wlr_seat *seat,
if (seat->pointer_state.button_count != 1 ||
seat->pointer_state.grab_serial != serial) {
wlr_log(WLR_DEBUG, "Pointer grab serial validation failed: "
"button_count=%"PRIu32" grab_serial=%"PRIu32" (got %"PRIu32")",
"button_count=%zu grab_serial=%"PRIu32" (got %"PRIu32")",
seat->pointer_state.button_count,
seat->pointer_state.grab_serial, serial);
return false;

View file

@ -57,22 +57,13 @@ static bool keyboard_modifier_update(struct wlr_keyboard *keyboard) {
static void keyboard_key_update(struct wlr_keyboard *keyboard,
struct wlr_event_keyboard_key *event) {
bool found = false;
size_t i = 0;
for (; i < keyboard->num_keycodes; ++i) {
if (keyboard->keycodes[i] == event->keycode) {
found = true;
break;
}
if (event->state == WLR_KEY_PRESSED) {
set_add(keyboard->keycodes, &keyboard->num_keycodes,
WLR_KEYBOARD_KEYS_CAP, event->keycode);
}
if (event->state == WLR_KEY_PRESSED && !found &&
keyboard->num_keycodes < WLR_KEYBOARD_KEYS_CAP) {
keyboard->keycodes[keyboard->num_keycodes++] = event->keycode;
}
if (event->state == WLR_KEY_RELEASED && found) {
keyboard->keycodes[i] = 0;
keyboard->num_keycodes = push_zeroes_to_end(keyboard->keycodes, WLR_KEYBOARD_KEYS_CAP);
if (event->state == WLR_KEY_RELEASED) {
set_remove(keyboard->keycodes, &keyboard->num_keycodes,
WLR_KEYBOARD_KEYS_CAP, event->keycode);
}
assert(keyboard->num_keycodes <= WLR_KEYBOARD_KEYS_CAP);