From 47bbc0b6733edcc08ae2c0a00cd65c021049c215 Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Sun, 21 Jul 2013 23:54:09 -0400 Subject: [PATCH] Added a clock on the bottom bar --- src/Makefile.am | 4 +- src/clock.cpp | 112 ++++++++++++++++++++++++++++++++++++++++++++++++ src/clock.h | 28 ++++++++++++ src/gmenu2x.cpp | 6 +++ 4 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 src/clock.cpp create mode 100644 src/clock.h diff --git a/src/Makefile.am b/src/Makefile.am index a896f34..78c9266 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -12,7 +12,7 @@ gmenu2x_SOURCES = asfont.cpp button.cpp cpu.cpp dirdialog.cpp filedialog.cpp \ textdialog.cpp textmanualdialog.cpp touchscreen.cpp translator.cpp \ utilities.cpp wallpaperdialog.cpp \ browsedialog.cpp buttonbox.cpp dialog.cpp \ - imageio.cpp powersaver.cpp monitor.cpp mediamonitor.cpp + imageio.cpp powersaver.cpp monitor.cpp mediamonitor.cpp clock.cpp noinst_HEADERS = asfont.h button.h cpu.h dirdialog.h FastDelegate.h \ filedialog.h filelister.h gmenu2x.h gp2x.h iconbutton.h imagedialog.h \ @@ -25,7 +25,7 @@ noinst_HEADERS = asfont.h button.h cpu.h dirdialog.h FastDelegate.h \ surfacecollection.h surface.h textdialog.h textmanualdialog.h \ touchscreen.h translator.h utilities.h wallpaperdialog.h \ browsedialog.h buttonbox.h dialog.h \ - imageio.h powersaver.h monitor.h mediamonitor.h + imageio.h powersaver.h monitor.h mediamonitor.h clock.h AM_CFLAGS= @CFLAGS@ @SDL_CFLAGS@ diff --git a/src/clock.cpp b/src/clock.cpp new file mode 100644 index 0000000..d3ee8d1 --- /dev/null +++ b/src/clock.cpp @@ -0,0 +1,112 @@ +#include +#include + +#include "clock.h" +#include "debug.h" +#include "inputmanager.h" + +Clock *Clock::instance = NULL; + +static void notify(void) +{ + SDL_UserEvent e = { + .type = SDL_USEREVENT, + .code = REPAINT_MENU, + .data1 = NULL, + .data2 = NULL, + }; + + /* Inject an user event, that will be handled as a "repaint" + * event by the InputManager */ + 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(); + + if (new_ticks > *old_ticks + timeout + 1000) { + DEBUG("Suspend occured, restarting timer\n"); + *old_ticks = new_ticks; + return timeout; + } + + Clock::getInstance()->resetTimer(); + notify(); + return 60000; +} + +std::string &Clock::getTime(bool is24) +{ + char buf[9]; + int h = hours; + bool pm = hours >= 12; + + if (!is24 && pm) + h -= 12; + + sprintf(buf, "%02i:%02i%s", h, minutes, is24 ? "" : (pm ? "pm" : "am")); + str = buf; + return str; +} + +int Clock::update(void) +{ + struct timeval tv; + struct tm result; + gettimeofday(&tv, NULL); + localtime_r(&tv.tv_sec, &result); + minutes = result.tm_min; + hours = result.tm_hour; + DEBUG("Time updated: %02i:%02i\n", hours, minutes); + return result.tm_sec; +} + +void Clock::resetTimer(void) +{ + SDL_RemoveTimer(timer); + timer = NULL; + + int secs = update(); + addTimer((60 - secs) * 1000); +} + +void Clock::addTimer(int timeout) +{ + if (timeout < 1000 || timeout > 60000) + timeout = 60000; + + timeout_startms = SDL_GetTicks(); + timer = SDL_AddTimer(timeout, clockCallback, &timeout_startms); + if (timer == NULL) + ERROR("Could not initialize SDLTimer: %s\n", SDL_GetError()); +} + +Clock::Clock(void) +{ + SDL_InitSubSystem(SDL_INIT_TIMER); + tzset(); + + int sec = update(); + addTimer((60 - sec) * 1000); +} + +Clock::~Clock() +{ + SDL_RemoveTimer(timer); + SDL_QuitSubSystem(SDL_INIT_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 new file mode 100644 index 0000000..6f94481 --- /dev/null +++ b/src/clock.h @@ -0,0 +1,28 @@ +#ifndef __CLOCK_H__ +#define __CLOCK_H__ + +#include +#include + +class Clock { +public: + static Clock *getInstance(); + ~Clock(); + + std::string &getTime(bool is24 = true); + static bool isRunning(); + void resetTimer(); + +private: + Clock(); + void addTimer(int timeout); + int update(); + + 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 bf60be4..c1af3f4 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -21,6 +21,7 @@ #include "gp2x.h" #include "asfont.h" +#include "clock.h" #include "cpu.h" #include "debug.h" #include "filedialog.h" @@ -273,6 +274,8 @@ GMenu2X::GMenu2X() GMenu2X::~GMenu2X() { if (PowerSaver::isRunning()) delete PowerSaver::getInstance(); + if (Clock::isRunning()) + delete Clock::getInstance(); quit(); delete menu; @@ -705,6 +708,9 @@ void GMenu2X::main() { //s->write( font, tr[batstr.c_str()], 20, 170 ); //On Screen Help + s->write(font, Clock::getInstance()->getTime(), + halfX, bottomBarTextY, + ASFont::HAlignCenter, ASFont::VAlignMiddle); if (helpDisplayed) { s->box(10,50,300,helpBoxHeight+4, skinConfColors[COLOR_MESSAGE_BOX_BG]);