~mil/sxmo-utils

ref: 1.5.2.1 sxmo-utils/scripts/core/sxmo_idle.sh -rw-r--r-- 2.1 KiB
cdf1f8c6 — Stacy Harper fix ssh mode since we dropped xdm-config 4 years ago
                                                                                
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/sh

# include common definitions
# shellcheck source=scripts/core/sxmo_common.sh
. "$(dirname "$0")/sxmo_common.sh"

set -e

LOCK=0
OFF=0
SUSPEND=0

readconfig() {
	if [ ! -f "$CACHEDIR"/sxmo.idle.state ]; then
		printf "LOCK 0\nOFF 0\nSUSPEND 0\n" >"$CACHEDIR"/sxmo.idle.state
	fi
	IDLE_STATE="$(cat "$CACHEDIR"/sxmo.idle.state)"

	LOCK="$(
		printf %s "$IDLE_STATE" |
			grep ^LOCK |
			cut -d' ' -f2
		)"

	OFF="$(
		printf %s "$IDLE_STATE" |
			grep ^OFF |
			cut -d' ' -f2
		)"

	SUSPEND="$(
		printf %s "$IDLE_STATE" |
			grep ^SUSPEND |
			cut -d' ' -f2
		)"
}

start() {
	if pgrep swayidle; then
		notify-send "Already running !"
		exit 1
	fi

	set --

	if [ "$LOCK" -gt 0 ]; then
		set -- "$@" timeout "$LOCK" "sxmo_screenlock.sh lock"
	fi

	if [ "$OFF" -gt 0 ]; then
		set -- "$@" timeout "$OFF" "sxmo_screenlock.sh off"
	fi

	if [ "$SUSPEND" -gt 0 ]; then
		set -- "$@" timeout "$SUSPEND" "sxmo_screenlock.sh off"
	fi

	if [ "$#" -eq 0 ]; then
		notify-send "Idle monitor disabled"
		exit 1
	fi

	exec swayidle "$@"
}

stop() {
	pkill swayidle || return 0
	sleep 1
}

configmenu() {
	PICKED="$(
		printf "LOCK %d\nOFF %d\nSUSPEND %d\n" "$LOCK" "$OFF" "$SUSPEND" | \
			sxmo_dmenu_with_kb.sh
	)"

	target="$(printf %s "$PICKED" | cut -d" " -f1)"
	old_value="$(printf %s "$PICKED" | cut -d" " -f2)"

	while [ -z "$new_value" ]; do
		new_value="$(
			printf "" | \
				sxmo_dmenu_with_kb.sh -p "New value"
		)"
		if ! [ "$new_value" -eq "$new_value" ]; then
			unset new_value # not an integer value
		elif [ "$new_value" -lt 5 ] && [ "$new_value" -ne 0 ]; then
			unset new_value
		fi
	done

	sed -i "s|$target $old_value|$target $new_value|" "$CACHEDIR"/sxmo.idle.state
}

readconfig

action="${1:-start}"
case "$action" in
	start)
		start
		sleep 1
		if pgrep swayidle; then
			notify-send "Dpms Started"
		fi
		;;
	stop)
		stop
		sleep 1
		if ! pgrep swayidle; then
			notify-send "Dpms Stopped"
		fi
		;;
	restart)
		stop
		swaymsg exec "$(basename "$0")" start
		sleep 1
		if pgrep swayidle; then
			notify-send "Dpms Restarted"
		fi
		;;
	config)
		configmenu
		swaymsg exec "$(basename "$0")" restart
		;;
esac