#!/bin/bash set -euo pipefail CONFIG_PATH="$HOME/.config/omarchy/dell-haptic.conf" DEFAULT_INTENSITY=100 LOW_INTENSITY=10 MID_INTENSITY=50 HIGH_INTENSITY=100 SERVICE_NAME="dell-xps-touchpad-haptics.service" usage() { printf 'Usage: %s get | set \n' "$(basename -- "$0")" } clamp_intensity() { local value=$1 if ! [[ $value =~ ^[0-9]+$ ]]; then printf '%s\n' "$DEFAULT_INTENSITY" return 0 fi if (( value < 0 )); then printf '0\n' elif (( value > 100 )); then printf '100\n' else printf '%s\n' "$value" fi } read_intensity() { local line="" local value="" [[ -f $CONFIG_PATH ]] || { printf '%s\n' "$DEFAULT_INTENSITY" return 0 } while IFS= read -r line; do line=${line#${line%%[![:space:]]*}} line=${line%${line##*[![:space:]]}} [[ -z $line || ${line:0:1} == "#" || $line != *=* ]] && continue if [[ ${line%%=*} == "INTENSITY" ]]; then value=${line#*=} value=${value#${value%%[![:space:]]*}} value=${value%${value##*[![:space:]]}} clamp_intensity "$value" return 0 fi done < "$CONFIG_PATH" printf '%s\n' "$DEFAULT_INTENSITY" } level_for_intensity() { local intensity=$1 if (( intensity <= 30 )); then printf 'low\n' elif (( intensity < 75 )); then printf 'mid\n' else printf 'high\n' fi } intensity_for_level() { case "$1" in low) printf '%s\n' "$LOW_INTENSITY" ;; mid | medium) printf '%s\n' "$MID_INTENSITY" ;; high) printf '%s\n' "$HIGH_INTENSITY" ;; *) echo "Haptic level must be one of: low, mid, high." >&2 exit 1 ;; esac } write_config() { local intensity=$1 install -d -m 755 "$(dirname -- "$CONFIG_PATH")" printf '# Dell XPS touchpad haptic intensity.\n# Presets: low=10, mid=50, high=100.\nINTENSITY=%s\n' "$intensity" >"$CONFIG_PATH" } service_active() { systemctl is-active --quiet "$SERVICE_NAME" 2>/dev/null } case "${1:-}" in get) if (( $# != 1 )); then usage >&2 exit 1 fi level_for_intensity "$(read_intensity)" ;; set) if (( $# != 2 )); then usage >&2 exit 1 fi previous_level=$(level_for_intensity "$(read_intensity)") intensity=$(intensity_for_level "$2") write_config "$intensity" if service_active; then printf 'Set Dell XPS touchpad haptics from %s to %s. The running service will apply and verify the new gain in the journal shortly.\n' "$previous_level" "$(level_for_intensity "$intensity")" else printf 'Set Dell XPS touchpad haptics from %s to %s. Start %s to apply it.\n' "$previous_level" "$(level_for_intensity "$intensity")" "$SERVICE_NAME" fi ;; --help | -h) usage ;; *) usage >&2 exit 1 ;; esac