1 293b5b9b 2023-06-05 shawtynoo #!/bin/sh -e
2 bebbb737 2023-07-17 boredpast # mpc-notify: sends notification of currently playing MPD music accompanied
3 bebbb737 2023-07-17 boredpast # by album cover art.
5 5f6069a6 2023-07-01 shawtynoo # exit early if we're in tty
6 5f6069a6 2023-07-01 shawtynoo if [ -z "$WAYLAND_DISPLAY" ] && [ -z "$DISPLAY" ]; then
10 70a17dba 2023-07-17 boredpast # globals
11 5f6069a6 2023-07-01 shawtynoo command="$1"
12 70a17dba 2023-07-17 boredpast use_preview=0
14 70a17dba 2023-07-17 boredpast # defaults
15 70a17dba 2023-07-17 boredpast # FEATURE: make defaults configurable through flags
16 70a17dba 2023-07-17 boredpast preview_dir="${XDG_CONFIG_HOME:-$HOME/.config}/mpc/previews"
17 9733f193 2023-06-05 shawtynoo preview_width=64
18 9733f193 2023-06-05 shawtynoo preview_height=64
19 9733f193 2023-06-05 shawtynoo default_song_icon="media-playback-start"
21 9733f193 2023-06-05 shawtynoo # issue an mpc command based on arguments and assemble notification message
22 659f0db2 2023-07-01 shawtynoo case "$command" in
24 289051ee 2023-07-17 boredpast # use defaults
25 289051ee 2023-07-17 boredpast song_head="Now Playing"
26 289051ee 2023-07-17 boredpast use_preview=1
28 2db3daa5 2025-01-13 boredpast "stop")
29 2db3daa5 2025-01-13 boredpast mpc -q stop
30 2db3daa5 2025-01-13 boredpast song_head="Stopped Playing"
31 2db3daa5 2025-01-13 boredpast song_icon="media-playback-stop"
33 289051ee 2023-07-17 boredpast "toggle")
34 289051ee 2023-07-17 boredpast mpc -q toggle
35 289051ee 2023-07-17 boredpast state="$(mpc status %state%)"
36 289051ee 2023-07-17 boredpast case "$state" in
37 289051ee 2023-07-17 boredpast "playing")
38 9733f193 2023-06-05 shawtynoo song_head="Now Playing"
39 9733f193 2023-06-05 shawtynoo use_preview=1
41 289051ee 2023-07-17 boredpast "paused")
42 289051ee 2023-07-17 boredpast song_head="Paused"
43 289051ee 2023-07-17 boredpast song_icon="media-playback-pause"
47 289051ee 2023-07-17 boredpast "shuffle")
48 289051ee 2023-07-17 boredpast mpc -q shuffle
49 289051ee 2023-07-17 boredpast song_head="Shuffled Playlist"
50 289051ee 2023-07-17 boredpast song_icon="media-playlist-shuffle"
52 289051ee 2023-07-17 boredpast "single")
53 289051ee 2023-07-17 boredpast mpc -q single
54 289051ee 2023-07-17 boredpast state="$(mpc status %single%)"
55 289051ee 2023-07-17 boredpast case "$state" in
57 289051ee 2023-07-17 boredpast song_head="Single Mode: On";;
58 289051ee 2023-07-17 boredpast "once")
59 289051ee 2023-07-17 boredpast song_head="Single Mode: Once";;
61 289051ee 2023-07-17 boredpast song_head="Single Mode: Off";;
63 bebbb737 2023-07-17 boredpast # TODO: consider a more appropriate icon
64 289051ee 2023-07-17 boredpast song_icon="multimedia-player"
66 289051ee 2023-07-17 boredpast "random")
67 289051ee 2023-07-17 boredpast mpc -q random
68 289051ee 2023-07-17 boredpast state="$(mpc status %random%)"
69 289051ee 2023-07-17 boredpast case "$state" in
71 289051ee 2023-07-17 boredpast song_head="Random Mode: On";;
73 289051ee 2023-07-17 boredpast song_head="Random Mode: Off";;
75 bebbb737 2023-07-17 boredpast # TODO: consider a more appropriate icon
76 289051ee 2023-07-17 boredpast song_icon="media-eject"
78 289051ee 2023-07-17 boredpast "repeat")
79 289051ee 2023-07-17 boredpast mpc -q repeat
80 289051ee 2023-07-17 boredpast state="$(mpc status %repeat%)"
81 289051ee 2023-07-17 boredpast case "$state" in
83 289051ee 2023-07-17 boredpast song_head="Repeat Mode: On";;
85 289051ee 2023-07-17 boredpast song_head="Repeat Mode: Off";;
87 289051ee 2023-07-17 boredpast song_icon="media-playlist-repeat"
90 289051ee 2023-07-17 boredpast mpc -q "$command"
91 289051ee 2023-07-17 boredpast # don't notify if mpc changed song because ncmpcpp would notify on
92 bebbb737 2023-07-17 boredpast # song change anyway
93 289051ee 2023-07-17 boredpast [ "$command" = "next" ] || [ "$command" = "prev" ] && exit
97 9733f193 2023-06-05 shawtynoo # use album preview for song icon
98 9733f193 2023-06-05 shawtynoo if [ "$use_preview" -eq 1 ]; then
99 9733f193 2023-06-05 shawtynoo [ -d "$preview_dir" ] || mkdir -p "$preview_dir"
101 70a17dba 2023-07-17 boredpast # find xdg music directory
102 70a17dba 2023-07-17 boredpast # user-dirs.dirs is not guaranteed to exist
103 70a17dba 2023-07-17 boredpast # shellcheck disable=SC1091
104 70a17dba 2023-07-17 boredpast . "${XDG_CONFIG_HOME:-$HOME/.config}/user-dirs.dirs" 2>/dev/null || true
105 70a17dba 2023-07-17 boredpast xdg_music_dir="${XDG_MUSIC_DIR:-$HOME/Music}"
107 9733f193 2023-06-05 shawtynoo # get preview filename
108 0cb713aa 2023-07-06 shawtynoo song_filename="$(mpc --format "${xdg_music_dir}/%file%" current)"
109 bebbb737 2023-07-17 boredpast song_album_name="$(mpc --format %album% current)"
110 bebbb737 2023-07-17 boredpast if [ -z "$song_album_name" ]; then
111 bebbb737 2023-07-17 boredpast # fallback to the default icon for an empty album name
112 9733f193 2023-06-05 shawtynoo song_icon="$default_song_icon"
114 bebbb737 2023-07-17 boredpast preview_path="${preview_dir}/$(echo "$song_album_name" | base64).png"
115 9733f193 2023-06-05 shawtynoo if [ -f "$preview_path" ]; then
116 9733f193 2023-06-05 shawtynoo # use cached preview
117 9733f193 2023-06-05 shawtynoo song_icon="$preview_path"
118 9733f193 2023-06-05 shawtynoo elif ffmpeg -y -i "$song_filename" -an -vf \
119 9733f193 2023-06-05 shawtynoo scale="${preview_width}:${preview_height}" \
120 9733f193 2023-06-05 shawtynoo "$preview_path" >/dev/null 2>&1;
122 9733f193 2023-06-05 shawtynoo # or use generated preview
123 9733f193 2023-06-05 shawtynoo song_icon="$preview_path"
125 9733f193 2023-06-05 shawtynoo song_dirname=$(dirname "$song_filename")
126 93ddae21 2023-07-17 boredpast if art_filename="$(find "$song_dirname" -type f |
127 9733f193 2023-06-05 shawtynoo grep -Esm 1 '.*\.(jpg|png)')"
129 9733f193 2023-06-05 shawtynoo # or use scaled cover art from directory
130 9733f193 2023-06-05 shawtynoo ffmpeg -y -i "$art_filename" -an -vf \
131 9733f193 2023-06-05 shawtynoo scale="${preview_width}:${preview_height}" \
132 9733f193 2023-06-05 shawtynoo "$preview_path" >/dev/null 2>&1;
133 9733f193 2023-06-05 shawtynoo song_icon="$preview_path"
135 9733f193 2023-06-05 shawtynoo # or fallback to default icon
136 9733f193 2023-06-05 shawtynoo song_icon="$default_song_icon"
142 bebbb737 2023-07-17 boredpast # now send the actual notification
143 70a17dba 2023-07-17 boredpast song_body="$(mpc --format '[%title%[ \n%artist% - %album%]]|[%file%]' current)"
144 9733f193 2023-06-05 shawtynoo notify-replace 'mpc-notify' "$song_head" "$song_body" -i "$song_icon" -u low