chiark / gitweb /
Prevent forced sleep on windows by going to away mode when supported
authorYouness Alaoui <kakaroto@kakaroto.homelinux.net>
Wed, 31 Dec 2014 03:41:45 +0000 (22:41 -0500)
committerYouness Alaoui <kakaroto@kakaroto.homelinux.net>
Wed, 31 Dec 2014 03:41:45 +0000 (22:41 -0500)
This will prevent the computer from going to sleep even if the user
puts the computer to sleep manually. It will go to awaymode
which is identical to sleep mode but the application will keep running
in the background until it is done.
This should help with issue #46

Cura/gui/printWindow.py

index 8a831bed9b616bc1f711a5f497b39e2f04e5722e..db57465ddc72f7b0d9aa00d1b9ef73526f156a78 100644 (file)
@@ -17,9 +17,13 @@ if sys.platform.startswith('win'):
                """
                ES_CONTINUOUS = 0x80000000
                ES_SYSTEM_REQUIRED = 0x00000001
+               ES_AWAYMODE_REQUIRED = 0x00000040
                #SetThreadExecutionState returns 0 when failed, which is ignored. The function should be supported from windows XP and up.
                if prevent:
-                       ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED)
+                       # For Vista and up we use ES_AWAYMODE_REQUIRED to prevent a print from failing if the PC does go to sleep
+                       # As it's not supported on XP, we catch the error and fallback to using ES_SYSTEM_REQUIRED only
+                       if ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED) == 0:
+                               ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED)
                else:
                        ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS)