96 lines
3.4 KiB
Bash
Executable File
96 lines
3.4 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# ollie:prompt
|
|
# ## gui_brightness
|
|
#
|
|
# Get/set screen brightness.
|
|
#
|
|
# **Args**: `[action]` or `[action, percent]`
|
|
# - Actions: `get`, `set`, `up`, `down`
|
|
#
|
|
# ```
|
|
# call_tool: calls=[{tool: "gui_brightness", args: ["get"]}]
|
|
# call_tool: calls=[{tool: "gui_brightness", args: ["set", "50"]}]
|
|
# ```
|
|
# ollie:end
|
|
set -euo pipefail
|
|
|
|
SERVICE="org.kde.Solid.PowerManagement"
|
|
OBJ_PATH="/org/kde/Solid/PowerManagement/Actions/BrightnessControl"
|
|
IFACE="org.kde.Solid.PowerManagement.Actions.BrightnessControl"
|
|
|
|
cmd="${1:-get}"
|
|
|
|
# Get max brightness (needed for percentage conversion)
|
|
get_max() {
|
|
gdbus call --session --dest "$SERVICE" \
|
|
--object-path "$OBJ_PATH" \
|
|
--method "$IFACE.brightnessMax" | \
|
|
sed 's/(//;s/,)//'
|
|
}
|
|
|
|
# Get current brightness (absolute)
|
|
get_current() {
|
|
gdbus call --session --dest "$SERVICE" \
|
|
--object-path "$OBJ_PATH" \
|
|
--method "$IFACE.brightness" | \
|
|
sed 's/(//;s/,)//'
|
|
}
|
|
|
|
case "$cmd" in
|
|
get)
|
|
max=$(get_max)
|
|
current=$(get_current)
|
|
percent=$(( current * 100 / max ))
|
|
echo "${percent}% ($current/$max)"
|
|
;;
|
|
set)
|
|
if [ $# -lt 2 ]; then echo '{"schema": {"type": "array", "items": [{"name": "action", "type": "string", "description": "get|set", "required": true}, {"name": "percent", "type": "string", "description": "Brightness percent 0-100 (for set)", "required": false}], "examples": [["get"], ["set", "75"]]}}'; exit 1; fi
|
|
percent="${2}"
|
|
if [ "$percent" -lt 0 ] || [ "$percent" -gt 100 ]; then
|
|
echo "ERROR: percent must be 0-100" >&2
|
|
exit 1
|
|
fi
|
|
max=$(get_max)
|
|
value=$(( percent * max / 100 ))
|
|
gdbus call --session --dest "$SERVICE" \
|
|
--object-path "$OBJ_PATH" \
|
|
--method "$IFACE.setBrightness" "$value" >/dev/null
|
|
echo "brightness: ${percent}% ($value/$max)"
|
|
;;
|
|
up)
|
|
step="${2:-10}"
|
|
max=$(get_max)
|
|
current=$(get_current)
|
|
current_pct=$(( current * 100 / max ))
|
|
new_pct=$(( current_pct + step ))
|
|
[ "$new_pct" -gt 100 ] && new_pct=100
|
|
value=$(( new_pct * max / 100 ))
|
|
gdbus call --session --dest "$SERVICE" \
|
|
--object-path "$OBJ_PATH" \
|
|
--method "$IFACE.setBrightness" "$value" >/dev/null
|
|
echo "brightness: ${new_pct}% ($value/$max)"
|
|
;;
|
|
down)
|
|
step="${2:-10}"
|
|
max=$(get_max)
|
|
current=$(get_current)
|
|
current_pct=$(( current * 100 / max ))
|
|
new_pct=$(( current_pct - step ))
|
|
[ "$new_pct" -lt 0 ] && new_pct=0
|
|
value=$(( new_pct * max / 100 ))
|
|
gdbus call --session --dest "$SERVICE" \
|
|
--object-path "$OBJ_PATH" \
|
|
--method "$IFACE.setBrightness" "$value" >/dev/null
|
|
echo "brightness: ${new_pct}% ($value/$max)"
|
|
;;
|
|
*)
|
|
echo '{"schema": {"type": "array", "items": [{"name": "action", "type": "string", "description": "get|set", "required": true}, {"name": "percent", "type": "string", "description": "Brightness percent 0-100 (for set)", "required": false}], "examples": [["get"], ["set", "75"]]}}' >&2
|
|
echo "" >&2
|
|
echo " get - show current brightness as percentage" >&2
|
|
echo " set <0-100> - set brightness to percentage" >&2
|
|
echo " up [step] - increase by step% (default 10)" >&2
|
|
echo " down [step] - decrease by step% (default 10)" >&2
|
|
exit 1
|
|
;;
|
|
esac
|