I made a small app to change backlight

Write these as root first:
Code:
chmod 666 /dev/backlight/*
echo 'devfs_system_ruleset="localrules"' >> /etc/rc.conf
service devfs restart
pkg install python3 gtk3

Then this is the python program I made. Just run it with python3 main.py

Code:
import gi

import subprocess


gi.require_version('Gtk', '3.0')

from gi.repository import Gtk


class BacklightAdjuster(Gtk.Window):

    def __init__(self):

        super().__init__(title="Backlight Controller")

        self.set_border_width(20)

        self.set_default_size(300, 100)

       

        self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)

        self.add(self.box)

       

        self.label = Gtk.Label(label="Adjust Brightness:")

        self.box.pack_start(self.label, False, False, 5)

       

        self.scale = Gtk.Scale.new_with_range(Gtk.Orientation.HORIZONTAL, 1, 100, 1)

        self.scale.set_value(self.get_current_brightness())

        self.scale.set_hexpand(True)

        self.scale.connect("value-changed", self.on_value_changed)

        self.box.pack_start(self.scale, True, True, 5)

       

    def get_current_brightness(self):

        try:

            result = subprocess.run(["backlight"], capture_output=True, text=True)

            return int(result.stdout.strip())

        except Exception:

            return 50  # Default to 50 if there's an issue

       

    def on_value_changed(self, scale):

        value = int(scale.get_value())

        subprocess.run(["backlight", str(value)])

       

if __name__ == "__main__":

    app = BacklightAdjuster()

    app.connect("destroy", Gtk.main_quit)

    app.show_all()

    Gtk.main()
 
chmod 666 /dev/backlight/*
This chmod won't persist over system reboots. devfs.conf(5) would be better suited.

But why chmod at all? A user belonging to the "video" group has read-write access to backlight0:
Rich (BB code):
 %  ls -l /dev/backlight/*
total 0
lrwxr-xr-x  1 root wheel    23 Mar  2 15:06 amdgpu_bl00 -> ../backlight/backlight0
crw-rw----  1 root video 0x19f Mar  2 15:06 backlight0

Alternatively backlight(8) could be bind to a key combination. Before I discovered acpi_ibm(4), which activated brightness function keys (hotkeys), I had it bind to "Windows" + 4 and 5 keys (4, 5 are beneath brightness fn keys), depending on the DE/WM, like
Code:
W+4   backlight  decr - 5
W+5   backlight  incr + 5
 
Well, technically it looks o.k., but as I stated before, it's not necessary to change owner and permissions for backlight0.

Simply add the user to the "video" group. This group has read-write permission on backlight0 (see post # 2).

To add user to the "video" group:

Option 1 (as root user):
Code:
 # pw groupmod video -m <user_name>

Option 2 (as root user): vigr(8).

To take effect of the new group, exit logged in user, log in again.
 
Very nice idea. FWIW, here's a single-line shell script equivalent:

sh:
zenity \
--scale \
--text="Adjust screen brightness" \
--min-value=1 \
--max-value=100 \
--value="$(backlight -q)" \
--hide-value \
--print-partial \
|while read VALUE; do \
    backlight "${VALUE}"; \
done

Edit: I split it in multiple lines for clarity.

Note: you might need to "pkg install zenity".

Edit 2: final version, that respects zenity's Cancel button (+ some eye candiness):
sh:
#!/bin/sh
(
ORIGINAL="$(backlight -q)"
zenity --scale \
    --modal \
    --title="Screen Brightness" \
    --text="  🌑 ⋯⋯⋯ 🌒 ⋯⋯⋯ 🌓 ⋯⋯⋯ 🌔 ⋯⋯⋯ 🌕" \
    --min-value=1 \
    --max-value=100 \
    --value="${ORIGINAL}" \
    --hide-value \
    --print-partial || backlight "${ORIGINAL}"
) | while read VALUE; do
    backlight "${VALUE}"
done
Enjoy!
 
Back
Top