Skip to content

Commit

Permalink
Let app set SDL_VIDEO_ALLOW_SCREENSAVER environment variable to overr…
Browse files Browse the repository at this point in the history
…ide SDL's

 attempt to disable screen savers. Works for Quartz (Mac OS X) and X11.

Need a formal API for this in 1.3, still.

  Fixes Bugzilla #415.
  • Loading branch information
icculus committed Apr 6, 2007
1 parent b6f9fad commit 5d95117
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 11 deletions.
12 changes: 7 additions & 5 deletions src/video/quartz/SDL_QuartzEvents.m
Original file line number Diff line number Diff line change
Expand Up @@ -734,11 +734,13 @@ void QZ_PumpEvents (_THIS)
return; /* don't do anything if there's no screen surface. */

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

pool = [ [ NSAutoreleasePool alloc ] init ];
Expand Down
2 changes: 2 additions & 0 deletions src/video/quartz/SDL_QuartzVideo.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
/* Main driver structure to store required state information */
typedef struct SDL_PrivateVideoData {

BOOL allow_screensaver; /* 0 == disable screensaver */
CGDirectDisplayID display; /* 0 == main display (only support single display) */
CFDictionaryRef mode; /* current mode of the display */
CFDictionaryRef save_mode; /* original mode of the display */
Expand Down Expand Up @@ -127,6 +128,7 @@ typedef struct SDL_PrivateVideoData {
#define display_id (this->hidden->display)
#define mode (this->hidden->mode)
#define save_mode (this->hidden->save_mode)
#define allow_screensaver (this->hidden->allow_screensaver)
#define mode_list (this->hidden->mode_list)
#define palette (this->hidden->palette)
#define gl_context (this->hidden->gl_context)
Expand Down
5 changes: 5 additions & 0 deletions src/video/quartz/SDL_QuartzVideo.m
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,17 @@ static void QZ_DeleteDevice (SDL_VideoDevice *device) {

static int QZ_VideoInit (_THIS, SDL_PixelFormat *video_format) {

const char *env = NULL;

/* Initialize the video settings; this data persists between mode switches */
display_id = kCGDirectMainDisplay;
save_mode = CGDisplayCurrentMode (display_id);
mode_list = CGDisplayAvailableModes (display_id);
palette = CGPaletteCreateDefaultColorPalette ();

env = SDL_getenv("SDL_VIDEO_ALLOW_SCREENSAVER");
allow_screensaver = ( env && SDL_atoi(env) ) ? YES : NO;

/* Gather some information that is useful to know about the display */
CFNumberGetValue (CFDictionaryGetValue (save_mode, kCGDisplayBitsPerPixel),
kCFNumberSInt32Type, &device_bpp);
Expand Down
14 changes: 12 additions & 2 deletions src/video/x11/SDL_x11events.c
Original file line number Diff line number Diff line change
Expand Up @@ -1136,9 +1136,14 @@ void X11_SaveScreenSaver(Display *display, int *saved_timeout, BOOL *dpms)
#endif /* SDL_VIDEO_DRIVER_X11_DPMS */
}

void X11_DisableScreenSaver(Display *display)
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);
Expand All @@ -1153,9 +1158,14 @@ void X11_DisableScreenSaver(Display *display)
#endif /* SDL_VIDEO_DRIVER_X11_DPMS */
}

void X11_RestoreScreenSaver(Display *display, int saved_timeout, BOOL 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);
Expand Down
4 changes: 2 additions & 2 deletions src/video/x11/SDL_x11events_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ extern void X11_PumpEvents(_THIS);
extern void X11_SetKeyboardState(Display *display, const char *key_vec);

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);
extern void X11_DisableScreenSaver(_THIS, Display *display);
extern void X11_RestoreScreenSaver(_THIS, Display *display, int saved_timeout, BOOL dpms);
9 changes: 7 additions & 2 deletions src/video/x11/SDL_x11video.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ static void create_aux_windows(_THIS)

static int X11_VideoInit(_THIS, SDL_PixelFormat *vformat)
{
const char *env = NULL;
char *display;
int i;

Expand Down Expand Up @@ -546,7 +547,7 @@ static int X11_VideoInit(_THIS, SDL_PixelFormat *vformat)

/* Save DPMS and screensaver settings */
X11_SaveScreenSaver(SDL_Display, &screensaver_timeout, &dpms_enabled);
X11_DisableScreenSaver(SDL_Display);
X11_DisableScreenSaver(this, SDL_Display);

/* See if we have been passed a window to use */
SDL_windowid = SDL_getenv("SDL_WINDOWID");
Expand All @@ -562,6 +563,10 @@ static int X11_VideoInit(_THIS, SDL_PixelFormat *vformat)
/* Fill in some window manager capabilities */
this->info.wm_available = 1;

/* Allow environment override of screensaver disable. */
env = SDL_getenv("SDL_VIDEO_ALLOW_SCREENSAVER");
this->hidden->allow_screensaver = ( (env && SDL_atoi(env)) ? 1 : 0 );

/* We're done! */
XFlush(SDL_Display);
return(0);
Expand Down Expand Up @@ -1375,7 +1380,7 @@ void X11_VideoQuit(_THIS)
}

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

/* Free that blank cursor */
if ( SDL_BlankCursor != NULL ) {
Expand Down
2 changes: 2 additions & 0 deletions src/video/x11/SDL_x11video.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ struct SDL_PrivateVideoData {
int use_xme;
int currently_fullscreen;

int allow_screensaver;

/* Automatic mode switching support (entering/leaving fullscreen) */
Uint32 switch_waiting;
Uint32 switch_time;
Expand Down

0 comments on commit 5d95117

Please sign in to comment.