SDL_dbus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2018 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. #if SDL_USE_LIBDBUS
  21. /* we never link directly to libdbus. */
  22. #include "SDL_loadso.h"
  23. static const char *dbus_library = "libdbus-1.so.3";
  24. static void *dbus_handle = NULL;
  25. static unsigned int screensaver_cookie = 0;
  26. static SDL_DBusContext dbus;
  27. static int
  28. LoadDBUSSyms(void)
  29. {
  30. #define SDL_DBUS_SYM2(x, y) \
  31. if (!(dbus.x = SDL_LoadFunction(dbus_handle, #y))) return -1
  32. #define SDL_DBUS_SYM(x) \
  33. SDL_DBUS_SYM2(x, dbus_##x)
  34. SDL_DBUS_SYM(bus_get_private);
  35. SDL_DBUS_SYM(bus_register);
  36. SDL_DBUS_SYM(bus_add_match);
  37. SDL_DBUS_SYM(connection_open_private);
  38. SDL_DBUS_SYM(connection_set_exit_on_disconnect);
  39. SDL_DBUS_SYM(connection_get_is_connected);
  40. SDL_DBUS_SYM(connection_add_filter);
  41. SDL_DBUS_SYM(connection_try_register_object_path);
  42. SDL_DBUS_SYM(connection_send);
  43. SDL_DBUS_SYM(connection_send_with_reply_and_block);
  44. SDL_DBUS_SYM(connection_close);
  45. SDL_DBUS_SYM(connection_unref);
  46. SDL_DBUS_SYM(connection_flush);
  47. SDL_DBUS_SYM(connection_read_write);
  48. SDL_DBUS_SYM(connection_dispatch);
  49. SDL_DBUS_SYM(message_is_signal);
  50. SDL_DBUS_SYM(message_new_method_call);
  51. SDL_DBUS_SYM(message_append_args);
  52. SDL_DBUS_SYM(message_append_args_valist);
  53. SDL_DBUS_SYM(message_get_args);
  54. SDL_DBUS_SYM(message_get_args_valist);
  55. SDL_DBUS_SYM(message_iter_init);
  56. SDL_DBUS_SYM(message_iter_next);
  57. SDL_DBUS_SYM(message_iter_get_basic);
  58. SDL_DBUS_SYM(message_iter_get_arg_type);
  59. SDL_DBUS_SYM(message_iter_recurse);
  60. SDL_DBUS_SYM(message_unref);
  61. SDL_DBUS_SYM(error_init);
  62. SDL_DBUS_SYM(error_is_set);
  63. SDL_DBUS_SYM(error_free);
  64. SDL_DBUS_SYM(get_local_machine_id);
  65. SDL_DBUS_SYM(free);
  66. SDL_DBUS_SYM(free_string_array);
  67. SDL_DBUS_SYM(shutdown);
  68. #undef SDL_DBUS_SYM
  69. #undef SDL_DBUS_SYM2
  70. return 0;
  71. }
  72. static void
  73. UnloadDBUSLibrary(void)
  74. {
  75. if (dbus_handle != NULL) {
  76. SDL_UnloadObject(dbus_handle);
  77. dbus_handle = NULL;
  78. }
  79. }
  80. static int
  81. LoadDBUSLibrary(void)
  82. {
  83. int retval = 0;
  84. if (dbus_handle == NULL) {
  85. dbus_handle = SDL_LoadObject(dbus_library);
  86. if (dbus_handle == NULL) {
  87. retval = -1;
  88. /* Don't call SDL_SetError(): SDL_LoadObject already did. */
  89. } else {
  90. retval = LoadDBUSSyms();
  91. if (retval < 0) {
  92. UnloadDBUSLibrary();
  93. }
  94. }
  95. }
  96. return retval;
  97. }
  98. void
  99. SDL_DBus_Init(void)
  100. {
  101. if (!dbus.session_conn && LoadDBUSLibrary() != -1) {
  102. DBusError err;
  103. dbus.error_init(&err);
  104. dbus.session_conn = dbus.bus_get_private(DBUS_BUS_SESSION, &err);
  105. if (!dbus.error_is_set(&err)) {
  106. dbus.system_conn = dbus.bus_get_private(DBUS_BUS_SYSTEM, &err);
  107. }
  108. if (dbus.error_is_set(&err)) {
  109. dbus.error_free(&err);
  110. SDL_DBus_Quit();
  111. return; /* oh well */
  112. }
  113. dbus.connection_set_exit_on_disconnect(dbus.system_conn, 0);
  114. dbus.connection_set_exit_on_disconnect(dbus.session_conn, 0);
  115. }
  116. }
  117. void
  118. SDL_DBus_Quit(void)
  119. {
  120. if (dbus.system_conn) {
  121. dbus.connection_close(dbus.system_conn);
  122. dbus.connection_unref(dbus.system_conn);
  123. }
  124. if (dbus.session_conn) {
  125. dbus.connection_close(dbus.session_conn);
  126. dbus.connection_unref(dbus.session_conn);
  127. }
  128. /* Don't do this - bug 3950
  129. 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.
  130. */
  131. #if 0
  132. if (dbus.shutdown) {
  133. dbus.shutdown();
  134. }
  135. #endif
  136. SDL_zero(dbus);
  137. UnloadDBUSLibrary();
  138. }
  139. SDL_DBusContext *
  140. SDL_DBus_GetContext(void)
  141. {
  142. if(!dbus_handle || !dbus.session_conn){
  143. SDL_DBus_Init();
  144. }
  145. if(dbus_handle && dbus.session_conn){
  146. return &dbus;
  147. } else {
  148. return NULL;
  149. }
  150. }
  151. static SDL_bool
  152. SDL_DBus_CallMethodInternal(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, va_list ap)
  153. {
  154. SDL_bool retval = SDL_FALSE;
  155. if (conn) {
  156. DBusMessage *msg = dbus.message_new_method_call(node, path, interface, method);
  157. if (msg) {
  158. int firstarg;
  159. va_list ap_reply;
  160. va_copy(ap_reply, ap); /* copy the arg list so we don't compete with D-Bus for it */
  161. firstarg = va_arg(ap, int);
  162. if ((firstarg == DBUS_TYPE_INVALID) || dbus.message_append_args_valist(msg, firstarg, ap)) {
  163. DBusMessage *reply = dbus.connection_send_with_reply_and_block(conn, msg, 300, NULL);
  164. if (reply) {
  165. /* skip any input args, get to output args. */
  166. while ((firstarg = va_arg(ap_reply, int)) != DBUS_TYPE_INVALID) {
  167. /* we assume D-Bus already validated all this. */
  168. { void *dumpptr = va_arg(ap_reply, void*); (void) dumpptr; }
  169. if (firstarg == DBUS_TYPE_ARRAY) {
  170. { const int dumpint = va_arg(ap_reply, int); (void) dumpint; }
  171. }
  172. }
  173. firstarg = va_arg(ap_reply, int);
  174. if ((firstarg == DBUS_TYPE_INVALID) || dbus.message_get_args_valist(reply, NULL, firstarg, ap_reply)) {
  175. retval = SDL_TRUE;
  176. }
  177. dbus.message_unref(reply);
  178. }
  179. }
  180. va_end(ap_reply);
  181. dbus.message_unref(msg);
  182. }
  183. }
  184. return retval;
  185. }
  186. SDL_bool
  187. SDL_DBus_CallMethodOnConnection(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, ...)
  188. {
  189. SDL_bool retval;
  190. va_list ap;
  191. va_start(ap, method);
  192. retval = SDL_DBus_CallMethodInternal(conn, node, path, interface, method, ap);
  193. va_end(ap);
  194. return retval;
  195. }
  196. SDL_bool
  197. SDL_DBus_CallMethod(const char *node, const char *path, const char *interface, const char *method, ...)
  198. {
  199. SDL_bool retval;
  200. va_list ap;
  201. va_start(ap, method);
  202. retval = SDL_DBus_CallMethodInternal(dbus.session_conn, node, path, interface, method, ap);
  203. va_end(ap);
  204. return retval;
  205. }
  206. static SDL_bool
  207. SDL_DBus_CallVoidMethodInternal(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, va_list ap)
  208. {
  209. SDL_bool retval = SDL_FALSE;
  210. if (conn) {
  211. DBusMessage *msg = dbus.message_new_method_call(node, path, interface, method);
  212. if (msg) {
  213. int firstarg = va_arg(ap, int);
  214. if ((firstarg == DBUS_TYPE_INVALID) || dbus.message_append_args_valist(msg, firstarg, ap)) {
  215. if (dbus.connection_send(conn, msg, NULL)) {
  216. dbus.connection_flush(conn);
  217. retval = SDL_TRUE;
  218. }
  219. }
  220. dbus.message_unref(msg);
  221. }
  222. }
  223. return retval;
  224. }
  225. SDL_bool
  226. SDL_DBus_CallVoidMethodOnConnection(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, ...)
  227. {
  228. SDL_bool retval;
  229. va_list ap;
  230. va_start(ap, method);
  231. retval = SDL_DBus_CallVoidMethodInternal(conn, node, path, interface, method, ap);
  232. va_end(ap);
  233. return retval;
  234. }
  235. SDL_bool
  236. SDL_DBus_CallVoidMethod(const char *node, const char *path, const char *interface, const char *method, ...)
  237. {
  238. SDL_bool retval;
  239. va_list ap;
  240. va_start(ap, method);
  241. retval = SDL_DBus_CallVoidMethodInternal(dbus.session_conn, node, path, interface, method, ap);
  242. va_end(ap);
  243. return retval;
  244. }
  245. SDL_bool
  246. SDL_DBus_QueryPropertyOnConnection(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *property, const int expectedtype, void *result)
  247. {
  248. SDL_bool retval = SDL_FALSE;
  249. if (conn) {
  250. DBusMessage *msg = dbus.message_new_method_call(node, path, "org.freedesktop.DBus.Properties", "Get");
  251. if (msg) {
  252. if (dbus.message_append_args(msg, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &property, DBUS_TYPE_INVALID)) {
  253. DBusMessage *reply = dbus.connection_send_with_reply_and_block(conn, msg, 300, NULL);
  254. if (reply) {
  255. DBusMessageIter iter, sub;
  256. dbus.message_iter_init(reply, &iter);
  257. if (dbus.message_iter_get_arg_type(&iter) == DBUS_TYPE_VARIANT) {
  258. dbus.message_iter_recurse(&iter, &sub);
  259. if (dbus.message_iter_get_arg_type(&sub) == expectedtype) {
  260. dbus.message_iter_get_basic(&sub, result);
  261. retval = SDL_TRUE;
  262. }
  263. }
  264. dbus.message_unref(reply);
  265. }
  266. }
  267. dbus.message_unref(msg);
  268. }
  269. }
  270. return retval;
  271. }
  272. SDL_bool
  273. SDL_DBus_QueryProperty(const char *node, const char *path, const char *interface, const char *property, const int expectedtype, void *result)
  274. {
  275. return SDL_DBus_QueryPropertyOnConnection(dbus.session_conn, node, path, interface, property, expectedtype, result);
  276. }
  277. void
  278. SDL_DBus_ScreensaverTickle(void)
  279. {
  280. SDL_DBus_CallVoidMethod("org.gnome.ScreenSaver", "/org/gnome/ScreenSaver", "org.gnome.ScreenSaver", "SimulateUserActivity", DBUS_TYPE_INVALID);
  281. }
  282. SDL_bool
  283. SDL_DBus_ScreensaverInhibit(SDL_bool inhibit)
  284. {
  285. if ( (inhibit && (screensaver_cookie != 0)) || (!inhibit && (screensaver_cookie == 0)) ) {
  286. return SDL_TRUE;
  287. } else {
  288. const char *node = "org.freedesktop.ScreenSaver";
  289. const char *path = "/org/freedesktop/ScreenSaver";
  290. const char *interface = "org.freedesktop.ScreenSaver";
  291. if (inhibit) {
  292. const char *app = "My SDL application";
  293. const char *reason = "Playing a game";
  294. if (!SDL_DBus_CallMethod(node, path, interface, "Inhibit",
  295. DBUS_TYPE_STRING, &app, DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID,
  296. DBUS_TYPE_UINT32, &screensaver_cookie, DBUS_TYPE_INVALID)) {
  297. return SDL_FALSE;
  298. }
  299. return (screensaver_cookie != 0) ? SDL_TRUE : SDL_FALSE;
  300. } else {
  301. if (!SDL_DBus_CallVoidMethod(node, path, interface, "UnInhibit", DBUS_TYPE_UINT32, &screensaver_cookie, DBUS_TYPE_INVALID)) {
  302. return SDL_FALSE;
  303. }
  304. screensaver_cookie = 0;
  305. }
  306. }
  307. return SDL_TRUE;
  308. }
  309. #endif
  310. /* vi: set ts=4 sw=4 expandtab: */