Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
window_placement.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <algorithm>
8#include <cctype>
9#include <cstdint>
10#include <filesystem>
11#include <string>
12#include <string_view>
13#include <vector>
14
15namespace platf::window_placement {
16
17 // Arguments have already been unquoted by the same split_unix() parser used
18 // for application commands elsewhere in process.cpp. Never execute a shell
19 // just to find out what an application entry launches.
20 inline bool names_program(const std::vector<std::string> &arguments, std::string_view program) {
21 return std::any_of(arguments.begin(), arguments.end(), [program](const auto &argument) {
22 return std::filesystem::path {argument}.filename() == program;
23 });
24 }
25
26 inline bool opens_big_picture(const std::vector<std::string> &arguments) {
27 const bool steam = names_program(arguments, "steam") ||
28 (names_program(arguments, "flatpak") &&
29 std::find(arguments.begin(), arguments.end(), "com.valvesoftware.Steam") != arguments.end());
30 return steam && std::any_of(arguments.begin(), arguments.end(), [](std::string_view argument) {
31 return argument == "steam://open/bigpicture" || argument.starts_with("steam://open/bigpicture?") ||
32 argument == "-gamepadui" || argument == "-bigpicture" || argument == "-tenfoot";
33 });
34 }
35
36 inline bool launches_gamescope(const std::vector<std::string> &arguments) {
37 return names_program(arguments, "gamescope") || names_program(arguments, "hermes-gamescope-launch");
38 }
39
49 inline bool gamescope_runs_steam(const std::vector<std::string> &arguments) {
50 if (names_program(arguments, "hermes-gamescope-launch")) {
51 return true;
52 }
53 return names_program(arguments, "gamescope") &&
54 (names_program(arguments, "steam") ||
55 std::find(arguments.begin(), arguments.end(), "com.valvesoftware.Steam") != arguments.end());
56 }
57
65 inline bool launches_steam(const std::vector<std::string> &arguments) {
66 return names_program(arguments, "steam") ||
67 std::find(arguments.begin(), arguments.end(), "com.valvesoftware.Steam") != arguments.end() ||
68 std::any_of(arguments.begin(), arguments.end(), [](std::string_view argument) {
69 return argument.starts_with("steam://");
70 }) ||
71 gamescope_runs_steam(arguments);
72 }
73
74 inline std::string kwin_script(const std::string &output, const std::vector<std::string> &classes, const std::vector<std::uint32_t> &gamescope_pids) {
75 // Both names become JavaScript string literals. Restrict them before
76 // interpolation, including when this helper is called outside VDISPLAY.
77 if (output.empty() || output.size() > 64 || classes.empty() ||
78 !std::all_of(output.begin(), output.end(), [](unsigned char c) {
79 return std::isalnum(c) || c == '-' || c == '_';
80 })) {
81 return {};
82 }
83 std::string class_list;
84 for (const auto &window_class : classes) {
85 if (window_class.empty() || window_class.find_first_not_of("abcdefghijklmnopqrstuvwxyz0123456789.-_") != std::string::npos) {
86 return {};
87 }
88 class_list += (class_list.empty() ? "\"" : ", \"") + window_class + '"';
89 }
90 std::string pid_list;
91 for (const auto pid : gamescope_pids) {
92 if (pid > 1) {
93 pid_list += (pid_list.empty() ? "" : ", ") + std::to_string(pid);
94 }
95 }
96
97 return "// Hermes: position only the streamed application's windows.\n"
98 "const output = workspace.screens.find(screen => screen.name === \"" + output + "\");\n"
99 "const classes = [" + class_list + "];\n"
100 "const gamescopePids = [" + pid_list + "];\n"
101 "function place(window) {\n"
102 " if (!output || !window.normalWindow || !classes.includes(window.resourceClass) || window.output === output) {\n"
103 " return;\n"
104 " }\n"
105 " // Other Gamescope instances belong to the local desktop. The shipped\n"
106 " // launcher execs Gamescope, preserving the PID Hermes started. Custom\n"
107 " // launchers that fork it instead are left alone, never matched by class.\n"
108 " if (window.resourceClass === \"gamescope\" && !gamescopePids.includes(window.pid)) {\n"
109 " return;\n"
110 " }\n"
111 " const wasFullScreen = window.fullScreen;\n"
112 " if (wasFullScreen) {\n"
113 " window.fullScreen = false;\n"
114 " }\n"
115 " workspace.sendClientToScreen(window, output);\n"
116 " if (wasFullScreen) {\n"
117 " window.fullScreen = true;\n"
118 " }\n"
119 "}\n"
120 "// A client can still pick its own output when it goes fullscreen. Steam\n"
121 "// opens Big Picture, is moved here, moves itself back to the primary\n"
122 "// monitor and goes fullscreen there; place it again once it has.\n"
123 "function watch(window) {\n"
124 " place(window);\n"
125 " if (window.fullScreenChanged) {\n"
126 " window.fullScreenChanged.connect(() => {\n"
127 " if (window.fullScreen) {\n"
128 " place(window);\n"
129 " }\n"
130 " });\n"
131 " }\n"
132 "}\n"
133 "workspace.windowList().forEach(watch);\n"
134 "workspace.windowAdded.connect(watch);\n";
135 }
136} // namespace platf::window_placement
bool gamescope_runs_steam(const std::vector< std::string > &arguments)
bool launches_steam(const std::vector< std::string > &arguments)