forked from ValveSoftware/steamos-compositor
-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathWaylandBackend.cpp
3036 lines (2550 loc) · 122 KB
/
WaylandBackend.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 "backend.h"
#include "rendervulkan.hpp"
#include "wlserver.hpp"
#include "vblankmanager.hpp"
#include "steamcompmgr.hpp"
#include "edid.h"
#include "Utils/Defer.h"
#include "Utils/Algorithm.h"
#include "convar.h"
#include "refresh_rate.h"
#include "waitable.h"
#include "Utils/TempFiles.h"
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <csignal>
#include <sys/mman.h>
#include <poll.h>
#include <linux/input-event-codes.h>
#include <xkbcommon/xkbcommon.h>
#include <libdecor.h>
#include "wlr_begin.hpp"
#include <wayland-client.h>
#include <xdg-shell-client-protocol.h>
#include <linux-dmabuf-v1-client-protocol.h>
#include <viewporter-client-protocol.h>
#include <single-pixel-buffer-v1-client-protocol.h>
#include <presentation-time-client-protocol.h>
#include <frog-color-management-v1-client-protocol.h>
#include <color-management-v1-client-protocol.h>
#include <pointer-constraints-unstable-v1-client-protocol.h>
#include <relative-pointer-unstable-v1-client-protocol.h>
#include <primary-selection-unstable-v1-client-protocol.h>
#include <fractional-scale-v1-client-protocol.h>
#include <xdg-toplevel-icon-v1-client-protocol.h>
#include "wlr_end.hpp"
#include "drm_include.h"
#define WL_FRACTIONAL_SCALE_DENOMINATOR 120
extern int g_nPreferredOutputWidth;
extern int g_nPreferredOutputHeight;
extern bool g_bForceHDR10OutputDebug;
extern bool g_bBorderlessOutputWindow;
extern gamescope::ConVar<bool> cv_adaptive_sync;
extern gamescope::ConVar<bool> cv_composite_force;
extern bool g_bColorSliderInUse;
extern bool fadingOut;
extern std::string g_reshade_effect;
using namespace std::literals;
static LogScope xdg_log( "xdg_backend" );
static const char *GAMESCOPE_plane_tag = "gamescope-plane";
template <typename Func, typename... Args>
auto CallWithAllButLast(Func pFunc, Args&&... args)
{
auto Forwarder = [&] <typename Tuple, size_t... idx> (Tuple&& tuple, std::index_sequence<idx...>)
{
return pFunc(std::get<idx>(std::forward<Tuple>(tuple))...);
};
return Forwarder(std::forward_as_tuple(args...), std::make_index_sequence<sizeof...(Args) - 1>());
}
static inline uint32_t WaylandScaleToPhysical( uint32_t pValue, uint32_t pFactor ) {
return pValue * pFactor / WL_FRACTIONAL_SCALE_DENOMINATOR;
}
static inline uint32_t WaylandScaleToLogical( uint32_t pValue, uint32_t pFactor ) {
return div_roundup( pValue * WL_FRACTIONAL_SCALE_DENOMINATOR, pFactor );
}
static bool IsSurfacePlane( wl_surface *pSurface ) {
// HACK: this probably should never be called with a null pointer, but it
// was happening after a window was closed.
return pSurface && (wl_proxy_get_tag( (wl_proxy *)pSurface ) == &GAMESCOPE_plane_tag);
}
#define WAYLAND_NULL() []<typename... Args> ( void *pData, Args... args ) { }
#define WAYLAND_USERDATA_TO_THIS(type, name) []<typename... Args> ( void *pData, Args... args ) { type *pThing = (type *)pData; pThing->name( std::forward<Args>(args)... ); }
// Libdecor puts its userdata ptr at the end... how fun! I shouldn't have spent so long writing this total atrocity to mankind.
#define LIBDECOR_USERDATA_TO_THIS(type, name) []<typename... Args> ( Args... args ) { type *pThing = (type *)std::get<sizeof...(Args)-1>(std::forward_as_tuple(args...)); CallWithAllButLast([&]<typename... Args2>(Args2... args2){ pThing->name(std::forward<Args2>(args2)...); }, std::forward<Args>(args)...); }
extern gamescope::ConVar<bool> cv_hdr_enabled;
namespace gamescope
{
extern std::shared_ptr<INestedHints::CursorInfo> GetX11HostCursor();
gamescope::ConVar<bool> cv_wayland_mouse_warp_without_keyboard_focus( "wayland_mouse_warp_without_keyboard_focus", true, "Should we only forward mouse warps to the app when we have keyboard focus?" );
gamescope::ConVar<bool> cv_wayland_mouse_relmotion_without_keyboard_focus( "wayland_mouse_relmotion_without_keyboard_focus", false, "Should we only forward mouse relative motion to the app when we have keyboard focus?" );
gamescope::ConVar<bool> cv_wayland_use_modifiers( "wayland_use_modifiers", true, "Use DMA-BUF modifiers?" );
class CWaylandConnector;
class CWaylandPlane;
class CWaylandBackend;
class CWaylandFb;
struct WaylandPlaneState
{
wl_buffer *pBuffer;
int32_t nDestX;
int32_t nDestY;
double flSrcX;
double flSrcY;
double flSrcWidth;
double flSrcHeight;
int32_t nDstWidth;
int32_t nDstHeight;
GamescopeAppTextureColorspace eColorspace;
bool bOpaque;
uint32_t uFractionalScale;
};
inline WaylandPlaneState ClipPlane( const WaylandPlaneState &state )
{
int32_t nClippedDstWidth = std::min<int32_t>( g_nOutputWidth, state.nDstWidth + state.nDestX ) - state.nDestX;
int32_t nClippedDstHeight = std::min<int32_t>( g_nOutputHeight, state.nDstHeight + state.nDestY ) - state.nDestY;
double flClippedSrcWidth = state.flSrcWidth * ( nClippedDstWidth / double( state.nDstWidth ) );
double flClippedSrcHeight = state.flSrcHeight * ( nClippedDstHeight / double( state.nDstHeight ) );
WaylandPlaneState outState = state;
outState.nDstWidth = nClippedDstWidth;
outState.nDstHeight = nClippedDstHeight;
outState.flSrcWidth = flClippedSrcWidth;
outState.flSrcHeight = flClippedSrcHeight;
return outState;
}
static int CreateShmBuffer( uint32_t uSize, void *pData )
{
char szShmBufferPath[ PATH_MAX ];
int nFd = MakeTempFile( szShmBufferPath, k_szGamescopeTempShmTemplate );
if ( nFd < 0 )
return -1;
if ( ftruncate( nFd, uSize ) < 0 )
{
close( nFd );
return -1;
}
if ( pData )
{
void *pMappedData = mmap( nullptr, uSize, PROT_READ | PROT_WRITE, MAP_SHARED, nFd, 0 );
if ( pMappedData == MAP_FAILED )
return -1;
defer( munmap( pMappedData, uSize ) );
memcpy( pMappedData, pData, uSize );
}
return nFd;
}
class CWaylandPlane
{
public:
CWaylandPlane( CWaylandConnector *pBackend );
~CWaylandPlane();
bool Init( CWaylandPlane *pParent, CWaylandPlane *pSiblingBelow );
uint32_t GetScale() const;
void Present( std::optional<WaylandPlaneState> oState );
void Present( const FrameInfo_t::Layer_t *pLayer );
void CommitLibDecor( libdecor_configuration *pConfiguration );
void Commit();
wl_surface *GetSurface() const { return m_pSurface; }
libdecor_frame *GetFrame() const { return m_pFrame; }
xdg_toplevel *GetXdgToplevel() const;
std::optional<WaylandPlaneState> GetCurrentState() { std::unique_lock lock( m_PlaneStateLock ); return m_oCurrentPlaneState; }
void UpdateVRRRefreshRate();
private:
void Wayland_Surface_Enter( wl_surface *pSurface, wl_output *pOutput );
void Wayland_Surface_Leave( wl_surface *pSurface, wl_output *pOutput );
static const wl_surface_listener s_SurfaceListener;
void LibDecor_Frame_Configure( libdecor_frame *pFrame, libdecor_configuration *pConfiguration );
void LibDecor_Frame_Close( libdecor_frame *pFrame );
void LibDecor_Frame_Commit( libdecor_frame *pFrame );
void LibDecor_Frame_DismissPopup( libdecor_frame *pFrame, const char *pSeatName );
static libdecor_frame_interface s_LibDecorFrameInterface;
void Wayland_PresentationFeedback_SyncOutput( struct wp_presentation_feedback *pFeedback, wl_output *pOutput );
void Wayland_PresentationFeedback_Presented( struct wp_presentation_feedback *pFeedback, uint32_t uTVSecHi, uint32_t uTVSecLo, uint32_t uTVNSec, uint32_t uRefresh, uint32_t uSeqHi, uint32_t uSeqLo, uint32_t uFlags );
void Wayland_PresentationFeedback_Discarded( struct wp_presentation_feedback *pFeedback );
static const wp_presentation_feedback_listener s_PresentationFeedbackListener;
void Wayland_FrogColorManagedSurface_PreferredMetadata(
frog_color_managed_surface *pFrogSurface,
uint32_t uTransferFunction,
uint32_t uOutputDisplayPrimaryRedX,
uint32_t uOutputDisplayPrimaryRedY,
uint32_t uOutputDisplayPrimaryGreenX,
uint32_t uOutputDisplayPrimaryGreenY,
uint32_t uOutputDisplayPrimaryBlueX,
uint32_t uOutputDisplayPrimaryBlueY,
uint32_t uOutputWhitePointX,
uint32_t uOutputWhitePointY,
uint32_t uMaxLuminance,
uint32_t uMinLuminance,
uint32_t uMaxFullFrameLuminance );
static const frog_color_managed_surface_listener s_FrogColorManagedSurfaceListener;
void Wayland_WPColorManagementSurfaceFeedback_PreferredChanged( wp_color_management_surface_feedback_v1 *pColorManagementSurface, unsigned int data );
static const wp_color_management_surface_feedback_v1_listener s_WPColorManagementSurfaceListener;
void UpdateWPPreferredColorManagement();
void Wayland_WPImageDescriptionInfo_Done( wp_image_description_info_v1 *pImageDescInfo );
void Wayland_WPImageDescriptionInfo_ICCFile( wp_image_description_info_v1 *pImageDescInfo, int32_t nICCFd, uint32_t uICCSize );
void Wayland_WPImageDescriptionInfo_Primaries( wp_image_description_info_v1 *pImageDescInfo, int32_t nRedX, int32_t nRedY, int32_t nGreenX, int32_t nGreenY, int32_t nBlueX, int32_t nBlueY, int32_t nWhiteX, int32_t nWhiteY );
void Wayland_WPImageDescriptionInfo_PrimariesNamed( wp_image_description_info_v1 *pImageDescInfo, uint32_t uPrimaries );
void Wayland_WPImageDescriptionInfo_TFPower( wp_image_description_info_v1 *pImageDescInfo, uint32_t uExp);
void Wayland_WPImageDescriptionInfo_TFNamed( wp_image_description_info_v1 *pImageDescInfo, uint32_t uTF);
void Wayland_WPImageDescriptionInfo_Luminances( wp_image_description_info_v1 *pImageDescInfo, uint32_t uMinLum, uint32_t uMaxLum, uint32_t uRefLum );
void Wayland_WPImageDescriptionInfo_TargetPrimaries( wp_image_description_info_v1 *pImageDescInfo, int32_t nRedX, int32_t nRedY, int32_t nGreenX, int32_t nGreenY, int32_t nBlueX, int32_t nBlueY, int32_t nWhiteX, int32_t nWhiteY );
void Wayland_WPImageDescriptionInfo_TargetLuminance( wp_image_description_info_v1 *pImageDescInfo, uint32_t uMinLum, uint32_t uMaxLum );
void Wayland_WPImageDescriptionInfo_Target_MaxCLL( wp_image_description_info_v1 *pImageDescInfo, uint32_t uMaxCLL );
void Wayland_WPImageDescriptionInfo_Target_MaxFALL( wp_image_description_info_v1 *pImageDescInfo, uint32_t uMaxFALL );
static const wp_image_description_info_v1_listener s_ImageDescriptionInfoListener;
void Wayland_FractionalScale_PreferredScale( wp_fractional_scale_v1 *pFractionalScale, uint32_t uScale );
static const wp_fractional_scale_v1_listener s_FractionalScaleListener;
CWaylandConnector *m_pConnector = nullptr;
CWaylandBackend *m_pBackend = nullptr;
CWaylandPlane *m_pParent = nullptr;
wl_surface *m_pSurface = nullptr;
wp_viewport *m_pViewport = nullptr;
frog_color_managed_surface *m_pFrogColorManagedSurface = nullptr;
wp_color_management_surface_v1 *m_pWPColorManagedSurface = nullptr;
wp_color_management_surface_feedback_v1 *m_pWPColorManagedSurfaceFeedback = nullptr;
wp_fractional_scale_v1 *m_pFractionalScale = nullptr;
wl_subsurface *m_pSubsurface = nullptr;
libdecor_frame *m_pFrame = nullptr;
libdecor_window_state m_eWindowState = LIBDECOR_WINDOW_STATE_NONE;
std::vector<wl_output *> m_pOutputs;
bool m_bNeedsDecorCommit = false;
uint32_t m_uFractionalScale = 120;
bool m_bHasRecievedScale = false;
std::mutex m_PlaneStateLock;
std::optional<WaylandPlaneState> m_oCurrentPlaneState;
};
const wl_surface_listener CWaylandPlane::s_SurfaceListener =
{
.enter = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_Surface_Enter ),
.leave = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_Surface_Leave ),
.preferred_buffer_scale = WAYLAND_NULL(),
.preferred_buffer_transform = WAYLAND_NULL(),
};
// Can't be const, libdecor api bad...
libdecor_frame_interface CWaylandPlane::s_LibDecorFrameInterface =
{
.configure = LIBDECOR_USERDATA_TO_THIS( CWaylandPlane, LibDecor_Frame_Configure ),
.close = LIBDECOR_USERDATA_TO_THIS( CWaylandPlane, LibDecor_Frame_Close ),
.commit = LIBDECOR_USERDATA_TO_THIS( CWaylandPlane, LibDecor_Frame_Commit ),
.dismiss_popup = LIBDECOR_USERDATA_TO_THIS( CWaylandPlane, LibDecor_Frame_DismissPopup ),
};
const wp_presentation_feedback_listener CWaylandPlane::s_PresentationFeedbackListener =
{
.sync_output = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_PresentationFeedback_SyncOutput ),
.presented = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_PresentationFeedback_Presented ),
.discarded = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_PresentationFeedback_Discarded ),
};
const frog_color_managed_surface_listener CWaylandPlane::s_FrogColorManagedSurfaceListener =
{
.preferred_metadata = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_FrogColorManagedSurface_PreferredMetadata ),
};
const wp_color_management_surface_feedback_v1_listener CWaylandPlane::s_WPColorManagementSurfaceListener =
{
.preferred_changed = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_WPColorManagementSurfaceFeedback_PreferredChanged ),
};
const wp_image_description_info_v1_listener CWaylandPlane::s_ImageDescriptionInfoListener =
{
.done = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_WPImageDescriptionInfo_Done ),
.icc_file = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_WPImageDescriptionInfo_ICCFile ),
.primaries = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_WPImageDescriptionInfo_Primaries ),
.primaries_named = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_WPImageDescriptionInfo_PrimariesNamed ),
.tf_power = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_WPImageDescriptionInfo_TFPower ),
.tf_named = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_WPImageDescriptionInfo_TFNamed ),
.luminances = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_WPImageDescriptionInfo_Luminances ),
.target_primaries = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_WPImageDescriptionInfo_TargetPrimaries ),
.target_luminance = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_WPImageDescriptionInfo_TargetLuminance ),
.target_max_cll = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_WPImageDescriptionInfo_Target_MaxCLL ),
.target_max_fall = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_WPImageDescriptionInfo_Target_MaxFALL ),
};
const wp_fractional_scale_v1_listener CWaylandPlane::s_FractionalScaleListener =
{
.preferred_scale = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_FractionalScale_PreferredScale ),
};
enum WaylandModifierIndex
{
GAMESCOPE_WAYLAND_MOD_CTRL,
GAMESCOPE_WAYLAND_MOD_SHIFT,
GAMESCOPE_WAYLAND_MOD_ALT,
GAMESCOPE_WAYLAND_MOD_META, // Super
GAMESCOPE_WAYLAND_MOD_NUM,
GAMESCOPE_WAYLAND_MOD_CAPS,
GAMESCOPE_WAYLAND_MOD_COUNT,
};
constexpr const char *WaylandModifierToXkbModifierName( WaylandModifierIndex eIndex )
{
switch ( eIndex )
{
case GAMESCOPE_WAYLAND_MOD_CTRL:
return XKB_MOD_NAME_CTRL;
case GAMESCOPE_WAYLAND_MOD_SHIFT:
return XKB_MOD_NAME_SHIFT;
case GAMESCOPE_WAYLAND_MOD_ALT:
return XKB_MOD_NAME_ALT;
case GAMESCOPE_WAYLAND_MOD_META:
return XKB_MOD_NAME_LOGO;
case GAMESCOPE_WAYLAND_MOD_NUM:
return XKB_MOD_NAME_NUM;
case GAMESCOPE_WAYLAND_MOD_CAPS:
return XKB_MOD_NAME_CAPS;
default:
return "Unknown";
}
}
struct WaylandOutputInfo
{
int32_t nRefresh = 60;
int32_t nScale = 1;
};
class CWaylandConnector final : public CBaseBackendConnector, public INestedHints
{
public:
CWaylandConnector( CWaylandBackend *pBackend, uint64_t ulVirtualConnectorKey );
virtual ~CWaylandConnector();
bool UpdateEdid();
bool Init();
void SetFullscreen( bool bFullscreen ); // Thread safe, can be called from the input thread.
void UpdateFullscreenState();
bool HostCompositorIsCurrentlyVRR() const { return m_bHostCompositorIsCurrentlyVRR; }
void SetHostCompositorIsCurrentlyVRR( bool bActive ) { m_bHostCompositorIsCurrentlyVRR = bActive; }
bool CurrentDisplaySupportsVRR() const { return HostCompositorIsCurrentlyVRR(); }
CWaylandBackend *GetBackend() const { return m_pBackend; }
/////////////////////
// IBackendConnector
/////////////////////
virtual int Present( const FrameInfo_t *pFrameInfo, bool bAsync ) override;
virtual gamescope::GamescopeScreenType GetScreenType() const override;
virtual GamescopePanelOrientation GetCurrentOrientation() const override;
virtual bool SupportsHDR() const override;
virtual bool IsHDRActive() const override;
virtual const BackendConnectorHDRInfo &GetHDRInfo() const override;
virtual bool IsVRRActive() const override;
virtual std::span<const BackendMode> GetModes() const override;
virtual bool SupportsVRR() const override;
virtual std::span<const uint8_t> GetRawEDID() const override;
virtual std::span<const uint32_t> GetValidDynamicRefreshRates() const override;
virtual void GetNativeColorimetry(
bool bHDR10,
displaycolorimetry_t *displayColorimetry, EOTF *displayEOTF,
displaycolorimetry_t *outputEncodingColorimetry, EOTF *outputEncodingEOTF ) const override;
virtual const char *GetName() const override
{
return "Wayland";
}
virtual const char *GetMake() const override
{
return "Gamescope";
}
virtual const char *GetModel() const override
{
return "Virtual Display";
}
virtual INestedHints *GetNestedHints() override
{
return this;
}
///////////////////
// INestedHints
///////////////////
virtual void SetCursorImage( std::shared_ptr<INestedHints::CursorInfo> info ) override;
virtual void SetRelativeMouseMode( bool bRelative ) override;
virtual void SetVisible( bool bVisible ) override;
virtual void SetTitle( std::shared_ptr<std::string> szTitle ) override;
virtual void SetIcon( std::shared_ptr<std::vector<uint32_t>> uIconPixels ) override;
virtual void SetSelection( std::shared_ptr<std::string> szContents, GamescopeSelection eSelection ) override;
private:
friend CWaylandPlane;
BackendConnectorHDRInfo m_HDRInfo{};
displaycolorimetry_t m_DisplayColorimetry = displaycolorimetry_709;
std::vector<uint8_t> m_FakeEdid;
CWaylandBackend *m_pBackend = nullptr;
CWaylandPlane m_Planes[8];
bool m_bVisible = true;
std::atomic<bool> m_bDesiredFullscreenState = { false };
bool m_bHostCompositorIsCurrentlyVRR = false;
};
class CWaylandFb final : public CBaseBackendFb
{
public:
CWaylandFb( CWaylandBackend *pBackend, wl_buffer *pHostBuffer );
~CWaylandFb();
void OnCompositorAcquire();
void OnCompositorRelease();
wl_buffer *GetHostBuffer() const { return m_pHostBuffer; }
wlr_buffer *GetClientBuffer() const { return m_pClientBuffer; }
void Wayland_Buffer_Release( wl_buffer *pBuffer );
static const wl_buffer_listener s_BufferListener;
private:
CWaylandBackend *m_pBackend = nullptr;
wl_buffer *m_pHostBuffer = nullptr;
wlr_buffer *m_pClientBuffer = nullptr;
bool m_bCompositorAcquired = false;
};
const wl_buffer_listener CWaylandFb::s_BufferListener =
{
.release = WAYLAND_USERDATA_TO_THIS( CWaylandFb, Wayland_Buffer_Release ),
};
class CWaylandInputThread
{
public:
CWaylandInputThread();
~CWaylandInputThread();
bool Init( CWaylandBackend *pBackend );
void ThreadFunc();
// This is only shared_ptr because it works nicely
// with std::atomic, std::any and such and makes it very easy.
//
// It could be a std::unique_ptr if you added a mutex,
// but I didn't seem it worth it.
template <typename T>
std::shared_ptr<T> QueueLaunder( T* pObject );
void SetRelativePointer( bool bRelative );
private:
void HandleKey( uint32_t uKey, bool bPressed );
CWaylandBackend *m_pBackend = nullptr;
CWaiter<4> m_Waiter;
std::thread m_Thread;
std::atomic<bool> m_bInitted = { false };
uint32_t m_uPointerEnterSerial = 0;
bool m_bMouseEntered = false;
bool m_bKeyboardEntered = false;
wl_event_queue *m_pQueue = nullptr;
std::shared_ptr<wl_display> m_pDisplayWrapper;
wl_seat *m_pSeat = nullptr;
wl_keyboard *m_pKeyboard = nullptr;
wl_pointer *m_pPointer = nullptr;
wl_touch *m_pTouch = nullptr;
zwp_relative_pointer_manager_v1 *m_pRelativePointerManager = nullptr;
uint32_t m_uFakeTimestamp = 0;
xkb_context *m_pXkbContext = nullptr;
xkb_keymap *m_pXkbKeymap = nullptr;
uint32_t m_uKeyModifiers = 0;
uint32_t m_uModMask[ GAMESCOPE_WAYLAND_MOD_COUNT ];
double m_flScrollAccum[2] = { 0.0, 0.0 };
uint32_t m_uAxisSource = WL_POINTER_AXIS_SOURCE_WHEEL;
CWaylandPlane *m_pCurrentCursorPlane = nullptr;
std::optional<wl_fixed_t> m_ofPendingCursorX;
std::optional<wl_fixed_t> m_ofPendingCursorY;
std::atomic<std::shared_ptr<zwp_relative_pointer_v1>> m_pRelativePointer = nullptr;
std::unordered_set<uint32_t> m_uScancodesHeld;
void Wayland_Registry_Global( wl_registry *pRegistry, uint32_t uName, const char *pInterface, uint32_t uVersion );
static const wl_registry_listener s_RegistryListener;
void Wayland_Seat_Capabilities( wl_seat *pSeat, uint32_t uCapabilities );
void Wayland_Seat_Name( wl_seat *pSeat, const char *pName );
static const wl_seat_listener s_SeatListener;
void Wayland_Pointer_Enter( wl_pointer *pPointer, uint32_t uSerial, wl_surface *pSurface, wl_fixed_t fSurfaceX, wl_fixed_t fSurfaceY );
void Wayland_Pointer_Leave( wl_pointer *pPointer, uint32_t uSerial, wl_surface *pSurface );
void Wayland_Pointer_Motion( wl_pointer *pPointer, uint32_t uTime, wl_fixed_t fSurfaceX, wl_fixed_t fSurfaceY );
void Wayland_Pointer_Button( wl_pointer *pPointer, uint32_t uSerial, uint32_t uTime, uint32_t uButton, uint32_t uState );
void Wayland_Pointer_Axis( wl_pointer *pPointer, uint32_t uTime, uint32_t uAxis, wl_fixed_t fValue );
void Wayland_Pointer_Axis_Source( wl_pointer *pPointer, uint32_t uAxisSource );
void Wayland_Pointer_Axis_Stop( wl_pointer *pPointer, uint32_t uTime, uint32_t uAxis );
void Wayland_Pointer_Axis_Discrete( wl_pointer *pPointer, uint32_t uAxis, int32_t nDiscrete );
void Wayland_Pointer_Axis_Value120( wl_pointer *pPointer, uint32_t uAxis, int32_t nValue120 );
void Wayland_Pointer_Frame( wl_pointer *pPointer );
static const wl_pointer_listener s_PointerListener;
void Wayland_Keyboard_Keymap( wl_keyboard *pKeyboard, uint32_t uFormat, int32_t nFd, uint32_t uSize );
void Wayland_Keyboard_Enter( wl_keyboard *pKeyboard, uint32_t uSerial, wl_surface *pSurface, wl_array *pKeys );
void Wayland_Keyboard_Leave( wl_keyboard *pKeyboard, uint32_t uSerial, wl_surface *pSurface );
void Wayland_Keyboard_Key( wl_keyboard *pKeyboard, uint32_t uSerial, uint32_t uTime, uint32_t uKey, uint32_t uState );
void Wayland_Keyboard_Modifiers( wl_keyboard *pKeyboard, uint32_t uSerial, uint32_t uModsDepressed, uint32_t uModsLatched, uint32_t uModsLocked, uint32_t uGroup );
void Wayland_Keyboard_RepeatInfo( wl_keyboard *pKeyboard, int32_t nRate, int32_t nDelay );
static const wl_keyboard_listener s_KeyboardListener;
void Wayland_RelativePointer_RelativeMotion( zwp_relative_pointer_v1 *pRelativePointer, uint32_t uTimeHi, uint32_t uTimeLo, wl_fixed_t fDx, wl_fixed_t fDy, wl_fixed_t fDxUnaccel, wl_fixed_t fDyUnaccel );
static const zwp_relative_pointer_v1_listener s_RelativePointerListener;
};
const wl_registry_listener CWaylandInputThread::s_RegistryListener =
{
.global = WAYLAND_USERDATA_TO_THIS( CWaylandInputThread, Wayland_Registry_Global ),
.global_remove = WAYLAND_NULL(),
};
const wl_seat_listener CWaylandInputThread::s_SeatListener =
{
.capabilities = WAYLAND_USERDATA_TO_THIS( CWaylandInputThread, Wayland_Seat_Capabilities ),
.name = WAYLAND_USERDATA_TO_THIS( CWaylandInputThread, Wayland_Seat_Name ),
};
const wl_pointer_listener CWaylandInputThread::s_PointerListener =
{
.enter = WAYLAND_USERDATA_TO_THIS( CWaylandInputThread, Wayland_Pointer_Enter ),
.leave = WAYLAND_USERDATA_TO_THIS( CWaylandInputThread, Wayland_Pointer_Leave ),
.motion = WAYLAND_USERDATA_TO_THIS( CWaylandInputThread, Wayland_Pointer_Motion ),
.button = WAYLAND_USERDATA_TO_THIS( CWaylandInputThread, Wayland_Pointer_Button ),
.axis = WAYLAND_USERDATA_TO_THIS( CWaylandInputThread, Wayland_Pointer_Axis ),
.frame = WAYLAND_USERDATA_TO_THIS( CWaylandInputThread, Wayland_Pointer_Frame ),
.axis_source = WAYLAND_USERDATA_TO_THIS( CWaylandInputThread, Wayland_Pointer_Axis_Source ),
.axis_stop = WAYLAND_USERDATA_TO_THIS( CWaylandInputThread, Wayland_Pointer_Axis_Stop ),
.axis_discrete = WAYLAND_USERDATA_TO_THIS( CWaylandInputThread, Wayland_Pointer_Axis_Discrete ),
.axis_value120 = WAYLAND_USERDATA_TO_THIS( CWaylandInputThread, Wayland_Pointer_Axis_Value120 ),
};
const wl_keyboard_listener CWaylandInputThread::s_KeyboardListener =
{
.keymap = WAYLAND_USERDATA_TO_THIS( CWaylandInputThread, Wayland_Keyboard_Keymap ),
.enter = WAYLAND_USERDATA_TO_THIS( CWaylandInputThread, Wayland_Keyboard_Enter ),
.leave = WAYLAND_USERDATA_TO_THIS( CWaylandInputThread, Wayland_Keyboard_Leave ),
.key = WAYLAND_USERDATA_TO_THIS( CWaylandInputThread, Wayland_Keyboard_Key ),
.modifiers = WAYLAND_USERDATA_TO_THIS( CWaylandInputThread, Wayland_Keyboard_Modifiers ),
.repeat_info = WAYLAND_USERDATA_TO_THIS( CWaylandInputThread, Wayland_Keyboard_RepeatInfo ),
};
const zwp_relative_pointer_v1_listener CWaylandInputThread::s_RelativePointerListener =
{
.relative_motion = WAYLAND_USERDATA_TO_THIS( CWaylandInputThread, Wayland_RelativePointer_RelativeMotion ),
};
class CWaylandBackend : public CBaseBackend
{
public:
CWaylandBackend();
/////////////
// IBackend
/////////////
virtual bool Init() override;
virtual bool PostInit() override;
virtual std::span<const char *const> GetInstanceExtensions() const override;
virtual std::span<const char *const> GetDeviceExtensions( VkPhysicalDevice pVkPhysicalDevice ) const override;
virtual VkImageLayout GetPresentLayout() const override;
virtual void GetPreferredOutputFormat( uint32_t *pPrimaryPlaneFormat, uint32_t *pOverlayPlaneFormat ) const override;
virtual bool ValidPhysicalDevice( VkPhysicalDevice pVkPhysicalDevice ) const override;
virtual void DirtyState( bool bForce = false, bool bForceModeset = false ) override;
virtual bool PollState() override;
virtual std::shared_ptr<BackendBlob> CreateBackendBlob( const std::type_info &type, std::span<const uint8_t> data ) override;
virtual OwningRc<IBackendFb> ImportDmabufToBackend( wlr_buffer *pBuffer, wlr_dmabuf_attributes *pDmaBuf ) override;
virtual bool UsesModifiers() const override;
virtual std::span<const uint64_t> GetSupportedModifiers( uint32_t uDrmFormat ) const override;
virtual IBackendConnector *GetCurrentConnector() override;
virtual IBackendConnector *GetConnector( GamescopeScreenType eScreenType ) override;
virtual bool SupportsPlaneHardwareCursor() const override;
virtual bool SupportsTearing() const override;
virtual bool UsesVulkanSwapchain() const override;
virtual bool IsSessionBased() const override;
virtual bool SupportsExplicitSync() const override;
virtual bool IsVisible() const override;
virtual glm::uvec2 CursorSurfaceSize( glm::uvec2 uvecSize ) const override;
virtual void HackUpdatePatchedEdid() override;
virtual bool UsesVirtualConnectors() override;
virtual std::shared_ptr<IBackendConnector> CreateVirtualConnector( uint64_t ulVirtualConnectorKey ) override;
virtual void ToggleFullscreen() override;
protected:
virtual void OnBackendBlobDestroyed( BackendBlob *pBlob ) override;
wl_surface *CursorInfoToSurface( const std::shared_ptr<INestedHints::CursorInfo> &info );
bool SupportsColorManagement() const;
void SetCursorImage( std::shared_ptr<INestedHints::CursorInfo> info );
void SetRelativeMouseMode( wl_surface *pSurface, bool bRelative );
void UpdateCursor();
friend CWaylandConnector;
friend CWaylandPlane;
friend CWaylandInputThread;
friend CWaylandFb;
wl_display *GetDisplay() const { return m_pDisplay; }
wl_shm *GetShm() const { return m_pShm; }
wl_compositor *GetCompositor() const { return m_pCompositor; }
wp_single_pixel_buffer_manager_v1 *GetSinglePixelBufferManager() const { return m_pSinglePixelBufferManager; }
wl_subcompositor *GetSubcompositor() const { return m_pSubcompositor; }
zwp_linux_dmabuf_v1 *GetLinuxDmabuf() const { return m_pLinuxDmabuf; }
xdg_wm_base *GetXDGWMBase() const { return m_pXdgWmBase; }
wp_viewporter *GetViewporter() const { return m_pViewporter; }
wp_presentation *GetPresentation() const { return m_pPresentation; }
frog_color_management_factory_v1 *GetFrogColorManagementFactory() const { return m_pFrogColorMgmtFactory; }
wp_color_manager_v1 *GetWPColorManager() const { return m_pWPColorManager; }
wp_image_description_v1 *GetWPImageDescription( GamescopeAppTextureColorspace eColorspace ) const { return m_pWPImageDescriptions[ (uint32_t)eColorspace ]; }
wp_fractional_scale_manager_v1 *GetFractionalScaleManager() const { return m_pFractionalScaleManager; }
xdg_toplevel_icon_manager_v1 *GetToplevelIconManager() const { return m_pToplevelIconManager; }
libdecor *GetLibDecor() const { return m_pLibDecor; }
void UpdateFullscreenState();
WaylandOutputInfo *GetOutputInfo( wl_output *pOutput )
{
auto iter = m_pOutputs.find( pOutput );
if ( iter == m_pOutputs.end() )
return nullptr;
return &iter->second;
}
wl_region *GetFullRegion() const { return m_pFullRegion; }
CWaylandFb *GetBlackFb() const { return m_BlackFb.get(); }
void OnConnectorDestroyed( CWaylandConnector *pConnector )
{
m_pFocusConnector.compare_exchange_strong( pConnector, nullptr );
}
private:
void Wayland_Registry_Global( wl_registry *pRegistry, uint32_t uName, const char *pInterface, uint32_t uVersion );
static const wl_registry_listener s_RegistryListener;
void Wayland_Modifier( zwp_linux_dmabuf_v1 *pDmabuf, uint32_t uFormat, uint32_t uModifierHi, uint32_t uModifierLo );
void Wayland_Output_Geometry( wl_output *pOutput, int32_t nX, int32_t nY, int32_t nPhysicalWidth, int32_t nPhysicalHeight, int32_t nSubpixel, const char *pMake, const char *pModel, int32_t nTransform );
void Wayland_Output_Mode( wl_output *pOutput, uint32_t uFlags, int32_t nWidth, int32_t nHeight, int32_t nRefresh );
void Wayland_Output_Done( wl_output *pOutput );
void Wayland_Output_Scale( wl_output *pOutput, int32_t nFactor );
void Wayland_Output_Name( wl_output *pOutput, const char *pName );
void Wayland_Output_Description( wl_output *pOutput, const char *pDescription );
static const wl_output_listener s_OutputListener;
void Wayland_Seat_Capabilities( wl_seat *pSeat, uint32_t uCapabilities );
static const wl_seat_listener s_SeatListener;
void Wayland_Pointer_Enter( wl_pointer *pPointer, uint32_t uSerial, wl_surface *pSurface, wl_fixed_t fSurfaceX, wl_fixed_t fSurfaceY );
void Wayland_Pointer_Leave( wl_pointer *pPointer, uint32_t uSerial, wl_surface *pSurface );
static const wl_pointer_listener s_PointerListener;
void Wayland_Keyboard_Enter( wl_keyboard *pKeyboard, uint32_t uSerial, wl_surface *pSurface, wl_array *pKeys );
void Wayland_Keyboard_Leave( wl_keyboard *pKeyboard, uint32_t uSerial, wl_surface *pSurface );
static const wl_keyboard_listener s_KeyboardListener;
void Wayland_WPColorManager_SupportedIntent( wp_color_manager_v1 *pWPColorManager, uint32_t uRenderIntent );
void Wayland_WPColorManager_SupportedFeature( wp_color_manager_v1 *pWPColorManager, uint32_t uFeature );
void Wayland_WPColorManager_SupportedTFNamed( wp_color_manager_v1 *pWPColorManager, uint32_t uTF );
void Wayland_WPColorManager_SupportedPrimariesNamed( wp_color_manager_v1 *pWPColorManager, uint32_t uPrimaries );
void Wayland_WPColorManager_ColorManagerDone( wp_color_manager_v1 *pWPColorManager );
static const wp_color_manager_v1_listener s_WPColorManagerListener;
void Wayland_DataSource_Send( struct wl_data_source *pSource, const char *pMime, int nFd );
void Wayland_DataSource_Cancelled( struct wl_data_source *pSource );
static const wl_data_source_listener s_DataSourceListener;
void Wayland_PrimarySelectionSource_Send( struct zwp_primary_selection_source_v1 *pSource, const char *pMime, int nFd );
void Wayland_PrimarySelectionSource_Cancelled( struct zwp_primary_selection_source_v1 *pSource );
static const zwp_primary_selection_source_v1_listener s_PrimarySelectionSourceListener;
CWaylandInputThread m_InputThread;
wl_display *m_pDisplay = nullptr;
wl_shm *m_pShm = nullptr;
wl_compositor *m_pCompositor = nullptr;
wp_single_pixel_buffer_manager_v1 *m_pSinglePixelBufferManager = nullptr;
wl_subcompositor *m_pSubcompositor = nullptr;
zwp_linux_dmabuf_v1 *m_pLinuxDmabuf = nullptr;
xdg_wm_base *m_pXdgWmBase = nullptr;
wp_viewporter *m_pViewporter = nullptr;
wl_region *m_pFullRegion = nullptr;
Rc<CWaylandFb> m_BlackFb;
OwningRc<CWaylandFb> m_pOwnedBlackFb;
OwningRc<CVulkanTexture> m_pBlackTexture;
wp_presentation *m_pPresentation = nullptr;
frog_color_management_factory_v1 *m_pFrogColorMgmtFactory = nullptr;
wp_color_manager_v1 *m_pWPColorManager = nullptr;
wp_image_description_v1 *m_pWPImageDescriptions[ GamescopeAppTextureColorspace_Count ]{};
zwp_pointer_constraints_v1 *m_pPointerConstraints = nullptr;
zwp_relative_pointer_manager_v1 *m_pRelativePointerManager = nullptr;
wp_fractional_scale_manager_v1 *m_pFractionalScaleManager = nullptr;
xdg_toplevel_icon_manager_v1 *m_pToplevelIconManager = nullptr;
// TODO: Restructure and remove the need for this.
std::atomic<CWaylandConnector *> m_pFocusConnector;
wl_data_device_manager *m_pDataDeviceManager = nullptr;
wl_data_device *m_pDataDevice = nullptr;
std::shared_ptr<std::string> m_pClipboard = nullptr;
zwp_primary_selection_device_manager_v1 *m_pPrimarySelectionDeviceManager = nullptr;
zwp_primary_selection_device_v1 *m_pPrimarySelectionDevice = nullptr;
std::shared_ptr<std::string> m_pPrimarySelection = nullptr;
struct
{
std::vector<wp_color_manager_v1_primaries> ePrimaries;
std::vector<wp_color_manager_v1_transfer_function> eTransferFunctions;
std::vector<wp_color_manager_v1_render_intent> eRenderIntents;
std::vector<wp_color_manager_v1_feature> eFeatures;
bool bSupportsGamescopeColorManagement = false; // Has everything we want and need?
} m_WPColorManagerFeatures;
std::unordered_map<wl_output *, WaylandOutputInfo> m_pOutputs;
libdecor *m_pLibDecor = nullptr;
wl_seat *m_pSeat = nullptr;
wl_keyboard *m_pKeyboard = nullptr;
wl_pointer *m_pPointer = nullptr;
wl_touch *m_pTouch = nullptr;
zwp_locked_pointer_v1 *m_pLockedPointer = nullptr;
wl_surface *m_pLockedSurface = nullptr;
zwp_relative_pointer_v1 *m_pRelativePointer = nullptr;
bool m_bCanUseModifiers = false;
std::unordered_map<uint32_t, std::vector<uint64_t>> m_FormatModifiers;
std::unordered_map<uint32_t, wl_buffer *> m_ImportedFbs;
uint32_t m_uPointerEnterSerial = 0;
bool m_bMouseEntered = false;
uint32_t m_uKeyboardEnterSerial = 0;
bool m_bKeyboardEntered = false;
std::shared_ptr<INestedHints::CursorInfo> m_pCursorInfo;
wl_surface *m_pCursorSurface = nullptr;
std::shared_ptr<INestedHints::CursorInfo> m_pDefaultCursorInfo;
wl_surface *m_pDefaultCursorSurface = nullptr;
};
const wl_registry_listener CWaylandBackend::s_RegistryListener =
{
.global = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_Registry_Global ),
.global_remove = WAYLAND_NULL(),
};
const wl_output_listener CWaylandBackend::s_OutputListener =
{
.geometry = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_Output_Geometry ),
.mode = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_Output_Mode ),
.done = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_Output_Done ),
.scale = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_Output_Scale ),
.name = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_Output_Name ),
.description = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_Output_Description ),
};
const wl_seat_listener CWaylandBackend::s_SeatListener =
{
.capabilities = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_Seat_Capabilities ),
.name = WAYLAND_NULL(),
};
const wl_pointer_listener CWaylandBackend::s_PointerListener =
{
.enter = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_Pointer_Enter ),
.leave = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_Pointer_Leave ),
.motion = WAYLAND_NULL(),
.button = WAYLAND_NULL(),
.axis = WAYLAND_NULL(),
.frame = WAYLAND_NULL(),
.axis_source = WAYLAND_NULL(),
.axis_stop = WAYLAND_NULL(),
.axis_discrete = WAYLAND_NULL(),
.axis_value120 = WAYLAND_NULL(),
};
const wl_keyboard_listener CWaylandBackend::s_KeyboardListener =
{
.keymap = WAYLAND_NULL(),
.enter = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_Keyboard_Enter ),
.leave = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_Keyboard_Leave ),
.key = WAYLAND_NULL(),
.modifiers = WAYLAND_NULL(),
.repeat_info = WAYLAND_NULL(),
};
const wp_color_manager_v1_listener CWaylandBackend::s_WPColorManagerListener
{
.supported_intent = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_WPColorManager_SupportedIntent ),
.supported_feature = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_WPColorManager_SupportedFeature ),
.supported_tf_named = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_WPColorManager_SupportedTFNamed ),
.supported_primaries_named = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_WPColorManager_SupportedPrimariesNamed ),
.done = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_WPColorManager_ColorManagerDone ),
};
const wl_data_source_listener CWaylandBackend::s_DataSourceListener =
{
.target = WAYLAND_NULL(),
.send = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_DataSource_Send ),
.cancelled = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_DataSource_Cancelled ),
.dnd_drop_performed = WAYLAND_NULL(),
.dnd_finished = WAYLAND_NULL(),
.action = WAYLAND_NULL(),
};
const zwp_primary_selection_source_v1_listener CWaylandBackend::s_PrimarySelectionSourceListener =
{
.send = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_PrimarySelectionSource_Send ),
.cancelled = WAYLAND_USERDATA_TO_THIS( CWaylandBackend, Wayland_PrimarySelectionSource_Cancelled ),
};
//////////////////
// CWaylandFb
//////////////////
CWaylandFb::CWaylandFb( CWaylandBackend *pBackend, wl_buffer *pHostBuffer )
: CBaseBackendFb()
, m_pBackend { pBackend }
, m_pHostBuffer { pHostBuffer }
{
wl_buffer_add_listener( pHostBuffer, &s_BufferListener, this );
}
CWaylandFb::~CWaylandFb()
{
// I own the pHostBuffer.
wl_buffer_destroy( m_pHostBuffer );
m_pHostBuffer = nullptr;
}
void CWaylandFb::OnCompositorAcquire()
{
// If the compositor has acquired us, track that
// and increment the ref count.
if ( !m_bCompositorAcquired )
{
m_bCompositorAcquired = true;
IncRef();
}
}
void CWaylandFb::OnCompositorRelease()
{
// Compositor has released us, decrement rc.
//assert( m_bCompositorAcquired );
if ( m_bCompositorAcquired )
{
m_bCompositorAcquired = false;
DecRef();
}
else
{
xdg_log.errorf( "Compositor released us but we were not acquired. Oh no." );
}
}
void CWaylandFb::Wayland_Buffer_Release( wl_buffer *pBuffer )
{
assert( m_pHostBuffer );
assert( m_pHostBuffer == pBuffer );
xdg_log.debugf( "buffer_release: %p", pBuffer );
OnCompositorRelease();
}
//////////////////
// CWaylandConnector
//////////////////
CWaylandConnector::CWaylandConnector( CWaylandBackend *pBackend, uint64_t ulVirtualConnectorKey )
: CBaseBackendConnector{ ulVirtualConnectorKey }
, m_pBackend( pBackend )
, m_Planes{ this, this, this, this, this, this, this, this }
{
m_HDRInfo.bAlwaysPatchEdid = true;
}
CWaylandConnector::~CWaylandConnector()
{
m_pBackend->OnConnectorDestroyed( this );
}
bool CWaylandConnector::UpdateEdid()
{
m_FakeEdid = GenerateSimpleEdid( g_nNestedWidth, g_nNestedHeight );
return true;
}
bool CWaylandConnector::Init()
{
for ( uint32_t i = 0; i < 8; i++ )
{
bool bSuccess = m_Planes[i].Init( i == 0 ? nullptr : &m_Planes[0], i == 0 ? nullptr : &m_Planes[ i - 1 ] );
if ( !bSuccess )
return false;
}
if ( g_bFullscreen )
{
m_bDesiredFullscreenState = true;
g_bFullscreen = false;
UpdateFullscreenState();
}
UpdateEdid();
m_pBackend->HackUpdatePatchedEdid();
if ( g_bForceRelativeMouse )
this->SetRelativeMouseMode( true );
return true;
}
void CWaylandConnector::SetFullscreen( bool bFullscreen )
{
m_bDesiredFullscreenState = bFullscreen;
}
void CWaylandConnector::UpdateFullscreenState()
{
if ( !m_bVisible )
g_bFullscreen = false;
if ( m_bDesiredFullscreenState != g_bFullscreen && m_bVisible )
{
if ( m_bDesiredFullscreenState )
libdecor_frame_set_fullscreen( m_Planes[0].GetFrame(), nullptr );
else
libdecor_frame_unset_fullscreen( m_Planes[0].GetFrame() );
g_bFullscreen = m_bDesiredFullscreenState;
}
}
int CWaylandConnector::Present( const FrameInfo_t *pFrameInfo, bool bAsync )
{
UpdateFullscreenState();
bool bNeedsFullComposite = false;
if ( !m_bVisible )
{
uint32_t uCurrentPlane = 0;
for ( int i = 0; i < 8 && uCurrentPlane < 8; i++ )
m_Planes[uCurrentPlane++].Present( nullptr );
}
else