Tue, 19 Jan 2021 20:25:47 +0100
NeoCube laser cutting improvements
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 | |
44 | cfstring = CoreFoundation.CFStringCreateWithCString(None, string, encoding) | |
45 | return objc.pyobjc_id(cfstring.nsstring()) | |
46 | ||
47 | def AssertionCreateWithName(framework, a_type, | |
48 | a_level, a_reason): | |
49 | # this method will create an assertion using the IOKit library | |
50 | # several parameters | |
51 | a_id = ctypes.c_uint32(0) | |
52 | a_type = StringToCFString(a_type) | |
53 | a_reason = StringToCFString(a_reason) | |
54 | a_error = framework.IOPMAssertionCreateWithName( | |
55 | a_type, a_level, a_reason, ctypes.byref(a_id)) | |
56 | ||
57 | # we get back a 0 or stderr, along with a unique c_uint | |
58 | # representing the assertion ID so we can release it later | |
59 | return a_error, a_id | |
60 | ||
61 | def AssertionRelease(framework, assertion_id): | |
62 | # releasing the assertion is easy, and also returns a 0 on | |
63 | # success, or stderr otherwise | |
64 | return framework.IOPMAssertionRelease(assertion_id) | |
65 | ||
66 | def inhibit_sleep_osx(reason): | |
67 | no_idle = "NoIdleSleepAssertion" | |
68 | ||
69 | # Initialize IOKit framework | |
70 | if inhibit_sleep_osx.framework is None: | |
71 | inhibit_sleep_osx.framework = SetUpIOFramework() | |
72 | framework = inhibit_sleep_osx.framework | |
73 | ||
74 | # Start inhibition | |
75 | ret, a_id = AssertionCreateWithName(framework, no_idle, 255, reason) | |
76 | inhibit_sleep_osx.assertion_id = a_id | |
77 | return ret | |
78 | inhibit_sleep_osx.framework = None | |
79 | ||
80 | def deinhibit_sleep_osx(): | |
81 | return AssertionRelease(inhibit_sleep_osx.framework, | |
82 | inhibit_sleep_osx.assertion_id) |