Blame


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.
4 9733f193 2023-06-05 shawtynoo
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
7 5f6069a6 2023-07-01 shawtynoo exit 2
8 5f6069a6 2023-07-01 shawtynoo fi
9 70a17dba 2023-07-17 boredpast
10 70a17dba 2023-07-17 boredpast # globals
11 5f6069a6 2023-07-01 shawtynoo command="$1"
12 70a17dba 2023-07-17 boredpast use_preview=0
13 5f6069a6 2023-07-01 shawtynoo
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"
20 0cb713aa 2023-07-06 shawtynoo
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
23 289051ee 2023-07-17 boredpast "")
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
27 289051ee 2023-07-17 boredpast ;;
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"
32 2db3daa5 2025-01-13 boredpast ;;
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
40 9733f193 2023-06-05 shawtynoo ;;
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"
44 9733f193 2023-06-05 shawtynoo ;;
45 289051ee 2023-07-17 boredpast esac
46 289051ee 2023-07-17 boredpast ;;
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"
51 289051ee 2023-07-17 boredpast ;;
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
56 289051ee 2023-07-17 boredpast "on")
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";;
60 289051ee 2023-07-17 boredpast "off")
61 289051ee 2023-07-17 boredpast song_head="Single Mode: Off";;
62 289051ee 2023-07-17 boredpast esac
63 bebbb737 2023-07-17 boredpast # TODO: consider a more appropriate icon
64 289051ee 2023-07-17 boredpast song_icon="multimedia-player"
65 289051ee 2023-07-17 boredpast ;;
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
70 289051ee 2023-07-17 boredpast "on")
71 289051ee 2023-07-17 boredpast song_head="Random Mode: On";;
72 289051ee 2023-07-17 boredpast "off")
73 289051ee 2023-07-17 boredpast song_head="Random Mode: Off";;
74 289051ee 2023-07-17 boredpast esac
75 bebbb737 2023-07-17 boredpast # TODO: consider a more appropriate icon
76 289051ee 2023-07-17 boredpast song_icon="media-eject"
77 289051ee 2023-07-17 boredpast ;;
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
82 289051ee 2023-07-17 boredpast "on")
83 289051ee 2023-07-17 boredpast song_head="Repeat Mode: On";;
84 289051ee 2023-07-17 boredpast "off")
85 289051ee 2023-07-17 boredpast song_head="Repeat Mode: Off";;
86 289051ee 2023-07-17 boredpast esac
87 289051ee 2023-07-17 boredpast song_icon="media-playlist-repeat"
88 289051ee 2023-07-17 boredpast ;;
89 289051ee 2023-07-17 boredpast *)
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
94 289051ee 2023-07-17 boredpast ;;
95 9733f193 2023-06-05 shawtynoo esac
96 9733f193 2023-06-05 shawtynoo
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"
100 9733f193 2023-06-05 shawtynoo
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}"
106 70a17dba 2023-07-17 boredpast
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"
113 9733f193 2023-06-05 shawtynoo else
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;
121 9733f193 2023-06-05 shawtynoo then
122 9733f193 2023-06-05 shawtynoo # or use generated preview
123 9733f193 2023-06-05 shawtynoo song_icon="$preview_path"
124 9733f193 2023-06-05 shawtynoo else
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)')"
128 93ddae21 2023-07-17 boredpast then
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"
134 9733f193 2023-06-05 shawtynoo else
135 9733f193 2023-06-05 shawtynoo # or fallback to default icon
136 9733f193 2023-06-05 shawtynoo song_icon="$default_song_icon"
137 9733f193 2023-06-05 shawtynoo fi
138 9733f193 2023-06-05 shawtynoo fi
139 9733f193 2023-06-05 shawtynoo fi
140 9733f193 2023-06-05 shawtynoo fi
141 9733f193 2023-06-05 shawtynoo
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