Tk Source Code

Artifact [ec4304f0]
Login

Artifact ec4304f0a6367b503dbe3e9147874734ebc72607:

Attachment "blankscreen4.patch" to ticket [1623922f] added by mdejong 2006-12-29 05:47:34.
Index: doc/wm.n
===================================================================
RCS file: /cvsroot/tktoolkit/tk/doc/wm.n,v
retrieving revision 1.28
diff -u -r1.28 wm.n
--- doc/wm.n	1 Dec 2006 19:48:00 -0000	1.28
+++ doc/wm.n	28 Dec 2006 22:25:10 -0000
@@ -83,6 +83,19 @@
 2000/XP+.  Where not supported, the \fB\-transparentcolor\fR value remains
 at \fB{}\fR.
 .TP
+\fB\-blankscreen\fR
+The -blankscreen attribute is used to disable screen blanking.
+A computer may activate a screen saver or power down the monitor
+after a period of time when no user events (like a key press
+or mouse movement) have been detected. But, a presentation application
+like a video player or a slide show viewer must be able to
+indicate to the system that the screen is in use, even if no
+user events have been detected. The \fB\-blankscreen\fR attribute
+defaults to 1 and should be set to 0 to disable screen blanking.
+Take care to reset this attribute to 1 to re-enable screen blanking
+once the presentation portion of the application has finished.
+This attribute is shared across all toplevels on the screen.
+.TP
 \fB\-fullscreen\fR
 Places the window in a mode that takes up the entire screen, has no
 borders, and covers the Start menu and taskbar.
Index: tests/winWm.test
===================================================================
RCS file: /cvsroot/tktoolkit/tk/tests/winWm.test,v
retrieving revision 1.18
diff -u -r1.18 winWm.test
--- tests/winWm.test	1 Dec 2006 19:48:00 -0000	1.18
+++ tests/winWm.test	28 Dec 2006 22:25:11 -0000
@@ -235,7 +235,7 @@
     destroy .t
     toplevel .t
     wm attributes .t
