Skip to content

Commit

Permalink
* Added configure option --enable-screensaver, to allow enabling the …
Browse files Browse the repository at this point in the history
…screensaver by default.

* Use XResetScreenSaver() instead of disabling screensaver entirely.

Full discussion summary from Erik on the SDL mailing list:

Current behaviour
=================

SDL changes the user's display power management settings without
permission from the user and without telling the user.

The interface that it uses to do so is DPMSDisable/DPMSEnable, which
should only ever be used by configuration utilities like KControl, never
by normal application programs, let alone by the libraries that they
use. Using an interface that is not at all intended for what SDL tries
to achieve means that it will not work as it should. Firstly, the power
management is completely disabled during the whole lifetime of the SDL
program, not only when it should be. Secondly, it makes SDL
non-reentrant, meaning that things will break when multiple SDL programs
are clients of the same X server simultaneously. Thirdly, no cleanup
mechanism ensures that the setting is restored if the client does not do
that (for example if it crashes).

In addition to that, this interface is broken on xorg,
[http://bugs.freedesktop.org/show_bug.cgi?id=13962], so what SDL tries
to do does not work at all on that implementation of the X Window
System. (The reason that the DPMSEnable works in KControl is that it
calls DPMSSetTimeout immediately after,
[http://websvn.kde.org/tags/KDE/3.5.9/kdebase/kcontrol/energy/energy.cpp?annotate=774532#l343]).


The problems that the current behaviour causes
==============================================
1. Information leak. When the user is away, someone might see what the
user has on the display when the user counts on the screensaver
preventing this. This does not even require physical access to the
workstation, it is enough to see it from a distance.
2. Draining battery. An SDL program that runs on a laptop will quickly
drain the battery while the user is away. The system will soon shut down
and require recharging before being usable again, while it should in
fact have consumed very little energy if the user's settings would have
been obeyed.
3. Wasting energy. Even if battery issues are not considered, energy as
such is wasted.
4. Display wear. The display may be worn out.


The problems that the current behaviour tries to solve
======================================================

1. Preventing screensaver while playing movies.
   Many SDL applications are media players. They have reasons to prevent
screensavers from being activated while a movie is being played. When a
user clicks on the play button it can be interpreted as saying "play
this movie, but do not turn off the display while playing it, because I
will watch it even though I do not interact with the system".

2. Preventing screensaver when some input bypasses X.
   Sometimes SDL uses input from another source than the X server, so
that the X server is bypassed. This obviously breaks the screensaver
handling. SDL tries to work around that.

3. Preventing screensaver when all input bypasses X.
   There is something called Direct Graphics Access mode, where a
program takes control of both the display and the input devices from the
X server. This obviously means that the X server can not handle the
screensaver alone, since screensaver handling depends on input handling.
SDL does not do what it should to help the X server to handle the
screensaver. Nor does SDL take care of screeensaver handling itself. SDL
simply disables the screensaver completely.


How the problems should be solved
=================================

The correct way for an application program to prevent the screensaver
under X is to call XResetScreenSaver. This was recently discovered and
implemented by the mplayer developers,
[http://svn.mplayerhq.hu/mplayer?view=rev&revision=25637]. SDL needs to
wrap this in an API call (SDL_ResetScreenSaver) and implement it for the
other video targets (if they do not have a corresponding call, SDL
should do what it takes on that particular target, for example sending
fake key events).

1. When a movie is played, the player should reset the screensaver when
the animation is advanced to a new frame. The same applies to anything
similar, like slideshows.

2. When the X server is handling input, it must handle all input
(keyboards, mice, gamepads, ...). This is necessary, not only to be able
to handle the screensaver, but also so that it can send the events to
the correct (the currently active) client. If there is an input device
that the X server can not handle for some reason (such as lack of Plug
and Play capability), the program that handles the device as a
workaround must simulate what would happen if the X server would have
handled the device, by calling XResetScreenSaver when input is received
from the device.

3. When the X server is not handling the input, it depends on the
program that does to call XResetScreenSaver whenever an input event
occurs. Alternatively the program must handle the screensaver countdown
internally and call XActivateScreenSaver.
  • Loading branch information
slouken committed Feb 29, 2008
1 parent c719fda commit 83133e4
Show file tree
Hide file tree
Showing 19 changed files with 134 additions and 138 deletions.
20 changes: 7 additions & 13 deletions configure.in
Expand Up @@ -1090,19 +1090,6 @@ AC_HELP_STRING([--enable-video-x11-xrandr], [enable X11 Xrandr extension for ful
if test x$definitely_enable_video_x11_xrandr = xyes; then
AC_DEFINE(SDL_VIDEO_DRIVER_X11_XRANDR)
fi
AC_ARG_ENABLE(video-x11-dpms,
AC_HELP_STRING([--enable-video-x11-dpms], [enable X11 DPMS extension [[default=yes]]]),
, enable_video_x11_dpms=yes)
if test x$enable_video_x11_dpms = xyes; then
AC_CHECK_HEADER(X11/extensions/dpms.h,
have_dpms_h_hdr=yes,
have_dpms_h_hdr=no,
[#include <X11/Xlib.h>
])
if test x$have_dpms_h_hdr = xyes; then
AC_DEFINE(SDL_VIDEO_DRIVER_X11_DPMS)
fi
fi
fi
fi
}
Expand Down Expand Up @@ -1673,6 +1660,13 @@ AC_HELP_STRING([--enable-osmesa-shared], [dynamically load OSMesa OpenGL support
fi
}

AC_ARG_ENABLE(screensaver,
AC_HELP_STRING([--enable-screensaver], [enable screensaver by default while any SDL application is running [[default=no]]]),
, enable_screensaver=no)
if test x$enable_screensaver = xno; then
AC_DEFINE(SDL_VIDEO_DISABLE_SCREENSAVER)
fi

dnl See if we can use the new unified event interface in Linux 2.4
CheckInputEvents()
{
Expand Down
39 changes: 39 additions & 0 deletions docs.html
Expand Up @@ -14,6 +14,45 @@

<H2> <A HREF="docs/index.html">API Documentation</A> </H2>

<H2> SDL 1.2.14 Release Notes </H2>
<P>
SDL 1.2.14 is a minor bug fix release.
</P>

<H3> General Notes </H3>

<BLOCKQUOTE>
<P>
</P>
</BLOCKQUOTE>

<H3> Unix Notes </H3>

<BLOCKQUOTE>
<P>
Added configure option --enable-screensaver, to allow enabling the screensaver by default.
</P>
<P>
Use XResetScreenSaver() instead of disabling screensaver entirely.
</P>
</BLOCKQUOTE>

<H3> Windows Notes </H3>

<BLOCKQUOTE>
<P>
</P>
</BLOCKQUOTE>

<H3> Mac OS X Notes </H3>

<BLOCKQUOTE>
<P>
</P>
</BLOCKQUOTE>

<IMG SRC="docs/images/rainbow.gif" ALT="[separator]" WIDTH="100%">

<H2> SDL 1.2.13 Release Notes </H2>
<P>
SDL 1.2.13 is a minor bug fix release.
Expand Down
4 changes: 3 additions & 1 deletion include/SDL_config.h.in
Expand Up @@ -279,7 +279,6 @@
#undef SDL_VIDEO_DRIVER_WSCONS
#undef SDL_VIDEO_DRIVER_X11
#undef SDL_VIDEO_DRIVER_X11_DGAMOUSE
#undef SDL_VIDEO_DRIVER_X11_DPMS
#undef SDL_VIDEO_DRIVER_X11_DYNAMIC
#undef SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT
#undef SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR
Expand All @@ -298,6 +297,9 @@
#undef SDL_VIDEO_OPENGL_OSMESA
#undef SDL_VIDEO_OPENGL_OSMESA_DYNAMIC

/* Disable screensaver */
#undef SDL_VIDEO_DISABLE_SCREENSAVER

/* Enable assembly routines */
#undef SDL_ASSEMBLY_ROUTINES
#undef SDL_HERMES_BLITTERS
Expand Down
3 changes: 3 additions & 0 deletions include/SDL_config_macosx.h
Expand Up @@ -126,6 +126,9 @@
/* Enable OpenGL support */
#define SDL_VIDEO_OPENGL 1

/* Disable screensaver */
#define SDL_VIDEO_DISABLE_SCREENSAVER 1

/* Enable assembly routines */
#define SDL_ASSEMBLY_ROUTINES 1
#ifdef __ppc__
Expand Down
3 changes: 3 additions & 0 deletions include/SDL_config_win32.h
Expand Up @@ -172,6 +172,9 @@ typedef unsigned int uintptr_t;
#define SDL_VIDEO_OPENGL_WGL 1
#endif

/* Disable screensaver */
#define SDL_VIDEO_DISABLE_SCREENSAVER 1

/* Enable assembly routines (Win64 doesn't have inline asm) */
#ifndef _WIN64
#define SDL_ASSEMBLY_ROUTINES 1
Expand Down
12 changes: 12 additions & 0 deletions src/video/dga/SDL_dgaevents.c
Expand Up @@ -137,9 +137,21 @@ void DGA_PumpEvents(_THIS)
{
/* Keep processing pending events */
LOCK_DISPLAY();

/* Update activity every five seconds to prevent screensaver. --ryan. */
if (!allow_screensaver) {
static Uint32 screensaverTicks;
Uint32 nowTicks = SDL_GetTicks();
if ((nowTicks - screensaverTicks) > 5000) {
XResetScreenSaver(DGA_Display);
screensaverTicks = nowTicks;
}
}

while ( X11_Pending(DGA_Display) ) {
DGA_DispatchEvent(this);
}

UNLOCK_DISPLAY();
}

Expand Down
23 changes: 12 additions & 11 deletions src/video/dga/SDL_dgavideo.c
Expand Up @@ -43,11 +43,6 @@

/*#define DGA_DEBUG*/

/* Heheh we're using X11 event code */
extern void X11_SaveScreenSaver(Display *display, int *saved_timeout, BOOL *dpms);
extern void X11_DisableScreenSaver(Display *display);
extern void X11_RestoreScreenSaver(Display *display, int saved_timeout, BOOL dpms);

/* Initialization/Query functions */
static int DGA_VideoInit(_THIS, SDL_PixelFormat *vformat);
static SDL_Rect **DGA_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
Expand Down Expand Up @@ -333,6 +328,7 @@ static void UpdateHWInfo(_THIS, SDL_NAME(XDGAMode) *mode)

static int DGA_VideoInit(_THIS, SDL_PixelFormat *vformat)
{
const char *env;
const char *display;
int event_base, error_base;
int major_version, minor_version;
Expand Down Expand Up @@ -401,9 +397,17 @@ static int DGA_VideoInit(_THIS, SDL_PixelFormat *vformat)
return(-1);
}

/* Save DPMS and screensaver settings */
X11_SaveScreenSaver(DGA_Display, &screensaver_timeout, &dpms_enabled);
X11_DisableScreenSaver(DGA_Display);
/* Allow environment override of screensaver disable. */
env = SDL_getenv("SDL_VIDEO_ALLOW_SCREENSAVER");
if ( env ) {
allow_screensaver = SDL_atoi(env);
} else {
#ifdef SDL_VIDEO_DISABLE_SCREENSAVER
allow_screensaver = 0;
#else
allow_screensaver = 1;
#endif
}

/* Query for the list of available video modes */
modes = SDL_NAME(XDGAQueryModes)(DGA_Display, DGA_Screen, &num_modes);
Expand Down Expand Up @@ -1091,9 +1095,6 @@ void DGA_VideoQuit(_THIS)
/* Clean up the memory bucket list */
DGA_FreeHWSurfaces(this);

/* Restore DPMS and screensaver settings */
X11_RestoreScreenSaver(DGA_Display, screensaver_timeout, dpms_enabled);

/* Close up the display */
XCloseDisplay(DGA_Display);
}
Expand Down
11 changes: 3 additions & 8 deletions src/video/dga/SDL_dgavideo.h
Expand Up @@ -32,10 +32,6 @@
#include "SDL_mutex.h"
#include "../SDL_sysvideo.h"

#if SDL_VIDEO_DRIVER_X11_DPMS
#include <X11/extensions/dpms.h>
#endif

/* Hidden "this" pointer for the video functions */
#define _THIS SDL_VideoDevice *this

Expand Down Expand Up @@ -94,9 +90,9 @@ struct SDL_PrivateVideoData {
#endif

/* Screensaver settings */
int screensaver_timeout;
BOOL dpms_enabled;
int allow_screensaver;
};

/* Old variable names */
#define DGA_Display (this->hidden->DGA_Display)
#define DGA_Screen DefaultScreen(DGA_Display)
Expand All @@ -117,7 +113,6 @@ struct SDL_PrivateVideoData {
#define hw_lock (this->hidden->hw_lock)
#define DGA_event_base (this->hidden->event_base)
#define event_lock (this->hidden->event_lock)
#define screensaver_timeout (this->hidden->screensaver_timeout)
#define dpms_enabled (this->hidden->dpms_enabled)
#define allow_screensaver (this->hidden->allow_screensaver)

#endif /* _SDL_dgavideo_h */
5 changes: 2 additions & 3 deletions src/video/quartz/SDL_QuartzEvents.m
Expand Up @@ -717,8 +717,6 @@ static int QZ_OtherMouseButtonToSDL(int button)

void QZ_PumpEvents (_THIS)
{
static Uint32 screensaverTicks = 0;
Uint32 nowTicks;
CGMouseDelta dx, dy;

NSDate *distantPast;
Expand All @@ -731,7 +729,8 @@ void QZ_PumpEvents (_THIS)

/* Update activity every five seconds to prevent screensaver. --ryan. */
if (!allow_screensaver) {
nowTicks = SDL_GetTicks();
static Uint32 screensaverTicks;
Uint32 nowTicks = SDL_GetTicks();
if ((nowTicks - screensaverTicks) > 5000)
{
UpdateSystemActivity(UsrActivity);
Expand Down
11 changes: 10 additions & 1 deletion src/video/quartz/SDL_QuartzVideo.m
Expand Up @@ -188,8 +188,17 @@ static int QZ_VideoInit (_THIS, SDL_PixelFormat *video_format) {
mode_list = CGDisplayAvailableModes (display_id);
palette = CGPaletteCreateDefaultColorPalette ();

/* Allow environment override of screensaver disable. */
env = SDL_getenv("SDL_VIDEO_ALLOW_SCREENSAVER");
allow_screensaver = ( env && SDL_atoi(env) ) ? YES : NO;
if ( env ) {
allow_screensaver = SDL_atoi(env);
} else {
#ifdef SDL_VIDEO_DISABLE_SCREENSAVER
allow_screensaver = 0;
#else
allow_screensaver = 1;
#endif
}

/* Gather some information that is useful to know about the display */
CFNumberGetValue (CFDictionaryGetValue (save_mode, kCGDisplayBitsPerPixel),
Expand Down
2 changes: 1 addition & 1 deletion src/video/windib/SDL_dibevents.c
Expand Up @@ -227,7 +227,7 @@ LRESULT DIB_HandleMessage(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPar
case WM_SYSCOMMAND: {
const DWORD val = (DWORD) (wParam & 0xFFF0);
if ((val == SC_SCREENSAVE) || (val == SC_MONITORPOWER)) {
if (!this->hidden->allow_screensaver) {
if (!allow_screensaver) {
/* Note that this doesn't stop anything on Vista
if the screensaver has a password. */
return(0);
Expand Down
10 changes: 9 additions & 1 deletion src/video/windib/SDL_dibvideo.c
Expand Up @@ -396,7 +396,15 @@ int DIB_VideoInit(_THIS, SDL_PixelFormat *vformat)

/* Allow environment override of screensaver disable. */
env = SDL_getenv("SDL_VIDEO_ALLOW_SCREENSAVER");
this->hidden->allow_screensaver = ( (env && SDL_atoi(env)) ? 1 : 0 );
if ( env ) {
allow_screensaver = SDL_atoi(env);
} else {
#ifdef SDL_VIDEO_DISABLE_SCREENSAVER
allow_screensaver = 0;
#else
allow_screensaver = 1;
#endif
}

/* We're done! */
return(0);
Expand Down
6 changes: 4 additions & 2 deletions src/video/windib/SDL_dibvideo.h
Expand Up @@ -43,8 +43,6 @@ struct SDL_PrivateVideoData {
LOGPALETTE *screen_logpal;
BOOL grab_palette;

int allow_screensaver;

#define NUM_MODELISTS 4 /* 8, 16, 24, and 32 bits-per-pixel */
int SDL_nummodes[NUM_MODELISTS];
SDL_Rect **SDL_modelist[NUM_MODELISTS];
Expand All @@ -56,6 +54,9 @@ struct SDL_PrivateVideoData {
int supportRotation; /* for Pocket PC devices */
DWORD origRotation; /* for Pocket PC devices */
#endif

/* Screensaver settings */
int allow_screensaver;
};
/* Old variable names */
#define screen_bmp (this->hidden->screen_bmp)
Expand All @@ -64,5 +65,6 @@ struct SDL_PrivateVideoData {
#define grab_palette (this->hidden->grab_palette)
#define SDL_nummodes (this->hidden->SDL_nummodes)
#define SDL_modelist (this->hidden->SDL_modelist)
#define allow_screensaver (this->hidden->allow_screensaver)

#endif /* _SDL_dibvideo_h */
74 changes: 10 additions & 64 deletions src/video/x11/SDL_x11events.c
Expand Up @@ -920,6 +920,16 @@ void X11_PumpEvents(_THIS)
{
int pending;

/* Update activity every five seconds to prevent screensaver. --ryan. */
if (!allow_screensaver) {
static Uint32 screensaverTicks;
Uint32 nowTicks = SDL_GetTicks();
if ((nowTicks - screensaverTicks) > 5000) {
XResetScreenSaver(SDL_Display);
screensaverTicks = nowTicks;
}
}

/* Keep processing pending events */
pending = 0;
while ( X11_Pending(SDL_Display) ) {
Expand Down Expand Up @@ -1389,67 +1399,3 @@ void X11_InitOSKeymap(_THIS)
X11_InitKeymap();
}

void X11_SaveScreenSaver(Display *display, int *saved_timeout, BOOL *dpms)
{
int timeout, interval, prefer_blank, allow_exp;
XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
*saved_timeout = timeout;

#if SDL_VIDEO_DRIVER_X11_DPMS
if ( SDL_X11_HAVE_DPMS ) {
int dummy;
if ( DPMSQueryExtension(display, &dummy, &dummy) ) {
CARD16 state;
DPMSInfo(display, &state, dpms);
}
}
#else
*dpms = 0;
#endif /* SDL_VIDEO_DRIVER_X11_DPMS */
}

void X11_DisableScreenSaver(_THIS, Display *display)
{
int timeout, interval, prefer_blank, allow_exp;

if (this->hidden->allow_screensaver) {
return;
}

XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
timeout = 0;
XSetScreenSaver(display, timeout, interval, prefer_blank, allow_exp);

#if SDL_VIDEO_DRIVER_X11_DPMS
if ( SDL_X11_HAVE_DPMS ) {
int dummy;
if ( DPMSQueryExtension(display, &dummy, &dummy) ) {
DPMSDisable(display);
}
}
#endif /* SDL_VIDEO_DRIVER_X11_DPMS */
}

void X11_RestoreScreenSaver(_THIS, Display *display, int saved_timeout, BOOL dpms)
{
int timeout, interval, prefer_blank, allow_exp;

if (this->hidden->allow_screensaver) {
return;
}

XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
timeout = saved_timeout;
XSetScreenSaver(display, timeout, interval, prefer_blank, allow_exp);

#if SDL_VIDEO_DRIVER_X11_DPMS
if ( SDL_X11_HAVE_DPMS ) {
int dummy;
if ( DPMSQueryExtension(display, &dummy, &dummy) ) {
if ( dpms ) {
DPMSEnable(display);
}
}
}
#endif /* SDL_VIDEO_DRIVER_X11_DPMS */
}

0 comments on commit 83133e4

Please sign in to comment.