some cleanup, correctness in naming, correctness in type (those ATOM should be BOOL)
[freeglut] / src / mswin / fg_init_mswin.c
1 /*
2  * freeglut_init_mswin.c
3  *
4  * The Windows-specific mouse cursor related stuff.
5  *
6  * Copyright (c) 2012 Stephen J. Baker. All Rights Reserved.
7  * Written by John F. Fay, <fayjf@sourceforge.net>
8  * Creation date: Thu Jan 19, 2012
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27
28 #define FREEGLUT_BUILDING_LIB
29 #include <GL/freeglut.h>
30 #include "../fg_internal.h"
31
32
33
34 extern LRESULT CALLBACK fgPlatformWindowProc( HWND hWnd, UINT uMsg,
35                                WPARAM wParam, LPARAM lParam );
36 extern void fgPlatformInitSystemTime();
37
38
39 /*
40  * A call to this function should initialize all the display stuff...
41  */
42 void fgPlatformInitialize( const char* displayName )
43 {
44     WNDCLASS wc;
45     BOOL atom;
46
47     /* What we need to do is to initialize the fgDisplay global structure here. */
48     fgDisplay.pDisplay.Instance = GetModuleHandle( NULL );
49     fgDisplay.pDisplay.DisplayName= displayName ? strdup(displayName) : 0 ;
50     atom = GetClassInfo( fgDisplay.pDisplay.Instance, _T("FREEGLUT"), &wc );
51
52     if( atom == 0 )
53     {
54         ZeroMemory( &wc, sizeof(WNDCLASS) );
55
56         /*
57          * Each of the windows should have its own device context, and we
58          * want redraw events during Vertical and Horizontal Resizes by
59          * the user.
60          */
61         wc.lpfnWndProc    = fgPlatformWindowProc;
62         wc.cbClsExtra     = 0;
63         wc.cbWndExtra     = 0;
64         wc.hInstance      = fgDisplay.pDisplay.Instance;
65         wc.hIcon          = LoadIcon( fgDisplay.pDisplay.Instance, _T("GLUT_ICON") );
66
67 #if defined(_WIN32_WCE)
68         wc.style          = CS_HREDRAW | CS_VREDRAW;
69 #else
70         wc.style          = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
71         if (!wc.hIcon)
72           wc.hIcon        = LoadIcon( NULL, IDI_WINLOGO );
73 #endif
74
75         wc.hCursor        = LoadCursor( NULL, IDC_ARROW );
76         wc.hbrBackground  = NULL;
77         wc.lpszMenuName   = NULL;
78         wc.lpszClassName  = _T("FREEGLUT");
79
80         /* Register the window class */
81         atom = RegisterClass( &wc );
82         FREEGLUT_INTERNAL_ERROR_EXIT ( atom, "Window Class Not Registered", "fgPlatformInitialize" );
83     }
84
85     /* The screen dimensions can be obtained via GetSystemMetrics() calls */
86     fgDisplay.ScreenWidth  = GetSystemMetrics( SM_CXSCREEN );
87     fgDisplay.ScreenHeight = GetSystemMetrics( SM_CYSCREEN );
88
89     {
90         HWND desktop = GetDesktopWindow( );
91         HDC  context = GetDC( desktop );
92
93         fgDisplay.ScreenWidthMM  = GetDeviceCaps( context, HORZSIZE );
94         fgDisplay.ScreenHeightMM = GetDeviceCaps( context, VERTSIZE );
95
96         ReleaseDC( desktop, context );
97     }
98     /* If we have a DisplayName try to use it for metrics */
99     if( fgDisplay.pDisplay.DisplayName )
100     {
101         HDC context = CreateDC(fgDisplay.pDisplay.DisplayName,0,0,0);
102         if( context )
103         {
104             fgDisplay.ScreenWidth  = GetDeviceCaps( context, HORZRES );
105             fgDisplay.ScreenHeight = GetDeviceCaps( context, VERTRES );
106             fgDisplay.ScreenWidthMM  = GetDeviceCaps( context, HORZSIZE );
107             fgDisplay.ScreenHeightMM = GetDeviceCaps( context, VERTSIZE );
108             DeleteDC(context);
109         }
110         else
111             fgWarning("fgPlatformInitialize: "
112                       "CreateDC failed, Screen size info may be incorrect\n"
113           "This is quite likely caused by a bad '-display' parameter");
114       
115     }
116     /* Set the timer granularity to 1 ms */
117     timeBeginPeriod ( 1 );
118     /* Init setup to deal with timer wrap, can't query system time before this is done */
119     fgPlatformInitSystemTime();
120     /* Get start time */
121     fgState.Time = fgSystemTime();
122
123
124     fgState.Initialised = GL_TRUE;
125
126     /* Avoid registering atexit callback on Win32 as it can result in an
127      * access violation due to calling into a module which has been
128      * unloaded.
129      * Any cleanup isn't needed on Windows anyway, the OS takes care of it.
130      * see: http://blogs.msdn.com/b/oldnewthing/archive/2012/01/05/10253268.aspx
131      */
132 /*    atexit(fgDeinitialize); */
133
134     /* InputDevice uses GlutTimerFunc(), so fgState.Initialised must be TRUE */
135     fgInitialiseInputDevices();
136 }
137
138
139
140 /* Platform-Specific Deinitialization Functions: */
141 extern void fghCloseInputDevices ( void );
142
143 void fgPlatformDeinitialiseInputDevices ( void )
144 {
145 #if !defined(_WIN32_WCE)
146         fghCloseInputDevices ();
147 #endif /* !defined(_WIN32_WCE) */
148     fgState.JoysticksInitialised = GL_FALSE;
149     fgState.InputDevsInitialised = GL_FALSE;
150 }
151
152 void fgPlatformCloseDisplay ( void )
153 {
154     if( fgDisplay.pDisplay.DisplayName )
155     {
156         free( fgDisplay.pDisplay.DisplayName );
157         fgDisplay.pDisplay.DisplayName = NULL;
158     }
159
160     /* Reset the timer granularity */
161     timeEndPeriod ( 1 );
162 }
163
164 void fgPlatformDestroyContext ( SFG_PlatformDisplay pDisplay, SFG_WindowContextType MContext )
165 {
166         /* Do nothing -- this is required for X11 */
167 }
168
169 /* -- PLATFORM-SPECIFIC INTERFACE FUNCTION -------------------------------------------------- */
170
171 void (__cdecl *__glutExitFunc)( int return_value ) = NULL;
172
173 void FGAPIENTRY __glutInitWithExit( int *pargc, char **argv, void (__cdecl *exit_function)(int) )
174 {
175   __glutExitFunc = exit_function;
176   glutInit(pargc, argv);
177 }
178