Skip to content

Commit

Permalink
Add play next
Browse files Browse the repository at this point in the history
  • Loading branch information
joelpurra committed Oct 9, 2014
1 parent 247d1b2 commit 36259ca
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 13 deletions.
8 changes: 8 additions & 0 deletions src/play-next.sh
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -e

source "${BASH_SOURCE%/*}/play-shared-functions.sh"
source "${BASH_SOURCE%/*}/play-shared-functionality.sh"
source "${BASH_SOURCE%/*}/play-shared-functionality-mutexed.sh"

killExternalPlayerIfRunning
3 changes: 3 additions & 0 deletions src/play-shared-functionality.sh
Expand Up @@ -26,3 +26,6 @@ sharedDaemonPidFile="$sharedDefaultDaemonPidFile"

readonly sharedDefaultPlayerPidFile="${sharedConfigFolder}/.playerpidfile~"
sharedPlayerPidFile="$sharedDefaultPlayerPidFile"

readonly sharedDefaultExternalPlayerPidFile="${sharedConfigFolder}/.externalplayerpidfile~"
sharedExternalPlayerPidFile="$sharedDefaultExternalPlayerPidFile"
100 changes: 93 additions & 7 deletions src/play-shared-functions.sh
Expand Up @@ -81,8 +81,36 @@ playOrder() {
fi
}

cleanupExternalPlayer() {
if [[ -e "$sharedExternalPlayerPidFile" ]];
then
rm "$sharedExternalPlayerPidFile"
fi
}

killExternalPlayer() {
if [[ -s "$sharedExternalPlayerPidFile" ]];
then
killPidFromFile "$sharedExternalPlayerPidFile"
fi
}

killExternalPlayerIfRunning() {
isValidPidFile "$sharedExternalPlayerPidFile" && isPidRunningFromFile "$sharedExternalPlayerPidFile" && killExternalPlayer
cleanupExternalPlayer
}

playSound() {
afplay "$@"
# TODO: use dynamic index?
local index=999
exitIfAlreadyRunning "$sharedExternalPlayerPidFile" "externalplayer"
afplay "$@" &
local externalplayerPid="$!"
savePidAtIndexButDeleteOnExit "$index" "externalplayer" "$externalplayerPid" "$sharedExternalPlayerPidFile"
wait "$externalplayerPid" &>/dev/null
killExternalPlayerIfRunning
cleanupExternalPlayer
clearPidAtIndex "$index"
}

highlight() {
Expand Down Expand Up @@ -127,19 +155,19 @@ exitIfAlreadyRunning() {
# return 0
# }

savePidButDeleteOnExit() {
local name="$1"
local pid="$2"
local pidFile="$3"
savePidAtIndexButDeleteOnExit() {
local index="$1"
local name="$2"
local pid="$3"
local pidFile="$4"
[[ -e "$pidFile" ]] && die "${pid} tried to save ${pidFile} but it already exists and contains $(cat "${pidFile}")"
debug "creating ${name} ${pid} ${pidFile}"
echo -n "$pid" >"$pidFile"
debug "created ${name} ${pid} ${pidFile} ($(cat "${pidFile}"))"

local index="${#pidFilesCreatedByThisInstance[@]}"
pidFilesCreatedByThisInstance[index]="$pidFile"
pidsCreatedByThisInstance[index]="$pid"
pidMessagesCreatedByThisInstance+=("(${name} $pid $pidFile)")
pidMessagesCreatedByThisInstance[index]="(${name} $pid $pidFile)"
debug "${#pidFilesCreatedByThisInstance[@]} pidFilesCreatedByThisInstance: ${pidFilesCreatedByThisInstance[@]}"
debug "${#pidsCreatedByThisInstance[@]} pidsCreatedByThisInstance: ${pidsCreatedByThisInstance[@]}"
debug "${#pidMessagesCreatedByThisInstance[@]} pidMessagesCreatedByThisInstance: ${pidMessagesCreatedByThisInstance[@]}"
Expand All @@ -148,6 +176,25 @@ savePidButDeleteOnExit() {
return 0
}

clearPidAtIndex() {
local index="$1"

unset pidFilesCreatedByThisInstance[index]
unset pidsCreatedByThisInstance[index]
unset pidMessagesCreatedByThisInstance[index]
debug "${#pidFilesCreatedByThisInstance[@]} pidFilesCreatedByThisInstance: ${pidFilesCreatedByThisInstance[@]}"
debug "${#pidsCreatedByThisInstance[@]} pidsCreatedByThisInstance: ${pidsCreatedByThisInstance[@]}"
debug "${#pidMessagesCreatedByThisInstance[@]} pidMessagesCreatedByThisInstance: ${pidMessagesCreatedByThisInstance[@]}"

# echo "$index"
return 0
}

savePidButDeleteOnExit() {
local index="${#pidFilesCreatedByThisInstance[@]}"
savePidAtIndexButDeleteOnExit "$index" "$1" "$2" "$3"
}

killPid() {
local pid="$1"

Expand All @@ -171,6 +218,18 @@ killPidChildren() {
debug "killed '${pid}' children '${children[@]}'"
}

isPidRunning() {
local pid="$1"
local psInfo=$(ps -o pid= -p "${pid}" | awk '{ print $1 }')

if (( psInfo == pid ));
then
return 0
else
return 1
fi
}

pidFromFile() {
local pidFile="$1"

Expand All @@ -183,6 +242,33 @@ pidFromFile() {
fi
}

isValidPidFile() {
local pidFile="$1"

if [[ -s "$pidFile" ]];
then
local pid=$(pidFromFile "$pidFile")

if (( pid > 0 ));
then
return 0
else
return 1
fi
else
return 2
fi
}

isPidRunningFromFile() {
local pidFile="$1"
local pid=$(pidFromFile "$pidFile")

[[ -z "${pid}" ]] && die "could not get the pid to check if it's running."

isPidRunning "$pid"
}

killPidFromFile() {
local pidFile="$1"
local pid=$(pidFromFile "$pidFile")
Expand Down
8 changes: 2 additions & 6 deletions src/play-start.sh
Expand Up @@ -4,12 +4,8 @@ set -e
source "${BASH_SOURCE%/*}/play-shared-functions.sh"
source "${BASH_SOURCE%/*}/play-shared-functionality.sh"

if [[ "$1" == "--wait" ]];
then
shift
thisInstanceIsAChild="${thisInstanceIsAChild:-0}"
thisInstanceIsAChild="$(( thisInstanceIsAChild + 1 ))"
fi
thisInstanceIsAChild="${thisInstanceIsAChild:-0}"
thisInstanceIsAChild="$(( thisInstanceIsAChild + 1 ))"

exitIfAlreadyRunning "$sharedPlayerPidFile" "player"
savePidButDeleteOnExit "player" "$$" "$sharedPlayerPidFile"
Expand Down

0 comments on commit 36259ca

Please sign in to comment.