on b842e2f help sign in
vty/src/Graphics/Vty.hs
149 lines 6.0 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 -- The above methods will throw an exception if executed after this is executed.
70 , shutdown :: IO ()
71 }
73-- | Set up the state object for using vty. At most one state object should be
74-- created at a time.
75mkVty :: IO Vty
76mkVty = mkVtyEscDelay 0
78-- | Set up the state object for using vty. At most one state object should be
79-- created at a time. The delay, in microseconds, specifies the period of time to wait for a key
80-- following reading ESC from the terminal before considering the ESC key press as a discrete event.
81mkVtyEscDelay :: Int -> IO Vty
82mkVtyEscDelay escDelay = do
83 term_info <- Terminfo.setupTermFromEnv
84 t <- terminal_handle
85 reserve_display t
86 (kvar, endi) <- initTermInput escDelay term_info
87 intMkVty kvar ( endi >> release_display t >> release_terminal t ) t
89intMkVty :: IO Event -> IO () -> TerminalHandle -> IO Vty
90intMkVty kvar fend t = do
91 last_pic_ref <- newIORef Nothing
92 last_update_ref <- newIORef Nothing
94 let inner_update in_pic = do
95 b <- display_bounds t
96 let DisplayRegion w h = b
97 cursor = pic_cursor in_pic
98 in_pic' = case cursor of
99 Cursor x y ->
100 let
101 x' = case x of
102 _ | x >= 0x80000000 -> 0
103 | x >= w -> w - 1
104 | otherwise -> x
105 y' = case y of
106 _ | y >= 0x80000000 -> 0
107 | y >= h -> h - 1
108 | otherwise -> y
109 in in_pic { pic_cursor = Cursor x' y' }
110 _ -> in_pic
111 mlast_update <- readIORef last_update_ref
112 update_data <- case mlast_update of
113 Nothing -> do
114 d <- display_context t b
115 output_picture d in_pic'
116 return (b, d)
117 Just (last_bounds, last_context) -> do
118 if b /= last_bounds
119 then do
120 d <- display_context t b
121 output_picture d in_pic'
122 return (b, d)
123 else do
124 output_picture last_context in_pic'
125 return (b, last_context)
126 writeIORef last_update_ref $ Just update_data
127 writeIORef last_pic_ref $ Just in_pic'
129 let inner_refresh
130 = writeIORef last_update_ref Nothing
131 >> readIORef last_pic_ref
132 >>= maybe ( return () ) ( \pic -> inner_update pic )
134 let gkey = do k <- kvar
135 case k of
136 (EvResize _ _) -> inner_refresh
137 >> display_bounds t
138 >>= return . ( \(DisplayRegion w h)
139 -> EvResize (fromEnum w) (fromEnum h)
140 )
141 _ -> return k
143 return $ Vty { update = inner_update
144 , next_event = gkey
145 , terminal = t
146 , refresh = inner_refresh
147 , shutdown = fend
148 }