File: sxmo_idle.sh

package info (click to toggle)
sxmo-utils 1.14.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 6,016 kB
  • sloc: sh: 9,166; ansic: 117; makefile: 68
file content (70 lines) | stat: -rwxr-xr-x 1,081 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2022 Sxmo Contributors

_swayidletoidles() {
	while [ $# -gt 0 ]; do
		case "$1" in
			timeout)
				printf "%s|%s" "$2" "$3"
				shift 3
				case "$1" in
					resume)
						printf "|%s" "$2"
						shift 2
				;;
				esac
				printf "\n"
				;;
			*)
				shift
				;;
		esac
	done
}

xorgidle() {
	idles="$(_swayidletoidles "$@")"
	resumes=""

	tick=0
	new_idle="$(xprintidle)"
	last_idle="$new_idle"

	finish() {
		sh -c "$resumes"
		resumes=""
		exit
	}
	trap 'finish' TERM INT EXIT

	while : ; do
		last_idle="$new_idle"
		new_idle="$(xprintidle)"
		if [ "$last_idle" -gt "$new_idle" ]; then
			sh -c "$resumes"
			tick=0
			resumes=""
		fi

		if printf "%b\n" "$idles" | grep -q "^$tick|"; then
			printf "%b\n" "$idles" | \
				grep "^$tick|" | \
				cut -d'|' -f2 | \
				xargs -I{} -0 sh -c "{}"
			resumes="$(printf "%b\n" "$idles" | grep "^$tick|" | cut -d'|' -f3);$resumes"
		fi

		sleep 1
		tick=$((tick + 1))
	done
}

case "$SXMO_WM" in
	dwm)
		xorgidle "$@"
		;;
	*)
		exec "${SXMO_WM}idle" "$@"
		;;
esac