21 #include "../../SDL_internal.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;
35 #define SDL_DBUS_SYM2(x, y) \
36 if (!(dbus.x = SDL_LoadFunction(dbus_handle, #y))) return -1
38 #define SDL_DBUS_SYM(x) \
39 SDL_DBUS_SYM2(x, dbus_##x)
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);
73 SDL_DBUS_SYM(free_string_array);
74 SDL_DBUS_SYM(shutdown);
83 UnloadDBUSLibrary(
void)
85 if (dbus_handle !=
NULL) {
95 if (dbus_handle ==
NULL) {
97 if (dbus_handle ==
NULL) {
115 if (!is_dbus_available) {
119 if (!dbus.session_conn) {
122 if (LoadDBUSLibrary() == -1) {
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);
132 if (dbus.error_is_set(&err)) {
133 dbus.error_free(&err);
138 dbus.connection_set_exit_on_disconnect(dbus.system_conn, 0);
139 dbus.connection_set_exit_on_disconnect(dbus.session_conn, 0);
146 if (dbus.system_conn) {
147 dbus.connection_close(dbus.system_conn);
148 dbus.connection_unref(dbus.system_conn);
150 if (dbus.session_conn) {
151 dbus.connection_close(dbus.session_conn);
152 dbus.connection_unref(dbus.session_conn);
167 SDL_DBus_GetContext(
void)
169 if (!dbus_handle || !dbus.session_conn) {
173 return (dbus_handle && dbus.session_conn) ? &dbus :
NULL;
177 SDL_DBus_CallMethodInternal(DBusConnection *conn,
const char *node,
const char *
path,
const char *interface,
const char *method, va_list ap)
182 DBusMessage *msg = dbus.message_new_method_call(node,
path, interface, method);
186 va_copy(ap_reply, ap);
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);
192 while ((firstarg = va_arg(ap_reply,
int)) != DBUS_TYPE_INVALID) {
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; }
199 firstarg = va_arg(ap_reply,
int);
200 if ((firstarg == DBUS_TYPE_INVALID) || dbus.message_get_args_valist(reply,
NULL, firstarg, ap_reply)) {
203 dbus.message_unref(reply);
207 dbus.message_unref(msg);
215 SDL_DBus_CallMethodOnConnection(DBusConnection *conn,
const char *node,
const char *
path,
const char *interface,
const char *method, ...)
219 va_start(ap, method);
220 retval = SDL_DBus_CallMethodInternal(conn, node,
path, interface, method, ap);
226 SDL_DBus_CallMethod(
const char *node,
const char *
path,
const char *interface,
const char *method, ...)
230 va_start(ap, method);
231 retval = SDL_DBus_CallMethodInternal(dbus.session_conn, node,
path, interface, method, ap);
237 SDL_DBus_CallVoidMethodInternal(DBusConnection *conn,
const char *node,
const char *
path,
const char *interface,
const char *method, va_list ap)
242 DBusMessage *msg = dbus.message_new_method_call(node,
path, interface, method);
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);
252 dbus.message_unref(msg);
260 SDL_DBus_CallVoidMethodOnConnection(DBusConnection *conn,
const char *node,
const char *
path,
const char *interface,
const char *method, ...)
264 va_start(ap, method);
265 retval = SDL_DBus_CallVoidMethodInternal(conn, node,
path, interface, method, ap);
271 SDL_DBus_CallVoidMethod(
const char *node,
const char *
path,
const char *interface,
const char *method, ...)
275 va_start(ap, method);
276 retval = SDL_DBus_CallVoidMethodInternal(dbus.session_conn, node,
path, interface, method, ap);
282 SDL_DBus_QueryPropertyOnConnection(DBusConnection *conn,
const char *node,
const char *
path,
const char *interface,
const char *property,
const int expectedtype,
void *
result)
287 DBusMessage *msg = dbus.message_new_method_call(node,
path,
"org.freedesktop.DBus.Properties",
"Get");
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);
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);
301 dbus.message_unref(reply);
304 dbus.message_unref(msg);
312 SDL_DBus_QueryProperty(
const char *node,
const char *
path,
const char *interface,
const char *property,
const int expectedtype,
void *
result)
314 return SDL_DBus_QueryPropertyOnConnection(dbus.session_conn, node,
path, interface, property, expectedtype,
result);
319 SDL_DBus_ScreensaverTickle(
void)
321 if (screensaver_cookie == 0) {
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);
329 SDL_DBus_ScreensaverInhibit(
SDL_bool inhibit)
331 if ( (inhibit && (screensaver_cookie != 0)) || (!inhibit && (screensaver_cookie == 0)) ) {
334 const char *node =
"org.freedesktop.ScreenSaver";
335 const char *
path =
"/org/freedesktop/ScreenSaver";
336 const char *
interface = "org.freedesktop.ScreenSaver";
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)) {
348 if (!SDL_DBus_CallVoidMethod(node,
path, interface,
"UnInhibit", DBUS_TYPE_UINT32, &screensaver_cookie, DBUS_TYPE_INVALID)) {
351 screensaver_cookie = 0;