src/core/linux/SDL_dbus.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 28 May 2017 07:08:10 -0400
changeset 11041 5aebcca5ff18
parent 10737 3406a0f8b041
child 11042 303c875e47f0
permissions -rw-r--r--
linux: Make system D-Bus connection available (in addition to session).
alex@8889
     1
/*
alex@8889
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 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) \
alex@8889
    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);
alex@8889
    59
    SDL_DBUS_SYM(message_get_args);
alex@8889
    60
    SDL_DBUS_SYM(message_iter_init);
alex@8889
    61
    SDL_DBUS_SYM(message_iter_next);
alex@8889
    62
    SDL_DBUS_SYM(message_iter_get_basic);
alex@8889
    63
    SDL_DBUS_SYM(message_iter_get_arg_type);
alex@8889
    64
    SDL_DBUS_SYM(message_iter_recurse);
alex@8889
    65
    SDL_DBUS_SYM(message_unref);
alex@8889
    66
    SDL_DBUS_SYM(error_init);
alex@8889
    67
    SDL_DBUS_SYM(error_is_set);
alex@8889
    68
    SDL_DBUS_SYM(error_free);
alex@8889
    69
    SDL_DBUS_SYM(get_local_machine_id);
alex@8889
    70
    SDL_DBUS_SYM(free);
alex@8977
    71
    SDL_DBUS_SYM(shutdown);
alex@8889
    72
alex@8889
    73
    #undef SDL_DBUS_SYM
alex@8889
    74
    #undef SDL_DBUS_SYM2
alex@8889
    75
alex@8889
    76
    return 0;
alex@8889
    77
}
alex@8889
    78
alex@8889
    79
static void
alex@8889
    80
UnloadDBUSLibrary(void)
alex@8889
    81
{
alex@8889
    82
    if (dbus_handle != NULL) {
alex@8889
    83
        SDL_UnloadObject(dbus_handle);
alex@8889
    84
        dbus_handle = NULL;
alex@8889
    85
    }
alex@8889
    86
}
alex@8889
    87
alex@8889
    88
static int
alex@8889
    89
LoadDBUSLibrary(void)
alex@8889
    90
{
alex@8889
    91
    int retval = 0;
alex@8889
    92
    if (dbus_handle == NULL) {
alex@8889
    93
        dbus_handle = SDL_LoadObject(dbus_library);
alex@8889
    94
        if (dbus_handle == NULL) {
alex@8889
    95
            retval = -1;
alex@8889
    96
            /* Don't call SDL_SetError(): SDL_LoadObject already did. */
alex@8889
    97
        } else {
icculus@9699
    98
            retval = LoadDBUSSyms();
alex@8889
    99
            if (retval < 0) {
alex@8889
   100
                UnloadDBUSLibrary();
alex@8889
   101
            }
alex@8889
   102
        }
alex@8889
   103
    }
alex@8889
   104
alex@8889
   105
    return retval;
alex@8889
   106
}
alex@8889
   107
alex@8889
   108
void
alex@8889
   109
SDL_DBus_Init(void)
alex@8889
   110
{
alex@8977
   111
    if (!dbus.session_conn && LoadDBUSLibrary() != -1) {
alex@8889
   112
        DBusError err;
alex@8889
   113
        dbus.error_init(&err);
alex@8889
   114
        dbus.session_conn = dbus.bus_get_private(DBUS_BUS_SESSION, &err);
icculus@11041
   115
        if (!dbus.error_is_set(&err)) {
icculus@11041
   116
            dbus.system_conn = dbus.bus_get_private(DBUS_BUS_SYSTEM, &err);
icculus@11041
   117
        }
alex@8889
   118
        if (dbus.error_is_set(&err)) {
alex@8889
   119
            dbus.error_free(&err);
icculus@11041
   120
            SDL_DBus_Quit();
alex@8889
   121
            return;  /* oh well */
alex@8889
   122
        }
icculus@11041
   123
        dbus.connection_set_exit_on_disconnect(dbus.system_conn, 0);
alex@8889
   124
        dbus.connection_set_exit_on_disconnect(dbus.session_conn, 0);
alex@8889
   125
    }
alex@8889
   126
}
alex@8889
   127
alex@8889
   128
void
alex@8889
   129
SDL_DBus_Quit(void)
alex@8889
   130
{
icculus@11041
   131
    if (dbus.system_conn) {
icculus@11041
   132
        dbus.connection_close(dbus.system_conn);
icculus@11041
   133
        dbus.connection_unref(dbus.system_conn);
icculus@11041
   134
    }
alex@8889
   135
    if (dbus.session_conn) {
alex@8889
   136
        dbus.connection_close(dbus.session_conn);
alex@8889
   137
        dbus.connection_unref(dbus.session_conn);
icculus@11041
   138
    }
icculus@11041
   139
    if (dbus.shutdown) {
alex@8977
   140
        dbus.shutdown();
alex@8889
   141
    }
icculus@11041
   142
    SDL_zero(dbus);
alex@8889
   143
    UnloadDBUSLibrary();
alex@8889
   144
}
alex@8889
   145
alex@8889
   146
SDL_DBusContext *
alex@8889
   147
