Root/
| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2006 by Massimiliano Torromeo * |
| 3 | * massimiliano.torromeo@gmail.com * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 2 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., * |
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | #include <unistd.h> |
| 22 | #include <iostream> |
| 23 | |
| 24 | #include "touchscreen.h" |
| 25 | |
| 26 | using namespace std; |
| 27 | |
| 28 | Touchscreen::Touchscreen() { |
| 29 | wm97xx = 0; |
| 30 | calibrated = false; |
| 31 | wasPressed = false; |
| 32 | _handled = false; |
| 33 | x = 0; |
| 34 | y = 0; |
| 35 | startX = 0; |
| 36 | startY = 0; |
| 37 | event.x = 0; |
| 38 | event.y = 0; |
| 39 | event.pressure = 0; |
| 40 | } |
| 41 | |
| 42 | Touchscreen::~Touchscreen() { |
| 43 | if (wm97xx) deinit(); |
| 44 | } |
| 45 | |
| 46 | bool Touchscreen::init() { |
| 47 | #ifdef TARGET_GP2X |
| 48 | wm97xx = open("/dev/touchscreen/wm97xx", O_RDONLY|O_NOCTTY); |
| 49 | #endif |
| 50 | return initialized(); |
| 51 | } |
| 52 | |
| 53 | bool Touchscreen::initialized() { |
| 54 | #ifdef TARGET_GP2X |
| 55 | return wm97xx>0; |
| 56 | #else |
| 57 | return true; |
| 58 | #endif |
| 59 | } |
| 60 | |
| 61 | void Touchscreen::deinit() { |
| 62 | #ifdef TARGET_GP2X |
| 63 | close(wm97xx); |
| 64 | wm97xx = 0; |
| 65 | #endif |
| 66 | } |
| 67 | |
| 68 | void Touchscreen::calibrate() { |
| 69 | if (event.pressure==0) { |
| 70 | calibX = ((event.x-200)*320/3750)/4; |
| 71 | calibY = (((event.y-200)*240/3750))/4; |
| 72 | calibrated = true; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | bool Touchscreen::poll() { |
| 77 | wasPressed = pressed(); |
| 78 | #ifdef TARGET_GP2X |
| 79 | read(wm97xx, &event, sizeof(TS_EVENT)); |
| 80 | if (!calibrated) calibrate(); |
| 81 | |
| 82 | if (event.pressure>0) { |
| 83 | x = ((event.x-200)*320/3750)-calibX; |
| 84 | y = (240 - ((event.y-200)*240/3750))-calibY; |
| 85 | } |
| 86 | #else |
| 87 | SDL_PumpEvents(); |
| 88 | int mx, my; |
| 89 | if (SDL_GetMouseState(&mx,&my) && SDL_BUTTON(1)) { |
| 90 | x = mx; |
| 91 | y = my; |
| 92 | event.pressure = 1; |
| 93 | } else { |
| 94 | event.pressure = 0; |
| 95 | } |
| 96 | #endif |
| 97 | _handled = false; |
| 98 | |
| 99 | if (!wasPressed && pressed()) { |
| 100 | startX = x; |
| 101 | startY = y; |
| 102 | } |
| 103 | |
| 104 | return pressed(); |
| 105 | } |
| 106 | |
| 107 | bool Touchscreen::handled() { |
| 108 | return _handled; |
| 109 | } |
| 110 | |
| 111 | void Touchscreen::setHandled() { |
| 112 | wasPressed = false; |
| 113 | _handled = true; |
| 114 | } |
| 115 | |
| 116 | bool Touchscreen::pressed() { |
| 117 | return !_handled && event.pressure>0; |
| 118 | } |
| 119 | |
| 120 | bool Touchscreen::released() { |
| 121 | return !pressed() && wasPressed; |
| 122 | } |
| 123 | |
| 124 | bool Touchscreen::inRect(SDL_Rect r) { |
| 125 | return !_handled && (y>=r.y) && (y<=r.y+r.h) && (x>=r.x) && (x<=r.x+r.w); |
| 126 | } |
| 127 | |
| 128 | bool Touchscreen::inRect(int x, int y, int w, int h) { |
| 129 | SDL_Rect rect = {x,y,w,h}; |
| 130 | return inRect(rect); |
| 131 | } |
| 132 | |
| 133 | bool Touchscreen::startedInRect(SDL_Rect r) { |
| 134 | return !_handled && (startY>=r.y) && (startY<=r.y+r.h) && (startX>=r.x) && (startX<=r.x+r.w); |
| 135 | } |
| 136 | |
| 137 | bool Touchscreen::startedInRect(int x, int y, int w, int h) { |
| 138 | SDL_Rect rect = {x,y,w,h}; |
| 139 | return startedInRect(rect); |
| 140 | } |
| 141 |
Branches:
install_locations
master
opkrun
packages
