summaryrefslogtreecommitdiff
path: root/scripts/lib/lib_handle.sh
blob: 5405e4a37706d0d45ee4294dde7518f21f911a7b (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/sh

. "lib_common.sh"

# This script defines a set of “*_handle” helper functions used to control
# system features.
#
# Each handler uses an external program and centralizes these calls
# here so they can be easily swapped or replaced in one location if
# you decide to use different tools later.
#
# Each function expects a first argument selecting an action, followed by
# any required parameters. They provide a simple, unified interface for
# controlling desktop-related functionality from other scripts.

volume_handle() {
	case "${1}" in
	"up") pactl set-sink-volume @DEFAULT_SINK@ +"${2}"% ;;
	"down") pactl set-sink-volume @DEFAULT_SINK@ -"${2}"% ;;
	"set") pactl set-sink-volume @DEFAULT_SINK@ "${2}"% ;;
	"toggle") pactl set-sink-mute @DEFAULT_SINK@ toggle;;
	"get-current")
		pactl get-sink-volume @DEFAULT_SINK@ |
			sed -n 's/.* \([0-9][0-9]*%\).*/\1/p'
		;;
	"check_program")
		check_program "pactl" "pulseaudio must be installed" ;;

	esac
}

brightness_handle() {
	case "${1}" in
	"up") brightnessctl set +"${2}"% ;;
	"down") brightnessctl set "${2}"-% ;;
	"set") brightnessctl set "${2}"% ;;
	"get-current")
		printf '%s\n' \
			$(( ($(brightnessctl g) * 100)  / $(brightnessctl m) ))
		;;
	"check_program") check_program "brightnessctl"
	esac
}

screenshot_handle() {
	case "${1}" in
	"fullscreen") flags="-z" ;;
	"select") flags="-zs" ;;
	"focused-window") flags="-zu" ;;
	"check_program")
		check_program "scrot"
		return
	;;
	esac
	shift
	scrot "${flags}" "${1}"
}

wallpaper_handle() {
	case "${1}" in
	"clear") xwallpaper --clear ;;
	"set") xwallpaper --zoom "${2}" ;;
	"check_program") check_program "xwallpaper" ;;
	esac

}

screenlock_handle() {
	if [ "${#}" -eq 0 ]; then
		slock
	else
		slock -f "${1}"
	fi
}

input_device_handle() {
	case "$1" in
	"enable") xinput enable "${2}";;
	"disable") xinput disable "${2}";;
	"list") xinput list ;;
	"get-id")
		dev=$(xinput list --name-only | grep -i -m1 "${2}" ) || return 1
		printf "%s" "${dev#∼ }"
	;;
	"is_enabled")
    		xinput list-props "${2}" | grep -q "Device Enabled.*1$"
    		return $?
		;;
	"check_program") check_program "xclip" ;;
	esac
}

clipboard_handle() {
	case "${1}" in
	"text") xclip -selection clipboard ;;
	"file") xclip -selection clipboard -t "${2}" -i "${3}" ;;
	"get-clipboard") xclip -o -selection clipboard ;;
	esac
}

image_handle() {
	case "${1}" in
	"blur") mogrify -blur 0x8 "${2}" ;;
	"check_program")
		check_program "mogrify" "imagemagick must be installed"
	;;
	esac
}

file_handle() {
	file=$2
	case $1 in
	get-mime_type)
	m=$(file --mime-type -b "$file" 2>/dev/null) && { echo "$m"; return; }
	m=$(file -I "$file" 2>/dev/null) && { m=${m#*: }; m=${m%%;*}; echo "$m"; return; }
	m=$(file -i "$file" 2>/dev/null) && { m=${m#*: }; m=${m%%;*}; echo "$m"; return; }
	return 1
	;;
	esac
}

notify_handle() {
	case "${1}" in
	"reload") xsetroot -name "fsignal:1" ;;
	"send")
		# Avoid Hanging
		printf "%s\n" "${2}" |
			dd of=/tmp/noti.fifo oflag=nonblock 2>/dev/null
		shift 2
		printf "%s\n" "$*" > /tmp/noti.txt
		xsetroot -name "fsignal:1"
	;;
	esac
}

compositor_handle() {
    case "${1}" in
        "start") pgrep -x picom >/dev/null || picom -b & ;;
        "stop") pgrep -x picom >/dev/null && pkill -x picom ;;
    esac
}

status_handle() {
	[ "${1}" = "reload" ] && slreload
}

menu_handle() {
	case "${1}" in
	"default") flags="" ;;
	"center") flags="-bw 1 -c -g 1 -l 25" ;;
	esac
	shift
	dmenu "${flags}"
}