-} {-alpha 1.0 -transparentcolor {} -disabled 0 -fullscreen 0 -toolwindow 0 -topmost 0}
+} {-alpha 1.0 -transparentcolor {} -disabled 0 -blankscreen 1 -fullscreen 0 -toolwindow 0 -topmost 0}
 test winWm-6.2 {wm attributes} win {
     destroy .t
     toplevel .t
@@ -246,7 +246,7 @@
     destroy .t
     toplevel .t
     list [catch {wm attributes .t -foo} msg] $msg
-} {1 {wrong # args: should be "wm attributes window ?-alpha ?double?? ?-transparentcolor ?color?? ?-disabled ?bool?? ?-fullscreen ?bool?? ?-toolwindow ?bool?? ?-topmost ?bool??"}}
+} {1 {wrong # args: should be "wm attributes window ?-alpha ?double?? ?-transparentcolor ?color?? ?-disabled ?bool?? ?-blankscreen ?bool?? ?-fullscreen ?bool?? ?-toolwindow ?bool?? ?-topmost ?bool??"}}
 
 test winWm-6.4 {wm attributes -alpha} win {
     # Expect this to return all 1.0 {} on pre-2K/XP
Index: tests/wm.test
===================================================================
RCS file: /cvsroot/tktoolkit/tk/tests/wm.test,v
retrieving revision 1.37
diff -u -r1.37 wm.test
--- tests/wm.test	28 Dec 2006 21:15:14 -0000	1.37
+++ tests/wm.test	28 Dec 2006 22:25:12 -0000
@@ -118,15 +119,15 @@
     # This is the wrong error to output - unix has it right, but it's
     # not critical.
     list [catch {wm attributes . _} err] $err
-} {1 {wrong # args: should be "wm attributes window ?-alpha ?double?? ?-transparentcolor ?color?? ?-disabled ?bool?? ?-fullscreen ?bool?? ?-toolwindow ?bool?? ?-topmost ?bool??"}}
+} {1 {wrong # args: should be "wm attributes window ?-alpha ?double?? ?-transparentcolor ?color?? ?-disabled ?bool?? ?-blankscreen ?bool?? ?-fullscreen ?bool?? ?-toolwindow ?bool?? ?-topmost ?bool??"}}
 test wm-attributes-1.2.2 {usage} win {
     list [catch {wm attributes . -alpha 1.0 -disabled} err] $err
-} {1 {wrong # args: should be "wm attributes window ?-alpha ?double?? ?-transparentcolor ?color?? ?-disabled ?bool?? ?-fullscreen ?bool?? ?-toolwindow ?bool?? ?-topmost ?bool??"}}
+} {1 {wrong # args: should be "wm attributes window ?-alpha ?double?? ?-transparentcolor ?color?? ?-disabled ?bool?? ?-blankscreen ?bool?? ?-fullscreen ?bool?? ?-toolwindow ?bool?? ?-topmost ?bool??"}}
 test wm-attributes-1.2.3 {usage} win {
     # This is the wrong error to output - unix has it right, but it's
     # not critical.
     list [catch {wm attributes . -to} err] $err
-} {1 {wrong # args: should be "wm attributes window ?-alpha ?double?? ?-transparentcolor ?color?? ?-disabled ?bool?? ?-fullscreen ?bool?? ?-toolwindow ?bool?? ?-topmost ?bool??"}}
+} {1 {wrong # args: should be "wm attributes window ?-alpha ?double?? ?-transparentcolor ?color?? ?-disabled ?bool?? ?-blankscreen ?bool?? ?-fullscreen ?bool?? ?-toolwindow ?bool?? ?-topmost ?bool??"}}
 test wm-attributes-1.2.4 {usage} {unix notAqua} {
     list [catch {wm attributes . _} err] $err
 } {1 {bad attribute "_": must be -alpha, -topmost, -zoomed, or -fullscreen}}
@@ -153,6 +154,8 @@
 } [list {} Miffo {}]
 
 
+### wm attributes . -fullscreen ###
+
 test wm-attributes-1.3.0 {default -fullscreen value} {win} {
     deleteWindows
     toplevel .t
@@ -443,9 +446,57 @@
     set results
 } {{. .a .b .c} {. .a .b .c} {. .a .b .c}}
 
+
+### wm attributes . -blankscreen ###
+
+test wm-attributes-1.6.0 {default -blankscreen value} {win} {
+    deleteWindows
+    toplevel .t
+    wm attributes .t -blankscreen
+} {1}
+
+test wm-attributes-1.6.1 {change -blankscreen} {win} {
+    deleteWindows
+    toplevel .t
+    wm attributes .t -blankscreen 0
+    wm attributes .t -blankscreen
+} {0}
+
+test wm-attributes-1.6.2 {change -blankscreen} {win} {
+    deleteWindows
+    toplevel .t
+    wm attributes .t -blankscreen 0
+    wm attributes .t -blankscreen 0
+    wm attributes .t -blankscreen
+} {0}
+
+test wm-attributes-1.6.3 {change -blankscreen} {win} {
+    deleteWindows
+    toplevel .t
+    wm attributes .t -blankscreen 0
+    wm attributes .t -blankscreen 1
+    wm attributes .t -blankscreen
+} {1}
+
+test wm-attributes-1.6.4 {-blankscreen shared across toplevels} {win} {
+    deleteWindows
+    toplevel .t
+    set results [list]
+    lappend results [wm attributes . -blankscreen]
+    lappend results [wm attributes .t -blankscreen]
+    wm attributes .t -blankscreen 0
+    lappend results [wm attributes . -blankscreen]
+    lappend results [wm attributes .t -blankscreen]
+    wm attributes .t -blankscreen 1
+    lappend results [wm attributes . -blankscreen]
+    lappend results [wm attributes .t -blankscreen]
+} {1 1 0 0 1 1}
+
+
 deleteWindows
 stdWindow
 
+
 ### wm colormapwindows ###
 test wm-colormapwindows-1.1 {usage} {
     list [catch {wm colormapwindows} err] $err
Index: win/tkWinWm.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/win/tkWinWm.c,v
retrieving revision 1.110
diff -u -r1.110 tkWinWm.c
--- win/tkWinWm.c	1 Dec 2006 19:48:00 -0000	1.110
+++ win/tkWinWm.c	28 Dec 2006 22:25:16 -0000
@@ -15,6 +15,9 @@
  * RCS: @(#) $Id: tkWinWm.c,v 1.110 2006/12/01 19:48:00 hobbs Exp $
  */
 
+/* Make EXECUTION_STATE and SetThreadExecutionState() available */
+#define _WIN32_WINNT 0x0500
+
 #include "tkWinInt.h"
 #include <shellapi.h>
 
@@ -317,6 +320,13 @@
  * WM_FULLSCREEN -		Non-zero means that this window has been placed
  *				in the full screen mode. It should be mapped at
  *				0,0 and be the width and height of the screen.
+ * WM_DISABLE_BLANKSCREEN -	non-zero means that this window is the main
+ *				window and that the -blankscreen state has
+ *				been set to 0 to disable the normal screen
+ *				blanking timers.
+ * WM_DISABLE_SCREENSAVER -	non-zero means that the screen saver was
+ *				active and has been disabled. This bit can
+ *				only be set with WM_DISABLE_BLANKSCREEN.
  */
 
 #define WM_NEVER_MAPPED			(1<<0)
@@ -333,6 +343,8 @@
 #define WM_HEIGHT_NOT_RESIZABLE		(1<<11)
 #define WM_WITHDRAWN			(1<<12)
 #define WM_FULLSCREEN			(1<<13)
+#define WM_DISABLE_BLANKSCREEN		(1<<14)
+#define WM_DISABLE_SCREENSAVER		(1<<15)
 
 /*
  * Window styles for various types of toplevel windows.
@@ -552,6 +564,15 @@
 			    Tcl_Obj *CONST objv[]);
 static void		WmUpdateGeom(WmInfo *wmPtr, TkWindow *winPtr);
 
+static int		WmSetBlankScreen(Tcl_Interp *interp,
+			    TkWindow *winPtr, int blank_screen_state);
+static int		WmGetBlankScreen(Tcl_Interp *interp);
+
+static int		WmSetScreenSaverState(TkWindow *winPtr, int state);
+static int		WmSetAPMState(int state);
+static TkWindow*	WmGetMainWindow(Tcl_Interp *interp);
+
+
 /*
  * Used in BytesPerLine
  */
@@ -2675,6 +2696,290 @@
 /*
  *----------------------------------------------------------------------
  *
+ * WmSetBlankScreen --
+ *
+ *	Sets the blankscreen state for the current application.
+ *	The -blankscreen attribute is set for a screen,
+ *	not a specific toplevel window on that screen.
+ *
+ * Results:
+ *	If there is an error disabling the blank screen state
+ *	then TCL_ERROR will be returned.
+ *
+ * Side effects:
+ *	Will enable/disable APM and the screen saver.
+ *
+ *----------------------------------------------------------------------
+ */
+
+static int
+WmSetBlankScreen(
+     Tcl_Interp *interp,	/* Current interpreter. Could be NULL */
+     TkWindow *winPtr,		/* Main window if interp is NULL */
+     int blank_screen_state)	/* -blankscreen value */
+{
+    TkWindow *mainWin;
+    WmInfo *wmPtr;
+
+    if (interp != NULL) {
+        mainWin = WmGetMainWindow(interp);
+    } else {
+        mainWin = winPtr;
+    }
+
+    if (mainWin == NULL) {
+	if (interp != NULL) {
+	    Tcl_AppendResult(interp, "main window does not exist", NULL);
+	}
+	return TCL_ERROR;
+    }
+
+    if (strcmp(mainWin->pathName, ".") != 0) {
+	if (interp != NULL) {
+	    Tcl_AppendResult(interp, "main window ",
+	        mainWin->pathName, " is not valid, must be .", NULL);
+	}
+	return TCL_ERROR;
+    }
+
+    wmPtr = mainWin->wmInfoPtr;
+
+    if ((blank_screen_state && !(wmPtr->flags & WM_DISABLE_BLANKSCREEN)) ||
+            (!blank_screen_state && (wmPtr->flags & WM_DISABLE_BLANKSCREEN))) {
+        /* Already set to the requested state */
+        return TCL_OK;
+    }
+
+    /* Disable/Enable APM */
+
+    int result = WmSetAPMState(!blank_screen_state);
+
+    /* Disable/Enable Screen Saver */
+
+    if (result == TCL_OK) {
+        result = WmSetScreenSaverState(mainWin, !blank_screen_state);
+    }
+
+    if (result != TCL_OK) {
+        Tcl_AppendResult(interp, "can't set -blankscreen property", (char *) NULL);
+        return TCL_ERROR;
+    }
+
+    if (result == TCL_OK) {
+        if (!blank_screen_state) {
+            wmPtr->flags |= WM_DISABLE_BLANKSCREEN;
+        } else {
+            wmPtr->flags &= ~WM_DISABLE_BLANKSCREEN;
+        }
+    }
+
+    return TCL_OK;
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * WmGetBlankScreen --
+ *
+ *	Get the -blankscreen state for this application.
+ *
+ * Results:
+ *	State is set.
+ *
+ * Side effects:
+ *	None.
+ *
+ *----------------------------------------------------------------------
+ */
+
+static int
+WmGetBlankScreen(
+     Tcl_Interp *interp)	/* Current interpreter. */
+{
+    TkWindow *mainWin;
+    WmInfo *wmPtr;
+
+    mainWin = WmGetMainWindow(interp);
+
+    if (mainWin == NULL) {
+	return 1; /* Default value */
+    }
+
+    wmPtr = mainWin->wmInfoPtr;
+
+    if (wmPtr->flags & WM_DISABLE_BLANKSCREEN) {
+        return 0;
+    } else {
+        return 1; /* Default value */
+    }
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * WmGetMainWindow --
+ *
+ *	Return the window that corresponds to "." in this application.
+ *
+ * Results:
+ *	None.
+ *
+ * Side effects:
+ *	None.
+ *
+ *----------------------------------------------------------------------
+ */
+
+static
+TkWindow*
+WmGetMainWindow(
+     Tcl_Interp *interp)	/* Current interpreter. */
+{
+    Tk_Window tkwin;
+    TkWindow *mainWin;
+    WmInfo *wmPtr;
+
+    tkwin = Tk_MainWindow(interp);
+    tkwin = Tk_NameToWindow(interp, ".", tkwin);
+    mainWin = (TkWindow *) tkwin;
+
+    if (mainWin == NULL) {
+	if (interp != NULL) {
+	    Tcl_AppendResult(interp, "main window does not exist", NULL);
+	}
+	return NULL;
+    }
+
+    if (strcmp(mainWin->pathName, ".") != 0) {
+	if (interp != NULL) {
+	    Tcl_AppendResult(interp, "main window ",
+	        mainWin->pathName, " is not valid, must be .", NULL);
+	}
+	return NULL;
+    }
+
+    return mainWin;
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * WmSetAPMState --
+ *
+ *	This procedure is used to diable APM power management when
+ *	screen blanking is disabled. This method will keep the APM
+ *	power management logic from shutting of the display when
+ *	no keyboard or mouse events are detected. The APM state
+ *	must be set back to enabled when finished.
+ *
+ * Results:
+ *	A standard Tcl result.
+ *
+ * Side effects:
+ *	Changes the system APM settings.
+ *
+ *----------------------------------------------------------------------
+ */
+
+int
+WmSetAPMState(
+    int state)		/* non-zero to disable APM, zero to enable */
+{
+    EXECUTION_STATE prev_state;
+    EXECUTION_STATE new_state;
+
+    if (state) {
+        /* Disable display power down via APM */
+        new_state = ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED;
+    } else {
+        /* Allow display power down via APM */
+        new_state = ES_CONTINUOUS;
+    }
+
+    prev_state = SetThreadExecutionState(new_state);
+
+    if (prev_state == 0) {
+        return TCL_ERROR;
+    } else {
+        return TCL_OK;
+    }
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * WmSetScreenSaverState --
+ *
+ *	This procedure will disable the screen saver for the
+ *	main window if the screen saver is active. The screen
+ *	saver could be disabled, if it is then nothing is done.
+ *	If the screen saver is disabled in this method, then
+ *	the WM_SCREENSAVER will be set.
+ *
+ * Results:
+ *	A standard Tcl result.
+ *
+ * Side effects:
+ *	Sets the WM_SCREENSAVER bit in wmPtr->flags.
+ *
+ *----------------------------------------------------------------------
+ */
+
+int
+WmSetScreenSaverState(
+    TkWindow *winPtr,   /* The main window for the application */
+    int state)		/* non-zero to disable screen saver, zero to enable */
+{
+    WmInfo *wmPtr = winPtr->wmInfoPtr;
+    BOOL isActive = 0;
+    BOOL success;
+
+    if (state) {
+        /* Check to see if the screen save is "active", meaning
+         * it has not been turned off in the display prefs. If
+         * the screen save is "active" this means it could
+         * take over the display at any time, not that it is
+         * actually drawing to the display at this moment.
+         */
+
+        success = SystemParametersInfo(SPI_GETSCREENSAVEACTIVE,
+            0, &isActive, 0);
+
+        if (!success) {
+            return TCL_ERROR;
+        }
+
+        if (isActive) {
+            success = SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,
+                FALSE,
+                NULL,
+                0);
+            if (!success) {
+                return TCL_ERROR;
+            }
+            wmPtr->flags |= WM_DISABLE_SCREENSAVER;
+        }
+    } else {
+        /* Reenable screen saver if it was disabled earlier. */
+
+        if (wmPtr->flags & WM_DISABLE_SCREENSAVER) {
+            success = SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,
+                TRUE,
+                NULL,
+                0);
+            if (!success) {
+                return TCL_ERROR;
+            }
+            wmPtr->flags &= ~WM_DISABLE_SCREENSAVER;
+        }
+    }
+
+    return TCL_OK;
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
  * TkpWinGetState --
  *
  *	This function returns state value of a toplevel window.
@@ -2849,6 +3154,14 @@
 	DecrIconRefCount(wmPtr->iconPtr);
     }
 
+    /*
+     * Enable APM and Screen saver if this was the main window.
+     */
+
+    if (wmPtr->flags & WM_DISABLE_BLANKSCREEN) {
+	WmSetBlankScreen(NULL, winPtr, 1);
+    }
+
     ckfree((char *) wmPtr);
     winPtr->wmInfoPtr = NULL;
 }
@@ -3149,6 +3462,8 @@
     int i, boolean, length;
     int config_fullscreen = 0;
     int fullscreen_attr_changed = 0, fullscreen_attr = 0;
+    int config_blankscreen = 0;
+    int blankscreen_attr_changed = 0, blankscreen_attr = 0;
 
     if ((objc < 3) || ((objc > 5) && ((objc%2) == 0))) {
     configArgs:
@@ -3157,6 +3472,7 @@
 		" ?-alpha ?double??"
 		" ?-transparentcolor ?color??"
 		" ?-disabled ?bool??"
+		" ?-blankscreen ?bool??"
 		" ?-fullscreen ?bool??"
 		" ?-toolwindow ?bool??"
 		" ?-topmost ?bool??");
@@ -3178,6 +3494,10 @@
 	Tcl_ListObjAppendElement(NULL, objPtr,
 		Tcl_NewBooleanObj((style & WS_DISABLED)));
 	Tcl_ListObjAppendElement(NULL, objPtr,
+		Tcl_NewStringObj("-blankscreen", -1));
+	Tcl_ListObjAppendElement(NULL, objPtr,
+		Tcl_NewBooleanObj(WmGetBlankScreen(interp)));
+	Tcl_ListObjAppendElement(NULL, objPtr,
 		Tcl_NewStringObj("-fullscreen", -1));
 	Tcl_ListObjAppendElement(NULL, objPtr,
 		Tcl_NewBooleanObj((wmPtr->flags & WM_FULLSCREEN)));
@@ -3205,6 +3525,9 @@
 					     length) == 0))) {
 	    stylePtr = &exStyle;
 	    styleBit = WS_EX_LAYERED;
+	} else if (strncmp(string, "-blankscreen", length) == 0) {
+	    config_blankscreen = 1;
+	    styleBit = 0;
 	} else if (strncmp(string, "-fullscreen", length) == 0) {
 	    config_fullscreen = 1;
 	    styleBit = 0;
@@ -3304,7 +3627,16 @@
 			    != TCL_OK)) {
 		return TCL_ERROR;
 	    }
-	    if (config_fullscreen) {
+	    if (config_blankscreen) {
+		if (objc == 4) {
+		    Tcl_SetBooleanObj(Tcl_GetObjResult(interp),
+			WmGetBlankScreen(interp));
+		} else {
+		    blankscreen_attr_changed = 1;
+		    blankscreen_attr = boolean;
+		}
+		config_blankscreen = 0;
+	    } else if (config_fullscreen) {
 		if (objc == 4) {
 		    Tcl_SetBooleanObj(Tcl_GetObjResult(interp),
 			(wmPtr->flags & WM_FULLSCREEN));
@@ -3357,7 +3689,11 @@
 	    UpdateWrapper(winPtr);
 	}
     }
-    if (fullscreen_attr_changed) {
+    if (blankscreen_attr_changed) {
+	if (WmSetBlankScreen(interp, NULL, blankscreen_attr) != TCL_OK) {
+	    return TCL_ERROR;
+	}
+    } else if (fullscreen_attr_changed) {
 	if (fullscreen_attr) {
 	    if (Tk_Attributes((Tk_Window) winPtr)->override_redirect) {
 		Tcl_AppendResult(interp,