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))
35 # define VFPRINTF(s,f,a) _doprnt((f),(a),(s))
37 # define VFPRINTF(s,f,a)
42 typedef struct GXDisplayProperties GXDisplayProperties;
43 typedef struct GXKeyList GXKeyList;
46 typedef struct GXKeyList (*GXGETDEFAULTKEYS)(int);
47 typedef int (*GXOPENINPUT)();
49 GXGETDEFAULTKEYS GXGetDefaultKeys_ = NULL;
50 GXOPENINPUT GXOpenInput_ = NULL;
52 struct GXKeyList gxKeyList;
54 #endif /* _WIN32_WCE */
57 * Try to get the maximum value allowed for ints, falling back to the minimum
58 * guaranteed by ISO C99 if there is no suitable header.
64 # define INT_MAX 32767
68 # define MIN(a,b) (((a)<(b)) ? (a) : (b))
73 * TODO BEFORE THE STABLE RELEASE:
75 * There are some issues concerning window redrawing under X11, and maybe
76 * some events are not handled. The Win32 version lacks some more features,
77 * but seems acceptable for not demanding purposes.
79 * Need to investigate why the X11 version breaks out with an error when
80 * closing a window (using the window manager, not glutDestroyWindow)...
83 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
86 * Handle a window configuration change. When no reshape
87 * callback is hooked, the viewport size is updated to
88 * match the new window size.
90 static void fghReshapeWindow ( SFG_Window *window, int width, int height )
92 SFG_Window *current_window = fgStructure.CurrentWindow;
94 freeglut_return_if_fail( window != NULL );
97 #if TARGET_HOST_POSIX_X11
99 XResizeWindow( fgDisplay.Display, window->Window.Handle,
101 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
103 #elif TARGET_HOST_MS_WINDOWS && !defined(_WIN32_WCE)
109 * For windowed mode, get the current position of the
110 * window and resize taking the size of the frame
111 * decorations into account.
114 /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
115 GetWindowRect( window->Window.Handle, &winRect );
121 if ( window->Parent == NULL )
123 if ( ! window->IsMenu && (window != fgStructure.GameModeWindow) )
125 w += GetSystemMetrics( SM_CXSIZEFRAME ) * 2;
126 h += GetSystemMetrics( SM_CYSIZEFRAME ) * 2 +
127 GetSystemMetrics( SM_CYCAPTION );
133 GetWindowRect( window->Parent->Window.Handle, &parentRect );
134 x -= parentRect.left + GetSystemMetrics( SM_CXSIZEFRAME ) * 2;
135 y -= parentRect.top + GetSystemMetrics( SM_CYSIZEFRAME ) * 2 +
136 GetSystemMetrics( SM_CYCAPTION );
140 * SWP_NOACTIVATE Do not activate the window
141 * SWP_NOOWNERZORDER Do not change position in z-order
142 * SWP_NOSENDCHANGING Supress WM_WINDOWPOSCHANGING message
143 * SWP_NOZORDER Retains the current Z order (ignore 2nd param)
146 SetWindowPos( window->Window.Handle,
149 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
156 * XXX Should update {window->State.OldWidth, window->State.OldHeight}
157 * XXX to keep in lockstep with POSIX_X11 code.
159 if( FETCH_WCB( *window, Reshape ) )
160 INVOKE_WCB( *window, Reshape, ( width, height ) );
163 fgSetWindow( window );
164 glViewport( 0, 0, width, height );
168 * Force a window redraw. In Windows at least this is only a partial
169 * solution: if the window is increasing in size in either dimension,
170 * the already-drawn part does not get drawn again and things look funny.
171 * But without this we get this bad behaviour whenever we resize the
174 window->State.Redisplay = GL_TRUE;
177 fgSetWindow( current_window );
181 * Calls a window's redraw method. This is used when
182 * a redraw is forced by the incoming window messages.
184 static void fghRedrawWindow ( SFG_Window *window )
186 SFG_Window *current_window = fgStructure.CurrentWindow;
188 freeglut_return_if_fail( window );
189 freeglut_return_if_fail( FETCH_WCB ( *window, Display ) );
191 window->State.Redisplay = GL_FALSE;
193 freeglut_return_if_fail( window->State.Visible );
195 fgSetWindow( window );
197 if( window->State.NeedToResize )
205 window->State.NeedToResize = GL_FALSE;
208 INVOKE_WCB( *window, Display, ( ) );
210 fgSetWindow( current_window );
214 * A static helper function to execute display callback for a window
216 static void fghcbDisplayWindow( SFG_Window *window,
217 SFG_Enumerator *enumerator )
219 if( window->State.Redisplay &&
220 window->State.Visible )
222 window->State.Redisplay = GL_FALSE;
224 #if TARGET_HOST_POSIX_X11
225 fghRedrawWindow ( window ) ;
226 #elif TARGET_HOST_MS_WINDOWS
228 window->Window.Handle, NULL, NULL,
229 RDW_NOERASE | RDW_INTERNALPAINT | RDW_INVALIDATE | RDW_UPDATENOW
234 fgEnumSubWindows( window, fghcbDisplayWindow, enumerator );
238 * Make all windows perform a display call
240 static void fghDisplayAll( void )
242 SFG_Enumerator enumerator;
244 enumerator.found = GL_FALSE;
245 enumerator.data = NULL;
247 fgEnumWindows( fghcbDisplayWindow, &enumerator );
251 * Window enumerator callback to check for the joystick polling code
253 static void fghcbCheckJoystickPolls( SFG_Window *window,
254 SFG_Enumerator *enumerator )
256 long int checkTime = fgElapsedTime( );
258 if( window->State.JoystickLastPoll + window->State.JoystickPollRate <=
261 #if !defined(_WIN32_WCE)
262 fgJoystickPollWindow( window );
263 #endif /* !defined(_WIN32_WCE) */
264 window->State.JoystickLastPoll = checkTime;
267 fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );
271 * Check all windows for joystick polling
273 static void fghCheckJoystickPolls( void )
275 SFG_Enumerator enumerator;
277 enumerator.found = GL_FALSE;
278 enumerator.data = NULL;
280 fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );
284 * Check the global timers
286 static void fghCheckTimers( void )
288 long checkTime = fgElapsedTime( );
290 while( fgState.Timers.First )
292 SFG_Timer *timer = fgState.Timers.First;
294 if( timer->TriggerTime > checkTime )
297 fgListRemove( &fgState.Timers, &timer->Node );
298 fgListAppend( &fgState.FreeTimers, &timer->Node );
300 timer->Callback( timer->ID );
305 /* Platform-dependent time in milliseconds, as an unsigned 32-bit integer.
306 * This value wraps every 49.7 days, but integer overflows cancel
307 * when subtracting an initial start time, unless the total time exceeds
308 * 32-bit, where the GLUT API return value is also overflowed.
310 unsigned long fgSystemTime(void) {
311 #if TARGET_HOST_POSIX_X11
313 gettimeofday( &now, NULL );
314 return now.tv_usec/1000 + now.tv_sec*1000;
315 #elif TARGET_HOST_MS_WINDOWS
316 # if defined(_WIN32_WCE)
317 return GetTickCount();
319 return timeGetTime();
327 long fgElapsedTime( void )
329 return (long) (fgSystemTime() - fgState.Time);
335 void fgError( const char *fmt, ... )
341 fprintf( stderr, "freeglut ");
342 if( fgState.ProgramName )
343 fprintf( stderr, "(%s): ", fgState.ProgramName );
344 VFPRINTF( stderr, fmt, ap );
345 fprintf( stderr, "\n" );
349 if ( fgState.Initialised )
355 void fgWarning( const char *fmt, ... )
361 fprintf( stderr, "freeglut ");
362 if( fgState.ProgramName )
363 fprintf( stderr, "(%s): ", fgState.ProgramName );
364 VFPRINTF( stderr, fmt, ap );
365 fprintf( stderr, "\n" );
371 * Indicates whether Joystick events are being used by ANY window.
373 * The current mechanism is to walk all of the windows and ask if
374 * there is a joystick callback. We have a short-circuit early
375 * return if we find any joystick handler registered.
377 * The real way to do this is to make use of the glutTimer() API
378 * to more cleanly re-implement the joystick API. Then, this code
379 * and all other "joystick timer" code can be yanked.
382 static void fghCheckJoystickCallback( SFG_Window* w, SFG_Enumerator* e)
384 if( FETCH_WCB( *w, Joystick ) )
389 fgEnumSubWindows( w, fghCheckJoystickCallback, e );
391 static int fghHaveJoystick( void )
393 SFG_Enumerator enumerator;
395 enumerator.found = GL_FALSE;
396 enumerator.data = NULL;
397 fgEnumWindows( fghCheckJoystickCallback, &enumerator );
398 return !!enumerator.data;
400 static void fghHavePendingRedisplaysCallback( SFG_Window* w, SFG_Enumerator* e)
402 if( w->State.Redisplay && w->State.Visible )
407 fgEnumSubWindows( w, fghHavePendingRedisplaysCallback, e );
409 static int fghHavePendingRedisplays (void)
411 SFG_Enumerator enumerator;
413 enumerator.found = GL_FALSE;
414 enumerator.data = NULL;
415 fgEnumWindows( fghHavePendingRedisplaysCallback, &enumerator );
416 return !!enumerator.data;
419 * Returns the number of GLUT ticks (milliseconds) till the next timer event.
421 static long fghNextTimer( void )
424 SFG_Timer *timer = fgState.Timers.First;
427 ret = timer->TriggerTime - fgElapsedTime();
434 * Does the magic required to relinquish the CPU until something interesting
437 static void fghSleepForEvents( void )
441 if( fgState.IdleCallback || fghHavePendingRedisplays( ) )
444 msec = fghNextTimer( );
445 /* XXX Use GLUT timers for joysticks... */
446 /* XXX Dumb; forces granularity to .01sec */
447 if( fghHaveJoystick( ) && ( msec > 10 ) )
450 #if TARGET_HOST_POSIX_X11
452 * Possibly due to aggressive use of XFlush() and friends,
453 * it is possible to have our socket drained but still have
454 * unprocessed events. (Or, this may just be normal with
455 * X, anyway?) We do non-trivial processing of X events
456 * after the event-reading loop, in any case, so we
457 * need to allow that we may have an empty socket but non-
460 if( ! XPending( fgDisplay.Display ) )
467 socket = ConnectionNumber( fgDisplay.Display );
469 FD_SET( socket, &fdset );
470 wait.tv_sec = msec / 1000;
471 wait.tv_usec = (msec % 1000) * 1000;
472 err = select( socket+1, &fdset, NULL, NULL, &wait );
474 if( ( -1 == err ) && ( errno != EINTR ) )
475 fgWarning ( "freeglut select() error: %d", errno );
477 #elif TARGET_HOST_MS_WINDOWS
478 MsgWaitForMultipleObjects( 0, NULL, FALSE, msec, QS_ALLINPUT );
482 #if TARGET_HOST_POSIX_X11
484 * Returns GLUT modifier mask for the state field of an X11 event.
486 static int fghGetXModifiers( int state )
490 if( state & ( ShiftMask | LockMask ) )
491 ret |= GLUT_ACTIVE_SHIFT;
492 if( state & ControlMask )
493 ret |= GLUT_ACTIVE_CTRL;
494 if( state & Mod1Mask )
495 ret |= GLUT_ACTIVE_ALT;
502 #if TARGET_HOST_POSIX_X11 && _DEBUG
504 static const char* fghTypeToString( int type )
507 case KeyPress: return "KeyPress";
508 case KeyRelease: return "KeyRelease";
509 case ButtonPress: return "ButtonPress";
510 case ButtonRelease: return "ButtonRelease";
511 case MotionNotify: return "MotionNotify";
512 case EnterNotify: return "EnterNotify";
513 case LeaveNotify: return "LeaveNotify";
514 case FocusIn: return "FocusIn";
515 case FocusOut: return "FocusOut";
516 case KeymapNotify: return "KeymapNotify";
517 case Expose: return "Expose";
518 case GraphicsExpose: return "GraphicsExpose";
519 case NoExpose: return "NoExpose";
520 case VisibilityNotify: return "VisibilityNotify";
521 case CreateNotify: return "CreateNotify";
522 case DestroyNotify: return "DestroyNotify";
523 case UnmapNotify: return "UnmapNotify";
524 case MapNotify: return "MapNotify";
525 case MapRequest: return "MapRequest";
526 case ReparentNotify: return "ReparentNotify";
527 case ConfigureNotify: return "ConfigureNotify";
528 case ConfigureRequest: return "ConfigureRequest";
529 case GravityNotify: return "GravityNotify";
530 case ResizeRequest: return "ResizeRequest";
531 case CirculateNotify: return "CirculateNotify";
532 case CirculateRequest: return "CirculateRequest";
533 case PropertyNotify: return "PropertyNotify";
534 case SelectionClear: return "SelectionClear";
535 case SelectionRequest: return "SelectionRequest";
536 case SelectionNotify: return "SelectionNotify";
537 case ColormapNotify: return "ColormapNotify";
538 case ClientMessage: return "ClientMessage";
539 case MappingNotify: return "MappingNotify";
540 default: return "UNKNOWN";
544 static const char* fghBoolToString( Bool b )
546 return b == False ? "False" : "True";
549 static const char* fghNotifyHintToString( char is_hint )
552 case NotifyNormal: return "NotifyNormal";
553 case NotifyHint: return "NotifyHint";
554 default: return "UNKNOWN";
558 static const char* fghNotifyModeToString( int mode )
561 case NotifyNormal: return "NotifyNormal";
562 case NotifyGrab: return "NotifyGrab";
563 case NotifyUngrab: return "NotifyUngrab";
564 case NotifyWhileGrabbed: return "NotifyWhileGrabbed";
565 default: return "UNKNOWN";
569 static const char* fghNotifyDetailToString( int detail )
572 case NotifyAncestor: return "NotifyAncestor";
573 case NotifyVirtual: return "NotifyVirtual";
574 case NotifyInferior: return "NotifyInferior";
575 case NotifyNonlinear: return "NotifyNonlinear";
576 case NotifyNonlinearVirtual: return "NotifyNonlinearVirtual";
577 case NotifyPointer: return "NotifyPointer";
578 case NotifyPointerRoot: return "NotifyPointerRoot";
579 case NotifyDetailNone: return "NotifyDetailNone";
580 default: return "UNKNOWN";
584 static const char* fghVisibilityToString( int state ) {
586 case VisibilityUnobscured: return "VisibilityUnobscured";
587 case VisibilityPartiallyObscured: return "VisibilityPartiallyObscured";
588 case VisibilityFullyObscured: return "VisibilityFullyObscured";
589 default: return "UNKNOWN";
593 static const char* fghConfigureDetailToString( int detail )
596 case Above: return "Above";
597 case Below: return "Below";
598 case TopIf: return "TopIf";
599 case BottomIf: return "BottomIf";
600 case Opposite: return "Opposite";
601 default: return "UNKNOWN";
605 static const char* fghPlaceToString( int place )
608 case PlaceOnTop: return "PlaceOnTop";
609 case PlaceOnBottom: return "PlaceOnBottom";
610 default: return "UNKNOWN";
614 static const char* fghMappingRequestToString( int request )
617 case MappingModifier: return "MappingModifier";
618 case MappingKeyboard: return "MappingKeyboard";
619 case MappingPointer: return "MappingPointer";
620 default: return "UNKNOWN";
624 static const char* fghPropertyStateToString( int state )
627 case PropertyNewValue: return "PropertyNewValue";
628 case PropertyDelete: return "PropertyDelete";
629 default: return "UNKNOWN";
633 static const char* fghColormapStateToString( int state )
636 case ColormapUninstalled: return "ColormapUninstalled";
637 case ColormapInstalled: return "ColormapInstalled";
638 default: return "UNKNOWN";
642 static void fghPrintEvent( XEvent *event )
644 switch( event->type ) {
648 XKeyEvent *e = &event->xkey;
649 fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "
650 "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "
651 "keycode=%u, same_screen=%s", fghTypeToString( e->type ),
652 e->window, e->root, e->subwindow, (unsigned long)e->time,
653 e->x, e->y, e->x_root, e->y_root, e->state, e->keycode,
654 fghBoolToString( e->same_screen ) );
659 case ButtonRelease: {
660 XButtonEvent *e = &event->xbutton;
661 fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "
662 "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "
663 "button=%u, same_screen=%d", fghTypeToString( e->type ),
664 e->window, e->root, e->subwindow, (unsigned long)e->time,
665 e->x, e->y, e->x_root, e->y_root, e->state, e->button,
666 fghBoolToString( e->same_screen ) );
671 XMotionEvent *e = &event->xmotion;
672 fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "
673 "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "
674 "is_hint=%s, same_screen=%d", fghTypeToString( e->type ),
675 e->window, e->root, e->subwindow, (unsigned long)e->time,
676 e->x, e->y, e->x_root, e->y_root, e->state,
677 fghNotifyHintToString( e->is_hint ),
678 fghBoolToString( e->same_screen ) );
684 XCrossingEvent *e = &event->xcrossing;
685 fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "
686 "(x,y)=(%d,%d), mode=%s, detail=%s, same_screen=%d, "
687 "focus=%d, state=0x%x", fghTypeToString( e->type ),
688 e->window, e->root, e->subwindow, (unsigned long)e->time,
689 e->x, e->y, fghNotifyModeToString( e->mode ),
690 fghNotifyDetailToString( e->detail ), (int)e->same_screen,
691 (int)e->focus, e->state );
697 XFocusChangeEvent *e = &event->xfocus;
698 fgWarning( "%s: window=0x%x, mode=%s, detail=%s",
699 fghTypeToString( e->type ), e->window,
700 fghNotifyModeToString( e->mode ),
701 fghNotifyDetailToString( e->detail ) );
706 XKeymapEvent *e = &event->xkeymap;
707 char buf[32 * 2 + 1];
709 for ( i = 0; i < 32; i++ ) {
710 snprintf( &buf[ i * 2 ], sizeof( buf ) - i * 2,
711 "%02x", e->key_vector[ i ] );
714 fgWarning( "%s: window=0x%x, %s", fghTypeToString( e->type ), e->window,
720 XExposeEvent *e = &event->xexpose;
721 fgWarning( "%s: window=0x%x, (x,y)=(%d,%d), (width,height)=(%d,%d), "
722 "count=%d", fghTypeToString( e->type ), e->window, e->x,
723 e->y, e->width, e->height, e->count );
727 case GraphicsExpose: {
728 XGraphicsExposeEvent *e = &event->xgraphicsexpose;
729 fgWarning( "%s: drawable=0x%x, (x,y)=(%d,%d), (width,height)=(%d,%d), "
730 "count=%d, (major_code,minor_code)=(%d,%d)",
731 fghTypeToString( e->type ), e->drawable, e->x, e->y,
732 e->width, e->height, e->count, e->major_code,
738 XNoExposeEvent *e = &event->xnoexpose;
739 fgWarning( "%s: drawable=0x%x, (major_code,minor_code)=(%d,%d)",
740 fghTypeToString( e->type ), e->drawable, e->major_code,
745 case VisibilityNotify: {
746 XVisibilityEvent *e = &event->xvisibility;
747 fgWarning( "%s: window=0x%x, state=%s", fghTypeToString( e->type ),
748 e->window, fghVisibilityToString( e->state) );
753 XCreateWindowEvent *e = &event->xcreatewindow;
754 fgWarning( "%s: (x,y)=(%d,%d), (width,height)=(%d,%d), border_width=%d, "
755 "window=0x%x, override_redirect=%s",
756 fghTypeToString( e->type ), e->x, e->y, e->width, e->height,
757 e->border_width, e->window,
758 fghBoolToString( e->override_redirect ) );
762 case DestroyNotify: {
763 XDestroyWindowEvent *e = &event->xdestroywindow;
764 fgWarning( "%s: event=0x%x, window=0x%x",
765 fghTypeToString( e->type ), e->event, e->window );
770 XUnmapEvent *e = &event->xunmap;
771 fgWarning( "%s: event=0x%x, window=0x%x, from_configure=%s",
772 fghTypeToString( e->type ), e->event, e->window,
773 fghBoolToString( e->from_configure ) );
778 XMapEvent *e = &event->xmap;
779 fgWarning( "%s: event=0x%x, window=0x%x, override_redirect=%s",
780 fghTypeToString( e->type ), e->event, e->window,
781 fghBoolToString( e->override_redirect ) );
786 XMapRequestEvent *e = &event->xmaprequest;
787 fgWarning( "%s: parent=0x%x, window=0x%x",
788 fghTypeToString( event->type ), e->parent, e->window );
792 case ReparentNotify: {
793 XReparentEvent *e = &event->xreparent;
794 fgWarning( "%s: event=0x%x, window=0x%x, parent=0x%x, (x,y)=(%d,%d), "
795 "override_redirect=%s", fghTypeToString( e->type ),
796 e->event, e->window, e->parent, e->x, e->y,
797 fghBoolToString( e->override_redirect ) );
801 case ConfigureNotify: {
802 XConfigureEvent *e = &event->xconfigure;
803 fgWarning( "%s: event=0x%x, window=0x%x, (x,y)=(%d,%d), "
804 "(width,height)=(%d,%d), border_width=%d, above=0x%x, "
805 "override_redirect=%s", fghTypeToString( e->type ), e->event,
806 e->window, e->x, e->y, e->width, e->height, e->border_width,
807 e->above, fghBoolToString( e->override_redirect ) );
811 case ConfigureRequest: {
812 XConfigureRequestEvent *e = &event->xconfigurerequest;
813 fgWarning( "%s: parent=0x%x, window=0x%x, (x,y)=(%d,%d), "
814 "(width,height)=(%d,%d), border_width=%d, above=0x%x, "
815 "detail=%s, value_mask=%lx", fghTypeToString( e->type ),
816 e->parent, e->window, e->x, e->y, e->width, e->height,
817 e->border_width, e->above,
818 fghConfigureDetailToString( e->detail ), e->value_mask );
822 case GravityNotify: {
823 XGravityEvent *e = &event->xgravity;
824 fgWarning( "%s: event=0x%x, window=0x%x, (x,y)=(%d,%d)",
825 fghTypeToString( e->type ), e->event, e->window, e->x, e->y );
829 case ResizeRequest: {
830 XResizeRequestEvent *e = &event->xresizerequest;
831 fgWarning( "%s: window=0x%x, (width,height)=(%d,%d)",
832 fghTypeToString( e->type ), e->window, e->width, e->height );
836 case CirculateNotify: {
837 XCirculateEvent *e = &event->xcirculate;
838 fgWarning( "%s: event=0x%x, window=0x%x, place=%s",
839 fghTypeToString( e->type ), e->event, e->window,
840 fghPlaceToString( e->place ) );
844 case CirculateRequest: {
845 XCirculateRequestEvent *e = &event->xcirculaterequest;
846 fgWarning( "%s: parent=0x%x, window=0x%x, place=%s",
847 fghTypeToString( e->type ), e->parent, e->window,
848 fghPlaceToString( e->place ) );
852 case PropertyNotify: {
853 XPropertyEvent *e = &event->xproperty;
854 fgWarning( "%s: window=0x%x, atom=%lu, time=%lu, state=%s",
855 fghTypeToString( e->type ), e->window,
856 (unsigned long)e->atom, (unsigned long)e->time,
857 fghPropertyStateToString( e->state ) );
861 case SelectionClear: {
862 XSelectionClearEvent *e = &event->xselectionclear;
863 fgWarning( "%s: window=0x%x, selection=%lu, time=%lu",
864 fghTypeToString( e->type ), e->window,
865 (unsigned long)e->selection, (unsigned long)e->time );
869 case SelectionRequest: {
870 XSelectionRequestEvent *e = &event->xselectionrequest;
871 fgWarning( "%s: owner=0x%x, requestor=0x%x, selection=0x%x, "
872 "target=0x%x, property=%lu, time=%lu",
873 fghTypeToString( e->type ), e->owner, e->requestor,
874 (unsigned long)e->selection, (unsigned long)e->target,
875 (unsigned long)e->property, (unsigned long)e->time );
879 case SelectionNotify: {
880 XSelectionEvent *e = &event->xselection;
881 fgWarning( "%s: requestor=0x%x, selection=0x%x, target=0x%x, "
882 "property=%lu, time=%lu", fghTypeToString( e->type ),
883 e->requestor, (unsigned long)e->selection,
884 (unsigned long)e->target, (unsigned long)e->property,
885 (unsigned long)e->time );
889 case ColormapNotify: {
890 XColormapEvent *e = &event->xcolormap;
891 fgWarning( "%s: window=0x%x, colormap=%lu, new=%s, state=%s",
892 fghTypeToString( e->type ), e->window,
893 (unsigned long)e->colormap, fghBoolToString( e->new ),
894 fghColormapStateToString( e->state ) );
898 case ClientMessage: {
899 XClientMessageEvent *e = &event->xclient;
902 char* end = buf + sizeof( buf );
904 switch( e->format ) {
906 for ( i = 0; i < 20; i++, p += 3 ) {
907 snprintf( p, end - p, " %02x", e->data.b[ i ] );
911 for ( i = 0; i < 10; i++, p += 5 ) {
912 snprintf( p, end - p, " %04x", e->data.s[ i ] );
916 for ( i = 0; i < 5; i++, p += 9 ) {
917 snprintf( p, end - p, " %08lx", e->data.l[ i ] );
922 fgWarning( "%s: window=0x%x, message_type=%lu, format=%d, data=(%s )",
923 fghTypeToString( e->type ), e->window,
924 (unsigned long)e->message_type, e->format, buf );
928 case MappingNotify: {
929 XMappingEvent *e = &event->xmapping;
930 fgWarning( "%s: window=0x%x, request=%s, first_keycode=%d, count=%d",
931 fghTypeToString( e->type ), e->window,
932 fghMappingRequestToString( e->request ), e->first_keycode,
938 fgWarning( "%s", fghTypeToString( event->type ) );
946 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
949 * Executes a single iteration in the freeglut processing loop.
951 void FGAPIENTRY glutMainLoopEvent( void )
953 #if TARGET_HOST_POSIX_X11
957 /* This code was repeated constantly, so here it goes into a definition: */
958 #define GETWINDOW(a) \
959 window = fgWindowByHandle( event.a.window ); \
960 if( window == NULL ) \
963 #define GETMOUSE(a) \
964 window->State.MouseX = event.a.x; \
965 window->State.MouseY = event.a.y;
967 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoopEvent" );
969 while( XPending( fgDisplay.Display ) )
971 XNextEvent( fgDisplay.Display, &event );
973 fghPrintEvent( &event );
979 /* Destroy the window when the WM_DELETE_WINDOW message arrives */
980 if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.DeleteWindow )
982 GETWINDOW( xclient );
984 fgDestroyWindow ( window );
986 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
991 else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS )
992 fgState.ExecState = GLUT_EXEC_STATE_STOP;
999 * CreateNotify causes a configure-event so that sub-windows are
1000 * handled compatibly with GLUT. Otherwise, your sub-windows
1001 * (in freeglut only) will not get an initial reshape event,
1002 * which can break things.
1004 * GLUT presumably does this because it generally tries to treat
1005 * sub-windows the same as windows.
1008 case ConfigureNotify:
1011 if( event.type == CreateNotify ) {
1012 GETWINDOW( xcreatewindow );
1013 width = event.xcreatewindow.width;
1014 height = event.xcreatewindow.height;
1016 GETWINDOW( xconfigure );
1017 width = event.xconfigure.width;
1018 height = event.xconfigure.height;
1021 if( ( width != window->State.OldWidth ) ||
1022 ( height != window->State.OldHeight ) )
1024 SFG_Window *current_window = fgStructure.CurrentWindow;
1026 window->State.OldWidth = width;
1027 window->State.OldHeight = height;
1028 if( FETCH_WCB( *window, Reshape ) )
1029 INVOKE_WCB( *window, Reshape, ( width, height ) );
1032 fgSetWindow( window );
1033 glViewport( 0, 0, width, height );
1035 glutPostRedisplay( );
1036 if( window->IsMenu )
1037 fgSetWindow( current_window );
1044 * This is sent to confirm the XDestroyWindow call.
1046 * XXX WHY is this commented out? Should we re-enable it?
1048 /* fgAddToWindowDestroyList ( window ); */
1053 * We are too dumb to process partial exposes...
1055 * XXX Well, we could do it. However, it seems to only
1056 * XXX be potentially useful for single-buffered (since
1057 * XXX double-buffered does not respect viewport when we
1058 * XXX do a buffer-swap).
1061 if( event.xexpose.count == 0 )
1063 GETWINDOW( xexpose );
1064 window->State.Redisplay = GL_TRUE;
1072 /* We get this when iconifying a window. */
1073 GETWINDOW( xunmap );
1074 INVOKE_WCB( *window, WindowStatus, ( GLUT_HIDDEN ) );
1075 window->State.Visible = GL_FALSE;
1080 * Have the client's keyboard knowledge updated (xlib.ps,
1081 * page 206, says that's a good thing to do)
1083 XRefreshKeyboardMapping( (XMappingEvent *) &event );
1086 case VisibilityNotify:
1089 * Sending this event, the X server can notify us that the window
1090 * has just acquired one of the three possible visibility states:
1091 * VisibilityUnobscured, VisibilityPartiallyObscured or
1092 * VisibilityFullyObscured. Note that we DO NOT receive a
1093 * VisibilityNotify event when iconifying a window, we only get an
1096 GETWINDOW( xvisibility );
1097 switch( event.xvisibility.state )
1099 case VisibilityUnobscured:
1100 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_RETAINED ) );
1101 window->State.Visible = GL_TRUE;
1104 case VisibilityPartiallyObscured:
1105 INVOKE_WCB( *window, WindowStatus,
1106 ( GLUT_PARTIALLY_RETAINED ) );
1107 window->State.Visible = GL_TRUE;
1110 case VisibilityFullyObscured:
1111 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_COVERED ) );
1112 window->State.Visible = GL_FALSE;
1116 fgWarning( "Unknown X visibility state: %d",
1117 event.xvisibility.state );
1125 GETWINDOW( xcrossing );
1126 GETMOUSE( xcrossing );
1127 if( ( event.type == LeaveNotify ) && window->IsMenu &&
1128 window->ActiveMenu && window->ActiveMenu->IsActive )
1129 fgUpdateMenuHighlight( window->ActiveMenu );
1131 INVOKE_WCB( *window, Entry, ( ( EnterNotify == event.type ) ?
1138 GETWINDOW( xmotion );
1139 GETMOUSE( xmotion );
1141 if( window->ActiveMenu )
1143 if( window == window->ActiveMenu->ParentWindow )
1145 window->ActiveMenu->Window->State.MouseX =
1146 event.xmotion.x_root - window->ActiveMenu->X;
1147 window->ActiveMenu->Window->State.MouseY =
1148 event.xmotion.y_root - window->ActiveMenu->Y;
1151 fgUpdateMenuHighlight( window->ActiveMenu );
1157 * XXX For more than 5 buttons, just check {event.xmotion.state},
1158 * XXX rather than a host of bit-masks? Or maybe we need to
1159 * XXX track ButtonPress/ButtonRelease events in our own
1162 fgState.Modifiers = fghGetXModifiers( event.xmotion.state );
1163 if ( event.xmotion.state & ( Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask ) ) {
1164 INVOKE_WCB( *window, Motion, ( event.xmotion.x,
1165 event.xmotion.y ) );
1167 INVOKE_WCB( *window, Passive, ( event.xmotion.x,
1168 event.xmotion.y ) );
1170 fgState.Modifiers = INVALID_MODIFIERS;
1177 GLboolean pressed = GL_TRUE;
1180 if( event.type == ButtonRelease )
1181 pressed = GL_FALSE ;
1184 * A mouse button has been pressed or released. Traditionally,
1185 * break if the window was found within the freeglut structures.
1187 GETWINDOW( xbutton );
1188 GETMOUSE( xbutton );
1191 * An X button (at least in XFree86) is numbered from 1.
1192 * A GLUT button is numbered from 0.
1193 * Old GLUT passed through buttons other than just the first
1194 * three, though it only gave symbolic names and official
1195 * support to the first three.
1197 button = event.xbutton.button - 1;
1200 * Do not execute the application's mouse callback if a menu
1201 * is hooked to this button. In that case an appropriate
1202 * private call should be generated.
1204 if( fgCheckActiveMenu( window, button, pressed,
1205 event.xbutton.x_root, event.xbutton.y_root ) )
1209 * Check if there is a mouse or mouse wheel callback hooked to the
1212 if( ! FETCH_WCB( *window, Mouse ) &&
1213 ! FETCH_WCB( *window, MouseWheel ) )
1216 fgState.Modifiers = fghGetXModifiers( event.xbutton.state );
1218 /* Finally execute the mouse or mouse wheel callback */
1219 if( ( button < glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS ) ) || ( ! FETCH_WCB( *window, MouseWheel ) ) )
1220 INVOKE_WCB( *window, Mouse, ( button,
1221 pressed ? GLUT_DOWN : GLUT_UP,
1228 * Map 4 and 5 to wheel zero; EVEN to +1, ODD to -1
1229 * " 6 and 7 " " one; ...
1231 * XXX This *should* be behind some variables/macros,
1232 * XXX since the order and numbering isn't certain
1233 * XXX See XFree86 configuration docs (even back in the
1234 * XXX 3.x days, and especially with 4.x).
1236 * XXX Note that {button} has already been decremeted
1237 * XXX in mapping from X button numbering to GLUT.
1239 int wheel_number = (button - glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS )) / 2;
1245 INVOKE_WCB( *window, MouseWheel, ( wheel_number,
1251 fgState.Modifiers = INVALID_MODIFIERS;
1258 FGCBKeyboard keyboard_cb;
1259 FGCBSpecial special_cb;
1264 /* Detect auto repeated keys, if configured globally or per-window */
1266 if ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE )
1268 if (event.type==KeyRelease)
1271 * Look at X11 keystate to detect repeat mode.
1272 * While X11 says the key is actually held down, we'll ignore KeyRelease/KeyPress pairs.
1276 XQueryKeymap( fgDisplay.Display, keys ); /* Look at X11 keystate to detect repeat mode */
1278 if ( event.xkey.keycode<256 ) /* XQueryKeymap is limited to 256 keycodes */
1280 if ( keys[event.xkey.keycode>>3] & (1<<(event.xkey.keycode%8)) )
1281 window->State.KeyRepeating = GL_TRUE;
1283 window->State.KeyRepeating = GL_FALSE;
1288 window->State.KeyRepeating = GL_FALSE;
1290 /* Cease processing this event if it is auto repeated */
1292 if (window->State.KeyRepeating)
1295 if( event.type == KeyPress )
1297 keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, Keyboard ));
1298 special_cb = (FGCBSpecial) ( FETCH_WCB( *window, Special ));
1302 keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, KeyboardUp ));
1303 special_cb = (FGCBSpecial) ( FETCH_WCB( *window, SpecialUp ));
1306 /* Is there a keyboard/special callback hooked for this window? */
1307 if( keyboard_cb || special_cb )
1309 XComposeStatus composeStatus;
1310 char asciiCode[ 32 ];
1314 /* Check for the ASCII/KeySym codes associated with the event: */
1315 len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode),
1316 &keySym, &composeStatus
1319 /* GLUT API tells us to have two separate callbacks... */
1322 /* ...one for the ASCII translateable keypresses... */
1325 fgSetWindow( window );
1326 fgState.Modifiers = fghGetXModifiers( event.xkey.state );
1327 keyboard_cb( asciiCode[ 0 ],
1328 event.xkey.x, event.xkey.y
1330 fgState.Modifiers = INVALID_MODIFIERS;
1338 * ...and one for all the others, which need to be
1339 * translated to GLUT_KEY_Xs...
1343 case XK_F1: special = GLUT_KEY_F1; break;
1344 case XK_F2: special = GLUT_KEY_F2; break;
1345 case XK_F3: special = GLUT_KEY_F3; break;
1346 case XK_F4: special = GLUT_KEY_F4; break;
1347 case XK_F5: special = GLUT_KEY_F5; break;
1348 case XK_F6: special = GLUT_KEY_F6; break;
1349 case XK_F7: special = GLUT_KEY_F7; break;
1350 case XK_F8: special = GLUT_KEY_F8; break;
1351 case XK_F9: special = GLUT_KEY_F9; break;
1352 case XK_F10: special = GLUT_KEY_F10; break;
1353 case XK_F11: special = GLUT_KEY_F11; break;
1354 case XK_F12: special = GLUT_KEY_F12; break;
1356 case XK_Left: special = GLUT_KEY_LEFT; break;
1357 case XK_Right: special = GLUT_KEY_RIGHT; break;
1358 case XK_Up: special = GLUT_KEY_UP; break;
1359 case XK_Down: special = GLUT_KEY_DOWN; break;
1362 case XK_Prior: special = GLUT_KEY_PAGE_UP; break;
1364 case XK_Next: special = GLUT_KEY_PAGE_DOWN; break;
1366 case XK_Home: special = GLUT_KEY_HOME; break;
1368 case XK_End: special = GLUT_KEY_END; break;
1370 case XK_Insert: special = GLUT_KEY_INSERT; break;
1374 * Execute the callback (if one has been specified),
1375 * given that the special code seems to be valid...
1377 if( special_cb && (special != -1) )
1379 fgSetWindow( window );
1380 fgState.Modifiers = fghGetXModifiers( event.xkey.state );
1381 special_cb( special, event.xkey.x, event.xkey.y );
1382 fgState.Modifiers = INVALID_MODIFIERS;
1389 case ReparentNotify:
1390 break; /* XXX Should disable this event */
1393 fgWarning ("Unknown X event type: %d\n", event.type);
1398 #elif TARGET_HOST_MS_WINDOWS
1402 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoopEvent" );
1404 while( PeekMessage( &stMsg, NULL, 0, 0, PM_NOREMOVE ) )
1406 if( GetMessage( &stMsg, NULL, 0, 0 ) == 0 )
1408 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
1413 else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS )
1414 fgState.ExecState = GLUT_EXEC_STATE_STOP;
1419 TranslateMessage( &stMsg );
1420 DispatchMessage( &stMsg );
1424 if( fgState.Timers.First )
1426 fghCheckJoystickPolls( );
1433 * Enters the freeglut processing loop.
1434 * Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".
1436 void FGAPIENTRY glutMainLoop( void )
1440 #if TARGET_HOST_MS_WINDOWS
1441 SFG_Window *window = (SFG_Window *)fgStructure.Windows.First ;
1444 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoop" );
1446 #if TARGET_HOST_MS_WINDOWS
1448 * Processing before the main loop: If there is a window which is open and
1449 * which has a visibility callback, call it. I know this is an ugly hack,
1450 * but I'm not sure what else to do about it. Ideally we should leave
1451 * something uninitialized in the create window code and initialize it in
1452 * the main loop, and have that initialization create a "WM_ACTIVATE"
1453 * message. Then we would put the visibility callback code in the
1454 * "case WM_ACTIVATE" block below. - John Fay -- 10/24/02
1458 if ( FETCH_WCB( *window, Visibility ) )
1460 SFG_Window *current_window = fgStructure.CurrentWindow ;
1462 INVOKE_WCB( *window, Visibility, ( window->State.Visible ) );
1463 fgSetWindow( current_window );
1466 window = (SFG_Window *)window->Node.Next ;
1470 fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;
1471 while( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )
1475 glutMainLoopEvent( );
1477 * Step through the list of windows, seeing if there are any
1478 * that are not menus
1480 for( window = ( SFG_Window * )fgStructure.Windows.First;
1482 window = ( SFG_Window * )window->Node.Next )
1483 if ( ! ( window->IsMenu ) )
1487 fgState.ExecState = GLUT_EXEC_STATE_STOP;
1490 if( fgState.IdleCallback )
1492 if( fgStructure.CurrentWindow &&
1493 fgStructure.CurrentWindow->IsMenu )
1495 fgSetWindow( window );
1496 fgState.IdleCallback( );
1499 fghSleepForEvents( );
1504 * When this loop terminates, destroy the display, state and structure
1505 * of a freeglut session, so that another glutInit() call can happen
1507 * Save the "ActionOnWindowClose" because "fgDeinitialize" resets it.
1509 action = fgState.ActionOnWindowClose;
1511 if( action == GLUT_ACTION_EXIT )
1516 * Leaves the freeglut processing loop.
1518 void FGAPIENTRY glutLeaveMainLoop( void )
1520 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveMainLoop" );
1521 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
1525 #if TARGET_HOST_MS_WINDOWS
1527 * Determine a GLUT modifer mask based on MS-WINDOWS system info.
1529 static int fghGetWin32Modifiers (void)
1532 ( ( ( GetKeyState( VK_LSHIFT ) < 0 ) ||
1533 ( GetKeyState( VK_RSHIFT ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
1534 ( ( ( GetKeyState( VK_LCONTROL ) < 0 ) ||
1535 ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL : 0 ) |
1536 ( ( ( GetKeyState( VK_LMENU ) < 0 ) ||
1537 ( GetKeyState( VK_RMENU ) < 0 )) ? GLUT_ACTIVE_ALT : 0 );
1541 * The window procedure for handling Win32 events
1543 LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,
1550 FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Event Handler" ) ;
1552 window = fgWindowByHandle( hWnd );
1554 if ( ( window == NULL ) && ( uMsg != WM_CREATE ) )
1555 return DefWindowProc( hWnd, uMsg, wParam, lParam );
1557 /* printf ( "Window %3d message <%04x> %12d %12d\n", window?window->ID:0,
1558 uMsg, wParam, lParam ); */
1562 /* The window structure is passed as the creation structure paramter... */
1563 window = (SFG_Window *) (((LPCREATESTRUCT) lParam)->lpCreateParams);
1564 FREEGLUT_INTERNAL_ERROR_EXIT ( ( window != NULL ), "Cannot create window",
1567 window->Window.Handle = hWnd;
1568 window->Window.Device = GetDC( hWnd );
1569 if( window->IsMenu )
1571 unsigned int current_DisplayMode = fgState.DisplayMode;
1572 fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;
1573 #if !defined(_WIN32_WCE)
1574 fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
1576 fgState.DisplayMode = current_DisplayMode;
1578 if( fgStructure.MenuContext )
1579 wglMakeCurrent( window->Window.Device,
1580 fgStructure.MenuContext->MContext
1584 fgStructure.MenuContext =
1585 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
1586 fgStructure.MenuContext->MContext =
1587 wglCreateContext( window->Window.Device );
1590 /* window->Window.Context = wglGetCurrentContext (); */
1591 window->Window.Context = wglCreateContext( window->Window.Device );
1595 #if !defined(_WIN32_WCE)
1596 fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
1599 if( ! fgState.UseCurrentContext )
1600 window->Window.Context =
1601 wglCreateContext( window->Window.Device );
1604 window->Window.Context = wglGetCurrentContext( );
1605 if( ! window->Window.Context )
1606 window->Window.Context =
1607 wglCreateContext( window->Window.Device );
1611 window->State.NeedToResize = GL_TRUE;
1612 if( ( window->State.Width < 0 ) || ( window->State.Height < 0 ) )
1614 SFG_Window *current_window = fgStructure.CurrentWindow;
1616 fgSetWindow( window );
1617 window->State.Width = glutGet( GLUT_WINDOW_WIDTH );
1618 window->State.Height = glutGet( GLUT_WINDOW_HEIGHT );
1619 fgSetWindow( current_window );
1622 ReleaseDC( window->Window.Handle, window->Window.Device );
1624 #if defined(_WIN32_WCE)
1625 /* Take over button handling */
1627 HINSTANCE dxDllLib=LoadLibrary(_T("gx.dll"));
1630 GXGetDefaultKeys_=(GXGETDEFAULTKEYS)GetProcAddress(dxDllLib, _T("?GXGetDefaultKeys@@YA?AUGXKeyList@@H@Z"));
1631 GXOpenInput_=(GXOPENINPUT)GetProcAddress(dxDllLib, _T("?GXOpenInput@@YAHXZ"));
1636 if(GXGetDefaultKeys_)
1637 gxKeyList = (*GXGetDefaultKeys_)(GX_LANDSCAPEKEYS);
1640 #endif /* defined(_WIN32_WCE) */
1645 * If the window is visible, then it is the user manually resizing it.
1646 * If it is not, then it is the system sending us a dummy resize with
1647 * zero dimensions on a "glutIconifyWindow" call.
1649 if( window->State.Visible )
1651 window->State.NeedToResize = GL_TRUE;
1652 #if defined(_WIN32_WCE)
1653 window->State.Width = HIWORD(lParam);
1654 window->State.Height = LOWORD(lParam);
1656 window->State.Width = LOWORD(lParam);
1657 window->State.Height = HIWORD(lParam);
1658 #endif /* defined(_WIN32_WCE) */
1664 /* printf("WM_SETFOCUS: %p\n", window ); */
1665 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1669 if (LOWORD(wParam) != WA_INACTIVE)
1671 /* printf("WM_ACTIVATE: fgSetCursor( %p, %d)\n", window,
1672 window->State.Cursor ); */
1673 fgSetCursor( window, window->State.Cursor );
1676 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1681 /* printf ( "Cursor event %x %x %x %x\n", window, window->State.Cursor, lParam, wParam ) ; */
1682 if( LOWORD( lParam ) == HTCLIENT )
1683 fgSetCursor ( window, window->State.Cursor ) ;
1685 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1689 window->State.Visible = GL_TRUE;
1690 window->State.Redisplay = GL_TRUE;
1694 /* Turn on the visibility in case it was turned off somehow */
1695 window->State.Visible = GL_TRUE;
1696 BeginPaint( hWnd, &ps );
1697 fghRedrawWindow( window );
1698 EndPaint( hWnd, &ps );
1702 fgDestroyWindow ( window );
1703 if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
1709 * The window already got destroyed, so don't bother with it.
1713 /* XXX For a future patch: we need a mouse entry event. Unfortunately Windows
1714 * XXX doesn't give us one, so we will probably need a "MouseInWindow" flag in
1715 * XXX the SFG_Window structure. Set it to true to begin with and then have the
1716 * XXX WM_MOUSELEAVE code set it to false. Then when we get a WM_MOUSEMOVE event,
1717 * XXX if the flag is false we invoke the Entry callback and set the flag to true.
1719 case 0x02a2: /* This is the message we get when the mouse is leaving the window */
1720 if( window->IsMenu &&
1721 window->ActiveMenu && window->ActiveMenu->IsActive )
1722 fgUpdateMenuHighlight( window->ActiveMenu );
1724 INVOKE_WCB( *window, Entry, ( GLUT_LEFT ) );
1729 #if defined(_WIN32_WCE)
1730 window->State.MouseX = 320-HIWORD( lParam );
1731 window->State.MouseY = LOWORD( lParam );
1733 window->State.MouseX = LOWORD( lParam );
1734 window->State.MouseY = HIWORD( lParam );
1735 #endif /* defined(_WIN32_WCE) */
1736 /* Restrict to [-32768, 32767] to match X11 behaviour */
1737 /* See comment in "freeglut_developer" mailing list 10/4/04 */
1738 if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;
1739 if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;
1741 if ( window->ActiveMenu )
1743 fgUpdateMenuHighlight( window->ActiveMenu );
1747 fgState.Modifiers = fghGetWin32Modifiers( );
1749 if( ( wParam & MK_LBUTTON ) ||
1750 ( wParam & MK_MBUTTON ) ||
1751 ( wParam & MK_RBUTTON ) )
1752 INVOKE_WCB( *window, Motion, ( window->State.MouseX,
1753 window->State.MouseY ) );
1755 INVOKE_WCB( *window, Passive, ( window->State.MouseX,
1756 window->State.MouseY ) );
1758 fgState.Modifiers = INVALID_MODIFIERS;
1762 case WM_LBUTTONDOWN:
1763 case WM_MBUTTONDOWN:
1764 case WM_RBUTTONDOWN:
1769 GLboolean pressed = GL_TRUE;
1772 #if defined(_WIN32_WCE)
1773 window->State.MouseX = 320-HIWORD( lParam );
1774 window->State.MouseY = LOWORD( lParam );
1776 window->State.MouseX = LOWORD( lParam );
1777 window->State.MouseY = HIWORD( lParam );
1778 #endif /* defined(_WIN32_WCE) */
1780 /* Restrict to [-32768, 32767] to match X11 behaviour */
1781 /* See comment in "freeglut_developer" mailing list 10/4/04 */
1782 if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;
1783 if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;
1787 case WM_LBUTTONDOWN:
1789 button = GLUT_LEFT_BUTTON;
1791 case WM_MBUTTONDOWN:
1793 button = GLUT_MIDDLE_BUTTON;
1795 case WM_RBUTTONDOWN:
1797 button = GLUT_RIGHT_BUTTON;
1801 button = GLUT_LEFT_BUTTON;
1805 button = GLUT_MIDDLE_BUTTON;
1809 button = GLUT_RIGHT_BUTTON;
1817 #if !defined(_WIN32_WCE)
1818 if( GetSystemMetrics( SM_SWAPBUTTON ) )
1820 if( button == GLUT_LEFT_BUTTON )
1821 button = GLUT_RIGHT_BUTTON;
1823 if( button == GLUT_RIGHT_BUTTON )
1824 button = GLUT_LEFT_BUTTON;
1826 #endif /* !defined(_WIN32_WCE) */
1829 return DefWindowProc( hWnd, uMsg, lParam, wParam );
1832 * Do not execute the application's mouse callback if a menu
1833 * is hooked to this button. In that case an appropriate
1834 * private call should be generated.
1836 if( fgCheckActiveMenu( window, button, pressed,
1837 window->State.MouseX, window->State.MouseY ) )
1840 /* Set capture so that the window captures all the mouse messages */
1842 * XXX - Multiple button support: Under X11, the mouse is not released
1843 * XXX - from the window until all buttons have been released, even if the
1844 * XXX - user presses a button in another window. This will take more
1845 * XXX - code changes than I am up to at the moment (10/5/04). The present
1846 * XXX - is a 90 percent solution.
1848 if ( pressed == GL_TRUE )
1849 SetCapture ( window->Window.Handle ) ;
1853 if( ! FETCH_WCB( *window, Mouse ) )
1856 fgSetWindow( window );
1857 fgState.Modifiers = fghGetWin32Modifiers( );
1862 pressed ? GLUT_DOWN : GLUT_UP,
1863 window->State.MouseX,
1864 window->State.MouseY
1868 fgState.Modifiers = INVALID_MODIFIERS;
1873 /* Should be WM_MOUSEWHEEL but my compiler doesn't recognize it */
1876 * XXX THIS IS SPECULATIVE -- John Fay, 10/2/03
1877 * XXX Should use WHEEL_DELTA instead of 120
1879 int wheel_number = LOWORD( wParam );
1880 short ticks = ( short )HIWORD( wParam ) / 120;
1890 * The mouse cursor has moved. Remember the new mouse cursor's position
1892 /* window->State.MouseX = LOWORD( lParam ); */
1893 /* Need to adjust by window position, */
1894 /* window->State.MouseY = HIWORD( lParam ); */
1895 /* change "lParam" to other parameter */
1897 if( ! FETCH_WCB( *window, MouseWheel ) &&
1898 ! FETCH_WCB( *window, Mouse ) )
1901 fgSetWindow( window );
1902 fgState.Modifiers = fghGetWin32Modifiers( );
1905 if( FETCH_WCB( *window, MouseWheel ) )
1906 INVOKE_WCB( *window, MouseWheel,
1909 window->State.MouseX,
1910 window->State.MouseY
1913 else /* No mouse wheel, call the mouse button callback twice */
1916 * Map wheel zero to button 3 and 4; +1 to 3, -1 to 4
1917 * " " one +1 to 5, -1 to 6, ...
1919 * XXX The below assumes that you have no more than 3 mouse
1920 * XXX buttons. Sorry.
1922 int button = wheel_number * 2 + 3;
1925 INVOKE_WCB( *window, Mouse,
1926 ( button, GLUT_DOWN,
1927 window->State.MouseX, window->State.MouseY )
1929 INVOKE_WCB( *window, Mouse,
1931 window->State.MouseX, window->State.MouseY )
1935 fgState.Modifiers = INVALID_MODIFIERS;
1945 if( ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE ) && (HIWORD(lParam) & KF_REPEAT) )
1949 * Remember the current modifiers state. This is done here in order
1950 * to make sure the VK_DELETE keyboard callback is executed properly.
1952 fgState.Modifiers = fghGetWin32Modifiers( );
1954 GetCursorPos( &mouse_pos );
1955 ScreenToClient( window->Window.Handle, &mouse_pos );
1957 window->State.MouseX = mouse_pos.x;
1958 window->State.MouseY = mouse_pos.y;
1960 /* Convert the Win32 keystroke codes to GLUTtish way */
1961 # define KEY(a,b) case a: keypress = b; break;
1965 KEY( VK_F1, GLUT_KEY_F1 );
1966 KEY( VK_F2, GLUT_KEY_F2 );
1967 KEY( VK_F3, GLUT_KEY_F3 );
1968 KEY( VK_F4, GLUT_KEY_F4 );
1969 KEY( VK_F5, GLUT_KEY_F5 );
1970 KEY( VK_F6, GLUT_KEY_F6 );
1971 KEY( VK_F7, GLUT_KEY_F7 );
1972 KEY( VK_F8, GLUT_KEY_F8 );
1973 KEY( VK_F9, GLUT_KEY_F9 );
1974 KEY( VK_F10, GLUT_KEY_F10 );
1975 KEY( VK_F11, GLUT_KEY_F11 );
1976 KEY( VK_F12, GLUT_KEY_F12 );
1977 KEY( VK_PRIOR, GLUT_KEY_PAGE_UP );
1978 KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );
1979 KEY( VK_HOME, GLUT_KEY_HOME );
1980 KEY( VK_END, GLUT_KEY_END );
1981 KEY( VK_LEFT, GLUT_KEY_LEFT );
1982 KEY( VK_UP, GLUT_KEY_UP );
1983 KEY( VK_RIGHT, GLUT_KEY_RIGHT );
1984 KEY( VK_DOWN, GLUT_KEY_DOWN );
1985 KEY( VK_INSERT, GLUT_KEY_INSERT );
1988 /* The delete key should be treated as an ASCII keypress: */
1989 INVOKE_WCB( *window, Keyboard,
1990 ( 127, window->State.MouseX, window->State.MouseY )
1994 #if defined(_WIN32_WCE)
1995 if(!(lParam & 0x40000000)) /* Prevent auto-repeat */
1997 if(wParam==(unsigned)gxKeyList.vkRight)
1998 keypress = GLUT_KEY_RIGHT;
1999 else if(wParam==(unsigned)gxKeyList.vkLeft)
2000 keypress = GLUT_KEY_LEFT;
2001 else if(wParam==(unsigned)gxKeyList.vkUp)
2002 keypress = GLUT_KEY_UP;
2003 else if(wParam==(unsigned)gxKeyList.vkDown)
2004 keypress = GLUT_KEY_DOWN;
2005 else if(wParam==(unsigned)gxKeyList.vkA)
2006 keypress = GLUT_KEY_F1;
2007 else if(wParam==(unsigned)gxKeyList.vkB)
2008 keypress = GLUT_KEY_F2;
2009 else if(wParam==(unsigned)gxKeyList.vkC)
2010 keypress = GLUT_KEY_F3;
2011 else if(wParam==(unsigned)gxKeyList.vkStart)
2012 keypress = GLUT_KEY_F4;
2016 if( keypress != -1 )
2017 INVOKE_WCB( *window, Special,
2019 window->State.MouseX, window->State.MouseY )
2022 fgState.Modifiers = INVALID_MODIFIERS;
2033 * Remember the current modifiers state. This is done here in order
2034 * to make sure the VK_DELETE keyboard callback is executed properly.
2036 fgState.Modifiers = fghGetWin32Modifiers( );
2038 GetCursorPos( &mouse_pos );
2039 ScreenToClient( window->Window.Handle, &mouse_pos );
2041 window->State.MouseX = mouse_pos.x;
2042 window->State.MouseY = mouse_pos.y;
2045 * Convert the Win32 keystroke codes to GLUTtish way.
2046 * "KEY(a,b)" was defined under "WM_KEYDOWN"
2051 KEY( VK_F1, GLUT_KEY_F1 );
2052 KEY( VK_F2, GLUT_KEY_F2 );
2053 KEY( VK_F3, GLUT_KEY_F3 );
2054 KEY( VK_F4, GLUT_KEY_F4 );
2055 KEY( VK_F5, GLUT_KEY_F5 );
2056 KEY( VK_F6, GLUT_KEY_F6 );
2057 KEY( VK_F7, GLUT_KEY_F7 );
2058 KEY( VK_F8, GLUT_KEY_F8 );
2059 KEY( VK_F9, GLUT_KEY_F9 );
2060 KEY( VK_F10, GLUT_KEY_F10 );
2061 KEY( VK_F11, GLUT_KEY_F11 );
2062 KEY( VK_F12, GLUT_KEY_F12 );
2063 KEY( VK_PRIOR, GLUT_KEY_PAGE_UP );
2064 KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );
2065 KEY( VK_HOME, GLUT_KEY_HOME );
2066 KEY( VK_END, GLUT_KEY_END );
2067 KEY( VK_LEFT, GLUT_KEY_LEFT );
2068 KEY( VK_UP, GLUT_KEY_UP );
2069 KEY( VK_RIGHT, GLUT_KEY_RIGHT );
2070 KEY( VK_DOWN, GLUT_KEY_DOWN );
2071 KEY( VK_INSERT, GLUT_KEY_INSERT );
2074 /* The delete key should be treated as an ASCII keypress: */
2075 INVOKE_WCB( *window, KeyboardUp,
2076 ( 127, window->State.MouseX, window->State.MouseY )
2082 #if !defined(_WIN32_WCE)
2086 GetKeyboardState( state );
2088 if( ToAscii( wParam, 0, state, code, 0 ) == 1 )
2091 INVOKE_WCB( *window, KeyboardUp,
2093 window->State.MouseX, window->State.MouseY )
2095 #endif /* !defined(_WIN32_WCE) */
2099 if( keypress != -1 )
2100 INVOKE_WCB( *window, SpecialUp,
2102 window->State.MouseX, window->State.MouseY )
2105 fgState.Modifiers = INVALID_MODIFIERS;
2112 if( (fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE) && (HIWORD(lParam) & KF_REPEAT) )
2115 fgState.Modifiers = fghGetWin32Modifiers( );
2116 INVOKE_WCB( *window, Keyboard,
2118 window->State.MouseX, window->State.MouseY )
2120 fgState.Modifiers = INVALID_MODIFIERS;
2124 case WM_CAPTURECHANGED:
2125 /* User has finished resizing the window, force a redraw */
2126 INVOKE_WCB( *window, Display, ( ) );
2128 /*lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ); */
2131 /* Other messages that I have seen and which are not handled already */
2132 case WM_SETTEXT: /* 0x000c */
2133 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2134 /* Pass it on to "DefWindowProc" to set the window text */
2137 case WM_GETTEXT: /* 0x000d */
2138 /* Ideally we would copy the title of the window into "lParam" */
2139 /* strncpy ( (char *)lParam, "Window Title", wParam );
2140 lRet = ( wParam > 12 ) ? 12 : wParam; */
2141 /* the number of characters copied */
2142 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2145 case WM_GETTEXTLENGTH: /* 0x000e */
2146 /* Ideally we would get the length of the title of the window */
2148 /* the number of characters in "Window Title\0" (see above) */
2151 case WM_ERASEBKGND: /* 0x0014 */
2152 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2155 #if !defined(_WIN32_WCE)
2156 case WM_SYNCPAINT: /* 0x0088 */
2157 /* Another window has moved, need to update this one */
2158 window->State.Redisplay = GL_TRUE;
2159 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2160 /* Help screen says this message must be passed to "DefWindowProc" */
2163 case WM_NCPAINT: /* 0x0085 */
2164 /* Need to update the border of this window */
2165 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2166 /* Pass it on to "DefWindowProc" to repaint a standard border */
2169 case WM_SYSCOMMAND : /* 0x0112 */
2172 * We have received a system command message. Try to act on it.
2173 * The commands are passed in through the "wParam" parameter:
2174 * The least significant digit seems to be which edge of the window
2175 * is being used for a resize event:
2179 * Congratulations and thanks to Richard Rauch for figuring this out..
2181 switch ( wParam & 0xfff0 )
2190 /* User has clicked on the "-" to minimize the window */
2191 /* Turn off the visibility */
2192 window->State.Visible = GL_FALSE ;
2199 case SC_NEXTWINDOW :
2202 case SC_PREVWINDOW :
2206 /* Followed very closely by a WM_CLOSE message */
2230 case SC_SCREENSAVE :
2238 fgWarning( "Unknown wParam type 0x%x", wParam );
2243 #endif /* !defined(_WIN32_WCE) */
2245 /* We need to pass the message on to the operating system as well */
2246 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2250 /* Handle unhandled messages */
2251 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2259 /*** END OF FILE ***/