-
Notifications
You must be signed in to change notification settings - Fork 0
/
brightness.py
222 lines (193 loc) · 6.41 KB
/
brightness.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
import sys
import subprocess
import enum
import curses
import json
import atexit
import signal
class PLACE (enum.IntEnum):
"""Placement of the monitor on the table"""
Left = 1
Middle = 2
Right = 3
ALL = 4
# TODO Identify adapters automatically and make the interface setup location at first startup
# Configure your adapters here. The connected adapters can be found using the command
# xrandr --prop | grep connected
# If you have only two monitors, you can delete place.Middle of adapters
adapters = {PLACE.Left:'DP-1',
PLACE.Middle: 'HDMI-1',
PLACE.Right: 'DVI-D-1-1'}
orderOfPlaces = []
currentPlaceIndex = 0
def setOrder():
for place in [PLACE.Left, PLACE.Middle, PLACE.Right]:
if place in adapters.keys():
orderOfPlaces.append(place)
orderOfPlaces.append(PLACE.ALL)
def nextPlace() -> PLACE:
global currentPlaceIndex
if currentPlaceIndex < len(orderOfPlaces) -1:
currentPlaceIndex += 1
return orderOfPlaces[currentPlaceIndex]
def prevPlace() -> PLACE:
global currentPlaceIndex
if currentPlaceIndex > 0:
currentPlaceIndex -= 1
return orderOfPlaces[currentPlaceIndex]
def currentPlace() -> PLACE:
global currentPlaceIndex
return orderOfPlaces[currentPlaceIndex]
BRIGHTNESS_MAX = 1
BRIGHTNESS_MIN = 0.2
class Monitor:
brightness = 0.6
gamma = [1.0,1.0,1.0]
place: PLACE
strAdapter: str
def __init__(self, place: PLACE):
"""Set the location on the table when creating a monitor"""
self.place = place
self.strAdapter = adapters[place]
def xrandrString(self):
strGamma = ''
for item in self.gamma:
if strGamma != '' :
strGamma += ":"
strGamma += str(item)
# TODO add gamma to return string
return 'xrandr --output ' + self.strAdapter + ' --brightness ' + str(self.brightness)
def xrandrCommand(self):
subprocess.run(self.xrandrString(), shell=True)
def jsonObject(self):
gamma = json.dumps(self.gamma)
brightness = round(self.brightness, 2)
j = f"""{{
"device": "monitor",
"adapter": "{self.strAdapter}",
"place": "{self.place.name}",
"brightness": {brightness},
"gamma":{gamma}
}}"""
return json.loads(j)
def parseJson(jsonObject):
for place in PLACE:
if place.name == jsonObject['place']:
newMonitor = Monitor(place)
newMonitor.strAdapter = jsonObject['adapter']
newMonitor.brightness = jsonObject['brightness']
newMonitor.gamma = jsonObject['gamma']
return place, newMonitor
monitors: 'dict[PLACE, Monitor]' = {}
def saveSettings():
monitorsArray = []
for m in monitors.values():
monitorsArray.append(m.jsonObject())
with open("monitors.json", "w") as write_file:
json.dump(monitorsArray, write_file, indent=4)
def loadSettings():
try:
with open("monitors.json", "r") as read_file:
monitorsArray = json.load(read_file)
for monitorJson in monitorsArray:
place, newMonitor = Monitor.parseJson(monitorJson)
monitors[place] = newMonitor
# print("monitors.json was read correctly")
return monitorsArray
except FileNotFoundError:
# print("monitors.json not found")
for p in adapters.keys():
m=Monitor(p)
monitors[p] = m
def getMonitor(place: PLACE) -> Monitor:
for m in monitors.values():
if m.place is place:
return m
def brightnessPlus():
if currentPlace() is PLACE.ALL:
for m in monitors.values():
if (m.brightness * 1.1) < BRIGHTNESS_MAX:
m.brightness *= 1.1
m.xrandrCommand()
else:
m = getMonitor(currentPlace())
if (m.brightness * 1.1) < BRIGHTNESS_MAX:
m.brightness *= 1.1
m.xrandrCommand()
def brightnessMinus():
if currentPlace() is PLACE.ALL:
for m in monitors.values():
if (m.brightness / 1.1) > BRIGHTNESS_MIN:
m.brightness /= 1.1
m.xrandrCommand()
else:
m = getMonitor(currentPlace())
if (m.brightness / 1.1) > BRIGHTNESS_MIN:
m.brightness /= 1.1
m.xrandrCommand()
def refreshBrightness():
for m in monitors.values():
m.xrandrCommand()
def main(stdscr):
def printTable():
headers = [" ", "Monitor", "Brightness"]
width = [5,12,12]
cursor = '>>>'
cursorAll = '>'
i = 0
headerString = ''
for header in headers:
headerString += f'{header:<{width[i]}}'
i += 1
stdscr.move(0, 0)
stdscr.addstr(headerString)
rowNumber = 1
for place in orderOfPlaces:
if place != PLACE.ALL:
row = ''
i = 0
if currentPlace() is place:
row += f'{cursor:<{width[i]}}'
elif currentPlace() is PLACE.ALL:
row += f'{cursorAll:<{width[i]}}'
else:
row += ' ' * width[i]
i += 1
m = getMonitor(place)
row += f'{m.place.name:<{width[i]}}'
i += 1
brightness = f'{m.brightness:4.2f}'
row += f'{brightness:<{width[i]}}'
stdscr.move(rowNumber, 0)
stdscr.addstr(row)
rowNumber +=1
printTable()
stdscr.nodelay(1)
stdscr.timeout(100)
while True:
# get keyboard input, returns -1 if none available
c = stdscr.getch()
if c != -1:
if c == 259: #Up
prevPlace()
if c == 258: #Down
nextPlace()
if c == 261: #Left
brightnessPlus()
if c == 260: #Right
brightnessMinus()
if c in {258,259,260,261}:
printTable()
if c == 32: #space
refreshBrightness()
if c == 113: # press "q" for exit
saveSettings()
sys.exit()
print('\33]0;Xrandr-Brightness\a', end='', flush=True)
loadSettings()
setOrder()
atexit.register(saveSettings)
signal.signal(signal.SIGHUP, saveSettings)
signal.signal(signal.SIGINT, saveSettings)
refreshBrightness()
curses.wrapper(main)