From a15339d425961ccd3fc38d2ba3ea9ea881e58b67 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Tue, 6 Aug 2013 21:55:31 +0200 Subject: [PATCH] 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. --- src/clock.cpp | 63 ++++++++++++++++++++++++------------------------- src/clock.h | 14 +++++------ src/gmenu2x.cpp | 6 ++--- src/gmenu2x.h | 2 ++ 4 files changed, 43 insertions(+), 42 deletions(-) diff --git a/src/clock.cpp b/src/clock.cpp index 69f6c62..3462c02 100644 --- a/src/clock.cpp +++ b/src/clock.cpp @@ -1,13 +1,12 @@ -#include -#include - #include "clock.h" + #include "debug.h" #include "inputmanager.h" -Clock *Clock::instance = NULL; +#include -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(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; } diff --git a/src/clock.h b/src/clock.h index 6f94481..26c29dd 100644 --- a/src/clock.h +++ b/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__ */ diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index f414915..e440cee 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -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); } diff --git a/src/gmenu2x.h b/src/gmenu2x.h index 7dff664..e48fe36 100644 --- a/src/gmenu2x.h +++ b/src/gmenu2x.h @@ -34,6 +34,7 @@ #include class Button; +class Clock; class Font; class HelpPopup; class IconButton; @@ -71,6 +72,7 @@ private: std::shared_ptr menu; MediaMonitor *monitor; std::string batteryIcon; + std::unique_ptr clock; std::vector> layers;