-
Notifications
You must be signed in to change notification settings - Fork 0
/
blight.py
executable file
·297 lines (244 loc) · 7.38 KB
/
blight.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/python3
import sys
import gi
gi.require_version("GUdev", "1.0")
from gi.repository import GUdev, GLib, Gio
import argparse
from functools import partial
import re
from math import floor, ceil, log, exp
from bisect import bisect_right as bisect, bisect_left
from operator import neg
def die(message):
print(message, file = sys.stderr)
exit(1)
def get_default_device():
gclient = GUdev.Client()
devices = gclient.query_by_subsystem("backlight")
devices = [ (dev.get_sysfs_attr("type"), dev) for dev in devices ]
# Prefer firmware
for devtype, dev in devices:
if devtype == "firmware":
return dev
# ... then platform
for devtype, dev in devices:
if devtype == "platform":
return dev
# ... then raw under enabled drm-connectors
for devtype, dev in devices:
if devtype == "raw":
parent = dev.get_parent()
enabled = parent.get_sysfs_attr("enabled")
if enabled == "enabled":
return dev
die(f"Cannot find a suitable backlight device")
def get_named_device(devname):
if '/' in devname:
try:
subsystem, name = devname.split('/')
except ValueError as e:
print(f"Invalid device name: {devname!r}")
exit(1)
else:
subsystem = 'backlight'
name = devname
gclient = GUdev.Client()
dev = gclient.query_by_subsystem_and_name(subsystem, name)
if dev:
return dev
else:
die(f"No such device: {devname!r}")
def devname(dev):
subsystem = dev.get_subsystem()
name = dev.get_name()
return f"{subsystem}/{name}"
def logsteps(maxb, steps):
steps = min(maxb - 1, steps)
ret = list(range(1, steps + 1)) + [maxb]
for n in range(1, steps):
scale = (maxb / n) ** (1 / (steps - n + 1))
sep = n * (scale - 1)
if sep > 1: break
for maxb in range(n, steps):
ret[maxb] = ret[maxb - 1] * scale
return list(map(round, ret))
def make_brightness_param(brightness, dev):
return GLib.Variant(
'(ssu)',
[
dev.get_subsystem(),
dev.get_name(),
round(brightness),
]
)
def parse_set_value(target, dev):
max_brightness = dev.get_sysfs_attr_as_int("max_brightness")
cur_brightness = dev.get_sysfs_attr_as_int("brightness")
percent = max_brightness / 100
if max_brightness < 99 and dev.get_sysfs_attr("type") == "raw":
min_brightness = 0
elif dev.get_subsystem() != "backlight":
min_brightness = 0
else:
min_brightness = 1
def clamp(brightness):
if brightness < min_brightness:
return min_brightness
if brightness > max_brightness:
return max_brightness
return brightness
# Log step
if target.startswith('+//') or target.startswith('-//'):
steps = int(target[0] + target[3:])
if steps < 0:
levels = logsteps(max_brightness, -steps)
level = bisect_left(levels, cur_brightness)
brightness = levels[max(0, level - 1)]
else:
levels = logsteps(max_brightness, steps)
level = bisect(levels, cur_brightness)
brightness = levels[min(steps, level)]
return clamp(brightness)
# Linear step
if target.startswith('+/') or target.startswith('-/'):
try:
steps = max_brightness // int(target[0] + target[2:])
except ValueError as e:
die(f"Invalid brightness value: {target!r}")
if not cur_brightness % steps:
brightness = cur_brightness + steps
else:
brightness = cur_brightness - (cur_brightness % -steps)
return clamp(brightness)
# Relative set
if target.startswith('x'):
try:
scale = float(target[1:])
except ValueError as e:
die(f"Invalid brightness value: {target!r}")
brightness = round(cur_brightness * scale)
if brightness == cur_brightness:
if scale > 1:
brightness += 1
elif scale < 1:
brightness -= 1
return clamp(brightness)
if target.startswith('/'):
try:
scale = float(target[1:])
except ValueError as e:
die(f"Invalid brightness value: {target!r}")
brightness = round(cur_brightness / scale)
if brightness == cur_brightness:
if scale > 1:
brightness -= 1
elif scale < 1:
brightness += 1
return clamp(brightness)
brightness = 0
if target.startswith('+') or target.startswith('-'):
brightness = cur_brightness
# Absolute set
value = 0
try:
if target.endswith('%'):
value += float(target[:-1]) * percent
else:
value += float(target)
except ValueError as e:
die(f"Invalid brightness value: {target!r}")
if value == 0:
return brightness
else:
return clamp(brightness + value)
def set_brightness(target, dev = None):
if not dev:
dev = get_default_device()
brightness = parse_set_value(target, dev)
param = make_brightness_param(brightness, dev)
logind_set_brightness(param)
def logind_set_brightness(param):
method = [
'org.freedesktop.login1',
'/org/freedesktop/login1/session/auto',
'org.freedesktop.login1.Session',
'SetBrightness'
]
bus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
try:
bus.call_sync(
*method, param, None,
Gio.DBusCallFlags.NONE, -1, None
)
except GLib.GError as e:
print(e.message, file = sys.stderr)
def parse_toggle_value(target, dev):
max_brightness = dev.get_sysfs_attr_as_int("max_brightness")
cur_brightness = dev.get_sysfs_attr_as_int("brightness")
if not target:
brightness = (cur_brightness + 1) % (max_brightness + 1)
return brightness
else:
try:
value = int(target)
except ValueError as e:
die(f"Invalid toggle value")
brightness = 0
if target.startswith('+') or target.startswith('-'):
brightness = (cur_brightness + value) % (max_brightness + 1)
elif not cur_brightness == value:
brightness = value
return brightness
def toggle_leds(target, dev = None):
if not dev:
dev = get_default_device()
brightness = parse_toggle_value(target, dev)
param = make_brightness_param(brightness, dev)
logind_set_brightness(param)
getters = {
'default-device': None, # Handled
'brightness': lambda dev: dev.get_sysfs_attr("brightness"),
'max-brightness': lambda dev: dev.get_sysfs_attr("max_brightness"),
}
def get_value(value, dev = None):
if value == "default-device":
dev = get_default_device()
return devname(dev)
if value == "help":
return list(getters)
if not dev:
dev = get_default_device()
if value in getters:
return getters[value](dev)
elif not value:
return getters["brightness"](dev)
else:
die(f"Unknown query: {value!r}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog = "blight")
parser.add_argument('-d', '--device', help = "Which backlight device to modify")
subparsers = parser.add_subparsers(dest = "action", required = True)
parser_set = subparsers.add_parser("set", help = "Set an exact or relative brightness value")
parser_set.add_argument("value", help = "A brightness value.")
parser_get = subparsers.add_parser("get", help = "Inspect brightness devices")
parser_get.add_argument("value", help = "Value to get", nargs = '?')
parser_toggle = subparsers.add_parser("toggle", help = "Toggle a led")
parser_toggle.add_argument("value", help = "Value to toggle when on", nargs = '?')
# replace '-' for numbers
escape = partial(re.sub, r'^-(?=[0-9/])', '\u2212')
argv = list(map(escape, sys.argv[1:]))
args = parser.parse_args(argv)
dev = get_named_device(args.device) if args.device else None
if args.action == "set":
value = args.value.replace('\u2212', '-')
set_brightness(value, dev = dev)
elif args.action == "toggle":
value = args.value and args.value.replace('\u2212', '-')
toggle_leds(value, dev = dev)
elif args.action == "get":
result = get_value(args.value, dev = dev)
if isinstance(result, list):
for item in result:
print(item)
else:
print(result)