printrun-src/printrun/power/osx.py

Tue, 19 Jan 2021 20:44:16 +0100

author
mdd
date
Tue, 19 Jan 2021 20:44:16 +0100
changeset 44
310be640a303
parent 15
0bbb006204fc
child 46
cce0af6351f0
permissions
-rw-r--r--

Added tag WORKING_BEFORE_UPGRADE_TO_GITMASTER for changeset f7e9bd735ce1

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

mercurial