7c9768aac0adcab7fced6e00f7765f8480b5a55f
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 struct timeval interval
;
32 struct timeval expire
;
34 __GlutTimerCallback func
;
37 struct __GlutTimer_s
*next
;
40 /*****************************************************************************/
42 static __GlutTimer
*g_timers
= NULL
;
44 /*****************************************************************************/
48 glutDisplayFunc( void (GLUTCALLBACK
*func
) (void) )
55 glutReshapeFunc( void (GLUTCALLBACK
*func
) (int width
, int height
) )
62 glutKeyboardFunc( void (GLUTCALLBACK
*func
) (unsigned char key
, int x
, int y
) )
69 glutMouseFunc( void (GLUTCALLBACK
*func
) (int button
, int state
, int x
, int y
) )
76 glutMotionFunc( void (GLUTCALLBACK
*func
) (int x
, int y
) )
83 glutPassiveMotionFunc( void (GLUTCALLBACK
*func
) (int x
, int y
) )
85 passive_motion_func
= func
;
90 glutEntryFunc( void (GLUTCALLBACK
*func
) (int state
) )
97 glutVisibilityFunc( void (GLUTCALLBACK
*func
) (int state
) )
99 visibility_func
= func
;
104 glutMenuStateFunc( void (GLUTCALLBACK
*func
) (int state
) )
106 menu_state_func
= func
;
111 glutSpecialFunc( void (GLUTCALLBACK
*func
) (int key
, int x
, int y
) )
118 glutSpaceballMotionFunc( void (GLUTCALLBACK
*func
) (int x
, int y
, int z
) )
124 glutSpaceballRotateFunc( void (GLUTCALLBACK
*func
) (int x
, int y
, int z
) )
130 glutSpaceballButtonFunc( void (GLUTCALLBACK
*func
) (int button
, int state
) )
136 glutButtonBoxFunc( void (GLUTCALLBACK
*func
) (int button
, int state
) )
142 glutDialsFunc( void (GLUTCALLBACK
*func
) (int dial
, int value
) )
148 glutTabletMotionFunc( void (GLUTCALLBACK
*func
) (int x
, int y
) )
154 glutTabletButtonFunc( void (GLUTCALLBACK
*func
) (int button
, int state
, int x
, int y
) )
160 glutMenuStatusFunc( void (GLUTCALLBACK
*func
) (int status
, int x
, int y
) )
166 glutOverlayDisplayFunc( void (GLUTCALLBACK
*func
) (void) )
172 glutWindowStatusFunc( void (GLUTCALLBACK
*func
) (int state
) )
178 glutKeyboardUpFunc( void (GLUTCALLBACK
*func
) (unsigned char key
, int x
, int y
) )
180 keyboard_up_func
= func
;
185 glutSpecialUpFunc( void (GLUTCALLBACK
*func
) (int key
, int x
, int y
) )
187 special_up_func
= func
;
192 glutJoystickFunc( void (GLUTCALLBACK
*func
)(unsigned int buttons
, int x
, int y
, int z
), int pollInterval
)
194 joystick_func
= func
;
195 /* FIXME: take care of pollInterval */
200 glutIdleFunc( void (GLUTCALLBACK
*func
) (void) )
207 glutTimerFunc( unsigned int msec
, void (GLUTCALLBACK
*func
) (int value
), int value
)
214 timer
= calloc( 1, sizeof(__GlutTimer
) );
216 __glutFatalError( "out of memory" );
218 timer
->interval
.tv_sec
= msec
/ 1000;
219 timer
->interval
.tv_usec
= (msec
% 1000) * 1000;
221 gettimeofday( &timer
->expire
, NULL
);
222 timer
->expire
.tv_usec
+= timer
->interval
.tv_usec
;
223 timer
->expire
.tv_sec
+= timer
->interval
.tv_sec
+ timer
->expire
.tv_usec
/1000000;
224 timer
->expire
.tv_usec
%= 1000000;
227 timer
->value
= value
;
229 timer
->next
= g_timers
;
235 __glutHandleTimers( void )
240 for (cur
= g_timers
; cur
; cur
= cur
->next
) {
241 gettimeofday( &now
, NULL
);
243 if (cur
->expire
.tv_sec
> now
.tv_sec
||
244 (cur
->expire
.tv_sec
== now
.tv_sec
&&
245 cur
->expire
.tv_usec
>= now
.tv_usec
))
249 cur
->func( cur
->value
);
251 cur
->expire
.tv_usec
+= cur
->interval
.tv_usec
;
252 cur
->expire
.tv_sec
+= cur
->interval
.tv_sec
+ cur
->expire
.tv_usec
/1000000;
253 cur
->expire
.tv_usec
%= 1000000;
260 __glutGetTimeout( int *ret_msec
)
263 struct timeval
*time
= NULL
;
266 for (cur
= g_timers
; cur
; cur
= cur
->next
) {
268 time
->tv_sec
> cur
->expire
.tv_sec
||
269 (time
->tv_sec
== cur
->expire
.tv_sec
&&
270 time
->tv_usec
> cur
->expire
.tv_usec
)) {
278 gettimeofday( &now
, NULL
);
280 *ret_msec
= (time
->tv_sec
- now
.tv_sec
) * 1000 +
281 (time
->tv_usec
- now
.tv_usec
+ 999) / 1000;
288 __glutFreeTimers( void )
290 __GlutTimer
*cur
= g_timers
;
293 __GlutTimer
*next
= cur
->next
;