19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_TIMER_MACOS
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <Types.h>
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <Timer.h>
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <OSUtils.h>
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <Gestalt.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <Processes.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <LowMem.h>
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_timer_c.h"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MS_PER_TICK	(1000/60)		/* MacOS tick = 1/60 second */
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Note: This is only a step above the original 1/60s implementation.
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *       For a good implementation, see FastTimes.[ch], by Matt Slot.
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define USE_MICROSECONDS
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define WideTo64bit(w)	(*(UInt64 *) &(w))
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUInt64 start;
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_StartTicks(void)
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_MICROSECONDS
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UnsignedWide now;
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Microseconds(&now);
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	start = WideTo64bit(now);
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* FIXME: Should we implement a wrapping algorithm, like Win32? */
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint32 SDL_GetTicks(void)
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_MICROSECONDS
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UnsignedWide now;
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Microseconds(&now);
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return (Uint32)((WideTo64bit(now)-start)/1000);
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(LMGetTicks()*MS_PER_TICK);
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_Delay(Uint32 ms)
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_MICROSECONDS
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 end_ms;
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	end_ms = SDL_GetTicks() + ms;
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	do {
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* FIXME: Yield CPU? */ ;
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} while ( SDL_GetTicks() < end_ms );
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UInt32		unused; /* MJS */
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Delay(ms/MS_PER_TICK, &unused);
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Data to handle a single periodic alarm */
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef struct _ExtendedTimerRec
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	TMTask		     tmTask;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ProcessSerialNumber  taskPSN;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} ExtendedTimerRec, *ExtendedTimerPtr;
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic ExtendedTimerRec gExtendedTimerRec;
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SYS_TimerInit(void)
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We don't need a setup? */
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_TimerQuit(void)
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We don't need a cleanup? */
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Our Stub routine to set up and then call the real routine. */
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallpascal void TimerCallbackProc(TMTaskPtr tmTaskPtr)
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 ms;
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WakeUpProcess(&((ExtendedTimerPtr) tmTaskPtr)->taskPSN);
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ms = SDL_alarm_callback(SDL_alarm_interval);
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ms ) {
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_alarm_interval = ROUND_RESOLUTION(ms);
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		PrimeTime((QElemPtr)&gExtendedTimerRec.tmTask,
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		          SDL_alarm_interval);
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_alarm_interval = 0;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SYS_StartTimer(void)
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * Configure the global structure that stores the timing information.
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gExtendedTimerRec.tmTask.qLink = NULL;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gExtendedTimerRec.tmTask.qType = 0;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gExtendedTimerRec.tmTask.tmAddr = NewTimerUPP(TimerCallbackProc);
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gExtendedTimerRec.tmTask.tmCount = 0;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gExtendedTimerRec.tmTask.tmWakeUp = 0;
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gExtendedTimerRec.tmTask.tmReserved = 0;
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GetCurrentProcess(&gExtendedTimerRec.taskPSN);
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Install the task record */
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	InsXTime((QElemPtr)&gExtendedTimerRec.tmTask);
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Go! */
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PrimeTime((QElemPtr)&gExtendedTimerRec.tmTask, SDL_alarm_interval);
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_StopTimer(void)
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	RmvTime((QElemPtr)&gExtendedTimerRec.tmTask);
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_TIMER_MACOS */
153