dbus_messaging.c 9.7 KB

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