2 * Copyright © 2001 Keith Packard, member of The XFree86 Project, Inc.
3 * Copyright © 2002 Hewlett Packard Company, Inc.
4 * Copyright © 2006 Intel Corporation
5 * Copyright © 2013 NVIDIA Corporation
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that copyright
10 * notice and this permission notice appear in supporting documentation, and
11 * that the name of the copyright holders not be used in advertising or
12 * publicity pertaining to distribution of the software without specific,
13 * written prior permission. The copyright holders make no representations
14 * about the suitability of this software for any purpose. It is provided "as
15 * is" without express or implied warranty.
17 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
25 * Thanks to Jim Gettys who wrote most of the client side code,
26 * and part of the server code for randr.
31 #include <X11/Xlibint.h>
32 #include <X11/Xproto.h>
33 #include <X11/Xatom.h>
34 #include <X11/extensions/Xrandr.h>
35 #include <X11/extensions/Xrender.h> /* we share subpixel information */
48 static char *program_name;
51 static int screen = -1;
52 static Bool verbose = False;
53 static Bool automatic = False;
54 static Bool properties = False;
55 static Bool grab_server = True;
56 static Bool no_primary = False;
58 static const char *direction[5] = {
65 static const char *reflections[5] = {
73 static const char *order[6] = {
85 { "+HSync", RR_HSyncPositive },
86 { "-HSync", RR_HSyncNegative },
87 { "+VSync", RR_VSyncPositive },
88 { "-VSync", RR_VSyncNegative },
89 { "Interlace", RR_Interlace },
90 { "DoubleScan", RR_DoubleScan },
91 { "CSync", RR_CSync },
92 { "+CSync", RR_CSyncPositive },
93 { "-CSync", RR_CSyncNegative },
100 printf("usage: %s [options]\n", program_name);
101 printf(" where options are:\n");
102 printf(" -display <display> or -d <display>\n");
104 printf(" -o <normal,inverted,left,right,0,1,2,3>\n");
105 printf(" or --orientation <normal,inverted,left,right,0,1,2,3>\n");
106 printf(" -q or --query\n");
107 printf(" -s <size>/<width>x<height> or --size <size>/<width>x<height>\n");
108 printf(" -r <rate> or --rate <rate> or --refresh <rate>\n");
109 printf(" -v or --version\n");
110 printf(" -x (reflect in x)\n");
111 printf(" -y (reflect in y)\n");
112 printf(" --screen <screen>\n");
113 printf(" --verbose\n");
114 printf(" --current\n");
115 printf(" --dryrun\n");
116 printf(" --nograb\n");
117 printf(" --prop or --properties\n");
118 printf(" --fb <width>x<height>\n");
119 printf(" --fbmm <width>x<height>\n");
120 printf(" --dpi <dpi>/<output>\n");
121 printf(" --output <output>\n");
123 printf(" --mode <mode>\n");
124 printf(" --preferred\n");
125 printf(" --pos <x>x<y>\n");
126 printf(" --rate <rate> or --refresh <rate>\n");
127 printf(" --reflect normal,x,y,xy\n");
128 printf(" --rotate normal,inverted,left,right\n");
129 printf(" --left-of <output>\n");
130 printf(" --right-of <output>\n");
131 printf(" --above <output>\n");
132 printf(" --below <output>\n");
133 printf(" --same-as <output>\n");
134 printf(" --set <property> <value>\n");
135 printf(" --scale <x>x<y>\n");
136 printf(" --scale-from <w>x<h>\n");
137 printf(" --transform <a>,<b>,<c>,<d>,<e>,<f>,<g>,<h>,<i>\n");
139 printf(" --crtc <crtc>\n");
140 printf(" --panning <w>x<h>[+<x>+<y>[/<track:w>x<h>+<x>+<y>[/<border:l>/<t>/<r>/<b>]]]\n");
141 printf(" --gamma <r>:<g>:<b>\n");
142 printf(" --primary\n");
143 printf(" --noprimary\n");
144 printf(" --newmode <name> <clock MHz>\n");
145 printf(" <hdisp> <hsync-start> <hsync-end> <htotal>\n");
146 printf(" <vdisp> <vsync-start> <vsync-end> <vtotal>\n");
147 printf(" [flags...]\n");
148 printf(" Valid flags: +HSync -HSync +VSync -VSync\n");
149 printf(" +CSync -CSync CSync Interlace DoubleScan\n");
150 printf(" --rmmode <name>\n");
151 printf(" --addmode <output> <name>\n");
152 printf(" --delmode <output> <name>\n");
153 printf(" --listproviders\n");
154 printf(" --setprovideroutputsource <prov-xid> <source-xid>\n");
155 printf(" --setprovideroffloadsink <prov-xid> <sink-xid>\n");
158 static void _X_NORETURN _X_ATTRIBUTE_PRINTF(1,2)
159 fatal (const char *format, ...)
163 va_start (ap, format);
164 fprintf (stderr, "%s: ", program_name);
165 vfprintf (stderr, format, ap);
171 static void _X_ATTRIBUTE_PRINTF(1,2)
172 warning (const char *format, ...)
176 va_start (ap, format);
177 fprintf (stderr, "%s: ", program_name);
178 vfprintf (stderr, format, ap);
182 static void _X_NORETURN _X_ATTRIBUTE_PRINTF(1,2)
183 argerr (const char *format, ...)
187 va_start (ap, format);
188 fprintf (stderr, "%s: ", program_name);
189 vfprintf (stderr, format, ap);
190 fprintf (stderr, "Try '%s --help' for more information.\n", program_name);
196 /* Because fmin requires C99 suppport */
197 static inline double dmin (double x, double y)
199 return x < y ? x : y;
203 rotation_name (Rotation rotation)
207 if ((rotation & 0xf) == 0)
209 for (i = 0; i < 4; i++)
210 if (rotation & (1 << i))
212 return "invalid rotation";
216 reflection_name (Rotation rotation)
218 rotation &= (RR_Reflect_X|RR_Reflect_Y);
226 case RR_Reflect_X|RR_Reflect_Y:
227 return "X and Y axis";
229 return "invalid reflection";
233 capability_name (int cap_bit)
236 case RR_Capability_SourceOutput:
237 return "Source Output";
238 case RR_Capability_SinkOutput:
239 return "Sink Output";
240 case RR_Capability_SourceOffload:
241 return "Source Offload";
242 case RR_Capability_SinkOffload:
243 return "Sink Offload";
245 return "invalid capability";
248 typedef enum _relation {
257 int x, y, width, height;
268 typedef enum _changes {
270 changes_crtc = (1 << 0),
271 changes_mode = (1 << 1),
272 changes_relation = (1 << 2),
273 changes_position = (1 << 3),
274 changes_rotation = (1 << 4),
275 changes_reflection = (1 << 5),
276 changes_automatic = (1 << 6),
277 changes_refresh = (1 << 7),
278 changes_property = (1 << 8),
279 changes_transform = (1 << 9),
280 changes_panning = (1 << 10),
281 changes_gamma = (1 << 11),
282 changes_primary = (1 << 12),
285 typedef enum _name_kind {
287 name_string = (1 << 0),
289 name_index = (1 << 2),
290 name_preferred = (1 << 3),
300 typedef struct _crtc crtc_t;
301 typedef struct _output output_t;
302 typedef struct _transform transform_t;
303 typedef struct _umode umode_t;
304 typedef struct _output_prop output_prop_t;
305 typedef struct _provider provider_t;
308 XTransform transform;
317 XRRCrtcInfo *crtc_info;
319 XRRModeInfo *mode_info;
320 XRRPanning *panning_info;
326 transform_t current_transform, pending_transform;
329 struct _output_prop {
330 struct _output_prop *next;
336 struct _output *next;
340 output_prop_t *props;
343 XRROutputInfo *output_info;
347 crtc_t *current_crtc_info;
351 XRRModeInfo *mode_info;
364 int scale_from_w, scale_from_h;
365 transform_t transform;
380 typedef enum _umode_action {
381 umode_create, umode_destroy, umode_add, umode_delete
388 umode_action_t action;
396 XRRProviderInfo *info;
399 static const char *connection[3] = {
402 "unknown connection"};
404 #define OUTPUT_NAME 1
408 #define CRTC_INDEX 0x40000000
417 static output_t *all_outputs = NULL;
418 static output_t **all_outputs_tail = &all_outputs;
419 static crtc_t *crtcs;
420 static provider_t *providers;
421 static umode_t *umodes;
422 static int num_crtcs, num_providers;
423 static XRRScreenResources *res;
424 static int fb_width = 0, fb_height = 0;
425 static int fb_width_mm = 0, fb_height_mm = 0;
426 static double dpi = 0;
427 static char *dpi_output_name = NULL;
428 static Bool dryrun = False;
429 static int minWidth, maxWidth, minHeight, maxHeight;
430 static Bool has_1_2 = False;
431 static Bool has_1_3 = False;
432 static Bool has_1_4 = False;
433 static name_t provider_name, output_source_provider_name, offload_sink_provider_name;
436 mode_height (XRRModeInfo *mode_info, Rotation rotation)
438 switch (rotation & 0xf) {
441 return mode_info->height;
444 return mode_info->width;
451 mode_width (XRRModeInfo *mode_info, Rotation rotation)
453 switch (rotation & 0xf) {
456 return mode_info->width;
459 return mode_info->height;
466 transform_point (XTransform *transform, double *xp, double *yp)
476 for (j = 0; j < 3; j++)
479 for (i = 0; i < 3; i++)
480 v += (XFixedToDouble (transform->matrix[j][i]) * vector[i]);
485 for (j = 0; j < 2; j++) {
486 vector[j] = result[j] / result[2];
487 if (vector[j] > 32767 || vector[j] < -32767)
496 path_bounds (XTransform *transform, point_t *points, int npoints, box_t *box)
501 for (i = 0; i < npoints; i++) {
505 transform_point (transform, &x, &y);
506 point.x1 = floor (x);
507 point.y1 = floor (y);
513 if (point.x1 < box->x1) box->x1 = point.x1;
514 if (point.y1 < box->y1) box->y1 = point.y1;
515 if (point.x2 > box->x2) box->x2 = point.x2;
516 if (point.y2 > box->y2) box->y2 = point.y2;
522 mode_geometry (XRRModeInfo *mode_info, Rotation rotation,
523 XTransform *transform,
527 int width = mode_width (mode_info, rotation);
528 int height = mode_height (mode_info, rotation);
538 path_bounds (transform, rect, 4, bounds);
541 /* v refresh frequency in Hz */
543 mode_refresh (XRRModeInfo *mode_info)
546 unsigned int vTotal = mode_info->vTotal;
548 if (mode_info->modeFlags & RR_DoubleScan) {
549 /* doublescan doubles the number of lines */
553 if (mode_info->modeFlags & RR_Interlace) {
554 /* interlace splits the frame into two fields */
555 /* the field rate is what is typically reported by monitors */
559 if (mode_info->hTotal && vTotal)
560 rate = ((double) mode_info->dotClock /
561 ((double) mode_info->hTotal * (double) vTotal));
567 /* h sync frequency in Hz */
569 mode_hsync (XRRModeInfo *mode_info)
573 if (mode_info->hTotal)
574 rate = (double) mode_info->dotClock / (double) mode_info->hTotal;
581 init_name (name_t *name)
583 name->kind = name_none;
587 set_name_string (name_t *name, char *string)
589 name->kind |= name_string;
590 name->string = string;
594 set_name_xid (name_t *name, XID xid)
596 name->kind |= name_xid;
601 set_name_index (name_t *name, int idx)
603 name->kind |= name_index;
608 set_name_preferred (name_t *name)
610 name->kind |= name_preferred;
614 set_name_all (name_t *name, name_t *old)
616 if (old->kind & name_xid)
617 name->xid = old->xid;
618 if (old->kind & name_string)
619 name->string = old->string;
620 if (old->kind & name_index)
621 name->index = old->index;
622 name->kind |= old->kind;
626 set_name (name_t *name, char *string, name_kind_t valid)
628 unsigned int xid; /* don't make it XID (which is unsigned long):
629 scanf() takes unsigned int */
632 if ((valid & name_xid) && sscanf (string, "0x%x", &xid) == 1)
633 set_name_xid (name, xid);
634 else if ((valid & name_index) && sscanf (string, "%d", &idx) == 1)
635 set_name_index (name, idx);
636 else if (valid & name_string)
637 set_name_string (name, string);
639 argerr ("invalid name '%s'\n", string);
643 print_name (const name_t *name)
645 name_kind_t kind = name->kind;
647 if ((kind & name_xid)) return printf("XID 0x%x", (unsigned int)name->xid);
648 else if ((kind & name_string)) return printf("name %s", name->string);
649 else if ((kind & name_index)) return printf("index %d", name->index);
650 else return printf("unknown name");
654 init_transform (transform_t *transform)
657 memset (&transform->transform, '\0', sizeof (transform->transform));
658 for (x = 0; x < 3; x++)
659 transform->transform.matrix[x][x] = XDoubleToFixed (1.0);
660 transform->filter = "";
661 transform->nparams = 0;
662 transform->params = NULL;
666 set_transform (transform_t *dest,
667 XTransform *transform,
672 dest->transform = *transform;
673 /* note: this string is leaked */
674 dest->filter = strdup (filter);
675 dest->nparams = nparams;
676 dest->params = malloc (nparams * sizeof (XFixed));
677 memcpy (dest->params, params, nparams * sizeof (XFixed));
681 copy_transform (transform_t *dest, transform_t *src)
683 set_transform (dest, &src->transform,
684 src->filter, src->params, src->nparams);
688 equal_transform (transform_t *a, transform_t *b)
690 if (memcmp (&a->transform, &b->transform, sizeof (XTransform)) != 0)
692 if (strcmp (a->filter, b->filter) != 0)
694 if (a->nparams != b->nparams)
696 if (memcmp (a->params, b->params, a->nparams * sizeof (XFixed)) != 0)
704 output_t *output = calloc (1, sizeof (output_t));
707 fatal ("out of memory\n");
709 output->found = False;
710 output->brightness = 1.0;
711 *all_outputs_tail = output;
712 all_outputs_tail = &output->next;
717 find_output (name_t *name)
721 for (output = all_outputs; output; output = output->next)
723 name_kind_t common = name->kind & output->output.kind;
725 if ((common & name_xid) && name->xid == output->output.xid)
727 if ((common & name_string) && !strcmp (name->string, output->output.string))
729 if ((common & name_index) && name->index == output->output.index)
736 find_output_by_xid (RROutput output)
740 init_name (&output_name);
741 set_name_xid (&output_name, output);
742 return find_output (&output_name);
746 find_output_by_name (char *name)
750 init_name (&output_name);
751 set_name_string (&output_name, name);
752 return find_output (&output_name);
756 find_crtc (name_t *name)
761 for (c = 0; c < num_crtcs; c++)
766 common = name->kind & crtc->crtc.kind;
768 if ((common & name_xid) && name->xid == crtc->crtc.xid)
770 if ((common & name_string) && !strcmp (name->string, crtc->crtc.string))
772 if ((common & name_index) && name->index == crtc->crtc.index)
780 find_crtc_by_xid (RRCrtc crtc)
784 init_name (&crtc_name);
785 set_name_xid (&crtc_name, crtc);
786 return find_crtc (&crtc_name);
790 find_mode (name_t *name, double refresh)
793 XRRModeInfo *best = NULL;
796 for (m = 0; m < res->nmode; m++)
798 XRRModeInfo *mode = &res->modes[m];
799 if ((name->kind & name_xid) && name->xid == mode->id)
804 if ((name->kind & name_string) && !strcmp (name->string, mode->name))
809 dist = fabs (mode_refresh (mode) - refresh);
812 if (!best || dist < bestDist)
823 find_mode_by_xid (RRMode mode)
827 init_name (&mode_name);
828 set_name_xid (&mode_name, mode);
829 return find_mode (&mode_name, 0);
834 find_mode_by_name (char *name)
837 init_name (&mode_name);
838 set_name_string (&mode_name, name);
839 return find_mode (&mode_name, 0);
845 find_mode_for_output (output_t *output, name_t *name)
847 XRROutputInfo *output_info = output->output_info;
849 XRRModeInfo *best = NULL;
852 for (m = 0; m < output_info->nmode; m++)
856 mode = find_mode_by_xid (output_info->modes[m]);
858 if ((name->kind & name_xid) && name->xid == mode->id)
863 if ((name->kind & name_string) && !strcmp (name->string, mode->name))
867 /* Stay away from doublescan modes unless refresh rate is specified. */
868 if (!output->refresh && (mode->modeFlags & RR_DoubleScan))
872 dist = fabs (mode_refresh (mode) - output->refresh);
875 if (!best || dist < bestDist)
886 preferred_mode (output_t *output)
888 XRROutputInfo *output_info = output->output_info;
895 for (m = 0; m < output_info->nmode; m++)
897 XRRModeInfo *mode_info = find_mode_by_xid (output_info->modes[m]);
900 if (m < output_info->npreferred)
902 else if (output_info->mm_height)
903 dist = (1000 * DisplayHeight(dpy, screen) / DisplayHeightMM(dpy, screen) -
904 1000 * mode_info->height / output_info->mm_height);
906 dist = DisplayHeight(dpy, screen) - mode_info->height;
908 if (dist < 0) dist = -dist;
909 if (!best || dist < bestDist)
919 output_can_use_crtc (output_t *output, crtc_t *crtc)
921 XRROutputInfo *output_info = output->output_info;
924 for (c = 0; c < output_info->ncrtc; c++)
925 if (output_info->crtcs[c] == crtc->crtc.xid)
931 output_can_use_mode (output_t *output, XRRModeInfo *mode)
933 XRROutputInfo *output_info = output->output_info;
936 for (m = 0; m < output_info->nmode; m++)
937 if (output_info->modes[m] == mode->id)
943 crtc_can_use_rotation (crtc_t *crtc, Rotation rotation)
945 Rotation rotations = crtc->crtc_info->rotations;
946 Rotation dir = rotation & (RR_Rotate_0|RR_Rotate_90|RR_Rotate_180|RR_Rotate_270);
947 Rotation reflect = rotation & (RR_Reflect_X|RR_Reflect_Y);
948 if (((rotations & dir) != 0) && ((rotations & reflect) == reflect))
955 crtc_can_use_transform (crtc_t *crtc, XTransform *transform)
959 XRRQueryVersion (dpy, &major, &minor);
960 if (major > 1 || (major == 1 && minor >= 3))
967 * Report only rotations that are supported by all crtcs
970 output_rotations (output_t *output)
973 Rotation rotation = RR_Rotate_0;
974 XRROutputInfo *output_info = output->output_info;
977 for (c = 0; c < output_info->ncrtc; c++)
979 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
983 rotation = crtc->crtc_info->rotations;
986 rotation &= crtc->crtc_info->rotations;
993 output_can_use_rotation (output_t *output, Rotation rotation)
995 XRROutputInfo *output_info = output->output_info;
998 /* make sure all of the crtcs can use this rotation.
999 * yes, this is not strictly necessary, but it is
1000 * simpler,and we expect most drivers to either
1001 * support rotation everywhere or nowhere
1003 for (c = 0; c < output_info->ncrtc; c++)
1005 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
1006 if (crtc && !crtc_can_use_rotation (crtc, rotation))
1013 output_is_primary(output_t *output)
1016 return XRRGetOutputPrimary(dpy, root) == output->output.xid;
1020 /* Returns the index of the last value in an array < 0xffff */
1022 find_last_non_clamped(CARD16 array[], int size) {
1024 for (i = size - 1; i > 0; i--) {
1025 if (array[i] < 0xffff)
1032 set_gamma_info(output_t *output)
1034 XRRCrtcGamma *crtc_gamma;
1035 double i1, v1, i2, v2;
1036 int size, middle, last_best, last_red, last_green, last_blue;
1039 if (!output->crtc_info)
1042 size = XRRGetCrtcGammaSize(dpy, output->crtc_info->crtc.xid);
1044 warning("Failed to get size of gamma for output %s\n", output->output.string);
1048 crtc_gamma = XRRGetCrtcGamma(dpy, output->crtc_info->crtc.xid);
1050 warning("Failed to get gamma for output %s\n", output->output.string);
1055 * Here is a bit tricky because gamma is a whole curve for each
1056 * color. So, typically, we need to represent 3 * 256 values as 3 + 1
1057 * values. Therefore, we approximate the gamma curve (v) by supposing
1058 * it always follows the way we set it: a power function (i^g)
1059 * multiplied by a brightness (b).
1061 * so g = (ln(v) - ln(b))/ln(i)
1062 * and b can be found using two points (v1,i1) and (v2, i2):
1063 * b = e^((ln(v2)*ln(i1) - ln(v1)*ln(i2))/ln(i1/i2))
1064 * For the best resolution, we select i2 at the highest place not
1065 * clamped and i1 at i2/2. Note that if i2 = 1 (as in most normal
1066 * cases), then b = v2.
1068 last_red = find_last_non_clamped(crtc_gamma->red, size);
1069 last_green = find_last_non_clamped(crtc_gamma->green, size);
1070 last_blue = find_last_non_clamped(crtc_gamma->blue, size);
1071 best_array = crtc_gamma->red;
1072 last_best = last_red;
1073 if (last_green > last_best) {
1074 last_best = last_green;
1075 best_array = crtc_gamma->green;
1077 if (last_blue > last_best) {
1078 last_best = last_blue;
1079 best_array = crtc_gamma->blue;
1084 middle = last_best / 2;
1085 i1 = (double)(middle + 1) / size;
1086 v1 = (double)(best_array[middle]) / 65535;
1087 i2 = (double)(last_best + 1) / size;
1088 v2 = (double)(best_array[last_best]) / 65535;
1089 if (v2 < 0.0001) { /* The screen is black */
1090 output->brightness = 0;
1091 output->gamma.red = 1;
1092 output->gamma.green = 1;
1093 output->gamma.blue = 1;
1095 if ((last_best + 1) == size)
1096 output->brightness = v2;
1098 output->brightness = exp((log(v2)*log(i1) - log(v1)*log(i2))/log(i1/i2));
1099 output->gamma.red = log((double)(crtc_gamma->red[last_red / 2]) / output->brightness
1100 / 65535) / log((double)((last_red / 2) + 1) / size);
1101 output->gamma.green = log((double)(crtc_gamma->green[last_green / 2]) / output->brightness
1102 / 65535) / log((double)((last_green / 2) + 1) / size);
1103 output->gamma.blue = log((double)(crtc_gamma->blue[last_blue / 2]) / output->brightness
1104 / 65535) / log((double)((last_blue / 2) + 1) / size);
1107 XRRFreeGamma(crtc_gamma);
1111 set_output_info (output_t *output, RROutput xid, XRROutputInfo *output_info)
1113 /* sanity check output info */
1114 if (output_info->connection != RR_Disconnected && !output_info->nmode)
1115 warning ("Output %s is not disconnected but has no modes\n",
1118 /* set output name and info */
1119 if (!(output->output.kind & name_xid))
1120 set_name_xid (&output->output, xid);
1121 if (!(output->output.kind & name_string))
1122 set_name_string (&output->output, output_info->name);
1123 output->output_info = output_info;
1125 /* set crtc name and info */
1126 if (!(output->changes & changes_crtc))
1127 set_name_xid (&output->crtc, output_info->crtc);
1129 if (output->crtc.kind == name_xid && output->crtc.xid == None)
1130 output->crtc_info = NULL;
1133 output->crtc_info = find_crtc (&output->crtc);
1134 if (!output->crtc_info)
1136 if (output->crtc.kind & name_xid)
1137 fatal ("cannot find crtc 0x%lx\n", output->crtc.xid);
1138 if (output->crtc.kind & name_index)
1139 fatal ("cannot find crtc %d\n", output->crtc.index);
1141 if (!output_can_use_crtc (output, output->crtc_info))
1142 fatal ("output %s cannot use crtc 0x%lx\n", output->output.string,
1143 output->crtc_info->crtc.xid);
1146 /* set mode name and info */
1147 if (!(output->changes & changes_mode))
1149 crtc_t *crtc = NULL;
1151 if (output_info->crtc)
1152 crtc = find_crtc_by_xid(output_info->crtc);
1153 if (crtc && crtc->crtc_info)
1154 set_name_xid (&output->mode, crtc->crtc_info->mode);
1155 else if (output->crtc_info)
1156 set_name_xid (&output->mode, output->crtc_info->crtc_info->mode);
1158 set_name_xid (&output->mode, None);
1159 if (output->mode.xid)
1161 output->mode_info = find_mode_by_xid (output->mode.xid);
1162 if (!output->mode_info)
1163 fatal ("server did not report mode 0x%lx for output %s\n",
1164 output->mode.xid, output->output.string);
1167 output->mode_info = NULL;
1169 else if (output->mode.kind == name_xid && output->mode.xid == None)
1170 output->mode_info = NULL;
1173 if (output->mode.kind == name_preferred)
1174 output->mode_info = preferred_mode (output);
1176 output->mode_info = find_mode_for_output (output, &output->mode);
1177 if (!output->mode_info)
1179 if (output->mode.kind & name_preferred)
1180 fatal ("cannot find preferred mode\n");
1181 if (output->mode.kind & name_string)
1182 fatal ("cannot find mode %s\n", output->mode.string);
1183 if (output->mode.kind & name_xid)
1184 fatal ("cannot find mode 0x%lx\n", output->mode.xid);
1186 if (!output_can_use_mode (output, output->mode_info))
1187 fatal ("output %s cannot use mode %s\n", output->output.string,
1188 output->mode_info->name);
1192 if (!(output->changes & changes_position))
1194 if (output->crtc_info)
1196 output->x = output->crtc_info->crtc_info->x;
1197 output->y = output->crtc_info->crtc_info->y;
1207 if (!(output->changes & changes_rotation))
1209 output->rotation &= ~0xf;
1210 if (output->crtc_info)
1211 output->rotation |= (output->crtc_info->crtc_info->rotation & 0xf);
1213 output->rotation = RR_Rotate_0;
1215 if (!(output->changes & changes_reflection))
1217 output->rotation &= ~(RR_Reflect_X|RR_Reflect_Y);
1218 if (output->crtc_info)
1219 output->rotation |= (output->crtc_info->crtc_info->rotation &
1220 (RR_Reflect_X|RR_Reflect_Y));
1222 if (!output_can_use_rotation (output, output->rotation))
1223 fatal ("output %s cannot use rotation \"%s\" reflection \"%s\"\n",
1224 output->output.string,
1225 rotation_name (output->rotation),
1226 reflection_name (output->rotation));
1229 if (!(output->changes & changes_gamma))
1230 set_gamma_info(output);
1232 /* set transformation */
1233 if (!(output->changes & changes_transform))
1235 if (output->crtc_info)
1236 copy_transform (&output->transform, &output->crtc_info->current_transform);
1238 init_transform (&output->transform);
1240 /* transform was already set for --scale or --transform */
1242 /* for --scale-from, figure out the mode size and compute the transform
1243 * for the target framebuffer area */
1244 if (output->scale_from_w > 0 && output->mode_info) {
1245 double sx = (double)output->scale_from_w /
1246 output->mode_info->width;
1247 double sy = (double)output->scale_from_h /
1248 output->mode_info->height;
1250 printf("scaling %s by %lfx%lf\n", output->output.string, sx,
1252 init_transform (&output->transform);
1253 output->transform.transform.matrix[0][0] = XDoubleToFixed (sx);
1254 output->transform.transform.matrix[1][1] = XDoubleToFixed (sy);
1255 output->transform.transform.matrix[2][2] = XDoubleToFixed (1.0);
1256 if (sx != 1 || sy != 1)
1257 output->transform.filter = "bilinear";
1259 output->transform.filter = "nearest";
1260 output->transform.nparams = 0;
1261 output->transform.params = NULL;
1266 if (!(output->changes & changes_primary))
1267 output->primary = output_is_primary(output);
1271 get_screen (Bool current)
1274 fatal ("Server RandR version before 1.2\n");
1279 XRRGetScreenSizeRange (dpy, root, &minWidth, &minHeight,
1280 &maxWidth, &maxHeight);
1283 res = XRRGetScreenResourcesCurrent (dpy, root);
1285 res = XRRGetScreenResources (dpy, root);
1286 if (!res) fatal ("could not get screen resources");
1294 num_crtcs = res->ncrtc;
1295 crtcs = calloc (num_crtcs, sizeof (crtc_t));
1296 if (!crtcs) fatal ("out of memory\n");
1298 for (c = 0; c < res->ncrtc; c++)
1300 XRRCrtcInfo *crtc_info = XRRGetCrtcInfo (dpy, res, res->crtcs[c]);
1301 XRRCrtcTransformAttributes *attr;
1302 XRRPanning *panning_info = NULL;
1306 memset(&zero, 0, sizeof(zero));
1307 panning_info = XRRGetPanning (dpy, res, res->crtcs[c]);
1308 zero.timestamp = panning_info->timestamp;
1309 if (!memcmp(panning_info, &zero, sizeof(zero))) {
1310 Xfree(panning_info);
1311 panning_info = NULL;
1315 set_name_xid (&crtcs[c].crtc, res->crtcs[c]);
1316 set_name_index (&crtcs[c].crtc, c);
1317 if (!crtc_info) fatal ("could not get crtc 0x%lx information\n", res->crtcs[c]);
1318 crtcs[c].crtc_info = crtc_info;
1319 crtcs[c].panning_info = panning_info;
1320 if (crtc_info->mode == None)
1322 crtcs[c].mode_info = NULL;
1325 crtcs[c].rotation = RR_Rotate_0;
1327 if (XRRGetCrtcTransform (dpy, res->crtcs[c], &attr) && attr) {
1328 set_transform (&crtcs[c].current_transform,
1329 &attr->currentTransform,
1330 attr->currentFilter,
1331 attr->currentParams,
1332 attr->currentNparams);
1337 init_transform (&crtcs[c].current_transform);
1339 copy_transform (&crtcs[c].pending_transform, &crtcs[c].current_transform);
1344 crtc_add_output (crtc_t *crtc, output_t *output)
1347 crtc->outputs = realloc (crtc->outputs, (crtc->noutput + 1) * sizeof (output_t *));
1350 crtc->outputs = malloc (sizeof (output_t *));
1351 crtc->x = output->x;
1352 crtc->y = output->y;
1353 crtc->rotation = output->rotation;
1354 crtc->mode_info = output->mode_info;
1355 copy_transform (&crtc->pending_transform, &output->transform);
1357 if (!crtc->outputs) fatal ("out of memory\n");
1358 crtc->outputs[crtc->noutput++] = output;
1366 for (output = all_outputs; output; output = output->next)
1368 if (!output->mode_info) continue;
1369 crtc_add_output (output->crtc_info, output);
1378 for (output = all_outputs; output; output = output->next)
1380 if (! output->crtc_info)
1382 if (! (output->changes & changes_panning))
1384 if (! output->crtc_info->panning_info)
1385 output->crtc_info->panning_info = malloc (sizeof(XRRPanning));
1386 memcpy (output->crtc_info->panning_info, &output->panning, sizeof(XRRPanning));
1387 output->crtc_info->changing = 1;
1396 for (output = all_outputs; output; output = output->next) {
1399 XRRCrtcGamma *crtc_gamma;
1404 if (!(output->changes & changes_gamma))
1407 if (!output->crtc_info) {
1408 fatal("Need crtc to set gamma on.\n");
1412 crtc = output->crtc_info;
1414 size = XRRGetCrtcGammaSize(dpy, crtc->crtc.xid);
1417 fatal("Gamma size is 0.\n");
1422 * The gamma-correction lookup table managed through XRR[GS]etCrtcGamma
1423 * is 2^n in size, where 'n' is the number of significant bits in
1424 * the X Color. Because an X Color is 16 bits, size cannot be larger
1428 fatal("Gamma correction table is impossibly large.\n");
1433 * The hardware color lookup table has a number of significant
1434 * bits equal to ffs(size) - 1; compute all values so that
1435 * they are in the range [0,size) then shift the values so
1436 * that they occupy the MSBs of the 16-bit X Color.
1438 shift = 16 - (ffs(size) - 1);
1440 crtc_gamma = XRRAllocGamma(size);
1442 fatal("Gamma allocation failed.\n");
1446 if (output->gamma.red == 0.0)
1447 output->gamma.red = 1.0;
1448 if (output->gamma.green == 0.0)
1449 output->gamma.green = 1.0;
1450 if (output->gamma.blue == 0.0)
1451 output->gamma.blue = 1.0;
1453 gammaRed = 1.0 / output->gamma.red;
1454 gammaGreen = 1.0 / output->gamma.green;
1455 gammaBlue = 1.0 / output->gamma.blue;
1457 for (i = 0; i < size; i++) {
1458 if (gammaRed == 1.0 && output->brightness == 1.0)
1459 crtc_gamma->red[i] = i;
1461 crtc_gamma->red[i] = dmin(pow((double)i/(double)(size - 1),
1462 gammaRed) * output->brightness,
1463 1.0) * (double)(size - 1);
1464 crtc_gamma->red[i] <<= shift;
1466 if (gammaGreen == 1.0 && output->brightness == 1.0)
1467 crtc_gamma->green[i] = i;
1469 crtc_gamma->green[i] = dmin(pow((double)i/(double)(size - 1),
1470 gammaGreen) * output->brightness,
1471 1.0) * (double)(size - 1);
1472 crtc_gamma->green[i] <<= shift;
1474 if (gammaBlue == 1.0 && output->brightness == 1.0)
1475 crtc_gamma->blue[i] = i;
1477 crtc_gamma->blue[i] = dmin(pow((double)i/(double)(size - 1),
1478 gammaBlue) * output->brightness,
1479 1.0) * (double)(size - 1);
1480 crtc_gamma->blue[i] <<= shift;
1483 XRRSetCrtcGamma(dpy, crtc->crtc.xid, crtc_gamma);
1495 XRRSetOutputPrimary(dpy, root, None);
1497 for (output = all_outputs; output; output = output->next) {
1498 if (!(output->changes & changes_primary))
1500 if (output->primary)
1501 XRRSetOutputPrimary(dpy, root, output->output.xid);
1507 crtc_disable (crtc_t *crtc)
1510 printf ("crtc %d: disable\n", crtc->crtc.index);
1513 return RRSetConfigSuccess;
1514 return XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1515 0, 0, None, RR_Rotate_0, NULL, 0);
1519 crtc_set_transform (crtc_t *crtc, transform_t *transform)
1523 XRRQueryVersion (dpy, &major, &minor);
1524 if (major > 1 || (major == 1 && minor >= 3))
1525 XRRSetCrtcTransform (dpy, crtc->crtc.xid,
1526 &transform->transform,
1529 transform->nparams);
1533 crtc_revert (crtc_t *crtc)
1535 XRRCrtcInfo *crtc_info = crtc->crtc_info;
1538 printf ("crtc %d: revert\n", crtc->crtc.index);
1541 return RRSetConfigSuccess;
1543 if (!equal_transform (&crtc->current_transform, &crtc->pending_transform))
1544 crtc_set_transform (crtc, &crtc->current_transform);
1545 return XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1546 crtc_info->x, crtc_info->y,
1547 crtc_info->mode, crtc_info->rotation,
1548 crtc_info->outputs, crtc_info->noutput);
1552 crtc_apply (crtc_t *crtc)
1554 RROutput *rr_outputs;
1559 if (!crtc->changing || !crtc->mode_info)
1560 return RRSetConfigSuccess;
1562 rr_outputs = calloc (crtc->noutput, sizeof (RROutput));
1565 for (o = 0; o < crtc->noutput; o++)
1566 rr_outputs[o] = crtc->outputs[o]->output.xid;
1567 mode = crtc->mode_info->id;
1569 printf ("crtc %d: %12s %6.1f +%d+%d", crtc->crtc.index,
1570 crtc->mode_info->name, mode_refresh (crtc->mode_info),
1572 for (o = 0; o < crtc->noutput; o++)
1573 printf (" \"%s\"", crtc->outputs[o]->output.string);
1578 s = RRSetConfigSuccess;
1581 if (!equal_transform (&crtc->current_transform, &crtc->pending_transform))
1582 crtc_set_transform (crtc, &crtc->pending_transform);
1583 s = XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1584 crtc->x, crtc->y, mode, crtc->rotation,
1585 rr_outputs, crtc->noutput);
1586 if (s == RRSetConfigSuccess && crtc->panning_info) {
1588 s = XRRSetPanning (dpy, res, crtc->crtc.xid, crtc->panning_info);
1590 fatal ("panning needs RandR 1.3\n");
1598 screen_revert (void)
1601 printf ("screen %d: revert\n", screen);
1605 XRRSetScreenSize (dpy, root,
1606 DisplayWidth (dpy, screen),
1607 DisplayHeight (dpy, screen),
1608 DisplayWidthMM (dpy, screen),
1609 DisplayHeightMM (dpy, screen));
1615 if (fb_width == DisplayWidth (dpy, screen) &&
1616 fb_height == DisplayHeight (dpy, screen) &&
1617 fb_width_mm == DisplayWidthMM (dpy, screen) &&
1618 fb_height_mm == DisplayHeightMM (dpy, screen))
1623 printf ("screen %d: %dx%d %dx%d mm %6.2fdpi\n", screen,
1624 fb_width, fb_height, fb_width_mm, fb_height_mm, dpi);
1627 XRRSetScreenSize (dpy, root, fb_width, fb_height,
1628 fb_width_mm, fb_height_mm);
1636 /* first disable all crtcs */
1637 for (c = 0; c < res->ncrtc; c++)
1638 crtc_disable (&crtcs[c]);
1639 /* next reset screen size */
1641 /* now restore all crtcs */
1642 for (c = 0; c < res->ncrtc; c++)
1643 crtc_revert (&crtcs[c]);
1647 * uh-oh, something bad happened in the middle of changing
1648 * the configuration. Revert to the previous configuration
1651 static void _X_NORETURN
1652 panic (Status s, crtc_t *crtc)
1654 int c = crtc->crtc.index;
1655 const char *message;
1658 case RRSetConfigSuccess: message = "succeeded"; break;
1659 case BadAlloc: message = "out of memory"; break;
1660 case RRSetConfigFailed: message = "failed"; break;
1661 case RRSetConfigInvalidConfigTime: message = "invalid config time"; break;
1662 case RRSetConfigInvalidTime: message = "invalid time"; break;
1663 default: message = "unknown failure"; break;
1666 fprintf (stderr, "%s: Configure crtc %d %s\n", program_name, c, message);
1678 * Hold the server grabbed while messing with
1679 * the screen so that apps which notice the resize
1680 * event and ask for xinerama information from the server
1681 * receive up-to-date information
1687 * Turn off any crtcs which are to be disabled or which are
1688 * larger than the target size
1690 for (c = 0; c < res->ncrtc; c++)
1692 crtc_t *crtc = &crtcs[c];
1693 XRRCrtcInfo *crtc_info = crtc->crtc_info;
1695 /* if this crtc is already disabled, skip it */
1696 if (crtc_info->mode == None)
1700 * If this crtc is to be left enabled, make
1701 * sure the old size fits then new screen
1703 if (crtc->mode_info)
1705 XRRModeInfo *old_mode = find_mode_by_xid (crtc_info->mode);
1710 panic (RRSetConfigFailed, crtc);
1712 /* old position and size information */
1713 mode_geometry (old_mode, crtc_info->rotation,
1714 &crtc->current_transform.transform,
1717 x = crtc_info->x + bounds.x1;
1718 y = crtc_info->y + bounds.y1;
1719 w = bounds.x2 - bounds.x1;
1720 h = bounds.y2 - bounds.y1;
1722 /* if it fits, skip it */
1723 if (x + w <= fb_width && y + h <= fb_height)
1725 crtc->changing = True;
1727 s = crtc_disable (crtc);
1728 if (s != RRSetConfigSuccess)
1733 * Set the screen size
1741 for (c = 0; c < res->ncrtc; c++)
1743 crtc_t *crtc = &crtcs[c];
1745 s = crtc_apply (crtc);
1746 if (s != RRSetConfigSuccess)
1753 * Release the server grab and let all clients
1754 * respond to the updated state
1757 XUngrabServer (dpy);
1761 * Use current output state to complete the output list
1769 for (o = 0; o < res->noutput; o++)
1771 XRROutputInfo *output_info = XRRGetOutputInfo (dpy, res, res->outputs[o]);
1774 if (!output_info) fatal ("could not get output 0x%lx information\n", res->outputs[o]);
1775 set_name_xid (&output_name, res->outputs[o]);
1776 set_name_index (&output_name, o);
1777 set_name_string (&output_name, output_info->name);
1778 output = find_output (&output_name);
1781 output = add_output ();
1782 set_name_all (&output->output, &output_name);
1784 * When global --automatic mode is set, turn on connected but off
1785 * outputs, turn off disconnected but on outputs
1789 switch (output_info->connection) {
1791 if (!output_info->crtc) {
1792 output->changes |= changes_automatic;
1793 output->automatic = True;
1796 case RR_Disconnected:
1797 if (output_info->crtc)
1799 output->changes |= changes_automatic;
1800 output->automatic = True;
1806 output->found = True;
1809 * Automatic mode -- track connection state and enable/disable outputs
1812 if (output->automatic)
1814 switch (output_info->connection) {
1816 case RR_UnknownConnection:
1817 if ((!(output->changes & changes_mode)))
1819 set_name_preferred (&output->mode);
1820 output->changes |= changes_mode;
1823 case RR_Disconnected:
1824 if ((!(output->changes & changes_mode)))
1826 set_name_xid (&output->mode, None);
1827 set_name_xid (&output->crtc, None);
1828 output->changes |= changes_mode;
1829 output->changes |= changes_crtc;
1835 set_output_info (output, res->outputs[o], output_info);
1837 for (q = all_outputs; q; q = q->next)
1841 fprintf(stderr, "warning: output %s not found; ignoring\n",
1848 mark_changing_crtcs (void)
1852 for (c = 0; c < num_crtcs; c++)
1854 crtc_t *crtc = &crtcs[c];
1858 /* walk old output list (to catch disables) */
1859 for (o = 0; o < crtc->crtc_info->noutput; o++)
1861 output = find_output_by_xid (crtc->crtc_info->outputs[o]);
1862 if (!output) fatal ("cannot find output 0x%lx\n",
1863 crtc->crtc_info->outputs[o]);
1864 if (output->changes)
1865 crtc->changing = True;
1867 /* walk new output list */
1868 for (o = 0; o < crtc->noutput; o++)
1870 output = crtc->outputs[o];
1871 if (output->changes)
1872 crtc->changing = True;
1878 * Test whether 'crtc' can be used for 'output'
1881 check_crtc_for_output (crtc_t *crtc, output_t *output)
1887 for (c = 0; c < output->output_info->ncrtc; c++)
1888 if (output->output_info->crtcs[c] == crtc->crtc.xid)
1890 if (c == output->output_info->ncrtc)
1892 for (other = all_outputs; other; other = other->next)
1894 if (other == output)
1897 if (other->mode_info == NULL)
1900 if (other->crtc_info != crtc)
1903 /* see if the output connected to the crtc can clone to this output */
1904 for (l = 0; l < output->output_info->nclone; l++)
1905 if (output->output_info->clones[l] == other->output.xid)
1907 /* not on the list, can't clone */
1908 if (l == output->output_info->nclone)
1914 /* make sure the state matches */
1915 if (crtc->mode_info != output->mode_info)
1917 if (crtc->x != output->x)
1919 if (crtc->y != output->y)
1921 if (crtc->rotation != output->rotation)
1923 if (!equal_transform (&crtc->current_transform, &output->transform))
1926 else if (crtc->crtc_info->noutput)
1928 /* make sure the state matches the already used state */
1929 XRRModeInfo *mode = find_mode_by_xid (crtc->crtc_info->mode);
1931 if (mode != output->mode_info)
1933 if (crtc->crtc_info->x != output->x)
1935 if (crtc->crtc_info->y != output->y)
1937 if (crtc->crtc_info->rotation != output->rotation)
1944 find_crtc_for_output (output_t *output)
1948 for (c = 0; c < output->output_info->ncrtc; c++)
1952 crtc = find_crtc_by_xid (output->output_info->crtcs[c]);
1953 if (!crtc) fatal ("cannot find crtc 0x%lx\n", output->output_info->crtcs[c]);
1955 if (check_crtc_for_output (crtc, output))
1962 set_positions (void)
1973 for (output = all_outputs; output; output = output->next)
1976 name_t relation_name;
1978 if (!(output->changes & changes_relation)) continue;
1980 if (output->mode_info == NULL) continue;
1982 init_name (&relation_name);
1983 set_name_string (&relation_name, output->relative_to);
1984 relation = find_output (&relation_name);
1985 if (!relation) fatal ("cannot find output \"%s\"\n", output->relative_to);
1987 if (relation->mode_info == NULL)
1991 output->changes |= changes_position;
1996 * Make sure the dependent object has been set in place
1998 if ((relation->changes & changes_relation) &&
1999 !(relation->changes & changes_position))
2005 switch (output->relation) {
2006 case relation_left_of:
2007 output->y = relation->y;
2008 output->x = relation->x - mode_width (output->mode_info, output->rotation);
2010 case relation_right_of:
2011 output->y = relation->y;
2012 output->x = relation->x + mode_width (relation->mode_info, relation->rotation);
2014 case relation_above:
2015 output->x = relation->x;
2016 output->y = relation->y - mode_height (output->mode_info, output->rotation);
2018 case relation_below:
2019 output->x = relation->x;
2020 output->y = relation->y + mode_height (relation->mode_info, relation->rotation);
2022 case relation_same_as:
2023 output->x = relation->x;
2024 output->y = relation->y;
2026 output->changes |= changes_position;
2032 fatal ("loop in relative position specifications\n");
2036 * Now normalize positions so the upper left corner of all outputs is at 0,0
2040 for (output = all_outputs; output; output = output->next)
2042 if (output->mode_info == NULL) continue;
2044 if (output->x < min_x) min_x = output->x;
2045 if (output->y < min_y) min_y = output->y;
2049 /* move all outputs */
2050 for (output = all_outputs; output; output = output->next)
2052 if (output->mode_info == NULL) continue;
2056 output->changes |= changes_position;
2062 set_screen_size (void)
2065 Bool fb_specified = fb_width != 0 && fb_height != 0;
2067 for (output = all_outputs; output; output = output->next)
2069 XRRModeInfo *mode_info = output->mode_info;
2073 if (!mode_info) continue;
2075 mode_geometry (mode_info, output->rotation,
2076 &output->transform.transform,
2078 x = output->x + bounds.x1;
2079 y = output->y + bounds.y1;
2080 w = bounds.x2 - bounds.x1;
2081 h = bounds.y2 - bounds.y1;
2082 /* make sure output fits in specified size */
2085 if (x + w > fb_width || y + h > fb_height)
2086 warning ("specified screen %dx%d not large enough for output %s (%dx%d+%d+%d)\n",
2087 fb_width, fb_height, output->output.string, w, h, x, y);
2089 /* fit fb to output */
2093 if (x + w > fb_width)
2095 if (y + h > fb_height)
2097 if (output->changes & changes_panning)
2098 pan = &output->panning;
2100 pan = output->crtc_info ? output->crtc_info->panning_info : NULL;
2101 if (pan && pan->left + pan->width > fb_width)
2102 fb_width = pan->left + pan->width;
2103 if (pan && pan->top + pan->height > fb_height)
2104 fb_height = pan->top + pan->height;
2108 if (fb_width > maxWidth || fb_height > maxHeight)
2109 fatal ("screen cannot be larger than %dx%d (desired size %dx%d)\n",
2110 maxWidth, maxHeight, fb_width, fb_height);
2113 if (fb_width < minWidth || fb_height < minHeight)
2114 fatal ("screen must be at least %dx%d\n", minWidth, minHeight);
2118 if (fb_width < minWidth) fb_width = minWidth;
2119 if (fb_height < minHeight) fb_height = minHeight;
2125 disable_outputs (output_t *outputs)
2129 outputs->crtc_info = NULL;
2130 outputs = outputs->next;
2135 * find the best mapping from output to crtc available
2138 pick_crtcs_score (output_t *outputs)
2151 outputs = outputs->next;
2153 * Score with this output disabled
2155 output->crtc_info = NULL;
2156 best_score = pick_crtcs_score (outputs);
2157 if (output->mode_info == NULL)
2162 * Now score with this output any valid crtc
2164 for (c = 0; c < output->output_info->ncrtc; c++)
2168 crtc = find_crtc_by_xid (output->output_info->crtcs[c]);
2170 fatal ("cannot find crtc 0x%lx\n", output->output_info->crtcs[c]);
2172 /* reset crtc allocation for following outputs */
2173 disable_outputs (outputs);
2174 if (!check_crtc_for_output (crtc, output))
2178 /* slight preference for existing connections */
2179 if (crtc == output->current_crtc_info)
2182 output->crtc_info = crtc;
2183 score = my_score + pick_crtcs_score (outputs);
2184 if (score > best_score)
2190 if (output->crtc_info != best_crtc)
2191 output->crtc_info = best_crtc;
2193 * Reset other outputs based on this one using the best crtc
2195 (void) pick_crtcs_score (outputs);
2201 * Pick crtcs for any changing outputs that don't have one
2209 * First try to match up newly enabled outputs with spare crtcs
2211 for (output = all_outputs; output; output = output->next)
2213 if (output->changes && output->mode_info)
2215 if (output->crtc_info) {
2216 if (output->crtc_info->crtc_info->noutput > 0 &&
2217 (output->crtc_info->crtc_info->noutput > 1 ||
2218 output != find_output_by_xid (output->crtc_info->crtc_info->outputs[0])))
2221 output->crtc_info = find_crtc_for_output (output);
2222 if (!output->crtc_info)
2233 * When the simple way fails, see if there is a way
2234 * to swap crtcs around and make things work
2236 for (output = all_outputs; output; output = output->next)
2237 output->current_crtc_info = output->crtc_info;
2238 pick_crtcs_score (all_outputs);
2239 for (output = all_outputs; output; output = output->next)
2241 if (output->mode_info && !output->crtc_info)
2242 fatal ("cannot find crtc for output %s\n", output->output.string);
2243 if (!output->changes && output->crtc_info != output->current_crtc_info)
2244 output->changes |= changes_crtc;
2249 check_strtol(char *s)
2252 int result = strtol(s, &endptr, 10);
2254 argerr ("failed to parse '%s' as a number\n", s);
2259 check_strtod(char *s)
2262 double result = strtod(s, &endptr);
2264 argerr ("failed to parse '%s' as a number\n", s);
2270 property_values_from_string(const char *str, const Atom type, const int format,
2271 int *returned_nitems)
2274 void *returned_bytes = NULL;
2275 int nitems = 0, bytes_per_item = format / 8;
2277 if ((type != XA_INTEGER && type != XA_CARDINAL) ||
2278 (format != 8 && format != 16 && format != 32))
2285 for (token = strtok (tmp, ","); token; token = strtok (NULL, ","))
2288 long int val = strtol (token, &endptr, 0);
2290 if (token == endptr || *endptr != '\0')
2292 argerr ("failed to parse '%s' as a number\n", token);
2295 returned_bytes = realloc (returned_bytes, (nitems + 1) * bytes_per_item);
2297 if (type == XA_INTEGER && format == 8)
2299 int8_t *ptr = returned_bytes;
2300 ptr[nitems] = (int8_t) val;
2302 else if (type == XA_INTEGER && format == 16)
2304 int16_t *ptr = returned_bytes;
2305 ptr[nitems] = (int16_t) val;
2307 else if (type == XA_INTEGER && format == 32)
2309 int32_t *ptr = returned_bytes;
2310 ptr[nitems] = (int32_t) val;
2312 else if (type == XA_CARDINAL && format == 8)
2314 uint8_t *ptr = returned_bytes;
2315 ptr[nitems] = (uint8_t) val;
2317 else if (type == XA_CARDINAL && format == 16)
2319 uint16_t *ptr = returned_bytes;
2320 ptr[nitems] = (uint16_t) val;
2322 else if (type == XA_CARDINAL && format == 32)
2324 uint32_t *ptr = returned_bytes;
2325 ptr[nitems] = (uint32_t) val;
2330 free (returned_bytes);
2339 *returned_nitems = nitems;
2340 return returned_bytes;
2345 print_output_property_value(Bool is_edid,
2346 int value_format, /* 8, 16, 32 */
2347 Atom value_type, /* XA_{ATOM,INTEGER,CARDINAL} */
2348 const void *value_bytes)
2350 /* special-case the EDID */
2351 if (is_edid && value_format == 8)
2353 const uint8_t *val = value_bytes;
2354 printf ("%02" PRIx8, *val);
2358 if (value_type == XA_ATOM && value_format == 32)
2360 const Atom *val = value_bytes;
2361 char *str = XGetAtomName (dpy, *val);
2370 if (value_type == XA_INTEGER)
2372 if (value_format == 8)
2374 const int8_t *val = value_bytes;
2375 printf ("%" PRId8, *val);
2378 if (value_format == 16)
2380 const int16_t *val = value_bytes;
2381 printf ("%" PRId16, *val);
2384 if (value_format == 32)
2386 const int32_t *val = value_bytes;
2387 printf ("%" PRId32, *val);
2392 if (value_type == XA_CARDINAL)
2394 if (value_format == 8)
2396 const uint8_t *val = value_bytes;
2397 printf ("%" PRIu8, *val);
2400 if (value_format == 16)
2402 const uint16_t *val = value_bytes;
2403 printf ("%" PRIu16, *val);
2406 if (value_format == 32)
2408 const uint32_t *val = value_bytes;
2409 printf ("%" PRIu32, *val);
2418 get_providers (void)
2420 XRRProviderResources *pr;
2423 if (!has_1_4 || providers)
2426 pr = XRRGetProviderResources(dpy, root);
2427 num_providers = pr->nproviders;
2428 providers = calloc (num_providers, sizeof (provider_t));
2430 fatal ("out of memory\n");
2432 for (i = 0; i < num_providers; i++) {
2433 provider_t *provider = &providers[i];
2434 name_t *name = &provider->provider;
2435 XRRProviderInfo *info = XRRGetProviderInfo(dpy, res, pr->providers[i]);
2437 provider->info = info;
2438 set_name_xid (name, pr->providers[i]);
2439 set_name_index (name, i);
2440 set_name_string (name, info->name);
2443 XRRFreeProviderResources(pr);
2447 find_provider (name_t *name)
2451 for (i = 0; i < num_providers; i++) {
2452 provider_t *p = &providers[i];
2453 name_kind_t common = name->kind & p->provider.kind;
2455 if ((common & name_xid) && name->xid == p->provider.xid)
2457 if ((common & name_string) && !strcmp (name->string, p->provider.string))
2459 if ((common & name_index) && name->index == p->provider.index)
2463 printf ("Could not find provider with ");
2471 main (int argc, char **argv)
2473 XRRScreenSize *sizes;
2474 XRRScreenConfiguration *sc;
2478 Status status = RRSetConfigFailed;
2481 int action_requested = False;
2482 Rotation current_rotation;
2484 XRRScreenChangeNotifyEvent *sce;
2485 char *display_name = NULL;
2487 SizeID current_size;
2493 Bool version = False;
2494 int event_base, error_base;
2496 int width = 0, height = 0;
2497 Bool have_pixel_size = False;
2499 output_t *config_output = NULL;
2500 Bool setit_1_2 = False;
2501 Bool query_1_2 = False;
2502 Bool modeit = False;
2503 Bool propit = False;
2504 Bool query_1 = False;
2505 Bool list_providers = False;
2506 Bool provsetoutsource = False;
2507 Bool provsetoffsink = False;
2509 Bool current = False;
2511 program_name = argv[0];
2512 for (i = 1; i < argc; i++) {
2513 if (!strcmp ("-display", argv[i]) || !strcmp ("-d", argv[i])) {
2514 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2515 display_name = argv[i];
2518 if (!strcmp("-help", argv[i]) || !strcmp("--help", argv[i])) {
2522 if (!strcmp ("--verbose", argv[i])) {
2526 if (!strcmp ("--dryrun", argv[i])) {
2531 if (!strcmp ("--nograb", argv[i])) {
2532 grab_server = False;
2535 if (!strcmp("--current", argv[i])) {
2540 if (!strcmp ("-s", argv[i]) || !strcmp ("--size", argv[i])) {
2541 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2542 if (sscanf (argv[i], "%dx%d", &width, &height) == 2) {
2543 have_pixel_size = True;
2545 size = check_strtol(argv[i]);
2546 if (size < 0) argerr ("--size argument must be nonnegative\n");
2549 action_requested = True;
2553 if (!strcmp ("-r", argv[i]) ||
2554 !strcmp ("--rate", argv[i]) ||
2555 !strcmp ("--refresh", argv[i]))
2557 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2558 rate = check_strtod(argv[i]);
2562 config_output->refresh = rate;
2563 config_output->changes |= changes_refresh;
2566 action_requested = True;
2570 if (!strcmp ("-v", argv[i]) || !strcmp ("--version", argv[i])) {
2572 action_requested = True;
2576 if (!strcmp ("-x", argv[i])) {
2577 reflection |= RR_Reflect_X;
2579 action_requested = True;
2582 if (!strcmp ("-y", argv[i])) {
2583 reflection |= RR_Reflect_Y;
2585 action_requested = True;
2588 if (!strcmp ("--screen", argv[i])) {
2589 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2590 screen = check_strtol(argv[i]);
2591 if (screen < 0) argerr ("--screen argument must be nonnegative\n");
2594 if (!strcmp ("-q", argv[i]) || !strcmp ("--query", argv[i])) {
2598 if (!strcmp ("-o", argv[i]) || !strcmp ("--orientation", argv[i])) {
2600 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2601 dirind = strtol(argv[i], &endptr, 10);
2602 if (argv[i] == endptr) {
2603 for (dirind = 0; dirind < 4; dirind++) {
2604 if (strcmp (direction[dirind], argv[i]) == 0) break;
2607 if ((dirind < 0) || (dirind > 3))
2608 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2611 action_requested = True;
2614 if (!strcmp ("--prop", argv[i]) ||
2615 !strcmp ("--props", argv[i]) ||
2616 !strcmp ("--madprops", argv[i]) ||
2617 !strcmp ("--properties", argv[i]))
2621 action_requested = True;
2624 if (!strcmp ("--output", argv[i])) {
2625 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2627 config_output = find_output_by_name (argv[i]);
2628 if (!config_output) {
2629 config_output = add_output ();
2630 set_name (&config_output->output, argv[i], name_string|name_xid);
2634 action_requested = True;
2637 if (!strcmp ("--crtc", argv[i])) {
2638 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2639 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2640 set_name (&config_output->crtc, argv[i], name_xid|name_index);
2641 config_output->changes |= changes_crtc;
2644 if (!strcmp ("--mode", argv[i])) {
2645 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2646 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2647 set_name (&config_output->mode, argv[i], name_string|name_xid);
2648 config_output->changes |= changes_mode;
2651 if (!strcmp ("--preferred", argv[i])) {
2652 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2653 set_name_preferred (&config_output->mode);
2654 config_output->changes |= changes_mode;
2657 if (!strcmp ("--pos", argv[i])) {
2658 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2659 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2660 if (sscanf (argv[i], "%dx%d",
2661 &config_output->x, &config_output->y) != 2)
2662 argerr ("failed to parse '%s' as a position\n", argv[i]);
2663 config_output->changes |= changes_position;
2666 if (!strcmp ("--rotation", argv[i]) || !strcmp ("--rotate", argv[i])) {
2667 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2668 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2669 for (dirind = 0; dirind < 4; dirind++) {
2670 if (strcmp (direction[dirind], argv[i]) == 0) break;
2673 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2674 config_output->rotation &= ~0xf;
2675 config_output->rotation |= 1 << dirind;
2676 config_output->changes |= changes_rotation;
2679 if (!strcmp ("--reflect", argv[i]) || !strcmp ("--reflection", argv[i])) {
2680 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2681 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2682 for (dirind = 0; dirind < 4; dirind++) {
2683 if (strcmp (reflections[dirind], argv[i]) == 0) break;
2686 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2687 config_output->rotation &= ~(RR_Reflect_X|RR_Reflect_Y);
2688 config_output->rotation |= dirind * RR_Reflect_X;
2689 config_output->changes |= changes_reflection;
2692 if (!strcmp ("--left-of", argv[i])) {
2693 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2694 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2695 config_output->relation = relation_left_of;
2696 config_output->relative_to = argv[i];
2697 config_output->changes |= changes_relation;
2700 if (!strcmp ("--right-of", argv[i])) {
2701 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2702 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2703 config_output->relation = relation_right_of;
2704 config_output->relative_to = argv[i];
2705 config_output->changes |= changes_relation;
2708 if (!strcmp ("--above", argv[i])) {
2709 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2710 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2711 config_output->relation = relation_above;
2712 config_output->relative_to = argv[i];
2713 config_output->changes |= changes_relation;
2716 if (!strcmp ("--below", argv[i])) {
2717 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2718 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2719 config_output->relation = relation_below;
2720 config_output->relative_to = argv[i];
2721 config_output->changes |= changes_relation;
2724 if (!strcmp ("--same-as", argv[i])) {
2725 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2726 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2727 config_output->relation = relation_same_as;
2728 config_output->relative_to = argv[i];
2729 config_output->changes |= changes_relation;
2732 if (!strcmp ("--panning", argv[i])) {
2734 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2735 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2736 pan = &config_output->panning;
2737 switch (sscanf (argv[i], "%dx%d+%d+%d/%dx%d+%d+%d/%d/%d/%d/%d",
2738 &pan->width, &pan->height, &pan->left, &pan->top,
2739 &pan->track_width, &pan->track_height,
2740 &pan->track_left, &pan->track_top,
2741 &pan->border_left, &pan->border_top,
2742 &pan->border_right, &pan->border_bottom)) {
2744 pan->left = pan->top = 0;
2747 pan->track_left = pan->track_top =
2748 pan->track_width = pan->track_height = 0;
2751 pan->border_left = pan->border_top =
2752 pan->border_right = pan->border_bottom = 0;
2757 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2759 config_output->changes |= changes_panning;
2762 if (!strcmp ("--gamma", argv[i])) {
2763 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2764 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2765 if (sscanf(argv[i], "%f:%f:%f", &config_output->gamma.red,
2766 &config_output->gamma.green, &config_output->gamma.blue) != 3)
2767 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2768 config_output->changes |= changes_gamma;
2772 if (!strcmp ("--brightness", argv[i])) {
2773 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2774 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2775 if (sscanf(argv[i], "%f", &config_output->brightness) != 1)
2776 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2777 config_output->changes |= changes_gamma;
2781 if (!strcmp ("--primary", argv[i])) {
2782 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2783 config_output->changes |= changes_primary;
2784 config_output->primary = True;
2788 if (!strcmp ("--noprimary", argv[i])) {
2793 if (!strcmp ("--set", argv[i])) {
2794 output_prop_t *prop;
2795 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2796 if (i+2 >= argc) argerr ("%s requires two arguments\n", argv[i]);
2797 prop = malloc (sizeof (output_prop_t));
2798 prop->next = config_output->props;
2799 config_output->props = prop;
2800 prop->name = argv[++i];
2801 prop->value = argv[++i];
2803 config_output->changes |= changes_property;
2807 if (!strcmp ("--scale", argv[i]))
2810 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2811 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2812 if (sscanf (argv[i], "%lfx%lf", &sx, &sy) != 2)
2813 argerr ("failed to parse '%s' as a scaling factor\n", argv[i]);
2814 init_transform (&config_output->transform);
2815 config_output->transform.transform.matrix[0][0] = XDoubleToFixed (sx);
2816 config_output->transform.transform.matrix[1][1] = XDoubleToFixed (sy);
2817 config_output->transform.transform.matrix[2][2] = XDoubleToFixed (1.0);
2818 if (sx != 1 || sy != 1)
2819 config_output->transform.filter = "bilinear";
2821 config_output->transform.filter = "nearest";
2822 config_output->transform.nparams = 0;
2823 config_output->transform.params = NULL;
2824 config_output->changes |= changes_transform;
2827 if (!strcmp ("--scale-from", argv[i]))
2830 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2831 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2832 if (sscanf (argv[i], "%dx%d", &w, &h) != 2)
2833 argerr ("failed to parse '%s' as a scale-from size\n", argv[i]);
2834 if (w <=0 || h <= 0)
2835 argerr ("--scale-from dimensions must be nonnegative\n");
2836 config_output->scale_from_w = w;
2837 config_output->scale_from_h = h;
2838 config_output->changes |= changes_transform;
2841 if (!strcmp ("--transform", argv[i])) {
2842 double transform[3][3];
2844 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2845 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2846 init_transform (&config_output->transform);
2847 if (strcmp (argv[i], "none") != 0)
2849 if (sscanf(argv[i], "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf",
2850 &transform[0][0],&transform[0][1],&transform[0][2],
2851 &transform[1][0],&transform[1][1],&transform[1][2],
2852 &transform[2][0],&transform[2][1],&transform[2][2])
2854 argerr ("failed to parse '%s' as a transformation\n", argv[i]);
2855 init_transform (&config_output->transform);
2856 for (k = 0; k < 3; k++)
2857 for (l = 0; l < 3; l++) {
2858 config_output->transform.transform.matrix[k][l] = XDoubleToFixed (transform[k][l]);
2860 config_output->transform.filter = "bilinear";
2861 config_output->transform.nparams = 0;
2862 config_output->transform.params = NULL;
2864 config_output->changes |= changes_transform;
2867 if (!strcmp ("--off", argv[i])) {
2868 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2869 set_name_xid (&config_output->mode, None);
2870 set_name_xid (&config_output->crtc, None);
2871 config_output->changes |= changes_mode;
2874 if (!strcmp ("--fb", argv[i])) {
2875 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2876 if (sscanf (argv[i], "%dx%d",
2877 &fb_width, &fb_height) != 2)
2878 argerr ("failed to parse '%s' as a framebuffer size\n", argv[i]);
2880 action_requested = True;
2883 if (!strcmp ("--fbmm", argv[i])) {
2884 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2885 if (sscanf (argv[i], "%dx%d",
2886 &fb_width_mm, &fb_height_mm) != 2)
2887 argerr ("failed to parse '%s' as a physical size\n", argv[i]);
2889 action_requested = True;
2892 if (!strcmp ("--dpi", argv[i])) {
2894 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2895 dpi = strtod(argv[i], &strtod_error);
2896 if (argv[i] == strtod_error)
2899 dpi_output_name = argv[i];
2902 action_requested = True;
2905 if (!strcmp ("--auto", argv[i])) {
2908 config_output->automatic = True;
2909 config_output->changes |= changes_automatic;
2914 action_requested = True;
2917 if (!strcmp ("--q12", argv[i]))
2922 if (!strcmp ("--q1", argv[i]))
2927 if (!strcmp ("--newmode", argv[i]))
2929 umode_t *m = calloc (1, sizeof (umode_t));
2934 argerr ("failed to parse '%s' as a mode specification\n", argv[i]);
2935 m->mode.name = argv[i];
2936 m->mode.nameLength = strlen (argv[i]);
2938 clock = check_strtod(argv[i++]);
2939 m->mode.dotClock = clock * 1e6;
2941 m->mode.width = check_strtol(argv[i++]);
2942 m->mode.hSyncStart = check_strtol(argv[i++]);
2943 m->mode.hSyncEnd = check_strtol(argv[i++]);
2944 m->mode.hTotal = check_strtol(argv[i++]);
2945 m->mode.height = check_strtol(argv[i++]);
2946 m->mode.vSyncStart = check_strtol(argv[i++]);
2947 m->mode.vSyncEnd = check_strtol(argv[i++]);
2948 m->mode.vTotal = check_strtol(argv[i++]);
2949 m->mode.modeFlags = 0;
2953 for (f = 0; mode_flags[f].string; f++)
2954 if (!strcasecmp (mode_flags[f].string, argv[i]))
2957 if (!mode_flags[f].string)
2959 m->mode.modeFlags |= mode_flags[f].flag;
2963 m->action = umode_create;
2966 action_requested = True;
2969 if (!strcmp ("--rmmode", argv[i]))
2971 umode_t *m = calloc (1, sizeof (umode_t));
2973 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2974 set_name (&m->name, argv[i], name_string|name_xid);
2975 m->action = umode_destroy;
2979 action_requested = True;
2982 if (!strcmp ("--addmode", argv[i]))
2984 umode_t *m = calloc (1, sizeof (umode_t));
2986 if (i+2 >= argc) argerr ("%s requires two arguments\n", argv[i]);
2987 set_name (&m->output, argv[++i], name_string|name_xid);
2988 set_name (&m->name, argv[++i], name_string|name_xid);
2989 m->action = umode_add;
2993 action_requested = True;
2996 if (!strcmp ("--delmode", argv[i]))
2998 umode_t *m = calloc (1, sizeof (umode_t));
3000 if (i+2 >= argc) argerr ("%s requires two arguments\n", argv[i]);
3001 set_name (&m->output, argv[++i], name_string|name_xid);
3002 set_name (&m->name, argv[++i], name_string|name_xid);
3003 m->action = umode_delete;
3007 action_requested = True;
3010 if (!strcmp ("--listproviders", argv[i]))
3012 list_providers = True;
3013 action_requested = True;
3016 if (!strcmp("--setprovideroutputsource", argv[i]))
3018 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
3019 set_name (&provider_name, argv[i], name_string|name_xid|name_index);
3021 set_name_xid (&output_source_provider_name, 0);
3023 set_name (&output_source_provider_name, argv[i], name_string|name_xid|name_index);
3024 action_requested = True;
3025 provsetoutsource = True;
3028 if (!strcmp("--setprovideroffloadsink", argv[i]))
3030 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
3031 set_name (&provider_name, argv[i], name_string|name_xid|name_index);
3033 set_name_xid (&offload_sink_provider_name, 0);
3035 set_name (&offload_sink_provider_name, argv[i], name_string|name_xid|name_index);
3036 action_requested = True;
3037 provsetoffsink = True;
3041 argerr ("unrecognized option '%s'\n", argv[i]);
3043 if (!action_requested)
3048 if (setit && !setit_1_2)
3052 printf("xrandr program version " VERSION "\n");
3054 dpy = XOpenDisplay (display_name);
3057 fprintf (stderr, "Can't open display %s\n", XDisplayName(display_name));
3061 screen = DefaultScreen (dpy);
3062 if (screen >= ScreenCount (dpy)) {
3063 fprintf (stderr, "Invalid screen number %d (display has %d)\n",
3064 screen, ScreenCount (dpy));
3068 root = RootWindow (dpy, screen);
3070 if (!XRRQueryExtension (dpy, &event_base, &error_base) ||
3071 !XRRQueryVersion (dpy, &major, &minor))
3073 fprintf (stderr, "RandR extension missing\n");
3076 if (major > 1 || (major == 1 && minor >= 2))
3078 if (major > 1 || (major == 1 && minor >= 3))
3080 if (major > 1 || (major == 1 && minor >= 4))
3083 if (has_1_2 && modeit)
3087 get_screen (current);
3091 for (m = umodes; m; m = m->next)
3096 switch (m->action) {
3098 XRRCreateMode (dpy, root, &m->mode);
3101 e = find_mode (&m->name, 0);
3103 fatal ("cannot find mode \"%s\"\n", m->name.string);
3104 XRRDestroyMode (dpy, e->id);
3107 o = find_output (&m->output);
3109 fatal ("cannot find output \"%s\"\n", m->output.string);
3110 e = find_mode (&m->name, 0);
3112 fatal ("cannot find mode \"%s\"\n", m->name.string);
3113 XRRAddOutputMode (dpy, o->output.xid, e->id);
3116 o = find_output (&m->output);
3118 fatal ("cannot find output \"%s\"\n", m->output.string);
3119 e = find_mode (&m->name, 0);
3121 fatal ("cannot find mode \"%s\"\n", m->name.string);
3122 XRRDeleteOutputMode (dpy, o->output.xid, e->id);
3132 if (has_1_2 && propit)
3136 get_screen (current);
3140 for (output = all_outputs; output; output = output->next)
3142 output_prop_t *prop;
3144 for (prop = output->props; prop; prop = prop->next)
3146 Atom name = XInternAtom (dpy, prop->name, False);
3149 unsigned char *data, *malloced_data = NULL;
3152 unsigned long ulong_value;
3153 unsigned char *prop_data;
3155 unsigned long nitems, bytes_after;
3157 XRRPropertyInfo *propinfo;
3159 type = AnyPropertyType;
3161 if (XRRGetOutputProperty (dpy, output->output.xid, name,
3162 0, 100, False, False,
3164 &actual_type, &actual_format,
3165 &nitems, &bytes_after, &prop_data) == Success &&
3167 (propinfo = XRRQueryOutputProperty(dpy, output->output.xid,
3171 format = actual_format;
3174 malloced_data = property_values_from_string
3175 (prop->value, type, actual_format, &nelements);
3179 data = malloced_data;
3181 format = actual_format;
3183 else if (type == AnyPropertyType &&
3184 (sscanf (prop->value, "%d", &int_value) == 1 ||
3185 sscanf (prop->value, "0x%x", &int_value) == 1))
3188 ulong_value = int_value;
3189 data = (unsigned char *) &ulong_value;
3193 else if ((type == XA_ATOM))
3195 ulong_value = XInternAtom (dpy, prop->value, False);
3196 data = (unsigned char *) &ulong_value;
3199 else if ((type == XA_STRING || type == AnyPropertyType))
3202 data = (unsigned char *) prop->value;
3203 nelements = strlen (prop->value);
3208 XRRChangeOutputProperty (dpy, output->output.xid,
3209 name, type, format, PropModeReplace,
3211 free (malloced_data);
3220 if (provsetoutsource)
3222 provider_t *provider, *source;
3225 fatal ("--setprovideroutputsource requires RandR 1.4\n");
3227 get_screen (current);
3230 provider = find_provider (&provider_name);
3231 source = find_provider(&output_source_provider_name);
3233 XRRSetProviderOutputSource(dpy, provider->provider.xid, source->provider.xid);
3237 provider_t *provider, *sink;
3240 fatal ("--setprovideroffloadsink requires RandR 1.4\n");
3242 get_screen (current);
3245 provider = find_provider (&provider_name);
3246 sink = find_provider(&offload_sink_provider_name);
3248 XRRSetProviderOffloadSink(dpy, provider->provider.xid, sink->provider.xid);
3252 get_screen (current);
3261 * Assign outputs to crtcs
3266 * Mark changing crtcs
3268 mark_changing_crtcs ();
3271 * If an output was specified to track dpi, use it
3273 if (dpi_output_name)
3275 output_t *dpi_output = find_output_by_name (dpi_output_name);
3276 XRROutputInfo *output_info;
3277 XRRModeInfo *mode_info;
3279 fatal ("Cannot find output %s\n", dpi_output_name);
3280 output_info = dpi_output->output_info;
3281 mode_info = dpi_output->mode_info;
3282 if (output_info && mode_info && output_info->mm_height)
3285 * When this output covers the whole screen, just use
3286 * the known physical size
3288 if (fb_width == mode_info->width &&
3289 fb_height == mode_info->height)
3291 fb_width_mm = output_info->mm_width;
3292 fb_height_mm = output_info->mm_height;
3296 dpi = (25.4 * mode_info->height) / output_info->mm_height;
3302 * Compute physical screen size
3304 if (fb_width_mm == 0 || fb_height_mm == 0)
3306 if (fb_width != DisplayWidth (dpy, screen) ||
3307 fb_height != DisplayHeight (dpy, screen) || dpi != 0.0)
3310 dpi = (25.4 * DisplayHeight (dpy, screen)) / DisplayHeightMM(dpy, screen);
3312 fb_width_mm = (25.4 * fb_width) / dpi;
3313 fb_height_mm = (25.4 * fb_height) / dpi;
3317 fb_width_mm = DisplayWidthMM (dpy, screen);
3318 fb_height_mm = DisplayHeightMM (dpy, screen);
3328 * Set gamma on crtc's that belong to the outputs.
3333 * Now apply all of the changes
3340 if (query_1_2 || (query && has_1_2 && !query_1))
3345 #define ModeShown 0x80000000
3347 get_screen (current);
3351 printf ("Screen %d: minimum %d x %d, current %d x %d, maximum %d x %d\n",
3352 screen, minWidth, minHeight,
3353 DisplayWidth (dpy, screen), DisplayHeight(dpy, screen),
3354 maxWidth, maxHeight);
3356 for (output = all_outputs; output; output = output->next)
3358 XRROutputInfo *output_info = output->output_info;
3359 crtc_t *cur_crtc = output->crtc_info;
3360 XRRCrtcInfo *crtc_info = cur_crtc ? cur_crtc->crtc_info : NULL;
3361 XRRModeInfo *cur_mode = output->mode_info;
3365 Rotation rotations = output_rotations (output);
3367 printf ("%s %s", output_info->name, connection[output_info->connection]);
3368 if (output->primary) {
3374 printf (" %dx%d+%d+%d",
3375 crtc_info->width, crtc_info->height,
3376 crtc_info->x, crtc_info->y);
3378 printf (" %dx%d+%d+%d",
3379 cur_mode->width, cur_mode->height, output->x,
3383 printf (" (0x%x)", (int)cur_mode->id);
3384 if (output->rotation != RR_Rotate_0 || verbose)
3387 rotation_name (output->rotation));
3388 if (output->rotation & (RR_Reflect_X|RR_Reflect_Y))
3389 printf (" %s", reflection_name (output->rotation));
3392 if (rotations != RR_Rotate_0 || verbose)
3396 for (i = 0; i < 4; i ++) {
3397 if ((rotations >> i) & 1) {
3398 if (!first) printf (" "); first = False;
3399 printf("%s", direction[i]);
3402 if (rotations & RR_Reflect_X)
3404 if (!first) printf (" "); first = False;
3407 if (rotations & RR_Reflect_Y)
3409 if (!first) printf (" ");
3417 printf (" %dmm x %dmm",
3418 (int)output_info->mm_width, (int)output_info->mm_height);
3421 if (cur_crtc && cur_crtc->panning_info &&
3422 cur_crtc->panning_info->width > 0)
3424 XRRPanning *pan = cur_crtc->panning_info;
3425 printf (" panning %dx%d+%d+%d",
3426 pan->width, pan->height, pan->left, pan->top);
3427 if ((pan->track_width != 0 &&
3428 (pan->track_left != pan->left ||
3429 pan->track_width != pan->width ||
3430 pan->border_left != 0 ||
3431 pan->border_right != 0)) ||
3432 (pan->track_height != 0 &&
3433 (pan->track_top != pan->top ||
3434 pan->track_height != pan->height ||
3435 pan->border_top != 0 ||
3436 pan->border_bottom != 0)))
3437 printf (" tracking %dx%d+%d+%d border %d/%d/%d/%d",
3438 pan->track_width, pan->track_height,
3439 pan->track_left, pan->track_top,
3440 pan->border_left, pan->border_top,
3441 pan->border_right, pan->border_bottom);
3447 printf ("\tIdentifier: 0x%x\n", (int)output->output.xid);
3448 printf ("\tTimestamp: %d\n", (int)output_info->timestamp);
3449 printf ("\tSubpixel: %s\n", order[output_info->subpixel_order]);
3450 if (output->gamma.red != 0.0 && output->gamma.green != 0.0 && output->gamma.blue != 0.0) {
3451 printf ("\tGamma: %#.2g:%#.2g:%#.2g\n",
3452 output->gamma.red, output->gamma.green, output->gamma.blue);
3453 printf ("\tBrightness: %#.2g\n", output->brightness);
3455 printf ("\tClones: ");
3456 for (j = 0; j < output_info->nclone; j++)
3458 output_t *clone = find_output_by_xid (output_info->clones[j]);
3460 if (clone) printf (" %s", clone->output.string);
3463 if (output->crtc_info)
3464 printf ("\tCRTC: %d\n", output->crtc_info->crtc.index);
3465 printf ("\tCRTCs: ");
3466 for (j = 0; j < output_info->ncrtc; j++)
3468 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[j]);
3470 printf (" %d", crtc->crtc.index);
3473 if (output->crtc_info && output->crtc_info->panning_info) {
3474 XRRPanning *pan = output->crtc_info->panning_info;
3475 printf ("\tPanning: %dx%d+%d+%d\n",
3476 pan->width, pan->height, pan->left, pan->top);
3477 printf ("\tTracking: %dx%d+%d+%d\n",
3478 pan->track_width, pan->track_height,
3479 pan->track_left, pan->track_top);
3480 printf ("\tBorder: %d/%d/%d/%d\n",
3481 pan->border_left, pan->border_top,
3482 pan->border_right, pan->border_bottom);
3489 printf ("\tTransform: ");
3490 for (y = 0; y < 3; y++)
3492 for (x = 0; x < 3; x++)
3493 printf (" %f", XFixedToDouble (output->transform.transform.matrix[y][x]));
3497 if (output->transform.filter)
3498 printf ("\n\t filter: %s", output->transform.filter);
3501 if (verbose || properties)
3503 props = XRRListOutputProperties (dpy, output->output.xid,
3505 for (j = 0; j < nprop; j++) {
3506 unsigned char *prop;
3508 unsigned long nitems, bytes_after;
3510 XRRPropertyInfo *propinfo;
3511 char *atom_name = XGetAtomName (dpy, props[j]);
3512 Bool is_edid = strcmp (atom_name, "EDID") == 0;
3513 int bytes_per_item, k;
3515 XRRGetOutputProperty (dpy, output->output.xid, props[j],
3516 0, 100, False, False,
3518 &actual_type, &actual_format,
3519 &nitems, &bytes_after, &prop);
3521 propinfo = XRRQueryOutputProperty(dpy, output->output.xid,
3524 bytes_per_item = actual_format / 8;
3526 printf ("\t%s: ", atom_name);
3533 for (k = 0; k < nitems; k++)
3542 print_output_property_value (is_edid, actual_format,
3544 prop + (k * bytes_per_item));
3552 if (propinfo->range && propinfo->num_values > 0)
3554 printf ("\t\trange%s: ",
3555 (propinfo->num_values == 2) ? "" : "s");
3556 for (k = 0; k < propinfo->num_values / 2; k++)
3559 print_output_property_value (False, 32, actual_type,
3560 (unsigned char *) &(propinfo->values[k * 2]));
3562 print_output_property_value (False, 32, actual_type,
3563 (unsigned char *) &(propinfo->values[k * 2 + 1]));
3565 if (k < propinfo->num_values / 2 - 1)
3570 if (!propinfo->range && propinfo->num_values > 0)
3572 printf ("\t\tsupported: ");
3573 for (k = 0; k < propinfo->num_values; k++)
3575 print_output_property_value (False, 32, actual_type,
3576 (unsigned char *) &(propinfo->values[k]));
3577 if (k < propinfo->num_values - 1)
3589 for (j = 0; j < output_info->nmode; j++)
3591 XRRModeInfo *mode = find_mode_by_xid (output_info->modes[j]);
3594 printf (" %s (0x%x) %6.1fMHz",
3595 mode->name, (int)mode->id,
3596 (double)mode->dotClock / 1000000.0);
3597 for (f = 0; mode_flags[f].flag; f++)
3598 if (mode->modeFlags & mode_flags[f].flag)
3599 printf (" %s", mode_flags[f].string);
3600 if (mode == output->mode_info)
3601 printf (" *current");
3602 if (j < output_info->npreferred)
3603 printf (" +preferred");
3605 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
3606 mode->width, mode->hSyncStart, mode->hSyncEnd,
3607 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
3608 printf (" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
3609 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
3610 mode_refresh (mode));
3611 mode->modeFlags |= ModeShown;
3616 mode_shown = calloc (output_info->nmode, sizeof (Bool));
3617 if (!mode_shown) fatal ("out of memory\n");
3618 for (j = 0; j < output_info->nmode; j++)
3620 XRRModeInfo *jmode, *kmode;
3623 if (mode_shown[j]) continue;
3625 jmode = find_mode_by_xid (output_info->modes[j]);
3627 printf (" %-12s", jmode->name);
3628 for (k = j; k < output_info->nmode; k++)
3630 if (mode_shown[k]) continue;
3631 kmode = find_mode_by_xid (output_info->modes[k]);
3632 if (strcmp (jmode->name, kmode->name) != 0) continue;
3633 mode_shown[k] = True;
3634 kmode->modeFlags |= ModeShown;
3635 printf (" %6.1f", mode_refresh (kmode));
3636 if (kmode == output->mode_info)
3640 if (k < output_info->npreferred)
3650 for (m = 0; m < res->nmode; m++)
3652 XRRModeInfo *mode = &res->modes[m];
3654 if (!(mode->modeFlags & ModeShown))
3656 printf (" %s (0x%x) %6.1fMHz\n",
3657 mode->name, (int)mode->id,
3658 (double)mode->dotClock / 1000000.0);
3659 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
3660 mode->width, mode->hSyncStart, mode->hSyncEnd,
3661 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
3662 printf (" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
3663 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
3664 mode_refresh (mode));
3669 if (list_providers) {
3673 printf ("RandR 1.4 not supported\n");
3677 get_screen (current);
3683 printf("Providers: number : %d\n", num_providers);
3685 for (j = 0; j < num_providers; j++) {
3686 provider_t *provider = &providers[j];
3687 XRRProviderInfo *info = provider->info;
3689 printf("Provider %d: id: 0x%x cap: 0x%x", j, (int)provider->provider.xid, info->capabilities);
3690 for (k = 0; k < 4; k++)
3691 if (info->capabilities & (1 << k))
3692 printf(", %s", capability_name(1<<k));
3694 printf(" crtcs: %d outputs: %d associated providers: %d name:%s\n", info->ncrtcs, info->noutputs, info->nassociatedproviders, info->name);
3699 sc = XRRGetScreenInfo (dpy, root);
3704 current_size = XRRConfigCurrentConfiguration (sc, ¤t_rotation);
3706 sizes = XRRConfigSizes(sc, &nsize);
3708 if (have_pixel_size) {
3709 for (size = 0; size < nsize; size++)
3711 if (sizes[size].width == width && sizes[size].height == height)
3714 if (size >= nsize) {
3716 "Size %dx%d not found in available modes\n", width, height);
3721 size = current_size;
3722 else if (size >= nsize) {
3724 "Size index %d is too large, there are only %d sizes\n",
3731 for (rot = 0; rot < 4; rot++)
3732 if (1 << rot == (current_rotation & 0xf))
3736 current_rate = XRRConfigCurrentRate (sc);
3740 if (size == current_size)
3741 rate = current_rate;
3747 rates = XRRConfigRates (sc, size, &nrate);
3748 for (i = 0; i < nrate; i++)
3749 if (rate == rates[i])
3752 fprintf (stderr, "Rate %.1f Hz not available for this size\n", rate);
3758 int major_version, minor_version;
3759 XRRQueryVersion (dpy, &major_version, &minor_version);
3760 printf("Server reports RandR version %d.%d\n",
3761 major_version, minor_version);
3764 if (query || query_1) {
3765 printf(" SZ: Pixels Physical Refresh\n");
3766 for (i = 0; i < nsize; i++) {
3769 printf ("%c%-2d %5d x %-5d (%4dmm x%4dmm )",
3770 i == current_size ? '*' : ' ',
3771 i, sizes[i].width, sizes[i].height,
3772 sizes[i].mwidth, sizes[i].mheight);
3773 rates = XRRConfigRates (sc, i, &nrate);
3774 if (nrate) printf (" ");
3775 for (j = 0; j < nrate; j++)
3777 i == current_size && rates[j] == current_rate ? '*' : ' ',
3784 Rotation rotations = XRRConfigRotations(sc, ¤t_rotation);
3787 printf("Current rotation - %s\n",
3788 rotation_name (current_rotation));
3790 printf("Current reflection - %s\n",
3791 reflection_name (current_rotation));
3793 printf ("Rotations possible - ");
3794 for (i = 0; i < 4; i ++) {
3795 if ((rotations >> i) & 1) printf("%s ", direction[i]);
3799 printf ("Reflections possible - ");
3800 if (rotations & (RR_Reflect_X|RR_Reflect_Y))
3802 if (rotations & RR_Reflect_X) printf ("X Axis ");
3803 if (rotations & RR_Reflect_Y) printf ("Y Axis");
3812 printf("Setting size to %d, rotation to %s\n", size, direction[rot]);
3814 printf ("Setting reflection on ");
3817 if (reflection & RR_Reflect_X) printf ("X Axis ");
3818 if (reflection & RR_Reflect_Y) printf ("Y Axis");
3821 printf ("neither axis");
3824 if (reflection & RR_Reflect_X) printf("Setting reflection on X axis\n");
3826 if (reflection & RR_Reflect_Y) printf("Setting reflection on Y axis\n");
3829 /* we should test configureNotify on the root window */
3830 XSelectInput (dpy, root, StructureNotifyMask);
3832 if (setit && !dryrun) XRRSelectInput (dpy, root,
3833 RRScreenChangeNotifyMask);
3834 if (setit && !dryrun) {
3835 Rotation rotation = 1 << rot;
3836 status = XRRSetScreenConfigAndRate (dpy, sc, root, (SizeID) size,
3837 (Rotation) (rotation | reflection),
3841 if (setit && !dryrun && status == RRSetConfigFailed) {
3842 printf ("Failed to change the screen configuration!\n");
3846 if (verbose && setit && !dryrun && size != current_size) {
3847 if (status == RRSetConfigSuccess)
3849 Bool seen_screen = False;
3850 while (!seen_screen) {
3852 XNextEvent(dpy, (XEvent *) &event);
3854 printf ("Event received, type = %d\n", event.type);
3855 /* update Xlib's knowledge of the event */
3856 XRRUpdateConfiguration (&event);
3857 if (event.type == ConfigureNotify)
3858 printf("Received ConfigureNotify Event!\n");
3860 switch (event.type - event_base) {
3861 case RRScreenChangeNotify:
3862 sce = (XRRScreenChangeNotifyEvent *) &event;
3864 printf("Got a screen change notify event!\n");
3865 printf(" window = %d\n root = %d\n size_index = %d\n rotation %d\n",
3866 (int) sce->window, (int) sce->root,
3867 sce->size_index, sce->rotation);
3868 printf(" timestamp = %ld, config_timestamp = %ld\n",
3869 sce->timestamp, sce->config_timestamp);
3870 printf(" Rotation = %x\n", sce->rotation);
3871 printf(" %d X %d pixels, %d X %d mm\n",
3872 sce->width, sce->height, sce->mwidth, sce->mheight);
3873 printf("Display width %d, height %d\n",
3874 DisplayWidth(dpy, screen), DisplayHeight(dpy, screen));
3875 printf("Display widthmm %d, heightmm %d\n",
3876 DisplayWidthMM(dpy, screen), DisplayHeightMM(dpy, screen));
3877 spo = sce->subpixel_order;
3878 if ((spo < 0) || (spo > 5))
3879 printf ("Unknown subpixel order, value = %d\n", spo);
3880 else printf ("new Subpixel rendering model is %s\n", order[spo]);
3884 if (event.type != ConfigureNotify)
3885 printf("unknown event received, type = %d!\n", event.type);
3890 XRRFreeScreenConfigInfo(sc);