forked from ValveSoftware/steamos-compositor
-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathmain.cpp
More file actions
1065 lines (941 loc) · 36.8 KB
/
main.cpp
File metadata and controls
1065 lines (941 loc) · 36.8 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
#include "Script/Script.h"
#include <X11/Xlib.h>
#include <cstdio>
#include <thread>
#include <mutex>
#include <vector>
#include <cstring>
#include <string>
#if HAVE_LIBCAP
#include <sys/capability.h>
#endif
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <getopt.h>
#include <signal.h>
#include <unistd.h>
#include <float.h>
#include <climits>
#include "main.hpp"
#include "steamcompmgr.hpp"
#include "rendervulkan.hpp"
#include "wlserver.hpp"
#include "convar.h"
#include "gpuvis_trace_utils.h"
#include "Utils/TempFiles.h"
#include "Utils/Version.h"
#include "Utils/Process.h"
#include "Utils/Defer.h"
#include "backends.h"
#include "refresh_rate.h"
#if HAVE_PIPEWIRE
#include "pipewire.hpp"
#endif
#include <wayland-client.h>
using namespace std::literals;
EStreamColorspace g_ForcedNV12ColorSpace = k_EStreamColorspace_Unknown;
extern gamescope::ConVar<bool> cv_adaptive_sync;
extern gamescope::ConVar<bool> cv_shutdown_on_primary_child_death;
const char *gamescope_optstring = nullptr;
const char *g_pOriginalDisplay = nullptr;
const char *g_pOriginalWaylandDisplay = nullptr;
bool g_bAllowDeferredBackend = false;
int g_nCursorScaleHeight = -1;
const struct option *gamescope_options = (struct option[]){
{ "help", no_argument, nullptr, 0 },
{ "version", no_argument, nullptr, 0 },
{ "nested-width", required_argument, nullptr, 'w' },
{ "nested-height", required_argument, nullptr, 'h' },
{ "nested-refresh", required_argument, nullptr, 'r' },
{ "max-scale", required_argument, nullptr, 'm' },
{ "scaler", required_argument, nullptr, 'S' },
{ "filter", required_argument, nullptr, 'F' },
{ "output-width", required_argument, nullptr, 'W' },
{ "output-height", required_argument, nullptr, 'H' },
{ "sharpness", required_argument, nullptr, 0 },
{ "fsr-sharpness", required_argument, nullptr, 0 },
{ "rt", no_argument, nullptr, 0 },
{ "prefer-vk-device", required_argument, 0 },
{ "expose-wayland", no_argument, 0 },
{ "mouse-sensitivity", required_argument, nullptr, 's' },
{ "mangoapp", no_argument, nullptr, 0 },
{ "adaptive-sync", no_argument, nullptr, 0 },
{ "backend", required_argument, nullptr, 0 },
// nested mode options
{ "nested-unfocused-refresh", required_argument, nullptr, 'o' },
{ "borderless", no_argument, nullptr, 'b' },
{ "fullscreen", no_argument, nullptr, 'f' },
{ "grab", no_argument, nullptr, 'g' },
{ "force-grab-cursor", no_argument, nullptr, 0 },
{ "display-index", required_argument, nullptr, 0 },
// embedded mode options
{ "disable-layers", no_argument, nullptr, 0 },
{ "debug-layers", no_argument, nullptr, 0 },
{ "prefer-output", required_argument, nullptr, 'O' },
{ "default-touch-mode", required_argument, nullptr, 0 },
{ "generate-drm-mode", required_argument, nullptr, 0 },
{ "immediate-flips", no_argument, nullptr, 0 },
{ "framerate-limit", required_argument, nullptr, 0 },
// openvr options
#if HAVE_OPENVR
{ "vr-overlay-key", required_argument, nullptr, 0 },
{ "vr-app-overlay-key", required_argument, nullptr, 0 },
{ "vr-overlay-explicit-name", required_argument, nullptr, 0 },
{ "vr-overlay-default-name", required_argument, nullptr, 0 },
{ "vr-overlay-icon", required_argument, nullptr, 0 },
{ "vr-overlay-show-immediately", no_argument, nullptr, 0 },
{ "vr-overlay-enable-control-bar", no_argument, nullptr, 0 },
{ "vr-overlay-enable-control-bar-keyboard", no_argument, nullptr, 0 },
{ "vr-overlay-enable-control-bar-close", no_argument, nullptr, 0 },
{ "vr-overlay-enable-click-stabilization", no_argument, nullptr, 0 },
{ "vr-overlay-modal", no_argument, nullptr, 0 },
{ "vr-overlay-physical-width", required_argument, nullptr, 0 },
{ "vr-overlay-physical-curvature", required_argument, nullptr, 0 },
{ "vr-overlay-physical-pre-curve-pitch", required_argument, nullptr, 0 },
{ "vr-scroll-speed", required_argument, nullptr, 0 },
{ "vr-session-manager", no_argument, nullptr, 0 },
#endif
// wlserver options
{ "xwayland-count", required_argument, nullptr, 0 },
// steamcompmgr options
{ "cursor", required_argument, nullptr, 0 },
{ "cursor-hotspot", required_argument, nullptr, 0 },
{ "cursor-scale-height", required_argument, nullptr, 0 },
{ "virtual-connector-strategy", required_argument, nullptr, 0 },
{ "ready-fd", required_argument, nullptr, 'R' },
{ "stats-path", required_argument, nullptr, 'T' },
{ "hide-cursor-delay", required_argument, nullptr, 'C' },
{ "debug-focus", no_argument, nullptr, 0 },
{ "synchronous-x11", no_argument, nullptr, 0 },
{ "debug-hud", no_argument, nullptr, 'v' },
{ "debug-events", no_argument, nullptr, 0 },
{ "steam", no_argument, nullptr, 'e' },
{ "force-composition", no_argument, nullptr, 'c' },
{ "composite-debug", no_argument, nullptr, 0 },
{ "disable-xres", no_argument, nullptr, 'x' },
{ "fade-out-duration", required_argument, nullptr, 0 },
{ "force-orientation", required_argument, nullptr, 0 },
{ "force-windows-fullscreen", no_argument, nullptr, 0 },
{ "disable-color-management", no_argument, nullptr, 0 },
{ "sdr-gamut-wideness", required_argument, nullptr, 0 },
{ "hdr-enabled", no_argument, nullptr, 0 },
{ "hdr-sdr-content-nits", required_argument, nullptr, 0 },
{ "hdr-itm-enabled", no_argument, nullptr, 0 },
{ "hdr-itm-sdr-nits", required_argument, nullptr, 0 },
{ "hdr-itm-target-nits", required_argument, nullptr, 0 },
{ "hdr-debug-force-support", no_argument, nullptr, 0 },
{ "hdr-debug-force-output", no_argument, nullptr, 0 },
{ "hdr-debug-heatmap", no_argument, nullptr, 0 },
{ "reshade-effect", required_argument, nullptr, 0 },
{ "reshade-technique-idx", required_argument, nullptr, 0 },
// Steam Deck options
{ "mura-map", required_argument, nullptr, 0 },
{ "allow-deferred-backend", no_argument, nullptr, 0 },
{ "keep-alive", no_argument, nullptr, 0 },
{} // keep last
};
const char usage[] =
"usage: gamescope [options...] -- [command...]\n"
"\n"
"Options:\n"
" --help show help message\n"
" -W, --output-width output width\n"
" -H, --output-height output height\n"
" -w, --nested-width game width\n"
" -h, --nested-height game height\n"
" -r, --nested-refresh game refresh rate (frames per second)\n"
" -m, --max-scale maximum scale factor\n"
" -S, --scaler upscaler type (auto, integer, fit, fill, stretch)\n"
" -F, --filter upscaler filter (linear, nearest, fsr, nis, pixel)\n"
" fsr => AMD FidelityFX™ Super Resolution 1.0\n"
" nis => NVIDIA Image Scaling v1.0.3\n"
" --sharpness, --fsr-sharpness upscaler sharpness from 0 (max) to 20 (min)\n"
" --expose-wayland support wayland clients using xdg-shell\n"
" -s, --mouse-sensitivity multiply mouse movement by given decimal number\n"
" --backend select rendering backend\n"
" auto => autodetect (default)\n"
#if HAVE_DRM
" drm => use DRM backend (standalone display session)\n"
#endif
#if HAVE_SDL2
" sdl => use SDL backend\n"
#endif
#if HAVE_OPENVR
" openvr => use OpenVR backend (outputs as a VR overlay)\n"
#endif
" headless => use headless backend (no window, no DRM output)\n"
" wayland => use Wayland backend\n"
" --cursor path to default cursor image\n"
" -R, --ready-fd notify FD when ready\n"
" --rt Use realtime scheduling\n"
" -T, --stats-path write statistics to path\n"
" -C, --hide-cursor-delay hide cursor image after delay\n"
" -e, --steam enable Steam integration\n"
" --xwayland-count create N xwayland servers\n"
" --prefer-vk-device prefer Vulkan device for compositing (ex: 1002:7300)\n"
" --force-orientation rotate the internal display (left, right, normal, upsidedown)\n"
" --force-windows-fullscreen force windows inside of gamescope to be the size of the nested display (fullscreen)\n"
" --cursor-scale-height if specified, sets a base output height to linearly scale the cursor against.\n"
" --virtual-connector-strategy Specifies how we should make virtual connectors.\n"
" --hdr-enabled enable HDR output (needs Gamescope WSI layer enabled for support from clients)\n"
" If this is not set, and there is a HDR client, it will be tonemapped SDR.\n"
" --sdr-gamut-wideness Set the 'wideness' of the gamut for SDR comment. 0 - 1.\n"
" --hdr-sdr-content-nits set the luminance of SDR content in nits. Default: 400 nits.\n"
" --hdr-itm-enabled enable SDR->HDR inverse tone mapping. only works for SDR input.\n"
" --hdr-itm-sdr-nits set the luminance of SDR content in nits used as the input for the inverse tone mapping process.\n"
" Default: 100 nits, Max: 1000 nits\n"
" --hdr-itm-target-nits set the target luminace of the inverse tone mapping process.\n"
" Default: 1000 nits, Max: 10000 nits\n"
" --framerate-limit Set a simple framerate limit. Used as a divisor of the refresh rate, rounds down eg 60 / 59 -> 60fps, 60 / 25 -> 30fps. Default: 0, disabled.\n"
" --mangoapp Launch with the mangoapp (mangohud) performance overlay enabled. You should use this instead of using mangohud on the game or gamescope.\n"
" --adaptive-sync Enable adaptive sync if available (variable rate refresh)\n"
"\n"
"Nested mode options:\n"
" -o, --nested-unfocused-refresh game refresh rate when unfocused\n"
" -b, --borderless make the window borderless\n"
" -f, --fullscreen make the window fullscreen\n"
" -g, --grab grab the keyboard\n"
" --force-grab-cursor always use relative mouse mode instead of flipping dependent on cursor visibility.\n"
" --display-index forces gamescope to use a specific display in nested mode."
"\n"
"Embedded mode options:\n"
" -O, --prefer-output list of connectors in order of preference (ex: DP-1,DP-2,DP-3,HDMI-A-1)\n"
" --default-touch-mode 0: hover, 1: left, 2: right, 3: middle, 4: passthrough\n"
" --generate-drm-mode DRM mode generation algorithm (cvt, fixed)\n"
" --immediate-flips Enable immediate flips, may result in tearing\n"
"\n"
#if HAVE_OPENVR
"VR mode options:\n"
" --vr-overlay-key Sets the SteamVR overlay key to this string\n"
" --vr-app-overlay-key Sets the SteamVR overlay key to use for child apps\n"
" --vr-overlay-explicit-name Force the SteamVR overlay name to always be this string\n"
" --vr-overlay-default-name Sets the fallback SteamVR overlay name when there is no window title\n"
" --vr-overlay-icon Sets the SteamVR overlay icon to this file\n"
" --vr-overlay-show-immediately Makes our VR overlay take focus immediately\n"
" --vr-overlay-enable-control-bar Enables the SteamVR control bar\n"
" --vr-overlay-enable-control-bar-keyboard Enables the SteamVR keyboard button on the control bar\n"
" --vr-overlay-enable-control-bar-close Enables the SteamVR close button on the control bar\n"
" --vr-overlay-enable-click-stabilization Enables the SteamVR click stabilization\n"
" --vr-overlay-modal Makes our VR overlay appear as a modal\n"
" --vr-overlay-physical-width Sets the physical width of our VR overlay in metres\n"
" --vr-overlay-physical-curvature Sets the curvature of our VR overlay\n"
" --vr-overlay-physical-pre-curve-pitch Sets the pre-curve pitch of our VR overlay\n"
" --vr-scrolls-speed Mouse scrolling speed of trackpad scroll in VR. Default: 8.0\n"
"\n"
#endif
"Debug options:\n"
" --disable-layers disable libliftoff (hardware planes)\n"
" --debug-layers debug libliftoff\n"
" --debug-focus debug XWM focus\n"
" --synchronous-x11 force X11 connection synchronization\n"
" --debug-hud paint HUD with debug info\n"
" --debug-events debug X11 events\n"
" --force-composition disable direct scan-out\n"
" --composite-debug draw frame markers on alternating corners of the screen when compositing\n"
" --disable-color-management disable color management\n"
" --disable-xres disable XRes for PID lookup\n"
" --hdr-debug-force-support forces support for HDR, etc even if the display doesn't support it. HDR clients will be outputted as SDR still in that case.\n"
" --hdr-debug-force-output forces support and output to HDR10 PQ even if the output does not support it (will look very wrong if it doesn't)\n"
" --hdr-debug-heatmap displays a heatmap-style debug view of HDR luminence across the scene in nits."
"\n"
"Reshade shader options:\n"
" --reshade-effect sets the name of a reshade shader to use in either /usr/share/gamescope/reshade/Shaders or ~/.local/share/gamescope/reshade/Shaders\n"
" --reshade-technique-idx sets technique idx to use from the reshade effect\n"
"\n"
"Steam Deck options:\n"
" --mura-map Set the mura compensation map to use for the display. Takes in a path to the mura map.\n"
"\n"
"Platform options:\n"
" --allow-deferred-backend Allows initting the backend in a deferred way, if it doesn't work immediately. (Note: This has some very minor correctness compromises that you should consider wrt. your platform with modifiers, etc).\n"
" --keep-alive Keep Gamescope alive even when the primary process has died.\n"
"\n"
"Keyboard shortcuts:\n"
" Super + F toggle fullscreen\n"
" Super + N toggle nearest neighbour filtering\n"
" Super + U toggle FSR upscaling\n"
" Super + Y toggle NIS upscaling\n"
" Super + I increase FSR sharpness by 1\n"
" Super + O decrease FSR sharpness by 1\n"
" Super + S take a screenshot\n"
" Super + G toggle keyboard grab\n"
"";
std::atomic< bool > g_bRun{true};
int g_nNestedWidth = 0;
int g_nNestedHeight = 0;
int g_nNestedRefresh = 0;
int g_nNestedUnfocusedRefresh = 0;
int g_nNestedDisplayIndex = 0;
uint32_t g_nOutputWidth = 0;
uint32_t g_nOutputHeight = 0;
int g_nOutputRefresh = 0;
bool g_bOutputHDREnabled = false;
bool g_bFullscreen = false;
bool g_bForceRelativeMouse = false;
bool g_bGrabbed = false;
bool g_bShortcutGrabbed = false;
float g_mouseSensitivity = 1.0;
GamescopeUpscaleFilter g_upscaleFilter = GamescopeUpscaleFilter::LINEAR;
GamescopeUpscaleScaler g_upscaleScaler = GamescopeUpscaleScaler::AUTO;
GamescopeUpscaleFilter g_wantedUpscaleFilter = GamescopeUpscaleFilter::LINEAR;
GamescopeUpscaleScaler g_wantedUpscaleScaler = GamescopeUpscaleScaler::AUTO;
int g_upscaleFilterSharpness = 2;
gamescope::GamescopeModeGeneration g_eGamescopeModeGeneration = gamescope::GAMESCOPE_MODE_GENERATE_CVT;
bool g_bBorderlessOutputWindow = false;
int g_nXWaylandCount = 1;
float g_flMaxWindowScale = FLT_MAX;
uint32_t g_preferVendorID = 0;
uint32_t g_preferDeviceID = 0;
pthread_t g_mainThread;
static void steamCompMgrThreadRun(int argc, char **argv);
static std::string build_optstring(const struct option *options)
{
std::string optstring;
for (size_t i = 0; options[i].name != nullptr; i++) {
if (!options[i].name || !options[i].val)
continue;
assert(optstring.find((char) options[i].val) == std::string::npos);
char str[] = { (char) options[i].val, '\0' };
optstring.append(str);
if (options[i].has_arg)
optstring.append(":");
}
return optstring;
}
static gamescope::GamescopeModeGeneration parse_gamescope_mode_generation( const char *str )
{
if ( str == "cvt"sv )
{
return gamescope::GAMESCOPE_MODE_GENERATE_CVT;
}
else if ( str == "fixed"sv )
{
return gamescope::GAMESCOPE_MODE_GENERATE_FIXED;
}
else
{
fprintf( stderr, "gamescope: invalid value for --generate-drm-mode\n" );
exit(1);
}
}
GamescopePanelOrientation g_DesiredInternalOrientation = GAMESCOPE_PANEL_ORIENTATION_AUTO;
static GamescopePanelOrientation force_orientation(const char *str)
{
if (strcmp(str, "normal") == 0) {
return GAMESCOPE_PANEL_ORIENTATION_0;
} else if (strcmp(str, "right") == 0) {
return GAMESCOPE_PANEL_ORIENTATION_270;
} else if (strcmp(str, "left") == 0) {
return GAMESCOPE_PANEL_ORIENTATION_90;
} else if (strcmp(str, "upsidedown") == 0) {
return GAMESCOPE_PANEL_ORIENTATION_180;
} else {
fprintf( stderr, "gamescope: invalid value for --force-orientation\n" );
exit(1);
}
}
static enum GamescopeUpscaleScaler parse_upscaler_scaler(const char *str)
{
if (strcmp(str, "auto") == 0) {
return GamescopeUpscaleScaler::AUTO;
} else if (strcmp(str, "integer") == 0) {
return GamescopeUpscaleScaler::INTEGER;
} else if (strcmp(str, "fit") == 0) {
return GamescopeUpscaleScaler::FIT;
} else if (strcmp(str, "fill") == 0) {
return GamescopeUpscaleScaler::FILL;
} else if (strcmp(str, "stretch") == 0) {
return GamescopeUpscaleScaler::STRETCH;
} else {
fprintf( stderr, "gamescope: invalid value for --scaler\n" );
exit(1);
}
}
static enum GamescopeUpscaleFilter parse_upscaler_filter(const char *str)
{
if (strcmp(str, "linear") == 0) {
return GamescopeUpscaleFilter::LINEAR;
} else if (strcmp(str, "nearest") == 0) {
return GamescopeUpscaleFilter::NEAREST;
} else if (strcmp(str, "fsr") == 0) {
return GamescopeUpscaleFilter::FSR;
} else if (strcmp(str, "nis") == 0) {
return GamescopeUpscaleFilter::NIS;
} else if (strcmp(str, "pixel") == 0) {
return GamescopeUpscaleFilter::PIXEL;
} else {
fprintf( stderr, "gamescope: invalid value for --filter\n" );
exit(1);
}
}
static enum gamescope::GamescopeBackend parse_backend_name(const char *str)
{
if (strcmp(str, "auto") == 0) {
return gamescope::GamescopeBackend::Auto;
#if HAVE_DRM
} else if (strcmp(str, "drm") == 0) {
return gamescope::GamescopeBackend::DRM;
#endif
#if HAVE_SDL2
} else if (strcmp(str, "sdl") == 0) {
return gamescope::GamescopeBackend::SDL;
#endif
#if HAVE_OPENVR
} else if (strcmp(str, "openvr") == 0) {
return gamescope::GamescopeBackend::OpenVR;
#endif
} else if (strcmp(str, "headless") == 0) {
return gamescope::GamescopeBackend::Headless;
} else if (strcmp(str, "wayland") == 0) {
return gamescope::GamescopeBackend::Wayland;
} else {
fprintf( stderr, "gamescope: invalid value for --backend\n" );
exit(1);
}
}
static int parse_integer(const char *str, const char *optionName)
{
auto result = gamescope::Parse<int>(str);
if ( result.has_value() )
{
return result.value();
}
else
{
fprintf( stderr, "gamescope: invalid value for --%s, \"%s\" is either not an integer or is far too large\n", optionName, str );
exit(1);
}
}
static float parse_float(const char *str, const char *optionName)
{
auto result = gamescope::Parse<float>(str);
if ( result.has_value() )
{
return result.value();
}
else
{
fprintf( stderr, "gamescope: invalid value for --%s, \"%s\" could not be interpreted as a real number\n", optionName, str );
exit(1);
}
}
struct sigaction handle_signal_action = {};
void ShutdownGamescope()
{
g_bRun = false;
nudge_steamcompmgr();
}
static gamescope::ConCommand cc_shutdown( "shutdown", "Cleanly shutdown gamescope",
[]( std::span<std::string_view> svArgs )
{
console_log.infof( "Shutting down..." );
ShutdownGamescope();
});
static void handle_signal( int sig )
{
switch ( sig ) {
case SIGUSR2:
gamescope::CScreenshotManager::Get().TakeScreenshot( true );
break;
case SIGHUP:
case SIGQUIT:
case SIGTERM:
case SIGINT:
ShutdownGamescope();
break;
case SIGUSR1:
fprintf( stderr, "gamescope: hi :3\n" );
break;
default:
assert( false ); // unreachable
}
}
static EStreamColorspace parse_colorspace_string( const char *pszStr )
{
if ( !pszStr || !*pszStr )
return k_EStreamColorspace_Unknown;
if ( !strcmp( pszStr, "k_EStreamColorspace_BT601" ) )
return k_EStreamColorspace_BT601;
else if ( !strcmp( pszStr, "k_EStreamColorspace_BT601_Full" ) )
return k_EStreamColorspace_BT601_Full;
else if ( !strcmp( pszStr, "k_EStreamColorspace_BT709" ) )
return k_EStreamColorspace_BT709;
else if ( !strcmp( pszStr, "k_EStreamColorspace_BT709_Full" ) )
return k_EStreamColorspace_BT709_Full;
else
return k_EStreamColorspace_Unknown;
}
static bool g_bSupportsWaylandPresentationTime = false;
static constexpr wl_registry_listener s_registryListener = {
.global = [](void* data, wl_registry* registry, uint32_t name, const char* interface, uint32_t version) {
if (interface == "wp_presentation"sv)
g_bSupportsWaylandPresentationTime = true;
},
.global_remove = [](void* data, wl_registry* registry, uint32_t name) {
},
};
static bool CheckWaylandPresentationTime()
{
wl_display *display = wl_display_connect(g_pOriginalWaylandDisplay);
if (!display) {
fprintf(stderr, "Failed to connect to wayland socket: %s.\n", g_pOriginalWaylandDisplay);
exit(1);
return false;
}
wl_registry *registry = wl_display_get_registry(display);
wl_registry_add_listener(registry, &s_registryListener, nullptr);
wl_display_dispatch(display);
wl_display_roundtrip(display);
wl_registry_destroy(registry);
wl_display_disconnect(display);
return g_bSupportsWaylandPresentationTime;
}
#if 0
static bool IsInDebugSession()
{
static FILE *fp;
if ( !fp )
{
fp = fopen( "/proc/self/status", "r" );
}
char rgchLine[256]; rgchLine[0] = '\0';
int nTracePid = 0;
if ( fp )
{
const char *pszSearchString = "TracerPid:";
const uint cchSearchString = strlen( pszSearchString );
rewind( fp );
fflush( fp );
while ( fgets( rgchLine, sizeof(rgchLine), fp ) )
{
if ( !strncasecmp( pszSearchString, rgchLine, cchSearchString ) )
{
char *pszVal = rgchLine+cchSearchString+1;
nTracePid = atoi( pszVal );
break;
}
}
}
return nTracePid != 0;
}
#endif
bool steamMode = false;
bool g_bLaunchMangoapp = false;
static void UpdateCompatEnvVars()
{
// Legacy env vars for compat.
if ( steamMode )
{
// We have NIS support.
setenv( "STEAM_GAMESCOPE_NIS_SUPPORTED", "1", 0 );
// Have SteamRT's xdg-open send http:// and https:// URLs to Steam
setenv( "SRT_URLOPEN_PREFER_STEAM", "1", 0 );
if ( g_nXWaylandCount > 1 )
{
setenv( "STEAM_MULTIPLE_XWAYLANDS", "1", 0 );
}
// If the backend exposes tearing, expose that to Steam.
if ( GetBackend()->SupportsTearing() )
{
setenv( "STEAM_GAMESCOPE_TEARING_SUPPORTED", "1", 0 );
setenv( "STEAM_GAMESCOPE_HAS_TEARING_SUPPORT", "1", 0 );
}
// We always support VRR (but not necessarily on every connector, etc.)
setenv( "STEAM_GAMESCOPE_VRR_SUPPORTED", "1", 0 );
// We no longer need to set GAMESCOPE_EXTERNAL_OVERLAY from steam, mangoapp now does it itself
setenv( "STEAM_DISABLE_MANGOAPP_ATOM_WORKAROUND", "1", 0 );
// Enable horizontal mangoapp bar
setenv( "STEAM_MANGOAPP_HORIZONTAL_SUPPORTED", "1", 0 );
// Scaling support
setenv( "STEAM_GAMESCOPE_FANCY_SCALING_SUPPORT", "1", 0 );
// We support HDR.
setenv( "STEAM_GAMESCOPE_HDR_SUPPORTED", "1", 0 );
// Gamescope WSI layer implements this.
setenv( "STEAM_GAMESCOPE_DYNAMIC_FPSLIMITER", "1", 0 );
// Set input method modules for Qt/GTK that will show the Steam keyboard
// These are mostly SteamOS specific, and are set by our Gamescope session,
// but might be useful for you.
//setenv( "QT_IM_MODULE", "steam", 1 );
//setenv( "GTK_IM_MODULE", "Steam", 1 );
//setenv( "QT_QPA_PLATFORM_THEME", "kde", 1 );
// Maybe we should expose a backend check for this...
// STEAM_GAMESCOPE_COLOR_MANAGED
// STEAM_GAMESCOPE_VIRTUAL_WHITE
// STEAM_USE_DYNAMIC_VRS is RADV specific, so don't expose this right now.
setenv( "STEAM_MANGOAPP_PRESETS_SUPPORTED", "1", 0 );
setenv( "STEAM_USE_MANGOAPP", "1", 0 );
}
// Always set this to false, we never want buffers to be waited on by Mesa.
// That is our job!
setenv( "vk_xwayland_wait_ready", "false", 1 );
if ( g_nCursorScaleHeight > 0 )
{
// We always want the biggest cursor size so we can scale it.
setenv( "XCURSOR_SIZE", "256", 1 );
}
// Legacy support for SteamOS.
setenv( "XWAYLAND_FORCE_ENABLE_EXTRA_MODES", "1", 1 );
// Don't minimise stuff on focus loss with SDL.
setenv( "SDL_VIDEO_MINIMIZE_ON_FOCUS_LOSS", "0", 1 );
const char *pszMangoConfigPath = getenv( "MANGOHUD_CONFIGFILE" );
if ( (g_bLaunchMangoapp && steamMode) && ( !pszMangoConfigPath || !*pszMangoConfigPath ) )
{
char szMangoConfigPath[ PATH_MAX ];
FILE *pMangoConfigFile = gamescope::MakeTempFile( szMangoConfigPath, gamescope::k_szGamescopeTempMangoappTemplate, "w", true );
if ( pMangoConfigFile )
{
setenv( "MANGOHUD_CONFIGFILE", szMangoConfigPath, 1 );
if ( steamMode )
{
const char szDefaultConfig[] = "no_display";
fwrite( szDefaultConfig, 1, sizeof( szDefaultConfig ), pMangoConfigFile );
}
fclose( pMangoConfigFile );
}
}
const char *pszLimiterFile = getenv( "GAMESCOPE_LIMITER_FILE" );
if ( !pszLimiterFile || !*pszLimiterFile )
{
char szLimiterPath[ PATH_MAX ];
int nLimiterFd = gamescope::MakeTempFile( szLimiterPath, gamescope::k_szGamescopeTempLimiterTemplate, true );
if ( nLimiterFd >= 0 )
{
setenv( "GAMESCOPE_LIMITER_FILE", szLimiterPath, 1 );
gamescope::Process::CloseFd( nLimiterFd );
}
}
}
int g_nPreferredOutputWidth = 0;
int g_nPreferredOutputHeight = 0;
bool g_bExposeWayland = false;
const char *g_sOutputName = nullptr;
bool g_bDebugLayers = false;
bool g_bForceDisableColorMgmt = false;
bool g_bRt = false;
// This will go away when we remove the getopt stuff from vr session.
// For now...
int g_argc;
char **g_argv;
int main(int argc, char **argv)
{
g_argc = argc;
g_argv = argv;
// Force disable this horrible broken layer.
setenv("DISABLE_LAYER_AMD_SWITCHABLE_GRAPHICS_1", "1", 1);
static std::string optstring = build_optstring(gamescope_options);
gamescope_optstring = optstring.c_str();
gamescope::GamescopeBackend eCurrentBackend = gamescope::GamescopeBackend::Auto;
gamescope::PrintVersion();
int o;
int opt_index = -1;
while ((o = getopt_long(argc, argv, gamescope_optstring, gamescope_options, &opt_index)) != -1)
{
const char *opt_name;
switch (o) {
case 'w':
g_nNestedWidth = parse_integer( optarg, "nested-width" );
break;
case 'h':
g_nNestedHeight = parse_integer( optarg, "nested-height" );
break;
case 'r':
g_nNestedRefresh = gamescope::ConvertHztomHz( parse_integer( optarg, "nested-refresh" ) );
break;
case 'W':
g_nPreferredOutputWidth = parse_integer( optarg, "output-width" );
break;
case 'H':
g_nPreferredOutputHeight = parse_integer( optarg, "output-height" );
break;
case 'o':
g_nNestedUnfocusedRefresh = gamescope::ConvertHztomHz( parse_integer( optarg, "nested-unfocused-refresh" ) );
break;
case 'm':
g_flMaxWindowScale = parse_float( optarg, "max-scale" );
break;
case 'S':
g_wantedUpscaleScaler = parse_upscaler_scaler(optarg);
break;
case 'F':
g_wantedUpscaleFilter = parse_upscaler_filter(optarg);
break;
case 'b':
g_bBorderlessOutputWindow = true;
break;
case 'f':
g_bFullscreen = true;
break;
case 'O':
g_sOutputName = optarg;
break;
case 'g':
g_bGrabbed = true;
break;
case 's':
g_mouseSensitivity = parse_float( optarg, "mouse-sensitivity" );
break;
case 'e':
steamMode = true;
if ( gamescope::cv_backend_virtual_connector_strategy == gamescope::VirtualConnectorStrategies::SingleApplication )
gamescope::cv_backend_virtual_connector_strategy = gamescope::VirtualConnectorStrategies::SteamControlled;
break;
case 0: // long options without a short option
opt_name = gamescope_options[opt_index].name;
if (strcmp(opt_name, "help") == 0) {
fprintf(stderr, "%s", usage);
return 0;
} else if (strcmp(opt_name, "version") == 0) {
// We always print the version to stderr anyway.
return 0;
} else if (strcmp(opt_name, "debug-layers") == 0) {
g_bDebugLayers = true;
} else if (strcmp(opt_name, "disable-color-management") == 0) {
g_bForceDisableColorMgmt = true;
} else if (strcmp(opt_name, "xwayland-count") == 0) {
g_nXWaylandCount = parse_integer( optarg, opt_name );
} else if (strcmp(opt_name, "composite-debug") == 0) {
cv_composite_debug |= CompositeDebugFlag::Markers;
cv_composite_debug |= CompositeDebugFlag::PlaneBorders;
} else if (strcmp(opt_name, "hdr-debug-heatmap") == 0) {
cv_composite_debug |= CompositeDebugFlag::Heatmap;
} else if (strcmp(opt_name, "default-touch-mode") == 0) {
gamescope::cv_touch_click_mode = (gamescope::TouchClickMode) parse_integer( optarg, opt_name );
} else if (strcmp(opt_name, "generate-drm-mode") == 0) {
g_eGamescopeModeGeneration = parse_gamescope_mode_generation( optarg );
} else if (strcmp(opt_name, "force-orientation") == 0) {
g_DesiredInternalOrientation = force_orientation( optarg );
} else if (strcmp(opt_name, "sharpness") == 0 ||
strcmp(opt_name, "fsr-sharpness") == 0) {
g_upscaleFilterSharpness = parse_integer( optarg, opt_name );
} else if (strcmp(opt_name, "rt") == 0) {
g_bRt = true;
} else if (strcmp(opt_name, "prefer-vk-device") == 0) {
unsigned vendorID;
unsigned deviceID;
sscanf( optarg, "%X:%X", &vendorID, &deviceID );
g_preferVendorID = vendorID;
g_preferDeviceID = deviceID;
} else if (strcmp(opt_name, "immediate-flips") == 0) {
cv_tearing_enabled = true;
} else if (strcmp(opt_name, "force-grab-cursor") == 0) {
g_bForceRelativeMouse = true;
} else if (strcmp(opt_name, "display-index") == 0) {
g_nNestedDisplayIndex = parse_integer( optarg, opt_name );
} else if (strcmp(opt_name, "adaptive-sync") == 0) {
cv_adaptive_sync = true;
} else if (strcmp(opt_name, "expose-wayland") == 0) {
g_bExposeWayland = true;
} else if (strcmp(opt_name, "backend") == 0) {
eCurrentBackend = parse_backend_name( optarg );
} else if (strcmp(opt_name, "cursor-scale-height") == 0) {
g_nCursorScaleHeight = parse_integer(optarg, opt_name);
} else if (strcmp(opt_name, "mangoapp") == 0) {
g_bLaunchMangoapp = true;
} else if (strcmp(opt_name, "allow-deferred-backend") == 0) {
g_bAllowDeferredBackend = true;
} else if (strcmp(opt_name, "keep-alive") == 0) {
cv_shutdown_on_primary_child_death = false;
} else if (strcmp(opt_name, "virtual-connector-strategy") == 0) {
for ( uint32_t i = 0; i < gamescope::VirtualConnectorStrategies::Count; i++ )
{
gamescope::VirtualConnectorStrategy eStrategy =
static_cast<gamescope::VirtualConnectorStrategy>( i );
if ( optarg == gamescope::VirtualConnectorStrategyToString( eStrategy ) )
{
gamescope::cv_backend_virtual_connector_strategy = eStrategy;
}
}
}
break;
case '?':
fprintf( stderr, "See --help for a list of options.\n" );
return 1;
}
}
if ( gamescope::Process::HasCapSysNice() )
{
gamescope::Process::SetNice( -20 );
if ( g_bRt )
gamescope::Process::SetRealtime();
}
else
{
fprintf( stderr, "No CAP_SYS_NICE, falling back to regular-priority compute and threads.\nPerformance will be affected.\n" );
}
#if 0
while( !IsInDebugSession() )
{
usleep( 100 );
}
#endif
gamescope::Process::RaiseFdLimit();
if ( gpuvis_trace_init() != -1 )
{
fprintf( stderr, "Tracing is enabled\n");
}
{
gamescope::CScriptScopedLock script;
script.Manager().RunDefaultScripts();
}
XInitThreads();
g_mainThread = pthread_self();
g_pOriginalDisplay = getenv("DISPLAY");
g_pOriginalWaylandDisplay = getenv("WAYLAND_DISPLAY");
if ( eCurrentBackend == gamescope::GamescopeBackend::Auto )
{
if ( g_pOriginalWaylandDisplay != NULL )
eCurrentBackend = gamescope::GamescopeBackend::Wayland;
else if ( g_pOriginalDisplay != NULL )
eCurrentBackend = gamescope::GamescopeBackend::SDL;
else
eCurrentBackend = gamescope::GamescopeBackend::DRM;
}
if ( g_pOriginalWaylandDisplay != NULL )
{
if (CheckWaylandPresentationTime())
{
// Default to SDL_VIDEODRIVER wayland under Wayland and force enable vk_khr_present_wait
// (not enabled by default in Mesa because instance does not know if Wayland
// compositor supports wp_presentation, but we can check that ourselves.)
setenv("vk_khr_present_wait", "true", 0);
setenv("SDL_VIDEODRIVER", "wayland", 0);
}
else
{
fprintf(stderr,
"Your Wayland compositor does NOT support wp_presentation/presentation-time which is required for VK_KHR_present_wait and VK_KHR_present_id.\n"
"Please complain to your compositor vendor for support. Falling back to X11 window with less accurate present wait.\n");
setenv("SDL_VIDEODRIVER", "x11", 1);
}
}
g_ForcedNV12ColorSpace = parse_colorspace_string( getenv( "GAMESCOPE_NV12_COLORSPACE" ) );
switch ( eCurrentBackend )
{
#if HAVE_DRM
case gamescope::GamescopeBackend::DRM:
gamescope::IBackend::Set<gamescope::CDRMBackend>();
break;
#endif
#if HAVE_SDL2
case gamescope::GamescopeBackend::SDL:
gamescope::IBackend::Set<gamescope::CSDLBackend>();
break;
#endif
#if HAVE_OPENVR
case gamescope::GamescopeBackend::OpenVR:
gamescope::IBackend::Set<gamescope::COpenVRBackend>();
break;
#endif
case gamescope::GamescopeBackend::Headless:
gamescope::IBackend::Set<gamescope::CHeadlessBackend>();
break;
case gamescope::GamescopeBackend::Wayland:
gamescope::IBackend::Set<gamescope::CWaylandBackend>();
#if HAVE_SDL2
if ( !GetBackend() )
gamescope::IBackend::Set<gamescope::CSDLBackend>();
#endif
break;
default:
abort();
}
if ( !GetBackend() )
{
fprintf( stderr, "Failed to create backend.\n" );
return 1;
}
UpdateCompatEnvVars();
if ( !vulkan_init_formats() )
{
fprintf( stderr, "vulkan_init_formats failed\n" );
return 1;
}
if ( !vulkan_make_output() )
{
fprintf( stderr, "vulkan_make_output failed\n" );
return 1;
}
// Prevent our clients from connecting to the parent compositor
unsetenv("WAYLAND_DISPLAY");
// If DRM format modifiers aren't supported, prevent our clients from using
// DCC, as this can cause tiling artifacts.
if ( !vulkan_supports_modifiers() )
{
const char *pchR600Debug = getenv( "R600_DEBUG" );
if ( pchR600Debug == nullptr )
{
setenv( "R600_DEBUG", "nodcc", 1 );
}
else if ( strstr( pchR600Debug, "nodcc" ) == nullptr )
{
std::string strPreviousR600Debug = pchR600Debug;
strPreviousR600Debug.append( ",nodcc" );
setenv( "R600_DEBUG", strPreviousR600Debug.c_str(), 1 );
}
}
if ( g_nNestedHeight == 0 )
{
if ( g_nNestedWidth != 0 )
{
fprintf( stderr, "Cannot specify -w without -h\n" );
return 1;
}
g_nNestedWidth = g_nOutputWidth;
g_nNestedHeight = g_nOutputHeight;
}
if ( g_nNestedWidth == 0 )
g_nNestedWidth = g_nNestedHeight * 16 / 9;