Debian Codegraph @

src/Graphics/UI/GLUT/State.hs

raw
1 --------------------------------------------------------------------------------
2 -- |
3 -- Module : Graphics.UI.GLUT.State
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 -- GLUT maintains a considerable amount of programmer visible state. Some (but
12 -- not all) of this state may be directly retrieved.
13 --
14 --------------------------------------------------------------------------------
15
16 module Graphics.UI.GLUT.State (
17 -- * State of all windows
18 windowBorderWidth, windowHeaderHeight, skipStaleMotionEvents,
19
20 -- * State of the /current window/
21
22 -- ** Framebuffer state
23 rgba,
24 BufferDepth, rgbaBufferDepths, colorBufferDepth,
25 doubleBuffered, stereo,
26 accumBufferDepths, depthBufferDepth, stencilBufferDepth,
27 SampleCount, sampleCount, formatID,
28
29 -- ** Full screen state
30 fullScreenMode,
31
32 -- ** Object rendering state
33 geometryVisualizeNormals,
34
35 -- ** Vertex attribute state
36 vertexAttribCoord3, vertexAttribNormal, vertexAttribTexCoord2,
37
38 -- ** Layer state
39 damaged,
40
41 -- * Timing
42 elapsedTime,
43
44 -- * Device information
45
46 -- $DeviceInformation
47 screenSize, screenSizeMM,
48 hasKeyboard,
49 ButtonCount, numMouseButtons,
50 numSpaceballButtons,
51 DialCount, numDialsAndButtons,
52 numTabletButtons,
53 AxisCount, PollRate, joystickInfo,
54 supportedNumAuxBuffers, supportedSamplesPerPixel,
55
56 -- * GLUT information
57 glutVersion, initState
58 ) where
59
60 import Control.Monad ( unless )
61 import Data.StateVar ( GettableStateVar, makeGettableStateVar
62 , SettableStateVar, makeSettableStateVar
63 , StateVar, makeStateVar )
64 import Foreign.C.Types ( CInt )
65 import Foreign.Marshal.Alloc ( alloca )
66 import Foreign.Marshal.Array ( peekArray )
67 import Foreign.Storable ( peek )
68 import Graphics.Rendering.OpenGL ( AttribLocation(..), Size(..), GLenum, GLint )
69
70 import Graphics.UI.GLUT.Overlay
71 import Graphics.UI.GLUT.QueryUtils
72 import Graphics.UI.GLUT.Raw
73 import Graphics.UI.GLUT.Window
74
75 --------------------------------------------------------------------------------
76
77 -- | Contains 'True' when the current layer of the /current window/ is in RGBA
78 -- mode, 'False' means color index mode.
79
80 rgba :: GettableStateVar Bool
81 rgba = makeGettableStateVar$ simpleGet i2b glut_WINDOW_RGBA
82
83 -- | Bit depth of a buffer
84
85 type BufferDepth = Int
86
87 -- | Contains the number of red, green, blue, and alpha bits in the color buffer
88 -- of the /current window\'s/ current layer (0 in color index mode).
89
90 rgbaBufferDepths ::
91 GettableStateVar (BufferDepth, BufferDepth, BufferDepth, BufferDepth)
92 rgbaBufferDepths = makeGettableStateVar $ do
93 r <- simpleGet fromIntegral glut_WINDOW_RED_SIZE
94 g <- simpleGet fromIntegral glut_WINDOW_GREEN_SIZE
95 b <- simpleGet fromIntegral glut_WINDOW_BLUE_SIZE
96 a <- simpleGet fromIntegral glut_WINDOW_ALPHA_SIZE
97 return (r, g, b, a)
98
99 -- | Contains the total number of bits in the color buffer of the /current
100 -- window\'s/ current layer. For an RGBA layer, this is the sum of the red,
101 -- green, blue, and alpha bits. For an color index layer, this is the number
102 -- of bits of the color indexes.
103
104 colorBufferDepth :: GettableStateVar BufferDepth
105 colorBufferDepth =
106 makeGettableStateVar $ simpleGet fromIntegral glut_WINDOW_BUFFER_SIZE
107
108 -- | Contains 'True' when the current layer of the /current window/ is double
109 -- buffered, 'False' otherwise.
110
111 doubleBuffered :: GettableStateVar Bool
112 doubleBuffered = makeGettableStateVar $ simpleGet i2b glut_WINDOW_DOUBLEBUFFER
113
114 -- | Contains 'True' when the current layer of the /current window/ is stereo,
115 -- 'False' otherwise.
116
117 stereo :: GettableStateVar Bool
118 stereo = makeGettableStateVar $ simpleGet i2b glut_WINDOW_STEREO
119
120 -- | Contains the number of red, green, blue, and alpha bits in the accumulation
121 -- buffer of the /current window\'s/ current layer (0 in color index mode).
122
123 accumBufferDepths ::
124 GettableStateVar (BufferDepth, BufferDepth, BufferDepth, BufferDepth)
125 accumBufferDepths = makeGettableStateVar $ do
126 r <- simpleGet fromIntegral glut_WINDOW_ACCUM_RED_SIZE
127 g <- simpleGet fromIntegral glut_WINDOW_ACCUM_GREEN_SIZE
128 b <- simpleGet fromIntegral glut_WINDOW_ACCUM_BLUE_SIZE
129 a <- simpleGet fromIntegral glut_WINDOW_ACCUM_ALPHA_SIZE
130 return (r, g, b, a)
131
132 -- | Contains the number of bits in the depth buffer of the /current window\'s/
133 -- current layer.
134
135 depthBufferDepth :: GettableStateVar BufferDepth
136 depthBufferDepth =
137 makeGettableStateVar $ simpleGet fromIntegral glut_WINDOW_DEPTH_SIZE
138
139 -- | Contains the number of bits in the stencil buffer of the /current
140 -- window\'s/ current layer.
141
142 stencilBufferDepth :: GettableStateVar BufferDepth
143 stencilBufferDepth =
144 makeGettableStateVar $ simpleGet fromIntegral glut_WINDOW_STENCIL_SIZE
145
146 -- | Number of samples for multisampling
147
148 type SampleCount = Int
149
150 -- | Contains the number of samples for multisampling for the /current window./
151
152 sampleCount :: GettableStateVar SampleCount
153 sampleCount =
154 makeGettableStateVar $ simpleGet fromIntegral glut_WINDOW_NUM_SAMPLES
155
156 -- | Contains the window system dependent format ID for the current layer of the
157 -- /current window/. On X11 GLUT implementations, this is the X visual ID. On
158 -- Win32 GLUT implementations, this is the Win32 Pixel Format Descriptor number.
159 -- This value is returned for debugging, benchmarking, and testing ease.
160
161 formatID :: GettableStateVar Int
162 formatID = makeGettableStateVar $ simpleGet fromIntegral glut_WINDOW_FORMAT_ID
163
164 --------------------------------------------------------------------------------
165
166 -- | (/freeglut only/) Contains 'True' if the /current window/ is in full screen
167 -- mode, 'False' otherwise.
168
169 fullScreenMode :: StateVar Bool
170 fullScreenMode = makeStateVar getFullScreenMode setFullScreenMode
171
172 getFullScreenMode :: IO Bool
173 getFullScreenMode = simpleGet i2b glut_FULL_SCREEN
174
175 setFullScreenMode :: Bool -> IO ()
176 setFullScreenMode newMode = do
177 oldMode <- getFullScreenMode
178 unless (newMode == oldMode) fullScreenToggle
179
180 --------------------------------------------------------------------------------
181
182 -- | (/freeglut only/) Controls if vectors representing the normals should be
183 -- drawn, too, when objects are drawn.
184
185 geometryVisualizeNormals :: StateVar Bool
186 geometryVisualizeNormals =
187 makeStateVar
188 (simpleGet i2b glut_GEOMETRY_VISUALIZE_NORMALS)
189 (glutSetOption glut_GEOMETRY_VISUALIZE_NORMALS . b2i)
190
191
192 --------------------------------------------------------------------------------
193
194 -- | (/freeglut only/) If 'vertexAttribCoord3' and 'vertexAttribNormal' both
195 -- contain 'Nothing', the fixed function pipeline is used to draw
196 -- objects. Otherwise VBOs are used and the coordinates are passed via 'Just'
197 -- this attribute location (for a vec3).
198
199 vertexAttribCoord3 :: SettableStateVar (Maybe AttribLocation)
200 vertexAttribCoord3 = setVertexAttribWith glutSetVertexAttribCoord3
201
202 setVertexAttribWith :: (GLint -> IO ()) -> SettableStateVar (Maybe AttribLocation)
203 setVertexAttribWith f = makeSettableStateVar $ f . getLocation
204 where getLocation = maybe (-1) (\(AttribLocation l) -> fromIntegral l)
205
206 -- | (/freeglut only/) If 'vertexAttribCoord3' and 'vertexAttribNormal' both
207 -- contain 'Nothing', the fixed function pipeline is used to draw
208 -- objects. Otherwise VBOs are used and the normals are passed via 'Just' this
209 -- attribute location (for a vec3).
210
211 vertexAttribNormal :: SettableStateVar (Maybe AttribLocation)
212 vertexAttribNormal = setVertexAttribWith glutSetVertexAttribNormal
213
214 -- | (/freeglut only/) If VBOs are used to draw objects (controlled via
215 -- 'vertexAttribCoord3' and 'vertexAttribNormal'), the texture coordinates are
216 -- passed via 'Just' this attribute location (for a vec2).
217
218 vertexAttribTexCoord2 :: SettableStateVar (Maybe AttribLocation)
219 vertexAttribTexCoord2 = setVertexAttribWith glutSetVertexAttribTexCoord2
220
221 --------------------------------------------------------------------------------
222
223 -- | Contains the number of milliseconds since
224 -- 'Graphics.UI.GLUT.Initialization.initialize' was called.
225
226 elapsedTime :: GettableStateVar Int
227 elapsedTime = makeGettableStateVar $ simpleGet fromIntegral glut_ELAPSED_TIME
228
229 --------------------------------------------------------------------------------
230
231 -- | Contains 'True' if the given plane of the /current window/ has been
232 -- damaged (by window system activity) since the last display callback was
233 -- triggered. Calling 'Graphics.UI.GLUT.Window.postRedisplay' or
234 -- 'Graphics.UI.GLUT.Overlay.postOverlayRedisplay' will not set this 'True'.
235
236 damaged :: Layer -> GettableStateVar Bool
237 damaged l = makeGettableStateVar $ layerGet isDamaged (marshalDamagedLayer l)
238 where isDamaged d = d /= 0 && d /= -1
239 marshalDamagedLayer x = case x of
240 Normal -> glut_NORMAL_DAMAGED
241 Overlay -> glut_OVERLAY_DAMAGED
242
243 --------------------------------------------------------------------------------
244
245 -- $DeviceInformation
246 -- If a device is not available, the following state variables contain
247 -- 'Nothing', otherwise they return 'Just' the specific device information.
248 -- Only a screen is always assumed.
249
250 --------------------------------------------------------------------------------
251
252 -- | The size of the screen in pixels.
253
254 screenSize :: GettableStateVar Size
255 screenSize =
256 makeGettableStateVar $ do
257 wpx <- simpleGet fromIntegral glut_SCREEN_WIDTH
258 hpx <- simpleGet fromIntegral glut_SCREEN_HEIGHT
259 return $ Size wpx hpx
260
261 -- | The size of the screen in millimeters.
262
263 screenSizeMM :: GettableStateVar Size
264 screenSizeMM =
265 makeGettableStateVar $ do
266 wmm <- simpleGet fromIntegral glut_SCREEN_WIDTH_MM
267 hmm <- simpleGet fromIntegral glut_SCREEN_HEIGHT_MM
268 return $ Size wmm hmm
269
270 --------------------------------------------------------------------------------
271
272 -- | Contains 'True' if a keyboard is present, 'False' otherwise.
273
274 hasKeyboard :: GettableStateVar Bool
275 hasKeyboard = makeGettableStateVar $ deviceGet i2b glut_HAS_KEYBOARD
276
277 --------------------------------------------------------------------------------
278
279 -- | Number of buttons of an input device
280
281 type ButtonCount = Int
282
283 -- | Contains 'Just' the number of buttons of an attached mouse or 'Nothing' if
284 -- there is none.
285
286 numMouseButtons :: GettableStateVar (Maybe ButtonCount)
287 numMouseButtons =
288 getDeviceInfo glut_HAS_MOUSE $
289 deviceGet fromIntegral glut_NUM_MOUSE_BUTTONS
290
291 --------------------------------------------------------------------------------
292
293 -- | Contains 'Just' the number of buttons of the attached Spaceball or 'Nothing'
294 -- if there is none.
295
296 numSpaceballButtons :: GettableStateVar (Maybe ButtonCount)
297 numSpaceballButtons =
298 getDeviceInfo glut_HAS_SPACEBALL $
299 deviceGet fromIntegral glut_NUM_SPACEBALL_BUTTONS
300
301 --------------------------------------------------------------------------------
302
303 -- | Number of dials of a dial and button box
304
305 type DialCount = Int
306
307 -- | Contains 'Just' the number of dials and buttons of an attached dial &
308 -- button box or 'Nothing' if there is none.
309
310 numDialsAndButtons :: GettableStateVar (Maybe (DialCount, ButtonCount))
311 numDialsAndButtons =
312 getDeviceInfo glut_HAS_DIAL_AND_BUTTON_BOX $ do
313 d <- deviceGet fromIntegral glut_NUM_DIALS
314 b <- deviceGet fromIntegral glut_NUM_BUTTON_BOX_BUTTONS
315 return (d, b)
316
317 --------------------------------------------------------------------------------
318
319 -- | Contains 'Just' the number of buttons of an attached tablet or 'Nothing' if
320 -- there is none.
321
322 numTabletButtons :: GettableStateVar (Maybe ButtonCount)
323 numTabletButtons =
324 getDeviceInfo glut_HAS_TABLET $
325 deviceGet fromIntegral glut_NUM_TABLET_BUTTONS
326
327 --------------------------------------------------------------------------------
328
329 -- | Number of axes of a joystick
330
331 type AxisCount = Int
332
333 -- | The a rate at which a joystick is polled (in milliseconds)
334
335 type PollRate = Int
336
337 -- | Contains 'Just' the number of buttons of an attached joystick, the number
338 -- of joystick axes, and the rate at which the joystick is polled. Contains
339 -- 'Nothing' if there is no joystick attached.
340
341 joystickInfo :: GettableStateVar (Maybe (ButtonCount, PollRate, AxisCount))
342 joystickInfo =
343 getDeviceInfo glut_HAS_JOYSTICK $ do
344 b <- deviceGet fromIntegral glut_JOYSTICK_BUTTONS
345 a <- deviceGet fromIntegral glut_JOYSTICK_AXES
346 r <- deviceGet fromIntegral glut_JOYSTICK_POLL_RATE
347 return (b, a, r)
348
349 -----------------------------------------------------------------------------
350
351 -- | (/freeglut only/) Contains a list of the number of auxiliary buffers
352 -- supported, in increasing order.
353
354 supportedNumAuxBuffers :: GettableStateVar [Int]
355 supportedNumAuxBuffers = getModeValues glut_AUX
356
357 -- | (/freeglut only/) Contains a list of the number of samples per pixel
358 -- supported for multisampling, in increasing order.
359
360 supportedSamplesPerPixel :: GettableStateVar [SampleCount]
361 supportedSamplesPerPixel = getModeValues (fromIntegral glut_MULTISAMPLE)
362
363 getModeValues :: Integral a => GLenum -> GettableStateVar [a]
364 getModeValues what = makeGettableStateVar $
365 alloca $ \sizeBuffer -> do
366 valuesBuffer <- glutGetModeValues what sizeBuffer
367 size <- peek sizeBuffer
368 fmap (map fromIntegral) $ peekArray (fromIntegral size) valuesBuffer
369
370 --------------------------------------------------------------------------------
371 -- Convenience (un-)marshalers
372
373 i2b :: CInt -> Bool
374 i2b = (/= 0)
375
376 b2i :: Bool -> CInt
377 b2i False = 0
378 b2i True = 1
379
380 --------------------------------------------------------------------------------
381
382 getDeviceInfo :: GLenum -> IO a -> GettableStateVar (Maybe a)
383 getDeviceInfo dev act =
384 makeGettableStateVar $ do
385 hasDevice <- deviceGet i2b dev
386 if hasDevice then fmap Just act else return Nothing
387
388 -----------------------------------------------------------------------------
389
390 -- | Contains version of GLUT in the form of
391 -- @/flavour/ /major/./minor/./patchlevel/@, where @/flavour/@ is one of
392 -- @GLUT@, @freeglut@ or @OpenGLUT@.
393
394 glutVersion :: GettableStateVar String
395 glutVersion = makeGettableStateVar $ do
396 let isGLUT = not `fmap` isKnown "glutSetOption"
397 isFreeglut = not `fmap` isKnown "glutSetWindowStayOnTop"
398 showVersionPart x = shows (x `mod` 100)
399 showVersion v = showVersionPart (v `div` 10000) . showChar '.' .
400 showVersionPart (v `div` 100) . showChar '.' .
401 showVersionPart v
402 g <- isGLUT
403 if g
404 then return "GLUT 3.7" -- ToDo: just guessing
405 else do f <- isFreeglut
406 v <- simpleGet id glut_VERSION
407 let prefix = if f then "freeglut" else "OpenGLUT"
408 return $ showString prefix . showChar ' ' . showVersion v $ ""
409
410 -----------------------------------------------------------------------------
411
412 -- | (/freeglut only/) Contains the thickness of the sizing border around the
413 -- perimeter of a window that can be resized, in pixels.
414
415 windowBorderWidth :: GettableStateVar Int
416 windowBorderWidth =
417 makeGettableStateVar (simpleGet fromIntegral glut_WINDOW_BORDER_WIDTH)
418
419 -----------------------------------------------------------------------------
420
421 -- | (/freeglut only/) Contains the height of the header\/caption area of a
422 -- window in pixels.
423
424 windowHeaderHeight :: GettableStateVar Int
425 windowHeaderHeight =
426 makeGettableStateVar (simpleGet fromIntegral glut_WINDOW_HEADER_HEIGHT)
427
428 -----------------------------------------------------------------------------
429
430 -- | (/freeglut on X11 only/) Controls if all but the last motion event should
431 -- be discarded.
432
433 skipStaleMotionEvents :: StateVar Bool
434 skipStaleMotionEvents =
435 makeStateVar
436 (simpleGet i2b glut_SKIP_STALE_MOTION_EVENTS)
437 (glutSetOption glut_SKIP_STALE_MOTION_EVENTS . b2i)
438
439 -----------------------------------------------------------------------------
440
441 -- | (/freeglut only/) Contains 'True' if GLUT has been initialized
442 -- with 'Graphics.UI.GLUT.Initialization.initialize' or
443 -- 'Graphics.UI.GLUT.Initialization.getArgsAndInitialize' has and not yet
444 -- been de-initialized with 'Graphics.UI.GLUT.Initialization.exit'. Contains
445 -- 'False' otherwise.
446
447 initState :: GettableStateVar Bool
448 initState = makeGettableStateVar$ simpleGet i2b glut_INIT_STATE