Backlight: Difference between revisions

imported>Vater
m form (fixing Category:Pages with syntax highlighting errors) and add a first (content) category
Kittyd (talk | contribs)
No edit summary
 
(5 intermediate revisions by 3 users not shown)
Line 41: Line 41:


You can use it by simply installing the package. Since <code>brightnessctl</code> supports the systemd-logind API it should work out of the box (i.e. without installing any udev rules or using a setuid wrapper).
You can use it by simply installing the package. Since <code>brightnessctl</code> supports the systemd-logind API it should work out of the box (i.e. without installing any udev rules or using a setuid wrapper).
The following commands will allow you to test <code>brightnessctl</code>:
* <code> brightnessctl set 5%-</code> - the screen should become darker.
* <code> brightnessctl set 5%+</code> - the screen should become brighter.
If you get an error like <code>Failed to set brightness: Protocol error</code>, check that you are using the right device. You can get a list of the devices with running <code>brightnessctl -l</code> and then specify the device with <code>-d deviceName</code>.
Example: <code>brightnessctl set 5%- -d intel_backlight</code>
== External Monitors  ==
The DDC/CI (I2C) interface may be available to control the brightness of your external monitor. There are two ways to do this:
=== Via <code>ddcutil</code> ===
<syntaxhighlight lang=shell>
$ sudo modprobe i2c-dev
$ nix-shell -p ddcutil --command "sudo ddcutil --bus=MYBUSNUM setvcp 10 MYBRIGHTNESSPCT"
</syntaxhighlight>
You can find the correct bus number by examining the paths in <code>/sys/bus/i2c/devices</code>. e.g. to control the <code>DP4</code> device below use device <code>17</code>.
<syntaxhighlight lang=shell>
$ ls -l /sys/bus/i2c/devices/
lrwxrwxrwx - root  2 Nov 12:34 i2c-13 -> ../../../devices/pci0000:00/0000:00:02.0/drm/card1/card1-eDP-1/i2c-13
lrwxrwxrwx - root  2 Nov 12:34 i2c-14 -> ../../../devices/pci0000:00/0000:00:02.0/drm/card1/card1-DP-1/i2c-14
lrwxrwxrwx - root  2 Nov 12:34 i2c-15 -> ../../../devices/pci0000:00/0000:00:02.0/drm/card1/card1-DP-2/i2c-15
lrwxrwxrwx - root  2 Nov 12:34 i2c-16 -> ../../../devices/pci0000:00/0000:00:02.0/drm/card1/card1-DP-3/i2c-16
lrwxrwxrwx - root  2 Nov 12:34 i2c-17 -> ../../../devices/pci0000:00/0000:00:02.0/drm/card1/card1-DP-4/i2c-17
</syntaxhighlight>
All together: <syntaxhighlight lang=shell>
nix-shell -p ddcutil --command "sudo ddcutil --bus=17 setvcp 10 65"
</syntaxhighlight>
You can then bind this to a hotkey or similar. <code>sudo</code> may be avoidable if your user is in the correct group, or udev rules have set the appropriate <code>/sys</code> permissions.
To make this permanent, add {{nixos:package|ddcutil}} to your system packages, and enable i2c support.<syntaxhighlight lang="nix">
hardware.i2c.enable = true
</syntaxhighlight>
=== Via <code>ddcci-driver</code> ===
A [https://gitlab.com/ddcci-driver-linux/ddcci-driver-linux driver] exists which exposes the ddcci control as a more standard <code>backlight</code> device, allowing it to be controlled by standard utilities such as <code>brightnessctl</code>. e.g. when setup:
<syntaxhighlight lang=shell>
$ find /sys/class/backlight
/sys/class/backlight
/sys/class/backlight/ddcci17
/sys/class/backlight/intel_backlight
$ brightnessctl --device=ddcci17 set 100%
Updated device 'ddcci17':
Device 'ddcci17' of class 'backlight':
Current brightness: 100 (100%)
Max brightness: 100
</syntaxhighlight>
Get the i2c name:
<syntaxhighlight lang=shell>
$ cat /sys/bus/i2c/devices/i2c-17/name                   
AUX USBC4/DDI TC4/PHY TC4
</syntaxhighlight>
<syntaxhighlight lang=nix>
boot.extraModulePackages = with config.boot.kernelPackages; [ ddcci-driver ];
boot.kernelModules = [ "ddcci-backlight" ];
services.udev.extraRules = let
      bash = "${pkgs.bash}/bin/bash";
      ddcciDev = "AUX USBC4/DDI TC4/PHY TC4";                                                                                                     
      ddcciNode = "/sys/bus/i2c/devices/i2c-17/new_device";
    in ''
      SUBSYSTEM=="i2c", ACTION=="add", ATTR{name}=="${ddcciDev}", RUN+="${bash} -c 'sleep 30; printf ddcci\ 0x37 > ${ddcciNode}'"
    '';
</syntaxhighlight>
Device autodetection is tricky with i2c so we need to trigger it manually with the <code>0x37</code> code. The <code>sleep</code> is often unfortunately necessary.
The driver has had some issues with kernels > 6.8, the below overlay should allow it to build if you encounter issues ([https://gitlab.com/ddcci-driver-linux/ddcci-driver-linux/-/merge_requests/17 source]).
<syntaxhighlight lang=nix>
  nixpkgs.overlays = [
      (self: super: {
        linuxPackages_latest = super.linuxPackages_latest.extend (lpself: lpsuper: {
          ddcci-driver = super.linuxPackages_latest.ddcci-driver.overrideAttrs (oldAttrs: {
            version = super.linuxPackages_latest.ddcci-driver.version + "-FIXED";
            src = pkgs.fetchFromGitLab {
              owner = "ddcci-driver-linux";
              repo = "ddcci-driver-linux";
              rev = "0233e1ee5eddb4b8a706464f3097bad5620b65f4";
              hash = "sha256-Osvojt8UE+cenOuMoSY+T+sODTAAKkvY/XmBa5bQX88=";
            };
            patches = [
              (pkgs.fetchpatch {
                name = "ddcci-e0605c9cdff7bf3fe9587434614473ba8b7e5f63.patch";
                url = "https://gitlab.com/nullbytepl/ddcci-driver-linux/-/commit/e0605c9cdff7bf3fe9587434614473ba8b7e5f63.patch";
                hash = "sha256-sTq03HtWQBd7Wy4o1XbdmMjXQE2dG+1jajx4HtwBHjM=";                                                                     
              })
            ];
          });
        });
      })
    ];
</syntaxhighlight>


== <code>/sys/class/backlight/...</code> ==
== <code>/sys/class/backlight/...</code> ==
Line 64: Line 167:
While controlling the backlight via the command line is useful, it would be preferable to control it using key bindings. This is especially true considering most laptops have backlight control keys.
While controlling the backlight via the command line is useful, it would be preferable to control it using key bindings. This is especially true considering most laptops have backlight control keys.


There are two main choices to add key bindings, using a system-level service like [[actkbd]] or using an X session tool, either provided by your [[Desktop environment]], [[Window manager]] or a tool like [[xbindkeys]].
There are two main choices to add key bindings, using a system-level service like [[actkbd]] or using an X session tool, either provided by your [[:Category:Desktop environment|Desktop environment]], [[Window manager]] or a tool like [[xbindkeys]].


Depending on the tools that work for controlling the backlight you will be able to choose one of those options.
Depending on the tools that work for controlling the backlight you will be able to choose one of those options.