From 820c153779419140be3c74638472707a8f0c5297 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 8 May 2006 06:38:13 +0000 Subject: [PATCH] Fixed bug #203 Disable screensaver and DPMS blanking while SDL app is running. --- configure.in | 14 ++++++++- include/SDL_config.h.in | 1 + src/video/x11/SDL_x11events.c | 52 +++++++++++++++++++++++++++++++++ src/video/x11/SDL_x11events_c.h | 3 ++ src/video/x11/SDL_x11sym.h | 12 +++++++- src/video/x11/SDL_x11video.c | 8 +++++ src/video/x11/SDL_x11video.h | 11 +++++++ 7 files changed, 99 insertions(+), 2 deletions(-) diff --git a/configure.in b/configure.in index 89ed18a9f..924e07d1f 100644 --- a/configure.in +++ b/configure.in @@ -927,10 +927,22 @@ AC_HELP_STRING([--enable-video-x11-xrandr], [enable X11 Xrandr extension for ful fi fi fi - 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 + ]) + if test x$have_dpms_h_hdr = xyes; then + AC_DEFINE(SDL_VIDEO_DRIVER_X11_DPMS) + fi + fi fi fi } diff --git a/include/SDL_config.h.in b/include/SDL_config.h.in index e556b2094..6f89569c9 100644 --- a/include/SDL_config.h.in +++ b/include/SDL_config.h.in @@ -278,6 +278,7 @@ #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 diff --git a/src/video/x11/SDL_x11events.c b/src/video/x11/SDL_x11events.c index dd914c335..0ad6eabad 100644 --- a/src/video/x11/SDL_x11events.c +++ b/src/video/x11/SDL_x11events.c @@ -1118,3 +1118,55 @@ void X11_InitOSKeymap(_THIS) X11_InitKeymap(); } +void X11_SaveScreenSaver(_THIS) +{ + int timeout, interval, prefer_blank, allow_exp; + XGetScreenSaver(SDL_Display, &timeout, &interval, &prefer_blank, &allow_exp); + screensaver_timeout = timeout; + +#if SDL_VIDEO_DRIVER_X11_DPMS + if ( SDL_X11_HAVE_DPMS ) { + int dummy; + if ( DPMSQueryExtension(SDL_Display, &dummy, &dummy) ) { + CARD16 state; + DPMSInfo(SDL_Display, &state, &dpms_enabled); + } + } +#endif /* SDL_VIDEO_DRIVER_X11_DPMS */ +} + +void X11_DisableScreenSaver(_THIS) +{ + int timeout, interval, prefer_blank, allow_exp; + XGetScreenSaver(SDL_Display, &timeout, &interval, &prefer_blank, &allow_exp); + timeout = 0; + XSetScreenSaver(SDL_Display, timeout, interval, prefer_blank, allow_exp); + +#if SDL_VIDEO_DRIVER_X11_DPMS + if ( SDL_X11_HAVE_DPMS ) { + int dummy; + if ( DPMSQueryExtension(SDL_Display, &dummy, &dummy) ) { + DPMSDisable(SDL_Display); + } + } +#endif /* SDL_VIDEO_DRIVER_X11_DPMS */ +} + +void X11_RestoreScreenSaver(_THIS) +{ + int timeout, interval, prefer_blank, allow_exp; + XGetScreenSaver(SDL_Display, &timeout, &interval, &prefer_blank, &allow_exp); + timeout = screensaver_timeout; + XSetScreenSaver(SDL_Display, timeout, interval, prefer_blank, allow_exp); + +#if SDL_VIDEO_DRIVER_X11_DPMS + if ( SDL_X11_HAVE_DPMS ) { + int dummy; + if ( DPMSQueryExtension(SDL_Display, &dummy, &dummy) ) { + if ( dpms_enabled ) { + DPMSEnable(SDL_Display); + } + } + } +#endif /* SDL_VIDEO_DRIVER_X11_DPMS */ +} diff --git a/src/video/x11/SDL_x11events_c.h b/src/video/x11/SDL_x11events_c.h index 3041287f4..b8326e453 100644 --- a/src/video/x11/SDL_x11events_c.h +++ b/src/video/x11/SDL_x11events_c.h @@ -28,3 +28,6 @@ extern void X11_InitOSKeymap(_THIS); extern void X11_PumpEvents(_THIS); extern void X11_SetKeyboardState(Display *display, const char *key_vec); +extern void X11_SaveScreenSaver(_THIS); +extern void X11_DisableScreenSaver(_THIS); +extern void X11_RestoreScreenSaver(_THIS); diff --git a/src/video/x11/SDL_x11sym.h b/src/video/x11/SDL_x11sym.h index 4088e6d4c..fe71cc0de 100644 --- a/src/video/x11/SDL_x11sym.h +++ b/src/video/x11/SDL_x11sym.h @@ -56,9 +56,9 @@ SDL_X11_SYM(int,XFreePixmap,(Display* a,Pixmap b),(a,b),return) SDL_X11_SYM(int,XGetErrorDatabaseText,(Display* a,_Xconst char* b,_Xconst char* c,_Xconst char* d,char* e,int f),(a,b,c,d,e,f),return) SDL_X11_SYM(XModifierKeymap*,XGetModifierMapping,(Display* a),(a),return) SDL_X11_SYM(int,XGetPointerControl,(Display* a,int* b,int* c,int* d),(a,b,c,d),return) +SDL_X11_SYM(int,XGetScreenSaver,(Display* a,int* b,int* c,int* d, int* e),(a,b,c,d,e),return) SDL_X11_SYM(XVisualInfo*,XGetVisualInfo,(Display* a,long b,XVisualInfo* c,int* d),(a,b,c,d),return) SDL_X11_SYM(XWMHints*,XGetWMHints,(Display* a,Window b),(a,b),return) -SDL_X11_SYM(Status,XGetTextProperty,(Display* a,Window b,XTextProperty* c,Atom d),(a,b,c,d),return) SDL_X11_SYM(Status,XGetWindowAttributes,(Display* a,Window b,XWindowAttributes* c),(a,b,c),return) SDL_X11_SYM(int,XGrabKeyboard,(Display* a,Window b,Bool c,int d,int e,Time f),(a,b,c,d,e,f),return) SDL_X11_SYM(int,XGrabPointer,(Display* a,Window b,Bool c,unsigned int d,int e,int f,Window g,Cursor h,Time i),(a,b,c,d,e,f,g,h,i),return) @@ -91,6 +91,7 @@ SDL_X11_SYM(Status,XSendEvent,(Display* a,Window b,Bool c,long d,XEvent* e),(a,b SDL_X11_SYM(int,XSetClassHint,(Display* a,Window b,XClassHint* c),(a,b,c),return) SDL_X11_SYM(XErrorHandler,XSetErrorHandler,(XErrorHandler a),(a),return) SDL_X11_SYM(XIOErrorHandler,XSetIOErrorHandler,(XIOErrorHandler a),(a),return) +SDL_X11_SYM(int,XSetScreenSaver,(Display* a,int b,int c,int d,int e),(a,b,c,d,e),return) SDL_X11_SYM(int,XSetTransientForHint,(Display* a,Window b,Window c),(a,b,c),return) SDL_X11_SYM(int,XSetWMHints,(Display* a,Window b,XWMHints* c),(a,b,c),return) SDL_X11_SYM(void,XSetTextProperty,(Display* a,Window b,XTextProperty* c,Atom d),(a,b,c,d),) @@ -187,5 +188,14 @@ SDL_X11_SYM(Status,XRRSetScreenConfig,(Display *dpy, XRRScreenConfiguration *con SDL_X11_SYM(void,XRRFreeScreenConfigInfo,(XRRScreenConfiguration *config),(config),) #endif +/* DPMS support */ +#if SDL_VIDEO_DRIVER_X11_DPMS +SDL_X11_MODULE(DPMS) +SDL_X11_SYM(Status,DPMSQueryExtension,(Display *dpy,int *major_versionp,int *minor_versionp),(dpy,major_versionp,minor_versionp),return) +SDL_X11_SYM(Status,DPMSInfo,(Display *dpy,CARD16 *state,BOOL *onoff),(dpy,state,onoff),return) +SDL_X11_SYM(Status,DPMSEnable,(Display *dpy),(dpy),return) +SDL_X11_SYM(Status,DPMSDisable,(Display *dpy),(dpy),return) +#endif + /* end of SDL_x11sym.h ... */ diff --git a/src/video/x11/SDL_x11video.c b/src/video/x11/SDL_x11video.c index 0e4aab666..c1f1d5736 100644 --- a/src/video/x11/SDL_x11video.c +++ b/src/video/x11/SDL_x11video.c @@ -544,6 +544,10 @@ static int X11_VideoInit(_THIS, SDL_PixelFormat *vformat) } X11_SaveVidModeGamma(this); + /* Save DPMS and screensaver settings */ + X11_SaveScreenSaver(this); + X11_DisableScreenSaver(this); + /* See if we have been passed a window to use */ SDL_windowid = SDL_getenv("SDL_WINDOWID"); @@ -1364,11 +1368,15 @@ void X11_VideoQuit(_THIS) SDL_free(SDL_iconcolors); SDL_iconcolors = NULL; } + /* Restore gamma settings if they've changed */ if ( SDL_GetAppState() & SDL_APPACTIVE ) { X11_SwapVidModeGamma(this); } + /* Restore DPMS and screensaver settings */ + X11_RestoreScreenSaver(this); + /* Free that blank cursor */ if ( SDL_BlankCursor != NULL ) { this->FreeWMCursor(this, SDL_BlankCursor); diff --git a/src/video/x11/SDL_x11video.h b/src/video/x11/SDL_x11video.h index 1f1bd0c1d..240e5b04a 100644 --- a/src/video/x11/SDL_x11video.h +++ b/src/video/x11/SDL_x11video.h @@ -46,6 +46,9 @@ #if SDL_VIDEO_DRIVER_X11_XME #include "../Xext/extensions/xme.h" #endif +#if SDL_VIDEO_DRIVER_X11_DPMS +#include +#endif #include "SDL_x11dyn.h" @@ -151,6 +154,12 @@ struct SDL_PrivateVideoData { int gamma_changed; /* flag: has VidMode gamma been modified? */ short *iconcolors; /* List of colors used by the icon */ + + /* Screensaver settings */ + int screensaver_timeout; +#if SDL_VIDEO_DRIVER_X11_DPMS + BOOL dpms_enabled; +#endif }; /* Old variable names */ @@ -201,6 +210,8 @@ struct SDL_PrivateVideoData { #define gamma_saved (this->hidden->gamma_saved) #define gamma_changed (this->hidden->gamma_changed) #define SDL_iconcolors (this->hidden->iconcolors) +#define screensaver_timeout (this->hidden->screensaver_timeout) +#define dpms_enabled (this->hidden->dpms_enabled) /* Some versions of XFree86 have bugs - detect if this is one of them */ #define BUGGY_XFREE86(condition, buggy_version) \ ((SDL_strcmp(ServerVendor(SDL_Display), "The XFree86 Project, Inc") == 0) && \