gamemode_client.h 12 KB

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