3afc655d5a4c2f6e887a703d903376dcc45e498e
[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     GLboolean gotChild = GL_FALSE;
232
233     FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Event Handler" ) ;
234
235     window = fgWindowByHandle( hWnd );
236
237     if ( ( window == NULL ) && ( uMsg != WM_CREATE ) )
238       return DefWindowProc( hWnd, uMsg, wParam, lParam );
239
240     /* printf ( "Window %3d message <%04x> %12d %12d\n", window?window->ID:0,
241              uMsg, wParam, lParam ); */
242
243     /* events only sent to main window. Check if the current window that the mouse
244        is over is a child window and if so, make sure we call the callback on that
245        child instead.
246     */
247     if (window && window->Children.First)
248     {
249         POINT mouse_pos;
250         SFG_WindowHandleType hwnd;
251
252         GetCursorPos( &mouse_pos );
253         ScreenToClient( window->Window.Handle, &mouse_pos );
254         hwnd = ChildWindowFromPoint(window->Window.Handle, mouse_pos);
255         if (hwnd)   /* can be NULL if mouse outside parent by the time we get here */
256         {
257             window = fgWindowByHandle(hwnd);
258             if (window->Parent)
259                 gotChild = GL_TRUE;
260         }
261     }
262
263     if ( window )
264     {
265       fgState.Modifiers = fgPlatformGetModifiers( );
266
267       /* Checking for CTRL, ALT, and SHIFT key positions:  Key Down! */
268       if ( !lControl && GetAsyncKeyState ( VK_LCONTROL ) )
269       {
270           INVOKE_WCB    ( *window, Special,
271                         ( GLUT_KEY_CTRL_L, window->State.MouseX, window->State.MouseY )
272                       );
273
274           lControl = 1;
275       }
276
277       if ( !rControl && GetAsyncKeyState ( VK_RCONTROL ) )
278       {
279           INVOKE_WCB ( *window, Special,
280                        ( GLUT_KEY_CTRL_R, window->State.MouseX, window->State.MouseY )
281                      );
282
283           rControl = 1;
284       }
285
286       if ( !lShift && GetAsyncKeyState ( VK_LSHIFT ) )
287       {
288           INVOKE_WCB ( *window, Special,
289                        ( GLUT_KEY_SHIFT_L, window->State.MouseX, window->State.MouseY )
290                      );
291
292           lShift = 1;
293       }
294
295       if ( !rShift && GetAsyncKeyState ( VK_RSHIFT ) )
296       {
297           INVOKE_WCB ( *window, Special,
298                        ( GLUT_KEY_SHIFT_R, window->State.MouseX, window->State.MouseY )
299                      );
300
301           rShift = 1;
302       }
303
304       if ( !lAlt && GetAsyncKeyState ( VK_LMENU ) )
305       {
306           INVOKE_WCB ( *window, Special,
307                        ( GLUT_KEY_ALT_L, window->State.MouseX, window->State.MouseY )
308                      );
309
310           lAlt = 1;
311       }
312
313       if ( !rAlt && GetAsyncKeyState ( VK_RMENU ) )
314       {
315           INVOKE_WCB ( *window, Special,
316                        ( GLUT_KEY_ALT_R, window->State.MouseX, window->State.MouseY )
317                      );
318
319           rAlt = 1;
320       }
321
322       /* Checking for CTRL, ALT, and SHIFT key positions:  Key Up! */
323       if ( lControl && !GetAsyncKeyState ( VK_LCONTROL ) )
324       {
325           INVOKE_WCB ( *window, SpecialUp,
326                        ( GLUT_KEY_CTRL_L, window->State.MouseX, window->State.MouseY )
327                      );
328
329           lControl = 0;
330       }
331
332       if ( rControl && !GetAsyncKeyState ( VK_RCONTROL ) )
333       {
334           INVOKE_WCB ( *window, SpecialUp,
335                        ( GLUT_KEY_CTRL_R, window->State.MouseX, window->State.MouseY )
336                      );
337
338           rControl = 0;
339       }
340
341       if ( lShift && !GetAsyncKeyState ( VK_LSHIFT ) )
342       {
343           INVOKE_WCB ( *window, SpecialUp,
344                        ( GLUT_KEY_SHIFT_L, window->State.MouseX, window->State.MouseY )
345                      );
346
347           lShift = 0;
348       }
349
350       if ( rShift && !GetAsyncKeyState ( VK_RSHIFT ) )
351       {
352           INVOKE_WCB ( *window, SpecialUp,
353                        ( GLUT_KEY_SHIFT_R, window->State.MouseX, window->State.MouseY )
354                      );
355
356           rShift = 0;
357       }
358
359       if ( lAlt && !GetAsyncKeyState ( VK_LMENU ) )
360       {
361           INVOKE_WCB ( *window, SpecialUp,
362                        ( GLUT_KEY_ALT_L, window->State.MouseX, window->State.MouseY )
363                      );
364
365           lAlt = 0;
366       }
367
368       if ( rAlt && !GetAsyncKeyState ( VK_RMENU ) )
369       {
370           INVOKE_WCB ( *window, SpecialUp,
371                        ( GLUT_KEY_ALT_R, window->State.MouseX, window->State.MouseY )
372                      );
373
374           rAlt = 0;
375       }
376
377       fgState.Modifiers = INVALID_MODIFIERS;
378     }
379
380     switch( uMsg )
381     {
382     case WM_CREATE:
383         /* The window structure is passed as the creation structure parameter... */
384         window = (SFG_Window *) (((LPCREATESTRUCT) lParam)->lpCreateParams);
385         FREEGLUT_INTERNAL_ERROR_EXIT ( ( window != NULL ), "Cannot create window",
386                                        "fgPlatformWindowProc" );
387
388         window->Window.Handle = hWnd;
389         window->Window.pContext.Device = GetDC( hWnd );
390         if( window->IsMenu )
391         {
392             unsigned int current_DisplayMode = fgState.DisplayMode;
393             fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;
394 #if !defined(_WIN32_WCE)
395             fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
396 #endif
397             fgState.DisplayMode = current_DisplayMode;
398
399             if( fgStructure.MenuContext )
400                 wglMakeCurrent( window->Window.pContext.Device,
401                                 fgStructure.MenuContext->MContext
402                 );
403             else
404             {
405                 fgStructure.MenuContext =
406                     (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
407                 fgStructure.MenuContext->MContext =
408                     wglCreateContext( window->Window.pContext.Device );
409             }
410
411             /* window->Window.Context = wglGetCurrentContext ();   */
412             window->Window.Context = wglCreateContext( window->Window.pContext.Device );
413         }
414         else
415         {
416 #if !defined(_WIN32_WCE)
417             fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
418 #endif
419
420             if( ! fgState.UseCurrentContext )
421                 window->Window.Context =
422                     wglCreateContext( window->Window.pContext.Device );
423             else
424             {
425                 window->Window.Context = wglGetCurrentContext( );
426                 if( ! window->Window.Context )
427                     window->Window.Context =
428                         wglCreateContext( window->Window.pContext.Device );
429             }
430
431 #if !defined(_WIN32_WCE)
432             fgNewWGLCreateContext( window );
433 #endif
434         }
435
436         window->State.NeedToResize = GL_TRUE;
437         /* if we used CW_USEDEFAULT (thats a negative value) for the size
438          * of the window, query the window now for the size at which it
439          * was created.
440          */
441         if( ( window->State.Width < 0 ) || ( window->State.Height < 0 ) )
442         {
443             SFG_Window *current_window = fgStructure.CurrentWindow;
444
445             fgSetWindow( window );
446             window->State.Width = glutGet( GLUT_WINDOW_WIDTH );
447             window->State.Height = glutGet( GLUT_WINDOW_HEIGHT );
448             fgSetWindow( current_window );
449         }
450
451         ReleaseDC( window->Window.Handle, window->Window.pContext.Device );
452
453 #if defined(_WIN32_WCE)
454         /* Take over button handling */
455         {
456             HINSTANCE dxDllLib=LoadLibrary(_T("gx.dll"));
457             if (dxDllLib)
458             {
459                 GXGetDefaultKeys_=(GXGETDEFAULTKEYS)GetProcAddress(dxDllLib, _T("?GXGetDefaultKeys@@YA?AUGXKeyList@@H@Z"));
460                 GXOpenInput_=(GXOPENINPUT)GetProcAddress(dxDllLib, _T("?GXOpenInput@@YAHXZ"));
461             }
462
463             if(GXOpenInput_)
464                 (*GXOpenInput_)();
465             if(GXGetDefaultKeys_)
466                 gxKeyList = (*GXGetDefaultKeys_)(GX_LANDSCAPEKEYS);
467         }
468
469 #endif /* defined(_WIN32_WCE) */
470         break;
471
472     case WM_SIZE:
473         /*
474          * If the window is visible, then it is the user manually resizing it.
475          * If it is not, then it is the system sending us a dummy resize with
476          * zero dimensions on a "glutIconifyWindow" call.
477          */
478         if( window->State.Visible )
479         {
480             window->State.NeedToResize = GL_TRUE;
481 #if defined(_WIN32_WCE)
482             window->State.Width  = HIWORD(lParam);
483             window->State.Height = LOWORD(lParam);
484 #else
485             window->State.Width  = LOWORD(lParam);
486             window->State.Height = HIWORD(lParam);
487 #endif /* defined(_WIN32_WCE) */
488         }
489
490         break;
491
492     case WM_SETFOCUS:
493 /*        printf("WM_SETFOCUS: %p\n", window ); */
494         if (gotChild)
495             /* If child should have focus instead, set it here. */
496             SetFocus(window->Window.Handle);
497
498         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
499         INVOKE_WCB( *window, Entry, ( GLUT_ENTERED ) );
500
501         UpdateWindow ( hWnd );
502         if (gotChild)
503             UpdateWindow ( window->Window.Handle );
504         break;
505
506     case WM_KILLFOCUS:
507         {
508             SFG_Menu* menu = NULL;
509 /*            printf("WM_KILLFOCUS: %p\n", window ); */
510             lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
511             INVOKE_WCB( *window, Entry, ( GLUT_LEFT ) );
512
513             /* If we have an open menu, see if the open menu should be closed
514              * when focus was lost because user either switched
515              * application or FreeGLUT window (if one is running multiple
516              * windows). If so, close menu the active menu.
517              */
518             if ( fgStructure.Menus.First )
519                 menu = fgGetActiveMenu();
520
521             if ( menu )
522             {
523                 SFG_Window* wnd = NULL;
524                 HWND hwnd = GetFocus();  /* Get window with current focus - NULL for non freeglut windows */
525                 if (hwnd)
526                     /* See which of our windows it is */
527                     wnd = fgWindowByHandle(hwnd);
528
529                 if (!hwnd || !wnd)
530                     /* User switched to another application*/
531                     fgDeactivateMenu(menu->ParentWindow);
532                 else if (!wnd->IsMenu && wnd!=menu->ParentWindow)   /* Make sure we don't kill the menu when trying to enter a submenu */
533                     /* User switched to another FreeGLUT window */
534                     fgDeactivateMenu(menu->ParentWindow);
535                 else
536                 {
537                     /* Check if focus lost because non-client area of
538                      * window was pressed (pressing on client area is
539                      * handled in fgCheckActiveMenu)
540                      */
541                     POINT mouse_pos;
542                     RECT clientArea = fghGetClientArea(menu->ParentWindow, GL_FALSE);
543                     GetCursorPos(&mouse_pos);
544                     if ( !PtInRect( &clientArea, mouse_pos ) )
545                         fgDeactivateMenu(menu->ParentWindow);
546                 }
547             }
548         }
549         break;
550
551 #if 0
552     case WM_ACTIVATE:
553         if (LOWORD(wParam) != WA_INACTIVE)
554         {
555 /*            printf("WM_ACTIVATE: fgSetCursor( %p, %d)\n", window,
556                    window->State.Cursor ); */
557             fgSetCursor( window, window->State.Cursor );
558         }
559
560         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
561         break;
562 #endif
563
564     case WM_SETCURSOR:
565 /*      printf ( "Cursor event %x %x %x %x\n", window, window->State.Cursor, lParam, wParam ) ; */
566         if( LOWORD( lParam ) == HTCLIENT )
567             fgSetCursor ( window, window->State.Cursor ) ;
568         else
569             lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
570         break;
571
572     case WM_SHOWWINDOW:
573         window->State.Visible = GL_TRUE;
574         window->State.Redisplay = GL_TRUE;
575         break;
576
577     case WM_PAINT:
578         /* Turn on the visibility in case it was turned off somehow */
579         window->State.Visible = GL_TRUE;
580         BeginPaint( hWnd, &ps );
581         fghRedrawWindow( window );
582         EndPaint( hWnd, &ps );
583         break;
584
585     case WM_CLOSE:
586         fgDestroyWindow ( window );
587         if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
588             PostQuitMessage(0);
589         break;
590
591     case WM_DESTROY:
592         /*
593          * The window already got destroyed, so don't bother with it.
594          */
595         return 0;
596
597     case WM_MOUSEMOVE:
598     {
599 #if defined(_WIN32_WCE)
600         window->State.MouseX = 320-HIWORD( lParam );
601         window->State.MouseY = LOWORD( lParam );
602 #else
603         window->State.MouseX = LOWORD( lParam );
604         window->State.MouseY = HIWORD( lParam );
605 #endif /* defined(_WIN32_WCE) */
606         /* Restrict to [-32768, 32767] to match X11 behaviour       */
607         /* See comment in "freeglut_developer" mailing list 10/4/04 */
608         if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;
609         if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;
610
611         if ( window->ActiveMenu )
612         {
613             fgUpdateMenuHighlight( window->ActiveMenu );
614             break;
615         }
616
617         fgState.Modifiers = fgPlatformGetModifiers( );
618
619         if( ( wParam & MK_LBUTTON ) ||
620             ( wParam & MK_MBUTTON ) ||
621             ( wParam & MK_RBUTTON ) )
622             INVOKE_WCB( *window, Motion, ( window->State.MouseX,
623                                            window->State.MouseY ) );
624         else
625             INVOKE_WCB( *window, Passive, ( window->State.MouseX,
626                                             window->State.MouseY ) );
627
628         fgState.Modifiers = INVALID_MODIFIERS;
629     }
630     break;
631
632     case WM_LBUTTONDOWN:
633     case WM_MBUTTONDOWN:
634     case WM_RBUTTONDOWN:
635     case WM_LBUTTONUP:
636     case WM_MBUTTONUP:
637     case WM_RBUTTONUP:
638     {
639         GLboolean pressed = GL_TRUE;
640         int button;
641
642 #if defined(_WIN32_WCE)
643         window->State.MouseX = 320-HIWORD( lParam );
644         window->State.MouseY = LOWORD( lParam );
645 #else
646         window->State.MouseX = LOWORD( lParam );
647         window->State.MouseY = HIWORD( lParam );
648 #endif /* defined(_WIN32_WCE) */
649
650         /* Restrict to [-32768, 32767] to match X11 behaviour       */
651         /* See comment in "freeglut_developer" mailing list 10/4/04 */
652         if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;
653         if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;
654
655         switch( uMsg )
656         {
657         case WM_LBUTTONDOWN:
658             pressed = GL_TRUE;
659             button = GLUT_LEFT_BUTTON;
660             break;
661         case WM_MBUTTONDOWN:
662             pressed = GL_TRUE;
663             button = GLUT_MIDDLE_BUTTON;
664             break;
665         case WM_RBUTTONDOWN:
666             pressed = GL_TRUE;
667             button = GLUT_RIGHT_BUTTON;
668             break;
669         case WM_LBUTTONUP:
670             pressed = GL_FALSE;
671             button = GLUT_LEFT_BUTTON;
672             break;
673         case WM_MBUTTONUP:
674             pressed = GL_FALSE;
675             button = GLUT_MIDDLE_BUTTON;
676             break;
677         case WM_RBUTTONUP:
678             pressed = GL_FALSE;
679             button = GLUT_RIGHT_BUTTON;
680             break;
681         default:
682             pressed = GL_FALSE;
683             button = -1;
684             break;
685         }
686
687 #if !defined(_WIN32_WCE)
688         if( GetSystemMetrics( SM_SWAPBUTTON ) )
689         {
690             if( button == GLUT_LEFT_BUTTON )
691                 button = GLUT_RIGHT_BUTTON;
692             else
693                 if( button == GLUT_RIGHT_BUTTON )
694                     button = GLUT_LEFT_BUTTON;
695         }
696 #endif /* !defined(_WIN32_WCE) */
697
698         if( button == -1 )
699             return DefWindowProc( hWnd, uMsg, lParam, wParam );
700
701         /*
702          * Do not execute the application's mouse callback if a menu
703          * is hooked to this button.  In that case an appropriate
704          * private call should be generated.
705          */
706         if( fgCheckActiveMenu( window, button, pressed,
707                                window->State.MouseX, window->State.MouseY ) )
708             break;
709
710         /* Set capture so that the window captures all the mouse messages */
711         /*
712          * XXX - Multiple button support:  Under X11, the mouse is not released
713          * XXX - from the window until all buttons have been released, even if the
714          * XXX - user presses a button in another window.  This will take more
715          * XXX - code changes than I am up to at the moment (10/5/04).  The present
716          * XXX - is a 90 percent solution.
717          */
718         if ( pressed == GL_TRUE )
719           SetCapture ( window->Window.Handle ) ;
720         else
721           ReleaseCapture () ;
722
723         if( ! FETCH_WCB( *window, Mouse ) )
724             break;
725
726         fgSetWindow( window );
727         fgState.Modifiers = fgPlatformGetModifiers( );
728
729         INVOKE_WCB(
730             *window, Mouse,
731             ( button,
732               pressed ? GLUT_DOWN : GLUT_UP,
733               window->State.MouseX,
734               window->State.MouseY
735             )
736         );
737
738         fgState.Modifiers = INVALID_MODIFIERS;
739     }
740     break;
741
742     case WM_MOUSEWHEEL:
743     {
744         int wheel_number = LOWORD( wParam );
745         short ticks = ( short )HIWORD( wParam );
746                 fgState.MouseWheelTicks += ticks;
747
748         /*
749          * XXX Should use WHEEL_DELTA instead of 120
750          */
751                 if ( abs ( fgState.MouseWheelTicks ) >= 120 )
752                 {
753                         int direction = ( fgState.MouseWheelTicks > 0 ) ? 1 : -1;
754
755             if( ! FETCH_WCB( *window, MouseWheel ) &&
756                 ! FETCH_WCB( *window, Mouse ) )
757                 break;
758
759             fgSetWindow( window );
760             fgState.Modifiers = fgPlatformGetModifiers( );
761
762             /*
763              * XXX Should use WHEEL_DELTA instead of 120
764              */
765             while( abs ( fgState.MouseWheelTicks ) >= 120 )
766                         {
767                 if( FETCH_WCB( *window, MouseWheel ) )
768                     INVOKE_WCB( *window, MouseWheel,
769                                 ( wheel_number,
770                                   direction,
771                                   window->State.MouseX,
772                                   window->State.MouseY
773                                 )
774                     );
775                 else  /* No mouse wheel, call the mouse button callback twice */
776                                 {
777                     /*
778                      * Map wheel zero to button 3 and 4; +1 to 3, -1 to 4
779                      *  "    "   one                     +1 to 5, -1 to 6, ...
780                      *
781                      * XXX The below assumes that you have no more than 3 mouse
782                      * XXX buttons.  Sorry.
783                      */
784                     int button = wheel_number * 2 + 3;
785                     if( direction < 0 )
786                         ++button;
787                     INVOKE_WCB( *window, Mouse,
788                                 ( button, GLUT_DOWN,
789                                   window->State.MouseX, window->State.MouseY )
790                     );
791                     INVOKE_WCB( *window, Mouse,
792                                 ( button, GLUT_UP,
793                                   window->State.MouseX, window->State.MouseY )
794                     );
795                                 }
796
797                 /*
798                  * XXX Should use WHEEL_DELTA instead of 120
799                  */
800                                 fgState.MouseWheelTicks -= 120 * direction;
801                         }
802
803             fgState.Modifiers = INVALID_MODIFIERS;
804                 }
805     }
806     break ;
807
808     case WM_SYSKEYDOWN:
809     case WM_KEYDOWN:
810     {
811         int keypress = -1;
812         POINT mouse_pos ;
813
814
815         if( ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE ) && (HIWORD(lParam) & KF_REPEAT) )
816             break;
817
818         /*
819          * Remember the current modifiers state. This is done here in order
820          * to make sure the VK_DELETE keyboard callback is executed properly.
821          */
822         fgState.Modifiers = fgPlatformGetModifiers( );
823
824         GetCursorPos( &mouse_pos );
825         ScreenToClient( window->Window.Handle, &mouse_pos );
826
827         window->State.MouseX = mouse_pos.x;
828         window->State.MouseY = mouse_pos.y;
829
830         /* Convert the Win32 keystroke codes to GLUTtish way */
831 #       define KEY(a,b) case a: keypress = b; break;
832
833         switch( wParam )
834         {
835             KEY( VK_F1,     GLUT_KEY_F1        );
836             KEY( VK_F2,     GLUT_KEY_F2        );
837             KEY( VK_F3,     GLUT_KEY_F3        );
838             KEY( VK_F4,     GLUT_KEY_F4        );
839             KEY( VK_F5,     GLUT_KEY_F5        );
840             KEY( VK_F6,     GLUT_KEY_F6        );
841             KEY( VK_F7,     GLUT_KEY_F7        );
842             KEY( VK_F8,     GLUT_KEY_F8        );
843             KEY( VK_F9,     GLUT_KEY_F9        );
844             KEY( VK_F10,    GLUT_KEY_F10       );
845             KEY( VK_F11,    GLUT_KEY_F11       );
846             KEY( VK_F12,    GLUT_KEY_F12       );
847             KEY( VK_PRIOR,  GLUT_KEY_PAGE_UP   );
848             KEY( VK_NEXT,   GLUT_KEY_PAGE_DOWN );
849             KEY( VK_HOME,   GLUT_KEY_HOME      );
850             KEY( VK_END,    GLUT_KEY_END       );
851             KEY( VK_LEFT,   GLUT_KEY_LEFT      );
852             KEY( VK_UP,     GLUT_KEY_UP        );
853             KEY( VK_RIGHT,  GLUT_KEY_RIGHT     );
854             KEY( VK_DOWN,   GLUT_KEY_DOWN      );
855             KEY( VK_INSERT, GLUT_KEY_INSERT    );
856
857         case VK_LCONTROL:  case VK_RCONTROL:  case VK_CONTROL:
858         case VK_LSHIFT:    case VK_RSHIFT:    case VK_SHIFT:
859         case VK_LMENU:     case VK_RMENU:     case VK_MENU:
860             /* These keypresses and releases are handled earlier in the function */
861             break;
862
863         case VK_DELETE:
864             /* The delete key should be treated as an ASCII keypress: */
865             INVOKE_WCB( *window, Keyboard,
866                         ( 127, window->State.MouseX, window->State.MouseY )
867             );
868         }
869
870 #if defined(_WIN32_WCE)
871         if(!(lParam & 0x40000000)) /* Prevent auto-repeat */
872         {
873             if(wParam==(unsigned)gxKeyList.vkRight)
874                 keypress = GLUT_KEY_RIGHT;
875             else if(wParam==(unsigned)gxKeyList.vkLeft)
876                 keypress = GLUT_KEY_LEFT;
877             else if(wParam==(unsigned)gxKeyList.vkUp)
878                 keypress = GLUT_KEY_UP;
879             else if(wParam==(unsigned)gxKeyList.vkDown)
880                 keypress = GLUT_KEY_DOWN;
881             else if(wParam==(unsigned)gxKeyList.vkA)
882                 keypress = GLUT_KEY_F1;
883             else if(wParam==(unsigned)gxKeyList.vkB)
884                 keypress = GLUT_KEY_F2;
885             else if(wParam==(unsigned)gxKeyList.vkC)
886                 keypress = GLUT_KEY_F3;
887             else if(wParam==(unsigned)gxKeyList.vkStart)
888                 keypress = GLUT_KEY_F4;
889         }
890 #endif
891
892         if( keypress != -1 )
893             INVOKE_WCB( *window, Special,
894                         ( keypress,
895                           window->State.MouseX, window->State.MouseY )
896             );
897
898         fgState.Modifiers = INVALID_MODIFIERS;
899     }
900     break;
901
902     case WM_SYSKEYUP:
903     case WM_KEYUP:
904     {
905         int keypress = -1;
906         POINT mouse_pos;
907
908         /*
909          * Remember the current modifiers state. This is done here in order
910          * to make sure the VK_DELETE keyboard callback is executed properly.
911          */
912         fgState.Modifiers = fgPlatformGetModifiers( );
913
914         GetCursorPos( &mouse_pos );
915         ScreenToClient( window->Window.Handle, &mouse_pos );
916
917         window->State.MouseX = mouse_pos.x;
918         window->State.MouseY = mouse_pos.y;
919
920         /*
921          * Convert the Win32 keystroke codes to GLUTtish way.
922          * "KEY(a,b)" was defined under "WM_KEYDOWN"
923          */
924
925         switch( wParam )
926         {
927             KEY( VK_F1,     GLUT_KEY_F1        );
928             KEY( VK_F2,     GLUT_KEY_F2        );
929             KEY( VK_F3,     GLUT_KEY_F3        );
930             KEY( VK_F4,     GLUT_KEY_F4        );
931             KEY( VK_F5,     GLUT_KEY_F5        );
932             KEY( VK_F6,     GLUT_KEY_F6        );
933             KEY( VK_F7,     GLUT_KEY_F7        );
934             KEY( VK_F8,     GLUT_KEY_F8        );
935             KEY( VK_F9,     GLUT_KEY_F9        );
936             KEY( VK_F10,    GLUT_KEY_F10       );
937             KEY( VK_F11,    GLUT_KEY_F11       );
938             KEY( VK_F12,    GLUT_KEY_F12       );
939             KEY( VK_PRIOR,  GLUT_KEY_PAGE_UP   );
940             KEY( VK_NEXT,   GLUT_KEY_PAGE_DOWN );
941             KEY( VK_HOME,   GLUT_KEY_HOME      );
942             KEY( VK_END,    GLUT_KEY_END       );
943             KEY( VK_LEFT,   GLUT_KEY_LEFT      );
944             KEY( VK_UP,     GLUT_KEY_UP        );
945             KEY( VK_RIGHT,  GLUT_KEY_RIGHT     );
946             KEY( VK_DOWN,   GLUT_KEY_DOWN      );
947             KEY( VK_INSERT, GLUT_KEY_INSERT    );
948
949           case VK_LCONTROL:  case VK_RCONTROL:  case VK_CONTROL:
950           case VK_LSHIFT:    case VK_RSHIFT:    case VK_SHIFT:
951           case VK_LMENU:     case VK_RMENU:     case VK_MENU:
952               /* These keypresses and releases are handled earlier in the function */
953               break;
954
955           case VK_DELETE:
956               /* The delete key should be treated as an ASCII keypress: */
957               INVOKE_WCB( *window, KeyboardUp,
958                           ( 127, window->State.MouseX, window->State.MouseY )
959               );
960               break;
961
962         default:
963         {
964 #if !defined(_WIN32_WCE)
965             BYTE state[ 256 ];
966             WORD code[ 2 ];
967
968             GetKeyboardState( state );
969
970             if( ToAscii( (UINT)wParam, 0, state, code, 0 ) == 1 )
971                 wParam=code[ 0 ];
972
973             INVOKE_WCB( *window, KeyboardUp,
974                         ( (char)wParam,
975                           window->State.MouseX, window->State.MouseY )
976             );
977 #endif /* !defined(_WIN32_WCE) */
978         }
979         }
980
981         if( keypress != -1 )
982             INVOKE_WCB( *window, SpecialUp,
983                         ( keypress,
984                           window->State.MouseX, window->State.MouseY )
985             );
986
987         fgState.Modifiers = INVALID_MODIFIERS;
988     }
989     break;
990
991     case WM_SYSCHAR:
992     case WM_CHAR:
993     {
994       if( (fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE) && (HIWORD(lParam) & KF_REPEAT) )
995             break;
996
997         fgState.Modifiers = fgPlatformGetModifiers( );
998         INVOKE_WCB( *window, Keyboard,
999                     ( (char)wParam,
1000                       window->State.MouseX, window->State.MouseY )
1001         );
1002         fgState.Modifiers = INVALID_MODIFIERS;
1003     }
1004     break;
1005
1006     case WM_CAPTURECHANGED:
1007         /* User has finished resizing the window, force a redraw */
1008         INVOKE_WCB( *window, Display, ( ) );
1009
1010         /*lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ); */
1011         break;
1012
1013         /* Other messages that I have seen and which are not handled already */
1014     case WM_SETTEXT:  /* 0x000c */
1015         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1016         /* Pass it on to "DefWindowProc" to set the window text */
1017         break;
1018
1019     case WM_GETTEXT:  /* 0x000d */
1020         /* Ideally we would copy the title of the window into "lParam" */
1021         /* strncpy ( (char *)lParam, "Window Title", wParam );
1022            lRet = ( wParam > 12 ) ? 12 : wParam;  */
1023         /* the number of characters copied */
1024         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1025         break;
1026
1027     case WM_GETTEXTLENGTH:  /* 0x000e */
1028         /* Ideally we would get the length of the title of the window */
1029         lRet = 12;
1030         /* the number of characters in "Window Title\0" (see above) */
1031         break;
1032
1033     case WM_ERASEBKGND:  /* 0x0014 */
1034         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1035         break;
1036
1037 #if !defined(_WIN32_WCE)
1038     case WM_SYNCPAINT:  /* 0x0088 */
1039         /* Another window has moved, need to update this one */
1040         window->State.Redisplay = GL_TRUE;
1041         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1042         /* Help screen says this message must be passed to "DefWindowProc" */
1043         break;
1044
1045     case WM_NCPAINT:  /* 0x0085 */
1046       /* Need to update the border of this window */
1047         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1048         /* Pass it on to "DefWindowProc" to repaint a standard border */
1049         break;
1050
1051     case WM_SYSCOMMAND :  /* 0x0112 */
1052         {
1053           /*
1054            * We have received a system command message.  Try to act on it.
1055            * The commands are passed in through the "wParam" parameter:
1056            * The least significant digit seems to be which edge of the window
1057            * is being used for a resize event:
1058            *     4  3  5
1059            *     1     2
1060            *     7  6  8
1061            * Congratulations and thanks to Richard Rauch for figuring this out..
1062            */
1063             switch ( wParam & 0xfff0 )
1064             {
1065             case SC_SIZE       :
1066                 break ;
1067
1068             case SC_MOVE       :
1069                 break ;
1070
1071             case SC_MINIMIZE   :
1072                 /* User has clicked on the "-" to minimize the window */
1073                 /* Turn off the visibility */
1074                 window->State.Visible = GL_FALSE ;
1075
1076                 break ;
1077
1078             case SC_MAXIMIZE   :
1079                 break ;
1080
1081             case SC_NEXTWINDOW :
1082                 break ;
1083
1084             case SC_PREVWINDOW :
1085                 break ;
1086
1087             case SC_CLOSE      :
1088                 /* Followed very closely by a WM_CLOSE message */
1089                 break ;
1090
1091             case SC_VSCROLL    :
1092                 break ;
1093
1094             case SC_HSCROLL    :
1095                 break ;
1096
1097             case SC_MOUSEMENU  :
1098                 break ;
1099
1100             case SC_KEYMENU    :
1101                 break ;
1102
1103             case SC_ARRANGE    :
1104                 break ;
1105
1106             case SC_RESTORE    :
1107                 break ;
1108
1109             case SC_TASKLIST   :
1110                 break ;
1111
1112             case SC_SCREENSAVE :
1113                 break ;
1114
1115             case SC_HOTKEY     :
1116                 break ;
1117
1118 #if(WINVER >= 0x0400)
1119             case SC_DEFAULT    :
1120                 break ;
1121
1122             case SC_MONITORPOWER    :
1123                 break ;
1124
1125             case SC_CONTEXTHELP    :
1126                 break ;
1127 #endif /* WINVER >= 0x0400 */
1128
1129             default:
1130 #if _DEBUG
1131                 fgWarning( "Unknown wParam type 0x%x", wParam );
1132 #endif
1133                 break;
1134             }
1135         }
1136 #endif /* !defined(_WIN32_WCE) */
1137
1138         /* We need to pass the message on to the operating system as well */
1139         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1140         break;
1141
1142 #ifdef WM_TOUCH
1143         /* handle multi-touch messages */
1144         case WM_TOUCH:
1145         {
1146                 unsigned int numInputs = (unsigned int)wParam;
1147                 unsigned int i = 0;
1148                 TOUCHINPUT* ti = (TOUCHINPUT*)malloc( sizeof(TOUCHINPUT)*numInputs);
1149
1150                 if (fghGetTouchInputInfo == (pGetTouchInputInfo)0xDEADBEEF) {
1151                     fghGetTouchInputInfo = (pGetTouchInputInfo)GetProcAddress(GetModuleHandle("user32"),"GetTouchInputInfo");
1152                     fghCloseTouchInputHandle = (pCloseTouchInputHandle)GetProcAddress(GetModuleHandle("user32"),"CloseTouchInputHandle");
1153                 }
1154
1155                 if (!fghGetTouchInputInfo) { 
1156                         free( (void*)ti );
1157                         break;
1158                 }
1159
1160                 if (fghGetTouchInputInfo( (HTOUCHINPUT)lParam, numInputs, ti, sizeof(TOUCHINPUT) )) {
1161                         /* Handle each contact point */
1162                         for (i = 0; i < numInputs; ++i ) {
1163
1164                                 POINT tp;
1165                                 tp.x = TOUCH_COORD_TO_PIXEL(ti[i].x);
1166                                 tp.y = TOUCH_COORD_TO_PIXEL(ti[i].y);
1167                                 ScreenToClient( hWnd, &tp );
1168
1169                                 ti[i].dwID = ti[i].dwID * 2;
1170
1171                                 if (ti[i].dwFlags & TOUCHEVENTF_DOWN) {
1172                                         INVOKE_WCB( *window, MultiEntry,  ( ti[i].dwID, GLUT_ENTERED ) );
1173                                         INVOKE_WCB( *window, MultiButton, ( ti[i].dwID, tp.x, tp.y, 0, GLUT_DOWN ) );
1174                                 } else if (ti[i].dwFlags & TOUCHEVENTF_MOVE) {
1175                                         INVOKE_WCB( *window, MultiMotion, ( ti[i].dwID, tp.x, tp.y ) );
1176                                 } else if (ti[i].dwFlags & TOUCHEVENTF_UP)   { 
1177                                         INVOKE_WCB( *window, MultiButton, ( ti[i].dwID, tp.x, tp.y, 0, GLUT_UP ) );
1178                                         INVOKE_WCB( *window, MultiEntry,  ( ti[i].dwID, GLUT_LEFT ) );
1179                                 }
1180                         }
1181                 }
1182                 fghCloseTouchInputHandle((HTOUCHINPUT)lParam);
1183                 free( (void*)ti );
1184                 lRet = 0; /*DefWindowProc( hWnd, uMsg, wParam, lParam );*/
1185                 break;
1186         }
1187 #endif
1188     default:
1189         /* Handle unhandled messages */
1190         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1191         break;
1192     }
1193
1194     return lRet;
1195 }