printrun-src/printrun/power/__init__.py

changeset 15
0bbb006204fc
child 46
cce0af6351f0
equal deleted inserted replaced
14:51bf56ba3c10 15:0bbb006204fc
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:
42 # GNOME uses the right object path, try it first
43 service_name = "org.freedesktop.ScreenSaver"
44 proxy = bus.get_object(service_name,
45 "/org/freedesktop/ScreenSaver")
46 inhibit_sleep_handler = dbus.Interface(proxy, service_name)
47 # Do a test run
48 token = inhibit_sleep_handler.Inhibit("printrun", "test")
49 inhibit_sleep_handler.UnInhibit(token)
50 except dbus.DBusException:
51 # KDE uses /ScreenSaver object path, let's try it as well
52 proxy = bus.get_object(service_name,
53 "/ScreenSaver")
54 inhibit_sleep_handler = dbus.Interface(proxy, service_name)
55 token = inhibit_sleep_handler.Inhibit("printrun", "test")
56 inhibit_sleep_handler.UnInhibit(token)
57
58 def inhibit_sleep(reason):
59 global inhibit_sleep_handler, inhibit_sleep_token
60 inhibit_sleep_token = inhibit_sleep_handler.Inhibit("printrun", reason)
61
62 def deinhibit_sleep():
63 global inhibit_sleep_handler, inhibit_sleep_token
64 if inhibit_sleep_handler is None or inhibit_sleep_token is None:
65 return
66 inhibit_sleep_handler.UnInhibit(inhibit_sleep_token)
67 inhibit_sleep_token = None
68 except Exception, e:
69 logging.warning("Could not setup DBus for sleep inhibition: %s" % e)
70
71 def inhibit_sleep(reason):
72 return
73
74 def deinhibit_sleep():
75 return
76
77 try:
78 import psutil
79
80 def get_nice(nice, p = None):
81 if not p: p = psutil.Process(os.getpid())
82 if callable(p.nice):
83 return p.nice()
84 else:
85 return p.nice
86
87 def set_nice(nice, p = None):
88 if not p: p = psutil.Process(os.getpid())
89 if callable(p.nice):
90 p.nice(nice)
91 else:
92 p.nice = nice
93
94 if platform.system() != "Windows":
95 import resource
96 if hasattr(psutil, "RLIMIT_NICE"):
97 nice_limit, _ = resource.getrlimit(psutil.RLIMIT_NICE)
98 high_priority_nice = 20 - nice_limit
99 else:
100 high_priority_nice = 0
101 # RLIMIT_NICE is not available (probably OSX), let's probe
102 # Try setting niceness to -20 .. -1
103 p = psutil.Process(os.getpid())
104 orig_nice = get_nice(p)
105 for i in range(-20, 0):
106 try:
107 set_nice(i, p)
108 high_priority_nice = i
109 break
110 except psutil.AccessDenied, e:
111 pass
112 set_nice(orig_nice, p)
113
114 def set_priority():
115 if platform.system() == "Windows":
116 set_nice(psutil.HIGH_PRIORITY_CLASS)
117 else:
118 if high_priority_nice < 0:
119 set_nice(high_priority_nice)
120
121 def reset_priority():
122 if platform.system() == "Windows":
123 set_nice(psutil.NORMAL_PRIORITY_CLASS)
124 else:
125 if high_priority_nice < 0:
126 set_nice(0)
127
128 def powerset_print_start(reason):
129 set_priority()
130 inhibit_sleep(reason)
131
132 def powerset_print_stop():
133 reset_priority()
134 deinhibit_sleep()
135 except ImportError, e:
136 logging.warning("psutil unavailable, could not import power utils:" + str(e))
137
138 def powerset_print_start(reason):
139 pass
140
141 def powerset_print_stop():
142 pass

mercurial