Wed, 20 Jan 2021 10:17:01 +0100
updated pronterface.py losing my own changes
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 | import platform | |
17 | import logging | |
18 | import os | |
19 | ||
20 | if platform.system() == "Darwin": | |
21 | from .osx import inhibit_sleep_osx, deinhibit_sleep_osx | |
22 | inhibit_sleep = inhibit_sleep_osx | |
23 | deinhibit_sleep = deinhibit_sleep_osx | |
24 | elif platform.system() == "Windows": | |
25 | import ctypes | |
26 | ES_CONTINUOUS = 0x80000000 | |
27 | ES_SYSTEM_REQUIRED = 0x00000001 | |
28 | ||
29 | def inhibit_sleep(reason): | |
30 | mode = ES_CONTINUOUS | ES_SYSTEM_REQUIRED | |
31 | ctypes.windll.kernel32.SetThreadExecutionState(ctypes.c_int(mode)) | |
32 | ||
33 | def deinhibit_sleep(): | |
34 | ctypes.windll.kernel32.SetThreadExecutionState(ctypes.c_int(ES_CONTINUOUS)) | |
35 | else: | |
36 | try: | |
37 | import dbus | |
38 | inhibit_sleep_handler = None | |
39 | inhibit_sleep_token = None | |
40 | bus = dbus.SessionBus() | |
41 | try: | |
46 | 42 | if os.environ.get('DESKTOP_SESSION') == "mate": |
43 | # Mate uses a special service | |
44 | service_name = "org.mate.ScreenSaver" | |
45 | object_path = "/org/mate/ScreenSaver" | |
46 | else: | |
47 | # standard service name | |
48 | service_name = "org.freedesktop.ScreenSaver" | |
49 | object_path = "/org/freedesktop/ScreenSaver" | |
50 | # GNOME and Mate use the right object path, try it first | |
51 | proxy = bus.get_object(service_name, object_path) | |
15 | 52 | inhibit_sleep_handler = dbus.Interface(proxy, service_name) |
53 | # Do a test run | |
54 | token = inhibit_sleep_handler.Inhibit("printrun", "test") | |
55 | inhibit_sleep_handler.UnInhibit(token) | |
56 | except dbus.DBusException: | |
57 | # KDE uses /ScreenSaver object path, let's try it as well | |
58 | proxy = bus.get_object(service_name, | |
59 | "/ScreenSaver") | |
60 | inhibit_sleep_handler = dbus.Interface(proxy, service_name) | |
61 | token = inhibit_sleep_handler.Inhibit("printrun", "test") | |
62 | inhibit_sleep_handler.UnInhibit(token) | |
63 | ||
64 | def inhibit_sleep(reason): | |
65 | global inhibit_sleep_handler, inhibit_sleep_token | |
66 | inhibit_sleep_token = inhibit_sleep_handler.Inhibit("printrun", reason) | |
67 | ||
68 | def deinhibit_sleep(): | |
69 | global inhibit_sleep_handler, inhibit_sleep_token | |
70 | if inhibit_sleep_handler is None or inhibit_sleep_token is None: | |
71 | return | |
72 | inhibit_sleep_handler.UnInhibit(inhibit_sleep_token) | |
73 | inhibit_sleep_token = None | |
46 | 74 | except Exception as e: |
15 | 75 | logging.warning("Could not setup DBus for sleep inhibition: %s" % e) |
76 | ||
77 | def inhibit_sleep(reason): | |
78 | return | |
79 | ||
80 | def deinhibit_sleep(): | |
81 | return | |
82 | ||
83 | try: | |
84 | import psutil | |
85 | ||
86 | def get_nice(nice, p = None): | |
87 | if not p: p = psutil.Process(os.getpid()) | |
88 | if callable(p.nice): | |
89 | return p.nice() | |
90 | else: | |
91 | return p.nice | |
92 | ||
93 | def set_nice(nice, p = None): | |
94 | if not p: p = psutil.Process(os.getpid()) | |
95 | if callable(p.nice): | |
96 | p.nice(nice) | |
97 | else: | |
98 | p.nice = nice | |
99 | ||
100 | if platform.system() != "Windows": | |
101 | import resource | |
102 | if hasattr(psutil, "RLIMIT_NICE"): | |
103 | nice_limit, _ = resource.getrlimit(psutil.RLIMIT_NICE) | |
104 | high_priority_nice = 20 - nice_limit | |
105 | else: | |
106 | high_priority_nice = 0 | |
107 | # RLIMIT_NICE is not available (probably OSX), let's probe | |
108 | # Try setting niceness to -20 .. -1 | |
109 | p = psutil.Process(os.getpid()) | |
110 | orig_nice = get_nice(p) | |
111 | for i in range(-20, 0): | |
112 | try: | |
113 | set_nice(i, p) | |
114 | high_priority_nice = i | |
115 | break | |
46 | 116 | except psutil.AccessDenied as e: |
15 | 117 | pass |
118 | set_nice(orig_nice, p) | |
119 | ||
120 | def set_priority(): | |
121 | if platform.system() == "Windows": | |
122 | set_nice(psutil.HIGH_PRIORITY_CLASS) | |
123 | else: | |
124 | if high_priority_nice < 0: | |
125 | set_nice(high_priority_nice) | |
126 | ||
127 | def reset_priority(): | |
128 | if platform.system() == "Windows": | |
129 | set_nice(psutil.NORMAL_PRIORITY_CLASS) | |
130 | else: | |
131 | if high_priority_nice < 0: | |
132 | set_nice(0) | |
133 | ||
134 | def powerset_print_start(reason): | |
135 | set_priority() | |
136 | inhibit_sleep(reason) | |
137 | ||
138 | def powerset_print_stop(): | |
139 | reset_priority() | |
140 | deinhibit_sleep() | |
46 | 141 | except ImportError as e: |
15 | 142 | logging.warning("psutil unavailable, could not import power utils:" + str(e)) |
143 | ||
144 | def powerset_print_start(reason): | |
145 | pass | |
146 | ||
147 | def powerset_print_stop(): | |
148 | pass |