Fixing the warning message when "glutGetModifiers" is called from within the special...
[freeglut] / src / mswin / fg_main_mswin.c
1 /*
2  * freeglut_main_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: Sat Jan 21, 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 #include <GL/freeglut.h>
29 #include "../fg_internal.h"
30
31
32 extern void fghRedrawWindow ( SFG_Window *window );
33
34 extern void fgNewWGLCreateContext( SFG_Window* window );
35 extern GLboolean fgSetupPixelFormat( SFG_Window* window, GLboolean checkOnly,
36                                      unsigned char layer_type );
37
38 #ifdef WM_TOUCH
39 typedef BOOL (WINAPI *pGetTouchInputInfo)(HTOUCHINPUT,UINT,PTOUCHINPUT,int);
40 typedef BOOL (WINAPI *pCloseTouchInputHandle)(HTOUCHINPUT);
41 static pGetTouchInputInfo fghGetTouchInputInfo = (pGetTouchInputInfo)0xDEADBEEF;
42 static pCloseTouchInputHandle fghCloseTouchInputHandle = (pCloseTouchInputHandle)0xDEADBEEF;
43 #endif
44
45 #ifdef _WIN32_WCE
46 typedef struct GXDisplayProperties GXDisplayProperties;
47 typedef struct GXKeyList GXKeyList;
48 #include <gx.h>
49
50 typedef struct GXKeyList (*GXGETDEFAULTKEYS)(int);
51 typedef int (*GXOPENINPUT)();
52
53 GXGETDEFAULTKEYS GXGetDefaultKeys_ = NULL;
54 GXOPENINPUT GXOpenInput_ = NULL;
55
56 struct GXKeyList gxKeyList;
57 #endif /* _WIN32_WCE */
58
59 /* 
60  * Helper functions for getting client area from the window rect
61  * and the window rect from the client area given the style of the window
62  * (or a valid window pointer from which the style can be queried).
63  */
64 extern void fghComputeWindowRectFromClientArea_QueryWindow( const SFG_Window *window, RECT *clientRect, BOOL posIsOutside );
65 extern RECT fghGetClientArea                              ( const SFG_Window *window,                   BOOL wantPosOutside );
66
67
68 void fgPlatformReshapeWindow ( SFG_Window *window, int width, int height )
69 {
70     RECT windowRect;
71
72     /*
73      * For windowed mode, get the current position of the
74      * window and resize taking the size of the frame
75      * decorations into account.
76      */
77
78     /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
79     GetWindowRect( window->Window.Handle, &windowRect );
80
81     /* Create rect in FreeGLUT format, (X,Y) topleft outside window, WxH of client area */
82     windowRect.right    = windowRect.left+width;
83     windowRect.bottom   = windowRect.top+height;
84
85     if (window->Parent == NULL)
86         /* get the window rect from this to feed to SetWindowPos, correct for window decorations */
87         fghComputeWindowRectFromClientArea_QueryWindow(window,&windowRect,TRUE);
88     else
89     {
90         /* correct rect for position client area of parent window
91          * (SetWindowPos input for child windows is in coordinates
92          * relative to the parent's client area).
93          * Child windows don't have decoration, so no need to correct
94          * for them.
95          */
96         RECT parentRect;
97         parentRect = fghGetClientArea( window->Parent, FALSE );
98         windowRect.left   -= parentRect.left;
99         windowRect.right  -= parentRect.left;
100         windowRect.top    -= parentRect.top;
101         windowRect.bottom -= parentRect.top;
102     }
103     
104     /* Do the actual resizing */
105     SetWindowPos( window->Window.Handle,
106                   HWND_TOP,
107                   windowRect.left, windowRect.top,
108                   windowRect.right - windowRect.left,
109                   windowRect.bottom- windowRect.top,
110                   SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
111                   SWP_NOZORDER
112     );
113 }
114
115
116 void fgPlatformDisplayWindow ( SFG_Window *window )
117 {
118     RedrawWindow(
119         window->Window.Handle, NULL, NULL,
120         RDW_NOERASE | RDW_INTERNALPAINT | RDW_INVALIDATE | RDW_UPDATENOW
121     );
122 }
123
124
125 fg_time_t fgPlatformSystemTime ( void )
126 {
127 #if defined(_WIN32_WCE)
128     return GetTickCount();
129 #else
130     /* TODO: do this with QueryPerformanceCounter as timeGetTime has
131      * insufficient resolution (only about 5 ms on system under low load).
132      * See:
133      * http://msdn.microsoft.com/en-us/library/windows/desktop/dd757629(v=vs.85).aspx
134      * Or maybe QueryPerformanceCounter is not a good idea either, see
135      * http://old.nabble.com/Re%3A-glutTimerFunc-does-not-detect-if-system-time-moved-backward-p33479674.html
136      * for some other ideas (at bottom)...
137      */
138     return timeGetTime();
139 #endif
140 }
141
142
143 void fgPlatformSleepForEvents( fg_time_t msec )
144 {
145     MsgWaitForMultipleObjects( 0, NULL, FALSE, (DWORD) msec, QS_ALLINPUT );
146 }
147
148
149 void fgPlatformProcessSingleEvent ( void )
150 {
151     MSG stMsg;
152
153     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoopEvent" );
154
155     while( PeekMessage( &stMsg, NULL, 0, 0, PM_NOREMOVE ) )
156     {
157         if( GetMessage( &stMsg, NULL, 0, 0 ) == 0 )
158         {
159             if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
160             {
161                 fgDeinitialize( );
162                 exit( 0 );
163             }
164             else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS )
165                 fgState.ExecState = GLUT_EXEC_STATE_STOP;
166
167             return;
168         }
169
170         TranslateMessage( &stMsg );
171         DispatchMessage( &stMsg );
172     }
173 }
174
175
176
177 void fgPlatformMainLoopPreliminaryWork ( void )
178 {
179     SFG_Window *window = (SFG_Window *)fgStructure.Windows.First ;
180
181     /*
182      * Processing before the main loop:  If there is a window which is open and
183      * which has a visibility callback, call it.  I know this is an ugly hack,
184      * but I'm not sure what else to do about it.  Ideally we should leave
185      * something uninitialized in the create window code and initialize it in
186      * the main loop, and have that initialization create a "WM_ACTIVATE"
187      * message.  Then we would put the visibility callback code in the
188      * "case WM_ACTIVATE" block below.         - John Fay -- 10/24/02
189      */
190     while( window )
191     {
192         if ( FETCH_WCB( *window, Visibility ) )
193         {
194             SFG_Window *current_window = fgStructure.CurrentWindow ;
195
196             INVOKE_WCB( *window, Visibility, ( window->State.Visible ) );
197             fgSetWindow( current_window );
198         }
199
200         window = (SFG_Window *)window->Node.Next ;
201     }
202 }
203
204
205 /*
206  * Determine a GLUT modifer mask based on MS-WINDOWS system info.
207  */
208 static int fgPlatformGetModifiers (void)
209 {
210     return
211         ( ( ( GetKeyState( VK_LSHIFT   ) < 0 ) ||
212             ( GetKeyState( VK_RSHIFT   ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
213         ( ( ( GetKeyState( VK_LCONTROL ) < 0 ) ||
214             ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL  : 0 ) |
215         ( ( ( GetKeyState( VK_LMENU    ) < 0 ) ||
216             ( GetKeyState( VK_RMENU    ) < 0 )) ? GLUT_ACTIVE_ALT   : 0 );
217 }
218
219 /*
220  * The window procedure for handling Win32 events
221  */
222 LRESULT CALLBACK fgPlatformWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,
223                                        LPARAM lParam )
224 {
225     static unsigned char lControl = 0, rControl = 0, lShift = 0,
226                          rShift = 0, lAlt = 0, rAlt = 0;
227
228     SFG_Window* window;
229     PAINTSTRUCT ps;
230     LRESULT lRet = 1;
231
232     FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Event Handler" ) ;
233
234     window = fgWindowByHandle( hWnd );
235
236     if ( ( window == NULL ) && ( uMsg != WM_CREATE ) )
237       return DefWindowProc( hWnd, uMsg, wParam, lParam );
238
239     /* printf ( "Window %3d message <%04x> %12d %12d\n", window?window->ID:0,
240              uMsg, wParam, lParam ); */
241
242     if ( window )
243     {
244       fgState.Modifiers = fgPlatformGetModifiers( );
245
246       /* Checking for CTRL, ALT, and SHIFT key positions:  Key Down! */
247       if ( !lControl && GetAsyncKeyState ( VK_LCONTROL ) )
248       {
249           INVOKE_WCB    ( *window, Special,
250                         ( GLUT_KEY_CTRL_L, window->State.MouseX, window->State.MouseY )
251                       );
252
253           lControl = 1;
254       }
255
256       if ( !rControl && GetAsyncKeyState ( VK_RCONTROL ) )
257       {
258           INVOKE_WCB ( *window, Special,
259                        ( GLUT_KEY_CTRL_R, window->State.MouseX, window->State.MouseY )
260                      );
261
262           rControl = 1;
263       }
264
265       if ( !lShift && GetAsyncKeyState ( VK_LSHIFT ) )
266       {
267           INVOKE_WCB ( *window, Special,
268                        ( GLUT_KEY_SHIFT_L, window->State.MouseX, window->State.MouseY )
269                      );
270
271           lShift = 1;
272       }
273
274       if ( !rShift && GetAsyncKeyState ( VK_RSHIFT ) )
275       {
276           INVOKE_WCB ( *window, Special,
277                        ( GLUT_KEY_SHIFT_R, window->State.MouseX, window->State.MouseY )
278                      );
279
280           rShift = 1;
281       }
282
283       if ( !lAlt && GetAsyncKeyState ( VK_LMENU ) )
284       {
285           INVOKE_WCB ( *window, Special,
286                        ( GLUT_KEY_ALT_L, window->State.MouseX, window->State.MouseY )
287                      );
288
289           lAlt = 1;
290       }
291
292       if ( !rAlt && GetAsyncKeyState ( VK_RMENU ) )
293       {
294           INVOKE_WCB ( *window, Special,
295                        ( GLUT_KEY_ALT_R, window->State.MouseX, window->State.MouseY )
296                      );
297
298           rAlt = 1;
299       }
300
301       /* Checking for CTRL, ALT, and SHIFT key positions:  Key Up! */
302       if ( lControl && !GetAsyncKeyState ( VK_LCONTROL ) )
303       {
304           INVOKE_WCB ( *window, SpecialUp,
305                        ( GLUT_KEY_CTRL_L, window->State.MouseX, window->State.MouseY )
306                      );
307
308           lControl = 0;
309       }
310
311       if ( rControl && !GetAsyncKeyState ( VK_RCONTROL ) )
312       {
313           INVOKE_WCB ( *window, SpecialUp,
314                        ( GLUT_KEY_CTRL_R, window->State.MouseX, window->State.MouseY )
315                      );
316
317           rControl = 0;
318       }
319
320       if ( lShift && !GetAsyncKeyState ( VK_LSHIFT ) )
321       {
322           INVOKE_WCB ( *window, SpecialUp,
323                        ( GLUT_KEY_SHIFT_L, window->State.MouseX, window->State.MouseY )
324                      );
325
326           lShift = 0;
327       }
328
329       if ( rShift && !GetAsyncKeyState ( VK_RSHIFT ) )
330       {
331           INVOKE_WCB ( *window, SpecialUp,
332                        ( GLUT_KEY_SHIFT_R, window->State.MouseX, window->State.MouseY )
333                      );
334
335           rShift = 0;
336       }
337
338       if ( lAlt && !GetAsyncKeyState ( VK_LMENU ) )
339       {
340           INVOKE_WCB ( *window, SpecialUp,
341                        ( GLUT_KEY_ALT_L, window->State.MouseX, window->State.MouseY )
342                      );
343
344           lAlt = 0;
345       }
346
347       if ( rAlt && !GetAsyncKeyState ( VK_RMENU ) )
348       {
349           INVOKE_WCB ( *window, SpecialUp,
350                        ( GLUT_KEY_ALT_R, window->State.MouseX, window->State.MouseY )
351                      );
352
353           rAlt = 0;
354       }
355
356       fgState.Modifiers = INVALID_MODIFIERS;
357     }
358
359     switch( uMsg )
360     {
361     case WM_CREATE:
362         /* The window structure is passed as the creation structure parameter... */
363         window = (SFG_Window *) (((LPCREATESTRUCT) lParam)->lpCreateParams);
364         FREEGLUT_INTERNAL_ERROR_EXIT ( ( window != NULL ), "Cannot create window",
365                                        "fgPlatformWindowProc" );
366
367         window->Window.Handle = hWnd;
368         window->Window.pContext.Device = GetDC( hWnd );
369         if( window->IsMenu )
370         {
371             unsigned int current_DisplayMode = fgState.DisplayMode;
372             fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;
373 #if !defined(_WIN32_WCE)
374             fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
375 #endif
376             fgState.DisplayMode = current_DisplayMode;
377
378             if( fgStructure.MenuContext )
379                 wglMakeCurrent( window->Window.pContext.Device,
380                                 fgStructure.MenuContext->MContext
381                 );
382             else
383             {
384                 fgStructure.MenuContext =
385                     (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
386                 fgStructure.MenuContext->MContext =
387                     wglCreateContext( window->Window.pContext.Device );
388             }
389
390             /* window->Window.Context = wglGetCurrentContext ();   */
391             window->Window.Context = wglCreateContext( window->Window.pContext.Device );
392         }
393         else
394         {
395 #if !defined(_WIN32_WCE)
396             fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
397 #endif
398
399             if( ! fgState.UseCurrentContext )
400                 window->Window.Context =
401                     wglCreateContext( window->Window.pContext.Device );
402             else
403             {
404                 window->Window.Context = wglGetCurrentContext( );
405                 if( ! window->Window.Context )
406                     window->Window.Context =
407                         wglCreateContext( window->Window.pContext.Device );
408             }
409
410 #if !defined(_WIN32_WCE)
411             fgNewWGLCreateContext( window );
412 #endif
413         }
414
415         window->State.NeedToResize = GL_TRUE;
416         /* if we used CW_USEDEFAULT (thats a negative value) for the size
417          * of the window, query the window now for the size at which it
418          * was created.
419          */
420         if( ( window->State.Width < 0 ) || ( window->State.Height < 0 ) )
421         {
422             SFG_Window *current_window = fgStructure.CurrentWindow;
423
424             fgSetWindow( window );
425             window->State.Width = glutGet( GLUT_WINDOW_WIDTH );
426             window->State.Height = glutGet( GLUT_WINDOW_HEIGHT );
427             fgSetWindow( current_window );
428         }
429
430         ReleaseDC( window->Window.Handle, window->Window.pContext.Device );
431
432 #if defined(_WIN32_WCE)
433         /* Take over button handling */
434         {
435             HINSTANCE dxDllLib=LoadLibrary(_T("gx.dll"));
436             if (dxDllLib)
437             {
438                 GXGetDefaultKeys_=(GXGETDEFAULTKEYS)GetProcAddress(dxDllLib, _T("?GXGetDefaultKeys@@YA?AUGXKeyList@@H@Z"));
439                 GXOpenInput_=(GXOPENINPUT)GetProcAddress(dxDllLib, _T("?GXOpenInput@@YAHXZ"));
440             }
441
442             if(GXOpenInput_)
443                 (*GXOpenInput_)();
444             if(GXGetDefaultKeys_)
445                 gxKeyList = (*GXGetDefaultKeys_)(GX_LANDSCAPEKEYS);
446         }
447
448 #endif /* defined(_WIN32_WCE) */
449         break;
450
451     case WM_SIZE:
452         /*
453          * If the window is visible, then it is the user manually resizing it.
454          * If it is not, then it is the system sending us a dummy resize with
455          * zero dimensions on a "glutIconifyWindow" call.
456          */
457         if( window->State.Visible )
458         {
459             window->State.NeedToResize = GL_TRUE;
460 #if defined(_WIN32_WCE)
461             window->State.Width  = HIWORD(lParam);
462             window->State.Height = LOWORD(lParam);
463 #else
464             window->State.Width  = LOWORD(lParam);
465             window->State.Height = HIWORD(lParam);
466 #endif /* defined(_WIN32_WCE) */
467         }
468
469         break;
470
471     case WM_SETFOCUS:
472 /*        printf("WM_SETFOCUS: %p\n", window ); */
473         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
474         INVOKE_WCB( *window, Entry, ( GLUT_ENTERED ) );
475
476                 UpdateWindow ( hWnd );
477         break;
478
479     case WM_KILLFOCUS:
480 /*        printf("WM_KILLFOCUS: %p\n", window ); */
481         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
482         INVOKE_WCB( *window, Entry, ( GLUT_LEFT ) );
483
484         if( window->IsMenu &&
485             window->ActiveMenu && window->ActiveMenu->IsActive )
486             fgUpdateMenuHighlight( window->ActiveMenu );
487
488         break;
489
490 #if 0
491     case WM_ACTIVATE:
492         if (LOWORD(wParam) != WA_INACTIVE)
493         {
494 /*            printf("WM_ACTIVATE: fgSetCursor( %p, %d)\n", window,
495                    window->State.Cursor ); */
496             fgSetCursor( window, window->State.Cursor );
497         }
498
499         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
500         break;
501 #endif
502
503     case WM_SETCURSOR:
504 /*      printf ( "Cursor event %x %x %x %x\n", window, window->State.Cursor, lParam, wParam ) ; */
505         if( LOWORD( lParam ) == HTCLIENT )
506             fgSetCursor ( window, window->State.Cursor ) ;
507         else
508             lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
509         break;
510
511     case WM_SHOWWINDOW:
512         window->State.Visible = GL_TRUE;
513         window->State.Redisplay = GL_TRUE;
514         break;
515
516     case WM_PAINT:
517         /* Turn on the visibility in case it was turned off somehow */
518         window->State.Visible = GL_TRUE;
519         BeginPaint( hWnd, &ps );
520         fghRedrawWindow( window );
521         EndPaint( hWnd, &ps );
522         break;
523
524     case WM_CLOSE:
525         fgDestroyWindow ( window );
526         if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
527             PostQuitMessage(0);
528         break;
529
530     case WM_DESTROY:
531         /*
532          * The window already got destroyed, so don't bother with it.
533          */
534         return 0;
535
536     case WM_MOUSEMOVE:
537     {
538 #if defined(_WIN32_WCE)
539         window->State.MouseX = 320-HIWORD( lParam );
540         window->State.MouseY = LOWORD( lParam );
541 #else
542         window->State.MouseX = LOWORD( lParam );
543         window->State.MouseY = HIWORD( lParam );
544 #endif /* defined(_WIN32_WCE) */
545         /* Restrict to [-32768, 32767] to match X11 behaviour       */
546         /* See comment in "freeglut_developer" mailing list 10/4/04 */
547         if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;
548         if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;
549
550         if ( window->ActiveMenu )
551         {
552             fgUpdateMenuHighlight( window->ActiveMenu );
553             break;
554         }
555         SetFocus(window->Window.Handle);
556
557         fgState.Modifiers = fgPlatformGetModifiers( );
558
559         if( ( wParam & MK_LBUTTON ) ||
560             ( wParam & MK_MBUTTON ) ||
561             ( wParam & MK_RBUTTON ) )
562             INVOKE_WCB( *window, Motion, ( window->State.MouseX,
563                                            window->State.MouseY ) );
564         else
565             INVOKE_WCB( *window, Passive, ( window->State.MouseX,
566                                             window->State.MouseY ) );
567
568         fgState.Modifiers = INVALID_MODIFIERS;
569     }
570     break;
571
572     case WM_LBUTTONDOWN:
573     case WM_MBUTTONDOWN:
574     case WM_RBUTTONDOWN:
575     case WM_LBUTTONUP:
576     case WM_MBUTTONUP:
577     case WM_RBUTTONUP:
578     {
579         GLboolean pressed = GL_TRUE;
580         int button;
581
582 #if defined(_WIN32_WCE)
583         window->State.MouseX = 320-HIWORD( lParam );
584         window->State.MouseY = LOWORD( lParam );
585 #else
586         window->State.MouseX = LOWORD( lParam );
587         window->State.MouseY = HIWORD( lParam );
588 #endif /* defined(_WIN32_WCE) */
589
590         /* Restrict to [-32768, 32767] to match X11 behaviour       */
591         /* See comment in "freeglut_developer" mailing list 10/4/04 */
592         if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;
593         if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;
594
595         switch( uMsg )
596         {
597         case WM_LBUTTONDOWN:
598             pressed = GL_TRUE;
599             button = GLUT_LEFT_BUTTON;
600             break;
601         case WM_MBUTTONDOWN:
602             pressed = GL_TRUE;
603             button = GLUT_MIDDLE_BUTTON;
604             break;
605         case WM_RBUTTONDOWN:
606             pressed = GL_TRUE;
607             button = GLUT_RIGHT_BUTTON;
608             break;
609         case WM_LBUTTONUP:
610             pressed = GL_FALSE;
611             button = GLUT_LEFT_BUTTON;
612             break;
613         case WM_MBUTTONUP:
614             pressed = GL_FALSE;
615             button = GLUT_MIDDLE_BUTTON;
616             break;
617         case WM_RBUTTONUP:
618             pressed = GL_FALSE;
619             button = GLUT_RIGHT_BUTTON;
620             break;
621         default:
622             pressed = GL_FALSE;
623             button = -1;
624             break;
625         }
626
627 #if !defined(_WIN32_WCE)
628         if( GetSystemMetrics( SM_SWAPBUTTON ) )
629         {
630             if( button == GLUT_LEFT_BUTTON )
631                 button = GLUT_RIGHT_BUTTON;
632             else
633                 if( button == GLUT_RIGHT_BUTTON )
634                     button = GLUT_LEFT_BUTTON;
635         }
636 #endif /* !defined(_WIN32_WCE) */
637
638         if( button == -1 )
639             return DefWindowProc( hWnd, uMsg, lParam, wParam );
640
641         /*
642          * Do not execute the application's mouse callback if a menu
643          * is hooked to this button.  In that case an appropriate
644          * private call should be generated.
645          */
646         if( fgCheckActiveMenu( window, button, pressed,
647                                window->State.MouseX, window->State.MouseY ) )
648             break;
649
650         /* Set capture so that the window captures all the mouse messages */
651         /*
652          * XXX - Multiple button support:  Under X11, the mouse is not released
653          * XXX - from the window until all buttons have been released, even if the
654          * XXX - user presses a button in another window.  This will take more
655          * XXX - code changes than I am up to at the moment (10/5/04).  The present
656          * XXX - is a 90 percent solution.
657          */
658         if ( pressed == GL_TRUE )
659           SetCapture ( window->Window.Handle ) ;
660         else
661           ReleaseCapture () ;
662
663         if( ! FETCH_WCB( *window, Mouse ) )
664             break;
665
666         fgSetWindow( window );
667         fgState.Modifiers = fgPlatformGetModifiers( );
668
669         INVOKE_WCB(
670             *window, Mouse,
671             ( button,
672               pressed ? GLUT_DOWN : GLUT_UP,
673               window->State.MouseX,
674               window->State.MouseY
675             )
676         );
677
678         fgState.Modifiers = INVALID_MODIFIERS;
679     }
680     break;
681
682     case 0x020a:
683         /* Should be WM_MOUSEWHEEL but my compiler doesn't recognize it */
684     {
685         int wheel_number = LOWORD( wParam );
686         short ticks = ( short )HIWORD( wParam );
687                 fgState.MouseWheelTicks += ticks;
688
689         /*
690          * XXX Should use WHEEL_DELTA instead of 120
691          */
692                 if ( abs ( fgState.MouseWheelTicks ) > 120 )
693                 {
694                         int direction = ( fgState.MouseWheelTicks > 0 ) ? 1 : -1;
695
696             if( ! FETCH_WCB( *window, MouseWheel ) &&
697                 ! FETCH_WCB( *window, Mouse ) )
698                 break;
699
700             fgSetWindow( window );
701             fgState.Modifiers = fgPlatformGetModifiers( );
702
703             /*
704              * XXX Should use WHEEL_DELTA instead of 120
705              */
706             while( abs ( fgState.MouseWheelTicks ) > 120 )
707                         {
708                 if( FETCH_WCB( *window, MouseWheel ) )
709                     INVOKE_WCB( *window, MouseWheel,
710                                 ( wheel_number,
711                                   direction,
712                                   window->State.MouseX,
713                                   window->State.MouseY
714                                 )
715                     );
716                 else  /* No mouse wheel, call the mouse button callback twice */
717                                 {
718                     /*
719                      * Map wheel zero to button 3 and 4; +1 to 3, -1 to 4
720                      *  "    "   one                     +1 to 5, -1 to 6, ...
721                      *
722                      * XXX The below assumes that you have no more than 3 mouse
723                      * XXX buttons.  Sorry.
724                      */
725                     int button = wheel_number * 2 + 3;
726                     if( direction < 0 )
727                         ++button;
728                     INVOKE_WCB( *window, Mouse,
729                                 ( button, GLUT_DOWN,
730                                   window->State.MouseX, window->State.MouseY )
731                     );
732                     INVOKE_WCB( *window, Mouse,
733                                 ( button, GLUT_UP,
734                                   window->State.MouseX, window->State.MouseY )
735                     );
736                                 }
737
738                 /*
739                  * XXX Should use WHEEL_DELTA instead of 120
740                  */
741                                 fgState.MouseWheelTicks -= 120 * direction;
742                         }
743
744             fgState.Modifiers = INVALID_MODIFIERS;
745                 }
746     }
747     break ;
748
749     case WM_SYSKEYDOWN:
750     case WM_KEYDOWN:
751     {
752         int keypress = -1;
753         POINT mouse_pos ;
754
755         if( ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE ) && (HIWORD(lParam) & KF_REPEAT) )
756             break;
757
758         /*
759          * Remember the current modifiers state. This is done here in order
760          * to make sure the VK_DELETE keyboard callback is executed properly.
761          */
762         fgState.Modifiers = fgPlatformGetModifiers( );
763
764         GetCursorPos( &mouse_pos );
765         ScreenToClient( window->Window.Handle, &mouse_pos );
766
767         window->State.MouseX = mouse_pos.x;
768         window->State.MouseY = mouse_pos.y;
769
770         /* Convert the Win32 keystroke codes to GLUTtish way */
771 #       define KEY(a,b) case a: keypress = b; break;
772
773         switch( wParam )
774         {
775             KEY( VK_F1,     GLUT_KEY_F1        );
776             KEY( VK_F2,     GLUT_KEY_F2        );
777             KEY( VK_F3,     GLUT_KEY_F3        );
778             KEY( VK_F4,     GLUT_KEY_F4        );
779             KEY( VK_F5,     GLUT_KEY_F5        );
780             KEY( VK_F6,     GLUT_KEY_F6        );
781             KEY( VK_F7,     GLUT_KEY_F7        );
782             KEY( VK_F8,     GLUT_KEY_F8        );
783             KEY( VK_F9,     GLUT_KEY_F9        );
784             KEY( VK_F10,    GLUT_KEY_F10       );
785             KEY( VK_F11,    GLUT_KEY_F11       );
786             KEY( VK_F12,    GLUT_KEY_F12       );
787             KEY( VK_PRIOR,  GLUT_KEY_PAGE_UP   );
788             KEY( VK_NEXT,   GLUT_KEY_PAGE_DOWN );
789             KEY( VK_HOME,   GLUT_KEY_HOME      );
790             KEY( VK_END,    GLUT_KEY_END       );
791             KEY( VK_LEFT,   GLUT_KEY_LEFT      );
792             KEY( VK_UP,     GLUT_KEY_UP        );
793             KEY( VK_RIGHT,  GLUT_KEY_RIGHT     );
794             KEY( VK_DOWN,   GLUT_KEY_DOWN      );
795             KEY( VK_INSERT, GLUT_KEY_INSERT    );
796
797         case VK_DELETE:
798             /* The delete key should be treated as an ASCII keypress: */
799             INVOKE_WCB( *window, Keyboard,
800                         ( 127, window->State.MouseX, window->State.MouseY )
801             );
802         }
803
804 #if defined(_WIN32_WCE)
805         if(!(lParam & 0x40000000)) /* Prevent auto-repeat */
806         {
807             if(wParam==(unsigned)gxKeyList.vkRight)
808                 keypress = GLUT_KEY_RIGHT;
809             else if(wParam==(unsigned)gxKeyList.vkLeft)
810                 keypress = GLUT_KEY_LEFT;
811             else if(wParam==(unsigned)gxKeyList.vkUp)
812                 keypress = GLUT_KEY_UP;
813             else if(wParam==(unsigned)gxKeyList.vkDown)
814                 keypress = GLUT_KEY_DOWN;
815             else if(wParam==(unsigned)gxKeyList.vkA)
816                 keypress = GLUT_KEY_F1;
817             else if(wParam==(unsigned)gxKeyList.vkB)
818                 keypress = GLUT_KEY_F2;
819             else if(wParam==(unsigned)gxKeyList.vkC)
820                 keypress = GLUT_KEY_F3;
821             else if(wParam==(unsigned)gxKeyList.vkStart)
822                 keypress = GLUT_KEY_F4;
823         }
824 #endif
825
826         if( keypress != -1 )
827             INVOKE_WCB( *window, Special,
828                         ( keypress,
829                           window->State.MouseX, window->State.MouseY )
830             );
831
832         fgState.Modifiers = INVALID_MODIFIERS;
833     }
834     break;
835
836     case WM_SYSKEYUP:
837     case WM_KEYUP:
838     {
839         int keypress = -1;
840         POINT mouse_pos;
841
842         /*
843          * Remember the current modifiers state. This is done here in order
844          * to make sure the VK_DELETE keyboard callback is executed properly.
845          */
846         fgState.Modifiers = fgPlatformGetModifiers( );
847
848         GetCursorPos( &mouse_pos );
849         ScreenToClient( window->Window.Handle, &mouse_pos );
850
851         window->State.MouseX = mouse_pos.x;
852         window->State.MouseY = mouse_pos.y;
853
854         /*
855          * Convert the Win32 keystroke codes to GLUTtish way.
856          * "KEY(a,b)" was defined under "WM_KEYDOWN"
857          */
858
859         switch( wParam )
860         {
861             KEY( VK_F1,     GLUT_KEY_F1        );
862             KEY( VK_F2,     GLUT_KEY_F2        );
863             KEY( VK_F3,     GLUT_KEY_F3        );
864             KEY( VK_F4,     GLUT_KEY_F4        );
865             KEY( VK_F5,     GLUT_KEY_F5        );
866             KEY( VK_F6,     GLUT_KEY_F6        );
867             KEY( VK_F7,     GLUT_KEY_F7        );
868             KEY( VK_F8,     GLUT_KEY_F8        );
869             KEY( VK_F9,     GLUT_KEY_F9        );
870             KEY( VK_F10,    GLUT_KEY_F10       );
871             KEY( VK_F11,    GLUT_KEY_F11       );
872             KEY( VK_F12,    GLUT_KEY_F12       );
873             KEY( VK_PRIOR,  GLUT_KEY_PAGE_UP   );
874             KEY( VK_NEXT,   GLUT_KEY_PAGE_DOWN );
875             KEY( VK_HOME,   GLUT_KEY_HOME      );
876             KEY( VK_END,    GLUT_KEY_END       );
877             KEY( VK_LEFT,   GLUT_KEY_LEFT      );
878             KEY( VK_UP,     GLUT_KEY_UP        );
879             KEY( VK_RIGHT,  GLUT_KEY_RIGHT     );
880             KEY( VK_DOWN,   GLUT_KEY_DOWN      );
881             KEY( VK_INSERT, GLUT_KEY_INSERT    );
882
883           case VK_DELETE:
884               /* The delete key should be treated as an ASCII keypress: */
885               INVOKE_WCB( *window, KeyboardUp,
886                           ( 127, window->State.MouseX, window->State.MouseY )
887               );
888               break;
889
890         default:
891         {
892 #if !defined(_WIN32_WCE)
893             BYTE state[ 256 ];
894             WORD code[ 2 ];
895
896             GetKeyboardState( state );
897
898             if( ToAscii( (UINT)wParam, 0, state, code, 0 ) == 1 )
899                 wParam=code[ 0 ];
900
901             INVOKE_WCB( *window, KeyboardUp,
902                         ( (char)wParam,
903                           window->State.MouseX, window->State.MouseY )
904             );
905 #endif /* !defined(_WIN32_WCE) */
906         }
907         }
908
909         if( keypress != -1 )
910             INVOKE_WCB( *window, SpecialUp,
911                         ( keypress,
912                           window->State.MouseX, window->State.MouseY )
913             );
914
915         fgState.Modifiers = INVALID_MODIFIERS;
916     }
917     break;
918
919     case WM_SYSCHAR:
920     case WM_CHAR:
921     {
922       if( (fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE) && (HIWORD(lParam) & KF_REPEAT) )
923             break;
924
925         fgState.Modifiers = fgPlatformGetModifiers( );
926         INVOKE_WCB( *window, Keyboard,
927                     ( (char)wParam,
928                       window->State.MouseX, window->State.MouseY )
929         );
930         fgState.Modifiers = INVALID_MODIFIERS;
931     }
932     break;
933
934     case WM_CAPTURECHANGED:
935         /* User has finished resizing the window, force a redraw */
936         INVOKE_WCB( *window, Display, ( ) );
937
938         /*lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ); */
939         break;
940
941         /* Other messages that I have seen and which are not handled already */
942     case WM_SETTEXT:  /* 0x000c */
943         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
944         /* Pass it on to "DefWindowProc" to set the window text */
945         break;
946
947     case WM_GETTEXT:  /* 0x000d */
948         /* Ideally we would copy the title of the window into "lParam" */
949         /* strncpy ( (char *)lParam, "Window Title", wParam );
950            lRet = ( wParam > 12 ) ? 12 : wParam;  */
951         /* the number of characters copied */
952         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
953         break;
954
955     case WM_GETTEXTLENGTH:  /* 0x000e */
956         /* Ideally we would get the length of the title of the window */
957         lRet = 12;
958         /* the number of characters in "Window Title\0" (see above) */
959         break;
960
961     case WM_ERASEBKGND:  /* 0x0014 */
962         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
963         break;
964
965 #if !defined(_WIN32_WCE)
966     case WM_SYNCPAINT:  /* 0x0088 */
967         /* Another window has moved, need to update this one */
968         window->State.Redisplay = GL_TRUE;
969         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
970         /* Help screen says this message must be passed to "DefWindowProc" */
971         break;
972
973     case WM_NCPAINT:  /* 0x0085 */
974       /* Need to update the border of this window */
975         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
976         /* Pass it on to "DefWindowProc" to repaint a standard border */
977         break;
978
979     case WM_SYSCOMMAND :  /* 0x0112 */
980         {
981           /*
982            * We have received a system command message.  Try to act on it.
983            * The commands are passed in through the "wParam" parameter:
984            * The least significant digit seems to be which edge of the window
985            * is being used for a resize event:
986            *     4  3  5
987            *     1     2
988            *     7  6  8
989            * Congratulations and thanks to Richard Rauch for figuring this out..
990            */
991             switch ( wParam & 0xfff0 )
992             {
993             case SC_SIZE       :
994                 break ;
995
996             case SC_MOVE       :
997                 break ;
998
999             case SC_MINIMIZE   :
1000                 /* User has clicked on the "-" to minimize the window */
1001                 /* Turn off the visibility */
1002                 window->State.Visible = GL_FALSE ;
1003
1004                 break ;
1005
1006             case SC_MAXIMIZE   :
1007                 break ;
1008
1009             case SC_NEXTWINDOW :
1010                 break ;
1011
1012             case SC_PREVWINDOW :
1013                 break ;
1014
1015             case SC_CLOSE      :
1016                 /* Followed very closely by a WM_CLOSE message */
1017                 break ;
1018
1019             case SC_VSCROLL    :
1020                 break ;
1021
1022             case SC_HSCROLL    :
1023                 break ;
1024
1025             case SC_MOUSEMENU  :
1026                 break ;
1027
1028             case SC_KEYMENU    :
1029                 break ;
1030
1031             case SC_ARRANGE    :
1032                 break ;
1033
1034             case SC_RESTORE    :
1035                 break ;
1036
1037             case SC_TASKLIST   :
1038                 break ;
1039
1040             case SC_SCREENSAVE :
1041                 break ;
1042
1043             case SC_HOTKEY     :
1044                 break ;
1045
1046 #if(WINVER >= 0x0400)
1047             case SC_DEFAULT    :
1048                 break ;
1049
1050             case SC_MONITORPOWER    :
1051                 break ;
1052
1053             case SC_CONTEXTHELP    :
1054                 break ;
1055 #endif /* WINVER >= 0x0400 */
1056
1057             default:
1058 #if _DEBUG
1059                 fgWarning( "Unknown wParam type 0x%x", wParam );
1060 #endif
1061                 break;
1062             }
1063         }
1064 #endif /* !defined(_WIN32_WCE) */
1065
1066         /* We need to pass the message on to the operating system as well */
1067         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1068         break;
1069
1070 #ifdef WM_TOUCH
1071         /* handle multi-touch messages */
1072         case WM_TOUCH:
1073         {
1074                 unsigned int numInputs = (unsigned int)wParam;
1075                 unsigned int i = 0;
1076                 TOUCHINPUT* ti = (TOUCHINPUT*)malloc( sizeof(TOUCHINPUT)*numInputs);
1077
1078                 if (fghGetTouchInputInfo == (pGetTouchInputInfo)0xDEADBEEF) {
1079                     fghGetTouchInputInfo = (pGetTouchInputInfo)GetProcAddress(GetModuleHandle("user32"),"GetTouchInputInfo");
1080                     fghCloseTouchInputHandle = (pCloseTouchInputHandle)GetProcAddress(GetModuleHandle("user32"),"CloseTouchInputHandle");
1081                 }
1082
1083                 if (!fghGetTouchInputInfo) { 
1084                         free( (void*)ti );
1085                         break;
1086                 }
1087
1088                 if (fghGetTouchInputInfo( (HTOUCHINPUT)lParam, numInputs, ti, sizeof(TOUCHINPUT) )) {
1089                         /* Handle each contact point */
1090                         for (i = 0; i < numInputs; ++i ) {
1091
1092                                 POINT tp;
1093                                 tp.x = TOUCH_COORD_TO_PIXEL(ti[i].x);
1094                                 tp.y = TOUCH_COORD_TO_PIXEL(ti[i].y);
1095                                 ScreenToClient( hWnd, &tp );
1096
1097                                 ti[i].dwID = ti[i].dwID * 2;
1098
1099                                 if (ti[i].dwFlags & TOUCHEVENTF_DOWN) {
1100                                         INVOKE_WCB( *window, MultiEntry,  ( ti[i].dwID, GLUT_ENTERED ) );
1101                                         INVOKE_WCB( *window, MultiButton, ( ti[i].dwID, tp.x, tp.y, 0, GLUT_DOWN ) );
1102                                 } else if (ti[i].dwFlags & TOUCHEVENTF_MOVE) {
1103                                         INVOKE_WCB( *window, MultiMotion, ( ti[i].dwID, tp.x, tp.y ) );
1104                                 } else if (ti[i].dwFlags & TOUCHEVENTF_UP)   { 
1105                                         INVOKE_WCB( *window, MultiButton, ( ti[i].dwID, tp.x, tp.y, 0, GLUT_UP ) );
1106                                         INVOKE_WCB( *window, MultiEntry,  ( ti[i].dwID, GLUT_LEFT ) );
1107                                 }
1108                         }
1109                 }
1110                 fghCloseTouchInputHandle((HTOUCHINPUT)lParam);
1111                 free( (void*)ti );
1112                 lRet = 0; /*DefWindowProc( hWnd, uMsg, wParam, lParam );*/
1113                 break;
1114         }
1115 #endif
1116     default:
1117         /* Handle unhandled messages */
1118         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1119         break;
1120     }
1121
1122     return lRet;
1123 }