on master help sign in
Use GHC Handles instead of POSIX Fds for output
d5c51d2coreyoconnor
src/Graphics/Vty/Terminal/Debug.hs+4
@@ -26,6 +26,8 @@ import Foreign.Marshal.Array ( peekArray )
26import Foreign.Ptr ( plusPtr )
26import Foreign.Ptr ( plusPtr )
27import Foreign.Storable ( poke )
27import Foreign.Storable ( poke )
28
28
29+import System.IO
30+
29import Unsafe.Coerce
31import Unsafe.Coerce
30
32
31-- | The debug display terminal produces a string representation of the requested picture. There is
33-- | The debug display terminal produces a string representation of the requested picture. There is
@@ -62,6 +64,8 @@ instance Terminal DebugTerminal where
62 >>= return . UTF8.fromRep . BSCore.pack
64 >>= return . UTF8.fromRep . BSCore.pack
63 >>= writeIORef (debug_terminal_last_output t)
65 >>= writeIORef (debug_terminal_last_output t)
64
66
67+ output_handle t = return stdout
68+
65data DebugDisplay = DebugDisplay
69data DebugDisplay = DebugDisplay
66 { debug_display_bounds :: DisplayRegion
70 { debug_display_bounds :: DisplayRegion
67 }
71 }
src/Graphics/Vty/Terminal/Generic.hs+6
@@ -26,6 +26,8 @@ import Data.Foldable
26import Data.IORef
26import Data.IORef
27import Data.String.UTF8 hiding ( foldl )
27import Data.String.UTF8 hiding ( foldl )
28
28
29+import System.IO
30+
29data TerminalHandle where
31data TerminalHandle where
30 TerminalHandle :: Terminal t => t -> IORef TerminalState -> TerminalHandle
32 TerminalHandle :: Terminal t => t -> IORef TerminalState -> TerminalHandle
31
33
@@ -82,6 +84,9 @@ class Terminal t where
82 -- end_ptr - start_ptr
84 -- end_ptr - start_ptr
83 output_byte_buffer :: t -> OutputBuffer -> Word -> IO ()
85 output_byte_buffer :: t -> OutputBuffer -> Word -> IO ()
84
86
87+ -- | Handle of output device
88+ output_handle :: t -> IO Handle
89+
85instance Terminal TerminalHandle where
90instance Terminal TerminalHandle where
86 terminal_ID (TerminalHandle t _) = terminal_ID t
91 terminal_ID (TerminalHandle t _) = terminal_ID t
87 release_terminal (TerminalHandle t _) = release_terminal t
92 release_terminal (TerminalHandle t _) = release_terminal t
@@ -90,6 +95,7 @@ instance Terminal TerminalHandle where
90 display_bounds (TerminalHandle t _) = display_bounds t
95 display_bounds (TerminalHandle t _) = display_bounds t
91 display_terminal_instance (TerminalHandle t _) = display_terminal_instance t
96 display_terminal_instance (TerminalHandle t _) = display_terminal_instance t
92 output_byte_buffer (TerminalHandle t _) = output_byte_buffer t
97 output_byte_buffer (TerminalHandle t _) = output_byte_buffer t
98+ output_handle (TerminalHandle t _) = output_handle t
93
99
94data DisplayHandle where
100data DisplayHandle where
95 DisplayHandle :: DisplayTerminal d => d -> TerminalHandle -> DisplayState -> DisplayHandle
101 DisplayHandle :: DisplayTerminal d => d -> TerminalHandle -> DisplayState -> DisplayHandle
src/Graphics/Vty/Terminal/MacOSX.hs+2
@@ -70,6 +70,8 @@ instance Terminal Term where
70
70
71 output_byte_buffer t = output_byte_buffer (super_term t)
71 output_byte_buffer t = output_byte_buffer (super_term t)
72
72
73+ output_handle t = output_handle (super_term t)
74+
73data DisplayContext = DisplayContext
75data DisplayContext = DisplayContext
74 { super_display :: DisplayHandle
76 { super_display :: DisplayHandle
75 }
77 }
src/Graphics/Vty/Terminal/TerminfoBased.hs+11−12
@@ -26,10 +26,10 @@ import Data.Word
26import Foreign.Ptr
26import Foreign.Ptr
27import Foreign.C.Types ( CLong, CSize, CInt )
27import Foreign.C.Types ( CLong, CSize, CInt )
28
28
29+import GHC.Handle
30+
29import qualified System.Console.Terminfo as Terminfo
31import qualified System.Console.Terminfo as Terminfo
30import System.IO
32import System.IO
31−import System.Posix.IO
32−import System.Posix.Types ( Fd(..) )
33
33
34data Term = Term
34data Term = Term
35 { term_info_ID :: String
35 { term_info_ID :: String
@@ -44,7 +44,7 @@ data Term = Term
44 , set_default_attr :: CapExpression
44 , set_default_attr :: CapExpression
45 , clear_screen :: CapExpression
45 , clear_screen :: CapExpression
46 , display_attr_caps :: DisplayAttrCaps
46 , display_attr_caps :: DisplayAttrCaps
47− , term_fd :: Fd
47+ , term_handle :: Handle
48 }
48 }
49
49
50data DisplayAttrCaps = DisplayAttrCaps
50data DisplayAttrCaps = DisplayAttrCaps
@@ -93,7 +93,7 @@ terminal_instance in_ID = do
93 case parse_result of
93 case parse_result of
94 Left e -> fail $ show e
94 Left e -> fail $ show e
95 Right cap -> return $ Just cap
95 Right cap -> return $ Just cap
96− the_fd <- liftIO $ dup stdOutput
96+ the_handle <- liftIO $ hDuplicate stdout
97 pure Term
97 pure Term
98 <*> pure in_ID
98 <*> pure in_ID
99 <*> pure ti
99 <*> pure ti
@@ -107,7 +107,7 @@ terminal_instance in_ID = do
107 <*> require_cap "sgr0"
107 <*> require_cap "sgr0"
108 <*> require_cap "clear"
108 <*> require_cap "clear"
109 <*> current_display_attr_caps ti
109 <*> current_display_attr_caps ti
110− <*> pure the_fd
110+ <*> pure the_handle
111
111
112current_display_attr_caps :: ( Applicative m, MonadIO m )
112current_display_attr_caps :: ( Applicative m, MonadIO m )
113 => Terminfo.Terminal
113 => Terminfo.Terminal
@@ -137,7 +137,7 @@ instance Terminal Term where
137 release_terminal t = do
137 release_terminal t = do
138 liftIO $ marshall_cap_to_terminal t set_default_attr []
138 liftIO $ marshall_cap_to_terminal t set_default_attr []
139 liftIO $ marshall_cap_to_terminal t cnorm []
139 liftIO $ marshall_cap_to_terminal t cnorm []
140− liftIO $ closeFd $ term_fd t
140+ liftIO $ hClose $ term_handle t
141 return ()
141 return ()
142
142
143 reserve_display t = do
143 reserve_display t = do
@@ -177,13 +177,12 @@ instance Terminal Term where
177 -- flush is required *before* the c_output_byte_buffer call
177 -- flush is required *before* the c_output_byte_buffer call
178 -- otherwise there may still be data in GHC's internal stdout buffer.
178 -- otherwise there may still be data in GHC's internal stdout buffer.
179 -- _ <- handleToFd stdout
179 -- _ <- 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 )
180+ hPutBuf (term_handle t) out_ptr (fromEnum out_byte_count)
183− else return ()
181+
182+ output_handle t = return (term_handle t)
184
183
185−foreign import ccall unsafe "output_buffer.h stdout_output_buffer" c_output_byte_buffer
184+-- foreign import ccall unsafe "output_buffer.h stdout_output_buffer" c_output_byte_buffer
186− :: CInt -> Ptr Word8 -> CSize -> IO ()
185+-- :: CInt -> Ptr Word8 -> CSize -> IO ()
187foreign import ccall "gwinsz.h c_get_window_size" c_get_window_size
186foreign import ccall "gwinsz.h c_get_window_size" c_get_window_size
188 :: IO CLong
187 :: IO CLong
189-- foreign import ccall "fdatasync" c_fdatasync
188-- foreign import ccall "fdatasync" c_fdatasync
src/Graphics/Vty/Terminal/XTermColor.hs+2
@@ -63,6 +63,8 @@ instance Terminal XTermColor where
63
63
64 output_byte_buffer t = output_byte_buffer (super_term t)
64 output_byte_buffer t = output_byte_buffer (super_term t)
65
65
66+ output_handle t = output_handle (super_term t)
67+
66data DisplayContext = DisplayContext
68data DisplayContext = DisplayContext
67 { super_display :: DisplayHandle
69 { super_display :: DisplayHandle
68 }
70 }