from subprocess import run, PIPE from typing import Optional from pynput import mouse from . import qtile_func class Backlight: def __init__(self, ctrl: Optional[str]=None) -> None: self.command = ['xbacklight'] if ctrl: self.command += ['-ctrl', ctrl] def _get_brightness(self) -> int: return int(run(self.command + ['-get'], stdout=PIPE).stdout) def _set_brightness(self, percentage: int) -> None: run(self.command + [f'-set', str(percentage)]) @qtile_func def change_backlight(self, action: str) -> None: """ Increase or decrease bightness. Takes 'dec' or 'inc' as parameter. Goes 1% percent at a time from 1% to 40% and 10% at a time from 40% to 100%. """ brightness = self._get_brightness() if brightness != 1 or action != 'dec': if (brightness > 49 and action == 'dec') \ or (brightness > 39 and action == 'inc'): run(self.command + [f'-{action}', '10', '-fps', '10']) else: run(self.command + [f'-{action}', '1']) @qtile_func def turn_off_screen(self, screen_offset: int) -> None: """ Turn off the laptop's screen. Use xset if there is only one screen, else, set backlight to zero. In both case, moving the mouse will turn on the screen. """ current_brightness = self._get_brightness() self._set_brightness(0) def on_move(x: int, y: int): if x > screen_offset: self._set_brightness(current_brightness) raise mouse.Listener.StopException mouse.Listener(on_move=on_move).start()