mirror of
git://projects.qi-hardware.com/gmenu2x.git
synced 2024-11-26 10:04:05 +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 "clock.h"
|
||||||
|
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include "inputmanager.h"
|
#include "inputmanager.h"
|
||||||
|
|
||||||
Clock *Clock::instance = NULL;
|
#include <sys/time.h>
|
||||||
|
|
||||||
static void notify(void)
|
|
||||||
|
static void notify()
|
||||||
{
|
{
|
||||||
SDL_UserEvent e = {
|
SDL_UserEvent e = {
|
||||||
.type = SDL_USEREVENT,
|
.type = SDL_USEREVENT,
|
||||||
@ -21,23 +20,37 @@ static void notify(void)
|
|||||||
SDL_PushEvent((SDL_Event *) &e);
|
SDL_PushEvent((SDL_Event *) &e);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Uint32 clockCallback(Uint32 timeout, void *d)
|
extern "C" Uint32 clockCallbackFunc(Uint32 timeout, void *d);
|
||||||
{
|
|
||||||
unsigned int *old_ticks = (unsigned int *) d;
|
|
||||||
unsigned int new_ticks = SDL_GetTicks();
|
|
||||||
|
|
||||||
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");
|
DEBUG("Suspend occured, restarting timer\n");
|
||||||
*old_ticks = new_ticks;
|
timeout_startms = now_ticks;
|
||||||
return timeout;
|
return timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
Clock::getInstance()->resetTimer();
|
resetTimer();
|
||||||
notify();
|
notify();
|
||||||
return 60000;
|
return 60000;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string &Clock::getTime(bool is24)
|
std::string Clock::getTime(bool is24)
|
||||||
{
|
{
|
||||||
char buf[9];
|
char buf[9];
|
||||||
int h = hours;
|
int h = hours;
|
||||||
@ -47,11 +60,10 @@ std::string &Clock::getTime(bool is24)
|
|||||||
h -= 12;
|
h -= 12;
|
||||||
|
|
||||||
sprintf(buf, "%02i:%02i%s", h, minutes, is24 ? "" : (pm ? "pm" : "am"));
|
sprintf(buf, "%02i:%02i%s", h, minutes, is24 ? "" : (pm ? "pm" : "am"));
|
||||||
str = buf;
|
return std::string(buf);
|
||||||
return str;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int Clock::update(void)
|
int Clock::update()
|
||||||
{
|
{
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
struct tm result;
|
struct tm result;
|
||||||
@ -63,7 +75,7 @@ int Clock::update(void)
|
|||||||
return result.tm_sec;
|
return result.tm_sec;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Clock::resetTimer(void)
|
void Clock::resetTimer()
|
||||||
{
|
{
|
||||||
SDL_RemoveTimer(timer);
|
SDL_RemoveTimer(timer);
|
||||||
timer = NULL;
|
timer = NULL;
|
||||||
@ -78,12 +90,12 @@ void Clock::addTimer(int timeout)
|
|||||||
timeout = 60000;
|
timeout = 60000;
|
||||||
|
|
||||||
timeout_startms = SDL_GetTicks();
|
timeout_startms = SDL_GetTicks();
|
||||||
timer = SDL_AddTimer(timeout, clockCallback, &timeout_startms);
|
timer = SDL_AddTimer(timeout, clockCallbackFunc, this);
|
||||||
if (timer == NULL)
|
if (timer == NULL)
|
||||||
ERROR("Could not initialize SDLTimer: %s\n", SDL_GetError());
|
ERROR("Could not initialize SDLTimer: %s\n", SDL_GetError());
|
||||||
}
|
}
|
||||||
|
|
||||||
Clock::Clock(void)
|
Clock::Clock()
|
||||||
{
|
{
|
||||||
tzset();
|
tzset();
|
||||||
|
|
||||||
@ -94,17 +106,4 @@ Clock::Clock(void)
|
|||||||
Clock::~Clock()
|
Clock::~Clock()
|
||||||
{
|
{
|
||||||
SDL_RemoveTimer(timer);
|
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 {
|
class Clock {
|
||||||
public:
|
public:
|
||||||
static Clock *getInstance();
|
Clock();
|
||||||
~Clock();
|
~Clock();
|
||||||
|
|
||||||
std::string &getTime(bool is24 = true);
|
std::string getTime(bool is24 = true);
|
||||||
static bool isRunning();
|
|
||||||
void resetTimer();
|
class Forwarder;
|
||||||
|
friend Forwarder;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Clock();
|
|
||||||
void addTimer(int timeout);
|
void addTimer(int timeout);
|
||||||
|
void resetTimer();
|
||||||
int update();
|
int update();
|
||||||
|
unsigned int clockCallback(unsigned int timeout);
|
||||||
|
|
||||||
static Clock *instance;
|
|
||||||
SDL_TimerID timer;
|
SDL_TimerID timer;
|
||||||
unsigned int timeout_startms;
|
unsigned int timeout_startms;
|
||||||
int minutes, hours;
|
int minutes, hours;
|
||||||
std::string str;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* __CLOCK_H__ */
|
#endif /* __CLOCK_H__ */
|
||||||
|
@ -227,6 +227,8 @@ GMenu2X::GMenu2X()
|
|||||||
quit();
|
quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clock.reset(new Clock());
|
||||||
|
|
||||||
s = Surface::openOutputSurface(resX, resY, confInt["videoBpp"]);
|
s = Surface::openOutputSurface(resX, resY, confInt["videoBpp"]);
|
||||||
|
|
||||||
bg = NULL;
|
bg = NULL;
|
||||||
@ -273,8 +275,6 @@ GMenu2X::GMenu2X()
|
|||||||
GMenu2X::~GMenu2X() {
|
GMenu2X::~GMenu2X() {
|
||||||
if (PowerSaver::isRunning())
|
if (PowerSaver::isRunning())
|
||||||
delete PowerSaver::getInstance();
|
delete PowerSaver::getInstance();
|
||||||
if (Clock::isRunning())
|
|
||||||
delete Clock::getInstance();
|
|
||||||
quit();
|
quit();
|
||||||
|
|
||||||
delete btnContextMenu;
|
delete btnContextMenu;
|
||||||
@ -625,7 +625,7 @@ void GMenu2X::paint() {
|
|||||||
sc.skinRes(batteryIcon)->blit( s, resX-19, bottomBarIconY );
|
sc.skinRes(batteryIcon)->blit( s, resX-19, bottomBarIconY );
|
||||||
//s->write( font, tr[batstr.c_str()], 20, 170 );
|
//s->write( font, tr[batstr.c_str()], 20, 170 );
|
||||||
|
|
||||||
s->write(font, Clock::getInstance()->getTime(),
|
s->write(font, clock->getTime(),
|
||||||
halfX, bottomBarTextY,
|
halfX, bottomBarTextY,
|
||||||
Font::HAlignCenter, Font::VAlignMiddle);
|
Font::HAlignCenter, Font::VAlignMiddle);
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class Button;
|
class Button;
|
||||||
|
class Clock;
|
||||||
class Font;
|
class Font;
|
||||||
class HelpPopup;
|
class HelpPopup;
|
||||||
class IconButton;
|
class IconButton;
|
||||||
@ -71,6 +72,7 @@ private:
|
|||||||
std::shared_ptr<Menu> menu;
|
std::shared_ptr<Menu> menu;
|
||||||
MediaMonitor *monitor;
|
MediaMonitor *monitor;
|
||||||
std::string batteryIcon;
|
std::string batteryIcon;
|
||||||
|
std::unique_ptr<Clock> clock;
|
||||||
|
|
||||||
std::vector<std::shared_ptr<Layer>> layers;
|
std::vector<std::shared_ptr<Layer>> layers;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user