gamemode-dbus.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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-helpers.h"
  29. #include "common-logging.h"
  30. #include "common-pidfds.h"
  31. #ifdef USE_ELOGIND
  32. #include <elogind/sd-bus.h>
  33. #include <elogind/sd-daemon.h>
  34. #else
  35. #include <systemd/sd-bus.h>
  36. #include <systemd/sd-daemon.h>
  37. #endif
  38. #include <assert.h>
  39. #include <errno.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #define GAME_PATH_PREFIX "/com/feralinteractive/GameMode/Games"
  43. /* maximum length of a valid game object path string:
  44. * The path prefix including \0 (sizeof), another '/', and 10 digits for uint32_t ('%u')*/
  45. #define GAME_PATH_MAX (sizeof(GAME_PATH_PREFIX) + 11)
  46. /* systemd dbus components */
  47. static sd_bus *bus = NULL;
  48. static sd_bus_slot *slot = NULL;
  49. /**
  50. * Clean up our private dbus state
  51. */
  52. static void clean_up(void)
  53. {
  54. if (slot) {
  55. sd_bus_slot_unref(slot);
  56. }
  57. slot = NULL;
  58. if (bus) {
  59. sd_bus_unref(bus);
  60. }
  61. bus = NULL;
  62. }
  63. /**
  64. * Handles the RegisterGame D-BUS Method
  65. */
  66. static int method_register_game(sd_bus_message *m, void *userdata,
  67. __attribute__((unused)) sd_bus_error *ret_error)
  68. {
  69. int pid = 0;
  70. GameModeContext *context = userdata;
  71. int ret = sd_bus_message_read(m, "i", &pid);
  72. if (ret < 0) {
  73. LOG_ERROR("Failed to parse input parameters: %s\n", strerror(-ret));
  74. return ret;
  75. }
  76. int status = game_mode_context_register(context, (pid_t)pid, (pid_t)pid);
  77. return sd_bus_reply_method_return(m, "i", status);
  78. }
  79. /**
  80. * Handles the UnregisterGame D-BUS Method
  81. */
  82. static int method_unregister_game(sd_bus_message *m, void *userdata,
  83. __attribute__((unused)) sd_bus_error *ret_error)
  84. {
  85. int pid = 0;
  86. GameModeContext *context = userdata;
  87. int ret = sd_bus_message_read(m, "i", &pid);
  88. if (ret < 0) {
  89. LOG_ERROR("Failed to parse input parameters: %s\n", strerror(-ret));
  90. return ret;
  91. }
  92. int status = game_mode_context_unregister(context, (pid_t)pid, (pid_t)pid);
  93. return sd_bus_reply_method_return(m, "i", status);
  94. }
  95. /**
  96. * Handles the QueryStatus D-BUS Method
  97. */
  98. static int method_query_status(sd_bus_message *m, void *userdata,
  99. __attribute__((unused)) sd_bus_error *ret_error)
  100. {
  101. int pid = 0;
  102. GameModeContext *context = userdata;
  103. int ret = sd_bus_message_read(m, "i", &pid);
  104. if (ret < 0) {
  105. LOG_ERROR("Failed to parse input parameters: %s\n", strerror(-ret));
  106. return ret;
  107. }
  108. int status = game_mode_context_query_status(context, (pid_t)pid, (pid_t)pid);
  109. return sd_bus_reply_method_return(m, "i", status);
  110. }
  111. /**
  112. * Handles the RegisterGameByPID D-BUS Method
  113. */
  114. static int method_register_game_by_pid(sd_bus_message *m, void *userdata,
  115. __attribute__((unused)) sd_bus_error *ret_error)
  116. {
  117. int callerpid = 0;
  118. int gamepid = 0;
  119. GameModeContext *context = userdata;
  120. int ret = sd_bus_message_read(m, "ii", &callerpid, &gamepid);
  121. if (ret < 0) {
  122. LOG_ERROR("Failed to parse input parameters: %s\n", strerror(-ret));
  123. return ret;
  124. }
  125. int reply = game_mode_context_register(context, (pid_t)gamepid, (pid_t)callerpid);
  126. return sd_bus_reply_method_return(m, "i", reply);
  127. }
  128. /**
  129. * Handles the UnregisterGameByPID D-BUS Method
  130. */
  131. static int method_unregister_game_by_pid(sd_bus_message *m, void *userdata,
  132. __attribute__((unused)) sd_bus_error *ret_error)
  133. {
  134. int callerpid = 0;
  135. int gamepid = 0;
  136. GameModeContext *context = userdata;
  137. int ret = sd_bus_message_read(m, "ii", &callerpid, &gamepid);
  138. if (ret < 0) {
  139. LOG_ERROR("Failed to parse input parameters: %s\n", strerror(-ret));
  140. return ret;
  141. }
  142. int reply = game_mode_context_unregister(context, (pid_t)gamepid, (pid_t)callerpid);
  143. return sd_bus_reply_method_return(m, "i", reply);
  144. }
  145. /**
  146. * Handles the QueryStatusByPID D-BUS Method
  147. */
  148. static int method_query_status_by_pid(sd_bus_message *m, void *userdata,
  149. __attribute__((unused)) sd_bus_error *ret_error)
  150. {
  151. int callerpid = 0;
  152. int gamepid = 0;
  153. GameModeContext *context = userdata;
  154. int ret = sd_bus_message_read(m, "ii", &callerpid, &gamepid);
  155. if (ret < 0) {
  156. LOG_ERROR("Failed to parse input parameters: %s\n", strerror(-ret));
  157. return ret;
  158. }
  159. int status = game_mode_context_query_status(context, (pid_t)gamepid, (pid_t)callerpid);
  160. return sd_bus_reply_method_return(m, "i", status);
  161. }
  162. /**
  163. * Handles the RegisterGameByPIDFd D-BUS Method
  164. */
  165. static int method_register_game_by_pidfd(sd_bus_message *m, void *userdata,
  166. __attribute__((unused)) sd_bus_error *ret_error)
  167. {
  168. int fds[2] = { -1, -1 };
  169. pid_t pids[2] = { 0, 0 };
  170. GameModeContext *context = userdata;
  171. int ret = sd_bus_message_read(m, "hh", &fds[0], &fds[1]);
  172. if (ret < 0) {
  173. LOG_ERROR("Failed to parse input parameters: %s\n", strerror(-ret));
  174. return ret;
  175. }
  176. int reply = pidfds_to_pids(fds, pids, 2);
  177. if (reply == 2)
  178. reply = game_mode_context_register(context, pids[0], pids[1]);
  179. else
  180. reply = -1;
  181. return sd_bus_reply_method_return(m, "i", reply);
  182. }
  183. /**
  184. * Handles the UnregisterGameByPIDFd D-BUS Method
  185. */
  186. static int method_unregister_game_by_pidfd(sd_bus_message *m, void *userdata,
  187. __attribute__((unused)) sd_bus_error *ret_error)
  188. {
  189. int fds[2] = { -1, -1 };
  190. pid_t pids[2] = { 0, 0 };
  191. GameModeContext *context = userdata;
  192. int ret = sd_bus_message_read(m, "hh", &fds[0], &fds[1]);
  193. if (ret < 0) {
  194. LOG_ERROR("Failed to parse input parameters: %s\n", strerror(-ret));
  195. return ret;
  196. }
  197. int reply = pidfds_to_pids(fds, pids, 2);
  198. if (reply == 2)
  199. reply = game_mode_context_unregister(context, pids[0], pids[1]);
  200. else
  201. reply = -1;
  202. return sd_bus_reply_method_return(m, "i", reply);
  203. }
  204. /**
  205. * Handles the QueryStatusByPIDFd D-BUS Method
  206. */
  207. static int method_query_status_by_pidfd(sd_bus_message *m, void *userdata,
  208. __attribute__((unused)) sd_bus_error *ret_error)
  209. {
  210. int fds[2] = { -1, -1 };
  211. pid_t pids[2] = { 0, 0 };
  212. GameModeContext *context = userdata;
  213. int ret = sd_bus_message_read(m, "hh", &fds[0], &fds[1]);
  214. if (ret < 0) {
  215. LOG_ERROR("Failed to parse input parameters: %s\n", strerror(-ret));
  216. return ret;
  217. }
  218. int reply = pidfds_to_pids(fds, pids, 2);
  219. if (reply == 2)
  220. reply = game_mode_context_query_status(context, pids[0], pids[1]);
  221. else
  222. reply = -1;
  223. return sd_bus_reply_method_return(m, "i", reply);
  224. }
  225. /**
  226. * Handles the ClientCount D-BUS Property
  227. */
  228. static int property_get_client_count(sd_bus *local_bus, const char *path, const char *interface,
  229. const char *property, sd_bus_message *reply, void *userdata,
  230. __attribute__((unused)) sd_bus_error *ret_error)
  231. {
  232. GameModeContext *context = userdata;
  233. int count;
  234. count = game_mode_context_num_clients(context);
  235. return sd_bus_message_append_basic(reply, 'i', &count);
  236. }
  237. /**
  238. * Handles the Refresh Config request
  239. */
  240. static int method_refresh_config(sd_bus_message *m, void *userdata,
  241. __attribute__((unused)) sd_bus_error *ret_error)
  242. {
  243. GameModeContext *context = userdata;
  244. int status = game_mode_reload_config(context);
  245. return sd_bus_reply_method_return(m, "i", status);
  246. }
  247. static inline void game_object_bus_path(pid_t pid, char path[static GAME_PATH_MAX])
  248. {
  249. snprintf(path, GAME_PATH_MAX, GAME_PATH_PREFIX "/%u", (uint32_t)pid);
  250. }
  251. /**
  252. * Handles the List Games
  253. */
  254. static int method_list_games(sd_bus_message *m, void *userdata,
  255. __attribute__((unused)) sd_bus_error *ret_error)
  256. {
  257. GameModeContext *context = userdata;
  258. sd_bus_message *reply = NULL;
  259. unsigned int count;
  260. pid_t *clients;
  261. int r;
  262. r = sd_bus_message_new_method_return(m, &reply);
  263. if (r < 0)
  264. return r;
  265. r = sd_bus_message_open_container(reply, 'a', "(io)");
  266. if (r < 0)
  267. return r;
  268. clients = game_mode_context_list_clients(context, &count);
  269. for (unsigned int i = 0; i < count; i++) {
  270. char path[GAME_PATH_MAX] = {
  271. 0,
  272. };
  273. pid_t pid = clients[i];
  274. game_object_bus_path(pid, path);
  275. r = sd_bus_message_append(reply, "(io)", (int32_t)pid, path);
  276. if (r < 0)
  277. break;
  278. }
  279. free(clients);
  280. if (r < 0)
  281. return r;
  282. r = sd_bus_message_close_container(reply);
  283. if (r < 0)
  284. return r;
  285. return sd_bus_send(NULL, reply, NULL);
  286. }
  287. /* Signal emission helper */
  288. static void game_mode_client_send_game_signal(pid_t pid, bool new_game)
  289. {
  290. char path[GAME_PATH_MAX] = {
  291. 0,
  292. };
  293. int ret;
  294. game_object_bus_path(pid, path);
  295. ret = sd_bus_emit_signal(bus,
  296. "/com/feralinteractive/GameMode",
  297. "com.feralinteractive.GameMode",
  298. new_game ? "GameRegistered" : "GameUnregistered",
  299. "io",
  300. (int32_t)pid,
  301. path);
  302. if (ret < 0)
  303. fprintf(stderr, "failed to emit signal: %s", strerror(-ret));
  304. (void)sd_bus_emit_properties_changed(bus,
  305. "/com/feralinteractive/GameMode",
  306. "com.feralinteractive.GameMode",
  307. "ClientCount",
  308. NULL);
  309. }
  310. /* Emit GameRegistered signal */
  311. void game_mode_client_registered(pid_t pid)
  312. {
  313. game_mode_client_send_game_signal(pid, true);
  314. }
  315. /* Emit GameUnregistered signal */
  316. void game_mode_client_unregistered(pid_t pid)
  317. {
  318. game_mode_client_send_game_signal(pid, false);
  319. }
  320. /**
  321. * D-BUS vtable to dispatch virtual methods
  322. */
  323. /* This bit seems to be formatted differently by different clang-format versions */
  324. /* clang-format off */
  325. static const sd_bus_vtable gamemode_vtable[] = {
  326. SD_BUS_VTABLE_START(0),
  327. SD_BUS_PROPERTY("ClientCount", "i", property_get_client_count, 0,
  328. SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
  329. SD_BUS_METHOD("RegisterGame", "i", "i", method_register_game, SD_BUS_VTABLE_UNPRIVILEGED),
  330. SD_BUS_METHOD("UnregisterGame", "i", "i", method_unregister_game, SD_BUS_VTABLE_UNPRIVILEGED),
  331. SD_BUS_METHOD("QueryStatus", "i", "i", method_query_status, SD_BUS_VTABLE_UNPRIVILEGED),
  332. SD_BUS_METHOD("RegisterGameByPID", "ii", "i", method_register_game_by_pid,
  333. SD_BUS_VTABLE_UNPRIVILEGED),
  334. SD_BUS_METHOD("UnregisterGameByPID", "ii", "i", method_unregister_game_by_pid,
  335. SD_BUS_VTABLE_UNPRIVILEGED),
  336. SD_BUS_METHOD("QueryStatusByPID", "ii", "i", method_query_status_by_pid,
  337. SD_BUS_VTABLE_UNPRIVILEGED),
  338. SD_BUS_METHOD("RegisterGameByPIDFd", "hh", "i", method_register_game_by_pidfd,
  339. SD_BUS_VTABLE_UNPRIVILEGED),
  340. SD_BUS_METHOD("UnregisterGameByPIDFd", "hh", "i", method_unregister_game_by_pidfd,
  341. SD_BUS_VTABLE_UNPRIVILEGED),
  342. SD_BUS_METHOD("QueryStatusByPIDFd", "hh", "i", method_query_status_by_pidfd,
  343. SD_BUS_VTABLE_UNPRIVILEGED),
  344. SD_BUS_METHOD("RefreshConfig", "", "i", method_refresh_config, SD_BUS_VTABLE_UNPRIVILEGED),
  345. SD_BUS_METHOD("ListGames", "", "a(io)", method_list_games, SD_BUS_VTABLE_UNPRIVILEGED),
  346. SD_BUS_SIGNAL("GameRegistered", "io", 0),
  347. SD_BUS_SIGNAL("GameUnregistered", "io", 0),
  348. SD_BUS_VTABLE_END
  349. };
  350. /**
  351. * Game Objects
  352. */
  353. static inline void pid_to_pointer(pid_t pid, void **pointer)
  354. {
  355. _Static_assert(sizeof (void *) >= sizeof (pid_t),
  356. "pointer type not large enough to store pid_t");
  357. *pointer = (void *) (intptr_t) pid;
  358. }
  359. static inline pid_t pid_from_pointer(const void *pointer)
  360. {
  361. return (pid_t) (intptr_t) pointer;
  362. }
  363. static int game_object_find(sd_bus *local_bus, const char *path, const char *interface,
  364. void *userdata, void **found, sd_bus_error *ret_error)
  365. {
  366. static const char prefix[] = GAME_PATH_PREFIX "/";
  367. const char *start;
  368. unsigned long int n;
  369. char *end;
  370. if (strncmp(path, prefix, strlen(prefix)) != 0)
  371. return 0;
  372. start = path + strlen(prefix);
  373. errno = 0;
  374. n = strtoul(start, &end, 10);
  375. if (start == end || errno != 0)
  376. return 0;
  377. pid_to_pointer((pid_t) n, found);
  378. return 1;
  379. }
  380. static int game_node_enumerator(sd_bus *local_bus, const char *path, void *userdata,
  381. char ***nodes,
  382. __attribute__((unused)) sd_bus_error *ret_error)
  383. {
  384. GameModeContext *context = userdata;
  385. unsigned int count;
  386. pid_t *clients;
  387. char **strv = NULL;
  388. clients = game_mode_context_list_clients(context, &count);
  389. strv = malloc (sizeof (char *) * (count + 1));
  390. for (unsigned int i = 0; i < count; i++) {
  391. char bus_path[GAME_PATH_MAX] = {0, };
  392. game_object_bus_path(clients[i], bus_path);
  393. strv[i] = strdup (bus_path);
  394. }
  395. strv[count] = NULL;
  396. *nodes = strv;
  397. free(clients);
  398. return 1;
  399. }
  400. /**
  401. * Handles the ProcessId property for Game objects
  402. */
  403. static int game_object_get_process_id(sd_bus *local_bus, const char *path, const char *interface,
  404. const char *property, sd_bus_message *reply, void *userdata,
  405. sd_bus_error *ret_error)
  406. {
  407. GameModeClient *client;
  408. GameModeContext *context;
  409. pid_t pid;
  410. int pv;
  411. int ret;
  412. pid = pid_from_pointer(userdata);
  413. context = game_mode_context_instance();
  414. client = game_mode_context_lookup_client(context, pid);
  415. pv = (int) pid;
  416. if (client == NULL) {
  417. return sd_bus_error_setf(ret_error,
  418. SD_BUS_ERROR_UNKNOWN_OBJECT,
  419. "No client registered with id '%d'", pv);
  420. }
  421. ret = sd_bus_message_append_basic(reply, 'i', &pv);
  422. game_mode_client_unref(client);
  423. return ret;
  424. }
  425. /**
  426. * Handles the Exectuable property for Game objects
  427. */
  428. static int game_object_get_executable(sd_bus *local_bus, const char *path, const char *interface,
  429. const char *property, sd_bus_message *reply, void *userdata,
  430. sd_bus_error *ret_error)
  431. {
  432. GameModeClient *client;
  433. GameModeContext *context;
  434. const char *exec;
  435. pid_t pid;
  436. int ret;
  437. pid = pid_from_pointer(userdata);
  438. context = game_mode_context_instance();
  439. client = game_mode_context_lookup_client(context, pid);
  440. if (client == NULL) {
  441. return sd_bus_error_setf(ret_error,
  442. SD_BUS_ERROR_UNKNOWN_OBJECT,
  443. "No client registered with id '%d'", (int) pid);
  444. }
  445. exec = game_mode_client_get_executable(client);
  446. ret = sd_bus_message_append_basic(reply, 's', exec);
  447. game_mode_client_unref(client);
  448. return ret;
  449. }
  450. /**
  451. * Handles the Requester property for Game objects
  452. */
  453. static int game_object_get_requester(sd_bus *local_bus, const char *path, const char *interface,
  454. const char *property, sd_bus_message *reply, void *userdata,
  455. sd_bus_error *ret_error)
  456. {
  457. GameModeClient *client;
  458. GameModeContext *context;
  459. pid_t requester;
  460. pid_t pid;
  461. int ret;
  462. int pv;
  463. pid = pid_from_pointer(userdata);
  464. context = game_mode_context_instance();
  465. client = game_mode_context_lookup_client(context, pid);
  466. if (client == NULL) {
  467. return sd_bus_error_setf(ret_error,
  468. SD_BUS_ERROR_UNKNOWN_OBJECT,
  469. "No client registered with id '%d'", (int) pid);
  470. }
  471. requester = game_mode_client_get_requester(client);
  472. pv = (int) requester;
  473. ret = sd_bus_message_append_basic(reply, 'i', &pv);
  474. game_mode_client_unref(client);
  475. return ret;
  476. }
  477. /**
  478. * Handles the Timestamp property for Game objects
  479. */
  480. static int game_object_get_timestamp(sd_bus *local_bus, const char *path, const char *interface,
  481. const char *property, sd_bus_message *reply, void *userdata,
  482. sd_bus_error *ret_error)
  483. {
  484. GameModeClient *client;
  485. GameModeContext *context;
  486. uint64_t timestamp;
  487. pid_t pid;
  488. int ret;
  489. pid = pid_from_pointer(userdata);
  490. context = game_mode_context_instance();
  491. client = game_mode_context_lookup_client(context, pid);
  492. if (client == NULL) {
  493. return sd_bus_error_setf(ret_error,
  494. SD_BUS_ERROR_UNKNOWN_OBJECT,
  495. "No client registered with id '%d'", (int) pid);
  496. }
  497. timestamp = game_mode_client_get_timestamp(client);
  498. ret = sd_bus_message_append_basic(reply, 't', &timestamp);
  499. game_mode_client_unref(client);
  500. return ret;
  501. }
  502. /* Same as above: this bit seems to be formatted differently by different clang-format versions */
  503. /* clang-format off */
  504. static const sd_bus_vtable game_vtable[] = {
  505. SD_BUS_VTABLE_START(0),
  506. SD_BUS_PROPERTY("ProcessId", "i", game_object_get_process_id, 0,
  507. SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
  508. SD_BUS_PROPERTY("Executable", "s", game_object_get_executable, 0,
  509. SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
  510. SD_BUS_PROPERTY("Requester", "i", game_object_get_requester, 0,
  511. SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
  512. SD_BUS_PROPERTY("Timestamp", "t", game_object_get_timestamp, 0,
  513. SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
  514. SD_BUS_VTABLE_END
  515. };
  516. /* clang-format on */
  517. /**
  518. * Main process loop for the daemon. Run until quitting has been requested.
  519. */
  520. void game_mode_context_loop(GameModeContext *context)
  521. {
  522. /* Set up function to handle clean up of resources */
  523. atexit(clean_up);
  524. int ret = 0;
  525. /* Connect to the session bus */
  526. ret = sd_bus_open_user(&bus);
  527. if (ret < 0) {
  528. FATAL_ERROR("Failed to connect to the bus: %s\n", strerror(-ret));
  529. }
  530. /* Create the object to allow connections */
  531. ret = sd_bus_add_object_vtable(bus,
  532. &slot,
  533. "/com/feralinteractive/GameMode",
  534. "com.feralinteractive.GameMode",
  535. gamemode_vtable,
  536. context);
  537. if (ret < 0) {
  538. FATAL_ERROR("Failed to install GameMode object: %s\n", strerror(-ret));
  539. }
  540. ret = sd_bus_add_fallback_vtable(bus,
  541. &slot,
  542. GAME_PATH_PREFIX,
  543. "com.feralinteractive.GameMode.Game",
  544. game_vtable,
  545. game_object_find,
  546. context);
  547. if (ret < 0) {
  548. FATAL_ERROR("Failed to install Game object: %s\n", strerror(-ret));
  549. }
  550. ret = sd_bus_add_node_enumerator(bus, &slot, GAME_PATH_PREFIX, game_node_enumerator, context);
  551. if (ret < 0) {
  552. FATAL_ERROR("Failed to install Game object enumerator: %s\n", strerror(-ret));
  553. }
  554. /* Request our name */
  555. ret = sd_bus_request_name(bus, "com.feralinteractive.GameMode", 0);
  556. if (ret < 0) {
  557. FATAL_ERROR("Failed to acquire service name: %s\n", strerror(-ret));
  558. }
  559. LOG_MSG("Successfully initialised bus with name [%s]...\n", "com.feralinteractive.GameMode");
  560. sd_notifyf(0, "STATUS=%sGameMode is ready to be activated.%s\n", "\x1B[1;36m", "\x1B[0m");
  561. /* Now loop, waiting for callbacks */
  562. for (;;) {
  563. ret = sd_bus_process(bus, NULL);
  564. if (ret < 0) {
  565. FATAL_ERROR("Failure when processing the bus: %s\n", strerror(-ret));
  566. }
  567. /* We're done processing */
  568. if (ret > 0) {
  569. continue;
  570. }
  571. /* Wait for more */
  572. ret = sd_bus_wait(bus, (uint64_t)-1);
  573. if (ret < 0 && -ret != EINTR) {
  574. FATAL_ERROR("Failure when waiting on bus: %s\n", strerror(-ret));
  575. }
  576. }
  577. }
  578. /**
  579. * Attempts to inhibit the screensaver
  580. * Uses the "org.freedesktop.ScreenSaver" interface
  581. */
  582. static unsigned int screensaver_inhibit_cookie = 0;
  583. int game_mode_inhibit_screensaver(bool inhibit)
  584. {
  585. const char *service = "org.freedesktop.ScreenSaver";
  586. const char *object_path = "/org/freedesktop/ScreenSaver";
  587. const char *interface = "org.freedesktop.ScreenSaver";
  588. const char *function = inhibit ? "Inhibit" : "UnInhibit";
  589. sd_bus_message *msg = NULL;
  590. sd_bus *bus_local = NULL;
  591. sd_bus_error err;
  592. memset(&err, 0, sizeof(sd_bus_error));
  593. int result = -1;
  594. // Open the user bus
  595. int ret = sd_bus_open_user(&bus_local);
  596. if (ret < 0) {
  597. LOG_ERROR("Could not connect to user bus: %s\n", strerror(-ret));
  598. return -1;
  599. }
  600. if (inhibit) {
  601. ret = sd_bus_call_method(bus_local,
  602. service,
  603. object_path,
  604. interface,
  605. function,
  606. &err,
  607. &msg,
  608. "ss",
  609. "com.feralinteractive.GameMode",
  610. "GameMode Activated");
  611. } else {
  612. ret = sd_bus_call_method(bus_local,
  613. service,
  614. object_path,
  615. interface,
  616. function,
  617. &err,
  618. &msg,
  619. "u",
  620. screensaver_inhibit_cookie);
  621. }
  622. if (ret < 0) {
  623. LOG_ERROR(
  624. "Could not call %s on %s: %s\n"
  625. "\t%s\n"
  626. "\t%s\n",
  627. function,
  628. service,
  629. strerror(-ret),
  630. err.name,
  631. err.message);
  632. } else if (inhibit) {
  633. // Read the reply
  634. ret = sd_bus_message_read(msg, "u", &screensaver_inhibit_cookie);
  635. if (ret < 0) {
  636. LOG_ERROR("Failure to parse response from %s on %s: %s\n",
  637. function,
  638. service,
  639. strerror(-ret));
  640. } else {
  641. result = 0;
  642. }
  643. } else {
  644. result = 0;
  645. }
  646. sd_bus_unref(bus_local);
  647. return result;
  648. }