diff options
author | 2019-01-10 10:55:22 +0000 | |
---|---|---|
committer | 2019-01-10 10:55:22 +0000 | |
commit | 15ac580b28a2906d799fd709e83cb75e0efc3f45 (patch) | |
tree | 9a1f67d98e90d12eb83ffc99b6cac659f8f3f533 /sway/input | |
parent | Merge pull request #3394 from RedSoxFan/bar-block-render (diff) | |
parent | bind{code,sym}: utilize mouse button helpers (diff) | |
download | sway-15ac580b28a2906d799fd709e83cb75e0efc3f45.tar.gz sway-15ac580b28a2906d799fd709e83cb75e0efc3f45.tar.zst sway-15ac580b28a2906d799fd709e83cb75e0efc3f45.zip |
Merge pull request #3341 from RedSoxFan/mouse-bindings-improved
Improve mouse button parsing: helpers and bind{code/sym}
Diffstat (limited to 'sway/input')
-rw-r--r-- | sway/input/cursor.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/sway/input/cursor.c b/sway/input/cursor.c index 409e7b12..9af7ef57 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> |
@@ -1533,3 +1535,66 @@ void cursor_warp_to_workspace(struct sway_cursor *cursor, | |||
1533 | 1535 | ||
1534 | wlr_cursor_warp(cursor->cursor, NULL, x, y); | 1536 | wlr_cursor_warp(cursor->cursor, NULL, x, y); |
1535 | } | 1537 | } |
1538 | |||
1539 | uint32_t get_mouse_bindsym(const char *name, char **error) { | ||
1540 | if (strncasecmp(name, "button", strlen("button")) == 0) { | ||
1541 | // Map to x11 mouse buttons | ||
1542 | int number = name[strlen("button")] - '0'; | ||
1543 | if (number < 1 || number > 9 || strlen(name) > strlen("button0")) { | ||
1544 | *error = strdup("Only buttons 1-9 are supported. For other mouse " | ||
1545 | "buttons, use the name of the event code."); | ||
1546 | return 0; | ||
1547 | } | ||
1548 | static const uint32_t buttons[] = {BTN_LEFT, BTN_MIDDLE, BTN_RIGHT, | ||
1549 | SWAY_SCROLL_UP, SWAY_SCROLL_DOWN, SWAY_SCROLL_LEFT, | ||
1550 | SWAY_SCROLL_RIGHT, BTN_SIDE, BTN_EXTRA}; | ||
1551 | return buttons[number - 1]; | ||
1552 | } else if (strncmp(name, "BTN_", strlen("BTN_")) == 0) { | ||
1553 | // Get event code from name | ||
1554 | int code = libevdev_event_code_from_name(EV_KEY, name); | ||
1555 | if (code == -1) { | ||
1556 | size_t len = snprintf(NULL, 0, "Unknown event %s", name) + 1; | ||
1557 | *error = malloc(len); | ||
1558 | if (*error) { | ||
1559 | snprintf(*error, len, "Unknown event %s", name); | ||
1560 | } | ||
1561 | return 0; | ||
1562 | } | ||
1563 | return code; | ||
1564 | } | ||
1565 | return 0; | ||
1566 | } | ||
1567 | |||
1568 | uint32_t get_mouse_bindcode(const char *name, char **error) { | ||
1569 | // Validate event code | ||
1570 | errno = 0; | ||
1571 | char *endptr; | ||
1572 | int code = strtol(name, &endptr, 10); | ||
1573 | if (endptr == name && code <= 0) { | ||
1574 | *error = strdup("Button event code must be a positive integer."); | ||
1575 | return 0; | ||
1576 | } else if (errno == ERANGE) { | ||
1577 | *error = strdup("Button event code out of range."); | ||
1578 | return 0; | ||
1579 | } | ||
1580 | const char *event = libevdev_event_code_get_name(EV_KEY, code); | ||
1581 | if (!event || strncmp(event, "BTN_", strlen("BTN_")) != 0) { | ||
1582 | size_t len = snprintf(NULL, 0, "Event code %d (%s) is not a button", | ||
1583 | code, event) + 1; | ||
1584 | *error = malloc(len); | ||
1585 | if (*error) { | ||
1586 | snprintf(*error, len, "Event code %d (%s) is not a button", | ||
1587 | code, event); | ||
1588 | } | ||
1589 | return 0; | ||
1590 | } | ||
1591 | return code; | ||
1592 | } | ||
1593 | |||
1594 | uint32_t get_mouse_button(const char *name, char **error) { | ||
1595 | uint32_t button = get_mouse_bindsym(name, error); | ||
1596 | if (!button && !error) { | ||
1597 | button = get_mouse_bindcode(name, error); | ||
1598 | } | ||
1599 | return button; | ||
1600 | } | ||