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

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.
This commit is contained in:
Maarten ter Huurne 2011-12-23 12:30:15 +01:00
parent c7af4b1aea
commit 944ab86f9c
7 changed files with 53 additions and 66 deletions

View File

@ -64,7 +64,7 @@ bool BrowseDialog::exec()
selected = 0; selected = 0;
close = false; close = false;
while (!close) { while (!close) {
if (gmenu2x->ts.initialized()) gmenu2x->ts.poll(); if (gmenu2x->ts.available()) gmenu2x->ts.poll();
paint(); paint();
@ -111,7 +111,7 @@ void BrowseDialog::handleInput()
action = getAction(button); 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] == "..") if (action == BrowseDialog::ACT_SELECT && (*fl)[selected] == "..")
action = BrowseDialog::ACT_GOUP; action = BrowseDialog::ACT_GOUP;
@ -250,7 +250,7 @@ void BrowseDialog::paint()
icon->blit(gmenu2x->s, 5, offsetY); icon->blit(gmenu2x->s, 5, offsetY);
gmenu2x->s->write(gmenu2x->font, (*fl)[i], 24, offsetY + 8, ASFont::HAlignLeft, ASFont::VAlignMiddle); 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; ts_pressed = true;
selected = i; selected = i;
} }

View File

@ -262,10 +262,7 @@ void GMenu2X::init() {
/* gp2x_mem = open("/dev/mem", O_RDWR); /* gp2x_mem = open("/dev/mem", O_RDWR);
gp2x_memregs=(unsigned short *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, gp2x_mem, 0xc0000000); gp2x_memregs=(unsigned short *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, gp2x_mem, 0xc0000000);
MEM_REG=&gp2x_memregs[0]; 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 #else
batteryHandle = fopen("/sys/class/power_supply/battery/capacity", "r"); batteryHandle = fopen("/sys/class/power_supply/battery/capacity", "r");
usbHandle = fopen("/sys/class/power_supply/usb/online", "r"); usbHandle = fopen("/sys/class/power_supply/usb/online", "r");
@ -287,8 +284,7 @@ void GMenu2X::deinit() {
gp2x_memregs[0x290C>>1]=640; gp2x_memregs[0x290C>>1]=640;
close(gp2x_mem); close(gp2x_mem);
} }
*/
if (f200) ts.deinit();*/
#else #else
if (batteryHandle) fclose(batteryHandle); if (batteryHandle) fclose(batteryHandle);
if (backlightHandle) fclose(backlightHandle); if (backlightHandle) fclose(backlightHandle);
@ -1059,7 +1055,7 @@ void GMenu2X::main() {
} }
} }
if (ts.initialized()) { if (ts.available()) {
btnContextMenu.paint(); btnContextMenu.paint();
} }
//check battery status every 60 seconds //check battery status every 60 seconds
@ -1123,7 +1119,7 @@ void GMenu2X::main() {
s->flip(); s->flip();
//touchscreen //touchscreen
if (ts.initialized()) { if (ts.available()) {
ts.poll(); ts.poll();
btnContextMenu.handleTS(); btnContextMenu.handleTS();
re.x = 0; re.y = 0; re.h = skinConfInt["topBarHeight"]; re.w = resX; re.x = 0; re.y = 0; re.h = skinConfInt["topBarHeight"]; re.w = resX;
@ -1593,7 +1589,7 @@ void GMenu2X::contextMenu() {
s->flip(); s->flip();
//touchscreen //touchscreen
if (ts.initialized()) { if (ts.available()) {
ts.poll(); ts.poll();
if (ts.released()) { if (ts.released()) {
if (!ts.inRect(box)) if (!ts.inRect(box))

View File

@ -174,7 +174,7 @@ public:
//firmware type and version //firmware type and version
std::string fwType, fwVersion; std::string fwType, fwVersion;
bool isF200() { return ts.initialized(); } bool isF200() { return ts.available(); }
// Open2x settings --------------------------------------------------------- // 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; 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;

View File

@ -174,7 +174,7 @@ bool InputDialog::exec() {
gmenu2x->skinConfColors[COLOR_SELECTION_BG]); gmenu2x->skinConfColors[COLOR_SELECTION_BG]);
} }
if (ts.initialized()) ts.poll(); if (ts.available()) ts.poll();
drawVirtualKeyboard(); drawVirtualKeyboard();
gmenu2x->s->flip(); gmenu2x->s->flip();
@ -291,7 +291,7 @@ void InputDialog::drawVirtualKeyboard() {
}; };
//if ts on rect, change selection //if ts on rect, change selection
if (ts.initialized() && ts.pressed() && ts.inRect(re)) { if (ts.available() && ts.pressed() && ts.inRect(re)) {
selCol = xc; selCol = xc;
selRow = l; selRow = l;
} }
@ -309,7 +309,7 @@ void InputDialog::drawVirtualKeyboard() {
//Ok/Cancel //Ok/Cancel
SDL_Rect re = {kbLeft-1, KB_TOP+kb->size()*KEY_HEIGHT, kbLength*KEY_WIDTH/2-1, KEY_HEIGHT-1}; 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]); 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; selCol = 0;
selRow = kb->size(); selRow = kb->size();
} }
@ -320,7 +320,7 @@ void InputDialog::drawVirtualKeyboard() {
re.x = kbLeft + kbLength * KEY_WIDTH / 2 - 1; re.x = kbLeft + kbLength * KEY_WIDTH / 2 - 1;
gmenu2x->s->rectangle(re, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); 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; selCol = 1;
selRow = kb->size(); selRow = kb->size();
} }

