gamemode_client.h 12 KB

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