#!/bin/sh
# Keep DRM DPMS and the schedutil rate limit in sync with the backlight state.
# Invoked by the backlight udev rule (as root) and by sxmo_wm.sh display on
# (via doas). The 2720 Flip shares one PWM backlight between both panels:
# it is active-low for the cover and active-high for the main panel.
set -u

BR_PATH=/sys/class/backlight/backlight/brightness
BL_POWER_PATH=/sys/class/backlight/backlight/bl_power
KBD_PATH=/sys/class/leds/white:kbd_backlight/brightness
RL_PATH=/sys/devices/system/cpu/cpufreq/schedutil/rate_limit_us
COVER_MARKER=/run/bananarchy/cover-backlight
FORCE_MARKER=/run/bananarchy/cover-bl-force
LID_STATE=/run/bananarchy/lid-state
CONF=/etc/bananarchy/cover-display.conf
COVER_BL_LEVEL=14
COVER_BL_INVERT=1

# A forced PWM update rewrites brightness/bl_power itself. Ignore the nested
# udev events so the two-step update cannot recurse.
if [ -f "$FORCE_MARKER" ]; then
  exit 0
fi

if [ -r "$CONF" ]; then
  _level=$(awk -F= '/^[[:space:]]*kernel_backlight_level[[:space:]]*=/ {gsub(/[[:space:]]/, "", $2); print $2; exit}' "$CONF" 2>/dev/null)
  case "${_level:-}" in ''|*[!0-9]*) ;; *) COVER_BL_LEVEL="$_level" ;; esac
  _invert=$(awk -F= '/^[[:space:]]*kernel_backlight_invert[[:space:]]*=/ {gsub(/[[:space:]]/, "", $2); print $2; exit}' "$CONF" 2>/dev/null)
  case "${_invert:-}" in 0|1) COVER_BL_INVERT="$_invert" ;; esac
fi

DPMS_PATH=
for _dpms in /sys/class/drm/card*-*/dpms; do
  [ -r "$_dpms" ] && DPMS_PATH="$_dpms" && break
done

read_value() {
  _path="$1"
  _value=""
  [ -r "$_path" ] && { read -r _value < "$_path" 2>/dev/null || _value=""; }
  printf '%s' "$_value"
}

set_if_changed() {
  _path="$1"
  _want="$2"
  _cur="$(read_value "$_path")"
  [ "$_cur" = "$_want" ] || printf '%s\n' "$_want" > "$_path" 2>/dev/null || true
}

force_backlight() {
  _want_br="$1"
  _want_blp="$2"
  printf '1\n' > "$FORCE_MARKER" 2>/dev/null || true
  set_if_changed "$BL_POWER_PATH" "4"
  printf '%s\n' "$_want_br" > "$BR_PATH" 2>/dev/null || true
  set_if_changed "$BL_POWER_PATH" "$_want_blp"
  printf '%s\n' "$_want_br" > "$BR_PATH" 2>/dev/null || true
  rm -f "$FORCE_MARKER" 2>/dev/null || true
}

force_dpms() {
  _want="$1"
  _cur="$(read_value "$DPMS_PATH")"
  [ "$_cur" = "$_want" ] && return 0
  _mode="$(printf '%s' "$_want" | tr 'A-Z' 'a-z')"
  DISPLAY=:0 xset dpms force "$_mode" >/dev/null 2>&1 || true
}

br="$(read_value "$BR_PATH")"
case "$br" in ''|*[!0-9]*) br=1 ;; esac
lid="$(read_value "$LID_STATE")"
marker=""
[ -r "$COVER_MARKER" ] && marker="$(read_value "$COVER_MARKER")"

if [ "$COVER_BL_INVERT" -eq 1 ]; then
  LIT_BR=0
  DARK_BR="$COVER_BL_LEVEL"
  DARK_BLP=0
else
  LIT_BR="$COVER_BL_LEVEL"
  DARK_BR=0
  DARK_BLP=4
fi

# An open flip always wins: remove a stale cover marker and restore the
# active-high internal-panel backlight before applying normal DPMS rules.
if [ "$lid" = "open" ]; then
  rm -f "$COVER_MARKER" 2>/dev/null || true
  force_backlight "$COVER_BL_LEVEL" "0"
  force_dpms "On"
  if [ -w "$RL_PATH" ]; then
    printf '40000\n' > "$RL_PATH" 2>/dev/null || true
  fi
  exit 0
fi

# While the cover owns the bus, the marker content selects its state:
# 1 = cover lit, 0 = cover dark. Never let the closed-lid hard gate force
# brightness 0 here, because that would turn the active-low cover backlight on.
if [ -f "$COVER_MARKER" ]; then
  set_if_changed "$KBD_PATH" "0"
  if [ "$marker" = "1" ]; then
    force_backlight "$LIT_BR" "0"
    force_dpms "On"
  else
    force_backlight "$DARK_BR" "$DARK_BLP"
    force_dpms "On"
  fi
  exit 0
fi

# Hard gate: while the flip is closed no path may relight the main panel or
# the keypad backlight. The cover daemon keeps gpio13 low in this state.
if ! "${BANANARCHY_LID_GATE:-/usr/bin/bananarchy-lid-gate.sh}"; then
  set_if_changed "$KBD_PATH" "0"
  force_backlight "0" "4"
  force_dpms "Off"
  exit 0
fi

if [ "$br" -eq 0 ]; then
  force_dpms "Off"
else
  force_dpms "On"
fi

if [ "$br" -ne 0 ] && [ -w "$RL_PATH" ]; then
  printf '40000\n' > "$RL_PATH" 2>/dev/null || true
fi

exit 0
