1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
#include <stdint.h>
#include <string.h>
// TODO: BSD et al
#include <linux/input-event-codes.h>
#include <wayland-server.h>
#include <wlr/types/wlr_cursor.h>
#include <wlr/util/log.h>
#include "rootston/config.h"
#include "rootston/input.h"
#include "rootston/desktop.h"
const struct roots_input_event *get_input_event(struct roots_input *input,
uint32_t serial) {
size_t len = sizeof(input->input_events) / sizeof(*input->input_events);
for (size_t i = 0; i < len; ++i) {
if (input->input_events[i].cursor
&& input->input_events[i].serial == serial) {
return &input->input_events[i];
}
}
return NULL;
}
void view_begin_move(struct roots_input *input, struct wlr_cursor *cursor,
struct roots_view *view) {
input->mode = ROOTS_CURSOR_MOVE;
input->offs_x = cursor->x - view->x;
input->offs_y = cursor->y - view->y;
wlr_seat_pointer_clear_focus(input->wl_seat);
}
void view_begin_resize(struct roots_input *input, struct wlr_cursor *cursor,
struct roots_view *view, uint32_t edges) {
input->mode = ROOTS_CURSOR_RESIZE;
wlr_log(L_DEBUG, "begin resize");
input->offs_x = cursor->x;
input->offs_y = cursor->y;
input->view_x = view->x;
input->view_y = view->y;
struct wlr_box size;
view_get_size(view, &size);
input->view_width = size.width;
input->view_height = size.height;
input->resize_edges = edges;
wlr_seat_pointer_clear_focus(input->wl_seat);
}
void cursor_update_position(struct roots_input *input, uint32_t time) {
struct roots_desktop *desktop = input->server->desktop;
struct roots_view *view;
struct wlr_surface *surface;
double sx, sy;
switch (input->mode) {
case ROOTS_CURSOR_PASSTHROUGH:
view = view_at(desktop, input->cursor->x, input->cursor->y, &surface,
&sx, &sy);
if (view) {
wlr_seat_pointer_enter(input->wl_seat, surface, sx, sy);
wlr_seat_pointer_send_motion(input->wl_seat, time, sx, sy);
} else {
wlr_seat_pointer_clear_focus(input->wl_seat);
}
break;
case ROOTS_CURSOR_MOVE:
if (input->active_view) {
input->active_view->x = input->cursor->x - input->offs_x;
input->active_view->y = input->cursor->y - input->offs_y;
}
break;
case ROOTS_CURSOR_RESIZE:
if (input->active_view) {
int dx = input->cursor->x - input->offs_x;
int dy = input->cursor->y - input->offs_y;
int width = input->view_width;
int height = input->view_height;
if (input->resize_edges & ROOTS_CURSOR_RESIZE_EDGE_TOP) {
input->active_view->y = input->view_y + dy;
height -= dy;
}
if (input->resize_edges & ROOTS_CURSOR_RESIZE_EDGE_BOTTOM) {
height += dy;
}
if (input->resize_edges & ROOTS_CURSOR_RESIZE_EDGE_LEFT) {
input->active_view->x = input->view_x + dx;
width -= dx;
}
if (input->resize_edges & ROOTS_CURSOR_RESIZE_EDGE_RIGHT) {
width += dx;
}
view_resize(input->active_view, width, height);
}
break;
case ROOTS_CURSOR_ROTATE:
break;
}
}
static void set_view_focus(struct roots_input *input,
struct roots_desktop *desktop, struct roots_view *view) {
if (input->active_view == view) {
return;
}
size_t index = 0;
for (size_t i = 0; i < desktop->views->length; ++i) {
struct roots_view *_view = desktop->views->items[i];
view_activate(_view, _view == view);
if (view == _view) {
index = i;
}
}
input->active_view = view;
input->mode = ROOTS_CURSOR_PASSTHROUGH;
// TODO: list_swap
list_del(desktop->views, index);
list_add(desktop->views, view);
}
static void handle_cursor_motion(struct wl_listener *listener, void *data) {
struct roots_input *input = wl_container_of(listener, input, cursor_motion);
struct wlr_event_pointer_motion *event = data;
wlr_cursor_move(input->cursor, event->device,
event->delta_x, event->delta_y);
cursor_update_position(input, (uint32_t)event->time_usec);
}
static void handle_cursor_motion_absolute(struct wl_listener *listener,
void *data) {
struct roots_input *input = wl_container_of(listener,
input, cursor_motion_absolute);
struct wlr_event_pointer_motion_absolute *event = data;
wlr_cursor_warp_absolute(input->cursor, event->device,
event->x_mm / event->width_mm, event->y_mm / event->height_mm);
cursor_update_position(input, (uint32_t)event->time_usec);
}
static void handle_cursor_axis(struct wl_listener *listener, void *data) {
struct roots_input *input =
wl_container_of(listener, input, cursor_axis);
struct wlr_event_pointer_axis *event = data;
wlr_seat_pointer_send_axis(input->wl_seat, event->time_sec,
event->orientation, event->delta);
}
static void do_cursor_button_press(struct roots_input *input,
struct wlr_cursor *cursor, struct wlr_input_device *device,
uint32_t time, uint32_t button, uint32_t state) {
struct roots_desktop *desktop = input->server->desktop;
struct wlr_surface *surface;
double sx, sy;
struct roots_view *view = view_at(desktop,
input->cursor->x, input->cursor->y, &surface, &sx, &sy);
uint32_t serial = wlr_seat_pointer_send_button(
input->wl_seat, time, button, state);
int i;
switch (state) {
case WLR_BUTTON_RELEASED:
input->active_view = NULL;
input->mode = ROOTS_CURSOR_PASSTHROUGH;
break;
case WLR_BUTTON_PRESSED:
i = input->input_events_idx;
input->input_events[i].serial = serial;
input->input_events[i].cursor = cursor;
input->input_events[i].device = device;
input->input_events_idx = (i + 1)
% (sizeof(input->input_events) / sizeof(input->input_events[0]));
set_view_focus(input, desktop, view);
if (view) {
wlr_seat_keyboard_enter(input->wl_seat, surface);
}
break;
}
}
static void handle_cursor_button(struct wl_listener *listener, void *data) {
struct roots_input *input = wl_container_of(listener, input, cursor_button);
struct wlr_event_pointer_button *event = data;
do_cursor_button_press(input, input->cursor, event->device,
(uint32_t)event->time_usec, event->button, event->state);
}
static void handle_tool_axis(struct wl_listener *listener, void *data) {
struct roots_input *input = wl_container_of(listener, input, cursor_tool_axis);
struct wlr_event_tablet_tool_axis *event = data;
if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_X) &&
(event->updated_axes & WLR_TABLET_TOOL_AXIS_Y)) {
wlr_cursor_warp_absolute(input->cursor, event->device,
event->x_mm / event->width_mm, event->y_mm / event->height_mm);
cursor_update_position(input, (uint32_t)event->time_usec);
}
}
static void handle_tool_tip(struct wl_listener *listener, void *data) {
struct roots_input *input = wl_container_of(listener, input, cursor_tool_tip);
struct wlr_event_tablet_tool_tip *event = data;
do_cursor_button_press(input, input->cursor, event->device,
(uint32_t)event->time_usec, BTN_LEFT, event->state);
}
void cursor_initialize(struct roots_input *input) {
struct wlr_cursor *cursor = input->cursor;
wl_list_init(&input->cursor_motion.link);
wl_signal_add(&cursor->events.motion, &input->cursor_motion);
input->cursor_motion.notify = handle_cursor_motion;
wl_list_init(&input->cursor_motion_absolute.link);
wl_signal_add(&cursor->events.motion_absolute,
&input->cursor_motion_absolute);
input->cursor_motion_absolute.notify = handle_cursor_motion_absolute;
wl_list_init(&input->cursor_button.link);
wl_signal_add(&cursor->events.button, &input->cursor_button);
input->cursor_button.notify = handle_cursor_button;
wl_list_init(&input->cursor_axis.link);
wl_signal_add(&cursor->events.axis, &input->cursor_axis);
input->cursor_axis.notify = handle_cursor_axis;
wl_list_init(&input->cursor_tool_axis.link);
wl_signal_add(&cursor->events.tablet_tool_axis, &input->cursor_tool_axis);
input->cursor_tool_axis.notify = handle_tool_axis;
wl_list_init(&input->cursor_tool_tip.link);
wl_signal_add(&cursor->events.tablet_tool_tip, &input->cursor_tool_tip);
input->cursor_tool_tip.notify = handle_tool_tip;
}
static void reset_device_mappings(struct roots_config *config,
struct wlr_cursor *cursor, struct wlr_input_device *device) {
wlr_cursor_map_input_to_output(cursor, device, NULL);
struct device_config *dconfig;
if ((dconfig = config_get_device(config, device))) {
wlr_cursor_map_input_to_region(cursor, device, dconfig->mapped_box);
}
}
static void set_device_output_mappings(struct roots_config *config,
struct wlr_cursor *cursor, struct wlr_output *output,
struct wlr_input_device *device) {
struct device_config *dconfig;
dconfig = config_get_device(config, device);
if (dconfig && dconfig->mapped_output &&
strcmp(dconfig->mapped_output, output->name) == 0) {
wlr_cursor_map_input_to_output(cursor, device, output);
}
}
void cursor_load_config(struct roots_config *config,
struct wlr_cursor *cursor,
struct roots_input *input,
struct roots_desktop *desktop) {
struct roots_pointer *pointer;
struct roots_touch *touch;
struct roots_tablet_tool *tablet_tool;
struct roots_output *output;
// reset mappings
wlr_cursor_map_to_output(cursor, NULL);
wl_list_for_each(pointer, &input->pointers, link) {
reset_device_mappings(config, cursor, pointer->device);
}
wl_list_for_each(touch, &input->touch, link) {
reset_device_mappings(config, cursor, touch->device);
}
wl_list_for_each(tablet_tool, &input->tablet_tools, link) {
reset_device_mappings(config, cursor, tablet_tool->device);
}
// configure device to output mappings
const char *mapped_output = config->cursor.mapped_output;
wl_list_for_each(output, &desktop->outputs, link) {
if (mapped_output && strcmp(mapped_output, output->wlr_output->name) == 0) {
wlr_cursor_map_to_output(cursor, output->wlr_output);
}
wl_list_for_each(pointer, &input->pointers, link) {
set_device_output_mappings(config, cursor, output->wlr_output,
pointer->device);
}
wl_list_for_each(tablet_tool, &input->tablet_tools, link) {
set_device_output_mappings(config, cursor, output->wlr_output,
tablet_tool->device);
}
wl_list_for_each(touch, &input->touch, link) {
set_device_output_mappings(config, cursor, output->wlr_output,
touch->device);
}
}
}
|