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
|
# -*- python -*-
import os
import sys
import time
OUTPUT, HEIGHT, YPOS, TOP = 0, 2 * 12, 0, True
LEFT_BUTTON = 1
MIDDLE_BUTTON = 2
RIGHT_BUTTON = 3
SCROLL_UP = 4
SCROLL_DOWN = 5
SCROLL_LEFT = 6
SCROLL_RIGHT = 7
FORWARD_BUTTON = 8
BACKWARD_BUTTON = 9
r, w = os.pipe()
pid = os.fork()
if not pid:
os.dup2(r, 0)
os.close(w)
os.execlp('adjbacklight', 'adjbacklight')
sys.exit(1)
os.close(r)
def get_backlight():
r, w = os.pipe()
pid = os.fork()
if not pid:
os.dup2(w, 1)
os.execlp('adjbacklight', 'adjbacklight', '-g')
sys.exit(1)
os.close(w)
ret = b''
while True:
bs = os.read(r, 512)
if not bs:
break
ret += bs
os.close(r)
os.waitpid(pid, 0)
ret = float(ret.decode('utf-8', 'replace').rstrip('%\n')) / 100
return ret
def set_backlight():
if 0 <= backlight <= 1:
os.write(w, b'%f%%\n' % (backlight * 100))
time.sleep(0.0001)
backlight = get_backlight()
def redraw():
x = int(bar.width * backlight + 0.5)
bar.change_colour(bar.foreground)
bar.window.fill_rectangle(bar.gc, 0, 0, x, HEIGHT)
bar.change_colour(bar.background)
bar.window.fill_rectangle(bar.gc, x, 0, bar.width - x, HEIGHT)
import x as _x
_get_event_mask = _x.get_event_mask
def get_event_mask():
return _get_event_mask() | Xlib.X.ButtonMotionMask
_x.get_event_mask = get_event_mask
def unhandled_event(e):
global backlight
if isinstance(e, Xlib.protocol.event.ButtonPress):
button, x = e.detail, e.event_x
if button == LEFT_BUTTON:
backlight = min(max(0, x / bar.width), 1)
set_backlight()
bar.invalidate()
elif button == SCROLL_UP:
backlight = min(backlight + 0.05, 1)
set_backlight()
bar.invalidate()
elif button == SCROLL_DOWN:
backlight = max(backlight - 0.05, 0)
set_backlight()
bar.invalidate()
elif button in (MIDDLE_BUTTON, RIGHT_BUTTON, FORWARD_BUTTON, BACKWARD_BUTTON):
os.close(w)
os.waitpid(pid, 0)
raise KeyboardInterrupt()
elif isinstance(e, Xlib.protocol.event.MotionNotify):
if e.detail == 0:
backlight = min(max(0, e.event_x / bar.width), 1)
set_backlight()
bar.invalidate()
|