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
|
#define _XOPEN_SOURCE 700
#include <stdlib.h>
#include <math.h>
#ifdef __linux__
#include <linux/input-event-codes.h>
#elif __FreeBSD__
#include <dev/evdev/input-event-codes.h>
#endif
#include <wlr/util/log.h>
#include "rootston/cursor.h"
struct roots_cursor *roots_cursor_create(struct roots_seat *seat) {
struct roots_cursor *cursor = calloc(1, sizeof(struct roots_cursor));
if (!cursor) {
return NULL;
}
cursor->cursor = wlr_cursor_create();
if (!cursor->cursor) {
return NULL;
}
return cursor;
}
void roots_cursor_destroy(struct roots_cursor *cursor) {
// TODO
}
static void cursor_set_xcursor_image(struct roots_cursor *cursor,
struct wlr_xcursor_image *image) {
wlr_cursor_set_image(cursor->cursor, image->buffer, image->width,
image->width, image->height, image->hotspot_x, image->hotspot_y);
}
static void roots_cursor_update_position(struct roots_cursor *cursor, uint32_t time) {
struct roots_desktop *desktop = cursor->seat->input->server->desktop;
struct roots_seat *seat = cursor->seat;
struct roots_view *view;
struct wlr_surface *surface;
double sx, sy;
switch (cursor->mode) {
case ROOTS_CURSOR_PASSTHROUGH:
view = view_at(desktop, cursor->cursor->x, cursor->cursor->y,
&surface, &sx, &sy);
bool set_compositor_cursor = !view && cursor->cursor_client;
if (view) {
struct wl_client *view_client =
wl_resource_get_client(view->wlr_surface->resource);
set_compositor_cursor = view_client != cursor->cursor_client;
}
if (set_compositor_cursor) {
struct wlr_xcursor *xcursor = get_default_xcursor(cursor->xcursor_theme);
cursor_set_xcursor_image(cursor, xcursor->images[0]);
cursor->cursor_client = NULL;
}
if (view) {
wlr_seat_pointer_notify_enter(seat->seat, surface, sx, sy);
wlr_seat_pointer_notify_motion(seat->seat, time, sx, sy);
} else {
wlr_seat_pointer_clear_focus(seat->seat);
}
break;
case ROOTS_CURSOR_MOVE:
if (seat->focus) {
double dx = cursor->cursor->x - cursor->offs_x;
double dy = cursor->cursor->y - cursor->offs_y;
view_move(seat->focus, cursor->view_x + dx,
cursor->view_y + dy);
}
break;
case ROOTS_CURSOR_RESIZE:
if (seat->focus) {
double dx = cursor->cursor->x - cursor->offs_x;
double dy = cursor->cursor->y - cursor->offs_y;
double active_x = seat->focus->x;
double active_y = seat->focus->y;
int width = cursor->view_width;
int height = cursor->view_height;
if (cursor->resize_edges & ROOTS_CURSOR_RESIZE_EDGE_TOP) {
active_y = cursor->view_y + dy;
height -= dy;
if (height < 0) {
active_y += height;
}
} else if (cursor->resize_edges & ROOTS_CURSOR_RESIZE_EDGE_BOTTOM) {
height += dy;
}
if (cursor->resize_edges & ROOTS_CURSOR_RESIZE_EDGE_LEFT) {
active_x = cursor->view_x + dx;
width -= dx;
if (width < 0) {
active_x += width;
}
} else if (cursor->resize_edges & ROOTS_CURSOR_RESIZE_EDGE_RIGHT) {
width += dx;
}
if (width < 0) {
width = 0;
}
if (height < 0) {
height = 0;
}
if (active_x != seat->focus->x ||
active_y != seat->focus->y) {
view_move_resize(seat->focus, active_x, active_y,
width, height);
} else {
view_resize(seat->focus, width, height);
}
}
break;
case ROOTS_CURSOR_ROTATE:
if (seat->focus) {
struct roots_view *view = seat->focus;
int ox = view->x + view->wlr_surface->current->width/2,
oy = view->y + view->wlr_surface->current->height/2;
int ux = cursor->offs_x - ox,
uy = cursor->offs_y - oy;
int vx = cursor->cursor->x - ox,
vy = cursor->cursor->y - oy;
float angle = atan2(vx*uy - vy*ux, vx*ux + vy*uy);
int steps = 12;
angle = round(angle/M_PI*steps) / (steps/M_PI);
view->rotation = cursor->view_rotation + angle;
}
break;
}
}
static void roots_cursor_press_button(struct roots_cursor *cursor,
struct wlr_input_device *device, uint32_t time, uint32_t button,
uint32_t state) {
struct roots_seat *seat = cursor->seat;
struct roots_desktop *desktop = seat->input->server->desktop;
struct wlr_surface *surface;
double sx, sy;
struct roots_view *view = view_at(desktop,
cursor->cursor->x, cursor->cursor->y, &surface, &sx, &sy);
if (state == WLR_BUTTON_PRESSED && view && roots_seat_has_meta_pressed(seat)) {
// TODO
roots_seat_focus_view(seat, view);
uint32_t edges;
switch (button) {
case BTN_LEFT:
// TODO
roots_seat_begin_move(seat, view);
break;
case BTN_RIGHT:
edges = 0;
if (sx < view->wlr_surface->current->width/2) {
edges |= ROOTS_CURSOR_RESIZE_EDGE_LEFT;
} else {
edges |= ROOTS_CURSOR_RESIZE_EDGE_RIGHT;
}
if (sy < view->wlr_surface->current->height/2) {
edges |= ROOTS_CURSOR_RESIZE_EDGE_TOP;
} else {
edges |= ROOTS_CURSOR_RESIZE_EDGE_BOTTOM;
}
roots_seat_begin_resize(seat, view, edges);
break;
case BTN_MIDDLE:
roots_seat_begin_rotate(seat, view);
break;
}
return;
}
// TODO
uint32_t serial =
wlr_seat_pointer_notify_button(seat->seat, time, button, state);
int i;
switch (state) {
case WLR_BUTTON_RELEASED:
seat->cursor->mode = ROOTS_CURSOR_PASSTHROUGH;
roots_cursor_update_position(cursor, time);
break;
case WLR_BUTTON_PRESSED:
// TODO
i = cursor->input_events_idx;
cursor->input_events[i].serial = serial;
cursor->input_events[i].cursor = cursor->cursor;
cursor->input_events[i].device = device;
cursor->input_events_idx = (i + 1)
% (sizeof(cursor->input_events) / sizeof(cursor->input_events[0]));
roots_seat_focus_view(seat, view);
break;
}
}
void roots_cursor_handle_motion(struct roots_cursor *cursor,
struct wlr_event_pointer_motion *event) {
wlr_cursor_move(cursor->cursor, event->device,
event->delta_x, event->delta_y);
roots_cursor_update_position(cursor, event->time_msec);
}
void roots_cursor_handle_motion_absolute(struct roots_cursor *cursor,
struct wlr_event_pointer_motion_absolute *event) {
wlr_cursor_warp_absolute(cursor->cursor, event->device,
event->x_mm / event->width_mm, event->y_mm / event->height_mm);
roots_cursor_update_position(cursor, event->time_msec);
}
void roots_cursor_handle_button(struct roots_cursor *cursor,
struct wlr_event_pointer_button *event) {
roots_cursor_press_button(cursor, event->device, event->time_msec,
event->button, event->state);
}
void roots_cursor_handle_axis(struct roots_cursor *cursor,
struct wlr_event_pointer_axis *event) {
wlr_seat_pointer_notify_axis(cursor->seat->seat, event->time_msec,
event->orientation, event->delta);
}
void roots_cursor_handle_touch_down(struct roots_cursor *cursor,
struct wlr_event_touch_down *event) {
struct roots_touch_point *point =
calloc(1, sizeof(struct roots_touch_point));
if (!point) {
wlr_log(L_ERROR, "could not allocate memory for touch point");
return;
}
point->device = event->device->data;
point->slot = event->slot;
point->x = event->x_mm / event->width_mm;
point->y = event->y_mm / event->height_mm;
wlr_cursor_warp_absolute(cursor->cursor, event->device, point->x, point->y);
roots_cursor_update_position(cursor, event->time_msec);
wl_list_insert(&cursor->touch_points, &point->link);
roots_cursor_press_button(cursor, event->device,
event->time_msec, BTN_LEFT, 1);
}
void roots_cursor_handle_touch_up(struct roots_cursor *cursor,
struct wlr_event_touch_up *event) {
struct roots_touch_point *point;
wl_list_for_each(point, &cursor->touch_points, link) {
if (point->slot == event->slot) {
wl_list_remove(&point->link);
free(point);
break;
}
}
roots_cursor_press_button(cursor, event->device,
event->time_msec, BTN_LEFT, 0);
}
void roots_cursor_handle_touch_motion(struct roots_cursor *cursor,
struct wlr_event_touch_motion *event) {
struct roots_touch_point *point;
wl_list_for_each(point, &cursor->touch_points, link) {
if (point->slot == event->slot) {
point->x = event->x_mm / event->width_mm;
point->y = event->y_mm / event->height_mm;
wlr_cursor_warp_absolute(cursor->cursor, event->device,
point->x, point->y);
roots_cursor_update_position(cursor, event->time_msec);
break;
}
}
}
void roots_cursor_handle_tool_axis(struct roots_cursor *cursor,
struct wlr_event_tablet_tool_axis *event) {
if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_X) &&
(event->updated_axes & WLR_TABLET_TOOL_AXIS_Y)) {
wlr_cursor_warp_absolute(cursor->cursor, event->device,
event->x_mm / event->width_mm, event->y_mm / event->height_mm);
roots_cursor_update_position(cursor, event->time_msec);
}
}
void roots_cursor_handle_tool_tip(struct roots_cursor *cursor,
struct wlr_event_tablet_tool_tip *event) {
roots_cursor_press_button(cursor, event->device,
event->time_msec, BTN_LEFT, event->state);
}
|