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 void text_at (cairo_t cr, point_t p, string text) {
53 text_extents_t e = text_extents (cr, text);
54 p.x = p.x - e.width / 2 - e.x_bearing;
55 p.y = p.y - e.height / 2 - e.y_bearing;
56 move_to (cr, p.x, p.y);
60 protected void draw (cairo_t cr, &quad_t quad) {
62 quad.p[2].x = quad.p[1].x = quad.geometry.width;
63 quad.p[3].y = quad.p[2].y = quad.geometry.height;
66 rectangle (cr, 0, 0, quad.geometry.width, quad.geometry.height);
67 set_source_rgba (cr, 0, 0, 0, .25);
69 for (int i = 0; i < dim (quad.p); i++)
70 line_to (cr, quad.p[i].x, quad.p[i].y);
72 set_line_width (cr, quad.line_width);
73 set_source_rgba (cr, quad.line_color.red, quad.line_color.green,
74 quad.line_color.blue, quad.line_color.alpha);
75 set_line_join (cr, line_join_t.ROUND);
77 set_source_rgba (cr, quad.corner_color.red, quad.corner_color.green,
78 quad.corner_color.blue, quad.corner_color.alpha);
81 set_source_rgba (cr, 1, 1, 1, 1);
82 for (int i = 0; i < dim (quad.p); i++)
83 text_at (cr, quad.p[i], sprintf ("%d", i));
86 int nearest (&quad_t quad, point_t p) {
90 for (int i = 0; i < dim (quad.p); i++) {
91 real dist2 = ((p.x - quad.p[i].x) ** 2 +
92 (p.y - quad.p[i].y) ** 2);
93 if (i == 0 || dist2 < best_dist2) {
101 protected void button (&quad_t quad, &button_event_t event) {
102 enum switch (event.type) {
105 quad.active = nearest (&quad, event);
115 protected void motion (&quad_t quad, &motion_event_t motion) {
117 motion.x = max (0, min (quad.geometry.width, motion.x));
118 motion.y = max (0, min (quad.geometry.height, motion.y));
119 quad.p[quad.active].x = motion.x;
120 quad.p[quad.active].y = motion.y;
121 quad.callback (&quad);
122 Widget::reoutline (&quad);
123 Widget::redraw (&quad);
127 protected void configure (&quad_t quad,
130 if (quad.geometry.width > 0 && quad.geometry.height > 0)
132 real x_scale = geometry.width / quad.geometry.width;
133 real y_scale = geometry.height / quad.geometry.height;
134 for (int i = 0; i< 4; i++) {
135 quad.p[i].x *= x_scale;
136 quad.p[i].y *= y_scale;
139 Widget::configure (&quad, geometry);
140 quad.callback (&quad);
143 protected void init (&quad_t quad,
144 &nichrome_t nichrome,
145 void (&quad_t) callback) {
146 Widget::init (&nichrome, &quad);
147 quad.outline = outline;
149 quad.button = button;
150 quad.motion = motion;
151 quad.configure = configure;
152 quad.p = (point_t[4]) {
155 quad.line_color = (rgba_color_t) {
156 red = 1, green = 0, blue = 0, alpha = .5
158 quad.line_width = 10;
159 quad.corner_color = (rgba_color_t) {
160 red = 0, green = 0, blue = 1, alpha = 0.75
162 quad.corner_diameter = 20;
165 quad.callback = callback;
166 quad.started = false;
169 protected *quad_t new (&nichrome_t nichrome, void(&quad_t) callback) {
172 init (&quad, &nichrome, callback);
178 import Nichrome::Box;
179 import Nichrome::Label;
180 import Nichrome::Button;
181 import Nichrome::Quad;
184 typedef real[3,3] m_t;
185 typedef point_t[4] q_t;
188 * Ok, given an source quad and a dest rectangle, compute
189 * a transform that maps the rectangle to q. That's easier
190 * as the rectangle has some nice simple properties. Invert
191 * the matrix to find the opposite mapping
203 * Set m22 to 1, and solve:
205 * | m02 , m12 , 1 | = | q0x, q0y, 1 |
207 * | m00 * w + q0x m10 * w + q0y |
208 * | ------------- , ------------- , 1 | = | q1x, q1y, 1 |
209 * | m20 * w + 1 m20 * w + 1 |
211 * m00*w + q0x = q1x*(m20*w + 1)
212 * m00 = m20*q1x + (q1x - q0x) / w;
214 * m10*w + q0y = q1y*(m20*w + 1)
215 * m10 = m20*q1y + (q1y - q0y) / w;
217 * m01*h + q0x = q3x*(m21*h + 1)
218 * m01 = m21*q3x + (q3x - q0x) / h;
220 * m11*h + q0y = q3y*(m21*h + 1)
221 * m11 = m21*q3y + (q3y - q0y) / h
223 * m00*w + m01*h + q0x = q2x*(m20*w + m21*h + 1)
225 * m20*q1x*w + q1x - q0x + m21*q3x*h + q3x - q0x + q0x = m20*q2x*w + m21*q2x*h + q2x
227 * m20*q1x*w - m20*q2x*w = m21*q2x*h - m21*q3x*h + q2x - q1x + q0x - q3x + q0x - q0x
229 * m20*(q1x - q2x)*w = m21*(q2x - q3x)*h + q2x - q1x - q3x + q0x
232 * m10*w + m11*h + q0y = q2y*(m20*w + m21*h + 1)
234 * m20*q1y*w + q1y - q0y + m21*q3y*h + q3y - q0y + q0y = m20*q2y*w + m21*q2y*h + q2y
236 * m20*q1y*w - m20*q2y*w = m21*q2y*h - m21*q3y*h + q2y - q1y + q0y - q3y + q0y - q0y
238 * m20*(q1y - q2y)*w = m21*(q2y - q3y)*h + q2y - q1y - q3y + q0y
241 * m20*(q1x - q2x)*(q1y - q2y)*w = m21*(q2x - q3x)*(q1y - q2y)*h + (q2x - q1x - q3x + q0x)*(q1y - q2y)
243 * m20*(q1y - q2y)*(q1x - q2x)*w = m21*(q2y - q3y)*(q1x - q2x)*h + (q2y - q1y - q3y + q0y)*(q1x - q2x)
245 * 0 = m21*((q2x - q3x)*(q1y - q2y) - (q2y - q3y)*(q1x - q2x))*h + (stuff)
248 * m21 = -(stuff) / (other stuff)
260 m_t solve (q_t q, real w, real h)
262 real q0x = q[0].x, q0y = q[0].y;
263 real q1x = q[1].x, q1y = q[1].y;
264 real q2x = q[2].x, q2y = q[2].y;
265 real q3x = q[3].x, q3y = q[3].y;
274 real a = ((q2x - q3x)*(q1y - q2y) - (q2y - q3y)*(q1x - q2x)) * h;
275 real b = (q2x - q1x - q3x + q0x) * (q1y - q2y) - (q2y - q1y - q3y + q0y) * (q1x - q2x);
279 m20 = (m21 * (q2x - q3x) * h + q2x - q1x - q3x + q0x) / ((q1x - q2x) * w);
281 m20 = (m21 * (q2y - q3y) * h + q2y - q1y - q3y + q0y) / ((q1y - q2y) * w);
283 m00 = m20 * q1x + (q1x - q0x) / w;
284 m10 = m20 * q1y + (q1y - q0y) / w;
286 m01 = m21 * q3x + (q3x - q0x) / h;
287 m11 = m21 * q3y + (q3y - q0y) / h;
302 static int[3] a = { 2, 2, 1 };
303 static int[3] b = { 1, 0, 0 };
306 for (i = 0; i < 3; i++) {
310 p = m[i,0] * (m[ai,2] * m[bi,1] - m[ai,1] * m[bi,2]);
316 for (j = 0; j < 3; j++) {
317 for (i = 0; i < 3; i++) {
324 p = m[ai,aj] * m[bi,bj] - m[ai,bj] * m[bi,aj];
325 if (((i + j) & 1) != 0)
334 rescale (m_t m, real limit)
337 for (int j = 0; j < 3; j++)
338 for (int i = 0; i < 3; i++)
339 if ((real v = abs (m[j,i])) > max)
341 real scale = limit / max;
342 for (int j = 0; j < 3; j++)
343 for (int i = 0; i < 3; i++)
353 return sprintf ("%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f",
354 m[0,0],m[0,1],m[0,2],
355 m[1,0],m[1,1],m[1,2],
356 m[2,0],m[2,1],m[2,2]);
358 return sprintf ("%v,%v,%v,%v,%v,%v,%v,%v,%v",
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]);
367 return floor (x * 65536 + 0.5) & 0xffffffff;
373 for (int i = 0; i < 3; i++)
375 printf (" { 0x%08x, 0x%08x, 0x%08x },\n",
376 fixed (m[i,0]), fixed (m[i,1]), fixed (m[i,2]));
381 m_row (m_t m, int row)
383 return sprintf ("%10.5f %10.5f %10.5f",
384 m[row,0],m[row,1],m[row,2]);
387 Cairo::point_t[*] scale(Cairo::point_t[*] p, real w, real h)
389 for (int i = 0; i < dim (p); i++) {
404 output_t[*] get_outputs () {
405 output_t[...] outputs = {};
406 twixt (file randr = Process::popen (Process::popen_direction.read,
407 false, "xrandr", "xrandr");
410 while (!File::end (randr)) {
411 string[*] words = String::wordsplit (File::fgets (randr), " ");
412 if (dim (words) >= 3 && words[1] == "connected" &&
413 File::sscanf (words[2], "%dx%d+%d+%d",
414 &(int width), &(int height),
415 &(int x), &(int y)) == 4)
417 outputs[dim(outputs)] = (output_t) {
420 x = x, y = y, width = width, height = height
431 m_t m = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } }, m_i, m_r;
432 bool m_available = true;
433 output_t[*] outputs = get_outputs ();
434 output_t target_output;
436 if (dim (outputs) == 0) {
437 File::fprintf (stderr, "%s: No enabled outputs\n", argv[0]);
441 if (dim (argv) > 1) {
443 for (i = 0; i < dim (outputs); i++)
444 if (argv[1] == outputs[i].name) {
445 target_output = outputs[i];
448 if (i == dim (outputs)) {
449 File::fprintf (stderr, "%s: no enabled output \"%s\"\n",
455 target_output = outputs[0];
457 real target_width = target_output.geometry.width;
458 real target_height = target_output.geometry.height;
460 real screen_width = 0;
461 real screen_height = 0;
463 for (int i = 0; i < dim (outputs); i++)
465 screen_width = max (screen_width,
466 outputs[i].geometry.x +
467 outputs[i].geometry.width);
468 screen_height = max (screen_height,
469 outputs[i].geometry.y +
470 outputs[i].geometry.height);
473 &nichrome_t nichrome = Nichrome::new ("Keystone Correction", 400, 350);
476 &label_t space = Label::new (&nichrome, "");
477 for (int i = 0; i < 3; i++) {
478 label[i] = Label::new (&nichrome, "matrix");
479 label[i]->font = "sans-9";
482 void callback (&quad_t quad) {
483 real w = quad.geometry.width;
484 real h = quad.geometry.height;
487 m = solve (scale (quad.p, target_width / w, target_height / h),
488 target_width, target_height);
490 m_r = rescale (m_i, 16384);
491 for (int i = 0; i < 3; i++)
492 text[i] = m_row (m_i,i);
494 } catch divide_by_zero (real a, real b) {
495 text = (string[3]) { "no solution", "" ... };
498 for (int i = 0; i < 3; i++)
499 Label::relabel (label[i], text[i]);
501 &quad_t quad = Quad::new (&nichrome, callback);
503 void doit_func (&widget_t widget, bool state)
507 Process::system ("xrandr",
510 sprintf ("%dx%d", screen_width, screen_height),
518 &button_t doit = Button::new (&nichrome, "doit", doit_func);
520 void show_func (&widget_t widget, bool state)
524 printf ("normal: %s\n", m_print (m));
525 printf ("inverse: %s\n", m_print (m_i));
526 printf ("scaled: %s\n", m_print (m_r));
532 &button_t show = Button::new (&nichrome, "show", show_func);
533 &button_t quit = Button::new (&nichrome, "quit",
534 void func (&widget_t w, bool state) {
535 w.nichrome.running = false;
538 &box_t hbox = Box::new (Box::dir_t.horizontal,
539 Box::widget_item (&doit, 0),
540 Box::widget_item (&show, 0),
542 Box::widget_item (&quit, 0),
544 &box_t box = Box::new (Box::dir_t.vertical,
545 Box::box_item (&hbox),
546 Box::widget_item (label[0], 0),
547 Box::widget_item (label[1], 0),
548 Box::widget_item (label[2], 0),
549 Box::widget_item (&space, 0),
550 Box::widget_item (&quad, 1));
551 Nichrome::set_box (&nichrome, &box);
552 Nichrome::main_loop (&nichrome);