SDL_dbus.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #include "SDL_dbus.h"
  20. #include "SDL_atomic.h"
  21. #if SDL_USE_LIBDBUS
  22. /* we never link directly to libdbus. */
  23. #include "SDL_loadso.h"
  24. static const char *dbus_library = "libdbus-1.so.3";
  25. static void *dbus_handle = NULL;
  26. static unsigned int screensaver_cookie = 0;
  27. static SDL_DBusContext dbus;
  28. static int
  29. LoadDBUSSyms(void)
  30. {
  31. #define SDL_DBUS_SYM2(x, y) \
  32. if (!(dbus.x = SDL_LoadFunction(dbus_handle, #y))) return -1
  33. #define SDL_DBUS_SYM(x) \
  34. SDL_DBUS_SYM2(x, dbus_##x)
  35. SDL_DBUS_SYM(bus_get_private);
  36. SDL_DBUS_SYM(bus_register);
  37. SDL_DBUS_SYM(bus_add_match);
  38. SDL_DBUS_SYM(connection_open_private);
  39. SDL_DBUS_SYM(connection_set_exit_on_disconnect);
  40. SDL_DBUS_SYM(connection_get_is_connected);
  41. SDL_DBUS_SYM(connection_add_filter);
  42. SDL_DBUS_SYM(connection_try_register_object_path);
  43. SDL_DBUS_SYM(connection_send);
  44. SDL_DBUS_SYM(connection_send_with_reply_and_block);
  45. SDL_DBUS_SYM(connection_close);
  46. SDL_DBUS_SYM(connection_unref);
  47. SDL_DBUS_SYM(connection_flush);
  48. SDL_DBUS_SYM(connection_read_write);
  49. SDL_DBUS_SYM(connection_dispatch);
  50. SDL_DBUS_SYM(message_is_signal);
  51. SDL_DBUS_SYM(message_new_method_call);
  52. SDL_DBUS_SYM(message_append_args);
  53. SDL_DBUS_SYM(message_append_args_valist);
  54. SDL_DBUS_SYM(message_iter_init_append);
  55. SDL_DBUS_SYM(message_iter_open_container);
  56. SDL_DBUS_SYM(message_iter_append_basic);
  57. SDL_DBUS_SYM(message_iter_close_container);
  58. SDL_DBUS_SYM(message_get_args);
  59. SDL_DBUS_SYM(message_get_args_valist);
  60. SDL_DBUS_SYM(message_iter_init);
  61. SDL_DBUS_SYM(message_iter_next);
  62. SDL_DBUS_SYM(message_iter_get_basic);
  63. SDL_DBUS_SYM(message_iter_get_arg_type);
  64. SDL_DBUS_SYM(message_iter_recurse);
  65. SDL_DBUS_SYM(message_unref);
  66. SDL_DBUS_SYM(threads_init_default);
  67. SDL_DBUS_SYM(error_init);
  68. SDL_DBUS_SYM(error_is_set);
  69. SDL_DBUS_SYM(error_free);
  70. SDL_DBUS_SYM(get_local_machine_id);
  71. SDL_DBUS_SYM(free);
  72. SDL_DBUS_SYM(free_string_array);
  73. SDL_DBUS_SYM(shutdown);
  74. #undef SDL_DBUS_SYM
  75. #undef SDL_DBUS_SYM2
  76. return 0;
  77. }
  78. static void
  79. UnloadDBUSLibrary(void)
  80. {
  81. if (dbus_handle != NULL) {
  82. SDL_UnloadObject(dbus_handle);
  83. dbus_handle = NULL;
  84. }
  85. }
  86. static int
  87. LoadDBUSLibrary(void)
  88. {
  89. int retval = 0;
  90. if (dbus_handle == NULL) {
  91. dbus_handle = SDL_LoadObject(dbus_library);
  92. if (dbus_handle == NULL) {
  93. retval = -1;
  94. /* Don't call SDL_SetError(): SDL_LoadObject already did. */
  95. } else {
  96. retval = LoadDBUSSyms();
  97. if (retval < 0) {
  98. UnloadDBUSLibrary();
  99. }
  100. }
  101. }
  102. return retval;
  103. }
  104. static SDL_SpinLock spinlock_dbus_init = 0;
  105. /* you must hold spinlock_dbus_init before calling this! */
  106. static void
  107. SDL_DBus_Init_Spinlocked(void)
  108. {
  109. static SDL_bool is_dbus_available = SDL_TRUE;
  110. if (!is_dbus_available) {
  111. return; /* don't keep trying if this fails. */
  112. }
  113. if (!dbus.session_conn) {
  114. DBusError err;
  115. if (LoadDBUSLibrary() == -1) {
  116. is_dbus_available = SDL_FALSE; /* can't load at all? Don't keep trying. */
  117. return; /* oh well */
  118. }
  119. if (!dbus.threads_init_default()) {
  120. is_dbus_available = SDL_FALSE;
  121. return;
  122. }
  123. dbus.error_init(&err);
  124. /* session bus is required */
  125. dbus.session_conn = dbus.bus_get_private(DBUS_BUS_SESSION, &err);
  126. if (dbus.error_is_set(&err)) {
  127. dbus.error_free(&err);
  128. SDL_DBus_Quit();
  129. is_dbus_available = SDL_FALSE;
  130. return; /* oh well */
  131. }
  132. dbus.connection_set_exit_on_disconnect(dbus.session_conn, 0);
  133. /* system bus is optional */
  134. dbus.system_conn = dbus.bus_get_private(DBUS_BUS_SYSTEM, &err);
  135. if (!dbus.error_is_set(&err)) {
  136. dbus.connection_set_exit_on_disconnect(dbus.system_conn, 0);
  137. }
  138. dbus.error_free(&err);
  139. }
  140. }
  141. void
  142. SDL_DBus_Init(void)
  143. {
  144. SDL_AtomicLock(&spinlock_dbus_init); /* make sure two threads can't init at same time, since this can happen before SDL_Init. */
  145. SDL_DBus_Init_Spinlocked();
  146. SDL_AtomicUnlock(&spinlock_dbus_init);
  147. }
  148. void
  149. SDL_DBus_Quit(void)
  150. {
  151. if (dbus.system_conn) {
  152. dbus.connection_close(dbus.system_conn);
  153. dbus.connection_unref(dbus.system_conn);
  154. }
  155. if (dbus.session_conn) {
  156. dbus.connection_close(dbus.session_conn);
  157. dbus.connection_unref(dbus.session_conn);
  158. }
  159. /* Don't do this - bug 3950
  160. dbus_shutdown() is a debug feature which closes all global resources in the dbus library. Calling this should be done by the app, not a library, because if there are multiple users of dbus in the process then SDL could shut it down even though another part is using it.
  161. */
  162. #if 0
  163. if (dbus.shutdown) {
  164. dbus.shutdown();
  165. }
  166. #endif
  167. SDL_zero(dbus);
  168. UnloadDBUSLibrary();
  169. }
  170. SDL_DBusContext *
  171. SDL_DBus_GetContext(void)
  172. {
  173. if (!dbus_handle || !dbus.session_conn) {
  174. SDL_DBus_Init();
  175. }
  176. return (dbus_handle && dbus.session_conn) ? &dbus : NULL;
  177. }
  178. static SDL_bool
  179. SDL_DBus_CallMethodInternal(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, va_list ap)
  180. {
  181. SDL_bool retval = SDL_FALSE;
  182. if (conn) {
  183. DBusMessage *msg = dbus.message_new_method_call(node, path, interface, method);
  184. if (msg) {
  185. int firstarg;
  186. va_list ap_reply;
  187. va_copy(ap_reply, ap); /* copy the arg list so we don't compete with D-Bus for it */
  188. firstarg = va_arg(ap, int);
  189. if ((firstarg == DBUS_TYPE_INVALID) || dbus.message_append_args_valist(msg, firstarg, ap)) {
  190. DBusMessage *reply = dbus.connection_send_with_reply_and_block(conn, msg, 300, NULL);
  191. if (reply) {
  192. /* skip any input args, get to output args. */
  193. while ((firstarg = va_arg(ap_reply, int)) != DBUS_TYPE_INVALID) {
  194. /* we assume D-Bus already validated all this. */
  195. { void *dumpptr = va_arg(ap_reply, void*); (void) dumpptr; }
  196. if (firstarg == DBUS_TYPE_ARRAY) {
  197. { const int dumpint = va_arg(ap_reply, int); (void) dumpint; }
  198. }
  199. }
  200. firstarg = va_arg(ap_reply, int);
  201. if ((firstarg == DBUS_TYPE_INVALID) || dbus.message_get_args_valist(reply, NULL, firstarg, ap_reply)) {
  202. retval = SDL_TRUE;
  203. }
  204. dbus.message_unref(reply);
  205. }
  206. }
  207. va_end(ap_reply);
  208. dbus.message_unref(msg);
  209. }
  210. }
  211. return retval;
  212. }
  213. SDL_bool
  214. SDL_DBus_CallMethodOnConnection(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, ...)
  215. {
  216. SDL_bool retval;
  217. va_list ap;
  218. va_start(ap, method);
  219. retval = SDL_DBus_CallMethodInternal(conn, node, path, interface, method, ap);
  220. va_end(ap);
  221. return retval;
  222. }
  223. SDL_bool
  224. SDL_DBus_CallMethod(const char *node, const char *path, const char *interface, const char *method, ...)
  225. {
  226. SDL_bool retval;
  227. va_list ap;
  228. va_start(ap, method);
  229. retval = SDL_DBus_CallMethodInternal(dbus.session_conn, node, path, interface, method, ap);
  230. va_end(ap);
  231. return retval;
  232. }
  233. static SDL_bool
  234. SDL_DBus_CallVoidMethodInternal(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, va_list ap)
  235. {
  236. SDL_bool retval = SDL_FALSE;
  237. if (conn) {
  238. DBusMessage *msg = dbus.message_new_method_call(node, path, interface, method);
  239. if (msg) {
  240. int firstarg = va_arg(ap, int);
  241. if ((firstarg == DBUS_TYPE_INVALID) || dbus.message_append_args_valist(msg, firstarg, ap)) {
  242. if (dbus.connection_send(conn, msg, NULL)) {
  243. dbus.connection_flush(conn);
  244. retval = SDL_TRUE;
  245. }
  246. }
  247. dbus.message_unref(msg);
  248. }
  249. }
  250. return retval;
  251. }
  252. SDL_bool
  253. SDL_DBus_CallVoidMethodOnConnection(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, ...)
  254. {
  255. SDL_bool retval;
  256. va_list ap;
  257. va_start(ap, method);
  258. retval = SDL_DBus_CallVoidMethodInternal(conn, node, path, interface, method, ap);
  259. va_end(ap);
  260. return retval;
  261. }
  262. SDL_bool
  263. SDL_DBus_CallVoidMethod(const char *node, const char *path, const char *interface, const char *method, ...)
  264. {
  265. SDL_bool retval;
  266. va_list ap;
  267. va_start(ap, method);
  268. retval = SDL_DBus_CallVoidMethodInternal(dbus.session_conn, node, path, interface, method, ap);
  269. va_end(ap);
  270. return retval;
  271. }
  272. SDL_bool
  273. SDL_DBus_QueryPropertyOnConnection(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *property, const int expectedtype, void *result)
  274. {
  275. SDL_bool retval = SDL_FALSE;
  276. if (conn) {
  277. DBusMessage *msg = dbus.message_new_method_call(node, path, "org.freedesktop.DBus.Properties", "Get");
  278. if (msg) {
  279. if (dbus.message_append_args(msg, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &property, DBUS_TYPE_INVALID)) {
  280. DBusMessage *reply = dbus.connection_send_with_reply_and_block(conn, msg, 300, NULL);
  281. if (reply) {
  282. DBusMessageIter iter, sub;
  283. dbus.message_iter_init(reply, &iter);
  284. if (dbus.message_iter_get_arg_type(&iter) == DBUS_TYPE_VARIANT) {
  285. dbus.message_iter_recurse(&iter, &sub);
  286. if (dbus.message_iter_get_arg_type(&sub) == expectedtype) {
  287. dbus.message_iter_get_basic(&sub, result);
  288. retval = SDL_TRUE;
  289. }
  290. }
  291. dbus.message_unref(reply);
  292. }
  293. }
  294. dbus.message_unref(msg);
  295. }
  296. }
  297. return retval;
  298. }
  299. SDL_bool
  300. SDL_DBus_QueryProperty(const char *node, const char *path, const char *interface, const char *property, const int expectedtype, void *result)
  301. {
  302. return SDL_DBus_QueryPropertyOnConnection(dbus.session_conn, node, path, interface, property, expectedtype, result);
  303. }
  304. void
  305. SDL_DBus_ScreensaverTickle(void)
  306. {
  307. if (screensaver_cookie == 0) { /* no need to tickle if we're inhibiting. */
  308. /* org.gnome.ScreenSaver is the legacy interface, but it'll either do nothing or just be a second harmless tickle on newer systems, so we leave it for now. */
  309. SDL_DBus_CallVoidMethod("org.gnome.ScreenSaver", "/org/gnome/ScreenSaver", "org.gnome.ScreenSaver", "SimulateUserActivity", DBUS_TYPE_INVALID);
  310. SDL_DBus_CallVoidMethod("org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver", "org.freedesktop.ScreenSaver", "SimulateUserActivity", DBUS_TYPE_INVALID);
  311. }
  312. }
  313. SDL_bool
  314. SDL_DBus_ScreensaverInhibit(SDL_bool inhibit)
  315. {
  316. if ( (inhibit && (screensaver_cookie != 0)) || (!inhibit && (screensaver_cookie == 0)) ) {
  317. return SDL_TRUE;
  318. } else {
  319. const char *node = "org.freedesktop.ScreenSaver";
  320. const char *path = "/org/freedesktop/ScreenSaver";
  321. const char *interface = "org.freedesktop.ScreenSaver";
  322. if (inhibit) {
  323. const char *app = "My SDL application";
  324. const char *reason = "Playing a game";
  325. if (!SDL_DBus_CallMethod(node, path, interface, "Inhibit",
  326. DBUS_TYPE_STRING, &app, DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID,
  327. DBUS_TYPE_UINT32, &screensaver_cookie, DBUS_TYPE_INVALID)) {
  328. return SDL_FALSE;
  329. }
  330. return (screensaver_cookie != 0) ? SDL_TRUE : SDL_FALSE;
  331. } else {
  332. if (!SDL_DBus_CallVoidMethod(node, path, interface, "UnInhibit", DBUS_TYPE_UINT32, &screensaver_cookie, DBUS_TYPE_INVALID)) {
  333. return SDL_FALSE;
  334. }
  335. screensaver_cookie = 0;
  336. }
  337. }
  338. return SDL_TRUE;
  339. }
  340. #endif
  341. /* vi: set ts=4 sw=4 expandtab: */