2 * Copyright (C) 2006 Claudio Ciccani <klan@users.sf.net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 typedef void (GLUTCALLBACK
*__GlutTimerCallback
) ( int value
);
30 typedef struct __GlutTimer_s
{
31 unsigned int interval
;
32 struct timeval expire
;
34 __GlutTimerCallback func
;
37 struct __GlutTimer_s
*next
;
38 struct __GlutTimer_s
*prev
;
41 /*****************************************************************************/
43 static __GlutTimer
*g_timers
= NULL
;
45 /*****************************************************************************/
49 glutDisplayFunc( void (GLUTCALLBACK
*func
) (void) )
56 glutReshapeFunc( void (GLUTCALLBACK
*func
) (int width
, int height
) )
63 glutKeyboardFunc( void (GLUTCALLBACK
*func
) (unsigned char key
, int x
, int y
) )
70 glutMouseFunc( void (GLUTCALLBACK
*func
) (int button
, int state
, int x
, int y
) )
77 glutMotionFunc( void (GLUTCALLBACK
*func
) (int x
, int y
) )
84 glutPassiveMotionFunc( void (GLUTCALLBACK
*func
) (int x
, int y
) )
86 passive_motion_func
= func
;
91 glutEntryFunc( void (GLUTCALLBACK
*func
) (int state
) )
98 glutVisibilityFunc( void (GLUTCALLBACK
*func
) (int state
) )
100 visibility_func
= func
;
105 glutMenuStateFunc( void (GLUTCALLBACK
*func
) (int state
) )
107 menu_state_func
= func
;
112 glutSpecialFunc( void (GLUTCALLBACK
*func
) (int key
, int x
, int y
) )
119 glutSpaceballMotionFunc( void (GLUTCALLBACK
*func
) (int x
, int y
, int z
) )
125 glutSpaceballRotateFunc( void (GLUTCALLBACK
*func
) (int x
, int y
, int z
) )
131 glutSpaceballButtonFunc( void (GLUTCALLBACK
*func
) (int button
, int state
) )
137 glutButtonBoxFunc( void (GLUTCALLBACK
*func
) (int button
, int state
) )
143 glutDialsFunc( void (GLUTCALLBACK
*func
) (int dial
, int value
) )
149 glutTabletMotionFunc( void (GLUTCALLBACK
*func
) (int x
, int y
) )
155 glutTabletButtonFunc( void (GLUTCALLBACK
*func
) (int button
, int state
, int x
, int y
) )
161 glutMenuStatusFunc( void (GLUTCALLBACK
*func
) (int status
, int x
, int y
) )
167 glutOverlayDisplayFunc( void (GLUTCALLBACK
*func
) (void) )
173 glutWindowStatusFunc( void (GLUTCALLBACK
*func
) (int state
) )
179 glutKeyboardUpFunc( void (GLUTCALLBACK
*func
) (unsigned char key
, int x
, int y
) )
181 keyboard_up_func
= func
;
186 glutSpecialUpFunc( void (GLUTCALLBACK
*func
) (int key
, int x
, int y
) )
188 special_up_func
= func
;
193 glutJoystickFunc( void (GLUTCALLBACK
*func
)(unsigned int buttons
, int x
, int y
, int z
), int pollInterval
)
195 joystick_func
= func
;
196 /* FIXME: take care of pollInterval */
201 glutIdleFunc( void (GLUTCALLBACK
*func
) (void) )
208 glutTimerFunc( unsigned int msec
, void (GLUTCALLBACK
*func
) (int value
), int value
)
216 timer
= calloc( 1, sizeof(__GlutTimer
) );
218 __glutFatalError( "out of memory" );
220 gettimeofday( &now
, NULL
);
222 timer
->interval
= msec
;
223 timer
->expire
.tv_sec
= now
.tv_sec
+ (now
.tv_usec
/1000 + msec
) / 1000;
224 timer
->expire
.tv_usec
= (now
.tv_usec
+ msec
*1000) % 1000000;
227 timer
->value
= value
;
230 timer
->prev
= g_timers
->prev
;
231 g_timers
->prev
->next
= timer
;
232 g_timers
->prev
= timer
;
236 g_timers
->prev
= timer
;
242 __glutHandleTimers( void )
247 for (cur
= g_timers
; cur
; cur
= cur
->next
) {
248 gettimeofday( &now
, NULL
);
250 if (cur
->expire
.tv_sec
> now
.tv_sec
||
251 (cur
->expire
.tv_sec
== now
.tv_sec
&&
252 cur
->expire
.tv_usec
>= now
.tv_usec
))
256 cur
->func( cur
->value
);
258 cur
->expire
.tv_sec
+= (cur
->expire
.tv_usec
/1000 + cur
->interval
) / 1000;
259 cur
->expire
.tv_usec
= (cur
->expire
.tv_usec
+ cur
->interval
*1000) % 1000000;
266 __glutFreeTimers( void )
268 __GlutTimer
*cur
= g_timers
;
271 __GlutTimer
*next
= cur
->next
;