blob: 3b9f2fcba1df3f4c5580060d12d002d10c5ffe1f [file] [log] [blame]
Rob Bradfordbf33b1c2012-12-03 19:44:15 +00001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <cairo.h>
28#include <math.h>
29#include <assert.h>
30
31#include <linux/input.h>
32#include <wayland-client.h>
33
34#include "window.h"
35#include "../shared/matrix.h"
36
37#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
38
39/* Our points for the calibration must be not be on a line */
40static const struct {
41 float x_ratio, y_ratio;
42} test_ratios[] = {
43 { 0.20, 0.40 },
44 { 0.80, 0.60 },
45 { 0.40, 0.80 }
46};
47
48struct calibrator {
49 struct tests {
50 int32_t drawn_x, drawn_y;
51 int32_t clicked_x, clicked_y;
52 } tests[ARRAY_LENGTH(test_ratios)];
53 int current_test;
54
55 struct display *display;
56 struct window *window;
57 struct widget *widget;
58};
59
60/*
61 * Calibration algorithm:
62 *
63 * The equation we want to apply at event time where x' and y' are the
64 * calibrated co-ordinates.
65 *
66 * x' = Ax + By + C
67 * y' = Dx + Ey + F
68 *
69 * For example "zero calibration" would be A=1.0 B=0.0 C=0.0, D=0.0, E=1.0,
70 * and F=0.0.
71 *
72 * With 6 unknowns we need 6 equations to find the constants:
73 *
74 * x1' = Ax1 + By1 + C
75 * y1' = Dx1 + Ey1 + F
76 * ...
77 * x3' = Ax3 + By3 + C
78 * y3' = Dx3 + Ey3 + F
79 *
80 * In matrix form:
81 *
82 * x1' x1 y1 1 A
83 * x2' = x2 y2 1 x B
84 * x3' x3 y3 1 C
85 *
86 * So making the matrix M we can find the constants with:
87 *
88 * A x1'
89 * B = M^-1 x x2'
90 * C x3'
91 *
92 * (and similarly for D, E and F)
93 *
94 * For the calibration the desired values x, y are the same values at which
95 * we've drawn at.
96 *
97 */
98static void
99finish_calibration (struct calibrator *calibrator)
100{
101 struct weston_matrix m;
102 struct weston_matrix inverse;
103 struct weston_vector x_calib, y_calib;
104 int i;
105
106
107 /*
108 * x1 y1 1 0
109 * x2 y2 1 0
110 * x3 y3 1 0
111 * 0 0 0 1
112 */
113 memset(&m, 0, sizeof(m));
114 for (i = 0; i < (int)ARRAY_LENGTH(test_ratios); i++) {
115 m.d[i] = calibrator->tests[i].clicked_x;
116 m.d[i + 4] = calibrator->tests[i].clicked_y;
117 m.d[i + 8] = 1;
118 }
119 m.d[15] = 1;
120
121 weston_matrix_invert(&inverse, &m);
122
123 memset(&x_calib, 0, sizeof(x_calib));
124 memset(&y_calib, 0, sizeof(y_calib));
125
126 for (i = 0; i < (int)ARRAY_LENGTH(test_ratios); i++) {
127 x_calib.f[i] = calibrator->tests[i].drawn_x;
128 y_calib.f[i] = calibrator->tests[i].drawn_y;
129 }
130
131 /* Multiples into the vector */
132 weston_matrix_transform(&inverse, &x_calib);
133 weston_matrix_transform(&inverse, &y_calib);
134
135 printf ("Calibration values: %f %f %f %f %f %f\n",
136 x_calib.f[0], x_calib.f[1], x_calib.f[2],
137 y_calib.f[0], y_calib.f[1], y_calib.f[2]);
138
139 exit(0);
140}
141
142
143static void
144button_handler(struct widget *widget,
145 struct input *input, uint32_t time,
146 uint32_t button,
147 enum wl_pointer_button_state state, void *data)
148{
149 struct calibrator *calibrator = data;
150 int32_t x, y;
151
152 if (state == WL_POINTER_BUTTON_STATE_PRESSED && button == BTN_LEFT) {
153 input_get_position(input, &x, &y);
154 calibrator->tests[calibrator->current_test].clicked_x = x;
155 calibrator->tests[calibrator->current_test].clicked_y = y;
156
157 calibrator->current_test--;
158 if (calibrator->current_test < 0)
159 finish_calibration(calibrator);
160 }
161
162 widget_schedule_redraw(widget);
163}
164
165static void
166redraw_handler(struct widget *widget, void *data)
167{
168 struct calibrator *calibrator = data;
169 struct rectangle allocation;
170 cairo_surface_t *surface;
171 cairo_t *cr;
172 int32_t drawn_x, drawn_y;
173
174 widget_get_allocation(calibrator->widget, &allocation);
175 surface = window_get_surface(calibrator->window);
176
177 cr = cairo_create(surface);
178 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
179 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
180 cairo_paint(cr);
181
182 drawn_x = test_ratios[calibrator->current_test].x_ratio * allocation.width;
183 drawn_y = test_ratios[calibrator->current_test].y_ratio * allocation.height;
184
185 calibrator->tests[calibrator->current_test].drawn_x = drawn_x;
186 calibrator->tests[calibrator->current_test].drawn_y = drawn_y;
187
188 cairo_translate(cr, drawn_x, drawn_y);
189 cairo_set_line_width(cr, 2.0);
190 cairo_set_source_rgb(cr, 1.0, 0.0, 0.0);
191 cairo_move_to(cr, 0, -10.0);
192 cairo_line_to(cr, 0, 10.0);
193 cairo_stroke(cr);
194 cairo_move_to(cr, -10.0, 0);
195 cairo_line_to(cr, 10.0, 0.0);
196 cairo_stroke(cr);
197
198 cairo_destroy(cr);
199 cairo_surface_destroy(surface);
200}
201
202static struct calibrator *
203calibrator_create(struct display *display)
204{
205 struct calibrator *calibrator;
206
207 calibrator = malloc(sizeof *calibrator);
208 if (calibrator == NULL)
209 return NULL;
210
211 calibrator->window = window_create(display);
212 calibrator->widget = window_add_widget(calibrator->window, calibrator);
213 window_set_title(calibrator->window, "Wayland calibrator");
214 calibrator->display = display;
215
216 calibrator->current_test = ARRAY_LENGTH(test_ratios) - 1;
217
218 widget_set_button_handler(calibrator->widget, button_handler);
219 widget_set_redraw_handler(calibrator->widget, redraw_handler);
220
221 window_set_fullscreen(calibrator->window, 1);
222
223 return calibrator;
224}
225
226static void
227calibrator_destroy(struct calibrator *calibrator)
228{
229 widget_destroy(calibrator->widget);
230 window_destroy(calibrator->window);
231 free(calibrator);
232}
233
234
235int
236main(int argc, char *argv[])
237{
238 struct display *display;
239 struct calibrator *calibrator;
240
241 display = display_create(argc, argv);
242
243 if (display == NULL) {
244 fprintf(stderr, "failed to create display: %m\n");
245 return -1;
246 }
247
248 calibrator = calibrator_create(display);
249
250 if (!calibrator)
251 return -1;
252
253 display_run(display);
254
255 calibrator_destroy(calibrator);
256 display_destroy(display);
257
258 return 0;
259}
260