Title: Z! Monitor
======

Tips:
=====
If you want the widget to look correctly, use a fixed-width font.
I'm using 5.SystemVIO, but this may not be the best choice if you run
at a different resolution than mine (I'm running at 1024x768).

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

Non-obvious values:

    ScrollFactor:   It is the number of characters from the song title
                    that will be displayed in the widget. If the title
                    is longer than this, it will begin scrolling.

    TimeWidth:      It is the width of the section containing the
                    running elapsed time. If you use a font different
                    than mine, you will probably have to change this.
                    Setting this up requires a bit of trial-and-error.


Refresh rate: 1000 ms
=============

Gauge width: 100 pixels (fixed)
============

Rexx code: (Gauge script)
=========================

/* Z! Controller - monitor */

/* -- User configuration Section -------------- */
ScrollFactor = 10
TimeWidth = 32
/* -- End User Configuration ---------------- */

PipeName = '\pipe\zmp3'
nl = '0D0A'x

gauge.1 = 0
gauge.3 = 100

status = stream(PipeName, 'C', 'OPEN')
if status \= 'READY:' then do
    rc = stream(PipeName, 'C', 'CLOSE')
    if gauge.user \= '' then do
        gauge.user = ''
        exit
    end
    gauge.text = '(Z! not running)'
    gauge.tooltip = ''
    gauge.user = ''
    exit
end

rc = lineout(PipeName, '*rawinfo')
rawinfo = linein(PipeName)
rc = stream(PipeName, 'C', 'CLOSE')

name = clean(substr(rawinfo, 1, 256))
if name \= '' then do
    songinfo = clean(substr(rawinfo, 257, 256))
    track = clean(substr(rawinfo, 513, 128))
    artist = clean(substr(rawinfo, 641, 128))
    album = clean(substr(rawinfo, 769, 128))

    playtime = clean(substr(rawinfo, 1031, 10))
    timenow = clean(substr(rawinfo, 1041, 10))

    gauge.2 = TimeWidth

    gauge.tooltip = 'track:' track nl||,
                    'artist:' artist nl||,
                    'album:' album nl||,
                    'playing time:' playtime

    if playtime \= '' then do
        parse var playtime mm ':' ss
        splaytime = mm*60+ss
        if timenow \= '' then do
            parse var timenow mm ':' ss
            stimenow = mm*60+ss
            gauge.1 = gauge.2 * stimenow % splaytime
            gauge.text = timenow || ' ' || scroller(track)
        end
    end
end; else do
    gauge.2 = 0
    gauge.text = '(not playing)'
    gauge.user = 1
end

exit

scroller: procedure expose ScrollFactor gauge.
    ScrollText = arg(1)
    ScrollInit = gauge.user
    if (ScrollInit = '') | ,
       (ScrollInit > (length(ScrollText) - ScrollFactor)),
    then
        ScrollInit = 1
    else
        ScrollInit = ScrollInit + 1

    gauge.user = ScrollInit
    return substr(ScrollText, ScrollInit, ScrollFactor)

clean:
    return strip(arg(1), , '0'x)


Rexx code: (Double click action)
================================

/* Double-click action for Z! Controller Monitor */
rc = RxFuncAdd('SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs')
if rc = 0 then call SysLoadFuncs

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

rc = SysOpenObject(ZPath, DEFAULT, TRUE)

