From 944ab86f9c2b02ae03b0d915f75ad05eff3019a5 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Fri, 23 Dec 2011 12:30:15 +0100 Subject: [PATCH] Cleanup of touch screen code. Initialize in constructor and clean up in destructor instead of having separate init() and deinit() methods. Don't close file descriptor if open failed (ts_fd == -1). Renamed initialized() to available(), since it tests whether the touch screen was found, not whether an initialization was attempted. Improved code layout and minor other cleanups. --- src/browsedialog.cpp | 6 ++--- src/gmenu2x.cpp | 14 ++++------- src/gmenu2x.h | 2 +- src/inputdialog.cpp | 8 +++--- src/settingsdialog.cpp | 6 ++--- src/touchscreen.cpp | 55 +++++++++++++++++++----------------------- src/touchscreen.h | 28 +++++++++------------ 7 files changed, 53 insertions(+), 66 deletions(-) diff --git a/src/browsedialog.cpp b/src/browsedialog.cpp index 1737beb..d8f33f3 100644 --- a/src/browsedialog.cpp +++ b/src/browsedialog.cpp @@ -64,7 +64,7 @@ bool BrowseDialog::exec() selected = 0; close = false; while (!close) { - if (gmenu2x->ts.initialized()) gmenu2x->ts.poll(); + if (gmenu2x->ts.available()) gmenu2x->ts.poll(); paint(); @@ -111,7 +111,7 @@ void BrowseDialog::handleInput() action = getAction(button); } - if (gmenu2x->ts.initialized() && gmenu2x->ts.pressed() && !gmenu2x->ts.inRect(touchRect)) ts_pressed = false; + if (gmenu2x->ts.available() && gmenu2x->ts.pressed() && !gmenu2x->ts.inRect(touchRect)) ts_pressed = false; if (action == BrowseDialog::ACT_SELECT && (*fl)[selected] == "..") action = BrowseDialog::ACT_GOUP; @@ -250,7 +250,7 @@ void BrowseDialog::paint() icon->blit(gmenu2x->s, 5, offsetY); gmenu2x->s->write(gmenu2x->font, (*fl)[i], 24, offsetY + 8, ASFont::HAlignLeft, ASFont::VAlignMiddle); - if (gmenu2x->ts.initialized() && gmenu2x->ts.pressed() && gmenu2x->ts.inRect(touchRect.x, offsetY + 3, touchRect.w, rowHeight)) { + if (gmenu2x->ts.available() && gmenu2x->ts.pressed() && gmenu2x->ts.inRect(touchRect.x, offsetY + 3, touchRect.w, rowHeight)) { ts_pressed = true; selected = i; } diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index d83ac11..ff8afda 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -262,10 +262,7 @@ void GMenu2X::init() { /* gp2x_mem = open("/dev/mem", O_RDWR); gp2x_memregs=(unsigned short *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, gp2x_mem, 0xc0000000); MEM_REG=&gp2x_memregs[0]; - if (f200) { - //if wm97xx fails to open, set f200 to false to prevent any further access to the touchscreen - f200 = ts.init(); - }*/ + */ #else batteryHandle = fopen("/sys/class/power_supply/battery/capacity", "r"); usbHandle = fopen("/sys/class/power_supply/usb/online", "r"); @@ -287,8 +284,7 @@ void GMenu2X::deinit() { gp2x_memregs[0x290C>>1]=640; close(gp2x_mem); } - - if (f200) ts.deinit();*/ +*/ #else if (batteryHandle) fclose(batteryHandle); if (backlightHandle) fclose(backlightHandle); @@ -1059,7 +1055,7 @@ void GMenu2X::main() { } } - if (ts.initialized()) { + if (ts.available()) { btnContextMenu.paint(); } //check battery status every 60 seconds @@ -1123,7 +1119,7 @@ void GMenu2X::main() { s->flip(); //touchscreen - if (ts.initialized()) { + if (ts.available()) { ts.poll(); btnContextMenu.handleTS(); re.x = 0; re.y = 0; re.h = skinConfInt["topBarHeight"]; re.w = resX; @@ -1593,7 +1589,7 @@ void GMenu2X::contextMenu() { s->flip(); //touchscreen - if (ts.initialized()) { + if (ts.available()) { ts.poll(); if (ts.released()) { if (!ts.inRect(box)) diff --git a/src/gmenu2x.h b/src/gmenu2x.h index 504ed9a..e82e9d3 100644 --- a/src/gmenu2x.h +++ b/src/gmenu2x.h @@ -174,7 +174,7 @@ public: //firmware type and version std::string fwType, fwVersion; - bool isF200() { return ts.initialized(); } + bool isF200() { return ts.available(); } // Open2x settings --------------------------------------------------------- bool o2x_usb_net_on_boot, o2x_ftp_on_boot, o2x_telnet_on_boot, o2x_gp2xjoy_on_boot, o2x_usb_host_on_boot, o2x_usb_hid_on_boot, o2x_usb_storage_on_boot; diff --git a/src/inputdialog.cpp b/src/inputdialog.cpp index 1f335a4..4ec37b7 100644 --- a/src/inputdialog.cpp +++ b/src/inputdialog.cpp @@ -174,7 +174,7 @@ bool InputDialog::exec() { gmenu2x->skinConfColors[COLOR_SELECTION_BG]); } - if (ts.initialized()) ts.poll(); + if (ts.available()) ts.poll(); drawVirtualKeyboard(); gmenu2x->s->flip(); @@ -291,7 +291,7 @@ void InputDialog::drawVirtualKeyboard() { }; //if ts on rect, change selection - if (ts.initialized() && ts.pressed() && ts.inRect(re)) { + if (ts.available() && ts.pressed() && ts.inRect(re)) { selCol = xc; selRow = l; } @@ -309,7 +309,7 @@ void InputDialog::drawVirtualKeyboard() { //Ok/Cancel SDL_Rect re = {kbLeft-1, KB_TOP+kb->size()*KEY_HEIGHT, kbLength*KEY_WIDTH/2-1, KEY_HEIGHT-1}; gmenu2x->s->rectangle(re, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); - if (ts.initialized() && ts.pressed() && ts.inRect(re)) { + if (ts.available() && ts.pressed() && ts.inRect(re)) { selCol = 0; selRow = kb->size(); } @@ -320,7 +320,7 @@ void InputDialog::drawVirtualKeyboard() { re.x = kbLeft + kbLength * KEY_WIDTH / 2 - 1; gmenu2x->s->rectangle(re, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); - if (ts.initialized() && ts.pressed() && ts.inRect(re)) { + if (ts.available() && ts.pressed() && ts.inRect(re)) { selCol = 1; selRow = kb->size(); } diff --git a/src/settingsdialog.cpp b/src/settingsdialog.cpp index 6bbfe6b..c886869 100644 --- a/src/settingsdialog.cpp +++ b/src/settingsdialog.cpp @@ -61,7 +61,7 @@ bool SettingsDialog::exec() { uint numRows = (gmenu2x->resY-gmenu2x->skinConfInt["topBarHeight"]-20)/rowHeight; while (!close) { - if (ts.initialized()) ts.poll(); + if (ts.available()) ts.poll(); bg.blit(gmenu2x->s,0,0); @@ -88,11 +88,11 @@ bool SettingsDialog::exec() { gmenu2x->s->setClipRect(clipRect); if (ts_pressed && !ts.pressed()) ts_pressed = false; - if (ts.initialized() && ts.pressed() && !ts.inRect(touchRect)) ts_pressed = false; + if (ts.available() && ts.pressed() && !ts.inRect(touchRect)) ts_pressed = false; for (i=firstElement; idraw(iY*rowHeight+gmenu2x->skinConfInt["topBarHeight"]+2); - if (ts.initialized() && ts.pressed() && ts.inRect(touchRect.x, touchRect.y+(iY*rowHeight), touchRect.w, rowHeight)) { + if (ts.available() && ts.pressed() && ts.inRect(touchRect.x, touchRect.y+(iY*rowHeight), touchRect.w, rowHeight)) { ts_pressed = true; sel = i; } diff --git a/src/touchscreen.cpp b/src/touchscreen.cpp index 0c659e3..6e100b0 100644 --- a/src/touchscreen.cpp +++ b/src/touchscreen.cpp @@ -18,15 +18,13 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -#include -#include - #include "touchscreen.h" -using namespace std; +#include +#include + Touchscreen::Touchscreen() { - ts_fd = 0; calibrated = false; wasPressed = false; _handled = false; @@ -37,29 +35,23 @@ Touchscreen::Touchscreen() { event.x = 0; event.y = 0; event.pressure = 0; +#ifdef PLATFORM_GP2X + ts_fd = open("/dev/touchscreen/wm97xx", O_RDONLY|O_NOCTTY); +#else + ts_fd = 0; +#endif } Touchscreen::~Touchscreen() { - deinit(); -} - -bool Touchscreen::init() { -#ifdef PLATFORM_GP2X - ts_fd = open("/dev/touchscreen/wm97xx", O_RDONLY|O_NOCTTY); -#endif - return initialized(); -} - -void Touchscreen::deinit() -{ - if (ts_fd) close(ts_fd); - ts_fd = 0; + if (ts_fd > 0) { + close(ts_fd); + } } void Touchscreen::calibrate() { - if (event.pressure==0) { - calibX = ((event.x-200)*320/3750)/4; - calibY = (((event.y-200)*240/3750))/4; + if (event.pressure == 0) { + calibX = ((event.x - 200) * 320 / 3750) / 4; + calibY = (((event.y - 200) * 240 / 3750)) / 4; calibrated = true; } } @@ -70,9 +62,9 @@ bool Touchscreen::poll() { read(ts_fd, &event, sizeof(TS_EVENT)); if (!calibrated) calibrate(); - if (event.pressure>0) { - x = ((event.x-200)*320/3750)-calibX; - y = (240 - ((event.y-200)*240/3750))-calibY; + if (event.pressure > 0) { + x = ((event.x - 200) * 320 / 3750) - calibX; + y = (240 - ((event.y - 200) * 240 / 3750)) - calibY; } #else SDL_PumpEvents(); @@ -105,7 +97,7 @@ void Touchscreen::setHandled() { } bool Touchscreen::pressed() { - return !_handled && event.pressure>0; + return !_handled && event.pressure > 0; } bool Touchscreen::released() { @@ -113,19 +105,22 @@ bool Touchscreen::released() { } bool Touchscreen::inRect(SDL_Rect r) { - return !_handled && (y>=r.y) && (y<=r.y+r.h) && (x>=r.x) && (x<=r.x+r.w); + return !_handled && + (y >= r.y) && (y <= r.y + r.h) && (x >= r.x) && (x <= r.x + r.w); } bool Touchscreen::inRect(int x, int y, int w, int h) { - SDL_Rect rect = {x,y,w,h}; + SDL_Rect rect = { x, y, w, h }; return inRect(rect); } bool Touchscreen::startedInRect(SDL_Rect r) { - return !_handled && (startY>=r.y) && (startY<=r.y+r.h) && (startX>=r.x) && (startX<=r.x+r.w); + return !_handled && + (startY >= r.y) && (startY <= r.y + r.h) && + (startX >= r.x) && (startX <= r.x + r.w); } bool Touchscreen::startedInRect(int x, int y, int w, int h) { - SDL_Rect rect = {x,y,w,h}; + SDL_Rect rect = { x, y, w, h }; return startedInRect(rect); } diff --git a/src/touchscreen.h b/src/touchscreen.h index b46e4b5..9987275 100644 --- a/src/touchscreen.h +++ b/src/touchscreen.h @@ -23,7 +23,6 @@ #include -#include #include typedef struct { @@ -35,26 +34,13 @@ typedef struct { } TS_EVENT; class Touchscreen { -private: - int ts_fd; - bool calibrated, _handled; - TS_EVENT event; - int calibX, calibY; - int x, y, startX, startY; - bool wasPressed; - - void calibrate(/*TS_EVENT event*/); - public: Touchscreen(); ~Touchscreen(); - bool init(); - void deinit(); - - bool initialized() { + bool available() { #ifdef PLATFORM_GP2X - return ts_fd>0; + return ts_fd > 0; #endif return false; } @@ -73,6 +59,16 @@ public: int getX() { return x; } int getY() { return y; } + +private: + int ts_fd; + bool calibrated, _handled; + TS_EVENT event; + int calibX, calibY; + int x, y, startX, startY; + bool wasPressed; + + void calibrate(/*TS_EVENT event*/); }; #endif