[cosmetic] separate c-lang format commit
[xbmc.git] / xbmc-xrandr.c
blob64438cdb00d60fd1f1b04a0be848768b8e7d62a4
1 /*
2 * Copyright © 2001 Keith Packard, member of The XFree86 Project, Inc.
3 * Copyright © 2002 Hewlett Packard Company, Inc.
4 * Copyright © 2006 Intel Corporation
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that copyright
9 * notice and this permission notice appear in supporting documentation, and
10 * that the name of the copyright holders not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. The copyright holders make no representations
13 * about the suitability of this software for any purpose. It is provided "as
14 * is" without express or implied warranty.
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
24 * Thanks to Jim Gettys who wrote most of the client side code,
25 * and part of the server code for randr.
28 #include <math.h>
29 #include <stdarg.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
35 #include <X11/Xatom.h>
36 #include <X11/Xlib.h>
37 #include <X11/Xlibint.h>
38 #include <X11/Xproto.h>
39 #include <X11/extensions/Xrandr.h>
40 #include <X11/extensions/Xrender.h> /* we share subpixel information */
41 #include <strings.h>
43 #ifndef _X_NORETURN
44 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)
45 #define _X_NORETURN __attribute((noreturn))
46 #else
47 #define _X_NORETURN
48 #endif
49 #endif
51 static char* program_name;
52 static Display* dpy;
53 static Window root;
54 static int screen = -1;
55 static Bool verbose = False;
56 static Bool automatic = False;
57 static Bool properties = False;
58 static Bool grab_server = True;
59 static Bool no_primary = False;
61 static char* direction[5] = {"normal", "left", "inverted", "right", "\n"};
63 static char* reflections[5] = {"normal", "x", "y", "xy", "\n"};
65 /* subpixel order */
66 static char* order[6] = {"unknown", "horizontal rgb", "horizontal bgr",
67 "vertical rgb", "vertical bgr", "no subpixels"};
69 static const struct
71 char* string;
72 unsigned long flag;
73 } mode_flags[] = {{"+HSync", RR_HSyncPositive}, {"-HSync", RR_HSyncNegative},
74 {"+VSync", RR_VSyncPositive}, {"-VSync", RR_VSyncNegative},
75 {"Interlace", RR_Interlace}, {"DoubleScan", RR_DoubleScan},
76 {"CSync", RR_CSync}, {"+CSync", RR_CSyncPositive},
77 {"-CSync", RR_CSyncNegative}, {NULL, 0}};
79 static void _X_NORETURN usage(void)
81 fprintf(stderr, "usage: %s [options]\n", program_name);
82 fprintf(stderr, " where options are:\n");
83 fprintf(stderr, " -display <display> or -d <display>\n");
84 fprintf(stderr, " -help\n");
85 fprintf(stderr, " -o <normal,inverted,left,right,0,1,2,3>\n");
86 fprintf(stderr, " or --orientation <normal,inverted,left,right,0,1,2,3>\n");
87 fprintf(stderr, " -q or --query\n");
88 fprintf(stderr, " -s <size>/<width>x<height> or --size <size>/<width>x<height>\n");
89 fprintf(stderr, " -r <rate> or --rate <rate> or --refresh <rate>\n");
90 fprintf(stderr, " -v or --version\n");
91 fprintf(stderr, " -x (reflect in x)\n");
92 fprintf(stderr, " -y (reflect in y)\n");
93 fprintf(stderr, " --screen <screen>\n");
94 fprintf(stderr, " --verbose\n");
95 fprintf(stderr, " --current\n");
96 fprintf(stderr, " --dryrun\n");
97 fprintf(stderr, " --nograb\n");
98 fprintf(stderr, " --prop or --properties\n");
99 fprintf(stderr, " --fb <width>x<height>\n");
100 fprintf(stderr, " --fbmm <width>x<height>\n");
101 fprintf(stderr, " --dpi <dpi>/<output>\n");
102 fprintf(stderr, " --output <output>\n");
103 fprintf(stderr, " --auto\n");
104 fprintf(stderr, " --mode <mode>\n");
105 fprintf(stderr, " --preferred\n");
106 fprintf(stderr, " --pos <x>x<y>\n");
107 fprintf(stderr, " --rate <rate> or --refresh <rate>\n");
108 fprintf(stderr, " --reflect normal,x,y,xy\n");
109 fprintf(stderr, " --rotate normal,inverted,left,right\n");
110 fprintf(stderr, " --left-of <output>\n");
111 fprintf(stderr, " --right-of <output>\n");
112 fprintf(stderr, " --above <output>\n");
113 fprintf(stderr, " --below <output>\n");
114 fprintf(stderr, " --same-as <output>\n");
115 fprintf(stderr, " --set <property> <value>\n");
116 fprintf(stderr, " --scale <x>x<y>\n");
117 fprintf(stderr, " --scale-from <w>x<h>\n");
118 fprintf(stderr, " --transform <a>,<b>,<c>,<d>,<e>,<f>,<g>,<h>,<i>\n");
119 fprintf(stderr, " --off\n");
120 fprintf(stderr, " --crtc <crtc>\n");
121 fprintf(stderr,
122 " --panning <w>x<h>[+<x>+<y>[/<track:w>x<h>+<x>+<y>[/<border:l>/<t>/<r>/<b>]]]\n");
123 fprintf(stderr, " --gamma <r>:<g>:<b>\n");
124 fprintf(stderr, " --primary\n");
125 fprintf(stderr, " --noprimary\n");
126 fprintf(stderr, " --newmode <name> <clock MHz>\n");
127 fprintf(stderr, " <hdisp> <hsync-start> <hsync-end> <htotal>\n");
128 fprintf(stderr, " <vdisp> <vsync-start> <vsync-end> <vtotal>\n");
129 fprintf(stderr, " [flags...]\n");
130 fprintf(stderr, " Valid flags: +HSync -HSync +VSync -VSync\n");
131 fprintf(stderr, " +CSync -CSync CSync Interlace DoubleScan\n");
132 fprintf(stderr, " --rmmode <name>\n");
133 fprintf(stderr, " --addmode <output> <name>\n");
134 fprintf(stderr, " --delmode <output> <name>\n");
136 exit(1);
137 /*NOTREACHED*/
140 static void _X_NORETURN fatal(const char* format, ...)
142 va_list ap;
144 va_start(ap, format);
145 fprintf(stderr, "%s: ", program_name);
146 vfprintf(stderr, format, ap);
147 va_end(ap);
148 exit(1);
149 /*NOTREACHED*/
152 static void warning(const char* format, ...)
154 va_list ap;
156 va_start(ap, format);
157 fprintf(stderr, "%s: ", program_name);
158 vfprintf(stderr, format, ap);
159 va_end(ap);
162 static char* rotation_name(Rotation rotation)
164 int i;
166 if ((rotation & 0xf) == 0)
167 return "normal";
168 for (i = 0; i < 4; i++)
169 if (rotation & (1 << i))
170 return direction[i];
171 return "invalid rotation";
174 static char* reflection_name(Rotation rotation)
176 rotation &= (RR_Reflect_X | RR_Reflect_Y);
177 switch (rotation)
179 case 0:
180 return "none";
181 case RR_Reflect_X:
182 return "X axis";
183 case RR_Reflect_Y:
184 return "Y axis";
185 case RR_Reflect_X | RR_Reflect_Y:
186 return "X and Y axis";
188 return "invalid reflection";
191 typedef enum _relation
193 relation_left_of,
194 relation_right_of,
195 relation_above,
196 relation_below,
197 relation_same_as,
198 } relation_t;
200 typedef struct
202 int x, y, width, height;
203 } rectangle_t;
205 typedef struct
207 int x1, y1, x2, y2;
208 } box_t;
210 typedef struct
212 int x, y;
213 } point_t;
215 typedef enum _changes
217 changes_none = 0,
218 changes_crtc = (1 << 0),
219 changes_mode = (1 << 1),
220 changes_relation = (1 << 2),
221 changes_position = (1 << 3),
222 changes_rotation = (1 << 4),
223 changes_reflection = (1 << 5),
224 changes_automatic = (1 << 6),
225 changes_refresh = (1 << 7),
226 changes_property = (1 << 8),
227 changes_transform = (1 << 9),
228 changes_panning = (1 << 10),
229 changes_gamma = (1 << 11),
230 changes_primary = (1 << 12),
231 } changes_t;
233 typedef enum _name_kind
235 name_none = 0,
236 name_string = (1 << 0),
237 name_xid = (1 << 1),
238 name_index = (1 << 2),
239 name_preferred = (1 << 3),
240 } name_kind_t;
242 typedef struct
244 name_kind_t kind;
245 char* string;
246 XID xid;
247 int index;
248 } name_t;
250 typedef struct _crtc crtc_t;
251 typedef struct _output output_t;
252 typedef struct _transform transform_t;
253 typedef struct _umode umode_t;
254 typedef struct _output_prop output_prop_t;
256 struct _transform
258 XTransform transform;
259 char* filter;
260 int nparams;
261 XFixed* params;
264 struct _crtc
266 name_t crtc;
267 Bool changing;
268 XRRCrtcInfo* crtc_info;
270 XRRModeInfo* mode_info;
271 XRRPanning* panning_info;
272 int x;
273 int y;
274 Rotation rotation;
275 output_t** outputs;
276 int noutput;
277 transform_t current_transform, pending_transform;
280 struct _output_prop
282 struct _output_prop* next;
283 char* name;
284 char* value;
287 struct _output
289 struct _output* next;
291 changes_t changes;
293 output_prop_t* props;
295 name_t output;
296 XRROutputInfo* output_info;
298 name_t crtc;
299 crtc_t* crtc_info;
300 crtc_t* current_crtc_info;
302 name_t mode;
303 double refresh;
304 XRRModeInfo* mode_info;
306 name_t addmode;
308 relation_t relation;
309 char* relative_to;
311 int x, y;
312 Rotation rotation;
314 XRRPanning panning;
316 Bool automatic;
317 int scale_from_w, scale_from_h;
318 transform_t transform;
320 struct
322 float red;
323 float green;
324 float blue;
325 } gamma;
327 float brightness;
329 Bool primary;
331 Bool found;
334 typedef enum _umode_action
336 umode_create,
337 umode_destroy,
338 umode_add,
339 umode_delete
340 } umode_action_t;
343 struct _umode
345 struct _umode* next;
347 umode_action_t action;
348 XRRModeInfo mode;
349 name_t output;
350 name_t name;
355 static char *connection[3] = {
356 "connected",
357 "disconnected",
358 "unknown connection"};
362 static char* connection[3] = {"true", "false", "unknown"};
364 #define OUTPUT_NAME 1
366 #define CRTC_OFF 2
367 #define CRTC_UNSET 3
368 #define CRTC_INDEX 0x40000000
370 #define MODE_NAME 1
371 #define MODE_OFF 2
372 #define MODE_UNSET 3
373 #define MODE_PREF 4
375 #define POS_UNSET -1
377 static output_t* outputs = NULL;
378 static output_t** outputs_tail = &outputs;
379 static crtc_t* crtcs;
380 static umode_t* umodes;
381 static int num_crtcs;
382 static XRRScreenResources* res;
383 static int fb_width = 0, fb_height = 0;
384 static int fb_width_mm = 0, fb_height_mm = 0;
385 static double dpi = 0;
386 static char* dpi_output = NULL;
387 static Bool dryrun = False;
388 static int minWidth, maxWidth, minHeight, maxHeight;
389 static Bool has_1_2 = False;
390 static Bool has_1_3 = False;
392 static int mode_height(XRRModeInfo* mode_info, Rotation rotation)
394 switch (rotation & 0xf)
396 case RR_Rotate_0:
397 case RR_Rotate_180:
398 return mode_info->height;
399 case RR_Rotate_90:
400 case RR_Rotate_270:
401 return mode_info->width;
402 default:
403 return 0;
407 static int mode_width(XRRModeInfo* mode_info, Rotation rotation)
409 switch (rotation & 0xf)
411 case RR_Rotate_0:
412 case RR_Rotate_180:
413 return mode_info->width;
414 case RR_Rotate_90:
415 case RR_Rotate_270:
416 return mode_info->height;
417 default:
418 return 0;
422 static Bool transform_point(XTransform* transform, double* xp, double* yp)
424 double vector[3];
425 double result[3];
426 int i, j;
427 double v;
429 vector[0] = *xp;
430 vector[1] = *yp;
431 vector[2] = 1;
432 for (j = 0; j < 3; j++)
434 v = 0;
435 for (i = 0; i < 3; i++)
436 v += (XFixedToDouble(transform->matrix[j][i]) * vector[i]);
437 result[j] = v;
439 if (!result[2])
440 return False;
441 for (j = 0; j < 2; j++)
443 vector[j] = result[j] / result[2];
444 if (vector[j] > 32767 || vector[j] < -32767)
445 return False;
447 *xp = vector[0];
448 *yp = vector[1];
449 return True;
452 static void path_bounds(XTransform* transform, point_t* points, int npoints, box_t* box)
454 int i;
455 box_t point;
457 for (i = 0; i < npoints; i++)
459 double x, y;
460 x = points[i].x;
461 y = points[i].y;
462 transform_point(transform, &x, &y);
463 point.x1 = floor(x);
464 point.y1 = floor(y);
465 point.x2 = ceil(x);
466 point.y2 = ceil(y);
467 if (i == 0)
468 *box = point;
469 else
471 if (point.x1 < box->x1)
472 box->x1 = point.x1;
473 if (point.y1 < box->y1)
474 box->y1 = point.y1;
475 if (point.x2 > box->x2)
476 box->x2 = point.x2;
477 if (point.y2 > box->y2)
478 box->y2 = point.y2;
483 static void mode_geometry(XRRModeInfo* mode_info,
484 Rotation rotation,
485 XTransform* transform,
486 box_t* bounds)
488 point_t rect[4];
489 int width = mode_width(mode_info, rotation);
490 int height = mode_height(mode_info, rotation);
492 rect[0].x = 0;
493 rect[0].y = 0;
494 rect[1].x = width;
495 rect[1].y = 0;
496 rect[2].x = width;
497 rect[2].y = height;
498 rect[3].x = 0;
499 rect[3].y = height;
500 path_bounds(transform, rect, 4, bounds);
503 /* v refresh frequency in Hz */
504 static double mode_refresh(XRRModeInfo* mode_info)
506 double rate;
508 if (mode_info->hTotal && mode_info->vTotal)
509 rate = ((double)mode_info->dotClock / ((double)mode_info->hTotal * (double)mode_info->vTotal));
510 else
511 rate = 0;
512 return rate;
515 /* h sync frequency in Hz */
516 static double mode_hsync(XRRModeInfo* mode_info)
518 double rate;
520 if (mode_info->hTotal)
521 rate = (double)mode_info->dotClock / (double)mode_info->hTotal;
522 else
523 rate = 0;
524 return rate;
527 static void init_name(name_t* name)
529 name->kind = name_none;
532 static void set_name_string(name_t* name, char* string)
534 name->kind |= name_string;
535 name->string = string;
538 static void set_name_xid(name_t* name, XID xid)
540 name->kind |= name_xid;
541 name->xid = xid;
544 static void set_name_index(name_t* name, int index)
546 name->kind |= name_index;
547 name->index = index;
550 static void set_name_preferred(name_t* name)
552 name->kind |= name_preferred;
555 static void set_name_all(name_t* name, name_t* old)
557 if (old->kind & name_xid)
558 name->xid = old->xid;
559 if (old->kind & name_string)
560 name->string = old->string;
561 if (old->kind & name_index)
562 name->index = old->index;
563 name->kind |= old->kind;
566 static void set_name(name_t* name, char* string, name_kind_t valid)
568 unsigned int xid; /* don't make it XID (which is unsigned long):
569 scanf() takes unsigned int */
570 int index;
572 if ((valid & name_xid) && sscanf(string, "0x%x", &xid) == 1)
573 set_name_xid(name, xid);
574 else if ((valid & name_index) && sscanf(string, "%d", &index) == 1)
575 set_name_index(name, index);
576 else if (valid & name_string)
577 set_name_string(name, string);
578 else
579 usage();
582 static void init_transform(transform_t* transform)
584 int x;
585 memset(&transform->transform, '\0', sizeof(transform->transform));
586 for (x = 0; x < 3; x++)
587 transform->transform.matrix[x][x] = XDoubleToFixed(1.0);
588 transform->filter = "";
589 transform->nparams = 0;
590 transform->params = NULL;
593 static void set_transform(
594 transform_t* dest, XTransform* transform, char* filter, XFixed* params, int nparams)
596 dest->transform = *transform;
597 dest->filter = strdup(filter);
598 dest->nparams = nparams;
599 dest->params = malloc(nparams * sizeof(XFixed));
600 memcpy(dest->params, params, nparams * sizeof(XFixed));
603 static void copy_transform(transform_t* dest, transform_t* src)
605 set_transform(dest, &src->transform, src->filter, src->params, src->nparams);
608 static Bool equal_transform(transform_t* a, transform_t* b)
610 if (memcmp(&a->transform, &b->transform, sizeof(XTransform)) != 0)
611 return False;
612 if (strcmp(a->filter, b->filter) != 0)
613 return False;
614 if (a->nparams != b->nparams)
615 return False;
616 if (memcmp(a->params, b->params, a->nparams * sizeof(XFixed)) != 0)
617 return False;
618 return True;
621 static output_t* add_output(void)
623 output_t* output = calloc(1, sizeof(output_t));
625 if (!output)
626 fatal("out of memory\n");
627 output->next = NULL;
628 output->found = False;
629 output->brightness = 1.0;
630 *outputs_tail = output;
631 outputs_tail = &output->next;
632 return output;
635 static output_t* find_output(name_t* name)
637 output_t* output;
639 for (output = outputs; output; output = output->next)
641 name_kind_t common = name->kind & output->output.kind;
643 if ((common & name_xid) && name->xid == output->output.xid)
644 break;
645 if ((common & name_string) && !strcmp(name->string, output->output.string))
646 break;
647 if ((common & name_index) && name->index == output->output.index)
648 break;
650 return output;
653 static output_t* find_output_by_xid(RROutput output)
655 name_t output_name;
657 init_name(&output_name);
658 set_name_xid(&output_name, output);
659 return find_output(&output_name);
662 static output_t* find_output_by_name(char* name)
664 name_t output_name;
666 init_name(&output_name);
667 set_name_string(&output_name, name);
668 return find_output(&output_name);
671 static crtc_t* find_crtc(name_t* name)
673 int c;
674 crtc_t* crtc = NULL;
676 for (c = 0; c < num_crtcs; c++)
678 name_kind_t common;
680 crtc = &crtcs[c];
681 common = name->kind & crtc->crtc.kind;
683 if ((common & name_xid) && name->xid == crtc->crtc.xid)
684 break;
685 if ((common & name_string) && !strcmp(name->string, crtc->crtc.string))
686 break;
687 if ((common & name_index) && name->index == crtc->crtc.index)
688 break;
689 crtc = NULL;
691 return crtc;
694 static crtc_t* find_crtc_by_xid(RRCrtc crtc)
696 name_t crtc_name;
698 init_name(&crtc_name);
699 set_name_xid(&crtc_name, crtc);
700 return find_crtc(&crtc_name);
703 static XRRModeInfo* find_mode(name_t* name, double refresh)
705 int m;
706 XRRModeInfo* best = NULL;
707 double bestDist = 0;
709 for (m = 0; m < res->nmode; m++)
711 XRRModeInfo* mode = &res->modes[m];
712 if ((name->kind & name_xid) && name->xid == mode->id)
714 best = mode;
715 break;
717 if ((name->kind & name_string) && !strcmp(name->string, mode->name))
719 double dist;
721 if (refresh)
722 dist = fabs(mode_refresh(mode) - refresh);
723 else
724 dist = 0;
725 if (!best || dist < bestDist)
727 bestDist = dist;
728 best = mode;
732 return best;
735 static XRRModeInfo* find_mode_by_xid(RRMode mode)
737 name_t mode_name;
739 init_name(&mode_name);
740 set_name_xid(&mode_name, mode);
741 return find_mode(&mode_name, 0);
744 #if 0
745 static XRRModeInfo *
746 find_mode_by_name (char *name)
748 name_t mode_name;
749 init_name (&mode_name);
750 set_name_string (&mode_name, name);
751 return find_mode (&mode_name, 0);
753 #endif
755 static XRRModeInfo* find_mode_for_output(output_t* output, name_t* name)
757 XRROutputInfo* output_info = output->output_info;
758 int m;
759 XRRModeInfo* best = NULL;
760 double bestDist = 0;
762 for (m = 0; m < output_info->nmode; m++)
764 XRRModeInfo* mode;
766 mode = find_mode_by_xid(output_info->modes[m]);
767 if (!mode)
768 continue;
769 if ((name->kind & name_xid) && name->xid == mode->id)
771 best = mode;
772 break;
774 if ((name->kind & name_string) && !strcmp(name->string, mode->name))
776 double dist;
778 /* Stay away from doublescan modes unless refresh rate is specified. */
779 if (!output->refresh && (mode->modeFlags & RR_DoubleScan))
780 continue;
782 if (output->refresh)
783 dist = fabs(mode_refresh(mode) - output->refresh);
784 else
785 dist = 0;
786 if (!best || dist < bestDist)
788 bestDist = dist;
789 best = mode;
793 return best;
796 static XRRModeInfo* preferred_mode(output_t* output)
798 XRROutputInfo* output_info = output->output_info;
799 int m;
800 XRRModeInfo* best;
801 int bestDist;
803 best = NULL;
804 bestDist = 0;
805 for (m = 0; m < output_info->nmode; m++)
807 XRRModeInfo* mode_info = find_mode_by_xid(output_info->modes[m]);
808 int dist;
810 if (m < output_info->npreferred)
811 dist = 0;
812 else if (output_info->mm_height)
813 dist = (1000 * DisplayHeight(dpy, screen) / DisplayHeightMM(dpy, screen) -
814 1000 * mode_info->height / output_info->mm_height);
815 else
816 dist = DisplayHeight(dpy, screen) - mode_info->height;
818 if (dist < 0)
819 dist = -dist;
820 if (!best || dist < bestDist)
822 best = mode_info;
823 bestDist = dist;
826 return best;
829 static Bool output_can_use_crtc(output_t* output, crtc_t* crtc)
831 XRROutputInfo* output_info = output->output_info;
832 int c;
834 for (c = 0; c < output_info->ncrtc; c++)
835 if (output_info->crtcs[c] == crtc->crtc.xid)
836 return True;
837 return False;
840 static Bool output_can_use_mode(output_t* output, XRRModeInfo* mode)
842 XRROutputInfo* output_info = output->output_info;
843 int m;
845 for (m = 0; m < output_info->nmode; m++)
846 if (output_info->modes[m] == mode->id)
847 return True;
848 return False;
851 static Bool crtc_can_use_rotation(crtc_t* crtc, Rotation rotation)
853 Rotation rotations = crtc->crtc_info->rotations;
854 Rotation dir = rotation & (RR_Rotate_0 | RR_Rotate_90 | RR_Rotate_180 | RR_Rotate_270);
855 Rotation reflect = rotation & (RR_Reflect_X | RR_Reflect_Y);
856 if (((rotations & dir) != 0) && ((rotations & reflect) == reflect))
857 return True;
858 return False;
861 #if 0
862 static Bool
863 crtc_can_use_transform (crtc_t *crtc, XTransform *transform)
865 int major, minor;
867 XRRQueryVersion (dpy, &major, &minor);
868 if (major > 1 || (major == 1 && minor >= 3))
869 return True;
870 return False;
874 * Report only rotations that are supported by all crtcs
876 static Rotation
877 output_rotations (output_t *output)
879 Bool found = False;
880 Rotation rotation = RR_Rotate_0;
881 XRROutputInfo *output_info = output->output_info;
882 int c;
884 for (c = 0; c < output_info->ncrtc; c++)
886 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
887 if (crtc)
889 if (!found) {
890 rotation = crtc->crtc_info->rotations;
891 found = True;
892 } else
893 rotation &= crtc->crtc_info->rotations;
896 return rotation;
898 #endif
900 static Bool output_can_use_rotation(output_t* output, Rotation rotation)
902 XRROutputInfo* output_info = output->output_info;
903 int c;
905 /* make sure all of the crtcs can use this rotation.
906 * yes, this is not strictly necessary, but it is
907 * simpler,and we expect most drivers to either
908 * support rotation everywhere or nowhere
910 for (c = 0; c < output_info->ncrtc; c++)
912 crtc_t* crtc = find_crtc_by_xid(output_info->crtcs[c]);
913 if (crtc && !crtc_can_use_rotation(crtc, rotation))
914 return False;
916 return True;
919 static Bool output_is_primary(output_t* output)
921 if (has_1_3)
922 return XRRGetOutputPrimary(dpy, root) == output->output.xid;
923 return False;
926 /* Returns the index of the last value in an array < 0xffff */
927 static int find_last_non_clamped(CARD16 array[], int size)
929 int i;
930 for (i = size - 1; i > 0; i--)
932 if (array[i] < 0xffff)
933 return i;
935 return 0;
938 static void set_gamma_info(output_t* output)
940 XRRCrtcGamma* gamma;
941 double i1, v1, i2, v2;
942 int size, middle, last_best, last_red, last_green, last_blue;
943 CARD16* best_array;
945 if (!output->crtc_info)
946 return;
948 size = XRRGetCrtcGammaSize(dpy, output->crtc_info->crtc.xid);
949 if (!size)
951 warning("Failed to get size of gamma for output %s\n", output->output.string);
952 return;
955 gamma = XRRGetCrtcGamma(dpy, output->crtc_info->crtc.xid);
956 if (!gamma)
958 warning("Failed to get gamma for output %s\n", output->output.string);
959 return;
963 * Here is a bit tricky because gamma is a whole curve for each
964 * color. So, typically, we need to represent 3 * 256 values as 3 + 1
965 * values. Therefore, we approximate the gamma curve (v) by supposing
966 * it always follows the way we set it: a power function (i^g)
967 * multiplied by a brightness (b).
968 * v = i^g * b
969 * so g = (ln(v) - ln(b))/ln(i)
970 * and b can be found using two points (v1,i1) and (v2, i2):
971 * b = e^((ln(v2)*ln(i1) - ln(v1)*ln(i2))/ln(i1/i2))
972 * For the best resolution, we select i2 at the highest place not
973 * clamped and i1 at i2/2. Note that if i2 = 1 (as in most normal
974 * cases), then b = v2.
976 last_red = find_last_non_clamped(gamma->red, size);
977 last_green = find_last_non_clamped(gamma->green, size);
978 last_blue = find_last_non_clamped(gamma->blue, size);
979 best_array = gamma->red;
980 last_best = last_red;
981 if (last_green > last_best)
983 last_best = last_green;
984 best_array = gamma->green;
986 if (last_blue > last_best)
988 last_best = last_blue;
989 best_array = gamma->blue;
991 if (last_best == 0)
992 last_best = 1;
994 middle = last_best / 2;
995 i1 = (double)(middle + 1) / size;
996 v1 = (double)(best_array[middle]) / 65535;
997 i2 = (double)(last_best + 1) / size;
998 v2 = (double)(best_array[last_best]) / 65535;
999 if (v2 < 0.0001)
1000 { /* The screen is black */
1001 output->brightness = 0;
1002 output->gamma.red = 1;
1003 output->gamma.green = 1;
1004 output->gamma.blue = 1;
1006 else
1008 if ((last_best + 1) == size)
1009 output->brightness = v2;
1010 else
1011 output->brightness = exp((log(v2) * log(i1) - log(v1) * log(i2)) / log(i1 / i2));
1012 output->gamma.red = logf((gamma->red[last_red / 2]) / output->brightness / 65535) /
1013 logf(((last_red / 2) + 1) / size);
1014 output->gamma.green = logf((gamma->green[last_green / 2]) / output->brightness / 65535) /
1015 logf(((last_green / 2) + 1) / size);
1016 output->gamma.blue = logf((gamma->blue[last_blue / 2]) / output->brightness / 65535) /
1017 logf(((last_blue / 2) + 1) / size);
1020 XRRFreeGamma(gamma);
1023 static void set_output_info(output_t* output, RROutput xid, XRROutputInfo* output_info)
1025 /* sanity check output info */
1026 if (output_info->connection != RR_Disconnected && !output_info->nmode)
1027 warning("Output %s is not disconnected but has no modes\n", output_info->name);
1029 /* set output name and info */
1030 if (!(output->output.kind & name_xid))
1031 set_name_xid(&output->output, xid);
1032 if (!(output->output.kind & name_string))
1033 set_name_string(&output->output, output_info->name);
1034 output->output_info = output_info;
1036 /* set crtc name and info */
1037 if (!(output->changes & changes_crtc))
1038 set_name_xid(&output->crtc, output_info->crtc);
1040 if (output->crtc.kind == name_xid && output->crtc.xid == None)
1041 output->crtc_info = NULL;
1042 else
1044 output->crtc_info = find_crtc(&output->crtc);
1045 if (!output->crtc_info)
1047 if (output->crtc.kind & name_xid)
1048 fatal("cannot find crtc 0x%x\n", output->crtc.xid);
1049 if (output->crtc.kind & name_index)
1050 fatal("cannot find crtc %d\n", output->crtc.index);
1052 if (!output_can_use_crtc(output, output->crtc_info))
1053 fatal("output %s cannot use crtc 0x%x\n", output->output.string, output->crtc_info->crtc.xid);
1056 /* set mode name and info */
1057 if (!(output->changes & changes_mode))
1059 crtc_t* crtc = NULL;
1061 if (output_info->crtc)
1062 crtc = find_crtc_by_xid(output_info->crtc);
1063 if (crtc && crtc->crtc_info)
1064 set_name_xid(&output->mode, crtc->crtc_info->mode);
1065 else if (output->crtc_info)
1066 set_name_xid(&output->mode, output->crtc_info->crtc_info->mode);
1067 else
1068 set_name_xid(&output->mode, None);
1069 if (output->mode.xid)
1071 output->mode_info = find_mode_by_xid(output->mode.xid);
1072 if (!output->mode_info)
1073 fatal("server did not report mode 0x%x for output %s\n", output->mode.xid,
1074 output->output.string);
1076 else
1077 output->mode_info = NULL;
1079 else if (output->mode.kind == name_xid && output->mode.xid == None)
1080 output->mode_info = NULL;
1081 else
1083 if (output->mode.kind == name_preferred)
1084 output->mode_info = preferred_mode(output);
1085 else
1086 output->mode_info = find_mode_for_output(output, &output->mode);
1087 if (!output->mode_info)
1089 if (output->mode.kind & name_preferred)
1090 fatal("cannot find preferred mode\n");
1091 if (output->mode.kind & name_string)
1092 fatal("cannot find mode %s\n", output->mode.string);
1093 if (output->mode.kind & name_xid)
1094 fatal("cannot find mode 0x%x\n", output->mode.xid);
1096 if (!output_can_use_mode(output, output->mode_info))
1097 fatal("output %s cannot use mode %s\n", output->output.string, output->mode_info->name);
1100 /* set position */
1101 if (!(output->changes & changes_position))
1103 if (output->crtc_info)
1105 output->x = output->crtc_info->crtc_info->x;
1106 output->y = output->crtc_info->crtc_info->y;
1108 else
1110 output->x = 0;
1111 output->y = 0;
1115 /* set rotation */
1116 if (!(output->changes & changes_rotation))
1118 output->rotation &= ~0xf;
1119 if (output->crtc_info)
1120 output->rotation |= (output->crtc_info->crtc_info->rotation & 0xf);
1121 else
1122 output->rotation = RR_Rotate_0;
1124 if (!(output->changes & changes_reflection))
1126 output->rotation &= ~(RR_Reflect_X | RR_Reflect_Y);
1127 if (output->crtc_info)
1128 output->rotation |= (output->crtc_info->crtc_info->rotation & (RR_Reflect_X | RR_Reflect_Y));
1130 if (!output_can_use_rotation(output, output->rotation))
1131 fatal("output %s cannot use rotation \"%s\" reflection \"%s\"\n", output->output.string,
1132 rotation_name(output->rotation), reflection_name(output->rotation));
1134 /* set gamma */
1135 if (!(output->changes & changes_gamma))
1136 set_gamma_info(output);
1138 /* set transformation */
1139 if (!(output->changes & changes_transform))
1141 if (output->crtc_info)
1142 copy_transform(&output->transform, &output->crtc_info->current_transform);
1143 else
1144 init_transform(&output->transform);
1146 else
1148 /* transform was already set for --scale or --transform */
1150 /* for --scale-from, figure out the mode size and compute the transform
1151 * for the target framebuffer area */
1152 if (output->scale_from_w > 0 && output->mode_info)
1154 double sx = (double)output->scale_from_w / output->mode_info->width;
1155 double sy = (double)output->scale_from_h / output->mode_info->height;
1156 if (verbose)
1157 printf("scaling %s by %lfx%lf\n", output->output.string, sx, sy);
1158 init_transform(&output->transform);
1159 output->transform.transform.matrix[0][0] = XDoubleToFixed(sx);
1160 output->transform.transform.matrix[1][1] = XDoubleToFixed(sy);
1161 output->transform.transform.matrix[2][2] = XDoubleToFixed(1.0);
1162 if (sx != 1 || sy != 1)
1163 output->transform.filter = "bilinear";
1164 else
1165 output->transform.filter = "nearest";
1166 output->transform.nparams = 0;
1167 output->transform.params = NULL;
1171 /* set primary */
1172 if (!(output->changes & changes_primary))
1173 output->primary = output_is_primary(output);
1176 static void get_screen(Bool current)
1178 if (!has_1_2)
1179 fatal("Server RandR version before 1.2\n");
1181 XRRGetScreenSizeRange(dpy, root, &minWidth, &minHeight, &maxWidth, &maxHeight);
1183 if (current)
1184 res = XRRGetScreenResourcesCurrent(dpy, root);
1185 else
1186 res = XRRGetScreenResources(dpy, root);
1187 if (!res)
1188 fatal("could not get screen resources");
1191 static void get_crtcs(void)
1193 int c;
1195 num_crtcs = res->ncrtc;
1196 crtcs = calloc(num_crtcs, sizeof(crtc_t));
1197 if (!crtcs)
1198 fatal("out of memory\n");
1200 for (c = 0; c < res->ncrtc; c++)
1202 XRRCrtcInfo* crtc_info = XRRGetCrtcInfo(dpy, res, res->crtcs[c]);
1203 XRRCrtcTransformAttributes* attr;
1204 XRRPanning* panning_info = NULL;
1206 if (has_1_3)
1208 XRRPanning zero;
1209 memset(&zero, 0, sizeof(zero));
1210 panning_info = XRRGetPanning(dpy, res, res->crtcs[c]);
1211 zero.timestamp = panning_info->timestamp;
1212 if (!memcmp(panning_info, &zero, sizeof(zero)))
1214 Xfree(panning_info);
1215 panning_info = NULL;
1219 set_name_xid(&crtcs[c].crtc, res->crtcs[c]);
1220 set_name_index(&crtcs[c].crtc, c);
1221 if (!crtc_info)
1222 fatal("could not get crtc 0x%x information\n", res->crtcs[c]);
1223 crtcs[c].crtc_info = crtc_info;
1224 crtcs[c].panning_info = panning_info;
1225 if (crtc_info->mode == None)
1227 crtcs[c].mode_info = NULL;
1228 crtcs[c].x = 0;
1229 crtcs[c].y = 0;
1230 crtcs[c].rotation = RR_Rotate_0;
1232 if (XRRGetCrtcTransform(dpy, res->crtcs[c], &attr) && attr)
1234 set_transform(&crtcs[c].current_transform, &attr->currentTransform, attr->currentFilter,
1235 attr->currentParams, attr->currentNparams);
1236 XFree(attr);
1238 else
1240 init_transform(&crtcs[c].current_transform);
1242 copy_transform(&crtcs[c].pending_transform, &crtcs[c].current_transform);
1246 static void crtc_add_output(crtc_t* crtc, output_t* output)
1248 if (crtc->outputs)
1249 crtc->outputs = realloc(crtc->outputs, (crtc->noutput + 1) * sizeof(output_t*));
1250 else
1252 crtc->outputs = malloc(sizeof(output_t*));
1253 crtc->x = output->x;
1254 crtc->y = output->y;
1255 crtc->rotation = output->rotation;
1256 crtc->mode_info = output->mode_info;
1257 copy_transform(&crtc->pending_transform, &output->transform);
1259 if (!crtc->outputs)
1260 fatal("out of memory\n");
1261 crtc->outputs[crtc->noutput++] = output;
1264 static void set_crtcs(void)
1266 output_t* output;
1268 for (output = outputs; output; output = output->next)
1270 if (!output->mode_info)
1271 continue;
1272 crtc_add_output(output->crtc_info, output);
1276 static void set_panning(void)
1278 output_t* output;
1280 for (output = outputs; output; output = output->next)
1282 if (!output->crtc_info)
1283 continue;
1284 if (!(output->changes & changes_panning))
1285 continue;
1286 if (!output->crtc_info->panning_info)
1287 output->crtc_info->panning_info = malloc(sizeof(XRRPanning));
1288 memcpy(output->crtc_info->panning_info, &output->panning, sizeof(XRRPanning));
1289 output->crtc_info->changing = 1;
1293 static void set_gamma(void)
1295 output_t* output;
1297 for (output = outputs; output; output = output->next)
1299 int i, size;
1300 crtc_t* crtc;
1301 XRRCrtcGamma* gamma;
1303 if (!(output->changes & changes_gamma))
1304 continue;
1306 if (!output->crtc_info)
1308 fatal("Need crtc to set gamma on.\n");
1309 continue;
1312 crtc = output->crtc_info;
1314 size = XRRGetCrtcGammaSize(dpy, crtc->crtc.xid);
1316 if (!size)
1318 fatal("Gamma size is 0.\n");
1319 continue;
1322 gamma = XRRAllocGamma(size);
1323 if (!gamma)
1325 fatal("Gamma allocation failed.\n");
1326 continue;
1329 if (output->gamma.red == 0.0f && output->gamma.green == 0.0f && output->gamma.blue == 0.0f)
1330 output->gamma.red = output->gamma.green = output->gamma.blue = 1.0f;
1332 for (i = 0; i < size; i++)
1334 if (output->gamma.red == 1.0f && output->brightness == 1.0f)
1335 gamma->red[i] = (i << 8) + i;
1336 else
1337 gamma->red[i] =
1338 min(powf((float)i / (float)(size - 1), output->gamma.red) * output->brightness, 1.0f) *
1339 65535.0f;
1341 if (output->gamma.green == 1.0f && output->brightness == 1.0f)
1342 gamma->green[i] = (i << 8) + i;
1343 else
1344 gamma->green[i] =
1345 min(powf((float)i / (float)(size - 1), output->gamma.green) * output->brightness,
1346 1.0f) *
1347 65535.0f;
1349 if (output->gamma.blue == 1.0f && output->brightness == 1.0f)
1350 gamma->blue[i] = (i << 8) + i;
1351 else
1352 gamma->blue[i] =
1353 min(powf((float)i / (float)(size - 1), output->gamma.blue) * output->brightness, 1.0f) *
1354 65535.0f;
1357 XRRSetCrtcGamma(dpy, crtc->crtc.xid, gamma);
1359 free(gamma);
1363 static void set_primary(void)
1365 output_t* output;
1367 if (no_primary)
1369 XRRSetOutputPrimary(dpy, root, None);
1371 else
1373 for (output = outputs; output; output = output->next)
1375 if (!(output->changes & changes_primary))
1376 continue;
1377 if (output->primary)
1378 XRRSetOutputPrimary(dpy, root, output->output.xid);
1383 static Status crtc_disable(crtc_t* crtc)
1385 if (verbose)
1386 printf("crtc %d: disable\n", crtc->crtc.index);
1388 if (dryrun)
1389 return RRSetConfigSuccess;
1390 return XRRSetCrtcConfig(dpy, res, crtc->crtc.xid, CurrentTime, 0, 0, None, RR_Rotate_0, NULL, 0);
1393 static void crtc_set_transform(crtc_t* crtc, transform_t* transform)
1395 int major, minor;
1397 XRRQueryVersion(dpy, &major, &minor);
1398 if (major > 1 || (major == 1 && minor >= 3))
1399 XRRSetCrtcTransform(dpy, crtc->crtc.xid, &transform->transform, transform->filter,
1400 transform->params, transform->nparams);
1403 static Status crtc_revert(crtc_t* crtc)
1405 XRRCrtcInfo* crtc_info = crtc->crtc_info;
1407 if (verbose)
1408 printf("crtc %d: revert\n", crtc->crtc.index);
1410 if (dryrun)
1411 return RRSetConfigSuccess;
1413 if (!equal_transform(&crtc->current_transform, &crtc->pending_transform))
1414 crtc_set_transform(crtc, &crtc->current_transform);
1415 return XRRSetCrtcConfig(dpy, res, crtc->crtc.xid, CurrentTime, crtc_info->x, crtc_info->y,
1416 crtc_info->mode, crtc_info->rotation, crtc_info->outputs,
1417 crtc_info->noutput);
1420 static Status crtc_apply(crtc_t* crtc)
1422 RROutput* rr_outputs;
1423 int o;
1424 Status s;
1425 RRMode mode = None;
1427 if (!crtc->changing || !crtc->mode_info)
1428 return RRSetConfigSuccess;
1430 rr_outputs = calloc(crtc->noutput, sizeof(RROutput));
1431 if (!rr_outputs)
1432 return BadAlloc;
1433 for (o = 0; o < crtc->noutput; o++)
1434 rr_outputs[o] = crtc->outputs[o]->output.xid;
1435 mode = crtc->mode_info->id;
1436 if (verbose)
1438 printf("crtc %d: %12s %6.1f +%d+%d", crtc->crtc.index, crtc->mode_info->name,
1439 mode_refresh(crtc->mode_info), crtc->x, crtc->y);
1440 for (o = 0; o < crtc->noutput; o++)
1441 printf(" \"%s\"", crtc->outputs[o]->output.string);
1442 printf("\n");
1445 if (dryrun)
1446 s = RRSetConfigSuccess;
1447 else
1449 if (!equal_transform(&crtc->current_transform, &crtc->pending_transform))
1450 crtc_set_transform(crtc, &crtc->pending_transform);
1451 s = XRRSetCrtcConfig(dpy, res, crtc->crtc.xid, CurrentTime, crtc->x, crtc->y, mode,
1452 crtc->rotation, rr_outputs, crtc->noutput);
1453 if (s == RRSetConfigSuccess && crtc->panning_info)
1455 if (has_1_3)
1456 s = XRRSetPanning(dpy, res, crtc->crtc.xid, crtc->panning_info);
1457 else
1458 fatal("panning needs RandR 1.3\n");
1461 free(rr_outputs);
1462 return s;
1465 static void screen_revert(void)
1467 if (verbose)
1468 printf("screen %d: revert\n", screen);
1470 if (dryrun)
1471 return;
1472 XRRSetScreenSize(dpy, root, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
1473 DisplayWidthMM(dpy, screen), DisplayHeightMM(dpy, screen));
1476 static void screen_apply(void)
1478 if (fb_width == DisplayWidth(dpy, screen) && fb_height == DisplayHeight(dpy, screen) &&
1479 fb_width_mm == DisplayWidthMM(dpy, screen) && fb_height_mm == DisplayHeightMM(dpy, screen))
1481 return;
1483 if (verbose)
1484 printf("screen %d: %dx%d %dx%d mm %6.2fdpi\n", screen, fb_width, fb_height, fb_width_mm,
1485 fb_height_mm, dpi);
1486 if (dryrun)
1487 return;
1488 XRRSetScreenSize(dpy, root, fb_width, fb_height, fb_width_mm, fb_height_mm);
1491 static void revert(void)
1493 int c;
1495 /* first disable all crtcs */
1496 for (c = 0; c < res->ncrtc; c++)
1497 crtc_disable(&crtcs[c]);
1498 /* next reset screen size */
1499 screen_revert();
1500 /* now restore all crtcs */
1501 for (c = 0; c < res->ncrtc; c++)
1502 crtc_revert(&crtcs[c]);
1506 * uh-oh, something bad happened in the middle of changing
1507 * the configuration. Revert to the previous configuration
1508 * and bail
1510 static void _X_NORETURN panic(Status s, crtc_t* crtc)
1512 int c = crtc->crtc.index;
1513 char* message;
1515 switch (s)
1517 case RRSetConfigSuccess:
1518 message = "succeeded";
1519 break;
1520 case BadAlloc:
1521 message = "out of memory";
1522 break;
1523 case RRSetConfigFailed:
1524 message = "failed";
1525 break;
1526 case RRSetConfigInvalidConfigTime:
1527 message = "invalid config time";
1528 break;
1529 case RRSetConfigInvalidTime:
1530 message = "invalid time";
1531 break;
1532 default:
1533 message = "unknown failure";
1534 break;
1537 fprintf(stderr, "%s: Configure crtc %d %s\n", program_name, c, message);
1538 revert();
1539 exit(1);
1542 static void apply(void)
1544 Status s;
1545 int c;
1548 * Hold the server grabbed while messing with
1549 * the screen so that apps which notice the resize
1550 * event and ask for xinerama information from the server
1551 * receive up-to-date information
1553 if (grab_server)
1554 XGrabServer(dpy);
1557 * Turn off any crtcs which are to be disabled or which are
1558 * larger than the target size
1560 for (c = 0; c < res->ncrtc; c++)
1562 crtc_t* crtc = &crtcs[c];
1563 XRRCrtcInfo* crtc_info = crtc->crtc_info;
1565 /* if this crtc is already disabled, skip it */
1566 if (crtc_info->mode == None)
1567 continue;
1570 * If this crtc is to be left enabled, make
1571 * sure the old size fits then new screen
1573 if (crtc->mode_info)
1575 XRRModeInfo* old_mode = find_mode_by_xid(crtc_info->mode);
1576 int x, y, w, h;
1577 box_t bounds;
1579 if (!old_mode)
1580 panic(RRSetConfigFailed, crtc);
1582 /* old position and size information */
1583 mode_geometry(old_mode, crtc_info->rotation, &crtc->current_transform.transform, &bounds);
1585 x = crtc_info->x + bounds.x1;
1586 y = crtc_info->y + bounds.y1;
1587 w = bounds.x2 - bounds.x1;
1588 h = bounds.y2 - bounds.y1;
1590 /* if it fits, skip it */
1591 if (x + w <= fb_width && y + h <= fb_height)
1592 continue;
1593 crtc->changing = True;
1595 s = crtc_disable(crtc);
1596 if (s != RRSetConfigSuccess)
1597 panic(s, crtc);
1601 * Set the screen size
1603 screen_apply();
1606 * Set crtcs
1609 for (c = 0; c < res->ncrtc; c++)
1611 crtc_t* crtc = &crtcs[c];
1613 s = crtc_apply(crtc);
1614 if (s != RRSetConfigSuccess)
1615 panic(s, crtc);
1618 set_primary();
1621 * Release the server grab and let all clients
1622 * respond to the updated state
1624 if (grab_server)
1625 XUngrabServer(dpy);
1629 * Use current output state to complete the output list
1631 static void get_outputs(void)
1633 int o;
1634 output_t* q;
1636 for (o = 0; o < res->noutput; o++)
1638 XRROutputInfo* output_info = XRRGetOutputInfo(dpy, res, res->outputs[o]);
1639 output_t* output;
1640 name_t output_name;
1641 if (!output_info)
1642 fatal("could not get output 0x%x information\n", res->outputs[o]);
1643 init_name(&output_name);
1644 set_name_xid(&output_name, res->outputs[o]);
1645 set_name_index(&output_name, o);
1646 set_name_string(&output_name, output_info->name);
1647 output = find_output(&output_name);
1648 if (!output)
1650 output = add_output();
1651 set_name_all(&output->output, &output_name);
1653 * When global --automatic mode is set, turn on connected but off
1654 * outputs, turn off disconnected but on outputs
1656 if (automatic)
1658 switch (output_info->connection)
1660 case RR_Connected:
1661 if (!output_info->crtc)
1663 output->changes |= changes_automatic;
1664 output->automatic = True;
1666 break;
1667 case RR_Disconnected:
1668 if (output_info->crtc)
1670 output->changes |= changes_automatic;
1671 output->automatic = True;
1673 break;
1677 output->found = True;
1680 * Automatic mode -- track connection state and enable/disable outputs
1681 * as necessary
1683 if (output->automatic)
1685 switch (output_info->connection)
1687 case RR_Connected:
1688 case RR_UnknownConnection:
1689 if ((!(output->changes & changes_mode)))
1691 set_name_preferred(&output->mode);
1692 output->changes |= changes_mode;
1694 break;
1695 case RR_Disconnected:
1696 if ((!(output->changes & changes_mode)))
1698 set_name_xid(&output->mode, None);
1699 set_name_xid(&output->crtc, None);
1700 output->changes |= changes_mode;
1701 output->changes |= changes_crtc;
1703 break;
1707 set_output_info(output, res->outputs[o], output_info);
1709 for (q = outputs; q; q = q->next)
1711 if (!q->found)
1713 fprintf(stderr, "warning: output %s not found; ignoring\n", q->output.string);
1718 static void mark_changing_crtcs(void)
1720 int c;
1722 for (c = 0; c < num_crtcs; c++)
1724 crtc_t* crtc = &crtcs[c];
1725 int o;
1726 output_t* output;
1728 /* walk old output list (to catch disables) */
1729 for (o = 0; o < crtc->crtc_info->noutput; o++)
1731 output = find_output_by_xid(crtc->crtc_info->outputs[o]);
1732 if (!output)
1733 fatal("cannot find output 0x%x\n", crtc->crtc_info->outputs[o]);
1734 if (output->changes)
1735 crtc->changing = True;
1737 /* walk new output list */
1738 for (o = 0; o < crtc->noutput; o++)
1740 output = crtc->outputs[o];
1741 if (output->changes)
1742 crtc->changing = True;
1748 * Test whether 'crtc' can be used for 'output'
1750 static Bool check_crtc_for_output(crtc_t* crtc, output_t* output)
1752 int c;
1753 int l;
1754 output_t* other;
1756 for (c = 0; c < output->output_info->ncrtc; c++)
1757 if (output->output_info->crtcs[c] == crtc->crtc.xid)
1758 break;
1759 if (c == output->output_info->ncrtc)
1760 return False;
1761 for (other = outputs; other; other = other->next)
1763 if (other == output)
1764 continue;
1766 if (other->mode_info == NULL)
1767 continue;
1769 if (other->crtc_info != crtc)
1770 continue;
1772 /* see if the output connected to the crtc can clone to this output */
1773 for (l = 0; l < output->output_info->nclone; l++)
1774 if (output->output_info->clones[l] == other->output.xid)
1775 break;
1776 /* not on the list, can't clone */
1777 if (l == output->output_info->nclone)
1778 return False;
1781 if (crtc->noutput)
1783 /* make sure the state matches */
1784 if (crtc->mode_info != output->mode_info)
1785 return False;
1786 if (crtc->x != output->x)
1787 return False;
1788 if (crtc->y != output->y)
1789 return False;
1790 if (crtc->rotation != output->rotation)
1791 return False;
1792 if (!equal_transform(&crtc->current_transform, &output->transform))
1793 return False;
1795 else if (crtc->crtc_info->noutput)
1797 /* make sure the state matches the already used state */
1798 XRRModeInfo* mode = find_mode_by_xid(crtc->crtc_info->mode);
1800 if (mode != output->mode_info)
1801 return False;
1802 if (crtc->crtc_info->x != output->x)
1803 return False;
1804 if (crtc->crtc_info->y != output->y)
1805 return False;
1806 if (crtc->crtc_info->rotation != output->rotation)
1807 return False;
1809 return True;
1812 static crtc_t* find_crtc_for_output(output_t* output)
1814 int c;
1816 for (c = 0; c < output->output_info->ncrtc; c++)
1818 crtc_t* crtc;
1820 crtc = find_crtc_by_xid(output->output_info->crtcs[c]);
1821 if (!crtc)
1822 fatal("cannot find crtc 0x%x\n", output->output_info->crtcs[c]);
1824 if (check_crtc_for_output(crtc, output))
1825 return crtc;
1827 return NULL;
1830 static void set_positions(void)
1832 output_t* output;
1833 Bool keep_going;
1834 Bool any_set;
1835 int min_x, min_y;
1837 for (;;)
1839 any_set = False;
1840 keep_going = False;
1841 for (output = outputs; output; output = output->next)
1843 output_t* relation;
1844 name_t relation_name;
1846 if (!(output->changes & changes_relation))
1847 continue;
1849 if (output->mode_info == NULL)
1850 continue;
1852 init_name(&relation_name);
1853 set_name_string(&relation_name, output->relative_to);
1854 relation = find_output(&relation_name);
1855 if (!relation)
1856 fatal("cannot find output \"%s\"\n", output->relative_to);
1858 if (relation->mode_info == NULL)
1860 output->x = 0;
1861 output->y = 0;
1862 output->changes |= changes_position;
1863 any_set = True;
1864 continue;
1867 * Make sure the dependent object has been set in place
1869 if ((relation->changes & changes_relation) && !(relation->changes & changes_position))
1871 keep_going = True;
1872 continue;
1875 switch (output->relation)
1877 case relation_left_of:
1878 output->y = relation->y;
1879 output->x = relation->x - mode_width(output->mode_info, output->rotation);
1880 break;
1881 case relation_right_of:
1882 output->y = relation->y;
1883 output->x = relation->x + mode_width(relation->mode_info, relation->rotation);
1884 break;
1885 case relation_above:
1886 output->x = relation->x;
1887 output->y = relation->y - mode_height(output->mode_info, output->rotation);
1888 break;
1889 case relation_below:
1890 output->x = relation->x;
1891 output->y = relation->y + mode_height(relation->mode_info, relation->rotation);
1892 break;
1893 case relation_same_as:
1894 output->x = relation->x;
1895 output->y = relation->y;
1897 output->changes |= changes_position;
1898 any_set = True;
1900 if (!keep_going)
1901 break;
1902 if (!any_set)
1903 fatal("loop in relative position specifications\n");
1907 * Now normalize positions so the upper left corner of all outputs is at 0,0
1909 min_x = 32768;
1910 min_y = 32768;
1911 for (output = outputs; output; output = output->next)
1913 if (output->mode_info == NULL)
1914 continue;
1916 if (output->x < min_x)
1917 min_x = output->x;
1918 if (output->y < min_y)
1919 min_y = output->y;
1921 if (min_x || min_y)
1923 /* move all outputs */
1924 for (output = outputs; output; output = output->next)
1926 if (output->mode_info == NULL)
1927 continue;
1929 output->x -= min_x;
1930 output->y -= min_y;
1931 output->changes |= changes_position;
1936 static void set_screen_size(void)
1938 output_t* output;
1939 Bool fb_specified = fb_width != 0 && fb_height != 0;
1941 for (output = outputs; output; output = output->next)
1943 XRRModeInfo* mode_info = output->mode_info;
1944 int x, y, w, h;
1945 box_t bounds;
1947 if (!mode_info)
1948 continue;
1950 mode_geometry(mode_info, output->rotation, &output->transform.transform, &bounds);
1951 x = output->x + bounds.x1;
1952 y = output->y + bounds.y1;
1953 w = bounds.x2 - bounds.x1;
1954 h = bounds.y2 - bounds.y1;
1955 /* make sure output fits in specified size */
1956 if (fb_specified)
1958 if (x + w > fb_width || y + h > fb_height)
1959 warning("specified screen %dx%d not large enough for output %s (%dx%d+%d+%d)\n", fb_width,
1960 fb_height, output->output.string, w, h, x, y);
1962 /* fit fb to output */
1963 else
1965 XRRPanning* pan;
1966 if (x + w > fb_width)
1967 fb_width = x + w;
1968 if (y + h > fb_height)
1969 fb_height = y + h;
1970 if (output->changes & changes_panning)
1971 pan = &output->panning;
1972 else
1973 pan = output->crtc_info ? output->crtc_info->panning_info : NULL;
1974 if (pan && pan->left + pan->width > (unsigned int)fb_width)
1975 fb_width = pan->left + pan->width;
1976 if (pan && pan->top + pan->height > (unsigned int)fb_height)
1977 fb_height = pan->top + pan->height;
1981 if (fb_width > maxWidth || fb_height > maxHeight)
1982 fatal("screen cannot be larger than %dx%d (desired size %dx%d)\n", maxWidth, maxHeight,
1983 fb_width, fb_height);
1984 if (fb_specified)
1986 if (fb_width < minWidth || fb_height < minHeight)
1987 fatal("screen must be at least %dx%d\n", minWidth, minHeight);
1989 else
1991 if (fb_width < minWidth)
1992 fb_width = minWidth;
1993 if (fb_height < minHeight)
1994 fb_height = minHeight;
1999 static void disable_outputs(output_t* outputs)
2001 while (outputs)
2003 outputs->crtc_info = NULL;
2004 outputs = outputs->next;
2009 * find the best mapping from output to crtc available
2011 static int pick_crtcs_score(output_t* outputs)
2013 output_t* output;
2014 int best_score;
2015 int my_score;
2016 int score;
2017 crtc_t* best_crtc;
2018 int c;
2020 if (!outputs)
2021 return 0;
2023 output = outputs;
2024 outputs = outputs->next;
2026 * Score with this output disabled
2028 output->crtc_info = NULL;
2029 best_score = pick_crtcs_score(outputs);
2030 if (output->mode_info == NULL)
2031 return best_score;
2033 best_crtc = NULL;
2035 * Now score with this output any valid crtc
2037 for (c = 0; c < output->output_info->ncrtc; c++)
2039 crtc_t* crtc;
2041 crtc = find_crtc_by_xid(output->output_info->crtcs[c]);
2042 if (!crtc)
2043 fatal("cannot find crtc 0x%x\n", output->output_info->crtcs[c]);
2045 /* reset crtc allocation for following outputs */
2046 disable_outputs(outputs);
2047 if (!check_crtc_for_output(crtc, output))
2048 continue;
2050 my_score = 1000;
2051 /* slight preference for existing connections */
2052 if (crtc == output->current_crtc_info)
2053 my_score++;
2055 output->crtc_info = crtc;
2056 score = my_score + pick_crtcs_score(outputs);
2057 if (score > best_score)
2059 best_crtc = crtc;
2060 best_score = score;
2063 if (output->crtc_info != best_crtc)
2064 output->crtc_info = best_crtc;
2066 * Reset other outputs based on this one using the best crtc
2068 (void)pick_crtcs_score(outputs);
2070 return best_score;
2074 * Pick crtcs for any changing outputs that don't have one
2076 static void pick_crtcs(void)
2078 output_t* output;
2081 * First try to match up newly enabled outputs with spare crtcs
2083 for (output = outputs; output; output = output->next)
2085 if (output->changes && output->mode_info)
2087 if (output->crtc_info)
2089 if (output->crtc_info->crtc_info->noutput > 0 &&
2090 (output->crtc_info->crtc_info->noutput > 1 ||
2091 output != find_output_by_xid(output->crtc_info->crtc_info->outputs[0])))
2092 break;
2094 else
2096 output->crtc_info = find_crtc_for_output(output);
2097 if (!output->crtc_info)
2098 break;
2103 * Everyone is happy
2105 if (!output)
2106 return;
2108 * When the simple way fails, see if there is a way
2109 * to swap crtcs around and make things work
2111 for (output = outputs; output; output = output->next)
2112 output->current_crtc_info = output->crtc_info;
2113 pick_crtcs_score(outputs);
2114 for (output = outputs; output; output = output->next)
2116 if (output->mode_info && !output->crtc_info)
2117 fatal("cannot find crtc for output %s\n", output->output.string);
2118 if (!output->changes && output->crtc_info != output->current_crtc_info)
2119 output->changes |= changes_crtc;
2123 static int check_strtol(char* s)
2125 char* endptr;
2126 int result = strtol(s, &endptr, 10);
2127 if (s == endptr)
2128 usage();
2129 return result;
2132 static double check_strtod(char* s)
2134 char* endptr;
2135 double result = strtod(s, &endptr);
2136 if (s == endptr)
2137 usage();
2138 return result;
2141 int main(int argc, char** argv)
2143 XRRScreenSize* sizes;
2144 XRRScreenConfiguration* sc;
2145 int nsize;
2146 int nrate;
2147 short* rates;
2148 Status status = RRSetConfigFailed;
2149 int rot = -1;
2150 int query = False;
2151 int action_requested = False;
2152 Rotation rotation, current_rotation, rotations;
2153 XRRScreenChangeNotifyEvent event;
2154 XRRScreenChangeNotifyEvent* sce;
2155 char* display_name = NULL;
2156 int i, j;
2157 SizeID current_size;
2158 short current_rate;
2159 double rate = -1;
2160 int size = -1;
2161 int dirind = 0;
2162 Bool setit = False;
2163 Bool version = False;
2164 int event_base, error_base;
2165 int reflection = 0;
2166 int width = 0, height = 0;
2167 Bool have_pixel_size = False;
2168 int ret = 0;
2169 output_t* output = NULL;
2170 Bool setit_1_2 = False;
2171 Bool query_1_2 = False;
2172 Bool modeit = False;
2173 Bool propit = False;
2174 Bool query_1 = False;
2175 int major, minor;
2176 Bool current = False;
2178 program_name = argv[0];
2179 for (i = 1; i < argc; i++)
2181 if (!strcmp("-display", argv[i]) || !strcmp("-d", argv[i]))
2183 if (++i >= argc)
2184 usage();
2185 display_name = argv[i];
2186 continue;
2188 if (!strcmp("-help", argv[i]))
2190 usage();
2191 action_requested = True;
2192 continue;
2194 if (!strcmp("--verbose", argv[i]))
2196 verbose = True;
2197 continue;
2199 if (!strcmp("--dryrun", argv[i]))
2201 dryrun = True;
2202 verbose = True;
2203 continue;
2205 if (!strcmp("--nograb", argv[i]))
2207 grab_server = False;
2208 continue;
2210 if (!strcmp("--current", argv[i]))
2212 current = True;
2213 continue;
2216 if (!strcmp("-s", argv[i]) || !strcmp("--size", argv[i]))
2218 if (++i >= argc)
2219 usage();
2220 if (sscanf(argv[i], "%dx%d", &width, &height) == 2)
2222 have_pixel_size = True;
2224 else
2226 size = check_strtol(argv[i]);
2227 if (size < 0)
2228 usage();
2230 setit = True;
2231 action_requested = True;
2232 continue;
2235 if (!strcmp("-r", argv[i]) || !strcmp("--rate", argv[i]) || !strcmp("--refresh", argv[i]))
2237 if (++i >= argc)
2238 usage();
2239 rate = check_strtod(argv[i]);
2240 setit = True;
2241 if (output)
2243 output->refresh = rate;
2244 output->changes |= changes_refresh;
2245 setit_1_2 = True;
2247 action_requested = True;
2248 continue;
2251 if (!strcmp("-v", argv[i]) || !strcmp("--version", argv[i]))
2253 version = True;
2254 action_requested = True;
2255 continue;
2258 if (!strcmp("-x", argv[i]))
2260 reflection |= RR_Reflect_X;
2261 setit = True;
2262 action_requested = True;
2263 continue;
2265 if (!strcmp("-y", argv[i]))
2267 reflection |= RR_Reflect_Y;
2268 setit = True;
2269 action_requested = True;
2270 continue;
2272 if (!strcmp("--screen", argv[i]))
2274 if (++i >= argc)
2275 usage();
2276 screen = check_strtol(argv[i]);
2277 if (screen < 0)
2278 usage();
2279 continue;
2281 if (!strcmp("-q", argv[i]) || !strcmp("--query", argv[i]))
2283 query = True;
2284 continue;
2286 if (!strcmp("-o", argv[i]) || !strcmp("--orientation", argv[i]))
2288 char* endptr;
2289 if (++i >= argc)
2290 usage();
2291 dirind = strtol(argv[i], &endptr, 10);
2292 if (argv[i] == endptr)
2294 for (dirind = 0; dirind < 4; dirind++)
2296 if (strcmp(direction[dirind], argv[i]) == 0)
2297 break;
2299 if ((dirind < 0) || (dirind > 3))
2300 usage();
2302 rot = dirind;
2303 setit = True;
2304 action_requested = True;
2305 continue;
2307 if (!strcmp("--prop", argv[i]) || !strcmp("--props", argv[i]) ||
2308 !strcmp("--madprops", argv[i]) || !strcmp("--properties", argv[i]))
2310 query_1_2 = True;
2311 properties = True;
2312 action_requested = True;
2313 continue;
2315 if (!strcmp("--output", argv[i]))
2317 if (++i >= argc)
2318 usage();
2320 output = find_output_by_name(argv[i]);
2321 if (!output)
2323 output = add_output();
2324 set_name(&output->output, argv[i], name_string | name_xid);
2327 setit_1_2 = True;
2328 action_requested = True;
2329 continue;
2331 if (!strcmp("--crtc", argv[i]))
2333 if (++i >= argc)
2334 usage();
2335 if (!output)
2336 usage();
2337 set_name(&output->crtc, argv[i], name_xid | name_index);
2338 output->changes |= changes_crtc;
2339 continue;
2341 if (!strcmp("--mode", argv[i]))
2343 if (++i >= argc)
2344 usage();
2345 if (!output)
2346 usage();
2347 set_name(&output->mode, argv[i], name_string | name_xid);
2348 output->changes |= changes_mode;
2349 continue;
2351 if (!strcmp("--preferred", argv[i]))
2353 if (!output)
2354 usage();
2355 set_name_preferred(&output->mode);
2356 output->changes |= changes_mode;
2357 continue;
2359 if (!strcmp("--pos", argv[i]))
2361 if (++i >= argc)
2362 usage();
2363 if (!output)
2364 usage();
2365 if (sscanf(argv[i], "%dx%d", &output->x, &output->y) != 2)
2366 usage();
2367 output->changes |= changes_position;
2368 continue;
2370 if (!strcmp("--rotation", argv[i]) || !strcmp("--rotate", argv[i]))
2372 if (++i >= argc)
2373 usage();
2374 if (!output)
2375 usage();
2376 for (dirind = 0; dirind < 4; dirind++)
2378 if (strcmp(direction[dirind], argv[i]) == 0)
2379 break;
2381 if (dirind == 4)
2382 usage();
2383 output->rotation &= ~0xf;
2384 output->rotation |= 1 << dirind;
2385 output->changes |= changes_rotation;
2386 continue;
2388 if (!strcmp("--reflect", argv[i]) || !strcmp("--reflection", argv[i]))
2390 if (++i >= argc)
2391 usage();
2392 if (!output)
2393 usage();
2394 for (dirind = 0; dirind < 4; dirind++)
2396 if (strcmp(reflections[dirind], argv[i]) == 0)
2397 break;
2399 if (dirind == 4)
2400 usage();
2401 output->rotation &= ~(RR_Reflect_X | RR_Reflect_Y);
2402 output->rotation |= dirind * RR_Reflect_X;
2403 output->changes |= changes_reflection;
2404 continue;
2406 if (!strcmp("--left-of", argv[i]))
2408 if (++i >= argc)
2409 usage();
2410 if (!output)
2411 usage();
2412 output->relation = relation_left_of;
2413 output->relative_to = argv[i];
2414 output->changes |= changes_relation;
2415 continue;
2417 if (!strcmp("--right-of", argv[i]))
2419 if (++i >= argc)
2420 usage();
2421 if (!output)
2422 usage();
2423 output->relation = relation_right_of;
2424 output->relative_to = argv[i];
2425 output->changes |= changes_relation;
2426 continue;
2428 if (!strcmp("--above", argv[i]))
2430 if (++i >= argc)
2431 usage();
2432 if (!output)
2433 usage();
2434 output->relation = relation_above;
2435 output->relative_to = argv[i];
2436 output->changes |= changes_relation;
2437 continue;
2439 if (!strcmp("--below", argv[i]))
2441 if (++i >= argc)
2442 usage();
2443 if (!output)
2444 usage();
2445 output->relation = relation_below;
2446 output->relative_to = argv[i];
2447 output->changes |= changes_relation;
2448 continue;
2450 if (!strcmp("--same-as", argv[i]))
2452 if (++i >= argc)
2453 usage();
2454 if (!output)
2455 usage();
2456 output->relation = relation_same_as;
2457 output->relative_to = argv[i];
2458 output->changes |= changes_relation;
2459 continue;
2461 if (!strcmp("--panning", argv[i]))
2463 XRRPanning* pan;
2464 if (++i >= argc)
2465 usage();
2466 if (!output)
2467 usage();
2468 pan = &output->panning;
2469 switch (sscanf(argv[i], "%dx%d+%d+%d/%dx%d+%d+%d/%d/%d/%d/%d", &pan->width, &pan->height,
2470 &pan->left, &pan->top, &pan->track_width, &pan->track_height, &pan->track_left,
2471 &pan->track_top, &pan->border_left, &pan->border_top, &pan->border_right,
2472 &pan->border_bottom))
2474 case 2:
2475 pan->left = pan->top = 0;
2476 /* fall through */
2477 __attribute__((fallthrough));
2478 case 4:
2479 pan->track_left = pan->track_top = pan->track_width = pan->track_height = 0;
2480 /* fall through */
2481 __attribute__((fallthrough));
2482 case 8:
2483 pan->border_left = pan->border_top = pan->border_right = pan->border_bottom = 0;
2484 /* fall through */
2485 __attribute__((fallthrough));
2486 case 12:
2487 break;
2488 default:
2489 usage();
2491 output->changes |= changes_panning;
2492 continue;
2494 if (!strcmp("--gamma", argv[i]))
2496 if (!output)
2497 usage();
2498 if (++i >= argc)
2499 usage();
2500 if (sscanf(argv[i], "%f:%f:%f", &output->gamma.red, &output->gamma.green,
2501 &output->gamma.blue) != 3)
2502 usage();
2503 output->changes |= changes_gamma;
2504 setit_1_2 = True;
2505 continue;
2507 if (!strcmp("--brightness", argv[i]))
2509 if (!output)
2510 usage();
2511 if (++i >= argc)
2512 usage();
2513 if (sscanf(argv[i], "%f", &output->brightness) != 1)
2514 usage();
2515 output->changes |= changes_gamma;
2516 setit_1_2 = True;
2517 continue;
2519 if (!strcmp("--primary", argv[i]))
2521 if (!output)
2522 usage();
2523 output->changes |= changes_primary;
2524 output->primary = True;
2525 setit_1_2 = True;
2526 continue;
2528 if (!strcmp("--noprimary", argv[i]))
2530 no_primary = True;
2531 setit_1_2 = True;
2532 continue;
2534 if (!strcmp("--set", argv[i]))
2536 output_prop_t* prop;
2537 if (!output)
2538 usage();
2539 prop = malloc(sizeof(output_prop_t));
2540 prop->next = output->props;
2541 output->props = prop;
2542 if (++i >= argc)
2543 usage();
2544 prop->name = argv[i];
2545 if (++i >= argc)
2546 usage();
2547 prop->value = argv[i];
2548 propit = True;
2549 output->changes |= changes_property;
2550 setit_1_2 = True;
2551 continue;
2553 if (!strcmp("--scale", argv[i]))
2555 double sx, sy;
2556 if (!output)
2557 usage();
2558 if (++i >= argc)
2559 usage();
2560 if (sscanf(argv[i], "%lfx%lf", &sx, &sy) != 2)
2561 usage();
2562 init_transform(&output->transform);
2563 output->transform.transform.matrix[0][0] = XDoubleToFixed(sx);
2564 output->transform.transform.matrix[1][1] = XDoubleToFixed(sy);
2565 output->transform.transform.matrix[2][2] = XDoubleToFixed(1.0);
2566 if (sx != 1 || sy != 1)
2567 output->transform.filter = "bilinear";
2568 else
2569 output->transform.filter = "nearest";
2570 output->transform.nparams = 0;
2571 output->transform.params = NULL;
2572 output->changes |= changes_transform;
2573 continue;
2575 if (!strcmp("--scale-from", argv[i]))
2577 int w, h;
2578 if (!output)
2579 usage();
2580 if (++i >= argc)
2581 usage();
2582 if (sscanf(argv[i], "%dx%d", &w, &h) != 2)
2583 usage();
2584 if (w <= 0 || h <= 0)
2585 usage();
2586 output->scale_from_w = w;
2587 output->scale_from_h = h;
2588 output->changes |= changes_transform;
2589 continue;
2591 if (!strcmp("--transform", argv[i]))
2593 double transform[3][3];
2594 int k, l;
2595 if (!output)
2596 usage();
2597 if (++i >= argc)
2598 usage();
2599 init_transform(&output->transform);
2600 if (strcmp(argv[i], "none") != 0)
2602 if (sscanf(argv[i], "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf", &transform[0][0],
2603 &transform[0][1], &transform[0][2], &transform[1][0], &transform[1][1],
2604 &transform[1][2], &transform[2][0], &transform[2][1], &transform[2][2]) != 9)
2605 usage();
2606 init_transform(&output->transform);
2607 for (k = 0; k < 3; k++)
2608 for (l = 0; l < 3; l++)
2610 output->transform.transform.matrix[k][l] = XDoubleToFixed(transform[k][l]);
2612 output->transform.filter = "bilinear";
2613 output->transform.nparams = 0;
2614 output->transform.params = NULL;
2616 output->changes |= changes_transform;
2617 continue;
2619 if (!strcmp("--off", argv[i]))
2621 if (!output)
2622 usage();
2623 set_name_xid(&output->mode, None);
2624 set_name_xid(&output->crtc, None);
2625 output->changes |= changes_mode;
2626 continue;
2628 if (!strcmp("--fb", argv[i]))
2630 if (++i >= argc)
2631 usage();
2632 if (sscanf(argv[i], "%dx%d", &fb_width, &fb_height) != 2)
2633 usage();
2634 setit_1_2 = True;
2635 action_requested = True;
2636 continue;
2638 if (!strcmp("--fbmm", argv[i]))
2640 if (++i >= argc)
2641 usage();
2642 if (sscanf(argv[i], "%dx%d", &fb_width_mm, &fb_height_mm) != 2)
2643 usage();
2644 setit_1_2 = True;
2645 action_requested = True;
2646 continue;
2648 if (!strcmp("--dpi", argv[i]))
2650 char* strtod_error;
2651 if (++i >= argc)
2652 usage();
2653 dpi = strtod(argv[i], &strtod_error);
2654 if (argv[i] == strtod_error)
2656 dpi = 0.0;
2657 dpi_output = argv[i];
2659 setit_1_2 = True;
2660 action_requested = True;
2661 continue;
2663 if (!strcmp("--auto", argv[i]))
2665 if (output)
2667 output->automatic = True;
2668 output->changes |= changes_automatic;
2670 else
2671 automatic = True;
2672 setit_1_2 = True;
2673 action_requested = True;
2674 continue;
2676 if (!strcmp("--q12", argv[i]))
2678 query_1_2 = True;
2679 continue;
2681 if (!strcmp("--q1", argv[i]))
2683 query_1 = True;
2684 continue;
2686 if (!strcmp("--newmode", argv[i]))
2688 umode_t* m = malloc(sizeof(umode_t));
2689 double clock;
2691 ++i;
2692 if (i + 9 >= argc)
2693 usage();
2694 m->mode.name = argv[i];
2695 m->mode.nameLength = strlen(argv[i]);
2696 i++;
2697 clock = check_strtod(argv[i++]);
2698 m->mode.dotClock = clock * 1e6;
2700 m->mode.width = check_strtol(argv[i++]);
2701 m->mode.hSyncStart = check_strtol(argv[i++]);
2702 m->mode.hSyncEnd = check_strtol(argv[i++]);
2703 m->mode.hTotal = check_strtol(argv[i++]);
2704 m->mode.height = check_strtol(argv[i++]);
2705 m->mode.vSyncStart = check_strtol(argv[i++]);
2706 m->mode.vSyncEnd = check_strtol(argv[i++]);
2707 m->mode.vTotal = check_strtol(argv[i++]);
2708 m->mode.modeFlags = 0;
2709 while (i < argc)
2711 int f;
2713 for (f = 0; mode_flags[f].string; f++)
2714 if (!strcasecmp(mode_flags[f].string, argv[i]))
2715 break;
2717 if (!mode_flags[f].string)
2718 break;
2719 m->mode.modeFlags |= mode_flags[f].flag;
2720 i++;
2722 m->next = umodes;
2723 m->action = umode_create;
2724 umodes = m;
2725 modeit = True;
2726 action_requested = True;
2727 continue;
2729 if (!strcmp("--rmmode", argv[i]))
2731 umode_t* m = malloc(sizeof(umode_t));
2733 if (++i >= argc)
2734 usage();
2735 set_name(&m->name, argv[i], name_string | name_xid);
2736 m->action = umode_destroy;
2737 m->next = umodes;
2738 umodes = m;
2739 modeit = True;
2740 action_requested = True;
2741 continue;
2743 if (!strcmp("--addmode", argv[i]))
2745 umode_t* m = malloc(sizeof(umode_t));
2747 if (++i >= argc)
2748 usage();
2749 set_name(&m->output, argv[i], name_string | name_xid);
2750 if (++i >= argc)
2751 usage();
2752 set_name(&m->name, argv[i], name_string | name_xid);
2753 m->action = umode_add;
2754 m->next = umodes;
2755 umodes = m;
2756 modeit = True;
2757 action_requested = True;
2758 continue;
2760 if (!strcmp("--delmode", argv[i]))
2762 umode_t* m = malloc(sizeof(umode_t));
2764 if (++i >= argc)
2765 usage();
2766 set_name(&m->output, argv[i], name_string | name_xid);
2767 if (++i >= argc)
2768 usage();
2769 set_name(&m->name, argv[i], name_string | name_xid);
2770 m->action = umode_delete;
2771 m->next = umodes;
2772 umodes = m;
2773 modeit = True;
2774 action_requested = True;
2775 continue;
2777 usage();
2779 if (!action_requested)
2780 query = True;
2781 if (verbose)
2783 query = True;
2784 if (setit && !setit_1_2)
2785 query_1 = True;
2789 if (version)
2790 printf("xrandr program version " VERSION "\n");
2793 dpy = XOpenDisplay(display_name);
2795 if (dpy == NULL)
2797 fprintf(stderr, "Can't open display %s\n", XDisplayName(display_name));
2798 exit(1);
2800 if (screen < 0)
2801 screen = DefaultScreen(dpy);
2802 if (screen >= ScreenCount(dpy))
2804 fprintf(stderr, "Invalid screen number %d (display has %d)\n", screen, ScreenCount(dpy));
2805 exit(1);
2808 root = RootWindow(dpy, screen);
2810 if (!XRRQueryExtension(dpy, &event_base, &error_base) || !XRRQueryVersion(dpy, &major, &minor))
2812 fprintf(stderr, "RandR extension missing\n");
2813 exit(1);
2815 if (major > 1 || (major == 1 && minor >= 2))
2816 has_1_2 = True;
2817 if (major > 1 || (major == 1 && minor >= 3))
2818 has_1_3 = True;
2820 if (has_1_2 && modeit)
2822 umode_t* m;
2824 get_screen(current);
2825 get_crtcs();
2826 get_outputs();
2828 for (m = umodes; m; m = m->next)
2830 XRRModeInfo* e;
2831 output_t* o;
2833 switch (m->action)
2835 case umode_create:
2836 XRRCreateMode(dpy, root, &m->mode);
2837 break;
2838 case umode_destroy:
2839 e = find_mode(&m->name, 0);
2840 if (!e)
2841 fatal("cannot find mode \"%s\"\n", m->name.string);
2842 XRRDestroyMode(dpy, e->id);
2843 break;
2844 case umode_add:
2845 o = find_output(&m->output);
2846 if (!o)
2847 fatal("cannot find output \"%s\"\n", m->output.string);
2848 e = find_mode(&m->name, 0);
2849 if (!e)
2850 fatal("cannot find mode \"%s\"\n", m->name.string);
2851 XRRAddOutputMode(dpy, o->output.xid, e->id);
2852 break;
2853 case umode_delete:
2854 o = find_output(&m->output);
2855 if (!o)
2856 fatal("cannot find output \"%s\"\n", m->output.string);
2857 e = find_mode(&m->name, 0);
2858 if (!e)
2859 fatal("cannot find mode \"%s\"\n", m->name.string);
2860 XRRDeleteOutputMode(dpy, o->output.xid, e->id);
2861 break;
2864 if (!setit_1_2)
2866 XSync(dpy, False);
2867 exit(0);
2870 if (has_1_2 && propit)
2873 get_screen(current);
2874 get_crtcs();
2875 get_outputs();
2877 for (output = outputs; output; output = output->next)
2879 output_prop_t* prop;
2881 for (prop = output->props; prop; prop = prop->next)
2883 Atom name = XInternAtom(dpy, prop->name, False);
2884 Atom type;
2885 int format = 0;
2886 unsigned char* data;
2887 int nelements;
2888 int int_value;
2889 unsigned long ulong_value;
2890 unsigned char* prop_data;
2891 int actual_format;
2892 unsigned long nitems, bytes_after;
2893 Atom actual_type;
2894 XRRPropertyInfo* propinfo;
2896 type = AnyPropertyType;
2898 if (XRRGetOutputProperty(dpy, output->output.xid, name, 0, 100, False, False,
2899 AnyPropertyType, &actual_type, &actual_format, &nitems,
2900 &bytes_after, &prop_data) == Success &&
2902 (propinfo = XRRQueryOutputProperty(dpy, output->output.xid, name)))
2904 type = actual_type;
2905 format = actual_format;
2908 if ((type == XA_INTEGER || type == AnyPropertyType) &&
2909 (sscanf(prop->value, "%d", &int_value) == 1 ||
2910 sscanf(prop->value, "0x%x", &int_value) == 1))
2912 type = XA_INTEGER;
2913 ulong_value = int_value;
2914 data = (unsigned char*)&ulong_value;
2915 nelements = 1;
2916 format = 32;
2918 else if (type == XA_ATOM)
2920 ulong_value = XInternAtom(dpy, prop->value, False);
2921 data = (unsigned char*)&ulong_value;
2922 nelements = 1;
2924 else if ((type == XA_STRING || type == AnyPropertyType))
2926 type = XA_STRING;
2927 data = (unsigned char*)prop->value;
2928 nelements = strlen(prop->value);
2929 format = 8;
2931 else
2932 continue;
2933 XRRChangeOutputProperty(dpy, output->output.xid, name, type, format, PropModeReplace, data,
2934 nelements);
2937 if (!setit_1_2)
2939 XSync(dpy, False);
2940 exit(0);
2943 if (setit_1_2)
2945 get_screen(current);
2946 get_crtcs();
2947 get_outputs();
2948 set_positions();
2949 set_screen_size();
2951 pick_crtcs();
2954 * Assign outputs to crtcs
2956 set_crtcs();
2959 * Mark changing crtcs
2961 mark_changing_crtcs();
2964 * If an output was specified to track dpi, use it
2966 if (dpi_output)
2968 output_t* output = find_output_by_name(dpi_output);
2969 XRROutputInfo* output_info;
2970 XRRModeInfo* mode_info;
2971 if (!output)
2972 fatal("Cannot find output %s\n", dpi_output);
2973 output_info = output->output_info;
2974 mode_info = output->mode_info;
2975 if (output_info && mode_info && output_info->mm_height)
2978 * When this output covers the whole screen, just use
2979 * the known physical size
2981 if ((unsigned int)fb_width == mode_info->width &&
2982 (unsigned int)fb_height == mode_info->height)
2984 fb_width_mm = output_info->mm_width;
2985 fb_height_mm = output_info->mm_height;
2987 else
2989 dpi = (25.4 * mode_info->height) / output_info->mm_height;
2995 * Compute physical screen size
2997 if (fb_width_mm == 0 || fb_height_mm == 0)
2999 if (fb_width != DisplayWidth(dpy, screen) || fb_height != DisplayHeight(dpy, screen) ||
3000 dpi != 0.0)
3002 if (dpi <= 0)
3003 dpi = (25.4 * DisplayHeight(dpy, screen)) / DisplayHeightMM(dpy, screen);
3005 fb_width_mm = (25.4 * fb_width) / dpi;
3006 fb_height_mm = (25.4 * fb_height) / dpi;
3008 else
3010 fb_width_mm = DisplayWidthMM(dpy, screen);
3011 fb_height_mm = DisplayHeightMM(dpy, screen);
3016 * Set panning
3018 set_panning();
3021 * Set gamma on crtc's that belong to the outputs.
3023 set_gamma();
3026 * Now apply all of the changes
3028 apply();
3030 XSync(dpy, False);
3031 exit(0);
3033 if (query_1_2 || (query && has_1_2 && !query_1))
3035 output_t* output;
3037 #define ModeShown 0x80000000
3039 get_screen(current);
3040 get_crtcs();
3041 get_outputs();
3043 printf("<screen id=\"%d\" minimum_w=\"%d\" minimum_h=\"%d\" current_w=\"%d\" current_h=\"%d\" "
3044 "maximum_w=\"%d\" maximum_h=\"%d\">\n",
3045 screen, minWidth, minHeight, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
3046 maxWidth, maxHeight);
3048 for (output = outputs; output; output = output->next)
3050 XRROutputInfo* output_info = output->output_info;
3051 crtc_t* crtc = output->crtc_info;
3052 XRRCrtcInfo* crtc_info = crtc ? crtc->crtc_info : NULL;
3053 XRRModeInfo* mode = output->mode_info;
3054 Atom* props;
3055 int j, k, nprop;
3056 Bool* mode_shown;
3057 // Rotation rotations = output_rotations (output);
3059 printf(" <output name=\"%s\" connected=\"%s\"", output_info->name,
3060 connection[output_info->connection]);
3061 if (mode)
3063 if (crtc_info)
3065 printf(" w=\"%d\" h=\"%d\" x=\"%d\" y=\"%d\" crtc=\"%d\"", crtc_info->width,
3066 crtc_info->height, crtc_info->x, crtc_info->y, crtc->crtc.index);
3068 else
3070 printf(" w=\"%d\" h=\"%d\" x=\"%d\" y=\"%d\"", mode->width, mode->height, output->x,
3071 output->y);
3073 if (verbose)
3074 printf(" id=\"%lx\"", mode->id);
3075 if (output->rotation != RR_Rotate_0 || verbose)
3077 printf(" rotation=\"%s\"", rotation_name(output->rotation));
3078 if (output->rotation & (RR_Reflect_X | RR_Reflect_Y))
3079 printf(" reflection=\"%s\"", reflection_name(output->rotation));
3083 if (rotations != RR_Rotate_0 || verbose)
3085 Bool first = True;
3086 printf (" (");
3087 for (i = 0; i < 4; i ++) {
3088 if ((rotations >> i) & 1) {
3089 if (!first) printf (" "); first = False;
3090 printf("%s", direction[i]);
3093 if (rotations & RR_Reflect_X)
3095 if (!first) printf (" "); first = False;
3096 printf ("x axis");
3098 if (rotations & RR_Reflect_Y)
3100 if (!first) printf (" ");
3101 printf ("y axis");
3103 printf (")");
3107 if (mode)
3109 printf(" wmm=\"%d\" hmm=\"%d\"", (int)output_info->mm_width, (int)output_info->mm_height);
3112 if (crtc && crtc->panning_info && crtc->panning_info->width > 0)
3114 XRRPanning* pan = crtc->panning_info;
3115 printf(" panning %dx%d+%d+%d", pan->width, pan->height, pan->left, pan->top);
3116 if ((pan->track_width != 0 &&
3117 (pan->track_left != pan->left || pan->track_width != pan->width ||
3118 pan->border_left != 0 || pan->border_right != 0)) ||
3119 (pan->track_height != 0 &&
3120 (pan->track_top != pan->top || pan->track_height != pan->height ||
3121 pan->border_top != 0 || pan->border_bottom != 0)))
3122 printf(" tracking %dx%d+%d+%d border %d/%d/%d/%d", pan->track_width, pan->track_height,
3123 pan->track_left, pan->track_top, pan->border_left, pan->border_top,
3124 pan->border_right, pan->border_bottom);
3126 printf(">\n");
3128 if (verbose)
3130 printf("\tIdentifier: 0x%x\n", (int)output->output.xid);
3131 printf("\tTimestamp: %d\n", (int)output_info->timestamp);
3132 printf("\tSubpixel: %s\n", order[output_info->subpixel_order]);
3133 if (output->gamma.red != 0.0f && output->gamma.green != 0.0f && output->gamma.blue != 0.0f)
3135 printf("\tGamma: %#.2g:%#.2g:%#.2g\n", (double)output->gamma.red,
3136 (double)output->gamma.green, (double)output->gamma.blue);
3137 printf("\tBrightness: %#.2g\n", (double)output->brightness);
3139 printf("\tClones: ");
3140 for (j = 0; j < output_info->nclone; j++)
3142 output_t* clone = find_output_by_xid(output_info->clones[j]);
3144 if (clone)
3145 printf(" %s", clone->output.string);
3147 printf("\n");
3148 if (output->crtc_info)
3149 printf("\tCRTC: %d\n", output->crtc_info->crtc.index);
3150 printf("\tCRTCs: ");
3151 for (j = 0; j < output_info->ncrtc; j++)
3153 crtc_t* crtc = find_crtc_by_xid(output_info->crtcs[j]);
3154 if (crtc)
3155 printf(" %d", crtc->crtc.index);
3157 printf("\n");
3158 if (output->crtc_info && output->crtc_info->panning_info)
3160 XRRPanning* pan = output->crtc_info->panning_info;
3161 printf("\tPanning: %dx%d+%d+%d\n", pan->width, pan->height, pan->left, pan->top);
3162 printf("\tTracking: %dx%d+%d+%d\n", pan->track_width, pan->track_height,
3163 pan->track_left, pan->track_top);
3164 printf("\tBorder: %d/%d/%d/%d\n", pan->border_left, pan->border_top,
3165 pan->border_right, pan->border_bottom);
3168 if (verbose)
3170 int x, y;
3172 printf("\tTransform: ");
3173 for (y = 0; y < 3; y++)
3175 for (x = 0; x < 3; x++)
3176 printf(" %f", XFixedToDouble(output->transform.transform.matrix[y][x]));
3177 if (y < 2)
3178 printf("\n\t ");
3180 if (output->transform.filter)
3181 printf("\n\t filter: %s", output->transform.filter);
3182 printf("\n");
3184 if (verbose || properties)
3186 props = XRRListOutputProperties(dpy, output->output.xid, &nprop);
3187 for (j = 0; j < nprop; j++)
3189 unsigned char* prop;
3190 int actual_format;
3191 unsigned long nitems, bytes_after;
3192 Atom actual_type;
3193 XRRPropertyInfo* propinfo;
3195 XRRGetOutputProperty(dpy, output->output.xid, props[j], 0, 100, False, False,
3196 AnyPropertyType, &actual_type, &actual_format, &nitems, &bytes_after,
3197 &prop);
3199 propinfo = XRRQueryOutputProperty(dpy, output->output.xid, props[j]);
3201 if (actual_type == XA_INTEGER && actual_format == 8)
3203 int k;
3205 printf("\t%s:\n", XGetAtomName(dpy, props[j]));
3206 for (k = 0; k < (long)nitems; k++)
3208 if (k % 16 == 0)
3209 printf("\t\t");
3210 printf("%02x", (unsigned char)prop[k]);
3211 if (k % 16 == 15)
3212 printf("\n");
3215 else if (actual_type == XA_INTEGER && actual_format == 32)
3217 printf("\t%s: ", XGetAtomName(dpy, props[j]));
3218 for (k = 0; k < (long)nitems; k++)
3220 if (k > 0)
3221 printf("\n\t\t\t");
3222 printf("%d (0x%08x)", (int)((INT32*)prop)[k], (int)((INT32*)prop)[k]);
3225 if (propinfo->range && propinfo->num_values > 0)
3227 if (nitems > 1)
3228 printf("\n\t\t");
3229 printf("\trange%s: ", (propinfo->num_values == 2) ? "" : "s");
3231 for (k = 0; k < propinfo->num_values / 2; k++)
3232 printf(" (%d,%d)", (int)propinfo->values[k * 2], (int)propinfo->values[k * 2 + 1]);
3235 printf("\n");
3237 else if (actual_type == XA_ATOM && actual_format == 32)
3239 printf("\t%s:", XGetAtomName(dpy, props[j]));
3240 for (k = 0; k < (long)nitems; k++)
3242 if (k > 0 && (k & 1) == 0)
3243 printf("\n\t\t");
3244 printf("\t%s", XGetAtomName(dpy, ((Atom*)prop)[k]));
3247 if (!propinfo->range && propinfo->num_values > 0)
3249 printf("\n\t\tsupported:");
3251 for (k = 0; k < propinfo->num_values; k++)
3253 printf(" %-12.12s", XGetAtomName(dpy, propinfo->values[k]));
3254 if (k % 4 == 3 && k < propinfo->num_values - 1)
3255 printf("\n\t\t ");
3258 printf("\n");
3260 else if (actual_format == 8)
3262 printf("\t%s: %s%s\n", XGetAtomName(dpy, props[j]), prop, bytes_after ? "..." : "");
3264 else
3266 char* type = actual_type ? XGetAtomName(dpy, actual_type) : "none";
3267 printf("\t%s: %s(%d) (format %d items %d) ????\n", XGetAtomName(dpy, props[j]), type,
3268 (int)actual_type, actual_format, (int)nitems);
3271 free(propinfo);
3275 if (verbose)
3277 for (j = 0; j < output_info->nmode; j++)
3279 XRRModeInfo* mode = find_mode_by_xid(output_info->modes[j]);
3280 int f;
3282 printf(" %s (0x%x) %6.1fMHz", mode->name, (int)mode->id,
3283 (double)mode->dotClock / 1000000.0);
3284 for (f = 0; mode_flags[f].flag; f++)
3285 if (mode->modeFlags & mode_flags[f].flag)
3286 printf(" %s", mode_flags[f].string);
3287 if (mode == output->mode_info)
3288 printf(" *current");
3289 if (j < output_info->npreferred)
3290 printf(" +preferred");
3291 printf("\n");
3292 printf(" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
3293 mode->width, mode->hSyncStart, mode->hSyncEnd, mode->hTotal, mode->hSkew,
3294 mode_hsync(mode) / 1000);
3295 printf(" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
3296 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal, mode_refresh(mode));
3297 mode->modeFlags |= ModeShown;
3300 else
3302 mode_shown = calloc(output_info->nmode, sizeof(Bool));
3303 if (!mode_shown)
3304 fatal("out of memory\n");
3305 for (j = 0; j < output_info->nmode; j++)
3307 XRRModeInfo *jmode, *kmode;
3309 if (mode_shown[j])
3310 continue;
3312 jmode = find_mode_by_xid(output_info->modes[j]);
3313 for (k = j; k < output_info->nmode; k++)
3315 if (mode_shown[k])
3316 continue;
3317 kmode = find_mode_by_xid(output_info->modes[k]);
3318 if (strcmp(jmode->name, kmode->name) != 0)
3319 continue;
3320 mode_shown[k] = True;
3321 kmode->modeFlags |= ModeShown;
3322 printf(" <mode id=\"0x%lx\" name=\"%s\" w=\"%d\" h=\"%d\" hz=\"%.5f\"", kmode->id,
3323 kmode->name, kmode->width, kmode->height, mode_refresh(kmode));
3324 if (kmode == output->mode_info)
3325 printf(" current=\"true\"");
3326 else
3327 printf(" current=\"false\"");
3328 if (k < output_info->npreferred)
3329 printf(" preferred=\"true\"");
3330 else
3331 printf(" preferred=\"false\"");
3332 printf("/>\n");
3335 free(mode_shown);
3337 printf(" </output>\n");
3341 for (m = 0; m < res->nmode; m++)
3343 XRRModeInfo *mode = &res->modes[m];
3345 if (!(mode->modeFlags & ModeShown))
3347 printf (" %s (0x%x) %6.1fMHz\n",
3348 mode->name, (int)mode->id,
3349 (double)mode->dotClock / 1000000.0);
3350 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
3351 mode->width, mode->hSyncStart, mode->hSyncEnd,
3352 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
3353 printf (" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
3354 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
3355 mode_refresh (mode));
3359 printf("</screen>\n");
3360 exit(0);
3363 sc = XRRGetScreenInfo(dpy, root);
3365 if (sc == NULL)
3366 exit(1);
3368 current_size = XRRConfigCurrentConfiguration(sc, &current_rotation);
3370 sizes = XRRConfigSizes(sc, &nsize);
3372 if (have_pixel_size)
3374 for (size = 0; size < nsize; size++)
3376 if (sizes[size].width == width && sizes[size].height == height)
3377 break;
3379 if (size >= nsize)
3381 fprintf(stderr, "Size %dx%d not found in available modes\n", width, height);
3382 exit(1);
3385 else if (size < 0)
3386 size = current_size;
3387 else if (size >= nsize)
3389 fprintf(stderr, "Size index %d is too large, there are only %d sizes\n", size, nsize);
3390 exit(1);
3393 if (rot < 0)
3395 for (rot = 0; rot < 4; rot++)
3396 if (1 << rot == (current_rotation & 0xf))
3397 break;
3400 current_rate = XRRConfigCurrentRate(sc);
3402 if (rate < 0)
3404 if (size == current_size)
3405 rate = current_rate;
3406 else
3407 rate = 0;
3409 else
3411 rates = XRRConfigRates(sc, size, &nrate);
3412 for (i = 0; i < nrate; i++)
3413 if (rate == rates[i])
3414 break;
3415 if (i == nrate)
3417 fprintf(stderr, "Rate %.1f Hz not available for this size\n", rate);
3418 exit(1);
3422 if (version)
3424 int major_version, minor_version;
3425 XRRQueryVersion(dpy, &major_version, &minor_version);
3426 printf("Server reports RandR version %d.%d\n", major_version, minor_version);
3429 if (query || query_1)
3431 printf(" SZ: Pixels Physical Refresh\n");
3432 for (i = 0; i < nsize; i++)
3434 printf("%c%-2d %5d x %-5d (%4dmm x%4dmm )", i == current_size ? '*' : ' ', i, sizes[i].width,
3435 sizes[i].height, sizes[i].mwidth, sizes[i].mheight);
3436 rates = XRRConfigRates(sc, i, &nrate);
3437 if (nrate)
3438 printf(" ");
3439 for (j = 0; j < nrate; j++)
3440 printf("%c%-4d", i == current_size && rates[j] == current_rate ? '*' : ' ', rates[j]);
3441 printf("\n");
3445 rotations = XRRConfigRotations(sc, &current_rotation);
3447 rotation = 1 << rot;
3448 if (query)
3450 printf("Current rotation - %s\n", rotation_name(current_rotation));
3452 printf("Current reflection - %s\n", reflection_name(current_rotation));
3454 printf("Rotations possible - ");
3455 for (i = 0; i < 4; i++)
3457 if ((rotations >> i) & 1)
3458 printf("%s ", direction[i]);
3460 printf("\n");
3462 printf("Reflections possible - ");
3463 if (rotations & (RR_Reflect_X | RR_Reflect_Y))
3465 if (rotations & RR_Reflect_X)
3466 printf("X Axis ");
3467 if (rotations & RR_Reflect_Y)
3468 printf("Y Axis");
3470 else
3471 printf("none");
3472 printf("\n");
3475 if (verbose)
3477 printf("Setting size to %d, rotation to %s\n", size, direction[rot]);
3479 printf("Setting reflection on ");
3480 if (reflection)
3482 if (reflection & RR_Reflect_X)
3483 printf("X Axis ");
3484 if (reflection & RR_Reflect_Y)
3485 printf("Y Axis");
3487 else
3488 printf("neither axis");
3489 printf("\n");
3491 if (reflection & RR_Reflect_X)
3492 printf("Setting reflection on X axis\n");
3494 if (reflection & RR_Reflect_Y)
3495 printf("Setting reflection on Y axis\n");
3498 /* we should test configureNotify on the root window */
3499 XSelectInput(dpy, root, StructureNotifyMask);
3501 if (setit && !dryrun)
3502 XRRSelectInput(dpy, root, RRScreenChangeNotifyMask);
3503 if (setit && !dryrun)
3504 status = XRRSetScreenConfigAndRate(dpy, sc, root, (SizeID)size,
3505 (Rotation)(rotation | reflection), rate, CurrentTime);
3507 if (setit && !dryrun && status == RRSetConfigFailed)
3509 printf("Failed to change the screen configuration!\n");
3510 ret = 1;
3513 if (verbose && setit && !dryrun && size != current_size)
3515 if (status == RRSetConfigSuccess)
3517 Bool seen_screen = False;
3518 while (!seen_screen)
3520 int spo;
3521 XNextEvent(dpy, (XEvent*)&event);
3523 printf("Event received, type = %d\n", event.type);
3524 /* update Xlib's knowledge of the event */
3525 XRRUpdateConfiguration((XEvent*)&event);
3526 if (event.type == ConfigureNotify)
3527 printf("Received ConfigureNotify Event!\n");
3529 switch (event.type - event_base)
3531 case RRScreenChangeNotify:
3532 sce = (XRRScreenChangeNotifyEvent*)&event;
3534 printf("Got a screen change notify event!\n");
3535 printf(" window = %d\n root = %d\n size_index = %d\n rotation %d\n", (int)sce->window,
3536 (int)sce->root, sce->size_index, sce->rotation);
3537 printf(" timestamp = %ld, config_timestamp = %ld\n", sce->timestamp,
3538 sce->config_timestamp);
3539 printf(" Rotation = %x\n", sce->rotation);
3540 printf(" %d X %d pixels, %d X %d mm\n", sce->width, sce->height, sce->mwidth,
3541 sce->mheight);
3542 printf("Display width %d, height %d\n", DisplayWidth(dpy, screen),
3543 DisplayHeight(dpy, screen));
3544 printf("Display widthmm %d, heightmm %d\n", DisplayWidthMM(dpy, screen),
3545 DisplayHeightMM(dpy, screen));
3546 spo = sce->subpixel_order;
3547 if ((spo < 0) || (spo > 5))
3548 printf("Unknown subpixel order, value = %d\n", spo);
3549 else
3550 printf("new Subpixel rendering model is %s\n", order[spo]);
3551 seen_screen = True;
3552 break;
3553 default:
3554 if (event.type != ConfigureNotify)
3555 printf("unknown event received, type = %d!\n", event.type);
3560 XRRFreeScreenConfigInfo(sc);
3561 return (ret);