2010-02-04 13:33:47 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2006 by Massimiliano Torromeo *
|
|
|
|
* massimiliano.torromeo@gmail.com *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "touchscreen.h"
|
|
|
|
|
2011-12-23 13:30:15 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2010-02-04 13:33:47 +02:00
|
|
|
|
|
|
|
Touchscreen::Touchscreen() {
|
|
|
|
calibrated = false;
|
|
|
|
wasPressed = false;
|
|
|
|
_handled = false;
|
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
startX = 0;
|
|
|
|
startY = 0;
|
2010-05-04 00:44:44 +03:00
|
|
|
event.x = 0;
|
|
|
|
event.y = 0;
|
|
|
|
event.pressure = 0;
|
2011-07-29 02:13:35 +03:00
|
|
|
#ifdef PLATFORM_GP2X
|
|
|
|
ts_fd = open("/dev/touchscreen/wm97xx", O_RDONLY|O_NOCTTY);
|
2011-12-23 13:30:15 +02:00
|
|
|
#else
|
|
|
|
ts_fd = 0;
|
2010-02-04 13:33:47 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-12-23 13:30:15 +02:00
|
|
|
Touchscreen::~Touchscreen() {
|
|
|
|
if (ts_fd > 0) {
|
|
|
|
close(ts_fd);
|
|
|
|
}
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Touchscreen::calibrate() {
|
2011-12-23 13:30:15 +02:00
|
|
|
if (event.pressure == 0) {
|
|
|
|
calibX = ((event.x - 200) * 320 / 3750) / 4;
|
|
|
|
calibY = (((event.y - 200) * 240 / 3750)) / 4;
|
2010-02-04 13:33:47 +02:00
|
|
|
calibrated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Touchscreen::poll() {
|
|
|
|
wasPressed = pressed();
|
2011-07-29 02:13:35 +03:00
|
|
|
#ifdef PLATFORM_GP2X
|
|
|
|
read(ts_fd, &event, sizeof(TS_EVENT));
|
2010-02-04 13:33:47 +02:00
|
|
|
if (!calibrated) calibrate();
|
|
|
|
|
2011-12-23 13:30:15 +02:00
|
|
|
if (event.pressure > 0) {
|
|
|
|
x = ((event.x - 200) * 320 / 3750) - calibX;
|
|
|
|
y = (240 - ((event.y - 200) * 240 / 3750)) - calibY;
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
SDL_PumpEvents();
|
|
|
|
int mx, my;
|
|
|
|
if (SDL_GetMouseState(&mx,&my) && SDL_BUTTON(1)) {
|
|
|
|
x = mx;
|
|
|
|
y = my;
|
|
|
|
event.pressure = 1;
|
|
|
|
} else {
|
|
|
|
event.pressure = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
_handled = false;
|
|
|
|
|
|
|
|
if (!wasPressed && pressed()) {
|
|
|
|
startX = x;
|
|
|
|
startY = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pressed();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Touchscreen::handled() {
|
|
|
|
return _handled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Touchscreen::setHandled() {
|
|
|
|
wasPressed = false;
|
|
|
|
_handled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Touchscreen::pressed() {
|
2011-12-23 13:30:15 +02:00
|
|
|
return !_handled && event.pressure > 0;
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Touchscreen::released() {
|
|
|
|
return !pressed() && wasPressed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Touchscreen::inRect(SDL_Rect r) {
|
2011-12-23 13:30:15 +02:00
|
|
|
return !_handled &&
|
|
|
|
(y >= r.y) && (y <= r.y + r.h) && (x >= r.x) && (x <= r.x + r.w);
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Touchscreen::inRect(int x, int y, int w, int h) {
|
2011-12-23 13:30:15 +02:00
|
|
|
SDL_Rect rect = { x, y, w, h };
|
2010-02-04 13:33:47 +02:00
|
|
|
return inRect(rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Touchscreen::startedInRect(SDL_Rect r) {
|
2011-12-23 13:30:15 +02:00
|
|
|
return !_handled &&
|
|
|
|
(startY >= r.y) && (startY <= r.y + r.h) &&
|
|
|
|
(startX >= r.x) && (startX <= r.x + r.w);
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Touchscreen::startedInRect(int x, int y, int w, int h) {
|
2011-12-23 13:30:15 +02:00
|
|
|
SDL_Rect rect = { x, y, w, h };
|
2010-02-04 13:33:47 +02:00
|
|
|
return startedInRect(rect);
|
|
|
|
}
|