Skip to content

Drop read thread #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions System/Console/Haskeline/Backend/Win32.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,23 @@ eventReader :: HANDLE -> IO [Event]
eventReader h = do
let waitTime = 500 -- milliseconds
ret <- c_WaitForSingleObject h waitTime
yield -- otherwise, the above foreign call causes the loop to never
-- respond to the killThread
if ret /= (#const WAIT_OBJECT_0)
then eventReader h
else do
es <- readEvents h
return $ combineSurrogatePairs $ mapMaybe processEvent es

isSurrogatePair :: Char -> Char -> Maybe Char
isSurrogatePair c1 c2
| 0xD800 <= ord c1 && ord c1 < 0xDC00 && 0xDC00 <= ord c2 && ord c2 < 0xE000
= Just $ (((ord c1 .&. 0x3FF) `shiftL` 10) .|. (ord c2 .&. 0x3FF)) + 0x10000
| otherwise
= Nothing

combineSurrogatePairs :: [Event] -> [Event]
combineSurrogatePairs (KeyInput [Key m1 (KeyChar c1)] : KeyInput [Key _ (KeyChar c2)] : es)
| 0xD800 <= ord c1 && ord c1 < 0xDC00 && 0xDC00 <= ord c2 && ord c2 < 0xE000
= let c = (((ord c1 .&. 0x3FF) `shiftL` 10) .|. (ord c2 .&. 0x3FF)) + 0x10000
in KeyInput [Key m1 (KeyChar (chr c))] : combineSurrogatePairs es
| Just c <- isSurrogatePair c1 c2
= KeyInput [Key m1 (KeyChar (chr c))] : combineSurrogatePairs es
combineSurrogatePairs (e:es) = e : combineSurrogatePairs es
combineSurrogatePairs [] = []

Expand Down
17 changes: 8 additions & 9 deletions System/Console/Haskeline/Term.hs
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,17 @@ keyEventLoop readEvents eventChan = do
isEmpty <- atomically $ isEmptyTChan eventChan
if not isEmpty
then atomically $ readTChan eventChan
else do
tid <- forkIO $ handleErrorEvent readerLoop
atomically (readTChan eventChan) `finally` killThread tid
else handleErrorEvent readerLoop
where
readerLoop :: IO Event
readerLoop = do
es <- readEvents
if null es
then readerLoop
else atomically $ mapM_ (writeTChan eventChan) es
handleErrorEvent = handle $ \e -> case fromException e of
Just ThreadKilled -> return ()
_ -> atomically $ writeTChan eventChan (ErrorEvent e)
case es of
[] -> readerLoop
e : rest -> do atomically $ mapM_ (writeTChan eventChan) rest
return e

handleErrorEvent = handle $ \e -> return (ErrorEvent e)

saveKeys :: TChan Event -> [Key] -> IO ()
saveKeys ch = atomically . writeTChan ch . KeyInput
Expand Down