1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-26 03:22:48 +02:00

Added a clock on the bottom bar

This commit is contained in:
Paul Cercueil 2013-07-21 23:54:09 -04:00
parent b0fa6db97d
commit 47bbc0b673
4 changed files with 148 additions and 2 deletions

View File

@ -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@

112
src/clock.cpp Normal file
View File

@ -0,0 +1,112 @@
#include <string>
#include <sys/time.h>
#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;
}

28
src/clock.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef __CLOCK_H__
#define __CLOCK_H__
#include <string>
#include <SDL.h>
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__ */

View File

@ -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]);