Debian Codegraph @

src/Graphics/UI/GLUT/Callbacks/Registration.hs

raw
1 {-# OPTIONS_GHC -fno-cse #-}
2
3 {-# OPTIONS_HADDOCK hide #-}
4 --------------------------------------------------------------------------------
5 -- |
6 -- Module : Graphics.UI.GLUT.Callbacks.Registration
7 -- Copyright : (c) Sven Panne 2002-2018
8 -- License : BSD3
9 --
10 -- Maintainer : Sven Panne <[email protected]>
11 -- Stability : stable
12 -- Portability : portable
13 --
14 --------------------------------------------------------------------------------
15
16 module Graphics.UI.GLUT.Callbacks.Registration (
17 CallbackType(..), registerForCleanup, setCallback, getCurrentWindow
18 ) where
19
20 --------------------------------------------------------------------------------
21
22 import Control.Monad ( when )
23 import Data.IORef ( IORef, newIORef, readIORef, writeIORef, modifyIORef )
24 import qualified Data.Map as M
25 import Data.StateVar ( get )
26 import Foreign.Ptr ( FunPtr, nullFunPtr, freeHaskellFunPtr )
27 import System.IO.Unsafe ( unsafePerformIO )
28
29 import Graphics.UI.GLUT.Raw
30 import Graphics.UI.GLUT.Window
31
32 --------------------------------------------------------------------------------
33 -- No timer callback here, because they are one-shot and "self destroy"
34
35 data CallbackType
36 = DisplayCB | OverlayDisplayCB | ReshapeCB
37 | KeyboardCB | KeyboardUpCB | MouseCB
38 | MotionCB | PassiveMotionCB | CrossingCB
39 | VisibilityCB | WindowStatusCB | SpecialCB
40 | SpecialUpCB | SpaceballMotionCB | SpaceballRotateCB
41 | SpaceballButtonCB | ButtonBoxCB | DialsCB
42 | TabletMotionCB | TabletButtonCB | JoystickCB
43 | MenuStatusCB | IdleCB
44 -- freeglut-only callback types
45 | CloseCB | MouseWheelCB | PositionCB
46 | MultiEntryCB | MultiMotionCB | MultiButtonCB
47 | MultiPassiveCB | InitContextCB | AppStatusCB
48 deriving ( Eq, Ord )
49
50 isGlobal :: CallbackType -> Bool
51 isGlobal MenuStatusCB = True
52 isGlobal IdleCB = True
53 isGlobal _ = False
54
55 --------------------------------------------------------------------------------
56 -- To uniquely identify a particular callback, the associated window is needed
57 -- for window callbacks.
58
59 data CallbackID = CallbackID (Maybe Window) CallbackType
60 deriving ( Eq, Ord )
61
62 getCallbackID :: CallbackType -> IO CallbackID
63 getCallbackID callbackType = do
64 maybeWindow <- if isGlobal callbackType
65 then return Nothing
66 else fmap Just $ getCurrentWindow "getCallbackID"
67 return $ CallbackID maybeWindow callbackType
68
69 getCurrentWindow :: String -> IO Window
70 getCurrentWindow func = do
71 win <- get currentWindow
72 maybe (error (func ++ ": no current window")) return win
73
74 --------------------------------------------------------------------------------
75 -- This seems to be a common Haskell hack nowadays: A plain old global variable
76 -- with an associated mutator. Perhaps some language/library support is needed?
77
78 {-# NOINLINE theCallbackTable #-}
79 theCallbackTable :: IORef (CallbackTable a)
80 theCallbackTable = unsafePerformIO (newIORef emptyCallbackTable)
81
82 getCallbackTable :: IO (CallbackTable a)
83 getCallbackTable = readIORef theCallbackTable
84
85 modifyCallbackTable :: (CallbackTable a -> CallbackTable a) -> IO ()
86 modifyCallbackTable = modifyIORef theCallbackTable
87
88 --------------------------------------------------------------------------------
89
90 type CallbackTable a = M.Map CallbackID (FunPtr a)
91
92 emptyCallbackTable :: CallbackTable a
93 emptyCallbackTable = M.empty
94
95 lookupInCallbackTable :: CallbackID -> IO (Maybe (FunPtr a))
96 lookupInCallbackTable callbackID =
97 fmap (M.lookup callbackID) getCallbackTable
98
99 deleteFromCallbackTable :: CallbackID -> IO ()
100 deleteFromCallbackTable callbackID =
101 modifyCallbackTable (M.delete callbackID)
102
103 addToCallbackTable :: CallbackID -> FunPtr a -> IO ()
104 addToCallbackTable callbackID funPtr =
105 modifyCallbackTable (M.insert callbackID funPtr)
106
107 --------------------------------------------------------------------------------
108 -- Another global mutable variable: The list of function pointers ready to be
109 -- freed by freeHaskellFunPtr
110
111 {-# NOINLINE theCleanupList #-}
112 theCleanupList :: IORef [FunPtr a]
113 theCleanupList = unsafePerformIO (newIORef [])
114
115 getCleanupList :: IO [FunPtr a]
116 getCleanupList = readIORef theCleanupList
117
118 setCleanupList :: [FunPtr a] -> IO ()
119 setCleanupList = writeIORef theCleanupList
120
121 --------------------------------------------------------------------------------
122 -- And yet another mutable (write-once) variable: A function pointer to a
123 -- callback which frees all function pointers on the cleanup list.
124
125 {-# NOINLINE theScavenger #-}
126 theScavenger :: IORef (FunPtr TimerFunc)
127 theScavenger = unsafePerformIO (newIORef =<< makeTimerFunc (\_ -> do
128 cleanupList <- getCleanupList
129 mapM_ freeHaskellFunPtr cleanupList
130 setCleanupList []))
131
132 getScavenger :: IO (FunPtr TimerFunc)
133 getScavenger = readIORef theScavenger
134
135 --------------------------------------------------------------------------------
136 -- Here is the really cunning stuff: If an element is added to the cleanup list
137 -- when it is empty, register an immediate callback at GLUT to free the list as
138 -- soon as possible.
139
140 registerForCleanup :: FunPtr a -> IO ()
141 registerForCleanup funPtr = do
142 oldCleanupList <- getCleanupList
143 setCleanupList (funPtr : oldCleanupList)
144 when (null oldCleanupList) $ do
145 scavenger <- getScavenger
146 glutTimerFunc 0 scavenger 0
147
148 --------------------------------------------------------------------------------
149
150 setCallback :: CallbackType -> (FunPtr a -> IO ()) -> (b -> IO (FunPtr a))
151 -> Maybe b -> IO ()
152 setCallback callbackType registerAtGLUT makeCallback maybeCallback = do
153 callbackID <- getCallbackID callbackType
154 maybeOldFunPtr <- lookupInCallbackTable callbackID
155 case maybeOldFunPtr of
156 Nothing -> return ()
157 Just oldFunPtr -> do registerForCleanup oldFunPtr
158 deleteFromCallbackTable callbackID
159 case maybeCallback of
160 Nothing -> registerAtGLUT nullFunPtr
161 Just callback -> do newFunPtr <- makeCallback callback
162 addToCallbackTable callbackID newFunPtr
163 registerAtGLUT newFunPtr