Updated Screensaver Logic

Posted on the forum a while back the following message. I’ve attached the patch for this - it might be useful.
Hi,

I am running Ubuntu 11.04 and recently installed Dosbox 0.74, which uses SDL. Current version of SDL shipped with Ubuntu is 1.2.14-6.1ubuntu3.

As it turns out there are a few “interesting” points I came across while using Dosbox with regard to the screensaver.

Initially I found that under no circumstance my OS screensaver would come on - whether dosbox had focus or not. After some puzzling I figured out that if I set SDL_VIDEO_ALLOW_SCREENSAVER=1 in my environment, the screensaver would come on - even if I was in the middle of playing a game.

Not quite the solution I was hoping for (albeit better than no screensaver at all - I’m lousy at remembering to lock the screen manually, and after all why do thing manually when you can get the machine to do it for you??).

So I spent some time looking through the code (SDL 1.2.14) and came up with a fairly simple solution to my problem. I’ve been running with my “home grown” version of SDL for awhile and so far it seems to do what I want it to, which is:

  1. Turn on screensaver when I’m away from keyboard (or mouse) for some period of time;
  2. Do not turn on screensaver when I’m doing something in an application that uses SDL.

I added the following bit of logic to SDL_x11events.c, lines 404 and on:

if ( xevent.type == KeyPress || xevent.type == ButtonPress ) {
XResetScreenSaver(SDL_Display);
#ifdef DEBUG_XEVENTS
printf(“Reset Screensaver.\n”);
#endif
}

The if statement could be augmented with an additional check for allow_screensaver, like:

if ( (xevent.type == KeyPress || xevent.type == ButtonPress) && allow_screensaver ) {

This would cause the additional behavior to only occur if the screensaver was explicitly enabled through the environment variable. This might be preferable if the application (for whatever reason) does not work properly when the screensaver does come on…

With the added bit of code, the screensaver will be reset every time I either hit a key on the keyboard or click a mouse button… Both are fairly unambiguous signs that the user is actually doing something with the application and therefore the screensaver can be reset.

The existing logic either disables the screensaver altogether by resetting it every 5 seconds (regardless of user activity), or never (regardless of user activity). The logic I added actually does take into account whether the user is interacting with the application or not.

I figured I best post this little snippet here - just in case somebody else has some similar issue. Obviously there are some drawbacks:

  1. I’ve only tested this with a single application that uses SDL (i.e. dosbox);
  2. I’ve only applied the noted logic for X11 - none of the other platforms.

There might be some good rationale for not doing something like what I’ve done - and I’m happy to listen - but for me the above works like a charm.
-------------- next part --------------
A non-text attachment was scrubbed…
Name: SDL-1.2.14_1_44b5aba60083.patch
Type: text/x-patch
Size: 840 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20110610/18e863ee/attachment.bin