1 //========================================================================
2 // GLFW - An OpenGL framework
3 // Platform:    Any
4 // API version: 2.7
5 // WWW:         http://www.glfw.org/
6 //------------------------------------------------------------------------
7 // Copyright (c) 2002-2006 Marcus Geelnard
8 // Copyright (c) 2006-2010 Camilla Berglund <elmindreda@elmindreda.org>
9 //
10 // This software is provided 'as-is', without any express or implied
11 // warranty. In no event will the authors be held liable for any damages
12 // arising from the use of this software.
13 //
14 // Permission is granted to anyone to use this software for any purpose,
15 // including commercial applications, and to alter it and redistribute it
16 // freely, subject to the following restrictions:
17 //
18 // 1. The origin of this software must not be misrepresented; you must not
19 //    claim that you wrote the original software. If you use this software
20 //    in a product, an acknowledgment in the product documentation would
21 //    be appreciated but is not required.
22 //
23 // 2. Altered source versions must be plainly marked as such, and must not
24 //    be misrepresented as being the original software.
25 //
26 // 3. This notice may not be removed or altered from any source
27 //    distribution.
28 //
29 //========================================================================
30 
31 #include "internal.h"
32 
33 
34 //************************************************************************
35 //****                  GLFW internal functions                       ****
36 //************************************************************************
37 
38 //========================================================================
39 // Enable (show) mouse cursor
40 //========================================================================
41 
enableMouseCursor(void)42 static void enableMouseCursor( void )
43 {
44     int centerPosX, centerPosY;
45 
46     if( !_glfwWin.opened || !_glfwWin.mouseLock )
47     {
48         return;
49     }
50 
51     if( _glfwWin.active )
52     {
53         // Show mouse cursor
54         _glfwPlatformShowMouseCursor();
55 
56         centerPosX = _glfwWin.width / 2;
57         centerPosY = _glfwWin.height / 2;
58 
59         if( centerPosX != _glfwInput.MousePosX || centerPosY != _glfwInput.MousePosY )
60         {
61             _glfwPlatformSetMouseCursorPos( centerPosX, centerPosY );
62 
63             _glfwInput.MousePosX = centerPosX;
64             _glfwInput.MousePosY = centerPosY;
65 
66             if( _glfwWin.mousePosCallback )
67             {
68                 _glfwWin.mousePosCallback( _glfwInput.MousePosX,
69                                         _glfwInput.MousePosY );
70             }
71         }
72     }
73 
74     // From now on the mouse is unlocked
75     _glfwWin.mouseLock = GL_FALSE;
76 }
77 
78 //========================================================================
79 // Disable (hide) mouse cursor
80 //========================================================================
81 
disableMouseCursor(void)82 static void disableMouseCursor( void )
83 {
84     if( !_glfwWin.opened || _glfwWin.mouseLock )
85     {
86         return;
87     }
88 
89     // Hide mouse cursor
90     if( _glfwWin.active )
91     {
92         _glfwPlatformHideMouseCursor();
93     }
94 
95     // From now on the mouse is locked
96     _glfwWin.mouseLock = GL_TRUE;
97 }
98 
99 
100 //========================================================================
101 // Enable sticky keys
102 //========================================================================
103 
enableStickyKeys(void)104 static void enableStickyKeys( void )
105 {
106     _glfwInput.StickyKeys = 1;
107 }
108 
109 //========================================================================
110 // Disable sticky keys
111 //========================================================================
112 
disableStickyKeys(void)113 static void disableStickyKeys( void )
114 {
115     int i;
116 
117     _glfwInput.StickyKeys = 0;
118 
119     // Release all sticky keys
120     for( i = 0; i <= GLFW_KEY_LAST; i++ )
121     {
122         if( _glfwInput.Key[ i ] == 2 )
123         {
124             _glfwInput.Key[ i ] = 0;
125         }
126     }
127 }
128 
129 
130 //========================================================================
131 // Enable sticky mouse buttons
132 //========================================================================
133 
enableStickyMouseButtons(void)134 static void enableStickyMouseButtons( void )
135 {
136     _glfwInput.StickyMouseButtons = 1;
137 }
138 
139 //========================================================================
140 // Disable sticky mouse buttons
141 //========================================================================
142 
disableStickyMouseButtons(void)143 static void disableStickyMouseButtons( void )
144 {
145     int i;
146 
147     _glfwInput.StickyMouseButtons = 0;
148 
149     // Release all sticky mouse buttons
150     for( i = 0; i <= GLFW_MOUSE_BUTTON_LAST; i++ )
151     {
152         if( _glfwInput.MouseButton[ i ] == 2 )
153         {
154             _glfwInput.MouseButton[ i ] = 0;
155         }
156     }
157 }
158 
159 
160 //========================================================================
161 // Enable system keys
162 //========================================================================
163 
enableSystemKeys(void)164 static void enableSystemKeys( void )
165 {
166     if( !_glfwWin.sysKeysDisabled )
167     {
168         return;
169     }
170 
171     _glfwPlatformEnableSystemKeys();
172 
173     // Indicate that system keys are no longer disabled
174     _glfwWin.sysKeysDisabled = GL_FALSE;
175 }
176 
177 //========================================================================
178 // Disable system keys
179 //========================================================================
180 
disableSystemKeys(void)181 static void disableSystemKeys( void )
182 {
183     if( _glfwWin.sysKeysDisabled )
184     {
185         return;
186     }
187 
188     _glfwPlatformDisableSystemKeys();
189 
190     // Indicate that system keys are now disabled
191     _glfwWin.sysKeysDisabled = GL_TRUE;
192 }
193 
194 
195 //========================================================================
196 // Enable key repeat
197 //========================================================================
198 
enableKeyRepeat(void)199 static void enableKeyRepeat( void )
200 {
201     _glfwInput.KeyRepeat = 1;
202 }
203 
204 //========================================================================
205 // Disable key repeat
206 //========================================================================
207 
disableKeyRepeat(void)208 static void disableKeyRepeat( void )
209 {
210     _glfwInput.KeyRepeat = 0;
211 }
212 
213 
214 //========================================================================
215 // Enable automatic event polling
216 //========================================================================
217 
enableAutoPollEvents(void)218 static void enableAutoPollEvents( void )
219 {
220     _glfwWin.autoPollEvents = 1;
221 }
222 
223 //========================================================================
224 // Disable automatic event polling
225 //========================================================================
226 
disableAutoPollEvents(void)227 static void disableAutoPollEvents( void )
228 {
229     _glfwWin.autoPollEvents = 0;
230 }
231 
232 
233 
234 //************************************************************************
235 //****                    GLFW user functions                         ****
236 //************************************************************************
237 
238 //========================================================================
239 // Enable certain GLFW/window/system functions.
240 //========================================================================
241 
glfwEnable(int token)242 GLFWAPI void GLFWAPIENTRY glfwEnable( int token )
243 {
244     // Is GLFW initialized?
245     if( !_glfwInitialized )
246     {
247         return;
248     }
249 
250     switch( token )
251     {
252         case GLFW_MOUSE_CURSOR:
253             enableMouseCursor();
254             break;
255         case GLFW_STICKY_KEYS:
256             enableStickyKeys();
257             break;
258         case GLFW_STICKY_MOUSE_BUTTONS:
259             enableStickyMouseButtons();
260             break;
261         case GLFW_SYSTEM_KEYS:
262             enableSystemKeys();
263             break;
264         case GLFW_KEY_REPEAT:
265             enableKeyRepeat();
266             break;
267         case GLFW_AUTO_POLL_EVENTS:
268             enableAutoPollEvents();
269             break;
270         default:
271             break;
272     }
273 }
274 
275 
276 //========================================================================
277 // Disable certain GLFW/window/system functions.
278 //========================================================================
279 
glfwDisable(int token)280 GLFWAPI void GLFWAPIENTRY glfwDisable( int token )
281 {
282     // Is GLFW initialized?
283     if( !_glfwInitialized )
284     {
285         return;
286     }
287 
288     switch( token )
289     {
290         case GLFW_MOUSE_CURSOR:
291             disableMouseCursor();
292             break;
293         case GLFW_STICKY_KEYS:
294             disableStickyKeys();
295             break;
296         case GLFW_STICKY_MOUSE_BUTTONS:
297             disableStickyMouseButtons();
298             break;
299         case GLFW_SYSTEM_KEYS:
300             disableSystemKeys();
301             break;
302         case GLFW_KEY_REPEAT:
303             disableKeyRepeat();
304             break;
305         case GLFW_AUTO_POLL_EVENTS:
306             disableAutoPollEvents();
307             break;
308         default:
309             break;
310     }
311 }
312 
313