blob: 5b6425852981be6a3066e97ffffc4dcf4a3539fb [file] [log] [blame]
Rob Bradfordbf33b1c2012-12-03 19:44:15 +00001/*
2 * Copyright © 2012 Intel Corporation
3 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07004 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
Rob Bradfordbf33b1c2012-12-03 19:44:15 +000010 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070011 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
Rob Bradfordbf33b1c2012-12-03 19:44:15 +000022 */
23
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010024#include "config.h"
25
Rob Bradfordbf33b1c2012-12-03 19:44:15 +000026#include <stdint.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <cairo.h>
31#include <math.h>
32#include <assert.h>
33
34#include <linux/input.h>
35#include <wayland-client.h>
36
37#include "window.h"
38#include "../shared/matrix.h"
39
40#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
41
42/* Our points for the calibration must be not be on a line */
43static const struct {
44 float x_ratio, y_ratio;
45} test_ratios[] = {
46 { 0.20, 0.40 },
47 { 0.80, 0.60 },
48 { 0.40, 0.80 }
49};
50
51struct calibrator {
52 struct tests {
53 int32_t drawn_x, drawn_y;
54 int32_t clicked_x, clicked_y;
55 } tests[ARRAY_LENGTH(test_ratios)];
56 int current_test;
57
58 struct display *display;
59 struct window *window;
60 struct widget *widget;
61};
62
63/*
64 * Calibration algorithm:
65 *
66 * The equation we want to apply at event time where x' and y' are the
67 * calibrated co-ordinates.
68 *
69 * x' = Ax + By + C
70 * y' = Dx + Ey + F
71 *
72 * For example "zero calibration" would be A=1.0 B=0.0 C=0.0, D=0.0, E=1.0,
73 * and F=0.0.
74 *
75 * With 6 unknowns we need 6 equations to find the constants:
76 *
77 * x1' = Ax1 + By1 + C
78 * y1' = Dx1 + Ey1 + F
79 * ...
80 * x3' = Ax3 + By3 + C
81 * y3' = Dx3 + Ey3 + F
82 *
83 * In matrix form:
84 *
85 * x1' x1 y1 1 A
86 * x2' = x2 y2 1 x B
87 * x3' x3 y3 1 C
88 *
89 * So making the matrix M we can find the constants with:
90 *
91 * A x1'
92 * B = M^-1 x x2'
93 * C x3'
94 *
95 * (and similarly for D, E and F)
96 *
97 * For the calibration the desired values x, y are the same values at which
98 * we've drawn at.
99 *
100 */
101static void
102finish_calibration (struct calibrator *calibrator)
103{
104 struct weston_matrix m;
105 struct weston_matrix inverse;
106 struct weston_vector x_calib, y_calib;
107 int i;
108
109
110 /*
111 * x1 y1 1 0
112 * x2 y2 1 0
113 * x3 y3 1 0
114 * 0 0 0 1
115 */
116 memset(&m, 0, sizeof(m));
117 for (i = 0; i < (int)ARRAY_LENGTH(test_ratios); i++) {
118 m.d[i] = calibrator->tests[i].clicked_x;
119 m.d[i + 4] = calibrator->tests[i].clicked_y;
120 m.d[i + 8] = 1;
121 }
122 m.d[15] = 1;
123
124 weston_matrix_invert(&inverse, &m);
125
126 memset(&x_calib, 0, sizeof(x_calib));
127 memset(&y_calib, 0, sizeof(y_calib));
128
129 for (i = 0; i < (int)ARRAY_LENGTH(test_ratios); i++) {
130 x_calib.f[i] = calibrator->tests[i].drawn_x;
131 y_calib.f[i] = calibrator->tests[i].drawn_y;
132 }
133
134 /* Multiples into the vector */
135 weston_matrix_transform(&inverse, &x_calib);
136 weston_matrix_transform(&inverse, &y_calib);
137
138 printf ("Calibration values: %f %f %f %f %f %f\n",
139 x_calib.f[0], x_calib.f[1], x_calib.f[2],
140 y_calib.f[0], y_calib.f[1], y_calib.f[2]);
141
142 exit(0);
143}
144
145
146static void
147button_handler(struct widget *widget,
148 struct input *input, uint32_t time,
149 uint32_t button,
150 enum wl_pointer_button_state state, void *data)
151{
152 struct calibrator *calibrator = data;
153 int32_t x, y;
154
155 if (state == WL_POINTER_BUTTON_STATE_PRESSED && button == BTN_LEFT) {
156 input_get_position(input, &x, &y);
157 calibrator->tests[calibrator->current_test].clicked_x = x;
158 calibrator->tests[calibrator->current_test].clicked_y = y;
159
160 calibrator->current_test--;
161 if (calibrator->current_test < 0)
162 finish_calibration(calibrator);
163 }
164
165 widget_schedule_redraw(widget);
166}
167
168static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700169touch_handler(struct widget *widget, struct input *input, uint32_t serial,
170 uint32_t time, int32_t id, float x, float y, void *data)
Rusty Lynch3ba12632013-08-08 21:24:19 -0700171{
172 struct calibrator *calibrator = data;
173
174 calibrator->tests[calibrator->current_test].clicked_x = x;
175 calibrator->tests[calibrator->current_test].clicked_y = y;
176 calibrator->current_test--;
177
178 if (calibrator->current_test < 0)
179 finish_calibration(calibrator);
180
181 widget_schedule_redraw(widget);
182}
183
184static void
Rob Bradfordbf33b1c2012-12-03 19:44:15 +0000185redraw_handler(struct widget *widget, void *data)
186{
187 struct calibrator *calibrator = data;
188 struct rectangle allocation;
189 cairo_surface_t *surface;
190 cairo_t *cr;
191 int32_t drawn_x, drawn_y;
192
193 widget_get_allocation(calibrator->widget, &allocation);
194 surface = window_get_surface(calibrator->window);
195
196 cr = cairo_create(surface);
197 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
198 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
199 cairo_paint(cr);
200
201 drawn_x = test_ratios[calibrator->current_test].x_ratio * allocation.width;
202 drawn_y = test_ratios[calibrator->current_test].y_ratio * allocation.height;
203
204 calibrator->tests[calibrator->current_test].drawn_x = drawn_x;
205 calibrator->tests[calibrator->current_test].drawn_y = drawn_y;
206
207 cairo_translate(cr, drawn_x, drawn_y);
208 cairo_set_line_width(cr, 2.0);
209 cairo_set_source_rgb(cr, 1.0, 0.0, 0.0);
210 cairo_move_to(cr, 0, -10.0);
211 cairo_line_to(cr, 0, 10.0);
212 cairo_stroke(cr);
213 cairo_move_to(cr, -10.0, 0);
214 cairo_line_to(cr, 10.0, 0.0);
215 cairo_stroke(cr);
216
217 cairo_destroy(cr);
218 cairo_surface_destroy(surface);
219}
220
221static struct calibrator *
222calibrator_create(struct display *display)
223{
224 struct calibrator *calibrator;
225
226 calibrator = malloc(sizeof *calibrator);
227 if (calibrator == NULL)
228 return NULL;
229
230 calibrator->window = window_create(display);
231 calibrator->widget = window_add_widget(calibrator->window, calibrator);
232 window_set_title(calibrator->window, "Wayland calibrator");
233 calibrator->display = display;
234
235 calibrator->current_test = ARRAY_LENGTH(test_ratios) - 1;
236
237 widget_set_button_handler(calibrator->widget, button_handler);
Rusty Lynch3ba12632013-08-08 21:24:19 -0700238 widget_set_touch_down_handler(calibrator->widget, touch_handler);
Rob Bradfordbf33b1c2012-12-03 19:44:15 +0000239 widget_set_redraw_handler(calibrator->widget, redraw_handler);
240
241 window_set_fullscreen(calibrator->window, 1);
242
243 return calibrator;
244}
245
246static void
247calibrator_destroy(struct calibrator *calibrator)
248{
249 widget_destroy(calibrator->widget);
250 window_destroy(calibrator->window);
251 free(calibrator);
252}
253
254
255int
256main(int argc, char *argv[])
257{
258 struct display *display;
259 struct calibrator *calibrator;
260
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500261 display = display_create(&argc, argv);
Rob Bradfordbf33b1c2012-12-03 19:44:15 +0000262
263 if (display == NULL) {
264 fprintf(stderr, "failed to create display: %m\n");
265 return -1;
266 }
267
268 calibrator = calibrator_create(display);
269
270 if (!calibrator)
271 return -1;
272
273 display_run(display);
274
275 calibrator_destroy(calibrator);
276 display_destroy(display);
277
278 return 0;
279}
280