Wed, 20 Jan 2021 10:15:13 +0100
updated and added new files for printrun
15 | 1 | # This file is part of the Printrun suite. |
2 | # | |
3 | # Printrun is free software: you can redistribute it and/or modify | |
4 | # it under the terms of the GNU General Public License as published by | |
5 | # the Free Software Foundation, either version 3 of the License, or | |
6 | # (at your option) any later version. | |
7 | # | |
8 | # Printrun is distributed in the hope that it will be useful, | |
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | # GNU General Public License for more details. | |
12 | # | |
13 | # You should have received a copy of the GNU General Public License | |
14 | # along with Printrun. If not, see <http://www.gnu.org/licenses/>. | |
15 | # | |
16 | # Imported from http://www.benden.us/journal/2014/OS-X-Power-Management-No-Sleep-Howto/ | |
17 | # Copyright (c) Joseph Benden 2014 | |
18 | ||
19 | import ctypes | |
20 | import CoreFoundation | |
21 | import objc | |
22 | ||
23 | def SetUpIOFramework(): | |
24 | # load the IOKit library | |
25 | framework = ctypes.cdll.LoadLibrary( | |
26 | '/System/Library/Frameworks/IOKit.framework/IOKit') | |
27 | ||
28 | # declare parameters as described in IOPMLib.h | |
29 | framework.IOPMAssertionCreateWithName.argtypes = [ | |
30 | ctypes.c_void_p, # CFStringRef | |
31 | ctypes.c_uint32, # IOPMAssertionLevel | |
32 | ctypes.c_void_p, # CFStringRef | |
33 | ctypes.POINTER(ctypes.c_uint32)] # IOPMAssertionID | |
34 | framework.IOPMAssertionRelease.argtypes = [ | |
35 | ctypes.c_uint32] # IOPMAssertionID | |
36 | return framework | |
37 | ||
38 | def StringToCFString(string): | |
39 | # we'll need to convert our strings before use | |
40 | try: | |
41 | encoding = CoreFoundation.kCFStringEncodingASCII | |
42 | except AttributeError: | |
43 | encoding = 0x600 | |
46 | 44 | string = string.encode('ascii') |
15 | 45 | cfstring = CoreFoundation.CFStringCreateWithCString(None, string, encoding) |
46 | return objc.pyobjc_id(cfstring.nsstring()) | |
47 | ||
48 | def AssertionCreateWithName(framework, a_type, | |
49 | a_level, a_reason): | |
50 | # this method will create an assertion using the IOKit library | |
51 | # several parameters | |
52 | a_id = ctypes.c_uint32(0) | |
53 | a_type = StringToCFString(a_type) | |
54 | a_reason = StringToCFString(a_reason) | |
55 | a_error = framework.IOPMAssertionCreateWithName( | |
56 | a_type, a_level, a_reason, ctypes.byref(a_id)) | |
57 | ||
58 | # we get back a 0 or stderr, along with a unique c_uint | |
59 | # representing the assertion ID so we can release it later | |
60 | return a_error, a_id | |
61 | ||
62 | def AssertionRelease(framework, assertion_id): | |
63 | # releasing the assertion is easy, and also returns a 0 on | |
64 | # success, or stderr otherwise | |
65 | return framework.IOPMAssertionRelease(assertion_id) | |
66 | ||
67 | def inhibit_sleep_osx(reason): | |
68 | no_idle = "NoIdleSleepAssertion" | |
69 | ||
70 | # Initialize IOKit framework | |
71 | if inhibit_sleep_osx.framework is None: | |
72 | inhibit_sleep_osx.framework = SetUpIOFramework() | |
73 | framework = inhibit_sleep_osx.framework | |
74 | ||
75 | # Start inhibition | |
76 | ret, a_id = AssertionCreateWithName(framework, no_idle, 255, reason) | |
77 | inhibit_sleep_osx.assertion_id = a_id | |
78 | return ret | |
79 | inhibit_sleep_osx.framework = None | |
80 | ||
81 | def deinhibit_sleep_osx(): | |
82 | return AssertionRelease(inhibit_sleep_osx.framework, | |
83 | inhibit_sleep_osx.assertion_id) |