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