-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplebrightness
More file actions
executable file
·177 lines (154 loc) · 5.78 KB
/
Copy pathsimplebrightness
File metadata and controls
executable file
·177 lines (154 loc) · 5.78 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
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
#!/usr/bin/python3
# MIT License
#
# Copyright (c) 2019, 2020 Javier O. Cordero Pérez
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import subprocess
import configparser
import json
from tkinter import Tk, Button, DoubleVar, Frame, Label, Scale, W, X, BOTTOM, CENTER, LEFT, HORIZONTAL
def updateBrightness(val):
# Compute brightness in the appropriate scale
brightness = round(float(val)*unit)
# Change brightness
process = subprocess.run(["brightnessctl", "-m", "s", str(brightness)], stdout=subprocess.PIPE)
# Display value from Scale in two decimal places
percentageLabel = "Brightness: " + '%.2f' % float(val) + "%"
label.config(text = percentageLabel)
def getCurrentPercentage():
process = subprocess.Popen(["brightnessctl", "-m"], stdout=subprocess.PIPE)
result = str(process.communicate()[0])[0:-3].split(',')
return round(100*float(result[2])/float(maxBrightness),3)
def toggleBrightness():
global tempPercentage
# Get current brightness
currentPercentage = getCurrentPercentage()
# If current brightness is greater than 0, save curent brightness and turn backlight off
if currentPercentage > 0:
tempPercentage = currentPercentage
percentage.set(0)
updateBrightness(0)
# Otherwise, return backlight back to its last observed value
else:
setBrightness(tempPercentage)
def setBrightness(value):
percentage.set(value)
updateBrightness(value)
def nextIntensity():
# Get current brightness
currentPercentage = getCurrentPercentage()
# Go through keys
global keys
for key in keys:
if key > currentPercentage:
print("goRightkey")
print(key)
setBrightness(key)
break
def previousIntensity():
# Get current brightness
currentPercentage = getCurrentPercentage()
# Go through keys
global keys
previous = None
l = len(keys)
for index, key in enumerate(keys):
if key >= currentPercentage and previous is not None:
print("goLeftkey")
print(previous)
setBrightness(previous)
break
else:
previous = keys[index]
def saveIntensity():
# Get current brightness
currentPercentage = getCurrentPercentage()
# Go through keys
global keys
for index, key in enumerate(keys):
# Unset Key
if key == currentPercentage:
if key > minBrightness and key < 100:
del keys[index];
break
# Set Key
elif key > currentPercentage:
keys.insert(index, currentPercentage)
break
def on_closing():
global keys
keysAsString = ', '.join(str(value) for value in keys)
keysAsString = '[' + keysAsString + ']'
config['DEFAULT']['Brightness'] = keysAsString
with open('simplebrightness.ini', 'w') as configfile:
config.write(configfile)
root.destroy()
# Get initial system values
process = subprocess.Popen(["brightnessctl", "-m"], stdout=subprocess.PIPE)
# Read process output: Take first line, convert to string, remove last 3 chars, and split on commas.
result = str(process.communicate()[0])[0:-3].split(',')
maxBrightness = int(result[4])
unit = (maxBrightness)/100
minBrightness = round(1/unit, 3)
current = round(100*float(result[2])/float(maxBrightness),3)
tempPercentage = current
# Get save file settings
config = configparser.ConfigParser()
config['DEFAULT'] = {'Brightness': '[0,' + str(minBrightness) +',100]'}
config.read('simplebrightness.ini')
# Initialize keys
keys = json.loads(config['DEFAULT']['Brightness'])
# Initialize GUI
root = Tk()
root.title("SimpleBrightness")
buttons = Frame(root)
buttons.pack(side = BOTTOM)
# Initialize brightness Label
label = Label(root, width=20, text="Brightness: "+'%.2f'%current+"%")
label.pack(anchor=CENTER, expand=True, fill=X)
# Initialize Scale
percentage = DoubleVar()
percentage.set(current)
# Using 0.001 resolution allows the scale to get precise values on a high res window
scale = Scale(root, from_=minBrightness, to=100, variable=percentage, orient=HORIZONTAL, command=updateBrightness, resolution=0.001, showvalue=0)
scale.pack(anchor=CENTER, expand=True, fill=X)
# Toggle brightness on or off
toggle = Button(buttons, command=toggleBrightness, text="\u2606")
toggle.pack(anchor=W,side=LEFT, expand=False, fill=X)
# Previous button
toggle = Button(buttons, command=previousIntensity, text="\u25C0")
toggle.pack(anchor=W, side=LEFT, expand=False, fill=X)
# Set button
toggle = Button(buttons, command=saveIntensity, text="\u25CF")
toggle.pack(anchor=W, side=LEFT, expand=False, fill=X)
# Next button
toggle = Button(buttons, command=nextIntensity, text="\u25B6")
toggle.pack(anchor=W, side=LEFT, expand=False, fill=X)
# Set minimum window size
root.update()
root.minsize(root.winfo_width(), root.winfo_height())
root.maxsize(65536, root.winfo_height())
# Trash values that are no longer necessary
del process
del result
del current
root.protocol("WM_DELETE_WINDOW", on_closing)
# Start GUI loop
root.mainloop()