Show More
@@ -0,0 +1,15 b'' | |||||
|
1 | ||||
|
2 | try: | |||
|
3 | from appnope import * | |||
|
4 | except ImportError: | |||
|
5 | __version__ = '0.0.5' | |||
|
6 | import sys | |||
|
7 | import platform | |||
|
8 | from distutils.version import LooseVersion as V | |||
|
9 | ||||
|
10 | if sys.platform != "darwin" or V(platform.mac_ver()[0]) < V("10.9"): | |||
|
11 | from _dummy import * | |||
|
12 | else: | |||
|
13 | from ._nope import * | |||
|
14 | ||||
|
15 | del sys, platform, V |
@@ -0,0 +1,30 b'' | |||||
|
1 | #----------------------------------------------------------------------------- | |||
|
2 | # Copyright (C) 2013 Min RK | |||
|
3 | # | |||
|
4 | # Distributed under the terms of the 2-clause BSD License. | |||
|
5 | #----------------------------------------------------------------------------- | |||
|
6 | ||||
|
7 | from contextlib import contextmanager | |||
|
8 | ||||
|
9 | def beginActivityWithOptions(options, reason=""): | |||
|
10 | return | |||
|
11 | ||||
|
12 | def endActivity(activity): | |||
|
13 | return | |||
|
14 | ||||
|
15 | def nope(): | |||
|
16 | return | |||
|
17 | ||||
|
18 | def nap(): | |||
|
19 | return | |||
|
20 | ||||
|
21 | ||||
|
22 | @contextmanager | |||
|
23 | def nope_scope( | |||
|
24 | options=0, | |||
|
25 | reason="Because Reasons" | |||
|
26 | ): | |||
|
27 | yield | |||
|
28 | ||||
|
29 | def napping_allowed(): | |||
|
30 | return True No newline at end of file |
@@ -0,0 +1,126 b'' | |||||
|
1 | #----------------------------------------------------------------------------- | |||
|
2 | # Copyright (C) 2013 Min RK | |||
|
3 | # | |||
|
4 | # Distributed under the terms of the 2-clause BSD License. | |||
|
5 | #----------------------------------------------------------------------------- | |||
|
6 | ||||
|
7 | from contextlib import contextmanager | |||
|
8 | ||||
|
9 | import ctypes | |||
|
10 | import ctypes.util | |||
|
11 | ||||
|
12 | objc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('objc')) | |||
|
13 | ||||
|
14 | void_p = ctypes.c_void_p | |||
|
15 | ull = ctypes.c_uint64 | |||
|
16 | ||||
|
17 | objc.objc_getClass.restype = void_p | |||
|
18 | objc.sel_registerName.restype = void_p | |||
|
19 | objc.objc_msgSend.restype = void_p | |||
|
20 | objc.objc_msgSend.argtypes = [void_p, void_p] | |||
|
21 | ||||
|
22 | msg = objc.objc_msgSend | |||
|
23 | ||||
|
24 | def _utf8(s): | |||
|
25 | """ensure utf8 bytes""" | |||
|
26 | if not isinstance(s, bytes): | |||
|
27 | s = s.encode('utf8') | |||
|
28 | return s | |||
|
29 | ||||
|
30 | def n(name): | |||
|
31 | """create a selector name (for methods)""" | |||
|
32 | return objc.sel_registerName(_utf8(name)) | |||
|
33 | ||||
|
34 | def C(classname): | |||
|
35 | """get an ObjC Class by name""" | |||
|
36 | return objc.objc_getClass(_utf8(classname)) | |||
|
37 | ||||
|
38 | # constants from Foundation | |||
|
39 | ||||
|
40 | NSActivityIdleDisplaySleepDisabled = (1 << 40) | |||
|
41 | NSActivityIdleSystemSleepDisabled = (1 << 20) | |||
|
42 | NSActivitySuddenTerminationDisabled = (1 << 14) | |||
|
43 | NSActivityAutomaticTerminationDisabled = (1 << 15) | |||
|
44 | NSActivityUserInitiated = (0x00FFFFFF | NSActivityIdleSystemSleepDisabled) | |||
|
45 | NSActivityUserInitiatedAllowingIdleSystemSleep = (NSActivityUserInitiated & ~NSActivityIdleSystemSleepDisabled) | |||
|
46 | NSActivityBackground = 0x000000FF | |||
|
47 | NSActivityLatencyCritical = 0xFF00000000 | |||
|
48 | ||||
|
49 | def beginActivityWithOptions(options, reason=""): | |||
|
50 | """Wrapper for: | |||
|
51 | ||||
|
52 | [ [ NSProcessInfo processInfo] | |||
|
53 | beginActivityWithOptions: (uint64)options | |||
|
54 | reason: (str)reason | |||
|
55 | ] | |||
|
56 | """ | |||
|
57 | NSProcessInfo = C('NSProcessInfo') | |||
|
58 | NSString = C('NSString') | |||
|
59 | ||||
|
60 | reason = msg(NSString, n("stringWithUTF8String:"), _utf8(reason)) | |||
|
61 | info = msg(NSProcessInfo, n('processInfo')) | |||
|
62 | activity = msg(info, | |||
|
63 | n('beginActivityWithOptions:reason:'), | |||
|
64 | ull(options), | |||
|
65 | void_p(reason) | |||
|
66 | ) | |||
|
67 | return activity | |||
|
68 | ||||
|
69 | def endActivity(activity): | |||
|
70 | """end a process activity assertion""" | |||
|
71 | NSProcessInfo = C('NSProcessInfo') | |||
|
72 | info = msg(NSProcessInfo, n('processInfo')) | |||
|
73 | msg(info, n("endActivity:"), void_p(activity)) | |||
|
74 | ||||
|
75 | _theactivity = None | |||
|
76 | ||||
|
77 | def nope(): | |||
|
78 | """disable App Nap by setting NSActivityUserInitiatedAllowingIdleSystemSleep""" | |||
|
79 | global _theactivity | |||
|
80 | _theactivity = beginActivityWithOptions( | |||
|
81 | NSActivityUserInitiatedAllowingIdleSystemSleep, | |||
|
82 | "Because Reasons" | |||
|
83 | ) | |||
|
84 | ||||
|
85 | def nap(): | |||
|
86 | """end the caffeinated state started by `nope`""" | |||
|
87 | global _theactivity | |||
|
88 | if _theactivity is not None: | |||
|
89 | endActivity(_theactivity) | |||
|
90 | _theactivity = None | |||
|
91 | ||||
|
92 | def napping_allowed(): | |||
|
93 | """is napping allowed?""" | |||
|
94 | return _theactivity is None | |||
|
95 | ||||
|
96 | @contextmanager | |||
|
97 | def nope_scope( | |||
|
98 | options=NSActivityUserInitiatedAllowingIdleSystemSleep, | |||
|
99 | reason="Because Reasons" | |||
|
100 | ): | |||
|
101 | """context manager for beginActivityWithOptions. | |||
|
102 | ||||
|
103 | Within this context, App Nap will be disabled. | |||
|
104 | """ | |||
|
105 | activity = beginActivityWithOptions(options, reason) | |||
|
106 | try: | |||
|
107 | yield | |||
|
108 | finally: | |||
|
109 | endActivity(activity) | |||
|
110 | ||||
|
111 | __all__ = [ | |||
|
112 | "NSActivityIdleDisplaySleepDisabled", | |||
|
113 | "NSActivityIdleSystemSleepDisabled", | |||
|
114 | "NSActivitySuddenTerminationDisabled", | |||
|
115 | "NSActivityAutomaticTerminationDisabled", | |||
|
116 | "NSActivityUserInitiated", | |||
|
117 | "NSActivityUserInitiatedAllowingIdleSystemSleep", | |||
|
118 | "NSActivityBackground", | |||
|
119 | "NSActivityLatencyCritical", | |||
|
120 | "beginActivityWithOptions", | |||
|
121 | "endActivity", | |||
|
122 | "nope", | |||
|
123 | "nap", | |||
|
124 | "napping_allowed", | |||
|
125 | "nope_scope", | |||
|
126 | ] |
General Comments 0
You need to be logged in to leave comments.
Login now