forked from ValveSoftware/steamos-compositor
/
wlserver.cpp
2768 lines (2220 loc) · 82.8 KB
/
wlserver.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 <xkbcommon/xkbcommon-keysyms.h>
#define _GNU_SOURCE 1
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <poll.h>
#include <fcntl.h>
#include <xf86drm.h>
#include <sys/eventfd.h>
#include <linux/input-event-codes.h>
#include <X11/extensions/XTest.h>
#include <xkbcommon/xkbcommon.h>
#include "wlr_begin.hpp"
#include <wlr/backend.h>
#include <wlr/backend/headless.h>
#if HAVE_DRM
#include <wlr/backend/libinput.h>
#endif
#include <wlr/backend/multi.h>
#include <wlr/interfaces/wlr_keyboard.h>
#include <wlr/render/wlr_renderer.h>
#include <wlr/render/timeline.h>
#include <wlr/types/wlr_compositor.h>
#include <wlr/types/wlr_keyboard.h>
#include <wlr/types/wlr_pointer.h>
#include <wlr/types/wlr_seat.h>
#include <wlr/types/wlr_touch.h>
#include <wlr/types/wlr_drm.h>
#include <wlr/util/log.h>
#include <wlr/xwayland/server.h>
#include <wlr/types/wlr_xdg_shell.h>
#include <wlr/types/wlr_relative_pointer_v1.h>
#include <wlr/types/wlr_pointer_constraints_v1.h>
#include <wlr/types/wlr_linux_drm_syncobj_v1.h>
#include <wlr/util/region.h>
#include "wlr_end.hpp"
#include "gamescope-xwayland-protocol.h"
#include "gamescope-pipewire-protocol.h"
#include "gamescope-control-protocol.h"
#include "gamescope-swapchain-protocol.h"
#include "presentation-time-protocol.h"
#include "wlserver.hpp"
#include "hdmi.h"
#include "main.hpp"
#include "steamcompmgr.hpp"
#include "log.hpp"
#include "ime.hpp"
#include "xwayland_ctx.hpp"
#include "refresh_rate.h"
#include "InputEmulation.h"
#include "commit.h"
#if HAVE_PIPEWIRE
#include "pipewire.hpp"
#endif
#include "gpuvis_trace_utils.h"
#include <algorithm>
#include <list>
#include <set>
static LogScope wl_log("wlserver");
//#define GAMESCOPE_SWAPCHAIN_DEBUG
struct wlserver_t wlserver = {
.touch_down_ids = {}
};
struct wlserver_content_override {
gamescope_xwayland_server_t *server;
struct wlr_surface *surface;
uint32_t x11_window;
struct wl_listener surface_destroy_listener;
struct wl_resource *gamescope_swapchain;
};
std::mutex g_wlserver_xdg_shell_windows_lock;
static struct wl_list pending_surfaces = {0};
static void wlserver_x11_surface_info_set_wlr( struct wlserver_x11_surface_info *surf, struct wlr_surface *wlr_surf, bool override );
wlserver_wl_surface_info *get_wl_surface_info(struct wlr_surface *wlr_surf);
static void wlserver_update_cursor_constraint();
static void handle_pointer_constraint(struct wl_listener *listener, void *data);
static void wlserver_constrain_cursor( struct wlr_pointer_constraint_v1 *pNewConstraint );
struct wlr_surface *wlserver_surface_to_main_surface( struct wlr_surface *pSurface );
std::vector<ResListEntry_t>& gamescope_xwayland_server_t::retrieve_commits()
{
static std::vector<ResListEntry_t> commits;
commits.clear();
commits.reserve(16);
{
std::lock_guard<std::mutex> lock( wayland_commit_lock );
commits.swap(wayland_commit_queue);
}
return commits;
}
void GamescopeTimelinePoint::Release()
{
assert( wlserver_is_lock_held() );
//fprintf( stderr, "Release: %lu\n", ulPoint );
drmSyncobjTimelineSignal( pTimeline->drm_fd, &pTimeline->handle, &ulPoint, 1 );
wlr_render_timeline_unref( pTimeline );
}
//
// Fence flags tl;dr
// 0 -> Wait for signal on a materialized fence, -ENOENT if not materialized
// DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE -> Wait only for materialization
// DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT -> Wait for materialization + signal
//
static std::optional<GamescopeAcquireTimelineState> TimelinePointToEventFd( const GamescopeTimelinePoint &point )
{
uint64_t uSignalledPoint = 0;
int nRet = drmSyncobjQuery( point.pTimeline->drm_fd, &point.pTimeline->handle, &uSignalledPoint, 1u );
if ( nRet != 0 )
{
wl_log.errorf( "Failed to query syncobj" );
return std::nullopt;
}
if ( uSignalledPoint >= point.ulPoint )
{
return GamescopeAcquireTimelineState{ -1, true };
}
else
{
int32_t nExplicitSyncEventFd = eventfd( 0, EFD_CLOEXEC );
if ( nExplicitSyncEventFd < 0 )
{
wl_log.errorf( "Failed to create eventfd" );
return std::nullopt;
}
drm_syncobj_eventfd syncobjEventFd =
{
.handle = point.pTimeline->handle,
// Only valid flags are: DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE
// -> Wait for fence materialization rather than signal.
.flags = 0u,
.point = point.ulPoint,
.fd = nExplicitSyncEventFd,
};
if ( drmIoctl( point.pTimeline->drm_fd, DRM_IOCTL_SYNCOBJ_EVENTFD, &syncobjEventFd ) != 0 )
{
wl_log.errorf_errno( "DRM_IOCTL_SYNCOBJ_EVENTFD failed" );
close( nExplicitSyncEventFd );
return std::nullopt;
}
return GamescopeAcquireTimelineState{ nExplicitSyncEventFd, false };
}
}
std::optional<ResListEntry_t> PrepareCommit( struct wlr_surface *surf, struct wlr_buffer *buf )
{
auto wl_surf = get_wl_surface_info( surf );
const auto& pFeedback = wlserver_surface_swapchain_feedback(surf);
wlr_linux_drm_syncobj_surface_v1_state *pSyncState =
wlr_linux_drm_syncobj_v1_get_surface_state( wlserver.wlr.drm_syncobj_manager_v1, surf );
std::optional<GamescopeAcquireTimelineState> oAcquireState;
std::optional<GamescopeTimelinePoint> oReleasePoint;
if ( pSyncState )
{
GamescopeTimelinePoint acquirePoint =
{
.pTimeline = pSyncState->acquire_timeline,
.ulPoint = pSyncState->acquire_point,
};
oAcquireState = TimelinePointToEventFd( acquirePoint );
if ( !oAcquireState )
{
return std::nullopt;
}
oReleasePoint = GamescopeTimelinePoint
{
.pTimeline = wlr_render_timeline_ref( pSyncState->release_timeline ),
.ulPoint = pSyncState->release_point,
};
}
ResListEntry_t newEntry = {
.surf = surf,
.buf = buf,
.async = wlserver_surface_is_async(surf),
.fifo = wlserver_surface_is_fifo(surf),
.feedback = pFeedback,
.presentation_feedbacks = std::move(wl_surf->pending_presentation_feedbacks),
.present_id = wl_surf->present_id,
.desired_present_time = wl_surf->desired_present_time,
.oAcquireState = oAcquireState,
.oReleasePoint = oReleasePoint,
};
wl_surf->present_id = std::nullopt;
wl_surf->desired_present_time = 0;
wl_surf->pending_presentation_feedbacks.clear();
wl_surf->oCurrentPresentMode = std::nullopt;
struct wlr_surface *pConstraintSurface = wlserver_surface_to_main_surface( surf );
if ( wlserver.mouse_constraint && wlserver.mouse_constraint->surface == pConstraintSurface )
wlserver_update_cursor_constraint();
return newEntry;
}
void gamescope_xwayland_server_t::wayland_commit(struct wlr_surface *surf, struct wlr_buffer *buf)
{
std::optional<ResListEntry_t> oEntry = PrepareCommit( surf, buf );
if ( !oEntry )
return;
{
std::lock_guard<std::mutex> lock( wayland_commit_lock );
wayland_commit_queue.emplace_back( std::move( *oEntry ) );
}
nudge_steamcompmgr();
}
struct PendingCommit_t
{
struct wlr_surface *surf;
struct wlr_buffer *buf;
};
std::list<PendingCommit_t> g_PendingCommits;
void wlserver_xdg_commit(struct wlr_surface *surf, struct wlr_buffer *buf)
{
std::optional<ResListEntry_t> oEntry = PrepareCommit( surf, buf );
if ( !oEntry )
return;
{
std::lock_guard<std::mutex> lock( wlserver.xdg_commit_lock );
wlserver.xdg_commit_queue.push_back( std::move( *oEntry ) );
}
nudge_steamcompmgr();
}
void xwayland_surface_commit(struct wlr_surface *wlr_surface) {
wlr_surface->current.committed = 0;
// Committing without buffer state is valid and commits the same buffer again.
// Mutter and Weston have forward progress on the frame callback in this situation,
// so let the commit go through. It will be duplication-eliminated later.
VulkanWlrTexture_t *tex = (VulkanWlrTexture_t *) wlr_surface_get_texture( wlr_surface );
if ( tex == NULL )
{
return;
}
struct wlr_buffer *buf = wlr_buffer_lock( tex->buf );
gpuvis_trace_printf( "xwayland_surface_commit wlr_surface %p", wlr_surface );
wlserver_x11_surface_info *wlserver_x11_surface_info = get_wl_surface_info(wlr_surface)->x11_surface;
wlserver_xdg_surface_info *wlserver_xdg_surface_info = get_wl_surface_info(wlr_surface)->xdg_surface;
if (wlserver_x11_surface_info)
{
assert(wlserver_x11_surface_info->xwayland_server);
wlserver_x11_surface_info->xwayland_server->wayland_commit( wlr_surface, buf );
}
else if (wlserver_xdg_surface_info)
{
wlserver_xdg_commit(wlr_surface, buf);
}
else
{
g_PendingCommits.push_back(PendingCommit_t{ wlr_surface, buf });
}
}
void gamescope_xwayland_server_t::on_xwayland_ready(void *data)
{
xwayland_ready = true;
if (!xwayland_server->options.no_touch_pointer_emulation)
wl_log.infof("Xwayland doesn't support -noTouchPointerEmulation, touch events might get duplicated");
dpy = XOpenDisplay( get_nested_display_name() );
}
void gamescope_xwayland_server_t::xwayland_ready_callback(struct wl_listener *listener, void *data)
{
gamescope_xwayland_server_t *server = wl_container_of( listener, server, xwayland_ready_listener );
server->on_xwayland_ready(data);
}
static void bump_input_counter()
{
inputCounter++;
nudge_steamcompmgr();
}
static void wlserver_handle_modifiers(struct wl_listener *listener, void *data)
{
struct wlserver_keyboard *keyboard = wl_container_of( listener, keyboard, modifiers );
wlr_seat_set_keyboard( wlserver.wlr.seat, keyboard->wlr );
wlr_seat_keyboard_notify_modifiers( wlserver.wlr.seat, &keyboard->wlr->modifiers );
bump_input_counter();
}
static void wlserver_handle_key(struct wl_listener *listener, void *data)
{
struct wlserver_keyboard *keyboard = wl_container_of( listener, keyboard, key );
struct wlr_keyboard_key_event *event = (struct wlr_keyboard_key_event *) data;
xkb_keycode_t keycode = event->keycode + 8;
xkb_keysym_t keysym = xkb_state_key_get_one_sym(keyboard->wlr->xkb_state, keycode);
#if HAVE_SESSION
if (wlserver.wlr.session && event->state == WL_KEYBOARD_KEY_STATE_PRESSED && keysym >= XKB_KEY_XF86Switch_VT_1 && keysym <= XKB_KEY_XF86Switch_VT_12) {
unsigned vt = keysym - XKB_KEY_XF86Switch_VT_1 + 1;
wlr_session_change_vt(wlserver.wlr.session, vt);
return;
}
#endif
bool forbidden_key =
keysym == XKB_KEY_XF86AudioLowerVolume ||
keysym == XKB_KEY_XF86AudioRaiseVolume ||
keysym == XKB_KEY_XF86PowerOff;
if ( ( event->state == WL_KEYBOARD_KEY_STATE_PRESSED || event->state == WL_KEYBOARD_KEY_STATE_RELEASED ) && forbidden_key )
{
// Always send volume+/- to root server only, to avoid it reaching the game.
struct wlr_surface *old_kb_surf = wlserver.kb_focus_surface;
struct wlr_surface *new_kb_surf = steamcompmgr_get_server_input_surface( 0 );
if ( new_kb_surf )
{
wlserver_keyboardfocus( new_kb_surf, false );
wlr_seat_set_keyboard( wlserver.wlr.seat, keyboard->wlr );
wlr_seat_keyboard_notify_key( wlserver.wlr.seat, event->time_msec, event->keycode, event->state );
wlserver_keyboardfocus( old_kb_surf, false );
return;
}
}
wlr_seat_set_keyboard( wlserver.wlr.seat, keyboard->wlr );
wlr_seat_keyboard_notify_key( wlserver.wlr.seat, event->time_msec, event->keycode, event->state );
bump_input_counter();
}
static void wlserver_perform_rel_pointer_motion(double unaccel_dx, double unaccel_dy)
{
assert( wlserver_is_lock_held() );
unaccel_dx *= g_mouseSensitivity;
unaccel_dy *= g_mouseSensitivity;
wlr_relative_pointer_manager_v1_send_relative_motion( wlserver.relative_pointer_manager, wlserver.wlr.seat, 0, unaccel_dx, unaccel_dy, unaccel_dx, unaccel_dy );
}
static void wlserver_handle_pointer_motion(struct wl_listener *listener, void *data)
{
struct wlr_pointer_motion_event *event = (struct wlr_pointer_motion_event *) data;
wlserver_mousemotion(event->unaccel_dx, event->unaccel_dy, event->time_msec);
}
void wlserver_open_steam_menu( bool qam )
{
gamescope_xwayland_server_t *server = wlserver_get_xwayland_server( 0 );
if (!server)
return;
uint32_t keycode = qam ? XK_2 : XK_1;
XTestFakeKeyEvent(server->get_xdisplay(), XKeysymToKeycode( server->get_xdisplay(), XK_Control_L ), True, CurrentTime);
XTestFakeKeyEvent(server->get_xdisplay(), XKeysymToKeycode( server->get_xdisplay(), keycode ), True, CurrentTime);
XTestFakeKeyEvent(server->get_xdisplay(), XKeysymToKeycode( server->get_xdisplay(), keycode ), False, CurrentTime);
XTestFakeKeyEvent(server->get_xdisplay(), XKeysymToKeycode( server->get_xdisplay(), XK_Control_L ), False, CurrentTime);
}
static void wlserver_handle_pointer_button(struct wl_listener *listener, void *data)
{
struct wlserver_pointer *pointer = wl_container_of( listener, pointer, button );
struct wlr_pointer_button_event *event = (struct wlr_pointer_button_event *) data;
wlr_seat_pointer_notify_button( wlserver.wlr.seat, event->time_msec, event->button, event->state );
}
static void wlserver_handle_pointer_axis(struct wl_listener *listener, void *data)
{
struct wlserver_pointer *pointer = wl_container_of( listener, pointer, axis );
struct wlr_pointer_axis_event *event = (struct wlr_pointer_axis_event *) data;
wlr_seat_pointer_notify_axis( wlserver.wlr.seat, event->time_msec, event->orientation, event->delta, event->delta_discrete, event->source, event->relative_direction );
}
static void wlserver_handle_pointer_frame(struct wl_listener *listener, void *data)
{
wlr_seat_pointer_notify_frame( wlserver.wlr.seat );
bump_input_counter();
}
static inline uint32_t TouchClickModeToLinuxButton( gamescope::TouchClickMode eTouchClickMode )
{
switch ( eTouchClickMode )
{
default:
case gamescope::TouchClickModes::Hover:
return 0;
case gamescope::TouchClickModes::Trackpad:
case gamescope::TouchClickModes::Left:
return BTN_LEFT;
case gamescope::TouchClickModes::Right:
return BTN_RIGHT;
case gamescope::TouchClickModes::Middle:
return BTN_MIDDLE;
}
}
std::atomic<bool> g_bPendingTouchMovement = { false };
static void wlserver_handle_touch_down(struct wl_listener *listener, void *data)
{
struct wlserver_touch *touch = wl_container_of( listener, touch, down );
struct wlr_touch_down_event *event = (struct wlr_touch_down_event *) data;
wlserver_touchdown( event->x, event->y, event->touch_id, event->time_msec );
}
static void wlserver_handle_touch_up(struct wl_listener *listener, void *data)
{
struct wlserver_touch *touch = wl_container_of( listener, touch, up );
struct wlr_touch_up_event *event = (struct wlr_touch_up_event *) data;
wlserver_touchup( event->touch_id, event->time_msec );
}
static void wlserver_handle_touch_motion(struct wl_listener *listener, void *data)
{
struct wlserver_touch *touch = wl_container_of( listener, touch, motion );
struct wlr_touch_motion_event *event = (struct wlr_touch_motion_event *) data;
wlserver_touchmotion( event->x, event->y, event->touch_id, event->time_msec );
}
static void wlserver_new_input(struct wl_listener *listener, void *data)
{
struct wlr_input_device *device = (struct wlr_input_device *) data;
switch ( device->type )
{
case WLR_INPUT_DEVICE_KEYBOARD:
{
struct wlserver_keyboard *keyboard = (struct wlserver_keyboard *) calloc( 1, sizeof( struct wlserver_keyboard ) );
keyboard->wlr = (struct wlr_keyboard *)device;
struct xkb_rule_names rules = { 0 };
struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
rules.rules = getenv("XKB_DEFAULT_RULES");
rules.model = getenv("XKB_DEFAULT_MODEL");
rules.layout = getenv("XKB_DEFAULT_LAYOUT");
rules.variant = getenv("XKB_DEFAULT_VARIANT");
rules.options = getenv("XKB_DEFAULT_OPTIONS");
struct xkb_keymap *keymap = xkb_keymap_new_from_names(context, &rules,
XKB_KEYMAP_COMPILE_NO_FLAGS);
wlr_keyboard_set_keymap(keyboard->wlr, keymap);
xkb_keymap_unref(keymap);
xkb_context_unref(context);
wlr_keyboard_set_repeat_info(keyboard->wlr, 25, 600);
keyboard->wlr->data = keyboard;
keyboard->modifiers.notify = wlserver_handle_modifiers;
wl_signal_add( &keyboard->wlr->events.modifiers, &keyboard->modifiers );
keyboard->key.notify = wlserver_handle_key;
wl_signal_add( &keyboard->wlr->events.key, &keyboard->key );
}
break;
case WLR_INPUT_DEVICE_POINTER:
{
struct wlserver_pointer *pointer = (struct wlserver_pointer *) calloc( 1, sizeof( struct wlserver_pointer ) );
pointer->wlr = (struct wlr_pointer *)device;
pointer->motion.notify = wlserver_handle_pointer_motion;
wl_signal_add( &pointer->wlr->events.motion, &pointer->motion );
pointer->button.notify = wlserver_handle_pointer_button;
wl_signal_add( &pointer->wlr->events.button, &pointer->button );
pointer->axis.notify = wlserver_handle_pointer_axis;
wl_signal_add( &pointer->wlr->events.axis, &pointer->axis);
pointer->frame.notify = wlserver_handle_pointer_frame;
wl_signal_add( &pointer->wlr->events.frame, &pointer->frame);
}
break;
case WLR_INPUT_DEVICE_TOUCH:
{
struct wlserver_touch *touch = (struct wlserver_touch *) calloc( 1, sizeof( struct wlserver_touch ) );
touch->wlr = (struct wlr_touch *)device;
touch->down.notify = wlserver_handle_touch_down;
wl_signal_add( &touch->wlr->events.down, &touch->down );
touch->up.notify = wlserver_handle_touch_up;
wl_signal_add( &touch->wlr->events.up, &touch->up );
touch->motion.notify = wlserver_handle_touch_motion;
wl_signal_add( &touch->wlr->events.motion, &touch->motion );
}
break;
default:
break;
}
}
static struct wl_listener new_input_listener = { .notify = wlserver_new_input };
wlserver_wl_surface_info *get_wl_surface_info(struct wlr_surface *wlr_surf)
{
if (!wlr_surf)
return NULL;
return reinterpret_cast<wlserver_wl_surface_info *>(wlr_surf->data);
}
static void handle_wl_surface_commit( struct wl_listener *l, void *data )
{
wlserver_wl_surface_info *surf = wl_container_of( l, surf, commit );
xwayland_surface_commit(surf->wlr);
}
static void handle_wl_surface_destroy( struct wl_listener *l, void *data )
{
wlserver_wl_surface_info *surf = wl_container_of( l, surf, destroy );
if (surf->x11_surface)
{
wlserver_x11_surface_info *x11_surface = surf->x11_surface;
wlserver_x11_surface_info_finish(x11_surface);
// Re-init it so it can be destroyed for good on the x11 side.
// This just clears it out from the main wl surface mainly.
//
// wl_list_remove leaves stuff in a weird state, so we need to call
// this to re-init the list to avoid a crash.
wlserver_x11_surface_info_init(x11_surface, x11_surface->xwayland_server, x11_surface->x11_id);
}
if ( surf->wlr == wlserver.mouse_focus_surface )
wlserver.mouse_focus_surface = nullptr;
if ( surf->wlr == wlserver.kb_focus_surface )
wlserver.kb_focus_surface = nullptr;
for (auto it = g_PendingCommits.begin(); it != g_PendingCommits.end();)
{
if (it->surf == surf->wlr)
{
// We owned the buffer lock, so unlock it here.
wlr_buffer_unlock(it->buf);
it = g_PendingCommits.erase(it);
}
else
{
it++;
}
}
for (auto& feedback : surf->pending_presentation_feedbacks)
wp_presentation_feedback_send_discarded(feedback);
surf->pending_presentation_feedbacks.clear();
surf->wlr->data = nullptr;
for ( wl_resource *pSwapchain : surf->gamescope_swapchains )
{
wl_resource_set_user_data( pSwapchain, nullptr );
}
delete surf;
}
static void wlserver_new_surface(struct wl_listener *l, void *data)
{
struct wlr_surface *wlr_surf = (struct wlr_surface *)data;
uint32_t id = wl_resource_get_id(wlr_surf->resource);
wlserver_wl_surface_info *wl_surface_info = new wlserver_wl_surface_info;
wl_surface_info->wlr = wlr_surf;
wl_surface_info->destroy.notify = handle_wl_surface_destroy;
wl_signal_add( &wlr_surf->events.destroy, &wl_surface_info->destroy );
wl_surface_info->commit.notify = handle_wl_surface_commit;
wl_signal_add( &wlr_surf->events.commit, &wl_surface_info->commit );
wlr_surf->data = wl_surface_info;
struct wlserver_x11_surface_info *s, *tmp;
wl_list_for_each_safe(s, tmp, &pending_surfaces, pending_link)
{
if (s->wl_id == id && s->main_surface == nullptr)
{
wlserver_x11_surface_info_set_wlr( s, wlr_surf, false );
}
}
}
static struct wl_listener new_surface_listener = { .notify = wlserver_new_surface };
void gamescope_xwayland_server_t::destroy_content_override( struct wlserver_content_override *co )
{
#ifdef GAMESCOPE_SWAPCHAIN_DEBUG
wl_log.infof( "destroy_content_override REAL: co: %p co->surface: %p co->x11_window: 0x%x co->gamescope_swapchain: %p", co, co->surface, co->x11_window, co->gamescope_swapchain );
#endif
if ( co->surface )
{
wlserver_wl_surface_info *wl_surface_info = get_wl_surface_info( co->surface );
if ( wl_surface_info )
wl_surface_info->x11_surface = nullptr;
}
if ( co->gamescope_swapchain )
{
gamescope_swapchain_send_retired(co->gamescope_swapchain);
}
wl_list_remove( &co->surface_destroy_listener.link );
content_overrides.erase( co->x11_window );
free( co );
}
void gamescope_xwayland_server_t::destroy_content_override( struct wlserver_x11_surface_info *x11_surface, struct wlr_surface *surf )
{
#ifdef GAMESCOPE_SWAPCHAIN_DEBUG
wl_log.infof( "destroy_content_override LOOKUP: x11_surface: %p x11_window: 0x%x surf: %p", x11_surface, x11_surface->x11_id, surf );
#endif
auto iter = content_overrides.find( x11_surface->x11_id );
if (iter == content_overrides.end())
return;
if ( x11_surface->override_surface == surf )
x11_surface->override_surface = nullptr;
struct wlserver_content_override *co = iter->second;
#ifdef GAMESCOPE_SWAPCHAIN_DEBUG
wl_log.infof( "destroy_content_override LOOKUP FOUND: x11_surface: %p x11_window: 0x%x surf: %p co: %p co->surface: %p", x11_surface, x11_surface->x11_id, surf, co, co->surface );
#endif
co->gamescope_swapchain = nullptr;
if (co->surface == surf)
destroy_content_override(iter->second);
}
static void content_override_handle_surface_destroy( struct wl_listener *listener, void *data )
{
struct wlserver_content_override *co = wl_container_of( listener, co, surface_destroy_listener );
gamescope_xwayland_server_t *server = co->server;
assert( server );
server->destroy_content_override( co );
}
static void gamescope_swapchain_destroy_co( struct wl_resource *resource );
void gamescope_xwayland_server_t::handle_override_window_content( struct wl_client *client, struct wl_resource *resource, struct wlr_surface *surface, uint32_t x11_window )
{
wlserver_x11_surface_info *x11_surface = lookup_x11_surface_info_from_xid( this, x11_window );
// If we found an x11_surface, go back up to our parent.
if ( x11_surface )
x11_window = x11_surface->x11_id;
#ifdef GAMESCOPE_SWAPCHAIN_DEBUG
wl_log.infof( "handle_override_window_content: (1) x11_window: 0x%x swapchain_resource: %p surface: %p", x11_window, resource, surface );
#endif
if ( content_overrides.count( x11_window ) ) {
if ( content_overrides[x11_window]->gamescope_swapchain == resource )
return;
#ifdef GAMESCOPE_SWAPCHAIN_DEBUG
wl_log.infof( "handle_override_window_content: (2) DESTROYING x11_window: 0x%x old_swapchain: %p new_swapchain: %p", x11_window, content_overrides[x11_window]->gamescope_swapchain, resource );
#endif
destroy_content_override( content_overrides[ x11_window ] );
}
gamescope_swapchain_destroy_co( resource );
struct wlserver_content_override *co = (struct wlserver_content_override *)calloc(1, sizeof(*co));
co->server = this;
co->surface = surface;
co->x11_window = x11_window;
co->gamescope_swapchain = resource;
co->surface_destroy_listener.notify = content_override_handle_surface_destroy;
wl_signal_add( &surface->events.destroy, &co->surface_destroy_listener );
content_overrides[ x11_window ] = co;
#ifdef GAMESCOPE_SWAPCHAIN_DEBUG
wl_log.infof( "handle_override_window_content: (3) x11_window: 0x%x swapchain_resource: %p surface: %p co: %p", x11_window, resource, surface, co );
#endif
if ( x11_surface )
wlserver_x11_surface_info_set_wlr( x11_surface, surface, true );
if ( x11_surface )
{
for (auto it = g_PendingCommits.begin(); it != g_PendingCommits.end();)
{
if (it->surf == surface)
{
PendingCommit_t pending = *it;
// Still have the buffer lock from before...
assert(x11_surface);
assert(x11_surface->xwayland_server);
x11_surface->xwayland_server->wayland_commit( pending.surf, pending.buf );
it = g_PendingCommits.erase(it);
}
else
{
it++;
}
}
}
}
struct wl_client *gamescope_xwayland_server_t::get_client()
{
if (!xwayland_server)
return nullptr;
return xwayland_server->client;
}
struct wlr_output *gamescope_xwayland_server_t::get_output()
{
return output;
}
struct wlr_output_state *gamescope_xwayland_server_t::get_output_state()
{
return output_state;
}
static void gamescope_xwayland_handle_override_window_content( struct wl_client *client, struct wl_resource *resource, struct wl_resource *surface_resource, uint32_t x11_window )
{
// This should ideally use the surface's xwayland, but we don't know it.
// We probably need to change our override_window_content protocol to add a
// xwayland socket name.
//
// Right now, the surface -> xwayland association comes from the
// handle_wl_id stuff from steamcompmgr.
// However, this surface has no associated X window, and won't recieve
// wl_id stuff as it's meant to replace another window's surface
// which we can't do without knowing the x11_window's xwayland server
// here for it to do that override logic in the first place.
//
// So... Just assume it comes from server 0 for now.
gamescope_xwayland_server_t *server = wlserver_get_xwayland_server( 0 );
assert( server );
struct wlr_surface *surface = wlr_surface_from_resource( surface_resource );
server->handle_override_window_content(client, resource, surface, x11_window);
}
static void gamescope_xwayland_handle_destroy( struct wl_client *client, struct wl_resource *resource )
{
wl_resource_destroy( resource );
}
static const struct gamescope_xwayland_interface gamescope_xwayland_impl = {
.destroy = gamescope_xwayland_handle_destroy,
.override_window_content = gamescope_xwayland_handle_override_window_content,
};
static void gamescope_xwayland_bind( struct wl_client *client, void *data, uint32_t version, uint32_t id )
{
struct wl_resource *resource = wl_resource_create( client, &gamescope_xwayland_interface, version, id );
wl_resource_set_implementation( resource, &gamescope_xwayland_impl, NULL, NULL );
}
static void create_gamescope_xwayland( void )
{
uint32_t version = 1;
wl_global_create( wlserver.display, &gamescope_xwayland_interface, version, NULL, gamescope_xwayland_bind );
}
static void gamescope_swapchain_destroy_co( struct wl_resource *resource )
{
#ifdef GAMESCOPE_SWAPCHAIN_DEBUG
wl_log.infof( "gamescope_swapchain_destroy_co swapchain: %p", resource );
#endif
wlserver_wl_surface_info *wl_surface_info = (wlserver_wl_surface_info *)wl_resource_get_user_data( resource );
if ( wl_surface_info )
{
#ifdef GAMESCOPE_SWAPCHAIN_DEBUG
wl_log.infof( "gamescope_swapchain_destroy_co swapchain: %p GOT WAYLAND SURFACE", resource );
#endif
wlserver_x11_surface_info *x11_surface = wl_surface_info->x11_surface;
if (x11_surface)
{
#ifdef GAMESCOPE_SWAPCHAIN_DEBUG
wl_log.infof( "gamescope_swapchain_destroy_co swapchain: %p GOT X11 SURFACE", resource );
#endif
x11_surface->xwayland_server->destroy_content_override( x11_surface, wl_surface_info->wlr );
}
}
}
static void gamescope_swapchain_handle_resource_destroy( struct wl_resource *resource )
{
#ifdef GAMESCOPE_SWAPCHAIN_DEBUG
wl_log.infof( "gamescope_swapchain_handle_resource_destroy swapchain: %p", resource );
#endif
wlserver_wl_surface_info *wl_surface_info = (wlserver_wl_surface_info *)wl_resource_get_user_data( resource );
if ( wl_surface_info )
{
gamescope_swapchain_destroy_co( resource );
std::erase(wl_surface_info->gamescope_swapchains, resource);
}
}
static void gamescope_swapchain_destroy( struct wl_client *client, struct wl_resource *resource )
{
wl_resource_destroy( resource );
}
static void gamescope_swapchain_override_window_content( struct wl_client *client, struct wl_resource *resource, uint32_t server_id, uint32_t x11_window )
{
wlserver_wl_surface_info *wl_surface_info = (wlserver_wl_surface_info *)wl_resource_get_user_data( resource );
gamescope_xwayland_server_t *server = wlserver_get_xwayland_server( server_id );
assert( server );
server->handle_override_window_content(client, resource, wl_surface_info->wlr, x11_window);
}
static void gamescope_swapchain_swapchain_feedback( struct wl_client *client, struct wl_resource *resource,
uint32_t image_count,
uint32_t vk_format,
uint32_t vk_colorspace,
uint32_t vk_composite_alpha,
uint32_t vk_pre_transform,
uint32_t vk_clipped)
{
wlserver_wl_surface_info *wl_info = (wlserver_wl_surface_info *)wl_resource_get_user_data( resource );
if ( wl_info )
{
wl_info->swapchain_feedback = std::make_unique<wlserver_vk_swapchain_feedback>(wlserver_vk_swapchain_feedback{
.image_count = image_count,
.vk_format = VkFormat(vk_format),
.vk_colorspace = VkColorSpaceKHR(vk_colorspace),
.vk_composite_alpha = VkCompositeAlphaFlagBitsKHR(vk_composite_alpha),
.vk_pre_transform = VkSurfaceTransformFlagBitsKHR(vk_pre_transform),
.vk_clipped = VkBool32(vk_clipped),
.hdr_metadata_blob = nullptr,
});
}
}
static void gamescope_swapchain_set_hdr_metadata( struct wl_client *client, struct wl_resource *resource,
uint32_t display_primary_red_x,
uint32_t display_primary_red_y,
uint32_t display_primary_green_x,
uint32_t display_primary_green_y,
uint32_t display_primary_blue_x,
uint32_t display_primary_blue_y,
uint32_t white_point_x,
uint32_t white_point_y,
uint32_t max_display_mastering_luminance,
uint32_t min_display_mastering_luminance,
uint32_t max_cll,
uint32_t max_fall)
{
wlserver_wl_surface_info *wl_info = (wlserver_wl_surface_info *)wl_resource_get_user_data( resource );
if ( wl_info )
{
if ( !wl_info->swapchain_feedback ) {
wl_log.errorf("set_hdr_metadata with no swapchain_feedback.");
return;
}
// Check validity of this metadata,
// if it's garbage, just toss it...
if (!max_cll || !max_fall || (!white_point_x && !white_point_y))
return;
hdr_output_metadata metadata = {};
metadata.metadata_type = 0;
hdr_metadata_infoframe& infoframe = metadata.hdmi_metadata_type1;
infoframe.eotf = HDMI_EOTF_ST2084;
infoframe.metadata_type = 0;
infoframe.display_primaries[0].x = display_primary_red_x;
infoframe.display_primaries[0].y = display_primary_red_y;
infoframe.display_primaries[1].x = display_primary_green_x;
infoframe.display_primaries[1].y = display_primary_green_y;
infoframe.display_primaries[2].x = display_primary_blue_x;
infoframe.display_primaries[2].y = display_primary_blue_y;
infoframe.white_point.x = white_point_x;
infoframe.white_point.y = white_point_y;
infoframe.max_display_mastering_luminance = max_display_mastering_luminance;
infoframe.min_display_mastering_luminance = min_display_mastering_luminance;
infoframe.max_cll = max_cll;
infoframe.max_fall = max_fall;
wl_info->swapchain_feedback->hdr_metadata_blob = GetBackend()->CreateBackendBlob( metadata );
}
}
static void gamescope_swapchain_set_present_time( struct wl_client *client, struct wl_resource *resource,
uint32_t present_id,
uint32_t desired_present_time_hi,
uint32_t desired_present_time_lo)
{
wlserver_wl_surface_info *wl_info = (wlserver_wl_surface_info *)wl_resource_get_user_data( resource );
if ( wl_info )
{
wl_info->present_id = present_id;
wl_info->desired_present_time = (uint64_t(desired_present_time_hi) << 32) | desired_present_time_lo;
}
}
static void gamescope_swapchain_set_present_mode( struct wl_client *client, struct wl_resource *resource, uint32_t present_mode )
{
wlserver_wl_surface_info *wl_info = (wlserver_wl_surface_info *)wl_resource_get_user_data( resource );
if ( wl_info )
{
wl_info->oCurrentPresentMode = VkPresentModeKHR( present_mode );
}
}
static const struct gamescope_swapchain_interface gamescope_swapchain_impl = {
.destroy = gamescope_swapchain_destroy,
.override_window_content = gamescope_swapchain_override_window_content,
.swapchain_feedback = gamescope_swapchain_swapchain_feedback,
.set_present_mode = gamescope_swapchain_set_present_mode,
.set_hdr_metadata = gamescope_swapchain_set_hdr_metadata,
.set_present_time = gamescope_swapchain_set_present_time,
};
static void gamescope_swapchain_factory_v2_destroy( struct wl_client *client, struct wl_resource *resource )
{
wl_resource_destroy( resource );
}
static void gamescope_swapchain_factory_v2_create_swapchain( struct wl_client *client, struct wl_resource *resource, struct wl_resource *surface_resource, uint32_t id )
{
struct wlr_surface *surface = wlr_surface_from_resource( surface_resource );
wlserver_wl_surface_info *wl_surface_info = get_wl_surface_info(surface);
struct wl_resource *gamescope_swapchain_resource
= wl_resource_create( client, &gamescope_swapchain_interface, wl_resource_get_version( resource ), id );
wl_resource_set_implementation( gamescope_swapchain_resource, &gamescope_swapchain_impl, wl_surface_info, gamescope_swapchain_handle_resource_destroy );
if (wl_surface_info->gamescope_swapchains.size())
wl_log.errorf("create_swapchain: Surface already had a gamescope_swapchain! Warning!");
wl_surface_info->gamescope_swapchains.emplace_back( gamescope_swapchain_resource );
}
static const struct gamescope_swapchain_factory_v2_interface gamescope_swapchain_factory_v2_impl = {
.destroy = gamescope_swapchain_factory_v2_destroy,
.create_swapchain = gamescope_swapchain_factory_v2_create_swapchain,
};