gamemode_client.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. char _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. volatile int _libgamemode_loaded = 1;
  42. /* Typedefs for the functions to load */
  43. typedef int (*_gamemode_request_start)(void);
  44. typedef int (*_gamemode_request_end)(void);
  45. typedef const char *(*_gamemode_error_string)(void);
  46. /* Storage for functors */
  47. _gamemode_request_start _REAL_gamemode_request_start = NULL;
  48. _gamemode_request_end _REAL_gamemode_request_end = NULL;
  49. _gamemode_error_string _REAL_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)) inline int _bind_libgamemode_symbol(void *handle, const char *name,
  56. void **out_func,
  57. size_t func_size)
  58. {
  59. void *symbol_lookup = NULL;
  60. char *dl_error = NULL;
  61. /* Safely look up the symbol */
  62. symbol_lookup = dlsym(handle, name);
  63. dl_error = dlerror();
  64. if (dl_error || !symbol_lookup) {
  65. snprintf(_client_error_string, sizeof(_client_error_string), "dlsym failed - %s", dl_error);
  66. return -1;
  67. }
  68. /* Have the symbol correctly, copy it to make it usable */
  69. memcpy(out_func, &symbol_lookup, func_size);
  70. return 0;
  71. }
  72. /**
  73. * Loads libgamemode and needed functions
  74. *
  75. * Returns 0 on success and -1 on failure
  76. */
  77. __attribute__((always_inline)) inline int _load_libgamemode(void)
  78. {
  79. /* We start at 1, 0 is a success and -1 is a fail */
  80. if (_libgamemode_loaded != 1) {
  81. return _libgamemode_loaded;
  82. }
  83. /* Anonymous struct type to define our bindings */
  84. struct binding {
  85. const char *name;
  86. void **functor;
  87. size_t func_size;
  88. } bindings[] = {
  89. { "real_gamemode_request_start",
  90. (void **)&_REAL_gamemode_request_start,
  91. sizeof(_REAL_gamemode_request_start) },
  92. { "real_gamemode_request_end",
  93. (void **)&_REAL_gamemode_request_end,
  94. sizeof(_REAL_gamemode_request_end) },
  95. { "real_gamemode_error_string",
  96. (void **)&_REAL_gamemode_error_string,
  97. sizeof(_REAL_gamemode_error_string) },
  98. };
  99. void *libgamemode = NULL;
  100. /* Try and load libgamemode */
  101. libgamemode = dlopen("libgamemode.so", RTLD_NOW);
  102. if (!libgamemode) {
  103. snprintf(_client_error_string,
  104. sizeof(_client_error_string),
  105. "dylopen failed - %s",
  106. dlerror());
  107. _libgamemode_loaded = -1;
  108. return -1;
  109. }
  110. /* Attempt to bind all symbols */
  111. for (size_t i = 0; i < sizeof(bindings) / sizeof(bindings[0]); i++) {
  112. struct binding *binder = &bindings[i];
  113. if (_bind_libgamemode_symbol(libgamemode,
  114. binder->name,
  115. binder->functor,
  116. binder->func_size) != 0) {
  117. _libgamemode_loaded = -1;
  118. return -1;
  119. };
  120. }
  121. /* Success */
  122. _libgamemode_loaded = 0;
  123. return 0;
  124. }
  125. /**
  126. * Redirect to the real libgamemode
  127. */
  128. __attribute__((always_inline)) inline const char *gamemode_error_string(void)
  129. {
  130. /* If we fail to load the system gamemode, return our error string */
  131. if (_load_libgamemode() < 0) {
  132. return _client_error_string;
  133. }
  134. return _REAL_gamemode_error_string();
  135. }
  136. /**
  137. * Redirect to the real libgamemod
  138. * Allow automatically requesting game mode
  139. * Also prints errors as they happen.
  140. */
  141. #ifdef GAMEMODE_AUTO
  142. __attribute__((constructor))
  143. #else
  144. __attribute__((always_inline)) inline
  145. #endif
  146. int gamemode_request_start(void)
  147. {
  148. /* Need to load gamemode */
  149. if (_load_libgamemode() < 0) {
  150. #ifdef GAMEMODE_AUTO
  151. fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
  152. #endif
  153. return -1;
  154. }
  155. if (_REAL_gamemode_request_start() < 0) {
  156. #ifdef GAMEMODE_AUTO
  157. fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
  158. #endif
  159. return -1;
  160. }
  161. return 0;
  162. }
  163. /* Redirect to the real libgamemode */
  164. #ifdef GAMEMODE_AUTO
  165. __attribute__((destructor))
  166. #else
  167. __attribute__((always_inline)) inline
  168. #endif
  169. int gamemode_request_end(void)
  170. {
  171. /* Need to load gamemode */
  172. if (_load_libgamemode() < 0) {
  173. #ifdef GAMEMODE_AUTO
  174. fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
  175. #endif
  176. return -1;
  177. }
  178. if (_REAL_gamemode_request_end() < 0) {
  179. #ifdef GAMEMODE_AUTO
  180. fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
  181. #endif
  182. return -1;
  183. }
  184. return 0;
  185. }
  186. #endif // CLIENT_GAMEMODE_H