gamemode.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. Copyright (c) 2017-2025, Feral Interactive and the GameMode contributors
  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 <stdint.h>
  29. #include <sys/types.h>
  30. #define INVALID_PROCFD -1
  31. typedef int procfd_t;
  32. /**
  33. * Opaque types
  34. */
  35. typedef struct GameModeContext GameModeContext;
  36. typedef struct GameModeConfig GameModeConfig;
  37. typedef struct GameModeClient GameModeClient;
  38. /**
  39. * GameModeClient related functions
  40. */
  41. /**
  42. * Decrement the usage count of client.
  43. */
  44. void game_mode_client_unref(GameModeClient *client);
  45. /**
  46. * Increment the usage count of client.
  47. */
  48. void game_mode_client_ref(GameModeClient *client);
  49. /**
  50. * The process identifier of the client.
  51. */
  52. pid_t game_mode_client_get_pid(GameModeClient *client);
  53. /**
  54. * The path to the executable of client.
  55. */
  56. const char *game_mode_client_get_executable(GameModeClient *client);
  57. /**
  58. * The process identifier of the requester.
  59. */
  60. pid_t game_mode_client_get_requester(GameModeClient *client);
  61. /**
  62. * The time that game mode was requested for the client.
  63. */
  64. u_int64_t game_mode_client_get_timestamp(GameModeClient *client);
  65. /**
  66. * Return the singleton instance
  67. */
  68. GameModeContext *game_mode_context_instance(void);
  69. /**
  70. * Initialise the GameModeContext
  71. *
  72. * This is performed in a thread-safe fashion.
  73. */
  74. void game_mode_context_init(GameModeContext *self);
  75. /**
  76. * Destroy the previously initialised GameModeContext.
  77. *
  78. * This is performed in a thread safe fashion.
  79. */
  80. void game_mode_context_destroy(GameModeContext *self);
  81. /**
  82. * Query the number of currently registered clients.
  83. *
  84. * @returns The number of clients. A number > 0 means that gamemode is active.
  85. */
  86. int game_mode_context_num_clients(GameModeContext *self);
  87. /**
  88. * List the currently active clients.
  89. * @param out holds the number of active clients.
  90. *
  91. * @returns A array of pid_t or NULL if there are no active clients.
  92. */
  93. pid_t *game_mode_context_list_clients(GameModeContext *self, unsigned int *count);
  94. /**
  95. * Lookup up information about a client via the pid;
  96. *
  97. * @returns A pointer to a GameModeClient struct or NULL in case no client
  98. * with the corresponding id could be found. Adds a reference to
  99. * GameModeClient that needs to be released.
  100. */
  101. GameModeClient *game_mode_context_lookup_client(GameModeContext *self, pid_t client);
  102. /**
  103. * Register a new game client with the context
  104. *
  105. * @param pid Process ID for the remote client
  106. * @param requester Process ID for the remote requestor
  107. * @returns 0 if the request was accepted and the client could be registered
  108. * -1 if the request was accepted but the client could not be registered
  109. * -2 if the request was rejected
  110. */
  111. int game_mode_context_register(GameModeContext *self, pid_t pid, pid_t requester);
  112. /**
  113. * Unregister an existing remote game client from the context
  114. *
  115. * @param pid Process ID for the remote client
  116. * @param requester Process ID for the remote requestor
  117. * @returns 0 if the request was accepted and the client existed
  118. * -1 if the request was accepted but the client did not exist
  119. * -2 if the request was rejected
  120. */
  121. int game_mode_context_unregister(GameModeContext *self, pid_t pid, pid_t requester);
  122. /**
  123. * Query the current status of gamemode
  124. *
  125. * @param pid Process ID for the remote client
  126. * @returns Positive if gamemode is active
  127. * 1 if gamemode is active but the client is not registered
  128. * 2 if gamemode is active and the client is registered
  129. * -2 if this requester was rejected
  130. */
  131. int game_mode_context_query_status(GameModeContext *self, pid_t pid, pid_t requester);
  132. /**
  133. * Query the config of a gamemode context
  134. *
  135. * @param context A gamemode context
  136. * @returns Configuration from the gamemode context
  137. */
  138. GameModeConfig *game_mode_config_from_context(const GameModeContext *context);
  139. /*
  140. * Reload the current configuration
  141. */
  142. int game_mode_reload_config(GameModeContext *context);
  143. /** gamemode-ioprio.c
  144. * Provides internal API functions specific to adjusting process
  145. * IO priorities.
  146. */
  147. int game_mode_get_ioprio(const pid_t client);
  148. void game_mode_apply_ioprio(const GameModeContext *self, const pid_t client, int expected);
  149. /** gamemode-sched.c
  150. * Provides internal API functions specific to adjusting process
  151. * scheduling.
  152. */
  153. int game_mode_get_renice(const pid_t client);
  154. void game_mode_apply_renice(const GameModeContext *self, const pid_t client, int expected);
  155. void game_mode_apply_scheduling(const GameModeContext *self, const pid_t client);
  156. /** gamemode-wine.c
  157. * Provides internal API functions specific to handling wine
  158. * prefixes.
  159. */
  160. char *game_mode_resolve_wine_preloader(const char *exe, const pid_t pid);
  161. /** gamemode-tests.c
  162. * Provides a test suite to verify gamemode behaviour
  163. */
  164. int game_mode_run_client_tests(void);
  165. /** gamemode-gpu.c
  166. * Provides internal APU functions to apply optimisations to gpus
  167. */
  168. typedef struct GameModeGPUInfo GameModeGPUInfo;
  169. int game_mode_initialise_gpu(GameModeConfig *config, GameModeGPUInfo **info);
  170. void game_mode_free_gpu(GameModeGPUInfo **info);
  171. int game_mode_apply_gpu(const GameModeGPUInfo *info);
  172. int game_mode_get_gpu(GameModeGPUInfo *info);
  173. /** gamemode-cpu.c
  174. * Provides internal functions to apply optimisations to cpus
  175. */
  176. typedef struct GameModeCPUInfo GameModeCPUInfo;
  177. int game_mode_initialise_cpu(GameModeConfig *config, GameModeCPUInfo **info);
  178. void game_mode_free_cpu(GameModeCPUInfo **info);
  179. void game_mode_reconfig_cpu(GameModeConfig *config, GameModeCPUInfo **info);
  180. int game_mode_park_cpu(const GameModeCPUInfo *info);
  181. int game_mode_unpark_cpu(const GameModeCPUInfo *info);
  182. void game_mode_apply_core_pinning(const GameModeCPUInfo *info, const pid_t client,
  183. const bool be_silent);
  184. void game_mode_undo_core_pinning(const GameModeCPUInfo *info, const pid_t client);
  185. /** gamemode-dbus.c
  186. * Provides an API interface for using dbus
  187. */
  188. typedef struct GameModeIdleInhibitor GameModeIdleInhibitor;
  189. void game_mode_context_loop(GameModeContext *context) __attribute__((noreturn));
  190. GameModeIdleInhibitor *game_mode_create_idle_inhibitor(void);
  191. void game_mode_destroy_idle_inhibitor(GameModeIdleInhibitor *inhibitor);
  192. void game_mode_client_registered(pid_t);
  193. void game_mode_client_unregistered(pid_t);