gamemode_client.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. Copyright (c) 2017-2018, 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. * int gamemode_query_status() - Query the current status of gamemode
  41. * 0 if gamemode is inactive
  42. * 1 if gamemode is active
  43. * 2 if gamemode is active and this client is registered
  44. * -1 if the query failed
  45. *
  46. * const char* gamemode_error_string() - Get an error string
  47. * returns a string describing any of the above errors
  48. */
  49. #include <stdbool.h>
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <unistd.h>
  53. #include <dlfcn.h>
  54. #include <errno.h>
  55. #include <string.h>
  56. static char internal_gamemode_client_error_string[512] = { 0 };
  57. /**
  58. * Load libgamemode dynamically to dislodge us from most dependencies.
  59. * This allows clients to link and/or use this regardless of runtime.
  60. * See SDL2 for an example of the reasoning behind this in terms of
  61. * dynamic versioning as well.
  62. */
  63. static volatile int internal_libgamemode_loaded = 1;
  64. /* Typedefs for the functions to load */
  65. typedef int (*internal_gamemode_request_start)(void);
  66. typedef int (*internal_gamemode_request_end)(void);
  67. typedef int (*internal_gamemode_query_status)(void);
  68. typedef const char *(*internal_gamemode_error_string)(void);
  69. /* Storage for functors */
  70. static internal_gamemode_request_start REAL_internal_gamemode_request_start = NULL;
  71. static internal_gamemode_request_end REAL_internal_gamemode_request_end = NULL;
  72. static internal_gamemode_query_status REAL_internal_gamemode_query_status = NULL;
  73. static internal_gamemode_error_string REAL_internal_gamemode_error_string = NULL;
  74. /**
  75. * Internal helper to perform the symbol binding safely.
  76. *
  77. * Returns 0 on success and -1 on failure
  78. */
  79. __attribute__((always_inline)) static inline int internal_bind_libgamemode_symbol(void *handle,
  80. const char *name,
  81. void **out_func,
  82. size_t func_size)
  83. {
  84. void *symbol_lookup = NULL;
  85. char *dl_error = NULL;
  86. /* Safely look up the symbol */
  87. symbol_lookup = dlsym(handle, name);
  88. dl_error = dlerror();
  89. if (dl_error || !symbol_lookup) {
  90. snprintf(internal_gamemode_client_error_string,
  91. sizeof(internal_gamemode_client_error_string),
  92. "dlsym failed - %s",
  93. dl_error);
  94. return -1;
  95. }
  96. /* Have the symbol correctly, copy it to make it usable */
  97. memcpy(out_func, &symbol_lookup, func_size);
  98. return 0;
  99. }
  100. /**
  101. * Loads libgamemode and needed functions
  102. *
  103. * Returns 0 on success and -1 on failure
  104. */
  105. __attribute__((always_inline)) static inline int internal_load_libgamemode(void)
  106. {
  107. /* We start at 1, 0 is a success and -1 is a fail */
  108. if (internal_libgamemode_loaded != 1) {
  109. return internal_libgamemode_loaded;
  110. }
  111. /* Anonymous struct type to define our bindings */
  112. struct binding {
  113. const char *name;
  114. void **functor;
  115. size_t func_size;
  116. bool required;
  117. } bindings[] = {
  118. { "real_gamemode_request_start",
  119. (void **)&REAL_internal_gamemode_request_start,
  120. sizeof(REAL_internal_gamemode_request_start),
  121. true },
  122. { "real_gamemode_request_end",
  123. (void **)&REAL_internal_gamemode_request_end,
  124. sizeof(REAL_internal_gamemode_request_end),
  125. true },
  126. { "real_gamemode_query_status",
  127. (void **)&REAL_internal_gamemode_query_status,
  128. sizeof(REAL_internal_gamemode_query_status),
  129. false },
  130. { "real_gamemode_error_string",
  131. (void **)&REAL_internal_gamemode_error_string,
  132. sizeof(REAL_internal_gamemode_error_string),
  133. true },
  134. };
  135. void *libgamemode = NULL;
  136. /* Try and load libgamemode */
  137. libgamemode = dlopen("libgamemode.so.0", RTLD_NOW);
  138. if (!libgamemode) {
  139. /* Attempt to load unversioned library for compatibility with older
  140. * versions (as of writing, there are no ABI changes between the two -
  141. * this may need to change if ever ABI-breaking changes are made) */
  142. libgamemode = dlopen("libgamemode.so", RTLD_NOW);
  143. if (!libgamemode) {
  144. snprintf(internal_gamemode_client_error_string,
  145. sizeof(internal_gamemode_client_error_string),
  146. "dlopen failed - %s",
  147. dlerror());
  148. internal_libgamemode_loaded = -1;
  149. return -1;
  150. }
  151. }
  152. /* Attempt to bind all symbols */
  153. for (size_t i = 0; i < sizeof(bindings) / sizeof(bindings[0]); i++) {
  154. struct binding *binder = &bindings[i];
  155. if (internal_bind_libgamemode_symbol(libgamemode,
  156. binder->name,
  157. binder->functor,
  158. binder->func_size) != 0 &&
  159. binder->required) {
  160. internal_libgamemode_loaded = -1;
  161. return -1;
  162. };
  163. }
  164. /* Success */
  165. internal_libgamemode_loaded = 0;
  166. return 0;
  167. }
  168. /**
  169. * Redirect to the real libgamemode
  170. */
  171. __attribute__((always_inline)) static inline const char *gamemode_error_string(void)
  172. {
  173. /* If we fail to load the system gamemode, or we have an error string already, return our error
  174. * string instead of diverting to the system version */
  175. if (internal_load_libgamemode() < 0 || internal_gamemode_client_error_string[0] != '\0') {
  176. return internal_gamemode_client_error_string;
  177. }
  178. return REAL_internal_gamemode_error_string();
  179. }
  180. /**
  181. * Redirect to the real libgamemod
  182. * Allow automatically requesting game mode
  183. * Also prints errors as they happen.
  184. */
  185. #ifdef GAMEMODE_AUTO
  186. __attribute__((constructor))
  187. #else
  188. __attribute__((always_inline)) static inline
  189. #endif
  190. int gamemode_request_start(void)
  191. {
  192. /* Need to load gamemode */
  193. if (internal_load_libgamemode() < 0) {
  194. #ifdef GAMEMODE_AUTO
  195. fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
  196. #endif
  197. return -1;
  198. }
  199. if (REAL_internal_gamemode_request_start() < 0) {
  200. #ifdef GAMEMODE_AUTO
  201. fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
  202. #endif
  203. return -1;
  204. }
  205. return 0;
  206. }
  207. /* Redirect to the real libgamemode */
  208. #ifdef GAMEMODE_AUTO
  209. __attribute__((destructor))
  210. #else
  211. __attribute__((always_inline)) static inline
  212. #endif
  213. int gamemode_request_end(void)
  214. {
  215. /* Need to load gamemode */
  216. if (internal_load_libgamemode() < 0) {
  217. #ifdef GAMEMODE_AUTO
  218. fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
  219. #endif
  220. return -1;
  221. }
  222. if (REAL_internal_gamemode_request_end() < 0) {
  223. #ifdef GAMEMODE_AUTO
  224. fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
  225. #endif
  226. return -1;
  227. }
  228. return 0;
  229. }
  230. /* Redirect to the real libgamemode */
  231. __attribute__((always_inline)) static inline int gamemode_query_status(void)
  232. {
  233. /* Need to load gamemode */
  234. if (internal_load_libgamemode() < 0) {
  235. return -1;
  236. }
  237. if (REAL_internal_gamemode_query_status == NULL) {
  238. snprintf(internal_gamemode_client_error_string,
  239. sizeof(internal_gamemode_client_error_string),
  240. "gamemode_query_status missing (older host?)");
  241. return -1;
  242. }
  243. return REAL_internal_gamemode_query_status();
  244. }
  245. #endif // CLIENT_GAMEMODE_H