Debian Codegraph @

src/Graphics/UI/GLUT/DeviceControl.hs

raw
1 --------------------------------------------------------------------------------
2 -- |
3 -- Module : Graphics.UI.GLUT.DeviceControl
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 offers some routines for controlling the key repeat and polling the
12 -- joystick.
13 --
14 --------------------------------------------------------------------------------
15
16 module Graphics.UI.GLUT.DeviceControl (
17 GlobalKeyRepeat(..), globalKeyRepeat,
18 PerWindowKeyRepeat(..), perWindowKeyRepeat,
19 forceJoystickCallback
20 ) where
21
22 import Control.Monad.IO.Class ( MonadIO(..) )
23 import Data.StateVar ( StateVar, makeStateVar )
24 import Foreign.C.Types ( CInt )
25
26 import Graphics.UI.GLUT.QueryUtils
27 import Graphics.UI.GLUT.Raw
28
29 --------------------------------------------------------------------------------
30
31 -- | The state of the global key repeat
32
33 data GlobalKeyRepeat
34 = GlobalKeyRepeatOff
35 | GlobalKeyRepeatOn
36 | GlobalKeyRepeatDefault
37 deriving ( Eq, Ord, Show )
38
39 marshalGlobalKeyRepeat :: GlobalKeyRepeat -> CInt
40 marshalGlobalKeyRepeat x = case x of
41 GlobalKeyRepeatOff -> glut_KEY_REPEAT_OFF
42 GlobalKeyRepeatOn -> glut_KEY_REPEAT_ON
43 GlobalKeyRepeatDefault -> glut_KEY_REPEAT_DEFAULT
44
45 unmarshalGlobalKeyRepeat :: CInt -> GlobalKeyRepeat
46 unmarshalGlobalKeyRepeat x
47 | x == glut_KEY_REPEAT_OFF = GlobalKeyRepeatOff
48 | x == glut_KEY_REPEAT_ON = GlobalKeyRepeatOn
49 | x == glut_KEY_REPEAT_DEFAULT = GlobalKeyRepeatDefault
50 | otherwise = error ("unmarshalGlobalKeyRepeat: illegal value " ++ show x)
51
52 --------------------------------------------------------------------------------
53
54 -- | Controls the key repeat mode for the window system on a global basis if
55 -- possible. If supported by the window system, the key repeat can either be
56 -- disabled, enabled, or set to the window system\'s default key repeat state.
57 --
58 -- /X Implementation Notes:/ X11 sends @KeyPress@ events repeatedly when the
59 -- window system\'s global auto repeat is enabled. 'perWindowKeyRepeat' can
60 -- prevent these auto repeated keystrokes from being reported as keyboard or
61 -- special callbacks, but there is still some minimal overhead by the X server
62 -- to continually stream @KeyPress@ events to the GLUT application. The
63 -- 'globalKeyRepeat' state variable can be used to actually disable the global
64 -- sending of auto repeated @KeyPress@ events. Note that 'globalKeyRepeat'
65 -- affects the global window system auto repeat state so other applications
66 -- will not auto repeat if you disable auto repeat globally through
67 -- 'globalKeyRepeat'. GLUT applications using the X11 GLUT implementation
68 -- should disable key repeat with 'globalKeyRepeat' to disable key repeats most
69 -- efficiently, but are responsible for explicitly restoring the default key
70 -- repeat state on exit.
71 --
72 -- /Win32 Implementation Notes:/ The Win32 implementation of 'globalKeyRepeat'
73 -- does nothing. The 'perWindowKeyRepeat' can be used in the Win32 GLUT
74 -- implementation to ignore repeated keys on a per-window basis without changing
75 -- the global window system key repeat.
76
77 globalKeyRepeat :: StateVar GlobalKeyRepeat
78 globalKeyRepeat =
79 makeStateVar (deviceGet unmarshalGlobalKeyRepeat glut_DEVICE_KEY_REPEAT)
80 (glutSetKeyRepeat . marshalGlobalKeyRepeat)
81
82 --------------------------------------------------------------------------------
83
84 -- | The state of the per-window key repeat
85
86 data PerWindowKeyRepeat
87 = PerWindowKeyRepeatOff
88 | PerWindowKeyRepeatOn
89 deriving ( Eq, Ord, Show )
90
91 marshalPerWindowKeyRepeat :: PerWindowKeyRepeat -> CInt
92 marshalPerWindowKeyRepeat x = case x of
93 PerWindowKeyRepeatOn -> 0
94 PerWindowKeyRepeatOff -> 1
95
96 unmarshalPerWindowKeyRepeat :: CInt -> PerWindowKeyRepeat
97 unmarshalPerWindowKeyRepeat x
98 | x == 0 = PerWindowKeyRepeatOn
99 | otherwise = PerWindowKeyRepeatOff
100
101 --------------------------------------------------------------------------------
102
103 -- | Controls if auto repeat keystrokes are reported to the /current window./
104 -- Ignoring auto repeated keystrokes is generally done in conjunction with using
105 -- the 'Graphics.UI.GLUT.Callbacks.Window.keyboardMouseCallback'. If you do
106 -- not ignore auto repeated keystrokes, your GLUT application will experience
107 -- repeated release\/press callbacks. Games using the keyboard will typically
108 -- want to ignore key repeat.
109
110 perWindowKeyRepeat :: StateVar PerWindowKeyRepeat
111 perWindowKeyRepeat =
112 makeStateVar
113 (deviceGet unmarshalPerWindowKeyRepeat glut_DEVICE_IGNORE_KEY_REPEAT)
114 (glutIgnoreKeyRepeat . marshalPerWindowKeyRepeat)
115
116 --------------------------------------------------------------------------------
117
118 -- | Execute the joystick callback set by
119 -- 'Graphics.UI.GLUT.Callbacks.Window.joystickCallback' once (if one exists).
120 -- This is done in a synchronous fashion within the current context, i.e. when
121 -- 'forceJoystickCallback' returns, the callback will have already happened.
122
123 forceJoystickCallback :: MonadIO m => m ()
124 forceJoystickCallback = glutForceJoystickFunc