+/*
+ * Indicates whether Joystick events are being used by ANY window.
+ *
+ * The current mechanism is to walk all of the windows and ask if
+ * there is a joystick callback. Certainly in some cases, maybe
+ * in all cases, the joystick is attached to the system and accessed
+ * from ONE point by GLUT/freeglut, so this is not the right way,
+ * in general, to do this. However, the Joystick code is segregated
+ * in its own little world, so we can't access the information that
+ * we need in order to do that nicely.
+ *
+ * Some alternatives:
+ * * Store Joystick data into freeglut global state.
+ * * Provide NON-static functions or data from Joystick *.c file.
+ *
+ * Basically, the RIGHT way to do this requires knowing something
+ * about the Joystick. Right now, the Joystick code is behind
+ * an opaque wall.
+ *
+ */
+static void fgCheckJoystickCallback( SFG_Window* w, SFG_Enumerator* e)
+{
+ if( FETCH_WCB( *w, Joystick ) )
+ {
+ e->found = GL_TRUE;
+ e->data = w;
+ }
+ fgEnumSubWindows( w, fgCheckJoystickCallback, e );
+}
+static int fgHaveJoystick( void )
+{
+ SFG_Enumerator enumerator;
+ enumerator.found = GL_FALSE;
+ enumerator.data = NULL;
+ fgEnumWindows( fgCheckJoystickCallback, &enumerator );
+ return !!enumerator.data;
+}
+static void fgHavePendingRedisplaysCallback( SFG_Window* w, SFG_Enumerator* e)
+{
+ if( w->State.Redisplay )
+ {
+ e->found = GL_TRUE;
+ e->data = w;
+ }
+ fgEnumSubWindows( w, fgHavePendingRedisplaysCallback, e );
+}
+static int fgHavePendingRedisplays (void)
+{
+ SFG_Enumerator enumerator;
+ enumerator.found = GL_FALSE;
+ enumerator.data = NULL;
+ fgEnumWindows( fgHavePendingRedisplaysCallback, &enumerator );
+ return !!enumerator.data;
+}
+/*
+ * Returns the number of GLUT ticks (milliseconds) till the next timer event.
+ */
+static long fgNextTimer( void )
+{
+ long ret = INT_MAX;
+ SFG_Timer *timer = fgState.Timers.First;
+
+ if( timer )
+ ret = timer->TriggerTime - fgElapsedTime();
+ if( ret < 0 )
+ ret = 0;
+
+ return ret;
+}
+/*
+ * Does the magic required to relinquish the CPU until something interesting
+ * happens.
+ */
+static void fgSleepForEvents( void )
+{
+#if TARGET_HOST_UNIX_X11
+ fd_set fdset;
+ int err;
+ int socket;
+ struct timeval wait;
+ long msec;
+
+ if( fgState.IdleCallback || fgHavePendingRedisplays( ) )
+ return;
+ socket = ConnectionNumber( fgDisplay.Display );
+ FD_ZERO( &fdset );
+ FD_SET( socket, &fdset );
+
+ msec = fgNextTimer( );
+ if( fgHaveJoystick( ) )
+ msec = MIN( msec, 10 );
+
+ wait.tv_sec = msec / 1000;
+ wait.tv_usec = (msec % 1000) * 1000;
+ err = select( socket+1, &fdset, NULL, NULL, &wait );
+
+ if( -1 == err )
+ fgWarning ( "freeglut select() error: %d\n", errno );
+
+#elif TARGET_HOST_WIN32
+#endif
+}
+
+#if TARGET_HOST_UNIX_X11
+/*
+ * Returns GLUT modifier mask for an XEvent.
+ */
+int fgGetXModifiers( XEvent *event )
+{
+ int ret = 0;
+
+ if( event->xkey.state & ( ShiftMask | LockMask ) )
+ ret |= GLUT_ACTIVE_SHIFT;
+ if( event->xkey.state & ControlMask )
+ ret |= GLUT_ACTIVE_CTRL;
+ if( event->xkey.state & Mod1Mask )
+ ret |= GLUT_ACTIVE_ALT;
+
+ return ret;
+}
+#endif
+
+