Add support for the freedesktop.org Idle Inhibition specification,
authorBastien Nocera
Sun, 02 Mar 2014 14:59:06 -0500
changeset 8287fee216d949e3
parent 8286 81319d3c2305
child 8288 4e3afa79b8d8
Add support for the freedesktop.org Idle Inhibition specification,
as per:
http://people.freedesktop.org/~hadess/idle-inhibition-spec/

This makes screensaver inhibition work with GNOME 3 and other
desktop environments that implement the specification.

https://bugzilla.libsdl.org/show_bug.cgi?id=2169
src/video/x11/SDL_x11events.c
src/video/x11/SDL_x11video.c
src/video/x11/SDL_x11video.h
     1.1 --- a/src/video/x11/SDL_x11events.c	Sun Mar 02 11:10:00 2014 -0800
     1.2 +++ b/src/video/x11/SDL_x11events.c	Sun Mar 02 14:59:06 2014 -0500
     1.3 @@ -1029,7 +1029,19 @@
     1.4      SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
     1.5      int dummy;
     1.6      int major_version, minor_version;
     1.7 +#endif /* SDL_VIDEO_DRIVER_X11_XSCRNSAVER */
     1.8  
     1.9 +#if SDL_USE_LIBDBUS
    1.10 +    if (SDL_dbus_screensaver_inhibit(_this)) {
    1.11 +        return;
    1.12 +    }
    1.13 +
    1.14 +    if (_this->suspend_screensaver) {
    1.15 +        SDL_dbus_screensaver_tickle(_this);
    1.16 +    }
    1.17 +#endif
    1.18 +
    1.19 +#if SDL_VIDEO_DRIVER_X11_XSCRNSAVER
    1.20      if (SDL_X11_HAVE_XSS) {
    1.21          /* X11_XScreenSaverSuspend was introduced in MIT-SCREEN-SAVER 1.1 */
    1.22          if (!X11_XScreenSaverQueryExtension(data->display, &dummy, &dummy) ||
    1.23 @@ -1043,12 +1055,6 @@
    1.24          X11_XResetScreenSaver(data->display);
    1.25      }
    1.26  #endif
    1.27 -
    1.28 -#if SDL_USE_LIBDBUS
    1.29 -    if (_this->suspend_screensaver) {
    1.30 -        SDL_dbus_screensaver_tickle(_this);
    1.31 -    }
    1.32 -#endif
    1.33  }
    1.34  
    1.35  #endif /* SDL_VIDEO_DRIVER_X11 */
     2.1 --- a/src/video/x11/SDL_x11video.c	Sun Mar 02 11:10:00 2014 -0800
     2.2 +++ b/src/video/x11/SDL_x11video.c	Sun Mar 02 14:59:06 2014 -0500
     2.3 @@ -45,6 +45,7 @@
     2.4  #include "SDL_loadso.h"
     2.5  static const char *dbus_library = "libdbus-1.so.3";
     2.6  static void *dbus_handle = NULL;
     2.7 +static unsigned int screensaver_cookie = 0;
     2.8  
     2.9  /* !!! FIXME: this is kinda ugly. */
    2.10  static SDL_bool
    2.11 @@ -63,10 +64,13 @@
    2.12  static DBusConnection *(*DBUS_dbus_bus_get_private)(DBusBusType, DBusError *) = NULL;
    2.13  static void (*DBUS_dbus_connection_set_exit_on_disconnect)(DBusConnection *, dbus_bool_t) = NULL;
    2.14  static dbus_bool_t (*DBUS_dbus_connection_send)(DBusConnection *, DBusMessage *, dbus_uint32_t *) = NULL;
    2.15 +static DBusMessage *(*DBUS_dbus_connection_send_with_reply_and_block)(DBusConnection *, DBusMessage *, int, DBusError *) = NULL;
    2.16  static void (*DBUS_dbus_connection_close)(DBusConnection *) = NULL;
    2.17  static void (*DBUS_dbus_connection_unref)(DBusConnection *) = NULL;
    2.18  static void (*DBUS_dbus_connection_flush)(DBusConnection *) = NULL;
    2.19  static DBusMessage *(*DBUS_dbus_message_new_method_call)(const char *, const char *, const char *, const char *) = NULL;
    2.20 +static dbus_bool_t (*DBUS_dbus_message_append_args)(DBusMessage *, int, ...) = NULL;
    2.21 +static dbus_bool_t (*DBUS_dbus_message_get_args)(DBusMessage *, DBusError *, int, ...) = NULL;
    2.22  static void (*DBUS_dbus_message_unref)(DBusMessage *) = NULL;
    2.23  static void (*DBUS_dbus_error_init)(DBusError *) = NULL;
    2.24  static dbus_bool_t (*DBUS_dbus_error_is_set)(const DBusError *) = NULL;
    2.25 @@ -82,9 +86,12 @@
    2.26      SDL_DBUS_SYM(dbus_bus_get_private);
    2.27      SDL_DBUS_SYM(dbus_connection_set_exit_on_disconnect);
    2.28      SDL_DBUS_SYM(dbus_connection_send);
    2.29 +    SDL_DBUS_SYM(dbus_connection_send_with_reply_and_block);
    2.30      SDL_DBUS_SYM(dbus_connection_close);
    2.31      SDL_DBUS_SYM(dbus_connection_unref);
    2.32      SDL_DBUS_SYM(dbus_connection_flush);
    2.33 +    SDL_DBUS_SYM(dbus_message_append_args);
    2.34 +    SDL_DBUS_SYM(dbus_message_get_args);
    2.35      SDL_DBUS_SYM(dbus_message_new_method_call);
    2.36      SDL_DBUS_SYM(dbus_message_unref);
    2.37      SDL_DBUS_SYM(dbus_error_init);
    2.38 @@ -174,6 +181,76 @@
    2.39          }
    2.40      }
    2.41  }
    2.42 +
    2.43 +SDL_bool
    2.44 +SDL_dbus_screensaver_inhibit(_THIS)
    2.45 +{
    2.46 +    const SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    2.47 +    DBusConnection *conn = data->dbus;
    2.48 +
    2.49 +    if (conn == NULL)
    2.50 +        return SDL_FALSE;
    2.51 +
    2.52 +    if (_this->suspend_screensaver &&
    2.53 +        screensaver_cookie != 0)
    2.54 +        return SDL_TRUE;
    2.55 +    if (!_this->suspend_screensaver &&
    2.56 +        screensaver_cookie == 0)
    2.57 +        return SDL_TRUE;
    2.58 +
    2.59 +    if (_this->suspend_screensaver) {
    2.60 +        const char *app = "My SDL application";
    2.61 +        const char *reason = "Playing a game";
    2.62 +
    2.63 +        DBusMessage *msg = DBUS_dbus_message_new_method_call("org.freedesktop.ScreenSaver",
    2.64 +                                                             "/org/freedesktop/ScreenSaver",
    2.65 +                                                             "org.freedesktop.ScreenSaver",
    2.66 +                                                             "Inhibit");
    2.67 +        if (msg != NULL) {
    2.68 +            DBUS_dbus_message_append_args (msg,
    2.69 +                                           DBUS_TYPE_STRING, &app,
    2.70 +                                           DBUS_TYPE_STRING, &reason,
    2.71 +                                           DBUS_TYPE_INVALID);
    2.72 +        }
    2.73 +
    2.74 +        if (msg != NULL) {
    2.75 +            DBusMessage *reply;
    2.76 +
    2.77 +            reply = DBUS_dbus_connection_send_with_reply_and_block(conn, msg, 300, NULL);
    2.78 +            if (reply) {
    2.79 +                if (!DBUS_dbus_message_get_args(reply, NULL,
    2.80 +                                                DBUS_TYPE_UINT32, &screensaver_cookie,
    2.81 +                                                DBUS_TYPE_INVALID))
    2.82 +                    screensaver_cookie = 0;
    2.83 +                DBUS_dbus_message_unref(reply);
    2.84 +            }
    2.85 +
    2.86 +            DBUS_dbus_message_unref(msg);
    2.87 +        }
    2.88 +
    2.89 +        if (screensaver_cookie == 0) {
    2.90 +            return SDL_FALSE;
    2.91 +        }
    2.92 +        return SDL_TRUE;
    2.93 +    } else {
    2.94 +        DBusMessage *msg = DBUS_dbus_message_new_method_call("org.freedesktop.ScreenSaver",
    2.95 +                                                             "/org/freedesktop/ScreenSaver",
    2.96 +                                                             "org.freedesktop.ScreenSaver",
    2.97 +                                                             "UnInhibit");
    2.98 +        DBUS_dbus_message_append_args (msg,
    2.99 +                                       DBUS_TYPE_UINT32, &screensaver_cookie,
   2.100 +                                       DBUS_TYPE_INVALID);
   2.101 +        if (msg != NULL) {
   2.102 +            if (DBUS_dbus_connection_send(conn, msg, NULL)) {
   2.103 +                DBUS_dbus_connection_flush(conn);
   2.104 +            }
   2.105 +            DBUS_dbus_message_unref(msg);
   2.106 +        }
   2.107 +
   2.108 +        screensaver_cookie = 0;
   2.109 +        return SDL_TRUE;
   2.110 +    }
   2.111 +}
   2.112  #endif
   2.113  
   2.114  /* Initialization/Query functions */
     3.1 --- a/src/video/x11/SDL_x11video.h	Sun Mar 02 11:10:00 2014 -0800
     3.2 +++ b/src/video/x11/SDL_x11video.h	Sun Mar 02 14:59:06 2014 -0500
     3.3 @@ -121,6 +121,7 @@
     3.4  
     3.5  extern SDL_bool X11_UseDirectColorVisuals(void);
     3.6  
     3.7 +SDL_bool SDL_dbus_screensaver_inhibit(_THIS);
     3.8  void SDL_dbus_screensaver_tickle(_THIS);
     3.9  
    3.10  #endif /* _SDL_x11video_h */