1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-07-02 17:52:54 +03:00

Put background painting and global keys in new Background layer

The battery status was put into a separate class instead of directly
into the Background class.
This commit is contained in:
Maarten ter Huurne 2013-08-09 18:01:51 +02:00
parent b0d1d9e55f
commit 346067896a
7 changed files with 205 additions and 96 deletions

View File

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

46
src/background.cpp Normal file
View File

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

32
src/background.h Normal file
View File

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

78
src/battery.cpp Normal file
View File

@ -0,0 +1,78 @@
#include "battery.h"
#include "surfacecollection.h"
#include <SDL.h>
#include <cstdio>
#include <sstream>
/**
* 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;
}
}

30
src/battery.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef __BATTERY_H__
#define __BATTERY_H__
#include <string>
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 &sc;
std::string iconPath;
unsigned int lastUpdate;
};
#endif /* __BATTERY_H__ */

View File

@ -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<Background>(*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<HelpPopup>(*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<HelpPopup>(*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);
}

View File

@ -34,7 +34,6 @@
#include <vector>
class Button;
class Clock;
class Font;
class HelpPopup;
class IconButton;
@ -71,8 +70,6 @@ private:
Touchscreen ts;
std::shared_ptr<Menu> menu;
MediaMonitor *monitor;
std::string batteryIcon;
std::unique_ptr<Clock> clock;
std::vector<std::shared_ptr<Layer>> 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<std::string>* directories, std::vector<std::string>* 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();