on master help sign in
Use GHC Handles instead of POSIX Fds for output

Ignore-this: bc16788a651ce4c7b5229b80e6cefe83 darcs-hash:20100907180247-f0a0d-05cdfbb6ae1d7b976a7bd75b4265d5596e659163.gz

commitd5c51d264fb72cfdfd374593b4e55fe71512ea3e authorcoreyoconnor <coreyoconnor@gmail.com> dateTue 7 Sep 2010, 11:02 (16 years ago) parentfddf5c9
5 files changed+25−12
src/Graphics/Vty/Terminal/Debug.hs+4 src/Graphics/Vty/Terminal/Generic.hs+6 src/Graphics/Vty/Terminal/MacOSX.hs+2 src/Graphics/Vty/Terminal/TerminfoBased.hs+11−12 src/Graphics/Vty/Terminal/XTermColor.hs+2
src/Graphics/Vty/Terminal/Debug.hs+4
@@ -26,6 +26,8 @@ import Foreign.Marshal.Array ( peekArray )
2626import Foreign.Ptr ( plusPtr )
2727import Foreign.Storable ( poke )
2828
29+import System.IO
30+
2931import Unsafe.Coerce
3032
3133-- | The debug display terminal produces a string representation of the requested picture. There is
@@ -62,6 +64,8 @@ instance Terminal DebugTerminal where
6264 >>= return . UTF8.fromRep . BSCore.pack
6365 >>= writeIORef (debug_terminal_last_output t)
6466
67+ output_handle t = return stdout
68+
6569data DebugDisplay = DebugDisplay
6670 { debug_display_bounds :: DisplayRegion
6771 }
src/Graphics/Vty/Terminal/Generic.hs+6
@@ -26,6 +26,8 @@ import Data.Foldable
2626import Data.IORef
2727import Data.String.UTF8 hiding ( foldl )
2828
29+import System.IO
30+
2931data TerminalHandle where
3032 TerminalHandle :: Terminal t => t -> IORef TerminalState -> TerminalHandle
3133
@@ -82,6 +84,9 @@ class Terminal t where
8284 -- end_ptr - start_ptr
8385 output_byte_buffer :: t -> OutputBuffer -> Word -> IO ()
8486
87+ -- | Handle of output device
88+ output_handle :: t -> IO Handle
89+
8590instance Terminal TerminalHandle where
8691 terminal_ID (TerminalHandle t _) = terminal_ID t
8792 release_terminal (TerminalHandle t _) = release_terminal t
@@ -90,6 +95,7 @@ instance Terminal TerminalHandle where
9095 display_bounds (TerminalHandle t _) = display_bounds t
9196 display_terminal_instance (TerminalHandle t _) = display_terminal_instance t
9297 output_byte_buffer (TerminalHandle t _) = output_byte_buffer t
98+ output_handle (TerminalHandle t _) = output_handle t
9399
94100data DisplayHandle where
95101 DisplayHandle :: DisplayTerminal d => d -> TerminalHandle -> DisplayState -> DisplayHandle
src/Graphics/Vty/Terminal/MacOSX.hs+2
@@ -70,6 +70,8 @@ instance Terminal Term where
7070
7171 output_byte_buffer t = output_byte_buffer (super_term t)
7272
73+ output_handle t = output_handle (super_term t)
74+
7375data DisplayContext = DisplayContext
7476 { super_display :: DisplayHandle
7577 }
src/Graphics/Vty/Terminal/TerminfoBased.hs+11−12
@@ -26,10 +26,10 @@ import Data.Word
2626import Foreign.Ptr
2727import Foreign.C.Types ( CLong, CSize, CInt )
2828
29+import GHC.Handle
30+
2931import qualified System.Console.Terminfo as Terminfo
3032import System.IO
31−import System.Posix.IO
32−import System.Posix.Types ( Fd(..) )
3333
3434data Term = Term
3535 { term_info_ID :: String
@@ -44,7 +44,7 @@ data Term = Term
4444 , set_default_attr :: CapExpression
4545 , clear_screen :: CapExpression
4646 , display_attr_caps :: DisplayAttrCaps
47− , term_fd :: Fd
47+ , term_handle :: Handle
4848 }
4949
5050data DisplayAttrCaps = DisplayAttrCaps
@@ -93,7 +93,7 @@ terminal_instance in_ID = do
9393 case parse_result of
9494 Left e -> fail $ show e
9595 Right cap -> return $ Just cap
96− the_fd <- liftIO $ dup stdOutput
96+ the_handle <- liftIO $ hDuplicate stdout
9797 pure Term
9898 <*> pure in_ID
9999 <*> pure ti
@@ -107,7 +107,7 @@ terminal_instance in_ID = do
107107 <*> require_cap "sgr0"
108108 <*> require_cap "clear"
109109 <*> current_display_attr_caps ti
110− <*> pure the_fd
110+ <*> pure the_handle
111111
112112current_display_attr_caps :: ( Applicative m, MonadIO m )
113113 => Terminfo.Terminal
@@ -137,7 +137,7 @@ instance Terminal Term where
137137 release_terminal t = do
138138 liftIO $ marshall_cap_to_terminal t set_default_attr []
139139 liftIO $ marshall_cap_to_terminal t cnorm []
140− liftIO $ closeFd $ term_fd t
140+ liftIO $ hClose $ term_handle t
141141 return ()
142142
143143 reserve_display t = do
@@ -177,13 +177,12 @@ instance Terminal Term where
177177 -- flush is required *before* the c_output_byte_buffer call
178178 -- otherwise there may still be data in GHC's internal stdout buffer.
179179 -- _ <- handleToFd stdout
180− let Fd c_fd = term_fd t
181− if out_byte_count /= 0
182− then c_output_byte_buffer c_fd out_ptr ( toEnum $! fromEnum out_byte_count )
183− else return ()
180+ hPutBuf (term_handle t) out_ptr (fromEnum out_byte_count)
181+
182+ output_handle t = return (term_handle t)
184183
185−foreign import ccall unsafe "output_buffer.h stdout_output_buffer" c_output_byte_buffer
186− :: CInt -> Ptr Word8 -> CSize -> IO ()
184+-- foreign import ccall unsafe "output_buffer.h stdout_output_buffer" c_output_byte_buffer
185+-- :: CInt -> Ptr Word8 -> CSize -> IO ()
187186foreign import ccall "gwinsz.h c_get_window_size" c_get_window_size
188187 :: IO CLong
189188-- foreign import ccall "fdatasync" c_fdatasync
src/Graphics/Vty/Terminal/XTermColor.hs+2
@@ -63,6 +63,8 @@ instance Terminal XTermColor where
6363
6464 output_byte_buffer t = output_byte_buffer (super_term t)
6565
66+ output_handle t = output_handle (super_term t)
67+
6668data DisplayContext = DisplayContext
6769 { super_display :: DisplayHandle
6870 }