Rob Bradford | bf33b1c | 2012-12-03 19:44:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Intel Corporation |
| 3 | * |
Bryce Harrington | 1f6b0d1 | 2015-06-10 22:48:59 -0700 | [diff] [blame] | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
Rob Bradford | bf33b1c | 2012-12-03 19:44:15 +0000 | [diff] [blame] | 10 | * |
Bryce Harrington | 1f6b0d1 | 2015-06-10 22:48:59 -0700 | [diff] [blame] | 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
Rob Bradford | bf33b1c | 2012-12-03 19:44:15 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
Andrew Wedgbury | 9cd661e | 2014-04-07 12:40:35 +0100 | [diff] [blame] | 24 | #include "config.h" |
| 25 | |
Rob Bradford | bf33b1c | 2012-12-03 19:44:15 +0000 | [diff] [blame] | 26 | #include <stdint.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <cairo.h> |
| 31 | #include <math.h> |
| 32 | #include <assert.h> |
| 33 | |
| 34 | #include <linux/input.h> |
| 35 | #include <wayland-client.h> |
| 36 | |
| 37 | #include "window.h" |
Jon Cruz | 35b2eaa | 2015-06-15 15:37:08 -0700 | [diff] [blame^] | 38 | #include "shared/helpers.h" |
Jon Cruz | 4678bab | 2015-06-15 15:37:07 -0700 | [diff] [blame] | 39 | #include "shared/matrix.h" |
Rob Bradford | bf33b1c | 2012-12-03 19:44:15 +0000 | [diff] [blame] | 40 | |
Rob Bradford | bf33b1c | 2012-12-03 19:44:15 +0000 | [diff] [blame] | 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 |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 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) |
Rusty Lynch | 3ba1263 | 2013-08-08 21:24:19 -0700 | [diff] [blame] | 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 |
Rob Bradford | bf33b1c | 2012-12-03 19:44:15 +0000 | [diff] [blame] | 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); |
Rusty Lynch | 3ba1263 | 2013-08-08 21:24:19 -0700 | [diff] [blame] | 237 | widget_set_touch_down_handler(calibrator->widget, touch_handler); |
Rob Bradford | bf33b1c | 2012-12-03 19:44:15 +0000 | [diff] [blame] | 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 | |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 260 | display = display_create(&argc, argv); |
Rob Bradford | bf33b1c | 2012-12-03 19:44:15 +0000 | [diff] [blame] | 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 | |