Initial commit
This commit is contained in:
189
backends/timer/default/default-timer.cpp
Normal file
189
backends/timer/default/default-timer.cpp
Normal file
@@ -0,0 +1,189 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "common/scummsys.h"
|
||||
#include "backends/timer/default/default-timer.h"
|
||||
#include "common/util.h"
|
||||
#include "common/system.h"
|
||||
|
||||
struct TimerSlot {
|
||||
Common::TimerManager::TimerProc callback;
|
||||
void *refCon;
|
||||
Common::String id;
|
||||
uint32 interval; // in microseconds
|
||||
|
||||
uint32 nextFireTime; // in milliseconds
|
||||
uint32 nextFireTimeMicro; // microseconds part of nextFire
|
||||
|
||||
TimerSlot *next;
|
||||
|
||||
TimerSlot() : callback(nullptr), refCon(nullptr), interval(0), nextFireTime(0), nextFireTimeMicro(0), next(nullptr) {}
|
||||
};
|
||||
|
||||
void insertPrioQueue(TimerSlot *head, TimerSlot *newSlot) {
|
||||
// The head points to a fake anchor TimerSlot; this common
|
||||
// trick allows us to get rid of many special cases.
|
||||
|
||||
const uint32 nextFireTime = newSlot->nextFireTime;
|
||||
TimerSlot *slot = head;
|
||||
newSlot->next = nullptr;
|
||||
|
||||
// Insert the new slot into the sorted list of already scheduled
|
||||
// timers in such a way that the list stays sorted...
|
||||
while (true) {
|
||||
assert(slot);
|
||||
if (slot->next == nullptr || nextFireTime < slot->next->nextFireTime) {
|
||||
newSlot->next = slot->next;
|
||||
slot->next = newSlot;
|
||||
return;
|
||||
}
|
||||
slot = slot->next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
DefaultTimerManager::DefaultTimerManager() :
|
||||
_timerCallbackNext(0),
|
||||
_head(nullptr) {
|
||||
|
||||
_head = new TimerSlot();
|
||||
}
|
||||
|
||||
DefaultTimerManager::~DefaultTimerManager() {
|
||||
Common::StackLock lock(_mutex);
|
||||
|
||||
TimerSlot *slot = _head;
|
||||
while (slot) {
|
||||
TimerSlot *next = slot->next;
|
||||
delete slot;
|
||||
slot = next;
|
||||
}
|
||||
_head = nullptr;
|
||||
}
|
||||
|
||||
void DefaultTimerManager::handler() {
|
||||
Common::StackLock lock(_mutex);
|
||||
|
||||
uint32 curTime = g_system->getMillis(true);
|
||||
|
||||
// On slow systems this could still be run after destructor
|
||||
if (!_head)
|
||||
return;
|
||||
|
||||
// Repeat as long as there is a TimerSlot that is scheduled to fire.
|
||||
TimerSlot *slot = _head->next;
|
||||
while (slot && slot->nextFireTime < curTime) {
|
||||
// Remove the slot from the priority queue
|
||||
_head->next = slot->next;
|
||||
|
||||
// Update the fire time and reinsert the TimerSlot into the priority
|
||||
// queue.
|
||||
assert(slot->interval > 0);
|
||||
slot->nextFireTime += (slot->interval / 1000);
|
||||
slot->nextFireTimeMicro += (slot->interval % 1000);
|
||||
if (slot->nextFireTimeMicro > 1000) {
|
||||
slot->nextFireTime += slot->nextFireTimeMicro / 1000;
|
||||
slot->nextFireTimeMicro %= 1000;
|
||||
}
|
||||
insertPrioQueue(_head, slot);
|
||||
|
||||
// Invoke the timer callback
|
||||
assert(slot->callback);
|
||||
slot->callback(slot->refCon);
|
||||
|
||||
// Look at the next scheduled timer
|
||||
slot = _head->next;
|
||||
}
|
||||
}
|
||||
|
||||
void DefaultTimerManager::checkTimers(uint32 interval) {
|
||||
uint32 curTime = g_system->getMillis();
|
||||
|
||||
// Timer checking & firing
|
||||
if (curTime >= _timerCallbackNext) {
|
||||
handler();
|
||||
_timerCallbackNext = curTime + interval;
|
||||
}
|
||||
}
|
||||
|
||||
bool DefaultTimerManager::installTimerProc(TimerProc callback, int32 interval, void *refCon, const Common::String &id) {
|
||||
assert(interval > 0);
|
||||
Common::StackLock lock(_mutex);
|
||||
|
||||
if (_callbacks.contains(id)) {
|
||||
if (_callbacks[id] != callback) {
|
||||
error("Different callbacks are referred by same name (%s)", id.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto &curCallback : _callbacks) {
|
||||
if (curCallback._value == callback) {
|
||||
error("Same callback added twice (old name: %s, new name: %s)", curCallback._key.c_str(), id.c_str());
|
||||
}
|
||||
}
|
||||
_callbacks[id] = callback;
|
||||
|
||||
TimerSlot *slot = new TimerSlot;
|
||||
slot->callback = callback;
|
||||
slot->refCon = refCon;
|
||||
slot->id = id;
|
||||
slot->interval = interval;
|
||||
slot->nextFireTime = g_system->getMillis() + interval / 1000;
|
||||
slot->nextFireTimeMicro = interval % 1000;
|
||||
slot->next = nullptr;
|
||||
|
||||
insertPrioQueue(_head, slot);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void DefaultTimerManager::removeTimerProc(TimerProc callback) {
|
||||
Common::StackLock lock(_mutex);
|
||||
|
||||
TimerSlot *slot = _head;
|
||||
|
||||
while (slot->next) {
|
||||
if (slot->next->callback == callback) {
|
||||
TimerSlot *next = slot->next->next;
|
||||
delete slot->next;
|
||||
slot->next = next;
|
||||
} else {
|
||||
slot = slot->next;
|
||||
}
|
||||
}
|
||||
|
||||
// We need to remove all names referencing the timer proc here.
|
||||
//
|
||||
// Else we run into troubles, when the client code removes and readds timer
|
||||
// callbacks.
|
||||
//
|
||||
// Another issues occurs when one plays a game with ALSA as music driver,
|
||||
// returns to launcher and starts a different engine game with ALSA as music driver.
|
||||
// In this case the MPU401 code will add different timer procs with the
|
||||
// same name, resulting in two different callbacks added with the same
|
||||
// name and causing installTimerProc to error out.
|
||||
// A good test case is running a SCUMM with ALSA output and then a KYRA
|
||||
// game for example.
|
||||
for (TimerSlotMap::iterator i = _callbacks.begin(), end = _callbacks.end(); i != end; ++i) {
|
||||
if (i->_value == callback)
|
||||
_callbacks.erase(i);
|
||||
}
|
||||
}
|
||||
60
backends/timer/default/default-timer.h
Normal file
60
backends/timer/default/default-timer.h
Normal file
@@ -0,0 +1,60 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef BACKENDS_TIMER_DEFAULT_H
|
||||
#define BACKENDS_TIMER_DEFAULT_H
|
||||
|
||||
#include "common/str.h"
|
||||
#include "common/hash-str.h"
|
||||
#include "common/timer.h"
|
||||
#include "common/mutex.h"
|
||||
|
||||
struct TimerSlot;
|
||||
|
||||
class DefaultTimerManager : public Common::TimerManager {
|
||||
private:
|
||||
typedef Common::HashMap<Common::String, TimerProc, Common::IgnoreCase_Hash, Common::IgnoreCase_EqualTo> TimerSlotMap;
|
||||
|
||||
Common::Mutex _mutex;
|
||||
TimerSlot *_head;
|
||||
TimerSlotMap _callbacks;
|
||||
|
||||
uint32 _timerCallbackNext;
|
||||
|
||||
public:
|
||||
DefaultTimerManager();
|
||||
virtual ~DefaultTimerManager();
|
||||
virtual bool installTimerProc(TimerProc proc, int32 interval, void *refCon, const Common::String &id);
|
||||
virtual void removeTimerProc(TimerProc proc);
|
||||
|
||||
/**
|
||||
* Timer callback, to be invoked at regular time intervals by the backend.
|
||||
*/
|
||||
void handler();
|
||||
|
||||
/*
|
||||
* Ensure that the callback is called at regular time intervals.
|
||||
* Should be called from pollEvents() on backends without threads.
|
||||
*/
|
||||
void checkTimers(uint32 interval = 10);
|
||||
};
|
||||
|
||||
#endif
|
||||
90
backends/timer/psp/timer.cpp
Normal file
90
backends/timer/psp/timer.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
// Disable printf override in common/forbidden.h to avoid
|
||||
// clashes with pspdebug.h from the PSP SDK.
|
||||
// That header file uses
|
||||
// __attribute__((format(printf,1,2)));
|
||||
// which gets messed up by our override mechanism; this could
|
||||
// be avoided by either changing the PSP SDK to use the equally
|
||||
// legal and valid
|
||||
// __attribute__((format(__printf__,1,2)));
|
||||
// or by refining our printf override to use a varadic macro
|
||||
// (which then wouldn't be portable, though).
|
||||
// Anyway, for now we just disable the printf override globally
|
||||
// for the PSP port
|
||||
#define FORBIDDEN_SYMBOL_EXCEPTION_printf
|
||||
|
||||
#include "common/scummsys.h"
|
||||
|
||||
#if defined(__PSP__)
|
||||
#include <pspthreadman.h>
|
||||
|
||||
#include "common/scummsys.h"
|
||||
#include "common/timer.h"
|
||||
#include "backends/platform/psp/thread.h"
|
||||
#include "backends/timer/psp/timer.h"
|
||||
|
||||
//#define __PSP_DEBUG_FUNCS__ /* For debugging function calls */
|
||||
//#define __PSP_DEBUG_PRINT__ /* For debug printouts */
|
||||
|
||||
#include "backends/platform/psp/trace.h"
|
||||
|
||||
PspTimerManager::PspTimerManager(uint32 interval) : _interval(interval * 1000), _threadId(-1), _init(false) {
|
||||
DEBUG_ENTER_FUNC();
|
||||
|
||||
_threadId = sceKernelCreateThread("timerThread", thread, PRIORITY_TIMER_THREAD, STACK_TIMER_THREAD, THREAD_ATTR_USER, 0);
|
||||
|
||||
if (_threadId < 0) { // error
|
||||
PSP_ERROR("failed to create timer thread. Error code %d\n", _threadId);
|
||||
return;
|
||||
}
|
||||
|
||||
PspTimerManager *_this = this; // trick to get into context when the thread starts
|
||||
_init = true;
|
||||
|
||||
if (sceKernelStartThread(_threadId, sizeof(uint32 *), &_this) < 0) {
|
||||
PSP_ERROR("failed to start thread %d\n", _threadId);
|
||||
return;
|
||||
}
|
||||
|
||||
PSP_DEBUG_PRINT("created timer thread[%x]\n", _threadId);
|
||||
}
|
||||
|
||||
int PspTimerManager::thread(SceSize, void *__this) {
|
||||
DEBUG_ENTER_FUNC();
|
||||
PspTimerManager *_this = *(PspTimerManager **)__this; // get our this for the context
|
||||
|
||||
_this->timerThread();
|
||||
return 0;
|
||||
};
|
||||
|
||||
void PspTimerManager::timerThread() {
|
||||
DEBUG_ENTER_FUNC();
|
||||
|
||||
while (_init) {
|
||||
sceKernelDelayThread(_interval);
|
||||
PSP_DEBUG_PRINT("calling callback!\n");
|
||||
handler();
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* __PSP__ */
|
||||
40
backends/timer/psp/timer.h
Normal file
40
backends/timer/psp/timer.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef PSP_TIMER_H
|
||||
#define PSP_TIMER_H
|
||||
|
||||
#include "backends/timer/default/default-timer.h"
|
||||
|
||||
class PspTimerManager : public DefaultTimerManager {
|
||||
public:
|
||||
PspTimerManager(uint32 interval = 10);
|
||||
~PspTimerManager() { _init = false; }
|
||||
|
||||
static int thread(SceSize, void *__this); // static thread to use as bridge
|
||||
void timerThread();
|
||||
private:
|
||||
uint32 _interval;
|
||||
int _threadId;
|
||||
bool _init;
|
||||
};
|
||||
|
||||
#endif // PSP_TIMER_H
|
||||
64
backends/timer/sdl/sdl-timer.cpp
Normal file
64
backends/timer/sdl/sdl-timer.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "common/scummsys.h"
|
||||
|
||||
#if defined(SDL_BACKEND)
|
||||
|
||||
#include "backends/timer/sdl/sdl-timer.h"
|
||||
|
||||
#include "common/textconsole.h"
|
||||
|
||||
#if SDL_VERSION_ATLEAST(3, 0, 0)
|
||||
static Uint32 timer_handler(void *userdata, SDL_TimerID timerID, Uint32 interval) {
|
||||
((DefaultTimerManager *)userdata)->handler();
|
||||
return interval;
|
||||
}
|
||||
#else
|
||||
static Uint32 timer_handler(Uint32 interval, void *param) {
|
||||
((DefaultTimerManager *)param)->handler();
|
||||
return interval;
|
||||
}
|
||||
#endif
|
||||
|
||||
SdlTimerManager::SdlTimerManager() {
|
||||
#if !SDL_VERSION_ATLEAST(3, 0, 0)
|
||||
// Initializes the SDL timer subsystem
|
||||
if (SDL_InitSubSystem(SDL_INIT_TIMER) == -1) {
|
||||
error("Could not initialize SDL: %s", SDL_GetError());
|
||||
}
|
||||
#endif
|
||||
|
||||
// Creates the timer callback
|
||||
_timerID = SDL_AddTimer(10, &timer_handler, this);
|
||||
}
|
||||
|
||||
SdlTimerManager::~SdlTimerManager() {
|
||||
// Removes the timer callback
|
||||
SDL_RemoveTimer(_timerID);
|
||||
|
||||
#if !SDL_VERSION_ATLEAST(3, 0, 0)
|
||||
SDL_QuitSubSystem(SDL_INIT_TIMER);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
43
backends/timer/sdl/sdl-timer.h
Normal file
43
backends/timer/sdl/sdl-timer.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef BACKENDS_TIMER_SDL_H
|
||||
#define BACKENDS_TIMER_SDL_H
|
||||
|
||||
#include "backends/timer/default/default-timer.h"
|
||||
|
||||
#include "backends/platform/sdl/sdl-sys.h"
|
||||
|
||||
/**
|
||||
* SDL timer manager. Setups the timer callback for
|
||||
* DefaultTimerManager.
|
||||
*/
|
||||
class SdlTimerManager : public DefaultTimerManager {
|
||||
public:
|
||||
SdlTimerManager();
|
||||
virtual ~SdlTimerManager();
|
||||
|
||||
protected:
|
||||
SDL_TimerID _timerID;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user