2 * Copyright © 2001 Keith Packard, member of The XFree86 Project, Inc.
3 * Copyright © 2002 Hewlett Packard Company, Inc.
4 * Copyright © 2006 Intel Corporation
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that copyright
9 * notice and this permission notice appear in supporting documentation, and
10 * that the name of the copyright holders not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. The copyright holders make no representations
13 * about the suitability of this software for any purpose. It is provided "as
14 * is" without express or implied warranty.
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * Thanks to Jim Gettys who wrote most of the client side code,
25 * and part of the server code for randr.
30 #include <X11/Xlibint.h>
31 #include <X11/Xproto.h>
32 #include <X11/Xatom.h>
33 #include <X11/extensions/Xrandr.h>
34 #include <X11/extensions/Xrender.h> /* we share subpixel information */
45 static char *program_name;
48 static int screen = -1;
49 static Bool verbose = False;
50 static Bool automatic = False;
51 static Bool properties = False;
52 static Bool providers = False;
53 static Bool grab_server = True;
54 static Bool no_primary = False;
56 static const char *direction[5] = {
63 static const char *reflections[5] = {
71 static const char *order[6] = {
83 { "+HSync", RR_HSyncPositive },
84 { "-HSync", RR_HSyncNegative },
85 { "+VSync", RR_VSyncPositive },
86 { "-VSync", RR_VSyncNegative },
87 { "Interlace", RR_Interlace },
88 { "DoubleScan", RR_DoubleScan },
89 { "CSync", RR_CSync },
90 { "+CSync", RR_CSyncPositive },
91 { "-CSync", RR_CSyncNegative },
95 static void _X_NORETURN
98 fprintf(stderr, "usage: %s [options]\n", program_name);
99 fprintf(stderr, " where options are:\n");
100 fprintf(stderr, " -display <display> or -d <display>\n");
101 fprintf(stderr, " -help\n");
102 fprintf(stderr, " -o <normal,inverted,left,right,0,1,2,3>\n");
103 fprintf(stderr, " or --orientation <normal,inverted,left,right,0,1,2,3>\n");
104 fprintf(stderr, " -q or --query\n");
105 fprintf(stderr, " -s <size>/<width>x<height> or --size <size>/<width>x<height>\n");
106 fprintf(stderr, " -r <rate> or --rate <rate> or --refresh <rate>\n");
107 fprintf(stderr, " -v or --version\n");
108 fprintf(stderr, " -x (reflect in x)\n");
109 fprintf(stderr, " -y (reflect in y)\n");
110 fprintf(stderr, " --screen <screen>\n");
111 fprintf(stderr, " --verbose\n");
112 fprintf(stderr, " --current\n");
113 fprintf(stderr, " --dryrun\n");
114 fprintf(stderr, " --nograb\n");
115 fprintf(stderr, " --prop or --properties\n");
116 fprintf(stderr, " --fb <width>x<height>\n");
117 fprintf(stderr, " --fbmm <width>x<height>\n");
118 fprintf(stderr, " --dpi <dpi>/<output>\n");
119 fprintf(stderr, " --output <output>\n");
120 fprintf(stderr, " --auto\n");
121 fprintf(stderr, " --mode <mode>\n");
122 fprintf(stderr, " --preferred\n");
123 fprintf(stderr, " --pos <x>x<y>\n");
124 fprintf(stderr, " --rate <rate> or --refresh <rate>\n");
125 fprintf(stderr, " --reflect normal,x,y,xy\n");
126 fprintf(stderr, " --rotate normal,inverted,left,right\n");
127 fprintf(stderr, " --left-of <output>\n");
128 fprintf(stderr, " --right-of <output>\n");
129 fprintf(stderr, " --above <output>\n");
130 fprintf(stderr, " --below <output>\n");
131 fprintf(stderr, " --same-as <output>\n");
132 fprintf(stderr, " --set <property> <value>\n");
133 fprintf(stderr, " --scale <x>x<y>\n");
134 fprintf(stderr, " --scale-from <w>x<h>\n");
135 fprintf(stderr, " --transform <a>,<b>,<c>,<d>,<e>,<f>,<g>,<h>,<i>\n");
136 fprintf(stderr, " --off\n");
137 fprintf(stderr, " --crtc <crtc>\n");
138 fprintf(stderr, " --panning <w>x<h>[+<x>+<y>[/<track:w>x<h>+<x>+<y>[/<border:l>/<t>/<r>/<b>]]]\n");
139 fprintf(stderr, " --gamma <r>:<g>:<b>\n");
140 fprintf(stderr, " --primary\n");
141 fprintf(stderr, " --noprimary\n");
142 fprintf(stderr, " --newmode <name> <clock MHz>\n");
143 fprintf(stderr, " <hdisp> <hsync-start> <hsync-end> <htotal>\n");
144 fprintf(stderr, " <vdisp> <vsync-start> <vsync-end> <vtotal>\n");
145 fprintf(stderr, " [flags...]\n");
146 fprintf(stderr, " Valid flags: +HSync -HSync +VSync -VSync\n");
147 fprintf(stderr, " +CSync -CSync CSync Interlace DoubleScan\n");
148 fprintf(stderr, " --rmmode <name>\n");
149 fprintf(stderr, " --addmode <output> <name>\n");
150 fprintf(stderr, " --delmode <output> <name>\n");
151 fprintf(stderr, " --listproviders\n");
152 fprintf(stderr, " --setprovideroutputsource <prov-xid> <source-xid>\n");
153 fprintf(stderr, " --setprovideroffloadsink <prov-xid> <sink-xid>\n");
159 static void _X_NORETURN
160 fatal (const char *format, ...)
164 va_start (ap, format);
165 fprintf (stderr, "%s: ", program_name);
166 vfprintf (stderr, format, ap);
173 warning (const char *format, ...)
177 va_start (ap, format);
178 fprintf (stderr, "%s: ", program_name);
179 vfprintf (stderr, format, ap);
183 /* Because fmin requires C99 suppport */
184 static inline double dmin (double x, double y)
186 return x < y ? x : y;
190 rotation_name (Rotation rotation)
194 if ((rotation & 0xf) == 0)
196 for (i = 0; i < 4; i++)
197 if (rotation & (1 << i))
199 return "invalid rotation";
203 reflection_name (Rotation rotation)
205 rotation &= (RR_Reflect_X|RR_Reflect_Y);
213 case RR_Reflect_X|RR_Reflect_Y:
214 return "X and Y axis";
216 return "invalid reflection";
220 capability_name (int cap_bit)
223 case RR_Capability_SourceOutput:
224 return "Source Output";
225 case RR_Capability_SinkOutput:
226 return "Sink Output";
227 case RR_Capability_SourceOffload:
228 return "Source Offload";
229 case RR_Capability_SinkOffload:
230 return "Sink Offload";
232 return "invalid capability";
235 typedef enum _relation {
244 int x, y, width, height;
255 typedef enum _changes {
257 changes_crtc = (1 << 0),
258 changes_mode = (1 << 1),
259 changes_relation = (1 << 2),
260 changes_position = (1 << 3),
261 changes_rotation = (1 << 4),
262 changes_reflection = (1 << 5),
263 changes_automatic = (1 << 6),
264 changes_refresh = (1 << 7),
265 changes_property = (1 << 8),
266 changes_transform = (1 << 9),
267 changes_panning = (1 << 10),
268 changes_gamma = (1 << 11),
269 changes_primary = (1 << 12),
272 typedef enum _name_kind {
274 name_string = (1 << 0),
276 name_index = (1 << 2),
277 name_preferred = (1 << 3),
287 typedef struct _crtc crtc_t;
288 typedef struct _output output_t;
289 typedef struct _transform transform_t;
290 typedef struct _umode umode_t;
291 typedef struct _output_prop output_prop_t;
294 XTransform transform;
303 XRRCrtcInfo *crtc_info;
305 XRRModeInfo *mode_info;
306 XRRPanning *panning_info;
312 transform_t current_transform, pending_transform;
315 struct _output_prop {
316 struct _output_prop *next;
322 struct _output *next;
326 output_prop_t *props;
329 XRROutputInfo *output_info;
333 crtc_t *current_crtc_info;
337 XRRModeInfo *mode_info;
350 int scale_from_w, scale_from_h;
351 transform_t transform;
366 typedef enum _umode_action {
367 umode_create, umode_destroy, umode_add, umode_delete
374 umode_action_t action;
380 static const char *connection[3] = {
383 "unknown connection"};
385 #define OUTPUT_NAME 1
389 #define CRTC_INDEX 0x40000000
398 static output_t *outputs = NULL;
399 static output_t **outputs_tail = &outputs;
400 static crtc_t *crtcs;
401 static umode_t *umodes;
402 static int num_crtcs;
403 static XRRScreenResources *res;
404 static int fb_width = 0, fb_height = 0;
405 static int fb_width_mm = 0, fb_height_mm = 0;
406 static double dpi = 0;
407 static char *dpi_output = NULL;
408 static Bool dryrun = False;
409 static int minWidth, maxWidth, minHeight, maxHeight;
410 static Bool has_1_2 = False;
411 static Bool has_1_3 = False;
412 static int provider_xid, output_source_provider_xid, offload_sink_provider_xid;
415 mode_height (XRRModeInfo *mode_info, Rotation rotation)
417 switch (rotation & 0xf) {
420 return mode_info->height;
423 return mode_info->width;
430 mode_width (XRRModeInfo *mode_info, Rotation rotation)
432 switch (rotation & 0xf) {
435 return mode_info->width;
438 return mode_info->height;
445 transform_point (XTransform *transform, double *xp, double *yp)
455 for (j = 0; j < 3; j++)
458 for (i = 0; i < 3; i++)
459 v += (XFixedToDouble (transform->matrix[j][i]) * vector[i]);
464 for (j = 0; j < 2; j++) {
465 vector[j] = result[j] / result[2];
466 if (vector[j] > 32767 || vector[j] < -32767)
475 path_bounds (XTransform *transform, point_t *points, int npoints, box_t *box)
480 for (i = 0; i < npoints; i++) {
484 transform_point (transform, &x, &y);
485 point.x1 = floor (x);
486 point.y1 = floor (y);
492 if (point.x1 < box->x1) box->x1 = point.x1;
493 if (point.y1 < box->y1) box->y1 = point.y1;
494 if (point.x2 > box->x2) box->x2 = point.x2;
495 if (point.y2 > box->y2) box->y2 = point.y2;
501 mode_geometry (XRRModeInfo *mode_info, Rotation rotation,
502 XTransform *transform,
506 int width = mode_width (mode_info, rotation);
507 int height = mode_height (mode_info, rotation);
517 path_bounds (transform, rect, 4, bounds);
520 /* v refresh frequency in Hz */
522 mode_refresh (XRRModeInfo *mode_info)
526 if (mode_info->hTotal && mode_info->vTotal)
527 rate = ((double) mode_info->dotClock /
528 ((double) mode_info->hTotal * (double) mode_info->vTotal));
534 /* h sync frequency in Hz */
536 mode_hsync (XRRModeInfo *mode_info)
540 if (mode_info->hTotal)
541 rate = (double) mode_info->dotClock / (double) mode_info->hTotal;
548 init_name (name_t *name)
550 name->kind = name_none;
554 set_name_string (name_t *name, char *string)
556 name->kind |= name_string;
557 name->string = string;
561 set_name_xid (name_t *name, XID xid)
563 name->kind |= name_xid;
568 set_name_index (name_t *name, int index)
570 name->kind |= name_index;
575 set_name_preferred (name_t *name)
577 name->kind |= name_preferred;
581 set_name_all (name_t *name, name_t *old)
583 if (old->kind & name_xid)
584 name->xid = old->xid;
585 if (old->kind & name_string)
586 name->string = old->string;
587 if (old->kind & name_index)
588 name->index = old->index;
589 name->kind |= old->kind;
593 set_name (name_t *name, char *string, name_kind_t valid)
595 unsigned int xid; /* don't make it XID (which is unsigned long):
596 scanf() takes unsigned int */
599 if ((valid & name_xid) && sscanf (string, "0x%x", &xid) == 1)
600 set_name_xid (name, xid);
601 else if ((valid & name_index) && sscanf (string, "%d", &index) == 1)
602 set_name_index (name, index);
603 else if (valid & name_string)
604 set_name_string (name, string);
610 init_transform (transform_t *transform)
613 memset (&transform->transform, '\0', sizeof (transform->transform));
614 for (x = 0; x < 3; x++)
615 transform->transform.matrix[x][x] = XDoubleToFixed (1.0);
616 transform->filter = "";
617 transform->nparams = 0;
618 transform->params = NULL;
622 set_transform (transform_t *dest,
623 XTransform *transform,
628 dest->transform = *transform;
629 /* note: this string is leaked */
630 dest->filter = strdup (filter);
631 dest->nparams = nparams;
632 dest->params = malloc (nparams * sizeof (XFixed));
633 memcpy (dest->params, params, nparams * sizeof (XFixed));
637 copy_transform (transform_t *dest, transform_t *src)
639 set_transform (dest, &src->transform,
640 src->filter, src->params, src->nparams);
644 equal_transform (transform_t *a, transform_t *b)
646 if (memcmp (&a->transform, &b->transform, sizeof (XTransform)) != 0)
648 if (strcmp (a->filter, b->filter) != 0)
650 if (a->nparams != b->nparams)
652 if (memcmp (a->params, b->params, a->nparams * sizeof (XFixed)) != 0)
660 output_t *output = calloc (1, sizeof (output_t));
663 fatal ("out of memory\n");
665 output->found = False;
666 output->brightness = 1.0;
667 *outputs_tail = output;
668 outputs_tail = &output->next;
673 find_output (name_t *name)
677 for (output = outputs; output; output = output->next)
679 name_kind_t common = name->kind & output->output.kind;
681 if ((common & name_xid) && name->xid == output->output.xid)
683 if ((common & name_string) && !strcmp (name->string, output->output.string))
685 if ((common & name_index) && name->index == output->output.index)
692 find_output_by_xid (RROutput output)
696 init_name (&output_name);
697 set_name_xid (&output_name, output);
698 return find_output (&output_name);
702 find_output_by_name (char *name)
706 init_name (&output_name);
707 set_name_string (&output_name, name);
708 return find_output (&output_name);
712 find_crtc (name_t *name)
717 for (c = 0; c < num_crtcs; c++)
722 common = name->kind & crtc->crtc.kind;
724 if ((common & name_xid) && name->xid == crtc->crtc.xid)
726 if ((common & name_string) && !strcmp (name->string, crtc->crtc.string))
728 if ((common & name_index) && name->index == crtc->crtc.index)
736 find_crtc_by_xid (RRCrtc crtc)
740 init_name (&crtc_name);
741 set_name_xid (&crtc_name, crtc);
742 return find_crtc (&crtc_name);
746 find_mode (name_t *name, double refresh)
749 XRRModeInfo *best = NULL;
752 for (m = 0; m < res->nmode; m++)
754 XRRModeInfo *mode = &res->modes[m];
755 if ((name->kind & name_xid) && name->xid == mode->id)
760 if ((name->kind & name_string) && !strcmp (name->string, mode->name))
765 dist = fabs (mode_refresh (mode) - refresh);
768 if (!best || dist < bestDist)
779 find_mode_by_xid (RRMode mode)
783 init_name (&mode_name);
784 set_name_xid (&mode_name, mode);
785 return find_mode (&mode_name, 0);
790 find_mode_by_name (char *name)
793 init_name (&mode_name);
794 set_name_string (&mode_name, name);
795 return find_mode (&mode_name, 0);
801 find_mode_for_output (output_t *output, name_t *name)
803 XRROutputInfo *output_info = output->output_info;
805 XRRModeInfo *best = NULL;
808 for (m = 0; m < output_info->nmode; m++)
812 mode = find_mode_by_xid (output_info->modes[m]);
814 if ((name->kind & name_xid) && name->xid == mode->id)
819 if ((name->kind & name_string) && !strcmp (name->string, mode->name))
823 /* Stay away from doublescan modes unless refresh rate is specified. */
824 if (!output->refresh && (mode->modeFlags & RR_DoubleScan))
828 dist = fabs (mode_refresh (mode) - output->refresh);
831 if (!best || dist < bestDist)
842 preferred_mode (output_t *output)
844 XRROutputInfo *output_info = output->output_info;
851 for (m = 0; m < output_info->nmode; m++)
853 XRRModeInfo *mode_info = find_mode_by_xid (output_info->modes[m]);
856 if (m < output_info->npreferred)
858 else if (output_info->mm_height)
859 dist = (1000 * DisplayHeight(dpy, screen) / DisplayHeightMM(dpy, screen) -
860 1000 * mode_info->height / output_info->mm_height);
862 dist = DisplayHeight(dpy, screen) - mode_info->height;
864 if (dist < 0) dist = -dist;
865 if (!best || dist < bestDist)
875 output_can_use_crtc (output_t *output, crtc_t *crtc)
877 XRROutputInfo *output_info = output->output_info;
880 for (c = 0; c < output_info->ncrtc; c++)
881 if (output_info->crtcs[c] == crtc->crtc.xid)
887 output_can_use_mode (output_t *output, XRRModeInfo *mode)
889 XRROutputInfo *output_info = output->output_info;
892 for (m = 0; m < output_info->nmode; m++)
893 if (output_info->modes[m] == mode->id)
899 crtc_can_use_rotation (crtc_t *crtc, Rotation rotation)
901 Rotation rotations = crtc->crtc_info->rotations;
902 Rotation dir = rotation & (RR_Rotate_0|RR_Rotate_90|RR_Rotate_180|RR_Rotate_270);
903 Rotation reflect = rotation & (RR_Reflect_X|RR_Reflect_Y);
904 if (((rotations & dir) != 0) && ((rotations & reflect) == reflect))
911 crtc_can_use_transform (crtc_t *crtc, XTransform *transform)
915 XRRQueryVersion (dpy, &major, &minor);
916 if (major > 1 || (major == 1 && minor >= 3))
923 * Report only rotations that are supported by all crtcs
926 output_rotations (output_t *output)
929 Rotation rotation = RR_Rotate_0;
930 XRROutputInfo *output_info = output->output_info;
933 for (c = 0; c < output_info->ncrtc; c++)
935 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
939 rotation = crtc->crtc_info->rotations;
942 rotation &= crtc->crtc_info->rotations;
949 output_can_use_rotation (output_t *output, Rotation rotation)
951 XRROutputInfo *output_info = output->output_info;
954 /* make sure all of the crtcs can use this rotation.
955 * yes, this is not strictly necessary, but it is
956 * simpler,and we expect most drivers to either
957 * support rotation everywhere or nowhere
959 for (c = 0; c < output_info->ncrtc; c++)
961 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
962 if (crtc && !crtc_can_use_rotation (crtc, rotation))
969 output_is_primary(output_t *output)
972 return XRRGetOutputPrimary(dpy, root) == output->output.xid;
976 /* Returns the index of the last value in an array < 0xffff */
978 find_last_non_clamped(CARD16 array[], int size) {
980 for (i = size - 1; i > 0; i--) {
981 if (array[i] < 0xffff)
988 set_gamma_info(output_t *output)
991 double i1, v1, i2, v2;
992 int size, middle, last_best, last_red, last_green, last_blue;
995 if (!output->crtc_info)
998 size = XRRGetCrtcGammaSize(dpy, output->crtc_info->crtc.xid);
1000 warning("Failed to get size of gamma for output %s\n", output->output.string);
1004 gamma = XRRGetCrtcGamma(dpy, output->crtc_info->crtc.xid);
1006 warning("Failed to get gamma for output %s\n", output->output.string);
1011 * Here is a bit tricky because gamma is a whole curve for each
1012 * color. So, typically, we need to represent 3 * 256 values as 3 + 1
1013 * values. Therefore, we approximate the gamma curve (v) by supposing
1014 * it always follows the way we set it: a power function (i^g)
1015 * multiplied by a brightness (b).
1017 * so g = (ln(v) - ln(b))/ln(i)
1018 * and b can be found using two points (v1,i1) and (v2, i2):
1019 * b = e^((ln(v2)*ln(i1) - ln(v1)*ln(i2))/ln(i1/i2))
1020 * For the best resolution, we select i2 at the highest place not
1021 * clamped and i1 at i2/2. Note that if i2 = 1 (as in most normal
1022 * cases), then b = v2.
1024 last_red = find_last_non_clamped(gamma->red, size);
1025 last_green = find_last_non_clamped(gamma->green, size);
1026 last_blue = find_last_non_clamped(gamma->blue, size);
1027 best_array = gamma->red;
1028 last_best = last_red;
1029 if (last_green > last_best) {
1030 last_best = last_green;
1031 best_array = gamma->green;
1033 if (last_blue > last_best) {
1034 last_best = last_blue;
1035 best_array = gamma->blue;
1040 middle = last_best / 2;
1041 i1 = (double)(middle + 1) / size;
1042 v1 = (double)(best_array[middle]) / 65535;
1043 i2 = (double)(last_best + 1) / size;
1044 v2 = (double)(best_array[last_best]) / 65535;
1045 if (v2 < 0.0001) { /* The screen is black */
1046 output->brightness = 0;
1047 output->gamma.red = 1;
1048 output->gamma.green = 1;
1049 output->gamma.blue = 1;
1051 if ((last_best + 1) == size)
1052 output->brightness = v2;
1054 output->brightness = exp((log(v2)*log(i1) - log(v1)*log(i2))/log(i1/i2));
1055 output->gamma.red = log((double)(gamma->red[last_red / 2]) / output->brightness
1056 / 65535) / log((double)((last_red / 2) + 1) / size);
1057 output->gamma.green = log((double)(gamma->green[last_green / 2]) / output->brightness
1058 / 65535) / log((double)((last_green / 2) + 1) / size);
1059 output->gamma.blue = log((double)(gamma->blue[last_blue / 2]) / output->brightness
1060 / 65535) / log((double)((last_blue / 2) + 1) / size);
1063 XRRFreeGamma(gamma);
1067 set_output_info (output_t *output, RROutput xid, XRROutputInfo *output_info)
1069 /* sanity check output info */
1070 if (output_info->connection != RR_Disconnected && !output_info->nmode)
1071 warning ("Output %s is not disconnected but has no modes\n",
1074 /* set output name and info */
1075 if (!(output->output.kind & name_xid))
1076 set_name_xid (&output->output, xid);
1077 if (!(output->output.kind & name_string))
1078 set_name_string (&output->output, output_info->name);
1079 output->output_info = output_info;
1081 /* set crtc name and info */
1082 if (!(output->changes & changes_crtc))
1083 set_name_xid (&output->crtc, output_info->crtc);
1085 if (output->crtc.kind == name_xid && output->crtc.xid == None)
1086 output->crtc_info = NULL;
1089 output->crtc_info = find_crtc (&output->crtc);
1090 if (!output->crtc_info)
1092 if (output->crtc.kind & name_xid)
1093 fatal ("cannot find crtc 0x%x\n", output->crtc.xid);
1094 if (output->crtc.kind & name_index)
1095 fatal ("cannot find crtc %d\n", output->crtc.index);
1097 if (!output_can_use_crtc (output, output->crtc_info))
1098 fatal ("output %s cannot use crtc 0x%x\n", output->output.string,
1099 output->crtc_info->crtc.xid);
1102 /* set mode name and info */
1103 if (!(output->changes & changes_mode))
1105 crtc_t *crtc = NULL;
1107 if (output_info->crtc)
1108 crtc = find_crtc_by_xid(output_info->crtc);
1109 if (crtc && crtc->crtc_info)
1110 set_name_xid (&output->mode, crtc->crtc_info->mode);
1111 else if (output->crtc_info)
1112 set_name_xid (&output->mode, output->crtc_info->crtc_info->mode);
1114 set_name_xid (&output->mode, None);
1115 if (output->mode.xid)
1117 output->mode_info = find_mode_by_xid (output->mode.xid);
1118 if (!output->mode_info)
1119 fatal ("server did not report mode 0x%x for output %s\n",
1120 output->mode.xid, output->output.string);
1123 output->mode_info = NULL;
1125 else if (output->mode.kind == name_xid && output->mode.xid == None)
1126 output->mode_info = NULL;
1129 if (output->mode.kind == name_preferred)
1130 output->mode_info = preferred_mode (output);
1132 output->mode_info = find_mode_for_output (output, &output->mode);
1133 if (!output->mode_info)
1135 if (output->mode.kind & name_preferred)
1136 fatal ("cannot find preferred mode\n");
1137 if (output->mode.kind & name_string)
1138 fatal ("cannot find mode %s\n", output->mode.string);
1139 if (output->mode.kind & name_xid)
1140 fatal ("cannot find mode 0x%x\n", output->mode.xid);
1142 if (!output_can_use_mode (output, output->mode_info))
1143 fatal ("output %s cannot use mode %s\n", output->output.string,
1144 output->mode_info->name);
1148 if (!(output->changes & changes_position))
1150 if (output->crtc_info)
1152 output->x = output->crtc_info->crtc_info->x;
1153 output->y = output->crtc_info->crtc_info->y;
1163 if (!(output->changes & changes_rotation))
1165 output->rotation &= ~0xf;
1166 if (output->crtc_info)
1167 output->rotation |= (output->crtc_info->crtc_info->rotation & 0xf);
1169 output->rotation = RR_Rotate_0;
1171 if (!(output->changes & changes_reflection))
1173 output->rotation &= ~(RR_Reflect_X|RR_Reflect_Y);
1174 if (output->crtc_info)
1175 output->rotation |= (output->crtc_info->crtc_info->rotation &
1176 (RR_Reflect_X|RR_Reflect_Y));
1178 if (!output_can_use_rotation (output, output->rotation))
1179 fatal ("output %s cannot use rotation \"%s\" reflection \"%s\"\n",
1180 output->output.string,
1181 rotation_name (output->rotation),
1182 reflection_name (output->rotation));
1185 if (!(output->changes & changes_gamma))
1186 set_gamma_info(output);
1188 /* set transformation */
1189 if (!(output->changes & changes_transform))
1191 if (output->crtc_info)
1192 copy_transform (&output->transform, &output->crtc_info->current_transform);
1194 init_transform (&output->transform);
1196 /* transform was already set for --scale or --transform */
1198 /* for --scale-from, figure out the mode size and compute the transform
1199 * for the target framebuffer area */
1200 if (output->scale_from_w > 0 && output->mode_info) {
1201 double sx = (double)output->scale_from_w /
1202 output->mode_info->width;
1203 double sy = (double)output->scale_from_h /
1204 output->mode_info->height;
1206 printf("scaling %s by %lfx%lf\n", output->output.string, sx,
1208 init_transform (&output->transform);
1209 output->transform.transform.matrix[0][0] = XDoubleToFixed (sx);
1210 output->transform.transform.matrix[1][1] = XDoubleToFixed (sy);
1211 output->transform.transform.matrix[2][2] = XDoubleToFixed (1.0);
1212 if (sx != 1 || sy != 1)
1213 output->transform.filter = "bilinear";
1215 output->transform.filter = "nearest";
1216 output->transform.nparams = 0;
1217 output->transform.params = NULL;
1222 if (!(output->changes & changes_primary))
1223 output->primary = output_is_primary(output);
1227 get_screen (Bool current)
1230 fatal ("Server RandR version before 1.2\n");
1232 XRRGetScreenSizeRange (dpy, root, &minWidth, &minHeight,
1233 &maxWidth, &maxHeight);
1236 res = XRRGetScreenResourcesCurrent (dpy, root);
1238 res = XRRGetScreenResources (dpy, root);
1239 if (!res) fatal ("could not get screen resources");
1247 num_crtcs = res->ncrtc;
1248 crtcs = calloc (num_crtcs, sizeof (crtc_t));
1249 if (!crtcs) fatal ("out of memory\n");
1251 for (c = 0; c < res->ncrtc; c++)
1253 XRRCrtcInfo *crtc_info = XRRGetCrtcInfo (dpy, res, res->crtcs[c]);
1254 XRRCrtcTransformAttributes *attr;
1255 XRRPanning *panning_info = NULL;
1259 memset(&zero, 0, sizeof(zero));
1260 panning_info = XRRGetPanning (dpy, res, res->crtcs[c]);
1261 zero.timestamp = panning_info->timestamp;
1262 if (!memcmp(panning_info, &zero, sizeof(zero))) {
1263 Xfree(panning_info);
1264 panning_info = NULL;
1268 set_name_xid (&crtcs[c].crtc, res->crtcs[c]);
1269 set_name_index (&crtcs[c].crtc, c);
1270 if (!crtc_info) fatal ("could not get crtc 0x%x information\n", res->crtcs[c]);
1271 crtcs[c].crtc_info = crtc_info;
1272 crtcs[c].panning_info = panning_info;
1273 if (crtc_info->mode == None)
1275 crtcs[c].mode_info = NULL;
1278 crtcs[c].rotation = RR_Rotate_0;
1280 if (XRRGetCrtcTransform (dpy, res->crtcs[c], &attr) && attr) {
1281 set_transform (&crtcs[c].current_transform,
1282 &attr->currentTransform,
1283 attr->currentFilter,
1284 attr->currentParams,
1285 attr->currentNparams);
1290 init_transform (&crtcs[c].current_transform);
1292 copy_transform (&crtcs[c].pending_transform, &crtcs[c].current_transform);
1297 crtc_add_output (crtc_t *crtc, output_t *output)
1300 crtc->outputs = realloc (crtc->outputs, (crtc->noutput + 1) * sizeof (output_t *));
1303 crtc->outputs = malloc (sizeof (output_t *));
1304 crtc->x = output->x;
1305 crtc->y = output->y;
1306 crtc->rotation = output->rotation;
1307 crtc->mode_info = output->mode_info;
1308 copy_transform (&crtc->pending_transform, &output->transform);
1310 if (!crtc->outputs) fatal ("out of memory\n");
1311 crtc->outputs[crtc->noutput++] = output;
1319 for (output = outputs; output; output = output->next)
1321 if (!output->mode_info) continue;
1322 crtc_add_output (output->crtc_info, output);
1331 for (output = outputs; output; output = output->next)
1333 if (! output->crtc_info)
1335 if (! (output->changes & changes_panning))
1337 if (! output->crtc_info->panning_info)
1338 output->crtc_info->panning_info = malloc (sizeof(XRRPanning));
1339 memcpy (output->crtc_info->panning_info, &output->panning, sizeof(XRRPanning));
1340 output->crtc_info->changing = 1;
1349 for (output = outputs; output; output = output->next) {
1352 XRRCrtcGamma *gamma;
1357 if (!(output->changes & changes_gamma))
1360 if (!output->crtc_info) {
1361 fatal("Need crtc to set gamma on.\n");
1365 crtc = output->crtc_info;
1367 size = XRRGetCrtcGammaSize(dpy, crtc->crtc.xid);
1370 fatal("Gamma size is 0.\n");
1375 * The gamma-correction lookup table managed through XRR[GS]etCrtcGamma
1376 * is 2^n in size, where 'n' is the number of significant bits in
1377 * the X Color. Because an X Color is 16 bits, size cannot be larger
1381 fatal("Gamma correction table is impossibly large.\n");
1386 * The hardware color lookup table has a number of significant
1387 * bits equal to ffs(size) - 1; compute all values so that
1388 * they are in the range [0,size) then shift the values so
1389 * that they occupy the MSBs of the 16-bit X Color.
1391 shift = 16 - (ffs(size) - 1);
1393 gamma = XRRAllocGamma(size);
1395 fatal("Gamma allocation failed.\n");
1399 if (output->gamma.red == 0.0)
1400 output->gamma.red = 1.0;
1401 if (output->gamma.green == 0.0)
1402 output->gamma.green = 1.0;
1403 if (output->gamma.blue == 0.0)
1404 output->gamma.blue = 1.0;
1406 gammaRed = 1.0 / output->gamma.red;
1407 gammaGreen = 1.0 / output->gamma.green;
1408 gammaBlue = 1.0 / output->gamma.blue;
1410 for (i = 0; i < size; i++) {
1411 if (gammaRed == 1.0 && output->brightness == 1.0)
1414 gamma->red[i] = dmin(pow((double)i/(double)(size - 1),
1415 gammaRed) * output->brightness,
1416 1.0) * (double)(size - 1);
1417 gamma->red[i] <<= shift;
1419 if (gammaGreen == 1.0 && output->brightness == 1.0)
1420 gamma->green[i] = i;
1422 gamma->green[i] = dmin(pow((double)i/(double)(size - 1),
1423 gammaGreen) * output->brightness,
1424 1.0) * (double)(size - 1);
1425 gamma->green[i] <<= shift;
1427 if (gammaBlue == 1.0 && output->brightness == 1.0)
1430 gamma->blue[i] = dmin(pow((double)i/(double)(size - 1),
1431 gammaBlue) * output->brightness,
1432 1.0) * (double)(size - 1);
1433 gamma->blue[i] <<= shift;
1436 XRRSetCrtcGamma(dpy, crtc->crtc.xid, gamma);
1448 XRRSetOutputPrimary(dpy, root, None);
1450 for (output = outputs; output; output = output->next) {
1451 if (!(output->changes & changes_primary))
1453 if (output->primary)
1454 XRRSetOutputPrimary(dpy, root, output->output.xid);
1460 crtc_disable (crtc_t *crtc)
1463 printf ("crtc %d: disable\n", crtc->crtc.index);
1466 return RRSetConfigSuccess;
1467 return XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1468 0, 0, None, RR_Rotate_0, NULL, 0);
1472 crtc_set_transform (crtc_t *crtc, transform_t *transform)
1476 XRRQueryVersion (dpy, &major, &minor);
1477 if (major > 1 || (major == 1 && minor >= 3))
1478 XRRSetCrtcTransform (dpy, crtc->crtc.xid,
1479 &transform->transform,
1482 transform->nparams);
1486 crtc_revert (crtc_t *crtc)
1488 XRRCrtcInfo *crtc_info = crtc->crtc_info;
1491 printf ("crtc %d: revert\n", crtc->crtc.index);
1494 return RRSetConfigSuccess;
1496 if (!equal_transform (&crtc->current_transform, &crtc->pending_transform))
1497 crtc_set_transform (crtc, &crtc->current_transform);
1498 return XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1499 crtc_info->x, crtc_info->y,
1500 crtc_info->mode, crtc_info->rotation,
1501 crtc_info->outputs, crtc_info->noutput);
1505 crtc_apply (crtc_t *crtc)
1507 RROutput *rr_outputs;
1512 if (!crtc->changing || !crtc->mode_info)
1513 return RRSetConfigSuccess;
1515 rr_outputs = calloc (crtc->noutput, sizeof (RROutput));
1518 for (o = 0; o < crtc->noutput; o++)
1519 rr_outputs[o] = crtc->outputs[o]->output.xid;
1520 mode = crtc->mode_info->id;
1522 printf ("crtc %d: %12s %6.1f +%d+%d", crtc->crtc.index,
1523 crtc->mode_info->name, mode_refresh (crtc->mode_info),
1525 for (o = 0; o < crtc->noutput; o++)
1526 printf (" \"%s\"", crtc->outputs[o]->output.string);
1531 s = RRSetConfigSuccess;
1534 if (!equal_transform (&crtc->current_transform, &crtc->pending_transform))
1535 crtc_set_transform (crtc, &crtc->pending_transform);
1536 s = XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1537 crtc->x, crtc->y, mode, crtc->rotation,
1538 rr_outputs, crtc->noutput);
1539 if (s == RRSetConfigSuccess && crtc->panning_info) {
1541 s = XRRSetPanning (dpy, res, crtc->crtc.xid, crtc->panning_info);
1543 fatal ("panning needs RandR 1.3\n");
1551 screen_revert (void)
1554 printf ("screen %d: revert\n", screen);
1558 XRRSetScreenSize (dpy, root,
1559 DisplayWidth (dpy, screen),
1560 DisplayHeight (dpy, screen),
1561 DisplayWidthMM (dpy, screen),
1562 DisplayHeightMM (dpy, screen));
1568 if (fb_width == DisplayWidth (dpy, screen) &&
1569 fb_height == DisplayHeight (dpy, screen) &&
1570 fb_width_mm == DisplayWidthMM (dpy, screen) &&
1571 fb_height_mm == DisplayHeightMM (dpy, screen))
1576 printf ("screen %d: %dx%d %dx%d mm %6.2fdpi\n", screen,
1577 fb_width, fb_height, fb_width_mm, fb_height_mm, dpi);
1580 XRRSetScreenSize (dpy, root, fb_width, fb_height,
1581 fb_width_mm, fb_height_mm);
1589 /* first disable all crtcs */
1590 for (c = 0; c < res->ncrtc; c++)
1591 crtc_disable (&crtcs[c]);
1592 /* next reset screen size */
1594 /* now restore all crtcs */
1595 for (c = 0; c < res->ncrtc; c++)
1596 crtc_revert (&crtcs[c]);
1600 * uh-oh, something bad happened in the middle of changing
1601 * the configuration. Revert to the previous configuration
1604 static void _X_NORETURN
1605 panic (Status s, crtc_t *crtc)
1607 int c = crtc->crtc.index;
1608 const char *message;
1611 case RRSetConfigSuccess: message = "succeeded"; break;
1612 case BadAlloc: message = "out of memory"; break;
1613 case RRSetConfigFailed: message = "failed"; break;
1614 case RRSetConfigInvalidConfigTime: message = "invalid config time"; break;
1615 case RRSetConfigInvalidTime: message = "invalid time"; break;
1616 default: message = "unknown failure"; break;
1619 fprintf (stderr, "%s: Configure crtc %d %s\n", program_name, c, message);
1631 * Hold the server grabbed while messing with
1632 * the screen so that apps which notice the resize
1633 * event and ask for xinerama information from the server
1634 * receive up-to-date information
1640 * Turn off any crtcs which are to be disabled or which are
1641 * larger than the target size
1643 for (c = 0; c < res->ncrtc; c++)
1645 crtc_t *crtc = &crtcs[c];
1646 XRRCrtcInfo *crtc_info = crtc->crtc_info;
1648 /* if this crtc is already disabled, skip it */
1649 if (crtc_info->mode == None)
1653 * If this crtc is to be left enabled, make
1654 * sure the old size fits then new screen
1656 if (crtc->mode_info)
1658 XRRModeInfo *old_mode = find_mode_by_xid (crtc_info->mode);
1663 panic (RRSetConfigFailed, crtc);
1665 /* old position and size information */
1666 mode_geometry (old_mode, crtc_info->rotation,
1667 &crtc->current_transform.transform,
1670 x = crtc_info->x + bounds.x1;
1671 y = crtc_info->y + bounds.y1;
1672 w = bounds.x2 - bounds.x1;
1673 h = bounds.y2 - bounds.y1;
1675 /* if it fits, skip it */
1676 if (x + w <= fb_width && y + h <= fb_height)
1678 crtc->changing = True;
1680 s = crtc_disable (crtc);
1681 if (s != RRSetConfigSuccess)
1686 * Set the screen size
1694 for (c = 0; c < res->ncrtc; c++)
1696 crtc_t *crtc = &crtcs[c];
1698 s = crtc_apply (crtc);
1699 if (s != RRSetConfigSuccess)
1706 * Release the server grab and let all clients
1707 * respond to the updated state
1710 XUngrabServer (dpy);
1714 * Use current output state to complete the output list
1722 for (o = 0; o < res->noutput; o++)
1724 XRROutputInfo *output_info = XRRGetOutputInfo (dpy, res, res->outputs[o]);
1727 if (!output_info) fatal ("could not get output 0x%x information\n", res->outputs[o]);
1728 set_name_xid (&output_name, res->outputs[o]);
1729 set_name_index (&output_name, o);
1730 set_name_string (&output_name, output_info->name);
1731 output = find_output (&output_name);
1734 output = add_output ();
1735 set_name_all (&output->output, &output_name);
1737 * When global --automatic mode is set, turn on connected but off
1738 * outputs, turn off disconnected but on outputs
1742 switch (output_info->connection) {
1744 if (!output_info->crtc) {
1745 output->changes |= changes_automatic;
1746 output->automatic = True;
1749 case RR_Disconnected:
1750 if (output_info->crtc)
1752 output->changes |= changes_automatic;
1753 output->automatic = True;
1759 output->found = True;
1762 * Automatic mode -- track connection state and enable/disable outputs
1765 if (output->automatic)
1767 switch (output_info->connection) {
1769 case RR_UnknownConnection:
1770 if ((!(output->changes & changes_mode)))
1772 set_name_preferred (&output->mode);
1773 output->changes |= changes_mode;
1776 case RR_Disconnected:
1777 if ((!(output->changes & changes_mode)))
1779 set_name_xid (&output->mode, None);
1780 set_name_xid (&output->crtc, None);
1781 output->changes |= changes_mode;
1782 output->changes |= changes_crtc;
1788 set_output_info (output, res->outputs[o], output_info);
1790 for (q = outputs; q; q = q->next)
1794 fprintf(stderr, "warning: output %s not found; ignoring\n",
1801 mark_changing_crtcs (void)
1805 for (c = 0; c < num_crtcs; c++)
1807 crtc_t *crtc = &crtcs[c];
1811 /* walk old output list (to catch disables) */
1812 for (o = 0; o < crtc->crtc_info->noutput; o++)
1814 output = find_output_by_xid (crtc->crtc_info->outputs[o]);
1815 if (!output) fatal ("cannot find output 0x%x\n",
1816 crtc->crtc_info->outputs[o]);
1817 if (output->changes)
1818 crtc->changing = True;
1820 /* walk new output list */
1821 for (o = 0; o < crtc->noutput; o++)
1823 output = crtc->outputs[o];
1824 if (output->changes)
1825 crtc->changing = True;
1831 * Test whether 'crtc' can be used for 'output'
1834 check_crtc_for_output (crtc_t *crtc, output_t *output)
1840 for (c = 0; c < output->output_info->ncrtc; c++)
1841 if (output->output_info->crtcs[c] == crtc->crtc.xid)
1843 if (c == output->output_info->ncrtc)
1845 for (other = outputs; other; other = other->next)
1847 if (other == output)
1850 if (other->mode_info == NULL)
1853 if (other->crtc_info != crtc)
1856 /* see if the output connected to the crtc can clone to this output */
1857 for (l = 0; l < output->output_info->nclone; l++)
1858 if (output->output_info->clones[l] == other->output.xid)
1860 /* not on the list, can't clone */
1861 if (l == output->output_info->nclone)
1867 /* make sure the state matches */
1868 if (crtc->mode_info != output->mode_info)
1870 if (crtc->x != output->x)
1872 if (crtc->y != output->y)
1874 if (crtc->rotation != output->rotation)
1876 if (!equal_transform (&crtc->current_transform, &output->transform))
1879 else if (crtc->crtc_info->noutput)
1881 /* make sure the state matches the already used state */
1882 XRRModeInfo *mode = find_mode_by_xid (crtc->crtc_info->mode);
1884 if (mode != output->mode_info)
1886 if (crtc->crtc_info->x != output->x)
1888 if (crtc->crtc_info->y != output->y)
1890 if (crtc->crtc_info->rotation != output->rotation)
1897 find_crtc_for_output (output_t *output)
1901 for (c = 0; c < output->output_info->ncrtc; c++)
1905 crtc = find_crtc_by_xid (output->output_info->crtcs[c]);
1906 if (!crtc) fatal ("cannot find crtc 0x%x\n", output->output_info->crtcs[c]);
1908 if (check_crtc_for_output (crtc, output))
1915 set_positions (void)
1926 for (output = outputs; output; output = output->next)
1929 name_t relation_name;
1931 if (!(output->changes & changes_relation)) continue;
1933 if (output->mode_info == NULL) continue;
1935 init_name (&relation_name);
1936 set_name_string (&relation_name, output->relative_to);
1937 relation = find_output (&relation_name);
1938 if (!relation) fatal ("cannot find output \"%s\"\n", output->relative_to);
1940 if (relation->mode_info == NULL)
1944 output->changes |= changes_position;
1949 * Make sure the dependent object has been set in place
1951 if ((relation->changes & changes_relation) &&
1952 !(relation->changes & changes_position))
1958 switch (output->relation) {
1959 case relation_left_of:
1960 output->y = relation->y;
1961 output->x = relation->x - mode_width (output->mode_info, output->rotation);
1963 case relation_right_of:
1964 output->y = relation->y;
1965 output->x = relation->x + mode_width (relation->mode_info, relation->rotation);
1967 case relation_above:
1968 output->x = relation->x;
1969 output->y = relation->y - mode_height (output->mode_info, output->rotation);
1971 case relation_below:
1972 output->x = relation->x;
1973 output->y = relation->y + mode_height (relation->mode_info, relation->rotation);
1975 case relation_same_as:
1976 output->x = relation->x;
1977 output->y = relation->y;
1979 output->changes |= changes_position;
1985 fatal ("loop in relative position specifications\n");
1989 * Now normalize positions so the upper left corner of all outputs is at 0,0
1993 for (output = outputs; output; output = output->next)
1995 if (output->mode_info == NULL) continue;
1997 if (output->x < min_x) min_x = output->x;
1998 if (output->y < min_y) min_y = output->y;
2002 /* move all outputs */
2003 for (output = outputs; output; output = output->next)
2005 if (output->mode_info == NULL) continue;
2009 output->changes |= changes_position;
2015 set_screen_size (void)
2018 Bool fb_specified = fb_width != 0 && fb_height != 0;
2020 for (output = outputs; output; output = output->next)
2022 XRRModeInfo *mode_info = output->mode_info;
2026 if (!mode_info) continue;
2028 mode_geometry (mode_info, output->rotation,
2029 &output->transform.transform,
2031 x = output->x + bounds.x1;
2032 y = output->y + bounds.y1;
2033 w = bounds.x2 - bounds.x1;
2034 h = bounds.y2 - bounds.y1;
2035 /* make sure output fits in specified size */
2038 if (x + w > fb_width || y + h > fb_height)
2039 warning ("specified screen %dx%d not large enough for output %s (%dx%d+%d+%d)\n",
2040 fb_width, fb_height, output->output.string, w, h, x, y);
2042 /* fit fb to output */
2046 if (x + w > fb_width)
2048 if (y + h > fb_height)
2050 if (output->changes & changes_panning)
2051 pan = &output->panning;
2053 pan = output->crtc_info ? output->crtc_info->panning_info : NULL;
2054 if (pan && pan->left + pan->width > fb_width)
2055 fb_width = pan->left + pan->width;
2056 if (pan && pan->top + pan->height > fb_height)
2057 fb_height = pan->top + pan->height;
2061 if (fb_width > maxWidth || fb_height > maxHeight)
2062 fatal ("screen cannot be larger than %dx%d (desired size %dx%d)\n",
2063 maxWidth, maxHeight, fb_width, fb_height);
2066 if (fb_width < minWidth || fb_height < minHeight)
2067 fatal ("screen must be at least %dx%d\n", minWidth, minHeight);
2071 if (fb_width < minWidth) fb_width = minWidth;
2072 if (fb_height < minHeight) fb_height = minHeight;
2078 disable_outputs (output_t *outputs)
2082 outputs->crtc_info = NULL;
2083 outputs = outputs->next;
2088 * find the best mapping from output to crtc available
2091 pick_crtcs_score (output_t *outputs)
2104 outputs = outputs->next;
2106 * Score with this output disabled
2108 output->crtc_info = NULL;
2109 best_score = pick_crtcs_score (outputs);
2110 if (output->mode_info == NULL)
2115 * Now score with this output any valid crtc
2117 for (c = 0; c < output->output_info->ncrtc; c++)
2121 crtc = find_crtc_by_xid (output->output_info->crtcs[c]);
2123 fatal ("cannot find crtc 0x%x\n", output->output_info->crtcs[c]);
2125 /* reset crtc allocation for following outputs */
2126 disable_outputs (outputs);
2127 if (!check_crtc_for_output (crtc, output))
2131 /* slight preference for existing connections */
2132 if (crtc == output->current_crtc_info)
2135 output->crtc_info = crtc;
2136 score = my_score + pick_crtcs_score (outputs);
2137 if (score > best_score)
2143 if (output->crtc_info != best_crtc)
2144 output->crtc_info = best_crtc;
2146 * Reset other outputs based on this one using the best crtc
2148 (void) pick_crtcs_score (outputs);
2154 * Pick crtcs for any changing outputs that don't have one
2162 * First try to match up newly enabled outputs with spare crtcs
2164 for (output = outputs; output; output = output->next)
2166 if (output->changes && output->mode_info)
2168 if (output->crtc_info) {
2169 if (output->crtc_info->crtc_info->noutput > 0 &&
2170 (output->crtc_info->crtc_info->noutput > 1 ||
2171 output != find_output_by_xid (output->crtc_info->crtc_info->outputs[0])))
2174 output->crtc_info = find_crtc_for_output (output);
2175 if (!output->crtc_info)
2186 * When the simple way fails, see if there is a way
2187 * to swap crtcs around and make things work
2189 for (output = outputs; output; output = output->next)
2190 output->current_crtc_info = output->crtc_info;
2191 pick_crtcs_score (outputs);
2192 for (output = outputs; output; output = output->next)
2194 if (output->mode_info && !output->crtc_info)
2195 fatal ("cannot find crtc for output %s\n", output->output.string);
2196 if (!output->changes && output->crtc_info != output->current_crtc_info)
2197 output->changes |= changes_crtc;
2202 check_strtol(char *s)
2205 int result = strtol(s, &endptr, 10);
2212 check_strtod(char *s)
2215 double result = strtod(s, &endptr);
2223 property_values_from_string(const char *str, const Atom type, const int format,
2224 int *returned_nitems)
2227 void *returned_bytes = NULL;
2228 int nitems = 0, bytes_per_item = format / 8;
2230 if ((type != XA_INTEGER && type != XA_CARDINAL) ||
2231 (format != 8 && format != 16 && format != 32))
2238 for (token = strtok (tmp, ","); token; token = strtok (NULL, ","))
2241 long int val = strtol (token, &endptr, 0);
2243 if (token == endptr || *endptr != '\0')
2248 returned_bytes = realloc (returned_bytes, (nitems + 1) * bytes_per_item);
2250 if (type == XA_INTEGER && format == 8)
2252 int8_t *ptr = returned_bytes;
2253 ptr[nitems] = (int8_t) val;
2255 else if (type == XA_INTEGER && format == 16)
2257 int16_t *ptr = returned_bytes;
2258 ptr[nitems] = (int16_t) val;
2260 else if (type == XA_INTEGER && format == 32)
2262 int32_t *ptr = returned_bytes;
2263 ptr[nitems] = (int32_t) val;
2265 else if (type == XA_CARDINAL && format == 8)
2267 uint8_t *ptr = returned_bytes;
2268 ptr[nitems] = (uint8_t) val;
2270 else if (type == XA_CARDINAL && format == 16)
2272 uint16_t *ptr = returned_bytes;
2273 ptr[nitems] = (uint16_t) val;
2275 else if (type == XA_CARDINAL && format == 32)
2277 uint32_t *ptr = returned_bytes;
2278 ptr[nitems] = (uint32_t) val;
2283 free (returned_bytes);
2292 *returned_nitems = nitems;
2293 return returned_bytes;
2298 print_output_property_value(Bool is_edid,
2299 int value_format, /* 8, 16, 32 */
2300 Atom value_type, /* XA_{ATOM,INTEGER,CARDINAL} */
2301 const void *value_bytes)
2303 /* special-case the EDID */
2304 if (is_edid && value_format == 8)
2306 const uint8_t *val = value_bytes;
2307 printf ("%02" PRIx8, *val);
2311 if (value_type == XA_ATOM && value_format == 32)
2313 const Atom *val = value_bytes;
2314 char *str = XGetAtomName (dpy, *val);
2323 if (value_type == XA_INTEGER)
2325 if (value_format == 8)
2327 const int8_t *val = value_bytes;
2328 printf ("%" PRId8, *val);
2331 if (value_format == 16)
2333 const int16_t *val = value_bytes;
2334 printf ("%" PRId16, *val);
2337 if (value_format == 32)
2339 const int32_t *val = value_bytes;
2340 printf ("%" PRId32, *val);
2345 if (value_type == XA_CARDINAL)
2347 if (value_format == 8)
2349 const uint8_t *val = value_bytes;
2350 printf ("%" PRIu8, *val);
2353 if (value_format == 16)
2355 const uint16_t *val = value_bytes;
2356 printf ("%" PRIu16, *val);
2359 if (value_format == 32)
2361 const uint32_t *val = value_bytes;
2362 printf ("%" PRIu32, *val);
2373 main (int argc, char **argv)
2375 XRRScreenSize *sizes;
2376 XRRScreenConfiguration *sc;
2380 Status status = RRSetConfigFailed;
2383 int action_requested = False;
2384 Rotation rotation, current_rotation, rotations;
2386 XRRScreenChangeNotifyEvent *sce;
2387 char *display_name = NULL;
2389 SizeID current_size;
2395 Bool version = False;
2396 int event_base, error_base;
2398 int width = 0, height = 0;
2399 Bool have_pixel_size = False;
2401 output_t *output = NULL;
2402 Bool setit_1_2 = False;
2403 Bool query_1_2 = False;
2404 Bool modeit = False;
2405 Bool propit = False;
2406 Bool query_1 = False;
2407 Bool provsetoutsource = False;
2408 Bool provsetoffsink = False;
2410 Bool current = False;
2412 program_name = argv[0];
2413 for (i = 1; i < argc; i++) {
2414 if (!strcmp ("-display", argv[i]) || !strcmp ("-d", argv[i])) {
2415 if (++i>=argc) usage ();
2416 display_name = argv[i];
2419 if (!strcmp("-help", argv[i])) {
2421 action_requested = True;
2424 if (!strcmp ("--verbose", argv[i])) {
2428 if (!strcmp ("--dryrun", argv[i])) {
2433 if (!strcmp ("--nograb", argv[i])) {
2434 grab_server = False;
2437 if (!strcmp("--current", argv[i])) {
2442 if (!strcmp ("-s", argv[i]) || !strcmp ("--size", argv[i])) {
2443 if (++i>=argc) usage ();
2444 if (sscanf (argv[i], "%dx%d", &width, &height) == 2) {
2445 have_pixel_size = True;
2447 size = check_strtol(argv[i]);
2448 if (size < 0) usage();
2451 action_requested = True;
2455 if (!strcmp ("-r", argv[i]) ||
2456 !strcmp ("--rate", argv[i]) ||
2457 !strcmp ("--refresh", argv[i]))
2459 if (++i>=argc) usage ();
2460 rate = check_strtod(argv[i]);
2464 output->refresh = rate;
2465 output->changes |= changes_refresh;
2468 action_requested = True;
2472 if (!strcmp ("-v", argv[i]) || !strcmp ("--version", argv[i])) {
2474 action_requested = True;
2478 if (!strcmp ("-x", argv[i])) {
2479 reflection |= RR_Reflect_X;
2481 action_requested = True;
2484 if (!strcmp ("-y", argv[i])) {
2485 reflection |= RR_Reflect_Y;
2487 action_requested = True;
2490 if (!strcmp ("--screen", argv[i])) {
2491 if (++i>=argc) usage ();
2492 screen = check_strtol(argv[i]);
2493 if (screen < 0) usage();
2496 if (!strcmp ("-q", argv[i]) || !strcmp ("--query", argv[i])) {
2500 if (!strcmp ("-o", argv[i]) || !strcmp ("--orientation", argv[i])) {
2502 if (++i>=argc) usage ();
2503 dirind = strtol(argv[i], &endptr, 10);
2504 if (argv[i] == endptr) {
2505 for (dirind = 0; dirind < 4; dirind++) {
2506 if (strcmp (direction[dirind], argv[i]) == 0) break;
2508 if ((dirind < 0) || (dirind > 3)) usage();
2512 action_requested = True;
2515 if (!strcmp ("--prop", argv[i]) ||
2516 !strcmp ("--props", argv[i]) ||
2517 !strcmp ("--madprops", argv[i]) ||
2518 !strcmp ("--properties", argv[i]))
2522 action_requested = True;
2525 if (!strcmp ("--output", argv[i])) {
2526 if (++i >= argc) usage();
2528 output = find_output_by_name (argv[i]);
2530 output = add_output ();
2531 set_name (&output->output, argv[i], name_string|name_xid);
2535 action_requested = True;
2538 if (!strcmp ("--crtc", argv[i])) {
2539 if (++i >= argc) usage();
2540 if (!output) usage();
2541 set_name (&output->crtc, argv[i], name_xid|name_index);
2542 output->changes |= changes_crtc;
2545 if (!strcmp ("--mode", argv[i])) {
2546 if (++i >= argc) usage();
2547 if (!output) usage();
2548 set_name (&output->mode, argv[i], name_string|name_xid);
2549 output->changes |= changes_mode;
2552 if (!strcmp ("--preferred", argv[i])) {
2553 if (!output) usage();
2554 set_name_preferred (&output->mode);
2555 output->changes |= changes_mode;
2558 if (!strcmp ("--pos", argv[i])) {
2559 if (++i>=argc) usage ();
2560 if (!output) usage();
2561 if (sscanf (argv[i], "%dx%d",
2562 &output->x, &output->y) != 2)
2564 output->changes |= changes_position;
2567 if (!strcmp ("--rotation", argv[i]) || !strcmp ("--rotate", argv[i])) {
2568 if (++i>=argc) usage ();
2569 if (!output) usage();
2570 for (dirind = 0; dirind < 4; dirind++) {
2571 if (strcmp (direction[dirind], argv[i]) == 0) break;
2575 output->rotation &= ~0xf;
2576 output->rotation |= 1 << dirind;
2577 output->changes |= changes_rotation;
2580 if (!strcmp ("--reflect", argv[i]) || !strcmp ("--reflection", argv[i])) {
2581 if (++i>=argc) usage ();
2582 if (!output) usage();
2583 for (dirind = 0; dirind < 4; dirind++) {
2584 if (strcmp (reflections[dirind], argv[i]) == 0) break;
2588 output->rotation &= ~(RR_Reflect_X|RR_Reflect_Y);
2589 output->rotation |= dirind * RR_Reflect_X;
2590 output->changes |= changes_reflection;
2593 if (!strcmp ("--left-of", argv[i])) {
2594 if (++i>=argc) usage ();
2595 if (!output) usage();
2596 output->relation = relation_left_of;
2597 output->relative_to = argv[i];
2598 output->changes |= changes_relation;
2601 if (!strcmp ("--right-of", argv[i])) {
2602 if (++i>=argc) usage ();
2603 if (!output) usage();
2604 output->relation = relation_right_of;
2605 output->relative_to = argv[i];
2606 output->changes |= changes_relation;
2609 if (!strcmp ("--above", argv[i])) {
2610 if (++i>=argc) usage ();
2611 if (!output) usage();
2612 output->relation = relation_above;
2613 output->relative_to = argv[i];
2614 output->changes |= changes_relation;
2617 if (!strcmp ("--below", argv[i])) {
2618 if (++i>=argc) usage ();
2619 if (!output) usage();
2620 output->relation = relation_below;
2621 output->relative_to = argv[i];
2622 output->changes |= changes_relation;
2625 if (!strcmp ("--same-as", argv[i])) {
2626 if (++i>=argc) usage ();
2627 if (!output) usage();
2628 output->relation = relation_same_as;
2629 output->relative_to = argv[i];
2630 output->changes |= changes_relation;
2633 if (!strcmp ("--panning", argv[i])) {
2635 if (++i>=argc) usage ();
2636 if (!output) usage();
2637 pan = &output->panning;
2638 switch (sscanf (argv[i], "%dx%d+%d+%d/%dx%d+%d+%d/%d/%d/%d/%d",
2639 &pan->width, &pan->height, &pan->left, &pan->top,
2640 &pan->track_width, &pan->track_height,
2641 &pan->track_left, &pan->track_top,
2642 &pan->border_left, &pan->border_top,
2643 &pan->border_right, &pan->border_bottom)) {
2645 pan->left = pan->top = 0;
2648 pan->track_left = pan->track_top =
2649 pan->track_width = pan->track_height = 0;
2652 pan->border_left = pan->border_top =
2653 pan->border_right = pan->border_bottom = 0;
2660 output->changes |= changes_panning;
2663 if (!strcmp ("--gamma", argv[i])) {
2664 if (!output) usage();
2665 if (++i>=argc) usage ();
2666 if (sscanf(argv[i], "%f:%f:%f", &output->gamma.red,
2667 &output->gamma.green, &output->gamma.blue) != 3)
2669 output->changes |= changes_gamma;
2673 if (!strcmp ("--brightness", argv[i])) {
2674 if (!output) usage();
2675 if (++i>=argc) usage();
2676 if (sscanf(argv[i], "%f", &output->brightness) != 1)
2678 output->changes |= changes_gamma;
2682 if (!strcmp ("--primary", argv[i])) {
2683 if (!output) usage();
2684 output->changes |= changes_primary;
2685 output->primary = True;
2689 if (!strcmp ("--noprimary", argv[i])) {
2694 if (!strcmp ("--set", argv[i])) {
2695 output_prop_t *prop;
2696 if (!output) usage();
2697 prop = malloc (sizeof (output_prop_t));
2698 prop->next = output->props;
2699 output->props = prop;
2700 if (++i>=argc) usage ();
2701 prop->name = argv[i];
2702 if (++i>=argc) usage ();
2703 prop->value = argv[i];
2705 output->changes |= changes_property;
2709 if (!strcmp ("--scale", argv[i]))
2712 if (!output) usage();
2713 if (++i>=argc) usage();
2714 if (sscanf (argv[i], "%lfx%lf", &sx, &sy) != 2)
2716 init_transform (&output->transform);
2717 output->transform.transform.matrix[0][0] = XDoubleToFixed (sx);
2718 output->transform.transform.matrix[1][1] = XDoubleToFixed (sy);
2719 output->transform.transform.matrix[2][2] = XDoubleToFixed (1.0);
2720 if (sx != 1 || sy != 1)
2721 output->transform.filter = "bilinear";
2723 output->transform.filter = "nearest";
2724 output->transform.nparams = 0;
2725 output->transform.params = NULL;
2726 output->changes |= changes_transform;
2729 if (!strcmp ("--scale-from", argv[i]))
2732 if (!output) usage();
2733 if (++i>=argc) usage();
2734 if (sscanf (argv[i], "%dx%d", &w, &h) != 2)
2736 if (w <=0 || h <= 0)
2738 output->scale_from_w = w;
2739 output->scale_from_h = h;
2740 output->changes |= changes_transform;
2743 if (!strcmp ("--transform", argv[i])) {
2744 double transform[3][3];
2746 if (!output) usage();
2747 if (++i>=argc) usage ();
2748 init_transform (&output->transform);
2749 if (strcmp (argv[i], "none") != 0)
2751 if (sscanf(argv[i], "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf",
2752 &transform[0][0],&transform[0][1],&transform[0][2],
2753 &transform[1][0],&transform[1][1],&transform[1][2],
2754 &transform[2][0],&transform[2][1],&transform[2][2])
2757 init_transform (&output->transform);
2758 for (k = 0; k < 3; k++)
2759 for (l = 0; l < 3; l++) {
2760 output->transform.transform.matrix[k][l] = XDoubleToFixed (transform[k][l]);
2762 output->transform.filter = "bilinear";
2763 output->transform.nparams = 0;
2764 output->transform.params = NULL;
2766 output->changes |= changes_transform;
2769 if (!strcmp ("--off", argv[i])) {
2770 if (!output) usage();
2771 set_name_xid (&output->mode, None);
2772 set_name_xid (&output->crtc, None);
2773 output->changes |= changes_mode;
2776 if (!strcmp ("--fb", argv[i])) {
2777 if (++i>=argc) usage ();
2778 if (sscanf (argv[i], "%dx%d",
2779 &fb_width, &fb_height) != 2)
2782 action_requested = True;
2785 if (!strcmp ("--fbmm", argv[i])) {
2786 if (++i>=argc) usage ();
2787 if (sscanf (argv[i], "%dx%d",
2788 &fb_width_mm, &fb_height_mm) != 2)
2791 action_requested = True;
2794 if (!strcmp ("--dpi", argv[i])) {
2796 if (++i>=argc) usage ();
2797 dpi = strtod(argv[i], &strtod_error);
2798 if (argv[i] == strtod_error)
2801 dpi_output = argv[i];
2804 action_requested = True;
2807 if (!strcmp ("--auto", argv[i])) {
2810 output->automatic = True;
2811 output->changes |= changes_automatic;
2816 action_requested = True;
2819 if (!strcmp ("--q12", argv[i]))
2824 if (!strcmp ("--q1", argv[i]))
2829 if (!strcmp ("--newmode", argv[i]))
2831 umode_t *m = malloc (sizeof (umode_t));
2835 if (i + 9 >= argc) usage ();
2836 m->mode.name = argv[i];
2837 m->mode.nameLength = strlen (argv[i]);
2839 clock = check_strtod(argv[i++]);
2840 m->mode.dotClock = clock * 1e6;
2842 m->mode.width = check_strtol(argv[i++]);
2843 m->mode.hSyncStart = check_strtol(argv[i++]);
2844 m->mode.hSyncEnd = check_strtol(argv[i++]);
2845 m->mode.hTotal = check_strtol(argv[i++]);
2846 m->mode.height = check_strtol(argv[i++]);
2847 m->mode.vSyncStart = check_strtol(argv[i++]);
2848 m->mode.vSyncEnd = check_strtol(argv[i++]);
2849 m->mode.vTotal = check_strtol(argv[i++]);
2850 m->mode.modeFlags = 0;
2854 for (f = 0; mode_flags[f].string; f++)
2855 if (!strcasecmp (mode_flags[f].string, argv[i]))
2858 if (!mode_flags[f].string)
2860 m->mode.modeFlags |= mode_flags[f].flag;
2864 m->action = umode_create;
2867 action_requested = True;
2870 if (!strcmp ("--rmmode", argv[i]))
2872 umode_t *m = malloc (sizeof (umode_t));
2874 if (++i>=argc) usage ();
2875 set_name (&m->name, argv[i], name_string|name_xid);
2876 m->action = umode_destroy;
2880 action_requested = True;
2883 if (!strcmp ("--addmode", argv[i]))
2885 umode_t *m = malloc (sizeof (umode_t));
2887 if (++i>=argc) usage ();
2888 set_name (&m->output, argv[i], name_string|name_xid);
2889 if (++i>=argc) usage();
2890 set_name (&m->name, argv[i], name_string|name_xid);
2891 m->action = umode_add;
2895 action_requested = True;
2898 if (!strcmp ("--delmode", argv[i]))
2900 umode_t *m = malloc (sizeof (umode_t));
2902 if (++i>=argc) usage ();
2903 set_name (&m->output, argv[i], name_string|name_xid);
2904 if (++i>=argc) usage();
2905 set_name (&m->name, argv[i], name_string|name_xid);
2906 m->action = umode_delete;
2910 action_requested = True;
2913 if (!strcmp ("--listproviders", argv[i]))
2916 action_requested = True;
2919 if (!strcmp("--setprovideroutputsource", argv[i]))
2921 if (++i>=argc) usage ();
2922 provider_xid = check_strtol(argv[i]);
2924 output_source_provider_xid = 0;
2926 output_source_provider_xid = check_strtol(argv[i]);
2928 if (provider_xid == 0)
2930 action_requested = True;
2931 provsetoutsource = True;
2934 if (!strcmp("--setprovideroffloadsink", argv[i]))
2936 if (++i>=argc) usage ();
2937 provider_xid = check_strtol(argv[i]);
2939 offload_sink_provider_xid = 0;
2941 offload_sink_provider_xid = check_strtol(argv[i]);
2943 if (provider_xid == 0)
2945 action_requested = True;
2946 provsetoffsink = True;
2952 if (!action_requested)
2957 if (setit && !setit_1_2)
2961 printf("xrandr program version " VERSION "\n");
2963 dpy = XOpenDisplay (display_name);
2966 fprintf (stderr, "Can't open display %s\n", XDisplayName(display_name));
2970 screen = DefaultScreen (dpy);
2971 if (screen >= ScreenCount (dpy)) {
2972 fprintf (stderr, "Invalid screen number %d (display has %d)\n",
2973 screen, ScreenCount (dpy));
2977 root = RootWindow (dpy, screen);
2979 if (!XRRQueryExtension (dpy, &event_base, &error_base) ||
2980 !XRRQueryVersion (dpy, &major, &minor))
2982 fprintf (stderr, "RandR extension missing\n");
2985 if (major > 1 || (major == 1 && minor >= 2))
2987 if (major > 1 || (major == 1 && minor >= 3))
2990 if (has_1_2 && modeit)
2994 get_screen (current);
2998 for (m = umodes; m; m = m->next)
3003 switch (m->action) {
3005 XRRCreateMode (dpy, root, &m->mode);
3008 e = find_mode (&m->name, 0);
3010 fatal ("cannot find mode \"%s\"\n", m->name.string);
3011 XRRDestroyMode (dpy, e->id);
3014 o = find_output (&m->output);
3016 fatal ("cannot find output \"%s\"\n", m->output.string);
3017 e = find_mode (&m->name, 0);
3019 fatal ("cannot find mode \"%s\"\n", m->name.string);
3020 XRRAddOutputMode (dpy, o->output.xid, e->id);
3023 o = find_output (&m->output);
3025 fatal ("cannot find output \"%s\"\n", m->output.string);
3026 e = find_mode (&m->name, 0);
3028 fatal ("cannot find mode \"%s\"\n", m->name.string);
3029 XRRDeleteOutputMode (dpy, o->output.xid, e->id);
3039 if (has_1_2 && propit)
3042 get_screen (current);
3046 for (output = outputs; output; output = output->next)
3048 output_prop_t *prop;
3050 for (prop = output->props; prop; prop = prop->next)
3052 Atom name = XInternAtom (dpy, prop->name, False);
3055 unsigned char *data, *malloced_data = NULL;
3058 unsigned long ulong_value;
3059 unsigned char *prop_data;
3061 unsigned long nitems, bytes_after;
3063 XRRPropertyInfo *propinfo;
3065 type = AnyPropertyType;
3067 if (XRRGetOutputProperty (dpy, output->output.xid, name,
3068 0, 100, False, False,
3070 &actual_type, &actual_format,
3071 &nitems, &bytes_after, &prop_data) == Success &&
3073 (propinfo = XRRQueryOutputProperty(dpy, output->output.xid,
3077 format = actual_format;
3080 malloced_data = property_values_from_string
3081 (prop->value, type, actual_format, &nelements);
3085 data = malloced_data;
3087 format = actual_format;
3089 else if (type == AnyPropertyType &&
3090 (sscanf (prop->value, "%d", &int_value) == 1 ||
3091 sscanf (prop->value, "0x%x", &int_value) == 1))
3094 ulong_value = int_value;
3095 data = (unsigned char *) &ulong_value;
3099 else if ((type == XA_ATOM))
3101 ulong_value = XInternAtom (dpy, prop->value, False);
3102 data = (unsigned char *) &ulong_value;
3105 else if ((type == XA_STRING || type == AnyPropertyType))
3108 data = (unsigned char *) prop->value;
3109 nelements = strlen (prop->value);
3114 XRRChangeOutputProperty (dpy, output->output.xid,
3115 name, type, format, PropModeReplace,
3117 free (malloced_data);
3126 if (has_1_2 && provsetoutsource)
3128 XRRSetProviderOutputSource(dpy, provider_xid, output_source_provider_xid);
3130 if (has_1_2 && provsetoffsink)
3132 XRRSetProviderOffloadSink(dpy, provider_xid, offload_sink_provider_xid);
3136 get_screen (current);
3145 * Assign outputs to crtcs
3150 * Mark changing crtcs
3152 mark_changing_crtcs ();
3155 * If an output was specified to track dpi, use it
3159 output_t *output = find_output_by_name (dpi_output);
3160 XRROutputInfo *output_info;
3161 XRRModeInfo *mode_info;
3163 fatal ("Cannot find output %s\n", dpi_output);
3164 output_info = output->output_info;
3165 mode_info = output->mode_info;
3166 if (output_info && mode_info && output_info->mm_height)
3169 * When this output covers the whole screen, just use
3170 * the known physical size
3172 if (fb_width == mode_info->width &&
3173 fb_height == mode_info->height)
3175 fb_width_mm = output_info->mm_width;
3176 fb_height_mm = output_info->mm_height;
3180 dpi = (25.4 * mode_info->height) / output_info->mm_height;
3186 * Compute physical screen size
3188 if (fb_width_mm == 0 || fb_height_mm == 0)
3190 if (fb_width != DisplayWidth (dpy, screen) ||
3191 fb_height != DisplayHeight (dpy, screen) || dpi != 0.0)
3194 dpi = (25.4 * DisplayHeight (dpy, screen)) / DisplayHeightMM(dpy, screen);
3196 fb_width_mm = (25.4 * fb_width) / dpi;
3197 fb_height_mm = (25.4 * fb_height) / dpi;
3201 fb_width_mm = DisplayWidthMM (dpy, screen);
3202 fb_height_mm = DisplayHeightMM (dpy, screen);
3212 * Set gamma on crtc's that belong to the outputs.
3217 * Now apply all of the changes
3224 if (query_1_2 || (query && has_1_2 && !query_1))
3229 #define ModeShown 0x80000000
3231 get_screen (current);
3235 printf ("Screen %d: minimum %d x %d, current %d x %d, maximum %d x %d\n",
3236 screen, minWidth, minHeight,
3237 DisplayWidth (dpy, screen), DisplayHeight(dpy, screen),
3238 maxWidth, maxHeight);
3240 for (output = outputs; output; output = output->next)
3242 XRROutputInfo *output_info = output->output_info;
3243 crtc_t *crtc = output->crtc_info;
3244 XRRCrtcInfo *crtc_info = crtc ? crtc->crtc_info : NULL;
3245 XRRModeInfo *mode = output->mode_info;
3249 Rotation rotations = output_rotations (output);
3251 printf ("%s %s", output_info->name, connection[output_info->connection]);
3252 if (output->primary) {
3258 printf (" %dx%d+%d+%d",
3259 crtc_info->width, crtc_info->height,
3260 crtc_info->x, crtc_info->y);
3262 printf (" %dx%d+%d+%d",
3263 mode->width, mode->height, output->x, output->y);
3266 printf (" (0x%x)", (int)mode->id);
3267 if (output->rotation != RR_Rotate_0 || verbose)
3270 rotation_name (output->rotation));
3271 if (output->rotation & (RR_Reflect_X|RR_Reflect_Y))
3272 printf (" %s", reflection_name (output->rotation));
3275 if (rotations != RR_Rotate_0 || verbose)
3279 for (i = 0; i < 4; i ++) {
3280 if ((rotations >> i) & 1) {
3281 if (!first) printf (" "); first = False;
3282 printf("%s", direction[i]);
3285 if (rotations & RR_Reflect_X)
3287 if (!first) printf (" "); first = False;
3290 if (rotations & RR_Reflect_Y)
3292 if (!first) printf (" ");
3300 printf (" %dmm x %dmm",
3301 (int)output_info->mm_width, (int)output_info->mm_height);
3304 if (crtc && crtc->panning_info && crtc->panning_info->width > 0)
3306 XRRPanning *pan = crtc->panning_info;
3307 printf (" panning %dx%d+%d+%d",
3308 pan->width, pan->height, pan->left, pan->top);
3309 if ((pan->track_width != 0 &&
3310 (pan->track_left != pan->left ||
3311 pan->track_width != pan->width ||
3312 pan->border_left != 0 ||
3313 pan->border_right != 0)) ||
3314 (pan->track_height != 0 &&
3315 (pan->track_top != pan->top ||
3316 pan->track_height != pan->height ||
3317 pan->border_top != 0 ||
3318 pan->border_bottom != 0)))
3319 printf (" tracking %dx%d+%d+%d border %d/%d/%d/%d",
3320 pan->track_width, pan->track_height,
3321 pan->track_left, pan->track_top,
3322 pan->border_left, pan->border_top,
3323 pan->border_right, pan->border_bottom);
3329 printf ("\tIdentifier: 0x%x\n", (int)output->output.xid);
3330 printf ("\tTimestamp: %d\n", (int)output_info->timestamp);
3331 printf ("\tSubpixel: %s\n", order[output_info->subpixel_order]);
3332 if (output->gamma.red != 0.0 && output->gamma.green != 0.0 && output->gamma.blue != 0.0) {
3333 printf ("\tGamma: %#.2g:%#.2g:%#.2g\n",
3334 output->gamma.red, output->gamma.green, output->gamma.blue);
3335 printf ("\tBrightness: %#.2g\n", output->brightness);
3337 printf ("\tClones: ");
3338 for (j = 0; j < output_info->nclone; j++)
3340 output_t *clone = find_output_by_xid (output_info->clones[j]);
3342 if (clone) printf (" %s", clone->output.string);
3345 if (output->crtc_info)
3346 printf ("\tCRTC: %d\n", output->crtc_info->crtc.index);
3347 printf ("\tCRTCs: ");
3348 for (j = 0; j < output_info->ncrtc; j++)
3350 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[j]);
3352 printf (" %d", crtc->crtc.index);
3355 if (output->crtc_info && output->crtc_info->panning_info) {
3356 XRRPanning *pan = output->crtc_info->panning_info;
3357 printf ("\tPanning: %dx%d+%d+%d\n",
3358 pan->width, pan->height, pan->left, pan->top);
3359 printf ("\tTracking: %dx%d+%d+%d\n",
3360 pan->track_width, pan->track_height,
3361 pan->track_left, pan->track_top);
3362 printf ("\tBorder: %d/%d/%d/%d\n",
3363 pan->border_left, pan->border_top,
3364 pan->border_right, pan->border_bottom);
3371 printf ("\tTransform: ");
3372 for (y = 0; y < 3; y++)
3374 for (x = 0; x < 3; x++)
3375 printf (" %f", XFixedToDouble (output->transform.transform.matrix[y][x]));
3379 if (output->transform.filter)
3380 printf ("\n\t filter: %s", output->transform.filter);
3383 if (verbose || properties)
3385 props = XRRListOutputProperties (dpy, output->output.xid,
3387 for (j = 0; j < nprop; j++) {
3388 unsigned char *prop;
3390 unsigned long nitems, bytes_after;
3392 XRRPropertyInfo *propinfo;
3393 char *atom_name = XGetAtomName (dpy, props[j]);
3394 Bool is_edid = strcmp (atom_name, "EDID") == 0;
3397 XRRGetOutputProperty (dpy, output->output.xid, props[j],
3398 0, 100, False, False,
3400 &actual_type, &actual_format,
3401 &nitems, &bytes_after, &prop);
3403 propinfo = XRRQueryOutputProperty(dpy, output->output.xid,
3406 bytes_per_item = actual_format / 8;
3408 printf ("\t%s: ", atom_name);
3415 for (k = 0; k < nitems; k++)
3424 print_output_property_value (is_edid, actual_format,
3426 prop + (k * bytes_per_item));
3434 if (propinfo->range && propinfo->num_values > 0)
3436 printf ("\t\trange%s: ",
3437 (propinfo->num_values == 2) ? "" : "s");
3438 for (k = 0; k < propinfo->num_values / 2; k++)
3441 print_output_property_value (False, 32, actual_type,
3442 (unsigned char *) &(propinfo->values[k * 2]));
3444 print_output_property_value (False, 32, actual_type,
3445 (unsigned char *) &(propinfo->values[k * 2 + 1]));
3450 if (!propinfo->range && propinfo->num_values > 0)
3452 printf ("\t\tsupported: ");
3453 for (k = 0; k < propinfo->num_values; k++)
3455 print_output_property_value (False, 32, actual_type,
3456 (unsigned char *) &(propinfo->values[k]));
3467 for (j = 0; j < output_info->nmode; j++)
3469 XRRModeInfo *mode = find_mode_by_xid (output_info->modes[j]);
3472 printf (" %s (0x%x) %6.1fMHz",
3473 mode->name, (int)mode->id,
3474 (double)mode->dotClock / 1000000.0);
3475 for (f = 0; mode_flags[f].flag; f++)
3476 if (mode->modeFlags & mode_flags[f].flag)
3477 printf (" %s", mode_flags[f].string);
3478 if (mode == output->mode_info)
3479 printf (" *current");
3480 if (j < output_info->npreferred)
3481 printf (" +preferred");
3483 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
3484 mode->width, mode->hSyncStart, mode->hSyncEnd,
3485 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
3486 printf (" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
3487 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
3488 mode_refresh (mode));
3489 mode->modeFlags |= ModeShown;
3494 mode_shown = calloc (output_info->nmode, sizeof (Bool));
3495 if (!mode_shown) fatal ("out of memory\n");
3496 for (j = 0; j < output_info->nmode; j++)
3498 XRRModeInfo *jmode, *kmode;
3500 if (mode_shown[j]) continue;
3502 jmode = find_mode_by_xid (output_info->modes[j]);
3504 printf (" %-12s", jmode->name);
3505 for (k = j; k < output_info->nmode; k++)
3507 if (mode_shown[k]) continue;
3508 kmode = find_mode_by_xid (output_info->modes[k]);
3509 if (strcmp (jmode->name, kmode->name) != 0) continue;
3510 mode_shown[k] = True;
3511 kmode->modeFlags |= ModeShown;
3512 printf (" %6.1f", mode_refresh (kmode));
3513 if (kmode == output->mode_info)
3517 if (k < output_info->npreferred)
3527 for (m = 0; m < res->nmode; m++)
3529 XRRModeInfo *mode = &res->modes[m];
3531 if (!(mode->modeFlags & ModeShown))
3533 printf (" %s (0x%x) %6.1fMHz\n",
3534 mode->name, (int)mode->id,
3535 (double)mode->dotClock / 1000000.0);
3536 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
3537 mode->width, mode->hSyncStart, mode->hSyncEnd,
3538 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
3539 printf (" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
3540 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
3541 mode_refresh (mode));
3547 XRRProviderResources *providers;
3549 get_screen (current);
3550 providers = XRRGetProviderResources(dpy, root);
3552 printf("Providers: number : %d\n", providers->nproviders);
3554 for (j = 0; j < providers->nproviders; j++) {
3555 XRRProviderInfo *info = XRRGetProviderInfo(dpy, res, providers->providers[j]);
3556 printf("Provider %d: id: %d cap: 0x%x", j, (int)providers->providers[j], info->capabilities);
3557 for (k = 0; k < 4; k++)
3558 if (info->capabilities & (1 << k))
3559 printf(", %s", capability_name(1<<k));
3561 printf(" crtcs: %d outputs: %d associated providers: %d name:%s\n", info->ncrtcs, info->noutputs, info->nassociatedproviders, info->name);
3562 XRRFreeProviderInfo(info);
3567 sc = XRRGetScreenInfo (dpy, root);
3572 current_size = XRRConfigCurrentConfiguration (sc, ¤t_rotation);
3574 sizes = XRRConfigSizes(sc, &nsize);
3576 if (have_pixel_size) {
3577 for (size = 0; size < nsize; size++)
3579 if (sizes[size].width == width && sizes[size].height == height)
3582 if (size >= nsize) {
3584 "Size %dx%d not found in available modes\n", width, height);
3589 size = current_size;
3590 else if (size >= nsize) {
3592 "Size index %d is too large, there are only %d sizes\n",
3599 for (rot = 0; rot < 4; rot++)
3600 if (1 << rot == (current_rotation & 0xf))
3604 current_rate = XRRConfigCurrentRate (sc);
3608 if (size == current_size)
3609 rate = current_rate;
3615 rates = XRRConfigRates (sc, size, &nrate);
3616 for (i = 0; i < nrate; i++)
3617 if (rate == rates[i])
3620 fprintf (stderr, "Rate %.1f Hz not available for this size\n", rate);
3626 int major_version, minor_version;
3627 XRRQueryVersion (dpy, &major_version, &minor_version);
3628 printf("Server reports RandR version %d.%d\n",
3629 major_version, minor_version);
3632 if (query || query_1) {
3633 printf(" SZ: Pixels Physical Refresh\n");
3634 for (i = 0; i < nsize; i++) {
3635 printf ("%c%-2d %5d x %-5d (%4dmm x%4dmm )",
3636 i == current_size ? '*' : ' ',
3637 i, sizes[i].width, sizes[i].height,
3638 sizes[i].mwidth, sizes[i].mheight);
3639 rates = XRRConfigRates (sc, i, &nrate);
3640 if (nrate) printf (" ");
3641 for (j = 0; j < nrate; j++)
3643 i == current_size && rates[j] == current_rate ? '*' : ' ',
3649 rotations = XRRConfigRotations(sc, ¤t_rotation);
3651 rotation = 1 << rot ;
3653 printf("Current rotation - %s\n",
3654 rotation_name (current_rotation));
3656 printf("Current reflection - %s\n",
3657 reflection_name (current_rotation));
3659 printf ("Rotations possible - ");
3660 for (i = 0; i < 4; i ++) {
3661 if ((rotations >> i) & 1) printf("%s ", direction[i]);
3665 printf ("Reflections possible - ");
3666 if (rotations & (RR_Reflect_X|RR_Reflect_Y))
3668 if (rotations & RR_Reflect_X) printf ("X Axis ");
3669 if (rotations & RR_Reflect_Y) printf ("Y Axis");
3677 printf("Setting size to %d, rotation to %s\n", size, direction[rot]);
3679 printf ("Setting reflection on ");
3682 if (reflection & RR_Reflect_X) printf ("X Axis ");
3683 if (reflection & RR_Reflect_Y) printf ("Y Axis");
3686 printf ("neither axis");
3689 if (reflection & RR_Reflect_X) printf("Setting reflection on X axis\n");
3691 if (reflection & RR_Reflect_Y) printf("Setting reflection on Y axis\n");
3694 /* we should test configureNotify on the root window */
3695 XSelectInput (dpy, root, StructureNotifyMask);
3697 if (setit && !dryrun) XRRSelectInput (dpy, root,
3698 RRScreenChangeNotifyMask);
3699 if (setit && !dryrun) status = XRRSetScreenConfigAndRate (dpy, sc,
3701 (SizeID) size, (Rotation) (rotation | reflection), rate, CurrentTime);
3703 if (setit && !dryrun && status == RRSetConfigFailed) {
3704 printf ("Failed to change the screen configuration!\n");
3708 if (verbose && setit && !dryrun && size != current_size) {
3709 if (status == RRSetConfigSuccess)
3711 Bool seen_screen = False;
3712 while (!seen_screen) {
3714 XNextEvent(dpy, (XEvent *) &event);
3716 printf ("Event received, type = %d\n", event.type);
3717 /* update Xlib's knowledge of the event */
3718 XRRUpdateConfiguration (&event);
3719 if (event.type == ConfigureNotify)
3720 printf("Received ConfigureNotify Event!\n");
3722 switch (event.type - event_base) {
3723 case RRScreenChangeNotify:
3724 sce = (XRRScreenChangeNotifyEvent *) &event;
3726 printf("Got a screen change notify event!\n");
3727 printf(" window = %d\n root = %d\n size_index = %d\n rotation %d\n",
3728 (int) sce->window, (int) sce->root,
3729 sce->size_index, sce->rotation);
3730 printf(" timestamp = %ld, config_timestamp = %ld\n",
3731 sce->timestamp, sce->config_timestamp);
3732 printf(" Rotation = %x\n", sce->rotation);
3733 printf(" %d X %d pixels, %d X %d mm\n",
3734 sce->width, sce->height, sce->mwidth, sce->mheight);
3735 printf("Display width %d, height %d\n",
3736 DisplayWidth(dpy, screen), DisplayHeight(dpy, screen));
3737 printf("Display widthmm %d, heightmm %d\n",
3738 DisplayWidthMM(dpy, screen), DisplayHeightMM(dpy, screen));
3739 spo = sce->subpixel_order;
3740 if ((spo < 0) || (spo > 5))
3741 printf ("Unknown subpixel order, value = %d\n", spo);
3742 else printf ("new Subpixel rendering model is %s\n", order[spo]);
3746 if (event.type != ConfigureNotify)
3747 printf("unknown event received, type = %d!\n", event.type);
3752 XRRFreeScreenConfigInfo(sc);