aboutsummaryrefslogblamecommitdiffstats
path: root/xpybar/config/mybrilliance.py
blob: f7647e7d61b9b138b2ab3f1c5d8ec9617c96f9f5 (plain) (tree)





































































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