#!/bin/bash

# Adjust the brightness on Apple Studio Displays and Apple XDR Displays using asdcontrol.

if [[ $# -eq 0 ]]; then
  echo "Adjust Apple Display Brightness by passing +5000 or -5000 (or any range from 0-60000)"
else
  DEVICE="$(sudo asdcontrol --detect /dev/usb/hiddev* | grep ^/dev/usb/hiddev | cut -d: -f1)"
  sudo asdcontrol "$DEVICE" -- "$1" >/dev/null
  VALUE="$(sudo asdcontrol "$DEVICE" | awk -F= '/BRIGHTNESS=/{print $2+0}')"
  swayosd-client \
    --monitor "$(hyprctl monitors -j | jq -r '.[]|select(.focused==true).name')" \
    --custom-icon display-brightness \
    --custom-progress "$(awk -v v="$VALUE" 'BEGIN{printf "%.2f", v/60000}')" \
    --custom-progress-text "$(( VALUE * 100 / 60000 ))%"
fi