View File

@ -61,7 +61,7 @@ bool SettingsDialog::exec() {
uint numRows = (gmenu2x->resY-gmenu2x->skinConfInt["topBarHeight"]-20)/rowHeight; uint numRows = (gmenu2x->resY-gmenu2x->skinConfInt["topBarHeight"]-20)/rowHeight;
while (!close) { while (!close) {
if (ts.initialized()) ts.poll(); if (ts.available()) ts.poll();
bg.blit(gmenu2x->s,0,0); bg.blit(gmenu2x->s,0,0);
@ -88,11 +88,11 @@ bool SettingsDialog::exec() {
gmenu2x->s->setClipRect(clipRect); gmenu2x->s->setClipRect(clipRect);
if (ts_pressed && !ts.pressed()) ts_pressed = false; 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; i<voices.size() && i<firstElement+numRows; i++) { for (i=firstElement; i<voices.size() && i<firstElement+numRows; i++) {
iY = i-firstElement; iY = i-firstElement;
voices[i]->draw(iY*rowHeight+gmenu2x->skinConfInt["topBarHeight"]+2); voices[i]->draw(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; ts_pressed = true;
sel = i; sel = i;
} }

View File

@ -18,15 +18,13 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/ ***************************************************************************/
#include <unistd.h>
#include <iostream>
#include "touchscreen.h" #include "touchscreen.h"
using namespace std; #include <fcntl.h>
#include <unistd.h>
Touchscreen::Touchscreen() { Touchscreen::Touchscreen() {
ts_fd = 0;
calibrated = false; calibrated = false;
wasPressed = false; wasPressed = false;
_handled = false; _handled = false;
@ -37,29 +35,23 @@ Touchscreen::Touchscreen() {
event.x = 0; event.x = 0;
event.y = 0; event.y = 0;
event.pressure = 0; event.pressure = 0;
#ifdef PLATFORM_GP2X
ts_fd = open("/dev/touchscreen/wm97xx", O_RDONLY|O_NOCTTY);
#else
ts_fd = 0;
#endif
} }
Touchscreen::~Touchscreen() { Touchscreen::~Touchscreen() {
deinit(); if (ts_fd > 0) {
} close(ts_fd);
}
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;
} }
void Touchscreen::calibrate() { void Touchscreen::calibrate() {
if (event.pressure==0) { if (event.pressure == 0) {
calibX = ((event.x-200)*320/3750)/4; calibX = ((event.x - 200) * 320 / 3750) / 4;
calibY = (((event.y-200)*240/3750))/4; calibY = (((event.y - 200) * 240 / 3750)) / 4;
calibrated = true; calibrated = true;
} }
} }
@ -70,9 +62,9 @@ bool Touchscreen::poll() {
read(ts_fd, &event, sizeof(TS_EVENT)); read(ts_fd, &event, sizeof(TS_EVENT));
if (!calibrated) calibrate(); if (!calibrated) calibrate();
if (event.pressure>0) { if (event.pressure > 0) {
x = ((event.x-200)*320/3750)-calibX; x = ((event.x - 200) * 320 / 3750) - calibX;
y = (240 - ((event.y-200)*240/3750))-calibY; y = (240 - ((event.y - 200) * 240 / 3750)) - calibY;
} }
#else #else
SDL_PumpEvents(); SDL_PumpEvents();
@ -105,7 +97,7 @@ void Touchscreen::setHandled() {
} }
bool Touchscreen::pressed() { bool Touchscreen::pressed() {
return !_handled && event.pressure>0; return !_handled && event.pressure > 0;
} }
bool Touchscreen::released() { bool Touchscreen::released() {
@ -113,19 +105,22 @@ bool Touchscreen::released() {
} }
bool Touchscreen::inRect(SDL_Rect r) { 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) { 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); return inRect(rect);
} }
bool Touchscreen::startedInRect(SDL_Rect r) { 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) { 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); return startedInRect(rect);
} }

View File

@ -23,7 +23,6 @@
#include <SDL.h> #include <SDL.h>
#include <fcntl.h>
#include <stdint.h> #include <stdint.h>
typedef struct { typedef struct {
@ -35,26 +34,13 @@ typedef struct {
} TS_EVENT; } TS_EVENT;
class Touchscreen { 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: public:
Touchscreen(); Touchscreen();
~Touchscreen(); ~Touchscreen();
bool init(); bool available() {
void deinit();
bool initialized() {
#ifdef PLATFORM_GP2X #ifdef PLATFORM_GP2X
return ts_fd>0; return ts_fd > 0;
#endif #endif
return false; return false;
} }
@ -73,6 +59,16 @@ public:
int getX() { return x; } int getX() { return x; }
int getY() { return y; } 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 #endif