diff --git a/src/main.sh b/src/main.sh index 2a64114..7e261bd 100755 --- a/src/main.sh +++ b/src/main.sh @@ -75,6 +75,7 @@ if [ "${1:-}" = "--play-release" ]; then deco=${3} base="$(dirname "$deco")" echo "Playing release ($mbid) [$deco]" >>/tmp/foo + tmplist=$(mktemp) mb_release "$mbid" | $JQ -r --slurpfile deco "$deco" --arg base "$base" '$deco[].tracks as $filenames | .media[] | @@ -93,15 +94,47 @@ if [ "${1:-}" = "--play-release" ]; then end ) | sort_by([.pos, .number]) | - map($base + "/" + .file)[]' >>/tmp/foo + map($base + "/" + .file)[]' >"$tmplist" + if [ "${4:-}" ]; then + mpv_play_list "$tmplist" + else + mpv_queue_list "$tmplist" + fi + rm -f "$tmplist" exit 0 fi if [ "${1:-}" = "--play-recording" ]; then echo "Playing recording $2" >>/tmp/foo + mpv_play_file "$2" + if [ "${3:-}" ]; then + mpv_play_file "$2" + else + mpv_queue_file "$2" + fi exit 0 fi +if [ "${1:-}" = "--play-toggle" ]; then + mpv_toggle_pause +fi + +if [ "${1:-}" = "--play-next" ]; then + mpv_next +fi + +if [ "${1:-}" = "--play-previous" ]; then + mpv_prev +fi + +if [ "${1:-}" = "--seek-forward" ]; then + mpv_seek_forward +fi + +if [ "${1:-}" = "--seek-backward" ]; then + mpv_seek_backward +fi + if [ "${1:-}" = "--internal-preview-artist" ]; then __preview_artist "$2" exit 0 @@ -387,6 +420,9 @@ case "${1:-}" in ;; esac +# Start mpv +mpv_start + while true; do case "${1:-}" in "--artist") @@ -407,7 +443,7 @@ while true; do --with-nth="{1}" \ --expect="ctrl-h,ctrl-a" \ --bind="alt-l:change-query($FORMAT_LOCAL )" \ - --bind="load:unbind(enter)+transform:[ \"\$FZF_TOTAL_COUNT\" -gt 0 ] && echo \"rebind:ctrl-l\" || echo \"unbind:ctrl-l)\"" \ + --bind="load:unbind(enter)+unbind(alt-enter)+transform:[ \"\$FZF_TOTAL_COUNT\" -gt 0 ] && echo \"rebind:ctrl-l\" || echo \"unbind:ctrl-l)\"" \ --bind="ctrl-l:accept" \ --bind="alt-b:execute-silent:xdg-open 'https://musicbrainz.org/release-group/{r2}' &" \ --bind="alt-1:change-query(!$secsymb ),alt-2:change-query($secsymb )" \ @@ -442,7 +478,18 @@ while true; do --bind="alt-l:change-query($FORMAT_LOCAL )" \ --bind="load:transform:[ \"\$FZF_TOTAL_COUNT\" -gt 0 ] && echo \"rebind:ctrl-l\" || echo \"unbind:ctrl-l\"" \ --bind="enter:execute-silent:$0 --play-release {2} {3}" \ - --bind="focus:transform:[ {3} ] && echo \"rebind:enter\" || echo \"unbind:enter\"" \ + --bind="alt-enter:execute-silent:$0 --play-release {2} {3} q" \ + --bind="space:execute-silent:$0 --play-toggle" \ + --bind="ctrl-n:execute-silent:$0 --play-next" \ + --bind="ctrl-p:execute-silent:$0 --play-previous" \ + --bind="shift-right:execute-silent:$0 --seek-forward" \ + --bind="shift-left:execute-silent:$0 --seek-backward" \ + --bind="focus:transform: + [ {3} ] && c=rebind || c=unbind + for key in enter alt-enter space ctrl-n ctrl-p shift-right shift-left; do + printf \"+%s(%s)\" \"\$c\" \"\$key\" + done + " \ --bind="ctrl-l:accept" \ --bind="alt-b:execute-silent:xdg-open 'https://musicbrainz.org/release/{r2}' &" \ --bind="ctrl-d:half-page-down,ctrl-u:half-page-up" @@ -474,7 +521,18 @@ while true; do --expect="ctrl-h,ctrl-a" \ --bind="alt-l:change-query($FORMAT_LOCAL )" \ --bind="enter:execute-silent:$0 --play-recording {3}" \ - --bind="focus:transform:[ {3} ] && echo \"rebind:enter\" || echo \"unbind:enter\"" \ + --bind="alt-enter:execute-silent:$0 --play-recording {3} q" \ + --bind="space:execute-silent:$0 --play-toggle" \ + --bind="ctrl-n:execute-silent:$0 --play-next" \ + --bind="ctrl-p:execute-silent:$0 --play-previous" \ + --bind="shift-right:execute-silent:$0 --seek-forward" \ + --bind="shift-left:execute-silent:$0 --seek-backward" \ + --bind="focus:transform: + [ {3} ] && c=rebind || c=unbind + for key in enter alt-enter space ctrl-n ctrl-p shift-right shift-left; do + printf \"+%s(%s)\" \"\$c\" \"\$key\" + done + " \ --bind="alt-b:execute-silent:xdg-open 'https://musicbrainz.org/track/{r2}' &" \ --bind="ctrl-d:half-page-down,ctrl-u:half-page-up" ) @@ -537,7 +595,7 @@ while true; do --expect="ctrl-/" \ --bind="alt-l:change-query($FORMAT_LOCAL )" \ --bind="start:transform:[ \"${2:-}\" ] && echo \"change-query:${2:-}\"" \ - --bind="load:unbind(enter)+transform:[ \"\$FZF_TOTAL_COUNT\" -gt 0 ] && echo \"rebind:ctrl-l\" || echo \"unbind:ctrl-l\"" \ + --bind="load:unbind(enter)+unbind(alt-enter)+transform:[ \"\$FZF_TOTAL_COUNT\" -gt 0 ] && echo \"rebind:ctrl-l\" || echo \"unbind:ctrl-l\"" \ --bind="ctrl-l:accept" \ --bind="ctrl-d:half-page-down,ctrl-u:half-page-up" \ --bind="alt-b:execute-silent:xdg-open 'https://musicbrainz.org/release-group/{r2}' &" \ @@ -575,7 +633,7 @@ while true; do --expect="ctrl-/" \ --bind="alt-l:change-query($FORMAT_LOCAL )" \ --bind="start:transform:[ \"${2:-}\" ] && echo \"change-query:${2:-}\"" \ - --bind="load:unbind(enter)+transform:[ \"\$FZF_TOTAL_COUNT\" -gt 0 ] && echo \"rebind:ctrl-l\" || echo \"unbind:ctrl-l\"" \ + --bind="load:unbind(enter)+unbind(alt-enter)+transform:[ \"\$FZF_TOTAL_COUNT\" -gt 0 ] && echo \"rebind:ctrl-l\" || echo \"unbind:ctrl-l\"" \ --bind="ctrl-l:accept" \ --bind="ctrl-d:half-page-down,ctrl-u:half-page-up" \ --bind="down:preview-half-page-down,up:preview-half-page-up" \ @@ -611,7 +669,18 @@ while true; do --bind="alt-l:change-query($FORMAT_LOCAL )" \ --bind="load:transform:[ \"\$FZF_TOTAL_COUNT\" -gt 0 ] && echo \"rebind:ctrl-l\" || echo \"unbind:ctrl-l\"" \ --bind="enter:execute-silent:$0 --play-release {2} {3}" \ - --bind="focus:transform:[ {3} ] && echo \"rebind:enter\" || echo \"unbind:enter\"" \ + --bind="alt-enter:execute-silent:$0 --play-release {2} {3} q" \ + --bind="space:execute-silent:$0 --play-toggle" \ + --bind="ctrl-n:execute-silent:$0 --play-next" \ + --bind="ctrl-p:execute-silent:$0 --play-previous" \ + --bind="shift-right:execute-silent:$0 --seek-forward" \ + --bind="shift-left:execute-silent:$0 --seek-backward" \ + --bind="focus:transform: + [ {3} ] && c=rebind || c=unbind + for key in enter alt-enter space ctrl-n ctrl-p shift-right shift-left; do + printf \"+%s(%s)\" \"\$c\" \"\$key\" + done + " \ --bind="ctrl-l:accept" \ --bind="alt-b:execute-silent:xdg-open 'https://musicbrainz.org/release/{r2}' &" \ --bind="ctrl-d:half-page-down,ctrl-u:half-page-up" || true @@ -646,7 +715,7 @@ while true; do --expect="ctrl-c,ctrl-q,tab" \ --expect="ctrl-/" \ --bind="alt-l:change-query($FORMAT_LOCAL )" \ - --bind="load:unbind(enter)+transform:[ \"\$FZF_TOTAL_COUNT\" -gt 0 ] && echo \"rebind:ctrl-l\" || echo \"unbind:ctrl-l\"" \ + --bind="load:unbind(enter)+unbind(alt-enter)+transform:[ \"\$FZF_TOTAL_COUNT\" -gt 0 ] && echo \"rebind:ctrl-l\" || echo \"unbind:ctrl-l\"" \ --bind="ctrl-l:accept" \ --bind="ctrl-d:half-page-down,ctrl-u:half-page-up" \ --bind="down:preview-half-page-down,up:preview-half-page-up" \ diff --git a/src/sh/api.sh b/src/sh/api.sh index 33ba37d..72e14e1 100644 --- a/src/sh/api.sh +++ b/src/sh/api.sh @@ -4,6 +4,79 @@ USER_AGENT="$APP_NAME/$APP_VERSION ($APP_WEBSITE)" SLEEP_ON_ERROR=1 export MB_BROWSE_STEPS +__mpv_command() { + echo "mpv_command: $*" >>/tmp/foo + printf "{ \"command\": [\"%s\"] }\n" "$1" | $SOCAT - "$MPV_SOCKET" +} + +__mpv_command_with_arg() { + echo "mpv_command_1: $*" >>/tmp/foo + printf "{ \"command\": [\"%s\", \"%s\"] }\n" "$1" "$2" | $SOCAT - "$MPV_SOCKET" +} + +__mpv_command_with_args2() { + echo "mpv_command_2: $*" >>/tmp/foo + printf "{ \"command\": [\"%s\", \"%s\", \"%s\"] }\n" "$1" "$2" "$3" | $SOCAT - "$MPV_SOCKET" +} + +__mpv_get() { + __mpv_command_with_arg "expand-text" "$2" | $JQ -r '.data' +} + +mpv_playlist_count() { + __mpv_get '${playlist/count}' +} + +mpv_playlist_position() { + __mpv_get '${playlist-pos}' +} + +mpv_quit() { + __mpv_command "quit" +} + +mpv_start() { + MPV_SOCKET="$(mktemp --suffix=.sock)" + trap 'mpv_quit; rm -f "$MPV_SOCKET"' EXIT INT + $MPV --no-config --no-terminal --input-ipc-server="$MPV_SOCKET" --idle --no-osc --no-input-default-bindings & +} + +mpv_play_file() { + __mpv_command_with_arg "loadfile" "$1" +} + +mpv_queue_file() { + __mpv_command_with_args2 "loadfile" "$1" "append-play" +} + +mpv_play_list() { + __mpv_command_with_arg "loadlist" "$1" +} + +mpv_queue_list() { + __mpv_command_with_arg "loadlist" "$1" "append-play" +} + +mpv_next() { + __mpv_command "playlist-next" +} + +mpv_prev() { + __mpv_command "playlist-prev" +} + +mpv_seek_forward() { + __mpv_command_with_arg "seek" "10" +} + +mpv_seek_backward() { + __mpv_command_with_arg "seek" "-10" +} + +mpv_toggle_pause() { + __mpv_command_with_arg "cycle" "pause" +} + __api_mb() { tmpout=$(mktemp) for _ in $(seq "$MB_MAX_RETRIES"); do diff --git a/src/sh/tools.sh b/src/sh/tools.sh index d9b3f5b..f847526 100644 --- a/src/sh/tools.sh +++ b/src/sh/tools.sh @@ -21,3 +21,19 @@ else exit 1 fi export JQ + +if command -v "mpv" >/dev/null; then + MPV="mpv" +else + err "Did not find mpv." + exit 1 +fi +export MPV + +if command -v "socat" >/dev/null; then + SOCAT="socat" +else + err "Did not find socat." + exit 1 +fi +export SOCAT