src/core/linux/SDL_dbus.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 05 Dec 2018 16:51:22 -0500
changeset 12456 f8041c025fd3
parent 12437 ab67e41946fe
child 12503 806492103856
permissions -rw-r--r--
linux: Move SDL_LinuxSetThreadPriority() elsewhere to fix build.

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