-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathwayland_display_window.cpp
1271 lines (1090 loc) · 36.8 KB
/
wayland_display_window.cpp
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "wayland_display_window.h"
#include <cstring>
bool WaylandDisplayWindow::exitRunLoop = false;
Size WaylandDisplayWindow::s_ScreenSize = Size(0, 0);
wayland::display_t WaylandDisplayWindow::s_waylandDisplay = wayland::display_t();
wayland::registry_t WaylandDisplayWindow::s_waylandRegistry;
std::vector<WaylandDisplayWindow*> WaylandDisplayWindow::s_Windows;
std::vector<WaylandDisplayWindow*>::iterator WaylandDisplayWindow::s_WindowsIterator;
WaylandDisplayWindow::WaylandDisplayWindow(DisplayWindowHost* windowHost, bool popupWindow, WaylandDisplayWindow* owner, RenderAPI renderAPI)
: m_owner(owner), windowHost(windowHost), m_PopupWindow(popupWindow)
{
if (!s_waylandDisplay)
throw std::runtime_error("Wayland Display initialization failed!");
s_waylandRegistry = s_waylandDisplay.get_registry();
s_waylandRegistry.on_global() = [&](uint32_t name, std::string interface, uint32_t version) {
if (interface == wayland::compositor_t::interface_name)
s_waylandRegistry.bind(name, m_waylandCompositor, 3);
if (interface == wayland::shm_t::interface_name)
s_waylandRegistry.bind(name, m_waylandSHM, version);
if (interface == wayland::output_t::interface_name)
s_waylandRegistry.bind(name, m_waylandOutput, version);
if (interface == wayland::seat_t::interface_name)
s_waylandRegistry.bind(name, m_waylandSeat, 8);
if (interface == wayland::data_device_manager_t::interface_name)
s_waylandRegistry.bind(name, m_DataDeviceManager, 3);
if (interface == wayland::xdg_wm_base_t::interface_name)
s_waylandRegistry.bind(name, m_XDGWMBase, 1);
if (interface == wayland::zxdg_output_manager_v1_t::interface_name)
s_waylandRegistry.bind(name, m_XDGOutputManager, version);
if (interface == wayland::zxdg_exporter_v2_t::interface_name)
s_waylandRegistry.bind(name, m_XDGExporter, 1);
if (interface == wayland::zwp_pointer_constraints_v1_t::interface_name)
s_waylandRegistry.bind(name, m_PointerConstraints, 1);
if (interface == wayland::xdg_activation_v1_t::interface_name)
s_waylandRegistry.bind(name, m_XDGActivation, 1);
if (interface == wayland::zxdg_decoration_manager_v1_t::interface_name)
s_waylandRegistry.bind(name, m_XDGDecorationManager, 1);
if (interface == wayland::fractional_scale_manager_v1_t::interface_name)
s_waylandRegistry.bind(name, m_FractionalScaleManager, 1);
};
s_waylandDisplay.roundtrip();
if (!m_XDGWMBase)
throw std::runtime_error("WaylandDisplayWindow: XDG-Shell is required!");
if (!m_XDGOutputManager)
throw std::runtime_error("WaylandDisplayWindow: xdg-output-manager-v1 is required!");
if (!m_XDGExporter)
throw std::runtime_error("WaylandDisplayWindow: xdg-foreign-unstable-v2 is required!");
if (!m_PointerConstraints)
throw std::runtime_error("WaylandDisplayWindow: pointer-constrains-unstable-v1 is required!");
m_AppSurface = m_waylandCompositor.create_surface();
m_waylandOutput.on_mode() = [&] (wayland::output_mode flags, int32_t width, int32_t height, int32_t refresh) {
s_ScreenSize = Size(width, height);
};
if (m_FractionalScaleManager)
{
m_FractionalScale = m_FractionalScaleManager.get_fractional_scale(m_AppSurface);
m_FractionalScale.on_preferred_scale() = [&](uint32_t scale_numerator) {
// parameter is the numerator of a fraction with the denominator of 120
m_ScaleFactor = scale_numerator / 120.0;
m_NeedsUpdate = true;
windowHost->OnWindowDpiScaleChanged();
};
}
else
{
m_waylandOutput.on_scale() = [&] (int32_t scale) {
m_ScaleFactor = scale;
m_NeedsUpdate = true;
windowHost->OnWindowDpiScaleChanged();
};
}
/*
m_DataDevice = m_DataDeviceManager.get_data_device(m_waylandSeat);
m_DataSource = m_DataDeviceManager.create_data_source();
m_DataSource.offer("text/plain");
m_DataSource.on_send() = [&] (std::string mime_type, int fd) {
if (mime_type != "text/plain")
return;
if (!m_ClipboardContents.empty())
write(fd, m_ClipboardContents.data(), m_ClipboardContents.size());
close(fd);
};
m_DataDevice.on_selection() = [&] (wayland::data_offer_t dataOffer) {
m_ClipboardContents.clear();
if (!dataOffer)
// Clipboard is empty
return;
int fds[2];
pipe(fds);
dataOffer.receive("text/plain", fds[1]);
close(fds[1]);
m_waylandDisplay.roundtrip();
while (true)
{
char buf[1024];
ssize_t n = read(fds[0], buf, sizeof(buf));
if (n <= 0)
break;
m_ClipboardContents += buf;
}
close(fds[0]);
dataOffer.proxy_release();
};
*/
m_XDGWMBase.on_ping() = [&] (uint32_t serial) {
m_XDGWMBase.pong(serial);
};
m_XDGSurface = m_XDGWMBase.get_xdg_surface(m_AppSurface);
m_XDGSurface.on_configure() = [&] (uint32_t serial) {
m_XDGSurface.ack_configure(serial);
};
if (popupWindow)
{
InitializePopup();
}
else
{
InitializeToplevel();
}
s_Windows.push_back(this);
s_WindowsIterator = s_Windows.end();
this->DrawSurface();
}
WaylandDisplayWindow::~WaylandDisplayWindow()
{
if (m_KeymapContext)
xkb_context_unref(m_KeymapContext);
if (m_KeyboardState)
xkb_state_unref(m_KeyboardState);
s_WindowsIterator = s_Windows.erase(s_WindowsIterator);
}
void WaylandDisplayWindow::InitializeToplevel()
{
m_XDGToplevel = m_XDGSurface.get_toplevel();
m_XDGToplevel.set_title("ZWidget Window");
if (m_owner)
m_XDGToplevel.set_parent(m_owner->m_XDGToplevel);
if (m_XDGDecorationManager)
{
// Force server side decorations if possible
m_XDGToplevelDecoration = m_XDGDecorationManager.get_toplevel_decoration(m_XDGToplevel);
m_XDGToplevelDecoration.set_mode(wayland::zxdg_toplevel_decoration_v1_mode::server_side);
}
m_waylandSeat.on_capabilities() = [&] (uint32_t capabilities) {
hasKeyboard = capabilities & wayland::seat_capability::keyboard;
hasPointer = capabilities & wayland::seat_capability::pointer;
};
m_XDGOutput = m_XDGOutputManager.get_xdg_output(m_waylandOutput);
m_XDGOutput.on_logical_position() = [&] (int32_t x, int32_t y) {
//m_WindowGlobalPos = Point(x, y);
};
m_XDGOutput.on_logical_size() = [&] (int32_t width, int32_t height) {
s_ScreenSize = Size(width, height);
m_NeedsUpdate = true;
windowHost->OnWindowGeometryChanged();
};
m_AppSurface.commit();
s_waylandDisplay.roundtrip();
// These have to be added after the roundtrip
m_XDGToplevel.on_configure() = [&] (int32_t width, int32_t height, wayland::array_t states) {
OnXDGToplevelConfigureEvent(width, height);
};
m_XDGToplevel.on_close() = [&] () {
OnExitEvent();
};
if (!hasKeyboard)
throw std::runtime_error("No keyboard detected!");
if (!hasPointer)
throw std::runtime_error("No pointer device detected!");
m_waylandKeyboard = m_waylandSeat.get_keyboard();
m_waylandPointer = m_waylandSeat.get_pointer();
m_cursorSurface = m_waylandCompositor.create_surface();
SetCursor(StandardCursor::arrow);
ConnectDeviceEvents();
m_XDGExported = m_XDGExporter.export_toplevel(m_AppSurface);
m_XDGExported.on_handle() = [&] (std::string handleStr) {
OnExportHandleEvent(handleStr);
};
}
void WaylandDisplayWindow::InitializePopup()
{
if (!m_owner)
throw std::runtime_error("Popup window must have an owner!");
wayland::xdg_positioner_t popupPositioner = m_XDGWMBase.create_positioner();
popupPositioner.set_anchor(wayland::xdg_positioner_anchor::bottom);
popupPositioner.set_anchor_rect(0, 0, 1, 30);
popupPositioner.set_size(1, 1);
m_XDGPopup = m_XDGSurface.get_popup(m_owner->m_XDGSurface, popupPositioner);
m_XDGPopup.on_configure() = [&] (int32_t x, int32_t y, int32_t width, int32_t height) {
SetClientFrame(Rect::xywh(x, y, width, height));
};
m_XDGPopup.on_popup_done() = [&] () {
OnExitEvent();
};
m_waylandKeyboard = m_waylandSeat.get_keyboard();
m_waylandPointer = m_waylandSeat.get_pointer();
m_cursorSurface = m_owner->m_cursorSurface;
m_AppSurface.commit();
s_waylandDisplay.roundtrip();
ConnectDeviceEvents();
}
void WaylandDisplayWindow::ConnectDeviceEvents()
{
m_KeymapContext = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
m_waylandKeyboard.on_keymap() = [&] (wayland::keyboard_keymap_format format, int fd, uint32_t size) {
if (format != wayland::keyboard_keymap_format::xkb_v1)
throw std::runtime_error("WaylandDisplayWindow: Unrecognized keymap format!");
char* mapSHM = (char*)mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0);
if (mapSHM == MAP_FAILED)
throw std::runtime_error("WaylandDisplayWindow: Keymap shared memory allocation failed!");
if (m_Keymap)
xkb_keymap_unref(m_Keymap);
m_Keymap = xkb_keymap_new_from_string(m_KeymapContext, mapSHM, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
munmap(mapSHM, size);
close(fd);
if (m_KeyboardState)
xkb_state_unref(m_KeyboardState);
m_KeyboardState = xkb_state_new(m_Keymap);
};
m_waylandKeyboard.on_modifiers() = [&] (uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched,
uint32_t mods_locked, uint32_t group) {
xkb_state_update_mask(m_KeyboardState, mods_depressed, mods_latched, mods_locked, 0, 0, group);
};
m_waylandKeyboard.on_enter() = [&] (uint32_t serial, wayland::surface_t surfaceEntered, wayland::array_t keys) {
std::vector<uint32_t> keysVec = keys;
m_KeyboardSerial = serial;
for (auto key: keysVec)
{
// keys parameter represents the keys pressed when entering the surface
// key variable is Linux evdev scancode, to translate it to XKB keycode, we must add 8
xkb_keysym_t sym = xkb_state_key_get_one_sym(m_KeyboardState, key + 8);
OnKeyboardKeyEvent(sym, wayland::keyboard_key_state::pressed);
// Also cause a Char event
char buf[128];
xkb_state_key_get_utf8(m_KeyboardState, key + 8, buf, sizeof(buf));
OnKeyboardCharEvent(buf, wayland::keyboard_key_state::pressed);
}
};
m_waylandKeyboard.on_key() = [&] (uint32_t serial, uint32_t time, uint32_t key, wayland::keyboard_key_state state) {
// key is Linux evdev scancode, to translate it to XKB keycode, we must add 8
xkb_keysym_t sym = xkb_state_key_get_one_sym(m_KeyboardState, key + 8);
OnKeyboardKeyEvent(sym, state);
// Also cause a Char event
char buf[128];
xkb_state_key_get_utf8(m_KeyboardState, key + 8, buf, sizeof(buf));
OnKeyboardCharEvent(buf, state);
//m_DataDevice.set_selection(m_DataSource, m_KeyboardSerial);
};
m_waylandPointer.on_enter() = [&](uint32_t serial, wayland::surface_t surfaceEntered, double surfaceX, double surfaceY) {
OnMouseEnterEvent(serial);
};
m_waylandPointer.on_leave() = [&](uint32_t serial, wayland::surface_t surfaceLeft) {
OnMouseLeaveEvent();
};
m_waylandPointer.on_motion() = [&] (uint32_t serial, double surfaceX, double surfaceY) {
OnMouseMoveEvent(Point(surfaceX, surfaceY));
};
m_waylandPointer.on_button() = [&] (uint32_t serial, uint32_t time, uint32_t button, wayland::pointer_button_state state) {
if (state == wayland::pointer_button_state::pressed)
OnMousePressEvent(LinuxInputEventCodeToInputKey(button));
if (state == wayland::pointer_button_state::released)
OnMouseReleaseEvent(LinuxInputEventCodeToInputKey(button));
};
m_waylandPointer.on_axis() = [&] (uint32_t serial, wayland::pointer_axis axis, double value) {
if (axis == wayland::pointer_axis::vertical_scroll && value > 0)
OnMouseWheelEvent(InputKey::MouseWheelDown);
if (axis == wayland::pointer_axis::vertical_scroll && value < 0)
OnMouseWheelEvent(InputKey::MouseWheelUp);
};
// High resolution scroll event
m_waylandPointer.on_axis_value120() = [&] (wayland::pointer_axis axis, int32_t value) {
if (axis == wayland::pointer_axis::vertical_scroll && value > 0)
OnMouseWheelEvent(InputKey::MouseWheelDown);
if (axis == wayland::pointer_axis::vertical_scroll && value < 0)
OnMouseWheelEvent(InputKey::MouseWheelUp);
};
m_keyboardDelayTimer = ZTimer();
m_keyboardRepeatTimer = ZTimer();
m_waylandKeyboard.on_repeat_info() = [&] (int32_t rate, int32_t delay) {
// rate is characters per second, delay is in milliseconds
m_keyboardDelayTimer.SetDuration(ZTimer::Duration(delay));
m_keyboardRepeatTimer.SetDuration(ZTimer::Duration(1000.0 / rate));
};
m_keyboardDelayTimer.SetCallback([&] () { OnKeyboardDelayEnd(); });
m_keyboardRepeatTimer.SetCallback([&] () { OnKeyboardRepeat(); });
m_keyboardRepeatTimer.SetRepeating(true);
m_previousTime = ZTimer::Clock::now();
m_currentTime = ZTimer::Clock::now();
}
void WaylandDisplayWindow::SetWindowTitle(const std::string& text)
{
if (m_XDGToplevel)
m_XDGToplevel.set_title(text);
}
void WaylandDisplayWindow::SetWindowFrame(const Rect& box)
{
// Resizing will be shown on the next commit
CreateBuffers(box.width, box.height);
windowHost->OnWindowGeometryChanged();
m_NeedsUpdate = true;
m_AppSurface.commit();
}
void WaylandDisplayWindow::SetClientFrame(const Rect& box)
{
SetWindowFrame(box);
}
void WaylandDisplayWindow::Show()
{
m_AppSurface.attach(m_AppSurfaceBuffer, 0, 0);
m_AppSurface.damage(0, 0, m_WindowSize.width, m_WindowSize.height);
m_AppSurface.commit();
}
void WaylandDisplayWindow::ShowFullscreen()
{
if (m_XDGToplevel)
m_XDGToplevel.set_fullscreen(m_waylandOutput);
}
void WaylandDisplayWindow::ShowMaximized()
{
if (m_XDGToplevel)
m_XDGToplevel.set_maximized();
}
void WaylandDisplayWindow::ShowMinimized()
{
if (m_XDGToplevel)
m_XDGToplevel.set_minimized();
}
void WaylandDisplayWindow::ShowNormal()
{
if (m_XDGToplevel)
m_XDGToplevel.unset_fullscreen();
}
void WaylandDisplayWindow::Hide()
{
// Apparently this is how hiding a window works
// By attaching a null buffer to the surface
// See: https://lists.freedesktop.org/archives/wayland-devel/2017-November/035963.html
m_AppSurface.attach(nullptr, 0, 0);
m_AppSurface.commit();
}
void WaylandDisplayWindow::Activate()
{
wayland::xdg_activation_token_v1_t xdgActivationToken = m_XDGActivation.get_activation_token();
std::string tokenString;
xdgActivationToken.on_done() = [&tokenString] (std::string obtainedString) {
tokenString = obtainedString;
};
xdgActivationToken.set_surface(m_AppSurface);
xdgActivationToken.commit(); // This will set our token string
m_XDGActivation.activate(tokenString, m_AppSurface);
windowHost->OnWindowActivated();
}
void WaylandDisplayWindow::ShowCursor(bool enable)
{
m_cursorSurface.attach(enable ? m_cursorBuffer : nullptr, 0, 0);
m_cursorSurface.commit();
}
void WaylandDisplayWindow::LockCursor()
{
m_LockedPointer = m_PointerConstraints.lock_pointer(m_AppSurface, m_waylandPointer, nullptr,
wayland::zwp_pointer_constraints_v1_lifetime::persistent);
m_cursorSurface.attach(nullptr, 0, 0);
m_cursorSurface.commit();
}
void WaylandDisplayWindow::UnlockCursor()
{
if (m_LockedPointer)
m_LockedPointer.proxy_release();
m_cursorSurface.attach(m_cursorBuffer, 0, 0);
m_cursorSurface.commit();
}
void WaylandDisplayWindow::CaptureMouse()
{
}
void WaylandDisplayWindow::ReleaseMouseCapture()
{
}
void WaylandDisplayWindow::Update()
{
m_NeedsUpdate = true;
}
bool WaylandDisplayWindow::GetKeyState(InputKey key)
{
auto it = inputKeyStates.find(key);
// if the key isn't "registered", then it is not pressed.
if (it == inputKeyStates.end())
return false;
return it->second;
}
void WaylandDisplayWindow::SetCursor(StandardCursor cursor)
{
std::string cursorName = GetWaylandCursorName(cursor);
// Perhaps the cursor size can be inferred from the user prefs as well?
wayland::cursor_theme_t cursorTheme = wayland::cursor_theme_t("default", 16, m_waylandSHM);
wayland::cursor_t obtainedCursor = cursorTheme.get_cursor(cursorName);
m_cursorImage = obtainedCursor.image(0);
m_cursorBuffer = m_cursorImage.get_buffer();
}
Rect WaylandDisplayWindow::GetWindowFrame() const
{
return Rect(m_WindowGlobalPos.x, m_WindowGlobalPos.y, m_WindowSize.width, m_WindowSize.height);
}
Size WaylandDisplayWindow::GetClientSize() const
{
return m_WindowSize / m_ScaleFactor;
}
int WaylandDisplayWindow::GetPixelWidth() const
{
return m_WindowSize.width;
}
int WaylandDisplayWindow::GetPixelHeight() const
{
return m_WindowSize.height;
}
double WaylandDisplayWindow::GetDpiScale() const
{
return m_ScaleFactor;
}
void WaylandDisplayWindow::PresentBitmap(int width, int height, const uint32_t* pixels)
{
// Make new buffers if the sizes don't match
if (width != m_WindowSize.width || height != m_WindowSize.height)
CreateBuffers(width, height);
std::memcpy(shared_mem->get_mem(), (void*)pixels, width * height * 4);
}
void WaylandDisplayWindow::SetBorderColor(uint32_t bgra8)
{
}
void WaylandDisplayWindow::SetCaptionColor(uint32_t bgra8)
{
}
void WaylandDisplayWindow::SetCaptionTextColor(uint32_t bgra8)
{
}
std::string WaylandDisplayWindow::GetClipboardText()
{
return m_ClipboardContents;
}
void WaylandDisplayWindow::SetClipboardText(const std::string& text)
{
m_ClipboardContents = text;
}
Point WaylandDisplayWindow::MapFromGlobal(const Point& pos) const
{
return (pos - m_WindowGlobalPos) / m_ScaleFactor;
}
Point WaylandDisplayWindow::MapToGlobal(const Point& pos) const
{
return (m_WindowGlobalPos + pos) / m_ScaleFactor;
}
void WaylandDisplayWindow::ProcessEvents()
{
while (s_waylandDisplay.dispatch() > 0)
{
}
}
void WaylandDisplayWindow::RunLoop()
{
exitRunLoop = false;
while (!exitRunLoop)
{
CheckNeedsUpdate();
for (auto window: s_Windows)
window->UpdateTimers();
if (s_waylandDisplay.dispatch() == -1)
break;
}
}
void WaylandDisplayWindow::ExitLoop()
{
exitRunLoop = true;
}
Size WaylandDisplayWindow::GetScreenSize()
{
return s_ScreenSize;
}
void * WaylandDisplayWindow::StartTimer(int timeoutMilliseconds, std::function<void ()> onTimer)
{
return nullptr;
}
void WaylandDisplayWindow::StopTimer(void* timerID)
{
}
void WaylandDisplayWindow::CheckNeedsUpdate()
{
for (auto window: s_Windows)
{
if (window->m_NeedsUpdate)
{
window->m_NeedsUpdate = false;
window->windowHost->OnWindowPaint();
}
}
}
void WaylandDisplayWindow::UpdateTimers()
{
m_currentTime = ZTimer::Clock::now();
m_keyboardDelayTimer.Update(m_currentTime - m_previousTime);
m_keyboardRepeatTimer.Update(m_currentTime - m_previousTime);
m_previousTime = m_currentTime;
}
void WaylandDisplayWindow::OnXDGToplevelConfigureEvent(int32_t width, int32_t height)
{
Rect rect = GetWindowFrame();
rect.width = width / m_ScaleFactor;
rect.height = height / m_ScaleFactor;
SetWindowFrame(rect);
windowHost->OnWindowGeometryChanged();
}
void WaylandDisplayWindow::OnKeyboardKeyEvent(xkb_keysym_t xkbKeySym, wayland::keyboard_key_state state)
{
InputKey inputKey = XKBKeySymToInputKey(xkbKeySym);
if (state == wayland::keyboard_key_state::pressed)
{
inputKeyStates[inputKey] = true;
windowHost->OnWindowKeyDown(inputKey);
if (inputKey != previousKey)
{
previousKey = inputKey;
m_keyboardDelayTimer.Stop();
m_keyboardRepeatTimer.Stop();
}
m_keyboardDelayTimer.Start();
}
if (state == wayland::keyboard_key_state::released)
{
inputKeyStates[inputKey] = false;
windowHost->OnWindowKeyUp(inputKey);
m_keyboardDelayTimer.Stop();
m_keyboardRepeatTimer.Stop();
}
}
void WaylandDisplayWindow::OnKeyboardCharEvent(const char* ch, wayland::keyboard_key_state state)
{
if (state == wayland::keyboard_key_state::pressed)
{
previousChars = std::string(ch);
windowHost->OnWindowKeyChar(previousChars);
}
}
void WaylandDisplayWindow::OnKeyboardDelayEnd()
{
if (inputKeyStates[previousKey])
m_keyboardRepeatTimer.Start();
}
void WaylandDisplayWindow::OnKeyboardRepeat()
{
if (inputKeyStates[previousKey])
{
windowHost->OnWindowKeyDown(previousKey);
windowHost->OnWindowKeyChar(previousChars);
}
}
void WaylandDisplayWindow::OnMouseEnterEvent(uint32_t serial)
{
m_cursorSurface.attach(m_cursorBuffer, 0, 0);
m_cursorSurface.damage(0, 0, m_cursorImage.width(), m_cursorImage.height());
m_cursorSurface.commit();
m_waylandPointer.set_cursor(serial, m_cursorSurface, 0, 0);
}
void WaylandDisplayWindow::OnMouseLeaveEvent()
{
windowHost->OnWindowMouseLeave();
}
void WaylandDisplayWindow::OnMousePressEvent(InputKey button)
{
windowHost->OnWindowMouseDown(m_SurfaceMousePos, button);
}
void WaylandDisplayWindow::OnMouseReleaseEvent(InputKey button)
{
windowHost->OnWindowMouseUp(m_SurfaceMousePos, button);
}
void WaylandDisplayWindow::OnMouseMoveEvent(Point surfacePos)
{
m_SurfaceMousePos = surfacePos / m_ScaleFactor;
windowHost->OnWindowMouseMove(m_SurfaceMousePos);
}
void WaylandDisplayWindow::OnMouseWheelEvent(InputKey button)
{
windowHost->OnWindowMouseWheel(m_SurfaceMousePos, button);
}
void WaylandDisplayWindow::OnExportHandleEvent(std::string exportedHandle)
{
m_windowID = exportedHandle;
}
void WaylandDisplayWindow::OnExitEvent()
{
windowHost->OnWindowClose();
}
void WaylandDisplayWindow::DrawSurface(uint32_t serial)
{
m_AppSurface.attach(m_AppSurfaceBuffer, 0, 0);
m_AppSurface.damage(0, 0, m_WindowSize.width, m_WindowSize.height);
m_FrameCallback = m_AppSurface.frame();
m_FrameCallback.on_done() = bind_mem_fn(&WaylandDisplayWindow::DrawSurface, this);
m_AppSurface.commit();
}
void WaylandDisplayWindow::CreateBuffers(int32_t width, int32_t height)
{
if (shared_mem)
shared_mem.reset();
int scaled_width = width * m_ScaleFactor;
int scaled_height = height * m_ScaleFactor;
shared_mem = std::make_shared<SharedMemHelper>(scaled_width * scaled_height * 4);
auto pool = m_waylandSHM.create_pool(shared_mem->get_fd(), scaled_width * scaled_height * 4);
m_AppSurfaceBuffer = pool.create_buffer(0, scaled_width, scaled_height, scaled_width * 4, wayland::shm_format::xrgb8888);
m_WindowSize = Size(scaled_width, scaled_height);
}
std::string WaylandDisplayWindow::GetWaylandCursorName(StandardCursor cursor)
{
// Checked out Adwaita and Breeze cursors for the names.
// Other cursor themes should adhere to the names these two have.
switch (cursor)
{
case StandardCursor::arrow:
return "default";
case StandardCursor::appstarting:
return "progress";
case StandardCursor::cross:
return "crosshair";
case StandardCursor::hand:
return "pointer";
case StandardCursor::ibeam:
return "text";
case StandardCursor::no:
return "not-allowed";
case StandardCursor::size_all:
return "fleur";
case StandardCursor::size_nesw:
return "nesw-resize";
case StandardCursor::size_ns:
return "ns-resize";
case StandardCursor::size_nwse:
return "nwse-resize";
case StandardCursor::size_we:
return "ew-resize";
case StandardCursor::uparrow:
// Breeze actually has an up-arrow cursor, but Adwaita doesn't, so the default cursor it is
return "default";
case StandardCursor::wait:
return "wait";
default:
return "default";
}
}
std::string WaylandDisplayWindow::GetWaylandWindowID()
{
return m_windowID;
}
InputKey WaylandDisplayWindow::XKBKeySymToInputKey(xkb_keysym_t keySym)
{
switch (keySym)
{
case XKB_KEY_Escape:
return InputKey::Escape;
case XKB_KEY_1:
return InputKey::_1;
case XKB_KEY_2:
return InputKey::_2;
case XKB_KEY_3:
return InputKey::_3;
case XKB_KEY_4:
return InputKey::_4;
case XKB_KEY_5:
return InputKey::_5;
case XKB_KEY_6:
return InputKey::_6;
case XKB_KEY_7:
return InputKey::_7;
case XKB_KEY_8:
return InputKey::_8;
case XKB_KEY_9:
return InputKey::_9;
case XKB_KEY_0:
return InputKey::_0;
case XKB_KEY_KP_1:
return InputKey::NumPad1;
case XKB_KEY_KP_2:
return InputKey::NumPad2;
case XKB_KEY_KP_3:
return InputKey::NumPad3;
case XKB_KEY_KP_4:
return InputKey::NumPad4;
case XKB_KEY_KP_5:
return InputKey::NumPad5;
case XKB_KEY_KP_6:
return InputKey::NumPad6;
case XKB_KEY_KP_7:
return InputKey::NumPad7;
case XKB_KEY_KP_8:
return InputKey::NumPad8;
case XKB_KEY_KP_9:
return InputKey::NumPad9;
case XKB_KEY_KP_0:
return InputKey::NumPad0;
case XKB_KEY_F1:
return InputKey::F1;
case XKB_KEY_F2:
return InputKey::F2;
case XKB_KEY_F3:
return InputKey::F3;
case XKB_KEY_F4:
return InputKey::F4;
case XKB_KEY_F5:
return InputKey::F5;
case XKB_KEY_F6:
return InputKey::F6;
case XKB_KEY_F7:
return InputKey::F7;
case XKB_KEY_F8:
return InputKey::F8;
case XKB_KEY_F9:
return InputKey::F9;
case XKB_KEY_F10:
return InputKey::F10;
case XKB_KEY_F11:
return InputKey::F11;
case XKB_KEY_F12:
return InputKey::F12;
case XKB_KEY_F13:
return InputKey::F13;
case XKB_KEY_F14:
return InputKey::F14;
case XKB_KEY_F15:
return InputKey::F15;
case XKB_KEY_F16:
return InputKey::F16;
case XKB_KEY_F17:
return InputKey::F17;
case XKB_KEY_F18:
return InputKey::F18;
case XKB_KEY_F19:
return InputKey::F19;
case XKB_KEY_F20:
return InputKey::F20;
case XKB_KEY_F21:
return InputKey::F21;
case XKB_KEY_F22:
return InputKey::F22;
case XKB_KEY_F23:
return InputKey::F23;
case XKB_KEY_F24:
return InputKey::F24;
case XKB_KEY_minus:
case XKB_KEY_KP_Subtract:
return InputKey::Minus;
case XKB_KEY_equal:
return InputKey::Equals;
case XKB_KEY_BackSpace:
return InputKey::Backspace;
case XKB_KEY_backslash:
return InputKey::Backslash;
case XKB_KEY_Tab:
return InputKey::Tab;
case XKB_KEY_braceleft:
return InputKey::LeftBracket;
case XKB_KEY_braceright:
return InputKey::RightBracket;
case XKB_KEY_Control_L:
case XKB_KEY_Control_R:
return InputKey::Ctrl;
case XKB_KEY_Alt_L:
case XKB_KEY_Alt_R:
return InputKey::Alt;
case XKB_KEY_Delete:
return InputKey::Delete;
case XKB_KEY_semicolon:
return InputKey::Semicolon;
case XKB_KEY_comma:
return InputKey::Comma;
case XKB_KEY_period:
return InputKey::Period;
case XKB_KEY_Num_Lock:
return InputKey::NumLock;
case XKB_KEY_Caps_Lock:
return InputKey::CapsLock;
case XKB_KEY_Scroll_Lock:
return InputKey::ScrollLock;
case XKB_KEY_Shift_L:
return InputKey::LShift;
case XKB_KEY_Shift_R:
return InputKey::RShift;
case XKB_KEY_grave:
return InputKey::Tilde;
case XKB_KEY_apostrophe:
return InputKey::SingleQuote;
case XKB_KEY_Up:
return InputKey::Up;
case XKB_KEY_Down:
return InputKey::Down;
case XKB_KEY_Left:
return InputKey::Left;
case XKB_KEY_Right:
return InputKey::Right;
case XKB_KEY_A:
case XKB_KEY_a:
return InputKey::A;
case XKB_KEY_B:
case XKB_KEY_b:
return InputKey::B;
case XKB_KEY_C:
case XKB_KEY_c:
return InputKey::C;
case XKB_KEY_D:
case XKB_KEY_d:
return InputKey::D;
case XKB_KEY_E:
case XKB_KEY_e:
return InputKey::E;
case XKB_KEY_F:
case XKB_KEY_f:
return InputKey::F;
case XKB_KEY_G:
case XKB_KEY_g:
return InputKey::G;
case XKB_KEY_H:
case XKB_KEY_h:
return InputKey::H;
case XKB_KEY_I:
case XKB_KEY_i:
return InputKey::I;
case XKB_KEY_J:
case XKB_KEY_j:
return InputKey::J;