diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2024-03-03 01:06:31 -0800 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2024-03-03 01:06:31 -0800 |
commit | 71033afece74a138716e7013d1af06976d76c0f2 (patch) | |
tree | 3cdf454c70c9a4fd176bf5ffaac5d8fd50cd89ca /panel.py | |
parent | 38bcd7a436a823ce41bf2fc438907274043b2ce3 (diff) | |
download | panel-71033afece74a138716e7013d1af06976d76c0f2.tar.gz panel-71033afece74a138716e7013d1af06976d76c0f2.tar.xz |
add battery module (udev)
Diffstat (limited to 'panel.py')
-rwxr-xr-x | panel.py | 64 |
1 files changed, 63 insertions, 1 deletions
@@ -336,6 +336,68 @@ class ModInputAvail(Mod): self.refreshing = False self.repaint() +class ModBattery(Mod): + def __init__(self, battery, adapter=None, **kwargs): + super().__init__(**kwargs) + self.battery = battery + self.adapter = adapter + + self._percent = None + self._charging = None + + def work(self): + # FIXME copied from ModInputAvail. should abstract udev code. + p = subprocess.Popen( + ('udevadm', 'monitor', '-ups', 'power_supply'), + stdout=subprocess.PIPE, text=True) + + with open(f'/sys/class/power_supply/{self.battery}/capacity') as f: + percent = f.read().rstrip('\n') + charging = None + if self.adapter: + with open(f'/sys/class/power_supply/{self.adapter}/online') as f: + charging = (f.read().rstrip('\n') == '1') + self._update_state(percent, charging) + + building = False + for line in iter(p.stdout.readline, ''): + line = line.rstrip() + + if not building and line.startswith('UDEV'): + building = True + e = {} + elif building: + if '=' in line: + k, v = line.split('=', 1) + e[k] = v + else: + building = False + if e: + self._process_event(e) + + def _process_event(self, e): + if (e.get('ACTION') == 'change' and + (name := e.get('POWER_SUPPLY_NAME'))): + if (name == self.battery and + (cap := e.get('POWER_SUPPLY_CAPACITY'))): + self._update_state(percent=cap) + elif (name == self.adapter and + (online := e.get('POWER_SUPPLY_ONLINE'))): + self._update_state(charging=(online == '1')) + + def _update_state(self, percent=None, charging=None): + self.refreshing = True + if percent is not None: + self._percent = percent + if charging is not None: + self._charging = charging + buf = f'{self._percent}%%' + if self._charging: + buf += ' (charging)' + self.out = Fmt.labelled('BAT', buf) + self.refreshing = False + self.repaint() + class IntervalTimer(): def __init__(self): self.t = None @@ -427,7 +489,7 @@ class Panel: mod.run() self.panel = subprocess.Popen( - ('/home/david/lemonbar-xft/lemonbar', + ('/home/david/dev/lemonbar-xft/lemonbar', '-bf', 'Monospace:size=10:dpi=96', '-f', 'Monospace:size=10:dpi=96:bold', '-g', f'{self.width}x{self.height}', |