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
31 #include <linux/input.h>
32 #include <wayland-client.h>
35 #include "../shared/matrix.h"
37 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
39 /* Our points for the calibration must be not be on a line */
41 float x_ratio, y_ratio;
50 int32_t drawn_x, drawn_y;
51 int32_t clicked_x, clicked_y;
52 } tests[ARRAY_LENGTH(test_ratios)];
55 struct display *display;
56 struct window *window;
57 struct widget *widget;
61 * Calibration algorithm:
63 * The equation we want to apply at event time where x' and y' are the
64 * calibrated co-ordinates.
69 * For example "zero calibration" would be A=1.0 B=0.0 C=0.0, D=0.0, E=1.0,
72 * With 6 unknowns we need 6 equations to find the constants:
86 * So making the matrix M we can find the constants with:
92 * (and similarly for D, E and F)
94 * For the calibration the desired values x, y are the same values at which
99 finish_calibration (struct calibrator *calibrator)
101 struct weston_matrix m;
102 struct weston_matrix inverse;
103 struct weston_vector x_calib, y_calib;
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;
121 weston_matrix_invert(&inverse, &m);
123 memset(&x_calib, 0, sizeof(x_calib));
124 memset(&y_calib, 0, sizeof(y_calib));
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;
131 /* Multiples into the vector */
132 weston_matrix_transform(&inverse, &x_calib);
133 weston_matrix_transform(&inverse, &y_calib);
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]);
144 button_handler(struct widget *widget,
145 struct input *input, uint32_t time,
147 enum wl_pointer_button_state state, void *data)
149 struct calibrator *calibrator = data;
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;
157 calibrator->current_test--;
158 if (calibrator->current_test < 0)
159 finish_calibration(calibrator);
162 widget_schedule_redraw(widget);
166 touch_handler(struct widget *widget, struct input *input, uint32_t serial,
167 uint32_t time, int32_t id, float x, float y, void *data)
169 struct calibrator *calibrator = data;
171 calibrator->tests[calibrator->current_test].clicked_x = x;
172 calibrator->tests[calibrator->current_test].clicked_y = y;
173 calibrator->current_test--;
175 if (calibrator->current_test < 0)
176 finish_calibration(calibrator);
178 widget_schedule_redraw(widget);
182 redraw_handler(struct widget *widget, void *data)
184 struct calibrator *calibrator = data;
185 struct rectangle allocation;
186 cairo_surface_t *surface;
188 int32_t drawn_x, drawn_y;
190 widget_get_allocation(calibrator->widget, &allocation);
191 surface = window_get_surface(calibrator->window);
193 cr = cairo_create(surface);
194 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
195 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
198 drawn_x = test_ratios[calibrator->current_test].x_ratio * allocation.width;
199 drawn_y = test_ratios[calibrator->current_test].y_ratio * allocation.height;
201 calibrator->tests[calibrator->current_test].drawn_x = drawn_x;
202 calibrator->tests[calibrator->current_test].drawn_y = drawn_y;
204 cairo_translate(cr, drawn_x, drawn_y);
205 cairo_set_line_width(cr, 2.0);
206 cairo_set_source_rgb(cr, 1.0, 0.0, 0.0);
207 cairo_move_to(cr, 0, -10.0);
208 cairo_line_to(cr, 0, 10.0);
210 cairo_move_to(cr, -10.0, 0);
211 cairo_line_to(cr, 10.0, 0.0);
215 cairo_surface_destroy(surface);
218 static struct calibrator *
219 calibrator_create(struct display *display)
221 struct calibrator *calibrator;
223 calibrator = malloc(sizeof *calibrator);
224 if (calibrator == NULL)
227 calibrator->window = window_create(display);
228 calibrator->widget = window_add_widget(calibrator->window, calibrator);
229 window_set_title(calibrator->window, "Wayland calibrator");
230 calibrator->display = display;
232 calibrator->current_test = ARRAY_LENGTH(test_ratios) - 1;
234 widget_set_button_handler(calibrator->widget, button_handler);
235 widget_set_touch_down_handler(calibrator->widget, touch_handler);
236 widget_set_redraw_handler(calibrator->widget, redraw_handler);
238 window_set_fullscreen(calibrator->window, 1);
244 calibrator_destroy(struct calibrator *calibrator)
246 widget_destroy(calibrator->widget);
247 window_destroy(calibrator->window);
253 main(int argc, char *argv[])
255 struct display *display;
256 struct calibrator *calibrator;
258 display = display_create(&argc, argv);
260 if (display == NULL) {
261 fprintf(stderr, "failed to create display: %m\n");
265 calibrator = calibrator_create(display);
270 display_run(display);
272 calibrator_destroy(calibrator);
273 display_destroy(display);