gamemode.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #pragma once
  27. #include <stdbool.h>
  28. #include <sys/types.h>
  29. #define INVALID_PROCFD -1
  30. typedef int procfd_t;
  31. /**
  32. * Opaque types
  33. */
  34. typedef struct GameModeContext GameModeContext;
  35. typedef struct GameModeConfig GameModeConfig;
  36. /**
  37. * Return the singleton instance
  38. */
  39. GameModeContext *game_mode_context_instance(void);
  40. /**
  41. * Initialise the GameModeContext
  42. *
  43. * This is performed in a thread-safe fashion.
  44. */
  45. void game_mode_context_init(GameModeContext *self);
  46. /**
  47. * Destroy the previously initialised GameModeContext.
  48. *
  49. * This is performed in a thread safe fashion.
  50. */
  51. void game_mode_context_destroy(GameModeContext *self);
  52. /**
  53. * Register a new game client with the context
  54. *
  55. * @param pid Process ID for the remote client
  56. * @returns True if the new client could be registered
  57. */
  58. bool game_mode_context_register(GameModeContext *self, pid_t pid);
  59. /**
  60. * Unregister an existing remote game client from the context
  61. *
  62. * @param pid Process ID for the remote client
  63. * @returns True if the client was removed, and existed.
  64. */
  65. bool game_mode_context_unregister(GameModeContext *self, pid_t pid);
  66. /**
  67. * Query the current status of gamemode
  68. *
  69. * @param pid Process ID for the remote client
  70. * @returns Positive if gamemode is active
  71. * 1 if gamemode is active but the client is not registered
  72. * 2 if gamemode is active and the client is registered
  73. */
  74. int game_mode_context_query_status(GameModeContext *self, pid_t pid);
  75. /**
  76. * Register a new game client with the context on behalf of another process
  77. *
  78. * @param callerpid Process ID for the remote client making the request
  79. * @param gamepid Process ID for the process to be registered
  80. * @returns 0 if the request was accepted and the client could be registered
  81. * -1 if the request was accepted but the client could not be registered
  82. * -2 if the request was rejected
  83. */
  84. int game_mode_context_register_by_pid(GameModeContext *self, pid_t callerpid, pid_t gamepid);
  85. /**
  86. * Unregister an existing remote game client from the context on behalf of another process
  87. *
  88. * @param callerpid Process ID for the remote client making the request
  89. * @param gamepid Process ID for the process to be registered
  90. * @returns 0 if the request was accepted and the client existed
  91. * -1 if the request was accepted but the client did not exist
  92. * -2 if the request was rejected
  93. */
  94. int game_mode_context_unregister_by_pid(GameModeContext *self, pid_t callerpid, pid_t gamepid);
  95. /**
  96. * Query the current status of gamemode for another process
  97. *
  98. * @param pid Process ID for the remote client
  99. * @returns Positive if gamemode is active
  100. * 1 if gamemode is active but the client is not registered
  101. * 2 if gamemode is active and the client is registered
  102. * -2 if this supervisor was rejected
  103. */
  104. int game_mode_context_query_status_for(GameModeContext *self, pid_t callerpid, pid_t gamepid);
  105. /**
  106. * Query the config of a gamemode context
  107. *
  108. * @param context A gamemode context
  109. * @returns Configuration from the gamemode context
  110. */
  111. GameModeConfig *game_mode_config_from_context(const GameModeContext *context);
  112. /** gamemode-env.c
  113. * Provides internal API functions specific to working environment
  114. * variables.
  115. */
  116. char *game_mode_lookup_proc_env(const procfd_t proc_fd, const char *var);
  117. char *game_mode_lookup_user_home(void);
  118. /** gamemode-ioprio.c
  119. * Provides internal API functions specific to adjusting process
  120. * IO priorities.
  121. */
  122. void game_mode_apply_ioprio(const GameModeContext *self, const pid_t client);
  123. /** gamemode-proc.c
  124. * Provides internal API functions specific to working with process
  125. * environments.
  126. */
  127. procfd_t game_mode_open_proc(const pid_t pid);
  128. int game_mode_close_proc(const procfd_t procfd);
  129. /** gamemode-sched.c
  130. * Provides internal API functions specific to adjusting process
  131. * scheduling.
  132. */
  133. void game_mode_apply_renice(const GameModeContext *self, const pid_t client);
  134. void game_mode_apply_scheduling(const GameModeContext *self, const pid_t client);
  135. /** gamemode-wine.c
  136. * Provides internal API functions specific to handling wine
  137. * prefixes.
  138. */
  139. bool game_mode_detect_wine_loader(const char *exe);
  140. bool game_mode_detect_wine_preloader(const char *exe);
  141. char *game_mode_resolve_wine_preloader(const pid_t pid);
  142. /** gamemode-tests.c
  143. * Provides a test suite to verify gamemode behaviour
  144. */
  145. int game_mode_run_client_tests(void);
  146. /** gamemode-gpu.c
  147. * Provides internal APU functions to apply optimisations to gpus
  148. */
  149. typedef struct GameModeGPUInfo GameModeGPUInfo;
  150. int game_mode_initialise_gpu(GameModeConfig *config, GameModeGPUInfo **info);
  151. void game_mode_free_gpu(GameModeGPUInfo **info);
  152. int game_mode_apply_gpu(const GameModeGPUInfo *info, bool apply);
  153. int game_mode_get_gpu(GameModeGPUInfo *info);