7c9768aac0adcab7fced6e00f7765f8480b5a55f
[mesa.git] / src / glut / directfb / callback.c
1 /*
2 * Copyright (C) 2006 Claudio Ciccani <klan@users.sf.net>
3 *
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.
8 *
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.
13 *
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
17 *
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/time.h>
24
25 #include "internal.h"
26
27
28 typedef void (GLUTCALLBACK *__GlutTimerCallback) ( int value );
29
30 typedef struct __GlutTimer_s {
31 struct timeval interval;
32 struct timeval expire;
33
34 __GlutTimerCallback func;
35 int value;
36
37 struct __GlutTimer_s *next;
38 } __GlutTimer;
39
40 /*****************************************************************************/
41
42 static __GlutTimer *g_timers = NULL;
43
44 /*****************************************************************************/
45
46
47 void GLUTAPIENTRY
48 glutDisplayFunc( void (GLUTCALLBACK *func) (void) )
49 {
50 display_func = func;
51 }
52
53
54 void GLUTAPIENTRY
55 glutReshapeFunc( void (GLUTCALLBACK *func) (int width, int height) )
56 {
57 reshape_func = func;
58 }
59
60
61 void GLUTAPIENTRY
62 glutKeyboardFunc( void (GLUTCALLBACK *func) (unsigned char key, int x, int y) )
63 {
64 keyboard_func = func;
65 }
66
67
68 void GLUTAPIENTRY
69 glutMouseFunc( void (GLUTCALLBACK *func) (int button, int state, int x, int y) )
70 {
71 mouse_func = func;
72 }
73
74
75 void GLUTAPIENTRY
76 glutMotionFunc( void (GLUTCALLBACK *func) (int x, int y) )
77 {
78 motion_func = func;
79 }
80
81
82 void GLUTAPIENTRY
83 glutPassiveMotionFunc( void (GLUTCALLBACK *func) (int x, int y) )
84 {
85 passive_motion_func = func;
86 }
87
88
89 void GLUTAPIENTRY
90 glutEntryFunc( void (GLUTCALLBACK *func) (int state) )
91 {
92 entry_func = func;
93 }
94
95
96 void GLUTAPIENTRY
97 glutVisibilityFunc( void (GLUTCALLBACK *func) (int state) )
98 {
99 visibility_func = func;
100 }
101
102
103 void GLUTAPIENTRY
104 glutMenuStateFunc( void (GLUTCALLBACK *func) (int state) )
105 {
106 menu_state_func = func;
107 }
108
109
110 void GLUTAPIENTRY
111 glutSpecialFunc( void (GLUTCALLBACK *func) (int key, int x, int y) )
112 {
113 special_func = func;
114 }
115
116
117 void GLUTAPIENTRY
118 glutSpaceballMotionFunc( void (GLUTCALLBACK *func) (int x, int y, int z) )
119 {
120 }
121
122
123 void GLUTAPIENTRY
124 glutSpaceballRotateFunc( void (GLUTCALLBACK *func) (int x, int y, int z) )
125 {
126 }
127
128
129 void GLUTAPIENTRY
130 glutSpaceballButtonFunc( void (GLUTCALLBACK *func) (int button, int state) )
131 {
132 }
133
134
135 void GLUTAPIENTRY
136 glutButtonBoxFunc( void (GLUTCALLBACK *func) (int button, int state) )
137 {
138 }
139
140
141 void GLUTAPIENTRY
142 glutDialsFunc( void (GLUTCALLBACK *func) (int dial, int value) )
143 {
144 }
145
146
147 void GLUTAPIENTRY
148 glutTabletMotionFunc( void (GLUTCALLBACK *func) (int x, int y) )
149 {
150 }
151
152
153 void GLUTAPIENTRY
154 glutTabletButtonFunc( void (GLUTCALLBACK *func) (int button, int state, int x, int y) )
155 {
156 }
157
158
159 void GLUTAPIENTRY
160 glutMenuStatusFunc( void (GLUTCALLBACK *func) (int status, int x, int y) )
161 {
162 }
163
164
165 void GLUTAPIENTRY
166 glutOverlayDisplayFunc( void (GLUTCALLBACK *func) (void) )
167 {
168 }
169
170
171 void GLUTAPIENTRY
172 glutWindowStatusFunc( void (GLUTCALLBACK *func) (int state) )
173 {
174 }
175
176
177 void GLUTAPIENTRY
178 glutKeyboardUpFunc( void (GLUTCALLBACK *func) (unsigned char key, int x, int y) )
179 {
180 keyboard_up_func = func;
181 }
182
183
184 void GLUTAPIENTRY
185 glutSpecialUpFunc( void (GLUTCALLBACK *func) (int key, int x, int y) )
186 {
187 special_up_func = func;
188 }
189
190
191 void GLUTAPIENTRY
192 glutJoystickFunc( void (GLUTCALLBACK *func)(unsigned int buttons, int x, int y, int z), int pollInterval )
193 {
194 joystick_func = func;
195 /* FIXME: take care of pollInterval */
196 }
197
198
199 void GLUTAPIENTRY
200 glutIdleFunc( void (GLUTCALLBACK *func) (void) )
201 {
202 idle_func = func;
203 }
204
205
206 void GLUTAPIENTRY
207 glutTimerFunc( unsigned int msec, void (GLUTCALLBACK *func) (int value), int value )
208 {
209 __GlutTimer *timer;
210
211 if (!func)
212 return;
213
214 timer = calloc( 1, sizeof(__GlutTimer) );
215 if (!timer)
216 __glutFatalError( "out of memory" );
217
218 timer->interval.tv_sec = msec / 1000;
219 timer->interval.tv_usec = (msec % 1000) * 1000;
220
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;
225
226 timer->func = func;
227 timer->value = value;
228
229 timer->next = g_timers;
230 g_timers = timer;
231 }
232
233
234 void
235 __glutHandleTimers( void )
236 {
237 __GlutTimer *cur;
238 struct timeval now;
239
240 for (cur = g_timers; cur; cur = cur->next ) {
241 gettimeofday( &now, NULL );
242
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))
246 {
247 g_idle = GL_FALSE;
248
249 cur->func( cur->value );
250
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;
254 }
255 }
256 }
257
258
259 GLboolean
260 __glutGetTimeout( int *ret_msec )
261 {
262 __GlutTimer *cur;
263 struct timeval *time = NULL;
264 struct timeval now;
265
266 for (cur = g_timers; cur; cur = cur->next) {
267 if (time == NULL ||
268 time->tv_sec > cur->expire.tv_sec ||
269 (time->tv_sec == cur->expire.tv_sec &&
270 time->tv_usec > cur->expire.tv_usec)) {
271 time = &cur->expire;
272 }
273 }
274
275 if (time == NULL)
276 return GL_FALSE;
277
278 gettimeofday( &now, NULL );
279
280 *ret_msec = (time->tv_sec - now.tv_sec) * 1000 +
281 (time->tv_usec - now.tv_usec + 999) / 1000;
282
283 return GL_TRUE;
284 }
285
286
287 void
288 __glutFreeTimers( void )
289 {
290 __GlutTimer *cur = g_timers;
291
292 while (cur) {
293 __GlutTimer *next = cur->next;
294 free( cur );
295 cur = next;
296 }
297
298 g_timers = NULL;
299 }
300