improved comments and mouse coord adjustments (John Fay)
[freeglut] / src / freeglut_main.c
1 /*
2  * freeglut_main.c
3  *
4  * The windows message processing methods.
5  *
6  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7  * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8  * Creation date: Fri Dec 3 1999
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <GL/freeglut.h>
34 #include "freeglut_internal.h"
35
36 #include <limits.h>
37 #if TARGET_HOST_UNIX_X11
38 #include <sys/types.h>
39 #include <sys/time.h>
40 #include <unistd.h>
41 #include <errno.h>
42 #include <sys/stat.h>
43 #elif TARGET_HOST_WIN32
44 #elif TARGET_HOST_WINCE
45
46 typedef struct GXDisplayProperties GXDisplayProperties;
47 typedef struct GXKeyList GXKeyList;
48 #include <gx.h>
49
50 typedef struct GXKeyList (*GXGETDEFAULTKEYS)(int);
51 typedef int (*GXOPENINPUT)();
52
53 GXGETDEFAULTKEYS GXGetDefaultKeys_ = NULL;
54 GXOPENINPUT GXOpenInput_ = NULL;
55
56 struct GXKeyList gxKeyList;
57
58 #endif
59
60 #ifndef MAX
61 #define MAX(a,b) (((a)>(b)) ? (a) : (b))
62 #endif
63
64 #ifndef MIN
65 #define MIN(a,b) (((a)<(b)) ? (a) : (b))
66 #endif
67
68
69 /*
70  * TODO BEFORE THE STABLE RELEASE:
71  *
72  * There are some issues concerning window redrawing under X11, and maybe
73  * some events are not handled. The Win32 version lacks some more features,
74  * but seems acceptable for not demanding purposes.
75  *
76  * Need to investigate why the X11 version breaks out with an error when
77  * closing a window (using the window manager, not glutDestroyWindow)...
78  */
79
80 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
81
82 /*
83  * Handle a window configuration change. When no reshape
84  * callback is hooked, the viewport size is updated to
85  * match the new window size.
86  */
87 static void fghReshapeWindow ( SFG_Window *window, int width, int height )
88 {
89     SFG_Window *current_window = fgStructure.Window;
90
91     freeglut_return_if_fail( window != NULL );
92
93
94 #if TARGET_HOST_UNIX_X11
95
96     XResizeWindow( fgDisplay.Display, window->Window.Handle,
97                    width, height );
98     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
99
100 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
101
102 #if !TARGET_HOST_WINCE
103     {
104         RECT winRect;
105         int x, y, w, h;
106
107         /*
108          * For windowed mode, get the current position of the
109          * window and resize taking the size of the frame
110          * decorations into account.
111          */
112
113         /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
114         GetWindowRect( window->Window.Handle, &winRect );
115         x = winRect.left;
116         y = winRect.top;
117         w = width;
118         h = height;
119
120         if ( window->Parent == NULL )
121         {
122             if ( ! window->IsMenu && !window->State.IsGameMode )
123             {
124                 w += GetSystemMetrics( SM_CXSIZEFRAME ) * 2;
125                 h += GetSystemMetrics( SM_CYSIZEFRAME ) * 2 +
126                      GetSystemMetrics( SM_CYCAPTION );
127             }
128         }
129         else
130         {
131             RECT parentRect;
132             GetWindowRect( window->Parent->Window.Handle, &parentRect );
133             x -= parentRect.left + GetSystemMetrics( SM_CXSIZEFRAME ) * 2;
134             y -= parentRect.top  + GetSystemMetrics( SM_CYSIZEFRAME ) * 2 +
135                                    GetSystemMetrics( SM_CYCAPTION );
136         }
137
138         /*
139          * SWP_NOACTIVATE      Do not activate the window
140          * SWP_NOOWNERZORDER   Do not change position in z-order
141          * SWP_NOSENDCHANGING  Supress WM_WINDOWPOSCHANGING message
142          * SWP_NOZORDER        Retains the current Z order (ignore 2nd param)
143          */
144
145         SetWindowPos( window->Window.Handle,
146                       HWND_TOP,
147                       x, y, w, h,
148                       SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
149                       SWP_NOZORDER
150         );
151     }
152 #endif /* TARGET_HOST_WINCE */
153
154     /*
155      * XXX Should update {window->State.OldWidth, window->State.OldHeight}
156      * XXX to keep in lockstep with UNIX_X11 code.
157      */
158     if( FETCH_WCB( *window, Reshape ) )
159         INVOKE_WCB( *window, Reshape, ( width, height ) );
160     else
161     {
162         fgSetWindow( window );
163         glViewport( 0, 0, width, height );
164     }
165
166 #endif
167
168     /*
169      * Force a window redraw.  In Windows at least this is only a partial
170      * solution:  if the window is increasing in size in either dimension,
171      * the already-drawn part does not get drawn again and things look funny.
172      * But without this we get this bad behaviour whenever we resize the
173      * window.
174      */
175     window->State.Redisplay = GL_TRUE;
176
177     if( window->IsMenu )
178         fgSetWindow( current_window );
179 }
180
181 /*
182  * Calls a window's redraw method. This is used when
183  * a redraw is forced by the incoming window messages.
184  */
185 static void fghRedrawWindow ( SFG_Window *window )
186 {
187     SFG_Window *current_window = fgStructure.Window;
188
189     freeglut_return_if_fail( window );
190     freeglut_return_if_fail( FETCH_WCB ( *window, Display ) );
191
192     window->State.Redisplay = GL_FALSE;
193
194     freeglut_return_if_fail( window->State.Visible );
195
196     fgSetWindow( window );
197
198     if( window->State.NeedToResize )
199     {
200         fghReshapeWindow(
201             window,
202             window->State.Width,
203             window->State.Height
204         );
205
206         window->State.NeedToResize = GL_FALSE;
207     }
208
209     INVOKE_WCB( *window, Display, ( ) );
210
211     fgSetWindow( current_window );
212 }
213
214 /*
215  * A static helper function to execute display callback for a window
216  */
217 static void fghcbDisplayWindow( SFG_Window *window,
218                                 SFG_Enumerator *enumerator )
219 {
220     if( window->State.Redisplay &&
221         window->State.Visible )
222     {
223         window->State.Redisplay = GL_FALSE;
224
225 #if TARGET_HOST_UNIX_X11
226         fghRedrawWindow ( window ) ;
227 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
228         RedrawWindow(
229             window->Window.Handle, NULL, NULL,
230             RDW_NOERASE | RDW_INTERNALPAINT | RDW_INVALIDATE | RDW_UPDATENOW
231         );
232 #endif
233     }
234
235     fgEnumSubWindows( window, fghcbDisplayWindow, enumerator );
236 }
237
238 /*
239  * Make all windows perform a display call
240  */
241 static void fghDisplayAll( void )
242 {
243     SFG_Enumerator enumerator;
244
245     enumerator.found = GL_FALSE;
246     enumerator.data  =  NULL;
247
248     fgEnumWindows( fghcbDisplayWindow, &enumerator );
249 }
250
251 /*
252  * Window enumerator callback to check for the joystick polling code
253  */
254 static void fghcbCheckJoystickPolls( SFG_Window *window,
255                                      SFG_Enumerator *enumerator )
256 {
257     long int checkTime = fgElapsedTime( );
258
259     if( window->State.JoystickLastPoll + window->State.JoystickPollRate <=
260         checkTime )
261     {
262 #if !TARGET_HOST_WINCE
263         fgJoystickPollWindow( window );
264 #endif /* !TARGET_HOST_WINCE */
265         window->State.JoystickLastPoll = checkTime;
266     }
267
268     fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );
269 }
270
271 /*
272  * Check all windows for joystick polling
273  */
274 static void fghCheckJoystickPolls( void )
275 {
276     SFG_Enumerator enumerator;
277
278     enumerator.found = GL_FALSE;
279     enumerator.data  =  NULL;
280
281     fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );
282 }
283
284 /*
285  * Check the global timers
286  */
287 static void fghCheckTimers( void )
288 {
289     long checkTime = fgElapsedTime( );
290
291     while( fgState.Timers.First )
292     {
293         SFG_Timer *timer = fgState.Timers.First;
294
295         if( timer->TriggerTime > checkTime )
296             break;
297
298         fgListRemove( &fgState.Timers, &timer->Node );
299         fgListAppend( &fgState.FreeTimers, &timer->Node );
300
301         timer->Callback( timer->ID );
302     }
303 }
304
305 /*
306  * Elapsed Time
307  */
308 long fgElapsedTime( void )
309 {
310     if ( fgState.Time.Set )
311     {
312 #if TARGET_HOST_UNIX_X11
313         struct timeval now;
314         long elapsed;
315
316         gettimeofday( &now, NULL );
317
318         elapsed = (now.tv_usec - fgState.Time.Value.tv_usec) / 1000;
319         elapsed += (now.tv_sec - fgState.Time.Value.tv_sec) * 1000;
320
321         return elapsed;
322 #elif TARGET_HOST_WIN32
323         return timeGetTime() - fgState.Time.Value;
324 #elif TARGET_HOST_WINCE
325         return GetTickCount() - fgState.Time.Value;
326 #endif
327     }
328     else
329     {
330 #if TARGET_HOST_UNIX_X11
331         gettimeofday( &fgState.Time.Value, NULL );
332 #elif TARGET_HOST_WIN32
333         fgState.Time.Value = timeGetTime ();
334 #elif TARGET_HOST_WINCE
335         fgState.Time.Value = GetTickCount();
336 #endif
337         fgState.Time.Set = GL_TRUE ;
338
339         return 0 ;
340     }
341 }
342
343 /*
344  * Error Messages.
345  */
346 void fgError( const char *fmt, ... )
347 {
348     va_list ap;
349
350     va_start( ap, fmt );
351
352     fprintf( stderr, "freeglut ");
353     if( fgState.ProgramName )
354         fprintf (stderr, "(%s): ", fgState.ProgramName);
355     vfprintf( stderr, fmt, ap );
356     fprintf( stderr, "\n" );
357
358     va_end( ap );
359
360     if ( fgState.Initialised )
361         fgDeinitialize ();
362
363     exit( 1 );
364 }
365
366 void fgWarning( const char *fmt, ... )
367 {
368     va_list ap;
369
370     va_start( ap, fmt );
371
372     fprintf( stderr, "freeglut ");
373     if( fgState.ProgramName )
374         fprintf( stderr, "(%s): ", fgState.ProgramName );
375     vfprintf( stderr, fmt, ap );
376     fprintf( stderr, "\n" );
377
378     va_end( ap );
379 }
380
381 /*
382  * Indicates whether Joystick events are being used by ANY window.
383  *
384  * The current mechanism is to walk all of the windows and ask if
385  * there is a joystick callback.  We have a short-circuit early
386  * return if we find any joystick handler registered.
387  *
388  * The real way to do this is to make use of the glutTimer() API
389  * to more cleanly re-implement the joystick API.  Then, this code
390  * and all other "joystick timer" code can be yanked.
391  *
392  */
393 static void fghCheckJoystickCallback( SFG_Window* w, SFG_Enumerator* e)
394 {
395     if( FETCH_WCB( *w, Joystick ) )
396     {
397         e->found = GL_TRUE;
398         e->data = w;
399     }
400     fgEnumSubWindows( w, fghCheckJoystickCallback, e );
401 }
402 static int fghHaveJoystick( void )
403 {
404     SFG_Enumerator enumerator;
405     enumerator.found = GL_FALSE;
406     enumerator.data = NULL;
407     fgEnumWindows( fghCheckJoystickCallback, &enumerator );
408     return !!enumerator.data;
409 }
410 static void fghHavePendingRedisplaysCallback( SFG_Window* w, SFG_Enumerator* e)
411 {
412     if( w->State.Redisplay )
413     {
414         e->found = GL_TRUE;
415         e->data = w;
416     }
417     fgEnumSubWindows( w, fghHavePendingRedisplaysCallback, e );
418 }
419 static int fghHavePendingRedisplays (void)
420 {
421     SFG_Enumerator enumerator;
422     enumerator.found = GL_FALSE;
423     enumerator.data = NULL;
424     fgEnumWindows( fghHavePendingRedisplaysCallback, &enumerator );
425     return !!enumerator.data;
426 }
427 /*
428  * Returns the number of GLUT ticks (milliseconds) till the next timer event.
429  */
430 static long fghNextTimer( void )
431 {
432     long ret = INT_MAX;
433     SFG_Timer *timer = fgState.Timers.First;
434
435     if( timer )
436         ret = timer->TriggerTime - fgElapsedTime();
437     if( ret < 0 )
438         ret = 0;
439
440     return ret;
441 }
442 /*
443  * Does the magic required to relinquish the CPU until something interesting
444  * happens.
445  */
446 static void fghSleepForEvents( void )
447 {
448     long msec;
449
450     if( fgState.IdleCallback || fghHavePendingRedisplays( ) )
451         return;
452
453     msec = fghNextTimer( );
454     if( fghHaveJoystick( ) )     /* XXX Use GLUT timers for joysticks... */
455         msec = MIN( msec, 10 );  /* XXX Dumb; forces granularity to .01sec */
456
457 #if TARGET_HOST_UNIX_X11
458     /*
459      * Possibly due to aggressive use of XFlush() and friends,
460      * it is possible to have our socket drained but still have
461      * unprocessed events.  (Or, this may just be normal with
462      * X, anyway?)  We do non-trivial processing of X events
463      * after the event-reading loop, in any case, so we
464      * need to allow that we may have an empty socket but non-
465      * empty event queue.
466      */
467     if( ! XPending( fgDisplay.Display ) )
468     {
469         fd_set fdset;
470         int err;
471         int socket;
472         struct timeval wait;
473
474         socket = ConnectionNumber( fgDisplay.Display );
475         FD_ZERO( &fdset );
476         FD_SET( socket, &fdset );
477         wait.tv_sec = msec / 1000;
478         wait.tv_usec = (msec % 1000) * 1000;
479         err = select( socket+1, &fdset, NULL, NULL, &wait );
480
481         if( -1 == err )
482             fgWarning ( "freeglut select() error: %d", errno );
483     }
484 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
485     MsgWaitForMultipleObjects( 0, NULL, FALSE, msec, QS_ALLEVENTS );
486 #endif
487 }
488
489 #if TARGET_HOST_UNIX_X11
490 /*
491  * Returns GLUT modifier mask for an XEvent.
492  */
493 static int fghGetXModifiers( XEvent *event )
494 {
495     int ret = 0;
496
497     if( event->xkey.state & ( ShiftMask | LockMask ) )
498         ret |= GLUT_ACTIVE_SHIFT;
499     if( event->xkey.state & ControlMask )
500         ret |= GLUT_ACTIVE_CTRL;
501     if( event->xkey.state & Mod1Mask )
502         ret |= GLUT_ACTIVE_ALT;
503
504     return ret;
505 }
506 #endif
507
508
509 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
510
511 /*
512  * Executes a single iteration in the freeglut processing loop.
513  */
514 void FGAPIENTRY glutMainLoopEvent( void )
515 {
516 #if TARGET_HOST_UNIX_X11
517     SFG_Window* window;
518     XEvent event;
519
520     /* This code was repeated constantly, so here it goes into a definition: */
521 #define GETWINDOW(a)                             \
522     window = fgWindowByHandle( event.a.window ); \
523     if( window == NULL )                         \
524         break;
525
526 #define GETMOUSE(a)                              \
527     window->State.MouseX = event.a.x;            \
528     window->State.MouseY = event.a.y;
529
530     freeglut_assert_ready;
531
532     while( XPending( fgDisplay.Display ) )
533     {
534         XNextEvent( fgDisplay.Display, &event );
535
536         switch( event.type )
537         {
538         case ClientMessage:
539             /* Destroy the window when the WM_DELETE_WINDOW message arrives */
540             if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.DeleteWindow )
541             {
542                 GETWINDOW( xclient );
543
544                 fgDestroyWindow ( window );
545
546                 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
547                 {
548                     fgDeinitialize( );
549                     exit( 0 );
550                 }
551                 else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS )
552                     fgState.ExecState = GLUT_EXEC_STATE_STOP;
553
554                 return;
555             }
556             break;
557
558             /*
559              * CreateNotify causes a configure-event so that sub-windows are
560              * handled compatibly with GLUT.  Otherwise, your sub-windows
561              * (in freeglut only) will not get an initial reshape event,
562              * which can break things.
563              *
564              * GLUT presumably does this because it generally tries to treat
565              * sub-windows the same as windows.
566              *
567              * XXX Technically, GETWINDOW( xconfigure ) and
568              * XXX {event.xconfigure} may not be legit ways to get at
569              * XXX data for CreateNotify events.  In practice, the data
570              * XXX is in a union which is laid out much the same either
571              * XXX way.  But if you want to split hairs, this isn't legit,
572              * XXX and we should instead duplicate some code.
573              */
574         case CreateNotify:
575         case ConfigureNotify:
576             GETWINDOW( xconfigure );
577             {
578                 int width = event.xconfigure.width;
579                 int height = event.xconfigure.height;
580
581                 if( ( width != window->State.OldWidth ) ||
582                     ( height != window->State.OldHeight ) )
583                 {
584                     window->State.OldWidth = width;
585                     window->State.OldHeight = height;
586                     if( FETCH_WCB( *window, Reshape ) )
587                         INVOKE_WCB( *window, Reshape, ( width, height ) );
588                     else
589                     {
590                         fgSetWindow( window );
591                         glViewport( 0, 0, width, height );
592                     }
593                     glutPostRedisplay( );
594                 }
595             }
596             break;
597
598         case DestroyNotify:
599             /*
600              * This is sent to confirm the XDestroyWindow call.
601              *
602              * XXX WHY is this commented out?  Should we re-enable it?
603              */
604             /* fgAddToWindowDestroyList ( window ); */
605             break;
606
607         case Expose:
608             /*
609              * We are too dumb to process partial exposes...
610              *
611              * XXX Well, we could do it.  However, it seems to only
612              * XXX be potentially useful for single-buffered (since
613              * XXX double-buffered does not respect viewport when we
614              * XXX do a buffer-swap).
615              *
616              */
617             if( event.xexpose.count == 0 )
618             {
619                 GETWINDOW( xexpose );
620                 fgSetWindow( window );
621                 glutPostRedisplay( );
622             }
623             break;
624
625         case MapNotify:
626         case UnmapNotify:
627             /*
628              * If we never do anything with this, can we just not ask to
629              * get these messages?
630              */
631             break;
632
633         case MappingNotify:
634             /*
635              * Have the client's keyboard knowledge updated (xlib.ps,
636              * page 206, says that's a good thing to do)
637              */
638             XRefreshKeyboardMapping( (XMappingEvent *) &event );
639             break;
640
641         case VisibilityNotify:
642         {
643             GETWINDOW( xvisibility );
644             /*
645              * XXX INVOKE_WCB() does this check for us.
646              */
647             if( ! FETCH_WCB( *window, WindowStatus ) )
648                 break;
649             fgSetWindow( window );
650
651             /*
652              * Sending this event, the X server can notify us that the window
653              * has just acquired one of the three possible visibility states:
654              * VisibilityUnobscured, VisibilityPartiallyObscured or
655              * VisibilityFullyObscured
656              */
657             switch( event.xvisibility.state )
658             {
659             case VisibilityUnobscured:
660                 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_RETAINED ) );
661                 window->State.Visible = GL_TRUE;
662                 break;
663
664             case VisibilityPartiallyObscured:
665                 INVOKE_WCB( *window, WindowStatus,
666                             ( GLUT_PARTIALLY_RETAINED ) );
667                 window->State.Visible = GL_TRUE;
668                 break;
669
670             case VisibilityFullyObscured:
671                 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_COVERED ) );
672                 window->State.Visible = GL_FALSE;
673                 break;
674
675             default:
676                 fgWarning( "Unknown X visibility state: %d",
677                            event.xvisibility.state );
678                 break;
679             }
680         }
681         break;
682
683         case EnterNotify:
684         case LeaveNotify:
685             GETWINDOW( xcrossing );
686             GETMOUSE( xcrossing );
687             INVOKE_WCB( *window, Entry, ( ( EnterNotify == event.type ) ?
688                                           GLUT_ENTERED :
689                                           GLUT_LEFT ) );
690             break;
691
692         case MotionNotify:
693         {
694             GETWINDOW( xmotion );
695             GETMOUSE( xmotion );
696
697             if( window->ActiveMenu )
698             {
699                 if( window == window->ActiveMenu->ParentWindow )
700                 {
701                     window->ActiveMenu->Window->State.MouseX =
702                         event.xmotion.x_root - window->ActiveMenu->X;
703                     window->ActiveMenu->Window->State.MouseY =
704                         event.xmotion.y_root - window->ActiveMenu->Y;
705                 }
706                 window->ActiveMenu->Window->State.Redisplay = GL_TRUE ;
707                 fgSetWindow( window->ActiveMenu->ParentWindow );
708
709                 break;
710             }
711
712             /*
713              * XXX For more than 5 buttons, just check {event.xmotion.state},
714              * XXX rather than a host of bit-masks?  Or maybe we need to
715              * XXX track ButtonPress/ButtonRelease events in our own
716              * XXX bit-mask?
717              */
718 #define BUTTON_MASK \
719   ( Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask )
720             if ( event.xmotion.state & BUTTON_MASK )
721                 INVOKE_WCB( *window, Motion, ( event.xmotion.x,
722                                                event.xmotion.y ) );
723             else
724                 INVOKE_WCB( *window, Passive, ( event.xmotion.x,
725                                                 event.xmotion.y ) );
726         }
727         break;
728
729         case ButtonRelease:
730         case ButtonPress:
731         {
732             GLboolean pressed = GL_TRUE;
733             int button;
734
735             if( event.type == ButtonRelease )
736                 pressed = GL_FALSE ;
737
738             /*
739              * A mouse button has been pressed or released. Traditionally,
740              * break if the window was found within the freeglut structures.
741              */
742             GETWINDOW( xbutton );
743             GETMOUSE( xbutton );
744
745             /*
746              * An X button (at least in XFree86) is numbered from 1.
747              * A GLUT button is numbered from 0.
748              * Old GLUT passed through buttons other than just the first
749              * three, though it only gave symbolic names and official
750              * support to the first three.
751              */
752             button = event.xbutton.button - 1;
753
754             /*
755              * XXX This comment is replicated in the WIN32 section and
756              * XXX maybe also in the menu code.  Can we move the info
757              * XXX to one central place and *reference* it from here?
758              *
759              * Do not execute the application's mouse callback if a menu
760              * is hooked to this button.  In that case an appropriate
761              * private call should be generated.
762              * Near as I can tell, this is the menu behaviour:
763              *  - Down-click the menu button, menu not active:  activate
764              *    the menu with its upper left-hand corner at the mouse
765              *    location.
766              *  - Down-click any button outside the menu, menu active:
767              *    deactivate the menu
768              *  - Down-click any button inside the menu, menu active:
769              *    select the menu entry and deactivate the menu
770              *  - Up-click the menu button, menu not active:  nothing happens
771              *  - Up-click the menu button outside the menu, menu active:
772              *    nothing happens
773              *  - Up-click the menu button inside the menu, menu active:
774              *    select the menu entry and deactivate the menu
775              */
776             /* Window has an active menu, it absorbs any mouse click */
777             if( window->ActiveMenu )
778             {
779                 if( window == window->ActiveMenu->ParentWindow )
780                 {
781                     window->ActiveMenu->Window->State.MouseX =
782                         event.xbutton.x_root - window->ActiveMenu->X;
783                     window->ActiveMenu->Window->State.MouseY =
784                         event.xbutton.y_root - window->ActiveMenu->Y;
785                 }
786
787                 /* In the menu, invoke the callback and deactivate the menu*/
788                 if( fgCheckActiveMenu( window->ActiveMenu->Window,
789                                        window->ActiveMenu ) )
790                 {
791                     /*
792                      * Save the current window and menu and set the current
793                      * window to the window whose menu this is
794                      */
795                     SFG_Window *save_window = fgStructure.Window;
796                     SFG_Menu *save_menu = fgStructure.Menu;
797                     SFG_Window *parent_window =
798                         window->ActiveMenu->ParentWindow;
799                     fgSetWindow( parent_window );
800                     fgStructure.Menu = window->ActiveMenu;
801
802                     /* Execute the menu callback */
803                     fgExecuteMenuCallback( window->ActiveMenu );
804                     fgDeactivateMenu( parent_window );
805
806                     /* Restore the current window and menu */
807                     fgSetWindow( save_window );
808                     fgStructure.Menu = save_menu;
809                 }
810                 else if( pressed )
811                     /*
812                      * Outside the menu, deactivate if it's a downclick
813                      *
814                      * XXX This isn't enough.  A downclick outside of
815                      * XXX the interior of our freeglut windows should also
816                      * XXX deactivate the menu.  This is more complicated.
817                      */
818                     fgDeactivateMenu( window->ActiveMenu->ParentWindow );
819
820                 /*
821                  * XXX Why does an active menu require a redisplay at
822                  * XXX this point?  If this can come out cleanly, then
823                  * XXX it probably should do so; if not, a comment should
824                  * XXX explain it.
825                  */
826                 window->State.Redisplay = GL_TRUE;
827                 break;
828             }
829
830             /* No active menu, let's check whether we need to activate one. */
831             if( ( 0 <= button ) &&
832                 ( FREEGLUT_MAX_MENUS > button ) &&
833                 ( window->Menu[ button ] ) &&
834                 pressed )
835             {
836                 /* XXX Posting a requisite Redisplay seems bogus. */
837                 window->State.Redisplay = GL_TRUE;
838                 fgSetWindow( window );
839                 fgActivateMenu( window, button );
840                 break;
841             }
842
843             /*
844              * Check if there is a mouse or mouse wheel callback hooked to the
845              * window
846              */
847             if( ! FETCH_WCB( *window, Mouse ) &&
848                 ! FETCH_WCB( *window, MouseWheel ) )
849                 break;
850
851             fgState.Modifiers = fghGetXModifiers( &event );
852
853             /*
854              * Finally execute the mouse or mouse wheel callback
855              *
856              * XXX Use a symbolic constant, *not* "4"!  ("3, sire!")
857              */
858             if( ( button < 3 ) || ( ! FETCH_WCB( *window, MouseWheel ) ) )
859                 INVOKE_WCB( *window, Mouse, ( button,
860                                               pressed ? GLUT_DOWN : GLUT_UP,
861                                               event.xbutton.x,
862                                               event.xbutton.y )
863                 );
864             else
865             {
866                 /*
867                  * Map 4 and 5 to wheel zero; EVEN to +1, ODD to -1
868                  *  "  6 and 7 "    "   one; ...
869                  *
870                  * XXX This *should* be behind some variables/macros,
871                  * XXX since the order and numbering isn't certain
872                  * XXX See XFree86 configuration docs (even back in the
873                  * XXX 3.x days, and especially with 4.x).
874                  *
875                  * XXX Note that {button} has already been decremeted
876                  * XXX in mapping from X button numbering to GLUT.
877                  */
878                 int wheel_number = (button - 3) / 2;
879                 int direction = -1;
880                 if( button % 2 )
881                     direction = 1;
882
883                 if( pressed )
884                     INVOKE_WCB( *window, MouseWheel, ( wheel_number,
885                                                        direction,
886                                                        event.xbutton.x,
887                                                        event.xbutton.y )
888                     );
889             }
890
891             /* Trash the modifiers state */
892             fgState.Modifiers = 0xffffffff;
893         }
894         break;
895
896         case KeyRelease:
897         case KeyPress:
898         {
899             FGCBKeyboard keyboard_cb;
900             FGCBSpecial special_cb;
901
902             GETWINDOW( xkey );
903             GETMOUSE( xkey );
904
905             /* Detect auto repeated keys, if configured globally or per-window */
906
907             if ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE )
908             {
909                 if (event.type==KeyRelease)
910                 {
911                     /*
912                      * Look at X11 keystate to detect repeat mode.
913                      * While X11 says the key is actually held down, we'll ignore KeyRelease/KeyPress pairs.
914                      */
915
916                     char keys[32];
917                     XQueryKeymap( fgDisplay.Display, keys ); /* Look at X11 keystate to detect repeat mode */
918
919                     if ( event.xkey.keycode<256 )            /* XQueryKeymap is limited to 256 keycodes    */
920                     {
921                         if ( keys[event.xkey.keycode>>3] & (1<<(event.xkey.keycode%8)) )
922                             window->State.KeyRepeating = GL_TRUE;
923                         else
924                             window->State.KeyRepeating = GL_FALSE;
925                     }
926                 }
927             }
928             else
929                 window->State.KeyRepeating = GL_FALSE;
930
931             /* Cease processing this event if it is auto repeated */
932
933             if (window->State.KeyRepeating)
934                 break;
935
936             if( event.type == KeyPress )
937             {
938                 keyboard_cb = FETCH_WCB( *window, Keyboard );
939                 special_cb  = FETCH_WCB( *window, Special  );
940             }
941             else
942             {
943                 keyboard_cb = FETCH_WCB( *window, KeyboardUp );
944                 special_cb  = FETCH_WCB( *window, SpecialUp  );
945             }
946
947             /* Is there a keyboard/special callback hooked for this window? */
948             if( keyboard_cb || special_cb )
949             {
950                 XComposeStatus composeStatus;
951                 char asciiCode[ 32 ];
952                 KeySym keySym;
953                 int len;
954
955                 /* Check for the ASCII/KeySym codes associated with the event: */
956                 len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode),
957                                      &keySym, &composeStatus
958                 );
959
960                 /* GLUT API tells us to have two separate callbacks... */
961                 if( len > 0 )
962                 {
963                     /* ...one for the ASCII translateable keypresses... */
964                     if( keyboard_cb )
965                     {
966                         fgSetWindow( window );
967                         fgState.Modifiers = fghGetXModifiers( &event );
968                         keyboard_cb( asciiCode[ 0 ],
969                                      event.xkey.x, event.xkey.y
970                         );
971                         fgState.Modifiers = 0xffffffff;
972                     }
973                 }
974                 else
975                 {
976                     int special = -1;
977
978                     /*
979                      * ...and one for all the others, which need to be
980                      * translated to GLUT_KEY_Xs...
981                      */
982                     switch( keySym )
983                     {
984                     case XK_F1:     special = GLUT_KEY_F1;     break;
985                     case XK_F2:     special = GLUT_KEY_F2;     break;
986                     case XK_F3:     special = GLUT_KEY_F3;     break;
987                     case XK_F4:     special = GLUT_KEY_F4;     break;
988                     case XK_F5:     special = GLUT_KEY_F5;     break;
989                     case XK_F6:     special = GLUT_KEY_F6;     break;
990                     case XK_F7:     special = GLUT_KEY_F7;     break;
991                     case XK_F8:     special = GLUT_KEY_F8;     break;
992                     case XK_F9:     special = GLUT_KEY_F9;     break;
993                     case XK_F10:    special = GLUT_KEY_F10;    break;
994                     case XK_F11:    special = GLUT_KEY_F11;    break;
995                     case XK_F12:    special = GLUT_KEY_F12;    break;
996
997                     case XK_Left:   special = GLUT_KEY_LEFT;   break;
998                     case XK_Right:  special = GLUT_KEY_RIGHT;  break;
999                     case XK_Up:     special = GLUT_KEY_UP;     break;
1000                     case XK_Down:   special = GLUT_KEY_DOWN;   break;
1001
1002                     case XK_KP_Prior:
1003                     case XK_Prior:  special = GLUT_KEY_PAGE_UP; break;
1004                     case XK_KP_Next:
1005                     case XK_Next:   special = GLUT_KEY_PAGE_DOWN; break;
1006                     case XK_KP_Home:
1007                     case XK_Home:   special = GLUT_KEY_HOME;   break;
1008                     case XK_KP_End:
1009                     case XK_End:    special = GLUT_KEY_END;    break;
1010                     case XK_KP_Insert:
1011                     case XK_Insert: special = GLUT_KEY_INSERT; break;
1012                     }
1013
1014                     /*
1015                      * Execute the callback (if one has been specified),
1016                      * given that the special code seems to be valid...
1017                      */
1018                     if( special_cb && (special != -1) )
1019                     {
1020                         fgSetWindow( window );
1021                         fgState.Modifiers = fghGetXModifiers( &event );
1022                         special_cb( special, event.xkey.x, event.xkey.y );
1023                         fgState.Modifiers = 0xffffffff;
1024                     }
1025                 }
1026             }
1027         }
1028         break;
1029
1030         case ReparentNotify:
1031             break; /* XXX Should disable this event */
1032
1033         default:
1034             fgWarning ("Unknown X event type: %d", event.type);
1035             break;
1036         }
1037     }
1038
1039 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
1040
1041     MSG stMsg;
1042
1043     while( PeekMessage( &stMsg, NULL, 0, 0, PM_NOREMOVE ) )
1044     {
1045         if( GetMessage( &stMsg, NULL, 0, 0 ) == 0 )
1046         {
1047             if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
1048             {
1049                 fgDeinitialize( );
1050                 exit( 0 );
1051             }
1052             else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS )
1053                 fgState.ExecState = GLUT_EXEC_STATE_STOP;
1054
1055             return;
1056         }
1057
1058         TranslateMessage( &stMsg );
1059         DispatchMessage( &stMsg );
1060     }
1061 #endif
1062
1063     if( fgState.Timers.First )
1064         fghCheckTimers( );
1065     fghCheckJoystickPolls( );
1066     fghDisplayAll( );
1067
1068     fgCloseWindows( );
1069 }
1070
1071 /*
1072  * Enters the freeglut processing loop.
1073  * Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".
1074  */
1075 void FGAPIENTRY glutMainLoop( void )
1076 {
1077     int action;
1078
1079 #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE
1080     SFG_Window *window = (SFG_Window *)fgStructure.Windows.First ;
1081 #endif
1082
1083     freeglut_assert_ready;
1084
1085 #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE
1086     /*
1087      * Processing before the main loop:  If there is a window which is open and
1088      * which has a visibility callback, call it.  I know this is an ugly hack,
1089      * but I'm not sure what else to do about it.  Ideally we should leave
1090      * something uninitialized in the create window code and initialize it in
1091      * the main loop, and have that initialization create a "WM_ACTIVATE"
1092      * message.  Then we would put the visibility callback code in the
1093      * "case WM_ACTIVATE" block below.         - John Fay -- 10/24/02
1094      */
1095     while( window )
1096     {
1097         if ( FETCH_WCB( *window, Visibility ) )
1098         {
1099             SFG_Window *current_window = fgStructure.Window ;
1100
1101             INVOKE_WCB( *window, Visibility, ( window->State.Visible ) );
1102             fgSetWindow( current_window );
1103         }
1104
1105         window = (SFG_Window *)window->Node.Next ;
1106     }
1107 #endif
1108
1109     fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;
1110     while( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )
1111     {
1112         SFG_Window *window;
1113
1114         glutMainLoopEvent( );
1115         /*
1116          * Step through the list of windows, seeing if there are any
1117          * that are not menus
1118          */
1119         for( window = ( SFG_Window * )fgStructure.Windows.First;
1120              window;
1121              window = ( SFG_Window * )window->Node.Next )
1122             if ( ! ( window->IsMenu ) )
1123                 break;
1124
1125         if( ! window )
1126             fgState.ExecState = GLUT_EXEC_STATE_STOP;
1127         else
1128         {
1129             if( fgState.IdleCallback )
1130                 fgState.IdleCallback( );
1131
1132             fghSleepForEvents( );
1133         }
1134     }
1135
1136     /*
1137      * When this loop terminates, destroy the display, state and structure
1138      * of a freeglut session, so that another glutInit() call can happen
1139      *
1140      * Save the "ActionOnWindowClose" because "fgDeinitialize" resets it.
1141      */
1142     action = fgState.ActionOnWindowClose;
1143     fgDeinitialize( );
1144     if( action == GLUT_ACTION_EXIT )
1145         exit( 0 );
1146 }
1147
1148 /*
1149  * Leaves the freeglut processing loop.
1150  */
1151 void FGAPIENTRY glutLeaveMainLoop( void )
1152 {
1153     fgState.ExecState = GLUT_EXEC_STATE_STOP ;
1154 }
1155
1156
1157 #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE
1158 /*
1159  * Determine a GLUT modifer mask based on MS-WINDOWS system info.
1160  */
1161 static int fghGetWin32Modifiers (void)
1162 {
1163     return
1164         ( ( ( GetKeyState( VK_LSHIFT   ) < 0 ) ||
1165             ( GetKeyState( VK_RSHIFT   ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
1166         ( ( ( GetKeyState( VK_LCONTROL ) < 0 ) ||
1167             ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL  : 0 ) |
1168         ( ( ( GetKeyState( VK_LMENU    ) < 0 ) ||
1169             ( GetKeyState( VK_RMENU    ) < 0 )) ? GLUT_ACTIVE_ALT   : 0 );
1170 }
1171
1172 /*
1173  * The window procedure for handling Win32 events
1174  */
1175 LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,
1176                                LPARAM lParam )
1177 {
1178     SFG_Window* window = fgWindowByHandle( hWnd );
1179     PAINTSTRUCT ps;
1180     LONG lRet = 1;
1181
1182     if ( ( window == NULL ) && ( uMsg != WM_CREATE ) )
1183       return DefWindowProc( hWnd, uMsg, wParam, lParam );
1184
1185     /* printf ( "Window %3d message <%04x> %12d %12d\n", window?window->ID:0,
1186              uMsg, wParam, lParam ); */
1187     switch( uMsg )
1188     {
1189     case WM_CREATE:
1190         /* The window structure is passed as the creation structure paramter... */
1191         window = (SFG_Window *) (((LPCREATESTRUCT) lParam)->lpCreateParams);
1192         assert( window != NULL );
1193
1194         window->Window.Handle = hWnd;
1195         window->Window.Device = GetDC( hWnd );
1196         if( window->IsMenu )
1197         {
1198             unsigned int current_DisplayMode = fgState.DisplayMode;
1199             fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;
1200 #if !TARGET_HOST_WINCE
1201             fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
1202 #endif
1203             fgState.DisplayMode = current_DisplayMode;
1204
1205             if( fgStructure.MenuContext )
1206                 wglMakeCurrent( window->Window.Device,
1207                                 fgStructure.MenuContext->Context
1208                 );
1209             else
1210             {
1211                 fgStructure.MenuContext =
1212                     (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
1213                 fgStructure.MenuContext->Context =
1214                     wglCreateContext( window->Window.Device );
1215             }
1216
1217             /* window->Window.Context = wglGetCurrentContext ();   */
1218             window->Window.Context = wglCreateContext( window->Window.Device );
1219         }
1220         else
1221         {
1222 #if !TARGET_HOST_WINCE
1223             fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
1224 #endif
1225
1226             if( ! fgState.UseCurrentContext )
1227                 window->Window.Context =
1228                     wglCreateContext( window->Window.Device );
1229             else
1230             {
1231                 window->Window.Context = wglGetCurrentContext( );
1232                 if( ! window->Window.Context )
1233                     window->Window.Context =
1234                         wglCreateContext( window->Window.Device );
1235             }
1236         }
1237
1238         window->State.NeedToResize = GL_TRUE;
1239         window->State.Width  = fgState.Size.X;
1240         window->State.Height = fgState.Size.Y;
1241
1242         ReleaseDC( window->Window.Handle, window->Window.Device );
1243
1244 #if TARGET_HOST_WINCE
1245         /* Take over button handling */
1246         {
1247             HINSTANCE dxDllLib=LoadLibrary(_T("gx.dll"));
1248             if (dxDllLib)
1249             {
1250                 GXGetDefaultKeys_=(GXGETDEFAULTKEYS)GetProcAddress(dxDllLib, _T("?GXGetDefaultKeys@@YA?AUGXKeyList@@H@Z"));
1251                 GXOpenInput_=(GXOPENINPUT)GetProcAddress(dxDllLib, _T("?GXOpenInput@@YAHXZ"));
1252             }
1253
1254             if(GXOpenInput_)
1255                 (*GXOpenInput_)();
1256             if(GXGetDefaultKeys_)
1257                 gxKeyList = (*GXGetDefaultKeys_)(GX_LANDSCAPEKEYS);
1258         }
1259
1260 #endif /* TARGET_HOST_WINCE */
1261         break;
1262
1263     case WM_SIZE:
1264         /*
1265          * If the window is visible, then it is the user manually resizing it.
1266          * If it is not, then it is the system sending us a dummy resize with
1267          * zero dimensions on a "glutIconifyWindow" call.
1268          */
1269         if( window->State.Visible )
1270         {
1271             window->State.NeedToResize = GL_TRUE;
1272 #if TARGET_HOST_WINCE
1273             window->State.Width  = HIWORD(lParam);
1274             window->State.Height = LOWORD(lParam);
1275 #else
1276             window->State.Width  = LOWORD(lParam);
1277             window->State.Height = HIWORD(lParam);
1278 #endif /* TARGET_HOST_WINCE */
1279         }
1280
1281         break;
1282 #if 0
1283     case WM_SETFOCUS:
1284 /*        printf("WM_SETFOCUS: %p\n", window ); */
1285         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1286         break;
1287
1288     case WM_ACTIVATE:
1289         if (LOWORD(wParam) != WA_INACTIVE)
1290         {
1291             /* glutSetCursor( fgStructure.Window->State.Cursor ); */
1292 /*            printf("WM_ACTIVATE: glutSetCursor( %p, %d)\n", window,
1293                    window->State.Cursor ); */
1294             glutSetCursor( window->State.Cursor );
1295         }
1296
1297         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1298         break;
1299 #endif
1300
1301         /*
1302          * XXX Why not re-use some common code with the glutSetCursor()
1303          * XXX function (or perhaps invoke glutSetCursor())?
1304          * XXX That is, why are we duplicating code, here, from
1305          * XXX glutSetCursor()?  The WIN32 code should be able to just
1306          * XXX call glutSetCursor() instead of defining two macros
1307          * XXX and implementing a nested case in-line.
1308          */
1309     case WM_SETCURSOR:
1310         /* Set the cursor AND change it for this window class. */
1311 #define MAP_CURSOR(a,b)                 \
1312     case a:                             \
1313     SetCursor( LoadCursor( NULL, b ) ); \
1314     break;
1315
1316         /* Nuke the cursor AND change it for this window class. */
1317 #define ZAP_CURSOR(a,b) \
1318     case a:             \
1319     SetCursor( NULL );  \
1320     break;
1321
1322         if( LOWORD( lParam ) == HTCLIENT )
1323             switch( window->State.Cursor )
1324             {
1325                 MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, IDC_ARROW     );
1326                 MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW,  IDC_ARROW     );
1327                 MAP_CURSOR( GLUT_CURSOR_INFO,        IDC_HELP      );
1328                 MAP_CURSOR( GLUT_CURSOR_DESTROY,     IDC_CROSS     );
1329                 MAP_CURSOR( GLUT_CURSOR_HELP,        IDC_HELP      );
1330                 MAP_CURSOR( GLUT_CURSOR_CYCLE,       IDC_SIZEALL   );
1331                 MAP_CURSOR( GLUT_CURSOR_SPRAY,       IDC_CROSS     );
1332                 MAP_CURSOR( GLUT_CURSOR_WAIT,        IDC_WAIT      );
1333                 MAP_CURSOR( GLUT_CURSOR_TEXT,        IDC_UPARROW   );
1334                 MAP_CURSOR( GLUT_CURSOR_CROSSHAIR,   IDC_CROSS     );
1335                 /* MAP_CURSOR( GLUT_CURSOR_NONE,        IDC_NO         ); */
1336                 ZAP_CURSOR( GLUT_CURSOR_NONE,        NULL          );
1337
1338             default:
1339                 MAP_CURSOR( GLUT_CURSOR_UP_DOWN,     IDC_ARROW     );
1340             }
1341         else
1342             lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1343         break;
1344
1345     case WM_SHOWWINDOW:
1346         window->State.Visible = GL_TRUE;
1347         window->State.Redisplay = GL_TRUE;
1348         break;
1349
1350     case WM_PAINT:
1351         /* Turn on the visibility in case it was turned off somehow */
1352         window->State.Visible = GL_TRUE;
1353         BeginPaint( hWnd, &ps );
1354         fghRedrawWindow( window );
1355         EndPaint( hWnd, &ps );
1356         break;
1357
1358     case WM_CLOSE:
1359         fgDestroyWindow ( window );
1360         if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
1361             PostQuitMessage(0);
1362         break;
1363
1364     case WM_DESTROY:
1365         /*
1366          * The window already got destroyed, so don't bother with it.
1367          */
1368         return 0;
1369
1370     case WM_MOUSEMOVE:
1371     {
1372 #if TARGET_HOST_WINCE
1373         window->State.MouseX = 320-HIWORD( lParam );
1374         window->State.MouseY = LOWORD( lParam );
1375 #else
1376         window->State.MouseX = LOWORD( lParam );
1377         window->State.MouseY = HIWORD( lParam );
1378 #endif /* TARGET_HOST_WINCE */
1379         /* Restrict to [-32768, 32767] to match X11 behaviour       */
1380         /* See comment in "freeglut_developer" mailing list 10/4/04 */
1381         if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;
1382         if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;
1383
1384         if ( window->ActiveMenu )
1385         {
1386             window->State.Redisplay = GL_TRUE;
1387             fgSetWindow ( window->ActiveMenu->ParentWindow );
1388             break;
1389         }
1390
1391         fgState.Modifiers = fghGetWin32Modifiers( );
1392
1393         if( ( wParam & MK_LBUTTON ) ||
1394             ( wParam & MK_MBUTTON ) ||
1395             ( wParam & MK_RBUTTON ) )
1396             INVOKE_WCB( *window, Motion, ( window->State.MouseX,
1397                                            window->State.MouseY ) );
1398         else
1399             INVOKE_WCB( *window, Passive, ( window->State.MouseX,
1400                                             window->State.MouseY ) );
1401
1402         fgState.Modifiers = 0xffffffff;
1403     }
1404     break;
1405
1406     case WM_LBUTTONDOWN:
1407     case WM_MBUTTONDOWN:
1408     case WM_RBUTTONDOWN:
1409     case WM_LBUTTONUP:
1410     case WM_MBUTTONUP:
1411     case WM_RBUTTONUP:
1412     {
1413         GLboolean pressed = GL_TRUE;
1414         int button;
1415
1416 #if TARGET_HOST_WINCE
1417         window->State.MouseX = 320-HIWORD( lParam );
1418         window->State.MouseY = LOWORD( lParam );
1419 #else
1420         window->State.MouseX = LOWORD( lParam );
1421         window->State.MouseY = HIWORD( lParam );
1422 #endif /* TARGET_HOST_WINCE */
1423
1424         /* Restrict to [-32768, 32767] to match X11 behaviour       */
1425         /* See comment in "freeglut_developer" mailing list 10/4/04 */
1426         if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;
1427         if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;
1428
1429         switch( uMsg )
1430         {
1431         case WM_LBUTTONDOWN:
1432             pressed = GL_TRUE;
1433             button = GLUT_LEFT_BUTTON;
1434             break;
1435         case WM_MBUTTONDOWN:
1436             pressed = GL_TRUE;
1437             button = GLUT_MIDDLE_BUTTON;
1438             break;
1439         case WM_RBUTTONDOWN:
1440             pressed = GL_TRUE;
1441             button = GLUT_RIGHT_BUTTON;
1442             break;
1443         case WM_LBUTTONUP:
1444             pressed = GL_FALSE;
1445             button = GLUT_LEFT_BUTTON;
1446             break;
1447         case WM_MBUTTONUP:
1448             pressed = GL_FALSE;
1449             button = GLUT_MIDDLE_BUTTON;
1450             break;
1451         case WM_RBUTTONUP:
1452             pressed = GL_FALSE;
1453             button = GLUT_RIGHT_BUTTON;
1454             break;
1455         default:
1456             pressed = GL_FALSE;
1457             button = -1;
1458             break;
1459         }
1460
1461 #if !TARGET_HOST_WINCE
1462         if( GetSystemMetrics( SM_SWAPBUTTON ) )
1463         {
1464             if( button == GLUT_LEFT_BUTTON )
1465                 button = GLUT_RIGHT_BUTTON;
1466             else
1467                 if( button == GLUT_RIGHT_BUTTON )
1468                     button = GLUT_LEFT_BUTTON;
1469         }
1470 #endif /* !TARGET_HOST_WINCE */
1471
1472         if( button == -1 )
1473             return DefWindowProc( hWnd, uMsg, lParam, wParam );
1474
1475         /*
1476          * XXX This comment is duplicated in two other spots.
1477          * XXX Can we centralize it?
1478          *
1479          * Do not execute the application's mouse callback if a
1480          * menu is hooked to this button.
1481          * In that case an appropriate private call should be generated.
1482          * Near as I can tell, this is the menu behaviour:
1483          *  - Down-click the menu button, menu not active:  activate
1484          *    the menu with its upper left-hand corner at the mouse location.
1485          *  - Down-click any button outside the menu, menu active:
1486          *    deactivate the menu
1487          *  - Down-click any button inside the menu, menu active:
1488          *    select the menu entry and deactivate the menu
1489          *  - Up-click the menu button, menu not active:  nothing happens
1490          *  - Up-click the menu button outside the menu, menu active:
1491          *    nothing happens
1492          *  - Up-click the menu button inside the menu, menu active:
1493          *    select the menu entry and deactivate the menu
1494          */
1495         /* Window has an active menu, it absorbs any mouse click */
1496         if( window->ActiveMenu )
1497         {
1498             /* Outside the menu, deactivate the menu if it's a downclick */
1499             if( ! fgCheckActiveMenu( window, window->ActiveMenu ) )
1500             {
1501                 if( pressed )
1502                     fgDeactivateMenu( window->ActiveMenu->ParentWindow );
1503             }
1504             else  /* In menu, invoke the callback and deactivate the menu*/
1505             {
1506                 /*
1507                  * Save the current window and menu and set the current
1508                  * window to the window whose menu this is
1509                  */
1510                 SFG_Window *save_window = fgStructure.Window;
1511                 SFG_Menu *save_menu = fgStructure.Menu;
1512                 SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
1513                 fgSetWindow( parent_window );
1514                 fgStructure.Menu = window->ActiveMenu;
1515
1516                 /* Execute the menu callback */
1517                 fgExecuteMenuCallback( window->ActiveMenu );
1518                 fgDeactivateMenu( parent_window );
1519
1520                 /* Restore the current window and menu */
1521                 fgSetWindow( save_window );
1522                 fgStructure.Menu = save_menu;
1523             }
1524
1525             /*
1526              * Let's make the window redraw as a result of the mouse
1527              * click and menu activity.
1528              */
1529             if( ! window->IsMenu )
1530                 window->State.Redisplay = GL_TRUE;
1531
1532             break;
1533         }
1534
1535         if( window->Menu[ button ] && pressed )
1536         {
1537             window->State.Redisplay = GL_TRUE;
1538             fgSetWindow( window );
1539             fgActivateMenu( window, button );
1540
1541             break;
1542         }
1543
1544         /* Set capture so that the window captures all the mouse messages */
1545         /*
1546          * XXX - Multiple button support:  Under X11, the mouse is not released
1547          * XXX - from the window until all buttons have been released, even if the
1548          * XXX - user presses a button in another window.  This will take more
1549          * XXX - code changes than I am up to at the moment (10/5/04).  The present
1550          * XXX - is a 90 percent solution.
1551          */
1552         if ( pressed == GL_TRUE )
1553           SetCapture ( window->Window.Handle ) ;
1554         else
1555           ReleaseCapture () ;
1556
1557         if( ! FETCH_WCB( *window, Mouse ) )
1558             break;
1559
1560         fgSetWindow( window );
1561         fgState.Modifiers = fghGetWin32Modifiers( );
1562
1563         INVOKE_WCB(
1564             *window, Mouse,
1565             ( button,
1566               pressed ? GLUT_DOWN : GLUT_UP,
1567               window->State.MouseX,
1568               window->State.MouseY
1569             )
1570         );
1571
1572         fgState.Modifiers = 0xffffffff;
1573     }
1574     break;
1575
1576     case 0x020a:
1577         /* Should be WM_MOUSEWHEEL but my compiler doesn't recognize it */
1578     {
1579         /*
1580          * XXX THIS IS SPECULATIVE -- John Fay, 10/2/03
1581          * XXX Should use WHEEL_DELTA instead of 120
1582          */
1583         int wheel_number = LOWORD( wParam );
1584         short ticks = ( short )HIWORD( wParam ) / 120;
1585         int direction = 1;
1586
1587         if( ticks < 0 )
1588         {
1589             direction = -1;
1590             ticks = -ticks;
1591         }
1592
1593         /*
1594          * The mouse cursor has moved. Remember the new mouse cursor's position
1595          */
1596         /*        window->State.MouseX = LOWORD( lParam ); */
1597         /* Need to adjust by window position, */
1598         /*        window->State.MouseY = HIWORD( lParam ); */
1599         /* change "lParam" to other parameter */
1600
1601         if( ! FETCH_WCB( *window, MouseWheel ) &&
1602             ! FETCH_WCB( *window, Mouse ) )
1603             break;
1604
1605         fgSetWindow( window );
1606         fgState.Modifiers = fghGetWin32Modifiers( );
1607
1608         while( ticks-- )
1609             if( FETCH_WCB( *window, MouseWheel ) )
1610                 INVOKE_WCB( *window, MouseWheel,
1611                             ( wheel_number,
1612                               direction,
1613                               window->State.MouseX,
1614                               window->State.MouseY
1615                             )
1616                 );
1617             else  /* No mouse wheel, call the mouse button callback twice */
1618             {
1619                 /*
1620                  * XXX The below assumes that you have no more than 3 mouse
1621                  * XXX buttons.  Sorry.
1622                  */
1623                 int button = wheel_number*2 + 4;
1624                 if( direction > 0 )
1625                     ++button;
1626                 INVOKE_WCB( *window, Mouse,
1627                             ( button, GLUT_DOWN,
1628                               window->State.MouseX, window->State.MouseY )
1629                 );
1630                 INVOKE_WCB( *window, Mouse,
1631                             ( button, GLUT_UP,
1632                               window->State.MouseX, window->State.MouseX )
1633                 );
1634             }
1635
1636         fgState.Modifiers = 0xffffffff;
1637     }
1638     break ;
1639
1640     case WM_SYSKEYDOWN:
1641     case WM_KEYDOWN:
1642     {
1643         int keypress = -1;
1644         POINT mouse_pos ;
1645
1646         if( ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE ) && (HIWORD(lParam) & KF_REPEAT) )
1647             break;
1648
1649         /*
1650          * Remember the current modifiers state. This is done here in order
1651          * to make sure the VK_DELETE keyboard callback is executed properly.
1652          */
1653         fgState.Modifiers = fghGetWin32Modifiers( );
1654
1655         GetCursorPos( &mouse_pos );
1656         ScreenToClient( window->Window.Handle, &mouse_pos );
1657
1658         window->State.MouseX = mouse_pos.x;
1659         window->State.MouseY = mouse_pos.y;
1660
1661         /* Convert the Win32 keystroke codes to GLUTtish way */
1662 #       define KEY(a,b) case a: keypress = b; break;
1663
1664         switch( wParam )
1665         {
1666             KEY( VK_F1,     GLUT_KEY_F1        );
1667             KEY( VK_F2,     GLUT_KEY_F2        );
1668             KEY( VK_F3,     GLUT_KEY_F3        );
1669             KEY( VK_F4,     GLUT_KEY_F4        );
1670             KEY( VK_F5,     GLUT_KEY_F5        );
1671             KEY( VK_F6,     GLUT_KEY_F6        );
1672             KEY( VK_F7,     GLUT_KEY_F7        );
1673             KEY( VK_F8,     GLUT_KEY_F8        );
1674             KEY( VK_F9,     GLUT_KEY_F9        );
1675             KEY( VK_F10,    GLUT_KEY_F10       );
1676             KEY( VK_F11,    GLUT_KEY_F11       );
1677             KEY( VK_F12,    GLUT_KEY_F12       );
1678             KEY( VK_PRIOR,  GLUT_KEY_PAGE_UP   );
1679             KEY( VK_NEXT,   GLUT_KEY_PAGE_DOWN );
1680             KEY( VK_HOME,   GLUT_KEY_HOME      );
1681             KEY( VK_END,    GLUT_KEY_END       );
1682             KEY( VK_LEFT,   GLUT_KEY_LEFT      );
1683             KEY( VK_UP,     GLUT_KEY_UP        );
1684             KEY( VK_RIGHT,  GLUT_KEY_RIGHT     );
1685             KEY( VK_DOWN,   GLUT_KEY_DOWN      );
1686             KEY( VK_INSERT, GLUT_KEY_INSERT    );
1687
1688         case VK_DELETE:
1689             /* The delete key should be treated as an ASCII keypress: */
1690             INVOKE_WCB( *window, Keyboard,
1691                         ( 127, window->State.MouseX, window->State.MouseY )
1692             );
1693         }
1694
1695 #if TARGET_HOST_WINCE
1696         if(!(lParam & 0x40000000)) /* Prevent auto-repeat */
1697         {
1698             if(wParam==(unsigned)gxKeyList.vkRight)
1699                 keypress = GLUT_KEY_RIGHT;
1700             else if(wParam==(unsigned)gxKeyList.vkLeft)
1701                 keypress = GLUT_KEY_LEFT;
1702             else if(wParam==(unsigned)gxKeyList.vkUp)
1703                 keypress = GLUT_KEY_UP;
1704             else if(wParam==(unsigned)gxKeyList.vkDown)
1705                 keypress = GLUT_KEY_DOWN;
1706             else if(wParam==(unsigned)gxKeyList.vkA)
1707                 keypress = GLUT_KEY_F1;
1708             else if(wParam==(unsigned)gxKeyList.vkB)
1709                 keypress = GLUT_KEY_F2;
1710             else if(wParam==(unsigned)gxKeyList.vkC)
1711                 keypress = GLUT_KEY_F3;
1712             else if(wParam==(unsigned)gxKeyList.vkStart)
1713                 keypress = GLUT_KEY_F4;
1714         }
1715 #endif
1716
1717         if( keypress != -1 )
1718             INVOKE_WCB( *window, Special,
1719                         ( keypress,
1720                           window->State.MouseX, window->State.MouseY )
1721             );
1722
1723         fgState.Modifiers = 0xffffffff;
1724     }
1725     break;
1726
1727     case WM_SYSKEYUP:
1728     case WM_KEYUP:
1729     {
1730         int keypress = -1;
1731         POINT mouse_pos;
1732
1733         /*
1734          * Remember the current modifiers state. This is done here in order
1735          * to make sure the VK_DELETE keyboard callback is executed properly.
1736          */
1737         fgState.Modifiers = fghGetWin32Modifiers( );
1738
1739         GetCursorPos( &mouse_pos );
1740         ScreenToClient( window->Window.Handle, &mouse_pos );
1741
1742         window->State.MouseX = mouse_pos.x;
1743         window->State.MouseY = mouse_pos.y;
1744
1745         /*
1746          * Convert the Win32 keystroke codes to GLUTtish way.
1747          * "KEY(a,b)" was defined under "WM_KEYDOWN"
1748          */
1749
1750         switch( wParam )
1751         {
1752             KEY( VK_F1,     GLUT_KEY_F1        );
1753             KEY( VK_F2,     GLUT_KEY_F2        );
1754             KEY( VK_F3,     GLUT_KEY_F3        );
1755             KEY( VK_F4,     GLUT_KEY_F4        );
1756             KEY( VK_F5,     GLUT_KEY_F5        );
1757             KEY( VK_F6,     GLUT_KEY_F6        );
1758             KEY( VK_F7,     GLUT_KEY_F7        );
1759             KEY( VK_F8,     GLUT_KEY_F8        );
1760             KEY( VK_F9,     GLUT_KEY_F9        );
1761             KEY( VK_F10,    GLUT_KEY_F10       );
1762             KEY( VK_F11,    GLUT_KEY_F11       );
1763             KEY( VK_F12,    GLUT_KEY_F12       );
1764             KEY( VK_PRIOR,  GLUT_KEY_PAGE_UP   );
1765             KEY( VK_NEXT,   GLUT_KEY_PAGE_DOWN );
1766             KEY( VK_HOME,   GLUT_KEY_HOME      );
1767             KEY( VK_END,    GLUT_KEY_END       );
1768             KEY( VK_LEFT,   GLUT_KEY_LEFT      );
1769             KEY( VK_UP,     GLUT_KEY_UP        );
1770             KEY( VK_RIGHT,  GLUT_KEY_RIGHT     );
1771             KEY( VK_DOWN,   GLUT_KEY_DOWN      );
1772             KEY( VK_INSERT, GLUT_KEY_INSERT    );
1773
1774           case VK_DELETE:
1775               /* The delete key should be treated as an ASCII keypress: */
1776               INVOKE_WCB( *window, KeyboardUp,
1777                           ( 127, window->State.MouseX, window->State.MouseY )
1778               );
1779               break;
1780
1781         default:
1782         {
1783 #if !TARGET_HOST_WINCE
1784             BYTE state[ 256 ];
1785             WORD code[ 2 ];
1786
1787             GetKeyboardState( state );
1788
1789             if( ToAscii( wParam, 0, state, code, 0 ) == 1 )
1790                 wParam=code[ 0 ];
1791
1792             INVOKE_WCB( *window, KeyboardUp,
1793                         ( (char)wParam,
1794                           window->State.MouseX, window->State.MouseY )
1795             );
1796 #endif /* !TARGET_HOST_WINCE */
1797         }
1798         }
1799
1800         if( keypress != -1 )
1801             INVOKE_WCB( *window, SpecialUp,
1802                         ( keypress,
1803                           window->State.MouseX, window->State.MouseY )
1804             );
1805
1806         fgState.Modifiers = 0xffffffff;
1807     }
1808     break;
1809
1810     case WM_SYSCHAR:
1811     case WM_CHAR:
1812     {
1813       if( (fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE) && (HIWORD(lParam) & KF_REPEAT) )
1814             break;
1815
1816         fgState.Modifiers = fghGetWin32Modifiers( );
1817         INVOKE_WCB( *window, Keyboard,
1818                     ( (char)wParam,
1819                       window->State.MouseX, window->State.MouseY )
1820         );
1821         fgState.Modifiers = 0xffffffff;
1822     }
1823     break;
1824
1825     case WM_CAPTURECHANGED:
1826         /* User has finished resizing the window, force a redraw */
1827         INVOKE_WCB( *window, Display, ( ) );
1828
1829         /*lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ); */
1830         break;
1831
1832         /* Other messages that I have seen and which are not handled already */
1833     case WM_SETTEXT:  /* 0x000c */
1834         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1835         /* Pass it on to "DefWindowProc" to set the window text */
1836         break;
1837
1838     case WM_GETTEXT:  /* 0x000d */
1839         /* Ideally we would copy the title of the window into "lParam" */
1840         /* strncpy ( (char *)lParam, "Window Title", wParam );
1841            lRet = ( wParam > 12 ) ? 12 : wParam;  */
1842         /* the number of characters copied */
1843         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1844         break;
1845
1846     case WM_GETTEXTLENGTH:  /* 0x000e */
1847         /* Ideally we would get the length of the title of the window */
1848         lRet = 12;
1849         /* the number of characters in "Window Title\0" (see above) */
1850         break;
1851
1852     case WM_ERASEBKGND:  /* 0x0014 */
1853         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1854         break;
1855
1856 #if !TARGET_HOST_WINCE
1857     case WM_SYNCPAINT:  /* 0x0088 */
1858         /* Another window has moved, need to update this one */
1859         window->State.Redisplay = GL_TRUE;
1860         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1861         /* Help screen says this message must be passed to "DefWindowProc" */
1862         break;
1863
1864     case WM_NCPAINT:  /* 0x0085 */
1865       /* Need to update the border of this window */
1866         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1867         /* Pass it on to "DefWindowProc" to repaint a standard border */
1868         break;
1869
1870     case WM_SYSCOMMAND :  /* 0x0112 */
1871         {
1872           /*
1873            * We have received a system command message.  Try to act on it.
1874            * The commands are passed in through the "wParam" parameter:
1875            * The least significant digit seems to be which edge of the window
1876            * is being used for a resize event:
1877            *     4  3  5
1878            *     1     2
1879            *     7  6  8
1880            * Congratulations and thanks to Richard Rauch for figuring this out..
1881            */
1882             switch ( wParam & 0xfff0 )
1883             {
1884             case SC_SIZE       :
1885                 break ;
1886
1887             case SC_MOVE       :
1888                 break ;
1889
1890             case SC_MINIMIZE   :
1891                 /* User has clicked on the "-" to minimize the window */
1892                 /* Turn off the visibility */
1893                 window->State.Visible = GL_FALSE ;
1894
1895                 break ;
1896
1897             case SC_MAXIMIZE   :
1898                 break ;
1899
1900             case SC_NEXTWINDOW :
1901                 break ;
1902
1903             case SC_PREVWINDOW :
1904                 break ;
1905
1906             case SC_CLOSE      :
1907                 /* Followed very closely by a WM_CLOSE message */
1908                 break ;
1909
1910             case SC_VSCROLL    :
1911                 break ;
1912
1913             case SC_HSCROLL    :
1914                 break ;
1915
1916             case SC_MOUSEMENU  :
1917                 break ;
1918
1919             case SC_KEYMENU    :
1920                 break ;
1921
1922             case SC_ARRANGE    :
1923                 break ;
1924
1925             case SC_RESTORE    :
1926                 break ;
1927
1928             case SC_TASKLIST   :
1929                 break ;
1930
1931             case SC_SCREENSAVE :
1932                 break ;
1933
1934             case SC_HOTKEY     :
1935                 break ;
1936
1937             default:
1938 #if _DEBUG
1939                 fgWarning( "Unknown wParam type 0x%x", wParam );
1940 #endif
1941                 break;
1942             }
1943         }
1944 #endif /* !TARGET_HOST_WINCE */
1945
1946         /* We need to pass the message on to the operating system as well */
1947         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1948         break;
1949
1950     default:
1951         /* Handle unhandled messages */
1952         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1953         break;
1954     }
1955
1956     return lRet;
1957 }
1958 #endif
1959
1960 /*** END OF FILE ***/