-
Notifications
You must be signed in to change notification settings - Fork 6
/
HyperPixelTouchScreen.cpp
174 lines (153 loc) · 3.92 KB
/
HyperPixelTouchScreen.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* HyperPixelTouchScreen.cpp
*
* Created on: 22 Jul 2018
* Author: andrewcapon
*/
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/input.h>
#include <unistd.h>
#include "HyperPixelTouchScreen.h"
HyperPixelTouchScreen::HyperPixelTouchScreen(uint8_t uDeviceNum, bool bBlocking)
: m_uDeviceNum(uDeviceNum),
m_bConnected(false),
m_bBlocking(bBlocking),
m_uCurrentSlot(0)
{
// filename
char *pszFilename;
if(asprintf(&pszFilename, "/dev/input/event%d", uDeviceNum))
{
m_hFile = open(pszFilename, O_RDONLY);
if(m_hFile >= 0)
{
int rc = ioctl(m_hFile, EVIOCGRAB, (void*)1);
if (rc == 0)
{
ioctl(m_hFile, EVIOCGRAB, (void*)0);
m_bConnected = true;
}
else
{
// failed to grab the file
fprintf(stderr, "Device is grabbed by another process. Use \"fuser -v %s\" to find the process.\n", pszFilename);
}
}
else
{
// failed to Open the file
fprintf(stderr, "Failed to open %s\n", pszFilename);
}
free(pszFilename);
}
else
{
// failed to grab the file
fprintf(stderr, "Internal Error: asprintf() failed, probably out of memory.\n");
}
}
HyperPixelTouchScreen::~HyperPixelTouchScreen()
{
if(m_hFile >= 0)
close(m_hFile);
}
void HyperPixelTouchScreen::Process(ITouchEventHandler *pEventHandler)
{
if(m_bConnected)
{
struct timeval timeout = {0,0};
fd_set rdfs;
FD_ZERO(&rdfs);
FD_SET(m_hFile, &rdfs);
struct timeval *pTimeout;
if(m_bBlocking)
pTimeout = NULL;
else
pTimeout = &timeout;
if(select(m_hFile + 1, &rdfs, NULL, NULL, pTimeout) != 0)
{
size_t uBytesRead = read(m_hFile, m_events, sizeof(m_events));
if(uBytesRead > sizeof(struct input_event))
{
uint8_t uEvents = uBytesRead / sizeof(struct input_event);
for (uint8_t uEvent = 0; uEvent < uEvents; uEvent++)
{
struct input_event *pEvent = &(m_events[uEvent]);
//printf("[%u] = (%u, %u) = %d\n", uEvent, m_events[uEvent].type, m_events[uEvent].code, m_events[uEvent].value);
switch(pEvent->type)
{
case EV_SYN:
{
if(m_touchEvents[m_uCurrentSlot].GetActive())
m_touchEventQueue.push(TouchQueueElement(tetTouchMove, m_touchEvents[m_uCurrentSlot]));
// send all events
while(!m_touchEventQueue.empty())
{
TouchQueueElement &tqElement = m_touchEventQueue.front();
switch(tqElement.first)
{
case tetTouchBegin:
pEventHandler->TouchBegin(tqElement.second);
break;
case tetTouchEnd:
pEventHandler->TouchEnd(tqElement.second);
break;
case tetTouchMove:
pEventHandler->TouchMove(tqElement.second);
break;
}
m_touchEventQueue.pop();
}
}
break;
case EV_ABS:
{
switch(pEvent->code)
{
case ABS_MT_SLOT:
{
m_uCurrentSlot = pEvent->value;
m_touchEventQueue.push(TouchQueueElement(tetTouchMove, m_touchEvents[m_uCurrentSlot]));
}
break;
case ABS_MT_POSITION_X:
{
m_touchEvents[m_uCurrentSlot].SetX(pEvent->value);
}
break;
case ABS_MT_POSITION_Y:
{
m_touchEvents[m_uCurrentSlot].SetY(pEvent->value);
}
break;
case ABS_MT_TOUCH_MAJOR:
{
m_touchEvents[m_uCurrentSlot].SetInitialArea(pEvent->value);
}
break;
case ABS_MT_TRACKING_ID:
{
if(pEvent->value == -1)
{
// end touch
m_touchEvents[m_uCurrentSlot].SetActive(false);
m_touchEventQueue.push(TouchQueueElement(tetTouchEnd, m_touchEvents[m_uCurrentSlot]));
}
else
{
// begin touch
m_touchEvents[m_uCurrentSlot].SetActive(true);
m_touchEventQueue.push(TouchQueueElement(tetTouchBegin, m_touchEvents[m_uCurrentSlot]));
}
}
break;
}
}
}
//printf("Slot = %u\n", m_uCurrentSlot);
}
}
}
}
}