From 9fd2107e6a7469fe7ba51448e4fe195bf54d7d29 Mon Sep 17 00:00:00 2001 From: joe Date: Sat, 15 Jun 2013 15:07:19 -0400 Subject: started project --- Presence/FGConsole.hs | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Presence/FGConsole.hs (limited to 'Presence/FGConsole.hs') diff --git a/Presence/FGConsole.hs b/Presence/FGConsole.hs new file mode 100644 index 00000000..fc1ece65 --- /dev/null +++ b/Presence/FGConsole.hs @@ -0,0 +1,62 @@ +{-# LANGUAGE ForeignFunctionInterface #-} +{-# LANGUAGE ScopedTypeVariables #-} +module FGConsole where + +import Data.Word +import System.Posix.IO +import System.Posix.Types +import Control.Concurrent +-- import GHC.IO.Handle +import Unsafe.Coerce +import Control.Exception as E +-- import Prelude as E +import Control.Monad +import Foreign.C.Error +import Foreign.C + +import Todo +import Debug.Trace +import System.Posix.Signals + +-- c_monitorTTY fd = trace "c_monitorTTY" (return ()) -- (trace "WTF" todo) + +foreign import ccall "monitorTTY" c_monitorTTY :: Fd -> IO () +foreign import ccall "closeTTY" c_closeTTY :: IO () + +monitorTTY :: (Word8 -> IO ()) -> IO (Fd,ThreadId) +monitorTTY handler = do + (rfd,wfd) <- createPipe + thread <- forkIO $ do + let cleanup = do + trace "quitting monitorTTY thread." (return ()) + closeFd wfd `E.catch` \(e::IOException) -> return () + closeFd rfd `E.catch` \(e::IOException) -> return () + c_closeTTY + -- rh <- fdToHandle rfd + c_monitorTTY wfd + let monitor = + (do + threadWaitRead rfd + (cs,cnt) <- fdRead rfd 1 + forM_ cs (handler . unsafeCoerce . trace "read byte") + monitor) + `E.catch` + \(e :: IOException) -> do + err <- getErrno + case () of + _ | err==eAGAIN -> monitor + _ | otherwise -> cleanup + `E.catch` + \(e :: AsyncException) -> cleanup + monitor + return (rfd,thread) + +unmonitorTTY (rfd,thread) = do + closeFd rfd + yield + killThread thread + raiseSignal sigUSR1 + -- threadDelay 1000000 + + +-- vim:ft=haskell: -- cgit v1.2.3