forked from mtorromeo/gmenu2x
-
Notifications
You must be signed in to change notification settings - Fork 15
/
touchscreen.cpp
138 lines (118 loc) · 3.44 KB
/
touchscreen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/***************************************************************************
* 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 <unistd.h>
// #include <iostream>
#include "touchscreen.h"
Touchscreen::Touchscreen() {
wm97xx = 0;
calibrated = false;
wasPressed = false;
_handled = false;
x = 0;
y = 0;
startX = 0;
startY = 0;
event.x = 0;
event.y = 0;
event.pressure = 0;
}
Touchscreen::~Touchscreen() {
if (wm97xx) deinit();
}
bool Touchscreen::init() {
#ifdef TARGET_GP2X
wm97xx = open("/dev/touchscreen/wm97xx", O_RDONLY|O_NOCTTY);
#endif
return initialized();
}
bool Touchscreen::initialized() {
#ifdef TARGET_GP2X
return wm97xx>0;
#else
return true;
#endif
}
void Touchscreen::deinit() {
#ifdef TARGET_GP2X
close(wm97xx);
wm97xx = 0;
#endif
}
void Touchscreen::calibrate() {
if (event.pressure==0) {
calibX = ((event.x-200)*320/3750)/4;
calibY = (((event.y-200)*240/3750))/4;
calibrated = true;
}
}
bool Touchscreen::poll() {
wasPressed = pressed();
#ifdef TARGET_GP2X
read(wm97xx, &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;
}
#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() {
return !_handled && event.pressure>0;
}
bool Touchscreen::released() {
return !pressed() && wasPressed;
}
bool Touchscreen::inRect(SDL_Rect r) {
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};
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);
}
bool Touchscreen::startedInRect(int x, int y, int w, int h) {
SDL_Rect rect = {x,y,w,h};
return startedInRect(rect);
}