Moving the source code files from 'src' to 'src/Common' as a first step towards separ...
[freeglut] / src / Common / freeglut_main.c
1 /*\r
2  * freeglut_main.c\r
3  *\r
4  * The windows message processing methods.\r
5  *\r
6  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.\r
7  * Written by Pawel W. Olszta, <olszta@sourceforge.net>\r
8  * Creation date: Fri Dec 3 1999\r
9  *\r
10  * Permission is hereby granted, free of charge, to any person obtaining a\r
11  * copy of this software and associated documentation files (the "Software"),\r
12  * to deal in the Software without restriction, including without limitation\r
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,\r
14  * and/or sell copies of the Software, and to permit persons to whom the\r
15  * Software is furnished to do so, subject to the following conditions:\r
16  *\r
17  * The above copyright notice and this permission notice shall be included\r
18  * in all copies or substantial portions of the Software.\r
19  *\r
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL\r
23  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
24  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
26  */\r
27 \r
28 #include <GL/freeglut.h>\r
29 #include "freeglut_internal.h"\r
30 #ifdef HAVE_ERRNO_H\r
31 #    include <errno.h>\r
32 #endif\r
33 #include <stdarg.h>\r
34 #ifdef  HAVE_VFPRINTF\r
35 #    define VFPRINTF(s,f,a) vfprintf((s),(f),(a))\r
36 #elif defined(HAVE__DOPRNT)\r
37 #    define VFPRINTF(s,f,a) _doprnt((f),(a),(s))\r
38 #else\r
39 #    define VFPRINTF(s,f,a)\r
40 #endif\r
41 \r
42 #ifdef _WIN32_WCE\r
43 \r
44 typedef struct GXDisplayProperties GXDisplayProperties;\r
45 typedef struct GXKeyList GXKeyList;\r
46 #include <gx.h>\r
47 \r
48 typedef struct GXKeyList (*GXGETDEFAULTKEYS)(int);\r
49 typedef int (*GXOPENINPUT)();\r
50 \r
51 GXGETDEFAULTKEYS GXGetDefaultKeys_ = NULL;\r
52 GXOPENINPUT GXOpenInput_ = NULL;\r
53 \r
54 struct GXKeyList gxKeyList;\r
55 \r
56 #endif /* _WIN32_WCE */\r
57 \r
58 /*\r
59  * Try to get the maximum value allowed for ints, falling back to the minimum\r
60  * guaranteed by ISO C99 if there is no suitable header.\r
61  */\r
62 #ifdef HAVE_LIMITS_H\r
63 #    include <limits.h>\r
64 #endif\r
65 #ifndef INT_MAX\r
66 #    define INT_MAX 32767\r
67 #endif\r
68 \r
69 #ifndef MIN\r
70 #    define MIN(a,b) (((a)<(b)) ? (a) : (b))\r
71 #endif\r
72 \r
73 #ifdef WM_TOUCH\r
74     typedef BOOL (WINAPI *pGetTouchInputInfo)(HTOUCHINPUT,UINT,PTOUCHINPUT,int);\r
75     typedef BOOL (WINAPI *pCloseTouchInputHandle)(HTOUCHINPUT);\r
76         static pGetTouchInputInfo fghGetTouchInputInfo = (pGetTouchInputInfo)0xDEADBEEF;\r
77         static pCloseTouchInputHandle fghCloseTouchInputHandle = (pCloseTouchInputHandle)0xDEADBEEF;\r
78 #endif\r
79 \r
80 /*\r
81  * TODO BEFORE THE STABLE RELEASE:\r
82  *\r
83  * There are some issues concerning window redrawing under X11, and maybe\r
84  * some events are not handled. The Win32 version lacks some more features,\r
85  * but seems acceptable for not demanding purposes.\r
86  *\r
87  * Need to investigate why the X11 version breaks out with an error when\r
88  * closing a window (using the window manager, not glutDestroyWindow)...\r
89  */\r
90 \r
91 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */\r
92 \r
93 /*\r
94  * Handle a window configuration change. When no reshape\r
95  * callback is hooked, the viewport size is updated to\r
96  * match the new window size.\r
97  */\r
98 static void fghReshapeWindow ( SFG_Window *window, int width, int height )\r
99 {\r
100     SFG_Window *current_window = fgStructure.CurrentWindow;\r
101 \r
102     freeglut_return_if_fail( window != NULL );\r
103 \r
104 #if TARGET_HOST_POSIX_X11\r
105 \r
106     XResizeWindow( fgDisplay.Display, window->Window.Handle,\r
107                    width, height );\r
108     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */\r
109 \r
110 #elif TARGET_HOST_MS_WINDOWS && !defined(_WIN32_WCE)\r
111     {\r
112         RECT windowRect;\r
113 \r
114         /*\r
115          * For windowed mode, get the current position of the\r
116          * window and resize taking the size of the frame\r
117          * decorations into account.\r
118          */\r
119 \r
120         /* "GetWindowRect" returns the pixel coordinates of the outside of the window */\r
121         GetWindowRect( window->Window.Handle, &windowRect );\r
122 \r
123         /* Create rect in FreeGLUT format, (X,Y) topleft outside window, WxH of client area */\r
124         windowRect.right    = windowRect.left+width;\r
125         windowRect.bottom   = windowRect.top+height;\r
126 \r
127         if (window->Parent == NULL)\r
128             /* get the window rect from this to feed to SetWindowPos, correct for window decorations */\r
129             fghComputeWindowRectFromClientArea_QueryWindow(window,&windowRect,TRUE);\r
130         else\r
131         {\r
132             /* correct rect for position client area of parent window\r
133              * (SetWindowPos input for child windows is in coordinates\r
134              * relative to the parent's client area).\r
135              * Child windows don't have decoration, so no need to correct\r
136              * for them.\r
137              */\r
138             RECT parentRect;\r
139             parentRect = fghGetClientArea( window->Parent, FALSE );\r
140             windowRect.left   -= parentRect.left;\r
141             windowRect.right  -= parentRect.left;\r
142             windowRect.top    -= parentRect.top;\r
143             windowRect.bottom -= parentRect.top;\r
144         }\r
145         \r
146         /* Do the actual resizing */\r
147         SetWindowPos( window->Window.Handle,\r
148                       HWND_TOP,\r
149                       windowRect.left, windowRect.top,\r
150                       windowRect.right - windowRect.left,\r
151                       windowRect.bottom- windowRect.top,\r
152                       SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |\r
153                       SWP_NOZORDER\r
154         );\r
155     }\r
156 #endif\r
157 \r
158     if( FETCH_WCB( *window, Reshape ) )\r
159         INVOKE_WCB( *window, Reshape, ( width, height ) );\r
160     else\r
161     {\r
162         fgSetWindow( window );\r
163         glViewport( 0, 0, width, height );\r
164     }\r
165 \r
166     /*\r
167      * Force a window redraw.  In Windows at least this is only a partial\r
168      * solution:  if the window is increasing in size in either dimension,\r
169      * the already-drawn part does not get drawn again and things look funny.\r
170      * But without this we get this bad behaviour whenever we resize the\r
171      * window.\r
172      */\r
173     window->State.Redisplay = GL_TRUE;\r
174 \r
175     if( window->IsMenu )\r
176         fgSetWindow( current_window );\r
177 }\r
178 \r
179 /*\r
180  * Calls a window's redraw method. This is used when\r
181  * a redraw is forced by the incoming window messages.\r
182  */\r
183 static void fghRedrawWindow ( SFG_Window *window )\r
184 {\r
185     SFG_Window *current_window = fgStructure.CurrentWindow;\r
186 \r
187     freeglut_return_if_fail( window );\r
188     freeglut_return_if_fail( FETCH_WCB ( *window, Display ) );\r
189 \r
190     window->State.Redisplay = GL_FALSE;\r
191 \r
192     freeglut_return_if_fail( window->State.Visible );\r
193 \r
194     fgSetWindow( window );\r
195 \r
196     if( window->State.NeedToResize )\r
197     {\r
198         fghReshapeWindow(\r
199             window,\r
200             window->State.Width,\r
201             window->State.Height\r
202         );\r
203 \r
204         window->State.NeedToResize = GL_FALSE;\r
205     }\r
206 \r
207     INVOKE_WCB( *window, Display, ( ) );\r
208 \r
209     fgSetWindow( current_window );\r
210 }\r
211 \r
212 /*\r
213  * A static helper function to execute display callback for a window\r
214  */\r
215 static void fghcbDisplayWindow( SFG_Window *window,\r
216                                 SFG_Enumerator *enumerator )\r
217 {\r
218     if( window->State.Redisplay &&\r
219         window->State.Visible )\r
220     {\r
221         window->State.Redisplay = GL_FALSE;\r
222 \r
223 #if TARGET_HOST_POSIX_X11\r
224         fghRedrawWindow ( window ) ;\r
225 #elif TARGET_HOST_MS_WINDOWS\r
226 \r
227         RedrawWindow(\r
228             window->Window.Handle, NULL, NULL,\r
229             RDW_NOERASE | RDW_INTERNALPAINT | RDW_INVALIDATE | RDW_UPDATENOW\r
230         );\r
231 #endif\r
232     }\r
233 \r
234     fgEnumSubWindows( window, fghcbDisplayWindow, enumerator );\r
235 }\r
236 \r
237 /*\r
238  * Make all windows perform a display call\r
239  */\r
240 static void fghDisplayAll( void )\r
241 {\r
242     SFG_Enumerator enumerator;\r
243 \r
244     enumerator.found = GL_FALSE;\r
245     enumerator.data  =  NULL;\r
246 \r
247     fgEnumWindows( fghcbDisplayWindow, &enumerator );\r
248 }\r
249 \r
250 /*\r
251  * Window enumerator callback to check for the joystick polling code\r
252  */\r
253 static void fghcbCheckJoystickPolls( SFG_Window *window,\r
254                                      SFG_Enumerator *enumerator )\r
255 {\r
256     long int checkTime = fgElapsedTime( );\r
257 \r
258     if( window->State.JoystickLastPoll + window->State.JoystickPollRate <=\r
259         checkTime )\r
260     {\r
261 #if !defined(_WIN32_WCE)\r
262         fgJoystickPollWindow( window );\r
263 #endif /* !defined(_WIN32_WCE) */\r
264         window->State.JoystickLastPoll = checkTime;\r
265     }\r
266 \r
267     fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );\r
268 }\r
269 \r
270 /*\r
271  * Check all windows for joystick polling\r
272  */\r
273 static void fghCheckJoystickPolls( void )\r
274 {\r
275     SFG_Enumerator enumerator;\r
276 \r
277     enumerator.found = GL_FALSE;\r
278     enumerator.data  =  NULL;\r
279 \r
280     fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );\r
281 }\r
282 \r
283 /*\r
284  * Check the global timers\r
285  */\r
286 static void fghCheckTimers( void )\r
287 {\r
288     long checkTime = fgElapsedTime( );\r
289 \r
290     while( fgState.Timers.First )\r
291     {\r
292         SFG_Timer *timer = fgState.Timers.First;\r
293 \r
294         if( timer->TriggerTime > checkTime )\r
295             break;\r
296 \r
297         fgListRemove( &fgState.Timers, &timer->Node );\r
298         fgListAppend( &fgState.FreeTimers, &timer->Node );\r
299 \r
300         timer->Callback( timer->ID );\r
301     }\r
302 }\r
303 \r
304  \r
305 /* Platform-dependent time in milliseconds, as an unsigned 32-bit integer.\r
306  * This value wraps every 49.7 days, but integer overflows cancel\r
307  * when subtracting an initial start time, unless the total time exceeds\r
308  * 32-bit, where the GLUT API return value is also overflowed.\r
309  */  \r
310 unsigned long fgSystemTime(void) {\r
311 #if TARGET_HOST_SOLARIS || HAVE_GETTIMEOFDAY\r
312     struct timeval now;\r
313     gettimeofday( &now, NULL );\r
314     return now.tv_usec/1000 + now.tv_sec*1000;\r
315 #elif TARGET_HOST_MS_WINDOWS\r
316 #    if defined(_WIN32_WCE)\r
317     return GetTickCount();\r
318 #    else\r
319     return timeGetTime();\r
320 #    endif\r
321 #endif\r
322 }\r
323   \r
324 /*\r
325  * Elapsed Time\r
326  */\r
327 long fgElapsedTime( void )\r
328 {\r
329     return (long) (fgSystemTime() - fgState.Time);\r
330 }\r
331 \r
332 /*\r
333  * Error Messages.\r
334  */\r
335 void fgError( const char *fmt, ... )\r
336 {\r
337     va_list ap;\r
338 \r
339     if (fgState.ErrorFunc) {\r
340 \r
341         va_start( ap, fmt );\r
342 \r
343         /* call user set error handler here */\r
344         fgState.ErrorFunc(fmt, ap);\r
345 \r
346         va_end( ap );\r
347 \r
348     } else {\r
349 \r
350         va_start( ap, fmt );\r
351 \r
352         fprintf( stderr, "freeglut ");\r
353         if( fgState.ProgramName )\r
354             fprintf( stderr, "(%s): ", fgState.ProgramName );\r
355         VFPRINTF( stderr, fmt, ap );\r
356         fprintf( stderr, "\n" );\r
357 \r
358         va_end( ap );\r
359 \r
360         if ( fgState.Initialised )\r
361             fgDeinitialize ();\r
362 \r
363         exit( 1 );\r
364     }\r
365 }\r
366 \r
367 void fgWarning( const char *fmt, ... )\r
368 {\r
369     va_list ap;\r
370 \r
371     if (fgState.WarningFunc) {\r
372 \r
373         va_start( ap, fmt );\r
374 \r
375         /* call user set warning handler here */\r
376         fgState.WarningFunc(fmt, ap);\r
377 \r
378         va_end( ap );\r
379 \r
380     } else {\r
381 \r
382         va_start( ap, fmt );\r
383 \r
384         fprintf( stderr, "freeglut ");\r
385         if( fgState.ProgramName )\r
386             fprintf( stderr, "(%s): ", fgState.ProgramName );\r
387         VFPRINTF( stderr, fmt, ap );\r
388         fprintf( stderr, "\n" );\r
389 \r
390         va_end( ap );\r
391     }\r
392 }\r
393 \r
394 \r
395 /*\r
396  * Indicates whether Joystick events are being used by ANY window.\r
397  *\r
398  * The current mechanism is to walk all of the windows and ask if\r
399  * there is a joystick callback.  We have a short-circuit early\r
400  * return if we find any joystick handler registered.\r
401  *\r
402  * The real way to do this is to make use of the glutTimer() API\r
403  * to more cleanly re-implement the joystick API.  Then, this code\r
404  * and all other "joystick timer" code can be yanked.\r
405  *\r
406  */\r
407 static void fghCheckJoystickCallback( SFG_Window* w, SFG_Enumerator* e)\r
408 {\r
409     if( FETCH_WCB( *w, Joystick ) )\r
410     {\r
411         e->found = GL_TRUE;\r
412         e->data = w;\r
413     }\r
414     fgEnumSubWindows( w, fghCheckJoystickCallback, e );\r
415 }\r
416 static int fghHaveJoystick( void )\r
417 {\r
418     SFG_Enumerator enumerator;\r
419 \r
420     enumerator.found = GL_FALSE;\r
421     enumerator.data = NULL;\r
422     fgEnumWindows( fghCheckJoystickCallback, &enumerator );\r
423     return !!enumerator.data;\r
424 }\r
425 static void fghHavePendingRedisplaysCallback( SFG_Window* w, SFG_Enumerator* e)\r
426 {\r
427     if( w->State.Redisplay && w->State.Visible )\r
428     {\r
429         e->found = GL_TRUE;\r
430         e->data = w;\r
431     }\r
432     fgEnumSubWindows( w, fghHavePendingRedisplaysCallback, e );\r
433 }\r
434 static int fghHavePendingRedisplays (void)\r
435 {\r
436     SFG_Enumerator enumerator;\r
437 \r
438     enumerator.found = GL_FALSE;\r
439     enumerator.data = NULL;\r
440     fgEnumWindows( fghHavePendingRedisplaysCallback, &enumerator );\r
441     return !!enumerator.data;\r
442 }\r
443 /*\r
444  * Returns the number of GLUT ticks (milliseconds) till the next timer event.\r
445  */\r
446 static long fghNextTimer( void )\r
447 {\r
448     long ret = INT_MAX;\r
449     SFG_Timer *timer = fgState.Timers.First;\r
450 \r
451     if( timer )\r
452         ret = timer->TriggerTime - fgElapsedTime();\r
453     if( ret < 0 )\r
454         ret = 0;\r
455 \r
456     return ret;\r
457 }\r
458 /*\r
459  * Does the magic required to relinquish the CPU until something interesting\r
460  * happens.\r
461  */\r
462 static void fghSleepForEvents( void )\r
463 {\r
464     long msec;\r
465 \r
466     if( fgState.IdleCallback || fghHavePendingRedisplays( ) )\r
467         return;\r
468 \r
469     msec = fghNextTimer( );\r
470     /* XXX Use GLUT timers for joysticks... */\r
471     /* XXX Dumb; forces granularity to .01sec */\r
472     if( fghHaveJoystick( ) && ( msec > 10 ) )     \r
473         msec = 10;\r
474 \r
475 #if TARGET_HOST_POSIX_X11\r
476     /*\r
477      * Possibly due to aggressive use of XFlush() and friends,\r
478      * it is possible to have our socket drained but still have\r
479      * unprocessed events.  (Or, this may just be normal with\r
480      * X, anyway?)  We do non-trivial processing of X events\r
481      * after the event-reading loop, in any case, so we\r
482      * need to allow that we may have an empty socket but non-\r
483      * empty event queue.\r
484      */\r
485     if( ! XPending( fgDisplay.Display ) )\r
486     {\r
487         fd_set fdset;\r
488         int err;\r
489         int socket;\r
490         struct timeval wait;\r
491 \r
492         socket = ConnectionNumber( fgDisplay.Display );\r
493         FD_ZERO( &fdset );\r
494         FD_SET( socket, &fdset );\r
495         wait.tv_sec = msec / 1000;\r
496         wait.tv_usec = (msec % 1000) * 1000;\r
497         err = select( socket+1, &fdset, NULL, NULL, &wait );\r
498 \r
499 #ifdef HAVE_ERRNO_H\r
500         if( ( -1 == err ) && ( errno != EINTR ) )\r
501             fgWarning ( "freeglut select() error: %d", errno );\r
502 #endif\r
503     }\r
504 #elif TARGET_HOST_MS_WINDOWS\r
505     MsgWaitForMultipleObjects( 0, NULL, FALSE, msec, QS_ALLINPUT );\r
506 #endif\r
507 }\r
508 \r
509 #if TARGET_HOST_POSIX_X11\r
510 /*\r
511  * Returns GLUT modifier mask for the state field of an X11 event.\r
512  */\r
513 int fghGetXModifiers( int state )\r
514 {\r
515     int ret = 0;\r
516 \r
517     if( state & ( ShiftMask | LockMask ) )\r
518         ret |= GLUT_ACTIVE_SHIFT;\r
519     if( state & ControlMask )\r
520         ret |= GLUT_ACTIVE_CTRL;\r
521     if( state & Mod1Mask )\r
522         ret |= GLUT_ACTIVE_ALT;\r
523 \r
524     return ret;\r
525 }\r
526 #endif\r
527 \r
528 \r
529 #if TARGET_HOST_POSIX_X11 && _DEBUG\r
530 \r
531 static const char* fghTypeToString( int type )\r
532 {\r
533     switch( type ) {\r
534     case KeyPress: return "KeyPress";\r
535     case KeyRelease: return "KeyRelease";\r
536     case ButtonPress: return "ButtonPress";\r
537     case ButtonRelease: return "ButtonRelease";\r
538     case MotionNotify: return "MotionNotify";\r
539     case EnterNotify: return "EnterNotify";\r
540     case LeaveNotify: return "LeaveNotify";\r
541     case FocusIn: return "FocusIn";\r
542     case FocusOut: return "FocusOut";\r
543     case KeymapNotify: return "KeymapNotify";\r
544     case Expose: return "Expose";\r
545     case GraphicsExpose: return "GraphicsExpose";\r
546     case NoExpose: return "NoExpose";\r
547     case VisibilityNotify: return "VisibilityNotify";\r
548     case CreateNotify: return "CreateNotify";\r
549     case DestroyNotify: return "DestroyNotify";\r
550     case UnmapNotify: return "UnmapNotify";\r
551     case MapNotify: return "MapNotify";\r
552     case MapRequest: return "MapRequest";\r
553     case ReparentNotify: return "ReparentNotify";\r
554     case ConfigureNotify: return "ConfigureNotify";\r
555     case ConfigureRequest: return "ConfigureRequest";\r
556     case GravityNotify: return "GravityNotify";\r
557     case ResizeRequest: return "ResizeRequest";\r
558     case CirculateNotify: return "CirculateNotify";\r
559     case CirculateRequest: return "CirculateRequest";\r
560     case PropertyNotify: return "PropertyNotify";\r
561     case SelectionClear: return "SelectionClear";\r
562     case SelectionRequest: return "SelectionRequest";\r
563     case SelectionNotify: return "SelectionNotify";\r
564     case ColormapNotify: return "ColormapNotify";\r
565     case ClientMessage: return "ClientMessage";\r
566     case MappingNotify: return "MappingNotify";\r
567     default: return "UNKNOWN";\r
568     }\r
569 }\r
570 \r
571 static const char* fghBoolToString( Bool b )\r
572 {\r
573     return b == False ? "False" : "True";\r
574 }\r
575 \r
576 static const char* fghNotifyHintToString( char is_hint )\r
577 {\r
578     switch( is_hint ) {\r
579     case NotifyNormal: return "NotifyNormal";\r
580     case NotifyHint: return "NotifyHint";\r
581     default: return "UNKNOWN";\r
582     }\r
583 }\r
584 \r
585 static const char* fghNotifyModeToString( int mode )\r
586 {\r
587     switch( mode ) {\r
588     case NotifyNormal: return "NotifyNormal";\r
589     case NotifyGrab: return "NotifyGrab";\r
590     case NotifyUngrab: return "NotifyUngrab";\r
591     case NotifyWhileGrabbed: return "NotifyWhileGrabbed";\r
592     default: return "UNKNOWN";\r
593     }\r
594 }\r
595 \r
596 static const char* fghNotifyDetailToString( int detail )\r
597 {\r
598     switch( detail ) {\r
599     case NotifyAncestor: return "NotifyAncestor";\r
600     case NotifyVirtual: return "NotifyVirtual";\r
601     case NotifyInferior: return "NotifyInferior";\r
602     case NotifyNonlinear: return "NotifyNonlinear";\r
603     case NotifyNonlinearVirtual: return "NotifyNonlinearVirtual";\r
604     case NotifyPointer: return "NotifyPointer";\r
605     case NotifyPointerRoot: return "NotifyPointerRoot";\r
606     case NotifyDetailNone: return "NotifyDetailNone";\r
607     default: return "UNKNOWN";\r
608     }\r
609 }\r
610 \r
611 static const char* fghVisibilityToString( int state ) {\r
612     switch( state ) {\r
613     case VisibilityUnobscured: return "VisibilityUnobscured";\r
614     case VisibilityPartiallyObscured: return "VisibilityPartiallyObscured";\r
615     case VisibilityFullyObscured: return "VisibilityFullyObscured";\r
616     default: return "UNKNOWN";\r
617     }\r
618 }\r
619 \r
620 static const char* fghConfigureDetailToString( int detail )\r
621 {\r
622     switch( detail ) {\r
623     case Above: return "Above";\r
624     case Below: return "Below";\r
625     case TopIf: return "TopIf";\r
626     case BottomIf: return "BottomIf";\r
627     case Opposite: return "Opposite";\r
628     default: return "UNKNOWN";\r
629     }\r
630 }\r
631 \r
632 static const char* fghPlaceToString( int place )\r
633 {\r
634     switch( place ) {\r
635     case PlaceOnTop: return "PlaceOnTop";\r
636     case PlaceOnBottom: return "PlaceOnBottom";\r
637     default: return "UNKNOWN";\r
638     }\r
639 }\r
640 \r
641 static const char* fghMappingRequestToString( int request )\r
642 {\r
643     switch( request ) {\r
644     case MappingModifier: return "MappingModifier";\r
645     case MappingKeyboard: return "MappingKeyboard";\r
646     case MappingPointer: return "MappingPointer";\r
647     default: return "UNKNOWN";\r
648     }\r
649 }\r
650 \r
651 static const char* fghPropertyStateToString( int state )\r
652 {\r
653     switch( state ) {\r
654     case PropertyNewValue: return "PropertyNewValue";\r
655     case PropertyDelete: return "PropertyDelete";\r
656     default: return "UNKNOWN";\r
657     }\r
658 }\r
659 \r
660 static const char* fghColormapStateToString( int state )\r
661 {\r
662     switch( state ) {\r
663     case ColormapUninstalled: return "ColormapUninstalled";\r
664     case ColormapInstalled: return "ColormapInstalled";\r
665     default: return "UNKNOWN";\r
666     }\r
667 }\r
668 \r
669 static void fghPrintEvent( XEvent *event )\r
670 {\r
671     switch( event->type ) {\r
672 \r
673     case KeyPress:\r
674     case KeyRelease: {\r
675         XKeyEvent *e = &event->xkey;\r
676         fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "\r
677                    "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "\r
678                    "keycode=%u, same_screen=%s", fghTypeToString( e->type ),\r
679                    e->window, e->root, e->subwindow, (unsigned long)e->time,\r
680                    e->x, e->y, e->x_root, e->y_root, e->state, e->keycode,\r
681                    fghBoolToString( e->same_screen ) );\r
682         break;\r
683     }\r
684 \r
685     case ButtonPress:\r
686     case ButtonRelease: {\r
687         XButtonEvent *e = &event->xbutton;\r
688         fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "\r
689                    "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "\r
690                    "button=%u, same_screen=%d", fghTypeToString( e->type ),\r
691                    e->window, e->root, e->subwindow, (unsigned long)e->time,\r
692                    e->x, e->y, e->x_root, e->y_root, e->state, e->button,\r
693                    fghBoolToString( e->same_screen ) );\r
694         break;\r
695     }\r
696 \r
697     case MotionNotify: {\r
698         XMotionEvent *e = &event->xmotion;\r
699         fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "\r
700                    "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "\r
701                    "is_hint=%s, same_screen=%d", fghTypeToString( e->type ),\r
702                    e->window, e->root, e->subwindow, (unsigned long)e->time,\r
703                    e->x, e->y, e->x_root, e->y_root, e->state,\r
704                    fghNotifyHintToString( e->is_hint ),\r
705                    fghBoolToString( e->same_screen ) );\r
706         break;\r
707     }\r
708 \r
709     case EnterNotify:\r
710     case LeaveNotify: {\r
711         XCrossingEvent *e = &event->xcrossing;\r
712         fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "\r
713                    "(x,y)=(%d,%d), mode=%s, detail=%s, same_screen=%d, "\r
714                    "focus=%d, state=0x%x", fghTypeToString( e->type ),\r
715                    e->window, e->root, e->subwindow, (unsigned long)e->time,\r
716                    e->x, e->y, fghNotifyModeToString( e->mode ),\r
717                    fghNotifyDetailToString( e->detail ), (int)e->same_screen,\r
718                    (int)e->focus, e->state );\r
719         break;\r
720     }\r
721 \r
722     case FocusIn:\r
723     case FocusOut: {\r
724         XFocusChangeEvent *e = &event->xfocus;\r
725         fgWarning( "%s: window=0x%x, mode=%s, detail=%s",\r
726                    fghTypeToString( e->type ), e->window,\r
727                    fghNotifyModeToString( e->mode ),\r
728                    fghNotifyDetailToString( e->detail ) );\r
729         break;\r
730     }\r
731 \r
732     case KeymapNotify: {\r
733         XKeymapEvent *e = &event->xkeymap;\r
734         char buf[32 * 2 + 1];\r
735         int i;\r
736         for ( i = 0; i < 32; i++ ) {\r
737             snprintf( &buf[ i * 2 ], sizeof( buf ) - i * 2,\r
738                       "%02x", e->key_vector[ i ] );\r
739         }\r
740         buf[ i ] = '\0';\r
741         fgWarning( "%s: window=0x%x, %s", fghTypeToString( e->type ), e->window,\r
742                    buf );\r
743         break;\r
744     }\r
745 \r
746     case Expose: {\r
747         XExposeEvent *e = &event->xexpose;\r
748         fgWarning( "%s: window=0x%x, (x,y)=(%d,%d), (width,height)=(%d,%d), "\r
749                    "count=%d", fghTypeToString( e->type ), e->window, e->x,\r
750                    e->y, e->width, e->height, e->count );\r
751         break;\r
752     }\r
753 \r
754     case GraphicsExpose: {\r
755         XGraphicsExposeEvent *e = &event->xgraphicsexpose;\r
756         fgWarning( "%s: drawable=0x%x, (x,y)=(%d,%d), (width,height)=(%d,%d), "\r
757                    "count=%d, (major_code,minor_code)=(%d,%d)",\r
758                    fghTypeToString( e->type ), e->drawable, e->x, e->y,\r
759                    e->width, e->height, e->count, e->major_code,\r
760                    e->minor_code );\r
761         break;\r
762     }\r
763 \r
764     case NoExpose: {\r
765         XNoExposeEvent *e = &event->xnoexpose;\r
766         fgWarning( "%s: drawable=0x%x, (major_code,minor_code)=(%d,%d)",\r
767                    fghTypeToString( e->type ), e->drawable, e->major_code,\r
768                    e->minor_code );\r
769         break;\r
770     }\r
771 \r
772     case VisibilityNotify: {\r
773         XVisibilityEvent *e = &event->xvisibility;\r
774         fgWarning( "%s: window=0x%x, state=%s", fghTypeToString( e->type ),\r
775                    e->window, fghVisibilityToString( e->state) );\r
776         break;\r
777     }\r
778 \r
779     case CreateNotify: {\r
780         XCreateWindowEvent *e = &event->xcreatewindow;\r
781         fgWarning( "%s: (x,y)=(%d,%d), (width,height)=(%d,%d), border_width=%d, "\r
782                    "window=0x%x, override_redirect=%s",\r
783                    fghTypeToString( e->type ), e->x, e->y, e->width, e->height,\r
784                    e->border_width, e->window,\r
785                    fghBoolToString( e->override_redirect ) );\r
786         break;\r
787     }\r
788 \r
789     case DestroyNotify: {\r
790         XDestroyWindowEvent *e = &event->xdestroywindow;\r
791         fgWarning( "%s: event=0x%x, window=0x%x",\r
792                    fghTypeToString( e->type ), e->event, e->window );\r
793         break;\r
794     }\r
795 \r
796     case UnmapNotify: {\r
797         XUnmapEvent *e = &event->xunmap;\r
798         fgWarning( "%s: event=0x%x, window=0x%x, from_configure=%s",\r
799                    fghTypeToString( e->type ), e->event, e->window,\r
800                    fghBoolToString( e->from_configure ) );\r
801         break;\r
802     }\r
803 \r
804     case MapNotify: {\r
805         XMapEvent *e = &event->xmap;\r
806         fgWarning( "%s: event=0x%x, window=0x%x, override_redirect=%s",\r
807                    fghTypeToString( e->type ), e->event, e->window,\r
808                    fghBoolToString( e->override_redirect ) );\r
809         break;\r
810     }\r
811 \r
812     case MapRequest: {\r
813         XMapRequestEvent *e = &event->xmaprequest;\r
814         fgWarning( "%s: parent=0x%x, window=0x%x",\r
815                    fghTypeToString( event->type ), e->parent, e->window );\r
816         break;\r
817     }\r
818 \r
819     case ReparentNotify: {\r
820         XReparentEvent *e = &event->xreparent;\r
821         fgWarning( "%s: event=0x%x, window=0x%x, parent=0x%x, (x,y)=(%d,%d), "\r
822                    "override_redirect=%s", fghTypeToString( e->type ),\r
823                    e->event, e->window, e->parent, e->x, e->y,\r
824                    fghBoolToString( e->override_redirect ) );\r
825         break;\r
826     }\r
827 \r
828     case ConfigureNotify: {\r
829         XConfigureEvent *e = &event->xconfigure;\r
830         fgWarning( "%s: event=0x%x, window=0x%x, (x,y)=(%d,%d), "\r
831                    "(width,height)=(%d,%d), border_width=%d, above=0x%x, "\r
832                    "override_redirect=%s", fghTypeToString( e->type ), e->event,\r
833                    e->window, e->x, e->y, e->width, e->height, e->border_width,\r
834                    e->above, fghBoolToString( e->override_redirect ) );\r
835         break;\r
836     }\r
837 \r
838     case ConfigureRequest: {\r
839         XConfigureRequestEvent *e = &event->xconfigurerequest;\r
840         fgWarning( "%s: parent=0x%x, window=0x%x, (x,y)=(%d,%d), "\r
841                    "(width,height)=(%d,%d), border_width=%d, above=0x%x, "\r
842                    "detail=%s, value_mask=%lx", fghTypeToString( e->type ),\r
843                    e->parent, e->window, e->x, e->y, e->width, e->height,\r
844                    e->border_width, e->above,\r
845                    fghConfigureDetailToString( e->detail ), e->value_mask );\r
846         break;\r
847     }\r
848 \r
849     case GravityNotify: {\r
850         XGravityEvent *e = &event->xgravity;\r
851         fgWarning( "%s: event=0x%x, window=0x%x, (x,y)=(%d,%d)",\r
852                    fghTypeToString( e->type ), e->event, e->window, e->x, e->y );\r
853         break;\r
854     }\r
855 \r
856     case ResizeRequest: {\r
857         XResizeRequestEvent *e = &event->xresizerequest;\r
858         fgWarning( "%s: window=0x%x, (width,height)=(%d,%d)",\r
859                    fghTypeToString( e->type ), e->window, e->width, e->height );\r
860         break;\r
861     }\r
862 \r
863     case CirculateNotify: {\r
864         XCirculateEvent *e = &event->xcirculate;\r
865         fgWarning( "%s: event=0x%x, window=0x%x, place=%s",\r
866                    fghTypeToString( e->type ), e->event, e->window,\r
867                    fghPlaceToString( e->place ) );\r
868         break;\r
869     }\r
870 \r
871     case CirculateRequest: {\r
872         XCirculateRequestEvent *e = &event->xcirculaterequest;\r
873         fgWarning( "%s: parent=0x%x, window=0x%x, place=%s",\r
874                    fghTypeToString( e->type ), e->parent, e->window,\r
875                    fghPlaceToString( e->place ) );\r
876         break;\r
877     }\r
878 \r
879     case PropertyNotify: {\r
880         XPropertyEvent *e = &event->xproperty;\r
881         fgWarning( "%s: window=0x%x, atom=%lu, time=%lu, state=%s",\r
882                    fghTypeToString( e->type ), e->window,\r
883                    (unsigned long)e->atom, (unsigned long)e->time,\r
884                    fghPropertyStateToString( e->state ) );\r
885         break;\r
886     }\r
887 \r
888     case SelectionClear: {\r
889         XSelectionClearEvent *e = &event->xselectionclear;\r
890         fgWarning( "%s: window=0x%x, selection=%lu, time=%lu",\r
891                    fghTypeToString( e->type ), e->window,\r
892                    (unsigned long)e->selection, (unsigned long)e->time );\r
893         break;\r
894     }\r
895 \r
896     case SelectionRequest: {\r
897         XSelectionRequestEvent *e = &event->xselectionrequest;\r
898         fgWarning( "%s: owner=0x%x, requestor=0x%x, selection=0x%x, "\r
899                    "target=0x%x, property=%lu, time=%lu",\r
900                    fghTypeToString( e->type ), e->owner, e->requestor,\r
901                    (unsigned long)e->selection, (unsigned long)e->target,\r
902                    (unsigned long)e->property, (unsigned long)e->time );\r
903         break;\r
904     }\r
905 \r
906     case SelectionNotify: {\r
907         XSelectionEvent *e = &event->xselection;\r
908         fgWarning( "%s: requestor=0x%x, selection=0x%x, target=0x%x, "\r
909                    "property=%lu, time=%lu", fghTypeToString( e->type ),\r
910                    e->requestor, (unsigned long)e->selection,\r
911                    (unsigned long)e->target, (unsigned long)e->property,\r
912                    (unsigned long)e->time );\r
913         break;\r
914     }\r
915 \r
916     case ColormapNotify: {\r
917         XColormapEvent *e = &event->xcolormap;\r
918         fgWarning( "%s: window=0x%x, colormap=%lu, new=%s, state=%s",\r
919                    fghTypeToString( e->type ), e->window,\r
920                    (unsigned long)e->colormap, fghBoolToString( e->new ),\r
921                    fghColormapStateToString( e->state ) );\r
922         break;\r
923     }\r
924 \r
925     case ClientMessage: {\r
926         XClientMessageEvent *e = &event->xclient;\r
927         char buf[ 61 ];\r
928         char* p = buf;\r
929         char* end = buf + sizeof( buf );\r
930         int i;\r
931         switch( e->format ) {\r
932         case 8:\r
933           for ( i = 0; i < 20; i++, p += 3 ) {\r
934                 snprintf( p, end - p, " %02x", e->data.b[ i ] );\r
935             }\r
936             break;\r
937         case 16:\r
938             for ( i = 0; i < 10; i++, p += 5 ) {\r
939                 snprintf( p, end - p, " %04x", e->data.s[ i ] );\r
940             }\r
941             break;\r
942         case 32:\r
943             for ( i = 0; i < 5; i++, p += 9 ) {\r
944                 snprintf( p, end - p, " %08lx", e->data.l[ i ] );\r
945             }\r
946             break;\r
947         }\r
948         *p = '\0';\r
949         fgWarning( "%s: window=0x%x, message_type=%lu, format=%d, data=(%s )",\r
950                    fghTypeToString( e->type ), e->window,\r
951                    (unsigned long)e->message_type, e->format, buf );\r
952         break;\r
953     }\r
954 \r
955     case MappingNotify: {\r
956         XMappingEvent *e = &event->xmapping;\r
957         fgWarning( "%s: window=0x%x, request=%s, first_keycode=%d, count=%d",\r
958                    fghTypeToString( e->type ), e->window,\r
959                    fghMappingRequestToString( e->request ), e->first_keycode,\r
960                    e->count );\r
961         break;\r
962     }\r
963 \r
964     default: {\r
965         fgWarning( "%s", fghTypeToString( event->type ) );\r
966         break;\r
967     }\r
968     }\r
969 }\r
970 \r
971 #endif\r
972 \r
973 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */\r
974 \r
975 /*\r
976  * Executes a single iteration in the freeglut processing loop.\r
977  */\r
978 void FGAPIENTRY glutMainLoopEvent( void )\r
979 {\r
980 #if TARGET_HOST_POSIX_X11\r
981     SFG_Window* window;\r
982     XEvent event;\r
983 \r
984     /* This code was repeated constantly, so here it goes into a definition: */\r
985 #define GETWINDOW(a)                             \\r
986     window = fgWindowByHandle( event.a.window ); \\r
987     if( window == NULL )                         \\r
988         break;\r
989 \r
990 #define GETMOUSE(a)                              \\r
991     window->State.MouseX = event.a.x;            \\r
992     window->State.MouseY = event.a.y;\r
993 \r
994     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoopEvent" );\r
995 \r
996     while( XPending( fgDisplay.Display ) )\r
997     {\r
998         XNextEvent( fgDisplay.Display, &event );\r
999 #if _DEBUG\r
1000         fghPrintEvent( &event );\r
1001 #endif\r
1002 \r
1003         switch( event.type )\r
1004         {\r
1005         case ClientMessage:\r
1006             if(fgIsSpaceballXEvent(&event)) {\r
1007                 fgSpaceballHandleXEvent(&event);\r
1008                 break;\r
1009             }\r
1010             /* Destroy the window when the WM_DELETE_WINDOW message arrives */\r
1011             if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.DeleteWindow )\r
1012             {\r
1013                 GETWINDOW( xclient );\r
1014 \r
1015                 fgDestroyWindow ( window );\r
1016 \r
1017                 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )\r
1018                 {\r
1019                     fgDeinitialize( );\r
1020                     exit( 0 );\r
1021                 }\r
1022                 else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS )\r
1023                     fgState.ExecState = GLUT_EXEC_STATE_STOP;\r
1024 \r
1025                 return;\r
1026             }\r
1027             break;\r
1028 \r
1029             /*\r
1030              * CreateNotify causes a configure-event so that sub-windows are\r
1031              * handled compatibly with GLUT.  Otherwise, your sub-windows\r
1032              * (in freeglut only) will not get an initial reshape event,\r
1033              * which can break things.\r
1034              *\r
1035              * GLUT presumably does this because it generally tries to treat\r
1036              * sub-windows the same as windows.\r
1037              */\r
1038         case CreateNotify:\r
1039         case ConfigureNotify:\r
1040             {\r
1041                 int width, height;\r
1042                 if( event.type == CreateNotify ) {\r
1043                     GETWINDOW( xcreatewindow );\r
1044                     width = event.xcreatewindow.width;\r
1045                     height = event.xcreatewindow.height;\r
1046                 } else {\r
1047                     GETWINDOW( xconfigure );\r
1048                     width = event.xconfigure.width;\r
1049                     height = event.xconfigure.height;\r
1050                 }\r
1051 \r
1052                 if( ( width != window->State.OldWidth ) ||\r
1053                     ( height != window->State.OldHeight ) )\r
1054                 {\r
1055                     SFG_Window *current_window = fgStructure.CurrentWindow;\r
1056 \r
1057                     window->State.OldWidth = width;\r
1058                     window->State.OldHeight = height;\r
1059                     if( FETCH_WCB( *window, Reshape ) )\r
1060                         INVOKE_WCB( *window, Reshape, ( width, height ) );\r
1061                     else\r
1062                     {\r
1063                         fgSetWindow( window );\r
1064                         glViewport( 0, 0, width, height );\r
1065                     }\r
1066                     glutPostRedisplay( );\r
1067                     if( window->IsMenu )\r
1068                         fgSetWindow( current_window );\r
1069                 }\r
1070             }\r
1071             break;\r
1072 \r
1073         case DestroyNotify:\r
1074             /*\r
1075              * This is sent to confirm the XDestroyWindow call.\r
1076              *\r
1077              * XXX WHY is this commented out?  Should we re-enable it?\r
1078              */\r
1079             /* fgAddToWindowDestroyList ( window ); */\r
1080             break;\r
1081 \r
1082         case Expose:\r
1083             /*\r
1084              * We are too dumb to process partial exposes...\r
1085              *\r
1086              * XXX Well, we could do it.  However, it seems to only\r
1087              * XXX be potentially useful for single-buffered (since\r
1088              * XXX double-buffered does not respect viewport when we\r
1089              * XXX do a buffer-swap).\r
1090              *\r
1091              */\r
1092             if( event.xexpose.count == 0 )\r
1093             {\r
1094                 GETWINDOW( xexpose );\r
1095                 window->State.Redisplay = GL_TRUE;\r
1096             }\r
1097             break;\r
1098 \r
1099         case MapNotify:\r
1100             break;\r
1101 \r
1102         case UnmapNotify:\r
1103             /* We get this when iconifying a window. */ \r
1104             GETWINDOW( xunmap );\r
1105             INVOKE_WCB( *window, WindowStatus, ( GLUT_HIDDEN ) );\r
1106             window->State.Visible = GL_FALSE;\r
1107             break;\r
1108 \r
1109         case MappingNotify:\r
1110             /*\r
1111              * Have the client's keyboard knowledge updated (xlib.ps,\r
1112              * page 206, says that's a good thing to do)\r
1113              */\r
1114             XRefreshKeyboardMapping( (XMappingEvent *) &event );\r
1115             break;\r
1116 \r
1117         case VisibilityNotify:\r
1118         {\r
1119             /*\r
1120              * Sending this event, the X server can notify us that the window\r
1121              * has just acquired one of the three possible visibility states:\r
1122              * VisibilityUnobscured, VisibilityPartiallyObscured or\r
1123              * VisibilityFullyObscured. Note that we DO NOT receive a\r
1124              * VisibilityNotify event when iconifying a window, we only get an\r
1125              * UnmapNotify then.\r
1126              */\r
1127             GETWINDOW( xvisibility );\r
1128             switch( event.xvisibility.state )\r
1129             {\r
1130             case VisibilityUnobscured:\r
1131                 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_RETAINED ) );\r
1132                 window->State.Visible = GL_TRUE;\r
1133                 break;\r
1134 \r
1135             case VisibilityPartiallyObscured:\r
1136                 INVOKE_WCB( *window, WindowStatus,\r
1137                             ( GLUT_PARTIALLY_RETAINED ) );\r
1138                 window->State.Visible = GL_TRUE;\r
1139                 break;\r
1140 \r
1141             case VisibilityFullyObscured:\r
1142                 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_COVERED ) );\r
1143                 window->State.Visible = GL_FALSE;\r
1144                 break;\r
1145 \r
1146             default:\r
1147                 fgWarning( "Unknown X visibility state: %d",\r
1148                            event.xvisibility.state );\r
1149                 break;\r
1150             }\r
1151         }\r
1152         break;\r
1153 \r
1154         case EnterNotify:\r
1155         case LeaveNotify:\r
1156             GETWINDOW( xcrossing );\r
1157             GETMOUSE( xcrossing );\r
1158             if( ( event.type == LeaveNotify ) && window->IsMenu &&\r
1159                 window->ActiveMenu && window->ActiveMenu->IsActive )\r
1160                 fgUpdateMenuHighlight( window->ActiveMenu );\r
1161 \r
1162             INVOKE_WCB( *window, Entry, ( ( EnterNotify == event.type ) ?\r
1163                                           GLUT_ENTERED :\r
1164                                           GLUT_LEFT ) );\r
1165             break;\r
1166 \r
1167         case MotionNotify:\r
1168         {\r
1169             GETWINDOW( xmotion );\r
1170             GETMOUSE( xmotion );\r
1171 \r
1172             if( window->ActiveMenu )\r
1173             {\r
1174                 if( window == window->ActiveMenu->ParentWindow )\r
1175                 {\r
1176                     window->ActiveMenu->Window->State.MouseX =\r
1177                         event.xmotion.x_root - window->ActiveMenu->X;\r
1178                     window->ActiveMenu->Window->State.MouseY =\r
1179                         event.xmotion.y_root - window->ActiveMenu->Y;\r
1180                 }\r
1181 \r
1182                 fgUpdateMenuHighlight( window->ActiveMenu );\r
1183 \r
1184                 break;\r
1185             }\r
1186 \r
1187             /*\r
1188              * XXX For more than 5 buttons, just check {event.xmotion.state},\r
1189              * XXX rather than a host of bit-masks?  Or maybe we need to\r
1190              * XXX track ButtonPress/ButtonRelease events in our own\r
1191              * XXX bit-mask?\r
1192              */\r
1193             fgState.Modifiers = fghGetXModifiers( event.xmotion.state );\r
1194             if ( event.xmotion.state & ( Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask ) ) {\r
1195                 INVOKE_WCB( *window, Motion, ( event.xmotion.x,\r
1196                                                event.xmotion.y ) );\r
1197             } else {\r
1198                 INVOKE_WCB( *window, Passive, ( event.xmotion.x,\r
1199                                                 event.xmotion.y ) );\r
1200             }\r
1201             fgState.Modifiers = INVALID_MODIFIERS;\r
1202         }\r
1203         break;\r
1204 \r
1205         case ButtonRelease:\r
1206         case ButtonPress:\r
1207         {\r
1208             GLboolean pressed = GL_TRUE;\r
1209             int button;\r
1210 \r
1211             if( event.type == ButtonRelease )\r
1212                 pressed = GL_FALSE ;\r
1213 \r
1214             /*\r
1215              * A mouse button has been pressed or released. Traditionally,\r
1216              * break if the window was found within the freeglut structures.\r
1217              */\r
1218             GETWINDOW( xbutton );\r
1219             GETMOUSE( xbutton );\r
1220 \r
1221             /*\r
1222              * An X button (at least in XFree86) is numbered from 1.\r
1223              * A GLUT button is numbered from 0.\r
1224              * Old GLUT passed through buttons other than just the first\r
1225              * three, though it only gave symbolic names and official\r
1226              * support to the first three.\r
1227              */\r
1228             button = event.xbutton.button - 1;\r
1229 \r
1230             /*\r
1231              * Do not execute the application's mouse callback if a menu\r
1232              * is hooked to this button.  In that case an appropriate\r
1233              * private call should be generated.\r
1234              */\r
1235             if( fgCheckActiveMenu( window, button, pressed,\r
1236                                    event.xbutton.x_root, event.xbutton.y_root ) )\r
1237                 break;\r
1238 \r
1239             /*\r
1240              * Check if there is a mouse or mouse wheel callback hooked to the\r
1241              * window\r
1242              */\r
1243             if( ! FETCH_WCB( *window, Mouse ) &&\r
1244                 ! FETCH_WCB( *window, MouseWheel ) )\r
1245                 break;\r
1246 \r
1247             fgState.Modifiers = fghGetXModifiers( event.xbutton.state );\r
1248 \r
1249             /* Finally execute the mouse or mouse wheel callback */\r
1250             if( ( button < glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS ) ) || ( ! FETCH_WCB( *window, MouseWheel ) ) )\r
1251                 INVOKE_WCB( *window, Mouse, ( button,\r
1252                                               pressed ? GLUT_DOWN : GLUT_UP,\r
1253                                               event.xbutton.x,\r
1254                                               event.xbutton.y )\r
1255                 );\r
1256             else\r
1257             {\r
1258                 /*\r
1259                  * Map 4 and 5 to wheel zero; EVEN to +1, ODD to -1\r
1260                  *  "  6 and 7 "    "   one; ...\r
1261                  *\r
1262                  * XXX This *should* be behind some variables/macros,\r
1263                  * XXX since the order and numbering isn't certain\r
1264                  * XXX See XFree86 configuration docs (even back in the\r
1265                  * XXX 3.x days, and especially with 4.x).\r
1266                  *\r
1267                  * XXX Note that {button} has already been decremented\r
1268                  * XXX in mapping from X button numbering to GLUT.\r
1269                                  *\r
1270                                  * XXX Should add support for partial wheel turns as Windows does -- 5/27/11\r
1271                  */\r
1272                 int wheel_number = (button - glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS )) / 2;\r
1273                 int direction = -1;\r
1274                 if( button % 2 )\r
1275                     direction = 1;\r
1276 \r
1277                 if( pressed )\r
1278                     INVOKE_WCB( *window, MouseWheel, ( wheel_number,\r
1279                                                        direction,\r
1280                                                        event.xbutton.x,\r
1281                                                        event.xbutton.y )\r
1282                     );\r
1283             }\r
1284             fgState.Modifiers = INVALID_MODIFIERS;\r
1285         }\r
1286         break;\r
1287 \r
1288         case KeyRelease:\r
1289         case KeyPress:\r
1290         {\r
1291             FGCBKeyboard keyboard_cb;\r
1292             FGCBSpecial special_cb;\r
1293 \r
1294             GETWINDOW( xkey );\r
1295             GETMOUSE( xkey );\r
1296 \r
1297             /* Detect auto repeated keys, if configured globally or per-window */\r
1298 \r
1299             if ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE )\r
1300             {\r
1301                 if (event.type==KeyRelease)\r
1302                 {\r
1303                     /*\r
1304                      * Look at X11 keystate to detect repeat mode.\r
1305                      * While X11 says the key is actually held down, we'll ignore KeyRelease/KeyPress pairs.\r
1306                      */\r
1307 \r
1308                     char keys[32];\r
1309                     XQueryKeymap( fgDisplay.Display, keys ); /* Look at X11 keystate to detect repeat mode */\r
1310 \r
1311                     if ( event.xkey.keycode<256 )            /* XQueryKeymap is limited to 256 keycodes    */\r
1312                     {\r
1313                         if ( keys[event.xkey.keycode>>3] & (1<<(event.xkey.keycode%8)) )\r
1314                             window->State.KeyRepeating = GL_TRUE;\r
1315                         else\r
1316                             window->State.KeyRepeating = GL_FALSE;\r
1317                     }\r
1318                 }\r
1319             }\r
1320             else\r
1321                 window->State.KeyRepeating = GL_FALSE;\r
1322 \r
1323             /* Cease processing this event if it is auto repeated */\r
1324 \r
1325             if (window->State.KeyRepeating)\r
1326             {\r
1327                 if (event.type == KeyPress) window->State.KeyRepeating = GL_FALSE;\r
1328                 break;\r
1329             }\r
1330 \r
1331             if( event.type == KeyPress )\r
1332             {\r
1333                 keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, Keyboard ));\r
1334                 special_cb  = (FGCBSpecial) ( FETCH_WCB( *window, Special  ));\r
1335             }\r
1336             else\r
1337             {\r
1338                 keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, KeyboardUp ));\r
1339                 special_cb  = (FGCBSpecial) ( FETCH_WCB( *window, SpecialUp  ));\r
1340             }\r
1341 \r
1342             /* Is there a keyboard/special callback hooked for this window? */\r
1343             if( keyboard_cb || special_cb )\r
1344             {\r
1345                 XComposeStatus composeStatus;\r
1346                 char asciiCode[ 32 ];\r
1347                 KeySym keySym;\r
1348                 int len;\r
1349 \r
1350                 /* Check for the ASCII/KeySym codes associated with the event: */\r
1351                 len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode),\r
1352                                      &keySym, &composeStatus\r
1353                 );\r
1354 \r
1355                 /* GLUT API tells us to have two separate callbacks... */\r
1356                 if( len > 0 )\r
1357                 {\r
1358                     /* ...one for the ASCII translateable keypresses... */\r
1359                     if( keyboard_cb )\r
1360                     {\r
1361                         fgSetWindow( window );\r
1362                         fgState.Modifiers = fghGetXModifiers( event.xkey.state );\r
1363                         keyboard_cb( asciiCode[ 0 ],\r
1364                                      event.xkey.x, event.xkey.y\r
1365                         );\r
1366                         fgState.Modifiers = INVALID_MODIFIERS;\r
1367                     }\r
1368                 }\r
1369                 else\r
1370                 {\r
1371                     int special = -1;\r
1372 \r
1373                     /*\r
1374                      * ...and one for all the others, which need to be\r
1375                      * translated to GLUT_KEY_Xs...\r
1376                      */\r
1377                     switch( keySym )\r
1378                     {\r
1379                     case XK_F1:     special = GLUT_KEY_F1;     break;\r
1380                     case XK_F2:     special = GLUT_KEY_F2;     break;\r
1381                     case XK_F3:     special = GLUT_KEY_F3;     break;\r
1382                     case XK_F4:     special = GLUT_KEY_F4;     break;\r
1383                     case XK_F5:     special = GLUT_KEY_F5;     break;\r
1384                     case XK_F6:     special = GLUT_KEY_F6;     break;\r
1385                     case XK_F7:     special = GLUT_KEY_F7;     break;\r
1386                     case XK_F8:     special = GLUT_KEY_F8;     break;\r
1387                     case XK_F9:     special = GLUT_KEY_F9;     break;\r
1388                     case XK_F10:    special = GLUT_KEY_F10;    break;\r
1389                     case XK_F11:    special = GLUT_KEY_F11;    break;\r
1390                     case XK_F12:    special = GLUT_KEY_F12;    break;\r
1391 \r
1392                     case XK_KP_Left:\r
1393                     case XK_Left:   special = GLUT_KEY_LEFT;   break;\r
1394                     case XK_KP_Right:\r
1395                     case XK_Right:  special = GLUT_KEY_RIGHT;  break;\r
1396                     case XK_KP_Up:\r
1397                     case XK_Up:     special = GLUT_KEY_UP;     break;\r
1398                     case XK_KP_Down:\r
1399                     case XK_Down:   special = GLUT_KEY_DOWN;   break;\r
1400 \r
1401                     case XK_KP_Prior:\r
1402                     case XK_Prior:  special = GLUT_KEY_PAGE_UP; break;\r
1403                     case XK_KP_Next:\r
1404                     case XK_Next:   special = GLUT_KEY_PAGE_DOWN; break;\r
1405                     case XK_KP_Home:\r
1406                     case XK_Home:   special = GLUT_KEY_HOME;   break;\r
1407                     case XK_KP_End:\r
1408                     case XK_End:    special = GLUT_KEY_END;    break;\r
1409                     case XK_KP_Insert:\r
1410                     case XK_Insert: special = GLUT_KEY_INSERT; break;\r
1411 \r
1412                     case XK_Num_Lock :  special = GLUT_KEY_NUM_LOCK;  break;\r
1413                     case XK_KP_Begin :  special = GLUT_KEY_BEGIN;     break;\r
1414                     case XK_KP_Delete:  special = GLUT_KEY_DELETE;    break;\r
1415 \r
1416                     case XK_Shift_L:   special = GLUT_KEY_SHIFT_L;    break;\r
1417                     case XK_Shift_R:   special = GLUT_KEY_SHIFT_R;    break;\r
1418                     case XK_Control_L: special = GLUT_KEY_CTRL_L;     break;\r
1419                     case XK_Control_R: special = GLUT_KEY_CTRL_R;     break;\r
1420                     case XK_Alt_L:     special = GLUT_KEY_ALT_L;      break;\r
1421                     case XK_Alt_R:     special = GLUT_KEY_ALT_R;      break;\r
1422                     }\r
1423 \r
1424                     /*\r
1425                      * Execute the callback (if one has been specified),\r
1426                      * given that the special code seems to be valid...\r
1427                      */\r
1428                     if( special_cb && (special != -1) )\r
1429                     {\r
1430                         fgSetWindow( window );\r
1431                         fgState.Modifiers = fghGetXModifiers( event.xkey.state );\r
1432                         special_cb( special, event.xkey.x, event.xkey.y );\r
1433                         fgState.Modifiers = INVALID_MODIFIERS;\r
1434                     }\r
1435                 }\r
1436             }\r
1437         }\r
1438         break;\r
1439 \r
1440         case ReparentNotify:\r
1441             break; /* XXX Should disable this event */\r
1442 \r
1443         /* Not handled */\r
1444         case GravityNotify:\r
1445             break;\r
1446 \r
1447         default:\r
1448             /* enter handling of Extension Events here */\r
1449             #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H\r
1450                 fgHandleExtensionEvents( &event );\r
1451             #endif\r
1452             break;\r
1453         }\r
1454     }\r
1455 \r
1456 #elif TARGET_HOST_MS_WINDOWS\r
1457 \r
1458     MSG stMsg;\r
1459 \r
1460     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoopEvent" );\r
1461 \r
1462     while( PeekMessage( &stMsg, NULL, 0, 0, PM_NOREMOVE ) )\r
1463     {\r
1464         if( GetMessage( &stMsg, NULL, 0, 0 ) == 0 )\r
1465         {\r
1466             if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )\r
1467             {\r
1468                 fgDeinitialize( );\r
1469                 exit( 0 );\r
1470             }\r
1471             else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS )\r
1472                 fgState.ExecState = GLUT_EXEC_STATE_STOP;\r
1473 \r
1474             return;\r
1475         }\r
1476 \r
1477         TranslateMessage( &stMsg );\r
1478         DispatchMessage( &stMsg );\r
1479     }\r
1480 #endif\r
1481 \r
1482     if( fgState.Timers.First )\r
1483         fghCheckTimers( );\r
1484     fghCheckJoystickPolls( );\r
1485     fghDisplayAll( );\r
1486 \r
1487     fgCloseWindows( );\r
1488 }\r
1489 \r
1490 /*\r
1491  * Enters the freeglut processing loop.\r
1492  * Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".\r
1493  */\r
1494 void FGAPIENTRY glutMainLoop( void )\r
1495 {\r
1496     int action;\r
1497 \r
1498 #if TARGET_HOST_MS_WINDOWS\r
1499     SFG_Window *window = (SFG_Window *)fgStructure.Windows.First ;\r
1500 #endif\r
1501 \r
1502     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoop" );\r
1503 \r
1504 #if TARGET_HOST_MS_WINDOWS\r
1505     /*\r
1506      * Processing before the main loop:  If there is a window which is open and\r
1507      * which has a visibility callback, call it.  I know this is an ugly hack,\r
1508      * but I'm not sure what else to do about it.  Ideally we should leave\r
1509      * something uninitialized in the create window code and initialize it in\r
1510      * the main loop, and have that initialization create a "WM_ACTIVATE"\r
1511      * message.  Then we would put the visibility callback code in the\r
1512      * "case WM_ACTIVATE" block below.         - John Fay -- 10/24/02\r
1513      */\r
1514     while( window )\r
1515     {\r
1516         if ( FETCH_WCB( *window, Visibility ) )\r
1517         {\r
1518             SFG_Window *current_window = fgStructure.CurrentWindow ;\r
1519 \r
1520             INVOKE_WCB( *window, Visibility, ( window->State.Visible ) );\r
1521             fgSetWindow( current_window );\r
1522         }\r
1523 \r
1524         window = (SFG_Window *)window->Node.Next ;\r
1525     }\r
1526 #endif\r
1527 \r
1528     fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;\r
1529     while( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )\r
1530     {\r
1531         SFG_Window *window;\r
1532 \r
1533         glutMainLoopEvent( );\r
1534         /*\r
1535          * Step through the list of windows, seeing if there are any\r
1536          * that are not menus\r
1537          */\r
1538         for( window = ( SFG_Window * )fgStructure.Windows.First;\r
1539              window;\r
1540              window = ( SFG_Window * )window->Node.Next )\r
1541             if ( ! ( window->IsMenu ) )\r
1542                 break;\r
1543 \r
1544         if( ! window )\r
1545             fgState.ExecState = GLUT_EXEC_STATE_STOP;\r
1546         else\r
1547         {\r
1548             if( fgState.IdleCallback )\r
1549             {\r
1550                 if( fgStructure.CurrentWindow &&\r
1551                     fgStructure.CurrentWindow->IsMenu )\r
1552                     /* fail safe */\r
1553                     fgSetWindow( window );\r
1554                 fgState.IdleCallback( );\r
1555             }\r
1556 \r
1557             fghSleepForEvents( );\r
1558         }\r
1559     }\r
1560 \r
1561     /*\r
1562      * When this loop terminates, destroy the display, state and structure\r
1563      * of a freeglut session, so that another glutInit() call can happen\r
1564      *\r
1565      * Save the "ActionOnWindowClose" because "fgDeinitialize" resets it.\r
1566      */\r
1567     action = fgState.ActionOnWindowClose;\r
1568     fgDeinitialize( );\r
1569     if( action == GLUT_ACTION_EXIT )\r
1570         exit( 0 );\r
1571 }\r
1572 \r
1573 /*\r
1574  * Leaves the freeglut processing loop.\r
1575  */\r
1576 void FGAPIENTRY glutLeaveMainLoop( void )\r
1577 {\r
1578     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveMainLoop" );\r
1579     fgState.ExecState = GLUT_EXEC_STATE_STOP ;\r
1580 }\r
1581 \r
1582 \r
1583 #if TARGET_HOST_MS_WINDOWS\r
1584 /*\r
1585  * Determine a GLUT modifer mask based on MS-WINDOWS system info.\r
1586  */\r
1587 static int fghGetWin32Modifiers (void)\r
1588 {\r
1589     return\r
1590         ( ( ( GetKeyState( VK_LSHIFT   ) < 0 ) ||\r
1591             ( GetKeyState( VK_RSHIFT   ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |\r
1592         ( ( ( GetKeyState( VK_LCONTROL ) < 0 ) ||\r
1593             ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL  : 0 ) |\r
1594         ( ( ( GetKeyState( VK_LMENU    ) < 0 ) ||\r
1595             ( GetKeyState( VK_RMENU    ) < 0 )) ? GLUT_ACTIVE_ALT   : 0 );\r
1596 }\r
1597 \r
1598 /*\r
1599  * The window procedure for handling Win32 events\r
1600  */\r
1601 LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,\r
1602                                LPARAM lParam )\r
1603 {\r
1604     static unsigned char lControl = 0, rControl = 0, lShift = 0,\r
1605                          rShift = 0, lAlt = 0, rAlt = 0;\r
1606 \r
1607     SFG_Window* window;\r
1608     PAINTSTRUCT ps;\r
1609     LRESULT lRet = 1;\r
1610 \r
1611     FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Event Handler" ) ;\r
1612 \r
1613     window = fgWindowByHandle( hWnd );\r
1614 \r
1615     if ( ( window == NULL ) && ( uMsg != WM_CREATE ) )\r
1616       return DefWindowProc( hWnd, uMsg, wParam, lParam );\r
1617 \r
1618     /* printf ( "Window %3d message <%04x> %12d %12d\n", window?window->ID:0,\r
1619              uMsg, wParam, lParam ); */\r
1620 \r
1621     if ( window )\r
1622     {\r
1623       /* Checking for CTRL, ALT, and SHIFT key positions:  Key Down! */\r
1624       if ( !lControl && GetAsyncKeyState ( VK_LCONTROL ) )\r
1625       {\r
1626           INVOKE_WCB    ( *window, Special,\r
1627                         ( GLUT_KEY_CTRL_L, window->State.MouseX, window->State.MouseY )\r
1628                       );\r
1629 \r
1630           lControl = 1;\r
1631       }\r
1632 \r
1633       if ( !rControl && GetAsyncKeyState ( VK_RCONTROL ) )\r
1634       {\r
1635           INVOKE_WCB ( *window, Special,\r
1636                        ( GLUT_KEY_CTRL_R, window->State.MouseX, window->State.MouseY )\r
1637                      );\r
1638 \r
1639           rControl = 1;\r
1640       }\r
1641 \r
1642       if ( !lShift && GetAsyncKeyState ( VK_LSHIFT ) )\r
1643       {\r
1644           INVOKE_WCB ( *window, Special,\r
1645                        ( GLUT_KEY_SHIFT_L, window->State.MouseX, window->State.MouseY )\r
1646                      );\r
1647 \r
1648           lShift = 1;\r
1649       }\r
1650 \r
1651       if ( !rShift && GetAsyncKeyState ( VK_RSHIFT ) )\r
1652       {\r
1653           INVOKE_WCB ( *window, Special,\r
1654                        ( GLUT_KEY_SHIFT_R, window->State.MouseX, window->State.MouseY )\r
1655                      );\r
1656 \r
1657           rShift = 1;\r
1658       }\r
1659 \r
1660       if ( !lAlt && GetAsyncKeyState ( VK_LMENU ) )\r
1661       {\r
1662           INVOKE_WCB ( *window, Special,\r
1663                        ( GLUT_KEY_ALT_L, window->State.MouseX, window->State.MouseY )\r
1664                      );\r
1665 \r
1666           lAlt = 1;\r
1667       }\r
1668 \r
1669       if ( !rAlt && GetAsyncKeyState ( VK_RMENU ) )\r
1670       {\r
1671           INVOKE_WCB ( *window, Special,\r
1672                        ( GLUT_KEY_ALT_R, window->State.MouseX, window->State.MouseY )\r
1673                      );\r
1674 \r
1675           rAlt = 1;\r
1676       }\r
1677 \r
1678       /* Checking for CTRL, ALT, and SHIFT key positions:  Key Up! */\r
1679       if ( lControl && !GetAsyncKeyState ( VK_LCONTROL ) )\r
1680       {\r
1681           INVOKE_WCB ( *window, SpecialUp,\r
1682                        ( GLUT_KEY_CTRL_L, window->State.MouseX, window->State.MouseY )\r
1683                      );\r
1684 \r
1685           lControl = 0;\r
1686       }\r
1687 \r
1688       if ( rControl && !GetAsyncKeyState ( VK_RCONTROL ) )\r
1689       {\r
1690           INVOKE_WCB ( *window, SpecialUp,\r
1691                        ( GLUT_KEY_CTRL_R, window->State.MouseX, window->State.MouseY )\r
1692                      );\r
1693 \r
1694           rControl = 0;\r
1695       }\r
1696 \r
1697       if ( lShift && !GetAsyncKeyState ( VK_LSHIFT ) )\r
1698       {\r
1699           INVOKE_WCB ( *window, SpecialUp,\r
1700                        ( GLUT_KEY_SHIFT_L, window->State.MouseX, window->State.MouseY )\r
1701                      );\r
1702 \r
1703           lShift = 0;\r
1704       }\r
1705 \r
1706       if ( rShift && !GetAsyncKeyState ( VK_RSHIFT ) )\r
1707       {\r
1708           INVOKE_WCB ( *window, SpecialUp,\r
1709                        ( GLUT_KEY_SHIFT_R, window->State.MouseX, window->State.MouseY )\r
1710                      );\r
1711 \r
1712           rShift = 0;\r
1713       }\r
1714 \r
1715       if ( lAlt && !GetAsyncKeyState ( VK_LMENU ) )\r
1716       {\r
1717           INVOKE_WCB ( *window, SpecialUp,\r
1718                        ( GLUT_KEY_ALT_L, window->State.MouseX, window->State.MouseY )\r
1719                      );\r
1720 \r
1721           lAlt = 0;\r
1722       }\r
1723 \r
1724       if ( rAlt && !GetAsyncKeyState ( VK_RMENU ) )\r
1725       {\r
1726           INVOKE_WCB ( *window, SpecialUp,\r
1727                        ( GLUT_KEY_ALT_R, window->State.MouseX, window->State.MouseY )\r
1728                      );\r
1729 \r
1730           rAlt = 0;\r
1731       }\r
1732     }\r
1733 \r
1734     switch( uMsg )\r
1735     {\r
1736     case WM_CREATE:\r
1737         /* The window structure is passed as the creation structure parameter... */\r
1738         window = (SFG_Window *) (((LPCREATESTRUCT) lParam)->lpCreateParams);\r
1739         FREEGLUT_INTERNAL_ERROR_EXIT ( ( window != NULL ), "Cannot create window",\r
1740                                        "fgWindowProc" );\r
1741 \r
1742         window->Window.Handle = hWnd;\r
1743         window->Window.Device = GetDC( hWnd );\r
1744         if( window->IsMenu )\r
1745         {\r
1746             unsigned int current_DisplayMode = fgState.DisplayMode;\r
1747             fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;\r
1748 #if !defined(_WIN32_WCE)\r
1749             fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );\r
1750 #endif\r
1751             fgState.DisplayMode = current_DisplayMode;\r
1752 \r
1753             if( fgStructure.MenuContext )\r
1754                 wglMakeCurrent( window->Window.Device,\r
1755                                 fgStructure.MenuContext->MContext\r
1756                 );\r
1757             else\r
1758             {\r
1759                 fgStructure.MenuContext =\r
1760                     (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );\r
1761                 fgStructure.MenuContext->MContext =\r
1762                     wglCreateContext( window->Window.Device );\r
1763             }\r
1764 \r
1765             /* window->Window.Context = wglGetCurrentContext ();   */\r
1766             window->Window.Context = wglCreateContext( window->Window.Device );\r
1767         }\r
1768         else\r
1769         {\r
1770 #if !defined(_WIN32_WCE)\r
1771             fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );\r
1772 #endif\r
1773 \r
1774             if( ! fgState.UseCurrentContext )\r
1775                 window->Window.Context =\r
1776                     wglCreateContext( window->Window.Device );\r
1777             else\r
1778             {\r
1779                 window->Window.Context = wglGetCurrentContext( );\r
1780                 if( ! window->Window.Context )\r
1781                     window->Window.Context =\r
1782                         wglCreateContext( window->Window.Device );\r
1783             }\r
1784 \r
1785 #if !defined(_WIN32_WCE)\r
1786             fgNewWGLCreateContext( window );\r
1787 #endif\r
1788         }\r
1789 \r
1790         window->State.NeedToResize = GL_TRUE;\r
1791         /* if we used CW_USEDEFAULT (thats a negative value) for the size\r
1792          * of the window, query the window now for the size at which it\r
1793          * was created.\r
1794          */\r
1795         if( ( window->State.Width < 0 ) || ( window->State.Height < 0 ) )\r
1796         {\r
1797             SFG_Window *current_window = fgStructure.CurrentWindow;\r
1798 \r
1799             fgSetWindow( window );\r
1800             window->State.Width = glutGet( GLUT_WINDOW_WIDTH );\r
1801             window->State.Height = glutGet( GLUT_WINDOW_HEIGHT );\r
1802             fgSetWindow( current_window );\r
1803         }\r
1804 \r
1805         ReleaseDC( window->Window.Handle, window->Window.Device );\r
1806 \r
1807 #if defined(_WIN32_WCE)\r
1808         /* Take over button handling */\r
1809         {\r
1810             HINSTANCE dxDllLib=LoadLibrary(_T("gx.dll"));\r
1811             if (dxDllLib)\r
1812             {\r
1813                 GXGetDefaultKeys_=(GXGETDEFAULTKEYS)GetProcAddress(dxDllLib, _T("?GXGetDefaultKeys@@YA?AUGXKeyList@@H@Z"));\r
1814                 GXOpenInput_=(GXOPENINPUT)GetProcAddress(dxDllLib, _T("?GXOpenInput@@YAHXZ"));\r
1815             }\r
1816 \r
1817             if(GXOpenInput_)\r
1818                 (*GXOpenInput_)();\r
1819             if(GXGetDefaultKeys_)\r
1820                 gxKeyList = (*GXGetDefaultKeys_)(GX_LANDSCAPEKEYS);\r
1821         }\r
1822 \r
1823 #endif /* defined(_WIN32_WCE) */\r
1824         break;\r
1825 \r
1826     case WM_SIZE:\r
1827         /*\r
1828          * If the window is visible, then it is the user manually resizing it.\r
1829          * If it is not, then it is the system sending us a dummy resize with\r
1830          * zero dimensions on a "glutIconifyWindow" call.\r
1831          */\r
1832         if( window->State.Visible )\r
1833         {\r
1834             window->State.NeedToResize = GL_TRUE;\r
1835 #if defined(_WIN32_WCE)\r
1836             window->State.Width  = HIWORD(lParam);\r
1837             window->State.Height = LOWORD(lParam);\r
1838 #else\r
1839             window->State.Width  = LOWORD(lParam);\r
1840             window->State.Height = HIWORD(lParam);\r
1841 #endif /* defined(_WIN32_WCE) */\r
1842         }\r
1843 \r
1844         break;\r
1845 \r
1846     case WM_SETFOCUS:\r
1847 /*        printf("WM_SETFOCUS: %p\n", window ); */\r
1848         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
1849         INVOKE_WCB( *window, Entry, ( GLUT_ENTERED ) );\r
1850         break;\r
1851 \r
1852     case WM_KILLFOCUS:\r
1853 /*        printf("WM_KILLFOCUS: %p\n", window ); */\r
1854         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
1855         INVOKE_WCB( *window, Entry, ( GLUT_LEFT ) );\r
1856 \r
1857         if( window->IsMenu &&\r
1858             window->ActiveMenu && window->ActiveMenu->IsActive )\r
1859             fgUpdateMenuHighlight( window->ActiveMenu );\r
1860 \r
1861         break;\r
1862 \r
1863 #if 0\r
1864     case WM_ACTIVATE:\r
1865         if (LOWORD(wParam) != WA_INACTIVE)\r
1866         {\r
1867 /*            printf("WM_ACTIVATE: fgSetCursor( %p, %d)\n", window,\r
1868                    window->State.Cursor ); */\r
1869             fgSetCursor( window, window->State.Cursor );\r
1870         }\r
1871 \r
1872         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
1873         break;\r
1874 #endif\r
1875 \r
1876     case WM_SETCURSOR:\r
1877 /*      printf ( "Cursor event %x %x %x %x\n", window, window->State.Cursor, lParam, wParam ) ; */\r
1878         if( LOWORD( lParam ) == HTCLIENT )\r
1879             fgSetCursor ( window, window->State.Cursor ) ;\r
1880         else\r
1881             lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
1882         break;\r
1883 \r
1884     case WM_SHOWWINDOW:\r
1885         window->State.Visible = GL_TRUE;\r
1886         window->State.Redisplay = GL_TRUE;\r
1887         break;\r
1888 \r
1889     case WM_PAINT:\r
1890         /* Turn on the visibility in case it was turned off somehow */\r
1891         window->State.Visible = GL_TRUE;\r
1892         BeginPaint( hWnd, &ps );\r
1893         fghRedrawWindow( window );\r
1894         EndPaint( hWnd, &ps );\r
1895         break;\r
1896 \r
1897     case WM_CLOSE:\r
1898         fgDestroyWindow ( window );\r
1899         if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )\r
1900             PostQuitMessage(0);\r
1901         break;\r
1902 \r
1903     case WM_DESTROY:\r
1904         /*\r
1905          * The window already got destroyed, so don't bother with it.\r
1906          */\r
1907         return 0;\r
1908 \r
1909     case WM_MOUSEMOVE:\r
1910     {\r
1911 #if defined(_WIN32_WCE)\r
1912         window->State.MouseX = 320-HIWORD( lParam );\r
1913         window->State.MouseY = LOWORD( lParam );\r
1914 #else\r
1915         window->State.MouseX = LOWORD( lParam );\r
1916         window->State.MouseY = HIWORD( lParam );\r
1917 #endif /* defined(_WIN32_WCE) */\r
1918         /* Restrict to [-32768, 32767] to match X11 behaviour       */\r
1919         /* See comment in "freeglut_developer" mailing list 10/4/04 */\r
1920         if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;\r
1921         if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;\r
1922 \r
1923         if ( window->ActiveMenu )\r
1924         {\r
1925             fgUpdateMenuHighlight( window->ActiveMenu );\r
1926             break;\r
1927         }\r
1928         SetFocus(window->Window.Handle);\r
1929 \r
1930         fgState.Modifiers = fghGetWin32Modifiers( );\r
1931 \r
1932         if( ( wParam & MK_LBUTTON ) ||\r
1933             ( wParam & MK_MBUTTON ) ||\r
1934             ( wParam & MK_RBUTTON ) )\r
1935             INVOKE_WCB( *window, Motion, ( window->State.MouseX,\r
1936                                            window->State.MouseY ) );\r
1937         else\r
1938             INVOKE_WCB( *window, Passive, ( window->State.MouseX,\r
1939                                             window->State.MouseY ) );\r
1940 \r
1941         fgState.Modifiers = INVALID_MODIFIERS;\r
1942     }\r
1943     break;\r
1944 \r
1945     case WM_LBUTTONDOWN:\r
1946     case WM_MBUTTONDOWN:\r
1947     case WM_RBUTTONDOWN:\r
1948     case WM_LBUTTONUP:\r
1949     case WM_MBUTTONUP:\r
1950     case WM_RBUTTONUP:\r
1951     {\r
1952         GLboolean pressed = GL_TRUE;\r
1953         int button;\r
1954 \r
1955 #if defined(_WIN32_WCE)\r
1956         window->State.MouseX = 320-HIWORD( lParam );\r
1957         window->State.MouseY = LOWORD( lParam );\r
1958 #else\r
1959         window->State.MouseX = LOWORD( lParam );\r
1960         window->State.MouseY = HIWORD( lParam );\r
1961 #endif /* defined(_WIN32_WCE) */\r
1962 \r
1963         /* Restrict to [-32768, 32767] to match X11 behaviour       */\r
1964         /* See comment in "freeglut_developer" mailing list 10/4/04 */\r
1965         if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;\r
1966         if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;\r
1967 \r
1968         switch( uMsg )\r
1969         {\r
1970         case WM_LBUTTONDOWN:\r
1971             pressed = GL_TRUE;\r
1972             button = GLUT_LEFT_BUTTON;\r
1973             break;\r
1974         case WM_MBUTTONDOWN:\r
1975             pressed = GL_TRUE;\r
1976             button = GLUT_MIDDLE_BUTTON;\r
1977             break;\r
1978         case WM_RBUTTONDOWN:\r
1979             pressed = GL_TRUE;\r
1980             button = GLUT_RIGHT_BUTTON;\r
1981             break;\r
1982         case WM_LBUTTONUP:\r
1983             pressed = GL_FALSE;\r
1984             button = GLUT_LEFT_BUTTON;\r
1985             break;\r
1986         case WM_MBUTTONUP:\r
1987             pressed = GL_FALSE;\r
1988             button = GLUT_MIDDLE_BUTTON;\r
1989             break;\r
1990         case WM_RBUTTONUP:\r
1991             pressed = GL_FALSE;\r
1992             button = GLUT_RIGHT_BUTTON;\r
1993             break;\r
1994         default:\r
1995             pressed = GL_FALSE;\r
1996             button = -1;\r
1997             break;\r
1998         }\r
1999 \r
2000 #if !defined(_WIN32_WCE)\r
2001         if( GetSystemMetrics( SM_SWAPBUTTON ) )\r
2002         {\r
2003             if( button == GLUT_LEFT_BUTTON )\r
2004                 button = GLUT_RIGHT_BUTTON;\r
2005             else\r
2006                 if( button == GLUT_RIGHT_BUTTON )\r
2007                     button = GLUT_LEFT_BUTTON;\r
2008         }\r
2009 #endif /* !defined(_WIN32_WCE) */\r
2010 \r
2011         if( button == -1 )\r
2012             return DefWindowProc( hWnd, uMsg, lParam, wParam );\r
2013 \r
2014         /*\r
2015          * Do not execute the application's mouse callback if a menu\r
2016          * is hooked to this button.  In that case an appropriate\r
2017          * private call should be generated.\r
2018          */\r
2019         if( fgCheckActiveMenu( window, button, pressed,\r
2020                                window->State.MouseX, window->State.MouseY ) )\r
2021             break;\r
2022 \r
2023         /* Set capture so that the window captures all the mouse messages */\r
2024         /*\r
2025          * XXX - Multiple button support:  Under X11, the mouse is not released\r
2026          * XXX - from the window until all buttons have been released, even if the\r
2027          * XXX - user presses a button in another window.  This will take more\r
2028          * XXX - code changes than I am up to at the moment (10/5/04).  The present\r
2029          * XXX - is a 90 percent solution.\r
2030          */\r
2031         if ( pressed == GL_TRUE )\r
2032           SetCapture ( window->Window.Handle ) ;\r
2033         else\r
2034           ReleaseCapture () ;\r
2035 \r
2036         if( ! FETCH_WCB( *window, Mouse ) )\r
2037             break;\r
2038 \r
2039         fgSetWindow( window );\r
2040         fgState.Modifiers = fghGetWin32Modifiers( );\r
2041 \r
2042         INVOKE_WCB(\r
2043             *window, Mouse,\r
2044             ( button,\r
2045               pressed ? GLUT_DOWN : GLUT_UP,\r
2046               window->State.MouseX,\r
2047               window->State.MouseY\r
2048             )\r
2049         );\r
2050 \r
2051         fgState.Modifiers = INVALID_MODIFIERS;\r
2052     }\r
2053     break;\r
2054 \r
2055     case 0x020a:\r
2056         /* Should be WM_MOUSEWHEEL but my compiler doesn't recognize it */\r
2057     {\r
2058         int wheel_number = LOWORD( wParam );\r
2059         short ticks = ( short )HIWORD( wParam );\r
2060                 fgState.MouseWheelTicks += ticks;\r
2061 \r
2062         /*\r
2063          * XXX Should use WHEEL_DELTA instead of 120\r
2064          */\r
2065                 if ( abs ( fgState.MouseWheelTicks ) > 120 )\r
2066                 {\r
2067                         int direction = ( fgState.MouseWheelTicks > 0 ) ? 1 : -1;\r
2068 \r
2069             if( ! FETCH_WCB( *window, MouseWheel ) &&\r
2070                 ! FETCH_WCB( *window, Mouse ) )\r
2071                 break;\r
2072 \r
2073             fgSetWindow( window );\r
2074             fgState.Modifiers = fghGetWin32Modifiers( );\r
2075 \r
2076             /*\r
2077              * XXX Should use WHEEL_DELTA instead of 120\r
2078              */\r
2079             while( abs ( fgState.MouseWheelTicks ) > 120 )\r
2080                         {\r
2081                 if( FETCH_WCB( *window, MouseWheel ) )\r
2082                     INVOKE_WCB( *window, MouseWheel,\r
2083                                 ( wheel_number,\r
2084                                   direction,\r
2085                                   window->State.MouseX,\r
2086                                   window->State.MouseY\r
2087                                 )\r
2088                     );\r
2089                 else  /* No mouse wheel, call the mouse button callback twice */\r
2090                                 {\r
2091                     /*\r
2092                      * Map wheel zero to button 3 and 4; +1 to 3, -1 to 4\r
2093                      *  "    "   one                     +1 to 5, -1 to 6, ...\r
2094                      *\r
2095                      * XXX The below assumes that you have no more than 3 mouse\r
2096                      * XXX buttons.  Sorry.\r
2097                      */\r
2098                     int button = wheel_number * 2 + 3;\r
2099                     if( direction < 0 )\r
2100                         ++button;\r
2101                     INVOKE_WCB( *window, Mouse,\r
2102                                 ( button, GLUT_DOWN,\r
2103                                   window->State.MouseX, window->State.MouseY )\r
2104                     );\r
2105                     INVOKE_WCB( *window, Mouse,\r
2106                                 ( button, GLUT_UP,\r
2107                                   window->State.MouseX, window->State.MouseY )\r
2108                     );\r
2109                                 }\r
2110 \r
2111                 /*\r
2112                  * XXX Should use WHEEL_DELTA instead of 120\r
2113                  */\r
2114                                 fgState.MouseWheelTicks -= 120 * direction;\r
2115                         }\r
2116 \r
2117             fgState.Modifiers = INVALID_MODIFIERS;\r
2118                 }\r
2119     }\r
2120     break ;\r
2121 \r
2122     case WM_SYSKEYDOWN:\r
2123     case WM_KEYDOWN:\r
2124     {\r
2125         int keypress = -1;\r
2126         POINT mouse_pos ;\r
2127 \r
2128         if( ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE ) && (HIWORD(lParam) & KF_REPEAT) )\r
2129             break;\r
2130 \r
2131         /*\r
2132          * Remember the current modifiers state. This is done here in order\r
2133          * to make sure the VK_DELETE keyboard callback is executed properly.\r
2134          */\r
2135         fgState.Modifiers = fghGetWin32Modifiers( );\r
2136 \r
2137         GetCursorPos( &mouse_pos );\r
2138         ScreenToClient( window->Window.Handle, &mouse_pos );\r
2139 \r
2140         window->State.MouseX = mouse_pos.x;\r
2141         window->State.MouseY = mouse_pos.y;\r
2142 \r
2143         /* Convert the Win32 keystroke codes to GLUTtish way */\r
2144 #       define KEY(a,b) case a: keypress = b; break;\r
2145 \r
2146         switch( wParam )\r
2147         {\r
2148             KEY( VK_F1,     GLUT_KEY_F1        );\r
2149             KEY( VK_F2,     GLUT_KEY_F2        );\r
2150             KEY( VK_F3,     GLUT_KEY_F3        );\r
2151             KEY( VK_F4,     GLUT_KEY_F4        );\r
2152             KEY( VK_F5,     GLUT_KEY_F5        );\r
2153             KEY( VK_F6,     GLUT_KEY_F6        );\r
2154             KEY( VK_F7,     GLUT_KEY_F7        );\r
2155             KEY( VK_F8,     GLUT_KEY_F8        );\r
2156             KEY( VK_F9,     GLUT_KEY_F9        );\r
2157             KEY( VK_F10,    GLUT_KEY_F10       );\r
2158             KEY( VK_F11,    GLUT_KEY_F11       );\r
2159             KEY( VK_F12,    GLUT_KEY_F12       );\r
2160             KEY( VK_PRIOR,  GLUT_KEY_PAGE_UP   );\r
2161             KEY( VK_NEXT,   GLUT_KEY_PAGE_DOWN );\r
2162             KEY( VK_HOME,   GLUT_KEY_HOME      );\r
2163             KEY( VK_END,    GLUT_KEY_END       );\r
2164             KEY( VK_LEFT,   GLUT_KEY_LEFT      );\r
2165             KEY( VK_UP,     GLUT_KEY_UP        );\r
2166             KEY( VK_RIGHT,  GLUT_KEY_RIGHT     );\r
2167             KEY( VK_DOWN,   GLUT_KEY_DOWN      );\r
2168             KEY( VK_INSERT, GLUT_KEY_INSERT    );\r
2169             KEY( VK_LCONTROL, GLUT_KEY_CTRL_L  );\r
2170             KEY( VK_RCONTROL, GLUT_KEY_CTRL_R  );\r
2171             KEY( VK_LSHIFT, GLUT_KEY_SHIFT_L   );\r
2172             KEY( VK_RSHIFT, GLUT_KEY_SHIFT_R   );\r
2173             KEY( VK_LMENU,  GLUT_KEY_ALT_L     );\r
2174             KEY( VK_RMENU,  GLUT_KEY_ALT_R     );\r
2175 \r
2176         case VK_DELETE:\r
2177             /* The delete key should be treated as an ASCII keypress: */\r
2178             INVOKE_WCB( *window, Keyboard,\r
2179                         ( 127, window->State.MouseX, window->State.MouseY )\r
2180             );\r
2181         }\r
2182 \r
2183 #if defined(_WIN32_WCE)\r
2184         if(!(lParam & 0x40000000)) /* Prevent auto-repeat */\r
2185         {\r
2186             if(wParam==(unsigned)gxKeyList.vkRight)\r
2187                 keypress = GLUT_KEY_RIGHT;\r
2188             else if(wParam==(unsigned)gxKeyList.vkLeft)\r
2189                 keypress = GLUT_KEY_LEFT;\r
2190             else if(wParam==(unsigned)gxKeyList.vkUp)\r
2191                 keypress = GLUT_KEY_UP;\r
2192             else if(wParam==(unsigned)gxKeyList.vkDown)\r
2193                 keypress = GLUT_KEY_DOWN;\r
2194             else if(wParam==(unsigned)gxKeyList.vkA)\r
2195                 keypress = GLUT_KEY_F1;\r
2196             else if(wParam==(unsigned)gxKeyList.vkB)\r
2197                 keypress = GLUT_KEY_F2;\r
2198             else if(wParam==(unsigned)gxKeyList.vkC)\r
2199                 keypress = GLUT_KEY_F3;\r
2200             else if(wParam==(unsigned)gxKeyList.vkStart)\r
2201                 keypress = GLUT_KEY_F4;\r
2202         }\r
2203 #endif\r
2204 \r
2205         if( keypress != -1 )\r
2206             INVOKE_WCB( *window, Special,\r
2207                         ( keypress,\r
2208                           window->State.MouseX, window->State.MouseY )\r
2209             );\r
2210 \r
2211         fgState.Modifiers = INVALID_MODIFIERS;\r
2212     }\r
2213     break;\r
2214 \r
2215     case WM_SYSKEYUP:\r
2216     case WM_KEYUP:\r
2217     {\r
2218         int keypress = -1;\r
2219         POINT mouse_pos;\r
2220 \r
2221         /*\r
2222          * Remember the current modifiers state. This is done here in order\r
2223          * to make sure the VK_DELETE keyboard callback is executed properly.\r
2224          */\r
2225         fgState.Modifiers = fghGetWin32Modifiers( );\r
2226 \r
2227         GetCursorPos( &mouse_pos );\r
2228         ScreenToClient( window->Window.Handle, &mouse_pos );\r
2229 \r
2230         window->State.MouseX = mouse_pos.x;\r
2231         window->State.MouseY = mouse_pos.y;\r
2232 \r
2233         /*\r
2234          * Convert the Win32 keystroke codes to GLUTtish way.\r
2235          * "KEY(a,b)" was defined under "WM_KEYDOWN"\r
2236          */\r
2237 \r
2238         switch( wParam )\r
2239         {\r
2240             KEY( VK_F1,     GLUT_KEY_F1        );\r
2241             KEY( VK_F2,     GLUT_KEY_F2        );\r
2242             KEY( VK_F3,     GLUT_KEY_F3        );\r
2243             KEY( VK_F4,     GLUT_KEY_F4        );\r
2244             KEY( VK_F5,     GLUT_KEY_F5        );\r
2245             KEY( VK_F6,     GLUT_KEY_F6        );\r
2246             KEY( VK_F7,     GLUT_KEY_F7        );\r
2247             KEY( VK_F8,     GLUT_KEY_F8        );\r
2248             KEY( VK_F9,     GLUT_KEY_F9        );\r
2249             KEY( VK_F10,    GLUT_KEY_F10       );\r
2250             KEY( VK_F11,    GLUT_KEY_F11       );\r
2251             KEY( VK_F12,    GLUT_KEY_F12       );\r
2252             KEY( VK_PRIOR,  GLUT_KEY_PAGE_UP   );\r
2253             KEY( VK_NEXT,   GLUT_KEY_PAGE_DOWN );\r
2254             KEY( VK_HOME,   GLUT_KEY_HOME      );\r
2255             KEY( VK_END,    GLUT_KEY_END       );\r
2256             KEY( VK_LEFT,   GLUT_KEY_LEFT      );\r
2257             KEY( VK_UP,     GLUT_KEY_UP        );\r
2258             KEY( VK_RIGHT,  GLUT_KEY_RIGHT     );\r
2259             KEY( VK_DOWN,   GLUT_KEY_DOWN      );\r
2260             KEY( VK_INSERT, GLUT_KEY_INSERT    );\r
2261             KEY( VK_LCONTROL, GLUT_KEY_CTRL_L  );\r
2262             KEY( VK_RCONTROL, GLUT_KEY_CTRL_R  );\r
2263             KEY( VK_LSHIFT, GLUT_KEY_SHIFT_L   );\r
2264             KEY( VK_RSHIFT, GLUT_KEY_SHIFT_R   );\r
2265             KEY( VK_LMENU,  GLUT_KEY_ALT_L     );\r
2266             KEY( VK_RMENU,  GLUT_KEY_ALT_R     );\r
2267 \r
2268           case VK_DELETE:\r
2269               /* The delete key should be treated as an ASCII keypress: */\r
2270               INVOKE_WCB( *window, KeyboardUp,\r
2271                           ( 127, window->State.MouseX, window->State.MouseY )\r
2272               );\r
2273               break;\r
2274 \r
2275         default:\r
2276         {\r
2277 #if !defined(_WIN32_WCE)\r
2278             BYTE state[ 256 ];\r
2279             WORD code[ 2 ];\r
2280 \r
2281             GetKeyboardState( state );\r
2282 \r
2283             if( ToAscii( (UINT)wParam, 0, state, code, 0 ) == 1 )\r
2284                 wParam=code[ 0 ];\r
2285 \r
2286             INVOKE_WCB( *window, KeyboardUp,\r
2287                         ( (char)wParam,\r
2288                           window->State.MouseX, window->State.MouseY )\r
2289             );\r
2290 #endif /* !defined(_WIN32_WCE) */\r
2291         }\r
2292         }\r
2293 \r
2294         if( keypress != -1 )\r
2295             INVOKE_WCB( *window, SpecialUp,\r
2296                         ( keypress,\r
2297                           window->State.MouseX, window->State.MouseY )\r
2298             );\r
2299 \r
2300         fgState.Modifiers = INVALID_MODIFIERS;\r
2301     }\r
2302     break;\r
2303 \r
2304     case WM_SYSCHAR:\r
2305     case WM_CHAR:\r
2306     {\r
2307       if( (fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE) && (HIWORD(lParam) & KF_REPEAT) )\r
2308             break;\r
2309 \r
2310         fgState.Modifiers = fghGetWin32Modifiers( );\r
2311         INVOKE_WCB( *window, Keyboard,\r
2312                     ( (char)wParam,\r
2313                       window->State.MouseX, window->State.MouseY )\r
2314         );\r
2315         fgState.Modifiers = INVALID_MODIFIERS;\r
2316     }\r
2317     break;\r
2318 \r
2319     case WM_CAPTURECHANGED:\r
2320         /* User has finished resizing the window, force a redraw */\r
2321         INVOKE_WCB( *window, Display, ( ) );\r
2322 \r
2323         /*lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ); */\r
2324         break;\r
2325 \r
2326         /* Other messages that I have seen and which are not handled already */\r
2327     case WM_SETTEXT:  /* 0x000c */\r
2328         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
2329         /* Pass it on to "DefWindowProc" to set the window text */\r
2330         break;\r
2331 \r
2332     case WM_GETTEXT:  /* 0x000d */\r
2333         /* Ideally we would copy the title of the window into "lParam" */\r
2334         /* strncpy ( (char *)lParam, "Window Title", wParam );\r
2335            lRet = ( wParam > 12 ) ? 12 : wParam;  */\r
2336         /* the number of characters copied */\r
2337         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
2338         break;\r
2339 \r
2340     case WM_GETTEXTLENGTH:  /* 0x000e */\r
2341         /* Ideally we would get the length of the title of the window */\r
2342         lRet = 12;\r
2343         /* the number of characters in "Window Title\0" (see above) */\r
2344         break;\r
2345 \r
2346     case WM_ERASEBKGND:  /* 0x0014 */\r
2347         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
2348         break;\r
2349 \r
2350 #if !defined(_WIN32_WCE)\r
2351     case WM_SYNCPAINT:  /* 0x0088 */\r
2352         /* Another window has moved, need to update this one */\r
2353         window->State.Redisplay = GL_TRUE;\r
2354         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
2355         /* Help screen says this message must be passed to "DefWindowProc" */\r
2356         break;\r
2357 \r
2358     case WM_NCPAINT:  /* 0x0085 */\r
2359       /* Need to update the border of this window */\r
2360         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
2361         /* Pass it on to "DefWindowProc" to repaint a standard border */\r
2362         break;\r
2363 \r
2364     case WM_SYSCOMMAND :  /* 0x0112 */\r
2365         {\r
2366           /*\r
2367            * We have received a system command message.  Try to act on it.\r
2368            * The commands are passed in through the "wParam" parameter:\r
2369            * The least significant digit seems to be which edge of the window\r
2370            * is being used for a resize event:\r
2371            *     4  3  5\r
2372            *     1     2\r
2373            *     7  6  8\r
2374            * Congratulations and thanks to Richard Rauch for figuring this out..\r
2375            */\r
2376             switch ( wParam & 0xfff0 )\r
2377             {\r
2378             case SC_SIZE       :\r
2379                 break ;\r
2380 \r
2381             case SC_MOVE       :\r
2382                 break ;\r
2383 \r
2384             case SC_MINIMIZE   :\r
2385                 /* User has clicked on the "-" to minimize the window */\r
2386                 /* Turn off the visibility */\r
2387                 window->State.Visible = GL_FALSE ;\r
2388 \r
2389                 break ;\r
2390 \r
2391             case SC_MAXIMIZE   :\r
2392                 break ;\r
2393 \r
2394             case SC_NEXTWINDOW :\r
2395                 break ;\r
2396 \r
2397             case SC_PREVWINDOW :\r
2398                 break ;\r
2399 \r
2400             case SC_CLOSE      :\r
2401                 /* Followed very closely by a WM_CLOSE message */\r
2402                 break ;\r
2403 \r
2404             case SC_VSCROLL    :\r
2405                 break ;\r
2406 \r
2407             case SC_HSCROLL    :\r
2408                 break ;\r
2409 \r
2410             case SC_MOUSEMENU  :\r
2411                 break ;\r
2412 \r
2413             case SC_KEYMENU    :\r
2414                 break ;\r
2415 \r
2416             case SC_ARRANGE    :\r
2417                 break ;\r
2418 \r
2419             case SC_RESTORE    :\r
2420                 break ;\r
2421 \r
2422             case SC_TASKLIST   :\r
2423                 break ;\r
2424 \r
2425             case SC_SCREENSAVE :\r
2426                 break ;\r
2427 \r
2428             case SC_HOTKEY     :\r
2429                 break ;\r
2430 \r
2431 #if(WINVER >= 0x0400)\r
2432             case SC_DEFAULT    :\r
2433                 break ;\r
2434 \r
2435             case SC_MONITORPOWER    :\r
2436                 break ;\r
2437 \r
2438             case SC_CONTEXTHELP    :\r
2439                 break ;\r
2440 #endif /* WINVER >= 0x0400 */\r
2441 \r
2442             default:\r
2443 #if _DEBUG\r
2444                 fgWarning( "Unknown wParam type 0x%x", wParam );\r
2445 #endif\r
2446                 break;\r
2447             }\r
2448         }\r
2449 #endif /* !defined(_WIN32_WCE) */\r
2450 \r
2451         /* We need to pass the message on to the operating system as well */\r
2452         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
2453         break;\r
2454 \r
2455 #ifdef WM_TOUCH\r
2456         /* handle multi-touch messages */\r
2457         case WM_TOUCH:\r
2458         {\r
2459                 unsigned int numInputs = (unsigned int)wParam;\r
2460                 unsigned int i = 0;\r
2461                 TOUCHINPUT* ti = (TOUCHINPUT*)malloc( sizeof(TOUCHINPUT)*numInputs);\r
2462 \r
2463                 if (fghGetTouchInputInfo == (pGetTouchInputInfo)0xDEADBEEF) {\r
2464                     fghGetTouchInputInfo = (pGetTouchInputInfo)GetProcAddress(GetModuleHandle("user32"),"GetTouchInputInfo");\r
2465                     fghCloseTouchInputHandle = (pCloseTouchInputHandle)GetProcAddress(GetModuleHandle("user32"),"CloseTouchInputHandle");\r
2466                 }\r
2467 \r
2468                 if (!fghGetTouchInputInfo) { \r
2469                         free( (void*)ti );\r
2470                         break;\r
2471                 }\r
2472 \r
2473                 if (fghGetTouchInputInfo( (HTOUCHINPUT)lParam, numInputs, ti, sizeof(TOUCHINPUT) )) {\r
2474                         /* Handle each contact point */\r
2475                         for (i = 0; i < numInputs; ++i ) {\r
2476 \r
2477                                 POINT tp;\r
2478                                 tp.x = TOUCH_COORD_TO_PIXEL(ti[i].x);\r
2479                                 tp.y = TOUCH_COORD_TO_PIXEL(ti[i].y);\r
2480                                 ScreenToClient( hWnd, &tp );\r
2481 \r
2482                                 ti[i].dwID = ti[i].dwID * 2;\r
2483 \r
2484                                 if (ti[i].dwFlags & TOUCHEVENTF_DOWN) {\r
2485                                         INVOKE_WCB( *window, MultiEntry,  ( ti[i].dwID, GLUT_ENTERED ) );\r
2486                                         INVOKE_WCB( *window, MultiButton, ( ti[i].dwID, tp.x, tp.y, 0, GLUT_DOWN ) );\r
2487                                 } else if (ti[i].dwFlags & TOUCHEVENTF_MOVE) {\r
2488                                         INVOKE_WCB( *window, MultiMotion, ( ti[i].dwID, tp.x, tp.y ) );\r
2489                                 } else if (ti[i].dwFlags & TOUCHEVENTF_UP)   { \r
2490                                         INVOKE_WCB( *window, MultiButton, ( ti[i].dwID, tp.x, tp.y, 0, GLUT_UP ) );\r
2491                                         INVOKE_WCB( *window, MultiEntry,  ( ti[i].dwID, GLUT_LEFT ) );\r
2492                                 }\r
2493                         }\r
2494                 }\r
2495                 fghCloseTouchInputHandle((HTOUCHINPUT)lParam);\r
2496                 free( (void*)ti );\r
2497                 lRet = 0; /*DefWindowProc( hWnd, uMsg, wParam, lParam );*/\r
2498                 break;\r
2499         }\r
2500 #endif\r
2501     default:\r
2502         /* Handle unhandled messages */\r
2503         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
2504         break;\r
2505     }\r
2506 \r
2507     return lRet;\r
2508 }\r
2509 #endif\r
2510 \r
2511 /*** END OF FILE ***/\r