Skip to content

Commit

Permalink
[modules/brightness] re-enable reading brightness from ACPI
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tobi-wan-kenobi committed Jun 28, 2020
1 parent 81c5e75 commit 8f3d48c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
32 changes: 30 additions & 2 deletions bumblebee_status/modules/contrib/brightness.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!
"""
Expand All @@ -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"):
Expand All @@ -42,13 +48,31 @@ 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)

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()

Expand All @@ -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"

Expand Down
2 changes: 2 additions & 0 deletions docs/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand Down

0 comments on commit 8f3d48c

Please sign in to comment.