gamemode-dbus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. Copyright (c) 2017-2019, 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. #define _GNU_SOURCE
  27. #include "gamemode.h"
  28. #include "common-logging.h"
  29. #include <systemd/sd-bus.h>
  30. #include <systemd/sd-daemon.h>
  31. /* systemd dbus components */
  32. static sd_bus *bus = NULL;
  33. static sd_bus_slot *slot = NULL;
  34. /**
  35. * Clean up our private dbus state
  36. */
  37. static void clean_up(void)
  38. {
  39. if (slot) {
  40. sd_bus_slot_unref(slot);
  41. }
  42. slot = NULL;
  43. if (bus) {
  44. sd_bus_unref(bus);
  45. }
  46. bus = NULL;
  47. }
  48. /**
  49. * Handles the RegisterGame D-BUS Method
  50. */
  51. static int method_register_game(sd_bus_message *m, void *userdata,
  52. __attribute__((unused)) sd_bus_error *ret_error)
  53. {
  54. int pid = 0;
  55. GameModeContext *context = userdata;
  56. int ret = sd_bus_message_read(m, "i", &pid);
  57. if (ret < 0) {
  58. LOG_ERROR("Failed to parse input parameters: %s\n", strerror(-ret));
  59. return ret;
  60. }
  61. int status = game_mode_context_register(context, (pid_t)pid, (pid_t)pid);
  62. return sd_bus_reply_method_return(m, "i", status);
  63. }
  64. /**
  65. * Handles the UnregisterGame D-BUS Method
  66. */
  67. static int method_unregister_game(sd_bus_message *m, void *userdata,
  68. __attribute__((unused)) sd_bus_error *ret_error)
  69. {
  70. int pid = 0;
  71. GameModeContext *context = userdata;
  72. int ret = sd_bus_message_read(m, "i", &pid);
  73. if (ret < 0) {
  74. LOG_ERROR("Failed to parse input parameters: %s\n", strerror(-ret));
  75. return ret;
  76. }
  77. int status = game_mode_context_unregister(context, (pid_t)pid, (pid_t)pid);
  78. return sd_bus_reply_method_return(m, "i", status);
  79. }
  80. /**
  81. * Handles the QueryStatus D-BUS Method
  82. */
  83. static int method_query_status(sd_bus_message *m, void *userdata,
  84. __attribute__((unused)) sd_bus_error *ret_error)
  85. {
  86. int pid = 0;
  87. GameModeContext *context = userdata;
  88. int ret = sd_bus_message_read(m, "i", &pid);
  89. if (ret < 0) {
  90. LOG_ERROR("Failed to parse input parameters: %s\n", strerror(-ret));
  91. return ret;
  92. }
  93. int status = game_mode_context_query_status(context, (pid_t)pid, (pid_t)pid);
  94. return sd_bus_reply_method_return(m, "i", status);
  95. }
  96. /**
  97. * Handles the RegisterGameByPID D-BUS Method
  98. */
  99. static int method_register_game_by_pid(sd_bus_message *m, void *userdata,
  100. __attribute__((unused)) sd_bus_error *ret_error)
  101. {
  102. int callerpid = 0;
  103. int gamepid = 0;
  104. GameModeContext *context = userdata;
  105. int ret = sd_bus_message_read(m, "ii", &callerpid, &gamepid);
  106. if (ret < 0) {
  107. LOG_ERROR("Failed to parse input parameters: %s\n", strerror(-ret));
  108. return ret;
  109. }
  110. int reply = game_mode_context_register(context, (pid_t)gamepid, (pid_t)callerpid);
  111. return sd_bus_reply_method_return(m, "i", reply);
  112. }
  113. /**
  114. * Handles the UnregisterGameByPID D-BUS Method
  115. */
  116. static int method_unregister_game_by_pid(sd_bus_message *m, void *userdata,
  117. __attribute__((unused)) sd_bus_error *ret_error)
  118. {
  119. int callerpid = 0;
  120. int gamepid = 0;
  121. GameModeContext *context = userdata;
  122. int ret = sd_bus_message_read(m, "ii", &callerpid, &gamepid);
  123. if (ret < 0) {
  124. LOG_ERROR("Failed to parse input parameters: %s\n", strerror(-ret));
  125. return ret;
  126. }
  127. int reply = game_mode_context_unregister(context, (pid_t)gamepid, (pid_t)callerpid);
  128. return sd_bus_reply_method_return(m, "i", reply);
  129. }
  130. /**
  131. * Handles the QueryStatus D-BUS Method
  132. */
  133. static int method_query_status_by_pid(sd_bus_message *m, void *userdata,
  134. __attribute__((unused)) sd_bus_error *ret_error)
  135. {
  136. int callerpid = 0;
  137. int gamepid = 0;
  138. GameModeContext *context = userdata;
  139. int ret = sd_bus_message_read(m, "ii", &callerpid, &gamepid);
  140. if (ret < 0) {
  141. LOG_ERROR("Failed to parse input parameters: %s\n", strerror(-ret));
  142. return ret;
  143. }
  144. int status = game_mode_context_query_status(context, (pid_t)gamepid, (pid_t)callerpid);
  145. return sd_bus_reply_method_return(m, "i", status);
  146. }
  147. /**
  148. * Handles the Active D-BUS Method
  149. */
  150. static int property_get_client_count(sd_bus *local_bus, const char *path, const char *interface,
  151. const char *property, sd_bus_message *reply, void *userdata,
  152. __attribute__((unused)) sd_bus_error *ret_error)
  153. {
  154. GameModeContext *context = userdata;
  155. int count;
  156. count = game_mode_context_num_clients(context);
  157. return sd_bus_message_append_basic(reply, 'i', &count);
  158. }
  159. void game_mode_client_count_changed(void)
  160. {
  161. (void)sd_bus_emit_properties_changed(bus,
  162. "/com/feralinteractive/GameMode",
  163. "com.feralinteractive.GameMode",
  164. "ClientCount",
  165. NULL);
  166. }
  167. /**
  168. * Handles the Refresh Config request
  169. */
  170. static int method_refresh_config(sd_bus_message *m, void *userdata,
  171. __attribute__((unused)) sd_bus_error *ret_error)
  172. {
  173. GameModeContext *context = userdata;
  174. int status = game_mode_reload_config(context);
  175. return sd_bus_reply_method_return(m, "i", status);
  176. }
  177. /**
  178. * D-BUS vtable to dispatch virtual methods
  179. */
  180. /* This bit seems to be formatted differently by different clang-format versions */
  181. /* clang-format off */
  182. static const sd_bus_vtable gamemode_vtable[] = {
  183. SD_BUS_VTABLE_START(0),
  184. SD_BUS_PROPERTY("ClientCount", "i", property_get_client_count, 0,
  185. SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
  186. SD_BUS_METHOD("RegisterGame", "i", "i", method_register_game, SD_BUS_VTABLE_UNPRIVILEGED),
  187. SD_BUS_METHOD("UnregisterGame", "i", "i", method_unregister_game, SD_BUS_VTABLE_UNPRIVILEGED),
  188. SD_BUS_METHOD("QueryStatus", "i", "i", method_query_status, SD_BUS_VTABLE_UNPRIVILEGED),
  189. SD_BUS_METHOD("RegisterGameByPID", "ii", "i", method_register_game_by_pid,
  190. SD_BUS_VTABLE_UNPRIVILEGED),
  191. SD_BUS_METHOD("UnregisterGameByPID", "ii", "i", method_unregister_game_by_pid,
  192. SD_BUS_VTABLE_UNPRIVILEGED),
  193. SD_BUS_METHOD("QueryStatusByPID", "ii", "i", method_query_status_by_pid,
  194. SD_BUS_VTABLE_UNPRIVILEGED),
  195. SD_BUS_METHOD("RefreshConfig", "", "i", method_refresh_config, SD_BUS_VTABLE_UNPRIVILEGED),
  196. SD_BUS_VTABLE_END
  197. };
  198. /* clang-format on */
  199. /**
  200. * Main process loop for the daemon. Run until quitting has been requested.
  201. */
  202. void game_mode_context_loop(GameModeContext *context)
  203. {
  204. /* Set up function to handle clean up of resources */
  205. atexit(clean_up);
  206. int ret = 0;
  207. /* Connect to the session bus */
  208. ret = sd_bus_open_user(&bus);
  209. if (ret < 0) {
  210. FATAL_ERROR("Failed to connect to the bus: %s\n", strerror(-ret));
  211. }
  212. /* Create the object to allow connections */
  213. ret = sd_bus_add_object_vtable(bus,
  214. &slot,
  215. "/com/feralinteractive/GameMode",
  216. "com.feralinteractive.GameMode",
  217. gamemode_vtable,
  218. context);
  219. if (ret < 0) {
  220. FATAL_ERROR("Failed to install GameMode object: %s\n", strerror(-ret));
  221. }
  222. /* Request our name */
  223. ret = sd_bus_request_name(bus, "com.feralinteractive.GameMode", 0);
  224. if (ret < 0) {
  225. FATAL_ERROR("Failed to acquire service name: %s\n", strerror(-ret));
  226. }
  227. LOG_MSG("Successfully initialised bus with name [%s]...\n", "com.feralinteractive.GameMode");
  228. sd_notifyf(0, "STATUS=%sGameMode is ready to be activated.%s\n", "\x1B[1;36m", "\x1B[0m");
  229. /* Now loop, waiting for callbacks */
  230. for (;;) {
  231. ret = sd_bus_process(bus, NULL);
  232. if (ret < 0) {
  233. FATAL_ERROR("Failure when processing the bus: %s\n", strerror(-ret));
  234. }
  235. /* We're done processing */
  236. if (ret > 0) {
  237. continue;
  238. }
  239. /* Wait for more */
  240. ret = sd_bus_wait(bus, (uint64_t)-1);
  241. if (ret < 0 && -ret != EINTR) {
  242. FATAL_ERROR("Failure when waiting on bus: %s\n", strerror(-ret));
  243. }
  244. }
  245. }
  246. /**
  247. * Attempts to inhibit the screensaver
  248. * Uses the "org.freedesktop.ScreenSaver" interface
  249. */
  250. static unsigned int screensaver_inhibit_cookie = 0;
  251. int game_mode_inhibit_screensaver(bool inhibit)
  252. {
  253. const char *service = "org.freedesktop.ScreenSaver";
  254. const char *object_path = "/org/freedesktop/ScreenSaver";
  255. const char *interface = "org.freedesktop.ScreenSaver";
  256. const char *function = inhibit ? "Inhibit" : "UnInhibit";
  257. sd_bus_message *msg = NULL;
  258. sd_bus *bus_local = NULL;
  259. sd_bus_error err;
  260. memset(&err, 0, sizeof(sd_bus_error));
  261. int result = -1;
  262. // Open the user bus
  263. int ret = sd_bus_open_user(&bus_local);
  264. if (ret < 0) {
  265. LOG_ERROR("Could not connect to user bus: %s\n", strerror(-ret));
  266. return -1;
  267. }
  268. if (inhibit) {
  269. ret = sd_bus_call_method(bus_local,
  270. service,
  271. object_path,
  272. interface,
  273. function,
  274. &err,
  275. &msg,
  276. "ss",
  277. "com.feralinteractive.GameMode",
  278. "GameMode Activated");
  279. } else {
  280. ret = sd_bus_call_method(bus_local,
  281. service,
  282. object_path,
  283. interface,
  284. function,
  285. &err,
  286. &msg,
  287. "u",
  288. screensaver_inhibit_cookie);
  289. }
  290. if (ret < 0) {
  291. LOG_ERROR(
  292. "Could not call %s on %s: %s\n"
  293. "\t%s\n"
  294. "\t%s\n",
  295. function,
  296. service,
  297. strerror(-ret),
  298. err.name,
  299. err.message);
  300. } else if (inhibit) {
  301. // Read the reply
  302. ret = sd_bus_message_read(msg, "u", &screensaver_inhibit_cookie);
  303. if (ret < 0) {
  304. LOG_ERROR("Failure to parse response from %s on %s: %s\n",
  305. function,
  306. service,
  307. strerror(-ret));
  308. } else {
  309. result = 0;
  310. }
  311. } else {
  312. result = 0;
  313. }
  314. sd_bus_unref(bus_local);
  315. return result;
  316. }