pituicat/src/Affection/Subsystems/AffectionKeyboard.hs

49 lines
1.4 KiB
Haskell
Raw Normal View History

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.Types
2017-12-13 14:19:53 +00:00
import Affection.Util
2017-12-13 14:53:51 +00:00
import Affection.MessageBus
import Affection.Subsystems.Class
2017-12-12 12:10:55 +00:00
2017-12-13 03:37:16 +00:00
import Control.Concurrent.STM as STM
2017-12-13 14:53:51 +00:00
import Control.Monad.IO.Class (liftIO)
2017-12-12 12:10:55 +00:00
import qualified SDL
2017-12-13 14:53:51 +00:00
data AffectionKeyboard us = AffectionKeyboard
{ keyboardSubscribers :: forall us. TVar [(UUID, KeyboardMessage -> Affection us ())]
2017-12-12 12:10:55 +00:00
}
2017-12-13 14:53:51 +00:00
instance Participant (AffectionKeyboard us) 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 14:53:51 +00:00
instance SDLSubsystem (AffectionKeyboard us) KeyboardMessage where
2017-12-13 14:19:53 +00:00
consumeSDLEvents ak eps = doConsume eps
2017-12-12 12:10:55 +00:00
where
2017-12-13 14:19:53 +00:00
doConsume (e:es) = do
ts <- getElapsedTime
case e of
SDL.KeyboardEvent dat -> do
partEmit ak (MsgKeyboardEvent
ts
(SDL.keyboardEventWindow dat)
(SDL.keyboardEventKeyMotion dat)
(SDL.keyboardEventRepeat dat)
(SDL.keyboardEventKeysym dat)
)
doConsume es
_ -> fmap (e :) (doConsume es)