aboutsummaryrefslogtreecommitdiffstats
path: root/xpybar/config/mybacklight.py
blob: bcb3d5342229cbe227d7a168e86a8fd19f061b65 (plain) (blame)
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
# -*- python -*-
import os
import sys
from common import *

class MyBacklight(Entry):
    def __init__(self, *args, **kwargs):
        self.show_label = True
        self.backlight = self.get_backlight()
        self.counter = 0
        Entry.__init__(self, *args, **kwargs)
    
    def get_backlight(self):
        r, w = os.pipe()
        pid = os.fork()
        if not pid:
            os.dup2(w, 1)
            os.execlp('adjbacklight', 'adjbacklight', '-g')
            sys.exit(1)
        os.close(w)
        ret = b''
        while True:
            bs = os.read(r, 512)
            if not bs:
                break
            ret += bs
        os.close(r)
        os.waitpid(pid, 0)
        ret = float(ret.decode('utf-8', 'replace').rstrip('%\n')) / 100
        return ret
    
    def set_backlight(self):
        pid = os.fork()
        if not pid:
            os.execlp('adjbacklight', 'adjbacklight', '-s', '%f%%' % (self.backlight * 100))
            sys.exit(1)
        os.waitpid(pid, 0)
    
    def action(self, col, button, x, y):
        if button == LEFT_BUTTON:
            pid = os.fork()
            if not pid:
                pid = os.fork()
                if not pid:
                    os.execlp('xpybar', 'xpybar', '-c', '%s/.config/xpybar/adjbacklight' % HOME)
                    sys.exit(1)
                sys.exit(0)
            os.waitpid(pid, 0)
        elif button == MIDDLE_BUTTON:
            self.show_label = not self.show_label
            self.invalidate()
        elif button == RIGHT_BUTTON:
            self.backlight = self.get_backlight()
            self.invalidate()
        elif button == SCROLL_UP:
            self.backlight = min(self.backlight + 0.05, 1)
            self.set_backlight()
            self.invalidate()
        elif button == SCROLL_DOWN:
            self.backlight = max(self.backlight - 0.05, 0)
            self.set_backlight()
            self.invalidate()
    
    def function(self):
        self.counter += 1
        if self.counter == 6:
            self.counter = 0
            self.backlight = self.get_backlight()
        val = int(self.backlight * 100 + 0.5)
        if val >= 100:
            val = str(val)
        else:
            val = '%2i%%' % val
        if self.show_label:
            val = 'Blight: ' + val
        return val