gamemode_client.h 6.6 KB

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