SDL_DBus_GetContext(void)
alex@8889
   148
{
alex@8889
   149
    if(!dbus_handle || !dbus.session_conn){
alex@8889
   150
        SDL_DBus_Init();
alex@8889
   151
    }
alex@8889
   152
    
alex@8889
   153
    if(dbus_handle && dbus.session_conn){
alex@8889
   154
        return &dbus;
alex@8889
   155
    } else {
alex@8889
   156
        return NULL;
alex@8889
   157
    }
alex@8889
   158
}
alex@8889
   159
alex@8889
   160
void
alex@8889
   161
SDL_DBus_ScreensaverTickle(void)
alex@8889
   162
{
alex@8889
   163
    DBusConnection *conn = dbus.session_conn;
alex@8889
   164
    if (conn != NULL) {
alex@8889
   165
        DBusMessage *msg = dbus.message_new_method_call("org.gnome.ScreenSaver",
alex@8889
   166
                                                        "/org/gnome/ScreenSaver",
alex@8889
   167
                                                        "org.gnome.ScreenSaver",
alex@8889
   168
                                                        "SimulateUserActivity");
alex@8889
   169
        if (msg != NULL) {
alex@8889
   170
            if (dbus.connection_send(conn, msg, NULL)) {
alex@8889
   171
                dbus.connection_flush(conn);
alex@8889
   172
            }
alex@8889
   173
            dbus.message_unref(msg);
alex@8889
   174
        }
alex@8889
   175
    }
alex@8889
   176
}
alex@8889
   177
alex@8889
   178
SDL_bool
alex@8889
   179
SDL_DBus_ScreensaverInhibit(SDL_bool inhibit)
alex@8889
   180
{
alex@8889
   181
    DBusConnection *conn = dbus.session_conn;
alex@8889
   182
alex@8889
   183
    if (conn == NULL)
alex@8889
   184
        return SDL_FALSE;
alex@8889
   185
alex@8889
   186
    if (inhibit &&
alex@8889
   187
        screensaver_cookie != 0)
alex@8889
   188
        return SDL_TRUE;
alex@8889
   189
    if (!inhibit &&
alex@8889
   190
        screensaver_cookie == 0)
alex@8889
   191
        return SDL_TRUE;
alex@8889
   192
alex@8889
   193
    if (inhibit) {
alex@8889
   194
        const char *app = "My SDL application";
alex@8889
   195
        const char *reason = "Playing a game";
alex@8889
   196
alex@8889
   197
        DBusMessage *msg = dbus.message_new_method_call("org.freedesktop.ScreenSaver",
alex@8889
   198
                                                         "/org/freedesktop/ScreenSaver",
alex@8889
   199
                                                         "org.freedesktop.ScreenSaver",
alex@8889
   200
                                                         "Inhibit");
alex@8889
   201
        if (msg != NULL) {
alex@8889
   202
            dbus.message_append_args (msg,
alex@8889
   203
                                      DBUS_TYPE_STRING, &app,
alex@8889
   204
                                      DBUS_TYPE_STRING, &reason,
alex@8889
   205
                                      DBUS_TYPE_INVALID);
alex@8889
   206
        }
alex@8889
   207
alex@8889
   208
        if (msg != NULL) {
alex@8889
   209
            DBusMessage *reply;
alex@8889
   210
alex@8889
   211
            reply = dbus.connection_send_with_reply_and_block(conn, msg, 300, NULL);
alex@8889
   212
            if (reply) {
alex@8889
   213
                if (!dbus.message_get_args(reply, NULL,
alex@8889
   214
                                           DBUS_TYPE_UINT32, &screensaver_cookie,
alex@8889
   215
                                           DBUS_TYPE_INVALID))
alex@8889
   216
                    screensaver_cookie = 0;
alex@8889
   217
                dbus.message_unref(reply);
alex@8889
   218
            }
alex@8889
   219
alex@8889
   220
            dbus.message_unref(msg);
alex@8889
   221
        }
alex@8889
   222
alex@8889
   223
        if (screensaver_cookie == 0) {
alex@8889
   224
            return SDL_FALSE;
alex@8889
   225
        }
alex@8889
   226
        return SDL_TRUE;
alex@8889
   227
    } else {
alex@8889
   228
        DBusMessage *msg = dbus.message_new_method_call("org.freedesktop.ScreenSaver",
alex@8889
   229
                                                        "/org/freedesktop/ScreenSaver",
alex@8889
   230
                                                        "org.freedesktop.ScreenSaver",
alex@8889
   231
                                                        "UnInhibit");
alex@8889
   232
        dbus.message_append_args (msg,
alex@8889
   233
                                  DBUS_TYPE_UINT32, &screensaver_cookie,
alex@8889
   234
                                  DBUS_TYPE_INVALID);
alex@8889
   235
        if (msg != NULL) {
alex@8889
   236
            if (dbus.connection_send(conn, msg, NULL)) {
alex@8889
   237
                dbus.connection_flush(conn);
alex@8889
   238
            }
alex@8889
   239
            dbus.message_unref(msg);
alex@8889
   240
        }
alex@8889
   241
alex@8889
   242
        screensaver_cookie = 0;
alex@8889
   243
        return SDL_TRUE;
alex@8889
   244
    }
alex@8889
   245
}
alex@8889
   246
#endif
slouken@10660
   247
slouken@10660
   248
/* vi: set ts=4 sw=4 expandtab: */