From 3e21f6d13c0a70db95fec8b5a71b758223ff4293 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 26 Jun 2021 13:18:37 +0200 Subject: Add xpybar MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- xpybar/config/mybacklight.py | 76 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 xpybar/config/mybacklight.py (limited to 'xpybar/config/mybacklight.py') diff --git a/xpybar/config/mybacklight.py b/xpybar/config/mybacklight.py new file mode 100644 index 0000000..bcb3d53 --- /dev/null +++ b/xpybar/config/mybacklight.py @@ -0,0 +1,76 @@ +# -*- 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 -- cgit v1.2.3-70-g09d2