You are not logged in.

#1 2024-10-28 14:16:47

mhd12
Member
Registered: 2024-10-24
Posts: 2

How to control brightness in dwm on a laptop ?

### This is a script I made for anyone to use. I hope it helps:

### Create a file called `brightness.py` in your `~` (Home) directory:

```bash
touch ~/brightness.py
```

### Put this code in it:

```python
import argparse
import subprocess


def get_brightness():
    result = subprocess.run(
        ["./brightness_l.sh"],
        capture_output=True,
        text=True
    )
    return float(result.stdout.strip())

def i_b():
    global brits
    if brits >= 1:
        print("MAX brightness")
        brits = 1
        subprocess.run(["xrandr", "--output", "eDP", "--brightness", str(brits)])
    else:
        print("brightness up")
        brits = brits + 0.05
        subprocess.run(["xrandr", "--output", "eDP", "--brightness", str(brits)])


def d_b():
    global brits
    if brits <= 0.05:
        print("You can't lower the brightness more than 5%")
        brits = 0.05
        subprocess.run(["xrandr", "--output", "eDP", "--brightness", str(brits)])
    else:
        print("brightness down")
        brits = brits - 0.05
        subprocess.run(["xrandr", "--output", "eDP", "--brightness", str(brits)])


parser = argparse.ArgumentParser(description="Control brightness")
parser.add_argument("action", choices=["up", "down"], help="Choose 'up' to increase or 'down' to decrease brightness")
args = parser.parse_args()

brits = get_brightness()

if args.action == "up":
    i_b()
elif args.action == "down":
    d_b()
```

### Now make another file in your `~` (Home) directory called `brightness_l.sh` (This helps me get the current brightness level):

### Put these lines in it:

```bash
#!/bin/bash

xrandr --verbose | grep -i brightness | awk '{print $2}'
```

### Now make it executable with this command:

```bash
chmod +x brightness_l.sh
```

### Try the Python script using:

```bash
python3 brightness.py [up or down]
```

Example:

```bash
python3 brightness.py down
```

If this works, you have done the last steps correctly.

### Now you will edit the `dwm` source code to bind the command to a key on your keyboard:

1. `cd` into the place you store the `dwm` source code in.
2. Use a text editor with `sudo` privileges to edit the `config.h` file (btw I use `vim`).
3. Add this line in the first line:

   ```c
   #include <X11/XF86keysym.h>
   ```

4. Add these 2 variables in your code:

   ```c
   static const char *brightness_up[]   = { "python3", "brightness.py", "up", NULL };
   static const char *brightness_down[] = { "python3", "brightness.py", "down", NULL };
   ```

5. Go to this line:

   ```c
   static const Key keys[] = {
   ```

   And under that, you will find a lot of key binds.

6. At the end of this list, add these 2 lines:

   ```c
   { 0, XF86XK_MonBrightnessUp,   spawn, {.v = brightness_up } },
   { 0, XF86XK_MonBrightnessDown, spawn, {.v = brightness_down } },
   ```

7. Finally, save the file and close the text editor, then compile the source code using:

   ```bash
   sudo make clean install
   ```

8. On your keyboard, do this shortcut to exit `dwm`: `alt` + `left shift` + `q`.
9. Then type `startx` and you should be good to go.
10. Try pressing `fn` + `your brightness keys`, and if it works, just thank me!

Offline

#2 2024-10-28 14:18:42

mhd12
Member
Registered: 2024-10-24
Posts: 2

Re: How to control brightness in dwm on a laptop ?

sorry I did not know that arch forms dosen`t support md !
https://www.reddit.com/r/suckless/comme … _a_laptop/
this is the post

Offline

#3 2024-10-28 17:51:52

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 23,331

Re: How to control brightness in dwm on a laptop ?

You can use [ code ] [ /code ] tags without spaces instead...

Note the forum is not the place for how to's as their content will get lost quickly, but the script is also not generic enough to be overly useful to the majority of people. The xrandr brightness extension basically only exists when using xf86-video-intel which is very much not reccommended and far more resilient and correctly working CLI tools are already proposed in: https://wiki.archlinux.org/title/Backli … _utilities -- you'd be much more future proof using any of these instead of custom rolling something relying on a non-widespread xrandr extension.

Offline

Board footer

Powered by FluxBB