Sunday 6 October 2024

Screen Brightness

OpenSUSE Leap 15.5 on ThinkPad X250

I used the X250 with older versions of OpenSUSE without major problems at work in about 2016–2019 (well, I never made the headset microphone work, but that’s another story).

When the Covid pandemic started, I got a new laptop at work and let my daughter use the old one. In 2024, she needed a more modern one, so the old X250 returned to me—and it was just in time, as my old Acer TravelMate had just died.

When installing missing software on the X250 and updating its configuration to my twenties’ likings, I discovered the brightness keys didn’t work.

I decided to fix them manually: It should be easy enough to write a script that changes the brightness and bind it to a XF86MonBrightnessDown and XF86MonBrightnessUp, right? The internet advised to use the xbacklight command, but surprisingly, it’s output was unhelpful:

No outputs have backlight property

After some more googling, I discovered /sys/class/backlight/intel_backlight/ which contained two interesting files: max_brightness and brightness. When I changed the value in the latter, it directly affected the monitor brightness.

So I wrote the following script:

#!/usr/bin/perl
use warnings;
use strict;

my $PATH = '/sys/class/backlight/intel_backlight/';

sub load {
    my ($file) = @_;
    open my $in, '<', $PATH . $file or die "$file: $!";
    my $value = <$in>;
    chomp $value;
    return $value
}

my $change = shift;
my $current = load('brightness');
my $max = load('max_brightness');
$current += $change;
if ($current < 0) {
    $current = 0;
} else {
    $current = $max if $current > $max;
}
open my $out, '>', $PATH . 'brightness';
print {$out} $current;
close $out;
exec 'notify-send', '-t', 500, 'Brightness',
sprintf '%d%%', 100 * $current / $max

notify-send comes from the libnotify-tools package, but it also needs some notify daemon to be running. I used mate-notification-daemon.

The next problem was the brightness file was only writable by root. When I ran chmod a+w on it, it only lasted up to a shutdown. I needed something to change the file permissions on every start, so I created a systemd service:

[Unit]
Description=Backlight service
After=graphical.target

[Service]
Type=simple
User=root
ExecStart=/root/bin/backlight-writable

[Install]
WantedBy=graphical.target

And the script contained a simple chmod.

Now I can change the brightness on my X250 by simply pressing a button again.

No comments:

Post a Comment