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%s", program_name,
101 " where options are:\n"
102 " --display <display> or -d <display>\n"
104 " -o <normal,inverted,left,right,0,1,2,3>\n"
105 " or --orientation <normal,inverted,left,right,0,1,2,3>\n"
107 " -s <size>/<width>x<height> or --size <size>/<width>x<height>\n"
108 " -r <rate> or --rate <rate> or --refresh <rate>\n"
110 " -x (reflect in x)\n"
111 " -y (reflect in y)\n"
112 " --screen <screen>\n"
117 " --prop or --properties\n"
118 " --fb <width>x<height>\n"
119 " --fbmm <width>x<height>\n"
120 " --dpi <dpi>/<output>\n"
121 " --output <output>\n"
126 " --rate <rate> or --refresh <rate>\n"
127 " --reflect normal,x,y,xy\n"
128 " --rotate normal,inverted,left,right\n"
129 " --left-of <output>\n"
130 " --right-of <output>\n"
131 " --above <output>\n"
132 " --below <output>\n"
133 " --same-as <output>\n"
134 " --set <property> <value>\n"
136 " --scale-from <w>x<h>\n"
137 " --transform <a>,<b>,<c>,<d>,<e>,<f>,<g>,<h>,<i>\n"
140 " --panning <w>x<h>[+<x>+<y>[/<track:w>x<h>+<x>+<y>[/<border:l>/<t>/<r>/<b>]]]\n"
141 " --gamma <r>:<g>:<b>\n"
142 " --brightness <value>\n"
145 " --newmode <name> <clock MHz>\n"
146 " <hdisp> <hsync-start> <hsync-end> <htotal>\n"
147 " <vdisp> <vsync-start> <vsync-end> <vtotal>\n"
149 " Valid flags: +HSync -HSync +VSync -VSync\n"
150 " +CSync -CSync CSync Interlace DoubleScan\n"
152 " --addmode <output> <name>\n"
153 " --delmode <output> <name>\n"
155 " --setprovideroutputsource <prov-xid> <source-xid>\n"
156 " --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 double 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");
1433 crtc_gamma = XRRAllocGamma(size);
1435 fatal("Gamma allocation failed.\n");
1439 if (output->gamma.red == 0.0)
1440 output->gamma.red = 1.0;
1441 if (output->gamma.green == 0.0)
1442 output->gamma.green = 1.0;
1443 if (output->gamma.blue == 0.0)
1444 output->gamma.blue = 1.0;
1446 gammaRed = 1.0 / output->gamma.red;
1447 gammaGreen = 1.0 / output->gamma.green;
1448 gammaBlue = 1.0 / output->gamma.blue;
1450 for (i = 0; i < size; i++) {
1451 if (gammaRed == 1.0 && output->brightness == 1.0)
1452 crtc_gamma->red[i] = (double)i / (double)(size - 1) * 65535.0;
1454 crtc_gamma->red[i] = dmin(pow((double)i/(double)(size - 1),
1455 gammaRed) * output->brightness,
1458 if (gammaGreen == 1.0 && output->brightness == 1.0)
1459 crtc_gamma->green[i] = (double)i / (double)(size - 1) * 65535.0;
1461 crtc_gamma->green[i] = dmin(pow((double)i/(double)(size - 1),
1462 gammaGreen) * output->brightness,
1465 if (gammaBlue == 1.0 && output->brightness == 1.0)
1466 crtc_gamma->blue[i] = (double)i / (double)(size - 1) * 65535.0;
1468 crtc_gamma->blue[i] = dmin(pow((double)i/(double)(size - 1),
1469 gammaBlue) * output->brightness,
1473 XRRSetCrtcGamma(dpy, crtc->crtc.xid, crtc_gamma);
1485 XRRSetOutputPrimary(dpy, root, None);
1487 for (output = all_outputs; output; output = output->next) {
1488 if (!(output->changes & changes_primary))
1490 if (output->primary)
1491 XRRSetOutputPrimary(dpy, root, output->output.xid);
1497 crtc_disable (crtc_t *crtc)
1500 printf ("crtc %d: disable\n", crtc->crtc.index);
1503 return RRSetConfigSuccess;
1504 return XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1505 0, 0, None, RR_Rotate_0, NULL, 0);
1509 crtc_set_transform (crtc_t *crtc, transform_t *transform)
1513 XRRQueryVersion (dpy, &major, &minor);
1514 if (major > 1 || (major == 1 && minor >= 3))
1515 XRRSetCrtcTransform (dpy, crtc->crtc.xid,
1516 &transform->transform,
1519 transform->nparams);
1523 crtc_revert (crtc_t *crtc)
1525 XRRCrtcInfo *crtc_info = crtc->crtc_info;
1528 printf ("crtc %d: revert\n", crtc->crtc.index);
1531 return RRSetConfigSuccess;
1533 if (!equal_transform (&crtc->current_transform, &crtc->pending_transform))
1534 crtc_set_transform (crtc, &crtc->current_transform);
1535 return XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1536 crtc_info->x, crtc_info->y,
1537 crtc_info->mode, crtc_info->rotation,
1538 crtc_info->outputs, crtc_info->noutput);
1542 crtc_apply (crtc_t *crtc)
1544 RROutput *rr_outputs;
1549 if (!crtc->changing || !crtc->mode_info)
1550 return RRSetConfigSuccess;
1552 rr_outputs = calloc (crtc->noutput, sizeof (RROutput));
1555 for (o = 0; o < crtc->noutput; o++)
1556 rr_outputs[o] = crtc->outputs[o]->output.xid;
1557 mode = crtc->mode_info->id;
1559 printf ("crtc %d: %12s %6.2f +%d+%d", crtc->crtc.index,
1560 crtc->mode_info->name, mode_refresh (crtc->mode_info),
1562 for (o = 0; o < crtc->noutput; o++)
1563 printf (" \"%s\"", crtc->outputs[o]->output.string);
1568 s = RRSetConfigSuccess;
1571 if (!equal_transform (&crtc->current_transform, &crtc->pending_transform))
1572 crtc_set_transform (crtc, &crtc->pending_transform);
1573 s = XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1574 crtc->x, crtc->y, mode, crtc->rotation,
1575 rr_outputs, crtc->noutput);
1576 if (s == RRSetConfigSuccess && crtc->panning_info) {
1578 s = XRRSetPanning (dpy, res, crtc->crtc.xid, crtc->panning_info);
1580 fatal ("panning needs RandR 1.3\n");
1588 screen_revert (void)
1591 printf ("screen %d: revert\n", screen);
1595 XRRSetScreenSize (dpy, root,
1596 DisplayWidth (dpy, screen),
1597 DisplayHeight (dpy, screen),
1598 DisplayWidthMM (dpy, screen),
1599 DisplayHeightMM (dpy, screen));
1605 if (fb_width == DisplayWidth (dpy, screen) &&
1606 fb_height == DisplayHeight (dpy, screen) &&
1607 fb_width_mm == DisplayWidthMM (dpy, screen) &&
1608 fb_height_mm == DisplayHeightMM (dpy, screen))
1613 printf ("screen %d: %dx%d %dx%d mm %6.2fdpi\n", screen,
1614 fb_width, fb_height, fb_width_mm, fb_height_mm, dpi);
1617 XRRSetScreenSize (dpy, root, fb_width, fb_height,
1618 fb_width_mm, fb_height_mm);
1626 /* first disable all crtcs */
1627 for (c = 0; c < res->ncrtc; c++)
1628 crtc_disable (&crtcs[c]);
1629 /* next reset screen size */
1631 /* now restore all crtcs */
1632 for (c = 0; c < res->ncrtc; c++)
1633 crtc_revert (&crtcs[c]);
1637 * uh-oh, something bad happened in the middle of changing
1638 * the configuration. Revert to the previous configuration
1641 static void _X_NORETURN
1642 panic (Status s, crtc_t *crtc)
1644 int c = crtc->crtc.index;
1645 const char *message;
1648 case RRSetConfigSuccess: message = "succeeded"; break;
1649 case BadAlloc: message = "out of memory"; break;
1650 case RRSetConfigFailed: message = "failed"; break;
1651 case RRSetConfigInvalidConfigTime: message = "invalid config time"; break;
1652 case RRSetConfigInvalidTime: message = "invalid time"; break;
1653 default: message = "unknown failure"; break;
1656 fprintf (stderr, "%s: Configure crtc %d %s\n", program_name, c, message);
1668 * Hold the server grabbed while messing with
1669 * the screen so that apps which notice the resize
1670 * event and ask for xinerama information from the server
1671 * receive up-to-date information
1677 * Turn off any crtcs which are to be disabled or which are
1678 * larger than the target size
1680 for (c = 0; c < res->ncrtc; c++)
1682 crtc_t *crtc = &crtcs[c];
1683 XRRCrtcInfo *crtc_info = crtc->crtc_info;
1685 /* if this crtc is already disabled, skip it */
1686 if (crtc_info->mode == None)
1690 * If this crtc is to be left enabled, make
1691 * sure the old size fits then new screen
1693 if (crtc->mode_info)
1695 XRRModeInfo *old_mode = find_mode_by_xid (crtc_info->mode);
1700 panic (RRSetConfigFailed, crtc);
1702 /* old position and size information */
1703 mode_geometry (old_mode, crtc_info->rotation,
1704 &crtc->current_transform.transform,
1707 x = crtc_info->x + bounds.x1;
1708 y = crtc_info->y + bounds.y1;
1709 w = bounds.x2 - bounds.x1;
1710 h = bounds.y2 - bounds.y1;
1712 /* if it fits, skip it */
1713 if (x + w <= fb_width && y + h <= fb_height)
1715 crtc->changing = True;
1717 s = crtc_disable (crtc);
1718 if (s != RRSetConfigSuccess)
1723 * Set the screen size
1731 for (c = 0; c < res->ncrtc; c++)
1733 crtc_t *crtc = &crtcs[c];
1735 s = crtc_apply (crtc);
1736 if (s != RRSetConfigSuccess)
1743 * Release the server grab and let all clients
1744 * respond to the updated state
1747 XUngrabServer (dpy);
1751 * Use current output state to complete the output list
1759 for (o = 0; o < res->noutput; o++)
1761 XRROutputInfo *output_info = XRRGetOutputInfo (dpy, res, res->outputs[o]);
1764 if (!output_info) fatal ("could not get output 0x%lx information\n", res->outputs[o]);
1765 set_name_xid (&output_name, res->outputs[o]);
1766 set_name_index (&output_name, o);
1767 set_name_string (&output_name, output_info->name);
1768 output = find_output (&output_name);
1771 output = add_output ();
1772 set_name_all (&output->output, &output_name);
1774 * When global --automatic mode is set, turn on connected but off
1775 * outputs, turn off disconnected but on outputs
1779 switch (output_info->connection) {
1781 if (!output_info->crtc) {
1782 output->changes |= changes_automatic;
1783 output->automatic = True;
1786 case RR_Disconnected:
1787 if (output_info->crtc)
1789 output->changes |= changes_automatic;
1790 output->automatic = True;
1796 output->found = True;
1799 * Automatic mode -- track connection state and enable/disable outputs
1802 if (output->automatic)
1804 switch (output_info->connection) {
1806 case RR_UnknownConnection:
1807 if ((!(output->changes & changes_mode)))
1809 set_name_preferred (&output->mode);
1810 output->changes |= changes_mode;
1813 case RR_Disconnected:
1814 if ((!(output->changes & changes_mode)))
1816 set_name_xid (&output->mode, None);
1817 set_name_xid (&output->crtc, None);
1818 output->changes |= changes_mode;
1819 output->changes |= changes_crtc;
1825 set_output_info (output, res->outputs[o], output_info);
1827 for (q = all_outputs; q; q = q->next)
1831 fprintf(stderr, "warning: output %s not found; ignoring\n",
1838 mark_changing_crtcs (void)
1842 for (c = 0; c < num_crtcs; c++)
1844 crtc_t *crtc = &crtcs[c];
1848 /* walk old output list (to catch disables) */
1849 for (o = 0; o < crtc->crtc_info->noutput; o++)
1851 output = find_output_by_xid (crtc->crtc_info->outputs[o]);
1852 if (!output) fatal ("cannot find output 0x%lx\n",
1853 crtc->crtc_info->outputs[o]);
1854 if (output->changes)
1855 crtc->changing = True;
1857 /* walk new output list */
1858 for (o = 0; o < crtc->noutput; o++)
1860 output = crtc->outputs[o];
1861 if (output->changes)
1862 crtc->changing = True;
1868 * Test whether 'crtc' can be used for 'output'
1871 check_crtc_for_output (crtc_t *crtc, output_t *output)
1877 for (c = 0; c < output->output_info->ncrtc; c++)
1878 if (output->output_info->crtcs[c] == crtc->crtc.xid)
1880 if (c == output->output_info->ncrtc)
1882 for (other = all_outputs; other; other = other->next)
1884 if (other == output)
1887 if (other->mode_info == NULL)
1890 if (other->crtc_info != crtc)
1893 /* see if the output connected to the crtc can clone to this output */
1894 for (l = 0; l < output->output_info->nclone; l++)
1895 if (output->output_info->clones[l] == other->output.xid)
1897 /* not on the list, can't clone */
1898 if (l == output->output_info->nclone)
1904 /* make sure the state matches */
1905 if (crtc->mode_info != output->mode_info)
1907 if (crtc->x != output->x)
1909 if (crtc->y != output->y)
1911 if (crtc->rotation != output->rotation)
1913 if (!equal_transform (&crtc->current_transform, &output->transform))
1916 else if (crtc->crtc_info->noutput)
1918 /* make sure the state matches the already used state */
1919 XRRModeInfo *mode = find_mode_by_xid (crtc->crtc_info->mode);
1921 if (mode != output->mode_info)
1923 if (crtc->crtc_info->x != output->x)
1925 if (crtc->crtc_info->y != output->y)
1927 if (crtc->crtc_info->rotation != output->rotation)
1934 find_crtc_for_output (output_t *output)
1938 for (c = 0; c < output->output_info->ncrtc; c++)
1942 crtc = find_crtc_by_xid (output->output_info->crtcs[c]);
1943 if (!crtc) fatal ("cannot find crtc 0x%lx\n", output->output_info->crtcs[c]);
1945 if (check_crtc_for_output (crtc, output))
1952 set_positions (void)
1963 for (output = all_outputs; output; output = output->next)
1966 name_t relation_name;
1968 if (!(output->changes & changes_relation)) continue;
1970 if (output->mode_info == NULL) continue;
1972 init_name (&relation_name);
1973 set_name_string (&relation_name, output->relative_to);
1974 relation = find_output (&relation_name);
1975 if (!relation) fatal ("cannot find output \"%s\"\n", output->relative_to);
1977 if (relation->mode_info == NULL)
1981 output->changes |= changes_position;
1986 * Make sure the dependent object has been set in place
1988 if ((relation->changes & changes_relation) &&
1989 !(relation->changes & changes_position))
1995 switch (output->relation) {
1996 case relation_left_of:
1997 output->y = relation->y;
1998 output->x = relation->x - mode_width (output->mode_info, output->rotation);
2000 case relation_right_of:
2001 output->y = relation->y;
2002 output->x = relation->x + mode_width (relation->mode_info, relation->rotation);
2004 case relation_above:
2005 output->x = relation->x;
2006 output->y = relation->y - mode_height (output->mode_info, output->rotation);
2008 case relation_below:
2009 output->x = relation->x;
2010 output->y = relation->y + mode_height (relation->mode_info, relation->rotation);
2012 case relation_same_as:
2013 output->x = relation->x;
2014 output->y = relation->y;
2016 output->changes |= changes_position;
2022 fatal ("loop in relative position specifications\n");
2026 * Now normalize positions so the upper left corner of all outputs is at 0,0
2030 for (output = all_outputs; output; output = output->next)
2032 if (output->mode_info == NULL) continue;
2034 if (output->x < min_x) min_x = output->x;
2035 if (output->y < min_y) min_y = output->y;
2039 /* move all outputs */
2040 for (output = all_outputs; output; output = output->next)
2042 if (output->mode_info == NULL) continue;
2046 output->changes |= changes_position;
2052 set_screen_size (void)
2055 Bool fb_specified = fb_width != 0 && fb_height != 0;
2057 for (output = all_outputs; output; output = output->next)
2059 XRRModeInfo *mode_info = output->mode_info;
2063 if (!mode_info) continue;
2065 mode_geometry (mode_info, output->rotation,
2066 &output->transform.transform,
2068 x = output->x + bounds.x1;
2069 y = output->y + bounds.y1;
2070 w = bounds.x2 - bounds.x1;
2071 h = bounds.y2 - bounds.y1;
2072 /* make sure output fits in specified size */
2075 if (x + w > fb_width || y + h > fb_height)
2076 warning ("specified screen %dx%d not large enough for output %s (%dx%d+%d+%d)\n",
2077 fb_width, fb_height, output->output.string, w, h, x, y);
2079 /* fit fb to output */
2083 if (x + w > fb_width)
2085 if (y + h > fb_height)
2087 if (output->changes & changes_panning)
2088 pan = &output->panning;
2090 pan = output->crtc_info ? output->crtc_info->panning_info : NULL;
2091 if (pan && pan->left + pan->width > fb_width)
2092 fb_width = pan->left + pan->width;
2093 if (pan && pan->top + pan->height > fb_height)
2094 fb_height = pan->top + pan->height;
2098 if (fb_width > maxWidth || fb_height > maxHeight)
2099 fatal ("screen cannot be larger than %dx%d (desired size %dx%d)\n",
2100 maxWidth, maxHeight, fb_width, fb_height);
2103 if (fb_width < minWidth || fb_height < minHeight)
2104 fatal ("screen must be at least %dx%d\n", minWidth, minHeight);
2108 if (fb_width < minWidth) fb_width = minWidth;
2109 if (fb_height < minHeight) fb_height = minHeight;
2115 disable_outputs (output_t *outputs)
2119 outputs->crtc_info = NULL;
2120 outputs = outputs->next;
2125 * find the best mapping from output to crtc available
2128 pick_crtcs_score (output_t *outputs)
2141 outputs = outputs->next;
2143 * Score with this output disabled
2145 output->crtc_info = NULL;
2146 best_score = pick_crtcs_score (outputs);
2147 if (output->mode_info == NULL)
2152 * Now score with this output any valid crtc
2154 for (c = 0; c < output->output_info->ncrtc; c++)
2158 crtc = find_crtc_by_xid (output->output_info->crtcs[c]);
2160 fatal ("cannot find crtc 0x%lx\n", output->output_info->crtcs[c]);
2162 /* reset crtc allocation for following outputs */
2163 disable_outputs (outputs);
2164 if (!check_crtc_for_output (crtc, output))
2168 /* slight preference for existing connections */
2169 if (crtc == output->current_crtc_info)
2172 output->crtc_info = crtc;
2173 score = my_score + pick_crtcs_score (outputs);
2174 if (score > best_score)
2180 if (output->crtc_info != best_crtc)
2181 output->crtc_info = best_crtc;
2183 * Reset other outputs based on this one using the best crtc
2185 (void) pick_crtcs_score (outputs);
2191 * Pick crtcs for any changing outputs that don't have one
2199 * First try to match up newly enabled outputs with spare crtcs
2201 for (output = all_outputs; output; output = output->next)
2203 if (output->changes && output->mode_info)
2205 if (output->crtc_info) {
2206 if (output->crtc_info->crtc_info->noutput > 0 &&
2207 (output->crtc_info->crtc_info->noutput > 1 ||
2208 output != find_output_by_xid (output->crtc_info->crtc_info->outputs[0])))
2211 output->crtc_info = find_crtc_for_output (output);
2212 if (!output->crtc_info)
2223 * When the simple way fails, see if there is a way
2224 * to swap crtcs around and make things work
2226 for (output = all_outputs; output; output = output->next)
2227 output->current_crtc_info = output->crtc_info;
2228 pick_crtcs_score (all_outputs);
2229 for (output = all_outputs; output; output = output->next)
2231 if (output->mode_info && !output->crtc_info)
2232 fatal ("cannot find crtc for output %s\n", output->output.string);
2233 if (!output->changes && output->crtc_info != output->current_crtc_info)
2234 output->changes |= changes_crtc;
2239 check_strtol(char *s)
2242 int result = strtol(s, &endptr, 10);
2244 argerr ("failed to parse '%s' as a number\n", s);
2249 check_strtod(char *s)
2252 double result = strtod(s, &endptr);
2254 argerr ("failed to parse '%s' as a number\n", s);
2260 property_values_from_string(const char *str, const Atom type, const int format,
2261 int *returned_nitems)
2264 void *returned_bytes = NULL;
2265 int nitems = 0, bytes_per_item;
2267 if (type != XA_INTEGER && type != XA_CARDINAL)
2270 /* compute memory needed for Xlib datatype (sigh) */
2273 bytes_per_item = sizeof(char);
2276 bytes_per_item = sizeof(short);
2279 bytes_per_item = sizeof(long);
2287 for (token = strtok (tmp, ","); token; token = strtok (NULL, ","))
2290 long int val = strtol (token, &endptr, 0);
2292 if (token == endptr || *endptr != '\0')
2294 argerr ("failed to parse '%s' as a number\n", token);
2297 returned_bytes = realloc (returned_bytes, (nitems + 1) * bytes_per_item);
2299 if (type == XA_INTEGER && format == 8)
2301 signed char *ptr = returned_bytes;
2302 ptr[nitems] = (char) val;
2304 else if (type == XA_INTEGER && format == 16)
2306 short *ptr = returned_bytes;
2307 ptr[nitems] = (short) val;
2309 else if (type == XA_INTEGER && format == 32)
2311 long *ptr = returned_bytes;
2312 ptr[nitems] = (long) val;
2314 else if (type == XA_CARDINAL && format == 8)
2316 unsigned char *ptr = returned_bytes;
2317 ptr[nitems] = (unsigned char) val;
2319 else if (type == XA_CARDINAL && format == 16)
2321 unsigned short *ptr = returned_bytes;
2322 ptr[nitems] = (unsigned short) val;
2324 else if (type == XA_CARDINAL && format == 32)
2326 unsigned long *ptr = returned_bytes;
2327 ptr[nitems] = (unsigned long) val;
2332 free (returned_bytes);
2341 *returned_nitems = nitems;
2342 return returned_bytes;
2347 print_output_property_value(int value_format, /* 8, 16, 32 */
2348 Atom value_type, /* XA_{ATOM,INTEGER,CARDINAL} */
2349 const void *value_bytes)
2351 if (value_type == XA_ATOM && value_format == 32)
2353 const Atom *val = value_bytes;
2354 char *str = XGetAtomName (dpy, *val);
2363 if (value_type == XA_INTEGER)
2365 if (value_format == 8)
2367 const signed char *val = value_bytes;
2368 printf ("%d", *val);
2371 if (value_format == 16)
2373 const short *val = value_bytes;
2374 printf ("%d", *val);
2377 if (value_format == 32)
2379 const long *val = value_bytes;
2380 printf ("%ld", *val);
2385 if (value_type == XA_CARDINAL)
2387 if (value_format == 8)
2389 const unsigned char *val = value_bytes;
2390 printf ("%u", *val);
2393 if (value_format == 16)
2395 const unsigned short *val = value_bytes;
2396 printf ("%u", *val);
2399 if (value_format == 32)
2401 const unsigned long *val = value_bytes;
2402 printf ("%lu", *val);
2411 print_edid(int nitems, const unsigned char *prop)
2417 for (k = 0; k < nitems; k++)
2419 if (k != 0 && (k % 16) == 0)
2424 printf("%02" PRIx8, prop[k]);
2431 print_guid(const unsigned char *prop)
2437 for (k = 0; k < 16; k++)
2439 printf("%02" PRIX8, prop[k]);
2440 if (k == 3 || k == 5 || k == 7 || k == 9)
2450 print_output_property(const char *atom_name,
2454 const unsigned char *prop)
2459 switch (value_format) {
2461 bytes_per_item = sizeof(char);
2464 bytes_per_item = sizeof(short);
2467 bytes_per_item = sizeof(long);
2473 * Check for properties that need special formatting.
2475 if (strcmp (atom_name, "EDID") == 0 && value_format == 8 &&
2476 value_type == XA_INTEGER)
2478 print_edid (nitems, prop);
2481 else if (strcmp (atom_name, "GUID") == 0 && value_format == 8 &&
2482 value_type == XA_INTEGER && nitems == 16)
2488 for (k = 0; k < nitems; k++)
2497 print_output_property_value (value_format, value_type,
2498 prop + (k * bytes_per_item));
2506 get_providers (void)
2508 XRRProviderResources *pr;
2511 if (!has_1_4 || providers)
2514 pr = XRRGetProviderResources(dpy, root);
2515 num_providers = pr->nproviders;
2516 providers = calloc (num_providers, sizeof (provider_t));
2518 fatal ("out of memory\n");
2520 for (i = 0; i < num_providers; i++) {
2521 provider_t *provider = &providers[i];
2522 name_t *name = &provider->provider;
2523 XRRProviderInfo *info = XRRGetProviderInfo(dpy, res, pr->providers[i]);
2525 provider->info = info;
2526 set_name_xid (name, pr->providers[i]);
2527 set_name_index (name, i);
2528 set_name_string (name, info->name);
2531 XRRFreeProviderResources(pr);
2535 find_provider (name_t *name)
2539 if ((name->kind & name_xid) && name->xid == 0)
2541 for (i = 0; i < num_providers; i++) {
2542 provider_t *p = &providers[i];
2543 name_kind_t common = name->kind & p->provider.kind;
2545 if ((common & name_xid) && name->xid == p->provider.xid)
2547 if ((common & name_string) && !strcmp (name->string, p->provider.string))
2549 if ((common & name_index) && name->index == p->provider.index)
2553 printf ("Could not find provider with ");
2561 main (int argc, char **argv)
2563 XRRScreenSize *sizes;
2564 XRRScreenConfiguration *sc;
2568 Status status = RRSetConfigFailed;
2571 int action_requested = False;
2572 Rotation current_rotation;
2574 XRRScreenChangeNotifyEvent *sce;
2575 char *display_name = NULL;
2577 SizeID current_size;
2583 Bool version = False;
2584 int event_base, error_base;
2586 int width = 0, height = 0;
2587 Bool have_pixel_size = False;
2589 output_t *config_output = NULL;
2590 Bool setit_1_2 = False;
2591 Bool query_1_2 = False;
2592 Bool modeit = False;
2593 Bool propit = False;
2594 Bool query_1 = False;
2595 Bool list_providers = False;
2596 Bool provsetoutsource = False;
2597 Bool provsetoffsink = False;
2599 Bool current = False;
2600 Bool toggle_x = False;
2601 Bool toggle_y = False;
2603 program_name = argv[0];
2604 for (i = 1; i < argc; i++) {
2605 if (!strcmp ("-display", argv[i]) || !strcmp ("--display", argv[i]) ||
2606 !strcmp ("-d", argv[i])) {
2607 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2608 display_name = argv[i];
2611 if (!strcmp("-help", argv[i]) || !strcmp("--help", argv[i])) {
2615 if (!strcmp ("--verbose", argv[i])) {
2619 if (!strcmp ("--dryrun", argv[i])) {
2624 if (!strcmp ("--nograb", argv[i])) {
2625 grab_server = False;
2628 if (!strcmp("--current", argv[i])) {
2633 if (!strcmp ("-s", argv[i]) || !strcmp ("--size", argv[i])) {
2634 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2635 if (sscanf (argv[i], "%dx%d", &width, &height) == 2) {
2636 have_pixel_size = True;
2638 size = check_strtol(argv[i]);
2639 if (size < 0) argerr ("--size argument must be nonnegative\n");
2642 action_requested = True;
2646 if (!strcmp ("-r", argv[i]) ||
2647 !strcmp ("--rate", argv[i]) ||
2648 !strcmp ("--refresh", argv[i]))
2650 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2651 rate = check_strtod(argv[i]);
2655 config_output->refresh = rate;
2656 config_output->changes |= changes_refresh;
2659 action_requested = True;
2663 if (!strcmp ("-v", argv[i]) || !strcmp ("--version", argv[i])) {
2665 action_requested = True;
2669 if (!strcmp ("-x", argv[i])) {
2672 action_requested = True;
2675 if (!strcmp ("-y", argv[i])) {
2678 action_requested = True;
2681 if (!strcmp ("--screen", argv[i])) {
2682 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2683 screen = check_strtol(argv[i]);
2684 if (screen < 0) argerr ("--screen argument must be nonnegative\n");
2687 if (!strcmp ("-q", argv[i]) || !strcmp ("--query", argv[i])) {
2691 if (!strcmp ("-o", argv[i]) || !strcmp ("--orientation", argv[i])) {
2693 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2694 dirind = strtol(argv[i], &endptr, 10);
2695 if (argv[i] == endptr) {
2696 for (dirind = 0; dirind < 4; dirind++) {
2697 if (strcmp (direction[dirind], argv[i]) == 0) break;
2700 if ((dirind < 0) || (dirind > 3))
2701 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2704 action_requested = True;
2707 if (!strcmp ("--prop", argv[i]) ||
2708 !strcmp ("--props", argv[i]) ||
2709 !strcmp ("--madprops", argv[i]) ||
2710 !strcmp ("--properties", argv[i]))
2714 action_requested = True;
2717 if (!strcmp ("--output", argv[i])) {
2718 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2720 config_output = find_output_by_name (argv[i]);
2721 if (!config_output) {
2722 config_output = add_output ();
2723 set_name (&config_output->output, argv[i], name_string|name_xid);
2727 action_requested = True;
2730 if (!strcmp ("--crtc", argv[i])) {
2731 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2732 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2733 set_name (&config_output->crtc, argv[i], name_xid|name_index);
2734 config_output->changes |= changes_crtc;
2737 if (!strcmp ("--mode", argv[i])) {
2738 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2739 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2740 set_name (&config_output->mode, argv[i], name_string|name_xid);
2741 config_output->changes |= changes_mode;
2744 if (!strcmp ("--preferred", argv[i])) {
2745 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2746 set_name_preferred (&config_output->mode);
2747 config_output->changes |= changes_mode;
2750 if (!strcmp ("--pos", argv[i])) {
2751 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2752 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2753 if (sscanf (argv[i], "%dx%d",
2754 &config_output->x, &config_output->y) != 2)
2755 argerr ("failed to parse '%s' as a position\n", argv[i]);
2756 config_output->changes |= changes_position;
2759 if (!strcmp ("--rotation", argv[i]) || !strcmp ("--rotate", argv[i])) {
2760 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2761 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2762 for (dirind = 0; dirind < 4; dirind++) {
2763 if (strcmp (direction[dirind], argv[i]) == 0) break;
2766 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2767 config_output->rotation &= ~0xf;
2768 config_output->rotation |= 1 << dirind;
2769 config_output->changes |= changes_rotation;
2772 if (!strcmp ("--reflect", argv[i]) || !strcmp ("--reflection", 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 for (dirind = 0; dirind < 4; dirind++) {
2776 if (strcmp (reflections[dirind], argv[i]) == 0) break;
2779 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2780 config_output->rotation &= ~(RR_Reflect_X|RR_Reflect_Y);
2781 config_output->rotation |= dirind * RR_Reflect_X;
2782 config_output->changes |= changes_reflection;
2785 if (!strcmp ("--left-of", argv[i])) {
2786 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2787 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2788 config_output->relation = relation_left_of;
2789 config_output->relative_to = argv[i];
2790 config_output->changes |= changes_relation;
2793 if (!strcmp ("--right-of", argv[i])) {
2794 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2795 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2796 config_output->relation = relation_right_of;
2797 config_output->relative_to = argv[i];
2798 config_output->changes |= changes_relation;
2801 if (!strcmp ("--above", argv[i])) {
2802 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2803 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2804 config_output->relation = relation_above;
2805 config_output->relative_to = argv[i];
2806 config_output->changes |= changes_relation;
2809 if (!strcmp ("--below", 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 config_output->relation = relation_below;
2813 config_output->relative_to = argv[i];
2814 config_output->changes |= changes_relation;
2817 if (!strcmp ("--same-as", argv[i])) {
2818 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2819 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2820 config_output->relation = relation_same_as;
2821 config_output->relative_to = argv[i];
2822 config_output->changes |= changes_relation;
2825 if (!strcmp ("--panning", argv[i])) {
2827 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2828 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2829 pan = &config_output->panning;
2830 switch (sscanf (argv[i], "%dx%d+%d+%d/%dx%d+%d+%d/%d/%d/%d/%d",
2831 &pan->width, &pan->height, &pan->left, &pan->top,
2832 &pan->track_width, &pan->track_height,
2833 &pan->track_left, &pan->track_top,
2834 &pan->border_left, &pan->border_top,
2835 &pan->border_right, &pan->border_bottom)) {
2837 pan->left = pan->top = 0;
2840 pan->track_left = pan->track_top =
2841 pan->track_width = pan->track_height = 0;
2844 pan->border_left = pan->border_top =
2845 pan->border_right = pan->border_bottom = 0;
2850 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2852 config_output->changes |= changes_panning;
2855 if (!strcmp ("--gamma", argv[i])) {
2856 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2857 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2858 if (sscanf(argv[i], "%f:%f:%f", &config_output->gamma.red,
2859 &config_output->gamma.green, &config_output->gamma.blue) != 3)
2860 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2861 config_output->changes |= changes_gamma;
2865 if (!strcmp ("--brightness", argv[i])) {
2866 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2867 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2868 if (sscanf(argv[i], "%f", &config_output->brightness) != 1)
2869 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2870 config_output->changes |= changes_gamma;
2874 if (!strcmp ("--primary", argv[i])) {
2875 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2876 config_output->changes |= changes_primary;
2877 config_output->primary = True;
2881 if (!strcmp ("--noprimary", argv[i])) {
2886 if (!strcmp ("--set", argv[i])) {
2887 output_prop_t *prop;
2888 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2889 if (i+2 >= argc) argerr ("%s requires two arguments\n", argv[i]);
2890 prop = malloc (sizeof (output_prop_t));
2891 prop->next = config_output->props;
2892 config_output->props = prop;
2893 prop->name = argv[++i];
2894 prop->value = argv[++i];
2896 config_output->changes |= changes_property;
2900 if (!strcmp ("--scale", argv[i]))
2903 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2904 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2905 if (sscanf (argv[i], "%lfx%lf", &sx, &sy) != 2)
2906 argerr ("failed to parse '%s' as a scaling factor\n", argv[i]);
2907 init_transform (&config_output->transform);
2908 config_output->transform.transform.matrix[0][0] = XDoubleToFixed (sx);
2909 config_output->transform.transform.matrix[1][1] = XDoubleToFixed (sy);
2910 config_output->transform.transform.matrix[2][2] = XDoubleToFixed (1.0);
2911 if (sx != 1 || sy != 1)
2912 config_output->transform.filter = "bilinear";
2914 config_output->transform.filter = "nearest";
2915 config_output->transform.nparams = 0;
2916 config_output->transform.params = NULL;
2917 config_output->changes |= changes_transform;
2920 if (!strcmp ("--scale-from", argv[i]))
2923 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2924 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2925 if (sscanf (argv[i], "%dx%d", &w, &h) != 2)
2926 argerr ("failed to parse '%s' as a scale-from size\n", argv[i]);
2927 if (w <=0 || h <= 0)
2928 argerr ("--scale-from dimensions must be nonnegative\n");
2929 config_output->scale_from_w = w;
2930 config_output->scale_from_h = h;
2931 config_output->changes |= changes_transform;
2934 if (!strcmp ("--transform", argv[i])) {
2935 double transform[3][3];
2937 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2938 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2939 init_transform (&config_output->transform);
2940 if (strcmp (argv[i], "none") != 0)
2942 if (sscanf(argv[i], "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf",
2943 &transform[0][0],&transform[0][1],&transform[0][2],
2944 &transform[1][0],&transform[1][1],&transform[1][2],
2945 &transform[2][0],&transform[2][1],&transform[2][2])
2947 argerr ("failed to parse '%s' as a transformation\n", argv[i]);
2948 init_transform (&config_output->transform);
2949 for (k = 0; k < 3; k++)
2950 for (l = 0; l < 3; l++) {
2951 config_output->transform.transform.matrix[k][l] = XDoubleToFixed (transform[k][l]);
2953 config_output->transform.filter = "bilinear";
2954 config_output->transform.nparams = 0;
2955 config_output->transform.params = NULL;
2957 config_output->changes |= changes_transform;
2960 if (!strcmp ("--off", argv[i])) {
2961 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2962 set_name_xid (&config_output->mode, None);
2963 set_name_xid (&config_output->crtc, None);
2964 config_output->changes |= changes_mode;
2967 if (!strcmp ("--fb", argv[i])) {
2968 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2969 if (sscanf (argv[i], "%dx%d",
2970 &fb_width, &fb_height) != 2)
2971 argerr ("failed to parse '%s' as a framebuffer size\n", argv[i]);
2973 action_requested = True;
2976 if (!strcmp ("--fbmm", argv[i])) {
2977 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2978 if (sscanf (argv[i], "%dx%d",
2979 &fb_width_mm, &fb_height_mm) != 2)
2980 argerr ("failed to parse '%s' as a physical size\n", argv[i]);
2982 action_requested = True;
2985 if (!strcmp ("--dpi", argv[i])) {
2987 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2988 dpi = strtod(argv[i], &strtod_error);
2989 if (argv[i] == strtod_error)
2992 dpi_output_name = argv[i];
2995 action_requested = True;
2998 if (!strcmp ("--auto", argv[i])) {
3001 config_output->automatic = True;
3002 config_output->changes |= changes_automatic;
3007 action_requested = True;
3010 if (!strcmp ("--q12", argv[i]))
3015 if (!strcmp ("--q1", argv[i]))
3020 if (!strcmp ("--newmode", argv[i]))
3022 umode_t *m = calloc (1, sizeof (umode_t));
3027 argerr ("failed to parse '%s' as a mode specification\n", argv[i]);
3028 m->mode.name = argv[i];
3029 m->mode.nameLength = strlen (argv[i]);
3031 clock = check_strtod(argv[i++]);
3032 m->mode.dotClock = clock * 1e6;
3034 m->mode.width = check_strtol(argv[i++]);
3035 m->mode.hSyncStart = check_strtol(argv[i++]);
3036 m->mode.hSyncEnd = check_strtol(argv[i++]);
3037 m->mode.hTotal = check_strtol(argv[i++]);
3038 m->mode.height = check_strtol(argv[i++]);
3039 m->mode.vSyncStart = check_strtol(argv[i++]);
3040 m->mode.vSyncEnd = check_strtol(argv[i++]);
3041 m->mode.vTotal = check_strtol(argv[i++]);
3042 m->mode.modeFlags = 0;
3046 for (f = 0; mode_flags[f].string; f++)
3047 if (!strcasecmp (mode_flags[f].string, argv[i]))
3050 if (!mode_flags[f].string)
3052 m->mode.modeFlags |= mode_flags[f].flag;
3056 m->action = umode_create;
3059 action_requested = True;
3062 if (!strcmp ("--rmmode", argv[i]))
3064 umode_t *m = calloc (1, sizeof (umode_t));
3066 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
3067 set_name (&m->name, argv[i], name_string|name_xid);
3068 m->action = umode_destroy;
3072 action_requested = True;
3075 if (!strcmp ("--addmode", argv[i]))
3077 umode_t *m = calloc (1, sizeof (umode_t));
3079 if (i+2 >= argc) argerr ("%s requires two arguments\n", argv[i]);
3080 set_name (&m->output, argv[++i], name_string|name_xid);
3081 set_name (&m->name, argv[++i], name_string|name_xid);
3082 m->action = umode_add;
3086 action_requested = True;
3089 if (!strcmp ("--delmode", argv[i]))
3091 umode_t *m = calloc (1, sizeof (umode_t));
3093 if (i+2 >= argc) argerr ("%s requires two arguments\n", argv[i]);
3094 set_name (&m->output, argv[++i], name_string|name_xid);
3095 set_name (&m->name, argv[++i], name_string|name_xid);
3096 m->action = umode_delete;
3100 action_requested = True;
3103 if (!strcmp ("--listproviders", argv[i]))
3105 list_providers = True;
3106 action_requested = True;
3109 if (!strcmp("--setprovideroutputsource", argv[i]))
3111 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
3112 set_name (&provider_name, argv[i], name_string|name_xid|name_index);
3114 set_name_xid (&output_source_provider_name, 0);
3116 set_name (&output_source_provider_name, argv[i], name_string|name_xid|name_index);
3117 action_requested = True;
3118 provsetoutsource = True;
3121 if (!strcmp("--setprovideroffloadsink", argv[i]))
3123 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
3124 set_name (&provider_name, argv[i], name_string|name_xid|name_index);
3126 set_name_xid (&offload_sink_provider_name, 0);
3128 set_name (&offload_sink_provider_name, argv[i], name_string|name_xid|name_index);
3129 action_requested = True;
3130 provsetoffsink = True;
3134 argerr ("unrecognized option '%s'\n", argv[i]);
3136 if (!action_requested)
3141 if (setit && !setit_1_2)
3145 printf("xrandr program version " VERSION "\n");
3147 dpy = XOpenDisplay (display_name);
3150 fprintf (stderr, "Can't open display %s\n", XDisplayName(display_name));
3154 screen = DefaultScreen (dpy);
3155 if (screen >= ScreenCount (dpy)) {
3156 fprintf (stderr, "Invalid screen number %d (display has %d)\n",
3157 screen, ScreenCount (dpy));
3161 root = RootWindow (dpy, screen);
3163 if (!XRRQueryExtension (dpy, &event_base, &error_base) ||
3164 !XRRQueryVersion (dpy, &major, &minor))
3166 fprintf (stderr, "RandR extension missing\n");
3169 if (major > 1 || (major == 1 && minor >= 2))
3171 if (major > 1 || (major == 1 && minor >= 3))
3173 if (major > 1 || (major == 1 && minor >= 4))
3176 if (has_1_2 && modeit)
3180 get_screen (current);
3184 for (m = umodes; m; m = m->next)
3189 switch (m->action) {
3191 XRRCreateMode (dpy, root, &m->mode);
3194 e = find_mode (&m->name, 0);
3196 fatal ("cannot find mode \"%s\"\n", m->name.string);
3197 XRRDestroyMode (dpy, e->id);
3200 o = find_output (&m->output);
3202 fatal ("cannot find output \"%s\"\n", m->output.string);
3203 e = find_mode (&m->name, 0);
3205 fatal ("cannot find mode \"%s\"\n", m->name.string);
3206 XRRAddOutputMode (dpy, o->output.xid, e->id);
3209 o = find_output (&m->output);
3211 fatal ("cannot find output \"%s\"\n", m->output.string);
3212 e = find_mode (&m->name, 0);
3214 fatal ("cannot find mode \"%s\"\n", m->name.string);
3215 XRRDeleteOutputMode (dpy, o->output.xid, e->id);
3225 if (has_1_2 && propit)
3229 get_screen (current);
3233 for (output = all_outputs; output; output = output->next)
3235 output_prop_t *prop;
3237 for (prop = output->props; prop; prop = prop->next)
3239 Atom name = XInternAtom (dpy, prop->name, False);
3242 unsigned char *data, *malloced_data = NULL;
3245 unsigned long ulong_value;
3246 unsigned char *prop_data;
3248 unsigned long nitems, bytes_after;
3250 XRRPropertyInfo *propinfo;
3252 type = AnyPropertyType;
3254 if (XRRGetOutputProperty (dpy, output->output.xid, name,
3255 0, 100, False, False,
3257 &actual_type, &actual_format,
3258 &nitems, &bytes_after, &prop_data) == Success &&
3260 (propinfo = XRRQueryOutputProperty(dpy, output->output.xid,
3264 format = actual_format;
3267 malloced_data = property_values_from_string
3268 (prop->value, type, actual_format, &nelements);
3272 data = malloced_data;
3274 format = actual_format;
3276 else if (type == AnyPropertyType &&
3277 (sscanf (prop->value, "%d", &int_value) == 1 ||
3278 sscanf (prop->value, "0x%x", &int_value) == 1))
3281 ulong_value = int_value;
3282 data = (unsigned char *) &ulong_value;
3286 else if (type == XA_ATOM)
3288 ulong_value = XInternAtom (dpy, prop->value, False);
3289 data = (unsigned char *) &ulong_value;
3292 else if (type == XA_STRING || type == AnyPropertyType)
3295 data = (unsigned char *) prop->value;
3296 nelements = strlen (prop->value);
3301 XRRChangeOutputProperty (dpy, output->output.xid,
3302 name, type, format, PropModeReplace,
3304 free (malloced_data);
3313 if (provsetoutsource)
3315 provider_t *provider, *source;
3318 fatal ("--setprovideroutputsource requires RandR 1.4\n");
3320 get_screen (current);
3323 provider = find_provider (&provider_name);
3324 source = find_provider(&output_source_provider_name);
3326 XRRSetProviderOutputSource(dpy, provider->provider.xid, source ? source->provider.xid : 0);
3330 provider_t *provider, *sink;
3333 fatal ("--setprovideroffloadsink requires RandR 1.4\n");
3335 get_screen (current);
3338 provider = find_provider (&provider_name);
3339 sink = find_provider(&offload_sink_provider_name);
3341 XRRSetProviderOffloadSink(dpy, provider->provider.xid, sink ? sink->provider.xid : 0);
3345 get_screen (current);
3354 * Assign outputs to crtcs
3359 * Mark changing crtcs
3361 mark_changing_crtcs ();
3364 * If an output was specified to track dpi, use it
3366 if (dpi_output_name)
3368 output_t *dpi_output = find_output_by_name (dpi_output_name);
3369 XRROutputInfo *output_info;
3370 XRRModeInfo *mode_info;
3372 fatal ("Cannot find output %s\n", dpi_output_name);
3373 output_info = dpi_output->output_info;
3374 mode_info = dpi_output->mode_info;
3375 if (output_info && mode_info && output_info->mm_height)
3378 * When this output covers the whole screen, just use
3379 * the known physical size
3381 if (fb_width == mode_info->width &&
3382 fb_height == mode_info->height)
3384 fb_width_mm = output_info->mm_width;
3385 fb_height_mm = output_info->mm_height;
3389 dpi = (25.4 * mode_info->height) / output_info->mm_height;
3395 * Compute physical screen size
3397 if (fb_width_mm == 0 || fb_height_mm == 0)
3399 if (fb_width != DisplayWidth (dpy, screen) ||
3400 fb_height != DisplayHeight (dpy, screen) || dpi != 0.0)
3403 dpi = (25.4 * DisplayHeight (dpy, screen)) / DisplayHeightMM(dpy, screen);
3405 fb_width_mm = (25.4 * fb_width) / dpi;
3406 fb_height_mm = (25.4 * fb_height) / dpi;
3410 fb_width_mm = DisplayWidthMM (dpy, screen);
3411 fb_height_mm = DisplayHeightMM (dpy, screen);
3421 * Set gamma on crtc's that belong to the outputs.
3426 * Now apply all of the changes
3433 if (query_1_2 || (query && has_1_2 && !query_1))
3438 #define ModeShown 0x80000000
3440 get_screen (current);
3444 printf ("Screen %d: minimum %d x %d, current %d x %d, maximum %d x %d\n",
3445 screen, minWidth, minHeight,
3446 DisplayWidth (dpy, screen), DisplayHeight(dpy, screen),
3447 maxWidth, maxHeight);
3449 for (output = all_outputs; output; output = output->next)
3451 XRROutputInfo *output_info = output->output_info;
3452 crtc_t *cur_crtc = output->crtc_info;
3453 XRRCrtcInfo *crtc_info = cur_crtc ? cur_crtc->crtc_info : NULL;
3454 XRRModeInfo *cur_mode = output->mode_info;
3458 Rotation rotations = output_rotations (output);
3460 printf ("%s %s", output_info->name, connection[output_info->connection]);
3461 if (output->primary) {
3467 printf (" %dx%d+%d+%d",
3468 crtc_info->width, crtc_info->height,
3469 crtc_info->x, crtc_info->y);
3471 printf (" %dx%d+%d+%d",
3472 cur_mode->width, cur_mode->height, output->x,
3476 printf (" (0x%x)", (int)cur_mode->id);
3477 if (output->rotation != RR_Rotate_0 || verbose)
3480 rotation_name (output->rotation));
3481 if (output->rotation & (RR_Reflect_X|RR_Reflect_Y))
3482 printf (" %s", reflection_name (output->rotation));
3485 if (rotations != RR_Rotate_0 || verbose)
3489 for (i = 0; i < 4; i ++) {
3490 if ((rotations >> i) & 1) {
3491 if (!first) printf (" "); first = False;
3492 printf("%s", direction[i]);
3495 if (rotations & RR_Reflect_X)
3497 if (!first) printf (" "); first = False;
3500 if (rotations & RR_Reflect_Y)
3502 if (!first) printf (" ");
3510 printf (" %dmm x %dmm",
3511 (int)output_info->mm_width, (int)output_info->mm_height);
3514 if (cur_crtc && cur_crtc->panning_info &&
3515 cur_crtc->panning_info->width > 0)
3517 XRRPanning *pan = cur_crtc->panning_info;
3518 printf (" panning %dx%d+%d+%d",
3519 pan->width, pan->height, pan->left, pan->top);
3520 if ((pan->track_width != 0 &&
3521 (pan->track_left != pan->left ||
3522 pan->track_width != pan->width ||
3523 pan->border_left != 0 ||
3524 pan->border_right != 0)) ||
3525 (pan->track_height != 0 &&
3526 (pan->track_top != pan->top ||
3527 pan->track_height != pan->height ||
3528 pan->border_top != 0 ||
3529 pan->border_bottom != 0)))
3530 printf (" tracking %dx%d+%d+%d border %d/%d/%d/%d",
3531 pan->track_width, pan->track_height,
3532 pan->track_left, pan->track_top,
3533 pan->border_left, pan->border_top,
3534 pan->border_right, pan->border_bottom);
3540 printf ("\tIdentifier: 0x%x\n", (int)output->output.xid);
3541 printf ("\tTimestamp: %d\n", (int)output_info->timestamp);
3542 printf ("\tSubpixel: %s\n", order[output_info->subpixel_order]);
3543 if (output->gamma.red != 0.0 && output->gamma.green != 0.0 && output->gamma.blue != 0.0) {
3544 printf ("\tGamma: %#.2g:%#.2g:%#.2g\n",
3545 output->gamma.red, output->gamma.green, output->gamma.blue);
3546 printf ("\tBrightness: %#.2g\n", output->brightness);
3548 printf ("\tClones: ");
3549 for (j = 0; j < output_info->nclone; j++)
3551 output_t *clone = find_output_by_xid (output_info->clones[j]);
3553 if (clone) printf (" %s", clone->output.string);
3556 if (output->crtc_info)
3557 printf ("\tCRTC: %d\n", output->crtc_info->crtc.index);
3558 printf ("\tCRTCs: ");
3559 for (j = 0; j < output_info->ncrtc; j++)
3561 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[j]);
3563 printf (" %d", crtc->crtc.index);
3566 if (output->crtc_info && output->crtc_info->panning_info) {
3567 XRRPanning *pan = output->crtc_info->panning_info;
3568 printf ("\tPanning: %dx%d+%d+%d\n",
3569 pan->width, pan->height, pan->left, pan->top);
3570 printf ("\tTracking: %dx%d+%d+%d\n",
3571 pan->track_width, pan->track_height,
3572 pan->track_left, pan->track_top);
3573 printf ("\tBorder: %d/%d/%d/%d\n",
3574 pan->border_left, pan->border_top,
3575 pan->border_right, pan->border_bottom);
3582 printf ("\tTransform: ");
3583 for (y = 0; y < 3; y++)
3585 for (x = 0; x < 3; x++)
3586 printf (" %f", XFixedToDouble (output->transform.transform.matrix[y][x]));
3590 if (output->transform.filter)
3591 printf ("\n\t filter: %s", output->transform.filter);
3594 if (verbose || properties)
3596 props = XRRListOutputProperties (dpy, output->output.xid,
3598 for (j = 0; j < nprop; j++) {
3599 unsigned char *prop;
3601 unsigned long nitems, bytes_after;
3603 XRRPropertyInfo *propinfo;
3604 char *atom_name = XGetAtomName (dpy, props[j]);
3607 XRRGetOutputProperty (dpy, output->output.xid, props[j],
3608 0, 100, False, False,
3610 &actual_type, &actual_format,
3611 &nitems, &bytes_after, &prop);
3613 propinfo = XRRQueryOutputProperty(dpy, output->output.xid,
3616 printf ("\t%s: ", atom_name);
3618 print_output_property(atom_name, actual_format,
3619 actual_type, nitems, prop);
3621 if (propinfo->range && propinfo->num_values > 0)
3623 printf ("\t\trange%s: ",
3624 (propinfo->num_values == 2) ? "" : "s");
3625 for (k = 0; k < propinfo->num_values / 2; k++)
3628 print_output_property_value (32, actual_type,
3629 (unsigned char *) &(propinfo->values[k * 2]));
3631 print_output_property_value (32, actual_type,
3632 (unsigned char *) &(propinfo->values[k * 2 + 1]));
3634 if (k < propinfo->num_values / 2 - 1)
3639 if (!propinfo->range && propinfo->num_values > 0)
3641 printf ("\t\tsupported: ");
3642 for (k = 0; k < propinfo->num_values; k++)
3644 print_output_property_value (32, actual_type,
3645 (unsigned char *) &(propinfo->values[k]));
3646 if (k < propinfo->num_values - 1)
3658 for (j = 0; j < output_info->nmode; j++)
3660 XRRModeInfo *mode = find_mode_by_xid (output_info->modes[j]);
3663 printf (" %s (0x%x) %6.3fMHz",
3664 mode->name, (int)mode->id,
3665 (double)mode->dotClock / 1000000.0);
3666 for (f = 0; mode_flags[f].flag; f++)
3667 if (mode->modeFlags & mode_flags[f].flag)
3668 printf (" %s", mode_flags[f].string);
3669 if (mode == output->mode_info)
3670 printf (" *current");
3671 if (j < output_info->npreferred)
3672 printf (" +preferred");
3674 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.2fKHz\n",
3675 mode->width, mode->hSyncStart, mode->hSyncEnd,
3676 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
3677 printf (" v: height %4d start %4d end %4d total %4d clock %6.2fHz\n",
3678 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
3679 mode_refresh (mode));
3680 mode->modeFlags |= ModeShown;
3685 mode_shown = calloc (output_info->nmode, sizeof (Bool));
3686 if (!mode_shown) fatal ("out of memory\n");
3687 for (j = 0; j < output_info->nmode; j++)
3689 XRRModeInfo *jmode, *kmode;
3692 if (mode_shown[j]) continue;
3694 jmode = find_mode_by_xid (output_info->modes[j]);
3696 printf (" %-12s", jmode->name);
3697 for (k = j; k < output_info->nmode; k++)
3699 if (mode_shown[k]) continue;
3700 kmode = find_mode_by_xid (output_info->modes[k]);
3701 if (strcmp (jmode->name, kmode->name) != 0) continue;
3702 mode_shown[k] = True;
3703 kmode->modeFlags |= ModeShown;
3704 printf (" %6.2f", mode_refresh (kmode));
3705 if (kmode == output->mode_info)
3709 if (k < output_info->npreferred)
3719 for (m = 0; m < res->nmode; m++)
3721 XRRModeInfo *mode = &res->modes[m];
3723 if (!(mode->modeFlags & ModeShown))
3725 printf (" %s (0x%x) %6.3fMHz\n",
3726 mode->name, (int)mode->id,
3727 (double)mode->dotClock / 1000000.0);
3728 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.2fKHz\n",
3729 mode->width, mode->hSyncStart, mode->hSyncEnd,
3730 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
3731 printf (" v: height %4d start %4d end %4d total %4d clock %6.2fHz\n",
3732 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
3733 mode_refresh (mode));
3738 if (list_providers) {
3742 printf ("RandR 1.4 not supported\n");
3746 get_screen (current);
3752 printf("Providers: number : %d\n", num_providers);
3754 for (j = 0; j < num_providers; j++) {
3755 provider_t *provider = &providers[j];
3756 XRRProviderInfo *info = provider->info;
3758 printf("Provider %d: id: 0x%x cap: 0x%x", j, (int)provider->provider.xid, info->capabilities);
3759 for (k = 0; k < 4; k++)
3760 if (info->capabilities & (1 << k))
3761 printf(", %s", capability_name(1<<k));
3763 printf(" crtcs: %d outputs: %d associated providers: %d name:%s\n", info->ncrtcs, info->noutputs, info->nassociatedproviders, info->name);
3768 sc = XRRGetScreenInfo (dpy, root);
3773 current_size = XRRConfigCurrentConfiguration (sc, ¤t_rotation);
3775 sizes = XRRConfigSizes(sc, &nsize);
3777 if (have_pixel_size) {
3778 for (size = 0; size < nsize; size++)
3780 if (sizes[size].width == width && sizes[size].height == height)
3783 if (size >= nsize) {
3785 "Size %dx%d not found in available modes\n", width, height);
3790 size = current_size;
3791 else if (size >= nsize) {
3793 "Size index %d is too large, there are only %d sizes\n",
3800 for (rot = 0; rot < 4; rot++)
3801 if (1 << rot == (current_rotation & 0xf))
3805 current_rate = XRRConfigCurrentRate (sc);
3809 if (size == current_size)
3810 rate = current_rate;
3816 rates = XRRConfigRates (sc, size, &nrate);
3817 for (i = 0; i < nrate; i++)
3818 if (rate == rates[i])
3821 fprintf (stderr, "Rate %.2f Hz not available for this size\n", rate);
3827 int major_version, minor_version;
3828 XRRQueryVersion (dpy, &major_version, &minor_version);
3829 printf("Server reports RandR version %d.%d\n",
3830 major_version, minor_version);
3833 if (query || query_1) {
3834 printf(" SZ: Pixels Physical Refresh\n");
3835 for (i = 0; i < nsize; i++) {
3838 printf ("%c%-2d %5d x %-5d (%4dmm x%4dmm )",
3839 i == current_size ? '*' : ' ',
3840 i, sizes[i].width, sizes[i].height,
3841 sizes[i].mwidth, sizes[i].mheight);
3842 rates = XRRConfigRates (sc, i, &nrate);
3843 if (nrate) printf (" ");
3844 for (j = 0; j < nrate; j++)
3846 i == current_size && rates[j] == current_rate ? '*' : ' ',
3853 Rotation rotations = XRRConfigRotations(sc, ¤t_rotation);
3855 if (toggle_x && !(current_rotation & RR_Reflect_X)) reflection |= RR_Reflect_X;
3856 if (toggle_y && !(current_rotation & RR_Reflect_Y)) reflection |= RR_Reflect_Y;
3859 printf("Current rotation - %s\n",
3860 rotation_name (current_rotation));
3862 printf("Current reflection - %s\n",
3863 reflection_name (current_rotation));
3865 printf ("Rotations possible - ");
3866 for (i = 0; i < 4; i ++) {
3867 if ((rotations >> i) & 1) printf("%s ", direction[i]);
3871 printf ("Reflections possible - ");
3872 if (rotations & (RR_Reflect_X|RR_Reflect_Y))
3874 if (rotations & RR_Reflect_X) printf ("X Axis ");
3875 if (rotations & RR_Reflect_Y) printf ("Y Axis");
3884 printf("Setting size to %d, rotation to %s\n", size, direction[rot]);
3886 printf ("Setting reflection on ");
3889 if (reflection & RR_Reflect_X) printf ("X Axis ");
3890 if (reflection & RR_Reflect_Y) printf ("Y Axis");
3893 printf ("neither axis");
3897 /* we should test configureNotify on the root window */
3898 XSelectInput (dpy, root, StructureNotifyMask);
3900 if (setit && !dryrun) XRRSelectInput (dpy, root,
3901 RRScreenChangeNotifyMask);
3902 if (setit && !dryrun) {
3903 Rotation rotation = 1 << rot;
3904 status = XRRSetScreenConfigAndRate (dpy, sc, root, (SizeID) size,
3905 (Rotation) (rotation | reflection),
3909 if (setit && !dryrun && status == RRSetConfigFailed) {
3910 printf ("Failed to change the screen configuration!\n");
3914 if (verbose && setit && !dryrun && size != current_size) {
3915 if (status == RRSetConfigSuccess)
3917 Bool seen_screen = False;
3918 while (!seen_screen) {
3920 XNextEvent(dpy, (XEvent *) &event);
3922 printf ("Event received, type = %d\n", event.type);
3923 /* update Xlib's knowledge of the event */
3924 XRRUpdateConfiguration (&event);
3925 if (event.type == ConfigureNotify)
3926 printf("Received ConfigureNotify Event!\n");
3928 switch (event.type - event_base) {
3929 case RRScreenChangeNotify:
3930 sce = (XRRScreenChangeNotifyEvent *) &event;
3932 printf("Got a screen change notify event!\n");
3933 printf(" window = %d\n root = %d\n size_index = %d\n rotation %d\n",
3934 (int) sce->window, (int) sce->root,
3935 sce->size_index, sce->rotation);
3936 printf(" timestamp = %ld, config_timestamp = %ld\n",
3937 sce->timestamp, sce->config_timestamp);
3938 printf(" Rotation = %x\n", sce->rotation);
3939 printf(" %d X %d pixels, %d X %d mm\n",
3940 sce->width, sce->height, sce->mwidth, sce->mheight);
3941 printf("Display width %d, height %d\n",
3942 DisplayWidth(dpy, screen), DisplayHeight(dpy, screen));
3943 printf("Display widthmm %d, heightmm %d\n",
3944 DisplayWidthMM(dpy, screen), DisplayHeightMM(dpy, screen));
3945 spo = sce->subpixel_order;
3946 if ((spo < 0) || (spo > 5))
3947 printf ("Unknown subpixel order, value = %d\n", spo);
3948 else printf ("new Subpixel rendering model is %s\n", order[spo]);
3952 if (event.type != ConfigureNotify)
3953 printf("unknown event received, type = %d!\n", event.type);
3958 XRRFreeScreenConfigInfo(sc);