Use STM chans to avoid deprecated interface (fixes #60)

This commit is contained in:
Jonathan Daugherty 2015-08-09 19:22:42 -07:00 committed by Corey O'Connor
parent 60e8ce6535
commit 04ac36ca76
5 changed files with 12 additions and 8 deletions

View File

@ -59,7 +59,7 @@ import Graphics.Vty.Input
import Graphics.Vty.Output import Graphics.Vty.Output
import Graphics.Vty.Picture import Graphics.Vty.Picture
import Control.Concurrent import Control.Concurrent.STM
import Data.IORef import Data.IORef
import Data.Monoid import Data.Monoid
@ -165,7 +165,7 @@ intMkVty input out = do
>> readIORef lastPicRef >> readIORef lastPicRef
>>= maybe ( return () ) ( \pic -> innerUpdate pic ) >>= maybe ( return () ) ( \pic -> innerUpdate pic )
let gkey = do k <- readChan $ _eventChannel input let gkey = do k <- atomically $ readTChan $ _eventChannel input
case k of case k of
(EvResize _ _) -> innerRefresh (EvResize _ _) -> innerRefresh
>> displayBounds out >> displayBounds out

View File

@ -136,7 +136,7 @@ import Graphics.Vty.Input.Events
import Graphics.Vty.Input.Loop import Graphics.Vty.Input.Loop
import Graphics.Vty.Input.Terminfo import Graphics.Vty.Input.Terminfo
import Control.Concurrent import Control.Concurrent.STM
import Control.Lens import Control.Lens
import qualified System.Console.Terminfo as Terminfo import qualified System.Console.Terminfo as Terminfo
@ -193,7 +193,7 @@ inputForConfig config@Config{ termName = Just termName
let pokeIO = Catch $ do let pokeIO = Catch $ do
let e = error "vty internal failure: this value should not propagate to users" let e = error "vty internal failure: this value should not propagate to users"
setAttrs setAttrs
writeChan (input^.eventChannel) (EvResize e e) atomically $ writeTChan (input^.eventChannel) (EvResize e e)
_ <- installHandler windowChange pokeIO Nothing _ <- installHandler windowChange pokeIO Nothing
_ <- installHandler continueProcess pokeIO Nothing _ <- installHandler continueProcess pokeIO Nothing
return $ input return $ input

View File

@ -19,6 +19,7 @@ import Graphics.Vty.Input.Events
import Control.Applicative import Control.Applicative
import Control.Concurrent import Control.Concurrent
import Control.Concurrent.STM
import Control.Exception (mask, try, SomeException) import Control.Exception (mask, try, SomeException)
import Control.Lens import Control.Lens
import Control.Monad (when, mzero, forM_) import Control.Monad (when, mzero, forM_)
@ -43,7 +44,7 @@ import Text.Printf (hPrintf)
data Input = Input data Input = Input
{ -- | Channel of events direct from input processing. Unlike 'nextEvent' this will not refresh { -- | Channel of events direct from input processing. Unlike 'nextEvent' this will not refresh
-- the display if the next event is an 'EvResize'. -- the display if the next event is an 'EvResize'.
_eventChannel :: Chan Event _eventChannel :: TChan Event
-- | Shuts down the input processing. This should return the terminal input state to before -- | Shuts down the input processing. This should return the terminal input state to before
-- the input initialized. -- the input initialized.
, shutdownInput :: IO () , shutdownInput :: IO ()
@ -96,7 +97,7 @@ addBytesToProcess block = unprocessedBytes <>= block
emit :: Event -> InputM () emit :: Event -> InputM ()
emit event = do emit event = do
logMsg $ "parsed event: " ++ show event logMsg $ "parsed event: " ++ show event
view eventChannel >>= liftIO . flip writeChan event view eventChannel >>= liftIO . atomically . flip writeTChan event
-- The timing requirements are assured by the VMIN and VTIME set for the device. -- The timing requirements are assured by the VMIN and VTIME set for the device.
-- --
@ -192,7 +193,7 @@ initInput config classifyTable = do
setFdOption fd NonBlockingRead False setFdOption fd NonBlockingRead False
applyConfig fd config applyConfig fd config
stopSync <- newEmptyMVar stopSync <- newEmptyMVar
input <- Input <$> newChan input <- Input <$> atomically newTChan
<*> pure (return ()) <*> pure (return ())
<*> newIORef config <*> newIORef config
<*> maybe (return Nothing) <*> maybe (return Nothing)

View File

@ -14,6 +14,7 @@ import Graphics.Vty.Input.Terminfo
import Control.Applicative import Control.Applicative
import Control.Concurrent import Control.Concurrent
import Control.Concurrent.STM
import Control.Exception import Control.Exception
import Control.Lens ((^.)) import Control.Lens ((^.))
import Control.Monad import Control.Monad
@ -128,7 +129,7 @@ assertEventsFromSynInput table inputSpec expectedEvents = do
let readEvents = readLoop eventCount let readEvents = readLoop eventCount
readLoop 0 = return () readLoop 0 = return ()
readLoop n = do readLoop n = do
e <- readChan $ input^.eventChannel e <- atomically $ readTChan $ input^.eventChannel
modifyIORef eventsRef ((:) e) modifyIORef eventsRef ((:) e)
readLoop (n - 1) readLoop (n - 1)
genEventsUsingIoActions maxDuration writeWaitClose readEvents genEventsUsingIoActions maxDuration writeWaitClose readEvents

View File

@ -59,6 +59,7 @@ library
mtl >= 1.1.1.0 && < 2.3, mtl >= 1.1.1.0 && < 2.3,
parallel >= 2.2 && < 3.3, parallel >= 2.2 && < 3.3,
parsec >= 2 && < 4, parsec >= 2 && < 4,
stm,
terminfo >= 0.3 && < 0.5, terminfo >= 0.3 && < 0.5,
transformers >= 0.3.0.0, transformers >= 0.3.0.0,
text >= 0.11.3, text >= 0.11.3,
@ -564,6 +565,7 @@ test-suite verify-using-mock-input
deepseq >= 1.1 && < 1.5, deepseq >= 1.1 && < 1.5,
lens >= 3.9.0.2 && < 5.0, lens >= 3.9.0.2 && < 5.0,
mtl >= 1.1.1.0 && < 2.3, mtl >= 1.1.1.0 && < 2.3,
stm,
terminfo >= 0.3 && < 0.5, terminfo >= 0.3 && < 0.5,
text >= 0.11.3, text >= 0.11.3,
unix, unix,