aboutsummaryrefslogtreecommitdiffstats
path: root/xpybar/config/mybrilliance.py
blob: f7647e7d61b9b138b2ab3f1c5d8ec9617c96f9f5 (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
# -*- python -*-
import os
import sys
from common import *

class MyBrilliance(Entry):
    def __init__(self, *args, **kwargs):
        self.show_label = True
        self.brilliance = self.get_brilliance()
        self.counter    = 0
        self.path       = os.getenv('XDG_RUNTIME_DIR', '/tmp') + '/.xpybar.brilliance'
        Entry.__init__(self, *args, **kwargs)
    
    def get_brilliance(self):
        try:
            with open(self.path, 'rb') as file:
                return float(file.read().decode('utf-8', 'strict'))
        except:
            return 1
    
    def set_brilliance(self):
        pid = os.fork()
        if not pid:
            if self.brilliance == 1:
                os.execlp('cg-brilliance', 'cg-brilliance', '-x')
            else:
                os.execlp('cg-brilliance', 'cg-brilliance', '--', '%f' % self.brilliance)
            sys.exit(1)
        os.waitpid(pid, 0)
        with open(self.path, 'wb') as file:
            return file.write(('%f' % self.brilliance).encode('utf-8'))
    
    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/adjbrilliance' % 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.brilliance = self.get_brilliance()
            self.invalidate()
        elif button == SCROLL_UP:
            self.brilliance = min(self.brilliance + 0.05, 1)
            self.set_brilliance()
            self.invalidate()
        elif button == SCROLL_DOWN:
            self.brilliance = max(self.brilliance - 0.05, 0)
            self.set_brilliance()
            self.invalidate()
    
    def function(self):
        self.counter += 1
        if self.counter == 6:
            self.counter = 0
            self.brilliance = self.get_brilliance()
        val = int(self.brilliance * 100 + 0.5)
        if val >= 100:
            val = str(val)
        else:
            val = '%2i%%' % val
        if self.show_label:
            val = 'Brill: ' + val
        return val