summaryrefslogtreecommitdiffstats
path: root/mad
blob: 3bb640ee92a17f1acb334cd648da2a0ab2066afe (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
#!/bin/zsh
CMD="$1"
ARG="$2"
typeset -A displacement dispatcher
displacement=(prev '-10' next '+10')

dispatcher=(
    slide_mplayer  ' mplayeraux s${cmd}                          '
    slide_mpd      ' mpc seek ${displacement[$cmd]}%             '
    toggle_mplayer ' mplayeraux pause                            '
    toggle_mpd     ' mpc toggle                                  '
    toggle_none    ' none                                        '
    next_mplayer   ' mplayeraux p$cmd                            '
    next_mpd       ' mpc $cmd                                    '
)


function running(){
    ecode=0
    if   mpc status | /bin/grep -q "\[playing\]";then
            retval="mpd"
            return
    fi
    mplayeraux status
    ecode=$?
    if  [[ $ecode == 0 ]];then
            retval="mplayer"
    else
            if pidof -s mplayer;then 
                latest mpd mplayer
                fret=$retval
            else
                fret=mpd
            fi
            [[ $ecode == 1 ]] && fret=mpd
            retval="$fret"
    fi
}

function latest(){
   typeset -a args
    args=($*)
    counter=1
    base=0
    output=""
    for procs in ${(@)args};do
        for pids in $(pidof $procs);do
            #print $pids
            temp=$(stat --printf=%Y /proc/$pids/statm)
            if [[ $temp -ge $base ]];then
                base=$temp;
                #retval=${(P)$counter}
                output=$counter
            fi
        done
        counter=$(( counter+1 ))
    done
    retval=$args[$output]
}


function {toggle,slide,next} {
    which="$0"
    cmd=${1:-next}
    running 
    function="${which}_${retval}"
    ${(e)=dispatcher[$function]}
}


$CMD $ARG