Debian Codegraph @

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

raw
1 --------------------------------------------------------------------------------
2 -- |
3 -- Module : Graphics.UI.GLUT.Callbacks.Window
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 --------------------------------------------------------------------------------
12
13 module Graphics.UI.GLUT.Callbacks.Window (
14 -- * Redisplay callbacks
15 DisplayCallback, displayCallback, overlayDisplayCallback,
16
17 -- * Reshape callback
18 ReshapeCallback, reshapeCallback,
19
20 -- * Position callback
21 PositionCallback, positionCallback,
22
23 -- * Callbacks for visibility changes
24 Visibility(..), VisibilityCallback, visibilityCallback,
25 WindowState(..), WindowStateCallback, windowStateCallback,
26
27 -- * Window close callback
28 CloseCallback, closeCallback,
29
30 -- * Life cycle callbacks for mobile platforms
31 InitContextCallback, initContextCallback,
32 AppStatus(..), AppStatusCallback, appStatusCallback,
33
34 -- * Keyboard callback
35 KeyboardCallback, keyboardCallback, keyboardUpCallback,
36
37 -- * Special callback
38 SpecialCallback, specialCallback, specialUpCallback,
39
40 -- * Mouse callback
41 MouseCallback, mouseCallback,
42
43 -- * Keyboard and mouse input callback
44 Key(..), SpecialKey(..), MouseButton(..), KeyState(..), Modifiers(..),
45 KeyboardMouseCallback, keyboardMouseCallback,
46
47 -- * Mouse wheel callback
48 WheelNumber, WheelDirection, MouseWheelCallback, mouseWheelCallback,
49
50 -- * Mouse movement callbacks
51 MotionCallback, motionCallback, passiveMotionCallback,
52 Crossing(..), CrossingCallback, crossingCallback,
53
54 -- * Spaceball callback
55 SpaceballMotion, SpaceballRotation, ButtonIndex, SpaceballInput(..),
56 SpaceballCallback, spaceballCallback,
57
58 -- * Dial & button box callback
59 DialAndButtonBoxInput(..), DialIndex,
60 DialAndButtonBoxCallback, dialAndButtonBoxCallback,
61
62 -- * Tablet callback
63 TabletPosition(..), TabletInput(..), TabletCallback, tabletCallback,
64
65 -- * Joystick callback
66 JoystickButtons(..), JoystickPosition(..),
67 JoystickCallback, joystickCallback,
68
69 -- * Multi-touch support
70 TouchID,
71 MultiMouseCallback, multiMouseCallback,
72 MultiCrossingCallback, multiCrossingCallback,
73 MultiMotionCallback, multiMotionCallback, multiPassiveMotionCallback
74
75 ) where
76
77 import Data.Bits ( (.&.) )
78 import Data.Char ( chr )
79 import Data.Maybe ( fromJust )
80 import Data.StateVar ( SettableStateVar, makeSettableStateVar )
81 import Foreign.C.Types ( CInt, CUInt )
82 import Graphics.Rendering.OpenGL ( Position(..), Size(..) )
83
84 import Graphics.UI.GLUT.Callbacks.Registration
85 import Graphics.UI.GLUT.Raw
86 import Graphics.UI.GLUT.State
87 import Graphics.UI.GLUT.Types
88
89 --------------------------------------------------------------------------------
90
91 -- | A display callback
92
93 type DisplayCallback = IO ()
94
95 -- | Controls the display callback for the /current window./ When GLUT determines
96 -- that the normal plane for the window needs to be redisplayed, the display
97 -- callback for the window is called. Before the callback, the /current window/
98 -- is set to the window needing to be redisplayed and (if no overlay display
99 -- callback is registered) the /layer in use/ is set to the normal plane. The
100 -- entire normal plane region should be redisplayed in response to the callback
101 -- (this includes ancillary buffers if your program depends on their state).
102 --
103 -- GLUT determines when the display callback should be triggered based on the
104 -- window\'s redisplay state. The redisplay state for a window can be either set
105 -- explicitly by calling 'Graphics.UI.GLUT.Window.postRedisplay' or implicitly
106 -- as the result of window damage reported by the window system. Multiple posted
107 -- redisplays for a window are coalesced by GLUT to minimize the number of
108 -- display callbacks called.
109 --
110 -- When an overlay is established for a window, but there is no overlay display
111 -- callback registered, the display callback is used for redisplaying both the
112 -- overlay and normal plane (that is, it will be called if either the redisplay
113 -- state or overlay redisplay state is set). In this case, the /layer in use/ is
114 -- not implicitly changed on entry to the display callback.
115 --
116 -- See 'overlayDisplayCallback' to understand how distinct callbacks for the
117 -- overlay and normal plane of a window may be established.
118 --
119 -- When a window is created, no display callback exists for the window. It is
120 -- the responsibility of the programmer to install a display callback for the
121 -- window before the window is shown. A display callback must be registered for
122 -- any window that is shown. If a window becomes displayed without a display
123 -- callback being registered, a fatal error occurs. There is no way to
124 -- \"deregister\" a display callback (though another callback routine can always
125 -- be registered).
126 --
127 -- Upon return from the display callback, the normal damaged state of the window
128 -- (see 'Graphics.UI.GLUT.State.damaged') is cleared. If there is no overlay
129 -- display callback registered the overlay damaged state of the window (see
130 -- 'Graphics.UI.GLUT.State.damaged') is also cleared.
131
132 displayCallback :: SettableStateVar DisplayCallback
133 displayCallback = makeSettableStateVar $
134 setCallback DisplayCB glutDisplayFunc makeDisplayFunc . Just
135
136 --------------------------------------------------------------------------------
137
138 -- | Controls the overlay display callback for the /current window./ The overlay
139 -- display callback is functionally the same as the window\'s display callback
140 -- except that the overlay display callback is used to redisplay the window\'s
141 -- overlay.
142 --
143 -- When GLUT determines that the overlay plane for the window needs to be
144 -- redisplayed, the overlay display callback for the window is called. Before
145 -- the callback, the /current window/ is set to the window needing to be
146 -- redisplayed and the /layer in use/ is set to the overlay. The entire overlay
147 -- region should be redisplayed in response to the callback (this includes
148 -- ancillary buffers if your program depends on their state).
149 --
150 -- GLUT determines when the overlay display callback should be triggered based
151 -- on the window\'s overlay redisplay state. The overlay redisplay state for a
152 -- window can be either set explicitly by calling
153 -- 'Graphics.UI.GLUT.Overlay.postOverlayRedisplay' or implicitly as the result
154 -- of window damage reported by the window system. Multiple posted overlay
155 -- redisplays for a window are coalesced by GLUT to minimize the number of
156 -- overlay display callbacks called.
157 --
158 -- Upon return from the overlay display callback, the overlay damaged state of
159 -- the window (see 'Graphics.UI.GLUT.State.damaged') is cleared.
160 --
161 -- Initially there is no overlay display callback registered when an overlay is
162 -- established. See 'displayCallback' to understand how the display callback
163 -- alone is used if an overlay display callback is not registered.
164
165 overlayDisplayCallback :: SettableStateVar (Maybe DisplayCallback)
166 overlayDisplayCallback = makeSettableStateVar $
167 setCallback OverlayDisplayCB glutOverlayDisplayFunc makeOverlayDisplayFunc
168
169 --------------------------------------------------------------------------------
170
171 -- | A reshape callback
172
173 type ReshapeCallback = Size -> IO ()
174
175 -- | Controls the reshape callback for the /current window./ The reshape callback
176 -- is triggered when a window is reshaped. A reshape callback is also triggered
177 -- immediately before a window\'s first display callback after a window is
178 -- created or whenever an overlay for the window is established. The parameter
179 -- of the callback specifies the new window size in pixels. Before the callback,
180 -- the /current window/ is set to the window that has been reshaped.
181 --
182 -- If a reshape callback is not registered for a window or 'reshapeCallback' is
183 -- set to 'Nothing' (to deregister a previously registered callback), the
184 -- default reshape callback is used. This default callback will simply call
185 --
186 -- @
187 -- 'Graphics.Rendering.OpenGL.GL.CoordTrans.viewport' ('Graphics.Rendering.OpenGL.GL.CoordTrans.Position' 0 0) ('Graphics.Rendering.OpenGL.GL.CoordTrans.Size' /width/ /height/)
188 -- @
189 --
190 -- on the normal plane (and on the overlay if one exists).
191 --
192 -- If an overlay is established for the window, a single reshape callback is
193 -- generated. It is the callback\'s responsibility to update both the normal
194 -- plane and overlay for the window (changing the layer in use as necessary).
195 --
196 -- When a top-level window is reshaped, subwindows are not reshaped. It is up to
197 -- the GLUT program to manage the size and positions of subwindows within a
198 -- top-level window. Still, reshape callbacks will be triggered for subwindows
199 -- when their size is changed using 'Graphics.UI.GLUT.Window.windowSize'.
200
201 reshapeCallback :: SettableStateVar (Maybe ReshapeCallback)
202 reshapeCallback = makeSettableStateVar $
203 setCallback ReshapeCB glutReshapeFunc (makeReshapeFunc . unmarshal)
204 where unmarshal cb w h = cb (Size (fromIntegral w) (fromIntegral h))
205
206 --------------------------------------------------------------------------------
207
208 -- | A position callback
209
210 type PositionCallback = Position -> IO ()
211
212 -- | (/freeglut only/) Controls the position callback for the /current window./
213 -- The position callback for a window is called when the position of a window
214 -- changes.
215
216 positionCallback :: SettableStateVar (Maybe PositionCallback)
217 positionCallback = makeSettableStateVar $
218 setCallback PositionCB glutPositionFunc (makePositionFunc . unmarshal)
219 where unmarshal cb x y = cb (Position (fromIntegral x) (fromIntegral y))
220
221 --------------------------------------------------------------------------------
222
223 -- | The visibility state of the /current window/
224
225 data Visibility
226 = NotVisible -- ^ No part of the /current window/ is visible, i.e., until the
227 -- window\'s visibility changes, all further rendering to the
228 -- window is discarded.
229 | Visible -- ^ The /current window/ is totally or partially visible. GLUT
230 -- considers a window visible if any pixel of the window is
231 -- visible or any pixel of any descendant window is visible on
232 -- the screen.
233 deriving ( Eq, Ord, Show )
234
235 unmarshalVisibility :: CInt -> Visibility
236 unmarshalVisibility x
237 | x == glut_NOT_VISIBLE = NotVisible
238 | x == glut_VISIBLE = Visible
239 | otherwise = error ("unmarshalVisibility: illegal value " ++ show x)
240
241 --------------------------------------------------------------------------------
242
243 -- | A visibility callback
244
245 type VisibilityCallback = Visibility -> IO ()
246
247 -- | Controls the visibility callback for the /current window./ The visibility
248 -- callback for a window is called when the visibility of a window changes.
249 --
250 -- If the visibility callback for a window is disabled and later re-enabled, the
251 -- visibility status of the window is undefined; any change in window visibility
252 -- will be reported, that is if you disable a visibility callback and re-enable
253 -- the callback, you are guaranteed the next visibility change will be reported.
254 --
255 -- Note that you can either use 'visibilityCallback' or 'windowStateCallback',
256 -- but not both, because the former is implemented via the latter.
257
258 visibilityCallback :: SettableStateVar (Maybe VisibilityCallback)
259 visibilityCallback = makeSettableStateVar $
260 setCallback VisibilityCB glutVisibilityFunc
261 (makeVisibilityFunc . unmarshal)
262 where unmarshal cb = cb . unmarshalVisibility
263
264 --------------------------------------------------------------------------------
265
266 -- | The window state of the /current window/
267
268 data WindowState
269 = Unmapped -- ^ The /current window/ is unmapped.
270 | FullyRetained -- ^ The /current window/ is unobscured.
271 | PartiallyRetained -- ^ The /current window/ is partially obscured.
272 | FullyCovered -- ^ The /current window/ is fully obscured.
273 deriving ( Eq, Ord, Show )
274
275 unmarshalWindowState :: CInt -> WindowState
276 unmarshalWindowState x
277 | x == glut_HIDDEN = Unmapped
278 | x == glut_FULLY_RETAINED = FullyRetained
279 | x == glut_PARTIALLY_RETAINED = PartiallyRetained
280 | x == glut_FULLY_COVERED = FullyCovered
281 | otherwise = error ("unmarshalWindowState: illegal value " ++ show x)
282
283 --------------------------------------------------------------------------------
284
285 -- | A window state callback
286
287 type WindowStateCallback = WindowState -> IO ()
288
289 -- | Controls the window state callback for the
290 -- /current window./ The window state callback for a window is called when the
291 -- window state of a window changes.
292 --
293 -- If the window state callback for a window is disabled and later re-enabled,
294 -- the window state state of the window is undefined; any change in the window
295 -- state will be reported, that is if you disable a window state callback and
296 -- re-enable the callback, you are guaranteed the next window state change will
297 -- be reported.
298 --
299 -- Note that you can either use 'visibilityCallback' or 'windowStateCallback',
300 -- but not both, because the former is implemented via the latter.
301
302 windowStateCallback :: SettableStateVar (Maybe WindowStateCallback)
303 windowStateCallback = makeSettableStateVar $
304 setCallback WindowStatusCB glutWindowStatusFunc
305 (makeWindowStatusFunc . unmarshal)
306 where unmarshal cb = cb . unmarshalWindowState
307
308 --------------------------------------------------------------------------------
309
310 -- | A window close callback
311
312 type CloseCallback = IO ()
313
314 -- | Controls the window close callback for the /current window/.
315
316 closeCallback :: SettableStateVar (Maybe CloseCallback)
317 closeCallback = makeSettableStateVar $
318 setCallback CloseCB glutCloseFunc makeCloseFunc
319
320 --------------------------------------------------------------------------------
321
322 -- | An initialize context callback
323
324 type InitContextCallback = IO ()
325
326 -- | (/freeglut only/) Controls the initialize context callback for the /current
327 -- window/.
328
329 initContextCallback :: SettableStateVar (Maybe InitContextCallback)
330 initContextCallback = makeSettableStateVar $
331 setCallback InitContextCB glutInitContextFunc makeInitContextFunc
332
333 --------------------------------------------------------------------------------
334
335 -- | The application status of the /current window/
336
337 data AppStatus
338 = AppStatusPause
339 | AppStatusResume
340 deriving ( Eq, Ord, Show )
341
342 unmarshalAppStatus :: CInt -> AppStatus
343 unmarshalAppStatus x
344 | x == glut_APPSTATUS_PAUSE = AppStatusPause
345 | x == glut_APPSTATUS_RESUME = AppStatusResume
346 | otherwise = error ("unmarshalAppStatus: illegal value " ++ show x)
347
348 --------------------------------------------------------------------------------
349
350 -- | An application status callback
351
352 type AppStatusCallback = AppStatus -> IO ()
353
354 -- | Controls the application status callback for the /current window./
355
356 appStatusCallback :: SettableStateVar (Maybe AppStatusCallback)
357 appStatusCallback = makeSettableStateVar $
358 setCallback AppStatusCB glutAppStatusFunc
359 (makeAppStatusFunc . unmarshal)
360 where unmarshal cb = cb . unmarshalAppStatus
361
362 --------------------------------------------------------------------------------
363
364 -- | A keyboard callback
365
366 type KeyboardCallback = Char -> Position -> IO ()
367
368 setKeyboardCallback :: Maybe KeyboardCallback -> IO ()
369 setKeyboardCallback =
370 setCallback KeyboardCB glutKeyboardFunc (makeKeyboardFunc . unmarshal)
371 where unmarshal cb c x y = cb (chr (fromIntegral c))
372 (Position (fromIntegral x) (fromIntegral y))
373
374 -- | Controls the keyboard callback for the /current window/. This is
375 -- activated only when a key is pressed.
376
377 keyboardCallback :: SettableStateVar (Maybe KeyboardCallback)
378 keyboardCallback = makeSettableStateVar setKeyboardCallback
379
380 --------------------------------------------------------------------------------
381
382 setKeyboardUpCallback :: Maybe KeyboardCallback -> IO ()
383 setKeyboardUpCallback =
384 setCallback KeyboardUpCB glutKeyboardUpFunc
385 (makeKeyboardUpFunc . unmarshal)
386 where unmarshal cb c x y = cb (chr (fromIntegral c))
387 (Position (fromIntegral x) (fromIntegral y))
388
389 -- | Controls the keyboard callback for the /current window/. This is
390 -- activated only when a key is released.
391
392 keyboardUpCallback :: SettableStateVar (Maybe KeyboardCallback)
393 keyboardUpCallback = makeSettableStateVar setKeyboardUpCallback
394
395 --------------------------------------------------------------------------------
396
397 -- | Special keys
398
399 data SpecialKey
400 = KeyF1
401 | KeyF2
402 | KeyF3
403 | KeyF4
404 | KeyF5
405 | KeyF6
406 | KeyF7
407 | KeyF8
408 | KeyF9
409 | KeyF10
410 | KeyF11
411 | KeyF12
412 | KeyLeft
413 | KeyUp
414 | KeyRight
415 | KeyDown
416 | KeyPageUp
417 | KeyPageDown
418 | KeyHome
419 | KeyEnd
420 | KeyInsert
421 | KeyNumLock
422 | KeyBegin
423 | KeyDelete
424 | KeyShiftL
425 | KeyShiftR
426 | KeyCtrlL
427 | KeyCtrlR
428 | KeyAltL
429 | KeyAltR
430 | KeyUnknown Int -- ^ You should actually never encounter this value, it is
431 -- just here as a safeguard against future changes in the
432 -- native GLUT library.
433 deriving ( Eq, Ord, Show )
434
435 unmarshalSpecialKey :: CInt -> SpecialKey
436 unmarshalSpecialKey x
437 | x == glut_KEY_F1 = KeyF1
438 | x == glut_KEY_F2 = KeyF2
439 | x == glut_KEY_F3 = KeyF3
440 | x == glut_KEY_F4 = KeyF4
441 | x == glut_KEY_F5 = KeyF5
442 | x == glut_KEY_F6 = KeyF6
443 | x == glut_KEY_F7 = KeyF7
444 | x == glut_KEY_F8 = KeyF8
445 | x == glut_KEY_F9 = KeyF9
446 | x == glut_KEY_F10 = KeyF10
447 | x == glut_KEY_F11 = KeyF11
448 | x == glut_KEY_F12 = KeyF12
449 | x == glut_KEY_LEFT = KeyLeft
450 | x == glut_KEY_UP = KeyUp
451 | x == glut_KEY_RIGHT = KeyRight
452 | x == glut_KEY_DOWN = KeyDown
453 | x == glut_KEY_PAGE_UP = KeyPageUp
454 | x == glut_KEY_PAGE_DOWN = KeyPageDown
455 | x == glut_KEY_HOME = KeyHome
456 | x == glut_KEY_END = KeyEnd
457 | x == glut_KEY_INSERT = KeyInsert
458 | x == glut_KEY_NUM_LOCK = KeyNumLock
459 | x == glut_KEY_BEGIN = KeyBegin
460 | x == glut_KEY_DELETE = KeyDelete
461 | x == glut_KEY_SHIFT_L = KeyShiftL
462 | x == glut_KEY_SHIFT_R = KeyShiftR
463 | x == glut_KEY_CTRL_L = KeyCtrlL
464 | x == glut_KEY_CTRL_R = KeyCtrlR
465 | x == glut_KEY_ALT_L = KeyAltL
466 | x == glut_KEY_ALT_R = KeyAltR
467 | otherwise = KeyUnknown (fromIntegral x)
468
469 --------------------------------------------------------------------------------
470
471 -- | A special key callback
472
473 type SpecialCallback = SpecialKey -> Position -> IO ()
474
475 setSpecialCallback :: Maybe SpecialCallback -> IO ()
476 setSpecialCallback =
477 setCallback SpecialCB glutSpecialFunc (makeSpecialFunc . unmarshal)
478 where unmarshal cb k x y = cb (unmarshalSpecialKey k)
479 (Position (fromIntegral x) (fromIntegral y))
480
481 -- | Controls the special key callback for the /current window/. This is
482 -- activated only when a special key is pressed.
483
484 specialCallback :: SettableStateVar (Maybe SpecialCallback)
485 specialCallback = makeSettableStateVar setSpecialCallback
486
487 --------------------------------------------------------------------------------
488
489 setSpecialUpCallback :: Maybe SpecialCallback -> IO ()
490 setSpecialUpCallback =
491 setCallback SpecialUpCB glutSpecialUpFunc (makeSpecialUpFunc . unmarshal)
492 where unmarshal cb k x y = cb (unmarshalSpecialKey k)
493 (Position (fromIntegral x) (fromIntegral y))
494
495 -- | Controls the special key callback for the /current window/. This is
496 -- activated only when a special key is released.
497
498 specialUpCallback :: SettableStateVar (Maybe SpecialCallback)
499 specialUpCallback = makeSettableStateVar setSpecialUpCallback
500
501 --------------------------------------------------------------------------------
502
503 -- | The current state of a key or button
504
505 data KeyState
506 = Down
507 | Up
508 deriving ( Eq, Ord, Show )
509
510 unmarshalKeyState :: CInt -> KeyState
511 unmarshalKeyState x
512 | x == glut_DOWN = Down
513 | x == glut_UP = Up
514 | otherwise = error ("unmarshalKeyState: illegal value " ++ show x)
515
516 --------------------------------------------------------------------------------
517
518 -- | A mouse callback
519
520 type MouseCallback = MouseButton -> KeyState -> Position -> IO ()
521
522 setMouseCallback :: Maybe MouseCallback -> IO ()
523 setMouseCallback =
524 setCallback MouseCB glutMouseFunc (makeMouseFunc . unmarshal)
525 where unmarshal cb b s x y = cb (unmarshalMouseButton b)
526 (unmarshalKeyState s)
527 (Position (fromIntegral x) (fromIntegral y))
528
529 -- | Controls the mouse callback for the /current window/.
530
531 mouseCallback :: SettableStateVar (Maybe MouseCallback)
532 mouseCallback = makeSettableStateVar setMouseCallback
533
534 --------------------------------------------------------------------------------
535
536 -- | The state of the keyboard modifiers
537
538 data Modifiers = Modifiers { shift, ctrl, alt :: KeyState }
539 deriving ( Eq, Ord, Show )
540
541 -- Could use fromBitfield + Enum/Bounded instances + marshalModifier instead...
542 unmarshalModifiers :: CInt -> Modifiers
543 unmarshalModifiers m = Modifiers {
544 shift = if (m .&. glut_ACTIVE_SHIFT) /= 0 then Down else Up,
545 ctrl = if (m .&. glut_ACTIVE_CTRL ) /= 0 then Down else Up,
546 alt = if (m .&. glut_ACTIVE_ALT ) /= 0 then Down else Up }
547
548 getModifiers :: IO Modifiers
549 getModifiers = fmap unmarshalModifiers glutGetModifiers
550
551 --------------------------------------------------------------------------------
552
553 -- | A generalized view of keys
554
555 data Key
556 = Char Char
557 | SpecialKey SpecialKey
558 | MouseButton MouseButton
559 deriving ( Eq, Ord, Show )
560
561 -- | A keyboard\/mouse callback
562
563 type KeyboardMouseCallback =
564 Key -> KeyState -> Modifiers -> Position -> IO ()
565
566 -- | Controls the keyboard\/mouse callback for the /current window./ The
567 -- keyboard\/mouse callback for a window is called when the state of a key or
568 -- mouse button changes. The callback parameters indicate the new state of the
569 -- key\/button, the state of the keyboard modifiers, and the mouse location in
570 -- window relative coordinates.
571 --
572 -- Note that this is a convenience function that should not ordinarily be used
573 -- in conjunction with `keyboardCallback`, `keyboardUpCallback`,
574 -- `specialCallback`, `specialUpCallback`, or `mouseCallback`.
575
576 keyboardMouseCallback :: SettableStateVar (Maybe KeyboardMouseCallback)
577 keyboardMouseCallback = makeSettableStateVar setKeyboardMouseCallback
578
579 setKeyboardMouseCallback :: Maybe KeyboardMouseCallback -> IO ()
580 setKeyboardMouseCallback Nothing = do
581 setKeyboardCallback Nothing
582 setKeyboardUpCallback Nothing
583 setSpecialCallback Nothing
584 setSpecialUpCallback Nothing
585 setMouseCallback Nothing
586 setKeyboardMouseCallback (Just cb) = do
587 setKeyboardCallback (Just (\c p -> do m <- getModifiers
588 cb (Char c) Down m p))
589 setKeyboardUpCallback (Just (\c p -> do m <- getModifiers
590 cb (Char c) Up m p))
591 setSpecialCallback (Just (\s p -> do m <- getModifiers
592 cb (SpecialKey s) Down m p))
593 setSpecialUpCallback (Just (\s p -> do m <- getModifiers
594 cb (SpecialKey s) Up m p))
595 setMouseCallback (Just (\b s p -> do m <- getModifiers
596 cb (MouseButton b) s m p))
597
598 --------------------------------------------------------------------------------
599
600 type WheelNumber = Int
601
602 type WheelDirection = Int
603
604 type MouseWheelCallback = WheelNumber -> WheelDirection -> Position -> IO ()
605
606 -- | (/freeglut only/) Controls the mouse wheel callback for the
607 -- /current window./ The mouse wheel callback for a window is called when a
608 -- mouse wheel is used and the wheel number is greater than or equal to
609 -- 'Graphics.UI.GLUT.State.numMouseButtons'.
610
611 mouseWheelCallback :: SettableStateVar (Maybe MouseWheelCallback)
612 mouseWheelCallback = makeSettableStateVar $
613 setCallback MouseWheelCB glutMouseWheelFunc (makeMouseWheelFunc . unmarshal)
614 where unmarshal cb n d x y = cb (fromIntegral n) (fromIntegral d)
615 (Position (fromIntegral x) (fromIntegral y))
616
617 --------------------------------------------------------------------------------
618
619 -- | A motion callback
620
621 type MotionCallback = Position -> IO ()
622
623 -- | Controls the motion callback for the /current window./ The motion callback
624 -- for a window is called when the mouse moves within the window while one or
625 -- more mouse buttons are pressed. The callback parameter indicates the mouse
626 -- location in window relative coordinates.
627
628 motionCallback :: SettableStateVar (Maybe MotionCallback)
629 motionCallback = makeSettableStateVar $
630 setCallback MotionCB glutMotionFunc (makeMotionFunc . unmarshal)
631 where unmarshal cb x y = cb (Position (fromIntegral x) (fromIntegral y))
632
633 --------------------------------------------------------------------------------
634
635 -- | Controls the passive motion callback for the /current window./ The passive
636 -- motion callback for a window is called when the mouse moves within the window
637 -- while /no/ mouse buttons are pressed. The callback parameter indicates the
638 -- mouse location in window relative coordinates.
639
640 passiveMotionCallback :: SettableStateVar (Maybe MotionCallback)
641 passiveMotionCallback = makeSettableStateVar $
642 setCallback PassiveMotionCB glutPassiveMotionFunc
643 (makePassiveMotionFunc . unmarshal)
644 where unmarshal cb x y = cb (Position (fromIntegral x) (fromIntegral y))
645
646 --------------------------------------------------------------------------------
647
648 -- | The relation between the mouse pointer and the /current window/ has
649 -- changed.
650
651 data Crossing
652 = WindowLeft -- ^ The mouse pointer has left the /current window./
653 | WindowEntered -- ^ The mouse pointer has entered the /current window./
654 deriving ( Eq, Ord, Show )
655
656 unmarshalCrossing :: CInt -> Crossing
657 unmarshalCrossing x
658 | x == glut_LEFT = WindowLeft
659 | x == glut_ENTERED = WindowEntered
660 | otherwise = error ("unmarshalCrossing: illegal value " ++ show x)
661
662 --------------------------------------------------------------------------------
663
664 -- | An enter\/leave callback
665
666 type CrossingCallback = Crossing -> IO ()
667
668 -- | Controls the mouse enter\/leave callback for the /current window./ Note
669 -- that some window systems may not generate accurate enter\/leave callbacks.
670 --
671 -- /X Implementation Notes:/ An X implementation of GLUT should generate
672 -- accurate enter\/leave callbacks.
673
674 crossingCallback :: SettableStateVar (Maybe CrossingCallback)
675 crossingCallback = makeSettableStateVar $
676 setCallback CrossingCB glutEntryFunc (makeEntryFunc . unmarshal)
677 where unmarshal cb = cb . unmarshalCrossing
678
679 --------------------------------------------------------------------------------
680
681 -- | Translation of the Spaceball along one axis, normalized to be in the range
682 -- of -1000 to +1000 inclusive
683
684 type SpaceballMotion = Int
685
686 -- | Rotation of the Spaceball along one axis, normalized to be in the range
687 -- of -1800 .. +1800 inclusive
688
689 type SpaceballRotation = Int
690
691 -- | The index of a specific buttons of an input device.
692
693 type ButtonIndex = Int
694
695 -- | The state of the Spaceball has changed.
696
697 data SpaceballInput
698 = SpaceballMotion SpaceballMotion SpaceballMotion SpaceballMotion
699 | SpaceballRotation SpaceballRotation SpaceballRotation SpaceballRotation
700 | SpaceballButton ButtonIndex KeyState
701 deriving ( Eq, Ord, Show )
702
703 -- | A SpaceballButton callback
704
705 type SpaceballCallback = SpaceballInput -> IO ()
706
707 -- | Controls the Spaceball callback for the /current window./ The Spaceball
708 -- callback for a window is called when the window has Spaceball input focus
709 -- (normally, when the mouse is in the window) and the user generates Spaceball
710 -- translations, rotations, or button presses. The number of available Spaceball
711 -- buttons can be determined with 'Graphics.UI.GLUT.State.numSpaceballButtons'.
712 --
713 -- Registering a Spaceball callback when a Spaceball device is not available has
714 -- no effect and is not an error. In this case, no Spaceball callbacks will be
715 -- generated.
716
717 spaceballCallback :: SettableStateVar (Maybe SpaceballCallback)
718 spaceballCallback = makeSettableStateVar setSpaceballCallback
719
720 setSpaceballCallback :: Maybe SpaceballCallback -> IO ()
721 setSpaceballCallback Nothing = do
722 setSpaceballMotionCallback Nothing
723 setSpaceballRotationCallback Nothing
724 setSpaceballButtonCallback Nothing
725 setSpaceballCallback (Just cb) = do
726 setSpaceballMotionCallback (Just (\x y z -> cb (SpaceballMotion x y z)))
727 setSpaceballRotationCallback (Just (\x y z -> cb (SpaceballRotation x y z)))
728 setSpaceballButtonCallback (Just (\b s -> cb (SpaceballButton b s)))
729
730 --------------------------------------------------------------------------------
731
732 type SpaceballMotionCallback =
733 SpaceballMotion -> SpaceballMotion -> SpaceballMotion -> IO ()
734
735 setSpaceballMotionCallback :: Maybe SpaceballMotionCallback -> IO ()
736 setSpaceballMotionCallback =
737 setCallback SpaceballMotionCB glutSpaceballMotionFunc
738 (makeSpaceballMotionFunc . unmarshal)
739 where unmarshal cb x y z =
740 cb (fromIntegral x) (fromIntegral y) (fromIntegral z)
741
742 --------------------------------------------------------------------------------
743
744 type SpaceballRotationCallback =
745 SpaceballRotation -> SpaceballRotation -> SpaceballRotation -> IO ()
746
747 setSpaceballRotationCallback :: Maybe SpaceballRotationCallback -> IO ()
748 setSpaceballRotationCallback =
749 setCallback SpaceballRotateCB glutSpaceballRotateFunc
750 (makeSpaceballRotateFunc . unmarshal)
751 where unmarshal cb x y z =
752 cb (fromIntegral x) (fromIntegral y) (fromIntegral z)
753
754 --------------------------------------------------------------------------------
755
756 type SpaceballButtonCallback = ButtonIndex -> KeyState -> IO ()
757
758 setSpaceballButtonCallback :: Maybe SpaceballButtonCallback -> IO ()
759 setSpaceballButtonCallback =
760 setCallback SpaceballButtonCB glutSpaceballButtonFunc
761 (makeSpaceballButtonFunc . unmarshal)
762 where unmarshal cb b s = cb (fromIntegral b) (unmarshalKeyState s)
763
764 --------------------------------------------------------------------------------
765
766 -- | The index of a specific dial of a dial and button box.
767
768 type DialIndex = Int
769
770 -- | The dial & button box state has changed.
771
772 data DialAndButtonBoxInput
773 = DialAndButtonBoxButton ButtonIndex KeyState
774 | DialAndButtonBoxDial DialIndex Int
775 deriving ( Eq, Ord, Show )
776
777 -- | A dial & button box callback
778
779 type DialAndButtonBoxCallback = DialAndButtonBoxInput -> IO ()
780
781 -- | Controls the dial & button box callback for the /current window./ The dial
782 -- & button box button callback for a window is called when the window has dial
783 -- & button box input focus (normally, when the mouse is in the window) and the
784 -- user generates dial & button box button presses or dial changes. The number
785 -- of available dial & button box buttons and dials can be determined with
786 -- 'Graphics.UI.GLUT.State.numDialsAndButtons'.
787 --
788 -- Registering a dial & button box callback when a dial & button box device is
789 -- not available is ineffectual and not an error. In this case, no dial & button
790 -- box button will be generated.
791
792 dialAndButtonBoxCallback :: SettableStateVar (Maybe DialAndButtonBoxCallback)
793 dialAndButtonBoxCallback = makeSettableStateVar setDialAndButtonBoxCallback
794
795 setDialAndButtonBoxCallback :: Maybe DialAndButtonBoxCallback -> IO ()
796 setDialAndButtonBoxCallback Nothing = do
797 setButtonBoxCallback Nothing
798 setDialsCallback Nothing
799 setDialAndButtonBoxCallback (Just cb) = do
800 setButtonBoxCallback (Just (\b s -> cb (DialAndButtonBoxButton b s)))
801 setDialsCallback (Just (\d x -> cb (DialAndButtonBoxDial d x)))
802
803 --------------------------------------------------------------------------------
804
805 type ButtonBoxCallback = ButtonIndex -> KeyState -> IO ()
806
807 setButtonBoxCallback :: Maybe ButtonBoxCallback -> IO ()
808 setButtonBoxCallback =
809 setCallback ButtonBoxCB glutButtonBoxFunc (makeButtonBoxFunc . unmarshal)
810 where unmarshal cb b s = cb (fromIntegral b) (unmarshalKeyState s)
811
812 --------------------------------------------------------------------------------
813
814 type DialsCallback = DialIndex -> Int -> IO ()
815
816 setDialsCallback :: Maybe DialsCallback -> IO ()
817 setDialsCallback =
818 setCallback DialsCB glutDialsFunc (makeDialsFunc . unmarshal)
819 where unmarshal cb d x = cb (fromIntegral d) (fromIntegral x)
820
821 --------------------------------------------------------------------------------
822
823 -- | Absolute tablet position, with coordinates normalized to be in the range of
824 -- 0 to 2000 inclusive
825
826 data TabletPosition = TabletPosition Int Int
827 deriving ( Eq, Ord, Show )
828
829 -- | The table state has changed.
830
831 data TabletInput
832 = TabletMotion
833 | TabletButton ButtonIndex KeyState
834 deriving ( Eq, Ord, Show )
835
836 -- | A tablet callback
837
838 type TabletCallback = TabletInput -> TabletPosition -> IO ()
839
840 -- | Controls the tablet callback for the /current window./ The tablet callback
841 -- for a window is called when the window has tablet input focus (normally, when
842 -- the mouse is in the window) and the user generates tablet motion or button
843 -- presses. The number of available tablet buttons can be determined with
844 -- 'Graphics.UI.GLUT.State.numTabletButtons'.
845 --
846 -- Registering a tablet callback when a tablet device is not available is
847 -- ineffectual and not an error. In this case, no tablet callbacks will be
848 -- generated.
849
850 tabletCallback :: SettableStateVar (Maybe TabletCallback)
851 tabletCallback = makeSettableStateVar setTabletCallback
852
853 setTabletCallback :: Maybe TabletCallback -> IO ()
854 setTabletCallback Nothing = do
855 setTabletMotionCallback Nothing
856 setTabletButtonCallback Nothing
857 setTabletCallback (Just cb) = do
858 setTabletMotionCallback (Just (\p -> cb TabletMotion p))
859 setTabletButtonCallback (Just (\b s p -> cb (TabletButton b s) p))
860
861 --------------------------------------------------------------------------------
862
863 type TabletMotionCallback = TabletPosition -> IO ()
864
865 setTabletMotionCallback :: Maybe TabletMotionCallback -> IO ()
866 setTabletMotionCallback =
867 setCallback TabletMotionCB glutTabletMotionFunc
868 (makeTabletMotionFunc . unmarshal)
869 where unmarshal cb x y =
870 cb (TabletPosition (fromIntegral x) (fromIntegral y))
871
872 --------------------------------------------------------------------------------
873
874 type TabletButtonCallback = ButtonIndex -> KeyState -> TabletPosition -> IO ()
875
876 setTabletButtonCallback :: Maybe TabletButtonCallback -> IO ()
877 setTabletButtonCallback =
878 setCallback TabletButtonCB glutTabletButtonFunc
879 (makeTabletButtonFunc . unmarshal)
880 where unmarshal cb b s x y =
881 cb (fromIntegral b) (unmarshalKeyState s)
882 (TabletPosition (fromIntegral x) (fromIntegral y))
883
884 --------------------------------------------------------------------------------
885
886 -- | The state of the joystick buttons
887
888 data JoystickButtons = JoystickButtons {
889 joystickButtonA, joystickButtonB,
890 joystickButtonC, joystickButtonD :: KeyState }
891 deriving ( Eq, Ord, Show )
892
893 -- Could use fromBitfield + Enum/Bounded instances + unmarshalJoystickButton
894 -- instead...
895 unmarshalJoystickButtons :: CUInt -> JoystickButtons
896 unmarshalJoystickButtons m = JoystickButtons {
897 joystickButtonA = if (m .&. glut_JOYSTICK_BUTTON_A) /= 0 then Down else Up,
898 joystickButtonB = if (m .&. glut_JOYSTICK_BUTTON_B) /= 0 then Down else Up,
899 joystickButtonC = if (m .&. glut_JOYSTICK_BUTTON_C) /= 0 then Down else Up,
900 joystickButtonD = if (m .&. glut_JOYSTICK_BUTTON_D) /= 0 then Down else Up }
901
902 --------------------------------------------------------------------------------
903
904 -- | Absolute joystick position, with coordinates normalized to be in the range
905 -- of -1000 to 1000 inclusive. The signs of the three axes mean the following:
906 --
907 -- * negative = left, positive = right
908 --
909 -- * negative = towards player, positive = away
910 --
911 -- * if available (e.g. rudder): negative = down, positive = up
912
913 data JoystickPosition = JoystickPosition Int Int Int
914 deriving ( Eq, Ord, Show )
915
916 --------------------------------------------------------------------------------
917
918 -- | A joystick callback
919
920 type JoystickCallback = JoystickButtons -> JoystickPosition -> IO ()
921
922 -- | Controls the joystick callback for the /current window./ The joystick
923 -- callback is called either due to polling of the joystick at the uniform timer
924 -- interval specified (if > 0) or in response to an explicit call of
925 -- 'Graphics.UI.GLUT.DeviceControl.forceJoystickCallback'.
926 --
927 -- /X Implementation Notes:/ Currently GLUT has no joystick support for X11.
928
929 -- joystickCallback :: SettableStateVar (Maybe JoystickCallback, PollRate)
930 joystickCallback :: SettableStateVar (Maybe (JoystickCallback, PollRate))
931 joystickCallback =
932 makeSettableStateVar $ \maybeCBAndRate ->
933 setCallback JoystickCB
934 (\f -> glutJoystickFunc f (fromIntegral (snd (fromJust maybeCBAndRate))))
935 (makeJoystickFunc . unmarshal)
936 (fmap fst maybeCBAndRate)
937 where unmarshal cb b x y z = cb (unmarshalJoystickButtons b)
938 (JoystickPosition (fromIntegral x)
939 (fromIntegral y)
940 (fromIntegral z))
941
942 --------------------------------------------------------------------------------
943
944 -- | A description where the multi-touch event is coming from, the freeglut
945 -- specs are very vague about the actual semantics. It contains the device ID
946 -- and\/or the cursor\/finger ID.
947
948 type TouchID = Int
949
950 -- | A multi-touch variant of 'MouseCallback'.
951
952 type MultiMouseCallback = TouchID -> MouseCallback
953
954 -- | (/freeglut only/) A multi-touch variant of 'mouseCallback'.
955
956 multiMouseCallback :: SettableStateVar (Maybe MultiMouseCallback)
957 multiMouseCallback = makeSettableStateVar $
958 setCallback MultiButtonCB glutMultiButtonFunc (makeMultiButtonFunc . unmarshal)
959 where unmarshal cb d x y b s = cb (fromIntegral d)
960 (unmarshalMouseButton b)
961 (unmarshalKeyState s)
962 (Position (fromIntegral x) (fromIntegral y))
963
964 -- | A multi-touch variant of 'CrossingCallback'.
965
966 type MultiCrossingCallback = TouchID -> CrossingCallback
967
968 -- | (/freeglut only/) A multi-touch variant of 'crossingCallback'.
969
970 multiCrossingCallback :: SettableStateVar (Maybe MultiCrossingCallback)
971 multiCrossingCallback = makeSettableStateVar $
972 setCallback MultiEntryCB glutMultiEntryFunc (makeMultiEntryFunc . unmarshal)
973 where unmarshal cb d c = cb (fromIntegral d) (unmarshalCrossing c)
974
975 -- | A multi-touch variant of 'MotionCallback'.
976
977 type MultiMotionCallback = TouchID -> MotionCallback
978
979 -- | (/freeglut only/) A multi-touch variant of 'motionCallback'.
980
981 multiMotionCallback :: SettableStateVar (Maybe MultiMotionCallback)
982 multiMotionCallback = makeSettableStateVar $
983 setCallback MultiMotionCB glutMultiMotionFunc (makeMultiMotionFunc . unmarshal)
984 where unmarshal cb d x y =
985 cb (fromIntegral d) (Position (fromIntegral x) (fromIntegral y))
986
987 -- | (/freeglut only/) A multi-touch variant of 'passiveMotionCallback'.
988
989 multiPassiveMotionCallback :: SettableStateVar (Maybe MultiMotionCallback)
990 multiPassiveMotionCallback = makeSettableStateVar $
991 setCallback MultiPassiveCB glutMultiPassiveFunc (makeMultiPassiveFunc . unmarshal)
992 where unmarshal cb d x y =
993 cb (fromIntegral d) (Position (fromIntegral x) (fromIntegral y))