Replaced Gnome screensaver hack with D-Bus messages to org.gnome.ScreenSaver.
authorRyan C. Gordon <icculus@icculus.org>
Fri, 28 Dec 2012 03:46:55 -0500
changeset 6774ad8522052ce6
parent 6773 5ebc5a9e35b4
child 6775 ca20a61fe03b
Replaced Gnome screensaver hack with D-Bus messages to org.gnome.ScreenSaver.
CMakeLists.txt
configure.in
include/SDL_config.h.cmake
include/SDL_config.h.in
src/video/x11/SDL_x11events.c
src/video/x11/SDL_x11video.c
src/video/x11/SDL_x11video.h
     1.1 --- a/CMakeLists.txt	Wed Dec 26 22:26:44 2012 -0800
     1.2 +++ b/CMakeLists.txt	Fri Dec 28 03:46:55 2012 -0500
     1.3 @@ -650,6 +650,9 @@
     1.4      endif(SDL_HAPTIC AND HAVE_INPUT_EVENTS)
     1.5  
     1.6      check_include_file("libudev.h" HAVE_LIBUDEV_H)
     1.7 +
     1.8 +    # !!! FIXME: this needs pkg-config to find the include path, I think.
     1.9 +    check_include_file("dbus/dbus.h" HAVE_DBUS_DBUS_H)
    1.10    endif(LINUX)
    1.11  
    1.12    if(INPUT_TSLIB)
     2.1 --- a/configure.in	Wed Dec 26 22:26:44 2012 -0800
     2.2 +++ b/configure.in	Fri Dec 28 03:46:55 2012 -0500
     2.3 @@ -1683,6 +1683,30 @@
     2.4      fi
     2.5  }
     2.6  
     2.7 +dnl See if the platform offers libdbus for various IPC techniques.
     2.8 +CheckDBus()
     2.9 +{
    2.10 +    AC_ARG_ENABLE(dbus,
    2.11 +AC_HELP_STRING([--enable-dbus], [enable D-Bus support [[default=yes]]]),
    2.12 +                        , enable_dbus=yes)
    2.13 +    if test x$enable_dbus = xyes; then
    2.14 +        AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
    2.15 +        if test x$PKG_CONFIG != xno; then
    2.16 +            DBUS_CFLAGS=`$PKG_CONFIG --cflags dbus-1`
    2.17 +            save_CFLAGS="$CFLAGS"
    2.18 +            CFLAGS="$save_CFLAGS $DBUS_CFLAGS"
    2.19 +            AC_CHECK_HEADER(dbus/dbus.h,
    2.20 +                            have_dbus_dbus_h_hdr=yes,
    2.21 +                            have_dbus_dbus_h_hdr=no)
    2.22 +            CFLAGS="$save_CFLAGS"
    2.23 +            if test x$have_dbus_dbus_h_hdr = xyes; then
    2.24 +                AC_DEFINE(HAVE_DBUS_DBUS_H, 1, [ ])
    2.25 +                EXTRA_CFLAGS="$EXTRA_CFLAGS $DBUS_CFLAGS"
    2.26 +            fi
    2.27 +        fi
    2.28 +    fi
    2.29 +}
    2.30 +
    2.31  dnl See if we can use the Touchscreen input library
    2.32  CheckTslib()
    2.33  {
    2.34 @@ -2208,6 +2232,7 @@
    2.35          CheckOpenGLX11
    2.36          CheckOpenGLESX11
    2.37          CheckLibUDev
    2.38 +        CheckDBus
    2.39          CheckInputEvents
    2.40          CheckTslib
    2.41          CheckUSBHID
     3.1 --- a/include/SDL_config.h.cmake	Wed Dec 26 22:26:44 2012 -0800
     3.2 +++ b/include/SDL_config.h.cmake	Fri Dec 28 03:46:55 2012 -0500
     3.3 @@ -66,6 +66,7 @@
     3.4  #cmakedefine HAVE_ALTIVEC_H 1
     3.5  #cmakedefine HAVE_PTHREAD_NP_H 1
     3.6  #cmakedefine HAVE_LIBUDEV_H 1
     3.7 +#cmakedefine HAVE_DBUS_DBUS_H 1
     3.8  
     3.9  /* C library functions */
    3.10  #cmakedefine HAVE_MALLOC 1
     4.1 --- a/include/SDL_config.h.in	Wed Dec 26 22:26:44 2012 -0800
     4.2 +++ b/include/SDL_config.h.in	Fri Dec 28 03:46:55 2012 -0500
     4.3 @@ -71,6 +71,7 @@
     4.4  #undef HAVE_ALTIVEC_H
     4.5  #undef HAVE_PTHREAD_NP_H
     4.6  #undef HAVE_LIBUDEV_H
     4.7 +#undef HAVE_DBUS_DBUS_H
     4.8  
     4.9  /* C library functions */
    4.10  #undef HAVE_MALLOC
     5.1 --- a/src/video/x11/SDL_x11events.c	Wed Dec 26 22:26:44 2012 -0800
     5.2 +++ b/src/video/x11/SDL_x11events.c	Fri Dec 28 03:46:55 2012 -0500
     5.3 @@ -675,7 +675,7 @@
     5.4  
     5.5  /* !!! FIXME: this should be exposed in a header, or something. */
     5.6  int SDL_GetNumTouch(void);
     5.7 -
     5.8 +void SDL_dbus_screensaver_tickle(_THIS);
     5.9  
    5.10  void
    5.11  X11_PumpEvents(_THIS)
    5.12 @@ -688,6 +688,11 @@
    5.13          if (!data->screensaver_activity ||
    5.14              (int) (now - data->screensaver_activity) >= 30000) {
    5.15              XResetScreenSaver(data->display);
    5.16 +
    5.17 +            #if SDL_USE_LIBDBUS
    5.18 +            SDL_dbus_screensaver_tickle(_this);
    5.19 +            #endif
    5.20 +
    5.21              data->screensaver_activity = now;
    5.22          }
    5.23      }   
    5.24 @@ -785,33 +790,6 @@
    5.25  #endif
    5.26  }
    5.27  
    5.28 -/* This is so wrong it hurts */
    5.29 -#define GNOME_SCREENSAVER_HACK
    5.30 -#ifdef GNOME_SCREENSAVER_HACK
    5.31 -#include <unistd.h>
    5.32 -static pid_t screensaver_inhibit_pid;
    5.33 -static void
    5.34 -gnome_screensaver_disable()
    5.35 -{
    5.36 -    screensaver_inhibit_pid = fork();
    5.37 -    if (screensaver_inhibit_pid == 0) {
    5.38 -        close(0);
    5.39 -        close(1);
    5.40 -        close(2);
    5.41 -        execl("/usr/bin/gnome-screensaver-command",
    5.42 -              "gnome-screensaver-command",
    5.43 -              "--inhibit",
    5.44 -              "--reason",
    5.45 -              "GNOME screensaver doesn't respect MIT-SCREEN-SAVER", NULL);
    5.46 -        exit(2);
    5.47 -    }
    5.48 -}
    5.49 -static void
    5.50 -gnome_screensaver_enable()
    5.51 -{
    5.52 -    kill(screensaver_inhibit_pid, 15);
    5.53 -}
    5.54 -#endif
    5.55  
    5.56  void
    5.57  X11_SuspendScreenSaver(_THIS)
    5.58 @@ -835,11 +813,9 @@
    5.59      }
    5.60  #endif
    5.61  
    5.62 -#ifdef GNOME_SCREENSAVER_HACK
    5.63 +#if SDL_USE_LIBDBUS
    5.64      if (_this->suspend_screensaver) {
    5.65 -        gnome_screensaver_disable();
    5.66 -    } else {
    5.67 -        gnome_screensaver_enable();
    5.68 +        SDL_dbus_screensaver_tickle(_this);
    5.69      }
    5.70  #endif
    5.71  }
     6.1 --- a/src/video/x11/SDL_x11video.c	Wed Dec 26 22:26:44 2012 -0800
     6.2 +++ b/src/video/x11/SDL_x11video.c	Fri Dec 28 03:46:55 2012 -0500
     6.3 @@ -39,6 +39,143 @@
     6.4  #include "SDL_x11opengles.h"
     6.5  #endif
     6.6  
     6.7 +/* !!! FIXME: move dbus stuff to somewhere under src/core/linux ... */
     6.8 +#if SDL_USE_LIBDBUS
     6.9 +/* we never link directly to libdbus. */
    6.10 +#include "SDL_loadso.h"
    6.11 +static const char *dbus_library = "libdbus-1.so.3";
    6.12 +static void *dbus_handle = NULL;
    6.13 +
    6.14 +/* !!! FIXME: this is kinda ugly. */
    6.15 +static SDL_bool
    6.16 +load_dbus_sym(const char *fn, void **addr)
    6.17 +{
    6.18 +    *addr = SDL_LoadFunction(dbus_handle, fn);
    6.19 +    if (*addr == NULL) {
    6.20 +        /* Don't call SDL_SetError(): SDL_LoadFunction already did. */
    6.21 +        return SDL_FALSE;
    6.22 +    }
    6.23 +
    6.24 +    return SDL_TRUE;
    6.25 +}
    6.26 +
    6.27 +/* libdbus entry points... */
    6.28 +static DBusConnection *(*DBUS_dbus_bus_get_private)(DBusBusType, DBusError *) = NULL;
    6.29 +static void (*DBUS_dbus_connection_set_exit_on_disconnect)(DBusConnection *, dbus_bool_t) = NULL;
    6.30 +static dbus_bool_t (*DBUS_dbus_connection_send)(DBusConnection *, DBusMessage *, dbus_uint32_t *) = NULL;
    6.31 +static void (*DBUS_dbus_connection_close)(DBusConnection *) = NULL;
    6.32 +static void (*DBUS_dbus_connection_unref)(DBusConnection *) = NULL;
    6.33 +static void (*DBUS_dbus_connection_flush)(DBusConnection *) = NULL;
    6.34 +static DBusMessage *(*DBUS_dbus_message_new_method_call)(const char *, const char *, const char *, const char *) = NULL;
    6.35 +static void (*DBUS_dbus_message_unref)(DBusMessage *) = NULL;
    6.36 +static void (*DBUS_dbus_error_init)(DBusError *) = NULL;
    6.37 +static dbus_bool_t (*DBUS_dbus_error_is_set)(const DBusError *) = NULL;
    6.38 +static void (*DBUS_dbus_error_free)(DBusError *) = NULL;
    6.39 +
    6.40 +static int
    6.41 +load_dbus_syms(void)
    6.42 +{
    6.43 +    /* cast funcs to char* first, to please GCC's strict aliasing rules. */
    6.44 +    #define SDL_DBUS_SYM(x) \
    6.45 +        if (!load_dbus_sym(#x, (void **) (char *) &DBUS_##x)) return -1
    6.46 +
    6.47 +    SDL_DBUS_SYM(dbus_bus_get_private);
    6.48 +    SDL_DBUS_SYM(dbus_connection_set_exit_on_disconnect);
    6.49 +    SDL_DBUS_SYM(dbus_connection_send);
    6.50 +    SDL_DBUS_SYM(dbus_connection_close);
    6.51 +    SDL_DBUS_SYM(dbus_connection_unref);
    6.52 +    SDL_DBUS_SYM(dbus_connection_flush);
    6.53 +    SDL_DBUS_SYM(dbus_message_new_method_call);
    6.54 +    SDL_DBUS_SYM(dbus_message_unref);
    6.55 +    SDL_DBUS_SYM(dbus_error_init);
    6.56 +    SDL_DBUS_SYM(dbus_error_is_set);
    6.57 +    SDL_DBUS_SYM(dbus_error_free);
    6.58 +
    6.59 +    #undef SDL_DBUS_SYM
    6.60 +
    6.61 +    return 0;
    6.62 +}
    6.63 +
    6.64 +static void
    6.65 +UnloadDBUSLibrary(void)
    6.66 +{
    6.67 +    if (dbus_handle != NULL) {
    6.68 +        SDL_UnloadObject(dbus_handle);
    6.69 +        dbus_handle = NULL;
    6.70 +    }
    6.71 +}
    6.72 +
    6.73 +static int
    6.74 +LoadDBUSLibrary(void)
    6.75 +{
    6.76 +    int retval = 0;
    6.77 +    if (dbus_handle == NULL) {
    6.78 +        dbus_handle = SDL_LoadObject(dbus_library);
    6.79 +        if (dbus_handle == NULL) {
    6.80 +            retval = -1;
    6.81 +            /* Don't call SDL_SetError(): SDL_LoadObject already did. */
    6.82 +        } else {
    6.83 +            retval = load_dbus_syms();
    6.84 +            if (retval < 0) {
    6.85 +                UnloadDBUSLibrary();
    6.86 +            }
    6.87 +        }
    6.88 +    }
    6.89 +
    6.90 +    return retval;
    6.91 +}
    6.92 +
    6.93 +static void
    6.94 +X11_InitDBus(_THIS)
    6.95 +{
    6.96 +    if (LoadDBUSLibrary() != -1) {
    6.97 +        SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    6.98 +        DBusError err;
    6.99 +        DBUS_dbus_error_init(&err);
   6.100 +        data->dbus = DBUS_dbus_bus_get_private(DBUS_BUS_SESSION, &err);
   6.101 +        if (DBUS_dbus_error_is_set(&err)) {
   6.102 +            DBUS_dbus_error_free(&err);
   6.103 +            if (data->dbus) {
   6.104 +                DBUS_dbus_connection_unref(data->dbus);
   6.105 +                data->dbus = NULL;
   6.106 +            }
   6.107 +            return;  /* oh well */
   6.108 +        }
   6.109 +        DBUS_dbus_connection_set_exit_on_disconnect(data->dbus, 0);
   6.110 +    }
   6.111 +}
   6.112 +
   6.113 +static void
   6.114 +X11_QuitDBus(_THIS)
   6.115 +{
   6.116 +    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   6.117 +    if (data->dbus) {
   6.118 +        DBUS_dbus_connection_close(data->dbus);
   6.119 +        DBUS_dbus_connection_unref(data->dbus);
   6.120 +        data->dbus = NULL;
   6.121 +    }
   6.122 +}
   6.123 +
   6.124 +void
   6.125 +SDL_dbus_screensaver_tickle(_THIS)
   6.126 +{
   6.127 +    const SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   6.128 +    DBusConnection *conn = data->dbus;
   6.129 +    if (conn != NULL) {
   6.130 +        DBusMessage *msg = DBUS_dbus_message_new_method_call("org.gnome.ScreenSaver",
   6.131 +                                                             "/org/gnome/ScreenSaver",
   6.132 +                                                             "org.gnome.ScreenSaver",
   6.133 +                                                             "SimulateUserActivity");
   6.134 +        if (msg != NULL) {
   6.135 +            if (DBUS_dbus_connection_send(conn, msg, NULL)) {
   6.136 +                DBUS_dbus_connection_flush(conn);
   6.137 +            }
   6.138 +            DBUS_dbus_message_unref(msg);
   6.139 +        }
   6.140 +    }
   6.141 +}
   6.142 +#endif
   6.143 +
   6.144  /* Initialization/Query functions */
   6.145  static int X11_VideoInit(_THIS);
   6.146  static void X11_VideoQuit(_THIS);
   6.147 @@ -349,6 +486,7 @@
   6.148  #endif
   6.149  }
   6.150  
   6.151 +
   6.152  int
   6.153  X11_VideoInit(_THIS)
   6.154  {
   6.155 @@ -401,6 +539,11 @@
   6.156      X11_InitMouse(_this);
   6.157  
   6.158      X11_InitTouch(_this);
   6.159 +
   6.160 +#if SDL_USE_LIBDBUS
   6.161 +    X11_InitDBus(_this);
   6.162 +#endif
   6.163 +
   6.164      return 0;
   6.165  }
   6.166  
   6.167 @@ -422,6 +565,10 @@
   6.168      X11_QuitKeyboard(_this);
   6.169      X11_QuitMouse(_this);
   6.170      X11_QuitTouch(_this);
   6.171 +
   6.172 +#if SDL_USE_LIBDBUS
   6.173 +    X11_QuitDBus(_this);
   6.174 +#endif
   6.175  }
   6.176  
   6.177  SDL_bool
     7.1 --- a/src/video/x11/SDL_x11video.h	Wed Dec 26 22:26:44 2012 -0800
     7.2 +++ b/src/video/x11/SDL_x11video.h	Fri Dec 28 03:46:55 2012 -0500
     7.3 @@ -53,6 +53,11 @@
     7.4  #include <X11/extensions/xf86vmode.h>
     7.5  #endif
     7.6  
     7.7 +#ifdef HAVE_DBUS_DBUS_H
     7.8 +#define SDL_USE_LIBDBUS 1
     7.9 +#include <dbus/dbus.h>
    7.10 +#endif
    7.11 +
    7.12  #include "SDL_x11dyn.h"
    7.13  
    7.14  #include "SDL_x11clipboard.h"
    7.15 @@ -98,6 +103,10 @@
    7.16  
    7.17      SDL_Scancode key_layout[256];
    7.18      SDL_bool selection_waiting;    
    7.19 +
    7.20 +#if SDL_USE_LIBDBUS
    7.21 +    DBusConnection *dbus;
    7.22 +#endif
    7.23  } SDL_VideoData;
    7.24  
    7.25  extern SDL_bool X11_UseDirectColorVisuals(void);