on master help sign in
Invoke write directly instead of via hPutBuf
4f06259coreyoconnor
cbits/output_buffer.cnew file+18
@@ -0,0 +1,18 @@
1+#define _XOPEN_SOURCE 500
2+#include <unistd.h>
3+
4+#include "output_buffer.h"
5+
6+void stdout_output_buffer ( uint8_t* buffer, size_t buffer_size )
7+{
8+ while ( buffer_size )
9+ {
10+ const ssize_t r = write( STDOUT_FILENO, (void*) buffer, buffer_size );
11+
12+ buffer_size -= (size_t) r;
13+ buffer += (size_t) r;
14+ }
15+
16+ fdatasync( STDOUT_FILENO );
17+}
18+
cbits/output_buffer.hnew file+5
@@ -0,0 +1,5 @@
1+#define _XOPEN_SOURCE 500
2+#include <unistd.h>
3+#include <stdint.h>
4+
5+void stdout_output_buffer ( uint8_t* buffer, size_t buffer_size );
src/Graphics/Vty/Terminal/TerminfoBased.hs+12−5
@@ -2,6 +2,7 @@
2{-# LANGUAGE ForeignFunctionInterface #-}
2{-# LANGUAGE ForeignFunctionInterface #-}
3{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE ScopedTypeVariables #-}
4{-# LANGUAGE FlexibleInstances #-}
4{-# LANGUAGE FlexibleInstances #-}
5+{-# OPTIONS_GHC -D_XOPEN_SOURCE=500 #-}
5module Graphics.Vty.Terminal.TerminfoBased ( terminal_instance
6module Graphics.Vty.Terminal.TerminfoBased ( terminal_instance
6 )
7 )
7 where
8 where
@@ -22,7 +23,8 @@ import Data.Bits ( (.&.) )
22import Data.Maybe ( isJust, isNothing, fromJust )
23import Data.Maybe ( isJust, isNothing, fromJust )
23import Data.Word
24import Data.Word
24
25
25−import Foreign.C.Types ( CLong )
26+import Foreign.Ptr
27+import Foreign.C.Types ( CLong, CSize )
26
28
27import qualified System.Console.Terminfo as Terminfo
29import qualified System.Console.Terminfo as Terminfo
28import System.IO
30import System.IO
@@ -164,12 +166,17 @@ instance Terminal Term where
164
166
165 -- Output the byte buffer of the specified size to the terminal device.
167 -- Output the byte buffer of the specified size to the terminal device.
166 output_byte_buffer _t out_ptr out_byte_count = do
168 output_byte_buffer _t out_ptr out_byte_count = do
167− if out_byte_count == 0
169+ -- flush is required *before* the c_output_byte_buffer call
168− then return ()
170+ -- otherwise there may still be data in GHC's internal stdout buffer.
169− else hPutBuf stdout out_ptr (fromEnum out_byte_count)
170 hFlush stdout
171 hFlush stdout
172+ if out_byte_count /= 0
173+ then c_output_byte_buffer out_ptr ( toEnum $! fromEnum out_byte_count )
174+ else return ()
171
175
176+foreign import ccall unsafe "output_buffer.h stdout_output_buffer" c_output_byte_buffer
177+ :: Ptr Word8 -> CSize -> IO ()
172−foreign import ccall "gwinsz.h c_get_window_size" c_get_window_size :: IO CLong
178+foreign import ccall "gwinsz.h c_get_window_size" c_get_window_size
179+ :: IO CLong
173
180
174get_window_size :: IO (Int,Int)
181get_window_size :: IO (Int,Int)
175get_window_size = do
182get_window_size = do
test/Makefile+1−1
@@ -26,7 +26,7 @@ $(VERIF_TESTS)
26$(shell mkdir -p objects )
26$(shell mkdir -p objects )
27
27
28# TODO: Tests should also be buildable referencing the currently installed vty
28# TODO: Tests should also be buildable referencing the currently installed vty
29−GHC_ARGS=--make -i../src -package parallel -package deepseq-1.1.0.0 -hide-package transformers -hide-package monads-fd -hide-package monads-tf -package QuickCheck-2.1.0.2 -ignore-package vty ../cbits/gwinsz.c ../cbits/set_term_timing.c ../cbits/mk_wcwidth.c -O -funbox-strict-fields -Wall -threaded -fno-full-laziness -fspec-constr -odir objects -hidir objects
29+GHC_ARGS=--make -i../src -package parallel -package deepseq-1.1.0.0 -hide-package transformers -hide-package monads-fd -hide-package monads-tf -package QuickCheck-2.1.0.2 -ignore-package vty ../cbits/gwinsz.c ../cbits/output_buffer.c ../cbits/set_term_timing.c ../cbits/mk_wcwidth.c -O -funbox-strict-fields -Wall -threaded -fno-full-laziness -fspec-constr -odir objects -hidir objects
30
30
31GHC_PROF_ARGS=-prof -auto-all $(GHC_ARGS)
31GHC_PROF_ARGS=-prof -auto-all $(GHC_ARGS)
32
32
vty.cabal+2
@@ -59,6 +59,8 @@ other-modules: Codec.Binary.UTF8.Width
59C-Sources: cbits/gwinsz.c
59C-Sources: cbits/gwinsz.c
60 cbits/set_term_timing.c
60 cbits/set_term_timing.c
61 cbits/mk_wcwidth.c
61 cbits/mk_wcwidth.c
62+ cbits/output_buffer.c
63+
62Include-Dirs: cbits
64Include-Dirs: cbits
63hs-source-dirs: src
65hs-source-dirs: src
64Extra-Source-Files: test/Makefile
66Extra-Source-Files: test/Makefile