aboutsummaryrefslogtreecommitdiff
path: root/damb
blob: 3f4e62064a6805cd912b04c37c7fea4f712bc088 (plain) (blame)
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
#!/bin/sh

# dir where audio files are stored
amb_dir="$HOME/.local/share/damb"
pid_file="${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}}/damb.pids"
config_dir="$HOME/.config/damb"
menu="wmenu"
ico="󰎆 "

die()
{
	: "${1:?}"

	command -v notify-send > /dev/null &&
		notify-send "${ico} damb" "$1"

	printf "\033[31;1merr: %b\033[0m\n" "$1"
	exit "${2:-1}"
}

dep_check()
{
	: "${1:?}"

	for dep; do
		command -v "$dep" 1>/dev/null ||
			die "$dep not found, please install it" 127
	done

	unset dep
}

menu_opt()
{
        printf "%s\n" "select" "links" "stream" "stop" | "$menu" -p "$ico"
}

get_ambs()
{
        find "$amb_dir" -type f -exec basename {} \;
}

get_link()
{
        grep -v "^#.*$" "$config_dir"/links |
                grep "$(grep -v "^#.*$" "$config_dir"/links | cut -d '=' -f1 | "$menu" -p "$ico" -l 25)" |
                cut -d '=' -f2 | tr -d ' '
}

main()
{
	[ -z "$WAYLAND_DISPLAY" ] &&
		menu="dmenu"

	dep_check "$menu"

	case "$(menu_opt)" in
	select)
		dep_check "ffplay"
		ffplay -volume 20 -nodisp -loop 0 "$amb_dir/$(get_ambs | "$menu" -p "$ico" -l 25)" &
		echo "$!" >> "$pid_file"
		;;
	links)
		dep_check "mpv"
		mpv --no-video --no-resume-playback "$(get_link)" &
		echo "$!" >> "$pid_file"
		;;
	stop)
		while read -r process
		do
			kill -9 "$process"
		done < "$pid_file"

		rm "$pid_file"
		;;
	stream)
		paste=
		dep_check "mpv"

		if [ -n "$WAYLAND_DISPLAY" ]; then
			dep_check "wl-paste"
			paste="$(wl-paste)"
		elif [ -n "$DISPLAY" ]; then
			dep_check "xclip"
			paste="$(xclip -o -sel clip)"
		else
			die "tty supported, run a display server"
		fi

		mpv --no-video "$paste" &
		echo "$!" >> "$pid_file"
		;;
	"")
		;; # stay quiet when user quits the program during selection
	*)
		die "option not implemented";;
	esac
}

main "$@"