SDL_dbus.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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_hints.h"
  20. #include "SDL_dbus.h"
  21. #include "SDL_atomic.h"
  22. #include "SDL_sandbox.h"
  23. #include "../../stdlib/SDL_vacopy.h"
  24. #if SDL_USE_LIBDBUS
  25. /* we never link directly to libdbus. */
  26. #include "SDL_loadso.h"
  27. static const char *dbus_library = "libdbus-1.so.3";
  28. static void *dbus_handle = NULL;
  29. static char *inhibit_handle = NULL;
  30. static unsigned int screensaver_cookie = 0;
  31. static SDL_DBusContext dbus;
  32. static int
  33. LoadDBUSSyms(void)
  34. {
  35. #define SDL_DBUS_SYM2(x, y) \
  36. if (!(dbus.x = SDL_LoadFunction(dbus_handle, #y))) return -1
  37. #define SDL_DBUS_SYM(x) \
  38. SDL_DBUS_SYM2(x, dbus_##x)
  39. SDL_DBUS_SYM(bus_get_private);
  40. SDL_DBUS_SYM(bus_register);
  41. SDL_DBUS_SYM(bus_add_match);
  42. SDL_DBUS_SYM(connection_open_private);
  43. SDL_DBUS_SYM(connection_set_exit_on_disconnect);
  44. SDL_DBUS_SYM(connection_get_is_connected);
  45. SDL_DBUS_SYM(connection_add_filter);
  46. SDL_DBUS_SYM(connection_try_register_object_path);
  47. SDL_DBUS_SYM(connection_send);
  48. SDL_DBUS_SYM(connection_send_with_reply_and_block);
  49. SDL_DBUS_SYM(connection_close);
  50. SDL_DBUS_SYM(connection_unref);
  51. SDL_DBUS_SYM(connection_flush);
  52. SDL_DBUS_SYM(connection_read_write);
  53. SDL_DBUS_SYM(connection_dispatch);
  54. SDL_DBUS_SYM(message_is_signal);
  55. SDL_DBUS_SYM(message_new_method_call);
  56. SDL_DBUS_SYM(message_append_args);
  57. SDL_DBUS_SYM(message_append_args_valist);
  58. SDL_DBUS_SYM(message_iter_init_append);
  59. SDL_DBUS_SYM(message_iter_open_container);
  60. SDL_DBUS_SYM(message_iter_append_basic);
  61. SDL_DBUS_SYM(message_iter_close_container);
  62. SDL_DBUS_SYM(message_get_args);
  63. SDL_DBUS_SYM(message_get_args_valist);
  64. SDL_DBUS_SYM(message_iter_init);
  65. SDL_DBUS_SYM(message_iter_next);
  66. SDL_DBUS_SYM(message_iter_get_basic);
  67. SDL_DBUS_SYM(message_iter_get_arg_type);
  68. SDL_DBUS_SYM(message_iter_recurse);
  69. SDL_DBUS_SYM(message_unref);
  70. SDL_DBUS_SYM(threads_init_default);
  71. SDL_DBUS_SYM(error_init);
  72. SDL_DBUS_SYM(error_is_set);
  73. SDL_DBUS_SYM(error_free);
  74. SDL_DBUS_SYM(get_local_machine_id);
  75. SDL_DBUS_SYM(free);
  76. SDL_DBUS_SYM(free_string_array);
  77. SDL_DBUS_SYM(shutdown);
  78. #undef SDL_DBUS_SYM
  79. #undef SDL_DBUS_SYM2
  80. return 0;
  81. }
  82. static void
  83. UnloadDBUSLibrary(void)
  84. {
  85. if (dbus_handle != NULL) {
  86. SDL_UnloadObject(dbus_handle);
  87. dbus_handle = NULL;
  88. }
  89. }
  90. static int
  91. LoadDBUSLibrary(void)
  92. {
  93. int retval = 0;
  94. if (dbus_handle == NULL) {
  95. dbus_handle = SDL_LoadObject(dbus_library);
  96. if (dbus_handle == NULL) {
  97. retval = -1;
  98. /* Don't call SDL_SetError(): SDL_LoadObject already did. */
  99. } else {
  100. retval = LoadDBUSSyms();
  101. if (retval < 0) {
  102. UnloadDBUSLibrary();
  103. }
  104. }
  105. }
  106. return retval;
  107. }
  108. static SDL_SpinLock spinlock_dbus_init = 0;
  109. /* you must hold spinlock_dbus_init before calling this! */
  110. static void
  111. SDL_DBus_Init_Spinlocked(void)
  112. {
  113. static SDL_bool is_dbus_available = SDL_TRUE;
  114. if (!is_dbus_available) {
  115. return; /* don't keep trying if this fails. */
  116. }
  117. if (!dbus.session_conn) {
  118. DBusError err;
  119. if (LoadDBUSLibrary() == -1) {
  120. is_dbus_available = SDL_FALSE; /* can't load at all? Don't keep trying. */
  121. return; /* oh well */
  122. }
  123. if (!dbus.threads_init_default()) {
  124. is_dbus_available = SDL_FALSE;
  125. return;
  126. }
  127. dbus.error_init(&err);
  128. /* session bus is required */
  129. dbus.session_conn = dbus.bus_get_private(DBUS_BUS_SESSION, &err);
  130. if (dbus.error_is_set(&err)) {
  131. dbus.error_free(&err);
  132. SDL_DBus_Quit();
  133. is_dbus_available = SDL_FALSE;
  134. return; /* oh well */
  135. }
  136. dbus.connection_set_exit_on_disconnect(dbus.session_conn, 0);
  137. /* system bus is optional */
  138. dbus.system_conn = dbus.bus_get_private(DBUS_BUS_SYSTEM, &err);
  139. if (!dbus.error_is_set(&err)) {
  140. dbus.connection_set_exit_on_disconnect(dbus.system_conn, 0);
  141. }
  142. dbus.error_free(&err);
  143. }
  144. }
  145. void
  146. SDL_DBus_Init(void)
  147. {
  148. SDL_AtomicLock(&spinlock_dbus_init); /* make sure two threads can't init at same time, since this can happen before SDL_Init. */
  149. SDL_DBus_Init_Spinlocked();
  150. SDL_AtomicUnlock(&spinlock_dbus_init);
  151. }
  152. void
  153. SDL_DBus_Quit(void)
  154. {
  155. if (dbus.system_conn) {
  156. dbus.connection_close(dbus.system_conn);
  157. dbus.connection_unref(dbus.system_conn);
  158. }
  159. if (dbus.session_conn) {
  160. dbus.connection_close(dbus.session_conn);
  161. dbus.connection_unref(dbus.session_conn);
  162. }
  163. /* Don't do this - bug 3950
  164. 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.
  165. */
  166. #if 0
  167. if (dbus.shutdown) {
  168. dbus.shutdown();
  169. }
  170. #endif
  171. SDL_zero(dbus);
  172. UnloadDBUSLibrary();
  173. SDL_free(inhibit_handle);
  174. inhibit_handle = NULL;
  175. }
  176. SDL_DBusContext *
  177. SDL_DBus_GetContext(void)
  178. {
  179. if (!dbus_handle || !dbus.session_conn) {
  180. SDL_DBus_Init();
  181. }
  182. return (dbus_handle && dbus.session_conn) ? &dbus : NULL;
  183. }
  184. static SDL_bool
  185. SDL_DBus_CallMethodInternal(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, va_list ap)
  186. {
  187. SDL_bool retval = SDL_FALSE;
  188. if (conn) {
  189. DBusMessage *msg = dbus.message_new_method_call(node, path, interface, method);
  190. if (msg) {
  191. int firstarg;
  192. va_list ap_reply;
  193. va_copy(ap_reply, ap); /* copy the arg list so we don't compete with D-Bus for it */
  194. firstarg = va_arg(ap, int);
  195. if ((firstarg == DBUS_TYPE_INVALID) || dbus.message_append_args_valist(msg, firstarg, ap)) {
  196. DBusMessage *reply = dbus.connection_send_with_reply_and_block(conn, msg, 300, NULL);
  197. if (reply) {
  198. /* skip any input args, get to output args. */
  199. while ((firstarg = va_arg(ap_reply, int)) != DBUS_TYPE_INVALID) {
  200. /* we assume D-Bus already validated all this. */
  201. { void *dumpptr = va_arg(ap_reply, void*); (void) dumpptr; }
  202. if (firstarg == DBUS_TYPE_ARRAY) {
  203. { const int dumpint = va_arg(ap_reply, int); (void) dumpint; }
  204. }
  205. }
  206. firstarg = va_arg(ap_reply, int);
  207. if ((firstarg == DBUS_TYPE_INVALID) || dbus.message_get_args_valist(reply, NULL, firstarg, ap_reply)) {
  208. retval = SDL_TRUE;
  209. }
  210. dbus.message_unref(reply);
  211. }
  212. }
  213. va_end(ap_reply);
  214. dbus.message_unref(msg);
  215. }
  216. }
  217. return retval;
  218. }
  219. SDL_bool
  220. SDL_DBus_CallMethodOnConnection(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, ...)
  221. {
  222. SDL_bool retval;
  223. va_list ap;
  224. va_start(ap, method);
  225. retval = SDL_DBus_CallMethodInternal(conn, node, path, interface, method, ap);
  226. va_end(ap);
  227. return retval;
  228. }
  229. SDL_bool
  230. SDL_DBus_CallMethod(const char *node, const char *path, const char *interface, const char *method, ...)
  231. {
  232. SDL_bool retval;
  233. va_list ap;
  234. va_start(ap, method);
  235. retval = SDL_DBus_CallMethodInternal(dbus.session_conn, node, path, interface, method, ap);
  236. va_end(ap);
  237. return retval;
  238. }
  239. static SDL_bool
  240. SDL_DBus_CallVoidMethodInternal(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, va_list ap)
  241. {
  242. SDL_bool retval = SDL_FALSE;
  243. if (conn) {
  244. DBusMessage *msg = dbus.message_new_method_call(node, path, interface, method);
  245. if (msg) {
  246. int firstarg = va_arg(ap, int);
  247. if ((firstarg == DBUS_TYPE_INVALID) || dbus.message_append_args_valist(msg, firstarg, ap)) {
  248. if (dbus.connection_send(conn, msg, NULL)) {
  249. dbus.connection_flush(conn);
  250. retval = SDL_TRUE;
  251. }
  252. }
  253. dbus.message_unref(msg);
  254. }
  255. }
  256. return retval;
  257. }
  258. static SDL_bool
  259. SDL_DBus_CallWithBasicReply(DBusConnection *conn, DBusMessage *msg, const int expectedtype, void *result)
  260. {
  261. SDL_bool retval = SDL_FALSE;
  262. DBusMessage *reply = dbus.connection_send_with_reply_and_block(conn, msg, 300, NULL);
  263. if (reply) {
  264. DBusMessageIter iter, actual_iter;
  265. dbus.message_iter_init(reply, &iter);
  266. if (dbus.message_iter_get_arg_type(&iter) == DBUS_TYPE_VARIANT) {
  267. dbus.message_iter_recurse(&iter, &actual_iter);
  268. } else {
  269. actual_iter = iter;
  270. }
  271. if (dbus.message_iter_get_arg_type(&actual_iter) == expectedtype) {
  272. dbus.message_iter_get_basic(&actual_iter, result);
  273. retval = SDL_TRUE;
  274. }
  275. dbus.message_unref(reply);
  276. }
  277. return retval;
  278. }
  279. SDL_bool
  280. SDL_DBus_CallVoidMethodOnConnection(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, ...)
  281. {
  282. SDL_bool retval;
  283. va_list ap;
  284. va_start(ap, method);
  285. retval = SDL_DBus_CallVoidMethodInternal(conn, node, path, interface, method, ap);
  286. va_end(ap);
  287. return retval;
  288. }
  289. SDL_bool
  290. SDL_DBus_CallVoidMethod(const char *node, const char *path, const char *interface, const char *method, ...)
  291. {
  292. SDL_bool retval;
  293. va_list ap;
  294. va_start(ap, method);
  295. retval = SDL_DBus_CallVoidMethodInternal(dbus.session_conn, node, path, interface, method, ap);
  296. va_end(ap);
  297. return retval;
  298. }
  299. SDL_bool
  300. SDL_DBus_QueryPropertyOnConnection(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *property, const int expectedtype, void *result)
  301. {
  302. SDL_bool retval = SDL_FALSE;
  303. if (conn) {
  304. DBusMessage *msg = dbus.message_new_method_call(node, path, "org.freedesktop.DBus.Properties", "Get");
  305. if (msg) {
  306. if (dbus.message_append_args(msg, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &property, DBUS_TYPE_INVALID)) {
  307. retval = SDL_DBus_CallWithBasicReply(conn, msg, expectedtype, result);
  308. }
  309. dbus.message_unref(msg);
  310. }
  311. }
  312. return retval;
  313. }
  314. SDL_bool
  315. SDL_DBus_QueryProperty(const char *node, const char *path, const char *interface, const char *property, const int expectedtype, void *result)
  316. {
  317. return SDL_DBus_QueryPropertyOnConnection(dbus.session_conn, node, path, interface, property, expectedtype, result);
  318. }
  319. void
  320. SDL_DBus_ScreensaverTickle(void)
  321. {
  322. if (screensaver_cookie == 0 && inhibit_handle == NULL) { /* no need to tickle if we're inhibiting. */
  323. /* 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. */
  324. SDL_DBus_CallVoidMethod("org.gnome.ScreenSaver", "/org/gnome/ScreenSaver", "org.gnome.ScreenSaver", "SimulateUserActivity", DBUS_TYPE_INVALID);
  325. SDL_DBus_CallVoidMethod("org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver", "org.freedesktop.ScreenSaver", "SimulateUserActivity", DBUS_TYPE_INVALID);
  326. }
  327. }
  328. static SDL_bool
  329. SDL_DBus_AppendDictWithKeyValue(DBusMessageIter *iterInit, const char *key, const char *value)
  330. {
  331. DBusMessageIter iterDict, iterEntry, iterValue;
  332. if (!dbus.message_iter_open_container(iterInit, DBUS_TYPE_ARRAY, "{sv}", &iterDict))
  333. goto failed;
  334. if (!dbus.message_iter_open_container(&iterDict, DBUS_TYPE_DICT_ENTRY, NULL, &iterEntry))
  335. goto failed;
  336. if (!dbus.message_iter_append_basic(&iterEntry, DBUS_TYPE_STRING, &key))
  337. goto failed;
  338. if (!dbus.message_iter_open_container(&iterEntry, DBUS_TYPE_VARIANT, DBUS_TYPE_STRING_AS_STRING, &iterValue))
  339. goto failed;
  340. if (!dbus.message_iter_append_basic(&iterValue, DBUS_TYPE_STRING, &value))
  341. goto failed;
  342. if (!dbus.message_iter_close_container(&iterEntry, &iterValue)
  343. || !dbus.message_iter_close_container(&iterDict, &iterEntry)
  344. || !dbus.message_iter_close_container(iterInit, &iterDict)) {
  345. goto failed;
  346. }
  347. return SDL_TRUE;
  348. failed:
  349. /* message_iter_abandon_container_if_open() and message_iter_abandon_container() might be
  350. * missing if libdbus is too old. Instead, we just return without cleaning up any eventual
  351. * open container */
  352. return SDL_FALSE;
  353. }
  354. SDL_bool
  355. SDL_DBus_ScreensaverInhibit(SDL_bool inhibit)
  356. {
  357. const char *default_inhibit_reason = "Playing a game";
  358. if ( (inhibit && (screensaver_cookie != 0 || inhibit_handle != NULL))
  359. || (!inhibit && (screensaver_cookie == 0 && inhibit_handle == NULL)) ) {
  360. return SDL_TRUE;
  361. }
  362. if (!dbus.session_conn) {
  363. /* We either lost connection to the session bus or were not able to
  364. * load the D-Bus library at all. */
  365. return SDL_FALSE;
  366. }
  367. if (SDL_DetectSandbox() != SDL_SANDBOX_NONE) {
  368. const char *bus_name = "org.freedesktop.portal.Desktop";
  369. const char *path = "/org/freedesktop/portal/desktop";
  370. const char *interface = "org.freedesktop.portal.Inhibit";
  371. const char *window = ""; /* As a future improvement we could gather the X11 XID or Wayland surface identifier */
  372. static const unsigned int INHIBIT_IDLE = 8; /* Taken from the portal API reference */
  373. DBusMessageIter iterInit;
  374. if (inhibit) {
  375. DBusMessage *msg;
  376. SDL_bool retval = SDL_FALSE;
  377. const char *key = "reason";
  378. const char *reply = NULL;
  379. const char *reason = SDL_GetHint(SDL_HINT_SCREENSAVER_INHIBIT_ACTIVITY_NAME);
  380. if (!reason || !reason[0]) {
  381. reason = default_inhibit_reason;
  382. }
  383. msg = dbus.message_new_method_call(bus_name, path, interface, "Inhibit");
  384. if (!msg) {
  385. return SDL_FALSE;
  386. }
  387. if (!dbus.message_append_args(msg, DBUS_TYPE_STRING, &window, DBUS_TYPE_UINT32, &INHIBIT_IDLE, DBUS_TYPE_INVALID)) {
  388. dbus.message_unref(msg);
  389. return SDL_FALSE;
  390. }
  391. dbus.message_iter_init_append(msg, &iterInit);
  392. if (!SDL_DBus_AppendDictWithKeyValue(&iterInit, key, reason)) {
  393. dbus.message_unref(msg);
  394. return SDL_FALSE;
  395. }
  396. if (SDL_DBus_CallWithBasicReply(dbus.session_conn, msg, DBUS_TYPE_OBJECT_PATH, &reply)) {
  397. inhibit_handle = SDL_strdup(reply);
  398. retval = SDL_TRUE;
  399. }
  400. dbus.message_unref(msg);
  401. return retval;
  402. } else {
  403. if (!SDL_DBus_CallVoidMethod(bus_name, inhibit_handle, "org.freedesktop.portal.Request", "Close", DBUS_TYPE_INVALID)) {
  404. return SDL_FALSE;
  405. }
  406. SDL_free(inhibit_handle);
  407. inhibit_handle = NULL;
  408. }
  409. } else {
  410. const char *bus_name = "org.freedesktop.ScreenSaver";
  411. const char *path = "/org/freedesktop/ScreenSaver";
  412. const char *interface = "org.freedesktop.ScreenSaver";
  413. if (inhibit) {
  414. const char *app = SDL_GetHint(SDL_HINT_APP_NAME);
  415. const char *reason = SDL_GetHint(SDL_HINT_SCREENSAVER_INHIBIT_ACTIVITY_NAME);
  416. if (!app || !app[0]) {
  417. app = "My SDL application";
  418. }
  419. if (!reason || !reason[0]) {
  420. reason = default_inhibit_reason;
  421. }
  422. if (!SDL_DBus_CallMethod(bus_name, path, interface, "Inhibit",
  423. DBUS_TYPE_STRING, &app, DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID,
  424. DBUS_TYPE_UINT32, &screensaver_cookie, DBUS_TYPE_INVALID)) {
  425. return SDL_FALSE;
  426. }
  427. return (screensaver_cookie != 0) ? SDL_TRUE : SDL_FALSE;
  428. } else {
  429. if (!SDL_DBus_CallVoidMethod(bus_name, path, interface, "UnInhibit", DBUS_TYPE_UINT32, &screensaver_cookie, DBUS_TYPE_INVALID)) {
  430. return SDL_FALSE;
  431. }
  432. screensaver_cookie = 0;
  433. }
  434. }
  435. return SDL_TRUE;
  436. }
  437. #endif
  438. /* vi: set ts=4 sw=4 expandtab: */