[modules/brightness] re-enable reading brightness from ACPI

to enable reading the brightness from ACPF, set the device path and -
other than previously - explicitly enable this by setting the parameter
"brightness.use_acpi" to "true".

fixes #665
This commit is contained in:
tobi-wan-kenobi 2020-06-28 10:59:43 +02:00
parent 81c5e75624
commit 8f3d48c0e6
2 changed files with 32 additions and 2 deletions

View file

@ -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 <https://github.com/TheEdgeOfRage>`_ - many thanks!
"""
@ -27,8 +29,12 @@ class Module(core.module.Module):
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 @@ class Module(core.module.Module):
"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 @@ class Module(core.module.Module):
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 @@ class Module(core.module.Module):
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"

View file

@ -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 <https://github.com/TheEdgeOfRage>`_ - many thanks!