summaryrefslogtreecommitdiffstats
path: root/mplayer.fns
blob: 3247f2cebe28f7bde020fdb4623fa1d4e70bf6e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/zsh
HIST="$HOME/.mplayer.hist"
AUX_INVO=0
POSITION=0
maxplayers=1
network=0
stdin=0
firstime=0
randnum=$((${RANDOM}%${maxplayers}))
mkdir -p /tmp/mplayer
MPSOCKET="/tmp/mplayer/$randnum.fif"
fixedvo=1
typeset -A vo_map
vo_map=(1 fixed-vo 0 nofixed-vo)

pecho(){
    msg="$1"
    file="$2"
    /bin/echo "$msg" >| $file &
    sleep 0.1
    echo < $file
}



check_pattern(){
        pecho "get_property filename" $1
        if tail -2 /tmp/mplayer.log | /bin/grep  'ANS_filename' | /bin/grep -v grep ;then
            pecho "get_property pause" $1
            if tail -2 /tmp/mplayer.log | /bin/grep  'ANS_pause=no' | /bin/grep -v grep ;then
                retval=0
                return 0
            fi
            retval=2
        fi
        [[ -z $retval ]] && retval=1
        return 1
}


parse(){
if [[ -z "$1" ]];then
      fnum=$(awk '{ print NR" "$0 }' $HIST | cut -d ":" -f 1 | uniq -f 1 | tac | ${=DMENU})
      [[ -z "$fnum" ]] && exit
      num=$(echo "$fnum" | cut -d " " -f 1)
      FILE=$(head -${num} $HIST | tail -1 | cut -d ":" -f 1)

      if [[ $fnum == *: ]];then
          name="$(cut -d ' ' -f 1 <<< $FILE)"
          POSITION="$(grep $name $HIST | tail -1 | cut -d ':' -f 2)"
      fi

else
    if [[ $1 == "-" ]];then
        stdin=1
        return
    fi
    if isurl "$(tr -d '.' <<< $1)";then # Fix /a/b/c/d type
        FILE="$1"
        network=1
    else
        if [[ "$@" == /* ]];then
            prefix=
        else
            prefix="$pwdir/"
        fi
        FILE="${prefix}$@"
    fi
fi
retval=$FILE
}

preplay(){
    mpc pause &>/dev/null
    ln -sf $MPSOCKET /tmp/mplayer.fif || exit 1
}

check_status(){
if [[ $fixedvo == 1 ]];then
    pidof mplayer || return 1
    echo >>| /tmp/mplayer.log
    retval=1
    for ff in  /tmp/mplayer/*.fif;do
        if check_pattern $ff;then
            return $retval
        fi
    done
    return $retval
else
    if xdotool search --class mplayer;then
        return 1
    else
        return 0
    fi
fi
}

aux(){
case $1 in
 status)
     check_status
     return $?
    ;;
 clean)
    rm /tmp/mplayer/* 2>/dev/null
    rm /tmp/mplayer.fif 2>/dev/null
    killall mplayer 2>/dev/null
    sleep 1
    killall -9 mplayer &>/dev/null
    rm /tmp/locks/mplayer* 2>/dev/null
    mv /tmp/mplayer.log /tmp/.mplayer.log
    exit
    ;;
 pause)
    pecho 'pause' $MPSOCKET
    ;;
 snext)
     pecho 'seek +10' $MPSOCKET 
     ;;
 sprev)
     pecho 'seek -10' $MPSOCKET 
     ;;
 pnext)
     pecho "pt_step 1"  $MPSOCKET
     ;;
 pprev)
     pecho "pt_step -1"  $MPSOCKET
     ;;
 stop)
    wheret
    echo "$retval" >>| ~/.mplayer.hist
    pecho 'stop'  $MPSOCKET
    echo "#STOPPED ${file}" >>| /tmp/mplayer.log
    ;;
   *)
       return 1
esac
}

wheret(){
    pecho 'get_time_pos'  $MPSOCKET
    pecho 'get_property path'  $MPSOCKET
    sleep 3
    LOG="tail -5 /tmp/mplayer.log"
    position=$(${=LOG} | /bin/grep ANS_TIME_POSITION | head -1 |  cut -d "=" -f 2)
    file=$(${=LOG} | /bin/grep ANS_path | head -1 |  cut -d "=" -f 2)
    retval="$file:$position"
}

# vim: set foldmethod=marker: