diff options
author | Danny Bautista <pyrolagus@nerdpol.ch> | 2018-04-10 11:32:37 -0400 |
---|---|---|
committer | Danny Bautista <pyrolagus@nerdpol.ch> | 2018-04-10 12:40:50 -0400 |
commit | 1edb2bd89254069066bab51d316962e85d4445b4 (patch) | |
tree | 6c9d9639fcb2d8e0e2b25553307522dd7062073b | |
parent | Merge pull request #1791 from RyanDwyer/fix-swaybar-output-config (diff) | |
download | sway-1edb2bd89254069066bab51d316962e85d4445b4.tar.gz sway-1edb2bd89254069066bab51d316962e85d4445b4.tar.zst sway-1edb2bd89254069066bab51d316962e85d4445b4.zip |
Implement cursor event simulation with sway commands.
-rw-r--r-- | include/sway/commands.h | 3 | ||||
-rw-r--r-- | include/sway/input/cursor.h | 1 | ||||
-rw-r--r-- | sway/commands.c | 1 | ||||
-rw-r--r-- | sway/commands/seat.c | 2 | ||||
-rw-r--r-- | sway/commands/seat/cursor.c | 83 | ||||
-rw-r--r-- | sway/input/cursor.c | 2 | ||||
-rw-r--r-- | sway/meson.build | 1 |
7 files changed, 91 insertions, 2 deletions
diff --git a/include/sway/commands.h b/include/sway/commands.h index bc5d5412..dbebaa49 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h | |||
@@ -204,8 +204,9 @@ sway_cmd input_cmd_xkb_options; | |||
204 | sway_cmd input_cmd_xkb_rules; | 204 | sway_cmd input_cmd_xkb_rules; |
205 | sway_cmd input_cmd_xkb_variant; | 205 | sway_cmd input_cmd_xkb_variant; |
206 | 206 | ||
207 | sway_cmd seat_cmd_fallback; | ||
208 | sway_cmd seat_cmd_attach; | 207 | sway_cmd seat_cmd_attach; |
208 | sway_cmd seat_cmd_fallback; | ||
209 | sway_cmd seat_cmd_cursor; | ||
209 | 210 | ||
210 | sway_cmd cmd_ipc_cmd; | 211 | sway_cmd cmd_ipc_cmd; |
211 | sway_cmd cmd_ipc_events; | 212 | sway_cmd cmd_ipc_events; |
diff --git a/include/sway/input/cursor.h b/include/sway/input/cursor.h index daf7d4ee..30169aa1 100644 --- a/include/sway/input/cursor.h +++ b/include/sway/input/cursor.h | |||
@@ -30,5 +30,6 @@ struct sway_cursor { | |||
30 | void sway_cursor_destroy(struct sway_cursor *cursor); | 30 | void sway_cursor_destroy(struct sway_cursor *cursor); |
31 | struct sway_cursor *sway_cursor_create(struct sway_seat *seat); | 31 | struct sway_cursor *sway_cursor_create(struct sway_seat *seat); |
32 | void cursor_send_pointer_motion(struct sway_cursor *cursor, uint32_t time); | 32 | void cursor_send_pointer_motion(struct sway_cursor *cursor, uint32_t time); |
33 | void dispatch_cursor_button(struct sway_cursor *cursor, uint32_t time_msec, uint32_t button, enum wlr_button_state state); | ||
33 | 34 | ||
34 | #endif | 35 | #endif |
diff --git a/sway/commands.c b/sway/commands.c index 55929659..54d84450 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -203,6 +203,7 @@ static struct cmd_handler input_handlers[] = { | |||
203 | // must be in order for the bsearch | 203 | // must be in order for the bsearch |
204 | static struct cmd_handler seat_handlers[] = { | 204 | static struct cmd_handler seat_handlers[] = { |
205 | { "attach", seat_cmd_attach }, | 205 | { "attach", seat_cmd_attach }, |
206 | { "cursor", seat_cmd_cursor }, | ||
206 | { "fallback", seat_cmd_fallback }, | 207 | { "fallback", seat_cmd_fallback }, |
207 | }; | 208 | }; |
208 | 209 | ||
diff --git a/sway/commands/seat.c b/sway/commands/seat.c index 819b769c..5916015f 100644 --- a/sway/commands/seat.c +++ b/sway/commands/seat.c | |||
@@ -40,6 +40,8 @@ struct cmd_results *cmd_seat(int argc, char **argv) { | |||
40 | struct cmd_results *res; | 40 | struct cmd_results *res; |
41 | if (strcasecmp("attach", argv[1]) == 0) { | 41 | if (strcasecmp("attach", argv[1]) == 0) { |
42 | res = seat_cmd_attach(argc_new, argv_new); | 42 | res = seat_cmd_attach(argc_new, argv_new); |
43 | } else if (strcasecmp("cursor", argv[1]) == 0) { | ||
44 | res = seat_cmd_cursor(argc_new, argv_new); | ||
43 | } else if (strcasecmp("fallback", argv[1]) == 0) { | 45 | } else if (strcasecmp("fallback", argv[1]) == 0) { |
44 | res = seat_cmd_fallback(argc_new, argv_new); | 46 | res = seat_cmd_fallback(argc_new, argv_new); |
45 | } else { | 47 | } else { |
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 | } | ||
diff --git a/sway/input/cursor.c b/sway/input/cursor.c index 0df01504..15a61cbf 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c | |||
@@ -166,7 +166,7 @@ static void handle_cursor_motion_absolute( | |||
166 | cursor_send_pointer_motion(cursor, event->time_msec); | 166 | cursor_send_pointer_motion(cursor, event->time_msec); |
167 | } | 167 | } |
168 | 168 | ||
169 | static void dispatch_cursor_button(struct sway_cursor *cursor, | 169 | void dispatch_cursor_button(struct sway_cursor *cursor, |
170 | uint32_t time_msec, uint32_t button, enum wlr_button_state state) { | 170 | uint32_t time_msec, uint32_t button, enum wlr_button_state state) { |
171 | struct wlr_surface *surface = NULL; | 171 | struct wlr_surface *surface = NULL; |
172 | double sx, sy; | 172 | double sx, sy; |
diff --git a/sway/meson.build b/sway/meson.build index d0730296..9e55e335 100644 --- a/sway/meson.build +++ b/sway/meson.build | |||
@@ -47,6 +47,7 @@ sway_sources = files( | |||
47 | 'commands/resize.c', | 47 | 'commands/resize.c', |
48 | 'commands/seat.c', | 48 | 'commands/seat.c', |
49 | 'commands/seat/attach.c', | 49 | 'commands/seat/attach.c', |
50 | 'commands/seat/cursor.c', | ||
50 | 'commands/seat/fallback.c', | 51 | 'commands/seat/fallback.c', |
51 | 'commands/set.c', | 52 | 'commands/set.c', |
52 | 'commands/split.c', | 53 | 'commands/split.c', |