Backlight: Difference between revisions

From NixOS Wiki
imported>Samueldr
m WIP, cleaning and re-working sections
imported>Samueldr
m Finalizes cleaning and re-working.
Line 1: Line 1:
{{expansion}}
{{expansion}}
== Introduction ==
 
This page documents methods for controlling backlight (aka screen brightness) and tips to control it via hotkeys.
This page documents methods for controlling backlight (aka screen brightness) and tips to control it via hotkeys.
== Kernel native ==
Some laptops, using a recent enough kernel, will automatically handle increasing and decreasing the backlight using the hot keys. The following tools will allow scripting or controlling the backlight using other means, if desired.
== Desktop Environment native ==
Some desktop environments will handle querying and setting the backlight, including configuring the backlight keys. These include at least: Plasma (KDE) and XFCE. It may be needed to configure or start some desktop environment-specific services.


== <code>xbacklight</code> ==
== <code>xbacklight</code> ==
Line 34: Line 42:
=== Key mapping ===
=== Key mapping ===


Once the backend functionality of controlling the backlight is verified, you can assign that to an actual key. Different laptops and keyboards may map the hotkeys to different key codes.
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]].


== Key mapping - obtain key codes via xev ==
Depending on the tools that work for controlling the backlight you will be able to choose one of those options.
While logged into an X session:<br />
<code>nix-shell -p xorg.xev</code><br />
<code>xev</code><br />
Xev reports key codes, but they did not seem to be the right codes for use with actkbd.


== Key mapping - obtain key codes via actkbd ==
{| class="wikitable" border="1"
First enable the actkbd service (see below).<br />
|-
To read key codes via actkbd, you need to select the right input event provider from /dev/input/. Each input device generating input events has it's own entry there, enumerated with an number. To find out which one generates the events from the hotkeys, it is viable (if tedious) to run the below command with each available entry, and see which one generates event notifications when pressing the brightness hotkeys:<br />
!  
<code>actkbd -v9 -n -s -d /dev/input/event<number></code><br />
| <code>light</code>
Once you discover the right one, it will report key presses like so:<br />
| <code>xbacklight</code>
<code>Event: <number>:key</code><br />
|-
<code>Event: <number>:rel</code><br />
! System service
Those represent the key-press and key-release actions. The number is the key code to use for mapping.
| Yes
| With hacks
|-
! X session
| Yes
| Yes
|}


== Key mapping - map via actkbd service ==
Follows, an example mapping for use with actkbd:
To map keys using actkbd service and the <code>light</code> program, add the following to your configuration.nix file:<br />
<syntaxhighlight lang="nix">services.actkbd = {
  enable = true;
  bindings = [
    {keys = [ <brightnessDown> ]; events = [ "key" ]; command = "run/wrappers/bin/light -U 10";}
    {keys = [ <brightnessUp> ]; events = [ "key" ]; command = "run/wrappers/bin/light -A 10";}
  ];
};</syntaxhighlight>
Where <brightnessUp> and <brightnessDown> are the key codes obtained before.


== Working configuration on Lenovo T440 ==
{{note|This was verified to work NixOS 18.03 on a Lenovo T440; it may work on other models, but is unconfirmed. See [[actkbd]] for details on finding out the proper key bindings.}}
To enable backlight control on a Lenovo T440, add to configuration.nix:<br />


<syntaxhighlight lang="nix">programs.light.enable = true;
<syntaxhighlight lang="nix">
services.actkbd = {
  programs.light.enable = true;
  enable = true;
  services.actkbd = {
  bindings = [
    enable = true;
  { keys = [ 224 ]; events = [ "key" ]; command = "/run/wrappers/bin/light -A 10"; }
    bindings = [
  { keys = [ 225 ]; events = [ "key" ]; command = "/run/wrappers/bin/light -U 10"; }
      { keys = [ 224 ]; events = [ "key" ]; command = "/run/wrappers/bin/light -A 10"; }
  ];
      { keys = [ 225 ]; events = [ "key" ]; command = "/run/wrappers/bin/light -U 10"; }
};</syntaxhighlight>
    ];
Tested with NixOS 18.03, and in combination with lightdm & i3.
  };
</syntaxhighlight>


== See also ==
== See also ==


* [https://wiki.archlinux.org/index.php/backlight Arch Linux wiki page about the backlight]
* [https://wiki.archlinux.org/index.php/backlight Arch Linux wiki page about the backlight]

Revision as of 17:48, 6 May 2018

This page documents methods for controlling backlight (aka screen brightness) and tips to control it via hotkeys.

Kernel native

Some laptops, using a recent enough kernel, will automatically handle increasing and decreasing the backlight using the hot keys. The following tools will allow scripting or controlling the backlight using other means, if desired.

Desktop Environment native

Some desktop environments will handle querying and setting the backlight, including configuring the backlight keys. These include at least: Plasma (KDE) and XFCE. It may be needed to configure or start some desktop environment-specific services.

xbacklight

xbacklight uses X to change the light settings. This can be inconvenient in some situations, e.g. for use with the services.actkbd service, which doesn't know about the X session. It, though, has an history of being more compatible with different hardware, especially newer hardware[citation needed]

To install xbacklight globally, add this to your configuration.nix.

  environment.systemPackages = with pkgs; [ xorg.xbacklight ];

Alternatively, use nix-env -iA nixos.xorg.xbacklight to install it to your user profile.

light

light does not use X to change the light settings. This can be used in situations where the X service isn't available. While it does not use X, it will need some privileges to work. This means that it needs to either be installed in a specific way (with a SUID wrapper) or used using sudo, or ran with superuser privileges.

To enable the use of light with SUID wrappers, add this to your configuration.nix.

  programs.light.enable = true;

The following commands will allow you to test light:

  • light -U 30 — the screen should become darker.
  • light -A 30 — the screen should become brighter.

Be careful using light -U, as you might turn your backlight completely off! You will not be able to see what you're typing anymore.

Tips

Key mapping

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.

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

  light xbacklight
System service Yes With hacks
X session Yes Yes

Follows, an example mapping for use with actkbd:

Note: This was verified to work NixOS 18.03 on a Lenovo T440; it may work on other models, but is unconfirmed. See actkbd for details on finding out the proper key bindings.
  programs.light.enable = true;
  services.actkbd = {
    enable = true;
    bindings = [
      { keys = [ 224 ]; events = [ "key" ]; command = "/run/wrappers/bin/light -A 10"; }
      { keys = [ 225 ]; events = [ "key" ]; command = "/run/wrappers/bin/light -U 10"; }
    ];
  };

See also