xdg-shell: bump experimental protocol version
[profile/ivi/weston-ivi-shell.git] / clients / calibrator.c
1 /*
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 "config.h"
24
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <cairo.h>
30 #include <math.h>
31 #include <assert.h>
32
33 #include <linux/input.h>
34 #include <wayland-client.h>
35
36 #include "window.h"
37 #include "../shared/matrix.h"
38
39 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
40
41 /* Our points for the calibration must be not be on a line */
42 static const struct {
43         float x_ratio, y_ratio;
44 } test_ratios[] =  {
45         { 0.20, 0.40 },
46         { 0.80, 0.60 },
47         { 0.40, 0.80 }
48 };
49
50 struct calibrator {
51         struct tests {
52                 int32_t drawn_x, drawn_y;
53                 int32_t clicked_x, clicked_y;
54         } tests[ARRAY_LENGTH(test_ratios)];
55         int current_test;
56
57         struct display *display;
58         struct window *window;
59         struct widget *widget;
60 };
61
62 /*
63  * Calibration algorithm:
64  *
65  * The equation we want to apply at event time where x' and y' are the
66  * calibrated co-ordinates.
67  *
68  * x' = Ax + By + C
69  * y' = Dx + Ey + F
70  *
71  * For example "zero calibration" would be A=1.0 B=0.0 C=0.0, D=0.0, E=1.0,
72  * and F=0.0.
73  *
74  * With 6 unknowns we need 6 equations to find the constants:
75  *
76  * x1' = Ax1 + By1 + C
77  * y1' = Dx1 + Ey1 + F
78  * ...
79  * x3' = Ax3 + By3 + C
80  * y3' = Dx3 + Ey3 + F
81  *
82  * In matrix form:
83  *
84  * x1'   x1 y1 1      A
85  * x2' = x2 y2 1  x   B
86  * x3'   x3 y3 1      C
87  *
88  * So making the matrix M we can find the constants with:
89  *
90  * A            x1'
91  * B = M^-1  x  x2'
92  * C            x3'
93  *
94  * (and similarly for D, E and F)
95  *
96  * For the calibration the desired values x, y are the same values at which
97  * we've drawn at.
98  *
99  */
100 static void
101 finish_calibration (struct calibrator *calibrator)
102 {
103         struct weston_matrix m;
104         struct weston_matrix inverse;
105         struct weston_vector x_calib, y_calib;
106         int i;
107
108
109         /*
110          * x1 y1  1  0
111          * x2 y2  1  0
112          * x3 y3  1  0
113          *  0  0  0  1
114          */
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;
119                 m.d[i + 8] = 1;
120         }
121         m.d[15] = 1;
122
123         weston_matrix_invert(&inverse, &m);
124
125         memset(&x_calib, 0, sizeof(x_calib));
126         memset(&y_calib, 0, sizeof(y_calib));
127
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;
131         }
132
133         /* Multiples into the vector */
134         weston_matrix_transform(&inverse, &x_calib);
135         weston_matrix_transform(&inverse, &y_calib);
136
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]);
140
141         exit(0);
142 }
143
144
145 static void
146 button_handler(struct widget *widget,
147                struct input *input, uint32_t time,
148                uint32_t button,
149                enum wl_pointer_button_state state, void *data)
150 {
151         struct calibrator *calibrator = data;
152         int32_t x, y;
153
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;
158
159                 calibrator->current_test--;
160                 if (calibrator->current_test < 0)
161                         finish_calibration(calibrator);
162         }
163
164         widget_schedule_redraw(widget);
165 }
166
167 static void
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)
170 {
171         struct calibrator *calibrator = data;
172
173         calibrator->tests[calibrator->current_test].clicked_x = x;
174         calibrator->tests[calibrator->current_test].clicked_y = y;
175         calibrator->current_test--;
176
177         if (calibrator->current_test < 0)
178                   finish_calibration(calibrator);
179
180         widget_schedule_redraw(widget);
181 }
182
183 static void
184 redraw_handler(struct widget *widget, void *data)
185 {
186         struct calibrator *calibrator = data;
187         struct rectangle allocation;
188         cairo_surface_t *surface;
189         cairo_t *cr;
190         int32_t drawn_x, drawn_y;
191
192         widget_get_allocation(calibrator->widget, &allocation);
193         surface = window_get_surface(calibrator->window);
194
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);
198         cairo_paint(cr);
199
200         drawn_x = test_ratios[calibrator->current_test].x_ratio * allocation.width;
201         drawn_y = test_ratios[calibrator->current_test].y_ratio * allocation.height;
202
203         calibrator->tests[calibrator->current_test].drawn_x = drawn_x;
204         calibrator->tests[calibrator->current_test].drawn_y = drawn_y;
205
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);
211         cairo_stroke(cr);
212         cairo_move_to(cr, -10.0, 0);
213         cairo_line_to(cr, 10.0, 0.0);
214         cairo_stroke(cr);
215
216         cairo_destroy(cr);
217         cairo_surface_destroy(surface);
218 }
219
220 static struct calibrator *
221 calibrator_create(struct display *display)
222 {
223         struct calibrator *calibrator;
224
225         calibrator = malloc(sizeof *calibrator);
226         if (calibrator == NULL)
227                 return NULL;
228
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;
233
234         calibrator->current_test = ARRAY_LENGTH(test_ratios) - 1;
235
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);
239
240         window_set_fullscreen(calibrator->window, 1);
241
242         return calibrator;
243 }
244
245 static void
246 calibrator_destroy(struct calibrator *calibrator)
247 {
248         widget_destroy(calibrator->widget);
249         window_destroy(calibrator->window);
250         free(calibrator);
251 }
252
253
254 int
255 main(int argc, char *argv[])
256 {
257         struct display *display;
258         struct calibrator *calibrator;
259
260         display = display_create(&argc, argv);
261
262         if (display == NULL) {
263                 fprintf(stderr, "failed to create display: %m\n");
264                 return -1;
265         }
266
267         calibrator = calibrator_create(display);
268
269         if (!calibrator)
270                 return -1;
271
272         display_run(display);
273
274         calibrator_destroy(calibrator);
275         display_destroy(display);
276
277         return 0;
278 }
279