mirror of
git://projects.qi-hardware.com/gmenu2x.git
synced 2024-11-22 13:05:20 +02:00
Cleanups of Clock class
Don't make Clock a singleton. While there should be no reason to instantiate this class more than once, there is no problem with doing that either. Removing the singleton makes it easier to control access to the instance. It also avoids the rather nasty construct that was used to delete it. Make sure the timer callback function is a proper C function, since SDL is a C library. This requires some trickery to be able to call a private method from the callback, but I found a way using an intermediate nested class. The compiler should be able to inline this to eliminate any overhead. Also some minor cleanups.
This commit is contained in:
parent
f71ea3bcee
commit
a15339d425
@ -1,13 +1,12 @@
|
||||
#include <string>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "clock.h"
|
||||
|
||||
#include "debug.h"
|
||||
#include "inputmanager.h"
|
||||
|
||||
Clock *Clock::instance = NULL;
|
||||
#include <sys/time.h>
|
||||
|
||||
static void notify(void)
|
||||
|
||||
static void notify()
|
||||
{
|
||||
SDL_UserEvent e = {
|
||||
.type = SDL_USEREVENT,
|
||||
@ -21,23 +20,37 @@ static void notify(void)
|
||||
SDL_PushEvent((SDL_Event *) &e);
|
||||
}
|
||||
|
||||
static Uint32 clockCallback(Uint32 timeout, void *d)
|
||||
{
|
||||
unsigned int *old_ticks = (unsigned int *) d;
|
||||
unsigned int new_ticks = SDL_GetTicks();
|
||||
extern "C" Uint32 clockCallbackFunc(Uint32 timeout, void *d);
|
||||
|
||||
if (new_ticks > *old_ticks + timeout + 1000) {
|
||||
class Clock::Forwarder {
|
||||
static unsigned int clockCallbackFunc(Clock *clock, unsigned int timeout)
|
||||
{
|
||||
return clock->clockCallback(timeout);
|
||||
}
|
||||
friend Uint32 clockCallbackFunc(Uint32 timeout, void *d);
|
||||
};
|
||||
|
||||
extern "C" Uint32 clockCallbackFunc(Uint32 timeout, void *d)
|
||||
{
|
||||
return Clock::Forwarder::clockCallbackFunc(static_cast<Clock *>(d), timeout);
|
||||
}
|
||||
|
||||
unsigned int Clock::clockCallback(unsigned int timeout)
|
||||
{
|
||||
unsigned int now_ticks = SDL_GetTicks();
|
||||
|
||||
if (now_ticks > timeout_startms + timeout + 1000) {
|
||||
DEBUG("Suspend occured, restarting timer\n");
|
||||
*old_ticks = new_ticks;
|
||||
timeout_startms = now_ticks;
|
||||
return timeout;
|
||||
}
|
||||
|
||||
Clock::getInstance()->resetTimer();
|
||||
resetTimer();
|
||||
notify();
|
||||
return 60000;
|
||||
}
|
||||
|
||||
std::string &Clock::getTime(bool is24)
|
||||
std::string Clock::getTime(bool is24)
|
||||
{
|
||||
char buf[9];
|
||||
int h = hours;
|
||||
@ -47,11 +60,10 @@ std::string &Clock::getTime(bool is24)
|
||||
h -= 12;
|
||||
|
||||
sprintf(buf, "%02i:%02i%s", h, minutes, is24 ? "" : (pm ? "pm" : "am"));
|
||||
str = buf;
|
||||
return str;
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
int Clock::update(void)
|
||||
int Clock::update()
|
||||
{
|
||||
struct timeval tv;
|
||||
struct tm result;
|
||||
@ -63,7 +75,7 @@ int Clock::update(void)
|
||||
return result.tm_sec;
|
||||
}
|
||||
|
||||
void Clock::resetTimer(void)
|
||||
void Clock::resetTimer()
|
||||
{
|
||||
SDL_RemoveTimer(timer);
|
||||
timer = NULL;
|
||||
@ -78,12 +90,12 @@ void Clock::addTimer(int timeout)
|
||||
timeout = 60000;
|
||||
|
||||
timeout_startms = SDL_GetTicks();
|
||||
timer = SDL_AddTimer(timeout, clockCallback, &timeout_startms);
|
||||
timer = SDL_AddTimer(timeout, clockCallbackFunc, this);
|
||||
if (timer == NULL)
|
||||
ERROR("Could not initialize SDLTimer: %s\n", SDL_GetError());
|
||||
}
|
||||
|
||||
Clock::Clock(void)
|
||||
Clock::Clock()
|
||||
{
|
||||
tzset();
|
||||
|
||||
@ -94,17 +106,4 @@ Clock::Clock(void)
|
||||
Clock::~Clock()
|
||||
{
|
||||
SDL_RemoveTimer(timer);
|
||||
instance = NULL;
|
||||
}
|
||||
|
||||
Clock *Clock::getInstance(void)
|
||||
{
|
||||
if (!instance)
|
||||
instance = new Clock();
|
||||
return instance;
|
||||
}
|
||||
|
||||
bool Clock::isRunning(void)
|
||||
{
|
||||
return instance != NULL;
|
||||
}
|
||||
|
14
src/clock.h
14
src/clock.h
@ -6,23 +6,23 @@
|
||||
|
||||
class Clock {
|
||||
public:
|
||||
static Clock *getInstance();
|
||||
Clock();
|
||||
~Clock();
|
||||
|
||||
std::string &getTime(bool is24 = true);
|
||||
static bool isRunning();
|
||||
void resetTimer();
|
||||
std::string getTime(bool is24 = true);
|
||||
|
||||
class Forwarder;
|
||||
friend Forwarder;
|
||||
|
||||
private:
|
||||
Clock();
|
||||
void addTimer(int timeout);
|
||||
void resetTimer();
|
||||
int update();
|
||||
unsigned int clockCallback(unsigned int timeout);
|
||||
|
||||
static Clock *instance;
|
||||
SDL_TimerID timer;
|
||||
unsigned int timeout_startms;
|
||||
int minutes, hours;
|
||||
std::string str;
|
||||
};
|
||||
|
||||
#endif /* __CLOCK_H__ */
|
||||
|
@ -227,6 +227,8 @@ GMenu2X::GMenu2X()
|
||||
quit();
|
||||
}
|
||||
|
||||
clock.reset(new Clock());
|
||||
|
||||
s = Surface::openOutputSurface(resX, resY, confInt["videoBpp"]);
|
||||
|
||||
bg = NULL;
|
||||
@ -273,8 +275,6 @@ GMenu2X::GMenu2X()
|
||||
GMenu2X::~GMenu2X() {
|
||||
if (PowerSaver::isRunning())
|
||||
delete PowerSaver::getInstance();
|
||||
if (Clock::isRunning())
|
||||
delete Clock::getInstance();
|
||||
quit();
|
||||
|
||||
delete btnContextMenu;
|
||||
@ -625,7 +625,7 @@ void GMenu2X::paint() {
|
||||
sc.skinRes(batteryIcon)->blit( s, resX-19, bottomBarIconY );
|
||||
//s->write( font, tr[batstr.c_str()], 20, 170 );
|
||||
|
||||
s->write(font, Clock::getInstance()->getTime(),
|
||||
s->write(font, clock->getTime(),
|
||||
halfX, bottomBarTextY,
|
||||
Font::HAlignCenter, Font::VAlignMiddle);
|
||||
}
|
||||
|
@ -34,6 +34,7 @@
|
||||
#include <vector>
|
||||
|
||||
class Button;
|
||||
class Clock;
|
||||
class Font;
|
||||
class HelpPopup;
|
||||
class IconButton;
|
||||
@ -71,6 +72,7 @@ private:
|
||||
std::shared_ptr<Menu> menu;
|
||||
MediaMonitor *monitor;
|
||||
std::string batteryIcon;
|
||||
std::unique_ptr<Clock> clock;
|
||||
|
||||
std::vector<std::shared_ptr<Layer>> layers;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user