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(" --brightness <value>\n");
143 printf(" --primary\n");
144 printf(" --noprimary\n");
145 printf(" --newmode <name> <clock MHz>\n");
146 printf(" <hdisp> <hsync-start> <hsync-end> <htotal>\n");
147 printf(" <vdisp> <vsync-start> <vsync-end> <vtotal>\n");
148 printf(" [flags...]\n");
149 printf(" Valid flags: +HSync -HSync +VSync -VSync\n");
150 printf(" +CSync -CSync CSync Interlace DoubleScan\n");
151 printf(" --rmmode <name>\n");
152 printf(" --addmode <output> <name>\n");
153 printf(" --delmode <output> <name>\n");
154 printf(" --listproviders\n");
155 printf(" --setprovideroutputsource <prov-xid> <source-xid>\n");
156 printf(" --setprovideroffloadsink <prov-xid> <sink-xid>\n");
159 static void _X_NORETURN _X_ATTRIBUTE_PRINTF(1,2)
160 fatal (const char *format, ...)
164 va_start (ap, format);
165 fprintf (stderr, "%s: ", program_name);
166 vfprintf (stderr, format, ap);
172 static void _X_ATTRIBUTE_PRINTF(1,2)
173 warning (const char *format, ...)
177 va_start (ap, format);
178 fprintf (stderr, "%s: ", program_name);
179 vfprintf (stderr, format, ap);
183 static void _X_NORETURN _X_ATTRIBUTE_PRINTF(1,2)
184 argerr (const char *format, ...)
188 va_start (ap, format);
189 fprintf (stderr, "%s: ", program_name);
190 vfprintf (stderr, format, ap);
191 fprintf (stderr, "Try '%s --help' for more information.\n", program_name);
197 /* Because fmin requires C99 suppport */
198 static inline double dmin (double x, double y)
200 return x < y ? x : y;
204 rotation_name (Rotation rotation)
208 if ((rotation & 0xf) == 0)
210 for (i = 0; i < 4; i++)
211 if (rotation & (1 << i))
213 return "invalid rotation";
217 reflection_name (Rotation rotation)
219 rotation &= (RR_Reflect_X|RR_Reflect_Y);
227 case RR_Reflect_X|RR_Reflect_Y:
228 return "X and Y axis";
230 return "invalid reflection";
234 capability_name (int cap_bit)
237 case RR_Capability_SourceOutput:
238 return "Source Output";
239 case RR_Capability_SinkOutput:
240 return "Sink Output";
241 case RR_Capability_SourceOffload:
242 return "Source Offload";
243 case RR_Capability_SinkOffload:
244 return "Sink Offload";
246 return "invalid capability";
249 typedef enum _relation {
258 int x, y, width, height;
269 typedef enum _changes {
271 changes_crtc = (1 << 0),
272 changes_mode = (1 << 1),
273 changes_relation = (1 << 2),
274 changes_position = (1 << 3),
275 changes_rotation = (1 << 4),
276 changes_reflection = (1 << 5),
277 changes_automatic = (1 << 6),
278 changes_refresh = (1 << 7),
279 changes_property = (1 << 8),
280 changes_transform = (1 << 9),
281 changes_panning = (1 << 10),
282 changes_gamma = (1 << 11),
283 changes_primary = (1 << 12),
286 typedef enum _name_kind {
288 name_string = (1 << 0),
290 name_index = (1 << 2),
291 name_preferred = (1 << 3),
301 typedef struct _crtc crtc_t;
302 typedef struct _output output_t;
303 typedef struct _transform transform_t;
304 typedef struct _umode umode_t;
305 typedef struct _output_prop output_prop_t;
306 typedef struct _provider provider_t;
309 XTransform transform;
318 XRRCrtcInfo *crtc_info;
320 XRRModeInfo *mode_info;
321 XRRPanning *panning_info;
327 transform_t current_transform, pending_transform;
330 struct _output_prop {
331 struct _output_prop *next;
337 struct _output *next;
341 output_prop_t *props;
344 XRROutputInfo *output_info;
348 crtc_t *current_crtc_info;
352 XRRModeInfo *mode_info;
365 int scale_from_w, scale_from_h;
366 transform_t transform;
381 typedef enum _umode_action {
382 umode_create, umode_destroy, umode_add, umode_delete
389 umode_action_t action;
397 XRRProviderInfo *info;
400 static const char *connection[3] = {
403 "unknown connection"};
405 #define OUTPUT_NAME 1
409 #define CRTC_INDEX 0x40000000
418 static output_t *all_outputs = NULL;
419 static output_t **all_outputs_tail = &all_outputs;
420 static crtc_t *crtcs;
421 static provider_t *providers;
422 static umode_t *umodes;
423 static int num_crtcs, num_providers;
424 static XRRScreenResources *res;
425 static int fb_width = 0, fb_height = 0;
426 static int fb_width_mm = 0, fb_height_mm = 0;
427 static double dpi = 0;
428 static char *dpi_output_name = NULL;
429 static Bool dryrun = False;
430 static int minWidth, maxWidth, minHeight, maxHeight;
431 static Bool has_1_2 = False;
432 static Bool has_1_3 = False;
433 static Bool has_1_4 = False;
434 static name_t provider_name, output_source_provider_name, offload_sink_provider_name;
437 mode_height (XRRModeInfo *mode_info, Rotation rotation)
439 switch (rotation & 0xf) {
442 return mode_info->height;
445 return mode_info->width;
452 mode_width (XRRModeInfo *mode_info, Rotation rotation)
454 switch (rotation & 0xf) {
457 return mode_info->width;
460 return mode_info->height;
467 transform_point (XTransform *transform, double *xp, double *yp)
477 for (j = 0; j < 3; j++)
480 for (i = 0; i < 3; i++)
481 v += (XFixedToDouble (transform->matrix[j][i]) * vector[i]);
486 for (j = 0; j < 2; j++) {
487 vector[j] = result[j] / result[2];
488 if (vector[j] > 32767 || vector[j] < -32767)
497 path_bounds (XTransform *transform, point_t *points, int npoints, box_t *box)
502 for (i = 0; i < npoints; i++) {
506 transform_point (transform, &x, &y);
507 point.x1 = floor (x);
508 point.y1 = floor (y);
514 if (point.x1 < box->x1) box->x1 = point.x1;
515 if (point.y1 < box->y1) box->y1 = point.y1;
516 if (point.x2 > box->x2) box->x2 = point.x2;
517 if (point.y2 > box->y2) box->y2 = point.y2;
523 mode_geometry (XRRModeInfo *mode_info, Rotation rotation,
524 XTransform *transform,
528 int width = mode_width (mode_info, rotation);
529 int height = mode_height (mode_info, rotation);
539 path_bounds (transform, rect, 4, bounds);
542 /* v refresh frequency in Hz */
544 mode_refresh (XRRModeInfo *mode_info)
547 unsigned int vTotal = mode_info->vTotal;
549 if (mode_info->modeFlags & RR_DoubleScan) {
550 /* doublescan doubles the number of lines */
554 if (mode_info->modeFlags & RR_Interlace) {
555 /* interlace splits the frame into two fields */
556 /* the field rate is what is typically reported by monitors */
560 if (mode_info->hTotal && vTotal)
561 rate = ((double) mode_info->dotClock /
562 ((double) mode_info->hTotal * (double) vTotal));
568 /* h sync frequency in Hz */
570 mode_hsync (XRRModeInfo *mode_info)
574 if (mode_info->hTotal)
575 rate = (double) mode_info->dotClock / (double) mode_info->hTotal;
582 init_name (name_t *name)
584 name->kind = name_none;
588 set_name_string (name_t *name, char *string)
590 name->kind |= name_string;
591 name->string = string;
595 set_name_xid (name_t *name, XID xid)
597 name->kind |= name_xid;
602 set_name_index (name_t *name, int idx)
604 name->kind |= name_index;
609 set_name_preferred (name_t *name)
611 name->kind |= name_preferred;
615 set_name_all (name_t *name, name_t *old)
617 if (old->kind & name_xid)
618 name->xid = old->xid;
619 if (old->kind & name_string)
620 name->string = old->string;
621 if (old->kind & name_index)
622 name->index = old->index;
623 name->kind |= old->kind;
627 set_name (name_t *name, char *string, name_kind_t valid)
629 unsigned int xid; /* don't make it XID (which is unsigned long):
630 scanf() takes unsigned int */
633 if ((valid & name_xid) && sscanf (string, "0x%x", &xid) == 1)
634 set_name_xid (name, xid);
635 else if ((valid & name_index) && sscanf (string, "%d", &idx) == 1)
636 set_name_index (name, idx);
637 else if (valid & name_string)
638 set_name_string (name, string);
640 argerr ("invalid name '%s'\n", string);
644 print_name (const name_t *name)
646 name_kind_t kind = name->kind;
648 if ((kind & name_xid)) return printf("XID 0x%x", (unsigned int)name->xid);
649 else if ((kind & name_string)) return printf("name %s", name->string);
650 else if ((kind & name_index)) return printf("index %d", name->index);
651 else return printf("unknown name");
655 init_transform (transform_t *transform)
658 memset (&transform->transform, '\0', sizeof (transform->transform));
659 for (x = 0; x < 3; x++)
660 transform->transform.matrix[x][x] = XDoubleToFixed (1.0);
661 transform->filter = "";
662 transform->nparams = 0;
663 transform->params = NULL;
667 set_transform (transform_t *dest,
668 XTransform *transform,
673 dest->transform = *transform;
674 /* note: this string is leaked */
675 dest->filter = strdup (filter);
676 dest->nparams = nparams;
677 dest->params = malloc (nparams * sizeof (XFixed));
678 memcpy (dest->params, params, nparams * sizeof (XFixed));
682 copy_transform (transform_t *dest, transform_t *src)
684 set_transform (dest, &src->transform,
685 src->filter, src->params, src->nparams);
689 equal_transform (transform_t *a, transform_t *b)
691 if (memcmp (&a->transform, &b->transform, sizeof (XTransform)) != 0)
693 if (strcmp (a->filter, b->filter) != 0)
695 if (a->nparams != b->nparams)
697 if (memcmp (a->params, b->params, a->nparams * sizeof (XFixed)) != 0)
705 output_t *output = calloc (1, sizeof (output_t));
708 fatal ("out of memory\n");
710 output->found = False;
711 output->brightness = 1.0;
712 *all_outputs_tail = output;
713 all_outputs_tail = &output->next;
718 find_output (name_t *name)
722 for (output = all_outputs; output; output = output->next)
724 name_kind_t common = name->kind & output->output.kind;
726 if ((common & name_xid) && name->xid == output->output.xid)
728 if ((common & name_string) && !strcmp (name->string, output->output.string))
730 if ((common & name_index) && name->index == output->output.index)
737 find_output_by_xid (RROutput output)
741 init_name (&output_name);
742 set_name_xid (&output_name, output);
743 return find_output (&output_name);
747 find_output_by_name (char *name)
751 init_name (&output_name);
752 set_name_string (&output_name, name);
753 return find_output (&output_name);
757 find_crtc (name_t *name)
762 for (c = 0; c < num_crtcs; c++)
767 common = name->kind & crtc->crtc.kind;
769 if ((common & name_xid) && name->xid == crtc->crtc.xid)
771 if ((common & name_string) && !strcmp (name->string, crtc->crtc.string))
773 if ((common & name_index) && name->index == crtc->crtc.index)
781 find_crtc_by_xid (RRCrtc crtc)
785 init_name (&crtc_name);
786 set_name_xid (&crtc_name, crtc);
787 return find_crtc (&crtc_name);
791 find_mode (name_t *name, double refresh)
794 XRRModeInfo *best = NULL;
797 for (m = 0; m < res->nmode; m++)
799 XRRModeInfo *mode = &res->modes[m];
800 if ((name->kind & name_xid) && name->xid == mode->id)
805 if ((name->kind & name_string) && !strcmp (name->string, mode->name))
810 dist = fabs (mode_refresh (mode) - refresh);
813 if (!best || dist < bestDist)
824 find_mode_by_xid (RRMode mode)
828 init_name (&mode_name);
829 set_name_xid (&mode_name, mode);
830 return find_mode (&mode_name, 0);
835 find_mode_by_name (char *name)
838 init_name (&mode_name);
839 set_name_string (&mode_name, name);
840 return find_mode (&mode_name, 0);
846 find_mode_for_output (output_t *output, name_t *name)
848 XRROutputInfo *output_info = output->output_info;
850 XRRModeInfo *best = NULL;
853 for (m = 0; m < output_info->nmode; m++)
857 mode = find_mode_by_xid (output_info->modes[m]);
859 if ((name->kind & name_xid) && name->xid == mode->id)
864 if ((name->kind & name_string) && !strcmp (name->string, mode->name))
868 /* Stay away from doublescan modes unless refresh rate is specified. */
869 if (!output->refresh && (mode->modeFlags & RR_DoubleScan))
873 dist = fabs (mode_refresh (mode) - output->refresh);
876 if (!best || dist < bestDist)
887 preferred_mode (output_t *output)
889 XRROutputInfo *output_info = output->output_info;
896 for (m = 0; m < output_info->nmode; m++)
898 XRRModeInfo *mode_info = find_mode_by_xid (output_info->modes[m]);
901 if (m < output_info->npreferred)
903 else if (output_info->mm_height)
904 dist = (1000 * DisplayHeight(dpy, screen) / DisplayHeightMM(dpy, screen) -
905 1000 * mode_info->height / output_info->mm_height);
907 dist = DisplayHeight(dpy, screen) - mode_info->height;
909 if (dist < 0) dist = -dist;
910 if (!best || dist < bestDist)
920 output_can_use_crtc (output_t *output, crtc_t *crtc)
922 XRROutputInfo *output_info = output->output_info;
925 for (c = 0; c < output_info->ncrtc; c++)
926 if (output_info->crtcs[c] == crtc->crtc.xid)
932 output_can_use_mode (output_t *output, XRRModeInfo *mode)
934 XRROutputInfo *output_info = output->output_info;
937 for (m = 0; m < output_info->nmode; m++)
938 if (output_info->modes[m] == mode->id)
944 crtc_can_use_rotation (crtc_t *crtc, Rotation rotation)
946 Rotation rotations = crtc->crtc_info->rotations;
947 Rotation dir = rotation & (RR_Rotate_0|RR_Rotate_90|RR_Rotate_180|RR_Rotate_270);
948 Rotation reflect = rotation & (RR_Reflect_X|RR_Reflect_Y);
949 if (((rotations & dir) != 0) && ((rotations & reflect) == reflect))
956 crtc_can_use_transform (crtc_t *crtc, XTransform *transform)
960 XRRQueryVersion (dpy, &major, &minor);
961 if (major > 1 || (major == 1 && minor >= 3))
968 * Report only rotations that are supported by all crtcs
971 output_rotations (output_t *output)
974 Rotation rotation = RR_Rotate_0;
975 XRROutputInfo *output_info = output->output_info;
978 for (c = 0; c < output_info->ncrtc; c++)
980 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
984 rotation = crtc->crtc_info->rotations;
987 rotation &= crtc->crtc_info->rotations;
994 output_can_use_rotation (output_t *output, Rotation rotation)
996 XRROutputInfo *output_info = output->output_info;
999 /* make sure all of the crtcs can use this rotation.
1000 * yes, this is not strictly necessary, but it is
1001 * simpler,and we expect most drivers to either
1002 * support rotation everywhere or nowhere
1004 for (c = 0; c < output_info->ncrtc; c++)
1006 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
1007 if (crtc && !crtc_can_use_rotation (crtc, rotation))
1014 output_is_primary(output_t *output)
1017 return XRRGetOutputPrimary(dpy, root) == output->output.xid;
1021 /* Returns the index of the last value in an array < 0xffff */
1023 find_last_non_clamped(CARD16 array[], int size) {
1025 for (i = size - 1; i > 0; i--) {
1026 if (array[i] < 0xffff)
1033 set_gamma_info(output_t *output)
1035 XRRCrtcGamma *crtc_gamma;
1036 double i1, v1, i2, v2;
1037 int size, middle, last_best, last_red, last_green, last_blue;
1040 if (!output->crtc_info)
1043 size = XRRGetCrtcGammaSize(dpy, output->crtc_info->crtc.xid);
1045 warning("Failed to get size of gamma for output %s\n", output->output.string);
1049 crtc_gamma = XRRGetCrtcGamma(dpy, output->crtc_info->crtc.xid);
1051 warning("Failed to get gamma for output %s\n", output->output.string);
1056 * Here is a bit tricky because gamma is a whole curve for each
1057 * color. So, typically, we need to represent 3 * 256 values as 3 + 1
1058 * values. Therefore, we approximate the gamma curve (v) by supposing
1059 * it always follows the way we set it: a power function (i^g)
1060 * multiplied by a brightness (b).
1062 * so g = (ln(v) - ln(b))/ln(i)
1063 * and b can be found using two points (v1,i1) and (v2, i2):
1064 * b = e^((ln(v2)*ln(i1) - ln(v1)*ln(i2))/ln(i1/i2))
1065 * For the best resolution, we select i2 at the highest place not
1066 * clamped and i1 at i2/2. Note that if i2 = 1 (as in most normal
1067 * cases), then b = v2.
1069 last_red = find_last_non_clamped(crtc_gamma->red, size);
1070 last_green = find_last_non_clamped(crtc_gamma->green, size);
1071 last_blue = find_last_non_clamped(crtc_gamma->blue, size);
1072 best_array = crtc_gamma->red;
1073 last_best = last_red;
1074 if (last_green > last_best) {
1075 last_best = last_green;
1076 best_array = crtc_gamma->green;
1078 if (last_blue > last_best) {
1079 last_best = last_blue;
1080 best_array = crtc_gamma->blue;
1085 middle = last_best / 2;
1086 i1 = (double)(middle + 1) / size;
1087 v1 = (double)(best_array[middle]) / 65535;
1088 i2 = (double)(last_best + 1) / size;
1089 v2 = (double)(best_array[last_best]) / 65535;
1090 if (v2 < 0.0001) { /* The screen is black */
1091 output->brightness = 0;
1092 output->gamma.red = 1;
1093 output->gamma.green = 1;
1094 output->gamma.blue = 1;
1096 if ((last_best + 1) == size)
1097 output->brightness = v2;
1099 output->brightness = exp((log(v2)*log(i1) - log(v1)*log(i2))/log(i1/i2));
1100 output->gamma.red = log((double)(crtc_gamma->red[last_red / 2]) / output->brightness
1101 / 65535) / log((double)((last_red / 2) + 1) / size);
1102 output->gamma.green = log((double)(crtc_gamma->green[last_green / 2]) / output->brightness
1103 / 65535) / log((double)((last_green / 2) + 1) / size);
1104 output->gamma.blue = log((double)(crtc_gamma->blue[last_blue / 2]) / output->brightness
1105 / 65535) / log((double)((last_blue / 2) + 1) / size);
1108 XRRFreeGamma(crtc_gamma);
1112 set_output_info (output_t *output, RROutput xid, XRROutputInfo *output_info)
1114 /* sanity check output info */
1115 if (output_info->connection != RR_Disconnected && !output_info->nmode)
1116 warning ("Output %s is not disconnected but has no modes\n",
1119 /* set output name and info */
1120 if (!(output->output.kind & name_xid))
1121 set_name_xid (&output->output, xid);
1122 if (!(output->output.kind & name_string))
1123 set_name_string (&output->output, output_info->name);
1124 output->output_info = output_info;
1126 /* set crtc name and info */
1127 if (!(output->changes & changes_crtc))
1128 set_name_xid (&output->crtc, output_info->crtc);
1130 if (output->crtc.kind == name_xid && output->crtc.xid == None)
1131 output->crtc_info = NULL;
1134 output->crtc_info = find_crtc (&output->crtc);
1135 if (!output->crtc_info)
1137 if (output->crtc.kind & name_xid)
1138 fatal ("cannot find crtc 0x%lx\n", output->crtc.xid);
1139 if (output->crtc.kind & name_index)
1140 fatal ("cannot find crtc %d\n", output->crtc.index);
1142 if (!output_can_use_crtc (output, output->crtc_info))
1143 fatal ("output %s cannot use crtc 0x%lx\n", output->output.string,
1144 output->crtc_info->crtc.xid);
1147 /* set mode name and info */
1148 if (!(output->changes & changes_mode))
1150 crtc_t *crtc = NULL;
1152 if (output_info->crtc)
1153 crtc = find_crtc_by_xid(output_info->crtc);
1154 if (crtc && crtc->crtc_info)
1155 set_name_xid (&output->mode, crtc->crtc_info->mode);
1156 else if (output->crtc_info)
1157 set_name_xid (&output->mode, output->crtc_info->crtc_info->mode);
1159 set_name_xid (&output->mode, None);
1160 if (output->mode.xid)
1162 output->mode_info = find_mode_by_xid (output->mode.xid);
1163 if (!output->mode_info)
1164 fatal ("server did not report mode 0x%lx for output %s\n",
1165 output->mode.xid, output->output.string);
1168 output->mode_info = NULL;
1170 else if (output->mode.kind == name_xid && output->mode.xid == None)
1171 output->mode_info = NULL;
1174 if (output->mode.kind == name_preferred)
1175 output->mode_info = preferred_mode (output);
1177 output->mode_info = find_mode_for_output (output, &output->mode);
1178 if (!output->mode_info)
1180 if (output->mode.kind & name_preferred)
1181 fatal ("cannot find preferred mode\n");
1182 if (output->mode.kind & name_string)
1183 fatal ("cannot find mode %s\n", output->mode.string);
1184 if (output->mode.kind & name_xid)
1185 fatal ("cannot find mode 0x%lx\n", output->mode.xid);
1187 if (!output_can_use_mode (output, output->mode_info))
1188 fatal ("output %s cannot use mode %s\n", output->output.string,
1189 output->mode_info->name);
1193 if (!(output->changes & changes_position))
1195 if (output->crtc_info)
1197 output->x = output->crtc_info->crtc_info->x;
1198 output->y = output->crtc_info->crtc_info->y;
1208 if (!(output->changes & changes_rotation))
1210 output->rotation &= ~0xf;
1211 if (output->crtc_info)
1212 output->rotation |= (output->crtc_info->crtc_info->rotation & 0xf);
1214 output->rotation = RR_Rotate_0;
1216 if (!(output->changes & changes_reflection))
1218 output->rotation &= ~(RR_Reflect_X|RR_Reflect_Y);
1219 if (output->crtc_info)
1220 output->rotation |= (output->crtc_info->crtc_info->rotation &
1221 (RR_Reflect_X|RR_Reflect_Y));
1223 if (!output_can_use_rotation (output, output->rotation))
1224 fatal ("output %s cannot use rotation \"%s\" reflection \"%s\"\n",
1225 output->output.string,
1226 rotation_name (output->rotation),
1227 reflection_name (output->rotation));
1230 if (!(output->changes & changes_gamma))
1231 set_gamma_info(output);
1233 /* set transformation */
1234 if (!(output->changes & changes_transform))
1236 if (output->crtc_info)
1237 copy_transform (&output->transform, &output->crtc_info->current_transform);
1239 init_transform (&output->transform);
1241 /* transform was already set for --scale or --transform */
1243 /* for --scale-from, figure out the mode size and compute the transform
1244 * for the target framebuffer area */
1245 if (output->scale_from_w > 0 && output->mode_info) {
1246 double sx = (double)output->scale_from_w /
1247 output->mode_info->width;
1248 double sy = (double)output->scale_from_h /
1249 output->mode_info->height;
1251 printf("scaling %s by %lfx%lf\n", output->output.string, sx,
1253 init_transform (&output->transform);
1254 output->transform.transform.matrix[0][0] = XDoubleToFixed (sx);
1255 output->transform.transform.matrix[1][1] = XDoubleToFixed (sy);
1256 output->transform.transform.matrix[2][2] = XDoubleToFixed (1.0);
1257 if (sx != 1 || sy != 1)
1258 output->transform.filter = "bilinear";
1260 output->transform.filter = "nearest";
1261 output->transform.nparams = 0;
1262 output->transform.params = NULL;
1267 if (!(output->changes & changes_primary))
1268 output->primary = output_is_primary(output);
1272 get_screen (Bool current)
1275 fatal ("Server RandR version before 1.2\n");
1280 XRRGetScreenSizeRange (dpy, root, &minWidth, &minHeight,
1281 &maxWidth, &maxHeight);
1284 res = XRRGetScreenResourcesCurrent (dpy, root);
1286 res = XRRGetScreenResources (dpy, root);
1287 if (!res) fatal ("could not get screen resources");
1295 num_crtcs = res->ncrtc;
1296 crtcs = calloc (num_crtcs, sizeof (crtc_t));
1297 if (!crtcs) fatal ("out of memory\n");
1299 for (c = 0; c < res->ncrtc; c++)
1301 XRRCrtcInfo *crtc_info = XRRGetCrtcInfo (dpy, res, res->crtcs[c]);
1302 XRRCrtcTransformAttributes *attr;
1303 XRRPanning *panning_info = NULL;
1307 memset(&zero, 0, sizeof(zero));
1308 panning_info = XRRGetPanning (dpy, res, res->crtcs[c]);
1309 zero.timestamp = panning_info->timestamp;
1310 if (!memcmp(panning_info, &zero, sizeof(zero))) {
1311 Xfree(panning_info);
1312 panning_info = NULL;
1316 set_name_xid (&crtcs[c].crtc, res->crtcs[c]);
1317 set_name_index (&crtcs[c].crtc, c);
1318 if (!crtc_info) fatal ("could not get crtc 0x%lx information\n", res->crtcs[c]);
1319 crtcs[c].crtc_info = crtc_info;
1320 crtcs[c].panning_info = panning_info;
1321 if (crtc_info->mode == None)
1323 crtcs[c].mode_info = NULL;
1326 crtcs[c].rotation = RR_Rotate_0;
1328 if (XRRGetCrtcTransform (dpy, res->crtcs[c], &attr) && attr) {
1329 set_transform (&crtcs[c].current_transform,
1330 &attr->currentTransform,
1331 attr->currentFilter,
1332 attr->currentParams,
1333 attr->currentNparams);
1338 init_transform (&crtcs[c].current_transform);
1340 copy_transform (&crtcs[c].pending_transform, &crtcs[c].current_transform);
1345 crtc_add_output (crtc_t *crtc, output_t *output)
1348 crtc->outputs = realloc (crtc->outputs, (crtc->noutput + 1) * sizeof (output_t *));
1351 crtc->outputs = malloc (sizeof (output_t *));
1352 crtc->x = output->x;
1353 crtc->y = output->y;
1354 crtc->rotation = output->rotation;
1355 crtc->mode_info = output->mode_info;
1356 copy_transform (&crtc->pending_transform, &output->transform);
1358 if (!crtc->outputs) fatal ("out of memory\n");
1359 crtc->outputs[crtc->noutput++] = output;
1367 for (output = all_outputs; output; output = output->next)
1369 if (!output->mode_info) continue;
1370 crtc_add_output (output->crtc_info, output);
1379 for (output = all_outputs; output; output = output->next)
1381 if (! output->crtc_info)
1383 if (! (output->changes & changes_panning))
1385 if (! output->crtc_info->panning_info)
1386 output->crtc_info->panning_info = malloc (sizeof(XRRPanning));
1387 memcpy (output->crtc_info->panning_info, &output->panning, sizeof(XRRPanning));
1388 output->crtc_info->changing = 1;
1397 for (output = all_outputs; output; output = output->next) {
1400 XRRCrtcGamma *crtc_gamma;
1405 if (!(output->changes & changes_gamma))
1408 if (!output->crtc_info) {
1409 fatal("Need crtc to set gamma on.\n");
1413 crtc = output->crtc_info;
1415 size = XRRGetCrtcGammaSize(dpy, crtc->crtc.xid);
1418 fatal("Gamma size is 0.\n");
1423 * The gamma-correction lookup table managed through XRR[GS]etCrtcGamma
1424 * is 2^n in size, where 'n' is the number of significant bits in
1425 * the X Color. Because an X Color is 16 bits, size cannot be larger
1429 fatal("Gamma correction table is impossibly large.\n");
1434 * The hardware color lookup table has a number of significant
1435 * bits equal to ffs(size) - 1; compute all values so that
1436 * they are in the range [0,size) then shift the values so
1437 * that they occupy the MSBs of the 16-bit X Color.
1439 shift = 16 - (ffs(size) - 1);
1441 crtc_gamma = XRRAllocGamma(size);
1443 fatal("Gamma allocation failed.\n");
1447 if (output->gamma.red == 0.0)
1448 output->gamma.red = 1.0;
1449 if (output->gamma.green == 0.0)
1450 output->gamma.green = 1.0;
1451 if (output->gamma.blue == 0.0)
1452 output->gamma.blue = 1.0;
1454 gammaRed = 1.0 / output->gamma.red;
1455 gammaGreen = 1.0 / output->gamma.green;
1456 gammaBlue = 1.0 / output->gamma.blue;
1458 for (i = 0; i < size; i++) {
1459 if (gammaRed == 1.0 && output->brightness == 1.0)
1460 crtc_gamma->red[i] = i;
1462 crtc_gamma->red[i] = dmin(pow((double)i/(double)(size - 1),
1463 gammaRed) * output->brightness,
1464 1.0) * (double)(size - 1);
1465 crtc_gamma->red[i] <<= shift;
1467 if (gammaGreen == 1.0 && output->brightness == 1.0)
1468 crtc_gamma->green[i] = i;
1470 crtc_gamma->green[i] = dmin(pow((double)i/(double)(size - 1),
1471 gammaGreen) * output->brightness,
1472 1.0) * (double)(size - 1);
1473 crtc_gamma->green[i] <<= shift;
1475 if (gammaBlue == 1.0 && output->brightness == 1.0)
1476 crtc_gamma->blue[i] = i;
1478 crtc_gamma->blue[i] = dmin(pow((double)i/(double)(size - 1),
1479 gammaBlue) * output->brightness,
1480 1.0) * (double)(size - 1);
1481 crtc_gamma->blue[i] <<= shift;
1484 XRRSetCrtcGamma(dpy, crtc->crtc.xid, crtc_gamma);
1496 XRRSetOutputPrimary(dpy, root, None);
1498 for (output = all_outputs; output; output = output->next) {
1499 if (!(output->changes & changes_primary))
1501 if (output->primary)
1502 XRRSetOutputPrimary(dpy, root, output->output.xid);
1508 crtc_disable (crtc_t *crtc)
1511 printf ("crtc %d: disable\n", crtc->crtc.index);
1514 return RRSetConfigSuccess;
1515 return XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1516 0, 0, None, RR_Rotate_0, NULL, 0);
1520 crtc_set_transform (crtc_t *crtc, transform_t *transform)
1524 XRRQueryVersion (dpy, &major, &minor);
1525 if (major > 1 || (major == 1 && minor >= 3))
1526 XRRSetCrtcTransform (dpy, crtc->crtc.xid,
1527 &transform->transform,
1530 transform->nparams);
1534 crtc_revert (crtc_t *crtc)
1536 XRRCrtcInfo *crtc_info = crtc->crtc_info;
1539 printf ("crtc %d: revert\n", crtc->crtc.index);
1542 return RRSetConfigSuccess;
1544 if (!equal_transform (&crtc->current_transform, &crtc->pending_transform))
1545 crtc_set_transform (crtc, &crtc->current_transform);
1546 return XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1547 crtc_info->x, crtc_info->y,
1548 crtc_info->mode, crtc_info->rotation,
1549 crtc_info->outputs, crtc_info->noutput);
1553 crtc_apply (crtc_t *crtc)
1555 RROutput *rr_outputs;
1560 if (!crtc->changing || !crtc->mode_info)
1561 return RRSetConfigSuccess;
1563 rr_outputs = calloc (crtc->noutput, sizeof (RROutput));
1566 for (o = 0; o < crtc->noutput; o++)
1567 rr_outputs[o] = crtc->outputs[o]->output.xid;
1568 mode = crtc->mode_info->id;
1570 printf ("crtc %d: %12s %6.1f +%d+%d", crtc->crtc.index,
1571 crtc->mode_info->name, mode_refresh (crtc->mode_info),
1573 for (o = 0; o < crtc->noutput; o++)
1574 printf (" \"%s\"", crtc->outputs[o]->output.string);
1579 s = RRSetConfigSuccess;
1582 if (!equal_transform (&crtc->current_transform, &crtc->pending_transform))
1583 crtc_set_transform (crtc, &crtc->pending_transform);
1584 s = XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1585 crtc->x, crtc->y, mode, crtc->rotation,
1586 rr_outputs, crtc->noutput);
1587 if (s == RRSetConfigSuccess && crtc->panning_info) {
1589 s = XRRSetPanning (dpy, res, crtc->crtc.xid, crtc->panning_info);
1591 fatal ("panning needs RandR 1.3\n");
1599 screen_revert (void)
1602 printf ("screen %d: revert\n", screen);
1606 XRRSetScreenSize (dpy, root,
1607 DisplayWidth (dpy, screen),
1608 DisplayHeight (dpy, screen),
1609 DisplayWidthMM (dpy, screen),
1610 DisplayHeightMM (dpy, screen));
1616 if (fb_width == DisplayWidth (dpy, screen) &&
1617 fb_height == DisplayHeight (dpy, screen) &&
1618 fb_width_mm == DisplayWidthMM (dpy, screen) &&
1619 fb_height_mm == DisplayHeightMM (dpy, screen))
1624 printf ("screen %d: %dx%d %dx%d mm %6.2fdpi\n", screen,
1625 fb_width, fb_height, fb_width_mm, fb_height_mm, dpi);
1628 XRRSetScreenSize (dpy, root, fb_width, fb_height,
1629 fb_width_mm, fb_height_mm);
1637 /* first disable all crtcs */
1638 for (c = 0; c < res->ncrtc; c++)
1639 crtc_disable (&crtcs[c]);
1640 /* next reset screen size */
1642 /* now restore all crtcs */
1643 for (c = 0; c < res->ncrtc; c++)
1644 crtc_revert (&crtcs[c]);
1648 * uh-oh, something bad happened in the middle of changing
1649 * the configuration. Revert to the previous configuration
1652 static void _X_NORETURN
1653 panic (Status s, crtc_t *crtc)
1655 int c = crtc->crtc.index;
1656 const char *message;
1659 case RRSetConfigSuccess: message = "succeeded"; break;
1660 case BadAlloc: message = "out of memory"; break;
1661 case RRSetConfigFailed: message = "failed"; break;
1662 case RRSetConfigInvalidConfigTime: message = "invalid config time"; break;
1663 case RRSetConfigInvalidTime: message = "invalid time"; break;
1664 default: message = "unknown failure"; break;
1667 fprintf (stderr, "%s: Configure crtc %d %s\n", program_name, c, message);
1679 * Hold the server grabbed while messing with
1680 * the screen so that apps which notice the resize
1681 * event and ask for xinerama information from the server
1682 * receive up-to-date information
1688 * Turn off any crtcs which are to be disabled or which are
1689 * larger than the target size
1691 for (c = 0; c < res->ncrtc; c++)
1693 crtc_t *crtc = &crtcs[c];
1694 XRRCrtcInfo *crtc_info = crtc->crtc_info;
1696 /* if this crtc is already disabled, skip it */
1697 if (crtc_info->mode == None)
1701 * If this crtc is to be left enabled, make
1702 * sure the old size fits then new screen
1704 if (crtc->mode_info)
1706 XRRModeInfo *old_mode = find_mode_by_xid (crtc_info->mode);
1711 panic (RRSetConfigFailed, crtc);
1713 /* old position and size information */
1714 mode_geometry (old_mode, crtc_info->rotation,
1715 &crtc->current_transform.transform,
1718 x = crtc_info->x + bounds.x1;
1719 y = crtc_info->y + bounds.y1;
1720 w = bounds.x2 - bounds.x1;
1721 h = bounds.y2 - bounds.y1;
1723 /* if it fits, skip it */
1724 if (x + w <= fb_width && y + h <= fb_height)
1726 crtc->changing = True;
1728 s = crtc_disable (crtc);
1729 if (s != RRSetConfigSuccess)
1734 * Set the screen size
1742 for (c = 0; c < res->ncrtc; c++)
1744 crtc_t *crtc = &crtcs[c];
1746 s = crtc_apply (crtc);
1747 if (s != RRSetConfigSuccess)
1754 * Release the server grab and let all clients
1755 * respond to the updated state
1758 XUngrabServer (dpy);
1762 * Use current output state to complete the output list
1770 for (o = 0; o < res->noutput; o++)
1772 XRROutputInfo *output_info = XRRGetOutputInfo (dpy, res, res->outputs[o]);
1775 if (!output_info) fatal ("could not get output 0x%lx information\n", res->outputs[o]);
1776 set_name_xid (&output_name, res->outputs[o]);
1777 set_name_index (&output_name, o);
1778 set_name_string (&output_name, output_info->name);
1779 output = find_output (&output_name);
1782 output = add_output ();
1783 set_name_all (&output->output, &output_name);
1785 * When global --automatic mode is set, turn on connected but off
1786 * outputs, turn off disconnected but on outputs
1790 switch (output_info->connection) {
1792 if (!output_info->crtc) {
1793 output->changes |= changes_automatic;
1794 output->automatic = True;
1797 case RR_Disconnected:
1798 if (output_info->crtc)
1800 output->changes |= changes_automatic;
1801 output->automatic = True;
1807 output->found = True;
1810 * Automatic mode -- track connection state and enable/disable outputs
1813 if (output->automatic)
1815 switch (output_info->connection) {
1817 case RR_UnknownConnection:
1818 if ((!(output->changes & changes_mode)))
1820 set_name_preferred (&output->mode);
1821 output->changes |= changes_mode;
1824 case RR_Disconnected:
1825 if ((!(output->changes & changes_mode)))
1827 set_name_xid (&output->mode, None);
1828 set_name_xid (&output->crtc, None);
1829 output->changes |= changes_mode;
1830 output->changes |= changes_crtc;
1836 set_output_info (output, res->outputs[o], output_info);
1838 for (q = all_outputs; q; q = q->next)
1842 fprintf(stderr, "warning: output %s not found; ignoring\n",
1849 mark_changing_crtcs (void)
1853 for (c = 0; c < num_crtcs; c++)
1855 crtc_t *crtc = &crtcs[c];
1859 /* walk old output list (to catch disables) */
1860 for (o = 0; o < crtc->crtc_info->noutput; o++)
1862 output = find_output_by_xid (crtc->crtc_info->outputs[o]);
1863 if (!output) fatal ("cannot find output 0x%lx\n",
1864 crtc->crtc_info->outputs[o]);
1865 if (output->changes)
1866 crtc->changing = True;
1868 /* walk new output list */
1869 for (o = 0; o < crtc->noutput; o++)
1871 output = crtc->outputs[o];
1872 if (output->changes)
1873 crtc->changing = True;
1879 * Test whether 'crtc' can be used for 'output'
1882 check_crtc_for_output (crtc_t *crtc, output_t *output)
1888 for (c = 0; c < output->output_info->ncrtc; c++)
1889 if (output->output_info->crtcs[c] == crtc->crtc.xid)
1891 if (c == output->output_info->ncrtc)
1893 for (other = all_outputs; other; other = other->next)
1895 if (other == output)
1898 if (other->mode_info == NULL)
1901 if (other->crtc_info != crtc)
1904 /* see if the output connected to the crtc can clone to this output */
1905 for (l = 0; l < output->output_info->nclone; l++)
1906 if (output->output_info->clones[l] == other->output.xid)
1908 /* not on the list, can't clone */
1909 if (l == output->output_info->nclone)
1915 /* make sure the state matches */
1916 if (crtc->mode_info != output->mode_info)
1918 if (crtc->x != output->x)
1920 if (crtc->y != output->y)
1922 if (crtc->rotation != output->rotation)
1924 if (!equal_transform (&crtc->current_transform, &output->transform))
1927 else if (crtc->crtc_info->noutput)
1929 /* make sure the state matches the already used state */
1930 XRRModeInfo *mode = find_mode_by_xid (crtc->crtc_info->mode);
1932 if (mode != output->mode_info)
1934 if (crtc->crtc_info->x != output->x)
1936 if (crtc->crtc_info->y != output->y)
1938 if (crtc->crtc_info->rotation != output->rotation)
1945 find_crtc_for_output (output_t *output)
1949 for (c = 0; c < output->output_info->ncrtc; c++)
1953 crtc = find_crtc_by_xid (output->output_info->crtcs[c]);
1954 if (!crtc) fatal ("cannot find crtc 0x%lx\n", output->output_info->crtcs[c]);
1956 if (check_crtc_for_output (crtc, output))
1963 set_positions (void)
1974 for (output = all_outputs; output; output = output->next)
1977 name_t relation_name;
1979 if (!(output->changes & changes_relation)) continue;
1981 if (output->mode_info == NULL) continue;
1983 init_name (&relation_name);
1984 set_name_string (&relation_name, output->relative_to);
1985 relation = find_output (&relation_name);
1986 if (!relation) fatal ("cannot find output \"%s\"\n", output->relative_to);
1988 if (relation->mode_info == NULL)
1992 output->changes |= changes_position;
1997 * Make sure the dependent object has been set in place
1999 if ((relation->changes & changes_relation) &&
2000 !(relation->changes & changes_position))
2006 switch (output->relation) {
2007 case relation_left_of:
2008 output->y = relation->y;
2009 output->x = relation->x - mode_width (output->mode_info, output->rotation);
2011 case relation_right_of:
2012 output->y = relation->y;
2013 output->x = relation->x + mode_width (relation->mode_info, relation->rotation);
2015 case relation_above:
2016 output->x = relation->x;
2017 output->y = relation->y - mode_height (output->mode_info, output->rotation);
2019 case relation_below:
2020 output->x = relation->x;
2021 output->y = relation->y + mode_height (relation->mode_info, relation->rotation);
2023 case relation_same_as:
2024 output->x = relation->x;
2025 output->y = relation->y;
2027 output->changes |= changes_position;
2033 fatal ("loop in relative position specifications\n");
2037 * Now normalize positions so the upper left corner of all outputs is at 0,0
2041 for (output = all_outputs; output; output = output->next)
2043 if (output->mode_info == NULL) continue;
2045 if (output->x < min_x) min_x = output->x;
2046 if (output->y < min_y) min_y = output->y;
2050 /* move all outputs */
2051 for (output = all_outputs; output; output = output->next)
2053 if (output->mode_info == NULL) continue;
2057 output->changes |= changes_position;
2063 set_screen_size (void)
2066 Bool fb_specified = fb_width != 0 && fb_height != 0;
2068 for (output = all_outputs; output; output = output->next)
2070 XRRModeInfo *mode_info = output->mode_info;
2074 if (!mode_info) continue;
2076 mode_geometry (mode_info, output->rotation,
2077 &output->transform.transform,
2079 x = output->x + bounds.x1;
2080 y = output->y + bounds.y1;
2081 w = bounds.x2 - bounds.x1;
2082 h = bounds.y2 - bounds.y1;
2083 /* make sure output fits in specified size */
2086 if (x + w > fb_width || y + h > fb_height)
2087 warning ("specified screen %dx%d not large enough for output %s (%dx%d+%d+%d)\n",
2088 fb_width, fb_height, output->output.string, w, h, x, y);
2090 /* fit fb to output */
2094 if (x + w > fb_width)
2096 if (y + h > fb_height)
2098 if (output->changes & changes_panning)
2099 pan = &output->panning;
2101 pan = output->crtc_info ? output->crtc_info->panning_info : NULL;
2102 if (pan && pan->left + pan->width > fb_width)
2103 fb_width = pan->left + pan->width;
2104 if (pan && pan->top + pan->height > fb_height)
2105 fb_height = pan->top + pan->height;
2109 if (fb_width > maxWidth || fb_height > maxHeight)
2110 fatal ("screen cannot be larger than %dx%d (desired size %dx%d)\n",
2111 maxWidth, maxHeight, fb_width, fb_height);
2114 if (fb_width < minWidth || fb_height < minHeight)
2115 fatal ("screen must be at least %dx%d\n", minWidth, minHeight);
2119 if (fb_width < minWidth) fb_width = minWidth;
2120 if (fb_height < minHeight) fb_height = minHeight;
2126 disable_outputs (output_t *outputs)
2130 outputs->crtc_info = NULL;
2131 outputs = outputs->next;
2136 * find the best mapping from output to crtc available
2139 pick_crtcs_score (output_t *outputs)
2152 outputs = outputs->next;
2154 * Score with this output disabled
2156 output->crtc_info = NULL;
2157 best_score = pick_crtcs_score (outputs);
2158 if (output->mode_info == NULL)
2163 * Now score with this output any valid crtc
2165 for (c = 0; c < output->output_info->ncrtc; c++)
2169 crtc = find_crtc_by_xid (output->output_info->crtcs[c]);
2171 fatal ("cannot find crtc 0x%lx\n", output->output_info->crtcs[c]);
2173 /* reset crtc allocation for following outputs */
2174 disable_outputs (outputs);
2175 if (!check_crtc_for_output (crtc, output))
2179 /* slight preference for existing connections */
2180 if (crtc == output->current_crtc_info)
2183 output->crtc_info = crtc;
2184 score = my_score + pick_crtcs_score (outputs);
2185 if (score > best_score)
2191 if (output->crtc_info != best_crtc)
2192 output->crtc_info = best_crtc;
2194 * Reset other outputs based on this one using the best crtc
2196 (void) pick_crtcs_score (outputs);
2202 * Pick crtcs for any changing outputs that don't have one
2210 * First try to match up newly enabled outputs with spare crtcs
2212 for (output = all_outputs; output; output = output->next)
2214 if (output->changes && output->mode_info)
2216 if (output->crtc_info) {
2217 if (output->crtc_info->crtc_info->noutput > 0 &&
2218 (output->crtc_info->crtc_info->noutput > 1 ||
2219 output != find_output_by_xid (output->crtc_info->crtc_info->outputs[0])))
2222 output->crtc_info = find_crtc_for_output (output);
2223 if (!output->crtc_info)
2234 * When the simple way fails, see if there is a way
2235 * to swap crtcs around and make things work
2237 for (output = all_outputs; output; output = output->next)
2238 output->current_crtc_info = output->crtc_info;
2239 pick_crtcs_score (all_outputs);
2240 for (output = all_outputs; output; output = output->next)
2242 if (output->mode_info && !output->crtc_info)
2243 fatal ("cannot find crtc for output %s\n", output->output.string);
2244 if (!output->changes && output->crtc_info != output->current_crtc_info)
2245 output->changes |= changes_crtc;
2250 check_strtol(char *s)
2253 int result = strtol(s, &endptr, 10);
2255 argerr ("failed to parse '%s' as a number\n", s);
2260 check_strtod(char *s)
2263 double result = strtod(s, &endptr);
2265 argerr ("failed to parse '%s' as a number\n", s);
2271 property_values_from_string(const char *str, const Atom type, const int format,
2272 int *returned_nitems)
2275 void *returned_bytes = NULL;
2276 int nitems = 0, bytes_per_item = format / 8;
2278 if ((type != XA_INTEGER && type != XA_CARDINAL) ||
2279 (format != 8 && format != 16 && format != 32))
2286 for (token = strtok (tmp, ","); token; token = strtok (NULL, ","))
2289 long int val = strtol (token, &endptr, 0);
2291 if (token == endptr || *endptr != '\0')
2293 argerr ("failed to parse '%s' as a number\n", token);
2296 returned_bytes = realloc (returned_bytes, (nitems + 1) * bytes_per_item);
2298 if (type == XA_INTEGER && format == 8)
2300 int8_t *ptr = returned_bytes;
2301 ptr[nitems] = (int8_t) val;
2303 else if (type == XA_INTEGER && format == 16)
2305 int16_t *ptr = returned_bytes;
2306 ptr[nitems] = (int16_t) val;
2308 else if (type == XA_INTEGER && format == 32)
2310 int32_t *ptr = returned_bytes;
2311 ptr[nitems] = (int32_t) val;
2313 else if (type == XA_CARDINAL && format == 8)
2315 uint8_t *ptr = returned_bytes;
2316 ptr[nitems] = (uint8_t) val;
2318 else if (type == XA_CARDINAL && format == 16)
2320 uint16_t *ptr = returned_bytes;
2321 ptr[nitems] = (uint16_t) val;
2323 else if (type == XA_CARDINAL && format == 32)
2325 uint32_t *ptr = returned_bytes;
2326 ptr[nitems] = (uint32_t) val;
2331 free (returned_bytes);
2340 *returned_nitems = nitems;
2341 return returned_bytes;
2346 print_output_property_value(Bool is_edid,
2347 int value_format, /* 8, 16, 32 */
2348 Atom value_type, /* XA_{ATOM,INTEGER,CARDINAL} */
2349 const void *value_bytes)
2351 /* special-case the EDID */
2352 if (is_edid && value_format == 8)
2354 const uint8_t *val = value_bytes;
2355 printf ("%02" PRIx8, *val);
2359 if (value_type == XA_ATOM && value_format == 32)
2361 const Atom *val = value_bytes;
2362 char *str = XGetAtomName (dpy, *val);
2371 if (value_type == XA_INTEGER)
2373 if (value_format == 8)
2375 const int8_t *val = value_bytes;
2376 printf ("%" PRId8, *val);
2379 if (value_format == 16)
2381 const int16_t *val = value_bytes;
2382 printf ("%" PRId16, *val);
2385 if (value_format == 32)
2387 const int32_t *val = value_bytes;
2388 printf ("%" PRId32, *val);
2393 if (value_type == XA_CARDINAL)
2395 if (value_format == 8)
2397 const uint8_t *val = value_bytes;
2398 printf ("%" PRIu8, *val);
2401 if (value_format == 16)
2403 const uint16_t *val = value_bytes;
2404 printf ("%" PRIu16, *val);
2407 if (value_format == 32)
2409 const uint32_t *val = value_bytes;
2410 printf ("%" PRIu32, *val);
2419 get_providers (void)
2421 XRRProviderResources *pr;
2424 if (!has_1_4 || providers)
2427 pr = XRRGetProviderResources(dpy, root);
2428 num_providers = pr->nproviders;
2429 providers = calloc (num_providers, sizeof (provider_t));
2431 fatal ("out of memory\n");
2433 for (i = 0; i < num_providers; i++) {
2434 provider_t *provider = &providers[i];
2435 name_t *name = &provider->provider;
2436 XRRProviderInfo *info = XRRGetProviderInfo(dpy, res, pr->providers[i]);
2438 provider->info = info;
2439 set_name_xid (name, pr->providers[i]);
2440 set_name_index (name, i);
2441 set_name_string (name, info->name);
2444 XRRFreeProviderResources(pr);
2448 find_provider (name_t *name)
2452 for (i = 0; i < num_providers; i++) {
2453 provider_t *p = &providers[i];
2454 name_kind_t common = name->kind & p->provider.kind;
2456 if ((common & name_xid) && name->xid == p->provider.xid)
2458 if ((common & name_string) && !strcmp (name->string, p->provider.string))
2460 if ((common & name_index) && name->index == p->provider.index)
2464 printf ("Could not find provider with ");
2472 main (int argc, char **argv)
2474 XRRScreenSize *sizes;
2475 XRRScreenConfiguration *sc;
2479 Status status = RRSetConfigFailed;
2482 int action_requested = False;
2483 Rotation current_rotation;
2485 XRRScreenChangeNotifyEvent *sce;
2486 char *display_name = NULL;
2488 SizeID current_size;
2494 Bool version = False;
2495 int event_base, error_base;
2497 int width = 0, height = 0;
2498 Bool have_pixel_size = False;
2500 output_t *config_output = NULL;
2501 Bool setit_1_2 = False;
2502 Bool query_1_2 = False;
2503 Bool modeit = False;
2504 Bool propit = False;
2505 Bool query_1 = False;
2506 Bool list_providers = False;
2507 Bool provsetoutsource = False;
2508 Bool provsetoffsink = False;
2510 Bool current = False;
2512 program_name = argv[0];
2513 for (i = 1; i < argc; i++) {
2514 if (!strcmp ("-display", argv[i]) || !strcmp ("--display", argv[i]) ||
2515 !strcmp ("-d", argv[i])) {
2516 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2517 display_name = argv[i];
2520 if (!strcmp("-help", argv[i]) || !strcmp("--help", argv[i])) {
2524 if (!strcmp ("--verbose", argv[i])) {
2528 if (!strcmp ("--dryrun", argv[i])) {
2533 if (!strcmp ("--nograb", argv[i])) {
2534 grab_server = False;
2537 if (!strcmp("--current", argv[i])) {
2542 if (!strcmp ("-s", argv[i]) || !strcmp ("--size", argv[i])) {
2543 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2544 if (sscanf (argv[i], "%dx%d", &width, &height) == 2) {
2545 have_pixel_size = True;
2547 size = check_strtol(argv[i]);
2548 if (size < 0) argerr ("--size argument must be nonnegative\n");
2551 action_requested = True;
2555 if (!strcmp ("-r", argv[i]) ||
2556 !strcmp ("--rate", argv[i]) ||
2557 !strcmp ("--refresh", argv[i]))
2559 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2560 rate = check_strtod(argv[i]);
2564 config_output->refresh = rate;
2565 config_output->changes |= changes_refresh;
2568 action_requested = True;
2572 if (!strcmp ("-v", argv[i]) || !strcmp ("--version", argv[i])) {
2574 action_requested = True;
2578 if (!strcmp ("-x", argv[i])) {
2579 reflection |= RR_Reflect_X;
2581 action_requested = True;
2584 if (!strcmp ("-y", argv[i])) {
2585 reflection |= RR_Reflect_Y;
2587 action_requested = True;
2590 if (!strcmp ("--screen", argv[i])) {
2591 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2592 screen = check_strtol(argv[i]);
2593 if (screen < 0) argerr ("--screen argument must be nonnegative\n");
2596 if (!strcmp ("-q", argv[i]) || !strcmp ("--query", argv[i])) {
2600 if (!strcmp ("-o", argv[i]) || !strcmp ("--orientation", argv[i])) {
2602 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2603 dirind = strtol(argv[i], &endptr, 10);
2604 if (argv[i] == endptr) {
2605 for (dirind = 0; dirind < 4; dirind++) {
2606 if (strcmp (direction[dirind], argv[i]) == 0) break;
2609 if ((dirind < 0) || (dirind > 3))
2610 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2613 action_requested = True;
2616 if (!strcmp ("--prop", argv[i]) ||
2617 !strcmp ("--props", argv[i]) ||
2618 !strcmp ("--madprops", argv[i]) ||
2619 !strcmp ("--properties", argv[i]))
2623 action_requested = True;
2626 if (!strcmp ("--output", argv[i])) {
2627 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2629 config_output = find_output_by_name (argv[i]);
2630 if (!config_output) {
2631 config_output = add_output ();
2632 set_name (&config_output->output, argv[i], name_string|name_xid);
2636 action_requested = True;
2639 if (!strcmp ("--crtc", argv[i])) {
2640 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2641 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2642 set_name (&config_output->crtc, argv[i], name_xid|name_index);
2643 config_output->changes |= changes_crtc;
2646 if (!strcmp ("--mode", argv[i])) {
2647 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2648 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2649 set_name (&config_output->mode, argv[i], name_string|name_xid);
2650 config_output->changes |= changes_mode;
2653 if (!strcmp ("--preferred", argv[i])) {
2654 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2655 set_name_preferred (&config_output->mode);
2656 config_output->changes |= changes_mode;
2659 if (!strcmp ("--pos", argv[i])) {
2660 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2661 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2662 if (sscanf (argv[i], "%dx%d",
2663 &config_output->x, &config_output->y) != 2)
2664 argerr ("failed to parse '%s' as a position\n", argv[i]);
2665 config_output->changes |= changes_position;
2668 if (!strcmp ("--rotation", argv[i]) || !strcmp ("--rotate", argv[i])) {
2669 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2670 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2671 for (dirind = 0; dirind < 4; dirind++) {
2672 if (strcmp (direction[dirind], argv[i]) == 0) break;
2675 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2676 config_output->rotation &= ~0xf;
2677 config_output->rotation |= 1 << dirind;
2678 config_output->changes |= changes_rotation;
2681 if (!strcmp ("--reflect", argv[i]) || !strcmp ("--reflection", argv[i])) {
2682 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2683 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2684 for (dirind = 0; dirind < 4; dirind++) {
2685 if (strcmp (reflections[dirind], argv[i]) == 0) break;
2688 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2689 config_output->rotation &= ~(RR_Reflect_X|RR_Reflect_Y);
2690 config_output->rotation |= dirind * RR_Reflect_X;
2691 config_output->changes |= changes_reflection;
2694 if (!strcmp ("--left-of", argv[i])) {
2695 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2696 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2697 config_output->relation = relation_left_of;
2698 config_output->relative_to = argv[i];
2699 config_output->changes |= changes_relation;
2702 if (!strcmp ("--right-of", argv[i])) {
2703 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2704 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2705 config_output->relation = relation_right_of;
2706 config_output->relative_to = argv[i];
2707 config_output->changes |= changes_relation;
2710 if (!strcmp ("--above", argv[i])) {
2711 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2712 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2713 config_output->relation = relation_above;
2714 config_output->relative_to = argv[i];
2715 config_output->changes |= changes_relation;
2718 if (!strcmp ("--below", argv[i])) {
2719 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2720 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2721 config_output->relation = relation_below;
2722 config_output->relative_to = argv[i];
2723 config_output->changes |= changes_relation;
2726 if (!strcmp ("--same-as", argv[i])) {
2727 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2728 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2729 config_output->relation = relation_same_as;
2730 config_output->relative_to = argv[i];
2731 config_output->changes |= changes_relation;
2734 if (!strcmp ("--panning", argv[i])) {
2736 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2737 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2738 pan = &config_output->panning;
2739 switch (sscanf (argv[i], "%dx%d+%d+%d/%dx%d+%d+%d/%d/%d/%d/%d",
2740 &pan->width, &pan->height, &pan->left, &pan->top,
2741 &pan->track_width, &pan->track_height,
2742 &pan->track_left, &pan->track_top,
2743 &pan->border_left, &pan->border_top,
2744 &pan->border_right, &pan->border_bottom)) {
2746 pan->left = pan->top = 0;
2749 pan->track_left = pan->track_top =
2750 pan->track_width = pan->track_height = 0;
2753 pan->border_left = pan->border_top =
2754 pan->border_right = pan->border_bottom = 0;
2759 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2761 config_output->changes |= changes_panning;
2764 if (!strcmp ("--gamma", argv[i])) {
2765 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2766 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2767 if (sscanf(argv[i], "%f:%f:%f", &config_output->gamma.red,
2768 &config_output->gamma.green, &config_output->gamma.blue) != 3)
2769 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2770 config_output->changes |= changes_gamma;
2774 if (!strcmp ("--brightness", argv[i])) {
2775 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2776 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2777 if (sscanf(argv[i], "%f", &config_output->brightness) != 1)
2778 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2779 config_output->changes |= changes_gamma;
2783 if (!strcmp ("--primary", argv[i])) {
2784 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2785 config_output->changes |= changes_primary;
2786 config_output->primary = True;
2790 if (!strcmp ("--noprimary", argv[i])) {
2795 if (!strcmp ("--set", argv[i])) {
2796 output_prop_t *prop;
2797 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2798 if (i+2 >= argc) argerr ("%s requires two arguments\n", argv[i]);
2799 prop = malloc (sizeof (output_prop_t));
2800 prop->next = config_output->props;
2801 config_output->props = prop;
2802 prop->name = argv[++i];
2803 prop->value = argv[++i];
2805 config_output->changes |= changes_property;
2809 if (!strcmp ("--scale", argv[i]))
2812 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2813 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2814 if (sscanf (argv[i], "%lfx%lf", &sx, &sy) != 2)
2815 argerr ("failed to parse '%s' as a scaling factor\n", argv[i]);
2816 init_transform (&config_output->transform);
2817 config_output->transform.transform.matrix[0][0] = XDoubleToFixed (sx);
2818 config_output->transform.transform.matrix[1][1] = XDoubleToFixed (sy);
2819 config_output->transform.transform.matrix[2][2] = XDoubleToFixed (1.0);
2820 if (sx != 1 || sy != 1)
2821 config_output->transform.filter = "bilinear";
2823 config_output->transform.filter = "nearest";
2824 config_output->transform.nparams = 0;
2825 config_output->transform.params = NULL;
2826 config_output->changes |= changes_transform;
2829 if (!strcmp ("--scale-from", argv[i]))
2832 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2833 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2834 if (sscanf (argv[i], "%dx%d", &w, &h) != 2)
2835 argerr ("failed to parse '%s' as a scale-from size\n", argv[i]);
2836 if (w <=0 || h <= 0)
2837 argerr ("--scale-from dimensions must be nonnegative\n");
2838 config_output->scale_from_w = w;
2839 config_output->scale_from_h = h;
2840 config_output->changes |= changes_transform;
2843 if (!strcmp ("--transform", argv[i])) {
2844 double transform[3][3];
2846 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2847 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2848 init_transform (&config_output->transform);
2849 if (strcmp (argv[i], "none") != 0)
2851 if (sscanf(argv[i], "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf",
2852 &transform[0][0],&transform[0][1],&transform[0][2],
2853 &transform[1][0],&transform[1][1],&transform[1][2],
2854 &transform[2][0],&transform[2][1],&transform[2][2])
2856 argerr ("failed to parse '%s' as a transformation\n", argv[i]);
2857 init_transform (&config_output->transform);
2858 for (k = 0; k < 3; k++)
2859 for (l = 0; l < 3; l++) {
2860 config_output->transform.transform.matrix[k][l] = XDoubleToFixed (transform[k][l]);
2862 config_output->transform.filter = "bilinear";
2863 config_output->transform.nparams = 0;
2864 config_output->transform.params = NULL;
2866 config_output->changes |= changes_transform;
2869 if (!strcmp ("--off", argv[i])) {
2870 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2871 set_name_xid (&config_output->mode, None);
2872 set_name_xid (&config_output->crtc, None);
2873 config_output->changes |= changes_mode;
2876 if (!strcmp ("--fb", argv[i])) {
2877 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2878 if (sscanf (argv[i], "%dx%d",
2879 &fb_width, &fb_height) != 2)
2880 argerr ("failed to parse '%s' as a framebuffer size\n", argv[i]);
2882 action_requested = True;
2885 if (!strcmp ("--fbmm", argv[i])) {
2886 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2887 if (sscanf (argv[i], "%dx%d",
2888 &fb_width_mm, &fb_height_mm) != 2)
2889 argerr ("failed to parse '%s' as a physical size\n", argv[i]);
2891 action_requested = True;
2894 if (!strcmp ("--dpi", argv[i])) {
2896 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2897 dpi = strtod(argv[i], &strtod_error);
2898 if (argv[i] == strtod_error)
2901 dpi_output_name = argv[i];
2904 action_requested = True;
2907 if (!strcmp ("--auto", argv[i])) {
2910 config_output->automatic = True;
2911 config_output->changes |= changes_automatic;
2916 action_requested = True;
2919 if (!strcmp ("--q12", argv[i]))
2924 if (!strcmp ("--q1", argv[i]))
2929 if (!strcmp ("--newmode", argv[i]))
2931 umode_t *m = calloc (1, sizeof (umode_t));
2936 argerr ("failed to parse '%s' as a mode specification\n", argv[i]);
2937 m->mode.name = argv[i];
2938 m->mode.nameLength = strlen (argv[i]);
2940 clock = check_strtod(argv[i++]);
2941 m->mode.dotClock = clock * 1e6;
2943 m->mode.width = check_strtol(argv[i++]);
2944 m->mode.hSyncStart = check_strtol(argv[i++]);
2945 m->mode.hSyncEnd = check_strtol(argv[i++]);
2946 m->mode.hTotal = check_strtol(argv[i++]);
2947 m->mode.height = check_strtol(argv[i++]);
2948 m->mode.vSyncStart = check_strtol(argv[i++]);
2949 m->mode.vSyncEnd = check_strtol(argv[i++]);
2950 m->mode.vTotal = check_strtol(argv[i++]);
2951 m->mode.modeFlags = 0;
2955 for (f = 0; mode_flags[f].string; f++)
2956 if (!strcasecmp (mode_flags[f].string, argv[i]))
2959 if (!mode_flags[f].string)
2961 m->mode.modeFlags |= mode_flags[f].flag;
2965 m->action = umode_create;
2968 action_requested = True;
2971 if (!strcmp ("--rmmode", argv[i]))
2973 umode_t *m = calloc (1, sizeof (umode_t));
2975 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2976 set_name (&m->name, argv[i], name_string|name_xid);
2977 m->action = umode_destroy;
2981 action_requested = True;
2984 if (!strcmp ("--addmode", argv[i]))
2986 umode_t *m = calloc (1, sizeof (umode_t));
2988 if (i+2 >= argc) argerr ("%s requires two arguments\n", argv[i]);
2989 set_name (&m->output, argv[++i], name_string|name_xid);
2990 set_name (&m->name, argv[++i], name_string|name_xid);
2991 m->action = umode_add;
2995 action_requested = True;
2998 if (!strcmp ("--delmode", argv[i]))
3000 umode_t *m = calloc (1, sizeof (umode_t));
3002 if (i+2 >= argc) argerr ("%s requires two arguments\n", argv[i]);
3003 set_name (&m->output, argv[++i], name_string|name_xid);
3004 set_name (&m->name, argv[++i], name_string|name_xid);
3005 m->action = umode_delete;
3009 action_requested = True;
3012 if (!strcmp ("--listproviders", argv[i]))
3014 list_providers = True;
3015 action_requested = True;
3018 if (!strcmp("--setprovideroutputsource", argv[i]))
3020 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
3021 set_name (&provider_name, argv[i], name_string|name_xid|name_index);
3023 set_name_xid (&output_source_provider_name, 0);
3025 set_name (&output_source_provider_name, argv[i], name_string|name_xid|name_index);
3026 action_requested = True;
3027 provsetoutsource = True;
3030 if (!strcmp("--setprovideroffloadsink", argv[i]))
3032 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
3033 set_name (&provider_name, argv[i], name_string|name_xid|name_index);
3035 set_name_xid (&offload_sink_provider_name, 0);
3037 set_name (&offload_sink_provider_name, argv[i], name_string|name_xid|name_index);
3038 action_requested = True;
3039 provsetoffsink = True;
3043 argerr ("unrecognized option '%s'\n", argv[i]);
3045 if (!action_requested)
3050 if (setit && !setit_1_2)
3054 printf("xrandr program version " VERSION "\n");
3056 dpy = XOpenDisplay (display_name);
3059 fprintf (stderr, "Can't open display %s\n", XDisplayName(display_name));
3063 screen = DefaultScreen (dpy);
3064 if (screen >= ScreenCount (dpy)) {
3065 fprintf (stderr, "Invalid screen number %d (display has %d)\n",
3066 screen, ScreenCount (dpy));
3070 root = RootWindow (dpy, screen);
3072 if (!XRRQueryExtension (dpy, &event_base, &error_base) ||
3073 !XRRQueryVersion (dpy, &major, &minor))
3075 fprintf (stderr, "RandR extension missing\n");
3078 if (major > 1 || (major == 1 && minor >= 2))
3080 if (major > 1 || (major == 1 && minor >= 3))
3082 if (major > 1 || (major == 1 && minor >= 4))
3085 if (has_1_2 && modeit)
3089 get_screen (current);
3093 for (m = umodes; m; m = m->next)
3098 switch (m->action) {
3100 XRRCreateMode (dpy, root, &m->mode);
3103 e = find_mode (&m->name, 0);
3105 fatal ("cannot find mode \"%s\"\n", m->name.string);
3106 XRRDestroyMode (dpy, e->id);
3109 o = find_output (&m->output);
3111 fatal ("cannot find output \"%s\"\n", m->output.string);
3112 e = find_mode (&m->name, 0);
3114 fatal ("cannot find mode \"%s\"\n", m->name.string);
3115 XRRAddOutputMode (dpy, o->output.xid, e->id);
3118 o = find_output (&m->output);
3120 fatal ("cannot find output \"%s\"\n", m->output.string);
3121 e = find_mode (&m->name, 0);
3123 fatal ("cannot find mode \"%s\"\n", m->name.string);
3124 XRRDeleteOutputMode (dpy, o->output.xid, e->id);
3134 if (has_1_2 && propit)
3138 get_screen (current);
3142 for (output = all_outputs; output; output = output->next)
3144 output_prop_t *prop;
3146 for (prop = output->props; prop; prop = prop->next)
3148 Atom name = XInternAtom (dpy, prop->name, False);
3151 unsigned char *data, *malloced_data = NULL;
3154 unsigned long ulong_value;
3155 unsigned char *prop_data;
3157 unsigned long nitems, bytes_after;
3159 XRRPropertyInfo *propinfo;
3161 type = AnyPropertyType;
3163 if (XRRGetOutputProperty (dpy, output->output.xid, name,
3164 0, 100, False, False,
3166 &actual_type, &actual_format,
3167 &nitems, &bytes_after, &prop_data) == Success &&
3169 (propinfo = XRRQueryOutputProperty(dpy, output->output.xid,
3173 format = actual_format;
3176 malloced_data = property_values_from_string
3177 (prop->value, type, actual_format, &nelements);
3181 data = malloced_data;
3183 format = actual_format;
3185 else if (type == AnyPropertyType &&
3186 (sscanf (prop->value, "%d", &int_value) == 1 ||
3187 sscanf (prop->value, "0x%x", &int_value) == 1))
3190 ulong_value = int_value;
3191 data = (unsigned char *) &ulong_value;
3195 else if ((type == XA_ATOM))
3197 ulong_value = XInternAtom (dpy, prop->value, False);
3198 data = (unsigned char *) &ulong_value;
3201 else if ((type == XA_STRING || type == AnyPropertyType))
3204 data = (unsigned char *) prop->value;
3205 nelements = strlen (prop->value);
3210 XRRChangeOutputProperty (dpy, output->output.xid,
3211 name, type, format, PropModeReplace,
3213 free (malloced_data);
3222 if (provsetoutsource)
3224 provider_t *provider, *source;
3227 fatal ("--setprovideroutputsource requires RandR 1.4\n");
3229 get_screen (current);
3232 provider = find_provider (&provider_name);
3233 source = find_provider(&output_source_provider_name);
3235 XRRSetProviderOutputSource(dpy, provider->provider.xid, source->provider.xid);
3239 provider_t *provider, *sink;
3242 fatal ("--setprovideroffloadsink requires RandR 1.4\n");
3244 get_screen (current);
3247 provider = find_provider (&provider_name);
3248 sink = find_provider(&offload_sink_provider_name);
3250 XRRSetProviderOffloadSink(dpy, provider->provider.xid, sink->provider.xid);
3254 get_screen (current);
3263 * Assign outputs to crtcs
3268 * Mark changing crtcs
3270 mark_changing_crtcs ();
3273 * If an output was specified to track dpi, use it
3275 if (dpi_output_name)
3277 output_t *dpi_output = find_output_by_name (dpi_output_name);
3278 XRROutputInfo *output_info;
3279 XRRModeInfo *mode_info;
3281 fatal ("Cannot find output %s\n", dpi_output_name);
3282 output_info = dpi_output->output_info;
3283 mode_info = dpi_output->mode_info;
3284 if (output_info && mode_info && output_info->mm_height)
3287 * When this output covers the whole screen, just use
3288 * the known physical size
3290 if (fb_width == mode_info->width &&
3291 fb_height == mode_info->height)
3293 fb_width_mm = output_info->mm_width;
3294 fb_height_mm = output_info->mm_height;
3298 dpi = (25.4 * mode_info->height) / output_info->mm_height;
3304 * Compute physical screen size
3306 if (fb_width_mm == 0 || fb_height_mm == 0)
3308 if (fb_width != DisplayWidth (dpy, screen) ||
3309 fb_height != DisplayHeight (dpy, screen) || dpi != 0.0)
3312 dpi = (25.4 * DisplayHeight (dpy, screen)) / DisplayHeightMM(dpy, screen);
3314 fb_width_mm = (25.4 * fb_width) / dpi;
3315 fb_height_mm = (25.4 * fb_height) / dpi;
3319 fb_width_mm = DisplayWidthMM (dpy, screen);
3320 fb_height_mm = DisplayHeightMM (dpy, screen);
3330 * Set gamma on crtc's that belong to the outputs.
3335 * Now apply all of the changes
3342 if (query_1_2 || (query && has_1_2 && !query_1))
3347 #define ModeShown 0x80000000
3349 get_screen (current);
3353 printf ("Screen %d: minimum %d x %d, current %d x %d, maximum %d x %d\n",
3354 screen, minWidth, minHeight,
3355 DisplayWidth (dpy, screen), DisplayHeight(dpy, screen),
3356 maxWidth, maxHeight);
3358 for (output = all_outputs; output; output = output->next)
3360 XRROutputInfo *output_info = output->output_info;
3361 crtc_t *cur_crtc = output->crtc_info;
3362 XRRCrtcInfo *crtc_info = cur_crtc ? cur_crtc->crtc_info : NULL;
3363 XRRModeInfo *cur_mode = output->mode_info;
3367 Rotation rotations = output_rotations (output);
3369 printf ("%s %s", output_info->name, connection[output_info->connection]);
3370 if (output->primary) {
3376 printf (" %dx%d+%d+%d",
3377 crtc_info->width, crtc_info->height,
3378 crtc_info->x, crtc_info->y);
3380 printf (" %dx%d+%d+%d",
3381 cur_mode->width, cur_mode->height, output->x,
3385 printf (" (0x%x)", (int)cur_mode->id);
3386 if (output->rotation != RR_Rotate_0 || verbose)
3389 rotation_name (output->rotation));
3390 if (output->rotation & (RR_Reflect_X|RR_Reflect_Y))
3391 printf (" %s", reflection_name (output->rotation));
3394 if (rotations != RR_Rotate_0 || verbose)
3398 for (i = 0; i < 4; i ++) {
3399 if ((rotations >> i) & 1) {
3400 if (!first) printf (" "); first = False;
3401 printf("%s", direction[i]);
3404 if (rotations & RR_Reflect_X)
3406 if (!first) printf (" "); first = False;
3409 if (rotations & RR_Reflect_Y)
3411 if (!first) printf (" ");
3419 printf (" %dmm x %dmm",
3420 (int)output_info->mm_width, (int)output_info->mm_height);
3423 if (cur_crtc && cur_crtc->panning_info &&
3424 cur_crtc->panning_info->width > 0)
3426 XRRPanning *pan = cur_crtc->panning_info;
3427 printf (" panning %dx%d+%d+%d",
3428 pan->width, pan->height, pan->left, pan->top);
3429 if ((pan->track_width != 0 &&
3430 (pan->track_left != pan->left ||
3431 pan->track_width != pan->width ||
3432 pan->border_left != 0 ||
3433 pan->border_right != 0)) ||
3434 (pan->track_height != 0 &&
3435 (pan->track_top != pan->top ||
3436 pan->track_height != pan->height ||
3437 pan->border_top != 0 ||
3438 pan->border_bottom != 0)))
3439 printf (" tracking %dx%d+%d+%d border %d/%d/%d/%d",
3440 pan->track_width, pan->track_height,
3441 pan->track_left, pan->track_top,
3442 pan->border_left, pan->border_top,
3443 pan->border_right, pan->border_bottom);
3449 printf ("\tIdentifier: 0x%x\n", (int)output->output.xid);
3450 printf ("\tTimestamp: %d\n", (int)output_info->timestamp);
3451 printf ("\tSubpixel: %s\n", order[output_info->subpixel_order]);
3452 if (output->gamma.red != 0.0 && output->gamma.green != 0.0 && output->gamma.blue != 0.0) {
3453 printf ("\tGamma: %#.2g:%#.2g:%#.2g\n",
3454 output->gamma.red, output->gamma.green, output->gamma.blue);
3455 printf ("\tBrightness: %#.2g\n", output->brightness);
3457 printf ("\tClones: ");
3458 for (j = 0; j < output_info->nclone; j++)
3460 output_t *clone = find_output_by_xid (output_info->clones[j]);
3462 if (clone) printf (" %s", clone->output.string);
3465 if (output->crtc_info)
3466 printf ("\tCRTC: %d\n", output->crtc_info->crtc.index);
3467 printf ("\tCRTCs: ");
3468 for (j = 0; j < output_info->ncrtc; j++)
3470 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[j]);
3472 printf (" %d", crtc->crtc.index);
3475 if (output->crtc_info && output->crtc_info->panning_info) {
3476 XRRPanning *pan = output->crtc_info->panning_info;
3477 printf ("\tPanning: %dx%d+%d+%d\n",
3478 pan->width, pan->height, pan->left, pan->top);
3479 printf ("\tTracking: %dx%d+%d+%d\n",
3480 pan->track_width, pan->track_height,
3481 pan->track_left, pan->track_top);
3482 printf ("\tBorder: %d/%d/%d/%d\n",
3483 pan->border_left, pan->border_top,
3484 pan->border_right, pan->border_bottom);
3491 printf ("\tTransform: ");
3492 for (y = 0; y < 3; y++)
3494 for (x = 0; x < 3; x++)
3495 printf (" %f", XFixedToDouble (output->transform.transform.matrix[y][x]));
3499 if (output->transform.filter)
3500 printf ("\n\t filter: %s", output->transform.filter);
3503 if (verbose || properties)
3505 props = XRRListOutputProperties (dpy, output->output.xid,
3507 for (j = 0; j < nprop; j++) {
3508 unsigned char *prop;
3510 unsigned long nitems, bytes_after;
3512 XRRPropertyInfo *propinfo;
3513 char *atom_name = XGetAtomName (dpy, props[j]);
3514 Bool is_edid = strcmp (atom_name, "EDID") == 0;
3515 int bytes_per_item, k;
3517 XRRGetOutputProperty (dpy, output->output.xid, props[j],
3518 0, 100, False, False,
3520 &actual_type, &actual_format,
3521 &nitems, &bytes_after, &prop);
3523 propinfo = XRRQueryOutputProperty(dpy, output->output.xid,
3526 bytes_per_item = actual_format / 8;
3528 printf ("\t%s: ", atom_name);
3535 for (k = 0; k < nitems; k++)
3544 print_output_property_value (is_edid, actual_format,
3546 prop + (k * bytes_per_item));
3554 if (propinfo->range && propinfo->num_values > 0)
3556 printf ("\t\trange%s: ",
3557 (propinfo->num_values == 2) ? "" : "s");
3558 for (k = 0; k < propinfo->num_values / 2; k++)
3561 print_output_property_value (False, 32, actual_type,
3562 (unsigned char *) &(propinfo->values[k * 2]));
3564 print_output_property_value (False, 32, actual_type,
3565 (unsigned char *) &(propinfo->values[k * 2 + 1]));
3567 if (k < propinfo->num_values / 2 - 1)
3572 if (!propinfo->range && propinfo->num_values > 0)
3574 printf ("\t\tsupported: ");
3575 for (k = 0; k < propinfo->num_values; k++)
3577 print_output_property_value (False, 32, actual_type,
3578 (unsigned char *) &(propinfo->values[k]));
3579 if (k < propinfo->num_values - 1)
3591 for (j = 0; j < output_info->nmode; j++)
3593 XRRModeInfo *mode = find_mode_by_xid (output_info->modes[j]);
3596 printf (" %s (0x%x) %6.1fMHz",
3597 mode->name, (int)mode->id,
3598 (double)mode->dotClock / 1000000.0);
3599 for (f = 0; mode_flags[f].flag; f++)
3600 if (mode->modeFlags & mode_flags[f].flag)
3601 printf (" %s", mode_flags[f].string);
3602 if (mode == output->mode_info)
3603 printf (" *current");
3604 if (j < output_info->npreferred)
3605 printf (" +preferred");
3607 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
3608 mode->width, mode->hSyncStart, mode->hSyncEnd,
3609 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
3610 printf (" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
3611 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
3612 mode_refresh (mode));
3613 mode->modeFlags |= ModeShown;
3618 mode_shown = calloc (output_info->nmode, sizeof (Bool));
3619 if (!mode_shown) fatal ("out of memory\n");
3620 for (j = 0; j < output_info->nmode; j++)
3622 XRRModeInfo *jmode, *kmode;
3625 if (mode_shown[j]) continue;
3627 jmode = find_mode_by_xid (output_info->modes[j]);
3629 printf (" %-12s", jmode->name);
3630 for (k = j; k < output_info->nmode; k++)
3632 if (mode_shown[k]) continue;
3633 kmode = find_mode_by_xid (output_info->modes[k]);
3634 if (strcmp (jmode->name, kmode->name) != 0) continue;
3635 mode_shown[k] = True;
3636 kmode->modeFlags |= ModeShown;
3637 printf (" %6.1f", mode_refresh (kmode));
3638 if (kmode == output->mode_info)
3642 if (k < output_info->npreferred)
3652 for (m = 0; m < res->nmode; m++)
3654 XRRModeInfo *mode = &res->modes[m];
3656 if (!(mode->modeFlags & ModeShown))
3658 printf (" %s (0x%x) %6.1fMHz\n",
3659 mode->name, (int)mode->id,
3660 (double)mode->dotClock / 1000000.0);
3661 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
3662 mode->width, mode->hSyncStart, mode->hSyncEnd,
3663 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
3664 printf (" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
3665 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
3666 mode_refresh (mode));
3671 if (list_providers) {
3675 printf ("RandR 1.4 not supported\n");
3679 get_screen (current);
3685 printf("Providers: number : %d\n", num_providers);
3687 for (j = 0; j < num_providers; j++) {
3688 provider_t *provider = &providers[j];
3689 XRRProviderInfo *info = provider->info;
3691 printf("Provider %d: id: 0x%x cap: 0x%x", j, (int)provider->provider.xid, info->capabilities);
3692 for (k = 0; k < 4; k++)
3693 if (info->capabilities & (1 << k))
3694 printf(", %s", capability_name(1<<k));
3696 printf(" crtcs: %d outputs: %d associated providers: %d name:%s\n", info->ncrtcs, info->noutputs, info->nassociatedproviders, info->name);
3701 sc = XRRGetScreenInfo (dpy, root);
3706 current_size = XRRConfigCurrentConfiguration (sc, ¤t_rotation);
3708 sizes = XRRConfigSizes(sc, &nsize);
3710 if (have_pixel_size) {
3711 for (size = 0; size < nsize; size++)
3713 if (sizes[size].width == width && sizes[size].height == height)
3716 if (size >= nsize) {
3718 "Size %dx%d not found in available modes\n", width, height);
3723 size = current_size;
3724 else if (size >= nsize) {
3726 "Size index %d is too large, there are only %d sizes\n",
3733 for (rot = 0; rot < 4; rot++)
3734 if (1 << rot == (current_rotation & 0xf))
3738 current_rate = XRRConfigCurrentRate (sc);
3742 if (size == current_size)
3743 rate = current_rate;
3749 rates = XRRConfigRates (sc, size, &nrate);
3750 for (i = 0; i < nrate; i++)
3751 if (rate == rates[i])
3754 fprintf (stderr, "Rate %.1f Hz not available for this size\n", rate);
3760 int major_version, minor_version;
3761 XRRQueryVersion (dpy, &major_version, &minor_version);
3762 printf("Server reports RandR version %d.%d\n",
3763 major_version, minor_version);
3766 if (query || query_1) {
3767 printf(" SZ: Pixels Physical Refresh\n");
3768 for (i = 0; i < nsize; i++) {
3771 printf ("%c%-2d %5d x %-5d (%4dmm x%4dmm )",
3772 i == current_size ? '*' : ' ',
3773 i, sizes[i].width, sizes[i].height,
3774 sizes[i].mwidth, sizes[i].mheight);
3775 rates = XRRConfigRates (sc, i, &nrate);
3776 if (nrate) printf (" ");
3777 for (j = 0; j < nrate; j++)
3779 i == current_size && rates[j] == current_rate ? '*' : ' ',
3786 Rotation rotations = XRRConfigRotations(sc, ¤t_rotation);
3789 printf("Current rotation - %s\n",
3790 rotation_name (current_rotation));
3792 printf("Current reflection - %s\n",
3793 reflection_name (current_rotation));
3795 printf ("Rotations possible - ");
3796 for (i = 0; i < 4; i ++) {
3797 if ((rotations >> i) & 1) printf("%s ", direction[i]);
3801 printf ("Reflections possible - ");
3802 if (rotations & (RR_Reflect_X|RR_Reflect_Y))
3804 if (rotations & RR_Reflect_X) printf ("X Axis ");
3805 if (rotations & RR_Reflect_Y) printf ("Y Axis");
3814 printf("Setting size to %d, rotation to %s\n", size, direction[rot]);
3816 printf ("Setting reflection on ");
3819 if (reflection & RR_Reflect_X) printf ("X Axis ");
3820 if (reflection & RR_Reflect_Y) printf ("Y Axis");
3823 printf ("neither axis");
3826 if (reflection & RR_Reflect_X) printf("Setting reflection on X axis\n");
3828 if (reflection & RR_Reflect_Y) printf("Setting reflection on Y axis\n");
3831 /* we should test configureNotify on the root window */
3832 XSelectInput (dpy, root, StructureNotifyMask);
3834 if (setit && !dryrun) XRRSelectInput (dpy, root,
3835 RRScreenChangeNotifyMask);
3836 if (setit && !dryrun) {
3837 Rotation rotation = 1 << rot;
3838 status = XRRSetScreenConfigAndRate (dpy, sc, root, (SizeID) size,
3839 (Rotation) (rotation | reflection),
3843 if (setit && !dryrun && status == RRSetConfigFailed) {
3844 printf ("Failed to change the screen configuration!\n");
3848 if (verbose && setit && !dryrun && size != current_size) {
3849 if (status == RRSetConfigSuccess)
3851 Bool seen_screen = False;
3852 while (!seen_screen) {
3854 XNextEvent(dpy, (XEvent *) &event);
3856 printf ("Event received, type = %d\n", event.type);
3857 /* update Xlib's knowledge of the event */
3858 XRRUpdateConfiguration (&event);
3859 if (event.type == ConfigureNotify)
3860 printf("Received ConfigureNotify Event!\n");
3862 switch (event.type - event_base) {
3863 case RRScreenChangeNotify:
3864 sce = (XRRScreenChangeNotifyEvent *) &event;
3866 printf("Got a screen change notify event!\n");
3867 printf(" window = %d\n root = %d\n size_index = %d\n rotation %d\n",
3868 (int) sce->window, (int) sce->root,
3869 sce->size_index, sce->rotation);
3870 printf(" timestamp = %ld, config_timestamp = %ld\n",
3871 sce->timestamp, sce->config_timestamp);
3872 printf(" Rotation = %x\n", sce->rotation);
3873 printf(" %d X %d pixels, %d X %d mm\n",
3874 sce->width, sce->height, sce->mwidth, sce->mheight);
3875 printf("Display width %d, height %d\n",
3876 DisplayWidth(dpy, screen), DisplayHeight(dpy, screen));
3877 printf("Display widthmm %d, heightmm %d\n",
3878 DisplayWidthMM(dpy, screen), DisplayHeightMM(dpy, screen));
3879 spo = sce->subpixel_order;
3880 if ((spo < 0) || (spo > 5))
3881 printf ("Unknown subpixel order, value = %d\n", spo);
3882 else printf ("new Subpixel rendering model is %s\n", order[spo]);
3886 if (event.type != ConfigureNotify)
3887 printf("unknown event received, type = %d!\n", event.type);
3892 XRRFreeScreenConfigInfo(sc);