2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 #ifdef SDL_TIMER_MACOS
30 #include <Processes.h>
34 #include "SDL_timer.h"
35 #include "../SDL_timer_c.h"
37 #include "FastTimes.h"
39 #if TARGET_API_MAC_CARBON
40 #define NewTimerProc NewTimerUPP
43 #define MS_PER_TICK (1000.0/60.0) /* MacOS tick = 1/60 second */
46 #define kTwoPower32 (4294967296.0) /* 2^32 */
48 static double start_tick;
49 static int is_fast_inited = 0;
51 void SDL_StartTicks(void)
53 if ( ! is_fast_inited ) // important to check or FastTime may hang machine!
56 start_tick = FastMicroseconds();
59 Uint32 SDL_GetTicks(void)
62 if ( ! is_fast_inited )
65 return FastMilliseconds();
68 void SDL_Delay(Uint32 ms)
72 stop = SDL_GetTicks() + ms;
74 #if TARGET_API_MAC_CARBON
82 } while ( stop > now );
86 void SDL_StartTicks(void)
88 // FIXME: Should we implement a wrapping algorithm, like Win32?
91 Uint32 SDL_GetTicks(void)
97 return ( ms.lo / 1000 );
100 void SDL_Delay(Uint32 ms)
103 UnsignedWide microsecs;
106 Microseconds (µsecs);
108 stop = microsecs.lo + (ms * 1000);
110 while ( stop > microsecs.lo ) {
114 Microseconds (µsecs);
119 /* Data to handle a single periodic alarm */
120 typedef struct _ExtendedTimerRec
123 ProcessSerialNumber taskPSN;
124 } ExtendedTimerRec, *ExtendedTimerPtr;
126 static ExtendedTimerRec gExtendedTimerRec;
129 int SDL_SYS_TimerInit(void)
137 void SDL_SYS_TimerQuit(void)
139 /* We don't need a cleanup? */
143 /* Our Stub routine to set up and then call the real routine. */
144 pascal void TimerCallbackProc(TMTaskPtr tmTaskPtr)
148 WakeUpProcess(&((ExtendedTimerPtr) tmTaskPtr)->taskPSN);
150 ms = SDL_alarm_callback(SDL_alarm_interval);
152 SDL_alarm_interval = ROUND_RESOLUTION(ms);
153 PrimeTime((QElemPtr)&gExtendedTimerRec.tmTask,
156 SDL_alarm_interval = 0;
160 int SDL_SYS_StartTimer(void)
163 * Configure the global structure that stores the timing information.
165 gExtendedTimerRec.tmTask.qLink = NULL;
166 gExtendedTimerRec.tmTask.qType = 0;
167 gExtendedTimerRec.tmTask.tmAddr = NewTimerProc(TimerCallbackProc);
168 gExtendedTimerRec.tmTask.tmCount = 0;
169 gExtendedTimerRec.tmTask.tmWakeUp = 0;
170 gExtendedTimerRec.tmTask.tmReserved = 0;
171 GetCurrentProcess(&gExtendedTimerRec.taskPSN);
173 /* Install the task record */
174 InsXTime((QElemPtr)&gExtendedTimerRec.tmTask);
177 PrimeTime((QElemPtr)&gExtendedTimerRec.tmTask, SDL_alarm_interval);
181 void SDL_SYS_StopTimer(void)
183 RmvTime((QElemPtr)&gExtendedTimerRec.tmTask);
186 #endif /* SDL_TIMER_MACOS */