4 * The windows message processing methods.
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
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:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
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.
32 #include "../include/GL/freeglut.h"
33 #include "freeglut_internal.h"
36 #if TARGET_HOST_UNIX_X11
37 #include <sys/types.h>
42 #elif TARGET_HOST_WIN32
46 #define MAX(a,b) (((a)>(b)) ? (a) : (b))
50 #define MIN(a,b) (((a)<(b)) ? (a) : (b))
55 * TODO BEFORE THE STABLE RELEASE:
57 * There are some issues concerning window redrawing under X11, and maybe
58 * some events are not handled. The Win32 version lacks some more features,
59 * but seems acceptable for not demanding purposes.
61 * Need to investigate why the X11 version breaks out with an error when
62 * closing a window (using the window manager, not glutDestroyWindow)...
65 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
68 * Calls a window's redraw method. This is used when
69 * a redraw is forced by the incoming window messages.
71 * XXX We can/should make a "unified" window handle type so that
72 * XXX the function headers don't need this silly #ifdef junk.
73 * XXX Make the type, say, {fgWindow}. On UNIX_X11, this is
74 * XXX {Window}. On WIN32, it is {HWND}. Then do the #ifdef
75 * XXX junk *once* in "freeglut_internal.h".
77 static void fghRedrawWindowByHandle
78 #if TARGET_HOST_UNIX_X11
80 #elif TARGET_HOST_WIN32
84 SFG_Window* window = fgWindowByHandle( handle );
85 freeglut_return_if_fail( window != NULL );
88 * XXX Other than clearing the Redisplay flag or not,
89 * XXX we may as well rely on the INVOK_WCB() doing this
92 * XXX If we do not invoke the display because the pointer
93 * XXX is not defined (should never happen, really), then
94 * XXX we may enter an infinite busy-loop trying to update
95 * XXX the window. Similarly, if we skip because the window
96 * XXX is not visible. However, if the window becomes visible
97 * XXX at a later time, the window should get its callback
98 * XXX invoked. I would recommend removing the first check,
99 * XXX and making the second check only affect whether the
100 * XXX callback is invoked---but always clear the flag, if
101 * XXX the {window} pointer is defined.
103 freeglut_return_if_fail( FETCH_WCB( *window, Display ) );
104 freeglut_return_if_fail( window->State.Visible == TRUE );
106 window->State.Redisplay = FALSE;
107 INVOKE_WCB( *window, Display, ( ) );
111 * Handle a window configuration change. When no reshape
112 * callback is hooked, the viewport size is updated to
113 * match the new window size.
115 static void fghReshapeWindowByHandle
116 #if TARGET_HOST_UNIX_X11
117 ( Window handle, int width, int height )
118 #elif TARGET_HOST_WIN32
119 ( HWND handle, int width, int height )
122 SFG_Window *current_window = fgStructure.Window;
124 SFG_Window* window = fgWindowByHandle( handle );
125 freeglut_return_if_fail( window != NULL );
127 if( !( FETCH_WCB( *window, Reshape ) ) )
129 fgSetWindow( window );
130 glViewport( 0, 0, width, height );
133 INVOKE_WCB( *window, Reshape, ( width, height ) );
136 * Force a window redraw. In Windows at least this is only a partial
137 * solution: if the window is increasing in size in either dimension,
138 * the already-drawn part does not get drawn again and things look funny.
139 * But without this we get this bad behaviour whenever we resize the
142 window->State.Redisplay = TRUE;
145 fgSetWindow( current_window );
149 * A static helper function to execute display callback for a window
151 static void fghcbDisplayWindow( SFG_Window *window, SFG_Enumerator *enumerator )
153 #if TARGET_HOST_UNIX_X11
155 * XXX Do we need/want to check the callback pointer here?
156 * XXX INVOKE_WCB() will check for us. Arguably, the
157 * XXX Redisplay status flag should be cleared regardless
158 * XXX of any concern but that {window} is a valid pointer
159 * XXX (which this function is assuming anyway).
160 * XXX Especially since old GLUT wouldn't even enter its main
161 * XXX loop if you didn't have a display callback defined...
163 if( ( FETCH_WCB( *window, Display ) ) &&
164 ( window->State.Redisplay == TRUE ) &&
165 ( window->State.Visible == TRUE ) )
167 SFG_Window *current_window = fgStructure.Window ;
169 window->State.Redisplay = FALSE;
170 INVOKE_WCB( *window, Display, ( ) );
171 fgSetWindow( current_window );
174 #elif TARGET_HOST_WIN32
176 if( window->State.NeedToResize )
178 SFG_Window *current_window = fgStructure.Window;
180 fgSetWindow( window );
182 fghReshapeWindowByHandle(
183 window->Window.Handle,
184 glutGet( GLUT_WINDOW_WIDTH ),
185 glutGet( GLUT_WINDOW_HEIGHT )
188 window->State.NeedToResize = FALSE;
189 fgSetWindow ( current_window );
193 * XXX See above comment about the Redisplay flag...
195 if( ( FETCH_WCB( *window, Display ) ) &&
196 ( window->State.Redisplay == TRUE ) &&
197 ( window->State.Visible == TRUE ) )
199 window->State.Redisplay = FALSE;
202 window->Window.Handle, NULL, NULL,
203 RDW_NOERASE | RDW_INTERNALPAINT | RDW_INVALIDATE | RDW_UPDATENOW
209 fgEnumSubWindows( window, fghcbDisplayWindow, enumerator );
213 * Make all windows perform a display call
215 static void fghDisplayAll( void )
217 SFG_Enumerator enumerator;
219 enumerator.found = FALSE;
220 enumerator.data = NULL;
222 fgEnumWindows( fghcbDisplayWindow, &enumerator );
226 * Window enumerator callback to check for the joystick polling code
228 static void fghcbCheckJoystickPolls( SFG_Window *window,
229 SFG_Enumerator *enumerator )
231 long int checkTime = fgElapsedTime( );
233 if( window->State.JoystickLastPoll + window->State.JoystickPollRate <=
236 fgJoystickPollWindow( window );
237 window->State.JoystickLastPoll = checkTime;
240 fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );
244 * Check all windows for joystick polling
246 static void fghCheckJoystickPolls( void )
248 SFG_Enumerator enumerator;
250 enumerator.found = FALSE;
251 enumerator.data = NULL;
253 fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );
257 * Check the global timers
259 static void fghCheckTimers( void )
261 long checkTime = fgElapsedTime( );
262 SFG_Timer *timer, *next;
265 fgListInit(&timedOut);
267 for( timer = (SFG_Timer *)fgState.Timers.First;
269 timer = (SFG_Timer *)next )
271 next = (SFG_Timer *)timer->Node.Next;
273 if( timer->TriggerTime <= checkTime )
275 fgListRemove( &fgState.Timers, &timer->Node );
276 fgListAppend( &timedOut, &timer->Node );
281 * Now feel free to execute all the hooked and timed out timer callbacks
282 * And delete the timed out timers...
284 while ( (timer = (SFG_Timer *)timedOut.First) )
286 if( timer->Callback != NULL )
287 timer->Callback( timer->ID );
288 fgListRemove( &timedOut, &timer->Node );
297 long fgElapsedTime( void )
299 if (fgState.Time.Set)
301 #if TARGET_HOST_UNIX_X11
305 gettimeofday( &now, NULL );
307 elapsed = (now.tv_usec - fgState.Time.Value.tv_usec) / 1000;
308 elapsed += (now.tv_sec - fgState.Time.Value.tv_sec) * 1000;
311 #elif TARGET_HOST_WIN32
312 return timeGetTime() - fgState.Time.Value;
317 #if TARGET_HOST_UNIX_X11
318 gettimeofday( &fgState.Time.Value, NULL );
319 #elif TARGET_HOST_WIN32
320 fgState.Time.Value = timeGetTime( );
322 fgState.Time.Set = TRUE;
329 void fgError( const char *fmt, ... )
335 fprintf( stderr, "freeglut ");
336 if( fgState.ProgramName )
337 fprintf (stderr, "(%s): ", fgState.ProgramName);
338 vfprintf( stderr, fmt, ap );
339 fprintf( stderr, "\n" );
343 if ( fgState.Initalized )
349 void fgWarning( const char *fmt, ... )
355 fprintf( stderr, "freeglut ");
356 if( fgState.ProgramName )
357 fprintf( stderr, "(%s): ", fgState.ProgramName );
358 vfprintf( stderr, fmt, ap );
359 fprintf( stderr, "\n" );
365 * Indicates whether Joystick events are being used by ANY window.
367 * The current mechanism is to walk all of the windows and ask if
368 * there is a joystick callback. Certainly in some cases, maybe
369 * in all cases, the joystick is attached to the system and accessed
370 * from ONE point by GLUT/freeglut, so this is not the right way,
371 * in general, to do this. However, the Joystick code is segregated
372 * in its own little world, so we can't access the information that
373 * we need in order to do that nicely.
376 * * Store Joystick data into freeglut global state.
377 * * Provide NON-static functions or data from Joystick *.c file.
379 * Basically, the RIGHT way to do this requires knowing something
380 * about the Joystick. Right now, the Joystick code is behind
384 static void fgCheckJoystickCallback( SFG_Window* w, SFG_Enumerator* e)
386 if( FETCH_WCB( *w, Joystick ) )
391 fgEnumSubWindows( w, fgCheckJoystickCallback, e );
393 static int fgHaveJoystick( void )
395 SFG_Enumerator enumerator;
396 enumerator.found = FALSE;
397 enumerator.data = NULL;
398 fgEnumWindows( fgCheckJoystickCallback, &enumerator );
399 return !!enumerator.data;
401 static void fgHavePendingRedisplaysCallback( SFG_Window* w, SFG_Enumerator* e)
403 if( w->State.Redisplay )
408 fgEnumSubWindows( w, fgHavePendingRedisplaysCallback, e );
410 static int fgHavePendingRedisplays (void)
412 SFG_Enumerator enumerator;
413 enumerator.found = FALSE;
414 enumerator.data = NULL;
415 fgEnumWindows( fgHavePendingRedisplaysCallback, &enumerator );
416 return !!enumerator.data;
419 * Indicates whether there are any outstanding timers.
421 static int fgHaveTimers( void )
423 return !!fgState.Timers.First;
426 * Returns the number of GLUT ticks (milliseconds) till the next timer event.
428 static long fgNextTimer( void )
430 long now = fgElapsedTime();
434 for( timer = (SFG_Timer *)fgState.Timers.First;
436 timer = (SFG_Timer *)timer->Node.Next )
437 ret = MIN( ret, MAX( 0, (timer->TriggerTime) - now ) );
442 * Does the magic required to relinquish the CPU until something interesting
445 static void fgSleepForEvents( void )
447 #if TARGET_HOST_UNIX_X11
454 if( fgState.IdleCallback || fgHavePendingRedisplays( ) )
456 socket = ConnectionNumber( fgDisplay.Display );
458 FD_SET( socket, &fdset );
460 msec = fgNextTimer( );
461 if( fgHaveJoystick( ) )
462 msec = MIN( msec, 10 );
464 wait.tv_sec = msec / 1000;
465 wait.tv_usec = (msec % 1000) * 1000;
466 err = select( socket+1, &fdset, NULL, NULL, &wait );
469 printf( "freeglut select() error: %d\n", errno );
471 #elif TARGET_HOST_WIN32
475 #if TARGET_HOST_UNIX_X11
477 * Returns GLUT modifier mask for an XEvent.
479 int fgGetXModifiers( XEvent *event )
483 if( event->xkey.state & ( ShiftMask | LockMask ) )
484 ret |= GLUT_ACTIVE_SHIFT;
485 if( event->xkey.state & ControlMask )
486 ret |= GLUT_ACTIVE_CTRL;
487 if( event->xkey.state & Mod1Mask )
488 ret |= GLUT_ACTIVE_ALT;
495 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
498 * Executes a single iteration in the freeglut processing loop.
500 void FGAPIENTRY glutMainLoopEvent( void )
502 #if TARGET_HOST_UNIX_X11
507 * This code was repeated constantly, so here it goes into a definition:
509 #define GETWINDOW(a) \
510 window = fgWindowByHandle( event.a.window ); \
511 if( window == NULL ) \
514 #define GETMOUSE(a) \
515 window->State.MouseX = event.a.x; \
516 window->State.MouseY = event.a.y;
518 freeglut_assert_ready;
520 while( XPending( fgDisplay.Display ) )
522 XNextEvent( fgDisplay.Display, &event );
528 * Destroy the window when the WM_DELETE_WINDOW message arrives
530 if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.DeleteWindow )
532 GETWINDOW( xclient );
534 fgCloseWindow ( window ) ;
535 fgAddToWindowDestroyList ( window, FALSE );
540 * CreateNotify causes a configure-event so that sub-windows are
541 * handled compatibly with GLUT. Otherwise, your sub-windows
542 * (in freeglut only) will not get an initial reshape event,
543 * which can break things.
545 * XXX NOTE that it is possible that you will more than one Reshape
546 * XXX event for your top-level window, but something like this
547 * XXX appears to be required for compatbility.
549 * GLUT presumably does this because it generally tries to treat
550 * sub-windows the same as windows.
553 case ConfigureNotify:
554 fghReshapeWindowByHandle(
555 event.xconfigure.window,
556 event.xconfigure.width,
557 event.xconfigure.height
563 * This is sent to confirm the XDestroyWindow call.
564 * XXX WHY is this commented out? Should we re-enable it?
566 /* fgAddToWindowDestroyList ( window, FALSE ); */
571 * We are too dumb to process partial exposes...
572 * XXX Well, we could do it. However, it seems to only
573 * XXX be potentially useful for single-buffered (since
574 * XXX double-buffered does not respect viewport when we
575 * XXX do a buffer-swap).
577 if( event.xexpose.count == 0 )
578 fghRedrawWindowByHandle( event.xexpose.window );
584 * If we never do anything with this, can we just not ask to
585 * get these messages?
591 * Have the client's keyboard knowledge updated (xlib.ps,
592 * page 206, says that's a good thing to do)
594 XRefreshKeyboardMapping( (XMappingEvent *) &event );
597 case VisibilityNotify:
599 GETWINDOW( xvisibility );
600 if( ! FETCH_WCB( *window, WindowStatus ) )
602 fgSetWindow( window );
605 * Sending this event, the X server can notify us that the window
606 * has just acquired one of the three possible visibility states:
607 * VisibilityUnobscured, VisibilityPartiallyObscured or
608 * VisibilityFullyObscured
610 switch( event.xvisibility.state )
612 case VisibilityUnobscured:
613 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_RETAINED ) );
614 window->State.Visible = TRUE;
617 case VisibilityPartiallyObscured:
618 INVOKE_WCB( *window, WindowStatus,
619 ( GLUT_PARTIALLY_RETAINED ) );
620 window->State.Visible = TRUE;
623 case VisibilityFullyObscured:
624 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_COVERED ) );
625 window->State.Visible = FALSE;
629 fgWarning( "Uknown X visibility state: %d",
630 event.xvisibility.state );
638 GETWINDOW( xcrossing );
639 GETMOUSE( xcrossing );
640 INVOKE_WCB( *window, Entry, ( ( EnterNotify == event.type ) ?
647 GETWINDOW( xmotion );
650 if( window->ActiveMenu )
652 if( window == window->ActiveMenu->ParentWindow )
654 window->ActiveMenu->Window->State.MouseX =
655 event.xmotion.x_root - window->ActiveMenu->X;
656 window->ActiveMenu->Window->State.MouseY =
657 event.xmotion.y_root - window->ActiveMenu->Y;
659 window->ActiveMenu->Window->State.Redisplay = TRUE ;
660 fgSetWindow( window->ActiveMenu->ParentWindow ) ;
666 * XXX For more than 5 buttons, just check {event.xmotion.state},
667 * XXX rather than a host of bit-masks?
669 if( (event.xmotion.state & Button1Mask) ||
670 (event.xmotion.state & Button2Mask) ||
671 (event.xmotion.state & Button3Mask) ||
672 (event.xmotion.state & Button4Mask) ||
673 (event.xmotion.state & Button5Mask) )
674 INVOKE_WCB( *window, Motion, ( event.xmotion.x,
677 INVOKE_WCB( *window, Passive, ( event.xmotion.x,
685 GLboolean pressed = TRUE;
688 if( event.type == ButtonRelease )
692 * A mouse button has been pressed or released. Traditionally,
693 * break if the window was found within the freeglut structures.
695 GETWINDOW( xbutton );
699 * An X button (at least in XFree86) is numbered from 1.
700 * A GLUT button is numbered from 0.
701 * Old GLUT passed through buttons other than just the first
702 * three, though it only gave symbolic names and official
703 * support to the first three.
705 button = event.xbutton.button - 1;
708 * XXX This comment is replicated in the WIN32 section and
709 * XXX maybe also in the menu code. Can we move the info
710 * XXX to one central place and *reference* it from here?
712 * Do not execute the application's mouse callback if a menu
713 * is hooked to this button. In that case an appropriate
714 * private call should be generated.
715 * Near as I can tell, this is the menu behaviour:
716 * - Down-click the menu button, menu not active: activate
717 * the menu with its upper left-hand corner at the mouse
719 * - Down-click any button outside the menu, menu active:
720 * deactivate the menu
721 * - Down-click any button inside the menu, menu active:
722 * select the menu entry and deactivate the menu
723 * - Up-click the menu button, menu not active: nothing happens
724 * - Up-click the menu button outside the menu, menu active:
726 * - Up-click the menu button inside the menu, menu active:
727 * select the menu entry and deactivate the menu
729 /* Window has an active menu, it absorbs any mouse click */
730 if( window->ActiveMenu )
732 if( window == window->ActiveMenu->ParentWindow )
734 window->ActiveMenu->Window->State.MouseX =
735 event.xbutton.x_root - window->ActiveMenu->X;
736 window->ActiveMenu->Window->State.MouseY =
737 event.xbutton.y_root - window->ActiveMenu->Y;
740 /* In the menu, invoke the callback and deactivate the menu*/
741 if( fgCheckActiveMenu( window->ActiveMenu->Window,
742 window->ActiveMenu ) == TRUE )
745 * Save the current window and menu and set the current
746 * window to the window whose menu this is
748 SFG_Window *save_window = fgStructure.Window;
749 SFG_Menu *save_menu = fgStructure.Menu;
750 SFG_Window *parent_window =
751 window->ActiveMenu->ParentWindow;
752 fgSetWindow( parent_window );
753 fgStructure.Menu = window->ActiveMenu;
755 /* Execute the menu callback */
756 fgExecuteMenuCallback( window->ActiveMenu );
757 fgDeactivateMenu( parent_window );
759 /* Restore the current window and menu */
760 fgSetWindow( save_window );
761 fgStructure.Menu = save_menu;
765 * Outside the menu, deactivate if it's a downclick
766 * XXX This isn't enough. A downclick outside of
767 * XXX the interior of our freeglut windows should also
768 * XXX deactivate the menu. This is more complicated.
770 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
772 window->State.Redisplay = TRUE;
777 * No active menu, let's check whether we need to activate one.
779 if( ( 0 <= button ) &&
781 ( window->Menu[ button ] ) &&
784 window->State.Redisplay = TRUE;
785 fgSetWindow( window );
786 fgActivateMenu( window, button );
791 * Check if there is a mouse or mouse wheel callback hooked to the
794 if( ! FETCH_WCB( *window, Mouse ) &&
795 ! FETCH_WCB( *window, MouseWheel ) )
799 * XXX Why don't we use {window}? Other code here does...
801 fgStructure.Window->State.Modifiers = fgGetXModifiers( &event );
804 * Finally execute the mouse or mouse wheel callback
806 * XXX Use a symbolic constant, *not* "4"!
808 if( ( button < 3 ) || ( ! FETCH_WCB( *window, MouseWheel ) ) )
809 INVOKE_WCB( *window, Mouse, ( button,
810 pressed ? GLUT_DOWN : GLUT_UP,
817 * Map 4 and 5 to wheel zero; EVEN to +1, ODD to -1
818 * " 6 and 7 " " one; ...
820 * XXX This *should* be behind some variables/macros,
821 * XXX since the order and numbering isn't certain
822 * XXX See XFree86 configuration docs (even back in the
823 * XXX 3.x days, and especially with 4.x).
825 * XXX Note that {button} has already been decremeted
826 * XXX in mapping from X button numbering to GLUT.
828 int wheel_number = (button - 3) / 2;
834 INVOKE_WCB( *window, MouseWheel, ( wheel_number,
842 * Trash the modifiers state
844 fgStructure.Window->State.Modifiers = 0xffffffff;
851 FGCBKeyboard keyboard_cb;
852 FGCBSpecial special_cb;
857 if( event.type == KeyPress )
859 keyboard_cb = FETCH_WCB( *window, Keyboard );
860 special_cb = FETCH_WCB( *window, Special );
864 keyboard_cb = FETCH_WCB( *window, KeyboardUp );
865 special_cb = FETCH_WCB( *window, SpecialUp );
869 * Is there a keyboard/special callback hooked for this window?
871 if( keyboard_cb || special_cb )
873 XComposeStatus composeStatus;
874 char asciiCode[ 32 ];
879 * Check for the ASCII/KeySym codes associated with the event:
881 len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode),
882 &keySym, &composeStatus );
885 * GLUT API tells us to have two separate callbacks...
890 * ...one for the ASCII translateable keypresses...
894 fgSetWindow( window );
895 window->State.Modifiers = fgGetXModifiers( &event );
896 keyboard_cb( asciiCode[ 0 ],
897 event.xkey.x, event.xkey.y );
898 window->State.Modifiers = 0xffffffff;
906 * ...and one for all the others, which need to be
907 * translated to GLUT_KEY_Xs...
911 case XK_F1: special = GLUT_KEY_F1; break;
912 case XK_F2: special = GLUT_KEY_F2; break;
913 case XK_F3: special = GLUT_KEY_F3; break;
914 case XK_F4: special = GLUT_KEY_F4; break;
915 case XK_F5: special = GLUT_KEY_F5; break;
916 case XK_F6: special = GLUT_KEY_F6; break;
917 case XK_F7: special = GLUT_KEY_F7; break;
918 case XK_F8: special = GLUT_KEY_F8; break;
919 case XK_F9: special = GLUT_KEY_F9; break;
920 case XK_F10: special = GLUT_KEY_F10; break;
921 case XK_F11: special = GLUT_KEY_F11; break;
922 case XK_F12: special = GLUT_KEY_F12; break;
924 case XK_Left: special = GLUT_KEY_LEFT; break;
925 case XK_Right: special = GLUT_KEY_RIGHT; break;
926 case XK_Up: special = GLUT_KEY_UP; break;
927 case XK_Down: special = GLUT_KEY_DOWN; break;
930 case XK_Prior: special = GLUT_KEY_PAGE_UP; break;
932 case XK_Next: special = GLUT_KEY_PAGE_DOWN; break;
934 case XK_Home: special = GLUT_KEY_HOME; break;
936 case XK_End: special = GLUT_KEY_END; break;
938 case XK_Insert: special = GLUT_KEY_INSERT; break;
942 * Execute the callback (if one has been specified),
943 * given that the special code seems to be valid...
945 if( special_cb && (special != -1) )
947 fgSetWindow( window );
948 window->State.Modifiers = fgGetXModifiers( &event );
949 special_cb( special, event.xkey.x, event.xkey.y );
950 window->State.Modifiers = 0xffffffff;
958 break; /* XXX Should disable this event */
961 fgWarning ("Unknown X event type: %d", event.type);
966 #elif TARGET_HOST_WIN32
970 while( PeekMessage( &stMsg, NULL, 0, 0, PM_NOREMOVE ) )
972 if( GetMessage( &stMsg, NULL, 0, 0 ) == 0 )
973 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
975 TranslateMessage( &stMsg );
976 DispatchMessage( &stMsg );
981 fghCheckJoystickPolls( );
988 * Enters the freeglut processing loop.
989 * Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".
991 void FGAPIENTRY glutMainLoop( void )
993 #if TARGET_HOST_WIN32
994 SFG_Window *window = (SFG_Window *)fgStructure.Windows.First ;
997 freeglut_assert_ready;
999 #if TARGET_HOST_WIN32
1001 * Processing before the main loop: If there is a window which is open and
1002 * which has a visibility callback, call it. I know this is an ugly hack,
1003 * but I'm not sure what else to do about it. Ideally we should leave
1004 * something uninitialized in the create window code and initialize it in
1005 * the main loop, and have that initialization create a "WM_ACTIVATE"
1006 * message. Then we would put the visibility callback code in the
1007 * "case WM_ACTIVATE" block below. - John Fay -- 10/24/02
1011 if ( FETCH_WCB( *window, Visibility ) )
1013 SFG_Window *current_window = fgStructure.Window ;
1015 INVOKE_WCB( *window, Visibility, ( window->State.Visible ) );
1016 fgSetWindow( current_window );
1019 window = (SFG_Window *)window->Node.Next ;
1023 fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;
1024 while( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )
1026 glutMainLoopEvent( );
1028 if( fgStructure.Windows.First == NULL )
1029 fgState.ExecState = GLUT_EXEC_STATE_STOP;
1032 if( fgState.IdleCallback )
1033 fgState.IdleCallback( );
1040 fgExecutionState execState = fgState.ExecState;
1043 * When this loop terminates, destroy the display, state and structure
1044 * of a freeglut session, so that another glutInit() call can happen
1048 if( execState == GLUT_ACTION_EXIT )
1054 * Leaves the freeglut processing loop.
1056 void FGAPIENTRY glutLeaveMainLoop( void )
1058 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
1062 #if TARGET_HOST_WIN32
1064 * Determine a GLUT modifer mask based on MS-WINDOWS system info.
1066 int fgGetWin32Modifiers (void)
1069 ( ( ( GetKeyState( VK_LSHIFT ) < 0 ) ||
1070 ( GetKeyState( VK_RSHIFT ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
1071 ( ( ( GetKeyState( VK_LCONTROL ) < 0 ) ||
1072 ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL : 0 ) |
1073 ( ( ( GetKeyState( VK_LMENU ) < 0 ) ||
1074 ( GetKeyState( VK_RMENU ) < 0 )) ? GLUT_ACTIVE_ALT : 0 );
1078 * The window procedure for handling Win32 events
1080 LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,
1083 SFG_Window* window = fgWindowByHandle( hWnd );
1087 if ( ( window == NULL ) && ( uMsg != WM_CREATE ) )
1088 return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
1090 /* printf ( "Window %3d message <%04x> %12d %12d\n", window?window->ID:0,
1091 uMsg, wParam, lParam ); */
1096 * The window structure is passed as the creation structure paramter...
1098 window = (SFG_Window *) (((LPCREATESTRUCT) lParam)->lpCreateParams);
1099 assert( window != NULL );
1101 window->Window.Handle = hWnd;
1102 window->Window.Device = GetDC( hWnd );
1103 if( fgState.BuildingAMenu )
1105 unsigned int current_DisplayMode = fgState.DisplayMode;
1106 fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;
1107 fgSetupPixelFormat( window, FALSE, PFD_MAIN_PLANE );
1108 fgState.DisplayMode = current_DisplayMode;
1110 if( fgStructure.MenuContext )
1111 wglMakeCurrent( window->Window.Device,
1112 fgStructure.MenuContext->Context ) ;
1115 fgStructure.MenuContext =
1116 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
1117 fgStructure.MenuContext->Context =
1118 wglCreateContext( window->Window.Device );
1121 /* window->Window.Context = wglGetCurrentContext () ; */
1122 window->Window.Context = wglCreateContext( window->Window.Device );
1126 fgSetupPixelFormat( window, FALSE, PFD_MAIN_PLANE );
1128 if( fgState.UseCurrentContext != TRUE )
1129 window->Window.Context =
1130 wglCreateContext( window->Window.Device );
1133 window->Window.Context = wglGetCurrentContext( );
1134 if( ! window->Window.Context )
1135 window->Window.Context =
1136 wglCreateContext( window->Window.Device );
1140 window->State.NeedToResize = TRUE;
1141 ReleaseDC( window->Window.Handle, window->Window.Device );
1146 * We got resized... But check if the window has been already added...
1148 fghReshapeWindowByHandle( hWnd, LOWORD(lParam), HIWORD(lParam) );
1152 printf("WM_SETFOCUS: %p\n", window );
1153 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1157 if (LOWORD(wParam) != WA_INACTIVE)
1159 /* glutSetCursor( fgStructure.Window->State.Cursor ); */
1160 printf("WM_ACTIVATE: glutSetCursor( %p, %d)\n", window,
1161 window->State.Cursor );
1162 glutSetCursor( window->State.Cursor );
1165 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1171 * Windows seems to need reminding to erase the cursor for NONE.
1174 if ((LOWORD(lParam) == HTCLIENT) &&
1175 (fgStructure.Window->State.Cursor == GLUT_CURSOR_NONE))
1179 /* Set the cursor AND change it for this window class. */
1180 #define MAP_CURSOR(a,b) \
1182 SetCursor( LoadCursor( NULL, b ) ); \
1185 /* Nuke the cursor AND change it for this window class. */
1186 #define ZAP_CURSOR(a,b) \
1188 SetCursor( NULL ); \
1191 if( LOWORD( lParam ) == HTCLIENT )
1192 switch( window->State.Cursor )
1194 MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, IDC_ARROW );
1195 MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW, IDC_ARROW );
1196 MAP_CURSOR( GLUT_CURSOR_INFO, IDC_HELP );
1197 MAP_CURSOR( GLUT_CURSOR_DESTROY, IDC_CROSS );
1198 MAP_CURSOR( GLUT_CURSOR_HELP, IDC_HELP );
1199 MAP_CURSOR( GLUT_CURSOR_CYCLE, IDC_SIZEALL );
1200 MAP_CURSOR( GLUT_CURSOR_SPRAY, IDC_CROSS );
1201 MAP_CURSOR( GLUT_CURSOR_WAIT, IDC_WAIT );
1202 MAP_CURSOR( GLUT_CURSOR_TEXT, IDC_UPARROW );
1203 MAP_CURSOR( GLUT_CURSOR_CROSSHAIR, IDC_CROSS );
1204 /* MAP_CURSOR( GLUT_CURSOR_NONE, IDC_NO ); */
1205 ZAP_CURSOR( GLUT_CURSOR_NONE, NULL );
1208 MAP_CURSOR( GLUT_CURSOR_UP_DOWN, IDC_ARROW );
1212 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1216 window->State.Visible = TRUE;
1217 window->State.Redisplay = TRUE;
1221 /* Turn on the visibility in case it was turned off somehow */
1222 window->State.Visible = GL_TRUE;
1223 BeginPaint( hWnd, &ps );
1224 fghRedrawWindowByHandle( hWnd );
1225 EndPaint( hWnd, &ps );
1230 * Make sure we don't close a window with current context active
1232 if( fgStructure.Window == window )
1237 wglMakeCurrent( NULL, NULL );
1239 * Step through the list of windows. If the rendering context
1240 * is not being used by another window, then we delete it.
1242 for( iter = (SFG_Window *)fgStructure.Windows.First;
1244 iter = (SFG_Window *)iter->Node.Next )
1246 if( ( iter->Window.Context == window->Window.Context ) &&
1247 ( iter != window ) )
1252 wglDeleteContext( window->Window.Context );
1256 * Put on a linked list of windows to be removed after all the
1257 * callbacks have returned
1259 fgAddToWindowDestroyList( window, FALSE );
1260 DestroyWindow( hWnd );
1265 * The window already got destroyed, so don't bother with it.
1271 window->State.MouseX = LOWORD( lParam );
1272 window->State.MouseY = HIWORD( lParam );
1274 if ( window->ActiveMenu )
1276 window->State.Redisplay = TRUE;
1277 fgSetWindow ( window->ActiveMenu->ParentWindow );
1281 window->State.Modifiers = fgGetWin32Modifiers( );
1283 if( ( wParam & MK_LBUTTON ) ||
1284 ( wParam & MK_MBUTTON ) ||
1285 ( wParam & MK_RBUTTON ) )
1286 INVOKE_WCB( *window, Motion, ( window->State.MouseX,
1287 window->State.MouseY ) );
1289 INVOKE_WCB( *window, Passive, ( window->State.MouseX,
1290 window->State.MouseY ) );
1292 window->State.Modifiers = 0xffffffff;
1296 case WM_LBUTTONDOWN:
1297 case WM_MBUTTONDOWN:
1298 case WM_RBUTTONDOWN:
1303 GLboolean pressed = TRUE;
1306 window->State.MouseX = LOWORD( lParam );
1307 window->State.MouseY = HIWORD( lParam );
1311 case WM_LBUTTONDOWN:
1312 pressed = TRUE; button = GLUT_LEFT_BUTTON; break;
1313 case WM_MBUTTONDOWN:
1314 pressed = TRUE; button = GLUT_MIDDLE_BUTTON; break;
1315 case WM_RBUTTONDOWN:
1316 pressed = TRUE; button = GLUT_RIGHT_BUTTON; break;
1318 pressed = FALSE; button = GLUT_LEFT_BUTTON; break;
1320 pressed = FALSE; button = GLUT_MIDDLE_BUTTON; break;
1322 pressed = FALSE; button = GLUT_RIGHT_BUTTON; break;
1324 pressed = FALSE; button = -1; break;
1327 if( GetSystemMetrics( SM_SWAPBUTTON ) )
1328 if( button == GLUT_LEFT_BUTTON )
1329 button = GLUT_RIGHT_BUTTON;
1330 else if( button == GLUT_RIGHT_BUTTON )
1331 button = GLUT_LEFT_BUTTON;
1334 return DefWindowProc( hWnd, uMsg, lParam, wParam );
1337 * Do not execute the application's mouse callback if a
1338 * menu is hooked to this button.
1339 * In that case an appropriate private call should be generated.
1340 * Near as I can tell, this is the menu behaviour:
1341 * - Down-click the menu button, menu not active: activate
1342 * the menu with its upper left-hand corner at the mouse location.
1343 * - Down-click any button outside the menu, menu active:
1344 * deactivate the menu
1345 * - Down-click any button inside the menu, menu active:
1346 * select the menu entry and deactivate the menu
1347 * - Up-click the menu button, menu not active: nothing happens
1348 * - Up-click the menu button outside the menu, menu active:
1350 * - Up-click the menu button inside the menu, menu active:
1351 * select the menu entry and deactivate the menu
1353 /* Window has an active menu, it absorbs any mouse click */
1354 if( window->ActiveMenu )
1356 /* Inside the menu, invoke the callback and deactivate the menu*/
1357 if( fgCheckActiveMenu( window, window->ActiveMenu ) == TRUE )
1360 * Save the current window and menu and set the current
1361 * window to the window whose menu this is
1363 SFG_Window *save_window = fgStructure.Window;
1364 SFG_Menu *save_menu = fgStructure.Menu;
1365 SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
1366 fgSetWindow( parent_window );
1367 fgStructure.Menu = window->ActiveMenu;
1369 /* Execute the menu callback */
1370 fgExecuteMenuCallback( window->ActiveMenu );
1371 fgDeactivateMenu( parent_window );
1373 /* Restore the current window and menu */
1374 fgSetWindow( save_window );
1375 fgStructure.Menu = save_menu;
1377 else /* Out of menu, deactivate the menu if it's a downclick */
1379 if( pressed == TRUE )
1380 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
1384 * Let's make the window redraw as a result of the mouse
1385 * click and menu activity.
1387 if( ! window->IsMenu )
1388 window->State.Redisplay = TRUE;
1393 if ( ( window->Menu[ button ] ) && ( pressed == TRUE ) )
1395 window->State.Redisplay = TRUE;
1396 fgSetWindow( window );
1397 fgActivateMenu( window, button );
1402 if( ! FETCH_WCB( *window, Mouse ) )
1405 fgSetWindow( window );
1406 fgStructure.Window->State.Modifiers = fgGetWin32Modifiers( );
1411 pressed == TRUE ? GLUT_DOWN : GLUT_UP,
1412 window->State.MouseX,
1413 window->State.MouseY
1417 fgStructure.Window->State.Modifiers = 0xffffffff;
1422 /* Should be WM_MOUSEWHEEL but my compiler doesn't recognize it */
1424 int wheel_number = LOWORD( wParam );
1425 /* THIS IS SPECULATIVE -- John Fay, 10/2/03 */
1426 short ticks = ( short )HIWORD( wParam ) / 120;
1427 /* Should be WHEEL_DELTA instead of 120 */
1437 * The mouse cursor has moved. Remember the new mouse cursor's position
1439 /* window->State.MouseX = LOWORD( lParam ); */
1440 /* Need to adjust by window position, */
1441 /* window->State.MouseY = HIWORD( lParam ); */
1442 /* change "lParam" to other parameter */
1444 if( ! FETCH_WCB( *window, MouseWheel ) &&
1445 ! FETCH_WCB( *window, Mouse ) )
1448 fgSetWindow( window );
1449 fgStructure.Window->State.Modifiers = fgGetWin32Modifiers( );
1452 if( FETCH_WCB( *window, MouseWheel ) )
1453 INVOKE_WCB( *window, MouseWheel,
1456 window->State.MouseX,
1457 window->State.MouseY
1460 else /* No mouse wheel, call the mouse button callback twice */
1463 * XXX The below assumes that you have no more than 3 mouse
1464 * XXX buttons. Sorry.
1466 int button = wheel_number*2 + 4;
1469 INVOKE_WCB( *window, Mouse,
1470 ( button, GLUT_DOWN,
1471 window->State.MouseX, window->State.MouseY )
1473 INVOKE_WCB( *window, Mouse,
1475 window->State.MouseX, window->State.MouseX )
1479 fgStructure.Window->State.Modifiers = 0xffffffff;
1489 if( fgState.IgnoreKeyRepeat && (lParam & KF_REPEAT) )
1493 * Remember the current modifiers state. This is done here in order
1494 * to make sure the VK_DELETE keyboard callback is executed properly.
1496 window->State.Modifiers = fgGetWin32Modifiers( );
1498 GetCursorPos( &mouse_pos );
1499 ScreenToClient( window->Window.Handle, &mouse_pos );
1501 window->State.MouseX = mouse_pos.x;
1502 window->State.MouseY = mouse_pos.y;
1505 * Convert the Win32 keystroke codes to GLUTtish way
1507 # define KEY(a,b) case a: keypress = b; break;
1511 KEY( VK_F1, GLUT_KEY_F1 );
1512 KEY( VK_F2, GLUT_KEY_F2 );
1513 KEY( VK_F3, GLUT_KEY_F3 );
1514 KEY( VK_F4, GLUT_KEY_F4 );
1515 KEY( VK_F5, GLUT_KEY_F5 );
1516 KEY( VK_F6, GLUT_KEY_F6 );
1517 KEY( VK_F7, GLUT_KEY_F7 );
1518 KEY( VK_F8, GLUT_KEY_F8 );
1519 KEY( VK_F9, GLUT_KEY_F9 );
1520 KEY( VK_F10, GLUT_KEY_F10 );
1521 KEY( VK_F11, GLUT_KEY_F11 );
1522 KEY( VK_F12, GLUT_KEY_F12 );
1523 KEY( VK_PRIOR, GLUT_KEY_PAGE_UP );
1524 KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );
1525 KEY( VK_HOME, GLUT_KEY_HOME );
1526 KEY( VK_END, GLUT_KEY_END );
1527 KEY( VK_LEFT, GLUT_KEY_LEFT );
1528 KEY( VK_UP, GLUT_KEY_UP );
1529 KEY( VK_RIGHT, GLUT_KEY_RIGHT );
1530 KEY( VK_DOWN, GLUT_KEY_DOWN );
1531 KEY( VK_INSERT, GLUT_KEY_INSERT );
1535 * The delete key should be treated as an ASCII keypress:
1537 INVOKE_WCB( *window, Keyboard,
1538 ( 127, window->State.MouseX, window->State.MouseY )
1542 if( keypress != -1 )
1543 INVOKE_WCB( *window, Special,
1545 window->State.MouseX, window->State.MouseY )
1548 window->State.Modifiers = 0xffffffff;
1559 * Remember the current modifiers state. This is done here in order
1560 * to make sure the VK_DELETE keyboard callback is executed properly.
1562 window->State.Modifiers = fgGetWin32Modifiers( );
1564 GetCursorPos( &mouse_pos );
1565 ScreenToClient( window->Window.Handle, &mouse_pos );
1567 window->State.MouseX = mouse_pos.x;
1568 window->State.MouseY = mouse_pos.y;
1571 * Convert the Win32 keystroke codes to GLUTtish way.
1572 * "KEY(a,b)" was defined under "WM_KEYDOWN"
1577 KEY( VK_F1, GLUT_KEY_F1 );
1578 KEY( VK_F2, GLUT_KEY_F2 );
1579 KEY( VK_F3, GLUT_KEY_F3 );
1580 KEY( VK_F4, GLUT_KEY_F4 );
1581 KEY( VK_F5, GLUT_KEY_F5 );
1582 KEY( VK_F6, GLUT_KEY_F6 );
1583 KEY( VK_F7, GLUT_KEY_F7 );
1584 KEY( VK_F8, GLUT_KEY_F8 );
1585 KEY( VK_F9, GLUT_KEY_F9 );
1586 KEY( VK_F10, GLUT_KEY_F10 );
1587 KEY( VK_F11, GLUT_KEY_F11 );
1588 KEY( VK_F12, GLUT_KEY_F12 );
1589 KEY( VK_PRIOR, GLUT_KEY_PAGE_UP );
1590 KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );
1591 KEY( VK_HOME, GLUT_KEY_HOME );
1592 KEY( VK_END, GLUT_KEY_END );
1593 KEY( VK_LEFT, GLUT_KEY_LEFT );
1594 KEY( VK_UP, GLUT_KEY_UP );
1595 KEY( VK_RIGHT, GLUT_KEY_RIGHT );
1596 KEY( VK_DOWN, GLUT_KEY_DOWN );
1597 KEY( VK_INSERT, GLUT_KEY_INSERT );
1601 * The delete key should be treated as an ASCII keypress:
1603 INVOKE_WCB( *window, KeyboardUp,
1604 ( 127, window->State.MouseX, window->State.MouseY )
1613 GetKeyboardState( state );
1615 if( ToAscii( wParam, 0, state, code, 0 ) == 1 )
1618 INVOKE_WCB( *window, KeyboardUp,
1620 window->State.MouseX, window->State.MouseY )
1625 if( keypress != -1 )
1626 INVOKE_WCB( *window, SpecialUp,
1628 window->State.MouseX, window->State.MouseY )
1631 window->State.Modifiers = 0xffffffff;
1638 if( fgState.IgnoreKeyRepeat && (lParam & KF_REPEAT) )
1642 * XXX INVOKE_WCB() takes care of the callback-pointer check.
1643 * XXX We could just uncoditionally find/trash the Modifiers
1644 * XXX and get rid of the "if( ... ) {" and "}". Unconditinal
1645 * XXX code is simpler code. (^&
1647 if( FETCH_WCB( *window, Keyboard ) )
1649 window->State.Modifiers = fgGetWin32Modifiers( );
1650 INVOKE_WCB( *window, Keyboard,
1652 window->State.MouseX, window->State.MouseY )
1654 window->State.Modifiers = 0xffffffff;
1659 case WM_CAPTURECHANGED:
1660 /* User has finished resizing the window, force a redraw */
1661 INVOKE_WCB( *window, Display, ( ) );
1663 /*lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ) ; */
1667 * Other messages that I have seen and which are not handled already
1669 case WM_SETTEXT: /* 0x000c */
1670 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1671 /* Pass it on to "DefWindowProc" to set the window text */
1674 case WM_GETTEXT: /* 0x000d */
1675 /* Ideally we would copy the title of the window into "lParam" */
1676 /* strncpy ( (char *)lParam, "Window Title", wParam );
1677 lRet = ( wParam > 12 ) ? 12 : wParam; */
1678 /* the number of characters copied */
1679 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1682 case WM_GETTEXTLENGTH: /* 0x000e */
1683 /* Ideally we would get the length of the title of the window */
1685 /* the number of characters in "Window Title\0" (see above) */
1688 case WM_ERASEBKGND: /* 0x0014 */
1689 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1692 case WM_SYNCPAINT: /* 0x0088 */
1693 /* Another window has moved, need to update this one */
1694 window->State.Redisplay = TRUE;
1695 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1696 /* Help screen says this message must be passed to "DefWindowProc" */
1699 case WM_NCPAINT: /* 0x0085 */
1700 /* Need to update the border of this window */
1701 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1702 /* Pass it on to "DefWindowProc" to repaint a standard border */
1705 case WM_SYSCOMMAND : /* 0x0112 */
1708 * We have received a system command message. Try to act on it.
1709 * The commands are passed in through the "lParam" parameter:
1710 * Clicking on a corner to resize the window gives a "F004" message
1711 * but this is not defined in my header file.
1722 /* User has clicked on the "-" to minimize the window */
1723 /* Turn off the visibility */
1724 window->State.Visible = GL_FALSE ;
1731 case SC_NEXTWINDOW :
1734 case SC_PREVWINDOW :
1738 /* Followed very closely by a WM_CLOSE message */
1762 case SC_SCREENSAVE :
1770 /* We need to pass the message on to the operating system as well */
1771 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1776 * Handle unhandled messages
1778 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1786 /*** END OF FILE ***/