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 | ||
4 | 1 | #include "clock.h" |
2 | ||
5 | 3 | #include "debug.h" |
6 | 4 | #include "inputmanager.h" |
7 | 5 | |
8 | Clock *Clock::instance = NULL; | |
6 | #include <sys/time.h> | |
7 | ||
9 | 8 | |
10 | static void notify(void) | |
9 | static void notify() | |
11 | 10 | { |
12 | 11 | SDL_UserEvent e = { |
13 | 12 | .type = SDL_USEREVENT, |
... | ... | |
21 | 20 | SDL_PushEvent((SDL_Event *) &e); |
22 | 21 | } |
23 | 22 | |
24 | static Uint32 clockCallback(Uint32 timeout, void *d) | |
23 | extern "C" Uint32 clockCallbackFunc(Uint32 timeout, void *d); | |
24 | ||
25 | class 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 | ||
33 | extern "C" Uint32 clockCallbackFunc(Uint32 timeout, void *d) | |
25 | 34 | { |
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 | } | |
28 | 37 | |
29 | if (new_ticks > *old_ticks + timeout + 1000) { | |
38 | unsigned int Clock::clockCallback(unsigned int timeout) | |
39 | { | |
40 | unsigned int now_ticks = SDL_GetTicks(); | |
41 | ||
42 | if (now_ticks > timeout_startms + timeout + 1000) { | |
30 | 43 | DEBUG("Suspend occured, restarting timer\n"); |
31 | *old_ticks = new_ticks; | |
44 | timeout_startms = now_ticks; | |
32 | 45 | return timeout; |
33 | 46 | } |
34 | 47 | |
35 | Clock::getInstance()->resetTimer(); | |
48 | resetTimer(); | |
36 | 49 | notify(); |
37 | 50 | return 60000; |
38 | 51 | } |
39 | 52 | |
40 | std::string &Clock::getTime(bool is24) | |
53 | std::string Clock::getTime(bool is24) | |
41 | 54 | { |
42 | 55 | char buf[9]; |
43 | 56 | int h = hours; |
... | ... | |
47 | 60 | h -= 12; |
48 | 61 | |
49 | 62 | sprintf(buf, "%02i:%02i%s", h, minutes, is24 ? "" : (pm ? "pm" : "am")); |
50 | str = buf; | |
51 | return str; | |
63 | return std::string(buf); | |
52 | 64 | } |
53 | 65 | |
54 | int Clock::update(void) | |
66 | int Clock::update() | |
55 | 67 | { |
56 | 68 | struct timeval tv; |
57 | 69 | struct tm result; |
... | ... | |
63 | 75 | return result.tm_sec; |
64 | 76 | } |
65 | 77 | |
66 | void Clock::resetTimer(void) | |
78 | void Clock::resetTimer() | |
67 | 79 | { |
68 | 80 | SDL_RemoveTimer(timer); |
69 | 81 | timer = NULL; |
... | ... | |
78 | 90 | timeout = 60000; |
79 | 91 | |
80 | 92 | timeout_startms = SDL_GetTicks(); |
81 | timer = SDL_AddTimer(timeout, clockCallback, &timeout_startms); | |
93 | timer = SDL_AddTimer(timeout, clockCallbackFunc, this); | |
82 | 94 | if (timer == NULL) |
83 | 95 | ERROR("Could not initialize SDLTimer: %s\n", SDL_GetError()); |
84 | 96 | } |
85 | 97 | |
86 | Clock::Clock(void) | |
98 | Clock::Clock() | |
87 | 99 | { |
88 | 100 | tzset(); |
89 | 101 | |
... | ... | |
94 | 106 | Clock::~Clock() |
95 | 107 | { |
96 | 108 | SDL_RemoveTimer(timer); |
97 | instance = NULL; | |
98 | } | |
99 | ||
100 | Clock *Clock::getInstance(void) | |
101 | { | |
102 | if (!instance) | |
103 | instance = new Clock(); | |
104 | return instance; | |
105 | } | |
106 | ||
107 | bool Clock::isRunning(void) | |
108 | { | |
109 | return instance != NULL; | |
110 | 109 | } |
src/clock.h | ||
---|---|---|
6 | 6 | |
7 | 7 | class Clock { |
8 | 8 | public: |
9 | static Clock *getInstance(); | |
9 | Clock(); | |
10 | 10 | ~Clock(); |
11 | 11 | |
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; | |
15 | 16 | |
16 | 17 | private: |
17 | Clock(); | |
18 | 18 | void addTimer(int timeout); |
19 | void resetTimer(); | |
19 | 20 | int update(); |
21 | unsigned int clockCallback(unsigned int timeout); | |
20 | 22 | |
21 | static Clock *instance; | |
22 | 23 | SDL_TimerID timer; |
23 | 24 | unsigned int timeout_startms; |
24 | 25 | int minutes, hours; |
25 | std::string str; | |
26 | 26 | }; |
27 | 27 | |
28 | 28 | #endif /* __CLOCK_H__ */ |
src/gmenu2x.cpp | ||
---|---|---|
227 | 227 | quit(); |
228 | 228 | } |
229 | 229 | |
230 | clock.reset(new Clock()); | |
231 | ||
230 | 232 | s = Surface::openOutputSurface(resX, resY, confInt["videoBpp"]); |
231 | 233 | |
232 | 234 | bg = NULL; |
... | ... | |
273 | 275 | GMenu2X::~GMenu2X() { |
274 | 276 | if (PowerSaver::isRunning()) |
275 | 277 | delete PowerSaver::getInstance(); |
276 | if (Clock::isRunning()) | |
277 | delete Clock::getInstance(); | |
278 | 278 | quit(); |
279 | 279 | |
280 | 280 | delete btnContextMenu; |
... | ... | |
625 | 625 | sc.skinRes(batteryIcon)->blit( s, resX-19, bottomBarIconY ); |
626 | 626 | //s->write( font, tr[batstr.c_str()], 20, 170 ); |
627 | 627 | |
628 | s->write(font, Clock::getInstance()->getTime(), | |
628 | s->write(font, clock->getTime(), | |
629 | 629 | halfX, bottomBarTextY, |
630 | 630 | Font::HAlignCenter, Font::VAlignMiddle); |
631 | 631 | } |
src/gmenu2x.h | ||
---|---|---|
34 | 34 | #include <vector> |
35 | 35 | |
36 | 36 | class Button; |
37 | class Clock; | |
37 | 38 | class Font; |
38 | 39 | class HelpPopup; |
39 | 40 | class IconButton; |
... | ... | |
71 | 72 | std::shared_ptr<Menu> menu; |
72 | 73 | MediaMonitor *monitor; |
73 | 74 | std::string batteryIcon; |
75 | std::unique_ptr<Clock> clock; | |
74 | 76 | |
75 | 77 | std::vector<std::shared_ptr<Layer>> layers; |
76 | 78 |
Branches:
install_locations
master
opkrun
packages