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