diff --git a/bumblebee_status/modules/contrib/brightness.py b/bumblebee_status/modules/contrib/brightness.py index eb68a4fc..28b94a82 100644 --- a/bumblebee_status/modules/contrib/brightness.py +++ b/bumblebee_status/modules/contrib/brightness.py @@ -4,6 +4,8 @@ Parameters: * brightness.step: The amount of increase/decrease on scroll in % (defaults to 2) + * brightness.device_path: The device path (defaults to /sys/class/backlight/intel_backlight), can contain wildcards (in this case, the first matching path will be used); This is only used when brightness.use_acpi is set to true + * brightness.use_acpi: If set to true, read brightness directly from the sys ACPI interface, using the device specified in brightness.device_path (defaults to false) contributed by `TheEdgeOfRage `_ - many thanks! """ @@ -27,8 +29,12 @@ def __init__(self, config, theme): self.__brightness = "n/a" self.__readcmd = None step = self.parameter("step", 2) + self.__device_path = self.find_device(self.parameter("device_path", "/sys/class/backlight/intel_backlight")) - if shutil.which("light"): + if util.format.asbool(self.parameter("use_acpi", False)): + self.__readcmd = self.__acpi + # TODO: add setting + elif shutil.which("light"): self.__readcmd = self.__light self.register_cmd("light -A {}%".format(step), "light -U {}%".format(step)) elif shutil.which("brightnessctl"): @@ -42,6 +48,12 @@ def __init__(self, config, theme): "xbacklight +{}%".format(step), "xbacklight -{}%".format(step) ) + def find_device(self, device_path): + res = glob.glob(device_path) + if len(res) == 0: + return device_path + return res[0] + def register_cmd(self, up_cmd, down_cmd): core.input.register(self, button=core.input.WHEEL_UP, cmd=up_cmd) core.input.register(self, button=core.input.WHEEL_DOWN, cmd=down_cmd) @@ -49,6 +61,18 @@ def register_cmd(self, up_cmd, down_cmd): def brightness(self, widget): return self.__brightness + def __acpi(self): + try: + backlight = 1 + max_brightness = 1 + with open("{}/brightness".format(self.__device_path)) as f: + backlight = int(f.readline()) + with open("{}/max_brightness".format(self.__device_path)) as f: + max_brightness = int(f.readline()) + return float(backlight*100)/max_brightness + except: + return "unable to read brightness from {}".format(self.__device_path) + def __light(self): return util.cli.execute("light").strip() @@ -62,7 +86,11 @@ def __xbacklight(self): def update(self): try: - self.__brightness = "{:3.0f}%".format(float(self.__readcmd())) + tmp = self.__readcmd() + if isinstance(tmp, str): + self.__brightness = tmp + else: + self.__brightness = "{:3.0f}%".format(float(tmp)) except: self.__brightness = "n/a" diff --git a/docs/modules.rst b/docs/modules.rst index 7e28095f..984abb95 100644 --- a/docs/modules.rst +++ b/docs/modules.rst @@ -404,6 +404,8 @@ Displays the brightness of a display Parameters: * brightness.step: The amount of increase/decrease on scroll in % (defaults to 2) + * brightness.device_path: The device path (defaults to /sys/class/backlight/intel_backlight), can contain wildcards (in this case, the first matching path will be used); This is only used when brightness.use_acpi is set to true + * brightness.use_acpi: If set to true, read brightness directly from the sys ACPI interface, using the device specified in brightness.device_path (defaults to false) contributed by `TheEdgeOfRage `_ - many thanks!