gamemode_client.h 11 KB

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