-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathdisplay.cpp
More file actions
702 lines (613 loc) · 24.9 KB
/
display.cpp
File metadata and controls
702 lines (613 loc) · 24.9 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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/*
* Copyright (C) 2015, 2016 Igalia S.L.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "display.h"
#ifdef BACKEND_BCM_NEXUS_WAYLAND
#include "nsc-client-protocol.h"
#endif
#include "xdg-shell-client-protocol.h"
#include "wayland-client-protocol.h"
#include <cassert>
#include <cstring>
#include <glib.h>
#include <linux/input.h>
#include <locale.h>
#include <memory>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
#include <wayland-client.h>
namespace Wayland {
class EventSource {
public:
static GSourceFuncs sourceFuncs;
GSource source;
GPollFD pfd;
struct wl_display* display;
};
GSourceFuncs EventSource::sourceFuncs = {
// prepare
[](GSource* base, gint* timeout) -> gboolean
{
auto* source = reinterpret_cast<EventSource*>(base);
struct wl_display* display = source->display;
*timeout = -1;
while (wl_display_prepare_read(display) != 0) {
if (wl_display_dispatch_pending(display) < 0) {
fprintf(stderr, "Wayland::Display: error in wayland prepare\n");
return FALSE;
}
}
wl_display_flush(display);
return FALSE;
},
// check
[](GSource* base) -> gboolean
{
auto* source = reinterpret_cast<EventSource*>(base);
struct wl_display* display = source->display;
if (source->pfd.revents & G_IO_IN) {
if (wl_display_read_events(display) < 0) {
fprintf(stderr, "Wayland::Display: error in wayland read\n");
return FALSE;
}
return TRUE;
} else {
wl_display_cancel_read(display);
return FALSE;
}
},
// dispatch
[](GSource* base, GSourceFunc, gpointer) -> gboolean
{
auto* source = reinterpret_cast<EventSource*>(base);
struct wl_display* display = source->display;
if (source->pfd.revents & G_IO_IN) {
if (wl_display_dispatch_pending(display) < 0) {
fprintf(stderr, "Wayland::Display: error in wayland dispatch\n");
return G_SOURCE_REMOVE;
}
}
if (source->pfd.revents & (G_IO_ERR | G_IO_HUP))
return G_SOURCE_REMOVE;
source->pfd.revents = 0;
return G_SOURCE_CONTINUE;
},
nullptr, // finalize
nullptr, // closure_callback
nullptr, // closure_marshall
};
const struct wl_registry_listener g_registryListener = {
// global
[](void* data, struct wl_registry* registry, uint32_t name, const char* interface, uint32_t version)
{
auto& interfaces = *static_cast<Display::Interfaces*>(data);
if (!std::strcmp(interface, "wl_compositor"))
interfaces.compositor = static_cast<struct wl_compositor*>(wl_registry_bind(registry, name, &wl_compositor_interface, 1));
#ifdef BACKEND_BCM_NEXUS_WAYLAND
if (!std::strcmp(interface, "wl_nsc"))
interfaces.nsc = static_cast<struct wl_nsc*>(wl_registry_bind(registry, name, &wl_nsc_interface, version));
#endif
if (!std::strcmp(interface, "wl_seat"))
interfaces.seat = static_cast<struct wl_seat*>(wl_registry_bind(registry, name, &wl_seat_interface, 4));
if (!std::strcmp(interface, "xdg_shell"))
interfaces.xdg = static_cast<struct xdg_shell*>(wl_registry_bind(registry, name, &xdg_shell_interface, 1));
if (!std::strcmp(interface, "wl_shell"))
interfaces.shell = static_cast<struct wl_shell*>(wl_registry_bind(registry, name, &wl_shell_interface, 1));
},
// global_remove
[](void*, struct wl_registry*, uint32_t) { },
};
static const struct xdg_shell_listener g_xdgShellListener = {
// ping
[](void*, struct xdg_shell* shell, uint32_t serial)
{
xdg_shell_pong(shell, serial);
},
};
static uint32_t
getModifiers(Display::SeatData& seatData)
{
uint32_t mask = seatData.keyboard.modifiers;
if (seatData.pointer.object)
mask |= seatData.pointer.modifiers;
return mask;
}
static const struct wl_pointer_listener g_pointerListener = {
// enter
[](void* data, struct wl_pointer*, uint32_t serial, struct wl_surface* surface, wl_fixed_t, wl_fixed_t)
{
auto& seatData = *static_cast<Display::SeatData*>(data);
seatData.serial = serial;
auto it = seatData.inputClients.find(surface);
if (it != seatData.inputClients.end())
seatData.pointer.target = *it;
},
// leave
[](void* data, struct wl_pointer*, uint32_t serial, struct wl_surface* surface)
{
auto& seatData = *static_cast<Display::SeatData*>(data);
seatData.serial = serial;
auto it = seatData.inputClients.find(surface);
if (it != seatData.inputClients.end() && seatData.pointer.target.first == it->first)
seatData.pointer.target = { nullptr, nullptr };
},
// motion
[](void* data, struct wl_pointer*, uint32_t time, wl_fixed_t fixedX, wl_fixed_t fixedY)
{
auto x = wl_fixed_to_int(fixedX);
auto y = wl_fixed_to_int(fixedY);
auto& seatData = *static_cast<Display::SeatData*>(data);
auto& pointer = seatData.pointer;
pointer.coords = { x, y };
struct wpe_input_pointer_event event = { wpe_input_pointer_event_type_motion, time, x, y, pointer.button, pointer.state, getModifiers(seatData) };
EventDispatcher::singleton().sendEvent( event );
if (pointer.target.first) {
struct wpe_view_backend* backend = pointer.target.second;
wpe_view_backend_dispatch_pointer_event(backend, &event);
}
},
// button
[](void* data, struct wl_pointer*, uint32_t serial, uint32_t time, uint32_t button, uint32_t state)
{
auto& seatData = *static_cast<Display::SeatData*>(data);
seatData.serial = serial;
if (button >= BTN_MOUSE)
button = button - BTN_MOUSE + 1;
else
button = 0;
auto& pointer = seatData.pointer;
auto& coords = pointer.coords;
pointer.button = !!state ? button : 0;
pointer.state = state;
uint32_t modifier = 0;
switch (button) {
case 1:
modifier = wpe_input_pointer_modifier_button1;
break;
case 2:
modifier = wpe_input_pointer_modifier_button2;
break;
case 3:
modifier = wpe_input_pointer_modifier_button3;
break;
case 4:
modifier = wpe_input_pointer_modifier_button4;
break;
case 5:
modifier = wpe_input_pointer_modifier_button5;
break;
default:
break;
}
if (state)
seatData.pointer.modifiers |= modifier;
else
seatData.pointer.modifiers &= ~modifier;
struct wpe_input_pointer_event event = { wpe_input_pointer_event_type_button, time, coords.first, coords.second, button, state, getModifiers(seatData) };
EventDispatcher::singleton().sendEvent( event );
if (pointer.target.first) {
struct wpe_view_backend* backend = pointer.target.second;
wpe_view_backend_dispatch_pointer_event(backend, &event);
}
},
// axis
[](void* data, struct wl_pointer*, uint32_t time, uint32_t axis, wl_fixed_t value)
{
auto& seatData = *static_cast<Display::SeatData*>(data);
auto& pointer = seatData.pointer;
auto& coords = pointer.coords;
struct wpe_input_axis_event event = { wpe_input_axis_event_type_motion, time, coords.first, coords.second, axis, -wl_fixed_to_int(value), getModifiers(seatData) };
EventDispatcher::singleton().sendEvent( event );
if (pointer.target.first) {
struct wpe_view_backend* backend = pointer.target.second;
wpe_view_backend_dispatch_axis_event(backend, &event);
}
},
};
static void
handleKeyEvent(Display::SeatData& seatData, uint32_t key, uint32_t state, uint32_t time)
{
uint32_t keysym = wpe_input_xkb_context_get_key_code(wpe_input_xkb_context_get_default(), key, state == WL_KEYBOARD_KEY_STATE_PRESSED);
if (!keysym)
return;
struct wpe_input_keyboard_event event = { time, keysym, key, !!state, getModifiers(seatData) };
EventDispatcher::singleton().sendEvent( event );
if (seatData.keyboard.target.first) {
struct wpe_view_backend* backend = seatData.keyboard.target.second;
wpe_view_backend_dispatch_keyboard_event(backend, &event);
}
}
static gboolean
repeatRateTimeout(void* data)
{
auto& seatData = *static_cast<Display::SeatData*>(data);
handleKeyEvent(seatData, seatData.repeatData.key, seatData.repeatData.state, seatData.repeatData.time);
return G_SOURCE_CONTINUE;
}
static gboolean
repeatDelayTimeout(void* data)
{
auto& seatData = *static_cast<Display::SeatData*>(data);
handleKeyEvent(seatData, seatData.repeatData.key, seatData.repeatData.state, seatData.repeatData.time);
seatData.repeatData.eventSource = g_timeout_add(seatData.repeatInfo.rate, static_cast<GSourceFunc>(repeatRateTimeout), data);
return G_SOURCE_REMOVE;
}
static const struct wl_keyboard_listener g_keyboardListener = {
// keymap
[](void* data, struct wl_keyboard*, uint32_t format, int fd, uint32_t size)
{
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
close(fd);
return;
}
void* mapping = mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0);
if (mapping == MAP_FAILED) {
close(fd);
return;
}
auto* xkb = wpe_input_xkb_context_get_default();
auto* keymap = xkb_keymap_new_from_string(wpe_input_xkb_context_get_context(xkb), static_cast<char*>(mapping),
XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
munmap(mapping, size);
close(fd);
wpe_input_xkb_context_set_keymap(xkb, keymap);
xkb_keymap_unref(keymap);
},
// enter
[](void* data, struct wl_keyboard*, uint32_t serial, struct wl_surface* surface, struct wl_array*)
{
auto& seatData = *static_cast<Display::SeatData*>(data);
seatData.serial = serial;
auto it = seatData.inputClients.find(surface);
if (it != seatData.inputClients.end())
seatData.keyboard.target = *it;
},
// leave
[](void* data, struct wl_keyboard*, uint32_t serial, struct wl_surface* surface)
{
auto& seatData = *static_cast<Display::SeatData*>(data);
seatData.serial = serial;
auto it = seatData.inputClients.find(surface);
if (it != seatData.inputClients.end() && seatData.keyboard.target.first == it->first)
seatData.keyboard.target = { nullptr, nullptr };
},
// key
[](void* data, struct wl_keyboard*, uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
{
// IDK.
key += 8;
auto& seatData = *static_cast<Display::SeatData*>(data);
seatData.serial = serial;
handleKeyEvent(seatData, key, state, time);
if (!seatData.repeatInfo.rate)
return;
auto* keymap = wpe_input_xkb_context_get_keymap(wpe_input_xkb_context_get_default());
if (state == WL_KEYBOARD_KEY_STATE_RELEASED
&& seatData.repeatData.key == key) {
if (seatData.repeatData.eventSource)
g_source_remove(seatData.repeatData.eventSource);
seatData.repeatData = { 0, 0, 0, 0 };
} else if (state == WL_KEYBOARD_KEY_STATE_PRESSED
&& keymap && xkb_keymap_key_repeats(keymap, key)) {
if (seatData.repeatData.eventSource)
g_source_remove(seatData.repeatData.eventSource);
seatData.repeatData = { key, time, state, g_timeout_add(seatData.repeatInfo.delay, static_cast<GSourceFunc>(repeatDelayTimeout), data) };
}
},
// modifiers
[](void* data, struct wl_keyboard*, uint32_t serial, uint32_t depressedMods, uint32_t latchedMods, uint32_t lockedMods, uint32_t group)
{
auto& seatData = *static_cast<Display::SeatData*>(data);
seatData.serial = serial;
seatData.keyboard.modifiers = wpe_input_xkb_context_get_modifiers(wpe_input_xkb_context_get_default(), depressedMods, latchedMods, lockedMods, group);
},
// repeat_info
[](void* data, struct wl_keyboard*, int32_t rate, int32_t delay)
{
auto& repeatInfo = static_cast<Display::SeatData*>(data)->repeatInfo;
repeatInfo = { rate, delay };
// A rate of zero disables any repeating.
if (!rate) {
auto& repeatData = static_cast<Display::SeatData*>(data)->repeatData;
if (repeatData.eventSource) {
g_source_remove(repeatData.eventSource);
repeatData = { 0, 0, 0, 0 };
}
}
},
};
static const struct wl_touch_listener g_touchListener = {
// down
[](void* data, struct wl_touch*, uint32_t serial, uint32_t time, struct wl_surface* surface, int32_t id, wl_fixed_t x, wl_fixed_t y)
{
auto& seatData = *static_cast<Display::SeatData*>(data);
seatData.serial = serial;
int32_t arraySize = std::tuple_size<decltype(seatData.touch.targets)>::value;
if (id < 0 || id >= arraySize)
return;
auto& touchPoints = seatData.touch.touchPoints;
touchPoints[id] = { wpe_input_touch_event_type_down, time, id, wl_fixed_to_int(x), wl_fixed_to_int(y) };
auto& target = seatData.touch.targets[id];
assert(!target.first && !target.second);
auto it = seatData.inputClients.find(surface);
if (it == seatData.inputClients.end()) {
// no surface properly registered, fallback to event_touch_simple
struct wpe_input_touch_event_raw event_touch_simple = { wpe_input_touch_event_type_down, time, id, wl_fixed_to_int(x), wl_fixed_to_int(y) };
EventDispatcher::singleton().sendEvent( event_touch_simple );
} else {
target = { surface, it->second };
struct wpe_input_touch_event event = { touchPoints.data(), touchPoints.size(), wpe_input_touch_event_type_down, id, time, getModifiers(seatData) };
struct wpe_view_backend* backend = target.second;
wpe_view_backend_dispatch_touch_event(backend, &event);
}
},
// up
[](void* data, struct wl_touch*, uint32_t serial, uint32_t time, int32_t id)
{
auto& seatData = *static_cast<Display::SeatData*>(data);
seatData.serial = serial;
int32_t arraySize = std::tuple_size<decltype(seatData.touch.targets)>::value;
if (id < 0 || id >= arraySize)
return;
auto& touchPoints = seatData.touch.touchPoints;
auto& point = touchPoints[id];
auto& target = seatData.touch.targets[id];
if (target.first && target.second) {
point = { wpe_input_touch_event_type_up, time, id, point.x, point.y };
struct wpe_input_touch_event event = { touchPoints.data(), touchPoints.size(), wpe_input_touch_event_type_up, id, time, getModifiers(seatData) };
struct wpe_view_backend* backend = target.second;
wpe_view_backend_dispatch_touch_event(backend, &event);
point = { wpe_input_touch_event_type_null, 0, 0, 0, 0 };
target = { nullptr, nullptr };
} else {
// no surface registered
struct wpe_input_touch_event_raw event_touch_simple = { wpe_input_touch_event_type_up, time, id, point.x, point.y };
EventDispatcher::singleton().sendEvent( event_touch_simple );
}
},
// motion
[](void* data, struct wl_touch*, uint32_t time, int32_t id, wl_fixed_t x, wl_fixed_t y)
{
auto& seatData = *static_cast<Display::SeatData*>(data);
int32_t arraySize = std::tuple_size<decltype(seatData.touch.targets)>::value;
if (id < 0 || id >= arraySize)
return;
auto& touchPoints = seatData.touch.touchPoints;
touchPoints[id] = { wpe_input_touch_event_type_motion, time, id, wl_fixed_to_int(x), wl_fixed_to_int(y) };
auto& target = seatData.touch.targets[id];
if (target.first && target.second) {
struct wpe_input_touch_event event = { touchPoints.data(), touchPoints.size(), wpe_input_touch_event_type_motion, id, time, getModifiers(seatData) };
struct wpe_view_backend* backend = target.second;
wpe_view_backend_dispatch_touch_event(backend, &event);
} else {
// no surface registered
struct wpe_input_touch_event_raw event_touch_simple = { wpe_input_touch_event_type_motion, time, id, wl_fixed_to_int(x), wl_fixed_to_int(y) };
EventDispatcher::singleton().sendEvent( event_touch_simple );
}
},
// frame
[](void*, struct wl_touch*)
{
// FIXME: Dispatching events via frame() would avoid dispatching events
// for every single event that's encapsulated in a frame with multiple
// other events.
},
// cancel
[](void*, struct wl_touch*) { },
};
static const struct wl_seat_listener g_seatListener = {
// capabilities
[](void* data, struct wl_seat* seat, uint32_t capabilities)
{
auto& seatData = *static_cast<Display::SeatData*>(data);
// WL_SEAT_CAPABILITY_POINTER
const bool hasPointerCap = capabilities & WL_SEAT_CAPABILITY_POINTER;
if (hasPointerCap && !seatData.pointer.object) {
seatData.pointer.object = wl_seat_get_pointer(seat);
wl_pointer_add_listener(seatData.pointer.object, &g_pointerListener, &seatData);
}
if (!hasPointerCap && seatData.pointer.object) {
wl_pointer_destroy(seatData.pointer.object);
seatData.pointer.object = nullptr;
}
// WL_SEAT_CAPABILITY_KEYBOARD
const bool hasKeyboardCap = capabilities & WL_SEAT_CAPABILITY_KEYBOARD;
if (hasKeyboardCap && !seatData.keyboard.object) {
seatData.keyboard.object = wl_seat_get_keyboard(seat);
wl_keyboard_add_listener(seatData.keyboard.object, &g_keyboardListener, &seatData);
}
if (!hasKeyboardCap && seatData.keyboard.object) {
wl_keyboard_destroy(seatData.keyboard.object);
seatData.keyboard.object = nullptr;
}
// WL_SEAT_CAPABILITY_TOUCH
const bool hasTouchCap = capabilities & WL_SEAT_CAPABILITY_TOUCH;
if (hasTouchCap && !seatData.touch.object) {
seatData.touch.object = wl_seat_get_touch(seat);
wl_touch_add_listener(seatData.touch.object, &g_touchListener, &seatData);
}
if (!hasTouchCap && seatData.touch.object) {
wl_touch_destroy(seatData.touch.object);
seatData.touch.object = nullptr;
}
},
// name
[](void*, struct wl_seat*, const char*) { }
};
Display& Display::singleton()
{
static Display display;
return display;
}
Display::Display()
{
m_display = wl_display_connect(nullptr);
if (!m_display) {
fprintf(stderr, "Wayland::Display: failed to connect\n");
abort();
}
m_registry = wl_display_get_registry(m_display);
wl_registry_add_listener(m_registry, &g_registryListener, &m_interfaces);
wl_display_roundtrip(m_display);
m_eventSource = g_source_new(&EventSource::sourceFuncs, sizeof(EventSource));
auto* source = reinterpret_cast<EventSource*>(m_eventSource);
source->display = m_display;
source->pfd.fd = wl_display_get_fd(m_display);
source->pfd.events = G_IO_IN | G_IO_ERR | G_IO_HUP;
source->pfd.revents = 0;
g_source_add_poll(m_eventSource, &source->pfd);
g_source_set_name(m_eventSource, "[WPE] Display");
g_source_set_priority(m_eventSource, G_PRIORITY_HIGH + 30);
g_source_set_can_recurse(m_eventSource, TRUE);
g_source_attach(m_eventSource, g_main_context_get_thread_default());
if (m_interfaces.xdg) {
xdg_shell_add_listener(m_interfaces.xdg, &g_xdgShellListener, nullptr);
xdg_shell_use_unstable_version(m_interfaces.xdg, 5);
}
if ( m_interfaces.seat )
wl_seat_add_listener(m_interfaces.seat, &g_seatListener, &m_seatData);
}
Display::~Display()
{
if (m_eventSource)
g_source_unref(m_eventSource);
m_eventSource = nullptr;
if (m_interfaces.compositor)
wl_compositor_destroy(m_interfaces.compositor);
if (m_interfaces.seat)
wl_seat_destroy(m_interfaces.seat);
#ifdef BACKEND_BCM_NEXUS_WAYLAND
if (m_interfaces.nsc)
wl_nsc_destroy(m_interfaces.nsc);
#endif
if (m_interfaces.xdg)
xdg_shell_destroy(m_interfaces.xdg);
if (m_interfaces.shell)
wl_shell_destroy(m_interfaces.shell);
m_interfaces = {
nullptr,
#ifdef BACKEND_BCM_NEXUS_WAYLAND
nullptr,
#endif
nullptr,
nullptr,
nullptr,
};
if (m_registry)
wl_registry_destroy(m_registry);
m_registry = nullptr;
if (m_display)
wl_display_disconnect(m_display);
m_display = nullptr;
if (m_seatData.pointer.object)
wl_pointer_destroy(m_seatData.pointer.object);
if (m_seatData.keyboard.object)
wl_keyboard_destroy(m_seatData.keyboard.object);
if (m_seatData.touch.object)
wl_touch_destroy(m_seatData.touch.object);
if (m_seatData.repeatData.eventSource)
g_source_remove(m_seatData.repeatData.eventSource);
m_seatData = SeatData{ };
}
void Display::registerInputClient(struct wl_surface* surface, struct wpe_view_backend* client)
{
#ifndef NDEBUG
auto result =
#endif
m_seatData.inputClients.insert({ surface, client });
assert(result.second);
}
void Display::unregisterInputClient(struct wl_surface* surface)
{
auto it = m_seatData.inputClients.find(surface);
assert(it != m_seatData.inputClients.end());
if (m_seatData.pointer.target.first == it->first)
m_seatData.pointer.target = { nullptr, nullptr };
if (m_seatData.keyboard.target.first == it->first)
m_seatData.keyboard.target = { nullptr, nullptr };
m_seatData.inputClients.erase(it);
}
EventDispatcher& EventDispatcher::singleton()
{
static EventDispatcher event;
return event;
}
void EventDispatcher::sendEvent( wpe_input_axis_event& event )
{
if ( m_ipc != nullptr )
{
IPC::Message message;
message.messageCode = MsgType::AXIS;
static_assert(sizeof(event) <= sizeof(message.messageData), "Message::messageData is of correct size");
memcpy( message.messageData, &event, sizeof(event) );
m_ipc->sendMessage(IPC::Message::data(message), IPC::Message::size);
}
}
void EventDispatcher::sendEvent( wpe_input_pointer_event& event )
{
if ( m_ipc != nullptr )
{
IPC::Message message;
message.messageCode = MsgType::POINTER;
memcpy( message.messageData, &event, sizeof(event) );
m_ipc->sendMessage(IPC::Message::data(message), IPC::Message::size);
}
}
void EventDispatcher::sendEvent( wpe_input_touch_event& event )
{
if ( m_ipc != nullptr )
{
IPC::Message message;
message.messageCode = MsgType::TOUCH;
memcpy( message.messageData, &event, sizeof(event) );
m_ipc->sendMessage(IPC::Message::data(message), IPC::Message::size);
}
}
void EventDispatcher::sendEvent( wpe_input_keyboard_event& event )
{
if ( m_ipc != nullptr )
{
IPC::Message message;
message.messageCode = MsgType::KEYBOARD;
memcpy( message.messageData, &event, sizeof(event) );
m_ipc->sendMessage(IPC::Message::data(message), IPC::Message::size);
}
}
void EventDispatcher::sendEvent( wpe_input_touch_event_raw& event )
{
if ( m_ipc != nullptr )
{
IPC::Message message;
message.messageCode = MsgType::TOUCHSIMPLE;
memcpy( message.messageData, &event, sizeof(event) );
m_ipc->sendMessage(IPC::Message::data(message), IPC::Message::size);
}
}
void EventDispatcher::setIPC( IPC::Client& ipcClient )
{
m_ipc = &ipcClient;
}
} // namespace Wayland