2017-12-12 12:10:55 +00:00
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
2017-12-13 04:05:18 +00:00
|
|
|
{-# LANGUAGE RankNTypes #-}
|
2017-12-12 12:10:55 +00:00
|
|
|
module Affection.Subsystems.AffectionKeyboard where
|
|
|
|
|
|
|
|
import Affection.MessageBus
|
|
|
|
import Affection.Subsystems.Class
|
|
|
|
import Affection.Types
|
|
|
|
|
|
|
|
import Control.Monad.IO.Class (liftIO)
|
2017-12-13 03:37:16 +00:00
|
|
|
import Control.Concurrent.STM as STM
|
2017-12-12 12:10:55 +00:00
|
|
|
|
|
|
|
import qualified SDL
|
|
|
|
|
2017-12-13 04:05:18 +00:00
|
|
|
data AffectionKeyboard sd = AffectionKeyboard
|
|
|
|
{ keyboardSubscribers :: forall sd. TVar [(UUID, KeyboardMessage -> Affection sd ())]
|
2017-12-12 12:10:55 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 04:05:18 +00:00
|
|
|
instance Participant (AffectionKeyboard sd) KeyboardMessage where
|
2017-12-13 03:37:16 +00:00
|
|
|
partSubscribe p funct = do
|
|
|
|
uuid <- genUUID
|
|
|
|
liftIO $ atomically $ modifyTVar' (keyboardSubscribers p) ((uuid, funct) :)
|
|
|
|
return uuid
|
|
|
|
|
|
|
|
partUnSubscribe p uuid =
|
|
|
|
liftIO $ atomically $ modifyTVar' (keyboardSubscribers p)
|
|
|
|
(filter (\(u, _) -> u /= uuid))
|
|
|
|
|
|
|
|
partSubscribers p = do
|
|
|
|
subTups <- liftIO $ readTVarIO $ keyboardSubscribers p
|
|
|
|
return $ map snd subTups
|
2017-12-12 12:10:55 +00:00
|
|
|
|
2017-12-13 04:05:18 +00:00
|
|
|
instance SDLSubsystem (AffectionKeyboard sd) KeyboardMessage where
|
2017-12-12 12:10:55 +00:00
|
|
|
consumeSDLEvents ak evs = doConsume evs
|
|
|
|
where
|
|
|
|
doConsume [] = return []
|
|
|
|
doConsume (e:es) = case SDL.eventPayload e of
|
|
|
|
SDL.KeyboardEvent dat -> do
|
|
|
|
partEmit ak (MsgKeyboardEvent
|
|
|
|
(SDL.eventTimestamp e)
|
|
|
|
(SDL.keyboardEventWindow dat)
|
|
|
|
(SDL.keyboardEventKeyMotion dat)
|
|
|
|
(SDL.keyboardEventRepeat dat)
|
|
|
|
(SDL.keyboardEventKeysym dat)
|
|
|
|
)
|
|
|
|
doConsume es
|
|
|
|
_ -> fmap (e :) (doConsume es)
|