gamemode_client.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 <sys/types.h>
  76. static char internal_gamemode_client_error_string[512] = { 0 };
  77. /**
  78. * Load libgamemode dynamically to dislodge us from most dependencies.
  79. * This allows clients to link and/or use this regardless of runtime.
  80. * See SDL2 for an example of the reasoning behind this in terms of
  81. * dynamic versioning as well.
  82. */
  83. static volatile int internal_libgamemode_loaded = 1;
  84. /* Typedefs for the functions to load */
  85. typedef int (*api_call_return_int)(void);
  86. typedef const char *(*api_call_return_cstring)(void);
  87. typedef int (*api_call_pid_return_int)(pid_t);
  88. /* Storage for functors */
  89. static api_call_return_int REAL_internal_gamemode_request_start = NULL;
  90. static api_call_return_int REAL_internal_gamemode_request_end = NULL;
  91. static api_call_return_int REAL_internal_gamemode_query_status = NULL;
  92. static api_call_return_cstring REAL_internal_gamemode_error_string = NULL;
  93. static api_call_pid_return_int REAL_internal_gamemode_request_start_for = NULL;
  94. static api_call_pid_return_int REAL_internal_gamemode_request_end_for = NULL;
  95. static api_call_pid_return_int REAL_internal_gamemode_query_status_for = NULL;
  96. /**
  97. * Internal helper to perform the symbol binding safely.
  98. *
  99. * Returns 0 on success and -1 on failure
  100. */
  101. __attribute__((always_inline)) static inline int internal_bind_libgamemode_symbol(
  102. void *handle, const char *name, void **out_func, size_t func_size, bool required)
  103. {
  104. void *symbol_lookup = NULL;
  105. char *dl_error = NULL;
  106. /* Safely look up the symbol */
  107. symbol_lookup = dlsym(handle, name);
  108. dl_error = dlerror();
  109. if (required && (dl_error || !symbol_lookup)) {
  110. snprintf(internal_gamemode_client_error_string,
  111. sizeof(internal_gamemode_client_error_string),
  112. "dlsym failed - %s",
  113. dl_error);
  114. return -1;
  115. }
  116. /* Have the symbol correctly, copy it to make it usable */
  117. memcpy(out_func, &symbol_lookup, func_size);
  118. return 0;
  119. }
  120. /**
  121. * Loads libgamemode and needed functions
  122. *
  123. * Returns 0 on success and -1 on failure
  124. */
  125. __attribute__((always_inline)) static inline int internal_load_libgamemode(void)
  126. {
  127. /* We start at 1, 0 is a success and -1 is a fail */
  128. if (internal_libgamemode_loaded != 1) {
  129. return internal_libgamemode_loaded;
  130. }
  131. /* Anonymous struct type to define our bindings */
  132. struct binding {
  133. const char *name;
  134. void **functor;
  135. size_t func_size;
  136. bool required;
  137. } bindings[] = {
  138. { "real_gamemode_request_start",
  139. (void **)&REAL_internal_gamemode_request_start,
  140. sizeof(REAL_internal_gamemode_request_start),
  141. true },
  142. { "real_gamemode_request_end",
  143. (void **)&REAL_internal_gamemode_request_end,
  144. sizeof(REAL_internal_gamemode_request_end),
  145. true },
  146. { "real_gamemode_query_status",
  147. (void **)&REAL_internal_gamemode_query_status,
  148. sizeof(REAL_internal_gamemode_query_status),
  149. false },
  150. { "real_gamemode_error_string",
  151. (void **)&REAL_internal_gamemode_error_string,
  152. sizeof(REAL_internal_gamemode_error_string),
  153. true },
  154. { "real_gamemode_request_start_for",
  155. (void **)&REAL_internal_gamemode_request_start_for,
  156. sizeof(REAL_internal_gamemode_request_start_for),
  157. false },
  158. { "real_gamemode_request_end_for",
  159. (void **)&REAL_internal_gamemode_request_end_for,
  160. sizeof(REAL_internal_gamemode_request_end_for),
  161. false },
  162. { "real_gamemode_query_status_for",
  163. (void **)&REAL_internal_gamemode_query_status_for,
  164. sizeof(REAL_internal_gamemode_query_status_for),
  165. false },
  166. };
  167. void *libgamemode = NULL;
  168. /* Try and load libgamemode */
  169. libgamemode = dlopen("libgamemode.so.0", RTLD_NOW);
  170. if (!libgamemode) {
  171. /* Attempt to load unversioned library for compatibility with older
  172. * versions (as of writing, there are no ABI changes between the two -
  173. * this may need to change if ever ABI-breaking changes are made) */
  174. libgamemode = dlopen("libgamemode.so", RTLD_NOW);
  175. if (!libgamemode) {
  176. snprintf(internal_gamemode_client_error_string,
  177. sizeof(internal_gamemode_client_error_string),
  178. "dlopen failed - %s",
  179. dlerror());
  180. internal_libgamemode_loaded = -1;
  181. return -1;
  182. }
  183. }
  184. /* Attempt to bind all symbols */
  185. for (size_t i = 0; i < sizeof(bindings) / sizeof(bindings[0]); i++) {
  186. struct binding *binder = &bindings[i];
  187. if (internal_bind_libgamemode_symbol(libgamemode,
  188. binder->name,
  189. binder->functor,
  190. binder->func_size,
  191. binder->required)) {
  192. internal_libgamemode_loaded = -1;
  193. return -1;
  194. };
  195. }
  196. /* Success */
  197. internal_libgamemode_loaded = 0;
  198. return 0;
  199. }
  200. /**
  201. * Redirect to the real libgamemode
  202. */
  203. __attribute__((always_inline)) static inline const char *gamemode_error_string(void)
  204. {
  205. /* If we fail to load the system gamemode, or we have an error string already, return our error
  206. * string instead of diverting to the system version */
  207. if (internal_load_libgamemode() < 0 || internal_gamemode_client_error_string[0] != '\0') {
  208. return internal_gamemode_client_error_string;
  209. }
  210. return REAL_internal_gamemode_error_string();
  211. }
  212. /**
  213. * Redirect to the real libgamemode
  214. * Allow automatically requesting game mode
  215. * Also prints errors as they happen.
  216. */
  217. #ifdef GAMEMODE_AUTO
  218. __attribute__((constructor))
  219. #else
  220. __attribute__((always_inline)) static inline
  221. #endif
  222. int gamemode_request_start(void)
  223. {
  224. /* Need to load gamemode */
  225. if (internal_load_libgamemode() < 0) {
  226. #ifdef GAMEMODE_AUTO
  227. fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
  228. #endif
  229. return -1;
  230. }
  231. if (REAL_internal_gamemode_request_start() < 0) {
  232. #ifdef GAMEMODE_AUTO
  233. fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
  234. #endif
  235. return -1;
  236. }
  237. return 0;
  238. }
  239. /* Redirect to the real libgamemode */
  240. #ifdef GAMEMODE_AUTO
  241. __attribute__((destructor))
  242. #else
  243. __attribute__((always_inline)) static inline
  244. #endif
  245. int gamemode_request_end(void)
  246. {
  247. /* Need to load gamemode */
  248. if (internal_load_libgamemode() < 0) {
  249. #ifdef GAMEMODE_AUTO
  250. fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
  251. #endif
  252. return -1;
  253. }
  254. if (REAL_internal_gamemode_request_end() < 0) {
  255. #ifdef GAMEMODE_AUTO
  256. fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
  257. #endif
  258. return -1;
  259. }
  260. return 0;
  261. }
  262. /* Redirect to the real libgamemode */
  263. __attribute__((always_inline)) static inline int gamemode_query_status(void)
  264. {
  265. /* Need to load gamemode */
  266. if (internal_load_libgamemode() < 0) {
  267. return -1;
  268. }
  269. if (REAL_internal_gamemode_query_status == NULL) {
  270. snprintf(internal_gamemode_client_error_string,
  271. sizeof(internal_gamemode_client_error_string),
  272. "gamemode_query_status missing (older host?)");
  273. return -1;
  274. }
  275. return REAL_internal_gamemode_query_status();
  276. }
  277. /* Redirect to the real libgamemode */
  278. __attribute__((always_inline)) static inline int gamemode_request_start_for(pid_t pid)
  279. {
  280. /* Need to load gamemode */
  281. if (internal_load_libgamemode() < 0) {
  282. return -1;
  283. }
  284. if (REAL_internal_gamemode_request_start_for == NULL) {
  285. snprintf(internal_gamemode_client_error_string,
  286. sizeof(internal_gamemode_client_error_string),
  287. "gamemode_request_start_for missing (older host?)");
  288. return -1;
  289. }
  290. return REAL_internal_gamemode_request_start_for(pid);
  291. }
  292. /* Redirect to the real libgamemode */
  293. __attribute__((always_inline)) static inline int gamemode_request_end_for(pid_t pid)
  294. {
  295. /* Need to load gamemode */
  296. if (internal_load_libgamemode() < 0) {
  297. return -1;
  298. }
  299. if (REAL_internal_gamemode_request_end_for == NULL) {
  300. snprintf(internal_gamemode_client_error_string,
  301. sizeof(internal_gamemode_client_error_string),
  302. "gamemode_request_end_for missing (older host?)");
  303. return -1;
  304. }
  305. return REAL_internal_gamemode_request_end_for(pid);
  306. }
  307. /* Redirect to the real libgamemode */
  308. __attribute__((always_inline)) static inline int gamemode_query_status_for(pid_t pid)
  309. {
  310. /* Need to load gamemode */
  311. if (internal_load_libgamemode() < 0) {
  312. return -1;
  313. }
  314. if (REAL_internal_gamemode_query_status_for == NULL) {
  315. snprintf(internal_gamemode_client_error_string,
  316. sizeof(internal_gamemode_client_error_string),
  317. "gamemode_query_status_for missing (older host?)");
  318. return -1;
  319. }
  320. return REAL_internal_gamemode_query_status_for(pid);
  321. }
  322. #endif // CLIENT_GAMEMODE_H