-
Notifications
You must be signed in to change notification settings - Fork 1
/
PvdSocket.hs
44 lines (40 loc) · 1.5 KB
/
PvdSocket.hs
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
module PvdSocket (
initSocket,
handleClient
) where
import Control.Concurrent (forkIO)
import Control.Concurrent.STM
import Control.Monad (liftM, liftM2, liftM3, when)
import Network.Socket
import qualified System.IO as IO
import PvdMonad
handleClient conf = do
(socket,dpy,win) <- runPvd conf (liftM3 (,,) getSocket getDpy getWin)
accept socket >>= forkIO . uncurry (processMessages dpy win)
where processMessages dpy win connsock clientaddr = do
connhdl <- socketToHandle connsock IO.ReadMode
IO.hSetBuffering connhdl IO.LineBuffering
messages <- IO.hGetContents connhdl
redraw <- fmap or $ mapM (runPvd conf . handleCmd) (lines messages)
IO.hClose connhdl
when redraw $ runPvd conf notifyChange
initSocket port = withSocketsDo $ do
addrinfos <- getAddrInfo
(Just (defaultHints {addrFlags = [AI_PASSIVE]}))
Nothing (Just port)
let serveraddr = head addrinfos
sock <- socket (addrFamily serveraddr) Stream defaultProtocol
bindSocket sock (addrAddress serveraddr)
listen sock 5
return sock
handleCmd :: String -> Pvd Bool
handleCmd cmd = case words cmd of
["next"] -> modIdx $ (1 +) . snd
["prev"] -> modIdx $ (-1 +) . snd
["last"] -> modIdx $ (-1 +) . fst
["first"] -> modIdx $ \_ -> 0
"playlist":"add":imgs -> modPlaylist (++ imgs)
"playlist":"replace":imgs -> liftM2 (||) (setIdx 0) (setPlaylist imgs)
"playlist":"insert":"0":imgs ->
liftM2 (||) (modPlaylist (imgs ++)) (modIdx $ (length imgs +) . snd)
_ -> return False