aboutsummaryrefslogtreecommitdiffstats
path: root/xpybar/config/adjbrilliance
blob: 0ffb09de3c31ece4e94ac3fe5a446460f2d37242 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- python -*-
import os
import sys
import time
import signal

OUTPUT, HEIGHT, YPOS, TOP = 0, 2 * 12, 0, True

LEFT_BUTTON = 1
MIDDLE_BUTTON = 2
RIGHT_BUTTON = 3
SCROLL_UP = 4
SCROLL_DOWN = 5
SCROLL_LEFT = 6
SCROLL_RIGHT = 7
FORWARD_BUTTON = 8                                                                                                               
BACKWARD_BUTTON = 9

path = os.getenv('XDG_RUNTIME_DIR', '/tmp') + '/.xpybar.brilliance'
pid = None
pending = False

def sigchld(*_):
    global pid, pending
    if pid is not None:
        r, _ = os.waitpid(-1, 0)
        if r == pid:
            pid = None
    if pending:
        pending = False
        set_brilliance()
    else:
        with open(path, 'wb') as file:
            return file.write(('%f' % brilliance).encode('utf-8'))

def get_brilliance():
    try:
        with open(path, 'rb') as file:
            return float(file.read().decode('utf-8', 'strict'))
    except:
        return 1

def set_brilliance():
    global pid, pending
    if pid is None:
        pid = os.fork()
        if not pid:
            if brilliance == 1:
                os.execlp('cg-brilliance', 'cg-brilliance', '-x')
            else:
                os.execlp('cg-brilliance', 'cg-brilliance', '--', '%f' % brilliance)
            sys.exit(1)
    else:
        pending = True

signal.signal(signal.SIGCHLD, sigchld)
brilliance = get_brilliance()

def redraw():
    x = int(bar.width * brilliance + 0.5)
    bar.change_colour(bar.foreground)
    bar.window.fill_rectangle(bar.gc, 0, 0, x, HEIGHT)
    bar.change_colour(bar.background)
    bar.window.fill_rectangle(bar.gc, x, 0, bar.width - x, HEIGHT)

import x as _x
_get_event_mask = _x.get_event_mask
def get_event_mask():
    return _get_event_mask() | Xlib.X.ButtonMotionMask
_x.get_event_mask = get_event_mask

def unhandled_event(e):
    global brilliance
    if isinstance(e, Xlib.protocol.event.ButtonPress):
        button, x = e.detail, e.event_x
        if button == LEFT_BUTTON:
            brilliance = min(max(0, x / bar.width), 1)
            set_brilliance()
            bar.invalidate()
        elif button == SCROLL_UP:
            brilliance = min(brilliance + 0.05, 1)
            set_brilliance()
            bar.invalidate()
        elif button == SCROLL_DOWN:
            brilliance = max(brilliance - 0.05, 0)
            set_brilliance()
            bar.invalidate()
        elif button in (MIDDLE_BUTTON, RIGHT_BUTTON, FORWARD_BUTTON, BACKWARD_BUTTON):
            raise KeyboardInterrupt()
    elif isinstance(e, Xlib.protocol.event.MotionNotify):
        if e.detail == 0:
            brilliance = min(max(0, e.event_x / bar.width), 1)
            set_brilliance()
            bar.invalidate()