1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * 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:
10  *
11  * 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.
22  */
23 
24 #include "config.h"
25 
26 #include <stdint.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <cairo.h>
32 #include <math.h>
33 #include <assert.h>
34 #include <getopt.h>
35 #include <errno.h>
36 
37 #include <linux/input.h>
38 #include <wayland-client.h>
39 
40 #include "window.h"
41 #include "shared/helpers.h"
42 #include <libweston/matrix.h>
43 
44 /* Our points for the calibration must be not be on a line */
45 static const struct {
46 	float x_ratio, y_ratio;
47 } test_ratios[] =  {
48 	{ 0.20, 0.40 },
49 	{ 0.80, 0.60 },
50 	{ 0.40, 0.80 }
51 };
52 
53 struct calibrator {
54 	struct tests {
55 		int32_t drawn_x, drawn_y;
56 		int32_t clicked_x, clicked_y;
57 	} tests[ARRAY_LENGTH(test_ratios)];
58 	int current_test;
59 
60 	struct display *display;
61 	struct window *window;
62 	struct widget *widget;
63 };
64 
65 /*
66  * Calibration algorithm:
67  *
68  * The equation we want to apply at event time where x' and y' are the
69  * calibrated co-ordinates.
70  *
71  * x' = Ax + By + C
72  * y' = Dx + Ey + F
73  *
74  * For example "zero calibration" would be A=1.0 B=0.0 C=0.0, D=0.0, E=1.0,
75  * and F=0.0.
76  *
77  * With 6 unknowns we need 6 equations to find the constants:
78  *
79  * x1' = Ax1 + By1 + C
80  * y1' = Dx1 + Ey1 + F
81  * ...
82  * x3' = Ax3 + By3 + C
83  * y3' = Dx3 + Ey3 + F
84  *
85  * In matrix form:
86  *
87  * x1'   x1 y1 1      A
88  * x2' = x2 y2 1  x   B
89  * x3'   x3 y3 1      C
90  *
91  * So making the matrix M we can find the constants with:
92  *
93  * A            x1'
94  * B = M^-1  x  x2'
95  * C            x3'
96  *
97  * (and similarly for D, E and F)
98  *
99  * For the calibration the desired values x, y are the same values at which
100  * we've drawn at.
101  *
102  */
103 static void
finish_calibration(struct calibrator * calibrator)104 finish_calibration (struct calibrator *calibrator)
105 {
106 	struct weston_matrix m;
107 	struct weston_matrix inverse;
108 	struct weston_vector x_calib, y_calib;
109 	int i;
110 
111 
112 	/*
113 	 * x1 y1  1  0
114 	 * x2 y2  1  0
115 	 * x3 y3  1  0
116 	 *  0  0  0  1
117 	 */
118 	memset(&m, 0, sizeof(m));
119 	for (i = 0; i < (int)ARRAY_LENGTH(test_ratios); i++) {
120 		m.d[i] = calibrator->tests[i].clicked_x;
121 		m.d[i + 4] = calibrator->tests[i].clicked_y;
122 		m.d[i + 8] = 1;
123 	}
124 	m.d[15] = 1;
125 
126 	weston_matrix_invert(&inverse, &m);
127 
128 	memset(&x_calib, 0, sizeof(x_calib));
129 	memset(&y_calib, 0, sizeof(y_calib));
130 
131 	for (i = 0; i < (int)ARRAY_LENGTH(test_ratios); i++) {
132 		x_calib.f[i] = calibrator->tests[i].drawn_x;
133 		y_calib.f[i] = calibrator->tests[i].drawn_y;
134 	}
135 
136 	/* Multiples into the vector */
137 	weston_matrix_transform(&inverse, &x_calib);
138 	weston_matrix_transform(&inverse, &y_calib);
139 
140 	printf ("Calibration values: %f %f %f %f %f %f\n",
141 		x_calib.f[0], x_calib.f[1], x_calib.f[2],
142 		y_calib.f[0], y_calib.f[1], y_calib.f[2]);
143 
144 	exit(0);
145 }
146 
147 
148 static void
button_handler(struct widget * widget,struct input * input,uint32_t time,uint32_t button,enum wl_pointer_button_state state,void * data)149 button_handler(struct widget *widget,
150 	       struct input *input, uint32_t time,
151 	       uint32_t button,
152 	       enum wl_pointer_button_state state, void *data)
153 {
154 	struct calibrator *calibrator = data;
155 	int32_t x, y;
156 
157 	if (state == WL_POINTER_BUTTON_STATE_PRESSED && button == BTN_LEFT) {
158 		input_get_position(input, &x, &y);
159 		calibrator->tests[calibrator->current_test].clicked_x = x;
160 		calibrator->tests[calibrator->current_test].clicked_y = y;
161 
162 		calibrator->current_test--;
163 		if (calibrator->current_test < 0)
164 			finish_calibration(calibrator);
165 	}
166 
167 	widget_schedule_redraw(widget);
168 }
169 
170 static void
touch_handler(struct widget * widget,struct input * input,uint32_t serial,uint32_t time,int32_t id,float x,float y,void * data)171 touch_handler(struct widget *widget, struct input *input, uint32_t serial,
172 	      uint32_t time, int32_t id, float x, float y, void *data)
173 {
174 	struct calibrator *calibrator = data;
175 
176 	calibrator->tests[calibrator->current_test].clicked_x = x;
177 	calibrator->tests[calibrator->current_test].clicked_y = y;
178 	calibrator->current_test--;
179 
180 	if (calibrator->current_test < 0)
181 		  finish_calibration(calibrator);
182 
183 	widget_schedule_redraw(widget);
184 }
185 
186 static void
redraw_handler(struct widget * widget,void * data)187 redraw_handler(struct widget *widget, void *data)
188 {
189 	struct calibrator *calibrator = data;
190 	struct rectangle allocation;
191 	cairo_surface_t *surface;
192 	cairo_t *cr;
193 	int32_t drawn_x, drawn_y;
194 
195 	widget_get_allocation(calibrator->widget, &allocation);
196 	surface = window_get_surface(calibrator->window);
197 
198 	cr = cairo_create(surface);
199 	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
200 	cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
201 	cairo_paint(cr);
202 
203 	drawn_x = test_ratios[calibrator->current_test].x_ratio * allocation.width;
204 	drawn_y = test_ratios[calibrator->current_test].y_ratio * allocation.height;
205 
206 	calibrator->tests[calibrator->current_test].drawn_x = drawn_x;
207 	calibrator->tests[calibrator->current_test].drawn_y = drawn_y;
208 
209 	cairo_translate(cr, drawn_x, drawn_y);
210 	cairo_set_line_width(cr, 2.0);
211 	cairo_set_source_rgb(cr, 1.0, 0.0, 0.0);
212 	cairo_move_to(cr, 0, -10.0);
213 	cairo_line_to(cr, 0, 10.0);
214 	cairo_stroke(cr);
215 	cairo_move_to(cr, -10.0, 0);
216 	cairo_line_to(cr, 10.0, 0.0);
217 	cairo_stroke(cr);
218 
219 	cairo_destroy(cr);
220 	cairo_surface_destroy(surface);
221 }
222 
223 static struct calibrator *
calibrator_create(struct display * display,bool enable_button)224 calibrator_create(struct display *display, bool enable_button)
225 {
226 	struct calibrator *calibrator;
227 
228 	calibrator = malloc(sizeof *calibrator);
229 	if (calibrator == NULL)
230 		return NULL;
231 
232 	calibrator->window = window_create(display);
233 	calibrator->widget = window_add_widget(calibrator->window, calibrator);
234 	window_set_title(calibrator->window, "Wayland calibrator");
235 	calibrator->display = display;
236 
237 	calibrator->current_test = ARRAY_LENGTH(test_ratios) - 1;
238 
239 	if (enable_button)
240 		widget_set_button_handler(calibrator->widget, button_handler);
241 	widget_set_touch_down_handler(calibrator->widget, touch_handler);
242 	widget_set_redraw_handler(calibrator->widget, redraw_handler);
243 
244 	window_set_fullscreen(calibrator->window, 1);
245 
246 	return calibrator;
247 }
248 
249 static void
calibrator_destroy(struct calibrator * calibrator)250 calibrator_destroy(struct calibrator *calibrator)
251 {
252 	widget_destroy(calibrator->widget);
253 	window_destroy(calibrator->window);
254 	free(calibrator);
255 }
256 
257 static void
help(const char * name)258 help(const char *name)
259 {
260 	fprintf(stderr, "Usage: %s [args...]\n", name);
261 	fprintf(stderr, "  -m, --enable-mouse       Enable mouse for testing the touchscreen\n");
262 	fprintf(stderr, "  -h, --help      Display this help message\n");
263 }
264 
265 int
main(int argc,char * argv[])266 main(int argc, char *argv[])
267 {
268 	struct display *display;
269 	struct calibrator *calibrator;
270 	int c;
271 	bool enable_mouse = 0;
272 	struct option opts[] = {
273 		{ "enable-mouse",     no_argument, NULL, 'm' },
274 		{ "help",    no_argument,       NULL, 'h' },
275 		{ 0,         0,                 NULL,  0  }
276 	};
277 
278 	while ((c = getopt_long(argc, argv, "mh", opts, NULL)) != -1) {
279 		switch (c) {
280 		case 'm':
281 			enable_mouse = 1;
282 			break;
283 		case 'h':
284 			help(argv[0]);
285 			exit(EXIT_FAILURE);
286 		default:
287 			break;
288 		}
289 	}
290 
291 	display = display_create(&argc, argv);
292 
293 	if (display == NULL) {
294 		fprintf(stderr, "failed to create display: %s\n",
295 			strerror(errno));
296 		return -1;
297 	}
298 
299 	calibrator = calibrator_create(display, enable_mouse);
300 
301 	if (!calibrator)
302 		return -1;
303 
304 	display_run(display);
305 
306 	calibrator_destroy(calibrator);
307 	display_destroy(display);
308 
309 	return 0;
310 }
311 
312