[plt-scheme] Another MysterX patch: GetActiveObject functionality
Following-up to a long-standing request...
Here's a servlet I sometimes use. It lets me play iTunes at my desk
but control it remotely from my phone using the phone's Web server as
the interface. (Use case: start up the music, then carry a baby
around w/ one hand and change what's playing using the phone browser
with the other....) I've kept the UI very simple to make the code
easy to understand.
Tested/used w/ PLT v4.1.1.
Exercises for extending the code:
- The use of state here means the volume in the servlet may be
inconsistent with that on iTunes; rather than remember it, always ask
iTunes for the latest value and increment/decrement that.
- Add support for playlists, which you can obtain with
(define libsrc (com-get-property iTunes "LibrarySource"))
(define playlists (com-get-property libsrc "Playlists"))
- Reduce footprint by using Filipe Cabecinhas's cgao/coclass
(com-get-active-object-from-coclass).
- This version expects you to set up iTunes on your desktop. (In
particular, Play/Pause/Stop don't mean quite what they ought to mean;
I had some difficulty reconstructing a precise spec.) Enable more of
it from the Web interface.
- Add basic security, like a passcode on startup (printed at terminal,
must be entered by Web client). This way, even if your neighbor hacks
into your house network, steals all your money, changes your locks,
and performs illicit activities using your identity, at least they
can't change what's currently playing on your speakers.
- Support more of iTunes! (There are some bugs w/ the current MysterX
that inhibit it from supporting all of it, or we could have a much
better interface to iTunes than iTunes itself.)
Shriram
----------------------------------------------------------------------
#lang scheme/base
(require web-server/servlet)
(provide (all-defined-out))
(define interface-version 'v1)
(define timeout +inf.0)
(require mysterx)
(require (lib "class.ss"))
(define iTunes (cci/coclass "iTunes Class"))
(define current-volume (com-get-property iTunes "SoundVolume"))
(define (play)
(com-invoke iTunes "Play"))
(define (stop)
(com-invoke iTunes "Stop"))
(define (next-track)
(com-invoke iTunes "NextTrack"))
(define (previous-track)
(com-invoke iTunes "PreviousTrack"))
(define (incr-volume)
(set! current-volume (min 100 (+ current-volume 5)))
(com-set-property! iTunes "SoundVolume" current-volume))
(define (decr-volume)
(set! current-volume (max 0 (- current-volume 5)))
(com-set-property! iTunes "SoundVolume" current-volume))
(define (display-loop)
(send/suspend/dispatch
(lambda (embed/url)
(define (make-button title callback)
`(a ([href ,(embed/url (lambda (req)
(callback)
(display-loop)))])
,title))
`(html
(head (title "iTunes Player"))
(body
(p ,(make-button "Play" play)
nbsp nbsp
,(make-button "Stop" stop))
(p ,(make-button "<-" previous-track)
nbsp nbsp
,(make-button "->" next-track))
(p ,(make-button "-" decr-volume)
nbsp nbsp
,(number->string current-volume)
nbsp nbsp
,(make-button "+" incr-volume)))))))
(define (start initial-request)
(display-loop))
----------------------------------------------------------------------