diff --git a/src/Makefile.am b/src/Makefile.am index bd765b2..0ce802a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -13,7 +13,7 @@ gmenu2x_SOURCES = font.cpp button.cpp cpu.cpp dirdialog.cpp filedialog.cpp \ utilities.cpp wallpaperdialog.cpp \ browsedialog.cpp buttonbox.cpp dialog.cpp \ imageio.cpp powersaver.cpp monitor.cpp mediamonitor.cpp clock.cpp \ - helppopup.cpp + helppopup.cpp background.cpp battery.cpp noinst_HEADERS = font.h button.h cpu.h dirdialog.h \ filedialog.h filelister.h gmenu2x.h gp2x.h iconbutton.h imagedialog.h \ @@ -27,7 +27,7 @@ noinst_HEADERS = font.h button.h cpu.h dirdialog.h \ touchscreen.h translator.h utilities.h wallpaperdialog.h \ browsedialog.h buttonbox.h dialog.h \ imageio.h powersaver.h monitor.h mediamonitor.h clock.h \ - layer.h helppopup.h + layer.h helppopup.h background.h battery.h AM_CFLAGS= @CFLAGS@ @SDL_CFLAGS@ diff --git a/src/background.cpp b/src/background.cpp new file mode 100644 index 0000000..a8e94d4 --- /dev/null +++ b/src/background.cpp @@ -0,0 +1,46 @@ +// Various authors. +// License: GPL version 2 or later. + +#include "background.h" + +#include "gmenu2x.h" + + +Background::Background(GMenu2X &gmenu2x) + : gmenu2x(gmenu2x) + , battery(gmenu2x.sc) +{ +} + +void Background::paint(Surface &s) { + Font &font = *gmenu2x.font; + SurfaceCollection &sc = gmenu2x.sc; + + sc["bgmain"]->blit(&s, 0, 0); + + s.write(&font, clock.getTime(), + s.width() / 2, gmenu2x.bottomBarTextY, + Font::HAlignCenter, Font::VAlignMiddle); + + battery.getIcon().blit(&s, s.width() - 19, gmenu2x.bottomBarIconY); +} + +bool Background::handleButtonPress(InputManager::Button button) { + switch (button) { + case InputManager::CANCEL: + gmenu2x.showHelpPopup(); + return true; + case InputManager::SETTINGS: + gmenu2x.showSettings(); + return true; + case InputManager::MENU: + gmenu2x.contextMenu(); + return true; + default: + return false; + } +} + +bool Background::handleTouchscreen(Touchscreen &/*ts*/) { + return false; +} diff --git a/src/background.h b/src/background.h new file mode 100644 index 0000000..ced3470 --- /dev/null +++ b/src/background.h @@ -0,0 +1,32 @@ +// Various authors. +// License: GPL version 2 or later. + +#ifndef BACKGROUND_H +#define BACKGROUND_H + +#include "battery.h" +#include "clock.h" +#include "layer.h" + +class GMenu2X; + + +/** + * The backmost layer. + */ +class Background : public Layer { +public: + Background(GMenu2X &gmenu2x); + + // Layer implementation: + virtual void paint(Surface &s); + virtual bool handleButtonPress(InputManager::Button button); + virtual bool handleTouchscreen(Touchscreen &ts); + +private: + GMenu2X &gmenu2x; + Battery battery; + Clock clock; +}; + +#endif // BACKGROUND_H diff --git a/src/battery.cpp b/src/battery.cpp new file mode 100644 index 0000000..6ad9e20 --- /dev/null +++ b/src/battery.cpp @@ -0,0 +1,78 @@ +#include "battery.h" + +#include "surfacecollection.h" + +#include +#include +#include + + +/** + * Reads the current battery state and returns a number representing its level + * of charge. + * @return A number representing battery charge: 0 means fully discharged, + * 5 means fully charged, 6 represents running on external power. + */ +static unsigned short getBatteryLevel() +{ + FILE *batteryHandle = NULL, *usbHandle = NULL; + +#if defined(PLATFORM_A320) || defined(PLATFORM_GCW0) || defined(PLATFORM_NANONOTE) + usbHandle = fopen("/sys/class/power_supply/usb/online", "r"); +#endif + if (usbHandle) { + int usbval = 0; + fscanf(usbHandle, "%d", &usbval); + fclose(usbHandle); + if (usbval == 1) + return 6; + } + +#if defined(PLATFORM_A320) || defined(PLATFORM_GCW0) || defined(PLATFORM_NANONOTE) + batteryHandle = fopen("/sys/class/power_supply/battery/capacity", "r"); +#endif + if (batteryHandle) { + int battval = 0; + fscanf(batteryHandle, "%d", &battval); + fclose(batteryHandle); + + if (battval>90) return 5; + if (battval>70) return 4; + if (battval>50) return 3; + if (battval>30) return 2; + if (battval>10) return 1; + } + + return 0; +} + +Battery::Battery(SurfaceCollection &sc_) + : sc(sc_) +{ + lastUpdate = SDL_GetTicks(); + update(); +} + +const Surface &Battery::getIcon() +{ + // Check battery status every 60 seconds. + unsigned int now = SDL_GetTicks(); + if (now - lastUpdate >= 60000) { + lastUpdate = now; + update(); + } + + return *sc.skinRes(iconPath); +} + +void Battery::update() +{ + unsigned short battlevel = getBatteryLevel(); + if (battlevel > 5) { + iconPath = "imgs/battery/ac.png"; + } else { + std::stringstream ss; + ss << "imgs/battery/" << battlevel << ".png"; + ss >> iconPath; + } +} diff --git a/src/battery.h b/src/battery.h new file mode 100644 index 0000000..964e5fd --- /dev/null +++ b/src/battery.h @@ -0,0 +1,30 @@ +#ifndef __BATTERY_H__ +#define __BATTERY_H__ + +#include + +class Surface; +class SurfaceCollection; + + +/** + * Keeps track of the battery status. + */ +class Battery { +public: + Battery(SurfaceCollection &sc); + + /** + * Gets the icon that reflects the current battery status. + */ + const Surface &getIcon(); + +private: + void update(); + + SurfaceCollection ≻ + std::string iconPath; + unsigned int lastUpdate; +}; + +#endif /* __BATTERY_H__ */ diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index e440cee..d9fe99a 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -20,7 +20,7 @@ #include "gp2x.h" -#include "clock.h" +#include "background.h" #include "cpu.h" #include "debug.h" #include "filedialog.h" @@ -227,14 +227,13 @@ GMenu2X::GMenu2X() quit(); } - clock.reset(new Clock()); - s = Surface::openOutputSurface(resX, resY, confInt["videoBpp"]); bg = NULL; font = NULL; btnContextMenu = nullptr; setSkin(confStr["skin"], !fileExists(confStr["wallpaper"])); + layers.insert(layers.begin(), make_shared(*this)); initMenu(); monitor = new MediaMonitor(CARD_ROOT); @@ -377,7 +376,7 @@ void GMenu2X::initMenu() { //Add virtual links in the setting section else if (menu->getSections()[i]=="settings") { - menu->addActionLink(i,"GMenu2X",BIND(&GMenu2X::options),tr["Configure GMenu2X's options"],"skin:icons/configure.png"); + menu->addActionLink(i,"GMenu2X",BIND(&GMenu2X::showSettings),tr["Configure GMenu2X's options"],"skin:icons/configure.png"); menu->addActionLink(i,tr["Skin"],BIND(&GMenu2X::skinMenu),tr["Configure skin"],"skin:icons/skin.png"); menu->addActionLink(i,tr["Wallpaper"],BIND(&GMenu2X::changeWallpaper),tr["Change GMenu2X wallpaper"],"skin:icons/wallpaper.png"); if (fileExists(getHome()+"/log.txt")) @@ -601,9 +600,6 @@ void GMenu2X::writeTmp(int selelem, const string &selectordir) { } void GMenu2X::paint() { - //Background - sc["bgmain"]->blit(s,0,0); - for (auto layer : layers) { layer->paint(*s); } @@ -621,38 +617,14 @@ void GMenu2X::paint() { if (ts.available()) { btnContextMenu->paint(); } - - sc.skinRes(batteryIcon)->blit( s, resX-19, bottomBarIconY ); - //s->write( font, tr[batstr.c_str()], 20, 170 ); - - s->write(font, clock->getTime(), - halfX, bottomBarTextY, - Font::HAlignCenter, Font::VAlignMiddle); } void GMenu2X::main() { - - batteryIcon = "imgs/battery/0.png"; - long tickBattery = -60000; - if (!fileExists(CARD_ROOT)) CARD_ROOT = ""; bool quit = false; while (!quit) { - //check battery status every 60 seconds - long tickNow = SDL_GetTicks(); - if (tickNow - tickBattery >= 60000) { - tickBattery = tickNow; - unsigned short battlevel = getBatteryLevel(); - if (battlevel>5) { - batteryIcon = "imgs/battery/ac.png"; - } else { - stringstream ss; - ss << "imgs/battery/" << battlevel << ".png"; - ss >> batteryIcon; - } - } paint(); s->flip(); @@ -661,30 +633,17 @@ void GMenu2X::main() { if (ts.available()) { ts.poll(); btnContextMenu->handleTS(); - bool handled = false; - for (auto it = layers.rbegin(); !handled && it != layers.rend(); ++it) { - handled = (*it)->handleTouchscreen(ts); + for (auto it = layers.rbegin(); it != layers.rend(); ++it) { + if ((*it)->handleTouchscreen(ts)) { + break; + } } } InputManager::Button button = input.waitForPressedButton(); - bool handled = false; - for (auto it = layers.rbegin(); !handled && it != layers.rend(); ++it) { - handled = (*it)->handleButtonPress(button); - } - if (!handled) { - switch (button) { - case InputManager::CANCEL: - layers.push_back(make_shared(*this)); - break; - case InputManager::SETTINGS: - options(); - break; - case InputManager::MENU: - contextMenu(); - break; - default: - break; + for (auto it = layers.rbegin(); it != layers.rend(); ++it) { + if ((*it)->handleButtonPress(button)) { + break; } } @@ -720,7 +679,11 @@ void GMenu2X::explorer() { } } -void GMenu2X::options() { +void GMenu2X::showHelpPopup() { + layers.push_back(make_shared(*this)); +} + +void GMenu2X::showSettings() { #ifdef ENABLE_CPUFREQ int curMenuClock = confInt["menuClock"]; #endif @@ -1342,39 +1305,6 @@ typedef struct { unsigned short remocon; } MMSP2ADC; -unsigned short GMenu2X::getBatteryLevel() { - FILE *batteryHandle = NULL, - *usbHandle = NULL; - -#if defined(PLATFORM_A320) || defined(PLATFORM_GCW0) || defined(PLATFORM_NANONOTE) - usbHandle = fopen("/sys/class/power_supply/usb/online", "r"); -#endif - if (usbHandle) { - int usbval = 0; - fscanf(usbHandle, "%d", &usbval); - fclose(usbHandle); - if (usbval == 1) - return 6; - } - -#if defined(PLATFORM_A320) || defined(PLATFORM_GCW0) || defined(PLATFORM_NANONOTE) - batteryHandle = fopen("/sys/class/power_supply/battery/capacity", "r"); -#endif - if (batteryHandle) { - int battval = 0; - fscanf(batteryHandle, "%d", &battval); - fclose(batteryHandle); - - if (battval>90) return 5; - if (battval>70) return 4; - if (battval>50) return 3; - if (battval>30) return 2; - if (battval>10) return 1; - } - - return 0; -} - void GMenu2X::setInputSpeed() { SDL_EnableKeyRepeat(1,150); } diff --git a/src/gmenu2x.h b/src/gmenu2x.h index e48fe36..f808261 100644 --- a/src/gmenu2x.h +++ b/src/gmenu2x.h @@ -34,7 +34,6 @@ #include class Button; -class Clock; class Font; class HelpPopup; class IconButton; @@ -71,8 +70,6 @@ private: Touchscreen ts; std::shared_ptr menu; MediaMonitor *monitor; - std::string batteryIcon; - std::unique_ptr clock; std::vector> layers; @@ -93,11 +90,6 @@ private: void initCPULimits(); #endif - /*! - Reads the current battery state and returns a number representing it's level of charge - @return A number representing battery charge. 0 means fully discharged. 5 means fully charged. 6 represents a gp2x using AC power. - */ - unsigned short getBatteryLevel(); void browsePath(const std::string &path, std::vector* directories, std::vector* files); /*! Starts the scanning of the nand and sd filesystems, searching for dge and gpu files and creating the links in 2 dedicated sections. @@ -167,7 +159,8 @@ public: //Status functions void main(); - void options(); + void showHelpPopup(); + void showSettings(); void skinMenu(); void about(); void viewLog();