aboutsummaryrefslogblamecommitdiffstats
path: root/xpybar/config/adjbrilliance
blob: 0ffb09de3c31ece4e94ac3fe5a446460f2d37242 (plain) (tree)





























































































                                                                                                                                 
# -*- 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()