Skip to content

Instantly share code, notes, and snippets.

@nicodebo

nicodebo/config Secret

Last active December 8, 2023 18:31
Show Gist options
  • Save nicodebo/297c1e134256ea24abf02a485ce41420 to your computer and use it in GitHub Desktop.
Save nicodebo/297c1e134256ea24abf02a485ce41420 to your computer and use it in GitHub Desktop.
waybar, display brightness value of external screen using ddcutil
"custom/ddcutil": {
"format": "{percentage}% {icon}",
"interval": 1,
"format-icons": ["", "", ""],
"exec": "/path/to/waybar-ddcutil.py",
"return-type": "json"
},
#custom-ddcutil {
background-color: #f1c40f;
color: #000000;
}
#!/usr/bin/env python
import subprocess
import json
data = {}
cmd = ["ddcutil", "getvcp", "10", "--bus", "1"]
value = subprocess.run(cmd, stdout=subprocess.PIPE).stdout.decode('utf8')
# output looks like this in my case: VCP code 0x10 (Brightness ): current value = 20, max value = 100
percentage = value.split(":")[1].split(",")[0].split("=")[1].strip(" ")
data['percentage'] = int(percentage)
print(json.dumps(data))
@nicodebo
Copy link
Author

20210226_21h34m58s_grim

@jones-sam
Copy link

Here is my implementation of the script, I am caching the value because many times when changing the brightness ddcutil wouldn't be able to get the value so it would break for a second and look really janky.

#!/bin/bash

output=$(ddcutil -t getvcp 10 2>/dev/null)

if [ $? -eq 0 ]; then
    trimmed=${output#* * * }
    value=${trimmed%% *}

    cached_value=$(cat /tmp/waybar_ddcutil_value 2>/dev/null)
    if [ "$cached_value" != "$value" ]; then
        echo $value > /tmp/waybar_ddcutil_value
    fi
    echo {\"percentage\": $value}
else
    echo {\"percentage\": $(cat /tmp/waybar_ddcutil_value 2>/dev/null || echo 0)}
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment