forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxbright
More file actions
executable file
·74 lines (62 loc) · 2.32 KB
/
xbright
File metadata and controls
executable file
·74 lines (62 loc) · 2.32 KB
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
#!/usr/bin/env python
# Change brightness incrementally using xrandr
# since the older XF86VidModeSetGammaRamp() method no longer works.
# Usage: xbright [val|+val|-val]
# val is floating point and can go from 0 to 1 (or maybe higher?)
import sys, os
# There is an xrandr library for python:
# from Xlib import X, display
# from Xlib.ext import randr
# but it has no documentation and no examples, so rather than spending
# 2 days trying to figure out the API from reading the entire source,
# I can spend 10 minutes and parse output from /usr/bin/xrandr instead:
import subprocess
XRANDR = "/usr/bin/xrandr"
def get_brightness():
# xrandr --verbose | grep -i brightness
proc = subprocess.Popen([XRANDR, "--verbose"],
shell=False, stdout=subprocess.PIPE)
s = proc.communicate()[0]
if not s:
print("No output from xrandr --verbose!")
sys.exit(1)
monitor = None
for line in s.split('\n'):
line = line.strip()
words = line.split()
if len(words) > 1 and words[1] == "connected":
monitor = words[0]
elif line.startswith("Brightness: "):
if not monitor:
print("Warning: couldn't tell which monitor to use")
return (float(line[12:]), monitor)
print("No Brightness line in xrandr --verbose!")
sys.exit(1)
def set_brightness(new_brightness):
args = [ XRANDR ]
if monitor:
args.append("--output")
args.append(monitor)
args.append("--brightness")
args.append(str(new_brightness))
subprocess.call(args)
def Usage():
print("Usage: %s [b | +b | -b]" % os.path.basename(sys.argv[0]))
sys.exit(1)
if __name__ == "__main__":
# We need to call cur_brightness even if we're setting an absolute
# brightness, because we need to get the relevant monitor.
cur_brightness, monitor = get_brightness()
if len(sys.argv) <= 1:
print("Currently brightness is %.1f on monitor %s\n" % (cur_brightness,
monitor))
Usage()
if sys.argv[1][0] == '-':
b = cur_brightness - float(sys.argv[1][1:])
if b < 0: b = 0
elif sys.argv[1][0] == '+':
b = cur_brightness + float(sys.argv[1][1:])
if b > 1: b = 1
else:
b = float(sys.argv[1])
set_brightness(b)