diff options
Diffstat (limited to 'externals')
| -rw-r--r-- | externals/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | externals/gamemode/CMakeLists.txt | 11 | ||||
| -rw-r--r-- | externals/gamemode/include/gamemode_client.h | 379 |
3 files changed, 394 insertions, 0 deletions
diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt index fc922c31b..070151bec 100644 --- a/externals/CMakeLists.txt +++ b/externals/CMakeLists.txt | |||
| @@ -189,6 +189,10 @@ if (ANDROID) | |||
| 189 | endif() | 189 | endif() |
| 190 | endif() | 190 | endif() |
| 191 | 191 | ||
| 192 | if (UNIX) | ||
| 193 | add_subdirectory(gamemode) | ||
| 194 | endif() | ||
| 195 | |||
| 192 | # Breakpad | 196 | # Breakpad |
| 193 | # https://github.com/microsoft/vcpkg/blob/master/ports/breakpad/CMakeLists.txt | 197 | # https://github.com/microsoft/vcpkg/blob/master/ports/breakpad/CMakeLists.txt |
| 194 | if (YUZU_CRASH_DUMPS AND NOT TARGET libbreakpad_client) | 198 | if (YUZU_CRASH_DUMPS AND NOT TARGET libbreakpad_client) |
diff --git a/externals/gamemode/CMakeLists.txt b/externals/gamemode/CMakeLists.txt new file mode 100644 index 000000000..87095642e --- /dev/null +++ b/externals/gamemode/CMakeLists.txt | |||
| @@ -0,0 +1,11 @@ | |||
| 1 | # SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project | ||
| 2 | # SPDX-License-Identifier: GPL-3.0-or-later | ||
| 3 | |||
| 4 | project(gamemode LANGUAGES CXX C) | ||
| 5 | |||
| 6 | add_library(gamemode include/gamemode_client.h) | ||
| 7 | |||
| 8 | target_link_libraries(gamemode PRIVATE common) | ||
| 9 | |||
| 10 | target_include_directories(gamemode PUBLIC include) | ||
| 11 | set_target_properties(gamemode PROPERTIES LINKER_LANGUAGE C) | ||
diff --git a/externals/gamemode/include/gamemode_client.h b/externals/gamemode/include/gamemode_client.h new file mode 100644 index 000000000..184812334 --- /dev/null +++ b/externals/gamemode/include/gamemode_client.h | |||
| @@ -0,0 +1,379 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2017-2019 Feral Interactive | ||
| 2 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 3 | |||
| 4 | /* | ||
| 5 | |||
| 6 | Copyright (c) 2017-2019, Feral Interactive | ||
| 7 | All rights reserved. | ||
| 8 | |||
| 9 | Redistribution and use in source and binary forms, with or without | ||
| 10 | modification, are permitted provided that the following conditions are met: | ||
| 11 | |||
| 12 | * Redistributions of source code must retain the above copyright notice, | ||
| 13 | this list of conditions and the following disclaimer. | ||
| 14 | * Redistributions in binary form must reproduce the above copyright | ||
| 15 | notice, this list of conditions and the following disclaimer in the | ||
| 16 | documentation and/or other materials provided with the distribution. | ||
| 17 | * Neither the name of Feral Interactive nor the names of its contributors | ||
| 18 | may be used to endorse or promote products derived from this software | ||
| 19 | without specific prior written permission. | ||
| 20 | |||
| 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 22 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
| 25 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 26 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 27 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 28 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 29 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 30 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 31 | POSSIBILITY OF SUCH DAMAGE. | ||
| 32 | |||
| 33 | */ | ||
| 34 | #ifndef CLIENT_GAMEMODE_H | ||
| 35 | #define CLIENT_GAMEMODE_H | ||
| 36 | /* | ||
| 37 | * GameMode supports the following client functions | ||
| 38 | * Requests are refcounted in the daemon | ||
| 39 | * | ||
| 40 | * int gamemode_request_start() - Request gamemode starts | ||
| 41 | * 0 if the request was sent successfully | ||
| 42 | * -1 if the request failed | ||
| 43 | * | ||
| 44 | * int gamemode_request_end() - Request gamemode ends | ||
| 45 | * 0 if the request was sent successfully | ||
| 46 | * -1 if the request failed | ||
| 47 | * | ||
| 48 | * GAMEMODE_AUTO can be defined to make the above two functions apply during static init and | ||
| 49 | * destruction, as appropriate. In this configuration, errors will be printed to stderr | ||
| 50 | * | ||
| 51 | * int gamemode_query_status() - Query the current status of gamemode | ||
| 52 | * 0 if gamemode is inactive | ||
| 53 | * 1 if gamemode is active | ||
| 54 | * 2 if gamemode is active and this client is registered | ||
| 55 | * -1 if the query failed | ||
| 56 | * | ||
| 57 | * int gamemode_request_start_for(pid_t pid) - Request gamemode starts for another process | ||
| 58 | * 0 if the request was sent successfully | ||
| 59 | * -1 if the request failed | ||
| 60 | * -2 if the request was rejected | ||
| 61 | * | ||
| 62 | * int gamemode_request_end_for(pid_t pid) - Request gamemode ends for another process | ||
| 63 | * 0 if the request was sent successfully | ||
| 64 | * -1 if the request failed | ||
| 65 | * -2 if the request was rejected | ||
| 66 | * | ||
| 67 | * int gamemode_query_status_for(pid_t pid) - Query status of gamemode for another process | ||
| 68 | * 0 if gamemode is inactive | ||
| 69 | * 1 if gamemode is active | ||
| 70 | * 2 if gamemode is active and this client is registered | ||
| 71 | * -1 if the query failed | ||
| 72 | * | ||
| 73 | * const char* gamemode_error_string() - Get an error string | ||
| 74 | * returns a string describing any of the above errors | ||
| 75 | * | ||
| 76 | * Note: All the above requests can be blocking - dbus requests can and will block while the daemon | ||
| 77 | * handles the request. It is not recommended to make these calls in performance critical code | ||
| 78 | */ | ||
| 79 | |||
| 80 | #include <stdbool.h> | ||
| 81 | #include <stdio.h> | ||
| 82 | |||
| 83 | #include <dlfcn.h> | ||
| 84 | #include <string.h> | ||
| 85 | |||
| 86 | #include <assert.h> | ||
| 87 | |||
| 88 | #include <sys/types.h> | ||
| 89 | |||
| 90 | static char internal_gamemode_client_error_string[512] = { 0 }; | ||
| 91 | |||
| 92 | /** | ||
| 93 | * Load libgamemode dynamically to dislodge us from most dependencies. | ||
| 94 | * This allows clients to link and/or use this regardless of runtime. | ||
| 95 | * See SDL2 for an example of the reasoning behind this in terms of | ||
| 96 | * dynamic versioning as well. | ||
| 97 | */ | ||
| 98 | static volatile int internal_libgamemode_loaded = 1; | ||
| 99 | |||
| 100 | /* Typedefs for the functions to load */ | ||
| 101 | typedef int (*api_call_return_int)(void); | ||
| 102 | typedef const char *(*api_call_return_cstring)(void); | ||
| 103 | typedef int (*api_call_pid_return_int)(pid_t); | ||
| 104 | |||
| 105 | /* Storage for functors */ | ||
| 106 | static api_call_return_int REAL_internal_gamemode_request_start = NULL; | ||
| 107 | static api_call_return_int REAL_internal_gamemode_request_end = NULL; | ||
| 108 | static api_call_return_int REAL_internal_gamemode_query_status = NULL; | ||
| 109 | static api_call_return_cstring REAL_internal_gamemode_error_string = NULL; | ||
| 110 | static api_call_pid_return_int REAL_internal_gamemode_request_start_for = NULL; | ||
| 111 | static api_call_pid_return_int REAL_internal_gamemode_request_end_for = NULL; | ||
| 112 | static api_call_pid_return_int REAL_internal_gamemode_query_status_for = NULL; | ||
| 113 | |||
| 114 | /** | ||
| 115 | * Internal helper to perform the symbol binding safely. | ||
| 116 | * | ||
| 117 | * Returns 0 on success and -1 on failure | ||
| 118 | */ | ||
| 119 | __attribute__((always_inline)) static inline int internal_bind_libgamemode_symbol( | ||
| 120 | void *handle, const char *name, void **out_func, size_t func_size, bool required) | ||
| 121 | { | ||
| 122 | void *symbol_lookup = NULL; | ||
| 123 | char *dl_error = NULL; | ||
| 124 | |||
| 125 | /* Safely look up the symbol */ | ||
| 126 | symbol_lookup = dlsym(handle, name); | ||
| 127 | dl_error = dlerror(); | ||
| 128 | if (required && (dl_error || !symbol_lookup)) { | ||
| 129 | snprintf(internal_gamemode_client_error_string, | ||
| 130 | sizeof(internal_gamemode_client_error_string), | ||
| 131 | "dlsym failed - %s", | ||
| 132 | dl_error); | ||
| 133 | return -1; | ||
| 134 | } | ||
| 135 | |||
| 136 | /* Have the symbol correctly, copy it to make it usable */ | ||
| 137 | memcpy(out_func, &symbol_lookup, func_size); | ||
| 138 | return 0; | ||
| 139 | } | ||
| 140 | |||
| 141 | /** | ||
| 142 | * Loads libgamemode and needed functions | ||
| 143 | * | ||
| 144 | * Returns 0 on success and -1 on failure | ||
| 145 | */ | ||
| 146 | __attribute__((always_inline)) static inline int internal_load_libgamemode(void) | ||
| 147 | { | ||
| 148 | /* We start at 1, 0 is a success and -1 is a fail */ | ||
| 149 | if (internal_libgamemode_loaded != 1) { | ||
| 150 | return internal_libgamemode_loaded; | ||
| 151 | } | ||
| 152 | |||
| 153 | /* Anonymous struct type to define our bindings */ | ||
| 154 | struct binding { | ||
| 155 | const char *name; | ||
| 156 | void **functor; | ||
| 157 | size_t func_size; | ||
| 158 | bool required; | ||
| 159 | } bindings[] = { | ||
| 160 | { "real_gamemode_request_start", | ||
| 161 | (void **)&REAL_internal_gamemode_request_start, | ||
| 162 | sizeof(REAL_internal_gamemode_request_start), | ||
| 163 | true }, | ||
| 164 | { "real_gamemode_request_end", | ||
| 165 | (void **)&REAL_internal_gamemode_request_end, | ||
| 166 | sizeof(REAL_internal_gamemode_request_end), | ||
| 167 | true }, | ||
| 168 | { "real_gamemode_query_status", | ||
| 169 | (void **)&REAL_internal_gamemode_query_status, | ||
| 170 | sizeof(REAL_internal_gamemode_query_status), | ||
| 171 | false }, | ||
| 172 | { "real_gamemode_error_string", | ||
| 173 | (void **)&REAL_internal_gamemode_error_string, | ||
| 174 | sizeof(REAL_internal_gamemode_error_string), | ||
| 175 | true }, | ||
| 176 | { "real_gamemode_request_start_for", | ||
| 177 | (void **)&REAL_internal_gamemode_request_start_for, | ||
| 178 | sizeof(REAL_internal_gamemode_request_start_for), | ||
| 179 | false }, | ||
| 180 | { "real_gamemode_request_end_for", | ||
| 181 | (void **)&REAL_internal_gamemode_request_end_for, | ||
| 182 | sizeof(REAL_internal_gamemode_request_end_for), | ||
| 183 | false }, | ||
| 184 | { "real_gamemode_query_status_for", | ||
| 185 | (void **)&REAL_internal_gamemode_query_status_for, | ||
| 186 | sizeof(REAL_internal_gamemode_query_status_for), | ||
| 187 | false }, | ||
| 188 | }; | ||
| 189 | |||
| 190 | void *libgamemode = NULL; | ||
| 191 | |||
| 192 | /* Try and load libgamemode */ | ||
| 193 | libgamemode = dlopen("libgamemode.so.0", RTLD_NOW); | ||
| 194 | if (!libgamemode) { | ||
| 195 | /* Attempt to load unversioned library for compatibility with older | ||
| 196 | * versions (as of writing, there are no ABI changes between the two - | ||
| 197 | * this may need to change if ever ABI-breaking changes are made) */ | ||
| 198 | libgamemode = dlopen("libgamemode.so", RTLD_NOW); | ||
| 199 | if (!libgamemode) { | ||
| 200 | snprintf(internal_gamemode_client_error_string, | ||
| 201 | sizeof(internal_gamemode_client_error_string), | ||
| 202 | "dlopen failed - %s", | ||
| 203 | dlerror()); | ||
| 204 | internal_libgamemode_loaded = -1; | ||
| 205 | return -1; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | /* Attempt to bind all symbols */ | ||
| 210 | for (size_t i = 0; i < sizeof(bindings) / sizeof(bindings[0]); i++) { | ||
| 211 | struct binding *binder = &bindings[i]; | ||
| 212 | |||
| 213 | if (internal_bind_libgamemode_symbol(libgamemode, | ||
| 214 | binder->name, | ||
| 215 | binder->functor, | ||
| 216 | binder->func_size, | ||
| 217 | binder->required)) { | ||
| 218 | internal_libgamemode_loaded = -1; | ||
| 219 | return -1; | ||
| 220 | }; | ||
| 221 | } | ||
| 222 | |||
| 223 | /* Success */ | ||
| 224 | internal_libgamemode_loaded = 0; | ||
| 225 | return 0; | ||
| 226 | } | ||
| 227 | |||
| 228 | /** | ||
| 229 | * Redirect to the real libgamemode | ||
| 230 | */ | ||
| 231 | __attribute__((always_inline)) static inline const char *gamemode_error_string(void) | ||
| 232 | { | ||
| 233 | /* If we fail to load the system gamemode, or we have an error string already, return our error | ||
| 234 | * string instead of diverting to the system version */ | ||
| 235 | if (internal_load_libgamemode() < 0 || internal_gamemode_client_error_string[0] != '\0') { | ||
| 236 | return internal_gamemode_client_error_string; | ||
| 237 | } | ||
| 238 | |||
| 239 | /* Assert for static analyser that the function is not NULL */ | ||
| 240 | assert(REAL_internal_gamemode_error_string != NULL); | ||
| 241 | |||
| 242 | return REAL_internal_gamemode_error_string(); | ||
| 243 | } | ||
| 244 | |||
| 245 | /** | ||
| 246 | * Redirect to the real libgamemode | ||
| 247 | * Allow automatically requesting game mode | ||
| 248 | * Also prints errors as they happen. | ||
| 249 | */ | ||
| 250 | #ifdef GAMEMODE_AUTO | ||
| 251 | __attribute__((constructor)) | ||
| 252 | #else | ||
| 253 | __attribute__((always_inline)) static inline | ||
| 254 | #endif | ||
| 255 | int gamemode_request_start(void) | ||
| 256 | { | ||
| 257 | /* Need to load gamemode */ | ||
| 258 | if (internal_load_libgamemode() < 0) { | ||
| 259 | #ifdef GAMEMODE_AUTO | ||
| 260 | fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string()); | ||
| 261 | #endif | ||
| 262 | return -1; | ||
| 263 | } | ||
| 264 | |||
| 265 | /* Assert for static analyser that the function is not NULL */ | ||
| 266 | assert(REAL_internal_gamemode_request_start != NULL); | ||
| 267 | |||
| 268 | if (REAL_internal_gamemode_request_start() < 0) { | ||
| 269 | #ifdef GAMEMODE_AUTO | ||
| 270 | fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string()); | ||
| 271 | #endif | ||
| 272 | return -1; | ||
| 273 | } | ||
| 274 | |||
| 275 | return 0; | ||
| 276 | } | ||
| 277 | |||
| 278 | /* Redirect to the real libgamemode */ | ||
| 279 | #ifdef GAMEMODE_AUTO | ||
| 280 | __attribute__((destructor)) | ||
| 281 | #else | ||
| 282 | __attribute__((always_inline)) static inline | ||
| 283 | #endif | ||
| 284 | int gamemode_request_end(void) | ||
| 285 | { | ||
| 286 | /* Need to load gamemode */ | ||
| 287 | if (internal_load_libgamemode() < 0) { | ||
| 288 | #ifdef GAMEMODE_AUTO | ||
| 289 | fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string()); | ||
| 290 | #endif | ||
| 291 | return -1; | ||
| 292 | } | ||
| 293 | |||
| 294 | /* Assert for static analyser that the function is not NULL */ | ||
| 295 | assert(REAL_internal_gamemode_request_end != NULL); | ||
| 296 | |||
| 297 | if (REAL_internal_gamemode_request_end() < 0) { | ||
| 298 | #ifdef GAMEMODE_AUTO | ||
| 299 | fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string()); | ||
| 300 | #endif | ||
| 301 | return -1; | ||
| 302 | } | ||
| 303 | |||
| 304 | return 0; | ||
| 305 | } | ||
| 306 | |||
| 307 | /* Redirect to the real libgamemode */ | ||
| 308 | __attribute__((always_inline)) static inline int gamemode_query_status(void) | ||
| 309 | { | ||
| 310 | /* Need to load gamemode */ | ||
| 311 | if (internal_load_libgamemode() < 0) { | ||
| 312 | return -1; | ||
| 313 | } | ||
| 314 | |||
| 315 | if (REAL_internal_gamemode_query_status == NULL) { | ||
| 316 | snprintf(internal_gamemode_client_error_string, | ||
| 317 | sizeof(internal_gamemode_client_error_string), | ||
| 318 | "gamemode_query_status missing (older host?)"); | ||
| 319 | return -1; | ||
| 320 | } | ||
| 321 | |||
| 322 | return REAL_internal_gamemode_query_status(); | ||
| 323 | } | ||
| 324 | |||
| 325 | /* Redirect to the real libgamemode */ | ||
| 326 | __attribute__((always_inline)) static inline int gamemode_request_start_for(pid_t pid) | ||
| 327 | { | ||
| 328 | /* Need to load gamemode */ | ||
| 329 | if (internal_load_libgamemode() < 0) { | ||
| 330 | return -1; | ||
| 331 | } | ||
| 332 | |||
| 333 | if (REAL_internal_gamemode_request_start_for == NULL) { | ||
| 334 | snprintf(internal_gamemode_client_error_string, | ||
| 335 | sizeof(internal_gamemode_client_error_string), | ||
| 336 | "gamemode_request_start_for missing (older host?)"); | ||
| 337 | return -1; | ||
| 338 | } | ||
| 339 | |||
| 340 | return REAL_internal_gamemode_request_start_for(pid); | ||
| 341 | } | ||
| 342 | |||
| 343 | /* Redirect to the real libgamemode */ | ||
| 344 | __attribute__((always_inline)) static inline int gamemode_request_end_for(pid_t pid) | ||
| 345 | { | ||
| 346 | /* Need to load gamemode */ | ||
| 347 | if (internal_load_libgamemode() < 0) { | ||
| 348 | return -1; | ||
| 349 | } | ||
| 350 | |||
| 351 | if (REAL_internal_gamemode_request_end_for == NULL) { | ||
| 352 | snprintf(internal_gamemode_client_error_string, | ||
| 353 | sizeof(internal_gamemode_client_error_string), | ||
| 354 | "gamemode_request_end_for missing (older host?)"); | ||
| 355 | return -1; | ||
| 356 | } | ||
| 357 | |||
| 358 | return REAL_internal_gamemode_request_end_for(pid); | ||
| 359 | } | ||
| 360 | |||
| 361 | /* Redirect to the real libgamemode */ | ||
| 362 | __attribute__((always_inline)) static inline int gamemode_query_status_for(pid_t pid) | ||
| 363 | { | ||
| 364 | /* Need to load gamemode */ | ||
| 365 | if (internal_load_libgamemode() < 0) { | ||
| 366 | return -1; | ||
| 367 | } | ||
| 368 | |||
| 369 | if (REAL_internal_gamemode_query_status_for == NULL) { | ||
| 370 | snprintf(internal_gamemode_client_error_string, | ||
| 371 | sizeof(internal_gamemode_client_error_string), | ||
| 372 | "gamemode_query_status_for missing (older host?)"); | ||
| 373 | return -1; | ||
| 374 | } | ||
| 375 | |||
| 376 | return REAL_internal_gamemode_query_status_for(pid); | ||
| 377 | } | ||
| 378 | |||
| 379 | #endif // CLIENT_GAMEMODE_H | ||