aboutsummaryrefslogtreecommitdiffstats
path: root/sway/input/cursor.c
diff options
context:
space:
mode:
authorLibravatar Brian Ashworth <bosrsf04@gmail.com>2018-12-28 13:48:09 -0500
committerLibravatar Brian Ashworth <bosrsf04@gmail.com>2019-01-09 11:29:04 -0500
commit6f6a9af60ec98a6f18f1163317dee74b88328dab (patch)
treecd174b6d0e549a1142444c6f00100442166cfa3f /sway/input/cursor.c
parentReset container dimensions when moving into workspace from direction (diff)
downloadsway-6f6a9af60ec98a6f18f1163317dee74b88328dab.tar.gz
sway-6f6a9af60ec98a6f18f1163317dee74b88328dab.tar.zst
sway-6f6a9af60ec98a6f18f1163317dee74b88328dab.zip
Add helpers for improved mouse button parsing
The following helper functions have been added to aid with parsing mouse buttons from a string: 1. `get_mouse_bindsym`: attempts to parse the string as an x11 button (button[1-9]) or as an event name (ex BTN_LEFT or BTN_SIDE) 2. `get_mouse_bindcode`: attempts to parse the string as an event code and validates that the event code is a button (starts with `BTN_`). 3. `get_mouse_button`: this is a conveniency function for callers that do not care whether a bindsym or bindcode are used and attempts to parse the string as a bindsym and then bindcode. None of these functions are used in this commit. The sole purpose of this commit is to make the larger set more granular and easier to review/manipulate. There will be a series of commits following this one that will modify any command which uses a mouse button to use these helpers.
Diffstat (limited to 'sway/input/cursor.c')
-rw-r--r--sway/input/cursor.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/sway/input/cursor.c b/sway/input/cursor.c
index 96feb47d..0d5e9361 100644
--- a/sway/input/cursor.c
+++ b/sway/input/cursor.c
@@ -2,8 +2,10 @@
2#include <math.h> 2#include <math.h>
3#include <libevdev/libevdev.h> 3#include <libevdev/libevdev.h>
4#include <linux/input-event-codes.h> 4#include <linux/input-event-codes.h>
5#include <errno.h>
5#include <float.h> 6#include <float.h>
6#include <limits.h> 7#include <limits.h>
8#include <strings.h>
7#include <wlr/types/wlr_cursor.h> 9#include <wlr/types/wlr_cursor.h>
8#include <wlr/types/wlr_xcursor_manager.h> 10#include <wlr/types/wlr_xcursor_manager.h>
9#include <wlr/types/wlr_idle.h> 11#include <wlr/types/wlr_idle.h>
@@ -1527,3 +1529,66 @@ void cursor_warp_to_workspace(struct sway_cursor *cursor,
1527 1529
1528 wlr_cursor_warp(cursor->cursor, NULL, x, y); 1530 wlr_cursor_warp(cursor->cursor, NULL, x, y);
1529} 1531}
1532
1533uint32_t get_mouse_bindsym(const char *name, char **error) {
1534 if (strncasecmp(name, "button", strlen("button")) == 0) {
1535 // Map to x11 mouse buttons
1536 int number = name[strlen("button")] - '0';
1537 if (number < 1 || number > 9 || strlen(name) > strlen("button0")) {
1538 *error = strdup("Only buttons 1-9 are supported. For other mouse "
1539 "buttons, use the name of the event code.");
1540 return 0;
1541 }
1542 static const uint32_t buttons[] = {BTN_LEFT, BTN_MIDDLE, BTN_RIGHT,
1543 SWAY_SCROLL_UP, SWAY_SCROLL_DOWN, SWAY_SCROLL_LEFT,
1544 SWAY_SCROLL_RIGHT, BTN_SIDE, BTN_EXTRA};
1545 return buttons[number - 1];
1546 } else if (strncmp(name, "BTN_", strlen("BTN_")) == 0) {
1547 // Get event code from name
1548 int code = libevdev_event_code_from_name(EV_KEY, name);
1549 if (code == -1) {
1550 size_t len = snprintf(NULL, 0, "Unknown event %s", name) + 1;
1551 *error = malloc(len);
1552 if (*error) {
1553 snprintf(*error, len, "Unknown event %s", name);
1554 }
1555 return 0;
1556 }
1557 return code;
1558 }
1559 return 0;
1560}
1561
1562uint32_t get_mouse_bindcode(const char *name, char **error) {
1563 // Validate event code
1564 errno = 0;
1565 char *endptr;
1566 int code = strtol(name, &endptr, 10);
1567 if (endptr == name && code <= 0) {
1568 *error = strdup("Button event code must be a positive integer.");
1569 return 0;
1570 } else if (errno == ERANGE) {
1571 *error = strdup("Button event code out of range.");
1572 return 0;
1573 }
1574 const char *event = libevdev_event_code_get_name(EV_KEY, code);
1575 if (!event || strncmp(event, "BTN_", strlen("BTN_")) != 0) {
1576 size_t len = snprintf(NULL, 0, "Event code %d (%s) is not a button",
1577 code, event) + 1;
1578 *error = malloc(len);
1579 if (*error) {
1580 snprintf(*error, len, "Event code %d (%s) is not a button",
1581 code, event);
1582 }
1583 return 0;
1584 }
1585 return code;
1586}
1587
1588uint32_t get_mouse_button(const char *name, char **error) {
1589 uint32_t button = get_mouse_bindsym(name, error);
1590 if (!button && !error) {
1591 button = get_mouse_bindcode(name, error);
1592 }
1593 return button;
1594}