gamemode_client.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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))
  51. inline int _load_libgamemode()
  52. {
  53. // We start at 1, 0 is a success and -1 is a fail
  54. if ( _libgamemode_loaded != 1 )
  55. return _libgamemode_loaded;
  56. void* libgamemode = NULL;
  57. // Try and load libgamemode
  58. libgamemode = dlopen( "libgamemode.so", RTLD_NOW );
  59. if( !libgamemode )
  60. snprintf( _client_error_string, sizeof(_client_error_string), "dylopen failed - %s", dlerror() );
  61. else
  62. {
  63. _REAL_gamemode_request_start = (_gamemode_request_start)dlsym( libgamemode, "real_gamemode_request_start" );
  64. _REAL_gamemode_request_end = (_gamemode_request_end) dlsym( libgamemode, "real_gamemode_request_end" );
  65. _REAL_gamemode_error_string = (_gamemode_error_string) dlsym( libgamemode, "real_gamemode_error_string" );
  66. // Verify we have the functions we want
  67. if( _REAL_gamemode_request_start && _REAL_gamemode_request_end && _REAL_gamemode_error_string )
  68. {
  69. _libgamemode_loaded = 0;
  70. return 0;
  71. }
  72. else
  73. snprintf( _client_error_string, sizeof(_client_error_string), "dlsym failed - %s", dlerror() );
  74. }
  75. _libgamemode_loaded = -1;
  76. return -1;
  77. }
  78. // Redirect to the real libgamemode
  79. __attribute__((always_inline))
  80. inline const char* gamemode_error_string()
  81. {
  82. // If we fail to load the system gamemode, return our error string
  83. if( _load_libgamemode() < 0 )
  84. return _client_error_string;
  85. return _REAL_gamemode_error_string();
  86. }
  87. // Redirect to the real libgamemode
  88. // Allow automatically requesting game mode
  89. // Also prints errors as they happen
  90. #ifdef GAMEMODE_AUTO
  91. __attribute__((constructor))
  92. #else
  93. __attribute__((always_inline))
  94. inline
  95. #endif
  96. int gamemode_request_start()
  97. {
  98. // Need to load gamemode
  99. if( _load_libgamemode() < 0 )
  100. {
  101. #ifdef GAMEMODE_AUTO
  102. fprintf( stderr, "gamemodeauto: %s\n", gamemode_error_string() );
  103. #endif
  104. return -1;
  105. }
  106. if( _REAL_gamemode_request_start() < 0 )
  107. {
  108. #ifdef GAMEMODE_AUTO
  109. fprintf( stderr, "gamemodeauto: %s\n", gamemode_error_string() );
  110. #endif
  111. return -1;
  112. }
  113. return 0;
  114. }
  115. // Redirect to the real libgamemode
  116. #ifdef GAMEMODE_AUTO
  117. __attribute__((destructor))
  118. #else
  119. __attribute__((always_inline))
  120. inline
  121. #endif
  122. int gamemode_request_end()
  123. {
  124. // Need to load gamemode
  125. if( _load_libgamemode() < 0 )
  126. {
  127. #ifdef GAMEMODE_AUTO
  128. fprintf( stderr, "gamemodeauto: %s\n", gamemode_error_string() );
  129. #endif
  130. return -1;
  131. }
  132. if( _REAL_gamemode_request_end() < 0 )
  133. {
  134. #ifdef GAMEMODE_AUTO
  135. fprintf( stderr, "gamemodeauto: %s\n", gamemode_error_string() );
  136. #endif
  137. return -1;
  138. }
  139. return 0;
  140. }
  141. #endif // _CLIENT_GAMEMODE_H_