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.
28 #include <GL/freeglut.h>
29 #include "freeglut_internal.h"
33 # define VFPRINTF(s,f,a) vfprintf((s),(f),(a))
36 # define VFPRINTF(s,f,a) vfprintf((s),(f),(a))
38 # define VFPRINTF(s,f,a) _doprnt((f),(a),(s))
40 # define VFPRINTF(s,f,a)
46 typedef struct GXDisplayProperties GXDisplayProperties;
47 typedef struct GXKeyList GXKeyList;
50 typedef struct GXKeyList (*GXGETDEFAULTKEYS)(int);
51 typedef int (*GXOPENINPUT)();
53 GXGETDEFAULTKEYS GXGetDefaultKeys_ = NULL;
54 GXOPENINPUT GXOpenInput_ = NULL;
56 struct GXKeyList gxKeyList;
61 * Try to get the maximum value allowed for ints, falling back to the minimum
62 * guaranteed by ISO C99 if there is no suitable header.
68 # define INT_MAX 32767
72 #define MIN(a,b) (((a)<(b)) ? (a) : (b))
77 * TODO BEFORE THE STABLE RELEASE:
79 * There are some issues concerning window redrawing under X11, and maybe
80 * some events are not handled. The Win32 version lacks some more features,
81 * but seems acceptable for not demanding purposes.
83 * Need to investigate why the X11 version breaks out with an error when
84 * closing a window (using the window manager, not glutDestroyWindow)...
87 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
90 * Handle a window configuration change. When no reshape
91 * callback is hooked, the viewport size is updated to
92 * match the new window size.
94 static void fghReshapeWindow ( SFG_Window *window, int width, int height )
96 SFG_Window *current_window = fgStructure.CurrentWindow;
98 freeglut_return_if_fail( window != NULL );
101 #if TARGET_HOST_UNIX_X11
103 XResizeWindow( fgDisplay.Display, window->Window.Handle,
105 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
107 #elif TARGET_HOST_WIN32
113 * For windowed mode, get the current position of the
114 * window and resize taking the size of the frame
115 * decorations into account.
118 /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
119 GetWindowRect( window->Window.Handle, &winRect );
125 if ( window->Parent == NULL )
127 if ( ! window->IsMenu && !window->State.IsGameMode )
129 w += GetSystemMetrics( SM_CXSIZEFRAME ) * 2;
130 h += GetSystemMetrics( SM_CYSIZEFRAME ) * 2 +
131 GetSystemMetrics( SM_CYCAPTION );
137 GetWindowRect( window->Parent->Window.Handle, &parentRect );
138 x -= parentRect.left + GetSystemMetrics( SM_CXSIZEFRAME ) * 2;
139 y -= parentRect.top + GetSystemMetrics( SM_CYSIZEFRAME ) * 2 +
140 GetSystemMetrics( SM_CYCAPTION );
144 * SWP_NOACTIVATE Do not activate the window
145 * SWP_NOOWNERZORDER Do not change position in z-order
146 * SWP_NOSENDCHANGING Supress WM_WINDOWPOSCHANGING message
147 * SWP_NOZORDER Retains the current Z order (ignore 2nd param)
150 SetWindowPos( window->Window.Handle,
153 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
160 * XXX Should update {window->State.OldWidth, window->State.OldHeight}
161 * XXX to keep in lockstep with UNIX_X11 code.
163 if( FETCH_WCB( *window, Reshape ) )
164 INVOKE_WCB( *window, Reshape, ( width, height ) );
167 fgSetWindow( window );
168 glViewport( 0, 0, width, height );
172 * Force a window redraw. In Windows at least this is only a partial
173 * solution: if the window is increasing in size in either dimension,
174 * the already-drawn part does not get drawn again and things look funny.
175 * But without this we get this bad behaviour whenever we resize the
178 window->State.Redisplay = GL_TRUE;
181 fgSetWindow( current_window );
185 * Calls a window's redraw method. This is used when
186 * a redraw is forced by the incoming window messages.
188 static void fghRedrawWindow ( SFG_Window *window )
190 SFG_Window *current_window = fgStructure.CurrentWindow;
192 freeglut_return_if_fail( window );
193 freeglut_return_if_fail( FETCH_WCB ( *window, Display ) );
195 window->State.Redisplay = GL_FALSE;
197 freeglut_return_if_fail( window->State.Visible );
199 fgSetWindow( window );
201 if( window->State.NeedToResize )
209 window->State.NeedToResize = GL_FALSE;
212 INVOKE_WCB( *window, Display, ( ) );
214 fgSetWindow( current_window );
218 * A static helper function to execute display callback for a window
220 static void fghcbDisplayWindow( SFG_Window *window,
221 SFG_Enumerator *enumerator )
223 if( window->State.Redisplay &&
224 window->State.Visible )
226 window->State.Redisplay = GL_FALSE;
228 #if TARGET_HOST_UNIX_X11
229 fghRedrawWindow ( window ) ;
230 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
232 window->Window.Handle, NULL, NULL,
233 RDW_NOERASE | RDW_INTERNALPAINT | RDW_INVALIDATE | RDW_UPDATENOW
238 fgEnumSubWindows( window, fghcbDisplayWindow, enumerator );
242 * Make all windows perform a display call
244 static void fghDisplayAll( void )
246 SFG_Enumerator enumerator;
248 enumerator.found = GL_FALSE;
249 enumerator.data = NULL;
251 fgEnumWindows( fghcbDisplayWindow, &enumerator );
255 * Window enumerator callback to check for the joystick polling code
257 static void fghcbCheckJoystickPolls( SFG_Window *window,
258 SFG_Enumerator *enumerator )
260 long int checkTime = fgElapsedTime( );
262 if( window->State.JoystickLastPoll + window->State.JoystickPollRate <=
265 #if !TARGET_HOST_WINCE
266 fgJoystickPollWindow( window );
267 #endif /* !TARGET_HOST_WINCE */
268 window->State.JoystickLastPoll = checkTime;
271 fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );
275 * Check all windows for joystick polling
277 static void fghCheckJoystickPolls( void )
279 SFG_Enumerator enumerator;
281 enumerator.found = GL_FALSE;
282 enumerator.data = NULL;
284 fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );
288 * Check the global timers
290 static void fghCheckTimers( void )
292 long checkTime = fgElapsedTime( );
294 while( fgState.Timers.First )
296 SFG_Timer *timer = fgState.Timers.First;
298 if( timer->TriggerTime > checkTime )
301 fgListRemove( &fgState.Timers, &timer->Node );
302 fgListAppend( &fgState.FreeTimers, &timer->Node );
304 timer->Callback( timer->ID );
311 long fgElapsedTime( void )
313 if ( fgState.Time.Set )
315 #if TARGET_HOST_UNIX_X11
319 gettimeofday( &now, NULL );
321 elapsed = (now.tv_usec - fgState.Time.Value.tv_usec) / 1000;
322 elapsed += (now.tv_sec - fgState.Time.Value.tv_sec) * 1000;
325 #elif TARGET_HOST_WIN32
326 return timeGetTime() - fgState.Time.Value;
327 #elif TARGET_HOST_WINCE
328 return GetTickCount() - fgState.Time.Value;
333 #if TARGET_HOST_UNIX_X11
334 gettimeofday( &fgState.Time.Value, NULL );
335 #elif TARGET_HOST_WIN32
336 fgState.Time.Value = timeGetTime ();
337 #elif TARGET_HOST_WINCE
338 fgState.Time.Value = GetTickCount();
340 fgState.Time.Set = GL_TRUE ;
349 void fgError( 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" );
363 if ( fgState.Initialised )
369 void fgWarning( const char *fmt, ... )
375 fprintf( stderr, "freeglut ");
376 if( fgState.ProgramName )
377 fprintf( stderr, "(%s): ", fgState.ProgramName );
378 VFPRINTF( stderr, fmt, ap );
379 fprintf( stderr, "\n" );
385 * Indicates whether Joystick events are being used by ANY window.
387 * The current mechanism is to walk all of the windows and ask if
388 * there is a joystick callback. We have a short-circuit early
389 * return if we find any joystick handler registered.
391 * The real way to do this is to make use of the glutTimer() API
392 * to more cleanly re-implement the joystick API. Then, this code
393 * and all other "joystick timer" code can be yanked.
396 static void fghCheckJoystickCallback( SFG_Window* w, SFG_Enumerator* e)
398 if( FETCH_WCB( *w, Joystick ) )
403 fgEnumSubWindows( w, fghCheckJoystickCallback, e );
405 static int fghHaveJoystick( void )
407 SFG_Enumerator enumerator;
409 enumerator.found = GL_FALSE;
410 enumerator.data = NULL;
411 fgEnumWindows( fghCheckJoystickCallback, &enumerator );
412 return !!enumerator.data;
414 static void fghHavePendingRedisplaysCallback( SFG_Window* w, SFG_Enumerator* e)
416 if( w->State.Redisplay )
421 fgEnumSubWindows( w, fghHavePendingRedisplaysCallback, e );
423 static int fghHavePendingRedisplays (void)
425 SFG_Enumerator enumerator;
427 enumerator.found = GL_FALSE;
428 enumerator.data = NULL;
429 fgEnumWindows( fghHavePendingRedisplaysCallback, &enumerator );
430 return !!enumerator.data;
433 * Returns the number of GLUT ticks (milliseconds) till the next timer event.
435 static long fghNextTimer( void )
438 SFG_Timer *timer = fgState.Timers.First;
441 ret = timer->TriggerTime - fgElapsedTime();
448 * Does the magic required to relinquish the CPU until something interesting
451 static void fghSleepForEvents( void )
455 if( fgState.IdleCallback || fghHavePendingRedisplays( ) )
458 msec = fghNextTimer( );
459 /* XXX Use GLUT timers for joysticks... */
460 /* XXX Dumb; forces granularity to .01sec */
461 if( fghHaveJoystick( ) && ( msec > 10 ) )
464 #if TARGET_HOST_UNIX_X11
466 * Possibly due to aggressive use of XFlush() and friends,
467 * it is possible to have our socket drained but still have
468 * unprocessed events. (Or, this may just be normal with
469 * X, anyway?) We do non-trivial processing of X events
470 * after the event-reading loop, in any case, so we
471 * need to allow that we may have an empty socket but non-
474 if( ! XPending( fgDisplay.Display ) )
481 socket = ConnectionNumber( fgDisplay.Display );
483 FD_SET( socket, &fdset );
484 wait.tv_sec = msec / 1000;
485 wait.tv_usec = (msec % 1000) * 1000;
486 err = select( socket+1, &fdset, NULL, NULL, &wait );
488 if( ( -1 == err ) && ( errno != EINTR ) )
489 fgWarning ( "freeglut select() error: %d", errno );
491 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
492 MsgWaitForMultipleObjects( 0, NULL, FALSE, msec, QS_ALLEVENTS );
496 #if TARGET_HOST_UNIX_X11
498 * Returns GLUT modifier mask for an XEvent.
500 static int fghGetXModifiers( XEvent *event )
504 if( event->xkey.state & ( ShiftMask | LockMask ) )
505 ret |= GLUT_ACTIVE_SHIFT;
506 if( event->xkey.state & ControlMask )
507 ret |= GLUT_ACTIVE_CTRL;
508 if( event->xkey.state & Mod1Mask )
509 ret |= GLUT_ACTIVE_ALT;
516 #if TARGET_HOST_UNIX_X11 && _DEBUG
518 static const char* fghTypeToString( int type )
521 case KeyPress: return "KeyPress";
522 case KeyRelease: return "KeyRelease";
523 case ButtonPress: return "ButtonPress";
524 case ButtonRelease: return "ButtonRelease";
525 case MotionNotify: return "MotionNotify";
526 case EnterNotify: return "EnterNotify";
527 case LeaveNotify: return "LeaveNotify";
528 case FocusIn: return "FocusIn";
529 case FocusOut: return "FocusOut";
530 case KeymapNotify: return "KeymapNotify";
531 case Expose: return "Expose";
532 case GraphicsExpose: return "GraphicsExpose";
533 case NoExpose: return "NoExpose";
534 case VisibilityNotify: return "VisibilityNotify";
535 case CreateNotify: return "CreateNotify";
536 case DestroyNotify: return "DestroyNotify";
537 case UnmapNotify: return "UnmapNotify";
538 case MapNotify: return "MapNotify";
539 case MapRequest: return "MapRequest";
540 case ReparentNotify: return "ReparentNotify";
541 case ConfigureNotify: return "ConfigureNotify";
542 case ConfigureRequest: return "ConfigureRequest";
543 case GravityNotify: return "GravityNotify";
544 case ResizeRequest: return "ResizeRequest";
545 case CirculateNotify: return "CirculateNotify";
546 case CirculateRequest: return "CirculateRequest";
547 case PropertyNotify: return "PropertyNotify";
548 case SelectionClear: return "SelectionClear";
549 case SelectionRequest: return "SelectionRequest";
550 case SelectionNotify: return "SelectionNotify";
551 case ColormapNotify: return "ColormapNotify";
552 case ClientMessage: return "ClientMessage";
553 case MappingNotify: return "MappingNotify";
554 default: return "UNKNOWN";
558 static const char* fghBoolToString( Bool b )
560 return b == False ? "False" : "True";
563 static const char* fghNotifyHintToString( char is_hint )
566 case NotifyNormal: return "NotifyNormal";
567 case NotifyHint: return "NotifyHint";
568 default: return "UNKNOWN";
572 static const char* fghNotifyModeToString( int mode )
575 case NotifyNormal: return "NotifyNormal";
576 case NotifyGrab: return "NotifyGrab";
577 case NotifyUngrab: return "NotifyUngrab";
578 case NotifyWhileGrabbed: return "NotifyWhileGrabbed";
579 default: return "UNKNOWN";
583 static const char* fghNotifyDetailToString( int detail )
586 case NotifyAncestor: return "NotifyAncestor";
587 case NotifyVirtual: return "NotifyVirtual";
588 case NotifyInferior: return "NotifyInferior";
589 case NotifyNonlinear: return "NotifyNonlinear";
590 case NotifyNonlinearVirtual: return "NotifyNonlinearVirtual";
591 case NotifyPointer: return "NotifyPointer";
592 case NotifyPointerRoot: return "NotifyPointerRoot";
593 case NotifyDetailNone: return "NotifyDetailNone";
594 default: return "UNKNOWN";
598 static const char* fghVisibilityToString( int state ) {
600 case VisibilityUnobscured: return "VisibilityUnobscured";
601 case VisibilityPartiallyObscured: return "VisibilityPartiallyObscured";
602 case VisibilityFullyObscured: return "VisibilityFullyObscured";
603 default: return "UNKNOWN";
607 static const char* fghConfigureDetailToString( int detail )
610 case Above: return "Above";
611 case Below: return "Below";
612 case TopIf: return "TopIf";
613 case BottomIf: return "BottomIf";
614 case Opposite: return "Opposite";
615 default: return "UNKNOWN";
619 static const char* fghPlaceToString( int place )
622 case PlaceOnTop: return "PlaceOnTop";
623 case PlaceOnBottom: return "PlaceOnBottom";
624 default: return "UNKNOWN";
628 static const char* fghMappingRequestToString( int request )
631 case MappingModifier: return "MappingModifier";
632 case MappingKeyboard: return "MappingKeyboard";
633 case MappingPointer: return "MappingPointer";
634 default: return "UNKNOWN";
638 static const char* fghPropertyStateToString( int state )
641 case PropertyNewValue: return "PropertyNewValue";
642 case PropertyDelete: return "PropertyDelete";
643 default: return "UNKNOWN";
647 static const char* fghColormapStateToString( int state )
650 case ColormapUninstalled: return "ColormapUninstalled";
651 case ColormapInstalled: return "ColormapInstalled";
652 default: return "UNKNOWN";
656 static void fghPrintEvent( XEvent *event )
658 switch( event->type ) {
662 XKeyEvent *e = &event->xkey;
663 fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "
664 "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "
665 "keycode=%u, same_screen=%s", fghTypeToString( e->type ),
666 e->window, e->root, e->subwindow, (unsigned long)e->time,
667 e->x, e->y, e->x_root, e->y_root, e->state, e->keycode,
668 fghBoolToString( e->same_screen ) );
673 case ButtonRelease: {
674 XButtonEvent *e = &event->xbutton;
675 fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "
676 "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "
677 "button=%u, same_screen=%d", fghTypeToString( e->type ),
678 e->window, e->root, e->subwindow, (unsigned long)e->time,
679 e->x, e->y, e->x_root, e->y_root, e->state, e->button,
680 fghBoolToString( e->same_screen ) );
685 XMotionEvent *e = &event->xmotion;
686 fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "
687 "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "
688 "is_hint=%s, same_screen=%d", fghTypeToString( e->type ),
689 e->window, e->root, e->subwindow, (unsigned long)e->time,
690 e->x, e->y, e->x_root, e->y_root, e->state,
691 fghNotifyHintToString( e->is_hint ),
692 fghBoolToString( e->same_screen ) );
698 XCrossingEvent *e = &event->xcrossing;
699 fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "
700 "(x,y)=(%d,%d), mode=%s, detail=%s, same_screen=%d, "
701 "focus=%d, state=0x%x", fghTypeToString( e->type ),
702 e->window, e->root, e->subwindow, (unsigned long)e->time,
703 e->x, e->y, fghNotifyModeToString( e->mode ),
704 fghNotifyDetailToString( e->detail ), (int)e->same_screen,
705 (int)e->focus, e->state );
711 XFocusChangeEvent *e = &event->xfocus;
712 fgWarning( "%s: window=0x%x, mode=%s, detail=%s",
713 fghTypeToString( e->type ), e->window,
714 fghNotifyModeToString( e->mode ),
715 fghNotifyDetailToString( e->detail ) );
720 XKeymapEvent *e = &event->xkeymap;
721 char buf[32 * 2 + 1];
723 for ( i = 0; i < 32; i++ ) {
724 snprintf( &buf[ i * 2 ], sizeof( buf ) - i * 2,
725 "%02x", e->key_vector[ i ] );
728 fgWarning( "%s: %s", fghTypeToString( e->type ), buf );
733 XExposeEvent *e = &event->xexpose;
734 fgWarning( "%s: (x,y)=(%d,%d), (width,height)=(%d,%d), count=%d",
735 fghTypeToString( e->type ), e->x, e->y, e->width, e->height,
740 case GraphicsExpose: {
741 XGraphicsExposeEvent *e = &event->xgraphicsexpose;
742 fgWarning( "%s: (x,y)=(%d,%d), (width,height)=(%d,%d), count=%d, "
743 "(major_code,minor_code)=(%d,%d)",
744 fghTypeToString( e->type ), e->x, e->y, e->width, e->height,
745 e->count, e->major_code, e->minor_code );
750 XNoExposeEvent *e = &event->xnoexpose;
751 fgWarning( "%s: (major_code,minor_code)=(%d,%d)",
752 fghTypeToString( e->type ), e->major_code, e->minor_code );
756 case VisibilityNotify: {
757 XVisibilityEvent *e = &event->xvisibility;
758 fgWarning( "%s: window=0x%x, state=%s", fghTypeToString( e->type ),
759 e->window, fghVisibilityToString( e->state) );
764 XCreateWindowEvent *e = &event->xcreatewindow;
765 fgWarning( "%s: (x,y)=(%d,%d), (width,height)=(%d,%d), border_width=%d, "
766 "window=0x%x, override_redirect=%s",
767 fghTypeToString( e->type ), e->x, e->y, e->width, e->height,
768 e->border_width, e->window,
769 fghBoolToString( e->override_redirect ) );
773 case DestroyNotify: {
774 XDestroyWindowEvent *e = &event->xdestroywindow;
775 fgWarning( "%s: event=0x%x, window=0x%x",
776 fghTypeToString( e->type ), e->event, e->window );
781 XUnmapEvent *e = &event->xunmap;
782 fgWarning( "%s: event=0x%x, window=0x%x, from_configure=%s",
783 fghTypeToString( e->type ), e->event, e->window,
784 fghBoolToString( e->from_configure ) );
789 XMapEvent *e = &event->xmap;
790 fgWarning( "%s: event=0x%x, window=0x%x, override_redirect=%s",
791 fghTypeToString( e->type ), e->event, e->window,
792 fghBoolToString( e->override_redirect ) );
797 XMapRequestEvent *e = &event->xmaprequest;
798 fgWarning( "%s: parent=0x%x, window=0x%x",
799 fghTypeToString( event->type ), e->parent, e->window );
803 case ReparentNotify: {
804 XReparentEvent *e = &event->xreparent;
805 fgWarning( "%s: event=0x%x, window=0x%x, parent=0x%x, (x,y)=(%d,%d), "
806 "override_redirect=%s", fghTypeToString( e->type ),
807 e->event, e->window, e->parent, e->x, e->y,
808 fghBoolToString( e->override_redirect ) );
812 case ConfigureNotify: {
813 XConfigureEvent *e = &event->xconfigure;
814 fgWarning( "%s: event=0x%x, window=0x%x, (x,y)=(%d,%d), "
815 "(width,height)=(%d,%d), border_width=%d, above=0x%x, "
816 "override_redirect=%s", fghTypeToString( e->type ), e->event,
817 e->window, e->x, e->y, e->width, e->height, e->border_width,
818 e->above, fghBoolToString( e->override_redirect ) );
822 case ConfigureRequest: {
823 XConfigureRequestEvent *e = &event->xconfigurerequest;
824 fgWarning( "%s: parent=0x%x, window=0x%x, (x,y)=(%d,%d), "
825 "(width,height)=(%d,%d), border_width=%d, above=0x%x, "
826 "detail=%s, value_mask=%lx", fghTypeToString( e->type ),
827 e->parent, e->window, e->x, e->y, e->width, e->height,
828 e->border_width, e->above,
829 fghConfigureDetailToString( e->detail ), e->value_mask );
833 case GravityNotify: {
834 XGravityEvent *e = &event->xgravity;
835 fgWarning( "%s: event=0x%x, window=0x%x, (x,y)=(%d,%d)",
836 fghTypeToString( e->type ), e->event, e->window, e->x, e->y );
840 case ResizeRequest: {
841 XResizeRequestEvent *e = &event->xresizerequest;
842 fgWarning( "%s: window=0x%x, (width,height)=(%d,%d)",
843 fghTypeToString( e->type ), e->window, e->width, e->height );
847 case CirculateNotify: {
848 XCirculateEvent *e = &event->xcirculate;
849 fgWarning( "%s: event=0x%x, window=0x%x, place=%s",
850 fghTypeToString( e->type ), e->event, e->window,
851 fghPlaceToString( e->place ) );
855 case CirculateRequest: {
856 XCirculateRequestEvent *e = &event->xcirculaterequest;
857 fgWarning( "%s: parent=0x%x, window=0x%x, place=%s",
858 fghTypeToString( e->type ), e->parent, e->window,
859 fghPlaceToString( e->place ) );
863 case PropertyNotify: {
864 XPropertyEvent *e = &event->xproperty;
865 fgWarning( "%s: window=0x%x, atom=%lu, time=%lu, state=%s",
866 fghTypeToString( e->type ), e->window,
867 (unsigned long)e->atom, (unsigned long)e->time,
868 fghPropertyStateToString( e->state ) );
872 case SelectionClear: {
873 XSelectionClearEvent *e = &event->xselectionclear;
874 fgWarning( "%s: window=0x%x, selection=%lu, time=%lu",
875 fghTypeToString( e->type ), e->window,
876 (unsigned long)e->selection, (unsigned long)e->time );
880 case SelectionRequest: {
881 XSelectionRequestEvent *e = &event->xselectionrequest;
882 fgWarning( "%s: owner=0x%x, requestor=0x%x, selection=0x%x, "
883 "target=0x%x, property=%lu, time=%lu",
884 fghTypeToString( e->type ), e->owner, e->requestor,
885 (unsigned long)e->selection, (unsigned long)e->target,
886 (unsigned long)e->property, (unsigned long)e->time );
890 case SelectionNotify: {
891 XSelectionEvent *e = &event->xselection;
892 fgWarning( "%s: requestor=0x%x, selection=0x%x, target=0x%x, "
893 "property=%lu, time=%lu", fghTypeToString( e->type ),
894 e->requestor, (unsigned long)e->selection,
895 (unsigned long)e->target, (unsigned long)e->property,
896 (unsigned long)e->time );
900 case ColormapNotify: {
901 XColormapEvent *e = &event->xcolormap;
902 fgWarning( "%s: window=0x%x, colormap=%lu, new=%s, state=%s",
903 fghTypeToString( e->type ), e->window,
904 (unsigned long)e->colormap, fghBoolToString( e->new ),
905 fghColormapStateToString( e->state ) );
909 case ClientMessage: {
910 XClientMessageEvent *e = &event->xclient;
913 char* end = buf + sizeof( buf );
915 switch( e->format ) {
917 for ( i = 0; i < 20; i++, p += 3 ) {
918 snprintf( p, end - p, " %02x", e->data.b[ i ] );
922 for ( i = 0; i < 10; i++, p += 5 ) {
923 snprintf( p, end - p, " %04x", e->data.s[ i ] );
927 for ( i = 0; i < 5; i++, p += 9 ) {
928 snprintf( p, end - p, " %08lx", e->data.l[ i ] );
933 fgWarning( "%s: window=0x%x, message_type=%lu, format=%d, data=(%s )",
934 fghTypeToString( e->type ), e->window,
935 (unsigned long)e->message_type, e->format, buf );
939 case MappingNotify: {
940 XMappingEvent *e = &event->xmapping;
941 fgWarning( "%s: window=0x%x, request=%s, first_keycode=%d, count=%d",
942 fghTypeToString( e->type ), e->window,
943 fghMappingRequestToString( e->request ), e->first_keycode,
949 fgWarning( "%s", fghTypeToString( event->type ) );
957 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
960 * Executes a single iteration in the freeglut processing loop.
962 void FGAPIENTRY glutMainLoopEvent( void )
964 #if TARGET_HOST_UNIX_X11
968 /* This code was repeated constantly, so here it goes into a definition: */
969 #define GETWINDOW(a) \
970 window = fgWindowByHandle( event.a.window ); \
971 if( window == NULL ) \
974 #define GETMOUSE(a) \
975 window->State.MouseX = event.a.x; \
976 window->State.MouseY = event.a.y;
978 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoopEvent" );
980 while( XPending( fgDisplay.Display ) )
982 XNextEvent( fgDisplay.Display, &event );
984 fghPrintEvent( &event );
990 /* Destroy the window when the WM_DELETE_WINDOW message arrives */
991 if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.DeleteWindow )
993 GETWINDOW( xclient );
995 fgDestroyWindow ( window );
997 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
1002 else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS )
1003 fgState.ExecState = GLUT_EXEC_STATE_STOP;
1010 * CreateNotify causes a configure-event so that sub-windows are
1011 * handled compatibly with GLUT. Otherwise, your sub-windows
1012 * (in freeglut only) will not get an initial reshape event,
1013 * which can break things.
1015 * GLUT presumably does this because it generally tries to treat
1016 * sub-windows the same as windows.
1018 * XXX Technically, GETWINDOW( xconfigure ) and
1019 * XXX {event.xconfigure} may not be legit ways to get at
1020 * XXX data for CreateNotify events. In practice, the data
1021 * XXX is in a union which is laid out much the same either
1022 * XXX way. But if you want to split hairs, this isn't legit,
1023 * XXX and we should instead duplicate some code.
1026 case ConfigureNotify:
1027 GETWINDOW( xconfigure );
1029 int width = event.xconfigure.width;
1030 int height = event.xconfigure.height;
1032 if( ( width != window->State.OldWidth ) ||
1033 ( height != window->State.OldHeight ) )
1035 SFG_Window *current_window = fgStructure.CurrentWindow;
1037 window->State.OldWidth = width;
1038 window->State.OldHeight = height;
1039 if( FETCH_WCB( *window, Reshape ) )
1040 INVOKE_WCB( *window, Reshape, ( width, height ) );
1043 fgSetWindow( window );
1044 glViewport( 0, 0, width, height );
1046 glutPostRedisplay( );
1047 if( window->IsMenu )
1048 fgSetWindow( current_window );
1055 * This is sent to confirm the XDestroyWindow call.
1057 * XXX WHY is this commented out? Should we re-enable it?
1059 /* fgAddToWindowDestroyList ( window ); */
1064 * We are too dumb to process partial exposes...
1066 * XXX Well, we could do it. However, it seems to only
1067 * XXX be potentially useful for single-buffered (since
1068 * XXX double-buffered does not respect viewport when we
1069 * XXX do a buffer-swap).
1072 if( event.xexpose.count == 0 )
1074 GETWINDOW( xexpose );
1075 window->State.Redisplay = GL_TRUE;
1082 * If we never do anything with this, can we just not ask to
1083 * get these messages?
1089 * Have the client's keyboard knowledge updated (xlib.ps,
1090 * page 206, says that's a good thing to do)
1092 XRefreshKeyboardMapping( (XMappingEvent *) &event );
1095 case VisibilityNotify:
1097 GETWINDOW( xvisibility );
1099 * XXX INVOKE_WCB() does this check for us.
1101 if( ! FETCH_WCB( *window, WindowStatus ) )
1103 fgSetWindow( window );
1106 * Sending this event, the X server can notify us that the window
1107 * has just acquired one of the three possible visibility states:
1108 * VisibilityUnobscured, VisibilityPartiallyObscured or
1109 * VisibilityFullyObscured
1111 switch( event.xvisibility.state )
1113 case VisibilityUnobscured:
1114 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_RETAINED ) );
1115 window->State.Visible = GL_TRUE;
1118 case VisibilityPartiallyObscured:
1119 INVOKE_WCB( *window, WindowStatus,
1120 ( GLUT_PARTIALLY_RETAINED ) );
1121 window->State.Visible = GL_TRUE;
1124 case VisibilityFullyObscured:
1125 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_COVERED ) );
1126 window->State.Visible = GL_FALSE;
1130 fgWarning( "Unknown X visibility state: %d",
1131 event.xvisibility.state );
1139 GETWINDOW( xcrossing );
1140 GETMOUSE( xcrossing );
1141 if( ( event.type == LeaveNotify ) && window->IsMenu &&
1142 window->ActiveMenu && window->ActiveMenu->IsActive )
1143 fgUpdateMenuHighlight( window->ActiveMenu );
1145 INVOKE_WCB( *window, Entry, ( ( EnterNotify == event.type ) ?
1152 GETWINDOW( xmotion );
1153 GETMOUSE( xmotion );
1155 if( window->ActiveMenu )
1157 if( window == window->ActiveMenu->ParentWindow )
1159 window->ActiveMenu->Window->State.MouseX =
1160 event.xmotion.x_root - window->ActiveMenu->X;
1161 window->ActiveMenu->Window->State.MouseY =
1162 event.xmotion.y_root - window->ActiveMenu->Y;
1165 fgUpdateMenuHighlight( window->ActiveMenu );
1171 * XXX For more than 5 buttons, just check {event.xmotion.state},
1172 * XXX rather than a host of bit-masks? Or maybe we need to
1173 * XXX track ButtonPress/ButtonRelease events in our own
1176 #define BUTTON_MASK \
1177 ( Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask )
1178 if ( event.xmotion.state & BUTTON_MASK )
1179 INVOKE_WCB( *window, Motion, ( event.xmotion.x,
1180 event.xmotion.y ) );
1182 INVOKE_WCB( *window, Passive, ( event.xmotion.x,
1183 event.xmotion.y ) );
1190 GLboolean pressed = GL_TRUE;
1193 if( event.type == ButtonRelease )
1194 pressed = GL_FALSE ;
1197 * A mouse button has been pressed or released. Traditionally,
1198 * break if the window was found within the freeglut structures.
1200 GETWINDOW( xbutton );
1201 GETMOUSE( xbutton );
1204 * An X button (at least in XFree86) is numbered from 1.
1205 * A GLUT button is numbered from 0.
1206 * Old GLUT passed through buttons other than just the first
1207 * three, though it only gave symbolic names and official
1208 * support to the first three.
1210 button = event.xbutton.button - 1;
1213 * Do not execute the application's mouse callback if a menu
1214 * is hooked to this button. In that case an appropriate
1215 * private call should be generated.
1217 if( fgCheckActiveMenu( window, button, pressed,
1218 event.xbutton.x_root, event.xbutton.y_root ) )
1222 * Check if there is a mouse or mouse wheel callback hooked to the
1225 if( ! FETCH_WCB( *window, Mouse ) &&
1226 ! FETCH_WCB( *window, MouseWheel ) )
1229 fgState.Modifiers = fghGetXModifiers( &event );
1231 /* Finally execute the mouse or mouse wheel callback */
1232 if( ( button < glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS ) ) || ( ! FETCH_WCB( *window, MouseWheel ) ) )
1233 INVOKE_WCB( *window, Mouse, ( button,
1234 pressed ? GLUT_DOWN : GLUT_UP,
1241 * Map 4 and 5 to wheel zero; EVEN to +1, ODD to -1
1242 * " 6 and 7 " " one; ...
1244 * XXX This *should* be behind some variables/macros,
1245 * XXX since the order and numbering isn't certain
1246 * XXX See XFree86 configuration docs (even back in the
1247 * XXX 3.x days, and especially with 4.x).
1249 * XXX Note that {button} has already been decremeted
1250 * XXX in mapping from X button numbering to GLUT.
1252 int wheel_number = (button - glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS )) / 2;
1258 INVOKE_WCB( *window, MouseWheel, ( wheel_number,
1265 /* Trash the modifiers state */
1266 fgState.Modifiers = 0xffffffff;
1273 FGCBKeyboard keyboard_cb;
1274 FGCBSpecial special_cb;
1279 /* Detect auto repeated keys, if configured globally or per-window */
1281 if ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE )
1283 if (event.type==KeyRelease)
1286 * Look at X11 keystate to detect repeat mode.
1287 * While X11 says the key is actually held down, we'll ignore KeyRelease/KeyPress pairs.
1291 XQueryKeymap( fgDisplay.Display, keys ); /* Look at X11 keystate to detect repeat mode */
1293 if ( event.xkey.keycode<256 ) /* XQueryKeymap is limited to 256 keycodes */
1295 if ( keys[event.xkey.keycode>>3] & (1<<(event.xkey.keycode%8)) )
1296 window->State.KeyRepeating = GL_TRUE;
1298 window->State.KeyRepeating = GL_FALSE;
1303 window->State.KeyRepeating = GL_FALSE;
1305 /* Cease processing this event if it is auto repeated */
1307 if (window->State.KeyRepeating)
1310 if( event.type == KeyPress )
1312 keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, Keyboard ));
1313 special_cb = (FGCBSpecial) ( FETCH_WCB( *window, Special ));
1317 keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, KeyboardUp ));
1318 special_cb = (FGCBSpecial) ( FETCH_WCB( *window, SpecialUp ));
1321 /* Is there a keyboard/special callback hooked for this window? */
1322 if( keyboard_cb || special_cb )
1324 XComposeStatus composeStatus;
1325 char asciiCode[ 32 ];
1329 /* Check for the ASCII/KeySym codes associated with the event: */
1330 len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode),
1331 &keySym, &composeStatus
1334 /* GLUT API tells us to have two separate callbacks... */
1337 /* ...one for the ASCII translateable keypresses... */
1340 fgSetWindow( window );
1341 fgState.Modifiers = fghGetXModifiers( &event );
1342 keyboard_cb( asciiCode[ 0 ],
1343 event.xkey.x, event.xkey.y
1345 fgState.Modifiers = 0xffffffff;
1353 * ...and one for all the others, which need to be
1354 * translated to GLUT_KEY_Xs...
1358 case XK_F1: special = GLUT_KEY_F1; break;
1359 case XK_F2: special = GLUT_KEY_F2; break;
1360 case XK_F3: special = GLUT_KEY_F3; break;
1361 case XK_F4: special = GLUT_KEY_F4; break;
1362 case XK_F5: special = GLUT_KEY_F5; break;
1363 case XK_F6: special = GLUT_KEY_F6; break;
1364 case XK_F7: special = GLUT_KEY_F7; break;
1365 case XK_F8: special = GLUT_KEY_F8; break;
1366 case XK_F9: special = GLUT_KEY_F9; break;
1367 case XK_F10: special = GLUT_KEY_F10; break;
1368 case XK_F11: special = GLUT_KEY_F11; break;
1369 case XK_F12: special = GLUT_KEY_F12; break;
1371 case XK_Left: special = GLUT_KEY_LEFT; break;
1372 case XK_Right: special = GLUT_KEY_RIGHT; break;
1373 case XK_Up: special = GLUT_KEY_UP; break;
1374 case XK_Down: special = GLUT_KEY_DOWN; break;
1377 case XK_Prior: special = GLUT_KEY_PAGE_UP; break;
1379 case XK_Next: special = GLUT_KEY_PAGE_DOWN; break;
1381 case XK_Home: special = GLUT_KEY_HOME; break;
1383 case XK_End: special = GLUT_KEY_END; break;
1385 case XK_Insert: special = GLUT_KEY_INSERT; break;
1389 * Execute the callback (if one has been specified),
1390 * given that the special code seems to be valid...
1392 if( special_cb && (special != -1) )
1394 fgSetWindow( window );
1395 fgState.Modifiers = fghGetXModifiers( &event );
1396 special_cb( special, event.xkey.x, event.xkey.y );
1397 fgState.Modifiers = 0xffffffff;
1404 case ReparentNotify:
1405 break; /* XXX Should disable this event */
1408 fgWarning ("Unknown X event type: %d", event.type);
1413 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
1417 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoopEvent" );
1419 while( PeekMessage( &stMsg, NULL, 0, 0, PM_NOREMOVE ) )
1421 if( GetMessage( &stMsg, NULL, 0, 0 ) == 0 )
1423 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
1428 else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS )
1429 fgState.ExecState = GLUT_EXEC_STATE_STOP;
1434 TranslateMessage( &stMsg );
1435 DispatchMessage( &stMsg );
1439 if( fgState.Timers.First )
1441 fghCheckJoystickPolls( );
1448 * Enters the freeglut processing loop.
1449 * Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".
1451 void FGAPIENTRY glutMainLoop( void )
1455 #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE
1456 SFG_Window *window = (SFG_Window *)fgStructure.Windows.First ;
1459 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoop" );
1461 #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE
1463 * Processing before the main loop: If there is a window which is open and
1464 * which has a visibility callback, call it. I know this is an ugly hack,
1465 * but I'm not sure what else to do about it. Ideally we should leave
1466 * something uninitialized in the create window code and initialize it in
1467 * the main loop, and have that initialization create a "WM_ACTIVATE"
1468 * message. Then we would put the visibility callback code in the
1469 * "case WM_ACTIVATE" block below. - John Fay -- 10/24/02
1473 if ( FETCH_WCB( *window, Visibility ) )
1475 SFG_Window *current_window = fgStructure.CurrentWindow ;
1477 INVOKE_WCB( *window, Visibility, ( window->State.Visible ) );
1478 fgSetWindow( current_window );
1481 window = (SFG_Window *)window->Node.Next ;
1485 fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;
1486 while( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )
1490 glutMainLoopEvent( );
1492 * Step through the list of windows, seeing if there are any
1493 * that are not menus
1495 for( window = ( SFG_Window * )fgStructure.Windows.First;
1497 window = ( SFG_Window * )window->Node.Next )
1498 if ( ! ( window->IsMenu ) )
1502 fgState.ExecState = GLUT_EXEC_STATE_STOP;
1505 if( fgState.IdleCallback )
1507 if( fgStructure.CurrentWindow &&
1508 fgStructure.CurrentWindow->IsMenu )
1510 fgSetWindow( window );
1511 fgState.IdleCallback( );
1514 fghSleepForEvents( );
1519 * When this loop terminates, destroy the display, state and structure
1520 * of a freeglut session, so that another glutInit() call can happen
1522 * Save the "ActionOnWindowClose" because "fgDeinitialize" resets it.
1524 action = fgState.ActionOnWindowClose;
1526 if( action == GLUT_ACTION_EXIT )
1531 * Leaves the freeglut processing loop.
1533 void FGAPIENTRY glutLeaveMainLoop( void )
1535 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveMainLoop" );
1536 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
1540 #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE
1542 * Determine a GLUT modifer mask based on MS-WINDOWS system info.
1544 static int fghGetWin32Modifiers (void)
1547 ( ( ( GetKeyState( VK_LSHIFT ) < 0 ) ||
1548 ( GetKeyState( VK_RSHIFT ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
1549 ( ( ( GetKeyState( VK_LCONTROL ) < 0 ) ||
1550 ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL : 0 ) |
1551 ( ( ( GetKeyState( VK_LMENU ) < 0 ) ||
1552 ( GetKeyState( VK_RMENU ) < 0 )) ? GLUT_ACTIVE_ALT : 0 );
1556 * The window procedure for handling Win32 events
1558 LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,
1565 FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Event Handler" ) ;
1567 window = fgWindowByHandle( hWnd );
1569 if ( ( window == NULL ) && ( uMsg != WM_CREATE ) )
1570 return DefWindowProc( hWnd, uMsg, wParam, lParam );
1572 /* printf ( "Window %3d message <%04x> %12d %12d\n", window?window->ID:0,
1573 uMsg, wParam, lParam ); */
1577 /* The window structure is passed as the creation structure paramter... */
1578 window = (SFG_Window *) (((LPCREATESTRUCT) lParam)->lpCreateParams);
1579 FREEGLUT_INTERNAL_ERROR_EXIT ( ( window != NULL ), "Cannot create window",
1582 window->Window.Handle = hWnd;
1583 window->Window.Device = GetDC( hWnd );
1584 if( window->IsMenu )
1586 unsigned int current_DisplayMode = fgState.DisplayMode;
1587 fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;
1588 #if !TARGET_HOST_WINCE
1589 fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
1591 fgState.DisplayMode = current_DisplayMode;
1593 if( fgStructure.MenuContext )
1594 wglMakeCurrent( window->Window.Device,
1595 fgStructure.MenuContext->Context
1599 fgStructure.MenuContext =
1600 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
1601 fgStructure.MenuContext->Context =
1602 wglCreateContext( window->Window.Device );
1605 /* window->Window.Context = wglGetCurrentContext (); */
1606 window->Window.Context = wglCreateContext( window->Window.Device );
1610 #if !TARGET_HOST_WINCE
1611 fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
1614 if( ! fgState.UseCurrentContext )
1615 window->Window.Context =
1616 wglCreateContext( window->Window.Device );
1619 window->Window.Context = wglGetCurrentContext( );
1620 if( ! window->Window.Context )
1621 window->Window.Context =
1622 wglCreateContext( window->Window.Device );
1626 window->State.NeedToResize = GL_TRUE;
1627 window->State.Width = fgState.Size.X;
1628 window->State.Height = fgState.Size.Y;
1630 ReleaseDC( window->Window.Handle, window->Window.Device );
1632 #if TARGET_HOST_WINCE
1633 /* Take over button handling */
1635 HINSTANCE dxDllLib=LoadLibrary(_T("gx.dll"));
1638 GXGetDefaultKeys_=(GXGETDEFAULTKEYS)GetProcAddress(dxDllLib, _T("?GXGetDefaultKeys@@YA?AUGXKeyList@@H@Z"));
1639 GXOpenInput_=(GXOPENINPUT)GetProcAddress(dxDllLib, _T("?GXOpenInput@@YAHXZ"));
1644 if(GXGetDefaultKeys_)
1645 gxKeyList = (*GXGetDefaultKeys_)(GX_LANDSCAPEKEYS);
1648 #endif /* TARGET_HOST_WINCE */
1653 * If the window is visible, then it is the user manually resizing it.
1654 * If it is not, then it is the system sending us a dummy resize with
1655 * zero dimensions on a "glutIconifyWindow" call.
1657 if( window->State.Visible )
1659 window->State.NeedToResize = GL_TRUE;
1660 #if TARGET_HOST_WINCE
1661 window->State.Width = HIWORD(lParam);
1662 window->State.Height = LOWORD(lParam);
1664 window->State.Width = LOWORD(lParam);
1665 window->State.Height = HIWORD(lParam);
1666 #endif /* TARGET_HOST_WINCE */
1672 /* printf("WM_SETFOCUS: %p\n", window ); */
1673 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1677 if (LOWORD(wParam) != WA_INACTIVE)
1679 /* printf("WM_ACTIVATE: fgSetCursor( %p, %d)\n", window,
1680 window->State.Cursor ); */
1681 fgSetCursor( window, window->State.Cursor );
1684 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1689 /* printf ( "Cursor event %x %x %x %x\n", window, window->State.Cursor, lParam, wParam ) ; */
1690 if( LOWORD( lParam ) == HTCLIENT )
1691 fgSetCursor ( window, window->State.Cursor ) ;
1693 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1697 window->State.Visible = GL_TRUE;
1698 window->State.Redisplay = GL_TRUE;
1702 /* Turn on the visibility in case it was turned off somehow */
1703 window->State.Visible = GL_TRUE;
1704 BeginPaint( hWnd, &ps );
1705 fghRedrawWindow( window );
1706 EndPaint( hWnd, &ps );
1710 fgDestroyWindow ( window );
1711 if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
1717 * The window already got destroyed, so don't bother with it.
1721 /* XXX For a future patch: we need a mouse entry event. Unfortunately Windows
1722 * XXX doesn't give us one, so we will probably need a "MouseInWindow" flag in
1723 * XXX the SFG_Window structure. Set it to true to begin with and then have the
1724 * XXX WM_MOUSELEAVE code set it to false. Then when we get a WM_MOUSEMOVE event,
1725 * XXX if the flag is false we invoke the Entry callback and set the flag to true.
1727 case 0x02a2: /* This is the message we get when the mouse is leaving the window */
1728 if( window->IsMenu &&
1729 window->ActiveMenu && window->ActiveMenu->IsActive )
1730 fgUpdateMenuHighlight( window->ActiveMenu );
1732 INVOKE_WCB( *window, Entry, ( GLUT_LEFT ) );
1737 #if TARGET_HOST_WINCE
1738 window->State.MouseX = 320-HIWORD( lParam );
1739 window->State.MouseY = LOWORD( lParam );
1741 window->State.MouseX = LOWORD( lParam );
1742 window->State.MouseY = HIWORD( lParam );
1743 #endif /* TARGET_HOST_WINCE */
1744 /* Restrict to [-32768, 32767] to match X11 behaviour */
1745 /* See comment in "freeglut_developer" mailing list 10/4/04 */
1746 if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;
1747 if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;
1749 if ( window->ActiveMenu )
1751 fgUpdateMenuHighlight( window->ActiveMenu );
1755 fgState.Modifiers = fghGetWin32Modifiers( );
1757 if( ( wParam & MK_LBUTTON ) ||
1758 ( wParam & MK_MBUTTON ) ||
1759 ( wParam & MK_RBUTTON ) )
1760 INVOKE_WCB( *window, Motion, ( window->State.MouseX,
1761 window->State.MouseY ) );
1763 INVOKE_WCB( *window, Passive, ( window->State.MouseX,
1764 window->State.MouseY ) );
1766 fgState.Modifiers = 0xffffffff;
1770 case WM_LBUTTONDOWN:
1771 case WM_MBUTTONDOWN:
1772 case WM_RBUTTONDOWN:
1777 GLboolean pressed = GL_TRUE;
1780 #if TARGET_HOST_WINCE
1781 window->State.MouseX = 320-HIWORD( lParam );
1782 window->State.MouseY = LOWORD( lParam );
1784 window->State.MouseX = LOWORD( lParam );
1785 window->State.MouseY = HIWORD( lParam );
1786 #endif /* TARGET_HOST_WINCE */
1788 /* Restrict to [-32768, 32767] to match X11 behaviour */
1789 /* See comment in "freeglut_developer" mailing list 10/4/04 */
1790 if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;
1791 if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;
1795 case WM_LBUTTONDOWN:
1797 button = GLUT_LEFT_BUTTON;
1799 case WM_MBUTTONDOWN:
1801 button = GLUT_MIDDLE_BUTTON;
1803 case WM_RBUTTONDOWN:
1805 button = GLUT_RIGHT_BUTTON;
1809 button = GLUT_LEFT_BUTTON;
1813 button = GLUT_MIDDLE_BUTTON;
1817 button = GLUT_RIGHT_BUTTON;
1825 #if !TARGET_HOST_WINCE
1826 if( GetSystemMetrics( SM_SWAPBUTTON ) )
1828 if( button == GLUT_LEFT_BUTTON )
1829 button = GLUT_RIGHT_BUTTON;
1831 if( button == GLUT_RIGHT_BUTTON )
1832 button = GLUT_LEFT_BUTTON;
1834 #endif /* !TARGET_HOST_WINCE */
1837 return DefWindowProc( hWnd, uMsg, lParam, wParam );
1840 * Do not execute the application's mouse callback if a menu
1841 * is hooked to this button. In that case an appropriate
1842 * private call should be generated.
1844 if( fgCheckActiveMenu( window, button, pressed,
1845 window->State.MouseX, window->State.MouseY ) )
1848 /* Set capture so that the window captures all the mouse messages */
1850 * XXX - Multiple button support: Under X11, the mouse is not released
1851 * XXX - from the window until all buttons have been released, even if the
1852 * XXX - user presses a button in another window. This will take more
1853 * XXX - code changes than I am up to at the moment (10/5/04). The present
1854 * XXX - is a 90 percent solution.
1856 if ( pressed == GL_TRUE )
1857 SetCapture ( window->Window.Handle ) ;
1861 if( ! FETCH_WCB( *window, Mouse ) )
1864 fgSetWindow( window );
1865 fgState.Modifiers = fghGetWin32Modifiers( );
1870 pressed ? GLUT_DOWN : GLUT_UP,
1871 window->State.MouseX,
1872 window->State.MouseY
1876 fgState.Modifiers = 0xffffffff;
1881 /* Should be WM_MOUSEWHEEL but my compiler doesn't recognize it */
1884 * XXX THIS IS SPECULATIVE -- John Fay, 10/2/03
1885 * XXX Should use WHEEL_DELTA instead of 120
1887 int wheel_number = LOWORD( wParam );
1888 short ticks = ( short )HIWORD( wParam ) / 120;
1898 * The mouse cursor has moved. Remember the new mouse cursor's position
1900 /* window->State.MouseX = LOWORD( lParam ); */
1901 /* Need to adjust by window position, */
1902 /* window->State.MouseY = HIWORD( lParam ); */
1903 /* change "lParam" to other parameter */
1905 if( ! FETCH_WCB( *window, MouseWheel ) &&
1906 ! FETCH_WCB( *window, Mouse ) )
1909 fgSetWindow( window );
1910 fgState.Modifiers = fghGetWin32Modifiers( );
1913 if( FETCH_WCB( *window, MouseWheel ) )
1914 INVOKE_WCB( *window, MouseWheel,
1917 window->State.MouseX,
1918 window->State.MouseY
1921 else /* No mouse wheel, call the mouse button callback twice */
1924 * Map wheel zero to button 3 and 4; +1 to 3, -1 to 4
1925 * " " one +1 to 5, -1 to 6, ...
1927 * XXX The below assumes that you have no more than 3 mouse
1928 * XXX buttons. Sorry.
1930 int button = wheel_number * 2 + 3;
1933 INVOKE_WCB( *window, Mouse,
1934 ( button, GLUT_DOWN,
1935 window->State.MouseX, window->State.MouseY )
1937 INVOKE_WCB( *window, Mouse,
1939 window->State.MouseX, window->State.MouseY )
1943 fgState.Modifiers = 0xffffffff;
1953 if( ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE ) && (HIWORD(lParam) & KF_REPEAT) )
1957 * Remember the current modifiers state. This is done here in order
1958 * to make sure the VK_DELETE keyboard callback is executed properly.
1960 fgState.Modifiers = fghGetWin32Modifiers( );
1962 GetCursorPos( &mouse_pos );
1963 ScreenToClient( window->Window.Handle, &mouse_pos );
1965 window->State.MouseX = mouse_pos.x;
1966 window->State.MouseY = mouse_pos.y;
1968 /* Convert the Win32 keystroke codes to GLUTtish way */
1969 # define KEY(a,b) case a: keypress = b; break;
1973 KEY( VK_F1, GLUT_KEY_F1 );
1974 KEY( VK_F2, GLUT_KEY_F2 );
1975 KEY( VK_F3, GLUT_KEY_F3 );
1976 KEY( VK_F4, GLUT_KEY_F4 );
1977 KEY( VK_F5, GLUT_KEY_F5 );
1978 KEY( VK_F6, GLUT_KEY_F6 );
1979 KEY( VK_F7, GLUT_KEY_F7 );
1980 KEY( VK_F8, GLUT_KEY_F8 );
1981 KEY( VK_F9, GLUT_KEY_F9 );
1982 KEY( VK_F10, GLUT_KEY_F10 );
1983 KEY( VK_F11, GLUT_KEY_F11 );
1984 KEY( VK_F12, GLUT_KEY_F12 );
1985 KEY( VK_PRIOR, GLUT_KEY_PAGE_UP );
1986 KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );
1987 KEY( VK_HOME, GLUT_KEY_HOME );
1988 KEY( VK_END, GLUT_KEY_END );
1989 KEY( VK_LEFT, GLUT_KEY_LEFT );
1990 KEY( VK_UP, GLUT_KEY_UP );
1991 KEY( VK_RIGHT, GLUT_KEY_RIGHT );
1992 KEY( VK_DOWN, GLUT_KEY_DOWN );
1993 KEY( VK_INSERT, GLUT_KEY_INSERT );
1996 /* The delete key should be treated as an ASCII keypress: */
1997 INVOKE_WCB( *window, Keyboard,
1998 ( 127, window->State.MouseX, window->State.MouseY )
2002 #if TARGET_HOST_WINCE
2003 if(!(lParam & 0x40000000)) /* Prevent auto-repeat */
2005 if(wParam==(unsigned)gxKeyList.vkRight)
2006 keypress = GLUT_KEY_RIGHT;
2007 else if(wParam==(unsigned)gxKeyList.vkLeft)
2008 keypress = GLUT_KEY_LEFT;
2009 else if(wParam==(unsigned)gxKeyList.vkUp)
2010 keypress = GLUT_KEY_UP;
2011 else if(wParam==(unsigned)gxKeyList.vkDown)
2012 keypress = GLUT_KEY_DOWN;
2013 else if(wParam==(unsigned)gxKeyList.vkA)
2014 keypress = GLUT_KEY_F1;
2015 else if(wParam==(unsigned)gxKeyList.vkB)
2016 keypress = GLUT_KEY_F2;
2017 else if(wParam==(unsigned)gxKeyList.vkC)
2018 keypress = GLUT_KEY_F3;
2019 else if(wParam==(unsigned)gxKeyList.vkStart)
2020 keypress = GLUT_KEY_F4;
2024 if( keypress != -1 )
2025 INVOKE_WCB( *window, Special,
2027 window->State.MouseX, window->State.MouseY )
2030 fgState.Modifiers = 0xffffffff;
2041 * Remember the current modifiers state. This is done here in order
2042 * to make sure the VK_DELETE keyboard callback is executed properly.
2044 fgState.Modifiers = fghGetWin32Modifiers( );
2046 GetCursorPos( &mouse_pos );
2047 ScreenToClient( window->Window.Handle, &mouse_pos );
2049 window->State.MouseX = mouse_pos.x;
2050 window->State.MouseY = mouse_pos.y;
2053 * Convert the Win32 keystroke codes to GLUTtish way.
2054 * "KEY(a,b)" was defined under "WM_KEYDOWN"
2059 KEY( VK_F1, GLUT_KEY_F1 );
2060 KEY( VK_F2, GLUT_KEY_F2 );
2061 KEY( VK_F3, GLUT_KEY_F3 );
2062 KEY( VK_F4, GLUT_KEY_F4 );
2063 KEY( VK_F5, GLUT_KEY_F5 );
2064 KEY( VK_F6, GLUT_KEY_F6 );
2065 KEY( VK_F7, GLUT_KEY_F7 );
2066 KEY( VK_F8, GLUT_KEY_F8 );
2067 KEY( VK_F9, GLUT_KEY_F9 );
2068 KEY( VK_F10, GLUT_KEY_F10 );
2069 KEY( VK_F11, GLUT_KEY_F11 );
2070 KEY( VK_F12, GLUT_KEY_F12 );
2071 KEY( VK_PRIOR, GLUT_KEY_PAGE_UP );
2072 KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );
2073 KEY( VK_HOME, GLUT_KEY_HOME );
2074 KEY( VK_END, GLUT_KEY_END );
2075 KEY( VK_LEFT, GLUT_KEY_LEFT );
2076 KEY( VK_UP, GLUT_KEY_UP );
2077 KEY( VK_RIGHT, GLUT_KEY_RIGHT );
2078 KEY( VK_DOWN, GLUT_KEY_DOWN );
2079 KEY( VK_INSERT, GLUT_KEY_INSERT );
2082 /* The delete key should be treated as an ASCII keypress: */
2083 INVOKE_WCB( *window, KeyboardUp,
2084 ( 127, window->State.MouseX, window->State.MouseY )
2090 #if !TARGET_HOST_WINCE
2094 GetKeyboardState( state );
2096 if( ToAscii( wParam, 0, state, code, 0 ) == 1 )
2099 INVOKE_WCB( *window, KeyboardUp,
2101 window->State.MouseX, window->State.MouseY )
2103 #endif /* !TARGET_HOST_WINCE */
2107 if( keypress != -1 )
2108 INVOKE_WCB( *window, SpecialUp,
2110 window->State.MouseX, window->State.MouseY )
2113 fgState.Modifiers = 0xffffffff;
2120 if( (fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE) && (HIWORD(lParam) & KF_REPEAT) )
2123 fgState.Modifiers = fghGetWin32Modifiers( );
2124 INVOKE_WCB( *window, Keyboard,
2126 window->State.MouseX, window->State.MouseY )
2128 fgState.Modifiers = 0xffffffff;
2132 case WM_CAPTURECHANGED:
2133 /* User has finished resizing the window, force a redraw */
2134 INVOKE_WCB( *window, Display, ( ) );
2136 /*lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ); */
2139 /* Other messages that I have seen and which are not handled already */
2140 case WM_SETTEXT: /* 0x000c */
2141 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2142 /* Pass it on to "DefWindowProc" to set the window text */
2145 case WM_GETTEXT: /* 0x000d */
2146 /* Ideally we would copy the title of the window into "lParam" */
2147 /* strncpy ( (char *)lParam, "Window Title", wParam );
2148 lRet = ( wParam > 12 ) ? 12 : wParam; */
2149 /* the number of characters copied */
2150 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2153 case WM_GETTEXTLENGTH: /* 0x000e */
2154 /* Ideally we would get the length of the title of the window */
2156 /* the number of characters in "Window Title\0" (see above) */
2159 case WM_ERASEBKGND: /* 0x0014 */
2160 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2163 #if !TARGET_HOST_WINCE
2164 case WM_SYNCPAINT: /* 0x0088 */
2165 /* Another window has moved, need to update this one */
2166 window->State.Redisplay = GL_TRUE;
2167 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2168 /* Help screen says this message must be passed to "DefWindowProc" */
2171 case WM_NCPAINT: /* 0x0085 */
2172 /* Need to update the border of this window */
2173 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2174 /* Pass it on to "DefWindowProc" to repaint a standard border */
2177 case WM_SYSCOMMAND : /* 0x0112 */
2180 * We have received a system command message. Try to act on it.
2181 * The commands are passed in through the "wParam" parameter:
2182 * The least significant digit seems to be which edge of the window
2183 * is being used for a resize event:
2187 * Congratulations and thanks to Richard Rauch for figuring this out..
2189 switch ( wParam & 0xfff0 )
2198 /* User has clicked on the "-" to minimize the window */
2199 /* Turn off the visibility */
2200 window->State.Visible = GL_FALSE ;
2207 case SC_NEXTWINDOW :
2210 case SC_PREVWINDOW :
2214 /* Followed very closely by a WM_CLOSE message */
2238 case SC_SCREENSAVE :
2246 fgWarning( "Unknown wParam type 0x%x", wParam );
2251 #endif /* !TARGET_HOST_WINCE */
2253 /* We need to pass the message on to the operating system as well */
2254 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2258 /* Handle unhandled messages */
2259 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2267 /*** END OF FILE ***/