forked from ValveSoftware/steamos-compositor
-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathSDLBackend.cpp
More file actions
1009 lines (858 loc) · 27.2 KB
/
SDLBackend.cpp
File metadata and controls
1009 lines (858 loc) · 27.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
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
// For the nested case, reads input from the SDL window and send to wayland
#include <X11/Xlib.h>
#include <thread>
#include <mutex>
#include <string>
#include <optional>
#include <linux/input-event-codes.h>
#include <signal.h>
#include "SDL_clipboard.h"
#include "SDL_events.h"
#include "gamescope_shared.h"
#include "main.hpp"
#include "wlserver.hpp"
#include <SDL.h>
#include <SDL_vulkan.h>
#include "rendervulkan.hpp"
#include "steamcompmgr.hpp"
#include "Utils/Defer.h"
#include "refresh_rate.h"
#include "sdlscancodetable.hpp"
static int g_nOldNestedRefresh = 0;
static bool g_bWindowFocused = true;
static int g_nOutputWidthPts = 0;
static int g_nOutputHeightPts = 0;
extern bool g_bForceHDR10OutputDebug;
extern bool steamMode;
extern bool g_bFirstFrame;
extern int g_nPreferredOutputWidth;
extern int g_nPreferredOutputHeight;
namespace gamescope
{
enum class SDLInitState
{
SDLInit_Waiting,
SDLInit_Success,
SDLInit_Failure,
};
enum SDLCustomEvents
{
GAMESCOPE_SDL_EVENT_TITLE,
GAMESCOPE_SDL_EVENT_ICON,
GAMESCOPE_SDL_EVENT_VISIBLE,
GAMESCOPE_SDL_EVENT_GRAB,
GAMESCOPE_SDL_EVENT_CURSOR,
GAMESCOPE_SDL_EVENT_COUNT,
};
class CSDLBackend;
class CSDLConnector final : public CBaseBackendConnector, public INestedHints
{
public:
CSDLConnector( CSDLBackend *pBackend );
virtual bool Init();
virtual ~CSDLConnector();
/////////////////////
// IBackendConnector
/////////////////////
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 "SDLWindow";
}
virtual const char *GetMake() const override
{
return "Gamescope";
}
virtual const char *GetModel() const override
{
return "Virtual Display";
}
virtual INestedHints *GetNestedHints() override
{
return this;
}
virtual int Present( const FrameInfo_t *pFrameInfo, bool bAsync ) override;
///////////////////
// 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;
//--
SDL_Window *GetSDLWindow() const { return m_pWindow; }
VkSurfaceKHR GetVulkanSurface() const { return m_pVkSurface; }
private:
CSDLBackend *m_pBackend = nullptr;
SDL_Window *m_pWindow = nullptr;
VkSurfaceKHR m_pVkSurface = VK_NULL_HANDLE;
BackendConnectorHDRInfo m_HDRInfo{};
};
class CSDLBackend : public CBaseBackend
{
public:
CSDLBackend();
/////////////
// 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_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 IsPaused() const override;
virtual bool IsVisible() const override;
virtual glm::uvec2 CursorSurfaceSize( glm::uvec2 uvecSize ) const override;
////////////////////////
// INestedHints Compat
///////////////////////
void SetCursorImage( std::shared_ptr<INestedHints::CursorInfo> info );
void SetRelativeMouseMode( bool bRelative );
void SetVisible( bool bVisible );
void SetTitle( std::shared_ptr<std::string> szTitle );
void SetIcon( std::shared_ptr<std::vector<uint32_t>> uIconPixels );
void SetSelection( std::shared_ptr<std::string> szContents, GamescopeSelection eSelection );
protected:
virtual void OnBackendBlobDestroyed( BackendBlob *pBlob ) override;
private:
void SDLThreadFunc();
uint32_t GetUserEventIndex( SDLCustomEvents eEvent ) const;
void PushUserEvent( SDLCustomEvents eEvent );
bool m_bShown = false;
CSDLConnector m_Connector; // Window.
uint32_t m_uUserEventIdBase = 0u;
std::vector<const char *> m_pszInstanceExtensions;
std::thread m_SDLThread;
std::atomic<SDLInitState> m_eSDLInit = { SDLInitState::SDLInit_Waiting };
std::atomic<bool> m_bApplicationGrabbed = { false };
std::atomic<bool> m_bApplicationVisible = { false };
std::atomic<std::shared_ptr<INestedHints::CursorInfo>> m_pApplicationCursor;
std::atomic<std::shared_ptr<std::string>> m_pApplicationTitle;
std::atomic<std::shared_ptr<std::vector<uint32_t>>> m_pApplicationIcon;
SDL_Surface *m_pIconSurface = nullptr;
SDL_Surface *m_pCursorSurface = nullptr;
SDL_Cursor *m_pCursor = nullptr;
};
//////////////////
// CSDLConnector
//////////////////
CSDLConnector::CSDLConnector( CSDLBackend *pBackend )
: m_pBackend{ pBackend }
{
}
CSDLConnector::~CSDLConnector()
{
if ( m_pWindow )
SDL_DestroyWindow( m_pWindow );
}
bool CSDLConnector::Init()
{
g_nOutputWidth = g_nPreferredOutputWidth;
g_nOutputHeight = g_nPreferredOutputHeight;
g_nOutputRefresh = g_nNestedRefresh;
if ( g_nOutputHeight == 0 )
{
if ( g_nOutputWidth != 0 )
{
fprintf( stderr, "Cannot specify -W without -H\n" );
return false;
}
g_nOutputHeight = 720;
}
if ( g_nOutputWidth == 0 )
g_nOutputWidth = g_nOutputHeight * 16 / 9;
if ( g_nOutputRefresh == 0 )
g_nOutputRefresh = gamescope::ConvertHztomHz( 60 );
uint32_t uSDLWindowFlags = SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_ALLOW_HIGHDPI;
if ( g_bBorderlessOutputWindow == true )
uSDLWindowFlags |= SDL_WINDOW_BORDERLESS;
if ( g_bFullscreen == true )
uSDLWindowFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
if ( g_bGrabbed == true )
uSDLWindowFlags |= SDL_WINDOW_KEYBOARD_GRABBED;
m_pWindow = SDL_CreateWindow(
"gamescope",
SDL_WINDOWPOS_UNDEFINED_DISPLAY( g_nNestedDisplayIndex ),
SDL_WINDOWPOS_UNDEFINED_DISPLAY( g_nNestedDisplayIndex ),
g_nOutputWidth,
g_nOutputHeight,
uSDLWindowFlags );
if ( m_pWindow == nullptr )
return false;
if ( !SDL_Vulkan_CreateSurface( m_pWindow, vulkan_get_instance(), &m_pVkSurface ) )
{
fprintf(stderr, "SDL_Vulkan_CreateSurface failed: %s", SDL_GetError () );
return false;
}
return true;
}
GamescopeScreenType CSDLConnector::GetScreenType() const
{
return gamescope::GAMESCOPE_SCREEN_TYPE_INTERNAL;
}
GamescopePanelOrientation CSDLConnector::GetCurrentOrientation() const
{
return GAMESCOPE_PANEL_ORIENTATION_0;
}
bool CSDLConnector::SupportsHDR() const
{
return GetHDRInfo().IsHDR10();
}
bool CSDLConnector::IsHDRActive() const
{
// XXX: blah
return false;
}
const BackendConnectorHDRInfo &CSDLConnector::GetHDRInfo() const
{
return m_HDRInfo;
}
bool CSDLConnector::IsVRRActive() const
{
return false;
}
std::span<const BackendMode> CSDLConnector::GetModes() const
{
return std::span<const BackendMode>{};
}
bool CSDLConnector::SupportsVRR() const
{
return false;
}
std::span<const uint8_t> CSDLConnector::GetRawEDID() const
{
return std::span<const uint8_t>{};
}
std::span<const uint32_t> CSDLConnector::GetValidDynamicRefreshRates() const
{
return std::span<const uint32_t>{};
}
void CSDLConnector::GetNativeColorimetry(
bool bHDR10,
displaycolorimetry_t *displayColorimetry, EOTF *displayEOTF,
displaycolorimetry_t *outputEncodingColorimetry, EOTF *outputEncodingEOTF ) const
{
if ( g_bForceHDR10OutputDebug )
{
*displayColorimetry = displaycolorimetry_2020;
*displayEOTF = EOTF_PQ;
*outputEncodingColorimetry = displaycolorimetry_2020;
*outputEncodingEOTF = EOTF_PQ;
}
else
{
*displayColorimetry = displaycolorimetry_709;
*displayEOTF = EOTF_Gamma22;
*outputEncodingColorimetry = displaycolorimetry_709;
*outputEncodingEOTF = EOTF_Gamma22;
}
}
int CSDLConnector::Present( const FrameInfo_t *pFrameInfo, bool bAsync )
{
// TODO: Resolve const crap
std::optional oCompositeResult = vulkan_composite( (FrameInfo_t *)pFrameInfo, nullptr, false );
if ( !oCompositeResult )
return -EINVAL;
vulkan_present_to_window();
// TODO: Hook up PresentationFeedback.
// Wait for the composite result on our side *after* we
// commit the buffer to the compositor to avoid a bubble.
vulkan_wait( *oCompositeResult, true );
GetVBlankTimer().UpdateWasCompositing( true );
GetVBlankTimer().UpdateLastDrawTime( get_time_in_nanos() - g_SteamCompMgrVBlankTime.ulWakeupTime );
return 0;
}
void CSDLConnector::SetCursorImage( std::shared_ptr<INestedHints::CursorInfo> info )
{
m_pBackend->SetCursorImage( std::move( info ) );
}
void CSDLConnector::SetRelativeMouseMode( bool bRelative )
{
m_pBackend->SetRelativeMouseMode( bRelative );
}
void CSDLConnector::SetVisible( bool bVisible )
{
m_pBackend->SetVisible( bVisible );
}
void CSDLConnector::SetTitle( std::shared_ptr<std::string> szTitle )
{
m_pBackend->SetTitle( std::move( szTitle ) );
}
void CSDLConnector::SetIcon( std::shared_ptr<std::vector<uint32_t>> uIconPixels )
{
m_pBackend->SetIcon( std::move( uIconPixels ) );
}
void CSDLConnector::SetSelection( std::shared_ptr<std::string> szContents, GamescopeSelection eSelection )
{
if (eSelection == GAMESCOPE_SELECTION_CLIPBOARD)
SDL_SetClipboardText(szContents->c_str());
else if (eSelection == GAMESCOPE_SELECTION_PRIMARY)
SDL_SetPrimarySelectionText(szContents->c_str());
}
////////////////
// CSDLBackend
////////////////
CSDLBackend::CSDLBackend()
: m_Connector{ this }
, m_SDLThread{ [this](){ this->SDLThreadFunc(); } }
{
}
bool CSDLBackend::Init()
{
m_eSDLInit.wait( SDLInitState::SDLInit_Waiting );
return m_eSDLInit == SDLInitState::SDLInit_Success;
}
bool CSDLBackend::PostInit()
{
return true;
}
std::span<const char *const> CSDLBackend::GetInstanceExtensions() const
{
return std::span<const char *const>{ m_pszInstanceExtensions.begin(), m_pszInstanceExtensions.end() };
}
std::span<const char *const> CSDLBackend::GetDeviceExtensions( VkPhysicalDevice pVkPhysicalDevice ) const
{
return std::span<const char *const>{};
}
VkImageLayout CSDLBackend::GetPresentLayout() const
{
return VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
}
void CSDLBackend::GetPreferredOutputFormat( uint32_t *pPrimaryPlaneFormat, uint32_t *pOverlayPlaneFormat ) const
{
*pPrimaryPlaneFormat = VulkanFormatToDRM( VK_FORMAT_A2B10G10R10_UNORM_PACK32 );
*pOverlayPlaneFormat = VulkanFormatToDRM( VK_FORMAT_B8G8R8A8_UNORM );
}
bool CSDLBackend::ValidPhysicalDevice( VkPhysicalDevice pVkPhysicalDevice ) const
{
return true;
}
void CSDLBackend::DirtyState( bool bForce, bool bForceModeset )
{
}
bool CSDLBackend::PollState()
{
return false;
}
std::shared_ptr<BackendBlob> CSDLBackend::CreateBackendBlob( const std::type_info &type, std::span<const uint8_t> data )
{
return std::make_shared<BackendBlob>( data );
}
OwningRc<IBackendFb> CSDLBackend::ImportDmabufToBackend( wlr_dmabuf_attributes *pDmaBuf )
{
return new CBaseBackendFb();
}
bool CSDLBackend::UsesModifiers() const
{
return false;
}
std::span<const uint64_t> CSDLBackend::GetSupportedModifiers( uint32_t uDrmFormat ) const
{
return std::span<const uint64_t>{};
}
IBackendConnector *CSDLBackend::GetCurrentConnector()
{
return &m_Connector;
}
IBackendConnector *CSDLBackend::GetConnector( GamescopeScreenType eScreenType )
{
if ( eScreenType == GAMESCOPE_SCREEN_TYPE_INTERNAL )
return &m_Connector;
return nullptr;
}
bool CSDLBackend::SupportsPlaneHardwareCursor() const
{
// We use the nested hints cursor stuff.
// Not our own plane.
return false;
}
bool CSDLBackend::SupportsTearing() const
{
return false;
}
bool CSDLBackend::UsesVulkanSwapchain() const
{
return true;
}
bool CSDLBackend::IsSessionBased() const
{
return false;
}
bool CSDLBackend::SupportsExplicitSync() const
{
// We use a Vulkan swapchain, so yes.
return true;
}
bool CSDLBackend::IsPaused() const
{
return false;
}
bool CSDLBackend::IsVisible() const
{
return true;
}
glm::uvec2 CSDLBackend::CursorSurfaceSize( glm::uvec2 uvecSize ) const
{
return uvecSize;
}
///////////////////
// INestedHints
///////////////////
void CSDLBackend::SetCursorImage( std::shared_ptr<INestedHints::CursorInfo> info )
{
m_pApplicationCursor = info;
PushUserEvent( GAMESCOPE_SDL_EVENT_CURSOR );
}
void CSDLBackend::SetRelativeMouseMode( bool bRelative )
{
m_bApplicationGrabbed = bRelative;
PushUserEvent( GAMESCOPE_SDL_EVENT_GRAB );
}
void CSDLBackend::SetVisible( bool bVisible )
{
m_bApplicationVisible = bVisible;
PushUserEvent( GAMESCOPE_SDL_EVENT_VISIBLE );
}
void CSDLBackend::SetTitle( std::shared_ptr<std::string> szTitle )
{
m_pApplicationTitle = szTitle;
PushUserEvent( GAMESCOPE_SDL_EVENT_TITLE );
}
void CSDLBackend::SetIcon( std::shared_ptr<std::vector<uint32_t>> uIconPixels )
{
m_pApplicationIcon = uIconPixels;
PushUserEvent( GAMESCOPE_SDL_EVENT_ICON );
}
void CSDLBackend::SetSelection( std::shared_ptr<std::string> szContents, GamescopeSelection eSelection )
{
if (eSelection == GAMESCOPE_SELECTION_CLIPBOARD)
SDL_SetClipboardText(szContents->c_str());
else if (eSelection == GAMESCOPE_SELECTION_PRIMARY)
SDL_SetPrimarySelectionText(szContents->c_str());
}
void CSDLBackend::OnBackendBlobDestroyed( BackendBlob *pBlob )
{
// Do nothing.
}
void CSDLBackend::SDLThreadFunc()
{
pthread_setname_np( pthread_self(), "gamescope-sdl" );
m_uUserEventIdBase = SDL_RegisterEvents( GAMESCOPE_SDL_EVENT_COUNT );
SDL_SetHint( SDL_HINT_APP_NAME, "Gamescope" );
SDL_SetHint( SDL_HINT_VIDEO_ALLOW_SCREENSAVER, "1" );
if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_EVENTS ) != 0 )
{
m_eSDLInit = SDLInitState::SDLInit_Failure;
m_eSDLInit.notify_all();
return;
}
if ( SDL_Vulkan_LoadLibrary( nullptr ) != 0 )
{
fprintf(stderr, "SDL_Vulkan_LoadLibrary failed: %s\n", SDL_GetError());
m_eSDLInit = SDLInitState::SDLInit_Failure;
m_eSDLInit.notify_all();
return;
}
unsigned int uExtCount = 0;
SDL_Vulkan_GetInstanceExtensions( nullptr, &uExtCount, nullptr );
m_pszInstanceExtensions.resize( uExtCount );
SDL_Vulkan_GetInstanceExtensions( nullptr, &uExtCount, m_pszInstanceExtensions.data() );
if ( !m_Connector.Init() )
{
m_eSDLInit = SDLInitState::SDLInit_Failure;
m_eSDLInit.notify_all();
return;
}
if ( !vulkan_init( vulkan_get_instance(), m_Connector.GetVulkanSurface() ) )
{
m_eSDLInit = SDLInitState::SDLInit_Failure;
m_eSDLInit.notify_all();
return;
}
if ( !wlsession_init() )
{
fprintf( stderr, "Failed to initialize Wayland session\n" );
m_eSDLInit = SDLInitState::SDLInit_Failure;
m_eSDLInit.notify_all();
return;
}
// Update g_nOutputWidthPts.
{
int width, height;
SDL_GetWindowSize( m_Connector.GetSDLWindow(), &width, &height );
g_nOutputWidthPts = width;
g_nOutputHeightPts = height;
#if SDL_VERSION_ATLEAST(2, 26, 0)
SDL_GetWindowSizeInPixels( m_Connector.GetSDLWindow(), &width, &height );
#endif
g_nOutputWidth = width;
g_nOutputHeight = height;
}
if ( g_bForceRelativeMouse )
{
SDL_SetRelativeMouseMode( SDL_TRUE );
m_bApplicationGrabbed = true;
}
SDL_SetHint( SDL_HINT_TOUCH_MOUSE_EVENTS, "0" );
g_nOldNestedRefresh = g_nNestedRefresh;
m_eSDLInit = SDLInitState::SDLInit_Success;
m_eSDLInit.notify_all();
static uint32_t fake_timestamp = 0;
wlserver.bWaylandServerRunning.wait( false );
SDL_Event event;
while( SDL_WaitEvent( &event ) )
{
fake_timestamp++;
switch( event.type )
{
case SDL_CLIPBOARDUPDATE:
{
char *pClipBoard = SDL_GetClipboardText();
char *pPrimarySelection = SDL_GetPrimarySelectionText();
gamescope_set_selection(pClipBoard, GAMESCOPE_SELECTION_CLIPBOARD);
gamescope_set_selection(pPrimarySelection, GAMESCOPE_SELECTION_PRIMARY);
SDL_free(pClipBoard);
SDL_free(pPrimarySelection);
}
break;
case SDL_MOUSEMOTION:
{
if ( m_bApplicationGrabbed )
{
if ( g_bWindowFocused )
{
wlserver_lock();
wlserver_mousemotion( event.motion.xrel, event.motion.yrel, fake_timestamp );
wlserver_unlock();
}
}
else
{
wlserver_lock();
wlserver_touchmotion(
event.motion.x / float(g_nOutputWidthPts),
event.motion.y / float(g_nOutputHeightPts),
0,
fake_timestamp );
wlserver_unlock();
}
}
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
{
wlserver_lock();
wlserver_mousebutton( SDLButtonToLinuxButton( event.button.button ),
event.button.state == SDL_PRESSED,
fake_timestamp );
wlserver_unlock();
}
break;
case SDL_MOUSEWHEEL:
{
wlserver_lock();
wlserver_mousewheel( -event.wheel.x, -event.wheel.y, fake_timestamp );
wlserver_unlock();
}
break;
case SDL_FINGERMOTION:
{
wlserver_lock();
wlserver_touchmotion( event.tfinger.x, event.tfinger.y, event.tfinger.fingerId, fake_timestamp );
wlserver_unlock();
}
break;
case SDL_FINGERDOWN:
{
wlserver_lock();
wlserver_touchdown( event.tfinger.x, event.tfinger.y, event.tfinger.fingerId, fake_timestamp );
wlserver_unlock();
}
break;
case SDL_FINGERUP:
{
wlserver_lock();
wlserver_touchup( event.tfinger.fingerId, fake_timestamp );
wlserver_unlock();
}
break;
case SDL_KEYDOWN:
{
// If this keydown event is super + one of the shortcut keys, consume the keydown event, since the corresponding keyup
// event will be consumed by the next case statement when the user releases the key
if ( event.key.keysym.mod & KMOD_LGUI )
{
uint32_t key = SDLScancodeToLinuxKey( event.key.keysym.scancode );
const uint32_t shortcutKeys[] = {KEY_F, KEY_N, KEY_B, KEY_U, KEY_Y, KEY_I, KEY_O, KEY_S, KEY_G};
const bool isShortcutKey = std::find(std::begin(shortcutKeys), std::end(shortcutKeys), key) != std::end(shortcutKeys);
if ( isShortcutKey )
{
break;
}
// Temporary grab keyboard while Super is pressed
if (!g_bGrabbed && !g_bShortcutGrabbed && key == KEY_LEFTMETA) {
g_bShortcutGrabbed = true;
SDL_SetWindowKeyboardGrab(m_Connector.GetSDLWindow(), g_bShortcutGrabbed);
}
}
}
[[fallthrough]];
case SDL_KEYUP:
{
uint32_t key = SDLScancodeToLinuxKey( event.key.keysym.scancode );
if ( event.type == SDL_KEYUP && ( (event.key.keysym.mod & KMOD_LGUI) != 0 || g_bShortcutGrabbed ) )
{
bool handled = true;
switch ( key )
{
case KEY_F:
g_bFullscreen = !g_bFullscreen;
SDL_SetWindowFullscreen( m_Connector.GetSDLWindow(), g_bFullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0 );
break;
case KEY_N:
g_wantedUpscaleFilter = GamescopeUpscaleFilter::PIXEL;
break;
case KEY_B:
g_wantedUpscaleFilter = GamescopeUpscaleFilter::LINEAR;
break;
case KEY_U:
g_wantedUpscaleFilter = (g_wantedUpscaleFilter == GamescopeUpscaleFilter::FSR) ?
GamescopeUpscaleFilter::LINEAR : GamescopeUpscaleFilter::FSR;
break;
case KEY_Y:
g_wantedUpscaleFilter = (g_wantedUpscaleFilter == GamescopeUpscaleFilter::NIS) ?
GamescopeUpscaleFilter::LINEAR : GamescopeUpscaleFilter::NIS;
break;
case KEY_I:
g_upscaleFilterSharpness = std::min(20, g_upscaleFilterSharpness + 1);
break;
case KEY_O:
g_upscaleFilterSharpness = std::max(0, g_upscaleFilterSharpness - 1);
break;
case KEY_S:
gamescope::CScreenshotManager::Get().TakeScreenshot( true );
break;
case KEY_G: {
// Reset shortcut grab and allow normal grab w/ title change
if (g_bShortcutGrabbed) {
g_bShortcutGrabbed = false;
}
g_bGrabbed = !g_bGrabbed;
SDL_SetWindowKeyboardGrab( m_Connector.GetSDLWindow(), g_bGrabbed ? SDL_TRUE : SDL_FALSE );
SDL_Event event;
event.type = GetUserEventIndex( GAMESCOPE_SDL_EVENT_TITLE );
SDL_PushEvent( &event );
} break;
case KEY_LEFTMETA: {
// Release shortcut grab
if (g_bShortcutGrabbed) {
g_bShortcutGrabbed = false;
SDL_SetWindowKeyboardGrab(m_Connector.GetSDLWindow(), g_bShortcutGrabbed);
}
} break;
default:
handled = false;
}
if ( handled )
{
break;
}
}
// On Wayland, clients handle key repetition
if ( event.key.repeat )
break;
wlserver_lock();
wlserver_key( key, event.type == SDL_KEYDOWN, fake_timestamp );
wlserver_unlock();
}
break;
case SDL_WINDOWEVENT:
{
switch( event.window.event )
{
case SDL_WINDOWEVENT_CLOSE:
raise( SIGTERM );
break;
default:
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
int width, height;
SDL_GetWindowSize( m_Connector.GetSDLWindow(), &width, &height );
g_nOutputWidthPts = width;
g_nOutputHeightPts = height;
#if SDL_VERSION_ATLEAST(2, 26, 0)
SDL_GetWindowSizeInPixels( m_Connector.GetSDLWindow(), &width, &height );
#endif
g_nOutputWidth = width;
g_nOutputHeight = height;
[[fallthrough]];
case SDL_WINDOWEVENT_MOVED:
case SDL_WINDOWEVENT_SHOWN:
{
int display_index = 0;
SDL_DisplayMode mode = { SDL_PIXELFORMAT_UNKNOWN, 0, 0, 0, 0 };
display_index = SDL_GetWindowDisplayIndex( m_Connector.GetSDLWindow() );
if ( SDL_GetDesktopDisplayMode( display_index, &mode ) == 0 )
{
g_nOutputRefresh = ConvertHztomHz( mode.refresh_rate );
}
}
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
g_nNestedRefresh = g_nNestedUnfocusedRefresh;
g_bWindowFocused = false;
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
g_nNestedRefresh = g_nOldNestedRefresh;
g_bWindowFocused = true;
break;
case SDL_WINDOWEVENT_EXPOSED:
force_repaint();
break;
}
}
break;
default:
{
if ( event.type == GetUserEventIndex( GAMESCOPE_SDL_EVENT_VISIBLE ) )
{
bool bVisible = m_bApplicationVisible;
// If we are Steam Mode in nested, show the window
// whenever we have had a first frame to match
// what we do in embedded with Steam for testing
// held commits, etc.
if ( steamMode )
bVisible |= !g_bFirstFrame;
if ( m_bShown != bVisible )
{
m_bShown = bVisible;
if ( m_bShown )
{
SDL_ShowWindow( m_Connector.GetSDLWindow() );
}
else
{
SDL_HideWindow( m_Connector.GetSDLWindow() );
}
}
}
else if ( event.type == GetUserEventIndex( GAMESCOPE_SDL_EVENT_TITLE ) )
{
std::shared_ptr<std::string> pAppTitle = m_pApplicationTitle;
std::string szTitle = pAppTitle ? *pAppTitle : "gamescope";
if ( g_bGrabbed )
szTitle += " (grabbed)";
SDL_SetWindowTitle( m_Connector.GetSDLWindow(), szTitle.c_str() );
szTitle = "Title: " + szTitle;
SDL_SetHint(SDL_HINT_SCREENSAVER_INHIBIT_ACTIVITY_NAME, szTitle.c_str() );
SDL_DisableScreenSaver();
}
else if ( event.type == GetUserEventIndex( GAMESCOPE_SDL_EVENT_ICON ) )
{
std::shared_ptr<std::vector<uint32_t>> pIcon = m_pApplicationIcon;
if ( m_pIconSurface )
{
SDL_FreeSurface( m_pIconSurface );
m_pIconSurface = nullptr;
}
if ( pIcon && pIcon->size() >= 3 )
{
const uint32_t uWidth = (*pIcon)[0];
const uint32_t uHeight = (*pIcon)[1];
m_pIconSurface = SDL_CreateRGBSurfaceFrom(
&(*pIcon)[2],
uWidth, uHeight,
32, uWidth * sizeof(uint32_t),
0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
}
SDL_SetWindowIcon( m_Connector.GetSDLWindow(), m_pIconSurface );
}
else if ( event.type == GetUserEventIndex( GAMESCOPE_SDL_EVENT_GRAB ) )
{
SDL_SetRelativeMouseMode( m_bApplicationGrabbed ? SDL_TRUE : SDL_FALSE );
}
else if ( event.type == GetUserEventIndex( GAMESCOPE_SDL_EVENT_CURSOR ) )
{
std::shared_ptr<INestedHints::CursorInfo> pCursorInfo = m_pApplicationCursor;
if ( m_pCursorSurface )
{
SDL_FreeSurface( m_pCursorSurface );
m_pCursorSurface = nullptr;
}
if ( m_pCursor )
{
SDL_FreeCursor( m_pCursor );
m_pCursor = nullptr;
}
if ( pCursorInfo )
{
m_pCursorSurface = SDL_CreateRGBSurfaceFrom(
pCursorInfo->pPixels.data(),
pCursorInfo->uWidth,
pCursorInfo->uHeight,
32,
pCursorInfo->uWidth * sizeof(uint32_t),
0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
m_pCursor = SDL_CreateColorCursor( m_pCursorSurface, pCursorInfo->uXHotspot, pCursorInfo->uYHotspot );
}
SDL_SetCursor( m_pCursor );
}
}
break;
}
}
}
uint32_t CSDLBackend::GetUserEventIndex( SDLCustomEvents eEvent ) const
{
return m_uUserEventIdBase + uint32_t( eEvent );
}
void CSDLBackend::PushUserEvent( SDLCustomEvents eEvent )
{
SDL_Event event =
{
.user =
{
.type = GetUserEventIndex( eEvent ),
},
};
SDL_PushEvent( &event );
}
/////////////////////////