summaryrefslogtreecommitdiff
path: root/bin/brightness_monitor
blob: a8617e00b98b87c081448a7a240caed0718265bd (plain)
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
#!/usr/bin/python3

import notify2
import os
import subprocess
import sys

if len(sys.argv) < 2:
    sys.stdout.write('Usage: %s /sys/.../<backlight dir>/\n')
    sys.exit(0)

path = sys.argv[1]

with open(os.path.join(path, 'max_brightness'), 'r') as f:
    brightness_max = float(f.read().strip())

notify2.init(os.path.basename(sys.argv[0]))

notification = notify2.Notification('Brightness')

inotify_cmd = ['inotifywait', '--monitor', '--event=close_write',
               os.path.join(path, 'brightness')]

child = subprocess.Popen(inotify_cmd, stdout = subprocess.PIPE, bufsize = 0)

while True:
    # wait for updates
    child.stdout.read(4096)
    if child.poll() is not None:
        notification.message = 'inotify monitor has terminated'
        notification.set_urgency(notify2.URGENCY_CRITICAL)
        notification.show()
        sys.stderr.write(notification.message + '\n')
        break

    with open(os.path.join(path, 'brightness'), 'r') as f:
        brightness = float(f.read().strip())

    notification.message = '%d%%' % (100 * brightness / brightness_max)
    notification.show()