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 */
42 static char *program_name;
45 static int screen = -1;
46 static Bool verbose = False;
47 static Bool automatic = False;
48 static Bool properties = False;
49 static Bool grab_server = True;
50 static Bool no_primary = False;
52 static char *direction[5] = {
59 static char *reflections[5] = {
67 static char *order[6] = {
79 { "+HSync", RR_HSyncPositive },
80 { "-HSync", RR_HSyncNegative },
81 { "+VSync", RR_VSyncPositive },
82 { "-VSync", RR_VSyncNegative },
83 { "Interlace", RR_Interlace },
84 { "DoubleScan", RR_DoubleScan },
85 { "CSync", RR_CSync },
86 { "+CSync", RR_CSyncPositive },
87 { "-CSync", RR_CSyncNegative },
91 static void _X_NORETURN
94 fprintf(stderr, "usage: %s [options]\n", program_name);
95 fprintf(stderr, " where options are:\n");
96 fprintf(stderr, " -display <display> or -d <display>\n");
97 fprintf(stderr, " -help\n");
98 fprintf(stderr, " -o <normal,inverted,left,right,0,1,2,3>\n");
99 fprintf(stderr, " or --orientation <normal,inverted,left,right,0,1,2,3>\n");
100 fprintf(stderr, " -q or --query\n");
101 fprintf(stderr, " -s <size>/<width>x<height> or --size <size>/<width>x<height>\n");
102 fprintf(stderr, " -r <rate> or --rate <rate> or --refresh <rate>\n");
103 fprintf(stderr, " -v or --version\n");
104 fprintf(stderr, " -x (reflect in x)\n");
105 fprintf(stderr, " -y (reflect in y)\n");
106 fprintf(stderr, " --screen <screen>\n");
107 fprintf(stderr, " --verbose\n");
108 fprintf(stderr, " --current\n");
109 fprintf(stderr, " --dryrun\n");
110 fprintf(stderr, " --nograb\n");
111 fprintf(stderr, " --prop or --properties\n");
112 fprintf(stderr, " --fb <width>x<height>\n");
113 fprintf(stderr, " --fbmm <width>x<height>\n");
114 fprintf(stderr, " --dpi <dpi>/<output>\n");
115 fprintf(stderr, " --output <output>\n");
116 fprintf(stderr, " --auto\n");
117 fprintf(stderr, " --mode <mode>\n");
118 fprintf(stderr, " --preferred\n");
119 fprintf(stderr, " --pos <x>x<y>\n");
120 fprintf(stderr, " --rate <rate> or --refresh <rate>\n");
121 fprintf(stderr, " --reflect normal,x,y,xy\n");
122 fprintf(stderr, " --rotate normal,inverted,left,right\n");
123 fprintf(stderr, " --left-of <output>\n");
124 fprintf(stderr, " --right-of <output>\n");
125 fprintf(stderr, " --above <output>\n");
126 fprintf(stderr, " --below <output>\n");
127 fprintf(stderr, " --same-as <output>\n");
128 fprintf(stderr, " --set <property> <value>\n");
129 fprintf(stderr, " --scale <x>x<y>\n");
130 fprintf(stderr, " --transform <a>,<b>,<c>,<d>,<e>,<f>,<g>,<h>,<i>\n");
131 fprintf(stderr, " --off\n");
132 fprintf(stderr, " --crtc <crtc>\n");
133 fprintf(stderr, " --panning <w>x<h>[+<x>+<y>[/<track:w>x<h>+<x>+<y>[/<border:l>/<t>/<r>/<b>]]]\n");
134 fprintf(stderr, " --gamma <r>:<g>:<b>\n");
135 fprintf(stderr, " --primary\n");
136 fprintf(stderr, " --noprimary\n");
137 fprintf(stderr, " --newmode <name> <clock MHz>\n");
138 fprintf(stderr, " <hdisp> <hsync-start> <hsync-end> <htotal>\n");
139 fprintf(stderr, " <vdisp> <vsync-start> <vsync-end> <vtotal>\n");
140 fprintf(stderr, " [+HSync] [-HSync] [+VSync] [-VSync]\n");
141 fprintf(stderr, " --rmmode <name>\n");
142 fprintf(stderr, " --addmode <output> <name>\n");
143 fprintf(stderr, " --delmode <output> <name>\n");
149 static void _X_NORETURN
150 fatal (const char *format, ...)
154 va_start (ap, format);
155 fprintf (stderr, "%s: ", program_name);
156 vfprintf (stderr, format, ap);
163 warning (const char *format, ...)
167 va_start (ap, format);
168 fprintf (stderr, "%s: ", program_name);
169 vfprintf (stderr, format, ap);
173 /* Because fmin requires C99 suppport */
174 static inline double dmin (double x, double y)
176 return x < y ? x : y;
180 rotation_name (Rotation rotation)
184 if ((rotation & 0xf) == 0)
186 for (i = 0; i < 4; i++)
187 if (rotation & (1 << i))
189 return "invalid rotation";
193 reflection_name (Rotation rotation)
195 rotation &= (RR_Reflect_X|RR_Reflect_Y);
203 case RR_Reflect_X|RR_Reflect_Y:
204 return "X and Y axis";
206 return "invalid reflection";
209 typedef enum _relation {
218 int x, y, width, height;
229 typedef enum _changes {
231 changes_crtc = (1 << 0),
232 changes_mode = (1 << 1),
233 changes_relation = (1 << 2),
234 changes_position = (1 << 3),
235 changes_rotation = (1 << 4),
236 changes_reflection = (1 << 5),
237 changes_automatic = (1 << 6),
238 changes_refresh = (1 << 7),
239 changes_property = (1 << 8),
240 changes_transform = (1 << 9),
241 changes_panning = (1 << 10),
242 changes_gamma = (1 << 11),
243 changes_primary = (1 << 12),
246 typedef enum _name_kind {
248 name_string = (1 << 0),
250 name_index = (1 << 2),
251 name_preferred = (1 << 3),
261 typedef struct _crtc crtc_t;
262 typedef struct _output output_t;
263 typedef struct _transform transform_t;
264 typedef struct _umode umode_t;
265 typedef struct _output_prop output_prop_t;
268 XTransform transform;
277 XRRCrtcInfo *crtc_info;
279 XRRModeInfo *mode_info;
280 XRRPanning *panning_info;
286 transform_t current_transform, pending_transform;
289 struct _output_prop {
290 struct _output_prop *next;
296 struct _output *next;
300 output_prop_t *props;
303 XRROutputInfo *output_info;
307 crtc_t *current_crtc_info;
311 XRRModeInfo *mode_info;
324 transform_t transform;
339 typedef enum _umode_action {
340 umode_create, umode_destroy, umode_add, umode_delete
347 umode_action_t action;
353 static char *connection[3] = {
356 "unknown connection"};
358 #define OUTPUT_NAME 1
362 #define CRTC_INDEX 0x40000000
371 static output_t *outputs = NULL;
372 static output_t **outputs_tail = &outputs;
373 static crtc_t *crtcs;
374 static umode_t *umodes;
375 static int num_crtcs;
376 static XRRScreenResources *res;
377 static int fb_width = 0, fb_height = 0;
378 static int fb_width_mm = 0, fb_height_mm = 0;
379 static double dpi = 0;
380 static char *dpi_output = NULL;
381 static Bool dryrun = False;
382 static int minWidth, maxWidth, minHeight, maxHeight;
383 static Bool has_1_2 = False;
384 static Bool has_1_3 = False;
387 mode_height (XRRModeInfo *mode_info, Rotation rotation)
389 switch (rotation & 0xf) {
392 return mode_info->height;
395 return mode_info->width;
402 mode_width (XRRModeInfo *mode_info, Rotation rotation)
404 switch (rotation & 0xf) {
407 return mode_info->width;
410 return mode_info->height;
417 transform_point (XTransform *transform, double *xp, double *yp)
427 for (j = 0; j < 3; j++)
430 for (i = 0; i < 3; i++)
431 v += (XFixedToDouble (transform->matrix[j][i]) * vector[i]);
432 if (v > 32767 || v < -32767)
438 for (j = 0; j < 2; j++)
439 vector[j] = result[j] / result[2];
446 path_bounds (XTransform *transform, point_t *points, int npoints, box_t *box)
451 for (i = 0; i < npoints; i++) {
455 transform_point (transform, &x, &y);
456 point.x1 = floor (x);
457 point.y1 = floor (y);
463 if (point.x1 < box->x1) box->x1 = point.x1;
464 if (point.y1 < box->y1) box->y1 = point.y1;
465 if (point.x2 > box->x2) box->x2 = point.x2;
466 if (point.y2 > box->y2) box->y2 = point.y2;
472 mode_geometry (XRRModeInfo *mode_info, Rotation rotation,
473 XTransform *transform,
477 int width = mode_width (mode_info, rotation);
478 int height = mode_height (mode_info, rotation);
488 path_bounds (transform, rect, 4, bounds);
491 /* v refresh frequency in Hz */
493 mode_refresh (XRRModeInfo *mode_info)
497 if (mode_info->hTotal && mode_info->vTotal)
498 rate = ((double) mode_info->dotClock /
499 ((double) mode_info->hTotal * (double) mode_info->vTotal));
505 /* h sync frequency in Hz */
507 mode_hsync (XRRModeInfo *mode_info)
511 if (mode_info->hTotal)
512 rate = (double) mode_info->dotClock / (double) mode_info->hTotal;
519 init_name (name_t *name)
521 name->kind = name_none;
525 set_name_string (name_t *name, char *string)
527 name->kind |= name_string;
528 name->string = string;
532 set_name_xid (name_t *name, XID xid)
534 name->kind |= name_xid;
539 set_name_index (name_t *name, int index)
541 name->kind |= name_index;
546 set_name_preferred (name_t *name)
548 name->kind |= name_preferred;
552 set_name_all (name_t *name, name_t *old)
554 if (old->kind & name_xid)
555 name->xid = old->xid;
556 if (old->kind & name_string)
557 name->string = old->string;
558 if (old->kind & name_index)
559 name->index = old->index;
560 name->kind |= old->kind;
564 set_name (name_t *name, char *string, name_kind_t valid)
566 unsigned int xid; /* don't make it XID (which is unsigned long):
567 scanf() takes unsigned int */
570 if ((valid & name_xid) && sscanf (string, "0x%x", &xid) == 1)
571 set_name_xid (name, xid);
572 else if ((valid & name_index) && sscanf (string, "%d", &index) == 1)
573 set_name_index (name, index);
574 else if (valid & name_string)
575 set_name_string (name, string);
581 init_transform (transform_t *transform)
584 memset (&transform->transform, '\0', sizeof (transform->transform));
585 for (x = 0; x < 3; x++)
586 transform->transform.matrix[x][x] = XDoubleToFixed (1.0);
587 transform->filter = "";
588 transform->nparams = 0;
589 transform->params = NULL;
593 set_transform (transform_t *dest,
594 XTransform *transform,
599 dest->transform = *transform;
600 dest->filter = strdup (filter);
601 dest->nparams = nparams;
602 dest->params = malloc (nparams * sizeof (XFixed));
603 memcpy (dest->params, params, nparams * sizeof (XFixed));
607 copy_transform (transform_t *dest, transform_t *src)
609 set_transform (dest, &src->transform,
610 src->filter, src->params, src->nparams);
614 equal_transform (transform_t *a, transform_t *b)
616 if (memcmp (&a->transform, &b->transform, sizeof (XTransform)) != 0)
618 if (strcmp (a->filter, b->filter) != 0)
620 if (a->nparams != b->nparams)
622 if (memcmp (a->params, b->params, a->nparams * sizeof (XFixed)) != 0)
630 output_t *output = calloc (1, sizeof (output_t));
633 fatal ("out of memory\n");
635 output->found = False;
636 output->brightness = 1.0;
637 *outputs_tail = output;
638 outputs_tail = &output->next;
643 find_output (name_t *name)
647 for (output = outputs; output; output = output->next)
649 name_kind_t common = name->kind & output->output.kind;
651 if ((common & name_xid) && name->xid == output->output.xid)
653 if ((common & name_string) && !strcmp (name->string, output->output.string))
655 if ((common & name_index) && name->index == output->output.index)
662 find_output_by_xid (RROutput output)
666 init_name (&output_name);
667 set_name_xid (&output_name, output);
668 return find_output (&output_name);
672 find_output_by_name (char *name)
676 init_name (&output_name);
677 set_name_string (&output_name, name);
678 return find_output (&output_name);
682 find_crtc (name_t *name)
687 for (c = 0; c < num_crtcs; c++)
692 common = name->kind & crtc->crtc.kind;
694 if ((common & name_xid) && name->xid == crtc->crtc.xid)
696 if ((common & name_string) && !strcmp (name->string, crtc->crtc.string))
698 if ((common & name_index) && name->index == crtc->crtc.index)
706 find_crtc_by_xid (RRCrtc crtc)
710 init_name (&crtc_name);
711 set_name_xid (&crtc_name, crtc);
712 return find_crtc (&crtc_name);
716 find_mode (name_t *name, double refresh)
719 XRRModeInfo *best = NULL;
722 for (m = 0; m < res->nmode; m++)
724 XRRModeInfo *mode = &res->modes[m];
725 if ((name->kind & name_xid) && name->xid == mode->id)
730 if ((name->kind & name_string) && !strcmp (name->string, mode->name))
735 dist = fabs (mode_refresh (mode) - refresh);
738 if (!best || dist < bestDist)
750 find_mode_by_xid (RRMode mode)
754 init_name (&mode_name);
755 set_name_xid (&mode_name, mode);
756 return find_mode (&mode_name, 0);
761 find_mode_by_name (char *name)
764 init_name (&mode_name);
765 set_name_string (&mode_name, name);
766 return find_mode (&mode_name, 0);
772 find_mode_for_output (output_t *output, name_t *name)
774 XRROutputInfo *output_info = output->output_info;
776 XRRModeInfo *best = NULL;
779 for (m = 0; m < output_info->nmode; m++)
783 mode = find_mode_by_xid (output_info->modes[m]);
785 if ((name->kind & name_xid) && name->xid == mode->id)
790 if ((name->kind & name_string) && !strcmp (name->string, mode->name))
794 /* Stay away from doublescan modes unless refresh rate is specified. */
795 if (!output->refresh && (mode->modeFlags & RR_DoubleScan))
799 dist = fabs (mode_refresh (mode) - output->refresh);
802 if (!best || dist < bestDist)
813 preferred_mode (output_t *output)
815 XRROutputInfo *output_info = output->output_info;
822 for (m = 0; m < output_info->nmode; m++)
824 XRRModeInfo *mode_info = find_mode_by_xid (output_info->modes[m]);
827 if (m < output_info->npreferred)
829 else if (output_info->mm_height)
830 dist = (1000 * DisplayHeight(dpy, screen) / DisplayHeightMM(dpy, screen) -
831 1000 * mode_info->height / output_info->mm_height);
833 dist = DisplayHeight(dpy, screen) - mode_info->height;
835 if (dist < 0) dist = -dist;
836 if (!best || dist < bestDist)
846 output_can_use_crtc (output_t *output, crtc_t *crtc)
848 XRROutputInfo *output_info = output->output_info;
851 for (c = 0; c < output_info->ncrtc; c++)
852 if (output_info->crtcs[c] == crtc->crtc.xid)
858 output_can_use_mode (output_t *output, XRRModeInfo *mode)
860 XRROutputInfo *output_info = output->output_info;
863 for (m = 0; m < output_info->nmode; m++)
864 if (output_info->modes[m] == mode->id)
870 crtc_can_use_rotation (crtc_t *crtc, Rotation rotation)
872 Rotation rotations = crtc->crtc_info->rotations;
873 Rotation dir = rotation & (RR_Rotate_0|RR_Rotate_90|RR_Rotate_180|RR_Rotate_270);
874 Rotation reflect = rotation & (RR_Reflect_X|RR_Reflect_Y);
875 if (((rotations & dir) != 0) && ((rotations & reflect) == reflect))
882 crtc_can_use_transform (crtc_t *crtc, XTransform *transform)
886 XRRQueryVersion (dpy, &major, &minor);
887 if (major > 1 || (major == 1 && minor >= 3))
894 * Report only rotations that are supported by all crtcs
897 output_rotations (output_t *output)
900 Rotation rotation = RR_Rotate_0;
901 XRROutputInfo *output_info = output->output_info;
904 for (c = 0; c < output_info->ncrtc; c++)
906 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
910 rotation = crtc->crtc_info->rotations;
913 rotation &= crtc->crtc_info->rotations;
920 output_can_use_rotation (output_t *output, Rotation rotation)
922 XRROutputInfo *output_info = output->output_info;
925 /* make sure all of the crtcs can use this rotation.
926 * yes, this is not strictly necessary, but it is
927 * simpler,and we expect most drivers to either
928 * support rotation everywhere or nowhere
930 for (c = 0; c < output_info->ncrtc; c++)
932 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
933 if (crtc && !crtc_can_use_rotation (crtc, rotation))
940 output_is_primary(output_t *output)
943 return XRRGetOutputPrimary(dpy, root) == output->output.xid;
947 /* Returns the index of the last value in an array < 0xffff */
949 find_last_non_clamped(CARD16 array[], int size) {
951 for (i = size - 1; i > 0; i--) {
952 if (array[i] < 0xffff)
959 set_gamma_info(output_t *output)
962 double i1, v1, i2, v2;
963 int size, middle, last_best, last_red, last_green, last_blue;
966 if (!output->crtc_info)
969 size = XRRGetCrtcGammaSize(dpy, output->crtc_info->crtc.xid);
971 warning("Failed to get size of gamma for output %s\n", output->output.string);
975 gamma = XRRGetCrtcGamma(dpy, output->crtc_info->crtc.xid);
977 warning("Failed to get gamma for output %s\n", output->output.string);
982 * Here is a bit tricky because gamma is a whole curve for each
983 * color. So, typically, we need to represent 3 * 256 values as 3 + 1
984 * values. Therefore, we approximate the gamma curve (v) by supposing
985 * it always follows the way we set it: a power function (i^g)
986 * multiplied by a brightness (b).
988 * so g = (ln(v) - ln(b))/ln(i)
989 * and b can be found using two points (v1,i1) and (v2, i2):
990 * b = e^((ln(v2)*ln(i1) - ln(v1)*ln(i2))/ln(i1/i2))
991 * For the best resolution, we select i2 at the highest place not
992 * clamped and i1 at i2/2. Note that if i2 = 1 (as in most normal
993 * cases), then b = v2.
995 last_red = find_last_non_clamped(gamma->red, size);
996 last_green = find_last_non_clamped(gamma->green, size);
997 last_blue = find_last_non_clamped(gamma->blue, size);
998 best_array = gamma->red;
999 last_best = last_red;
1000 if (last_green > last_best) {
1001 last_best = last_green;
1002 best_array = gamma->green;
1004 if (last_blue > last_best) {
1005 last_best = last_blue;
1006 best_array = gamma->blue;
1011 middle = last_best / 2;
1012 i1 = (double)(middle + 1) / size;
1013 v1 = (double)(best_array[middle]) / 65535;
1014 i2 = (double)(last_best + 1) / size;
1015 v2 = (double)(best_array[last_best]) / 65535;
1016 if (v2 < 0.0001) { /* The screen is black */
1017 output->brightness = 0;
1018 output->gamma.red = 1;
1019 output->gamma.green = 1;
1020 output->gamma.blue = 1;
1022 if ((last_best + 1) == size)
1023 output->brightness = v2;
1025 output->brightness = exp((log(v2)*log(i1) - log(v1)*log(i2))/log(i1/i2));
1026 output->gamma.red = log((double)(gamma->red[last_red / 2]) / output->brightness
1027 / 65535) / log((double)((last_red / 2) + 1) / size);
1028 output->gamma.green = log((double)(gamma->green[last_green / 2]) / output->brightness
1029 / 65535) / log((double)((last_green / 2) + 1) / size);
1030 output->gamma.blue = log((double)(gamma->blue[last_blue / 2]) / output->brightness
1031 / 65535) / log((double)((last_blue / 2) + 1) / size);
1034 XRRFreeGamma(gamma);
1038 set_output_info (output_t *output, RROutput xid, XRROutputInfo *output_info)
1040 /* sanity check output info */
1041 if (output_info->connection != RR_Disconnected && !output_info->nmode)
1042 warning ("Output %s is not disconnected but has no modes\n",
1045 /* set output name and info */
1046 if (!(output->output.kind & name_xid))
1047 set_name_xid (&output->output, xid);
1048 if (!(output->output.kind & name_string))
1049 set_name_string (&output->output, output_info->name);
1050 output->output_info = output_info;
1052 /* set crtc name and info */
1053 if (!(output->changes & changes_crtc))
1054 set_name_xid (&output->crtc, output_info->crtc);
1056 if (output->crtc.kind == name_xid && output->crtc.xid == None)
1057 output->crtc_info = NULL;
1060 output->crtc_info = find_crtc (&output->crtc);
1061 if (!output->crtc_info)
1063 if (output->crtc.kind & name_xid)
1064 fatal ("cannot find crtc 0x%x\n", output->crtc.xid);
1065 if (output->crtc.kind & name_index)
1066 fatal ("cannot find crtc %d\n", output->crtc.index);
1068 if (!output_can_use_crtc (output, output->crtc_info))
1069 fatal ("output %s cannot use crtc 0x%x\n", output->output.string,
1070 output->crtc_info->crtc.xid);
1073 /* set mode name and info */
1074 if (!(output->changes & changes_mode))
1076 if (output->crtc_info)
1077 set_name_xid (&output->mode, output->crtc_info->crtc_info->mode);
1079 set_name_xid (&output->mode, None);
1080 if (output->mode.xid)
1082 output->mode_info = find_mode_by_xid (output->mode.xid);
1083 if (!output->mode_info)
1084 fatal ("server did not report mode 0x%x for output %s\n",
1085 output->mode.xid, output->output.string);
1088 output->mode_info = NULL;
1090 else if (output->mode.kind == name_xid && output->mode.xid == None)
1091 output->mode_info = NULL;
1094 if (output->mode.kind == name_preferred)
1095 output->mode_info = preferred_mode (output);
1097 output->mode_info = find_mode_for_output (output, &output->mode);
1098 if (!output->mode_info)
1100 if (output->mode.kind & name_preferred)
1101 fatal ("cannot find preferred mode\n");
1102 if (output->mode.kind & name_string)
1103 fatal ("cannot find mode %s\n", output->mode.string);
1104 if (output->mode.kind & name_xid)
1105 fatal ("cannot find mode 0x%x\n", output->mode.xid);
1107 if (!output_can_use_mode (output, output->mode_info))
1108 fatal ("output %s cannot use mode %s\n", output->output.string,
1109 output->mode_info->name);
1113 if (!(output->changes & changes_position))
1115 if (output->crtc_info)
1117 output->x = output->crtc_info->crtc_info->x;
1118 output->y = output->crtc_info->crtc_info->y;
1128 if (!(output->changes & changes_rotation))
1130 output->rotation &= ~0xf;
1131 if (output->crtc_info)
1132 output->rotation |= (output->crtc_info->crtc_info->rotation & 0xf);
1134 output->rotation = RR_Rotate_0;
1136 if (!(output->changes & changes_reflection))
1138 output->rotation &= ~(RR_Reflect_X|RR_Reflect_Y);
1139 if (output->crtc_info)
1140 output->rotation |= (output->crtc_info->crtc_info->rotation &
1141 (RR_Reflect_X|RR_Reflect_Y));
1143 if (!output_can_use_rotation (output, output->rotation))
1144 fatal ("output %s cannot use rotation \"%s\" reflection \"%s\"\n",
1145 output->output.string,
1146 rotation_name (output->rotation),
1147 reflection_name (output->rotation));
1150 if (!(output->changes & changes_gamma))
1151 set_gamma_info(output);
1153 /* set transformation */
1154 if (!(output->changes & changes_transform))
1156 if (output->crtc_info)
1157 copy_transform (&output->transform, &output->crtc_info->current_transform);
1159 init_transform (&output->transform);
1163 if (!(output->changes & changes_primary))
1164 output->primary = output_is_primary(output);
1168 get_screen (Bool current)
1171 fatal ("Server RandR version before 1.2\n");
1173 XRRGetScreenSizeRange (dpy, root, &minWidth, &minHeight,
1174 &maxWidth, &maxHeight);
1177 res = XRRGetScreenResourcesCurrent (dpy, root);
1179 res = XRRGetScreenResources (dpy, root);
1180 if (!res) fatal ("could not get screen resources");
1188 num_crtcs = res->ncrtc;
1189 crtcs = calloc (num_crtcs, sizeof (crtc_t));
1190 if (!crtcs) fatal ("out of memory\n");
1192 for (c = 0; c < res->ncrtc; c++)
1194 XRRCrtcInfo *crtc_info = XRRGetCrtcInfo (dpy, res, res->crtcs[c]);
1195 XRRCrtcTransformAttributes *attr;
1196 XRRPanning *panning_info = NULL;
1200 memset(&zero, 0, sizeof(zero));
1201 panning_info = XRRGetPanning (dpy, res, res->crtcs[c]);
1202 zero.timestamp = panning_info->timestamp;
1203 if (!memcmp(panning_info, &zero, sizeof(zero))) {
1204 Xfree(panning_info);
1205 panning_info = NULL;
1209 set_name_xid (&crtcs[c].crtc, res->crtcs[c]);
1210 set_name_index (&crtcs[c].crtc, c);
1211 if (!crtc_info) fatal ("could not get crtc 0x%x information\n", res->crtcs[c]);
1212 crtcs[c].crtc_info = crtc_info;
1213 crtcs[c].panning_info = panning_info;
1214 if (crtc_info->mode == None)
1216 crtcs[c].mode_info = NULL;
1219 crtcs[c].rotation = RR_Rotate_0;
1221 if (XRRGetCrtcTransform (dpy, res->crtcs[c], &attr) && attr) {
1222 set_transform (&crtcs[c].current_transform,
1223 &attr->currentTransform,
1224 attr->currentFilter,
1225 attr->currentParams,
1226 attr->currentNparams);
1231 init_transform (&crtcs[c].current_transform);
1233 copy_transform (&crtcs[c].pending_transform, &crtcs[c].current_transform);
1238 crtc_add_output (crtc_t *crtc, output_t *output)
1241 crtc->outputs = realloc (crtc->outputs, (crtc->noutput + 1) * sizeof (output_t *));
1244 crtc->outputs = malloc (sizeof (output_t *));
1245 crtc->x = output->x;
1246 crtc->y = output->y;
1247 crtc->rotation = output->rotation;
1248 crtc->mode_info = output->mode_info;
1249 copy_transform (&crtc->pending_transform, &output->transform);
1251 if (!crtc->outputs) fatal ("out of memory\n");
1252 crtc->outputs[crtc->noutput++] = output;
1260 for (output = outputs; output; output = output->next)
1262 if (!output->mode_info) continue;
1263 crtc_add_output (output->crtc_info, output);
1272 for (output = outputs; output; output = output->next)
1274 if (! output->crtc_info)
1276 if (! (output->changes & changes_panning))
1278 if (! output->crtc_info->panning_info)
1279 output->crtc_info->panning_info = malloc (sizeof(XRRPanning));
1280 memcpy (output->crtc_info->panning_info, &output->panning, sizeof(XRRPanning));
1281 output->crtc_info->changing = 1;
1290 for (output = outputs; output; output = output->next) {
1293 XRRCrtcGamma *gamma;
1295 if (!(output->changes & changes_gamma))
1298 if (!output->crtc_info) {
1299 fatal("Need crtc to set gamma on.\n");
1303 crtc = output->crtc_info;
1305 size = XRRGetCrtcGammaSize(dpy, crtc->crtc.xid);
1308 fatal("Gamma size is 0.\n");
1312 gamma = XRRAllocGamma(size);
1314 fatal("Gamma allocation failed.\n");
1318 if(output->gamma.red == 0.0 && output->gamma.green == 0.0 && output->gamma.blue == 0.0)
1319 output->gamma.red = output->gamma.green = output->gamma.blue = 1.0;
1321 for (i = 0; i < size; i++) {
1322 if (output->gamma.red == 1.0 && output->brightness == 1.0)
1323 gamma->red[i] = (i << 8) + i;
1325 gamma->red[i] = dmin(pow((double)i/(double)(size - 1),
1326 output->gamma.red) * output->brightness,
1329 if (output->gamma.green == 1.0 && output->brightness == 1.0)
1330 gamma->green[i] = (i << 8) + i;
1332 gamma->green[i] = dmin(pow((double)i/(double)(size - 1),
1333 output->gamma.green) * output->brightness,
1336 if (output->gamma.blue == 1.0 && output->brightness == 1.0)
1337 gamma->blue[i] = (i << 8) + i;
1339 gamma->blue[i] = dmin(pow((double)i/(double)(size - 1),
1340 output->gamma.blue) * output->brightness,
1344 XRRSetCrtcGamma(dpy, crtc->crtc.xid, gamma);
1356 XRRSetOutputPrimary(dpy, root, None);
1358 for (output = outputs; output; output = output->next) {
1359 if (!(output->changes & changes_primary))
1361 if (output->primary)
1362 XRRSetOutputPrimary(dpy, root, output->output.xid);
1368 crtc_disable (crtc_t *crtc)
1371 printf ("crtc %d: disable\n", crtc->crtc.index);
1374 return RRSetConfigSuccess;
1375 return XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1376 0, 0, None, RR_Rotate_0, NULL, 0);
1380 crtc_set_transform (crtc_t *crtc, transform_t *transform)
1384 XRRQueryVersion (dpy, &major, &minor);
1385 if (major > 1 || (major == 1 && minor >= 3))
1386 XRRSetCrtcTransform (dpy, crtc->crtc.xid,
1387 &transform->transform,
1390 transform->nparams);
1394 crtc_revert (crtc_t *crtc)
1396 XRRCrtcInfo *crtc_info = crtc->crtc_info;
1399 printf ("crtc %d: revert\n", crtc->crtc.index);
1402 return RRSetConfigSuccess;
1404 if (!equal_transform (&crtc->current_transform, &crtc->pending_transform))
1405 crtc_set_transform (crtc, &crtc->current_transform);
1406 return XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1407 crtc_info->x, crtc_info->y,
1408 crtc_info->mode, crtc_info->rotation,
1409 crtc_info->outputs, crtc_info->noutput);
1413 crtc_apply (crtc_t *crtc)
1415 RROutput *rr_outputs;
1420 if (!crtc->changing || !crtc->mode_info)
1421 return RRSetConfigSuccess;
1423 rr_outputs = calloc (crtc->noutput, sizeof (RROutput));
1426 for (o = 0; o < crtc->noutput; o++)
1427 rr_outputs[o] = crtc->outputs[o]->output.xid;
1428 mode = crtc->mode_info->id;
1430 printf ("crtc %d: %12s %6.1f +%d+%d", crtc->crtc.index,
1431 crtc->mode_info->name, mode_refresh (crtc->mode_info),
1433 for (o = 0; o < crtc->noutput; o++)
1434 printf (" \"%s\"", crtc->outputs[o]->output.string);
1439 s = RRSetConfigSuccess;
1442 if (!equal_transform (&crtc->current_transform, &crtc->pending_transform))
1443 crtc_set_transform (crtc, &crtc->pending_transform);
1444 s = XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1445 crtc->x, crtc->y, mode, crtc->rotation,
1446 rr_outputs, crtc->noutput);
1447 if (s == RRSetConfigSuccess && crtc->panning_info) {
1449 s = XRRSetPanning (dpy, res, crtc->crtc.xid, crtc->panning_info);
1451 fatal ("panning needs RandR 1.3\n");
1459 screen_revert (void)
1462 printf ("screen %d: revert\n", screen);
1466 XRRSetScreenSize (dpy, root,
1467 DisplayWidth (dpy, screen),
1468 DisplayHeight (dpy, screen),
1469 DisplayWidthMM (dpy, screen),
1470 DisplayHeightMM (dpy, screen));
1476 if (fb_width == DisplayWidth (dpy, screen) &&
1477 fb_height == DisplayHeight (dpy, screen) &&
1478 fb_width_mm == DisplayWidthMM (dpy, screen) &&
1479 fb_height_mm == DisplayHeightMM (dpy, screen))
1484 printf ("screen %d: %dx%d %dx%d mm %6.2fdpi\n", screen,
1485 fb_width, fb_height, fb_width_mm, fb_height_mm, dpi);
1488 XRRSetScreenSize (dpy, root, fb_width, fb_height,
1489 fb_width_mm, fb_height_mm);
1497 /* first disable all crtcs */
1498 for (c = 0; c < res->ncrtc; c++)
1499 crtc_disable (&crtcs[c]);
1500 /* next reset screen size */
1502 /* now restore all crtcs */
1503 for (c = 0; c < res->ncrtc; c++)
1504 crtc_revert (&crtcs[c]);
1508 * uh-oh, something bad happened in the middle of changing
1509 * the configuration. Revert to the previous configuration
1512 static void _X_NORETURN
1513 panic (Status s, crtc_t *crtc)
1515 int c = crtc->crtc.index;
1519 case RRSetConfigSuccess: message = "succeeded"; break;
1520 case BadAlloc: message = "out of memory"; break;
1521 case RRSetConfigFailed: message = "failed"; break;
1522 case RRSetConfigInvalidConfigTime: message = "invalid config time"; break;
1523 case RRSetConfigInvalidTime: message = "invalid time"; break;
1524 default: message = "unknown failure"; break;
1527 fprintf (stderr, "%s: Configure crtc %d %s\n", program_name, c, message);
1539 * Hold the server grabbed while messing with
1540 * the screen so that apps which notice the resize
1541 * event and ask for xinerama information from the server
1542 * receive up-to-date information
1548 * Turn off any crtcs which are to be disabled or which are
1549 * larger than the target size
1551 for (c = 0; c < res->ncrtc; c++)
1553 crtc_t *crtc = &crtcs[c];
1554 XRRCrtcInfo *crtc_info = crtc->crtc_info;
1556 /* if this crtc is already disabled, skip it */
1557 if (crtc_info->mode == None)
1561 * If this crtc is to be left enabled, make
1562 * sure the old size fits then new screen
1564 if (crtc->mode_info)
1566 XRRModeInfo *old_mode = find_mode_by_xid (crtc_info->mode);
1571 panic (RRSetConfigFailed, crtc);
1573 /* old position and size information */
1574 mode_geometry (old_mode, crtc_info->rotation,
1575 &crtc->current_transform.transform,
1578 x = crtc_info->x + bounds.x1;
1579 y = crtc_info->y + bounds.y1;
1580 w = bounds.x2 - bounds.x1;
1581 h = bounds.y2 - bounds.y1;
1583 /* if it fits, skip it */
1584 if (x + w <= fb_width && y + h <= fb_height)
1586 crtc->changing = True;
1588 s = crtc_disable (crtc);
1589 if (s != RRSetConfigSuccess)
1594 * Set the screen size
1602 for (c = 0; c < res->ncrtc; c++)
1604 crtc_t *crtc = &crtcs[c];
1606 s = crtc_apply (crtc);
1607 if (s != RRSetConfigSuccess)
1614 * Release the server grab and let all clients
1615 * respond to the updated state
1618 XUngrabServer (dpy);
1622 * Use current output state to complete the output list
1630 for (o = 0; o < res->noutput; o++)
1632 XRROutputInfo *output_info = XRRGetOutputInfo (dpy, res, res->outputs[o]);
1635 if (!output_info) fatal ("could not get output 0x%x information\n", res->outputs[o]);
1636 set_name_xid (&output_name, res->outputs[o]);
1637 set_name_index (&output_name, o);
1638 set_name_string (&output_name, output_info->name);
1639 output = find_output (&output_name);
1642 output = add_output ();
1643 set_name_all (&output->output, &output_name);
1645 * When global --automatic mode is set, turn on connected but off
1646 * outputs, turn off disconnected but on outputs
1650 switch (output_info->connection) {
1652 if (!output_info->crtc) {
1653 output->changes |= changes_automatic;
1654 output->automatic = True;
1657 case RR_Disconnected:
1658 if (output_info->crtc)
1660 output->changes |= changes_automatic;
1661 output->automatic = True;
1667 output->found = True;
1670 * Automatic mode -- track connection state and enable/disable outputs
1673 if (output->automatic)
1675 switch (output_info->connection) {
1677 case RR_UnknownConnection:
1678 if ((!(output->changes & changes_mode)))
1680 set_name_preferred (&output->mode);
1681 output->changes |= changes_mode;
1684 case RR_Disconnected:
1685 if ((!(output->changes & changes_mode)))
1687 set_name_xid (&output->mode, None);
1688 set_name_xid (&output->crtc, None);
1689 output->changes |= changes_mode;
1690 output->changes |= changes_crtc;
1696 set_output_info (output, res->outputs[o], output_info);
1698 for (q = outputs; q; q = q->next)
1702 fprintf(stderr, "warning: output %s not found; ignoring\n",
1709 mark_changing_crtcs (void)
1713 for (c = 0; c < num_crtcs; c++)
1715 crtc_t *crtc = &crtcs[c];
1719 /* walk old output list (to catch disables) */
1720 for (o = 0; o < crtc->crtc_info->noutput; o++)
1722 output = find_output_by_xid (crtc->crtc_info->outputs[o]);
1723 if (!output) fatal ("cannot find output 0x%x\n",
1724 crtc->crtc_info->outputs[o]);
1725 if (output->changes)
1726 crtc->changing = True;
1728 /* walk new output list */
1729 for (o = 0; o < crtc->noutput; o++)
1731 output = crtc->outputs[o];
1732 if (output->changes)
1733 crtc->changing = True;
1739 * Test whether 'crtc' can be used for 'output'
1742 check_crtc_for_output (crtc_t *crtc, output_t *output)
1748 for (c = 0; c < output->output_info->ncrtc; c++)
1749 if (output->output_info->crtcs[c] == crtc->crtc.xid)
1751 if (c == output->output_info->ncrtc)
1753 for (other = outputs; other; other = other->next)
1755 if (other == output)
1758 if (other->mode_info == NULL)
1761 if (other->crtc_info != crtc)
1764 /* see if the output connected to the crtc can clone to this output */
1765 for (l = 0; l < output->output_info->nclone; l++)
1766 if (output->output_info->clones[l] == other->output.xid)
1768 /* not on the list, can't clone */
1769 if (l == output->output_info->nclone)
1775 /* make sure the state matches */
1776 if (crtc->mode_info != output->mode_info)
1778 if (crtc->x != output->x)
1780 if (crtc->y != output->y)
1782 if (crtc->rotation != output->rotation)
1784 if (!equal_transform (&crtc->current_transform, &output->transform))
1787 else if (crtc->crtc_info->noutput)
1789 /* make sure the state matches the already used state */
1790 XRRModeInfo *mode = find_mode_by_xid (crtc->crtc_info->mode);
1792 if (mode != output->mode_info)
1794 if (crtc->crtc_info->x != output->x)
1796 if (crtc->crtc_info->y != output->y)
1798 if (crtc->crtc_info->rotation != output->rotation)
1805 find_crtc_for_output (output_t *output)
1809 for (c = 0; c < output->output_info->ncrtc; c++)
1813 crtc = find_crtc_by_xid (output->output_info->crtcs[c]);
1814 if (!crtc) fatal ("cannot find crtc 0x%x\n", output->output_info->crtcs[c]);
1816 if (check_crtc_for_output (crtc, output))
1823 set_positions (void)
1834 for (output = outputs; output; output = output->next)
1837 name_t relation_name;
1839 if (!(output->changes & changes_relation)) continue;
1841 if (output->mode_info == NULL) continue;
1843 init_name (&relation_name);
1844 set_name_string (&relation_name, output->relative_to);
1845 relation = find_output (&relation_name);
1846 if (!relation) fatal ("cannot find output \"%s\"\n", output->relative_to);
1848 if (relation->mode_info == NULL)
1852 output->changes |= changes_position;
1857 * Make sure the dependent object has been set in place
1859 if ((relation->changes & changes_relation) &&
1860 !(relation->changes & changes_position))
1866 switch (output->relation) {
1867 case relation_left_of:
1868 output->y = relation->y;
1869 output->x = relation->x - mode_width (output->mode_info, output->rotation);
1871 case relation_right_of:
1872 output->y = relation->y;
1873 output->x = relation->x + mode_width (relation->mode_info, relation->rotation);
1875 case relation_above:
1876 output->x = relation->x;
1877 output->y = relation->y - mode_height (output->mode_info, output->rotation);
1879 case relation_below:
1880 output->x = relation->x;
1881 output->y = relation->y + mode_height (relation->mode_info, relation->rotation);
1883 case relation_same_as:
1884 output->x = relation->x;
1885 output->y = relation->y;
1887 output->changes |= changes_position;
1893 fatal ("loop in relative position specifications\n");
1897 * Now normalize positions so the upper left corner of all outputs is at 0,0
1901 for (output = outputs; output; output = output->next)
1903 if (output->mode_info == NULL) continue;
1905 if (output->x < min_x) min_x = output->x;
1906 if (output->y < min_y) min_y = output->y;
1910 /* move all outputs */
1911 for (output = outputs; output; output = output->next)
1913 if (output->mode_info == NULL) continue;
1917 output->changes |= changes_position;
1923 set_screen_size (void)
1926 Bool fb_specified = fb_width != 0 && fb_height != 0;
1928 for (output = outputs; output; output = output->next)
1930 XRRModeInfo *mode_info = output->mode_info;
1934 if (!mode_info) continue;
1936 mode_geometry (mode_info, output->rotation,
1937 &output->transform.transform,
1939 x = output->x + bounds.x1;
1940 y = output->y + bounds.y1;
1941 w = bounds.x2 - bounds.x1;
1942 h = bounds.y2 - bounds.y1;
1943 /* make sure output fits in specified size */
1946 if (x + w > fb_width || y + h > fb_height)
1947 warning ("specified screen %dx%d not large enough for output %s (%dx%d+%d+%d)\n",
1948 fb_width, fb_height, output->output.string, w, h, x, y);
1950 /* fit fb to output */
1954 if (x + w > fb_width)
1956 if (y + h > fb_height)
1958 if (output->changes & changes_panning)
1959 pan = &output->panning;
1961 pan = output->crtc_info ? output->crtc_info->panning_info : NULL;
1962 if (pan && pan->left + pan->width > fb_width)
1963 fb_width = pan->left + pan->width;
1964 if (pan && pan->top + pan->height > fb_height)
1965 fb_height = pan->top + pan->height;
1969 if (fb_width > maxWidth || fb_height > maxHeight)
1970 fatal ("screen cannot be larger than %dx%d (desired size %dx%d)\n",
1971 maxWidth, maxHeight, fb_width, fb_height);
1974 if (fb_width < minWidth || fb_height < minHeight)
1975 fatal ("screen must be at least %dx%d\n", minWidth, minHeight);
1979 if (fb_width < minWidth) fb_width = minWidth;
1980 if (fb_height < minHeight) fb_height = minHeight;
1986 disable_outputs (output_t *outputs)
1990 outputs->crtc_info = NULL;
1991 outputs = outputs->next;
1996 * find the best mapping from output to crtc available
1999 pick_crtcs_score (output_t *outputs)
2012 outputs = outputs->next;
2014 * Score with this output disabled
2016 output->crtc_info = NULL;
2017 best_score = pick_crtcs_score (outputs);
2018 if (output->mode_info == NULL)
2023 * Now score with this output any valid crtc
2025 for (c = 0; c < output->output_info->ncrtc; c++)
2029 crtc = find_crtc_by_xid (output->output_info->crtcs[c]);
2031 fatal ("cannot find crtc 0x%x\n", output->output_info->crtcs[c]);
2033 /* reset crtc allocation for following outputs */
2034 disable_outputs (outputs);
2035 if (!check_crtc_for_output (crtc, output))
2039 /* slight preference for existing connections */
2040 if (crtc == output->current_crtc_info)
2043 output->crtc_info = crtc;
2044 score = my_score + pick_crtcs_score (outputs);
2045 if (score > best_score)
2051 if (output->crtc_info != best_crtc)
2052 output->crtc_info = best_crtc;
2054 * Reset other outputs based on this one using the best crtc
2056 (void) pick_crtcs_score (outputs);
2062 * Pick crtcs for any changing outputs that don't have one
2070 * First try to match up newly enabled outputs with spare crtcs
2072 for (output = outputs; output; output = output->next)
2074 if (output->changes && output->mode_info)
2076 if (output->crtc_info) {
2077 if (output->crtc_info->crtc_info->noutput > 0 &&
2078 (output->crtc_info->crtc_info->noutput > 1 ||
2079 output != find_output_by_xid (output->crtc_info->crtc_info->outputs[0])))
2082 output->crtc_info = find_crtc_for_output (output);
2083 if (!output->crtc_info)
2094 * When the simple way fails, see if there is a way
2095 * to swap crtcs around and make things work
2097 for (output = outputs; output; output = output->next)
2098 output->current_crtc_info = output->crtc_info;
2099 pick_crtcs_score (outputs);
2100 for (output = outputs; output; output = output->next)
2102 if (output->mode_info && !output->crtc_info)
2103 fatal ("cannot find crtc for output %s\n", output->output.string);
2104 if (!output->changes && output->crtc_info != output->current_crtc_info)
2105 output->changes |= changes_crtc;
2110 check_strtol(char *s)
2113 int result = strtol(s, &endptr, 10);
2120 check_strtod(char *s)
2123 double result = strtod(s, &endptr);
2130 main (int argc, char **argv)
2132 XRRScreenSize *sizes;
2133 XRRScreenConfiguration *sc;
2137 Status status = RRSetConfigFailed;
2140 int action_requested = False;
2141 Rotation rotation, current_rotation, rotations;
2143 XRRScreenChangeNotifyEvent *sce;
2144 char *display_name = NULL;
2146 SizeID current_size;
2152 Bool version = False;
2153 int event_base, error_base;
2155 int width = 0, height = 0;
2156 Bool have_pixel_size = False;
2158 output_t *output = NULL;
2159 Bool setit_1_2 = False;
2160 Bool query_1_2 = False;
2161 Bool modeit = False;
2162 Bool propit = False;
2163 Bool query_1 = False;
2165 Bool current = False;
2167 program_name = argv[0];
2168 for (i = 1; i < argc; i++) {
2169 if (!strcmp ("-display", argv[i]) || !strcmp ("-d", argv[i])) {
2170 if (++i>=argc) usage ();
2171 display_name = argv[i];
2174 if (!strcmp("-help", argv[i])) {
2176 action_requested = True;
2179 if (!strcmp ("--verbose", argv[i])) {
2183 if (!strcmp ("--dryrun", argv[i])) {
2188 if (!strcmp ("--nograb", argv[i])) {
2189 grab_server = False;
2192 if (!strcmp("--current", argv[i])) {
2197 if (!strcmp ("-s", argv[i]) || !strcmp ("--size", argv[i])) {
2198 if (++i>=argc) usage ();
2199 if (sscanf (argv[i], "%dx%d", &width, &height) == 2) {
2200 have_pixel_size = True;
2202 size = check_strtol(argv[i]);
2203 if (size < 0) usage();
2206 action_requested = True;
2210 if (!strcmp ("-r", argv[i]) ||
2211 !strcmp ("--rate", argv[i]) ||
2212 !strcmp ("--refresh", argv[i]))
2214 if (++i>=argc) usage ();
2215 rate = check_strtod(argv[i]);
2219 output->refresh = rate;
2220 output->changes |= changes_refresh;
2223 action_requested = True;
2227 if (!strcmp ("-v", argv[i]) || !strcmp ("--version", argv[i])) {
2229 action_requested = True;
2233 if (!strcmp ("-x", argv[i])) {
2234 reflection |= RR_Reflect_X;
2236 action_requested = True;
2239 if (!strcmp ("-y", argv[i])) {
2240 reflection |= RR_Reflect_Y;
2242 action_requested = True;
2245 if (!strcmp ("--screen", argv[i])) {
2246 if (++i>=argc) usage ();
2247 screen = check_strtol(argv[i]);
2248 if (screen < 0) usage();
2251 if (!strcmp ("-q", argv[i]) || !strcmp ("--query", argv[i])) {
2255 if (!strcmp ("-o", argv[i]) || !strcmp ("--orientation", argv[i])) {
2257 if (++i>=argc) usage ();
2258 dirind = strtol(argv[i], &endptr, 10);
2259 if (argv[i] == endptr) {
2260 for (dirind = 0; dirind < 4; dirind++) {
2261 if (strcmp (direction[dirind], argv[i]) == 0) break;
2263 if ((dirind < 0) || (dirind > 3)) usage();
2267 action_requested = True;
2270 if (!strcmp ("--prop", argv[i]) ||
2271 !strcmp ("--props", argv[i]) ||
2272 !strcmp ("--madprops", argv[i]) ||
2273 !strcmp ("--properties", argv[i]))
2277 action_requested = True;
2280 if (!strcmp ("--output", argv[i])) {
2281 if (++i >= argc) usage();
2283 output = find_output_by_name (argv[i]);
2285 output = add_output ();
2286 set_name (&output->output, argv[i], name_string|name_xid);
2290 action_requested = True;
2293 if (!strcmp ("--crtc", argv[i])) {
2294 if (++i >= argc) usage();
2295 if (!output) usage();
2296 set_name (&output->crtc, argv[i], name_xid|name_index);
2297 output->changes |= changes_crtc;
2300 if (!strcmp ("--mode", argv[i])) {
2301 if (++i >= argc) usage();
2302 if (!output) usage();
2303 set_name (&output->mode, argv[i], name_string|name_xid);
2304 output->changes |= changes_mode;
2307 if (!strcmp ("--preferred", argv[i])) {
2308 if (!output) usage();
2309 set_name_preferred (&output->mode);
2310 output->changes |= changes_mode;
2313 if (!strcmp ("--pos", argv[i])) {
2314 if (++i>=argc) usage ();
2315 if (!output) usage();
2316 if (sscanf (argv[i], "%dx%d",
2317 &output->x, &output->y) != 2)
2319 output->changes |= changes_position;
2322 if (!strcmp ("--rotation", argv[i]) || !strcmp ("--rotate", argv[i])) {
2323 if (++i>=argc) usage ();
2324 if (!output) usage();
2325 for (dirind = 0; dirind < 4; dirind++) {
2326 if (strcmp (direction[dirind], argv[i]) == 0) break;
2330 output->rotation &= ~0xf;
2331 output->rotation |= 1 << dirind;
2332 output->changes |= changes_rotation;
2335 if (!strcmp ("--reflect", argv[i]) || !strcmp ("--reflection", argv[i])) {
2336 if (++i>=argc) usage ();
2337 if (!output) usage();
2338 for (dirind = 0; dirind < 4; dirind++) {
2339 if (strcmp (reflections[dirind], argv[i]) == 0) break;
2343 output->rotation &= ~(RR_Reflect_X|RR_Reflect_Y);
2344 output->rotation |= dirind * RR_Reflect_X;
2345 output->changes |= changes_reflection;
2348 if (!strcmp ("--left-of", argv[i])) {
2349 if (++i>=argc) usage ();
2350 if (!output) usage();
2351 output->relation = relation_left_of;
2352 output->relative_to = argv[i];
2353 output->changes |= changes_relation;
2356 if (!strcmp ("--right-of", argv[i])) {
2357 if (++i>=argc) usage ();
2358 if (!output) usage();
2359 output->relation = relation_right_of;
2360 output->relative_to = argv[i];
2361 output->changes |= changes_relation;
2364 if (!strcmp ("--above", argv[i])) {
2365 if (++i>=argc) usage ();
2366 if (!output) usage();
2367 output->relation = relation_above;
2368 output->relative_to = argv[i];
2369 output->changes |= changes_relation;
2372 if (!strcmp ("--below", argv[i])) {
2373 if (++i>=argc) usage ();
2374 if (!output) usage();
2375 output->relation = relation_below;
2376 output->relative_to = argv[i];
2377 output->changes |= changes_relation;
2380 if (!strcmp ("--same-as", argv[i])) {
2381 if (++i>=argc) usage ();
2382 if (!output) usage();
2383 output->relation = relation_same_as;
2384 output->relative_to = argv[i];
2385 output->changes |= changes_relation;
2388 if (!strcmp ("--panning", argv[i])) {
2390 if (++i>=argc) usage ();
2391 if (!output) usage();
2392 pan = &output->panning;
2393 switch (sscanf (argv[i], "%dx%d+%d+%d/%dx%d+%d+%d/%d/%d/%d/%d",
2394 &pan->width, &pan->height, &pan->left, &pan->top,
2395 &pan->track_width, &pan->track_height,
2396 &pan->track_left, &pan->track_top,
2397 &pan->border_left, &pan->border_top,
2398 &pan->border_right, &pan->border_bottom)) {
2400 pan->left = pan->top = 0;
2403 pan->track_left = pan->track_top =
2404 pan->track_width = pan->track_height = 0;
2407 pan->border_left = pan->border_top =
2408 pan->border_right = pan->border_bottom = 0;
2415 output->changes |= changes_panning;
2418 if (!strcmp ("--gamma", argv[i])) {
2419 if (!output) usage();
2420 if (++i>=argc) usage ();
2421 if (sscanf(argv[i], "%f:%f:%f", &output->gamma.red,
2422 &output->gamma.green, &output->gamma.blue) != 3)
2424 output->changes |= changes_gamma;
2428 if (!strcmp ("--brightness", argv[i])) {
2429 if (!output) usage();
2430 if (++i>=argc) usage();
2431 if (sscanf(argv[i], "%f", &output->brightness) != 1)
2433 output->changes |= changes_gamma;
2437 if (!strcmp ("--primary", argv[i])) {
2438 if (!output) usage();
2439 output->changes |= changes_primary;
2440 output->primary = True;
2444 if (!strcmp ("--noprimary", argv[i])) {
2449 if (!strcmp ("--set", argv[i])) {
2450 output_prop_t *prop;
2451 if (!output) usage();
2452 prop = malloc (sizeof (output_prop_t));
2453 prop->next = output->props;
2454 output->props = prop;
2455 if (++i>=argc) usage ();
2456 prop->name = argv[i];
2457 if (++i>=argc) usage ();
2458 prop->value = argv[i];
2460 output->changes |= changes_property;
2464 if (!strcmp ("--scale", argv[i]))
2467 if (!output) usage();
2468 if (++i>=argc) usage();
2469 if (sscanf (argv[i], "%lfx%lf", &sx, &sy) != 2)
2471 init_transform (&output->transform);
2472 output->transform.transform.matrix[0][0] = XDoubleToFixed (sx);
2473 output->transform.transform.matrix[1][1] = XDoubleToFixed (sy);
2474 output->transform.transform.matrix[2][2] = XDoubleToFixed (1.0);
2475 if (sx != 1 || sy != 1)
2476 output->transform.filter = "bilinear";
2478 output->transform.filter = "nearest";
2479 output->transform.nparams = 0;
2480 output->transform.params = NULL;
2481 output->changes |= changes_transform;
2484 if (!strcmp ("--transform", argv[i])) {
2485 double transform[3][3];
2487 if (!output) usage();
2488 if (++i>=argc) usage ();
2489 init_transform (&output->transform);
2490 if (strcmp (argv[i], "none") != 0)
2492 if (sscanf(argv[i], "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf",
2493 &transform[0][0],&transform[0][1],&transform[0][2],
2494 &transform[1][0],&transform[1][1],&transform[1][2],
2495 &transform[2][0],&transform[2][1],&transform[2][2])
2498 init_transform (&output->transform);
2499 for (k = 0; k < 3; k++)
2500 for (l = 0; l < 3; l++) {
2501 output->transform.transform.matrix[k][l] = XDoubleToFixed (transform[k][l]);
2503 output->transform.filter = "bilinear";
2504 output->transform.nparams = 0;
2505 output->transform.params = NULL;
2507 output->changes |= changes_transform;
2510 if (!strcmp ("--off", argv[i])) {
2511 if (!output) usage();
2512 set_name_xid (&output->mode, None);
2513 set_name_xid (&output->crtc, None);
2514 output->changes |= changes_mode;
2517 if (!strcmp ("--fb", argv[i])) {
2518 if (++i>=argc) usage ();
2519 if (sscanf (argv[i], "%dx%d",
2520 &fb_width, &fb_height) != 2)
2523 action_requested = True;
2526 if (!strcmp ("--fbmm", argv[i])) {
2527 if (++i>=argc) usage ();
2528 if (sscanf (argv[i], "%dx%d",
2529 &fb_width_mm, &fb_height_mm) != 2)
2532 action_requested = True;
2535 if (!strcmp ("--dpi", argv[i])) {
2537 if (++i>=argc) usage ();
2538 dpi = strtod(argv[i], &strtod_error);
2539 if (argv[i] == strtod_error)
2542 dpi_output = argv[i];
2545 action_requested = True;
2548 if (!strcmp ("--auto", argv[i])) {
2551 output->automatic = True;
2552 output->changes |= changes_automatic;
2557 action_requested = True;
2560 if (!strcmp ("--q12", argv[i]))
2565 if (!strcmp ("--q1", argv[i]))
2570 if (!strcmp ("--newmode", argv[i]))
2572 umode_t *m = malloc (sizeof (umode_t));
2576 if (i + 9 >= argc) usage ();
2577 m->mode.name = argv[i];
2578 m->mode.nameLength = strlen (argv[i]);
2580 clock = check_strtod(argv[i++]);
2581 m->mode.dotClock = clock * 1e6;
2583 m->mode.width = check_strtol(argv[i++]);
2584 m->mode.hSyncStart = check_strtol(argv[i++]);
2585 m->mode.hSyncEnd = check_strtol(argv[i++]);
2586 m->mode.hTotal = check_strtol(argv[i++]);
2587 m->mode.height = check_strtol(argv[i++]);
2588 m->mode.vSyncStart = check_strtol(argv[i++]);
2589 m->mode.vSyncEnd = check_strtol(argv[i++]);
2590 m->mode.vTotal = check_strtol(argv[i++]);
2591 m->mode.modeFlags = 0;
2595 for (f = 0; mode_flags[f].string; f++)
2596 if (!strcasecmp (mode_flags[f].string, argv[i]))
2599 if (!mode_flags[f].string)
2601 m->mode.modeFlags |= mode_flags[f].flag;
2605 m->action = umode_create;
2608 action_requested = True;
2611 if (!strcmp ("--rmmode", argv[i]))
2613 umode_t *m = malloc (sizeof (umode_t));
2615 if (++i>=argc) usage ();
2616 set_name (&m->name, argv[i], name_string|name_xid);
2617 m->action = umode_destroy;
2621 action_requested = True;
2624 if (!strcmp ("--addmode", argv[i]))
2626 umode_t *m = malloc (sizeof (umode_t));
2628 if (++i>=argc) usage ();
2629 set_name (&m->output, argv[i], name_string|name_xid);
2630 if (++i>=argc) usage();
2631 set_name (&m->name, argv[i], name_string|name_xid);
2632 m->action = umode_add;
2636 action_requested = True;
2639 if (!strcmp ("--delmode", argv[i]))
2641 umode_t *m = malloc (sizeof (umode_t));
2643 if (++i>=argc) usage ();
2644 set_name (&m->output, argv[i], name_string|name_xid);
2645 if (++i>=argc) usage();
2646 set_name (&m->name, argv[i], name_string|name_xid);
2647 m->action = umode_delete;
2651 action_requested = True;
2656 if (!action_requested)
2661 if (setit && !setit_1_2)
2665 printf("xrandr program version " VERSION "\n");
2667 dpy = XOpenDisplay (display_name);
2670 fprintf (stderr, "Can't open display %s\n", XDisplayName(display_name));
2674 screen = DefaultScreen (dpy);
2675 if (screen >= ScreenCount (dpy)) {
2676 fprintf (stderr, "Invalid screen number %d (display has %d)\n",
2677 screen, ScreenCount (dpy));
2681 root = RootWindow (dpy, screen);
2683 if (!XRRQueryExtension (dpy, &event_base, &error_base) ||
2684 !XRRQueryVersion (dpy, &major, &minor))
2686 fprintf (stderr, "RandR extension missing\n");
2689 if (major > 1 || (major == 1 && minor >= 2))
2691 if (major > 1 || (major == 1 && minor >= 3))
2694 if (has_1_2 && modeit)
2698 get_screen (current);
2702 for (m = umodes; m; m = m->next)
2707 switch (m->action) {
2709 XRRCreateMode (dpy, root, &m->mode);
2712 e = find_mode (&m->name, 0);
2714 fatal ("cannot find mode \"%s\"\n", m->name.string);
2715 XRRDestroyMode (dpy, e->id);
2718 o = find_output (&m->output);
2720 fatal ("cannot find output \"%s\"\n", m->output.string);
2721 e = find_mode (&m->name, 0);
2723 fatal ("cannot find mode \"%s\"\n", m->name.string);
2724 XRRAddOutputMode (dpy, o->output.xid, e->id);
2727 o = find_output (&m->output);
2729 fatal ("cannot find output \"%s\"\n", m->output.string);
2730 e = find_mode (&m->name, 0);
2732 fatal ("cannot find mode \"%s\"\n", m->name.string);
2733 XRRDeleteOutputMode (dpy, o->output.xid, e->id);
2743 if (has_1_2 && propit)
2746 get_screen (current);
2750 for (output = outputs; output; output = output->next)
2752 output_prop_t *prop;
2754 for (prop = output->props; prop; prop = prop->next)
2756 Atom name = XInternAtom (dpy, prop->name, False);
2759 unsigned char *data;
2762 unsigned long ulong_value;
2763 unsigned char *prop_data;
2765 unsigned long nitems, bytes_after;
2767 XRRPropertyInfo *propinfo;
2769 type = AnyPropertyType;
2771 if (XRRGetOutputProperty (dpy, output->output.xid, name,
2772 0, 100, False, False,
2774 &actual_type, &actual_format,
2775 &nitems, &bytes_after, &prop_data) == Success &&
2777 (propinfo = XRRQueryOutputProperty(dpy, output->output.xid,
2781 format = actual_format;
2784 if ((type == XA_INTEGER || type == AnyPropertyType) &&
2785 (sscanf (prop->value, "%d", &int_value) == 1 ||
2786 sscanf (prop->value, "0x%x", &int_value) == 1))
2789 ulong_value = int_value;
2790 data = (unsigned char *) &ulong_value;
2794 else if ((type == XA_ATOM))
2796 ulong_value = XInternAtom (dpy, prop->value, False);
2797 data = (unsigned char *) &ulong_value;
2800 else if ((type == XA_STRING || type == AnyPropertyType))
2803 data = (unsigned char *) prop->value;
2804 nelements = strlen (prop->value);
2809 XRRChangeOutputProperty (dpy, output->output.xid,
2810 name, type, format, PropModeReplace,
2822 get_screen (current);
2831 * Assign outputs to crtcs
2836 * Mark changing crtcs
2838 mark_changing_crtcs ();
2841 * If an output was specified to track dpi, use it
2845 output_t *output = find_output_by_name (dpi_output);
2846 XRROutputInfo *output_info;
2847 XRRModeInfo *mode_info;
2849 fatal ("Cannot find output %s\n", dpi_output);
2850 output_info = output->output_info;
2851 mode_info = output->mode_info;
2852 if (output_info && mode_info && output_info->mm_height)
2855 * When this output covers the whole screen, just use
2856 * the known physical size
2858 if (fb_width == mode_info->width &&
2859 fb_height == mode_info->height)
2861 fb_width_mm = output_info->mm_width;
2862 fb_height_mm = output_info->mm_height;
2866 dpi = (25.4 * mode_info->height) / output_info->mm_height;
2872 * Compute physical screen size
2874 if (fb_width_mm == 0 || fb_height_mm == 0)
2876 if (fb_width != DisplayWidth (dpy, screen) ||
2877 fb_height != DisplayHeight (dpy, screen) || dpi != 0.0)
2880 dpi = (25.4 * DisplayHeight (dpy, screen)) / DisplayHeightMM(dpy, screen);
2882 fb_width_mm = (25.4 * fb_width) / dpi;
2883 fb_height_mm = (25.4 * fb_height) / dpi;
2887 fb_width_mm = DisplayWidthMM (dpy, screen);
2888 fb_height_mm = DisplayHeightMM (dpy, screen);
2898 * Set gamma on crtc's that belong to the outputs.
2903 * Now apply all of the changes
2910 if (query_1_2 || (query && has_1_2 && !query_1))
2915 #define ModeShown 0x80000000
2917 get_screen (current);
2921 printf ("Screen %d: minimum %d x %d, current %d x %d, maximum %d x %d\n",
2922 screen, minWidth, minHeight,
2923 DisplayWidth (dpy, screen), DisplayHeight(dpy, screen),
2924 maxWidth, maxHeight);
2926 for (output = outputs; output; output = output->next)
2928 XRROutputInfo *output_info = output->output_info;
2929 crtc_t *crtc = output->crtc_info;
2930 XRRCrtcInfo *crtc_info = crtc ? crtc->crtc_info : NULL;
2931 XRRModeInfo *mode = output->mode_info;
2935 Rotation rotations = output_rotations (output);
2937 printf ("%s %s", output_info->name, connection[output_info->connection]);
2941 printf (" %dx%d+%d+%d",
2942 crtc_info->width, crtc_info->height,
2943 crtc_info->x, crtc_info->y);
2945 printf (" %dx%d+%d+%d",
2946 mode->width, mode->height, output->x, output->y);
2949 printf (" (0x%x)", (int)mode->id);
2950 if (output->rotation != RR_Rotate_0 || verbose)
2953 rotation_name (output->rotation));
2954 if (output->rotation & (RR_Reflect_X|RR_Reflect_Y))
2955 printf (" %s", reflection_name (output->rotation));
2958 if (rotations != RR_Rotate_0 || verbose)
2962 for (i = 0; i < 4; i ++) {
2963 if ((rotations >> i) & 1) {
2964 if (!first) printf (" "); first = False;
2965 printf("%s", direction[i]);
2969 if (rotations & RR_Reflect_X)
2971 if (!first) printf (" "); first = False;
2974 if (rotations & RR_Reflect_Y)
2976 if (!first) printf (" "); first = False;
2984 printf (" %dmm x %dmm",
2985 (int)output_info->mm_width, (int)output_info->mm_height);
2988 if (crtc && crtc->panning_info && crtc->panning_info->width > 0)
2990 XRRPanning *pan = crtc->panning_info;
2991 printf (" panning %dx%d+%d+%d",
2992 pan->width, pan->height, pan->left, pan->top);
2993 if ((pan->track_width != 0 &&
2994 (pan->track_left != pan->left ||
2995 pan->track_width != pan->width ||
2996 pan->border_left != 0 ||
2997 pan->border_right != 0)) ||
2998 (pan->track_height != 0 &&
2999 (pan->track_top != pan->top ||
3000 pan->track_height != pan->height ||
3001 pan->border_top != 0 ||
3002 pan->border_bottom != 0)))
3003 printf (" tracking %dx%d+%d+%d border %d/%d/%d/%d",
3004 pan->track_width, pan->track_height,
3005 pan->track_left, pan->track_top,
3006 pan->border_left, pan->border_top,
3007 pan->border_right, pan->border_bottom);
3013 printf ("\tIdentifier: 0x%x\n", (int)output->output.xid);
3014 printf ("\tTimestamp: %d\n", (int)output_info->timestamp);
3015 printf ("\tSubpixel: %s\n", order[output_info->subpixel_order]);
3016 if (output->gamma.red != 0.0 && output->gamma.green != 0.0 && output->gamma.blue != 0.0) {
3017 printf ("\tGamma: %#.2g:%#.2g:%#.2g\n",
3018 output->gamma.red, output->gamma.green, output->gamma.blue);
3019 printf ("\tBrightness: %#.2g\n", output->brightness);
3021 printf ("\tClones: ");
3022 for (j = 0; j < output_info->nclone; j++)
3024 output_t *clone = find_output_by_xid (output_info->clones[j]);
3026 if (clone) printf (" %s", clone->output.string);
3029 if (output->crtc_info)
3030 printf ("\tCRTC: %d\n", output->crtc_info->crtc.index);
3031 printf ("\tCRTCs: ");
3032 for (j = 0; j < output_info->ncrtc; j++)
3034 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[j]);
3036 printf (" %d", crtc->crtc.index);
3039 if (output->crtc_info && output->crtc_info->panning_info) {
3040 XRRPanning *pan = output->crtc_info->panning_info;
3041 printf ("\tPanning: %dx%d+%d+%d\n",
3042 pan->width, pan->height, pan->left, pan->top);
3043 printf ("\tTracking: %dx%d+%d+%d\n",
3044 pan->track_width, pan->track_height,
3045 pan->track_left, pan->track_top);
3046 printf ("\tBorder: %d/%d/%d/%d\n",
3047 pan->border_left, pan->border_top,
3048 pan->border_right, pan->border_bottom);
3055 printf ("\tTransform: ");
3056 for (y = 0; y < 3; y++)
3058 for (x = 0; x < 3; x++)
3059 printf (" %f", XFixedToDouble (output->transform.transform.matrix[y][x]));
3063 if (output->transform.filter)
3064 printf ("\n\t filter: %s", output->transform.filter);
3067 if (verbose || properties)
3069 props = XRRListOutputProperties (dpy, output->output.xid,
3071 for (j = 0; j < nprop; j++) {
3072 unsigned char *prop;
3074 unsigned long nitems, bytes_after;
3076 XRRPropertyInfo *propinfo;
3078 XRRGetOutputProperty (dpy, output->output.xid, props[j],
3079 0, 100, False, False,
3081 &actual_type, &actual_format,
3082 &nitems, &bytes_after, &prop);
3084 propinfo = XRRQueryOutputProperty(dpy, output->output.xid,
3087 if (actual_type == XA_INTEGER && actual_format == 8) {
3090 printf("\t%s:\n", XGetAtomName (dpy, props[j]));
3091 for (k = 0; k < nitems; k++) {
3094 printf("%02x", (unsigned char)prop[k]);
3098 } else if (actual_type == XA_INTEGER &&
3099 actual_format == 32)
3101 printf("\t%s: ", XGetAtomName (dpy, props[j]));
3102 for (k = 0; k < nitems; k++) {
3104 printf ("\n\t\t\t");
3105 printf("%d (0x%08x)",
3106 (int)((INT32 *)prop)[k], (int)((INT32 *)prop)[k]);
3109 if (propinfo->range && propinfo->num_values > 0) {
3112 printf("\trange%s: ",
3113 (propinfo->num_values == 2) ? "" : "s");
3115 for (k = 0; k < propinfo->num_values / 2; k++)
3116 printf(" (%d,%d)", (int)propinfo->values[k * 2],
3117 (int)propinfo->values[k * 2 + 1]);
3121 } else if (actual_type == XA_ATOM &&
3122 actual_format == 32)
3124 printf("\t%s:", XGetAtomName (dpy, props[j]));
3125 for (k = 0; k < nitems; k++) {
3126 if (k > 0 && (k & 1) == 0)
3128 printf("\t%s", XGetAtomName (dpy, ((Atom *)prop)[k]));
3131 if (!propinfo->range && propinfo->num_values > 0) {
3132 printf("\n\t\tsupported:");
3134 for (k = 0; k < propinfo->num_values; k++)
3136 printf(" %-12.12s", XGetAtomName (dpy,
3137 propinfo->values[k]));
3138 if (k % 4 == 3 && k < propinfo->num_values - 1)
3143 } else if (actual_format == 8) {
3144 printf ("\t%s: %s%s\n", XGetAtomName (dpy, props[j]),
3145 prop, bytes_after ? "..." : "");
3147 char *type = actual_type ? XGetAtomName (dpy, actual_type) : "none";
3148 printf ("\t%s: %s(%d) (format %d items %d) ????\n",
3149 XGetAtomName (dpy, props[j]),
3150 type, (int)actual_type, actual_format, (int)nitems);
3159 for (j = 0; j < output_info->nmode; j++)
3161 XRRModeInfo *mode = find_mode_by_xid (output_info->modes[j]);
3164 printf (" %s (0x%x) %6.1fMHz",
3165 mode->name, (int)mode->id,
3166 (double)mode->dotClock / 1000000.0);
3167 for (f = 0; mode_flags[f].flag; f++)
3168 if (mode->modeFlags & mode_flags[f].flag)
3169 printf (" %s", mode_flags[f].string);
3170 if (mode == output->mode_info)
3171 printf (" *current");
3172 if (j < output_info->npreferred)
3173 printf (" +preferred");
3175 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
3176 mode->width, mode->hSyncStart, mode->hSyncEnd,
3177 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
3178 printf (" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
3179 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
3180 mode_refresh (mode));
3181 mode->modeFlags |= ModeShown;
3186 mode_shown = calloc (output_info->nmode, sizeof (Bool));
3187 if (!mode_shown) fatal ("out of memory\n");
3188 for (j = 0; j < output_info->nmode; j++)
3190 XRRModeInfo *jmode, *kmode;
3192 if (mode_shown[j]) continue;
3194 jmode = find_mode_by_xid (output_info->modes[j]);
3196 printf (" %-12s", jmode->name);
3197 for (k = j; k < output_info->nmode; k++)
3199 if (mode_shown[k]) continue;
3200 kmode = find_mode_by_xid (output_info->modes[k]);
3201 if (strcmp (jmode->name, kmode->name) != 0) continue;
3202 mode_shown[k] = True;
3203 kmode->modeFlags |= ModeShown;
3204 printf (" %6.1f", mode_refresh (kmode));
3205 if (kmode == output->mode_info)
3209 if (k < output_info->npreferred)
3219 for (m = 0; m < res->nmode; m++)
3221 XRRModeInfo *mode = &res->modes[m];
3223 if (!(mode->modeFlags & ModeShown))
3225 printf (" %s (0x%x) %6.1fMHz\n",
3226 mode->name, (int)mode->id,
3227 (double)mode->dotClock / 1000000.0);
3228 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
3229 mode->width, mode->hSyncStart, mode->hSyncEnd,
3230 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
3231 printf (" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
3232 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
3233 mode_refresh (mode));
3239 sc = XRRGetScreenInfo (dpy, root);
3244 current_size = XRRConfigCurrentConfiguration (sc, ¤t_rotation);
3246 sizes = XRRConfigSizes(sc, &nsize);
3248 if (have_pixel_size) {
3249 for (size = 0; size < nsize; size++)
3251 if (sizes[size].width == width && sizes[size].height == height)
3254 if (size >= nsize) {
3256 "Size %dx%d not found in available modes\n", width, height);
3261 size = current_size;
3262 else if (size >= nsize) {
3264 "Size index %d is too large, there are only %d sizes\n",
3271 for (rot = 0; rot < 4; rot++)
3272 if (1 << rot == (current_rotation & 0xf))
3276 current_rate = XRRConfigCurrentRate (sc);
3280 if (size == current_size)
3281 rate = current_rate;
3287 rates = XRRConfigRates (sc, size, &nrate);
3288 for (i = 0; i < nrate; i++)
3289 if (rate == rates[i])
3292 fprintf (stderr, "Rate %.1f Hz not available for this size\n", rate);
3298 int major_version, minor_version;
3299 XRRQueryVersion (dpy, &major_version, &minor_version);
3300 printf("Server reports RandR version %d.%d\n",
3301 major_version, minor_version);
3304 if (query || query_1) {
3305 printf(" SZ: Pixels Physical Refresh\n");
3306 for (i = 0; i < nsize; i++) {
3307 printf ("%c%-2d %5d x %-5d (%4dmm x%4dmm )",
3308 i == current_size ? '*' : ' ',
3309 i, sizes[i].width, sizes[i].height,
3310 sizes[i].mwidth, sizes[i].mheight);
3311 rates = XRRConfigRates (sc, i, &nrate);
3312 if (nrate) printf (" ");
3313 for (j = 0; j < nrate; j++)
3315 i == current_size && rates[j] == current_rate ? '*' : ' ',
3321 rotations = XRRConfigRotations(sc, ¤t_rotation);
3323 rotation = 1 << rot ;
3325 printf("Current rotation - %s\n",
3326 rotation_name (current_rotation));
3328 printf("Current reflection - %s\n",
3329 reflection_name (current_rotation));
3331 printf ("Rotations possible - ");
3332 for (i = 0; i < 4; i ++) {
3333 if ((rotations >> i) & 1) printf("%s ", direction[i]);
3337 printf ("Reflections possible - ");
3338 if (rotations & (RR_Reflect_X|RR_Reflect_Y))
3340 if (rotations & RR_Reflect_X) printf ("X Axis ");
3341 if (rotations & RR_Reflect_Y) printf ("Y Axis");
3349 printf("Setting size to %d, rotation to %s\n", size, direction[rot]);
3351 printf ("Setting reflection on ");
3354 if (reflection & RR_Reflect_X) printf ("X Axis ");
3355 if (reflection & RR_Reflect_Y) printf ("Y Axis");
3358 printf ("neither axis");
3361 if (reflection & RR_Reflect_X) printf("Setting reflection on X axis\n");
3363 if (reflection & RR_Reflect_Y) printf("Setting reflection on Y axis\n");
3366 /* we should test configureNotify on the root window */
3367 XSelectInput (dpy, root, StructureNotifyMask);
3369 if (setit && !dryrun) XRRSelectInput (dpy, root,
3370 RRScreenChangeNotifyMask);
3371 if (setit && !dryrun) status = XRRSetScreenConfigAndRate (dpy, sc,
3373 (SizeID) size, (Rotation) (rotation | reflection), rate, CurrentTime);
3375 if (setit && !dryrun && status == RRSetConfigFailed) {
3376 printf ("Failed to change the screen configuration!\n");
3380 if (verbose && setit && !dryrun && size != current_size) {
3381 if (status == RRSetConfigSuccess)
3383 Bool seen_screen = False;
3384 while (!seen_screen) {
3386 XNextEvent(dpy, (XEvent *) &event);
3388 printf ("Event received, type = %d\n", event.type);
3389 /* update Xlib's knowledge of the event */
3390 XRRUpdateConfiguration (&event);
3391 if (event.type == ConfigureNotify)
3392 printf("Received ConfigureNotify Event!\n");
3394 switch (event.type - event_base) {
3395 case RRScreenChangeNotify:
3396 sce = (XRRScreenChangeNotifyEvent *) &event;
3398 printf("Got a screen change notify event!\n");
3399 printf(" window = %d\n root = %d\n size_index = %d\n rotation %d\n",
3400 (int) sce->window, (int) sce->root,
3401 sce->size_index, sce->rotation);
3402 printf(" timestamp = %ld, config_timestamp = %ld\n",
3403 sce->timestamp, sce->config_timestamp);
3404 printf(" Rotation = %x\n", sce->rotation);
3405 printf(" %d X %d pixels, %d X %d mm\n",
3406 sce->width, sce->height, sce->mwidth, sce->mheight);
3407 printf("Display width %d, height %d\n",
3408 DisplayWidth(dpy, screen), DisplayHeight(dpy, screen));
3409 printf("Display widthmm %d, heightmm %d\n",
3410 DisplayWidthMM(dpy, screen), DisplayHeightMM(dpy, screen));
3411 spo = sce->subpixel_order;
3412 if ((spo < 0) || (spo > 5))
3413 printf ("Unknown subpixel order, value = %d\n", spo);
3414 else printf ("new Subpixel rendering model is %s\n", order[spo]);
3418 if (event.type != ConfigureNotify)
3419 printf("unknown event received, type = %d!\n", event.type);
3424 XRRFreeScreenConfigInfo(sc);