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 */
43 static char *program_name;
46 static int screen = -1;
47 static Bool verbose = False;
48 static Bool automatic = False;
49 static Bool properties = False;
50 static Bool grab_server = True;
51 static Bool no_primary = False;
53 static char *direction[5] = {
60 static char *reflections[5] = {
68 static char *order[6] = {
80 { "+HSync", RR_HSyncPositive },
81 { "-HSync", RR_HSyncNegative },
82 { "+VSync", RR_VSyncPositive },
83 { "-VSync", RR_VSyncNegative },
84 { "Interlace", RR_Interlace },
85 { "DoubleScan", RR_DoubleScan },
86 { "CSync", RR_CSync },
87 { "+CSync", RR_CSyncPositive },
88 { "-CSync", RR_CSyncNegative },
92 static void _X_NORETURN
95 fprintf(stderr, "usage: %s [options]\n", program_name);
96 fprintf(stderr, " where options are:\n");
97 fprintf(stderr, " -display <display> or -d <display>\n");
98 fprintf(stderr, " -help\n");
99 fprintf(stderr, " -o <normal,inverted,left,right,0,1,2,3>\n");
100 fprintf(stderr, " or --orientation <normal,inverted,left,right,0,1,2,3>\n");
101 fprintf(stderr, " -q or --query\n");
102 fprintf(stderr, " -s <size>/<width>x<height> or --size <size>/<width>x<height>\n");
103 fprintf(stderr, " -r <rate> or --rate <rate> or --refresh <rate>\n");
104 fprintf(stderr, " -v or --version\n");
105 fprintf(stderr, " -x (reflect in x)\n");
106 fprintf(stderr, " -y (reflect in y)\n");
107 fprintf(stderr, " --screen <screen>\n");
108 fprintf(stderr, " --verbose\n");
109 fprintf(stderr, " --current\n");
110 fprintf(stderr, " --dryrun\n");
111 fprintf(stderr, " --nograb\n");
112 fprintf(stderr, " --prop or --properties\n");
113 fprintf(stderr, " --fb <width>x<height>\n");
114 fprintf(stderr, " --fbmm <width>x<height>\n");
115 fprintf(stderr, " --dpi <dpi>/<output>\n");
116 fprintf(stderr, " --output <output>\n");
117 fprintf(stderr, " --auto\n");
118 fprintf(stderr, " --mode <mode>\n");
119 fprintf(stderr, " --preferred\n");
120 fprintf(stderr, " --pos <x>x<y>\n");
121 fprintf(stderr, " --rate <rate> or --refresh <rate>\n");
122 fprintf(stderr, " --reflect normal,x,y,xy\n");
123 fprintf(stderr, " --rotate normal,inverted,left,right\n");
124 fprintf(stderr, " --left-of <output>\n");
125 fprintf(stderr, " --right-of <output>\n");
126 fprintf(stderr, " --above <output>\n");
127 fprintf(stderr, " --below <output>\n");
128 fprintf(stderr, " --same-as <output>\n");
129 fprintf(stderr, " --set <property> <value>\n");
130 fprintf(stderr, " --scale <x>x<y>\n");
131 fprintf(stderr, " --transform <a>,<b>,<c>,<d>,<e>,<f>,<g>,<h>,<i>\n");
132 fprintf(stderr, " --off\n");
133 fprintf(stderr, " --crtc <crtc>\n");
134 fprintf(stderr, " --panning <w>x<h>[+<x>+<y>[/<track:w>x<h>+<x>+<y>[/<border:l>/<t>/<r>/<b>]]]\n");
135 fprintf(stderr, " --gamma <r>:<g>:<b>\n");
136 fprintf(stderr, " --primary\n");
137 fprintf(stderr, " --noprimary\n");
138 fprintf(stderr, " --newmode <name> <clock MHz>\n");
139 fprintf(stderr, " <hdisp> <hsync-start> <hsync-end> <htotal>\n");
140 fprintf(stderr, " <vdisp> <vsync-start> <vsync-end> <vtotal>\n");
141 fprintf(stderr, " [flags...]\n");
142 fprintf(stderr, " Valid flags: +HSync -HSync +VSync -VSync\n");
143 fprintf(stderr, " +CSync -CSync CSync Interlace DoubleScan\n");
144 fprintf(stderr, " --rmmode <name>\n");
145 fprintf(stderr, " --addmode <output> <name>\n");
146 fprintf(stderr, " --delmode <output> <name>\n");
152 static void _X_NORETURN
153 fatal (const char *format, ...)
157 va_start (ap, format);
158 fprintf (stderr, "%s: ", program_name);
159 vfprintf (stderr, format, ap);
166 warning (const char *format, ...)
170 va_start (ap, format);
171 fprintf (stderr, "%s: ", program_name);
172 vfprintf (stderr, format, ap);
176 /* Because fmin requires C99 suppport */
177 static inline double dmin (double x, double y)
179 return x < y ? x : y;
183 rotation_name (Rotation rotation)
187 if ((rotation & 0xf) == 0)
189 for (i = 0; i < 4; i++)
190 if (rotation & (1 << i))
192 return "invalid rotation";
196 reflection_name (Rotation rotation)
198 rotation &= (RR_Reflect_X|RR_Reflect_Y);
206 case RR_Reflect_X|RR_Reflect_Y:
207 return "X and Y axis";
209 return "invalid reflection";
212 typedef enum _relation {
221 int x, y, width, height;
232 typedef enum _changes {
234 changes_crtc = (1 << 0),
235 changes_mode = (1 << 1),
236 changes_relation = (1 << 2),
237 changes_position = (1 << 3),
238 changes_rotation = (1 << 4),
239 changes_reflection = (1 << 5),
240 changes_automatic = (1 << 6),
241 changes_refresh = (1 << 7),
242 changes_property = (1 << 8),
243 changes_transform = (1 << 9),
244 changes_panning = (1 << 10),
245 changes_gamma = (1 << 11),
246 changes_primary = (1 << 12),
249 typedef enum _name_kind {
251 name_string = (1 << 0),
253 name_index = (1 << 2),
254 name_preferred = (1 << 3),
264 typedef struct _crtc crtc_t;
265 typedef struct _output output_t;
266 typedef struct _transform transform_t;
267 typedef struct _umode umode_t;
268 typedef struct _output_prop output_prop_t;
271 XTransform transform;
280 XRRCrtcInfo *crtc_info;
282 XRRModeInfo *mode_info;
283 XRRPanning *panning_info;
289 transform_t current_transform, pending_transform;
292 struct _output_prop {
293 struct _output_prop *next;
299 struct _output *next;
303 output_prop_t *props;
306 XRROutputInfo *output_info;
310 crtc_t *current_crtc_info;
314 XRRModeInfo *mode_info;
327 transform_t transform;
342 typedef enum _umode_action {
343 umode_create, umode_destroy, umode_add, umode_delete
350 umode_action_t action;
356 static char *connection[3] = {
359 "unknown connection"};
361 #define OUTPUT_NAME 1
365 #define CRTC_INDEX 0x40000000
374 static output_t *outputs = NULL;
375 static output_t **outputs_tail = &outputs;
376 static crtc_t *crtcs;
377 static umode_t *umodes;
378 static int num_crtcs;
379 static XRRScreenResources *res;
380 static int fb_width = 0, fb_height = 0;
381 static int fb_width_mm = 0, fb_height_mm = 0;
382 static double dpi = 0;
383 static char *dpi_output = NULL;
384 static Bool dryrun = False;
385 static int minWidth, maxWidth, minHeight, maxHeight;
386 static Bool has_1_2 = False;
387 static Bool has_1_3 = False;
390 mode_height (XRRModeInfo *mode_info, Rotation rotation)
392 switch (rotation & 0xf) {
395 return mode_info->height;
398 return mode_info->width;
405 mode_width (XRRModeInfo *mode_info, Rotation rotation)
407 switch (rotation & 0xf) {
410 return mode_info->width;
413 return mode_info->height;
420 transform_point (XTransform *transform, double *xp, double *yp)
430 for (j = 0; j < 3; j++)
433 for (i = 0; i < 3; i++)
434 v += (XFixedToDouble (transform->matrix[j][i]) * vector[i]);
439 for (j = 0; j < 2; j++) {
440 vector[j] = result[j] / result[2];
441 if (vector[j] > 32767 || vector[j] < -32767)
450 path_bounds (XTransform *transform, point_t *points, int npoints, box_t *box)
455 for (i = 0; i < npoints; i++) {
459 transform_point (transform, &x, &y);
460 point.x1 = floor (x);
461 point.y1 = floor (y);
467 if (point.x1 < box->x1) box->x1 = point.x1;
468 if (point.y1 < box->y1) box->y1 = point.y1;
469 if (point.x2 > box->x2) box->x2 = point.x2;
470 if (point.y2 > box->y2) box->y2 = point.y2;
476 mode_geometry (XRRModeInfo *mode_info, Rotation rotation,
477 XTransform *transform,
481 int width = mode_width (mode_info, rotation);
482 int height = mode_height (mode_info, rotation);
492 path_bounds (transform, rect, 4, bounds);
495 /* v refresh frequency in Hz */
497 mode_refresh (XRRModeInfo *mode_info)
501 if (mode_info->hTotal && mode_info->vTotal)
502 rate = ((double) mode_info->dotClock /
503 ((double) mode_info->hTotal * (double) mode_info->vTotal));
509 /* h sync frequency in Hz */
511 mode_hsync (XRRModeInfo *mode_info)
515 if (mode_info->hTotal)
516 rate = (double) mode_info->dotClock / (double) mode_info->hTotal;
523 init_name (name_t *name)
525 name->kind = name_none;
529 set_name_string (name_t *name, char *string)
531 name->kind |= name_string;
532 name->string = string;
536 set_name_xid (name_t *name, XID xid)
538 name->kind |= name_xid;
543 set_name_index (name_t *name, int index)
545 name->kind |= name_index;
550 set_name_preferred (name_t *name)
552 name->kind |= name_preferred;
556 set_name_all (name_t *name, name_t *old)
558 if (old->kind & name_xid)
559 name->xid = old->xid;
560 if (old->kind & name_string)
561 name->string = old->string;
562 if (old->kind & name_index)
563 name->index = old->index;
564 name->kind |= old->kind;
568 set_name (name_t *name, char *string, name_kind_t valid)
570 unsigned int xid; /* don't make it XID (which is unsigned long):
571 scanf() takes unsigned int */
574 if ((valid & name_xid) && sscanf (string, "0x%x", &xid) == 1)
575 set_name_xid (name, xid);
576 else if ((valid & name_index) && sscanf (string, "%d", &index) == 1)
577 set_name_index (name, index);
578 else if (valid & name_string)
579 set_name_string (name, string);
585 init_transform (transform_t *transform)
588 memset (&transform->transform, '\0', sizeof (transform->transform));
589 for (x = 0; x < 3; x++)
590 transform->transform.matrix[x][x] = XDoubleToFixed (1.0);
591 transform->filter = "";
592 transform->nparams = 0;
593 transform->params = NULL;
597 set_transform (transform_t *dest,
598 XTransform *transform,
603 dest->transform = *transform;
604 dest->filter = strdup (filter);
605 dest->nparams = nparams;
606 dest->params = malloc (nparams * sizeof (XFixed));
607 memcpy (dest->params, params, nparams * sizeof (XFixed));
611 copy_transform (transform_t *dest, transform_t *src)
613 set_transform (dest, &src->transform,
614 src->filter, src->params, src->nparams);
618 equal_transform (transform_t *a, transform_t *b)
620 if (memcmp (&a->transform, &b->transform, sizeof (XTransform)) != 0)
622 if (strcmp (a->filter, b->filter) != 0)
624 if (a->nparams != b->nparams)
626 if (memcmp (a->params, b->params, a->nparams * sizeof (XFixed)) != 0)
634 output_t *output = calloc (1, sizeof (output_t));
637 fatal ("out of memory\n");
639 output->found = False;
640 output->brightness = 1.0;
641 *outputs_tail = output;
642 outputs_tail = &output->next;
647 find_output (name_t *name)
651 for (output = outputs; output; output = output->next)
653 name_kind_t common = name->kind & output->output.kind;
655 if ((common & name_xid) && name->xid == output->output.xid)
657 if ((common & name_string) && !strcmp (name->string, output->output.string))
659 if ((common & name_index) && name->index == output->output.index)
666 find_output_by_xid (RROutput output)
670 init_name (&output_name);
671 set_name_xid (&output_name, output);
672 return find_output (&output_name);
676 find_output_by_name (char *name)
680 init_name (&output_name);
681 set_name_string (&output_name, name);
682 return find_output (&output_name);
686 find_crtc (name_t *name)
691 for (c = 0; c < num_crtcs; c++)
696 common = name->kind & crtc->crtc.kind;
698 if ((common & name_xid) && name->xid == crtc->crtc.xid)
700 if ((common & name_string) && !strcmp (name->string, crtc->crtc.string))
702 if ((common & name_index) && name->index == crtc->crtc.index)
710 find_crtc_by_xid (RRCrtc crtc)
714 init_name (&crtc_name);
715 set_name_xid (&crtc_name, crtc);
716 return find_crtc (&crtc_name);
720 find_mode (name_t *name, double refresh)
723 XRRModeInfo *best = NULL;
726 for (m = 0; m < res->nmode; m++)
728 XRRModeInfo *mode = &res->modes[m];
729 if ((name->kind & name_xid) && name->xid == mode->id)
734 if ((name->kind & name_string) && !strcmp (name->string, mode->name))
739 dist = fabs (mode_refresh (mode) - refresh);
742 if (!best || dist < bestDist)
753 find_mode_by_xid (RRMode mode)
757 init_name (&mode_name);
758 set_name_xid (&mode_name, mode);
759 return find_mode (&mode_name, 0);
764 find_mode_by_name (char *name)
767 init_name (&mode_name);
768 set_name_string (&mode_name, name);
769 return find_mode (&mode_name, 0);
775 find_mode_for_output (output_t *output, name_t *name)
777 XRROutputInfo *output_info = output->output_info;
779 XRRModeInfo *best = NULL;
782 for (m = 0; m < output_info->nmode; m++)
786 mode = find_mode_by_xid (output_info->modes[m]);
788 if ((name->kind & name_xid) && name->xid == mode->id)
793 if ((name->kind & name_string) && !strcmp (name->string, mode->name))
797 /* Stay away from doublescan modes unless refresh rate is specified. */
798 if (!output->refresh && (mode->modeFlags & RR_DoubleScan))
802 dist = fabs (mode_refresh (mode) - output->refresh);
805 if (!best || dist < bestDist)
816 preferred_mode (output_t *output)
818 XRROutputInfo *output_info = output->output_info;
825 for (m = 0; m < output_info->nmode; m++)
827 XRRModeInfo *mode_info = find_mode_by_xid (output_info->modes[m]);
830 if (m < output_info->npreferred)
832 else if (output_info->mm_height)
833 dist = (1000 * DisplayHeight(dpy, screen) / DisplayHeightMM(dpy, screen) -
834 1000 * mode_info->height / output_info->mm_height);
836 dist = DisplayHeight(dpy, screen) - mode_info->height;
838 if (dist < 0) dist = -dist;
839 if (!best || dist < bestDist)
849 output_can_use_crtc (output_t *output, crtc_t *crtc)
851 XRROutputInfo *output_info = output->output_info;
854 for (c = 0; c < output_info->ncrtc; c++)
855 if (output_info->crtcs[c] == crtc->crtc.xid)
861 output_can_use_mode (output_t *output, XRRModeInfo *mode)
863 XRROutputInfo *output_info = output->output_info;
866 for (m = 0; m < output_info->nmode; m++)
867 if (output_info->modes[m] == mode->id)
873 crtc_can_use_rotation (crtc_t *crtc, Rotation rotation)
875 Rotation rotations = crtc->crtc_info->rotations;
876 Rotation dir = rotation & (RR_Rotate_0|RR_Rotate_90|RR_Rotate_180|RR_Rotate_270);
877 Rotation reflect = rotation & (RR_Reflect_X|RR_Reflect_Y);
878 if (((rotations & dir) != 0) && ((rotations & reflect) == reflect))
885 crtc_can_use_transform (crtc_t *crtc, XTransform *transform)
889 XRRQueryVersion (dpy, &major, &minor);
890 if (major > 1 || (major == 1 && minor >= 3))
897 * Report only rotations that are supported by all crtcs
900 output_rotations (output_t *output)
903 Rotation rotation = RR_Rotate_0;
904 XRROutputInfo *output_info = output->output_info;
907 for (c = 0; c < output_info->ncrtc; c++)
909 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
913 rotation = crtc->crtc_info->rotations;
916 rotation &= crtc->crtc_info->rotations;
923 output_can_use_rotation (output_t *output, Rotation rotation)
925 XRROutputInfo *output_info = output->output_info;
928 /* make sure all of the crtcs can use this rotation.
929 * yes, this is not strictly necessary, but it is
930 * simpler,and we expect most drivers to either
931 * support rotation everywhere or nowhere
933 for (c = 0; c < output_info->ncrtc; c++)
935 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
936 if (crtc && !crtc_can_use_rotation (crtc, rotation))
943 output_is_primary(output_t *output)
946 return XRRGetOutputPrimary(dpy, root) == output->output.xid;
950 /* Returns the index of the last value in an array < 0xffff */
952 find_last_non_clamped(CARD16 array[], int size) {
954 for (i = size - 1; i > 0; i--) {
955 if (array[i] < 0xffff)
962 set_gamma_info(output_t *output)
965 double i1, v1, i2, v2;
966 int size, middle, last_best, last_red, last_green, last_blue;
969 if (!output->crtc_info)
972 size = XRRGetCrtcGammaSize(dpy, output->crtc_info->crtc.xid);
974 warning("Failed to get size of gamma for output %s\n", output->output.string);
978 gamma = XRRGetCrtcGamma(dpy, output->crtc_info->crtc.xid);
980 warning("Failed to get gamma for output %s\n", output->output.string);
985 * Here is a bit tricky because gamma is a whole curve for each
986 * color. So, typically, we need to represent 3 * 256 values as 3 + 1
987 * values. Therefore, we approximate the gamma curve (v) by supposing
988 * it always follows the way we set it: a power function (i^g)
989 * multiplied by a brightness (b).
991 * so g = (ln(v) - ln(b))/ln(i)
992 * and b can be found using two points (v1,i1) and (v2, i2):
993 * b = e^((ln(v2)*ln(i1) - ln(v1)*ln(i2))/ln(i1/i2))
994 * For the best resolution, we select i2 at the highest place not
995 * clamped and i1 at i2/2. Note that if i2 = 1 (as in most normal
996 * cases), then b = v2.
998 last_red = find_last_non_clamped(gamma->red, size);
999 last_green = find_last_non_clamped(gamma->green, size);
1000 last_blue = find_last_non_clamped(gamma->blue, size);
1001 best_array = gamma->red;
1002 last_best = last_red;
1003 if (last_green > last_best) {
1004 last_best = last_green;
1005 best_array = gamma->green;
1007 if (last_blue > last_best) {
1008 last_best = last_blue;
1009 best_array = gamma->blue;
1014 middle = last_best / 2;
1015 i1 = (double)(middle + 1) / size;
1016 v1 = (double)(best_array[middle]) / 65535;
1017 i2 = (double)(last_best + 1) / size;
1018 v2 = (double)(best_array[last_best]) / 65535;
1019 if (v2 < 0.0001) { /* The screen is black */
1020 output->brightness = 0;
1021 output->gamma.red = 1;
1022 output->gamma.green = 1;
1023 output->gamma.blue = 1;
1025 if ((last_best + 1) == size)
1026 output->brightness = v2;
1028 output->brightness = exp((log(v2)*log(i1) - log(v1)*log(i2))/log(i1/i2));
1029 output->gamma.red = log((double)(gamma->red[last_red / 2]) / output->brightness
1030 / 65535) / log((double)((last_red / 2) + 1) / size);
1031 output->gamma.green = log((double)(gamma->green[last_green / 2]) / output->brightness
1032 / 65535) / log((double)((last_green / 2) + 1) / size);
1033 output->gamma.blue = log((double)(gamma->blue[last_blue / 2]) / output->brightness
1034 / 65535) / log((double)((last_blue / 2) + 1) / size);
1037 XRRFreeGamma(gamma);
1041 set_output_info (output_t *output, RROutput xid, XRROutputInfo *output_info)
1043 /* sanity check output info */
1044 if (output_info->connection != RR_Disconnected && !output_info->nmode)
1045 warning ("Output %s is not disconnected but has no modes\n",
1048 /* set output name and info */
1049 if (!(output->output.kind & name_xid))
1050 set_name_xid (&output->output, xid);
1051 if (!(output->output.kind & name_string))
1052 set_name_string (&output->output, output_info->name);
1053 output->output_info = output_info;
1055 /* set crtc name and info */
1056 if (!(output->changes & changes_crtc))
1057 set_name_xid (&output->crtc, output_info->crtc);
1059 if (output->crtc.kind == name_xid && output->crtc.xid == None)
1060 output->crtc_info = NULL;
1063 output->crtc_info = find_crtc (&output->crtc);
1064 if (!output->crtc_info)
1066 if (output->crtc.kind & name_xid)
1067 fatal ("cannot find crtc 0x%x\n", output->crtc.xid);
1068 if (output->crtc.kind & name_index)
1069 fatal ("cannot find crtc %d\n", output->crtc.index);
1071 if (!output_can_use_crtc (output, output->crtc_info))
1072 fatal ("output %s cannot use crtc 0x%x\n", output->output.string,
1073 output->crtc_info->crtc.xid);
1076 /* set mode name and info */
1077 if (!(output->changes & changes_mode))
1079 crtc_t *crtc = NULL;
1081 if (output_info->crtc)
1082 crtc = find_crtc_by_xid(output_info->crtc);
1083 if (crtc && crtc->crtc_info)
1084 set_name_xid (&output->mode, crtc->crtc_info->mode);
1085 else if (output->crtc_info)
1086 set_name_xid (&output->mode, output->crtc_info->crtc_info->mode);
1088 set_name_xid (&output->mode, None);
1089 if (output->mode.xid)
1091 output->mode_info = find_mode_by_xid (output->mode.xid);
1092 if (!output->mode_info)
1093 fatal ("server did not report mode 0x%x for output %s\n",
1094 output->mode.xid, output->output.string);
1097 output->mode_info = NULL;
1099 else if (output->mode.kind == name_xid && output->mode.xid == None)
1100 output->mode_info = NULL;
1103 if (output->mode.kind == name_preferred)
1104 output->mode_info = preferred_mode (output);
1106 output->mode_info = find_mode_for_output (output, &output->mode);
1107 if (!output->mode_info)
1109 if (output->mode.kind & name_preferred)
1110 fatal ("cannot find preferred mode\n");
1111 if (output->mode.kind & name_string)
1112 fatal ("cannot find mode %s\n", output->mode.string);
1113 if (output->mode.kind & name_xid)
1114 fatal ("cannot find mode 0x%x\n", output->mode.xid);
1116 if (!output_can_use_mode (output, output->mode_info))
1117 fatal ("output %s cannot use mode %s\n", output->output.string,
1118 output->mode_info->name);
1122 if (!(output->changes & changes_position))
1124 if (output->crtc_info)
1126 output->x = output->crtc_info->crtc_info->x;
1127 output->y = output->crtc_info->crtc_info->y;
1137 if (!(output->changes & changes_rotation))
1139 output->rotation &= ~0xf;
1140 if (output->crtc_info)
1141 output->rotation |= (output->crtc_info->crtc_info->rotation & 0xf);
1143 output->rotation = RR_Rotate_0;
1145 if (!(output->changes & changes_reflection))
1147 output->rotation &= ~(RR_Reflect_X|RR_Reflect_Y);
1148 if (output->crtc_info)
1149 output->rotation |= (output->crtc_info->crtc_info->rotation &
1150 (RR_Reflect_X|RR_Reflect_Y));
1152 if (!output_can_use_rotation (output, output->rotation))
1153 fatal ("output %s cannot use rotation \"%s\" reflection \"%s\"\n",
1154 output->output.string,
1155 rotation_name (output->rotation),
1156 reflection_name (output->rotation));
1159 if (!(output->changes & changes_gamma))
1160 set_gamma_info(output);
1162 /* set transformation */
1163 if (!(output->changes & changes_transform))
1165 if (output->crtc_info)
1166 copy_transform (&output->transform, &output->crtc_info->current_transform);
1168 init_transform (&output->transform);
1172 if (!(output->changes & changes_primary))
1173 output->primary = output_is_primary(output);
1177 get_screen (Bool current)
1180 fatal ("Server RandR version before 1.2\n");
1182 XRRGetScreenSizeRange (dpy, root, &minWidth, &minHeight,
1183 &maxWidth, &maxHeight);
1186 res = XRRGetScreenResourcesCurrent (dpy, root);
1188 res = XRRGetScreenResources (dpy, root);
1189 if (!res) fatal ("could not get screen resources");
1197 num_crtcs = res->ncrtc;
1198 crtcs = calloc (num_crtcs, sizeof (crtc_t));
1199 if (!crtcs) fatal ("out of memory\n");
1201 for (c = 0; c < res->ncrtc; c++)
1203 XRRCrtcInfo *crtc_info = XRRGetCrtcInfo (dpy, res, res->crtcs[c]);
1204 XRRCrtcTransformAttributes *attr;
1205 XRRPanning *panning_info = NULL;
1209 memset(&zero, 0, sizeof(zero));
1210 panning_info = XRRGetPanning (dpy, res, res->crtcs[c]);
1211 zero.timestamp = panning_info->timestamp;
1212 if (!memcmp(panning_info, &zero, sizeof(zero))) {
1213 Xfree(panning_info);
1214 panning_info = NULL;
1218 set_name_xid (&crtcs[c].crtc, res->crtcs[c]);
1219 set_name_index (&crtcs[c].crtc, c);
1220 if (!crtc_info) fatal ("could not get crtc 0x%x information\n", res->crtcs[c]);
1221 crtcs[c].crtc_info = crtc_info;
1222 crtcs[c].panning_info = panning_info;
1223 if (crtc_info->mode == None)
1225 crtcs[c].mode_info = NULL;
1228 crtcs[c].rotation = RR_Rotate_0;
1230 if (XRRGetCrtcTransform (dpy, res->crtcs[c], &attr) && attr) {
1231 set_transform (&crtcs[c].current_transform,
1232 &attr->currentTransform,
1233 attr->currentFilter,
1234 attr->currentParams,
1235 attr->currentNparams);
1240 init_transform (&crtcs[c].current_transform);
1242 copy_transform (&crtcs[c].pending_transform, &crtcs[c].current_transform);
1247 crtc_add_output (crtc_t *crtc, output_t *output)
1250 crtc->outputs = realloc (crtc->outputs, (crtc->noutput + 1) * sizeof (output_t *));
1253 crtc->outputs = malloc (sizeof (output_t *));
1254 crtc->x = output->x;
1255 crtc->y = output->y;
1256 crtc->rotation = output->rotation;
1257 crtc->mode_info = output->mode_info;
1258 copy_transform (&crtc->pending_transform, &output->transform);
1260 if (!crtc->outputs) fatal ("out of memory\n");
1261 crtc->outputs[crtc->noutput++] = output;
1269 for (output = outputs; output; output = output->next)
1271 if (!output->mode_info) continue;
1272 crtc_add_output (output->crtc_info, output);
1281 for (output = outputs; output; output = output->next)
1283 if (! output->crtc_info)
1285 if (! (output->changes & changes_panning))
1287 if (! output->crtc_info->panning_info)
1288 output->crtc_info->panning_info = malloc (sizeof(XRRPanning));
1289 memcpy (output->crtc_info->panning_info, &output->panning, sizeof(XRRPanning));
1290 output->crtc_info->changing = 1;
1299 for (output = outputs; output; output = output->next) {
1302 XRRCrtcGamma *gamma;
1304 if (!(output->changes & changes_gamma))
1307 if (!output->crtc_info) {
1308 fatal("Need crtc to set gamma on.\n");
1312 crtc = output->crtc_info;
1314 size = XRRGetCrtcGammaSize(dpy, crtc->crtc.xid);
1317 fatal("Gamma size is 0.\n");
1321 gamma = XRRAllocGamma(size);
1323 fatal("Gamma allocation failed.\n");
1327 if(output->gamma.red == 0.0 && output->gamma.green == 0.0 && output->gamma.blue == 0.0)
1328 output->gamma.red = output->gamma.green = output->gamma.blue = 1.0;
1330 for (i = 0; i < size; i++) {
1331 if (output->gamma.red == 1.0 && output->brightness == 1.0)
1332 gamma->red[i] = (i << 8) + i;
1334 gamma->red[i] = dmin(pow((double)i/(double)(size - 1),
1335 output->gamma.red) * output->brightness,
1338 if (output->gamma.green == 1.0 && output->brightness == 1.0)
1339 gamma->green[i] = (i << 8) + i;
1341 gamma->green[i] = dmin(pow((double)i/(double)(size - 1),
1342 output->gamma.green) * output->brightness,
1345 if (output->gamma.blue == 1.0 && output->brightness == 1.0)
1346 gamma->blue[i] = (i << 8) + i;
1348 gamma->blue[i] = dmin(pow((double)i/(double)(size - 1),
1349 output->gamma.blue) * output->brightness,
1353 XRRSetCrtcGamma(dpy, crtc->crtc.xid, gamma);
1365 XRRSetOutputPrimary(dpy, root, None);
1367 for (output = outputs; output; output = output->next) {
1368 if (!(output->changes & changes_primary))
1370 if (output->primary)
1371 XRRSetOutputPrimary(dpy, root, output->output.xid);
1377 crtc_disable (crtc_t *crtc)
1380 printf ("crtc %d: disable\n", crtc->crtc.index);
1383 return RRSetConfigSuccess;
1384 return XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1385 0, 0, None, RR_Rotate_0, NULL, 0);
1389 crtc_set_transform (crtc_t *crtc, transform_t *transform)
1393 XRRQueryVersion (dpy, &major, &minor);
1394 if (major > 1 || (major == 1 && minor >= 3))
1395 XRRSetCrtcTransform (dpy, crtc->crtc.xid,
1396 &transform->transform,
1399 transform->nparams);
1403 crtc_revert (crtc_t *crtc)
1405 XRRCrtcInfo *crtc_info = crtc->crtc_info;
1408 printf ("crtc %d: revert\n", crtc->crtc.index);
1411 return RRSetConfigSuccess;
1413 if (!equal_transform (&crtc->current_transform, &crtc->pending_transform))
1414 crtc_set_transform (crtc, &crtc->current_transform);
1415 return XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1416 crtc_info->x, crtc_info->y,
1417 crtc_info->mode, crtc_info->rotation,
1418 crtc_info->outputs, crtc_info->noutput);
1422 crtc_apply (crtc_t *crtc)
1424 RROutput *rr_outputs;
1429 if (!crtc->changing || !crtc->mode_info)
1430 return RRSetConfigSuccess;
1432 rr_outputs = calloc (crtc->noutput, sizeof (RROutput));
1435 for (o = 0; o < crtc->noutput; o++)
1436 rr_outputs[o] = crtc->outputs[o]->output.xid;
1437 mode = crtc->mode_info->id;
1439 printf ("crtc %d: %12s %6.1f +%d+%d", crtc->crtc.index,
1440 crtc->mode_info->name, mode_refresh (crtc->mode_info),
1442 for (o = 0; o < crtc->noutput; o++)
1443 printf (" \"%s\"", crtc->outputs[o]->output.string);
1448 s = RRSetConfigSuccess;
1451 if (!equal_transform (&crtc->current_transform, &crtc->pending_transform))
1452 crtc_set_transform (crtc, &crtc->pending_transform);
1453 s = XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1454 crtc->x, crtc->y, mode, crtc->rotation,
1455 rr_outputs, crtc->noutput);
1456 if (s == RRSetConfigSuccess && crtc->panning_info) {
1458 s = XRRSetPanning (dpy, res, crtc->crtc.xid, crtc->panning_info);
1460 fatal ("panning needs RandR 1.3\n");
1468 screen_revert (void)
1471 printf ("screen %d: revert\n", screen);
1475 XRRSetScreenSize (dpy, root,
1476 DisplayWidth (dpy, screen),
1477 DisplayHeight (dpy, screen),
1478 DisplayWidthMM (dpy, screen),
1479 DisplayHeightMM (dpy, screen));
1485 if (fb_width == DisplayWidth (dpy, screen) &&
1486 fb_height == DisplayHeight (dpy, screen) &&
1487 fb_width_mm == DisplayWidthMM (dpy, screen) &&
1488 fb_height_mm == DisplayHeightMM (dpy, screen))
1493 printf ("screen %d: %dx%d %dx%d mm %6.2fdpi\n", screen,
1494 fb_width, fb_height, fb_width_mm, fb_height_mm, dpi);
1497 XRRSetScreenSize (dpy, root, fb_width, fb_height,
1498 fb_width_mm, fb_height_mm);
1506 /* first disable all crtcs */
1507 for (c = 0; c < res->ncrtc; c++)
1508 crtc_disable (&crtcs[c]);
1509 /* next reset screen size */
1511 /* now restore all crtcs */
1512 for (c = 0; c < res->ncrtc; c++)
1513 crtc_revert (&crtcs[c]);
1517 * uh-oh, something bad happened in the middle of changing
1518 * the configuration. Revert to the previous configuration
1521 static void _X_NORETURN
1522 panic (Status s, crtc_t *crtc)
1524 int c = crtc->crtc.index;
1528 case RRSetConfigSuccess: message = "succeeded"; break;
1529 case BadAlloc: message = "out of memory"; break;
1530 case RRSetConfigFailed: message = "failed"; break;
1531 case RRSetConfigInvalidConfigTime: message = "invalid config time"; break;
1532 case RRSetConfigInvalidTime: message = "invalid time"; break;
1533 default: message = "unknown failure"; break;
1536 fprintf (stderr, "%s: Configure crtc %d %s\n", program_name, c, message);
1548 * Hold the server grabbed while messing with
1549 * the screen so that apps which notice the resize
1550 * event and ask for xinerama information from the server
1551 * receive up-to-date information
1557 * Turn off any crtcs which are to be disabled or which are
1558 * larger than the target size
1560 for (c = 0; c < res->ncrtc; c++)
1562 crtc_t *crtc = &crtcs[c];
1563 XRRCrtcInfo *crtc_info = crtc->crtc_info;
1565 /* if this crtc is already disabled, skip it */
1566 if (crtc_info->mode == None)
1570 * If this crtc is to be left enabled, make
1571 * sure the old size fits then new screen
1573 if (crtc->mode_info)
1575 XRRModeInfo *old_mode = find_mode_by_xid (crtc_info->mode);
1580 panic (RRSetConfigFailed, crtc);
1582 /* old position and size information */
1583 mode_geometry (old_mode, crtc_info->rotation,
1584 &crtc->current_transform.transform,
1587 x = crtc_info->x + bounds.x1;
1588 y = crtc_info->y + bounds.y1;
1589 w = bounds.x2 - bounds.x1;
1590 h = bounds.y2 - bounds.y1;
1592 /* if it fits, skip it */
1593 if (x + w <= fb_width && y + h <= fb_height)
1595 crtc->changing = True;
1597 s = crtc_disable (crtc);
1598 if (s != RRSetConfigSuccess)
1603 * Set the screen size
1611 for (c = 0; c < res->ncrtc; c++)
1613 crtc_t *crtc = &crtcs[c];
1615 s = crtc_apply (crtc);
1616 if (s != RRSetConfigSuccess)
1623 * Release the server grab and let all clients
1624 * respond to the updated state
1627 XUngrabServer (dpy);
1631 * Use current output state to complete the output list
1639 for (o = 0; o < res->noutput; o++)
1641 XRROutputInfo *output_info = XRRGetOutputInfo (dpy, res, res->outputs[o]);
1644 if (!output_info) fatal ("could not get output 0x%x information\n", res->outputs[o]);
1645 set_name_xid (&output_name, res->outputs[o]);
1646 set_name_index (&output_name, o);
1647 set_name_string (&output_name, output_info->name);
1648 output = find_output (&output_name);
1651 output = add_output ();
1652 set_name_all (&output->output, &output_name);
1654 * When global --automatic mode is set, turn on connected but off
1655 * outputs, turn off disconnected but on outputs
1659 switch (output_info->connection) {
1661 if (!output_info->crtc) {
1662 output->changes |= changes_automatic;
1663 output->automatic = True;
1666 case RR_Disconnected:
1667 if (output_info->crtc)
1669 output->changes |= changes_automatic;
1670 output->automatic = True;
1676 output->found = True;
1679 * Automatic mode -- track connection state and enable/disable outputs
1682 if (output->automatic)
1684 switch (output_info->connection) {
1686 case RR_UnknownConnection:
1687 if ((!(output->changes & changes_mode)))
1689 set_name_preferred (&output->mode);
1690 output->changes |= changes_mode;
1693 case RR_Disconnected:
1694 if ((!(output->changes & changes_mode)))
1696 set_name_xid (&output->mode, None);
1697 set_name_xid (&output->crtc, None);
1698 output->changes |= changes_mode;
1699 output->changes |= changes_crtc;
1705 set_output_info (output, res->outputs[o], output_info);
1707 for (q = outputs; q; q = q->next)
1711 fprintf(stderr, "warning: output %s not found; ignoring\n",
1718 mark_changing_crtcs (void)
1722 for (c = 0; c < num_crtcs; c++)
1724 crtc_t *crtc = &crtcs[c];
1728 /* walk old output list (to catch disables) */
1729 for (o = 0; o < crtc->crtc_info->noutput; o++)
1731 output = find_output_by_xid (crtc->crtc_info->outputs[o]);
1732 if (!output) fatal ("cannot find output 0x%x\n",
1733 crtc->crtc_info->outputs[o]);
1734 if (output->changes)
1735 crtc->changing = True;
1737 /* walk new output list */
1738 for (o = 0; o < crtc->noutput; o++)
1740 output = crtc->outputs[o];
1741 if (output->changes)
1742 crtc->changing = True;
1748 * Test whether 'crtc' can be used for 'output'
1751 check_crtc_for_output (crtc_t *crtc, output_t *output)
1757 for (c = 0; c < output->output_info->ncrtc; c++)
1758 if (output->output_info->crtcs[c] == crtc->crtc.xid)
1760 if (c == output->output_info->ncrtc)
1762 for (other = outputs; other; other = other->next)
1764 if (other == output)
1767 if (other->mode_info == NULL)
1770 if (other->crtc_info != crtc)
1773 /* see if the output connected to the crtc can clone to this output */
1774 for (l = 0; l < output->output_info->nclone; l++)
1775 if (output->output_info->clones[l] == other->output.xid)
1777 /* not on the list, can't clone */
1778 if (l == output->output_info->nclone)
1784 /* make sure the state matches */
1785 if (crtc->mode_info != output->mode_info)
1787 if (crtc->x != output->x)
1789 if (crtc->y != output->y)
1791 if (crtc->rotation != output->rotation)
1793 if (!equal_transform (&crtc->current_transform, &output->transform))
1796 else if (crtc->crtc_info->noutput)
1798 /* make sure the state matches the already used state */
1799 XRRModeInfo *mode = find_mode_by_xid (crtc->crtc_info->mode);
1801 if (mode != output->mode_info)
1803 if (crtc->crtc_info->x != output->x)
1805 if (crtc->crtc_info->y != output->y)
1807 if (crtc->crtc_info->rotation != output->rotation)
1814 find_crtc_for_output (output_t *output)
1818 for (c = 0; c < output->output_info->ncrtc; c++)
1822 crtc = find_crtc_by_xid (output->output_info->crtcs[c]);
1823 if (!crtc) fatal ("cannot find crtc 0x%x\n", output->output_info->crtcs[c]);
1825 if (check_crtc_for_output (crtc, output))
1832 set_positions (void)
1843 for (output = outputs; output; output = output->next)
1846 name_t relation_name;
1848 if (!(output->changes & changes_relation)) continue;
1850 if (output->mode_info == NULL) continue;
1852 init_name (&relation_name);
1853 set_name_string (&relation_name, output->relative_to);
1854 relation = find_output (&relation_name);
1855 if (!relation) fatal ("cannot find output \"%s\"\n", output->relative_to);
1857 if (relation->mode_info == NULL)
1861 output->changes |= changes_position;
1866 * Make sure the dependent object has been set in place
1868 if ((relation->changes & changes_relation) &&
1869 !(relation->changes & changes_position))
1875 switch (output->relation) {
1876 case relation_left_of:
1877 output->y = relation->y;
1878 output->x = relation->x - mode_width (output->mode_info, output->rotation);
1880 case relation_right_of:
1881 output->y = relation->y;
1882 output->x = relation->x + mode_width (relation->mode_info, relation->rotation);
1884 case relation_above:
1885 output->x = relation->x;
1886 output->y = relation->y - mode_height (output->mode_info, output->rotation);
1888 case relation_below:
1889 output->x = relation->x;
1890 output->y = relation->y + mode_height (relation->mode_info, relation->rotation);
1892 case relation_same_as:
1893 output->x = relation->x;
1894 output->y = relation->y;
1896 output->changes |= changes_position;
1902 fatal ("loop in relative position specifications\n");
1906 * Now normalize positions so the upper left corner of all outputs is at 0,0
1910 for (output = outputs; output; output = output->next)
1912 if (output->mode_info == NULL) continue;
1914 if (output->x < min_x) min_x = output->x;
1915 if (output->y < min_y) min_y = output->y;
1919 /* move all outputs */
1920 for (output = outputs; output; output = output->next)
1922 if (output->mode_info == NULL) continue;
1926 output->changes |= changes_position;
1932 set_screen_size (void)
1935 Bool fb_specified = fb_width != 0 && fb_height != 0;
1937 for (output = outputs; output; output = output->next)
1939 XRRModeInfo *mode_info = output->mode_info;
1943 if (!mode_info) continue;
1945 mode_geometry (mode_info, output->rotation,
1946 &output->transform.transform,
1948 x = output->x + bounds.x1;
1949 y = output->y + bounds.y1;
1950 w = bounds.x2 - bounds.x1;
1951 h = bounds.y2 - bounds.y1;
1952 /* make sure output fits in specified size */
1955 if (x + w > fb_width || y + h > fb_height)
1956 warning ("specified screen %dx%d not large enough for output %s (%dx%d+%d+%d)\n",
1957 fb_width, fb_height, output->output.string, w, h, x, y);
1959 /* fit fb to output */
1963 if (x + w > fb_width)
1965 if (y + h > fb_height)
1967 if (output->changes & changes_panning)
1968 pan = &output->panning;
1970 pan = output->crtc_info ? output->crtc_info->panning_info : NULL;
1971 if (pan && pan->left + pan->width > fb_width)
1972 fb_width = pan->left + pan->width;
1973 if (pan && pan->top + pan->height > fb_height)
1974 fb_height = pan->top + pan->height;
1978 if (fb_width > maxWidth || fb_height > maxHeight)
1979 fatal ("screen cannot be larger than %dx%d (desired size %dx%d)\n",
1980 maxWidth, maxHeight, fb_width, fb_height);
1983 if (fb_width < minWidth || fb_height < minHeight)
1984 fatal ("screen must be at least %dx%d\n", minWidth, minHeight);
1988 if (fb_width < minWidth) fb_width = minWidth;
1989 if (fb_height < minHeight) fb_height = minHeight;
1995 disable_outputs (output_t *outputs)
1999 outputs->crtc_info = NULL;
2000 outputs = outputs->next;
2005 * find the best mapping from output to crtc available
2008 pick_crtcs_score (output_t *outputs)
2021 outputs = outputs->next;
2023 * Score with this output disabled
2025 output->crtc_info = NULL;
2026 best_score = pick_crtcs_score (outputs);
2027 if (output->mode_info == NULL)
2032 * Now score with this output any valid crtc
2034 for (c = 0; c < output->output_info->ncrtc; c++)
2038 crtc = find_crtc_by_xid (output->output_info->crtcs[c]);
2040 fatal ("cannot find crtc 0x%x\n", output->output_info->crtcs[c]);
2042 /* reset crtc allocation for following outputs */
2043 disable_outputs (outputs);
2044 if (!check_crtc_for_output (crtc, output))
2048 /* slight preference for existing connections */
2049 if (crtc == output->current_crtc_info)
2052 output->crtc_info = crtc;
2053 score = my_score + pick_crtcs_score (outputs);
2054 if (score > best_score)
2060 if (output->crtc_info != best_crtc)
2061 output->crtc_info = best_crtc;
2063 * Reset other outputs based on this one using the best crtc
2065 (void) pick_crtcs_score (outputs);
2071 * Pick crtcs for any changing outputs that don't have one
2079 * First try to match up newly enabled outputs with spare crtcs
2081 for (output = outputs; output; output = output->next)
2083 if (output->changes && output->mode_info)
2085 if (output->crtc_info) {
2086 if (output->crtc_info->crtc_info->noutput > 0 &&
2087 (output->crtc_info->crtc_info->noutput > 1 ||
2088 output != find_output_by_xid (output->crtc_info->crtc_info->outputs[0])))
2091 output->crtc_info = find_crtc_for_output (output);
2092 if (!output->crtc_info)
2103 * When the simple way fails, see if there is a way
2104 * to swap crtcs around and make things work
2106 for (output = outputs; output; output = output->next)
2107 output->current_crtc_info = output->crtc_info;
2108 pick_crtcs_score (outputs);
2109 for (output = outputs; output; output = output->next)
2111 if (output->mode_info && !output->crtc_info)
2112 fatal ("cannot find crtc for output %s\n", output->output.string);
2113 if (!output->changes && output->crtc_info != output->current_crtc_info)
2114 output->changes |= changes_crtc;
2119 check_strtol(char *s)
2122 int result = strtol(s, &endptr, 10);
2129 check_strtod(char *s)
2132 double result = strtod(s, &endptr);
2139 main (int argc, char **argv)
2141 XRRScreenSize *sizes;
2142 XRRScreenConfiguration *sc;
2146 Status status = RRSetConfigFailed;
2149 int action_requested = False;
2150 Rotation rotation, current_rotation, rotations;
2152 XRRScreenChangeNotifyEvent *sce;
2153 char *display_name = NULL;
2155 SizeID current_size;
2161 Bool version = False;
2162 int event_base, error_base;
2164 int width = 0, height = 0;
2165 Bool have_pixel_size = False;
2167 output_t *output = NULL;
2168 Bool setit_1_2 = False;
2169 Bool query_1_2 = False;
2170 Bool modeit = False;
2171 Bool propit = False;
2172 Bool query_1 = False;
2174 Bool current = False;
2176 program_name = argv[0];
2177 for (i = 1; i < argc; i++) {
2178 if (!strcmp ("-display", argv[i]) || !strcmp ("-d", argv[i])) {
2179 if (++i>=argc) usage ();
2180 display_name = argv[i];
2183 if (!strcmp("-help", argv[i])) {
2185 action_requested = True;
2188 if (!strcmp ("--verbose", argv[i])) {
2192 if (!strcmp ("--dryrun", argv[i])) {
2197 if (!strcmp ("--nograb", argv[i])) {
2198 grab_server = False;
2201 if (!strcmp("--current", argv[i])) {
2206 if (!strcmp ("-s", argv[i]) || !strcmp ("--size", argv[i])) {
2207 if (++i>=argc) usage ();
2208 if (sscanf (argv[i], "%dx%d", &width, &height) == 2) {
2209 have_pixel_size = True;
2211 size = check_strtol(argv[i]);
2212 if (size < 0) usage();
2215 action_requested = True;
2219 if (!strcmp ("-r", argv[i]) ||
2220 !strcmp ("--rate", argv[i]) ||
2221 !strcmp ("--refresh", argv[i]))
2223 if (++i>=argc) usage ();
2224 rate = check_strtod(argv[i]);
2228 output->refresh = rate;
2229 output->changes |= changes_refresh;
2232 action_requested = True;
2236 if (!strcmp ("-v", argv[i]) || !strcmp ("--version", argv[i])) {
2238 action_requested = True;
2242 if (!strcmp ("-x", argv[i])) {
2243 reflection |= RR_Reflect_X;
2245 action_requested = True;
2248 if (!strcmp ("-y", argv[i])) {
2249 reflection |= RR_Reflect_Y;
2251 action_requested = True;
2254 if (!strcmp ("--screen", argv[i])) {
2255 if (++i>=argc) usage ();
2256 screen = check_strtol(argv[i]);
2257 if (screen < 0) usage();
2260 if (!strcmp ("-q", argv[i]) || !strcmp ("--query", argv[i])) {
2264 if (!strcmp ("-o", argv[i]) || !strcmp ("--orientation", argv[i])) {
2266 if (++i>=argc) usage ();
2267 dirind = strtol(argv[i], &endptr, 10);
2268 if (argv[i] == endptr) {
2269 for (dirind = 0; dirind < 4; dirind++) {
2270 if (strcmp (direction[dirind], argv[i]) == 0) break;
2272 if ((dirind < 0) || (dirind > 3)) usage();
2276 action_requested = True;
2279 if (!strcmp ("--prop", argv[i]) ||
2280 !strcmp ("--props", argv[i]) ||
2281 !strcmp ("--madprops", argv[i]) ||
2282 !strcmp ("--properties", argv[i]))
2286 action_requested = True;
2289 if (!strcmp ("--output", argv[i])) {
2290 if (++i >= argc) usage();
2292 output = find_output_by_name (argv[i]);
2294 output = add_output ();
2295 set_name (&output->output, argv[i], name_string|name_xid);
2299 action_requested = True;
2302 if (!strcmp ("--crtc", argv[i])) {
2303 if (++i >= argc) usage();
2304 if (!output) usage();
2305 set_name (&output->crtc, argv[i], name_xid|name_index);
2306 output->changes |= changes_crtc;
2309 if (!strcmp ("--mode", argv[i])) {
2310 if (++i >= argc) usage();
2311 if (!output) usage();
2312 set_name (&output->mode, argv[i], name_string|name_xid);
2313 output->changes |= changes_mode;
2316 if (!strcmp ("--preferred", argv[i])) {
2317 if (!output) usage();
2318 set_name_preferred (&output->mode);
2319 output->changes |= changes_mode;
2322 if (!strcmp ("--pos", argv[i])) {
2323 if (++i>=argc) usage ();
2324 if (!output) usage();
2325 if (sscanf (argv[i], "%dx%d",
2326 &output->x, &output->y) != 2)
2328 output->changes |= changes_position;
2331 if (!strcmp ("--rotation", argv[i]) || !strcmp ("--rotate", argv[i])) {
2332 if (++i>=argc) usage ();
2333 if (!output) usage();
2334 for (dirind = 0; dirind < 4; dirind++) {
2335 if (strcmp (direction[dirind], argv[i]) == 0) break;
2339 output->rotation &= ~0xf;
2340 output->rotation |= 1 << dirind;
2341 output->changes |= changes_rotation;
2344 if (!strcmp ("--reflect", argv[i]) || !strcmp ("--reflection", argv[i])) {
2345 if (++i>=argc) usage ();
2346 if (!output) usage();
2347 for (dirind = 0; dirind < 4; dirind++) {
2348 if (strcmp (reflections[dirind], argv[i]) == 0) break;
2352 output->rotation &= ~(RR_Reflect_X|RR_Reflect_Y);
2353 output->rotation |= dirind * RR_Reflect_X;
2354 output->changes |= changes_reflection;
2357 if (!strcmp ("--left-of", argv[i])) {
2358 if (++i>=argc) usage ();
2359 if (!output) usage();
2360 output->relation = relation_left_of;
2361 output->relative_to = argv[i];
2362 output->changes |= changes_relation;
2365 if (!strcmp ("--right-of", argv[i])) {
2366 if (++i>=argc) usage ();
2367 if (!output) usage();
2368 output->relation = relation_right_of;
2369 output->relative_to = argv[i];
2370 output->changes |= changes_relation;
2373 if (!strcmp ("--above", argv[i])) {
2374 if (++i>=argc) usage ();
2375 if (!output) usage();
2376 output->relation = relation_above;
2377 output->relative_to = argv[i];
2378 output->changes |= changes_relation;
2381 if (!strcmp ("--below", argv[i])) {
2382 if (++i>=argc) usage ();
2383 if (!output) usage();
2384 output->relation = relation_below;
2385 output->relative_to = argv[i];
2386 output->changes |= changes_relation;
2389 if (!strcmp ("--same-as", argv[i])) {
2390 if (++i>=argc) usage ();
2391 if (!output) usage();
2392 output->relation = relation_same_as;
2393 output->relative_to = argv[i];
2394 output->changes |= changes_relation;
2397 if (!strcmp ("--panning", argv[i])) {
2399 if (++i>=argc) usage ();
2400 if (!output) usage();
2401 pan = &output->panning;
2402 switch (sscanf (argv[i], "%dx%d+%d+%d/%dx%d+%d+%d/%d/%d/%d/%d",
2403 &pan->width, &pan->height, &pan->left, &pan->top,
2404 &pan->track_width, &pan->track_height,
2405 &pan->track_left, &pan->track_top,
2406 &pan->border_left, &pan->border_top,
2407 &pan->border_right, &pan->border_bottom)) {
2409 pan->left = pan->top = 0;
2412 pan->track_left = pan->track_top =
2413 pan->track_width = pan->track_height = 0;
2416 pan->border_left = pan->border_top =
2417 pan->border_right = pan->border_bottom = 0;
2424 output->changes |= changes_panning;
2427 if (!strcmp ("--gamma", argv[i])) {
2428 if (!output) usage();
2429 if (++i>=argc) usage ();
2430 if (sscanf(argv[i], "%f:%f:%f", &output->gamma.red,
2431 &output->gamma.green, &output->gamma.blue) != 3)
2433 output->changes |= changes_gamma;
2437 if (!strcmp ("--brightness", argv[i])) {
2438 if (!output) usage();
2439 if (++i>=argc) usage();
2440 if (sscanf(argv[i], "%f", &output->brightness) != 1)
2442 output->changes |= changes_gamma;
2446 if (!strcmp ("--primary", argv[i])) {
2447 if (!output) usage();
2448 output->changes |= changes_primary;
2449 output->primary = True;
2453 if (!strcmp ("--noprimary", argv[i])) {
2458 if (!strcmp ("--set", argv[i])) {
2459 output_prop_t *prop;
2460 if (!output) usage();
2461 prop = malloc (sizeof (output_prop_t));
2462 prop->next = output->props;
2463 output->props = prop;
2464 if (++i>=argc) usage ();
2465 prop->name = argv[i];
2466 if (++i>=argc) usage ();
2467 prop->value = argv[i];
2469 output->changes |= changes_property;
2473 if (!strcmp ("--scale", argv[i]))
2476 if (!output) usage();
2477 if (++i>=argc) usage();
2478 if (sscanf (argv[i], "%lfx%lf", &sx, &sy) != 2)
2480 init_transform (&output->transform);
2481 output->transform.transform.matrix[0][0] = XDoubleToFixed (sx);
2482 output->transform.transform.matrix[1][1] = XDoubleToFixed (sy);
2483 output->transform.transform.matrix[2][2] = XDoubleToFixed (1.0);
2484 if (sx != 1 || sy != 1)
2485 output->transform.filter = "bilinear";
2487 output->transform.filter = "nearest";
2488 output->transform.nparams = 0;
2489 output->transform.params = NULL;
2490 output->changes |= changes_transform;
2493 if (!strcmp ("--transform", argv[i])) {
2494 double transform[3][3];
2496 if (!output) usage();
2497 if (++i>=argc) usage ();
2498 init_transform (&output->transform);
2499 if (strcmp (argv[i], "none") != 0)
2501 if (sscanf(argv[i], "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf",
2502 &transform[0][0],&transform[0][1],&transform[0][2],
2503 &transform[1][0],&transform[1][1],&transform[1][2],
2504 &transform[2][0],&transform[2][1],&transform[2][2])
2507 init_transform (&output->transform);
2508 for (k = 0; k < 3; k++)
2509 for (l = 0; l < 3; l++) {
2510 output->transform.transform.matrix[k][l] = XDoubleToFixed (transform[k][l]);
2512 output->transform.filter = "bilinear";
2513 output->transform.nparams = 0;
2514 output->transform.params = NULL;
2516 output->changes |= changes_transform;
2519 if (!strcmp ("--off", argv[i])) {
2520 if (!output) usage();
2521 set_name_xid (&output->mode, None);
2522 set_name_xid (&output->crtc, None);
2523 output->changes |= changes_mode;
2526 if (!strcmp ("--fb", argv[i])) {
2527 if (++i>=argc) usage ();
2528 if (sscanf (argv[i], "%dx%d",
2529 &fb_width, &fb_height) != 2)
2532 action_requested = True;
2535 if (!strcmp ("--fbmm", argv[i])) {
2536 if (++i>=argc) usage ();
2537 if (sscanf (argv[i], "%dx%d",
2538 &fb_width_mm, &fb_height_mm) != 2)
2541 action_requested = True;
2544 if (!strcmp ("--dpi", argv[i])) {
2546 if (++i>=argc) usage ();
2547 dpi = strtod(argv[i], &strtod_error);
2548 if (argv[i] == strtod_error)
2551 dpi_output = argv[i];
2554 action_requested = True;
2557 if (!strcmp ("--auto", argv[i])) {
2560 output->automatic = True;
2561 output->changes |= changes_automatic;
2566 action_requested = True;
2569 if (!strcmp ("--q12", argv[i]))
2574 if (!strcmp ("--q1", argv[i]))
2579 if (!strcmp ("--newmode", argv[i]))
2581 umode_t *m = malloc (sizeof (umode_t));
2585 if (i + 9 >= argc) usage ();
2586 m->mode.name = argv[i];
2587 m->mode.nameLength = strlen (argv[i]);
2589 clock = check_strtod(argv[i++]);
2590 m->mode.dotClock = clock * 1e6;
2592 m->mode.width = check_strtol(argv[i++]);
2593 m->mode.hSyncStart = check_strtol(argv[i++]);
2594 m->mode.hSyncEnd = check_strtol(argv[i++]);
2595 m->mode.hTotal = check_strtol(argv[i++]);
2596 m->mode.height = check_strtol(argv[i++]);
2597 m->mode.vSyncStart = check_strtol(argv[i++]);
2598 m->mode.vSyncEnd = check_strtol(argv[i++]);
2599 m->mode.vTotal = check_strtol(argv[i++]);
2600 m->mode.modeFlags = 0;
2604 for (f = 0; mode_flags[f].string; f++)
2605 if (!strcasecmp (mode_flags[f].string, argv[i]))
2608 if (!mode_flags[f].string)
2610 m->mode.modeFlags |= mode_flags[f].flag;
2614 m->action = umode_create;
2617 action_requested = True;
2620 if (!strcmp ("--rmmode", argv[i]))
2622 umode_t *m = malloc (sizeof (umode_t));
2624 if (++i>=argc) usage ();
2625 set_name (&m->name, argv[i], name_string|name_xid);
2626 m->action = umode_destroy;
2630 action_requested = True;
2633 if (!strcmp ("--addmode", argv[i]))
2635 umode_t *m = malloc (sizeof (umode_t));
2637 if (++i>=argc) usage ();
2638 set_name (&m->output, argv[i], name_string|name_xid);
2639 if (++i>=argc) usage();
2640 set_name (&m->name, argv[i], name_string|name_xid);
2641 m->action = umode_add;
2645 action_requested = True;
2648 if (!strcmp ("--delmode", argv[i]))
2650 umode_t *m = malloc (sizeof (umode_t));
2652 if (++i>=argc) usage ();
2653 set_name (&m->output, argv[i], name_string|name_xid);
2654 if (++i>=argc) usage();
2655 set_name (&m->name, argv[i], name_string|name_xid);
2656 m->action = umode_delete;
2660 action_requested = True;
2665 if (!action_requested)
2670 if (setit && !setit_1_2)
2674 printf("xrandr program version " VERSION "\n");
2676 dpy = XOpenDisplay (display_name);
2679 fprintf (stderr, "Can't open display %s\n", XDisplayName(display_name));
2683 screen = DefaultScreen (dpy);
2684 if (screen >= ScreenCount (dpy)) {
2685 fprintf (stderr, "Invalid screen number %d (display has %d)\n",
2686 screen, ScreenCount (dpy));
2690 root = RootWindow (dpy, screen);
2692 if (!XRRQueryExtension (dpy, &event_base, &error_base) ||
2693 !XRRQueryVersion (dpy, &major, &minor))
2695 fprintf (stderr, "RandR extension missing\n");
2698 if (major > 1 || (major == 1 && minor >= 2))
2700 if (major > 1 || (major == 1 && minor >= 3))
2703 if (has_1_2 && modeit)
2707 get_screen (current);
2711 for (m = umodes; m; m = m->next)
2716 switch (m->action) {
2718 XRRCreateMode (dpy, root, &m->mode);
2721 e = find_mode (&m->name, 0);
2723 fatal ("cannot find mode \"%s\"\n", m->name.string);
2724 XRRDestroyMode (dpy, 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 XRRAddOutputMode (dpy, o->output.xid, e->id);
2736 o = find_output (&m->output);
2738 fatal ("cannot find output \"%s\"\n", m->output.string);
2739 e = find_mode (&m->name, 0);
2741 fatal ("cannot find mode \"%s\"\n", m->name.string);
2742 XRRDeleteOutputMode (dpy, o->output.xid, e->id);
2752 if (has_1_2 && propit)
2755 get_screen (current);
2759 for (output = outputs; output; output = output->next)
2761 output_prop_t *prop;
2763 for (prop = output->props; prop; prop = prop->next)
2765 Atom name = XInternAtom (dpy, prop->name, False);
2768 unsigned char *data;
2771 unsigned long ulong_value;
2772 unsigned char *prop_data;
2774 unsigned long nitems, bytes_after;
2776 XRRPropertyInfo *propinfo;
2778 type = AnyPropertyType;
2780 if (XRRGetOutputProperty (dpy, output->output.xid, name,
2781 0, 100, False, False,
2783 &actual_type, &actual_format,
2784 &nitems, &bytes_after, &prop_data) == Success &&
2786 (propinfo = XRRQueryOutputProperty(dpy, output->output.xid,
2790 format = actual_format;
2793 if ((type == XA_INTEGER || type == AnyPropertyType) &&
2794 (sscanf (prop->value, "%d", &int_value) == 1 ||
2795 sscanf (prop->value, "0x%x", &int_value) == 1))
2798 ulong_value = int_value;
2799 data = (unsigned char *) &ulong_value;
2803 else if ((type == XA_ATOM))
2805 ulong_value = XInternAtom (dpy, prop->value, False);
2806 data = (unsigned char *) &ulong_value;
2809 else if ((type == XA_STRING || type == AnyPropertyType))
2812 data = (unsigned char *) prop->value;
2813 nelements = strlen (prop->value);
2818 XRRChangeOutputProperty (dpy, output->output.xid,
2819 name, type, format, PropModeReplace,
2831 get_screen (current);
2840 * Assign outputs to crtcs
2845 * Mark changing crtcs
2847 mark_changing_crtcs ();
2850 * If an output was specified to track dpi, use it
2854 output_t *output = find_output_by_name (dpi_output);
2855 XRROutputInfo *output_info;
2856 XRRModeInfo *mode_info;
2858 fatal ("Cannot find output %s\n", dpi_output);
2859 output_info = output->output_info;
2860 mode_info = output->mode_info;
2861 if (output_info && mode_info && output_info->mm_height)
2864 * When this output covers the whole screen, just use
2865 * the known physical size
2867 if (fb_width == mode_info->width &&
2868 fb_height == mode_info->height)
2870 fb_width_mm = output_info->mm_width;
2871 fb_height_mm = output_info->mm_height;
2875 dpi = (25.4 * mode_info->height) / output_info->mm_height;
2881 * Compute physical screen size
2883 if (fb_width_mm == 0 || fb_height_mm == 0)
2885 if (fb_width != DisplayWidth (dpy, screen) ||
2886 fb_height != DisplayHeight (dpy, screen) || dpi != 0.0)
2889 dpi = (25.4 * DisplayHeight (dpy, screen)) / DisplayHeightMM(dpy, screen);
2891 fb_width_mm = (25.4 * fb_width) / dpi;
2892 fb_height_mm = (25.4 * fb_height) / dpi;
2896 fb_width_mm = DisplayWidthMM (dpy, screen);
2897 fb_height_mm = DisplayHeightMM (dpy, screen);
2907 * Set gamma on crtc's that belong to the outputs.
2912 * Now apply all of the changes
2919 if (query_1_2 || (query && has_1_2 && !query_1))
2924 #define ModeShown 0x80000000
2926 get_screen (current);
2930 printf ("Screen %d: minimum %d x %d, current %d x %d, maximum %d x %d\n",
2931 screen, minWidth, minHeight,
2932 DisplayWidth (dpy, screen), DisplayHeight(dpy, screen),
2933 maxWidth, maxHeight);
2935 for (output = outputs; output; output = output->next)
2937 XRROutputInfo *output_info = output->output_info;
2938 crtc_t *crtc = output->crtc_info;
2939 XRRCrtcInfo *crtc_info = crtc ? crtc->crtc_info : NULL;
2940 XRRModeInfo *mode = output->mode_info;
2944 Rotation rotations = output_rotations (output);
2946 printf ("%s %s", output_info->name, connection[output_info->connection]);
2950 printf (" %dx%d+%d+%d",
2951 crtc_info->width, crtc_info->height,
2952 crtc_info->x, crtc_info->y);
2954 printf (" %dx%d+%d+%d",
2955 mode->width, mode->height, output->x, output->y);
2958 printf (" (0x%x)", (int)mode->id);
2959 if (output->rotation != RR_Rotate_0 || verbose)
2962 rotation_name (output->rotation));
2963 if (output->rotation & (RR_Reflect_X|RR_Reflect_Y))
2964 printf (" %s", reflection_name (output->rotation));
2967 if (rotations != RR_Rotate_0 || verbose)
2971 for (i = 0; i < 4; i ++) {
2972 if ((rotations >> i) & 1) {
2973 if (!first) printf (" "); first = False;
2974 printf("%s", direction[i]);
2977 if (rotations & RR_Reflect_X)
2979 if (!first) printf (" "); first = False;
2982 if (rotations & RR_Reflect_Y)
2984 if (!first) printf (" ");
2992 printf (" %dmm x %dmm",
2993 (int)output_info->mm_width, (int)output_info->mm_height);
2996 if (crtc && crtc->panning_info && crtc->panning_info->width > 0)
2998 XRRPanning *pan = crtc->panning_info;
2999 printf (" panning %dx%d+%d+%d",
3000 pan->width, pan->height, pan->left, pan->top);
3001 if ((pan->track_width != 0 &&
3002 (pan->track_left != pan->left ||
3003 pan->track_width != pan->width ||
3004 pan->border_left != 0 ||
3005 pan->border_right != 0)) ||
3006 (pan->track_height != 0 &&
3007 (pan->track_top != pan->top ||
3008 pan->track_height != pan->height ||
3009 pan->border_top != 0 ||
3010 pan->border_bottom != 0)))
3011 printf (" tracking %dx%d+%d+%d border %d/%d/%d/%d",
3012 pan->track_width, pan->track_height,
3013 pan->track_left, pan->track_top,
3014 pan->border_left, pan->border_top,
3015 pan->border_right, pan->border_bottom);
3021 printf ("\tIdentifier: 0x%x\n", (int)output->output.xid);
3022 printf ("\tTimestamp: %d\n", (int)output_info->timestamp);
3023 printf ("\tSubpixel: %s\n", order[output_info->subpixel_order]);
3024 if (output->gamma.red != 0.0 && output->gamma.green != 0.0 && output->gamma.blue != 0.0) {
3025 printf ("\tGamma: %#.2g:%#.2g:%#.2g\n",
3026 output->gamma.red, output->gamma.green, output->gamma.blue);
3027 printf ("\tBrightness: %#.2g\n", output->brightness);
3029 printf ("\tClones: ");
3030 for (j = 0; j < output_info->nclone; j++)
3032 output_t *clone = find_output_by_xid (output_info->clones[j]);
3034 if (clone) printf (" %s", clone->output.string);
3037 if (output->crtc_info)
3038 printf ("\tCRTC: %d\n", output->crtc_info->crtc.index);
3039 printf ("\tCRTCs: ");
3040 for (j = 0; j < output_info->ncrtc; j++)
3042 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[j]);
3044 printf (" %d", crtc->crtc.index);
3047 if (output->crtc_info && output->crtc_info->panning_info) {
3048 XRRPanning *pan = output->crtc_info->panning_info;
3049 printf ("\tPanning: %dx%d+%d+%d\n",
3050 pan->width, pan->height, pan->left, pan->top);
3051 printf ("\tTracking: %dx%d+%d+%d\n",
3052 pan->track_width, pan->track_height,
3053 pan->track_left, pan->track_top);
3054 printf ("\tBorder: %d/%d/%d/%d\n",
3055 pan->border_left, pan->border_top,
3056 pan->border_right, pan->border_bottom);
3063 printf ("\tTransform: ");
3064 for (y = 0; y < 3; y++)
3066 for (x = 0; x < 3; x++)
3067 printf (" %f", XFixedToDouble (output->transform.transform.matrix[y][x]));
3071 if (output->transform.filter)
3072 printf ("\n\t filter: %s", output->transform.filter);
3075 if (verbose || properties)
3077 props = XRRListOutputProperties (dpy, output->output.xid,
3079 for (j = 0; j < nprop; j++) {
3080 unsigned char *prop;
3082 unsigned long nitems, bytes_after;
3084 XRRPropertyInfo *propinfo;
3086 XRRGetOutputProperty (dpy, output->output.xid, props[j],
3087 0, 100, False, False,
3089 &actual_type, &actual_format,
3090 &nitems, &bytes_after, &prop);
3092 propinfo = XRRQueryOutputProperty(dpy, output->output.xid,
3095 if (actual_type == XA_INTEGER && actual_format == 8) {
3098 printf("\t%s:\n", XGetAtomName (dpy, props[j]));
3099 for (k = 0; k < nitems; k++) {
3102 printf("%02x", (unsigned char)prop[k]);
3106 } else if (actual_type == XA_INTEGER &&
3107 actual_format == 32)
3109 printf("\t%s: ", XGetAtomName (dpy, props[j]));
3110 for (k = 0; k < nitems; k++) {
3112 printf ("\n\t\t\t");
3113 printf("%d (0x%08x)",
3114 (int)((INT32 *)prop)[k], (int)((INT32 *)prop)[k]);
3117 if (propinfo->range && propinfo->num_values > 0) {
3120 printf("\trange%s: ",
3121 (propinfo->num_values == 2) ? "" : "s");
3123 for (k = 0; k < propinfo->num_values / 2; k++)
3124 printf(" (%d,%d)", (int)propinfo->values[k * 2],
3125 (int)propinfo->values[k * 2 + 1]);
3129 } else if (actual_type == XA_ATOM &&
3130 actual_format == 32)
3132 printf("\t%s:", XGetAtomName (dpy, props[j]));
3133 for (k = 0; k < nitems; k++) {
3134 if (k > 0 && (k & 1) == 0)
3136 printf("\t%s", XGetAtomName (dpy, ((Atom *)prop)[k]));
3139 if (!propinfo->range && propinfo->num_values > 0) {
3140 printf("\n\t\tsupported:");
3142 for (k = 0; k < propinfo->num_values; k++)
3144 printf(" %-12.12s", XGetAtomName (dpy,
3145 propinfo->values[k]));
3146 if (k % 4 == 3 && k < propinfo->num_values - 1)
3151 } else if (actual_format == 8) {
3152 printf ("\t%s: %s%s\n", XGetAtomName (dpy, props[j]),
3153 prop, bytes_after ? "..." : "");
3155 char *type = actual_type ? XGetAtomName (dpy, actual_type) : "none";
3156 printf ("\t%s: %s(%d) (format %d items %d) ????\n",
3157 XGetAtomName (dpy, props[j]),
3158 type, (int)actual_type, actual_format, (int)nitems);
3167 for (j = 0; j < output_info->nmode; j++)
3169 XRRModeInfo *mode = find_mode_by_xid (output_info->modes[j]);
3172 printf (" %s (0x%x) %6.1fMHz",
3173 mode->name, (int)mode->id,
3174 (double)mode->dotClock / 1000000.0);
3175 for (f = 0; mode_flags[f].flag; f++)
3176 if (mode->modeFlags & mode_flags[f].flag)
3177 printf (" %s", mode_flags[f].string);
3178 if (mode == output->mode_info)
3179 printf (" *current");
3180 if (j < output_info->npreferred)
3181 printf (" +preferred");
3183 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
3184 mode->width, mode->hSyncStart, mode->hSyncEnd,
3185 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
3186 printf (" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
3187 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
3188 mode_refresh (mode));
3189 mode->modeFlags |= ModeShown;
3194 mode_shown = calloc (output_info->nmode, sizeof (Bool));
3195 if (!mode_shown) fatal ("out of memory\n");
3196 for (j = 0; j < output_info->nmode; j++)
3198 XRRModeInfo *jmode, *kmode;
3200 if (mode_shown[j]) continue;
3202 jmode = find_mode_by_xid (output_info->modes[j]);
3204 printf (" %-12s", jmode->name);
3205 for (k = j; k < output_info->nmode; k++)
3207 if (mode_shown[k]) continue;
3208 kmode = find_mode_by_xid (output_info->modes[k]);
3209 if (strcmp (jmode->name, kmode->name) != 0) continue;
3210 mode_shown[k] = True;
3211 kmode->modeFlags |= ModeShown;
3212 printf (" %6.1f", mode_refresh (kmode));
3213 if (kmode == output->mode_info)
3217 if (k < output_info->npreferred)
3227 for (m = 0; m < res->nmode; m++)
3229 XRRModeInfo *mode = &res->modes[m];
3231 if (!(mode->modeFlags & ModeShown))
3233 printf (" %s (0x%x) %6.1fMHz\n",
3234 mode->name, (int)mode->id,
3235 (double)mode->dotClock / 1000000.0);
3236 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
3237 mode->width, mode->hSyncStart, mode->hSyncEnd,
3238 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
3239 printf (" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
3240 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
3241 mode_refresh (mode));
3247 sc = XRRGetScreenInfo (dpy, root);
3252 current_size = XRRConfigCurrentConfiguration (sc, ¤t_rotation);
3254 sizes = XRRConfigSizes(sc, &nsize);
3256 if (have_pixel_size) {
3257 for (size = 0; size < nsize; size++)
3259 if (sizes[size].width == width && sizes[size].height == height)
3262 if (size >= nsize) {
3264 "Size %dx%d not found in available modes\n", width, height);
3269 size = current_size;
3270 else if (size >= nsize) {
3272 "Size index %d is too large, there are only %d sizes\n",
3279 for (rot = 0; rot < 4; rot++)
3280 if (1 << rot == (current_rotation & 0xf))
3284 current_rate = XRRConfigCurrentRate (sc);
3288 if (size == current_size)
3289 rate = current_rate;
3295 rates = XRRConfigRates (sc, size, &nrate);
3296 for (i = 0; i < nrate; i++)
3297 if (rate == rates[i])
3300 fprintf (stderr, "Rate %.1f Hz not available for this size\n", rate);
3306 int major_version, minor_version;
3307 XRRQueryVersion (dpy, &major_version, &minor_version);
3308 printf("Server reports RandR version %d.%d\n",
3309 major_version, minor_version);
3312 if (query || query_1) {
3313 printf(" SZ: Pixels Physical Refresh\n");
3314 for (i = 0; i < nsize; i++) {
3315 printf ("%c%-2d %5d x %-5d (%4dmm x%4dmm )",
3316 i == current_size ? '*' : ' ',
3317 i, sizes[i].width, sizes[i].height,
3318 sizes[i].mwidth, sizes[i].mheight);
3319 rates = XRRConfigRates (sc, i, &nrate);
3320 if (nrate) printf (" ");
3321 for (j = 0; j < nrate; j++)
3323 i == current_size && rates[j] == current_rate ? '*' : ' ',
3329 rotations = XRRConfigRotations(sc, ¤t_rotation);
3331 rotation = 1 << rot ;
3333 printf("Current rotation - %s\n",
3334 rotation_name (current_rotation));
3336 printf("Current reflection - %s\n",
3337 reflection_name (current_rotation));
3339 printf ("Rotations possible - ");
3340 for (i = 0; i < 4; i ++) {
3341 if ((rotations >> i) & 1) printf("%s ", direction[i]);
3345 printf ("Reflections possible - ");
3346 if (rotations & (RR_Reflect_X|RR_Reflect_Y))
3348 if (rotations & RR_Reflect_X) printf ("X Axis ");
3349 if (rotations & RR_Reflect_Y) printf ("Y Axis");
3357 printf("Setting size to %d, rotation to %s\n", size, direction[rot]);
3359 printf ("Setting reflection on ");
3362 if (reflection & RR_Reflect_X) printf ("X Axis ");
3363 if (reflection & RR_Reflect_Y) printf ("Y Axis");
3366 printf ("neither axis");
3369 if (reflection & RR_Reflect_X) printf("Setting reflection on X axis\n");
3371 if (reflection & RR_Reflect_Y) printf("Setting reflection on Y axis\n");
3374 /* we should test configureNotify on the root window */
3375 XSelectInput (dpy, root, StructureNotifyMask);
3377 if (setit && !dryrun) XRRSelectInput (dpy, root,
3378 RRScreenChangeNotifyMask);
3379 if (setit && !dryrun) status = XRRSetScreenConfigAndRate (dpy, sc,
3381 (SizeID) size, (Rotation) (rotation | reflection), rate, CurrentTime);
3383 if (setit && !dryrun && status == RRSetConfigFailed) {
3384 printf ("Failed to change the screen configuration!\n");
3388 if (verbose && setit && !dryrun && size != current_size) {
3389 if (status == RRSetConfigSuccess)
3391 Bool seen_screen = False;
3392 while (!seen_screen) {
3394 XNextEvent(dpy, (XEvent *) &event);
3396 printf ("Event received, type = %d\n", event.type);
3397 /* update Xlib's knowledge of the event */
3398 XRRUpdateConfiguration (&event);
3399 if (event.type == ConfigureNotify)
3400 printf("Received ConfigureNotify Event!\n");
3402 switch (event.type - event_base) {
3403 case RRScreenChangeNotify:
3404 sce = (XRRScreenChangeNotifyEvent *) &event;
3406 printf("Got a screen change notify event!\n");
3407 printf(" window = %d\n root = %d\n size_index = %d\n rotation %d\n",
3408 (int) sce->window, (int) sce->root,
3409 sce->size_index, sce->rotation);
3410 printf(" timestamp = %ld, config_timestamp = %ld\n",
3411 sce->timestamp, sce->config_timestamp);
3412 printf(" Rotation = %x\n", sce->rotation);
3413 printf(" %d X %d pixels, %d X %d mm\n",
3414 sce->width, sce->height, sce->mwidth, sce->mheight);
3415 printf("Display width %d, height %d\n",
3416 DisplayWidth(dpy, screen), DisplayHeight(dpy, screen));
3417 printf("Display widthmm %d, heightmm %d\n",
3418 DisplayWidthMM(dpy, screen), DisplayHeightMM(dpy, screen));
3419 spo = sce->subpixel_order;
3420 if ((spo < 0) || (spo > 5))
3421 printf ("Unknown subpixel order, value = %d\n", spo);
3422 else printf ("new Subpixel rendering model is %s\n", order[spo]);
3426 if (event.type != ConfigureNotify)
3427 printf("unknown event received, type = %d!\n", event.type);
3432 XRRFreeScreenConfigInfo(sc);