Date:2013-08-06 21:55:31 (10 years 7 months ago)
Author:Maarten ter Huurne
Commit:a15339d425961ccd3fc38d2ba3ea9ea881e58b67
Message: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.
Files: src/clock.cpp (6 diffs)
src/clock.h (1 diff)
src/gmenu2x.cpp (3 diffs)
src/gmenu2x.h (2 diffs)

Change Details

src/clock.cpp
1#include <string>
2#include <sys/time.h>
3
41#include "clock.h"
2
53#include "debug.h"
64#include "inputmanager.h"
75
8Clock *Clock::instance = NULL;
6#include <sys/time.h>
7
98
10static void notify(void)
9static void notify()
1110{
1211    SDL_UserEvent e = {
1312        .type = SDL_USEREVENT,
...... 
2120    SDL_PushEvent((SDL_Event *) &e);
2221}
2322
24static Uint32 clockCallback(Uint32 timeout, void *d)
23extern "C" Uint32 clockCallbackFunc(Uint32 timeout, void *d);
24
25class Clock::Forwarder {
26    static unsigned int clockCallbackFunc(Clock *clock, unsigned int timeout)
27    {
28        return clock->clockCallback(timeout);
29    }
30    friend Uint32 clockCallbackFunc(Uint32 timeout, void *d);
31};
32
33extern "C" Uint32 clockCallbackFunc(Uint32 timeout, void *d)
2534{
26    unsigned int *old_ticks = (unsigned int *) d;
27    unsigned int new_ticks = SDL_GetTicks();
35    return Clock::Forwarder::clockCallbackFunc(static_cast<Clock *>(d), timeout);
36}
2837
29    if (new_ticks > *old_ticks + timeout + 1000) {
38unsigned int Clock::clockCallback(unsigned int timeout)
39{
40    unsigned int now_ticks = SDL_GetTicks();
41
42    if (now_ticks > timeout_startms + timeout + 1000) {
3043        DEBUG("Suspend occured, restarting timer\n");
31        *old_ticks = new_ticks;
44        timeout_startms = now_ticks;
3245        return timeout;
3346    }
3447
35    Clock::getInstance()->resetTimer();
48    resetTimer();
3649    notify();
3750    return 60000;
3851}
3952
40std::string &Clock::getTime(bool is24)
53std::string Clock::getTime(bool is24)
4154{
4255    char buf[9];
4356    int h = hours;
...... 
4760        h -= 12;
4861
4962    sprintf(buf, "%02i:%02i%s", h, minutes, is24 ? "" : (pm ? "pm" : "am"));
50    str = buf;
51    return str;
63    return std::string(buf);
5264}
5365
54int Clock::update(void)
66int Clock::update()
5567{
5668    struct timeval tv;
5769    struct tm result;
...... 
6375    return result.tm_sec;
6476}
6577
66void Clock::resetTimer(void)
78void Clock::resetTimer()
6779{
6880    SDL_RemoveTimer(timer);
6981    timer = NULL;
...... 
7890        timeout = 60000;
7991
8092    timeout_startms = SDL_GetTicks();
81    timer = SDL_AddTimer(timeout, clockCallback, &timeout_startms);
93    timer = SDL_AddTimer(timeout, clockCallbackFunc, this);
8294    if (timer == NULL)
8395        ERROR("Could not initialize SDLTimer: %s\n", SDL_GetError());
8496}
8597
86Clock::Clock(void)
98Clock::Clock()
8799{
88100    tzset();
89101
...... 
94106Clock::~Clock()
95107{
96108    SDL_RemoveTimer(timer);
97    instance = NULL;
98}
99
100Clock *Clock::getInstance(void)
101{
102    if (!instance)
103        instance = new Clock();
104    return instance;
105}
106
107bool Clock::isRunning(void)
108{
109    return instance != NULL;
110109}
src/clock.h
66
77class Clock {
88public:
9    static Clock *getInstance();
9    Clock();
1010    ~Clock();
1111
12    std::string &getTime(bool is24 = true);
13    static bool isRunning();
14    void resetTimer();
12    std::string getTime(bool is24 = true);
13
14    class Forwarder;
15    friend Forwarder;
1516
1617private:
17    Clock();
1818    void addTimer(int timeout);
19    void resetTimer();
1920    int update();
21    unsigned int clockCallback(unsigned int timeout);
2022
21    static Clock *instance;
2223    SDL_TimerID timer;
2324    unsigned int timeout_startms;
2425    int minutes, hours;
25    std::string str;
2626};
2727
2828#endif /* __CLOCK_H__ */
src/gmenu2x.cpp
227227        quit();
228228    }
229229
230    clock.reset(new Clock());
231
230232    s = Surface::openOutputSurface(resX, resY, confInt["videoBpp"]);
231233
232234    bg = NULL;
...... 
273275GMenu2X::~GMenu2X() {
274276    if (PowerSaver::isRunning())
275277        delete PowerSaver::getInstance();
276    if (Clock::isRunning())
277        delete Clock::getInstance();
278278    quit();
279279
280280    delete btnContextMenu;
...... 
625625    sc.skinRes(batteryIcon)->blit( s, resX-19, bottomBarIconY );
626626    //s->write( font, tr[batstr.c_str()], 20, 170 );
627627
628    s->write(font, Clock::getInstance()->getTime(),
628    s->write(font, clock->getTime(),
629629                halfX, bottomBarTextY,
630630                Font::HAlignCenter, Font::VAlignMiddle);
631631}
src/gmenu2x.h
3434#include <vector>
3535
3636class Button;
37class Clock;
3738class Font;
3839class HelpPopup;
3940class IconButton;
...... 
7172    std::shared_ptr<Menu> menu;
7273    MediaMonitor *monitor;
7374    std::string batteryIcon;
75    std::unique_ptr<Clock> clock;
7476
7577    std::vector<std::shared_ptr<Layer>> layers;
7678

Archive Download the corresponding diff file



interactive