Search code examples
linuxbashinotifybacklight

Why doesn't inotifywait notice the change in /sys/class/backlight/intel_backlight/brightness (Linux)


My laptop has a 4K OLED screen, so it has no backlight to change the brightness of the screen. To control the brightness, you need to use xrandr with the --brightness parameter. I have successfully done this in the i3 window manager before but I would now like to to something similar in Budgie.

Budgie will detect the brightness keystrokes and will change the /sys/class/backlight/intel_backlight/brightness file contents to a value that would normally update the backlight setting.

I would like to monitor this file for changes and when it changes, just call xrandr with the correct value... I found out that you can watch file changes with inotifywait but for some reason, it doesn't work for me.

$ cat /sys/class/backlight/intel_backlight/brightness
21750
# Now pressing the brightness up key on the keyboard
$ cat /sys/class/backlight/intel_backlight/brightness
28000
$ inotifywait /sys/class/backlight/intel_backlight/brightness
Setting up watches.
Watches established.
# Now pressing the brightness up key on the keyboard....... nothing happens.
# Same when I add the -e close_write parameter.

Anyone knows why this happens?

I could of course just check the file's contents every second but I don't like that kind of solution.

FYI: Arch Linux, up to date

Edit: I just noticed that it DOES notice changes in /sys/class/backlight/acpi_video0/brightness which solves my problem, but the question is still valid.


Solution

  • Inotify works at the VFS level (filesystem level, so to speak). If the file is written into, inotify will be notified of it. However, since sysfs (and procfs, and all other virtual filesystems) have contents that are possibly updated dynamically & asynchronously not via the filesystem, using inotify may not yield the expected results.

    In your case - the value of /sys/class/backlight/intel_backlight/brightness may be updated someway else, not via the filesystem, while /sys/class/backlight/acpi_video0/brightness does get updated through the filesystem. That depends on the controlling software. This may be the cause for your observation.

    For the record - I have tested it on my laptop: with inotifywatch /sys/class/backlight/intel_backlight/brightness running in the background, I get notified on:

    • cat /sys/class/backlight/intel_backlight/brightness - close_nowrite.
    • echo 50000 | sudo tee /sys/class/backlight/intel_backlight/brightness - changes the brightness and I get close_write.
    • Changing the brightness via the keyboard shortcut - for each increase/decrease I get 1 close_write event (running GNOME Shell 3.36.4)