-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsleep_manager.py
129 lines (104 loc) · 3.87 KB
/
sleep_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import ctypes
import platform
from PySide6.QtDBus import QDBusConnection, QDBusInterface, QDBusMessage
mac_assertion_id = None
linux_cookie = None
def create_cfstring(string):
"""Helper function to create a CFStringRef"""
CoreFoundation = ctypes.cdll.LoadLibrary(
"/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"
)
CoreFoundation.CFStringCreateWithCString.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_uint32,
]
CoreFoundation.CFStringCreateWithCString.restype = ctypes.c_void_p
kCFStringEncodingUTF8 = 0x08000100
return CoreFoundation.CFStringCreateWithCString(
None, string.encode("utf-8"), kCFStringEncodingUTF8
)
def prevent_sleep():
if platform.system() == "Windows":
prevent_sleep_windows()
elif platform.system() == "Darwin":
prevent_sleep_macos()
elif platform.system() == "Linux":
prevent_sleep_linux()
def allow_sleep():
if platform.system() == "Windows":
allow_sleep_windows()
elif platform.system() == "Darwin":
allow_sleep_macos()
elif platform.system() == "Linux":
allow_sleep_linux()
def prevent_sleep_windows():
ES_CONTINUOUS = 0x80000000
ES_SYSTEM_REQUIRED = 0x00000001
ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED)
def allow_sleep_windows():
ES_CONTINUOUS = 0x80000000
ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS)
def prevent_sleep_macos():
global mac_assertion_id
IOKit = ctypes.cdll.LoadLibrary("/System/Library/Frameworks/IOKit.framework/IOKit")
# Define CFSTR constants
kIOPMAssertionTypeNoDisplaySleep = create_cfstring("NoDisplaySleepAssertion")
reasonForActivity = create_cfstring("QiTV video playback")
# Function signature
IOKit.IOPMAssertionCreateWithName.argtypes = [
ctypes.c_void_p,
ctypes.c_uint32,
ctypes.c_void_p,
ctypes.POINTER(ctypes.c_uint32),
]
IOKit.IOPMAssertionCreateWithName.restype = ctypes.c_uint32
# Constants
kIOPMAssertionLevelOn = 255
assertionID = ctypes.c_uint32(0)
# Create assertion
success = IOKit.IOPMAssertionCreateWithName(
kIOPMAssertionTypeNoDisplaySleep,
kIOPMAssertionLevelOn,
reasonForActivity,
ctypes.byref(assertionID),
)
if success == 0: # kIOReturnSuccess == 0
mac_assertion_id = assertionID.value
def allow_sleep_macos():
global mac_assertion_id
if mac_assertion_id is not None:
IOKit = ctypes.cdll.LoadLibrary(
"/System/Library/Frameworks/IOKit.framework/IOKit"
)
IOKit.IOPMAssertionRelease.argtypes = [ctypes.c_uint32]
IOKit.IOPMAssertionRelease(mac_assertion_id)
mac_assertion_id = None
def prevent_sleep_linux():
global linux_cookie
interface = "org.freedesktop.ScreenSaver"
object_path = "/org/freedesktop/ScreenSaver"
service_name = "org.freedesktop.ScreenSaver"
connection = QDBusConnection.sessionBus()
screensaver_interface = QDBusInterface(
service_name, object_path, interface, connection
)
if screensaver_interface.isValid():
reply = screensaver_interface.call("Inhibit", "QiTV", "Playing video")
try:
if reply.type() == QDBusMessage.ReplyMessage:
linux_cookie = reply.arguments()[0]
except:
pass
def allow_sleep_linux():
global linux_cookie
if linux_cookie:
interface = "org.freedesktop.ScreenSaver"
object_path = "/org/freedesktop/ScreenSaver"
service_name = "org.freedesktop.ScreenSaver"
connection = QDBusConnection.sessionBus()
screensaver_interface = QDBusInterface(
service_name, object_path, interface, connection
)
if screensaver_interface.isValid():
screensaver_interface.call("UnInhibit", linux_cookie)