@@ -71,6 +71,13 @@ makeLenses ''InputState
72type InputM a = StateT InputState (ReaderT Input IO) a
72type InputM a = StateT InputState (ReaderT Input IO) a
74+logMsg :: String -> InputM ()
79+ Just h -> liftIO $ hPutStrLn h msg >> hFlush h
74-- this must be run on an OS thread dedicated to this input handling.
81-- this must be run on an OS thread dedicated to this input handling.
75-- otherwise the terminal timing read behavior will block the execution of the lightweight threads.
82-- otherwise the terminal timing read behavior will block the execution of the lightweight threads.
76loopInputProcessor :: InputM ()
83loopInputProcessor :: InputM ()
@@ -85,11 +92,15 @@ addBytesToProcess :: String -> InputM ()
85addBytesToProcess block = unprocessedBytes <>= block
92addBytesToProcess block = unprocessedBytes <>= block
87emit :: Event -> InputM ()
94emit :: Event -> InputM ()
96+ logMsg $ "parsed event: " ++ show event
88−emit event = view eventChannel >>= liftIO . flip writeChan event
97+ view eventChannel >>= liftIO . flip writeChan event
90−-- There should be two versions of this method:
99+-- The timing requirements are assured by the VMIN and VTIME set for the device.
91−-- 1. using VMIN and VTIME when under the threaded runtime
100+--
101+-- Precondition: Under the threaded runtime. Only current use is from a forkOS thread. That case
102+-- satisfies precondition.
92−-- 2. emulating VMIN and VTIME in userspace when under the non-threaded runtime.
103+-- TODO: When under the non-threaded runtime emulate VMIN and VTIME
93readFromDevice :: InputM String
104readFromDevice :: InputM String
94readFromDevice = do
105readFromDevice = do
95 newConfig <- view configRef >>= liftIO . readIORef
106 newConfig <- view configRef >>= liftIO . readIORef
@@ -100,11 +111,13 @@ readFromDevice = do
100 appliedConfig .= newConfig
111 appliedConfig .= newConfig
101 bufferPtr <- use $ inputBuffer.ptr
112 bufferPtr <- use $ inputBuffer.ptr
102 maxBytes <- use $ inputBuffer.size
113 maxBytes <- use $ inputBuffer.size
103− liftIO $ do
114+ stringRep <- liftIO $ do
104 bytesRead <- fdReadBuf fd bufferPtr (fromIntegral maxBytes)
115 bytesRead <- fdReadBuf fd bufferPtr (fromIntegral maxBytes)
105 if bytesRead > 0
116 if bytesRead > 0
106 then fmap (map $ chr . fromIntegral) $ peekArray (fromIntegral bytesRead) bufferPtr
117 then fmap (map $ chr . fromIntegral) $ peekArray (fromIntegral bytesRead) bufferPtr
107 else return []
118 else return []
119+ logMsg $ "input bytes: " ++ show stringRep
109applyTimingConfig :: Fd -> Config -> IO ()
122applyTimingConfig :: Fd -> Config -> IO ()
110applyTimingConfig fd config =
123applyTimingConfig fd config =
@@ -170,9 +183,10 @@ initInputForFd config classifyTable inFd = do
170 <*> pure (writeIORef stopFlag True)
183 <*> pure (writeIORef stopFlag True)
171 <*> newIORef config
184 <*> newIORef config
172 <*> pure inFd
185 <*> pure inFd
186+ <*> maybe (return Nothing)
173− <*> maybe Nothing (\f -> Just <$> openFile f AppendMode)
187+ (\f -> Just <$> openFile f AppendMode)
174− (debugLog config)
188+ (debugLog config)
175− logClassifyTable classifyTable
189+ logClassifyTable input classifyTable
176 _ <- forkOS $ runInputProcessorLoop classifyTable input stopFlag
190 _ <- forkOS $ runInputProcessorLoop classifyTable input stopFlag
177 return input
191 return input