1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "common/scummsys.h"
24 #include "backends/timer/default/default-timer.h"
25 #include "common/util.h"
26 #include "common/system.h"
27 
28 struct TimerSlot {
29 	Common::TimerManager::TimerProc callback;
30 	void *refCon;
31 	Common::String id;
32 	uint32 interval;	// in microseconds
33 
34 	uint32 nextFireTime;	// in milliseconds
35 	uint32 nextFireTimeMicro;	// microseconds part of nextFire
36 
37 	TimerSlot *next;
38 
TimerSlotTimerSlot39 	TimerSlot() : callback(nullptr), refCon(nullptr), interval(0), nextFireTime(0), nextFireTimeMicro(0), next(nullptr) {}
40 };
41 
insertPrioQueue(TimerSlot * head,TimerSlot * newSlot)42 void insertPrioQueue(TimerSlot *head, TimerSlot *newSlot) {
43 	// The head points to a fake anchor TimerSlot; this common
44 	// trick allows us to get rid of many special cases.
45 
46 	const uint32 nextFireTime = newSlot->nextFireTime;
47 	TimerSlot *slot = head;
48 	newSlot->next = 0;
49 
50 	// Insert the new slot into the sorted list of already scheduled
51 	// timers in such a way that the list stays sorted...
52 	while (true) {
53 		assert(slot);
54 		if (slot->next == 0 || nextFireTime < slot->next->nextFireTime) {
55 			newSlot->next = slot->next;
56 			slot->next = newSlot;
57 			return;
58 		}
59 		slot = slot->next;
60 	}
61 }
62 
63 
DefaultTimerManager()64 DefaultTimerManager::DefaultTimerManager() :
65 	_head(0) {
66 
67 	_head = new TimerSlot();
68 }
69 
~DefaultTimerManager()70 DefaultTimerManager::~DefaultTimerManager() {
71 	Common::StackLock lock(_mutex);
72 
73 	TimerSlot *slot = _head;
74 	while (slot) {
75 		TimerSlot *next = slot->next;
76 		delete slot;
77 		slot = next;
78 	}
79 	_head = 0;
80 }
81 
handler()82 void DefaultTimerManager::handler() {
83 	Common::StackLock lock(_mutex);
84 
85 	uint32 curTime = g_system->getMillis(true);
86 
87 	// Repeat as long as there is a TimerSlot that is scheduled to fire.
88 	TimerSlot *slot = _head->next;
89 	while (slot && slot->nextFireTime < curTime) {
90 		// Remove the slot from the priority queue
91 		_head->next = slot->next;
92 
93 		// Update the fire time and reinsert the TimerSlot into the priority
94 		// queue.
95 		assert(slot->interval > 0);
96 		slot->nextFireTime += (slot->interval / 1000);
97 		slot->nextFireTimeMicro += (slot->interval % 1000);
98 		if (slot->nextFireTimeMicro > 1000) {
99 			slot->nextFireTime += slot->nextFireTimeMicro / 1000;
100 			slot->nextFireTimeMicro %= 1000;
101 		}
102 		insertPrioQueue(_head, slot);
103 
104 		// Invoke the timer callback
105 		assert(slot->callback);
106 		slot->callback(slot->refCon);
107 
108 		// Look at the next scheduled timer
109 		slot = _head->next;
110 	}
111 }
112 
installTimerProc(TimerProc callback,int32 interval,void * refCon,const Common::String & id)113 bool DefaultTimerManager::installTimerProc(TimerProc callback, int32 interval, void *refCon, const Common::String &id) {
114 	assert(interval > 0);
115 	Common::StackLock lock(_mutex);
116 
117 	if (_callbacks.contains(id)) {
118 		if (_callbacks[id] != callback) {
119 			error("Different callbacks are referred by same name (%s)", id.c_str());
120 		}
121 	}
122 	TimerSlotMap::const_iterator i;
123 
124 	for (i = _callbacks.begin(); i != _callbacks.end(); ++i) {
125 		if (i->_value == callback) {
126 			error("Same callback added twice (old name: %s, new name: %s)", i->_key.c_str(), id.c_str());
127 		}
128 	}
129 	_callbacks[id] = callback;
130 
131 	TimerSlot *slot = new TimerSlot;
132 	slot->callback = callback;
133 	slot->refCon = refCon;
134 	slot->id = id;
135 	slot->interval = interval;
136 	slot->nextFireTime = g_system->getMillis() + interval / 1000;
137 	slot->nextFireTimeMicro = interval % 1000;
138 	slot->next = 0;
139 
140 	insertPrioQueue(_head, slot);
141 
142 	return true;
143 }
144 
removeTimerProc(TimerProc callback)145 void DefaultTimerManager::removeTimerProc(TimerProc callback) {
146 	Common::StackLock lock(_mutex);
147 
148 	TimerSlot *slot = _head;
149 
150 	while (slot->next) {
151 		if (slot->next->callback == callback) {
152 			TimerSlot *next = slot->next->next;
153 			delete slot->next;
154 			slot->next = next;
155 		} else {
156 			slot = slot->next;
157 		}
158 	}
159 
160 	// We need to remove all names referencing the timer proc here.
161 	//
162 	// Else we run into troubles, when the client code removes and readds timer
163 	// callbacks.
164 	//
165 	// Another issues occurs when one plays a game with ALSA as music driver,
166 	// does RTL and starts a different engine game with ALSA as music driver.
167 	// In this case the MPU401 code will add different timer procs with the
168 	// same name, resulting in two different callbacks added with the same
169 	// name and causing installTimerProc to error out.
170 	// A good test case is running a SCUMM with ALSA output and then a KYRA
171 	// game for example.
172 	for (TimerSlotMap::iterator i = _callbacks.begin(), end = _callbacks.end(); i != end; ++i) {
173 		if (i->_value == callback)
174 			_callbacks.erase(i);
175 	}
176 }
177