rsps-scripts

Automation scripts for private servers
Log | Files | Refs

functions.sh (3258B)


      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 50 100
     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 50 100
     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 250 350
     41 }
     42 
     43 function current_mouse() {
     44 	hyprctl cursorpos
     45 }
     46 
     47 function close_bank() {
     48 	msleep 100 200
     49 	move_mouse 1086 323 14 14
     50 	msleep 100 200
     51 	left_click
     52 	msleep 200 300
     53 }
     54 
     55 function move_mouse() {
     56 	# debug line: printf 'args(%d):' "$#"; printf ' [%q]' "$@"; printf '\n' >&2
     57 	local relative=0
     58 	if [[ "${1:-}" == "-r" ]]; then
     59 		relative=1
     60 		shift
     61 	fi
     62 
     63 	local x y width height
     64 	case "$#" in
     65 		2)
     66 			x="$1"
     67 			y="$2"
     68 			;;
     69 
     70 		4)
     71 			x="$1"
     72 			y="$2"
     73 			width="$3"
     74 			height="$4"
     75 
     76 			# arithmetic instead of shuf so negative relative offsets work
     77 			x=$((x + RANDOM % (width + 1)))
     78 			y=$((y + RANDOM % (height + 1)))
     79 			;;
     80 
     81 		*)
     82 			echo "Usage: move_mouse [-r] x y [width height]" >&2
     83 			return 1
     84 			;;
     85 	esac
     86 
     87 	if ((relative)); then
     88 		local cx cy
     89 		# hyprctl cursorpos prints "150, 150"
     90 		IFS=', ' read -r cx cy < <(hyprctl cursorpos)
     91 		x=$((cx + x))
     92 		y=$((cy + y))
     93 	fi
     94 
     95 	ydotool mousemove -a -x "$x" -y "$y"
     96 }
     97 
     98 function cleanup() {
     99 	hctl eval 'hl.unbind("SUPER + F12")' >/dev/null
    100 	hctl eval 'hl.unbind("F11")' 2>/dev/null
    101 	hctl eval 'hl.unbind("F10")' 2>/dev/null
    102 	rm -f "$pipe"
    103 	# restore cursor hiding
    104 	hctl eval 'hl.config({ ["cursor.inactive_timeout"] = 3 })'
    105 
    106 	# stop ydotoold if we started it
    107 	if [[ -n "${YDOTOOLD_PID:-}" ]]; then
    108 		kill "$YDOTOOLD_PID" 2>/dev/null
    109 		wait "$YDOTOOLD_PID" 2>/dev/null
    110 	fi
    111 }
    112 
    113 function startup() {
    114 	# cleanup on term or int
    115 	trap 'cleanup; exit 0' TERM INT
    116 	# pipe setup
    117 	pipe="/tmp/rsps.pipe"
    118 	mkfifo "$pipe"
    119 
    120 	# start ydotoold in background (skip if already running)
    121 	if pgrep -x ydotoold >/dev/null; then
    122 		printf ' - ydotoold already running, skipping start\n'
    123 		YDOTOOLD_PID=""
    124 	else
    125 		ydotoold >/dev/null 2>&1 &
    126 		YDOTOOLD_PID=$!
    127 		sleep 0.3
    128 	fi
    129 	# fullscreen client
    130 	fullscreen
    131 	# disable mouse cursor hiding
    132 	hctl eval 'hl.config({ ["cursor.inactive_timeout"] = 0 })'
    133 	printf 'Press Super+F12 to quit.\n'
    134 	hctl eval "hl.bind(\"SUPER + F12\", hl.dsp.exec_cmd(\"kill $BASHPID\"))" >/dev/null
    135 	hctl eval "hl.bind(\"F11\", hl.dsp.exec_cmd(\"echo go > $pipe\"))"
    136 	hctl eval "hl.bind(\"F10\", hl.dsp.exec_cmd(\"echo second > $pipe\"))"
    137 }
    138 
    139 function complete() {
    140 	printf '\nFinished.\n'
    141 	cleanup
    142 }