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#include "SDL_timer.h"
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer_c.h"
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_mutex.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_systimer.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* #define DEBUG_TIMERS */
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_timer_started = 0;
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_timer_running = 0;
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Data to handle a single periodic alarm */
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint32 SDL_alarm_interval = 0;
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_TimerCallback SDL_alarm_callback;
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Data used for a thread-based timer */
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_timer_threaded = 0;
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct _SDL_TimerID {
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 interval;
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_NewTimerCallback cb;
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	void *param;
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 last_alarm;
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct _SDL_TimerID *next;
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_TimerID SDL_timers = NULL;
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_mutex *SDL_timer_mutex;
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic volatile SDL_bool list_changed = SDL_FALSE;
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Set whether or not the timer should use a thread.
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   This should not be called while the timer subsystem is running.
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SetTimerThreaded(int value)
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_timer_started ) {
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Timer already initialized");
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = -1;
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = 0;
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_timer_threaded = value;
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return retval;
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_TimerInit(void)
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = 0;
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_timer_started ) {
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_TimerQuit();
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! SDL_timer_threaded ) {
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = SDL_SYS_TimerInit();
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_timer_threaded ) {
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_timer_mutex = SDL_CreateMutex();
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( retval == 0 ) {
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_timer_started = 1;
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(retval);
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_TimerQuit(void)
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetTimer(0, NULL);
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_timer_threaded < 2 ) {
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SYS_TimerQuit();
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_timer_threaded ) {
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_DestroyMutex(SDL_timer_mutex);
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_timer_mutex = NULL;
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_timer_started = 0;
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_timer_threaded = 0;
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_ThreadedTimerCheck(void)
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 now, ms;
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_TimerID t, prev, next;
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_bool removed;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_mutexP(SDL_timer_mutex);
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	list_changed = SDL_FALSE;
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	now = SDL_GetTicks();
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( prev = NULL, t = SDL_timers; t; t = next ) {
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		removed = SDL_FALSE;
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ms = t->interval - SDL_TIMESLICE;
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		next = t->next;
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (int)(now - t->last_alarm) > (int)ms ) {
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			struct _SDL_TimerID timer;
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( (now - t->last_alarm) < t->interval ) {
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				t->last_alarm += t->interval;
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				t->last_alarm = now;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_TIMERS
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			printf("Executing timer %p (thread = %d)\n",
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				t, SDL_ThreadID());
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			timer = *t;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_mutexV(SDL_timer_mutex);
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			ms = timer.cb(timer.interval, timer.param);
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_mutexP(SDL_timer_mutex);
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( list_changed ) {
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* Abort, list of timers modified */
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* FIXME: what if ms was changed? */
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( ms != t->interval ) {
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( ms ) {
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					t->interval = ROUND_RESOLUTION(ms);
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* Remove timer from the list */
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_TIMERS
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					printf("SDL: Removing timer %p\n", t);
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( prev ) {
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						prev->next = next;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					} else {
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						SDL_timers = next;
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_free(t);
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					--SDL_timer_running;
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					removed = SDL_TRUE;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Don't update prev if the timer has disappeared */
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ! removed ) {
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			prev = t;
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_mutexV(SDL_timer_mutex);
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_TimerID SDL_AddTimerInternal(Uint32 interval, SDL_NewTimerCallback callback, void *param)
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_TimerID t;
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	t = (SDL_TimerID) SDL_malloc(sizeof(struct _SDL_TimerID));
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( t ) {
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		t->interval = ROUND_RESOLUTION(interval);
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		t->cb = callback;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		t->param = param;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		t->last_alarm = SDL_GetTicks();
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		t->next = SDL_timers;
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_timers = t;
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		++SDL_timer_running;
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		list_changed = SDL_TRUE;
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_TIMERS
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("SDL_AddTimer(%d) = %08x num_timers = %d\n", interval, (Uint32)t, SDL_timer_running);
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return t;
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_TimerID SDL_AddTimer(Uint32 interval, SDL_NewTimerCallback callback, void *param)
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_TimerID t;
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! SDL_timer_mutex ) {
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_timer_started ) {
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("This platform doesn't support multiple timers");
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("You must call SDL_Init(SDL_INIT_TIMER) first");
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return NULL;
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! SDL_timer_threaded ) {
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Multiple timers require threaded events!");
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return NULL;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_mutexP(SDL_timer_mutex);
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	t = SDL_AddTimerInternal(interval, callback, param);
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_mutexV(SDL_timer_mutex);
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return t;
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_bool SDL_RemoveTimer(SDL_TimerID id)
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_TimerID t, prev = NULL;
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_bool removed;
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	removed = SDL_FALSE;
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_mutexP(SDL_timer_mutex);
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Look for id in the linked list of timers */
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (t = SDL_timers; t; prev=t, t = t->next ) {
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( t == id ) {
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if(prev) {
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				prev->next = t->next;
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_timers = t->next;
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(t);
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			--SDL_timer_running;
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			removed = SDL_TRUE;
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			list_changed = SDL_TRUE;
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_TIMERS
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("SDL_RemoveTimer(%08x) = %d num_timers = %d thread = %d\n", (Uint32)id, removed, SDL_timer_running, SDL_ThreadID());
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_mutexV(SDL_timer_mutex);
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return removed;
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Old style callback functions are wrapped through this */
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint32 SDLCALL callback_wrapper(Uint32 ms, void *param)
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_TimerCallback func = (SDL_TimerCallback) param;
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return (*func)(ms);
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SetTimer(Uint32 ms, SDL_TimerCallback callback)
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_TIMERS
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("SDL_SetTimer(%d)\n", ms);
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = 0;
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_timer_threaded ) {
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_mutexP(SDL_timer_mutex);
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_timer_running ) {	/* Stop any currently running timer */
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_timer_threaded ) {
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			while ( SDL_timers ) {
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_TimerID freeme = SDL_timers;
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_timers = SDL_timers->next;
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_free(freeme);
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_timer_running = 0;
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			list_changed = SDL_TRUE;
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SYS_StopTimer();
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_timer_running = 0;
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ms ) {
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_timer_threaded ) {
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( SDL_AddTimerInternal(ms, callback_wrapper, (void *)callback) == NULL ) {
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				retval = -1;
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_timer_running = 1;
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_alarm_interval = ms;
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_alarm_callback = callback;
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			retval = SDL_SYS_StartTimer();
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_timer_threaded ) {
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_mutexV(SDL_timer_mutex);
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return retval;
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
286