Debian Codegraph @

src/Graphics/UI/GLUT/Begin.hs

raw
1 --------------------------------------------------------------------------------
2 -- |
3 -- Module : Graphics.UI.GLUT.Begin
4 -- Copyright : (c) Sven Panne 2002-2018
5 -- License : BSD3
6 --
7 -- Maintainer : Sven Panne <[email protected]>
8 -- Stability : stable
9 -- Portability : portable
10 --
11 -- After a GLUT program has done initial setup such as creating windows and
12 -- menus, GLUT programs enter the GLUT event processing loop by calling
13 -- 'mainLoop' or handle events iteratively with 'mainLoopEvent'.
14 --
15 --------------------------------------------------------------------------------
16
17 module Graphics.UI.GLUT.Begin (
18 -- * Handling events
19 mainLoop, mainLoopEvent, leaveMainLoop,
20
21 -- * Controlling the behaviour when windows are closed
22 ActionOnWindowClose(..), actionOnWindowClose
23 ) where
24
25 import Control.Monad.IO.Class ( MonadIO(..) )
26 import Data.StateVar ( StateVar, makeStateVar )
27 import Foreign.C.Types ( CInt )
28
29 import Graphics.UI.GLUT.QueryUtils
30 import Graphics.UI.GLUT.Raw
31
32 --------------------------------------------------------------------------------
33
34 -- | Enter the GLUT event processing loop; it will call as necessary any
35 -- callbacks that have been registered. This routine should be called at most
36 -- once in a GLUT program.
37
38 mainLoop :: MonadIO m => m ()
39 mainLoop = glutMainLoop
40
41 --------------------------------------------------------------------------------
42
43 -- | (/freeglut only/) Process one iteration's worth of events in its event loop.
44 -- This allows the application to control its own event loop and still use the
45 -- GLUT package.
46
47 mainLoopEvent :: MonadIO m => m ()
48 mainLoopEvent = glutMainLoopEvent
49
50 --------------------------------------------------------------------------------
51
52 -- | (/freeglut only/) Stop the event loop. If 'actionOnWindowClose' contains
53 -- 'Exit', the application will exit; otherwise control will return to the
54 -- function which called 'mainLoop'.
55 --
56 -- If the application has two nested calls to 'mainLoop' and calls
57 -- 'leaveMainLoop', the behaviour is undefined. It may leave only the inner
58 -- nested loop or it may leave both loops. If the reader has a strong preference
59 -- for one behaviour over the other he should contact the freeglut Programming
60 -- Consortium and ask for the code to be fixed.
61
62 leaveMainLoop :: MonadIO m => m ()
63 leaveMainLoop = glutLeaveMainLoop
64
65 --------------------------------------------------------------------------------
66
67 -- | The behaviour when the user closes a window.
68
69 data ActionOnWindowClose
70 = -- | Exit the whole program when any window is closed or 'leaveMainLoop'
71 -- is called (default).
72 Exit
73 | -- | Return from mainLoop when any window is closed.
74 MainLoopReturns
75 | -- | Return from mainLoop after the last window is closed.
76 ContinueExecution
77 deriving ( Eq, Ord, Show )
78
79 marshalActionOnWindowClose :: ActionOnWindowClose -> CInt
80 marshalActionOnWindowClose x = case x of
81 Exit -> glut_ACTION_EXIT
82 MainLoopReturns -> glut_ACTION_GLUTMAINLOOP_RETURNS
83 ContinueExecution -> glut_ACTION_CONTINUE_EXECUTION
84
85 unmarshalActionOnWindowClose :: CInt -> ActionOnWindowClose
86 unmarshalActionOnWindowClose x
87 | x == glut_ACTION_EXIT = Exit
88 | x == glut_ACTION_GLUTMAINLOOP_RETURNS = MainLoopReturns
89 | x == glut_ACTION_CONTINUE_EXECUTION = ContinueExecution
90 | otherwise = error ("unmarshalActionOnWindowClose: illegal value " ++ show x)
91
92 -----------------------------------------------------------------------------
93
94 -- | (/freeglut only/) Controls the behaviour when the user closes a window.
95
96 actionOnWindowClose :: StateVar ActionOnWindowClose
97 actionOnWindowClose =
98 makeStateVar
99 (simpleGet unmarshalActionOnWindowClose glut_ACTION_ON_WINDOW_CLOSE)
100 (glutSetOption glut_ACTION_ON_WINDOW_CLOSE . marshalActionOnWindowClose)