-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkbd.py
executable file
·84 lines (53 loc) · 1.94 KB
/
kbd.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
#!/usr/bin/env python3
import argparse
import subprocess
def userOwns() -> bool:
owner = subprocess.run(['ls', '-n', '/sys/class/leds/kbd_backlight/brightness', '|', 'awk', "'{print", "$3}'"], capture_output=True, text=True)
user = subprocess.run(['id', '-u'], capture_output=True, text=True)
return bool(user == owner)
def getCurrent() -> int:
with open("/sys/class/leds/kbd_backlight/brightness", "r") as f:
return int(f.read())
def getMax() -> int:
with open("/sys/class/leds/kbd_backlight/max_brightness", "r") as f:
return int(f.read())
def setBright(value: int) -> int:
if not userOwns():
subprocess.run(['pkexec', 'chmod', '666', '/sys/class/leds/kbd_backlight/brightness'])
with open("/sys/class/leds/kbd_backlight/brightness", "w") as f:
f.write(str(value))
return value
def inc(value: int) -> int:
if (new := getCurrent() + value) <= getMax():
return setBright(new)
else:
return setBright(getMax())
def dec(value: int) -> int:
if (new := getCurrent() - value) >= 0:
return setBright(new)
else:
return 0
def cli():
parser = argparse.ArgumentParser(
description='Increase, decrease, or set the keyboard brightness level in Linux + GNOME\nThis functionality is missing from GNOME Desktop, so I made it myself.',
epilog='Keyboard Brightness Controller is subject to copyright under the GNU General Public License (v3). See LICENSE for details.'
)
parser.add_argument(
'mode',
choices=['inc', 'dec', 'set'],
help='Selects the mode of operation: inc to increase by the specified amount; dec to decrease by the specified amount; set to snap to the specified amount.'
)
parser.add_argument(
'value',
type=int,
help='percentage to increase/decrease by or to set to (0 to 100)',
)
args = parser.parse_args()
if args.mode == 'set':
print(setBright(args.value))
elif args.mode == 'inc':
print(inc(args.value))
elif args.mode == 'dec':
print(dec(args.value))
if __name__ == '__main__':
cli()