gamemode-dbus.c 19 KB

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