Permalink
Browse files

Introduce playd state. Add save-sate & restore-state commands

  • Loading branch information...
1 parent 2bb7730 commit 87011ff22e1e5e5cb64d5d1d56e135c6209f2512 Aldis Berjoza committed Nov 27, 2010
Showing with 70 additions and 8 deletions.
  1. +49 −6 bin/playd.sh
  2. +21 −2 man/man1/playd.1
View
@@ -33,7 +33,7 @@
# 1}}}
# project email: playd@bsdroot.lv
-readonly PLAYD_VERSION='1.15.1'
+readonly PLAYD_VERSION='1.16.0'
readonly PLAYD_NAME="${0##*/}"
readonly PLAYD_FILE_FORMATS='mp3|flac|og[agxmv]|wv|aac|mp[421a]|wav|aif[cf]?|m4[abpr]|ape|mk[av]|avi|mpf|vob|di?vx|mpga?|mov|3gp|wm[av]|midi?'
readonly PLAYD_PLAYLIST_FORMATS='plst?|m3u8?|asx|xspf|ram|qtl|wax|wpl'
@@ -71,6 +71,7 @@ PLAYD_PIPE="${PLAYD_PIPE:-"$PLAYD_HOME/playd.fifo"}"
PLAYD_PLAYLIST="${PLAYD_PLAYLIST:-"$PLAYD_HOME/playlist.plst"}"
PLAYD_FAV_PLAYLIST="${PLAYD_FAV_PLAYLIST:-"$PLAYD_HOME/favourite.plst"}"
PLAYD_LOCK="${PLAYD_LOCK:-"$PLAYD_HOME/mplayer.lock"}"
+PLAYD_POS="${PLAYD_POS:-"$PLAYD_HOME/playlist.pos"}"
PAGER=${PAGER:-more}
FORMAT_SHORTNAMES=${FORMAT_SHORTNAMES:-yes}
@@ -176,7 +177,7 @@ playd_playlist_add() { # {{{1
[ -f "$1" ] || { playd_warn "File doesn't exist:" " $1"; return 1; }
[ $PLAYD_APPEND -eq 1 ] \
&& echo "$1" >> "$PLAYD_PLAYLIST" \
- || echo "$1" > "$PLAYD_PLAYLIST"
+ || { echo "$1" > "$PLAYD_PLAYLIST"; rm -f "$PLAYD_POS"; }
[ $NOPLAY -eq 0 ] && playd_put 'loadfile' "$1" $PLAYD_APPEND
} # 1}}}
@@ -186,7 +187,7 @@ playd_playlist_addlist() { # {{{1
[ -f "$1" ] || { playd_warn "Playlist doesn't exist:" " $1"; return 1; }
[ $PLAYD_APPEND -eq 1 ] \
&& cat "$1" >> "$PLAYD_PLAYLIST" \
- || cat "$1" > "$PLAYD_PLAYLIST"
+ || { cat "$1" > "$PLAYD_PLAYLIST"; rm -f "$PLAYD_POS"; }
[ $NOPLAY -eq 0 ] && playd_put 'loadlist' "$1" $PLAYD_APPEND
} # 1}}}
@@ -325,12 +326,33 @@ playd_cat_playlist() { # {{{1
playd_longcat_playlist() { # {{{1
if [ -f "$PLAYD_PLAYLIST" ]; then
local PADDING=`awk 'END { print length(NR) }' $PLAYD_PLAYLIST`
- awk "/^`playd_current_file_escaped`"'$/ { printf("%0'$PADDING'd|* %s\n", NR, $0); next } /.*/ { printf("%0'$PADDING'd| %s\n", NR, $0) }' "$PLAYD_PLAYLIST"
+ playd_check
+ if [ $? -ne 0 ]; then
+ awk "/^`playd_current_file_escaped`"'$/ { printf("%0'$PADDING'd|* %s\n", NR, $0); next } { printf("%0'$PADDING'd| %s\n", NR, $0) }' "$PLAYD_PLAYLIST"
+ else
+ if [ -f "$PLAYD_POS" ]; then
+ POS=`cat "$PLAYD_POS"`
+ awk 'NR == '$POS' { printf("%0'$PADDING'd|* %s\n", NR, $0); next }; { printf("%0'$PADDING'd| %s\n", NR, $0) }' "$PLAYD_PLAYLIST"
+ else
+ awk '{ printf("%0'$PADDING'd| %s\n", NR, $0) }' "$PLAYD_PLAYLIST"
+ fi
+ fi
else
playd_warn "Default playlist doesn't exist."
fi
} # 1}}}
+playd_save_pos() { # {{{1
+ if [ -f "$PLAYD_PLAYLIST" ]; then
+ CURRENT_SONG=`playd_current_file_escaped`
+ if [ "$CURRENT_SONG" != '' ]; then
+ awk 'BEGIN { printed=0 }; /'"$CURRENT_SONG"'/ && printed == 0 { print NR; printed=1 }' "$PLAYD_PLAYLIST" > "$PLAYD_POS"
+ return 0
+ fi
+ fi
+ return 1
+} # 1}}}
+
# checking for mplayer
[ "`which mplayer`" ] || playd_die 'mplayer not found'
[ -d "$PLAYD_HOME" ] || { mkdir -p "$PLAYD_HOME" || playd_die "Can't create \"$PLAYD_HOME\""; }
@@ -364,10 +386,14 @@ while [ $# -gt 0 ]; do
'rmlist' ) rm -f "$PLAYD_PLAYLIST" ;;
'rnd' | 'randomise' ) mv `playd_randomise "$PLAYD_PLAYLIST"` "$PLAYD_PLAYLIST" ;;
'status' ) playd_check && echo 'playd is not running' || echo "playd is running. PID: $?" ;;
- 'stop') playd_stop ;;
'switch-audio' | 'sw-audio' ) playd_put 'switch_audio' ;;
'switch-subtitles' | 'sw-subs' ) playd_put 'sub_select' ;;
+ 'stop' | 'save-state' | 'save' )
+ playd_save_pos || playd_warn "Failed to save sate. mplayer doesn't seem to have opened file, or no default playlist."
+ [ "$1" = 'stop' ] && { playd_stop; exit; }
+ ;;
+
'cmd' )
[ -n "$2" ] \
&& { playd_put "$2"; shift; } \
@@ -419,7 +445,7 @@ while [ $# -gt 0 ]; do
shift $NOVID
playd_start
;;
-
+
'loop' )
if [ -n $2 ]; then
playd_put 'loop' $2
@@ -434,6 +460,21 @@ while [ $# -gt 0 ]; do
fi
;;
+ 'restore-sate' | 'restore' )
+ if [ -f "$PLAYD_PLAYLIST" ]; then
+ if [ -f "$PLAYD_POS" ]; then
+ playd_put 'loadlist' "$PLAYD_PLAYLIST" 0
+ POS=`cat "$PLAYD_POS"`
+ [ $POS -gt 1 ] && playd_put 'pt_step' $(($POS - 1))
+ else
+ playd_warn "Previous state not saved."
+ fi
+ else
+ playd_warn "Default playlist doesn't exist"
+ rm -f "$PLAYD_POS"
+ fi
+ ;;
+
'play' )
if [ $2 ]; then
if [ $2 -ne 0 ]; then
@@ -547,5 +588,7 @@ while [ $# -gt 0 ]; do
shift
done
+playd_save_pos
+
exit 0
# vim: set ts=4 sw=4 foldminlines=3 fdm=marker:
View
@@ -302,7 +302,7 @@ This will randomise playlist, but won't start playing it... you'll need to run
.Cm playlist
to load new playlist.
.Pp
-Aldis:
+Alias:
.Cm rnd
.\" }}}
.\" restart {{{
@@ -322,12 +322,31 @@ while mplayer doesn't. So if you don't get expected behaviour from
and check if everything works. Only send bug report if same problem appears
after restart. If
.Cm novid
-is set, mplayer will only output sound.
+is set, mplayer will only output sound.
+.\" }}}
+.\" restore-state {{{
+.It Cm restore-state
+Restore previous state if possible and start playing music.
+.Pp
+Alias:
+.Cm restore
.\" }}}
.\" rmlist {{{
.It Cm rmlist
Remove default playlist.
.\" }}}
+.\" save-state {{{
+.It Cm save-state
+Save playd position in playlist (works well only with playlists, that contain
+unique files).
+Note that playd will save current state every time you call playd.
+Also playd will save state when you exit playd with
+.Cm stop
+command
+.Pp
+Alias:
+.Cm save
+.\" }}}
.\" saturation {{{
.It Cm saturation Xo
.Ar value

0 comments on commit 87011ff

Please sign in to comment.