From 5aed9a71c4915de4b042ca6f904ebe562cf582d9 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Wed, 25 Apr 2018 19:01:54 -0400 Subject: bin/dpmsmouse: New script --- (limited to 'bin') diff --git a/bin/dpmsmouse b/bin/dpmsmouse new file mode 100755 index 0000000..a9ebf09 --- /dev/null +++ b/bin/dpmsmouse @@ -0,0 +1,154 @@ +#!/bin/sh + +set -eu + +DEFAULT_POLL_TIME=5 + +debug=0 + +err() +{ + local fmt="${1}" + shift 1 + + printf "Error: ${fmt}\n" "${@}" 1>&2 + return 0 +} + +dbg() +{ + local fmt="${1}" + shift 1 + + case "${debug}" in '1') + printf "DEBUG: ${fmt}\n" "${@}" 1>&2 + esac + + return 0 +} + +enable_devices() +{ + local device= + + dbg 'Enabling devices:' + for device in "${@}"; do + dbg ' %s' "${device}" + xinput --enable "${device}" + done +} + +disable_devices() +{ + local device= + + dbg 'Disabling devices:' + for device in "${@}"; do + dbg ' %s' "${device}" + xinput --disable "${device}" + done +} + +poll() +{ + local poll_time=${1} + shift 1 + local displays= + local prev_state= + local displays_on= + local displays_off= + local display= + local status= + local dpms= + local state= + + displays="$(printf '%s ' \ + /sys/devices/pci????:??/????:??:??.?/drm/card*/card*-*/ \ + /sys/devices/pci????:??/????:??:??.?/????:??:??.?/drm/card*/card*-*/)" + + prev_state='' + while :; do + displays_on=0 + displays_off=0 + for display in ${displays}; do + [ -f "${display}/status" ] || continue + read status 0<"${display}/status" + case "${status}" in 'disconnected') + continue + esac + read dpms 0<"${display}/dpms" + case "${dpms}" in + 'On') displays_on=1;; + 'Off') displays_off=1;; + esac + done + + state='enable_devices' + case ${displays_on} in 0) + case ${displays_off} in 1) + state='disable_devices' + esac + esac + + case "${state}" in + "${prev_state}");; + *) + "${state}" "${@}" + prev_state="${state}" + ;; + esac + + sleep ${poll_time} + done +} + +usage() +{ + printf 'Usage: %s [] [ ...]\n' "${0}" + printf '\n' + printf 'Options:\n' + printf ' -p Poll at least every seconds\n' + printf ' -d Enable debugging output\n' +} + +main() +{ + local move_win= + local opt= + local dir= + local dx= + local dy= + local cur_dt= + local new_dt= + + poll_time=${DEFAULT_POLL_TIME} + + while getopts 'p:d' opt "${@}"; do + case "${opt}" in + 'p') + case "${OPTARG}" in *[!0-9]* | '') + usage 1>&2 + return 1 + esac + poll_time="${OPTARG}" + ;; + 'd') debug=1;; + '?') + usage 1>&2 + return 1 + ;; + esac + done + shift $((${OPTIND} - 1)) + + if [ ${#} -lt 1 ]; then + usage 1>&2 + return 1 + fi + + poll ${poll_time} "${@}" + + return 0 +} + +main "${@}" -- cgit v0.9.1