changeset 15:35f5b4a9ba80

mpc: initial import
author David Demelier <markand@malikania.fr>
date Thu, 09 Nov 2017 15:11:19 +0100
parents 4c66316076df
children e15807412421
files bin/dmenu_mpc
diffstat 1 files changed, 185 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/dmenu_mpc	Thu Nov 09 15:11:19 2017 +0100
@@ -0,0 +1,185 @@
+#!/bin/sh
+#
+# dmenu_mpc -- convenient mpc module
+#
+# Copyright (c) 2016-2017 David Demelier <markand@malikania.fr>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+
+TOOL="filesel"
+TOP=$(dirname "$(readlink -f "$0")")
+
+. ${TOP}/../libexec/dmenutools/dmenu.subr
+
+if [ -z "${MPD_HOST}" ] && [ -n "${mpc_host}" ]; then
+	export MPD_HOST="${mpc_host}"
+fi
+if [ -z "${MPD_PORT}" ] && [ -n "${mpc_port}" ]; then
+	export MPD_PORT="${mpc_port}"
+fi
+
+#
+# Get the list of files or directories.
+#
+# Arguments:
+#	- base, the optional base component path.
+#
+dmt_mpc_list()
+{
+	path="/tmp/dmenutools-$(id -u).mpc_list.txt"
+
+	echo "." > "${path}"
+
+	if [ -n "$1" ]; then
+		echo ".." >> "${path}"
+	fi
+
+	list=$(mpc ls "$1" >> "${path}" 2> /dev/null)
+
+	if [ $? -eq 0 ]; then
+		cat ${path} | while read n; do
+			basename "$n"
+		done
+	fi
+
+	rm -f "${path}" > /dev/null 2>&1
+}
+
+#
+# Select a file using dmt_mpc_list in successive dmenu calls.
+#
+dmt_mpc_select()
+{
+	while [ -z "${selection}" ]; do
+		entry="$(dmt_mpc_list "${root}" | dmenu -p "${root}" -l 16)"
+
+		# Abort on empty selection.
+		if [ -z "${entry}" ]; then
+			exit 1
+		fi
+
+		if [ "${entry}" = ".." ]; then
+			parent="$(dirname "${root}")"
+
+			if [ "${parent}" = "." ]; then
+				root=""
+			else
+				root="${parent}"
+			fi
+
+			continue;
+		elif [ "${entry}" = "." ]; then
+			selection="${root}"
+		else
+			#
+			# Selection, in that case we check if the selection is a directory
+			# or a file by checking if the mpc ls on the selection returns
+			# more than one row. This is not the best but I can't find a better
+			# solution at the moment.
+			#
+
+			# Convert to absolute.
+			if [ -n "${root}" ]; then
+				entry="${root}/${entry}"
+			fi
+
+			list=$(mpc ls "${entry}")
+
+			if [ "${entry}" != "${list}" ]; then
+				root="${entry}"
+			else
+				selection="${entry}"
+			fi
+		fi
+	done
+
+	echo "${selection}"
+}
+
+#
+# Wrap a mpc command and execute dmt_fatal in case of errors.
+#
+dmt_mpc_run()
+{
+	path="/tmp/dmenutools-$(id -u).err"
+
+	if ! mpc "$@" > /dev/null 2> "${path}"; then
+		dmt_fatal "$(cat ${path})"
+		exit 1
+	fi
+}
+
+#
+# Echo all menu entries.
+#
+dmt_mpc_menu()
+{
+	echo "add..."
+	echo ""
+	echo "previous"
+	echo "next"
+	echo ""
+	echo "play"
+	echo "pause"
+	echo "stop"
+	echo "clear"
+	echo ""
+	echo "toggle consume"
+	echo "toggle repeat"
+	echo "shuffle queue"
+	echo ""
+	echo "update database"
+}
+
+cmd=$(dmt_mpc_menu | dmenu -p mpd -l 16)
+
+case ${cmd} in
+add*)
+	select="$(dmt_mpc_select)"
+
+	if [ -n "${select}" ]; then
+		dmt_mpc_run add "${select}"
+	fi
+	;;
+previous)
+	dmt_mpc_run prev
+	;;
+next)
+	dmt_mpc_run next
+	;;
+play)
+	dmt_mpc_run play
+	;;
+pause)
+	dmt_mpc_run pause
+	;;
+stop)
+	dmt_mpc_run stop
+	;;
+clear)
+	dmt_mpc_run clear
+	;;
+"toggle consume")
+	dmt_mpc_run consume
+	;;
+"toggle repeat")
+	dmt_mpc_run repeat
+	;;
+shuffle*)
+	dmt_mpc_run shuffle
+	;;
+update*)
+	dmt_mpc_run update
+	;;
+esac