-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalibrator.cpp
More file actions
372 lines (315 loc) · 12.2 KB
/
calibrator.cpp
File metadata and controls
372 lines (315 loc) · 12.2 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* Copyright (c) 2009 Tias Guns
* Copyright (c) 2009 Soren Hauberg
* Copyright (c) 2011 Antoine Hue
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <algorithm>
#include <sys/types.h>
#include <dirent.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include "calibrator.hh"
#include "calibratorbuilder.hh"
// static instances
bool Calibrator::verbose = false;
std::string Calibrator::pathResource = "./";
const char* Calibrator::SYSFS_INPUT="/sys/class/input";
const char* Calibrator::SYSFS_DEVNAME="device/name";
int Calibrator::argX = 0;
int Calibrator::argY = 0;
std::shared_ptr<std::vector<CalibratorData>> Calibrator::arrayCalibrators = std::make_shared<std::vector<CalibratorData>>();
std::unordered_map<int, XYinfo> Calibrator::coordsMap;
Calibrator::Calibrator(const PtrCalibratorBuilder builder)
{
old_axys = builder->getAxys();
restore_axys = {};
options = builder;
clicked.num = 0;
}
bool Calibrator::add_click(int x, int y)
{
// Double-click detection
if (options->getThrDoubleclick() > 0 && clicked.num > 0) {
int i = clicked.num - 1;
while (i >= 0) {
if (abs(x - clicked.x[i]) <= options->getThrDoubleclick()
&& abs(y - clicked.y[i]) <= options->getThrDoubleclick()) {
if (verbose) {
printf("DEBUG: Not adding click %i (X=%i, Y=%i): within %i pixels of previous click\n",
clicked.num, x, y, options->getThrDoubleclick());
}
return false;
}
i--;
}
}
//std::cout << "clicked.num = " << clicked.num << std::endl;
// Mis-click detection
if (options->getThrMisclick() > 0 && clicked.num > 0)
{
bool misclick = true;
switch (clicked.num) {
case 1:
// check that along one axis of first point
if (along_axis(x,clicked.x[UL],clicked.y[UL]) ||
along_axis(y,clicked.x[UL],clicked.y[UL]))
{
misclick = false;
} else if (verbose) {
printf("DEBUG: Mis-click detected, click %i (X=%i, Y=%i) not aligned with click 0 (X=%i, Y=%i) (threshold=%i)\n",
clicked.num, x, y, clicked.x[UL], clicked.y[UL], options->getThrMisclick());
}
break;
case 2:
// check that along other axis of first point than second point
if ((along_axis( y, clicked.x[UL], clicked.y[UL])
&& along_axis( clicked.x[UR], clicked.x[UL], clicked.y[UL]))
|| (along_axis( x, clicked.x[UL], clicked.y[UL])
&& along_axis( clicked.y[UR], clicked.x[UL], clicked.y[UL])))
{
misclick = false;
printf("DEBUG: click %i (X=%i, Y=%i) not aligned with click 0 (X=%i, Y=%i) or click 1 (X=%i, Y=%i) (threshold=%i)\n",
clicked.num, x, y, clicked.x[UL], clicked.y[UL], clicked.x[UR], clicked.y[UR], options->getThrMisclick());
} else if (verbose) {
printf("DEBUG: Mis-click detected, click %i (X=%i, Y=%i) not aligned with click 0 (X=%i, Y=%i) or click 1 (X=%i, Y=%i) (threshold=%i)\n",
clicked.num, x, y, clicked.x[UL], clicked.y[UL], clicked.x[UR], clicked.y[UR], options->getThrMisclick());
}
break;
case 3:
// check that along both axis of second and third point
if ( ( along_axis( x, clicked.x[UR], clicked.y[UR])
&& along_axis( y, clicked.x[LL], clicked.y[LL]) )
||( along_axis( y, clicked.x[UR], clicked.y[UR])
&& along_axis( x, clicked.x[LL], clicked.y[LL]) ) )
{
misclick = false;
printf("DEBUG: click %i (X=%i, Y=%i) not aligned with click 1 (X=%i, Y=%i) or click 2 (X=%i, Y=%i) (threshold=%i)\n",
clicked.num, x, y, clicked.x[UR], clicked.y[UR], clicked.x[LL], clicked.y[LL], options->getThrMisclick());
}
else
if (verbose) {
printf("DEBUG: Mis-click detected, click %i (X=%i, Y=%i) not aligned with click 1 (X=%i, Y=%i) or click 2 (X=%i, Y=%i) (threshold=%i)\n",
clicked.num, x, y, clicked.x[UR], clicked.y[UR], clicked.x[LL], clicked.y[LL], options->getThrMisclick());
}
break;
case 4: // confirm input
{
clicked.num++;
return true;
}
}
if (misclick) {
reset();
return false;
}
}
add_click_simple(x, y);
if (verbose)
printf("DEBUG: Adding click %i (X=%i, Y=%i)\n", clicked.num-1, x, y);
return true;
}
void Calibrator::add_click_simple(int x, int y)
{
clicked.x.push_back(x);
clicked.y.push_back(y);
clicked.num++;
}
inline bool Calibrator::along_axis(int xy, int x0, int y0)
{
return ((abs(xy - x0) <= options->getThrMisclick()) ||
(abs(xy - y0) <= options->getThrMisclick()));
}
bool Calibrator::finish(int width, int height)
{
if (get_numclicks() != NUM_POINTS) {
return false;
}
// new axis origin and scaling
// based on old_axys: inversion/swapping is relative to the old axis
XYinfo new_axis(old_axys);
// calculate average of clicks
float x_min = (clicked.x[UL] + clicked.x[LL])/2.0;
float x_max = (clicked.x[UR] + clicked.x[LR])/2.0;
float y_min = (clicked.y[UL] + clicked.y[UR])/2.0;
float y_max = (clicked.y[LL] + clicked.y[LR])/2.0;
// Should x and y be swapped?
if (abs(clicked.x[UL] - clicked.x[UR]) < abs(clicked.y[UL] - clicked.y[UR])) {
new_axis.swap_xy = !new_axis.swap_xy;
std::swap(x_min, y_min);
std::swap(x_max, y_max);
}
// the screen was divided in num_blocks blocks, and the touch points were at
// one block away from the true edges of the screen.
const float block_x = width/(float)num_blocks;
const float block_y = height/(float)num_blocks;
// rescale these blocks from the range of the drawn touchpoints to the range of the
// actually clicked coordinates, and substract/add from the clicked coordinates
// to obtain the coordinates corresponding to the edges of the screen.
float scale_x = (x_max - x_min)/(width - 2*block_x);
x_min -= block_x * scale_x;
x_max += block_x * scale_x;
float scale_y = (y_max - y_min)/(height - 2*block_y);
y_min -= block_y * scale_y;
y_max += block_y * scale_y;
// now, undo the transformations done by the X server, to obtain the true 'raw' value in X.
// The raw value was scaled from old_axis to the device min/max, and from the device min/max
// to the screen min/max
// hence, the reverse transformation is from screen to old_axis
x_min = scaleAxis(x_min, old_axys.x.max, old_axys.x.min, width, 0);
x_max = scaleAxis(x_max, old_axys.x.max, old_axys.x.min, width, 0);
y_min = scaleAxis(y_min, old_axys.y.max, old_axys.y.min, height, 0);
y_max = scaleAxis(y_max, old_axys.y.max, old_axys.y.min, height, 0);
// round and put in new_axis struct
new_axis.x.min = round(x_min); new_axis.x.max = round(x_max);
new_axis.y.min = round(y_min); new_axis.y.max = round(y_max);
// finish the data, driver/calibrator specific
return finish_data(new_axis);
}
const char* Calibrator::get_sysfs_name()
{
if (is_sysfs_name(options->getDevice_name()))
{
return options->getDevice_name();
}
// TODO: more mechanisms
return NULL;
}
bool Calibrator::is_sysfs_name(const char* name) {
DIR* dp = opendir(SYSFS_INPUT);
if (dp == NULL)
return false;
while (dirent* ep = readdir(dp)) {
if (strncmp(ep->d_name, "event", strlen("event")) == 0) {
// got event name, get its sysfs device name
char filename[40]; // actually 35, but hey...
(void) sprintf(filename, "%s/%s/%s", SYSFS_INPUT, ep->d_name, SYSFS_DEVNAME);
std::ifstream ifile(filename);
if (ifile.is_open()) {
if (!ifile.eof()) {
std::string devname;
std::getline(ifile, devname);
if (devname == name) {
if (verbose)
printf("DEBUG: Found that '%s' is a sysfs name.\n", name);
return true;
}
}
ifile.close();
}
}
}
(void) closedir(dp);
if (verbose)
printf("DEBUG: Name '%s' does not match any in '%s/event*/%s'\n",
name, SYSFS_INPUT, SYSFS_DEVNAME);
return false;
}
bool Calibrator::has_xorgconfd_support(Display* dpy) {
bool has_support = false;
Display* display = dpy;
if (dpy == NULL) // no connection to reuse
display = XOpenDisplay(NULL);
if (display == NULL) {
fprintf(stderr, "Unable to connect to X server\n");
exit(1);
}
if (strstr(ServerVendor(display), "X.Org") &&
VendorRelease(display) >= 10800000) {
has_support = true;
}
if (dpy == NULL) // no connection to reuse
XCloseDisplay(display);
return has_support;
}
void Calibrator::setOld_axys(const XYinfo&value)
{
old_axys = value;
}
const std::string& Calibrator::getPathResource()
{
return pathResource;
}
bool Calibrator::getVerbose()
{
return verbose;
}
/*
* FROM xf86Xinput.c
*
* Cx - raw data from touch screen
* to_max - scaled highest dimension
* (remember, this is of rows - 1 because of 0 origin)
* to_min - scaled lowest dimension
* from_max - highest raw value from touch screen calibration
* from_min - lowest raw value from touch screen calibration
*
* This function is the same for X or Y coordinates.
* You may have to reverse the high and low values to compensate for
* different orgins on the touch screen vs X.
*
* e.g. to scale from device coordinates into screen coordinates, call
* xf86ScaleAxis(x, 0, screen_width, dev_min, dev_max);
*/
int
xf86ScaleAxis(int Cx, int to_max, int to_min, int from_max, int from_min)
{
int X;
int64_t to_width = to_max - to_min;
int64_t from_width = from_max - from_min;
if (from_width) {
X = (int) (((to_width * (Cx - from_min)) / from_width) + to_min);
}
else {
X = 0;
printf("Divide by Zero in xf86ScaleAxis\n");
exit(1);
}
if (X > to_max)
X = to_max;
if (X < to_min)
X = to_min;
return X;
}
// same but without rounding to min/max
float
scaleAxis(float Cx, int to_max, int to_min, int from_max, int from_min)
{
float X;
int64_t to_width = to_max - to_min;
int64_t from_width = from_max - from_min;
if (from_width) {
X = (((to_width * (Cx - from_min)) / from_width) + to_min);
}
else {
X = 0;
printf("Divide by Zero in scaleAxis\n");
exit(1);
}
/* no rounding to max/min
if (X > to_max)
X = to_max;
if (X < to_min)
X = to_min;
*/
return X;
}