-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcompositor_input.cpp
More file actions
459 lines (407 loc) · 14.2 KB
/
compositor_input.cpp
File metadata and controls
459 lines (407 loc) · 14.2 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#include "compositor_state.hpp"
#include <ctime>
#include <linux/input-event-codes.h>
#include <memory>
#include <unistd.h>
extern "C" {
#include <wlr/interfaces/wlr_keyboard.h>
#include <wlr/types/wlr_keyboard.h>
#include <wlr/types/wlr_pointer_constraints_v1.h>
#include <wlr/types/wlr_relative_pointer_v1.h>
#include <wlr/types/wlr_seat.h>
#include <xkbcommon/xkbcommon.h>
}
#include <goggles/profiling.hpp>
#include <util/logging.hpp>
namespace goggles::compositor {
namespace {
auto get_time_msec() -> uint32_t {
timespec ts{};
clock_gettime(CLOCK_MONOTONIC, &ts);
return static_cast<uint32_t>(ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
}
auto sdl_to_linux_keycode(SDL_Scancode scancode) -> uint32_t {
switch (scancode) {
case SDL_SCANCODE_A:
return KEY_A;
case SDL_SCANCODE_B:
return KEY_B;
case SDL_SCANCODE_C:
return KEY_C;
case SDL_SCANCODE_D:
return KEY_D;
case SDL_SCANCODE_E:
return KEY_E;
case SDL_SCANCODE_F:
return KEY_F;
case SDL_SCANCODE_G:
return KEY_G;
case SDL_SCANCODE_H:
return KEY_H;
case SDL_SCANCODE_I:
return KEY_I;
case SDL_SCANCODE_J:
return KEY_J;
case SDL_SCANCODE_K:
return KEY_K;
case SDL_SCANCODE_L:
return KEY_L;
case SDL_SCANCODE_M:
return KEY_M;
case SDL_SCANCODE_N:
return KEY_N;
case SDL_SCANCODE_O:
return KEY_O;
case SDL_SCANCODE_P:
return KEY_P;
case SDL_SCANCODE_Q:
return KEY_Q;
case SDL_SCANCODE_R:
return KEY_R;
case SDL_SCANCODE_S:
return KEY_S;
case SDL_SCANCODE_T:
return KEY_T;
case SDL_SCANCODE_U:
return KEY_U;
case SDL_SCANCODE_V:
return KEY_V;
case SDL_SCANCODE_W:
return KEY_W;
case SDL_SCANCODE_X:
return KEY_X;
case SDL_SCANCODE_Y:
return KEY_Y;
case SDL_SCANCODE_Z:
return KEY_Z;
case SDL_SCANCODE_1:
return KEY_1;
case SDL_SCANCODE_2:
return KEY_2;
case SDL_SCANCODE_3:
return KEY_3;
case SDL_SCANCODE_4:
return KEY_4;
case SDL_SCANCODE_5:
return KEY_5;
case SDL_SCANCODE_6:
return KEY_6;
case SDL_SCANCODE_7:
return KEY_7;
case SDL_SCANCODE_8:
return KEY_8;
case SDL_SCANCODE_9:
return KEY_9;
case SDL_SCANCODE_0:
return KEY_0;
case SDL_SCANCODE_ESCAPE:
return KEY_ESC;
case SDL_SCANCODE_RETURN:
return KEY_ENTER;
case SDL_SCANCODE_BACKSPACE:
return KEY_BACKSPACE;
case SDL_SCANCODE_TAB:
return KEY_TAB;
case SDL_SCANCODE_SPACE:
return KEY_SPACE;
case SDL_SCANCODE_UP:
return KEY_UP;
case SDL_SCANCODE_DOWN:
return KEY_DOWN;
case SDL_SCANCODE_LEFT:
return KEY_LEFT;
case SDL_SCANCODE_RIGHT:
return KEY_RIGHT;
case SDL_SCANCODE_LCTRL:
return KEY_LEFTCTRL;
case SDL_SCANCODE_LSHIFT:
return KEY_LEFTSHIFT;
case SDL_SCANCODE_LALT:
return KEY_LEFTALT;
case SDL_SCANCODE_RCTRL:
return KEY_RIGHTCTRL;
case SDL_SCANCODE_RSHIFT:
return KEY_RIGHTSHIFT;
case SDL_SCANCODE_RALT:
return KEY_RIGHTALT;
default:
return 0;
}
}
auto sdl_to_linux_button(uint8_t sdl_button) -> uint32_t {
switch (sdl_button) {
case SDL_BUTTON_LEFT:
return BTN_LEFT;
case SDL_BUTTON_MIDDLE:
return BTN_MIDDLE;
case SDL_BUTTON_RIGHT:
return BTN_RIGHT;
case SDL_BUTTON_X1:
return BTN_SIDE;
case SDL_BUTTON_X2:
return BTN_EXTRA;
default:
if (sdl_button == 6) {
return BTN_FORWARD;
}
if (sdl_button == 7) {
return BTN_BACK;
}
if (sdl_button == 8) {
return BTN_TASK;
}
if (sdl_button > 8) {
GOGGLES_LOG_TRACE("Unmapped SDL button {} -> BTN_MISC+{}", sdl_button, sdl_button - 8);
return BTN_MISC + (sdl_button - 8);
}
return 0;
}
}
} // namespace
auto CompositorState::setup_input_devices() -> Result<void> {
GOGGLES_PROFILE_FUNCTION();
seat = wlr_seat_create(display, "seat0");
if (!seat) {
return make_error<void>(ErrorCode::input_init_failed, "Failed to create seat");
}
wlr_seat_set_capabilities(seat, WL_SEAT_CAPABILITY_KEYBOARD | WL_SEAT_CAPABILITY_POINTER);
xkb_ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
if (!xkb_ctx) {
return make_error<void>(ErrorCode::input_init_failed, "Failed to create xkb context");
}
xkb_keymap* keymap = xkb_keymap_new_from_names(xkb_ctx, nullptr, XKB_KEYMAP_COMPILE_NO_FLAGS);
if (!keymap) {
return make_error<void>(ErrorCode::input_init_failed, "Failed to create xkb keymap");
}
keyboard = UniqueKeyboard(std::make_unique<wlr_keyboard>().release());
wlr_keyboard_init(keyboard.get(), nullptr, "virtual-keyboard");
wlr_keyboard_set_keymap(keyboard.get(), keymap);
xkb_keymap_unref(keymap);
wlr_seat_set_keyboard(seat, keyboard.get());
relative_pointer_manager = wlr_relative_pointer_manager_v1_create(display);
if (!relative_pointer_manager) {
return make_error<void>(ErrorCode::input_init_failed,
"Failed to create relative pointer manager");
}
pointer_constraints = wlr_pointer_constraints_v1_create(display);
if (!pointer_constraints) {
return make_error<void>(ErrorCode::input_init_failed,
"Failed to create pointer constraints");
}
wl_list_init(&listeners.new_pointer_constraint.link);
listeners.new_pointer_constraint.notify = [](wl_listener* listener, void* data) {
auto* list = reinterpret_cast<Listeners*>(reinterpret_cast<char*>(listener) -
offsetof(Listeners, new_pointer_constraint));
list->state->handle_new_pointer_constraint(static_cast<wlr_pointer_constraint_v1*>(data));
};
wl_signal_add(&pointer_constraints->events.new_constraint, &listeners.new_pointer_constraint);
return {};
}
auto CompositorServer::forward_key(const SDL_KeyboardEvent& event) -> Result<void> {
GOGGLES_PROFILE_FUNCTION();
uint32_t linux_keycode = sdl_to_linux_keycode(event.scancode);
if (linux_keycode == 0) {
GOGGLES_LOG_TRACE("Unmapped key scancode={}, down={}", static_cast<int>(event.scancode),
event.down);
return {};
}
GOGGLES_LOG_TRACE("Forward key scancode={}, down={} -> linux_keycode={}",
static_cast<int>(event.scancode), event.down, linux_keycode);
InputEvent input_event{};
input_event.type = InputEventType::key;
input_event.code = linux_keycode;
input_event.pressed = event.down;
if (!inject_event(input_event)) {
GOGGLES_LOG_DEBUG("Input queue full, dropped key event");
}
return {};
}
auto CompositorServer::forward_mouse_button(const SDL_MouseButtonEvent& event) -> Result<void> {
GOGGLES_PROFILE_FUNCTION();
uint32_t button = sdl_to_linux_button(event.button);
if (button == 0) {
GOGGLES_LOG_TRACE("Unmapped mouse button {}", event.button);
return {};
}
InputEvent input_event{};
input_event.type = InputEventType::pointer_button;
input_event.code = button;
input_event.pressed = event.down;
if (!inject_event(input_event)) {
GOGGLES_LOG_DEBUG("Input queue full, dropped button event");
}
return {};
}
auto CompositorServer::forward_mouse_motion(const SDL_MouseMotionEvent& event) -> Result<void> {
GOGGLES_PROFILE_FUNCTION();
InputEvent input_event{};
input_event.type = InputEventType::pointer_motion;
input_event.dx = static_cast<double>(event.xrel);
input_event.dy = static_cast<double>(event.yrel);
if (!inject_event(input_event)) {
GOGGLES_LOG_DEBUG("Input queue full, dropped motion event");
}
return {};
}
auto CompositorServer::forward_mouse_wheel(const SDL_MouseWheelEvent& event) -> Result<void> {
GOGGLES_PROFILE_FUNCTION();
if (event.y != 0) {
InputEvent input_event{};
input_event.type = InputEventType::pointer_axis;
input_event.value = static_cast<double>(-event.y) * 15.0;
input_event.horizontal = false;
if (!inject_event(input_event)) {
GOGGLES_LOG_DEBUG("Input queue full, dropped axis event");
}
}
if (event.x != 0) {
InputEvent input_event{};
input_event.type = InputEventType::pointer_axis;
input_event.value = static_cast<double>(event.x) * 15.0;
input_event.horizontal = true;
if (!inject_event(input_event)) {
GOGGLES_LOG_DEBUG("Input queue full, dropped axis event");
}
}
return {};
}
auto CompositorServer::inject_event(const InputEvent& event) -> bool {
GOGGLES_PROFILE_FUNCTION();
if (!m_state->event_queue.try_push(event)) {
return false;
}
return m_state->wake_event_loop();
}
bool CompositorState::wake_event_loop() {
GOGGLES_PROFILE_FUNCTION();
if (!event_fd.valid()) {
return false;
}
uint64_t value = 1;
auto result = write(event_fd.get(), &value, sizeof(value));
return result == sizeof(value);
}
void CompositorState::request_focus_target(uint32_t surface_id) {
if (surface_id == NO_FOCUS_TARGET) {
return;
}
pending_focus_target.store(surface_id, std::memory_order_release);
wake_event_loop();
}
void CompositorState::request_surface_resize(uint32_t surface_id, const SurfaceResizeInfo& resize) {
if (surface_id == NO_FOCUS_TARGET) {
return;
}
SurfaceResizeRequest request{};
request.surface_id = surface_id;
request.resize = resize;
if (!resize_queue.try_push(request)) {
return;
}
wake_event_loop();
}
void CompositorState::process_input_events() {
GOGGLES_PROFILE_FUNCTION();
handle_focus_request();
handle_surface_resize_requests();
if (present_reset_requested.exchange(false, std::memory_order_acq_rel)) {
refresh_presented_frame();
}
process_capture_pacing();
while (auto event_opt = event_queue.try_pop()) {
auto& event = *event_opt;
uint32_t time = get_time_msec();
switch (event.type) {
case InputEventType::key:
handle_key_event(event, time);
break;
case InputEventType::pointer_motion:
handle_pointer_motion_event(event, time);
break;
case InputEventType::pointer_button:
handle_pointer_button_event(event, time);
break;
case InputEventType::pointer_axis:
handle_pointer_axis_event(event, time);
break;
}
}
}
void CompositorState::handle_key_event(const InputEvent& event, uint32_t time) {
GOGGLES_PROFILE_FUNCTION();
auto target = get_input_target(*this);
wlr_surface* target_surface = target.surface;
if (!target_surface) {
return;
}
this->prepare_keyboard_dispatch(target);
auto state = event.pressed ? WL_KEYBOARD_KEY_STATE_PRESSED : WL_KEYBOARD_KEY_STATE_RELEASED;
wlr_seat_keyboard_notify_key(seat, time, event.code, state);
}
void CompositorState::handle_pointer_motion_event(const InputEvent& event, uint32_t time) {
GOGGLES_PROFILE_FUNCTION();
auto root_target = get_root_input_target(*this);
if (!root_target.root_surface) {
return;
}
if (relative_pointer_manager && (event.dx != 0.0 || event.dy != 0.0)) {
wlr_relative_pointer_manager_v1_send_relative_motion(
relative_pointer_manager, seat, static_cast<uint64_t>(time) * 1000, event.dx, event.dy,
event.dx, event.dy);
}
if (active_constraint && active_constraint->type == WLR_POINTER_CONSTRAINT_V1_LOCKED) {
apply_cursor_hint_if_needed();
wlr_seat_pointer_notify_frame(seat);
return;
}
update_cursor_position(event, root_target);
auto target = resolve_input_target(*this, root_target, true);
wlr_surface* target_surface = target.surface;
if (!target_surface) {
return;
}
const auto [local_x, local_y] = get_surface_local_coords(*this, target);
this->prepare_pointer_motion_dispatch(target, local_x, local_y);
wlr_seat_pointer_notify_motion(seat, time, local_x, local_y);
wlr_seat_pointer_notify_frame(seat);
}
void CompositorState::handle_pointer_button_event(const InputEvent& event, uint32_t time) {
GOGGLES_PROFILE_FUNCTION();
auto root_target = get_root_input_target(*this);
auto target = resolve_input_target(*this, root_target, true);
wlr_surface* target_surface = target.surface;
wlr_surface* cursor_reference = target.root_surface ? target.root_surface : target_surface;
if (!target_surface) {
return;
}
if (cursor_surface != cursor_reference || !cursor_initialized) {
reset_cursor_for_surface(cursor_reference);
}
const auto [local_x, local_y] = get_surface_local_coords(*this, target);
this->prepare_pointer_button_dispatch(target, local_x, local_y);
auto state = event.pressed ? WL_POINTER_BUTTON_STATE_PRESSED : WL_POINTER_BUTTON_STATE_RELEASED;
wlr_seat_pointer_notify_button(seat, time, event.code, state);
wlr_seat_pointer_notify_frame(seat);
}
void CompositorState::handle_pointer_axis_event(const InputEvent& event, uint32_t time) {
GOGGLES_PROFILE_FUNCTION();
auto root_target = get_root_input_target(*this);
auto target = resolve_input_target(*this, root_target, true);
wlr_surface* target_surface = target.surface;
wlr_surface* cursor_reference = target.root_surface ? target.root_surface : target_surface;
if (!target_surface) {
return;
}
if (cursor_surface != cursor_reference || !cursor_initialized) {
reset_cursor_for_surface(cursor_reference);
}
const auto [local_x, local_y] = get_surface_local_coords(*this, target);
this->prepare_pointer_axis_dispatch(target, local_x, local_y);
auto orientation =
event.horizontal ? WL_POINTER_AXIS_HORIZONTAL_SCROLL : WL_POINTER_AXIS_VERTICAL_SCROLL;
wlr_seat_pointer_notify_axis(seat, time, orientation, event.value, 0,
WL_POINTER_AXIS_SOURCE_WHEEL,
WL_POINTER_AXIS_RELATIVE_DIRECTION_IDENTICAL);
wlr_seat_pointer_notify_frame(seat);
}
} // namespace goggles::compositor