gamemode_client.h 7.8 KB

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