Coverage for /Users/Newville/Codes/xraylarch/larch/wxlib/allow_idle_macosx.py: 100%
33 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-09 10:08 -0600
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-09 10:08 -0600
1#-----------------------------------------------------------------------------
2# adopted from IPython.lib.external._nope.py from IPython
3# [ Copyright (C) 2013 Min RK
4# Distributed under the terms of the 2-clause BSD License.
5# ]
6#-----------------------------------------------------------------------------
8from contextlib import contextmanager
10import ctypes
11import ctypes.util
13void_p = ctypes.c_void_p
14uint64 = ctypes.c_uint64
16objc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('objc'))
18objc.objc_getClass.restype = void_p
19objc.sel_registerName.restype = void_p
20objc.objc_msgSend.restype = void_p
21objc.objc_msgSend.argtypes = [void_p, void_p]
23msgSend = objc.objc_msgSend
25def as_utf8(s):
26 """ensure utf8 bytes"""
27 if not isinstance(s, bytes):
28 s = s.encode('utf8')
29 return s
31def SelName(name):
32 """create a selector name (for methods)"""
33 return objc.sel_registerName(as_utf8(name))
35def GetClass(classname):
36 """get an ObjC Class by name"""
37 return objc.objc_getClass(as_utf8(classname))
39# constants from Foundation
41NSActivityIdleDisplaySleepDisabled = (1 << 40)
42NSActivityIdleSystemSleepDisabled = (1 << 20)
43NSActivitySuddenTerminationDisabled = (1 << 14)
44NSActivityAutomaticTerminationDisabled = (1 << 15)
45NSActivityUserInitiated = (0x00FFFFFF | NSActivityIdleSystemSleepDisabled)
46NSActivityUserInitiatedAllowingIdleSystemSleep = (NSActivityUserInitiated & ~NSActivityIdleSystemSleepDisabled)
47NSActivityBackground = 0x000000FF
48NSActivityLatencyCritical = 0xFF00000000
50_activity = None
52def allow_idle():
53 """disable App Nap by setting NSActivityUserInitiatedAllowingIdleSystemSleep"""
54 global _activity
56 reason = msgSend(GetClass('NSString'),
57 SelName("stringWithUTF8String:"),
58 as_utf8('reason'))
59 info = msgSend(GetClass('NSProcessInfo'),
60 SelName('processInfo'))
61 options = uint64(NSActivityUserInitiatedAllowingIdleSystemSleep)
62 _activity = msgSend(info,
63 SelName('beginActivityWithOptions:reason:'),
64 options, void_p(reason))