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