Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
gamescope_session.h
Go to the documentation of this file.
1
10#pragma once
11
12#include <cctype>
13#include <format>
14#include <optional>
15#include <span>
16#include <string>
17#include <string_view>
18
19namespace platf::gamescope {
20
35
36 inline std::string_view drm_selector_flag(drm_selector_e selector) {
37 switch (selector) {
38 case drm_selector_e::prefer_drm:
39 return "--prefer-drm";
40 case drm_selector_e::drm_device:
41 return "--drm-device";
42 case drm_selector_e::none:
43 break;
44 }
45 return {};
46 }
47
52 inline bool help_lists_option(std::string_view help, std::string_view option) {
53 const auto is_option_char = [](char ch) {
54 return std::isalnum(static_cast<unsigned char>(ch)) != 0 || ch == '-' || ch == '_';
55 };
56 for (auto pos = help.find(option); pos != std::string_view::npos; pos = help.find(option, pos + 1)) {
57 const auto end = pos + option.size();
58 const bool starts_alone = pos == 0 || !is_option_char(help[pos - 1]);
59 const bool ends_alone = end == help.size() || !is_option_char(help[end]);
60 if (starts_alone && ends_alone) {
61 return true;
62 }
63 }
64 return false;
65 }
66
74 inline std::optional<drm_selector_e> drm_selector_from_help(std::string_view help) {
75 if (help_lists_option(help, "--prefer-drm")) {
77 }
78 if (help_lists_option(help, "--drm-device")) {
80 }
81 return std::nullopt;
82 }
83
89 inline std::optional<drm_selector_e> drm_selector_from_override(std::string_view value) {
90 if (value == "--prefer-drm" || value == "prefer-drm") {
92 }
93 if (value == "--drm-device" || value == "drm-device") {
95 }
96 if (value == "none") {
98 }
99 return std::nullopt;
100 }
101
107 inline bool is_primary_card_path(std::string_view path) {
108 constexpr std::string_view prefix {"/dev/dri/card"};
109 if (!path.starts_with(prefix) || path.size() == prefix.size()) {
110 return false;
111 }
112 for (const char ch : path.substr(prefix.size())) {
113 if (std::isdigit(static_cast<unsigned char>(ch)) == 0) {
114 return false;
115 }
116 }
117 return true;
118 }
119
129 inline bool steam_mode_for(std::string_view mode, std::string_view command) {
130 if (mode == "on") {
131 return true;
132 }
133 if (mode == "off") {
134 return false;
135 }
136 const auto begin = command.find_first_not_of(" \t");
137 if (begin == std::string_view::npos) {
138 return false;
139 }
140 auto program = command.substr(begin, command.find_first_of(" \t", begin) - begin);
141 if (const auto slash = program.rfind('/'); slash != std::string_view::npos) {
142 program.remove_prefix(slash + 1);
143 }
144 return program == "steam" || program == "hermes-gamescope-launch" || program == "apollo-gamescope-launch";
145 }
146
148 constexpr std::string_view default_args {"--expose-wayland --force-windows-fullscreen"};
149
150 // Call with the same parsed argument tokens run_command() will receive.
151 // A per-app option must not redirect an isolated compositor to the host's
152 // KMS card/backend, or replace the negotiated mode and connector.
153 inline std::optional<std::string> session_option_override(std::span<const std::string> arguments) {
154 constexpr std::string_view reserved[] {
155 "--backend",
156 "--prefer-drm",
157 "--drm-device",
158 "--output-width",
159 "--output-height",
160 "--nested-width",
161 "--nested-height",
162 "--nested-refresh",
163 "--prefer-output"
164 };
165 for (const auto &argument : arguments) {
166 const std::string_view option = std::string_view {argument}.substr(0, argument.find('='));
167 if (option == "--") {
168 return argument;
169 }
170 if (option.starts_with("--")) {
171 // getopt_long accepts a unique prefix as well as the full option.
172 for (const auto name : reserved) {
173 if (name.starts_with(option)) {
174 return argument;
175 }
176 }
177 } else if (argument.starts_with('-')) {
178 // GNU short options can be clustered (-eW800). Once an option takes
179 // a value the rest of that token is its value, e.g. -Ffsr, not flags.
180 for (const char flag : std::string_view {argument}.substr(1)) {
181 if (std::string_view {"WHwhrO"}.find(flag) != std::string_view::npos) {
182 return argument;
183 }
184 if (std::string_view {"mSFsoRTC"}.find(flag) != std::string_view::npos) {
185 break;
186 }
187 }
188 }
189 }
190 return std::nullopt;
191 }
192
193 struct launch_t {
194 std::string binary {"gamescope"};
195 int width = 1920;
196 int height = 1080;
197 int refresh_hz = 60;
198 std::string connector;
200 std::string scanout_card;
201 bool steam_mode = false;
202 std::string args;
203 std::string command;
204 };
205
225 inline std::string launch_command(const launch_t &launch) {
226 auto command = std::format(
227 "{} --backend=drm -W {} -H {} -w {} -h {} -r {}",
228 launch.binary.empty() ? std::string {"gamescope"} : launch.binary,
229 launch.width,
230 launch.height,
231 launch.width,
232 launch.height,
233 launch.refresh_hz
234 );
235 if (launch.selector != drm_selector_e::none && !launch.scanout_card.empty()) {
236 command += std::format(" {} {}", drm_selector_flag(launch.selector), launch.scanout_card);
237 }
238 command += launch.steam_mode ? " -e --xwayland-count 2" : " --xwayland-count 1";
239 command += " ";
240 command += launch.args.empty() ? std::string {default_args} : launch.args;
241 if (!launch.connector.empty()) {
242 command += " -O " + launch.connector;
243 }
244 command += " -- " + launch.command;
245 return command;
246 }
247
248} // namespace platf::gamescope
constexpr std::string_view default_args
The optional flags an application session gets when its app sets none.
@ drm_device
–drm-device, the shorter downstream patch reported in Hermes#57.
@ none
No option: the build scans out on the device it renders with.
@ prefer_drm
–prefer-drm, ValveSoftware/gamescope#2220; also read from GAMESCOPE_PREFER_DRM.
bool steam_mode_for(std::string_view mode, std::string_view command)
std::optional< drm_selector_e > drm_selector_from_help(std::string_view help)
bool is_primary_card_path(std::string_view path)
std::string launch_command(const launch_t &launch)
bool help_lists_option(std::string_view help, std::string_view option)
std::optional< drm_selector_e > drm_selector_from_override(std::string_view value)
Functions for handling command line arguments.
std::string connector
The session card's connector, e.g. Virtual-2.
std::string binary
The executable; an absolute path for Hermes' own build.
std::string command
What runs inside Gamescope, after –.
std::string scanout_card
The session card's primary node.