------------------------------------------------------------------------------

-- ClockListener.hs
-- created: Mon Aug 30 13:50:58 JST 2010

------------------------------------------------------------------------------

module Sound.Conductive.ClockListener where

import Sound.Conductive.ClockDisplay
import Graphics.Vty
import Sound.OpenSoundControl

stringDatum :: OSC -> String
stringDatum (Message x [String ys]) = ys

-- decodeClockTimeOSC
--   :: Data.ByteString.Lazy.Internal.ByteString -> String
-- decodeClockTimeOSC bs = stringDatum $  decodeOSC bs

receiveLoop :: UDP -> Graphics.Vty.Vty -> IO b
receiveLoop server vty = do
    osc <- recvFrom server
    -- putStrLn $ stringDatum $ fst osc
    update vty $ pic_for_image $ string def_attr $ stringDatum $ fst osc
    receiveLoop server vty

listener :: Int -> Graphics.Vty.Vty -> IO b
listener port vty = do
    s <- udpServer "127.0.0.1" port
    receiveLoop s vty