Title: Z! Controller
======

Tips:
=====
Please, remember to customize the script before copying it to the
widget. You need only customize the section between markers.

To make the new volume control functional, you have to copy the
icons named zctrl?.ico (where '?' represents a number between 0
and 20) to the same directory where z.exe is stored.


Rexx code:
==========

/* Z! mini controller */

/* -- User configuration Section -------------- */
ZPath = 'E:\MYAPPS\Z\'
/* -- End User Configuration ---------------- */

rc = RxFuncAdd('SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs')
if rc = 0 then call SysLoadFuncs

PosX = Arg(1)
PosY = Arg(2)
PipeName = '\pipe\zmp3'

if DragItem.0 = 0 then
    if PosY >= 50 then do
        select
            when PosX <= 33 then do
                rc = lineout(PipeName, '*pause')
                rc = stream(PipeName, 'C', 'CLOSE')
                call SetVol
            end
            when (PosX > 33) & (PosX <= 66) then do
                rc = lineout(PipeName, '*stop')
                rc = stream(PipeName, 'C', 'CLOSE')
            end
            when PosX > 66 then do
                rc = lineout(PipeName, '*vol+')
                rc = stream(PipeName, 'C', 'CLOSE')
                call SetVol
            end
            otherwise
                nop
        end
    end; else do
        select
            when PosX <= 33 then do
                rc = lineout(PipeName, '*previous')
                rc = stream(PipeName, 'C', 'CLOSE')
                call SetVol
            end
            when (PosX > 33) & (PosX <= 66) then do
                rc = lineout(PipeName, '*next')
                rc = stream(PipeName, 'C', 'CLOSE')
                call SetVol
            end
            when PosX > 66 then do
                rc = lineout(PipeName, '*vol-')
                rc = stream(PipeName, 'C', 'CLOSE')
                call SetVol
            end
            otherwise
                nop
        end
    end
else do
    call LaunchZ
    do F = 1 to DragItem.0
        if (Right(FileSpec('name', DragItem.F), 3) = 'mp3')|,
           (Right(FileSpec('name', DragItem.F), 3) = 'MP3')
        then do
            message = '*play ' || DragItem.F
            rc = lineout(PipeName, message)
            rc = stream(PipeName, 'C', 'CLOSE')
            call SetVol
        end; else do
            message = '*addlist ' || DragItem.F
            rc = lineout(PipeName, message)
            rc = stream(PipeName, 'C', 'CLOSE')
            call SysSleep 1
            message = '*play'
            rc = lineout(PipeName, message)
            rc = stream(PipeName, 'C', 'CLOSE')
            call SetVol
        end
    end F
end

exit

LaunchZ: procedure expose PipeName ZPath
    status = stream(PipeName, 'C', 'OPEN')
    if status \= 'READY:' then call SysOpenObject ZPath||'z.exe', DEFAULT, TRUE
    rc = SysSleep(1)
    return ''

SetVol: procedure expose PipeName ZPath button.
    rc = SysSleep(1)
                rc = lineout(PipeName, '*status volume')
    zvol = linein(PipeName)
                rc = stream(PipeName, 'C', 'CLOSE')
    if zvol \= '' then do
        button.icon = ZPath || 'zctrl' || (zvol % 5) || '.ico'
        button.tooltip = 'Volume: ' || zvol || '%'
    end; else do
        button.icon = ZPath || 'zctrl0.ico'
        button.tooltip = ''
    end
    return ''

