2 * Copyright © 2012 Intel Corporation
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.
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
33 #include <linux/input.h>
34 #include <wayland-client.h>
37 #include "../shared/matrix.h"
39 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
41 /* Our points for the calibration must be not be on a line */
43 float x_ratio, y_ratio;
52 int32_t drawn_x, drawn_y;
53 int32_t clicked_x, clicked_y;
54 } tests[ARRAY_LENGTH(test_ratios)];
57 struct display *display;
58 struct window *window;
59 struct widget *widget;
63 * Calibration algorithm:
65 * The equation we want to apply at event time where x' and y' are the
66 * calibrated co-ordinates.
71 * For example "zero calibration" would be A=1.0 B=0.0 C=0.0, D=0.0, E=1.0,
74 * With 6 unknowns we need 6 equations to find the constants:
88 * So making the matrix M we can find the constants with:
94 * (and similarly for D, E and F)
96 * For the calibration the desired values x, y are the same values at which
101 finish_calibration (struct calibrator *calibrator)
103 struct weston_matrix m;
104 struct weston_matrix inverse;
105 struct weston_vector x_calib, y_calib;
115 memset(&m, 0, sizeof(m));
116 for (i = 0; i < (int)ARRAY_LENGTH(test_ratios); i++) {
117 m.d[i] = calibrator->tests[i].clicked_x;
118 m.d[i + 4] = calibrator->tests[i].clicked_y;
123 weston_matrix_invert(&inverse, &m);
125 memset(&x_calib, 0, sizeof(x_calib));
126 memset(&y_calib, 0, sizeof(y_calib));
128 for (i = 0; i < (int)ARRAY_LENGTH(test_ratios); i++) {
129 x_calib.f[i] = calibrator->tests[i].drawn_x;
130 y_calib.f[i] = calibrator->tests[i].drawn_y;
133 /* Multiples into the vector */
134 weston_matrix_transform(&inverse, &x_calib);
135 weston_matrix_transform(&inverse, &y_calib);
137 printf ("Calibration values: %f %f %f %f %f %f\n",
138 x_calib.f[0], x_calib.f[1], x_calib.f[2],
139 y_calib.f[0], y_calib.f[1], y_calib.f[2]);
146 button_handler(struct widget *widget,
147 struct input *input, uint32_t time,
149 enum wl_pointer_button_state state, void *data)
151 struct calibrator *calibrator = data;
154 if (state == WL_POINTER_BUTTON_STATE_PRESSED && button == BTN_LEFT) {
155 input_get_position(input, &x, &y);
156 calibrator->tests[calibrator->current_test].clicked_x = x;
157 calibrator->tests[calibrator->current_test].clicked_y = y;
159 calibrator->current_test--;
160 if (calibrator->current_test < 0)
161 finish_calibration(calibrator);
164 widget_schedule_redraw(widget);
168 touch_handler(struct widget *widget, struct input *input, uint32_t serial,
169 uint32_t time, int32_t id, float x, float y, void *data)
171 struct calibrator *calibrator = data;
173 calibrator->tests[calibrator->current_test].clicked_x = x;
174 calibrator->tests[calibrator->current_test].clicked_y = y;
175 calibrator->current_test--;
177 if (calibrator->current_test < 0)
178 finish_calibration(calibrator);
180 widget_schedule_redraw(widget);
184 redraw_handler(struct widget *widget, void *data)
186 struct calibrator *calibrator = data;
187 struct rectangle allocation;
188 cairo_surface_t *surface;
190 int32_t drawn_x, drawn_y;
192 widget_get_allocation(calibrator->widget, &allocation);
193 surface = window_get_surface(calibrator->window);
195 cr = cairo_create(surface);
196 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
197 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
200 drawn_x = test_ratios[calibrator->current_test].x_ratio * allocation.width;
201 drawn_y = test_ratios[calibrator->current_test].y_ratio * allocation.height;
203 calibrator->tests[calibrator->current_test].drawn_x = drawn_x;
204 calibrator->tests[calibrator->current_test].drawn_y = drawn_y;
206 cairo_translate(cr, drawn_x, drawn_y);
207 cairo_set_line_width(cr, 2.0);
208 cairo_set_source_rgb(cr, 1.0, 0.0, 0.0);
209 cairo_move_to(cr, 0, -10.0);
210 cairo_line_to(cr, 0, 10.0);
212 cairo_move_to(cr, -10.0, 0);
213 cairo_line_to(cr, 10.0, 0.0);
217 cairo_surface_destroy(surface);
220 static struct calibrator *
221 calibrator_create(struct display *display)
223 struct calibrator *calibrator;
225 calibrator = malloc(sizeof *calibrator);
226 if (calibrator == NULL)
229 calibrator->window = window_create(display);
230 calibrator->widget = window_add_widget(calibrator->window, calibrator);
231 window_set_title(calibrator->window, "Wayland calibrator");
232 calibrator->display = display;
234 calibrator->current_test = ARRAY_LENGTH(test_ratios) - 1;
236 widget_set_button_handler(calibrator->widget, button_handler);
237 widget_set_touch_down_handler(calibrator->widget, touch_handler);
238 widget_set_redraw_handler(calibrator->widget, redraw_handler);
240 window_set_fullscreen(calibrator->window, 1);
246 calibrator_destroy(struct calibrator *calibrator)
248 widget_destroy(calibrator->widget);
249 window_destroy(calibrator->window);
255 main(int argc, char *argv[])
257 struct display *display;
258 struct calibrator *calibrator;
260 display = display_create(&argc, argv);
262 if (display == NULL) {
263 fprintf(stderr, "failed to create display: %m\n");
267 calibrator = calibrator_create(display);
272 display_run(display);
274 calibrator_destroy(calibrator);
275 display_destroy(display);