on 71cd98f help sign in
vty/src/Graphics/Vty.hs
148 lines 5.9 kB history raw
1-- Copyright 2009-2010 Corey O'Connor
2{-# LANGUAGE ForeignFunctionInterface, BangPatterns, UnboxedTuples #-}
3{-# CFILES gwinsz.c #-}
5-- Good sources of documentation for terminal programming are:
6-- vt100 control sequences: http://vt100.net/docs/vt100-ug/chapter3.html#S3.3.3
7-- Xterm control sequences: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
9module Graphics.Vty ( Vty(..)
10 , mkVty
11 , mkVtyEscDelay
12 , module Graphics.Vty.Terminal
13 , module Graphics.Vty.Picture
14 , module Graphics.Vty.DisplayRegion
15 , Key(..)
16 , Modifier(..)
17 , Button(..)
18 , Event(..)
19 )
20 where
23import Graphics.Vty.Terminal
24import Graphics.Vty.Picture
25import Graphics.Vty.DisplayRegion
26import Graphics.Vty.LLInput
28import Data.IORef
30import qualified System.Console.Terminfo as Terminfo
32-- | The main object. At most one should be created.
33-- An alternative is to use unsafePerformIO to automatically create a singleton Vty instance when
34-- required.
35--
36-- This does not assure any thread safety. In theory, as long as an update action is not executed
37-- when another update action is already then it's safe to call this on multiple threads.
38--
39-- todo: Once the Terminal interface encompasses input this interface will be deprecated.
40-- Currently, just using the Terminal interface there is no support for input events.
41data Vty = Vty
42 { -- | Outputs the given Picture. Equivalent to output_picture applied to a display context
43 -- implicitly managed by Vty.
44 update :: Picture -> IO ()
45 -- | Get one Event object, blocking if necessary.
46 , next_event :: IO Event
47 -- | Handle to the terminal interface. See `Terminal`
48 --
49 -- The use of Vty typically follows this process:
50 --
51 -- 0. initialize vty
52 --
53 -- 1. use the update equation of Vty to display a picture
54 --
55 -- 2. repeat
56 --
57 -- 3. shutdown vty.
58 --
59 -- todo: provide a similar abstraction to Graphics.Vty.Terminal for input. Use haskeline's
60 -- input backend for implementation.
61 --
62 -- todo: remove explicit `shutdown` requirement.
63 , terminal :: TerminalHandle
64 -- | Refresh the display. Normally the library takes care of refreshing. Nonetheless, some
65 -- other program might output to the terminal and mess the display. In that case the user
66 -- might want to force a refresh.
67 , refresh :: IO ()
68 -- | Clean up after vty.
69 , shutdown :: IO ()
70 }
72-- | Set up the state object for using vty. At most one state object should be
73-- created at a time.
74mkVty :: IO Vty
75mkVty = mkVtyEscDelay 0
77-- | Set up the state object for using vty. At most one state object should be
78-- created at a time. The delay, in microseconds, specifies the period of time to wait for a key
79-- following reading ESC from the terminal before considering the ESC key press as a discrete event.
80mkVtyEscDelay :: Int -> IO Vty
81mkVtyEscDelay escDelay = do
82 term_info <- Terminfo.setupTermFromEnv
83 t <- terminal_handle
84 reserve_display t
85 (kvar, endi) <- initTermInput escDelay term_info
86 intMkVty kvar ( endi >> release_display t >> release_terminal t ) t
88intMkVty :: IO Event -> IO () -> TerminalHandle -> IO Vty
89intMkVty kvar fend t = do
90 last_pic_ref <- newIORef Nothing
91 last_update_ref <- newIORef Nothing
93 let inner_update in_pic = do
94 b <- display_bounds t
95 let DisplayRegion w h = b
96 cursor = pic_cursor in_pic
97 in_pic' = case cursor of
98 Cursor x y ->
99 let
100 x' = case x of
101 _ | x >= 0x80000000 -> 0
102 | x >= w -> w - 1
103 | otherwise -> x
104 y' = case y of
105 _ | y >= 0x80000000 -> 0
106 | y >= h -> h - 1
107 | otherwise -> y
108 in in_pic { pic_cursor = Cursor x' y' }
109 _ -> in_pic
110 mlast_update <- readIORef last_update_ref
111 update_data <- case mlast_update of
112 Nothing -> do
113 d <- display_context t b
114 output_picture d in_pic'
115 return (b, d)
116 Just (last_bounds, last_context) -> do
117 if b /= last_bounds
118 then do
119 d <- display_context t b
120 output_picture d in_pic'
121 return (b, d)
122 else do
123 output_picture last_context in_pic'
124 return (b, last_context)
125 writeIORef last_update_ref $ Just update_data
126 writeIORef last_pic_ref $ Just in_pic'
128 let inner_refresh
129 = writeIORef last_update_ref Nothing
130 >> readIORef last_pic_ref
131 >>= maybe ( return () ) ( \pic -> inner_update pic )
133 let gkey = do k <- kvar
134 case k of
135 (EvResize _ _) -> inner_refresh
136 >> display_bounds t
137 >>= return . ( \(DisplayRegion w h)
138 -> EvResize (fromEnum w) (fromEnum h)
139 )
140 _ -> return k
142 return $ Vty { update = inner_update
143 , next_event = gkey
144 , terminal = t
145 , refresh = inner_refresh
146 , shutdown = fend
147 }