SDL  2.0
SDL_dbus.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22 #include "SDL_dbus.h"
23 
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 unsigned int screensaver_cookie = 0;
30 static SDL_DBusContext dbus;
31 
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 
38  #define SDL_DBUS_SYM(x) \
39  SDL_DBUS_SYM2(x, dbus_##x)
40 
41  SDL_DBUS_SYM(bus_get_private);
42  SDL_DBUS_SYM(bus_register);
43  SDL_DBUS_SYM(bus_add_match);
44  SDL_DBUS_SYM(connection_open_private);
45  SDL_DBUS_SYM(connection_set_exit_on_disconnect);
46  SDL_DBUS_SYM(connection_get_is_connected);
47  SDL_DBUS_SYM(connection_add_filter);
48  SDL_DBUS_SYM(connection_try_register_object_path);
49  SDL_DBUS_SYM(connection_send);
50  SDL_DBUS_SYM(connection_send_with_reply_and_block);
51  SDL_DBUS_SYM(connection_close);
52  SDL_DBUS_SYM(connection_unref);
53  SDL_DBUS_SYM(connection_flush);
54  SDL_DBUS_SYM(connection_read_write);
55  SDL_DBUS_SYM(connection_dispatch);
56  SDL_DBUS_SYM(message_is_signal);
57  SDL_DBUS_SYM(message_new_method_call);
58  SDL_DBUS_SYM(message_append_args);
59  SDL_DBUS_SYM(message_append_args_valist);
60  SDL_DBUS_SYM(message_get_args);
61  SDL_DBUS_SYM(message_get_args_valist);
62  SDL_DBUS_SYM(message_iter_init);
63  SDL_DBUS_SYM(message_iter_next);
64  SDL_DBUS_SYM(message_iter_get_basic);
65  SDL_DBUS_SYM(message_iter_get_arg_type);
66  SDL_DBUS_SYM(message_iter_recurse);
67  SDL_DBUS_SYM(message_unref);
68  SDL_DBUS_SYM(error_init);
69  SDL_DBUS_SYM(error_is_set);
70  SDL_DBUS_SYM(error_free);
71  SDL_DBUS_SYM(get_local_machine_id);
72  SDL_DBUS_SYM(free);
73  SDL_DBUS_SYM(free_string_array);
74  SDL_DBUS_SYM(shutdown);
75 
76  #undef SDL_DBUS_SYM
77  #undef SDL_DBUS_SYM2
78 
79  return 0;
80 }
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 
91 static int
92 LoadDBUSLibrary(void)
93 {
94  int retval = 0;
95  if (dbus_handle == NULL) {
96  dbus_handle = SDL_LoadObject(dbus_library);
97  if (dbus_handle == NULL) {
98  retval = -1;
99  /* Don't call SDL_SetError(): SDL_LoadObject already did. */
100  } else {
101  retval = LoadDBUSSyms();
102  if (retval < 0) {
103  UnloadDBUSLibrary();
104  }
105  }
106  }
107 
108  return retval;
109 }
110 
111 void
112 SDL_DBus_Init(void)
113 {
114  static SDL_bool is_dbus_available = SDL_TRUE;
115  if (!is_dbus_available) {
116  return; /* don't keep trying if this fails. */
117  }
118 
119  if (!dbus.session_conn) {
120  DBusError err;
121 
122  if (LoadDBUSLibrary() == -1) {
123  is_dbus_available = SDL_FALSE; /* can't load at all? Don't keep trying. */
124  return; /* oh well */
125  }
126 
127  dbus.error_init(&err);
128  dbus.session_conn = dbus.bus_get_private(DBUS_BUS_SESSION, &err);
129  if (!dbus.error_is_set(&err)) {
130  dbus.system_conn = dbus.bus_get_private(DBUS_BUS_SYSTEM, &err);
131  }
132  if (dbus.error_is_set(&err)) {
133  dbus.error_free(&err);
134  SDL_DBus_Quit();
135  is_dbus_available = SDL_FALSE;
136  return; /* oh well */
137  }
138  dbus.connection_set_exit_on_disconnect(dbus.system_conn, 0);
139  dbus.connection_set_exit_on_disconnect(dbus.session_conn, 0);
140  }
141 }
142 
143 void
144 SDL_DBus_Quit(void)
145 {
146  if (dbus.system_conn) {
147  dbus.connection_close(dbus.system_conn);
148  dbus.connection_unref(dbus.system_conn);
149  }
150  if (dbus.session_conn) {
151  dbus.connection_close(dbus.session_conn);
152  dbus.connection_unref(dbus.session_conn);
153  }
154 /* Don't do this - bug 3950
155  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.
156 */
157 #if 0
158  if (dbus.shutdown) {
159  dbus.shutdown();
160  }
161 #endif
162  SDL_zero(dbus);
163  UnloadDBUSLibrary();
164 }
165 
166 SDL_DBusContext *
167 SDL_DBus_GetContext(void)
168 {
169  if (!dbus_handle || !dbus.session_conn) {
170  SDL_DBus_Init();
171  }
172 
173  return (dbus_handle && dbus.session_conn) ? &dbus : NULL;
174 }
175 
176 static SDL_bool
177 SDL_DBus_CallMethodInternal(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, va_list ap)
178 {
180 
181  if (conn) {
182  DBusMessage *msg = dbus.message_new_method_call(node, path, interface, method);
183  if (msg) {
184  int firstarg;
185  va_list ap_reply;
186  va_copy(ap_reply, ap); /* copy the arg list so we don't compete with D-Bus for it */
187  firstarg = va_arg(ap, int);
188  if ((firstarg == DBUS_TYPE_INVALID) || dbus.message_append_args_valist(msg, firstarg, ap)) {
189  DBusMessage *reply = dbus.connection_send_with_reply_and_block(conn, msg, 300, NULL);
190  if (reply) {
191  /* skip any input args, get to output args. */
192  while ((firstarg = va_arg(ap_reply, int)) != DBUS_TYPE_INVALID) {
193  /* we assume D-Bus already validated all this. */
194  { void *dumpptr = va_arg(ap_reply, void*); (void) dumpptr; }
195  if (firstarg == DBUS_TYPE_ARRAY) {
196  { const int dumpint = va_arg(ap_reply, int); (void) dumpint; }
197  }
198  }
199  firstarg = va_arg(ap_reply, int);
200  if ((firstarg == DBUS_TYPE_INVALID) || dbus.message_get_args_valist(reply, NULL, firstarg, ap_reply)) {
201  retval = SDL_TRUE;
202  }
203  dbus.message_unref(reply);
204  }
205  }
206  va_end(ap_reply);
207  dbus.message_unref(msg);
208  }
209  }
210 
211  return retval;
212 }
213 
214 SDL_bool
215 SDL_DBus_CallMethodOnConnection(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, ...)
216 {
218  va_list ap;
219  va_start(ap, method);
220  retval = SDL_DBus_CallMethodInternal(conn, node, path, interface, method, ap);
221  va_end(ap);
222  return retval;
223 }
224 
225 SDL_bool
226 SDL_DBus_CallMethod(const char *node, const char *path, const char *interface, const char *method, ...)
227 {
229  va_list ap;
230  va_start(ap, method);
231  retval = SDL_DBus_CallMethodInternal(dbus.session_conn, node, path, interface, method, ap);
232  va_end(ap);
233  return retval;
234 }
235 
236 static SDL_bool
237 SDL_DBus_CallVoidMethodInternal(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, va_list ap)
238 {
240 
241  if (conn) {
242  DBusMessage *msg = dbus.message_new_method_call(node, path, interface, method);
243  if (msg) {
244  int firstarg = va_arg(ap, int);
245  if ((firstarg == DBUS_TYPE_INVALID) || dbus.message_append_args_valist(msg, firstarg, ap)) {
246  if (dbus.connection_send(conn, msg, NULL)) {
247  dbus.connection_flush(conn);
248  retval = SDL_TRUE;
249  }
250  }
251 
252  dbus.message_unref(msg);
253  }
254  }
255 
256  return retval;
257 }
258 
259 SDL_bool
260 SDL_DBus_CallVoidMethodOnConnection(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, ...)
261 {
263  va_list ap;
264  va_start(ap, method);
265  retval = SDL_DBus_CallVoidMethodInternal(conn, node, path, interface, method, ap);
266  va_end(ap);
267  return retval;
268 }
269 
270 SDL_bool
271 SDL_DBus_CallVoidMethod(const char *node, const char *path, const char *interface, const char *method, ...)
272 {
274  va_list ap;
275  va_start(ap, method);
276  retval = SDL_DBus_CallVoidMethodInternal(dbus.session_conn, node, path, interface, method, ap);
277  va_end(ap);
278  return retval;
279 }
280 
281 SDL_bool
282 SDL_DBus_QueryPropertyOnConnection(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *property, const int expectedtype, void *result)
283 {
285 
286  if (conn) {
287  DBusMessage *msg = dbus.message_new_method_call(node, path, "org.freedesktop.DBus.Properties", "Get");
288  if (msg) {
289  if (dbus.message_append_args(msg, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &property, DBUS_TYPE_INVALID)) {
290  DBusMessage *reply = dbus.connection_send_with_reply_and_block(conn, msg, 300, NULL);
291  if (reply) {
292  DBusMessageIter iter, sub;
293  dbus.message_iter_init(reply, &iter);
294  if (dbus.message_iter_get_arg_type(&iter) == DBUS_TYPE_VARIANT) {
295  dbus.message_iter_recurse(&iter, &sub);
296  if (dbus.message_iter_get_arg_type(&sub) == expectedtype) {
297  dbus.message_iter_get_basic(&sub, result);
298  retval = SDL_TRUE;
299  }
300  }
301  dbus.message_unref(reply);
302  }
303  }
304  dbus.message_unref(msg);
305  }
306  }
307 
308  return retval;
309 }
310 
311 SDL_bool
312 SDL_DBus_QueryProperty(const char *node, const char *path, const char *interface, const char *property, const int expectedtype, void *result)
313 {
314  return SDL_DBus_QueryPropertyOnConnection(dbus.session_conn, node, path, interface, property, expectedtype, result);
315 }
316 
317 
318 void
319 SDL_DBus_ScreensaverTickle(void)
320 {
321  if (screensaver_cookie == 0) { /* no need to tickle if we're inhibiting. */
322  /* 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. */
323  SDL_DBus_CallVoidMethod("org.gnome.ScreenSaver", "/org/gnome/ScreenSaver", "org.gnome.ScreenSaver", "SimulateUserActivity", DBUS_TYPE_INVALID);
324  SDL_DBus_CallVoidMethod("org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver", "org.freedesktop.ScreenSaver", "SimulateUserActivity", DBUS_TYPE_INVALID);
325  }
326 }
327 
328 SDL_bool
329 SDL_DBus_ScreensaverInhibit(SDL_bool inhibit)
330 {
331  if ( (inhibit && (screensaver_cookie != 0)) || (!inhibit && (screensaver_cookie == 0)) ) {
332  return SDL_TRUE;
333  } else {
334  const char *node = "org.freedesktop.ScreenSaver";
335  const char *path = "/org/freedesktop/ScreenSaver";
336  const char *interface = "org.freedesktop.ScreenSaver";
337 
338  if (inhibit) {
339  const char *app = "My SDL application";
340  const char *reason = "Playing a game";
341  if (!SDL_DBus_CallMethod(node, path, interface, "Inhibit",
342  DBUS_TYPE_STRING, &app, DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID,
343  DBUS_TYPE_UINT32, &screensaver_cookie, DBUS_TYPE_INVALID)) {
344  return SDL_FALSE;
345  }
346  return (screensaver_cookie != 0) ? SDL_TRUE : SDL_FALSE;
347  } else {
348  if (!SDL_DBus_CallVoidMethod(node, path, interface, "UnInhibit", DBUS_TYPE_UINT32, &screensaver_cookie, DBUS_TYPE_INVALID)) {
349  return SDL_FALSE;
350  }
351  screensaver_cookie = 0;
352  }
353  }
354 
355  return SDL_TRUE;
356 }
357 #endif
358 
359 /* vi: set ts=4 sw=4 expandtab: */
SDL_zero
#define SDL_zero(x)
Definition: SDL_stdinc.h:418
NULL
#define NULL
Definition: begin_code.h:167
SDL_UnloadObject
#define SDL_UnloadObject
Definition: SDL_dynapi_overrides.h:234
path
GLsizei const GLchar *const * path
Definition: SDL_opengl_glext.h:3733
result
GLuint64EXT * result
Definition: SDL_opengl_glext.h:9435
SDL_LoadObject
#define SDL_LoadObject
Definition: SDL_dynapi_overrides.h:232
retval
SDL_bool retval
Definition: testgamecontroller.c:65
SDL_dbus.h
sub
set set set set set set set set set set set set set set set set set set set set *set set set macro pixldst op &r &cond WK op &r &cond WK op &r &cond WK else op &m &cond &ia op &r &cond WK else op &m &cond &ia elseif elseif else error unsupported base if elseif elseif else error unsupported unaligned pixldst unaligned endm macro pixst base base else pixldst base endif endm macro PF base if bpp PF set rept prefetch_distance PF set OFFSET endr endif endm macro preload_leading_step2 base if bpp ifc DST PF PF else if bpp lsl PF PF lsl PF sub
Definition: pixman-arm-simd-asm.h:208
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:162
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
void
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 void
Definition: SDL_dynapi_procs.h:89
free
SDL_EventEntry * free
Definition: SDL_events.c:89
SDL_loadso.h