diff options
Diffstat (limited to 'sway/commands/seat/cursor.c')
-rw-r--r-- | sway/commands/seat/cursor.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/sway/commands/seat/cursor.c b/sway/commands/seat/cursor.c new file mode 100644 index 00000000..2f2aa5ca --- /dev/null +++ b/sway/commands/seat/cursor.c | |||
@@ -0,0 +1,83 @@ | |||
1 | #define _XOPEN_SOURCE 700 | ||
2 | #ifdef __linux__ | ||
3 | #include <linux/input-event-codes.h> | ||
4 | #elif __FreeBSD__ | ||
5 | #include <dev/evdev/input-event-codes.h> | ||
6 | #endif | ||
7 | |||
8 | #include <strings.h> | ||
9 | #include <wlr/types/wlr_cursor.h> | ||
10 | #include "sway/commands.h" | ||
11 | #include "sway/input/cursor.h" | ||
12 | |||
13 | static struct cmd_results *press_or_release(struct sway_cursor *cursor, char *action, char *button_str); | ||
14 | |||
15 | static const char *expected_syntax = "Expected 'cursor <move> <x> <y>' or " | ||
16 | "'cursor <set> <x> <y>' or " | ||
17 | "'curor <press|release> <left|right|1|2|3...>'"; | ||
18 | |||
19 | struct cmd_results *seat_cmd_cursor(int argc, char **argv) { | ||
20 | struct cmd_results *error = NULL; | ||
21 | if ((error = checkarg(argc, "cursor", EXPECTED_AT_LEAST, 2))) { | ||
22 | return error; | ||
23 | } | ||
24 | struct sway_seat *seat = config->handler_context.seat; | ||
25 | if (!seat) { | ||
26 | return cmd_results_new(CMD_FAILURE, "cursor", "No seat defined"); | ||
27 | } | ||
28 | |||
29 | struct sway_cursor *cursor = seat->cursor; | ||
30 | |||
31 | if (strcasecmp(argv[0], "move") == 0) { | ||
32 | if (argc < 3) { | ||
33 | return cmd_results_new(CMD_INVALID, "cursor", expected_syntax); | ||
34 | } | ||
35 | int delta_x = strtol(argv[1], NULL, 10); | ||
36 | int delta_y = strtol(argv[2], NULL, 10); | ||
37 | wlr_cursor_move(cursor->cursor, NULL, delta_x, delta_y); | ||
38 | cursor_send_pointer_motion(cursor, 1); | ||
39 | } else if (strcasecmp(argv[0], "set") == 0) { | ||
40 | if (argc < 3) { | ||
41 | return cmd_results_new(CMD_INVALID, "cursor", expected_syntax); | ||
42 | } | ||
43 | // map absolute coords (0..1,0..1) to root container coords | ||
44 | float x = strtof(argv[1], NULL) / root_container.width; | ||
45 | float y = strtof(argv[2], NULL) / root_container.height; | ||
46 | wlr_cursor_warp_absolute(cursor->cursor, NULL, x, y); | ||
47 | cursor_send_pointer_motion(cursor, 0); | ||
48 | } else { | ||
49 | if (argc < 2) { | ||
50 | return cmd_results_new(CMD_INVALID, "cursor", expected_syntax); | ||
51 | } | ||
52 | if ((error = press_or_release(cursor, argv[0], argv[1]))) { | ||
53 | return error; | ||
54 | } | ||
55 | } | ||
56 | |||
57 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
58 | } | ||
59 | |||
60 | static struct cmd_results *press_or_release(struct sway_cursor *cursor, char *action, char *button_str) { | ||
61 | enum wlr_button_state state; | ||
62 | uint32_t button; | ||
63 | if (strcasecmp(action, "press") == 0) { | ||
64 | state = WLR_BUTTON_PRESSED; | ||
65 | } else if (strcasecmp(action, "release") == 0) { | ||
66 | state = WLR_BUTTON_RELEASED; | ||
67 | } else { | ||
68 | return cmd_results_new(CMD_INVALID, "cursor", expected_syntax); | ||
69 | } | ||
70 | |||
71 | if (strcasecmp(button_str, "left") == 0) { | ||
72 | button = BTN_LEFT; | ||
73 | } else if (strcasecmp(button_str, "right") == 0) { | ||
74 | button = BTN_RIGHT; | ||
75 | } else { | ||
76 | button = strtol(button_str, NULL, 10); | ||
77 | if (button == 0) { | ||
78 | return cmd_results_new(CMD_INVALID, "cursor", expected_syntax); | ||
79 | } | ||
80 | } | ||
81 | dispatch_cursor_button(cursor, 1, button, state); | ||
82 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
83 | } | ||