Platform independent wrapper for controlling sleep

This commit is contained in:
Kovid Goyal 2021-10-24 12:05:56 +05:30
parent 3fd1383c80
commit a44cb71cbb
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 40 additions and 1 deletions

View File

@ -0,0 +1,39 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2021, Kovid Goyal <kovid at kovidgoyal.net>
from calibre.constants import ismacos, iswindows
if iswindows:
from winutil import (
ES_CONTINUOUS, ES_DISPLAY_REQUIRED, ES_SYSTEM_REQUIRED,
set_thread_execution_state
)
def prevent_sleep(reason=''):
set_thread_execution_state(ES_CONTINUOUS | ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED)
print(11111111111)
return 1
def allow_sleep(cookie):
set_thread_execution_state(ES_CONTINUOUS)
print(2222222222)
elif ismacos:
from cocoa import (
create_io_pm_assertion, kIOPMAssertionTypeNoDisplaySleep,
release_io_pm_assertion
)
def prevent_sleep(reason=''):
return create_io_pm_assertion(kIOPMAssertionTypeNoDisplaySleep, reason or 'E-book viewer automated reading in progress')
def allow_sleep(cookie):
release_io_pm_assertion(cookie)
else:
def prevent_sleep(reason=''):
return 0
def allow_sleep(cookie):
pass

View File

@ -988,7 +988,7 @@ wait_named_pipe(PyObject *self, PyObject *args) {
static PyObject*
set_thread_execution_state(PyObject *self, PyObject *args) {
unsigned long new_state;
if (!PyArg_ParseTuple(args, "k", new_state)) return NULL;
if (!PyArg_ParseTuple(args, "k", &new_state)) return NULL;
if (SetThreadExecutionState(new_state) == NULL) return PyErr_SetFromWindowsErr(0);
Py_RETURN_NONE;
}