1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-06-30 21:17:18 +03: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;
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;
}

View File

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

View File

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

View File

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

View File

@ -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; i<voices.size() && i<firstElement+numRows; i++) {
iY = i-firstElement;
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;
sel = i;
}

View File

@ -18,15 +18,13 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <unistd.h>
#include <iostream>
#include "touchscreen.h"
using namespace std;
#include <fcntl.h>
#include <unistd.h>
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);
}

View File

@ -23,7 +23,6 @@
#include <SDL.h>
#include <fcntl.h>
#include <stdint.h>
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