2 * Copyright © 2008 Keith Packard
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
25 autoload Nichrome::Box;
26 autoload Nichrome::Label;
27 autoload Nichrome::Button;
29 extend namespace Nichrome {
30 public namespace Quad {
31 public typedef quad_t;
32 public typedef widget_t + struct {
36 rgba_color_t line_color;
37 rgba_color_t corner_color;
41 void(&quad_t) callback;
44 protected void outline (cairo_t cr, &quad_t quad) {
45 for (int i = 0; i < dim (quad.p); i++) {
46 arc (cr, quad.p[i].x, quad.p[i].y,
47 quad.corner_diameter / 2, 0, 2 * pi);
52 protected void natural (cairo_t cr, &quad_t quad) {
53 rectangle (cr, 0, 0, 400, 300);
56 void text_at (cairo_t cr, point_t p, string text) {
57 text_extents_t e = text_extents (cr, text);
58 p.x = p.x - e.width / 2 - e.x_bearing;
59 p.y = p.y - e.height / 2 - e.y_bearing;
60 move_to (cr, p.x, p.y);
64 protected void draw (cairo_t cr, &quad_t quad) {
66 quad.p[2].x = quad.p[1].x = quad.geometry.width;
67 quad.p[3].y = quad.p[2].y = quad.geometry.height;
70 rectangle (cr, 0, 0, quad.geometry.width, quad.geometry.height);
71 set_source_rgba (cr, 0, 0, 0, .25);
73 for (int i = 0; i < dim (quad.p); i++)
74 line_to (cr, quad.p[i].x, quad.p[i].y);
76 set_line_width (cr, quad.line_width);
77 set_source_rgba (cr, quad.line_color.red, quad.line_color.green,
78 quad.line_color.blue, quad.line_color.alpha);
79 set_line_join (cr, line_join_t.ROUND);
81 set_source_rgba (cr, quad.corner_color.red, quad.corner_color.green,
82 quad.corner_color.blue, quad.corner_color.alpha);
85 set_source_rgba (cr, 1, 1, 1, 1);
86 for (int i = 0; i < dim (quad.p); i++)
87 text_at (cr, quad.p[i], sprintf ("%d", i));
90 int nearest (&quad_t quad, point_t p) {
94 for (int i = 0; i < dim (quad.p); i++) {
95 real dist2 = ((p.x - quad.p[i].x) ** 2 +
96 (p.y - quad.p[i].y) ** 2);
97 if (i == 0 || dist2 < best_dist2) {
105 protected void button (&quad_t quad, &button_event_t event) {
106 enum switch (event.type) {
109 quad.active_corner = nearest (&quad, event);
119 protected void motion (&quad_t quad, &motion_event_t motion) {
121 motion.x = max (0, min (quad.geometry.width, motion.x));
122 motion.y = max (0, min (quad.geometry.height, motion.y));
123 quad.p[quad.active_corner].x = motion.x;
124 quad.p[quad.active_corner].y = motion.y;
125 quad.callback (&quad);
126 Widget::reoutline (&quad);
127 Widget::redraw (&quad);
131 protected void configure (&quad_t quad,
134 if (quad.geometry.width > 0 && quad.geometry.height > 0)
136 real x_scale = geometry.width / quad.geometry.width;
137 real y_scale = geometry.height / quad.geometry.height;
138 for (int i = 0; i< 4; i++) {
139 quad.p[i].x *= x_scale;
140 quad.p[i].y *= y_scale;
143 Widget::configure (&quad, geometry);
144 quad.callback (&quad);
147 protected void init (&quad_t quad,
148 &nichrome_t nichrome,
149 void (&quad_t) callback) {
150 Widget::init (&nichrome, &quad);
151 quad.outline = outline;
153 quad.button = button;
154 quad.motion = motion;
155 quad.configure = configure;
156 quad.natural = natural;
157 quad.p = (point_t[4]) {
160 quad.line_color = (rgba_color_t) {
161 red = 1, green = 0, blue = 0, alpha = .5
163 quad.line_width = 10;
164 quad.corner_color = (rgba_color_t) {
165 red = 0, green = 0, blue = 1, alpha = 0.75
167 quad.corner_diameter = 20;
169 quad.active_corner = -1;
170 quad.callback = callback;
171 quad.started = false;
174 protected *quad_t new (&nichrome_t nichrome, void(&quad_t) callback) {
177 init (&quad, &nichrome, callback);
183 import Nichrome::Box;
184 import Nichrome::Label;
185 import Nichrome::Button;
186 import Nichrome::Quad;
189 typedef real[3,3] m_t;
190 typedef point_t[4] q_t;
193 * Ok, given an source quad and a dest rectangle, compute
194 * a transform that maps the rectangle to q. That's easier
195 * as the rectangle has some nice simple properties. Invert
196 * the matrix to find the opposite mapping
208 * Set m22 to 1, and solve:
210 * | m02 , m12 , 1 | = | q0x, q0y, 1 |
212 * | m00 * w + q0x m10 * w + q0y |
213 * | ------------- , ------------- , 1 | = | q1x, q1y, 1 |
214 * | m20 * w + 1 m20 * w + 1 |
216 * m00*w + q0x = q1x*(m20*w + 1)
217 * m00 = m20*q1x + (q1x - q0x) / w;
219 * m10*w + q0y = q1y*(m20*w + 1)
220 * m10 = m20*q1y + (q1y - q0y) / w;
222 * m01*h + q0x = q3x*(m21*h + 1)
223 * m01 = m21*q3x + (q3x - q0x) / h;
225 * m11*h + q0y = q3y*(m21*h + 1)
226 * m11 = m21*q3y + (q3y - q0y) / h
228 * m00*w + m01*h + q0x = q2x*(m20*w + m21*h + 1)
230 * m20*q1x*w + q1x - q0x + m21*q3x*h + q3x - q0x + q0x = m20*q2x*w + m21*q2x*h + q2x
232 * m20*q1x*w - m20*q2x*w = m21*q2x*h - m21*q3x*h + q2x - q1x + q0x - q3x + q0x - q0x
234 * m20*(q1x - q2x)*w = m21*(q2x - q3x)*h + q2x - q1x - q3x + q0x
237 * m10*w + m11*h + q0y = q2y*(m20*w + m21*h + 1)
239 * m20*q1y*w + q1y - q0y + m21*q3y*h + q3y - q0y + q0y = m20*q2y*w + m21*q2y*h + q2y
241 * m20*q1y*w - m20*q2y*w = m21*q2y*h - m21*q3y*h + q2y - q1y + q0y - q3y + q0y - q0y
243 * m20*(q1y - q2y)*w = m21*(q2y - q3y)*h + q2y - q1y - q3y + q0y
246 * m20*(q1x - q2x)*(q1y - q2y)*w = m21*(q2x - q3x)*(q1y - q2y)*h + (q2x - q1x - q3x + q0x)*(q1y - q2y)
248 * m20*(q1y - q2y)*(q1x - q2x)*w = m21*(q2y - q3y)*(q1x - q2x)*h + (q2y - q1y - q3y + q0y)*(q1x - q2x)
250 * 0 = m21*((q2x - q3x)*(q1y - q2y) - (q2y - q3y)*(q1x - q2x))*h + (stuff)
253 * m21 = -(stuff) / (other stuff)
265 m_t solve (q_t q, real w, real h)
267 real q0x = q[0].x, q0y = q[0].y;
268 real q1x = q[1].x, q1y = q[1].y;
269 real q2x = q[2].x, q2y = q[2].y;
270 real q3x = q[3].x, q3y = q[3].y;
279 real a = ((q2x - q3x)*(q1y - q2y) - (q2y - q3y)*(q1x - q2x)) * h;
280 real b = (q2x - q1x - q3x + q0x) * (q1y - q2y) - (q2y - q1y - q3y + q0y) * (q1x - q2x);
284 m20 = (m21 * (q2x - q3x) * h + q2x - q1x - q3x + q0x) / ((q1x - q2x) * w);
286 m20 = (m21 * (q2y - q3y) * h + q2y - q1y - q3y + q0y) / ((q1y - q2y) * w);
288 m00 = m20 * q1x + (q1x - q0x) / w;
289 m10 = m20 * q1y + (q1y - q0y) / w;
291 m01 = m21 * q3x + (q3x - q0x) / h;
292 m11 = m21 * q3y + (q3y - q0y) / h;
307 static int[3] a = { 2, 2, 1 };
308 static int[3] b = { 1, 0, 0 };
311 for (i = 0; i < 3; i++) {
315 p = m[i,0] * (m[ai,2] * m[bi,1] - m[ai,1] * m[bi,2]);
321 for (j = 0; j < 3; j++) {
322 for (i = 0; i < 3; i++) {
329 p = m[ai,aj] * m[bi,bj] - m[ai,bj] * m[bi,aj];
330 if (((i + j) & 1) != 0)
339 rescale (m_t m, real limit)
342 for (int j = 0; j < 3; j++)
343 for (int i = 0; i < 3; i++)
344 if ((real v = abs (m[j,i])) > max)
346 real scale = limit / max;
347 for (int j = 0; j < 3; j++)
348 for (int i = 0; i < 3; i++)
358 return sprintf ("%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f",
359 m[0,0],m[0,1],m[0,2],
360 m[1,0],m[1,1],m[1,2],
361 m[2,0],m[2,1],m[2,2]);
363 return sprintf ("%v,%v,%v,%v,%v,%v,%v,%v,%v",
364 m[0,0],m[0,1],m[0,2],
365 m[1,0],m[1,1],m[1,2],
366 m[2,0],m[2,1],m[2,2]);
373 return floor (x * 65536 + 0.5) & 0xffffffff;
381 for (int i = 0; i < 3; i++)
382 for (int j = 0; j < 3; j++)
383 r[i,j] = floor(m[i,j] * 65536 + 0.5) / 65536;
392 for (int i = 0; i < 3; i++)
393 for (int j = 0; j < 3; j++) {
394 real diff = abs(r[i,j] - f[i,j]);
396 e[i,j] = diff / abs(r[i,j]);
406 for (int i = 0; i < 3; i++)
408 printf (" { 0x%08x, 0x%08x, 0x%08x },\n",
409 fixed (m[i,0]), fixed (m[i,1]), fixed (m[i,2]));
414 map(m_t m, real x, real y)
416 real[3] in = { x, y, 1 };
417 real[3] out = { 0 ... };
419 for (int i = 0; i < 3; i++) {
420 for (int j = 0; j < 3; j++)
421 out[i] += m[i,j] * in[j];
423 return (real[2]) { out[0] / out[2], out[1]/ out[2] };
427 position_error(m_t r, m_t f, real x, real y)
429 real[2] p_r = map(r, x, y);
430 real[2] p_f = map(f, x, y);
433 error = sqrt((p_r[0] - p_f[0]) ** 2 + (p_r[1] - p_f[1]) ** 2);
434 printf ("x: %g y: %g error %g\n", x, y, error);
435 printf ("\treal x: %g y: %g\n", p_r[0], p_r[1]);
436 printf ("\tfix x: %g y: %g\n", p_f[0], p_f[1]);
441 max_error(m_t r, m_t f) {
442 real max = 0, max_x = 0, max_y = 0;
444 for (int x = 0; x <= 2560; x += 2560)
445 for (int y = 0; y <= 1600; y += 1600) {
446 real error = position_error(r, f, x, y);
453 printf ("max error %g at %d, %d\n", max, max_x, max_y);
454 real[2] p_r = map(r, max_x ,max_y);
455 real[2] p_f = map(f, max_x, max_y);
456 printf ("\tdesired %7.2f, %7.2f actual %7.2f, %7.2f\n",
463 m_row (m_t m, int row)
465 return sprintf ("%10.5f %10.5f %10.5f",
466 m[row,0],m[row,1],m[row,2]);
469 Cairo::point_t[*] scale(Cairo::point_t[*] p, real w, real h)
471 for (int i = 0; i < dim (p); i++) {
486 output_t[*] get_outputs () {
487 output_t[...] outputs = {};
488 twixt (file randr = Process::popen (Process::popen_direction.read,
489 false, "xrandr", "xrandr");
492 while (!File::end (randr)) {
493 string[*] words = String::wordsplit (File::fgets (randr), " ");
494 if (dim (words) >= 3 && words[1] == "connected") {
496 if (words[geom] == "primary")
498 if (File::sscanf (words[geom], "%dx%d+%d+%d",
499 &(int width), &(int height),
500 &(int x), &(int y)) == 4)
502 outputs[dim(outputs)] = (output_t) {
505 x = x, y = y, width = width, height = height
517 m_t m = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } }, m_i, m_r, m_f, m_e;
518 bool m_available = true;
519 output_t[*] outputs = get_outputs ();
520 output_t target_output;
522 if (dim (outputs) == 0) {
523 File::fprintf (stderr, "%s: No enabled outputs\n", argv[0]);
527 if (dim (argv) > 1) {
529 for (i = 0; i < dim (outputs); i++)
530 if (argv[1] == outputs[i].name) {
531 target_output = outputs[i];
534 if (i == dim (outputs)) {
535 File::fprintf (stderr, "%s: no enabled output \"%s\"\n",
541 target_output = outputs[0];
543 real target_width = target_output.geometry.width;
544 real target_height = target_output.geometry.height;
546 real screen_width = 0;
547 real screen_height = 0;
549 for (int i = 0; i < dim (outputs); i++)
551 screen_width = max (screen_width,
552 outputs[i].geometry.x +
553 outputs[i].geometry.width);
554 screen_height = max (screen_height,
555 outputs[i].geometry.y +
556 outputs[i].geometry.height);
559 &nichrome_t nichrome = Nichrome::new ("Keystone Correction", 400, 350);
562 &label_t space = Label::new (&nichrome, "");
563 for (int i = 0; i < 3; i++) {
564 label[i] = Label::new (&nichrome, "matrix");
565 label[i]->font = "sans-9";
568 void callback (&quad_t quad) {
569 real w = quad.geometry.width;
570 real h = quad.geometry.height;
573 m = solve (scale (quad.p, target_width / w, target_height / h),
574 target_width, target_height);
576 m_r = rescale (m_i, 16384);
577 for (int i = 0; i < 3; i++)
578 text[i] = m_row (m_i,i);
580 } catch divide_by_zero (real a, real b) {
581 text = (string[3]) { "no solution", "" ... };
584 for (int i = 0; i < 3; i++)
585 Label::relabel (label[i], text[i]);
587 &quad_t quad = Quad::new (&nichrome, callback);
589 void doit_func (&widget_t widget, bool state)
593 Process::system ("xrandr",
596 sprintf ("%dx%d", screen_width, screen_height),
604 &button_t doit = Button::new (&nichrome, "doit", doit_func);
606 void show_func (&widget_t widget, bool state)
610 printf ("normal: %s\n", m_print (m));
611 printf ("inverse: %s\n", m_print (m_i));
612 printf ("scaled: %s\n", m_print (m_r));
614 printf ("round: %s\n", m_print(m_f));
617 m_e = error(m_r, m_f);
618 printf ("error: %s\n", m_print (m_e));
623 &button_t show = Button::new (&nichrome, "show", show_func);
624 &button_t quit = Button::new (&nichrome, "quit",
625 void func (&widget_t w, bool state) {
626 w.nichrome.running = false;
629 &box_t hbox = Box::new (Box::dir_t.horizontal,
630 Box::widget_item (&doit, 0),
631 Box::widget_item (&show, 0),
632 Box::widget_item (&quit, 0),
634 &box_t box = Box::new (Box::dir_t.vertical,
635 Box::box_item (&hbox),
636 Box::widget_item (label[0], 1, 0),
637 Box::widget_item (label[1], 1, 0),
638 Box::widget_item (label[2], 1, 0),
639 Box::widget_item (&space, 1, 0),
640 Box::widget_item (&quad, 1));
641 Nichrome::set_box (&nichrome, &box);
642 Nichrome::main_loop (&nichrome);