2 * freeglut_init_mswin.c
\r
4 * The Windows-specific mouse cursor related stuff.
\r
6 * Copyright (c) 2012 Stephen J. Baker. All Rights Reserved.
\r
7 * Written by John F. Fay, <fayjf@sourceforge.net>
\r
8 * Creation date: Thu Jan 19, 2012
\r
10 * Permission is hereby granted, free of charge, to any person obtaining a
\r
11 * copy of this software and associated documentation files (the "Software"),
\r
12 * to deal in the Software without restriction, including without limitation
\r
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
\r
14 * and/or sell copies of the Software, and to permit persons to whom the
\r
15 * Software is furnished to do so, subject to the following conditions:
\r
17 * The above copyright notice and this permission notice shall be included
\r
18 * in all copies or substantial portions of the Software.
\r
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
\r
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
\r
23 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
24 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
28 #define FREEGLUT_BUILDING_LIB
\r
29 #include <GL/freeglut.h>
\r
30 #include "../Common/freeglut_internal.h"
\r
34 extern LRESULT CALLBACK fgPlatformWindowProc( HWND hWnd, UINT uMsg,
\r
35 WPARAM wParam, LPARAM lParam );
\r
39 * A call to this function should initialize all the display stuff...
\r
41 void fgPlatformInitialize( const char* displayName )
\r
46 /* What we need to do is to initialize the fgDisplay global structure here. */
\r
47 fgDisplay.pDisplay.Instance = GetModuleHandle( NULL );
\r
48 fgDisplay.pDisplay.DisplayName= displayName ? strdup(displayName) : 0 ;
\r
49 atom = GetClassInfo( fgDisplay.pDisplay.Instance, _T("FREEGLUT"), &wc );
\r
53 ZeroMemory( &wc, sizeof(WNDCLASS) );
\r
56 * Each of the windows should have its own device context, and we
\r
57 * want redraw events during Vertical and Horizontal Resizes by
\r
60 * XXX Old code had "| CS_DBCLCKS" commented out. Plans for the
\r
61 * XXX future? Dead-end idea?
\r
63 wc.lpfnWndProc = fgPlatformWindowProc;
\r
66 wc.hInstance = fgDisplay.pDisplay.Instance;
\r
67 wc.hIcon = LoadIcon( fgDisplay.pDisplay.Instance, _T("GLUT_ICON") );
\r
69 #if defined(_WIN32_WCE)
\r
70 wc.style = CS_HREDRAW | CS_VREDRAW;
\r
72 wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
\r
74 wc.hIcon = LoadIcon( NULL, IDI_WINLOGO );
\r
77 wc.hCursor = LoadCursor( NULL, IDC_ARROW );
\r
78 wc.hbrBackground = NULL;
\r
79 wc.lpszMenuName = NULL;
\r
80 wc.lpszClassName = _T("FREEGLUT");
\r
82 /* Register the window class */
\r
83 atom = RegisterClass( &wc );
\r
84 FREEGLUT_INTERNAL_ERROR_EXIT ( atom, "Window Class Not Registered", "fgPlatformInitialize" );
\r
87 /* The screen dimensions can be obtained via GetSystemMetrics() calls */
\r
88 fgDisplay.ScreenWidth = GetSystemMetrics( SM_CXSCREEN );
\r
89 fgDisplay.ScreenHeight = GetSystemMetrics( SM_CYSCREEN );
\r
92 HWND desktop = GetDesktopWindow( );
\r
93 HDC context = GetDC( desktop );
\r
95 fgDisplay.ScreenWidthMM = GetDeviceCaps( context, HORZSIZE );
\r
96 fgDisplay.ScreenHeightMM = GetDeviceCaps( context, VERTSIZE );
\r
98 ReleaseDC( desktop, context );
\r
100 /* If we have a DisplayName try to use it for metrics */
\r
101 if( fgDisplay.pDisplay.DisplayName )
\r
103 HDC context = CreateDC(fgDisplay.pDisplay.DisplayName,0,0,0);
\r
106 fgDisplay.ScreenWidth = GetDeviceCaps( context, HORZRES );
\r
107 fgDisplay.ScreenHeight = GetDeviceCaps( context, VERTRES );
\r
108 fgDisplay.ScreenWidthMM = GetDeviceCaps( context, HORZSIZE );
\r
109 fgDisplay.ScreenHeightMM = GetDeviceCaps( context, VERTSIZE );
\r
113 fgWarning("fgPlatformInitialize: "
\r
114 "CreateDC failed, Screen size info may be incorrect\n"
\r
115 "This is quite likely caused by a bad '-display' parameter");
\r
118 /* Set the timer granularity to 1 ms */
\r
119 timeBeginPeriod ( 1 );
\r
122 fgState.Initialised = GL_TRUE;
\r
124 /* Avoid registering atexit callback on Win32 as it results in an access
\r
125 * violation due to calling into a module which has been unloaded.
\r
126 * Any cleanup isn't needed on Windows anyway, the OS takes care of it.c
\r
127 * see: http://blogs.msdn.com/b/oldnewthing/archive/2012/01/05/10253268.aspx
\r
129 /* atexit(fgDeinitialize); */
\r
131 /* InputDevice uses GlutTimerFunc(), so fgState.Initialised must be TRUE */
\r
132 fgInitialiseInputDevices();
\r
137 /* Platform-Specific Deinitialization Functions: */
\r
138 extern void fghCloseInputDevices ( void );
\r
140 void fgPlatformDeinitialiseInputDevices ( void )
\r
142 #if !defined(_WIN32_WCE)
\r
143 fghCloseInputDevices ();
\r
144 #endif /* !defined(_WIN32_WCE) */
\r
145 fgState.JoysticksInitialised = GL_FALSE;
\r
146 fgState.InputDevsInitialised = GL_FALSE;
\r
149 void fgPlatformCloseDisplay ( void )
\r
151 if( fgDisplay.pDisplay.DisplayName )
\r
153 free( fgDisplay.pDisplay.DisplayName );
\r
154 fgDisplay.pDisplay.DisplayName = NULL;
\r
157 /* Reset the timer granularity */
\r
158 timeEndPeriod ( 1 );
\r
164 * Everything down to the end of the next two functions is copied from the X sources.
\r
169 Copyright 1985, 1986, 1987,1998 The Open Group
\r
171 Permission to use, copy, modify, distribute, and sell this software and its
\r
172 documentation for any purpose is hereby granted without fee, provided that
\r
173 the above copyright notice appear in all copies and that both that
\r
174 copyright notice and this permission notice appear in supporting
\r
177 The above copyright notice and this permission notice shall be included
\r
178 in all copies or substantial portions of the Software.
\r
180 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
\r
181 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
182 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
\r
183 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
\r
184 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
\r
185 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
\r
186 OTHER DEALINGS IN THE SOFTWARE.
\r
188 Except as contained in this notice, the name of The Open Group shall
\r
189 not be used in advertising or otherwise to promote the sale, use or
\r
190 other dealings in this Software without prior written authorization
\r
191 from The Open Group.
\r
195 #define NoValue 0x0000
\r
196 #define XValue 0x0001
\r
197 #define YValue 0x0002
\r
198 #define WidthValue 0x0004
\r
199 #define HeightValue 0x0008
\r
200 #define AllValues 0x000F
\r
201 #define XNegative 0x0010
\r
202 #define YNegative 0x0020
\r
205 * XParseGeometry parses strings of the form
\r
206 * "=<width>x<height>{+-}<xoffset>{+-}<yoffset>", where
\r
207 * width, height, xoffset, and yoffset are unsigned integers.
\r
208 * Example: "=80x24+300-49"
\r
209 * The equal sign is optional.
\r
210 * It returns a bitmask that indicates which of the four values
\r
211 * were actually found in the string. For each value found,
\r
212 * the corresponding argument is updated; for each value
\r
213 * not found, the corresponding argument is left unchanged.
\r
217 ReadInteger(char *string, char **NextString)
\r
219 register int Result = 0;
\r
222 if (*string == '+')
\r
224 else if (*string == '-')
\r
229 for (; (*string >= '0') && (*string <= '9'); string++)
\r
231 Result = (Result * 10) + (*string - '0');
\r
233 *NextString = string;
\r
240 int XParseGeometry (
\r
241 const char *string,
\r
244 unsigned int *width, /* RETURN */
\r
245 unsigned int *height) /* RETURN */
\r
247 int mask = NoValue;
\r
248 register char *strind;
\r
249 unsigned int tempWidth = 0, tempHeight = 0;
\r
250 int tempX = 0, tempY = 0;
\r
251 char *nextCharacter;
\r
253 if ( (string == NULL) || (*string == '\0'))
\r
255 if (*string == '=')
\r
256 string++; /* ignore possible '=' at beg of geometry spec */
\r
258 strind = (char *)string;
\r
259 if (*strind != '+' && *strind != '-' && *strind != 'x') {
\r
260 tempWidth = ReadInteger(strind, &nextCharacter);
\r
261 if (strind == nextCharacter)
\r
263 strind = nextCharacter;
\r
264 mask |= WidthValue;
\r
267 if (*strind == 'x' || *strind == 'X') {
\r
269 tempHeight = ReadInteger(strind, &nextCharacter);
\r
270 if (strind == nextCharacter)
\r
272 strind = nextCharacter;
\r
273 mask |= HeightValue;
\r
276 if ((*strind == '+') || (*strind == '-')) {
\r
277 if (*strind == '-') {
\r
279 tempX = -ReadInteger(strind, &nextCharacter);
\r
280 if (strind == nextCharacter)
\r
282 strind = nextCharacter;
\r
288 tempX = ReadInteger(strind, &nextCharacter);
\r
289 if (strind == nextCharacter)
\r
291 strind = nextCharacter;
\r
294 if ((*strind == '+') || (*strind == '-')) {
\r
295 if (*strind == '-') {
\r
297 tempY = -ReadInteger(strind, &nextCharacter);
\r
298 if (strind == nextCharacter)
\r
300 strind = nextCharacter;
\r
306 tempY = ReadInteger(strind, &nextCharacter);
\r
307 if (strind == nextCharacter)
\r
309 strind = nextCharacter;
\r
315 /* If strind isn't at the end of the string the it's an invalid
\r
316 geometry specification. */
\r
318 if (*strind != '\0') return 0;
\r
324 if (mask & WidthValue)
\r
325 *width = tempWidth;
\r
326 if (mask & HeightValue)
\r
327 *height = tempHeight;
\r
333 /* -- PLATFORM-SPECIFIC INTERFACE FUNCTION -------------------------------------------------- */
\r
335 void (__cdecl *__glutExitFunc)( int return_value ) = NULL;
\r
337 void FGAPIENTRY __glutInitWithExit( int *pargc, char **argv, void (__cdecl *exit_function)(int) )
\r
339 __glutExitFunc = exit_function;
\r
340 glutInit(pargc, argv);
\r