-
Notifications
You must be signed in to change notification settings - Fork 116
/
xbright
executable file
·115 lines (94 loc) · 3.74 KB
/
xbright
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
#!/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?)
from __future__ import print_function
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"
# Order of preference of monitors. If there's an external monitor connected,
# adjust that monitor rather than the built-in one.
monprefs = [ "HDMI", "DP", "VGA", "eDP", "LVDM" ]
def get_brightness():
"""Return the current brightness setting from the first
(as defined by xrandr --verbose) currently active monitor.
Currently this is a float between 0 and 1.
"""
# xrandr --verbose --current | grep Brightness:
proc = subprocess.Popen([ XRANDR, "--verbose", "--current" ],
shell=False, stdout=subprocess.PIPE)
lines = proc.communicate()[0].decode().split('\n')
if not lines:
print("No output from xrandr --verbose!")
sys.exit(1)
newmonitor = None
monitors = []
for line in lines:
line = line.strip()
words = line.split()
if len(words) > 1 and words[1] == "connected":
newmonitor = words[0]
elif line.startswith("Brightness: "):
if not newmonitor:
print("Warning: couldn't tell which monitor to use")
newmonitor = 'UNKNOWN'
monitors.append( (newmonitor, float(line[12:])) )
newmonitor = None
# Now monitors is a list of active monitors [ (monitor, brightness) ]
for montype in monprefs:
for monitor, brightness in monitors:
if monitor.startswith(montype):
return brightness, monitor
print("Didn't find any monitors of type", ",".join(monprefs))
if monitors:
print("Using", monitors[0][0])
return monitors[0][1], monitors[0][0]
print("No Brightness line in xrandr --verbose!")
sys.exit(1)
def set_brightness(new_brightness, monitor, redshift=True):
"""Set the brightness.
If redshift is True, then adjust the color balance
to get redder as the screen gets dimmer, for evening use.
"""
args = [ XRANDR ]
if monitor:
args.append("--output")
args.append(monitor)
args.append("--brightness")
args.append(str(new_brightness))
if redshift:
red = (1.0 + new_brightness) / 2.
blue = new_brightness
green = (red*2 + blue) / 3.
# print(red, green, blue)
args.append("--gamma")
args.append('%0.2f:%0.2f:%0.2f' % (red, green, blue))
subprocess.call(args)
def Usage():
print("Usage: %s [b | +b | -b]" % os.path.basename(sys.argv[0]))
print("Brightness range is from 0 to 1.")
sys.exit(1)
if __name__ == "__main__":
# Need to call cur_brightness even when setting an absolute brightness,
# in order to 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, monitor)