@@ -71,6 +71,13 @@ makeLenses ''InputState
7171
7272type InputM a = StateT InputState (ReaderT Input IO) a
7373
74+logMsg :: String -> InputM ()
75+logMsg msg = do
76+ d <- view inputDebug
77+ case d of
78+ Nothing -> return ()
79+ Just h -> liftIO $ hPutStrLn h msg >> hFlush h
80+
7481-- this must be run on an OS thread dedicated to this input handling.
7582-- otherwise the terminal timing read behavior will block the execution of the lightweight threads.
7683loopInputProcessor :: InputM ()
@@ -85,11 +92,15 @@ addBytesToProcess :: String -> InputM ()
8592addBytesToProcess block = unprocessedBytes <>= block
8693
8794emit :: Event -> InputM ()
88−emit event = view eventChannel >>= liftIO . flip writeChan event
95+emit event = do
96+ logMsg $ "parsed event: " ++ show event
97+ view eventChannel >>= liftIO . flip writeChan event
8998
90−-- There should be two versions of this method:
91−-- 1. using VMIN and VTIME when under the threaded runtime
92−-- 2. emulating VMIN and VTIME in userspace when under the non-threaded runtime.
99+-- The timing requirements are assured by the VMIN and VTIME set for the device.
100+--
101+-- Precondition: Under the threaded runtime. Only current use is from a forkOS thread. That case
102+-- satisfies precondition.
103+-- TODO: When under the non-threaded runtime emulate VMIN and VTIME
93104readFromDevice :: InputM String
94105readFromDevice = do
95106 newConfig <- view configRef >>= liftIO . readIORef
@@ -100,11 +111,13 @@ readFromDevice = do
100111 appliedConfig .= newConfig
101112 bufferPtr <- use $ inputBuffer.ptr
102113 maxBytes <- use $ inputBuffer.size
103− liftIO $ do
114+ stringRep <- liftIO $ do
104115 bytesRead <- fdReadBuf fd bufferPtr (fromIntegral maxBytes)
105116 if bytesRead > 0
106117 then fmap (map $ chr . fromIntegral) $ peekArray (fromIntegral bytesRead) bufferPtr
107118 else return []
119+ logMsg $ "input bytes: " ++ show stringRep
120+ return stringRep
108121
109122applyTimingConfig :: Fd -> Config -> IO ()
110123applyTimingConfig fd config =
@@ -170,9 +183,10 @@ initInputForFd config classifyTable inFd = do
170183 <*> pure (writeIORef stopFlag True)
171184 <*> newIORef config
172185 <*> pure inFd
173− <*> maybe Nothing (\f -> Just <$> openFile f AppendMode)
174− (debugLog config)
175− logClassifyTable classifyTable
186+ <*> maybe (return Nothing)
187+ (\f -> Just <$> openFile f AppendMode)
188+ (debugLog config)
189+ logClassifyTable input classifyTable
176190 _ <- forkOS $ runInputProcessorLoop classifyTable input stopFlag
177191 return input
178192