/ de-omarchy / bin / omarchy-brightness-display-apple
swadhinbiswas Initial de-omarchy: Omarchy UI layer for Arch-based distros · 8/22/2026
9f393b2
95 lines · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash

# omarchy:summary=Show or adjust Apple Studio Display and Apple XDR Display brightness using asdcontrol.
# omarchy:args=[--no-osd] [+N%|N%-|N%]
# omarchy:examples=omarchy brightness display apple | omarchy brightness display apple +5% | omarchy brightness display apple --no-osd 50%

device_cache="${XDG_RUNTIME_DIR:-/tmp}/omarchy-brightness-display-apple.device"
no_osd=0
if [[ ${1:-} == "--no-osd" ]]; then
  no_osd=1
  shift
fi

detect_apple_display_device() {
  local devices=()
  local path=""

  for path in /dev/usb/hiddev* /dev/hiddev*; do
    [[ -e $path ]] && devices+=("$path")
  done

  (( ${#devices[@]} > 0 )) || return 1

  sudo asdcontrol --detect "${devices[@]}" 2>/dev/null | awk -F: '/^\/dev\/(usb\/)?hiddev/{ print $1; exit }'
}

find_apple_display_device() {
  local cached=""
  local device=""

  if [[ -r $device_cache ]]; then
    read -r cached <"$device_cache" || true
    if [[ -n $cached && -e $cached ]]; then
      printf '%s\n' "$cached"
      return 0
    fi
  fi

  device="$(detect_apple_display_device)" || return 1
  [[ -n $device ]] || return 1

  printf '%s\n' "$device" >"$device_cache"
  printf '%s\n' "$device"
}

current_brightness() {
  local device="$1"

  sudo asdcontrol "$device" 2>/dev/null | awk -F= '
    /BRIGHTNESS=/ {
      print int($2 * 100 / 60000)
      found = 1
    }

    END { exit !found }
  '
}

retry_with_fresh_device() {
  rm -f "$device_cache"
  device="$(find_apple_display_device || true)"
  if [[ -z $device ]]; then
    echo "No Apple Display HID device found" >&2
    exit 1
  fi
}

device="$(find_apple_display_device || true)"
if [[ -z $device ]]; then
  echo "No Apple Display HID device found" >&2
  exit 1
fi

if (( $# == 0 )); then
  if current_brightness "$device"; then
    exit
  else
    retry_with_fresh_device
    current_brightness "$device"
    exit
  fi
fi

step="$1"
if [[ $step =~ ^([0-9]+)%-$ ]]; then
  step="-${BASH_REMATCH[1]}%"
fi

if ! sudo asdcontrol "$device" -- "$step" >/dev/null; then
  retry_with_fresh_device
  sudo asdcontrol "$device" -- "$step" >/dev/null
fi

(( no_osd )) || omarchy-osd -i brightness -p "$(current_brightness "$device")"