gamemode_client.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. Copyright (c) 2017, 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] = {};
  35. // Load libgamemode dynamically to dislodge us from most dependencies
  36. // This allows clients to link and/or use this regardless of runtime
  37. // See SDL2 for an example of the reasoning behind this in terms of
  38. // dynamic versioning as well
  39. int _libgamemode_loaded = 1;
  40. // Typedefs for the functions to load
  41. typedef int (*_gamemode_request_start)();
  42. typedef int (*_gamemode_request_end)();
  43. typedef const char *(*_gamemode_error_string)();
  44. // Storage for functors
  45. _gamemode_request_start _REAL_gamemode_request_start = NULL;
  46. _gamemode_request_end _REAL_gamemode_request_end = NULL;
  47. _gamemode_error_string _REAL_gamemode_error_string = NULL;
  48. // Loads libgamemode and needed functions
  49. // returns 0 on success and -1 on failure
  50. __attribute__((always_inline)) inline int _load_libgamemode()
  51. {
  52. // We start at 1, 0 is a success and -1 is a fail
  53. if (_libgamemode_loaded != 1) {
  54. return _libgamemode_loaded;
  55. }
  56. void *libgamemode = NULL;
  57. // Try and load libgamemode
  58. libgamemode = dlopen("libgamemode.so", RTLD_NOW);
  59. if (!libgamemode) {
  60. snprintf(_client_error_string,
  61. sizeof(_client_error_string),
  62. "dylopen failed - %s",
  63. dlerror());
  64. } else {
  65. _REAL_gamemode_request_start =
  66. (_gamemode_request_start)dlsym(libgamemode, "real_gamemode_request_start");
  67. _REAL_gamemode_request_end =
  68. (_gamemode_request_end)dlsym(libgamemode, "real_gamemode_request_end");
  69. _REAL_gamemode_error_string =
  70. (_gamemode_error_string)dlsym(libgamemode, "real_gamemode_error_string");
  71. // Verify we have the functions we want
  72. if (_REAL_gamemode_request_start && _REAL_gamemode_request_end &&
  73. _REAL_gamemode_error_string) {
  74. _libgamemode_loaded = 0;
  75. return 0;
  76. } else {
  77. snprintf(_client_error_string,
  78. sizeof(_client_error_string),
  79. "dlsym failed - %s",
  80. dlerror());
  81. }
  82. }
  83. _libgamemode_loaded = -1;
  84. return -1;
  85. }
  86. // Redirect to the real libgamemode
  87. __attribute__((always_inline)) inline const char *gamemode_error_string()
  88. {
  89. // If we fail to load the system gamemode, return our error string
  90. if (_load_libgamemode() < 0) {
  91. return _client_error_string;
  92. }
  93. return _REAL_gamemode_error_string();
  94. }
  95. // Redirect to the real libgamemode
  96. // Allow automatically requesting game mode
  97. // Also prints errors as they happen
  98. #ifdef GAMEMODE_AUTO
  99. __attribute__((constructor))
  100. #else
  101. __attribute__((always_inline)) inline
  102. #endif
  103. int gamemode_request_start()
  104. {
  105. // Need to load gamemode
  106. if (_load_libgamemode() < 0) {
  107. #ifdef GAMEMODE_AUTO
  108. fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
  109. #endif
  110. return -1;
  111. }
  112. if (_REAL_gamemode_request_start() < 0) {
  113. #ifdef GAMEMODE_AUTO
  114. fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
  115. #endif
  116. return -1;
  117. }
  118. return 0;
  119. }
  120. // Redirect to the real libgamemode
  121. #ifdef GAMEMODE_AUTO
  122. __attribute__((destructor))
  123. #else
  124. __attribute__((always_inline)) inline
  125. #endif
  126. int gamemode_request_end()
  127. {
  128. // Need to load gamemode
  129. if (_load_libgamemode() < 0) {
  130. #ifdef GAMEMODE_AUTO
  131. fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
  132. #endif
  133. return -1;
  134. }
  135. if (_REAL_gamemode_request_end() < 0) {
  136. #ifdef GAMEMODE_AUTO
  137. fprintf(stderr, "gamemodeauto: %s\n", gamemode_error_string());
  138. #endif
  139. return -1;
  140. }
  141. return 0;
  142. }
  143. #endif // _CLIENT_GAMEMODE_H_