mirror of
https://github.com/hyprwm/wlroots-hyprland.git
synced 2024-11-22 04:45:58 +01:00
util/set: overhaul
This commit is contained in:
parent
20c208d46a
commit
7333a4602a
3 changed files with 41 additions and 83 deletions
|
@ -2,24 +2,28 @@
|
||||||
#define UTIL_SET_H
|
#define UTIL_SET_H
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <sys/types.h>
|
||||||
size_t push_zeroes_to_end(uint32_t arr[], size_t n);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add `target` to `values` if it doesn't exist
|
* Add target to values.
|
||||||
* "set"s should only be modified with set_* functions
|
*
|
||||||
* Values MUST be greater than 0
|
* Target is added to the end of the set.
|
||||||
|
*
|
||||||
|
* Returns the index of target, or -1 if the set is full or target already
|
||||||
|
* exists.
|
||||||
*/
|
*/
|
||||||
bool set_add(uint32_t values[], size_t *len, size_t cap, uint32_t target);
|
ssize_t set_add(uint32_t values[], size_t *len, size_t cap, uint32_t target);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove `target` from `values` if it exists
|
* Remove target from values.
|
||||||
* "set"s should only be modified with set_* functions
|
*
|
||||||
* Values MUST be greater than 0
|
* When target is removed, the last element of the set is moved to where
|
||||||
|
* target was.
|
||||||
|
*
|
||||||
|
* Returns the previous index of target, or -1 if target wasn't in values.
|
||||||
*/
|
*/
|
||||||
bool set_remove(uint32_t values[], size_t *len, size_t cap, uint32_t target);
|
ssize_t set_remove(uint32_t values[], size_t *len, size_t cap, uint32_t target);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -257,50 +257,26 @@ struct wlr_tablet_tool_client_v2 *tablet_tool_client_from_resource(struct wl_res
|
||||||
return wl_resource_get_user_data(resource);
|
return wl_resource_get_user_data(resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Actual protocol foo */
|
|
||||||
|
|
||||||
// Button 0 is KEY_RESERVED in input-event-codes on linux (and freebsd)
|
|
||||||
static ssize_t tablet_tool_button_update(struct wlr_tablet_v2_tablet_tool *tool,
|
static ssize_t tablet_tool_button_update(struct wlr_tablet_v2_tablet_tool *tool,
|
||||||
uint32_t button, enum zwp_tablet_pad_v2_button_state state) {
|
uint32_t button, enum zwp_tablet_pad_v2_button_state state) {
|
||||||
bool found = false;
|
ssize_t i;
|
||||||
size_t i = 0;
|
if (state == ZWP_TABLET_PAD_V2_BUTTON_STATE_PRESSED) {
|
||||||
for (; i < tool->num_buttons; ++i) {
|
i = set_add(tool->pressed_buttons, &tool->num_buttons,
|
||||||
if (tool->pressed_buttons[i] == button) {
|
WLR_TABLET_V2_TOOL_BUTTONS_CAP, button);
|
||||||
found = true;
|
if (i != -1) {
|
||||||
wlr_log(WLR_DEBUG, "Found the button \\o/: %u", button);
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (state == ZWP_TABLET_PAD_V2_BUTTON_STATE_PRESSED && found) {
|
|
||||||
/* Already have the button saved, durr */
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (state == ZWP_TABLET_PAD_V2_BUTTON_STATE_PRESSED && !found) {
|
|
||||||
if (tool->num_buttons < WLR_TABLET_V2_TOOL_BUTTONS_CAP) {
|
|
||||||
i = tool->num_buttons++;
|
|
||||||
tool->pressed_buttons[i] = button;
|
|
||||||
tool->pressed_serials[i] = -1;
|
tool->pressed_serials[i] = -1;
|
||||||
} else {
|
} else {
|
||||||
i = -1;
|
wlr_log(WLR_ERROR, "Failed to add tablet tool button %x", button);
|
||||||
wlr_log(WLR_ERROR, "You pressed more than %d tablet tool buttons. "
|
}
|
||||||
"This is currently not supported by wlroots. Please report this "
|
} else {
|
||||||
"with a description of your tablet, since this is either a "
|
i = set_remove(tool->pressed_buttons, &tool->num_buttons,
|
||||||
"bug, or fancy hardware", WLR_TABLET_V2_TOOL_BUTTONS_CAP);
|
WLR_TABLET_V2_TOOL_BUTTONS_CAP, button);
|
||||||
|
if (i != -1) {
|
||||||
|
tool->pressed_serials[i] = tool->pressed_serials[tool->num_buttons];
|
||||||
|
} else {
|
||||||
|
wlr_log(WLR_ERROR, "Failed to remove tablet tool button %x", button);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (state == ZWP_TABLET_PAD_V2_BUTTON_STATE_RELEASED && found) {
|
|
||||||
wlr_log(WLR_DEBUG, "Removed the button \\o/: %u", button);
|
|
||||||
tool->pressed_buttons[i] = 0;
|
|
||||||
tool->pressed_serials[i] = 0;
|
|
||||||
tool->num_buttons = push_zeroes_to_end(tool->pressed_buttons, WLR_TABLET_V2_TOOL_BUTTONS_CAP);
|
|
||||||
tool->num_buttons = push_zeroes_to_end(tool->pressed_serials, WLR_TABLET_V2_TOOL_BUTTONS_CAP);
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(tool->num_buttons <= WLR_TABLET_V2_TOOL_BUTTONS_CAP);
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
46
util/set.c
46
util/set.c
|
@ -1,47 +1,25 @@
|
||||||
#include "util/set.h"
|
#include "util/set.h"
|
||||||
|
|
||||||
// https://www.geeksforgeeks.org/move-zeroes-end-array/
|
ssize_t set_add(uint32_t values[], size_t *len, size_t cap, uint32_t target) {
|
||||||
size_t push_zeroes_to_end(uint32_t arr[], size_t n) {
|
for (uint32_t i = 0; i < *len; ++i) {
|
||||||
size_t count = 0;
|
if (values[i] == target) {
|
||||||
|
return i;
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool set_add(uint32_t values[], size_t *len, size_t cap, uint32_t target) {
|
|
||||||
if (*len == cap) {
|
if (*len == cap) {
|
||||||
return false;
|
return -1;
|
||||||
}
|
}
|
||||||
for (uint32_t i = 0; i < *len; ++i) {
|
values[*len] = target;
|
||||||
if (values[i] == target) {
|
return (*len)++;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
values[(*len)++] = target;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool set_remove(uint32_t values[], size_t *len, size_t cap, uint32_t target) {
|
ssize_t set_remove(uint32_t values[], size_t *len, size_t cap, uint32_t target) {
|
||||||
for (uint32_t i = 0; i < *len; ++i) {
|
for (uint32_t i = 0; i < *len; ++i) {
|
||||||
if (values[i] == target) {
|
if (values[i] == target) {
|
||||||
// Set to 0 and swap with the end element so that
|
--(*len);
|
||||||
// zeroes exist only after all the values.
|
values[i] = values[*len];
|
||||||
size_t last_elem_pos = --(*len);
|
return i;
|
||||||
values[i] = values[last_elem_pos];
|
|
||||||
values[last_elem_pos] = 0;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue