rsps-scripts

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

functions.sh (3146B)


      1 #!/usr/bin/env bash
      2 
      3 # all functions assume you're running in fullscreen on a 1920x1080 monitor
      4 
      5 # currentpos: hyprctl cursorpos
      6 # currentpos: slurp
      7 
      8 set -uo pipefail
      9 
     10 function msleep() {
     11 	local ms
     12 	case "$#" in
     13 		1) ms=$1 ;;
     14 		2) ms=$(shuf -i "$1-$2" -n 1) ;;
     15 		*) return 1 ;;
     16 	esac
     17 	sleep "$(printf '%s' "$ms" | awk '{printf "%.3f", $1 / 1000}')"
     18 	read -r -t "$((ms / 1000)).$((ms % 1000))" <<< ""
     19 }
     20 
     21 function hctl() {
     22 	hyprctl "$@" >/dev/null
     23 }
     24 
     25 function fullscreen() {
     26 	target_class="org-eclipse-jdt-internal-jarinjarloader-JarRsrcLoader"
     27 	hctl dispatch "hl.dsp.window.fullscreen({ window = 'class:$target_class', action = 'set' })"
     28 }
     29 
     30 function left_click() {
     31 	msleep 100 200
     32 	hctl dispatch 'hl.dsp.send_key_state({ mods = 0, key = "mouse:272", state = "down" })' >/dev/null
     33 	hctl dispatch 'hl.dsp.send_key_state({ mods = 0, key = "mouse:272", state = "up" })' >/dev/null
     34 }
     35 
     36 function right_click() {
     37 	msleep 100 200
     38 	hctl dispatch 'hl.dsp.send_key_state({ mods = 0, key = "mouse:273", state = "down" })' >/dev/null
     39 	hctl dispatch 'hl.dsp.send_key_state({ mods = 0, key = "mouse:273", state = "up" })' >/dev/null
     40 	msleep 100 200
     41 }
     42 
     43 function current_mouse() {
     44 	hyprctl cursorpos
     45 }
     46 
     47 function move_mouse() {
     48 	# debug line: printf 'args(%d):' "$#"; printf ' [%q]' "$@"; printf '\n' >&2
     49 	local relative=0
     50 	if [[ "${1:-}" == "-r" ]]; then
     51 		relative=1
     52 		shift
     53 	fi
     54 
     55 	local x y width height
     56 	case "$#" in
     57 		2)
     58 			x="$1"
     59 			y="$2"
     60 			;;
     61 
     62 		4)
     63 			x="$1"
     64 			y="$2"
     65 			width="$3"
     66 			height="$4"
     67 
     68 			# arithmetic instead of shuf so negative relative offsets work
     69 			x=$((x + RANDOM % (width + 1)))
     70 			y=$((y + RANDOM % (height + 1)))
     71 			;;
     72 
     73 		*)
     74 			echo "Usage: move_mouse [-r] x y [width height]" >&2
     75 			return 1
     76 			;;
     77 	esac
     78 
     79 	if ((relative)); then
     80 		local cx cy
     81 		# hyprctl cursorpos prints "150, 150"
     82 		IFS=', ' read -r cx cy < <(hyprctl cursorpos)
     83 		x=$((cx + x))
     84 		y=$((cy + y))
     85 	fi
     86 
     87 	ydotool mousemove -a -x "$x" -y "$y"
     88 }
     89 
     90 function cleanup() {
     91 	hctl eval 'hl.unbind("SUPER + F12")' >/dev/null
     92 	hctl eval 'hl.unbind("F11")' 2>/dev/null
     93 	hctl eval 'hl.unbind("F10")' 2>/dev/null
     94 	rm -f "$pipe"
     95 	# restore cursor hiding
     96 	hctl eval 'hl.config({ ["cursor.inactive_timeout"] = 3 })'
     97 
     98 	# stop ydotoold if we started it
     99 	if [[ -n "${YDOTOOLD_PID:-}" ]]; then
    100 		kill "$YDOTOOLD_PID" 2>/dev/null
    101 		wait "$YDOTOOLD_PID" 2>/dev/null
    102 	fi
    103 }
    104 
    105 function startup() {
    106 	# cleanup on term or int
    107 	trap 'cleanup; exit 0' TERM INT
    108 	# pipe setup
    109 	pipe="/tmp/rsps.pipe"
    110 	mkfifo "$pipe"
    111 
    112 	# start ydotoold in background (skip if already running)
    113 	if pgrep -x ydotoold >/dev/null; then
    114 		printf ' - ydotoold already running, skipping start\n'
    115 		YDOTOOLD_PID=""
    116 	else
    117 		ydotoold >/dev/null 2>&1 &
    118 		YDOTOOLD_PID=$!
    119 		sleep 0.3
    120 	fi
    121 	# fullscreen client
    122 	fullscreen
    123 	# disable mouse cursor hiding
    124 	hctl eval 'hl.config({ ["cursor.inactive_timeout"] = 0 })'
    125 	printf 'Press Super+F12 to quit.\n'
    126 	hctl eval "hl.bind(\"SUPER + F12\", hl.dsp.exec_cmd(\"kill $BASHPID\"))" >/dev/null
    127 	hctl eval "hl.bind(\"F11\", hl.dsp.exec_cmd(\"echo go > $pipe\"))"
    128 	hctl eval "hl.bind(\"F10\", hl.dsp.exec_cmd(\"echo second > $pipe\"))"
    129 }
    130 
    131 function complete() {
    132 	printf '\nFinished.\n'
    133 	cleanup
    134 }