Initial commit
[profile/ivi/xinput_calibrator.git] / src / calibrator / EvdevTester.cpp
1 /*
2  * Copyright (c) 2009 Tias Guns
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22
23 #include "calibrator/Evdev.hpp"
24 #include "calibrator/EvdevTester.hpp"
25
26 #include <cstdio>
27
28 CalibratorEvdevTester::CalibratorEvdevTester(const char* const device_name0, const XYinfo& axys0, const int thr_misclick, const int thr_doubleclick, const OutputType output_type, const char* geometry)
29   : CalibratorEvdev(device_name0, axys0, thr_misclick, thr_doubleclick, output_type, geometry)
30 {
31     //printf("Starting test driver\n");
32 }
33
34 bool CalibratorEvdevTester::finish_data(const XYinfo axis)
35 {
36     new_axis = axis;
37
38     return true;
39 }
40
41 XYinfo CalibratorEvdevTester::emulate_driver(const XYinfo& raw, bool useNewAxis, const XYinfo& screen, const XYinfo& device) {
42     XYinfo calibAxis;
43     if (useNewAxis)
44         calibAxis = new_axis;
45     else
46         calibAxis = old_axys;
47
48     // call evdev's code (minimally modified to fit us)
49     int mins[2] = {raw.x.min, raw.y.min};
50     evdev_270_processvaluator(device, calibAxis, mins);
51     int maxs[2] = {raw.x.max, raw.y.max};
52     evdev_270_processvaluator(device, calibAxis, maxs);
53
54     XYinfo result(mins[0], maxs[0], mins[1], maxs[1]);
55
56
57     // the last step is usually done by the X server,
58     // or transparently somewhere on the way
59     result.do_xf86ScaleAxis(screen, device);
60     return result;
61 }
62
63 // return in 'vals'
64 void CalibratorEvdevTester::evdev_270_processvaluator(const XYinfo& devAxis, const XYinfo& axis, int* vals)
65 {
66         //int vals[2] = {valX, valY};
67         int absinfo_min[2] = {devAxis.x.min, devAxis.y.min};
68         int absinfo_max[2] = {devAxis.x.max, devAxis.y.max};
69
70         /*
71          * Code from xf86-input-evdev: src/evdev.c
72          * function: static void EvdevProcessValuators(InputInfoPtr pInfo)
73          * last change: 2011-12-14 'Fix absolute events with swapped axes'
74          * last change id: 8d6dfd13b0c4177305555294218e366a6cddc83f
75          *
76          * All valuator_mask_isset() test can be skipped here:
77          * its a requirement to have both X and Y coordinates
78          */
79         int i;
80
81         // if (pEvdev->swap_axes) {
82         if (axis.swap_xy) {
83             int swapped_isset[2] = {0, 0};
84             int swapped_values[2];
85
86             for(i = 0; i <= 1; i++) {
87                 //if (valuator_mask_isset(pEvdev->vals, i)) {
88                     swapped_isset[1 - i] = 1;
89
90                     /* in all sensible cases, the absinfo is the same for the
91                      * X and Y axis. In that case, the below simplifies to:
92                      * wapped_values[1 - i] = vals[i]
93                      * However, the code below accounts for the oddball
94                      * device for which this would not be the case.
95                      */
96                     swapped_values[1 - i] =
97                         //xf86ScaleAxis(valuator_mask_get(pEvdev->vals, i),
98                         //              pEvdev->absinfo[1 - i].maximum,
99                         //              pEvdev->absinfo[1 - i].minimum,
100                         //              pEvdev->absinfo[i].maximum,
101                         //              pEvdev->absinfo[i].minimum);
102                         xf86ScaleAxis(vals[i],
103                                       absinfo_max[1 - i],
104                                       absinfo_min[1 - i],
105                                       absinfo_max[i],
106                                       absinfo_min[i]);
107                 //}
108             }
109
110             for (i = 0; i <= 1; i++) {
111                 if (swapped_isset[i])
112                     //valuator_mask_set(pEvdev->vals, i, swapped_values[i]);
113                     vals[i] = swapped_values[i];
114                 //else
115                 //    valuator_mask_unset(pEvdev->vals, i);
116             }
117         }
118
119         for (i = 0; i <= 1; i++) {
120             int val;
121             int calib_min;
122             int calib_max;
123
124             //if (!valuator_mask_isset(pEvdev->vals, i))
125             //    continue;
126
127             //val = valuator_mask_get(pEvdev->vals, i);
128             val = vals[i];
129
130             if (i == 0) {
131                 //calib_min = pEvdev->calibration.min_x;
132                 calib_min = axis.x.min;
133                 //calib_max = pEvdev->calibration.max_x;
134                 calib_max = axis.x.max;
135             } else {
136                 //calib_min = pEvdev->calibration.min_y;
137                 calib_min = axis.y.min;
138                 //calib_max = pEvdev->calibration.max_y;
139                 calib_max = axis.y.max;
140             }
141
142             //if (pEvdev->flags & EVDEV_CALIBRATED)
143             if (true)
144                 //val = xf86ScaleAxis(val, pEvdev->absinfo[i].maximum,
145                 //                    pEvdev->absinfo[i].minimum, calib_max,
146                 //                    calib_min);
147                 val = xf86ScaleAxis(val, absinfo_max[i],
148                                     absinfo_min[i], calib_max,
149                                     calib_min);
150
151             //if ((i == 0 && pEvdev->invert_x) || (i == 1 && pEvdev->invert_y))
152             if ((i == 0 && axis.x.invert) || (i == 1 && axis.y.invert))
153                 //val = (pEvdev->absinfo[i].maximum - val +
154                 //       pEvdev->absinfo[i].minimum);
155                 val = (absinfo_max[i] - val +
156                        absinfo_min[i]);
157
158             //valuator_mask_set(pEvdev->vals, i, val);
159             vals[i] = val;
160         }
161
162         //return vals;
163 }