1 #define _POSIX_C_SOURCE 200809L
2 #include <linux/input-event-codes.h>
3
4 #include <strings.h>
5 #include <wlr/types/wlr_cursor.h>
6 #include <wlr/types/wlr_pointer.h>
7 #include "sway/commands.h"
8 #include "sway/input/cursor.h"
9
10 static struct cmd_results *press_or_release(struct sway_cursor *cursor,
11 char *action, char *button_str);
12
13 static const char expected_syntax[] = "Expected 'cursor <move> <x> <y>' or "
14 "'cursor <set> <x> <y>' or "
15 "'cursor <press|release> <button[1-9]|event-name-or-code>'";
16
handle_command(struct sway_cursor * cursor,int argc,char ** argv)17 static struct cmd_results *handle_command(struct sway_cursor *cursor,
18 int argc, char **argv) {
19 if (strcasecmp(argv[0], "move") == 0) {
20 if (argc < 3) {
21 return cmd_results_new(CMD_INVALID, expected_syntax);
22 }
23 int delta_x = strtol(argv[1], NULL, 10);
24 int delta_y = strtol(argv[2], NULL, 10);
25 wlr_cursor_move(cursor->cursor, NULL, delta_x, delta_y);
26 cursor_rebase(cursor);
27 wlr_seat_pointer_notify_frame(cursor->seat->wlr_seat);
28 } else if (strcasecmp(argv[0], "set") == 0) {
29 if (argc < 3) {
30 return cmd_results_new(CMD_INVALID, expected_syntax);
31 }
32 // map absolute coords (0..1,0..1) to root container coords
33 float x = strtof(argv[1], NULL) / root->width;
34 float y = strtof(argv[2], NULL) / root->height;
35 wlr_cursor_warp_absolute(cursor->cursor, NULL, x, y);
36 cursor_rebase(cursor);
37 wlr_seat_pointer_notify_frame(cursor->seat->wlr_seat);
38 } else {
39 if (argc < 2) {
40 return cmd_results_new(CMD_INVALID, expected_syntax);
41 }
42 struct cmd_results *error = NULL;
43 if ((error = press_or_release(cursor, argv[0], argv[1]))) {
44 return error;
45 }
46 }
47
48 return cmd_results_new(CMD_SUCCESS, NULL);
49 }
50
seat_cmd_cursor(int argc,char ** argv)51 struct cmd_results *seat_cmd_cursor(int argc, char **argv) {
52 struct cmd_results *error = NULL;
53 if ((error = checkarg(argc, "cursor", EXPECTED_AT_LEAST, 2))) {
54 return error;
55 }
56 struct seat_config *sc = config->handler_context.seat_config;
57 if (!sc) {
58 return cmd_results_new(CMD_FAILURE, "No seat defined");
59 }
60
61 if (config->reading || !config->active) {
62 return cmd_results_new(CMD_DEFER, NULL);
63 }
64
65 if (strcmp(sc->name, "*") != 0) {
66 struct sway_seat *seat = input_manager_get_seat(sc->name, false);
67 if (!seat) {
68 return cmd_results_new(CMD_FAILURE,
69 "Seat %s does not exist", sc->name);
70 }
71 error = handle_command(seat->cursor, argc, argv);
72 } else {
73 struct sway_seat *seat = NULL;
74 wl_list_for_each(seat, &server.input->seats, link) {
75 error = handle_command(seat->cursor, argc, argv);
76 if (error && error->status != CMD_SUCCESS) {
77 break;
78 }
79 }
80 }
81
82 return error ? error : cmd_results_new(CMD_SUCCESS, NULL);
83 }
84
press_or_release(struct sway_cursor * cursor,char * action,char * button_str)85 static struct cmd_results *press_or_release(struct sway_cursor *cursor,
86 char *action, char *button_str) {
87 enum wlr_button_state state;
88 uint32_t button;
89 if (strcasecmp(action, "press") == 0) {
90 state = WLR_BUTTON_PRESSED;
91 } else if (strcasecmp(action, "release") == 0) {
92 state = WLR_BUTTON_RELEASED;
93 } else {
94 return cmd_results_new(CMD_INVALID, expected_syntax);
95 }
96
97 char *message = NULL;
98 button = get_mouse_button(button_str, &message);
99 if (message) {
100 struct cmd_results *error =
101 cmd_results_new(CMD_INVALID, message);
102 free(message);
103 return error;
104 } else if (button == SWAY_SCROLL_UP || button == SWAY_SCROLL_DOWN
105 || button == SWAY_SCROLL_LEFT || button == SWAY_SCROLL_RIGHT) {
106 // Dispatch axis event
107 enum wlr_axis_orientation orientation =
108 (button == SWAY_SCROLL_UP || button == SWAY_SCROLL_DOWN)
109 ? WLR_AXIS_ORIENTATION_VERTICAL
110 : WLR_AXIS_ORIENTATION_HORIZONTAL;
111 double delta = (button == SWAY_SCROLL_UP || button == SWAY_SCROLL_LEFT)
112 ? -1 : 1;
113 struct wlr_event_pointer_axis event = {
114 .device = NULL,
115 .time_msec = 0,
116 .source = WLR_AXIS_SOURCE_WHEEL,
117 .orientation = orientation,
118 .delta = delta * 15,
119 .delta_discrete = delta
120 };
121 dispatch_cursor_axis(cursor, &event);
122 wlr_seat_pointer_notify_frame(cursor->seat->wlr_seat);
123 return cmd_results_new(CMD_SUCCESS, NULL);
124 } else if (!button) {
125 return cmd_results_new(CMD_INVALID, "Unknown button %s", button_str);
126 }
127 dispatch_cursor_button(cursor, NULL, 0, button, state);
128 wlr_seat_pointer_notify_frame(cursor->seat->wlr_seat);
129 return cmd_results_new(CMD_SUCCESS, NULL);
130 }
131