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