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 },
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, " --dryrun\n");
109 fprintf(stderr, " --nograb\n");
110 fprintf(stderr, " --prop or --properties\n");
111 fprintf(stderr, " --fb <width>x<height>\n");
112 fprintf(stderr, " --fbmm <width>x<height>\n");
113 fprintf(stderr, " --dpi <dpi>/<output>\n");
114 fprintf(stderr, " --output <output>\n");
115 fprintf(stderr, " --auto\n");
116 fprintf(stderr, " --mode <mode>\n");
117 fprintf(stderr, " --preferred\n");
118 fprintf(stderr, " --pos <x>x<y>\n");
119 fprintf(stderr, " --rate <rate> or --refresh <rate>\n");
120 fprintf(stderr, " --reflect normal,x,y,xy\n");
121 fprintf(stderr, " --rotate normal,inverted,left,right\n");
122 fprintf(stderr, " --left-of <output>\n");
123 fprintf(stderr, " --right-of <output>\n");
124 fprintf(stderr, " --above <output>\n");
125 fprintf(stderr, " --below <output>\n");
126 fprintf(stderr, " --same-as <output>\n");
127 fprintf(stderr, " --set <property> <value>\n");
128 fprintf(stderr, " --scale <x>x<y>\n");
129 fprintf(stderr, " --transform <a>,<b>,<c>,<d>,<e>,<f>,<g>,<h>,<i>\n");
130 fprintf(stderr, " --off\n");
131 fprintf(stderr, " --crtc <crtc>\n");
132 fprintf(stderr, " --panning <w>x<h>[+<x>+<y>[/<track:w>x<h>+<x>+<y>[/<border:l>/<t>/<r>/<b>]]]\n");
133 fprintf(stderr, " --gamma <r>:<g>:<b>\n");
134 fprintf(stderr, " --primary\n");
135 fprintf(stderr, " --noprimary\n");
136 fprintf(stderr, " --newmode <name> <clock MHz>\n");
137 fprintf(stderr, " <hdisp> <hsync-start> <hsync-end> <htotal>\n");
138 fprintf(stderr, " <vdisp> <vsync-start> <vsync-end> <vtotal>\n");
139 fprintf(stderr, " [+HSync] [-HSync] [+VSync] [-VSync]\n");
140 fprintf(stderr, " --rmmode <name>\n");
141 fprintf(stderr, " --addmode <output> <name>\n");
142 fprintf(stderr, " --delmode <output> <name>\n");
149 fatal (const char *format, ...)
153 va_start (ap, format);
154 fprintf (stderr, "%s: ", program_name);
155 vfprintf (stderr, format, ap);
162 warning (const char *format, ...)
166 va_start (ap, format);
167 fprintf (stderr, "%s: ", program_name);
168 vfprintf (stderr, format, ap);
172 /* Because fmin requires C99 suppport */
173 static inline double dmin (double x, double y)
175 return x < y ? x : y;
179 rotation_name (Rotation rotation)
183 if ((rotation & 0xf) == 0)
185 for (i = 0; i < 4; i++)
186 if (rotation & (1 << i))
188 return "invalid rotation";
192 reflection_name (Rotation rotation)
194 rotation &= (RR_Reflect_X|RR_Reflect_Y);
202 case RR_Reflect_X|RR_Reflect_Y:
203 return "X and Y axis";
205 return "invalid reflection";
208 typedef enum _relation {
217 int x, y, width, height;
228 typedef enum _changes {
230 changes_crtc = (1 << 0),
231 changes_mode = (1 << 1),
232 changes_relation = (1 << 2),
233 changes_position = (1 << 3),
234 changes_rotation = (1 << 4),
235 changes_reflection = (1 << 5),
236 changes_automatic = (1 << 6),
237 changes_refresh = (1 << 7),
238 changes_property = (1 << 8),
239 changes_transform = (1 << 9),
240 changes_panning = (1 << 10),
241 changes_gamma = (1 << 11),
242 changes_primary = (1 << 12),
245 typedef enum _name_kind {
247 name_string = (1 << 0),
249 name_index = (1 << 2),
250 name_preferred = (1 << 3),
260 typedef struct _crtc crtc_t;
261 typedef struct _output output_t;
262 typedef struct _transform transform_t;
263 typedef struct _umode umode_t;
264 typedef struct _output_prop output_prop_t;
267 XTransform transform;
276 XRRCrtcInfo *crtc_info;
278 XRRModeInfo *mode_info;
279 XRRPanning *panning_info;
285 transform_t current_transform, pending_transform;
288 struct _output_prop {
289 struct _output_prop *next;
295 struct _output *next;
299 output_prop_t *props;
302 XRROutputInfo *output_info;
306 crtc_t *current_crtc_info;
310 XRRModeInfo *mode_info;
323 transform_t transform;
338 typedef enum _umode_action {
339 umode_create, umode_destroy, umode_add, umode_delete
346 umode_action_t action;
352 static char *connection[3] = {
355 "unknown connection"};
357 #define OUTPUT_NAME 1
361 #define CRTC_INDEX 0x40000000
370 static output_t *outputs = NULL;
371 static output_t **outputs_tail = &outputs;
372 static crtc_t *crtcs;
373 static umode_t *umodes;
374 static int num_crtcs;
375 static XRRScreenResources *res;
376 static int fb_width = 0, fb_height = 0;
377 static int fb_width_mm = 0, fb_height_mm = 0;
378 static double dpi = 0;
379 static char *dpi_output = NULL;
380 static Bool dryrun = False;
381 static int minWidth, maxWidth, minHeight, maxHeight;
382 static Bool has_1_2 = False;
383 static Bool has_1_3 = False;
386 mode_height (XRRModeInfo *mode_info, Rotation rotation)
388 switch (rotation & 0xf) {
391 return mode_info->height;
394 return mode_info->width;
401 mode_width (XRRModeInfo *mode_info, Rotation rotation)
403 switch (rotation & 0xf) {
406 return mode_info->width;
409 return mode_info->height;
416 transform_point (XTransform *transform, double *xp, double *yp)
426 for (j = 0; j < 3; j++)
429 for (i = 0; i < 3; i++)
430 v += (XFixedToDouble (transform->matrix[j][i]) * vector[i]);
431 if (v > 32767 || v < -32767)
437 for (j = 0; j < 2; j++)
438 vector[j] = result[j] / result[2];
445 path_bounds (XTransform *transform, point_t *points, int npoints, box_t *box)
450 for (i = 0; i < npoints; i++) {
454 transform_point (transform, &x, &y);
455 point.x1 = floor (x);
456 point.y1 = floor (y);
462 if (point.x1 < box->x1) box->x1 = point.x1;
463 if (point.y1 < box->y1) box->y1 = point.y1;
464 if (point.x2 > box->x2) box->x2 = point.x2;
465 if (point.y2 > box->y2) box->y2 = point.y2;
471 mode_geometry (XRRModeInfo *mode_info, Rotation rotation,
472 XTransform *transform,
476 int width = mode_width (mode_info, rotation);
477 int height = mode_height (mode_info, rotation);
487 path_bounds (transform, rect, 4, bounds);
490 /* v refresh frequency in Hz */
492 mode_refresh (XRRModeInfo *mode_info)
496 if (mode_info->hTotal && mode_info->vTotal)
497 rate = ((double) mode_info->dotClock /
498 ((double) mode_info->hTotal * (double) mode_info->vTotal));
504 /* h sync frequency in Hz */
506 mode_hsync (XRRModeInfo *mode_info)
510 if (mode_info->hTotal)
511 rate = (double) mode_info->dotClock / (double) mode_info->hTotal;
518 init_name (name_t *name)
520 name->kind = name_none;
524 set_name_string (name_t *name, char *string)
526 name->kind |= name_string;
527 name->string = string;
531 set_name_xid (name_t *name, XID xid)
533 name->kind |= name_xid;
538 set_name_index (name_t *name, int index)
540 name->kind |= name_index;
545 set_name_preferred (name_t *name)
547 name->kind |= name_preferred;
551 set_name_all (name_t *name, name_t *old)
553 if (old->kind & name_xid)
554 name->xid = old->xid;
555 if (old->kind & name_string)
556 name->string = old->string;
557 if (old->kind & name_index)
558 name->index = old->index;
559 name->kind |= old->kind;
563 set_name (name_t *name, char *string, name_kind_t valid)
565 unsigned int xid; /* don't make it XID (which is unsigned long):
566 scanf() takes unsigned int */
569 if ((valid & name_xid) && sscanf (string, "0x%x", &xid) == 1)
570 set_name_xid (name, xid);
571 else if ((valid & name_index) && sscanf (string, "%d", &index) == 1)
572 set_name_index (name, index);
573 else if (valid & name_string)
574 set_name_string (name, string);
580 init_transform (transform_t *transform)
583 memset (&transform->transform, '\0', sizeof (transform->transform));
584 for (x = 0; x < 3; x++)
585 transform->transform.matrix[x][x] = XDoubleToFixed (1.0);
586 transform->filter = "";
587 transform->nparams = 0;
588 transform->params = NULL;
592 set_transform (transform_t *dest,
593 XTransform *transform,
598 dest->transform = *transform;
599 dest->filter = strdup (filter);
600 dest->nparams = nparams;
601 dest->params = malloc (nparams * sizeof (XFixed));
602 memcpy (dest->params, params, nparams * sizeof (XFixed));
606 copy_transform (transform_t *dest, transform_t *src)
608 set_transform (dest, &src->transform,
609 src->filter, src->params, src->nparams);
613 equal_transform (transform_t *a, transform_t *b)
615 if (memcmp (&a->transform, &b->transform, sizeof (XTransform)) != 0)
617 if (strcmp (a->filter, b->filter) != 0)
619 if (a->nparams != b->nparams)
621 if (memcmp (a->params, b->params, a->nparams * sizeof (XFixed)) != 0)
629 output_t *output = calloc (1, sizeof (output_t));
632 fatal ("out of memory\n");
634 output->found = False;
635 output->brightness = 1.0;
636 *outputs_tail = output;
637 outputs_tail = &output->next;
642 find_output (name_t *name)
646 for (output = outputs; output; output = output->next)
648 name_kind_t common = name->kind & output->output.kind;
650 if ((common & name_xid) && name->xid == output->output.xid)
652 if ((common & name_string) && !strcmp (name->string, output->output.string))
654 if ((common & name_index) && name->index == output->output.index)
661 find_output_by_xid (RROutput output)
665 init_name (&output_name);
666 set_name_xid (&output_name, output);
667 return find_output (&output_name);
671 find_output_by_name (char *name)
675 init_name (&output_name);
676 set_name_string (&output_name, name);
677 return find_output (&output_name);
681 find_crtc (name_t *name)
686 for (c = 0; c < num_crtcs; c++)
691 common = name->kind & crtc->crtc.kind;
693 if ((common & name_xid) && name->xid == crtc->crtc.xid)
695 if ((common & name_string) && !strcmp (name->string, crtc->crtc.string))
697 if ((common & name_index) && name->index == crtc->crtc.index)
705 find_crtc_by_xid (RRCrtc crtc)
709 init_name (&crtc_name);
710 set_name_xid (&crtc_name, crtc);
711 return find_crtc (&crtc_name);
715 find_mode (name_t *name, double refresh)
718 XRRModeInfo *best = NULL;
721 for (m = 0; m < res->nmode; m++)
723 XRRModeInfo *mode = &res->modes[m];
724 if ((name->kind & name_xid) && name->xid == mode->id)
729 if ((name->kind & name_string) && !strcmp (name->string, mode->name))
734 dist = fabs (mode_refresh (mode) - refresh);
737 if (!best || dist < bestDist)
749 find_mode_by_xid (RRMode mode)
753 init_name (&mode_name);
754 set_name_xid (&mode_name, mode);
755 return find_mode (&mode_name, 0);
760 find_mode_by_name (char *name)
763 init_name (&mode_name);
764 set_name_string (&mode_name, name);
765 return find_mode (&mode_name, 0);
771 find_mode_for_output (output_t *output, name_t *name)
773 XRROutputInfo *output_info = output->output_info;
775 XRRModeInfo *best = NULL;
778 for (m = 0; m < output_info->nmode; m++)
782 mode = find_mode_by_xid (output_info->modes[m]);
784 if ((name->kind & name_xid) && name->xid == mode->id)
789 if ((name->kind & name_string) && !strcmp (name->string, mode->name))
793 /* Stay away from doublescan modes unless refresh rate is specified. */
794 if (!output->refresh && (mode->modeFlags & RR_DoubleScan))
798 dist = fabs (mode_refresh (mode) - output->refresh);
801 if (!best || dist < bestDist)
812 preferred_mode (output_t *output)
814 XRROutputInfo *output_info = output->output_info;
821 for (m = 0; m < output_info->nmode; m++)
823 XRRModeInfo *mode_info = find_mode_by_xid (output_info->modes[m]);
826 if (m < output_info->npreferred)
828 else if (output_info->mm_height)
829 dist = (1000 * DisplayHeight(dpy, screen) / DisplayHeightMM(dpy, screen) -
830 1000 * mode_info->height / output_info->mm_height);
832 dist = DisplayHeight(dpy, screen) - mode_info->height;
834 if (dist < 0) dist = -dist;
835 if (!best || dist < bestDist)
845 output_can_use_crtc (output_t *output, crtc_t *crtc)
847 XRROutputInfo *output_info = output->output_info;
850 for (c = 0; c < output_info->ncrtc; c++)
851 if (output_info->crtcs[c] == crtc->crtc.xid)
857 output_can_use_mode (output_t *output, XRRModeInfo *mode)
859 XRROutputInfo *output_info = output->output_info;
862 for (m = 0; m < output_info->nmode; m++)
863 if (output_info->modes[m] == mode->id)
869 crtc_can_use_rotation (crtc_t *crtc, Rotation rotation)
871 Rotation rotations = crtc->crtc_info->rotations;
872 Rotation dir = rotation & (RR_Rotate_0|RR_Rotate_90|RR_Rotate_180|RR_Rotate_270);
873 Rotation reflect = rotation & (RR_Reflect_X|RR_Reflect_Y);
874 if (((rotations & dir) != 0) && ((rotations & reflect) == reflect))
881 crtc_can_use_transform (crtc_t *crtc, XTransform *transform)
885 XRRQueryVersion (dpy, &major, &minor);
886 if (major > 1 || (major == 1 && minor >= 3))
893 * Report only rotations that are supported by all crtcs
896 output_rotations (output_t *output)
899 Rotation rotation = RR_Rotate_0;
900 XRROutputInfo *output_info = output->output_info;
903 for (c = 0; c < output_info->ncrtc; c++)
905 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
909 rotation = crtc->crtc_info->rotations;
912 rotation &= crtc->crtc_info->rotations;
919 output_can_use_rotation (output_t *output, Rotation rotation)
921 XRROutputInfo *output_info = output->output_info;
924 /* make sure all of the crtcs can use this rotation.
925 * yes, this is not strictly necessary, but it is
926 * simpler,and we expect most drivers to either
927 * support rotation everywhere or nowhere
929 for (c = 0; c < output_info->ncrtc; c++)
931 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
932 if (crtc && !crtc_can_use_rotation (crtc, rotation))
939 output_is_primary(output_t *output)
942 return XRRGetOutputPrimary(dpy, root) == output->output.xid;
946 /* Returns the index of the last value in an array < 0xffff */
948 find_last_non_clamped(CARD16 array[], int size) {
950 for (i = size - 1; i > 0; i--) {
951 if (array[i] < 0xffff)
958 set_gamma_info(output_t *output)
961 double i1, v1, i2, v2;
962 int size, middle, last_best, last_red, last_green, last_blue;
965 if (!output->crtc_info)
968 size = XRRGetCrtcGammaSize(dpy, output->crtc_info->crtc.xid);
970 warning("Failed to get size of gamma for output %s\n", output->output.string);
974 gamma = XRRGetCrtcGamma(dpy, output->crtc_info->crtc.xid);
976 warning("Failed to get gamma for output %s\n", output->output.string);
981 * Here is a bit tricky because gamma is a whole curve for each
982 * color. So, typically, we need to represent 3 * 256 values as 3 + 1
983 * values. Therefore, we approximate the gamma curve (v) by supposing
984 * it always follows the way we set it: a power function (i^g)
985 * multiplied by a brightness (b).
987 * so g = (ln(v) - ln(b))/ln(i)
988 * and b can be found using two points (v1,i1) and (v2, i2):
989 * b = e^((ln(v2)*ln(i1) - ln(v1)*ln(i2))/ln(i1/i2))
990 * For the best resolution, we select i2 at the highest place not
991 * clamped and i1 at i2/2. Note that if i2 = 1 (as in most normal
992 * cases), then b = v2.
994 last_red = find_last_non_clamped(gamma->red, size);
995 last_green = find_last_non_clamped(gamma->green, size);
996 last_blue = find_last_non_clamped(gamma->blue, size);
997 best_array = gamma->red;
998 last_best = last_red;
999 if (last_green > last_best) {
1000 last_best = last_green;
1001 best_array = gamma->green;
1003 if (last_blue > last_best) {
1004 last_best = last_blue;
1005 best_array = gamma->blue;
1010 middle = last_best / 2;
1011 i1 = (double)(middle + 1) / size;
1012 v1 = (double)(best_array[middle]) / 65535;
1013 i2 = (double)(last_best + 1) / size;
1014 v2 = (double)(best_array[last_best]) / 65535;
1015 if (v2 < 0.0001) { /* The screen is black */
1016 output->brightness = 0;
1017 output->gamma.red = 1;
1018 output->gamma.green = 1;
1019 output->gamma.blue = 1;
1021 if ((last_best + 1) == size)
1022 output->brightness = v2;
1024 output->brightness = exp((log(v2)*log(i1) - log(v1)*log(i2))/log(i1/i2));
1025 output->gamma.red = log((double)(gamma->red[last_red / 2]) / output->brightness
1026 / 65535) / log((double)((last_red / 2) + 1) / size);
1027 output->gamma.green = log((double)(gamma->green[last_green / 2]) / output->brightness
1028 / 65535) / log((double)((last_green / 2) + 1) / size);
1029 output->gamma.blue = log((double)(gamma->blue[last_blue / 2]) / output->brightness
1030 / 65535) / log((double)((last_blue / 2) + 1) / size);
1033 XRRFreeGamma(gamma);
1037 set_output_info (output_t *output, RROutput xid, XRROutputInfo *output_info)
1039 /* sanity check output info */
1040 if (output_info->connection != RR_Disconnected && !output_info->nmode)
1041 warning ("Output %s is not disconnected but has no modes\n",
1044 /* set output name and info */
1045 if (!(output->output.kind & name_xid))
1046 set_name_xid (&output->output, xid);
1047 if (!(output->output.kind & name_string))
1048 set_name_string (&output->output, output_info->name);
1049 output->output_info = output_info;
1051 /* set crtc name and info */
1052 if (!(output->changes & changes_crtc))
1053 set_name_xid (&output->crtc, output_info->crtc);
1055 if (output->crtc.kind == name_xid && output->crtc.xid == None)
1056 output->crtc_info = NULL;
1059 output->crtc_info = find_crtc (&output->crtc);
1060 if (!output->crtc_info)
1062 if (output->crtc.kind & name_xid)
1063 fatal ("cannot find crtc 0x%x\n", output->crtc.xid);
1064 if (output->crtc.kind & name_index)
1065 fatal ("cannot find crtc %d\n", output->crtc.index);
1067 if (!output_can_use_crtc (output, output->crtc_info))
1068 fatal ("output %s cannot use crtc 0x%x\n", output->output.string,
1069 output->crtc_info->crtc.xid);
1072 /* set mode name and info */
1073 if (!(output->changes & changes_mode))
1075 if (output->crtc_info)
1076 set_name_xid (&output->mode, output->crtc_info->crtc_info->mode);
1078 set_name_xid (&output->mode, None);
1079 if (output->mode.xid)
1081 output->mode_info = find_mode_by_xid (output->mode.xid);
1082 if (!output->mode_info)
1083 fatal ("server did not report mode 0x%x for output %s\n",
1084 output->mode.xid, output->output.string);
1087 output->mode_info = NULL;
1089 else if (output->mode.kind == name_xid && output->mode.xid == None)
1090 output->mode_info = NULL;
1093 if (output->mode.kind == name_preferred)
1094 output->mode_info = preferred_mode (output);
1096 output->mode_info = find_mode_for_output (output, &output->mode);
1097 if (!output->mode_info)
1099 if (output->mode.kind & name_preferred)
1100 fatal ("cannot find preferred mode\n");
1101 if (output->mode.kind & name_string)
1102 fatal ("cannot find mode %s\n", output->mode.string);
1103 if (output->mode.kind & name_xid)
1104 fatal ("cannot find mode 0x%x\n", output->mode.xid);
1106 if (!output_can_use_mode (output, output->mode_info))
1107 fatal ("output %s cannot use mode %s\n", output->output.string,
1108 output->mode_info->name);
1112 if (!(output->changes & changes_position))
1114 if (output->crtc_info)
1116 output->x = output->crtc_info->crtc_info->x;
1117 output->y = output->crtc_info->crtc_info->y;
1127 if (!(output->changes & changes_rotation))
1129 output->rotation &= ~0xf;
1130 if (output->crtc_info)
1131 output->rotation |= (output->crtc_info->crtc_info->rotation & 0xf);
1133 output->rotation = RR_Rotate_0;
1135 if (!(output->changes & changes_reflection))
1137 output->rotation &= ~(RR_Reflect_X|RR_Reflect_Y);
1138 if (output->crtc_info)
1139 output->rotation |= (output->crtc_info->crtc_info->rotation &
1140 (RR_Reflect_X|RR_Reflect_Y));
1142 if (!output_can_use_rotation (output, output->rotation))
1143 fatal ("output %s cannot use rotation \"%s\" reflection \"%s\"\n",
1144 output->output.string,
1145 rotation_name (output->rotation),
1146 reflection_name (output->rotation));
1149 if (!(output->changes & changes_gamma))
1150 set_gamma_info(output);
1152 /* set transformation */
1153 if (!(output->changes & changes_transform))
1155 if (output->crtc_info)
1156 copy_transform (&output->transform, &output->crtc_info->current_transform);
1158 init_transform (&output->transform);
1162 if (!(output->changes & changes_primary))
1163 output->primary = output_is_primary(output);
1167 get_screen (Bool current)
1170 fatal ("Server RandR version before 1.2\n");
1172 XRRGetScreenSizeRange (dpy, root, &minWidth, &minHeight,
1173 &maxWidth, &maxHeight);
1176 res = XRRGetScreenResourcesCurrent (dpy, root);
1178 res = XRRGetScreenResources (dpy, root);
1179 if (!res) fatal ("could not get screen resources");
1187 num_crtcs = res->ncrtc;
1188 crtcs = calloc (num_crtcs, sizeof (crtc_t));
1189 if (!crtcs) fatal ("out of memory\n");
1191 for (c = 0; c < res->ncrtc; c++)
1193 XRRCrtcInfo *crtc_info = XRRGetCrtcInfo (dpy, res, res->crtcs[c]);
1194 XRRCrtcTransformAttributes *attr;
1195 XRRPanning *panning_info = NULL;
1199 memset(&zero, 0, sizeof(zero));
1200 panning_info = XRRGetPanning (dpy, res, res->crtcs[c]);
1201 zero.timestamp = panning_info->timestamp;
1202 if (!memcmp(panning_info, &zero, sizeof(zero))) {
1203 Xfree(panning_info);
1204 panning_info = NULL;
1208 set_name_xid (&crtcs[c].crtc, res->crtcs[c]);
1209 set_name_index (&crtcs[c].crtc, c);
1210 if (!crtc_info) fatal ("could not get crtc 0x%x information\n", res->crtcs[c]);
1211 crtcs[c].crtc_info = crtc_info;
1212 crtcs[c].panning_info = panning_info;
1213 if (crtc_info->mode == None)
1215 crtcs[c].mode_info = NULL;
1218 crtcs[c].rotation = RR_Rotate_0;
1220 if (XRRGetCrtcTransform (dpy, res->crtcs[c], &attr) && attr) {
1221 set_transform (&crtcs[c].current_transform,
1222 &attr->currentTransform,
1223 attr->currentFilter,
1224 attr->currentParams,
1225 attr->currentNparams);
1230 init_transform (&crtcs[c].current_transform);
1232 copy_transform (&crtcs[c].pending_transform, &crtcs[c].current_transform);
1237 crtc_add_output (crtc_t *crtc, output_t *output)
1240 crtc->outputs = realloc (crtc->outputs, (crtc->noutput + 1) * sizeof (output_t *));
1243 crtc->outputs = malloc (sizeof (output_t *));
1244 crtc->x = output->x;
1245 crtc->y = output->y;
1246 crtc->rotation = output->rotation;
1247 crtc->mode_info = output->mode_info;
1248 copy_transform (&crtc->pending_transform, &output->transform);
1250 if (!crtc->outputs) fatal ("out of memory\n");
1251 crtc->outputs[crtc->noutput++] = output;
1259 for (output = outputs; output; output = output->next)
1261 if (!output->mode_info) continue;
1262 crtc_add_output (output->crtc_info, output);
1271 for (output = outputs; output; output = output->next)
1273 if (! output->crtc_info)
1275 if (! (output->changes & changes_panning))
1277 if (! output->crtc_info->panning_info)
1278 output->crtc_info->panning_info = malloc (sizeof(XRRPanning));
1279 memcpy (output->crtc_info->panning_info, &output->panning, sizeof(XRRPanning));
1280 output->crtc_info->changing = 1;
1289 for (output = outputs; output; output = output->next) {
1292 XRRCrtcGamma *gamma;
1294 if (!(output->changes & changes_gamma))
1297 if (!output->crtc_info) {
1298 fatal("Need crtc to set gamma on.\n");
1302 crtc = output->crtc_info;
1304 size = XRRGetCrtcGammaSize(dpy, crtc->crtc.xid);
1307 fatal("Gamma size is 0.\n");
1311 gamma = XRRAllocGamma(size);
1313 fatal("Gamma allocation failed.\n");
1317 if(output->gamma.red == 0.0 && output->gamma.green == 0.0 && output->gamma.blue == 0.0)
1318 output->gamma.red = output->gamma.green = output->gamma.blue = 1.0;
1320 for (i = 0; i < size; i++) {
1321 if (output->gamma.red == 1.0 && output->brightness == 1.0)
1322 gamma->red[i] = (i << 8) + i;
1324 gamma->red[i] = dmin(pow((double)i/(double)(size - 1),
1325 output->gamma.red) * output->brightness,
1328 if (output->gamma.green == 1.0 && output->brightness == 1.0)
1329 gamma->green[i] = (i << 8) + i;
1331 gamma->green[i] = dmin(pow((double)i/(double)(size - 1),
1332 output->gamma.green) * output->brightness,
1335 if (output->gamma.blue == 1.0 && output->brightness == 1.0)
1336 gamma->blue[i] = (i << 8) + i;
1338 gamma->blue[i] = dmin(pow((double)i/(double)(size - 1),
1339 output->gamma.blue) * output->brightness,
1343 XRRSetCrtcGamma(dpy, crtc->crtc.xid, gamma);
1355 XRRSetOutputPrimary(dpy, root, None);
1357 for (output = outputs; output; output = output->next) {
1358 if (!(output->changes & changes_primary))
1360 if (output->primary)
1361 XRRSetOutputPrimary(dpy, root, output->output.xid);
1367 crtc_disable (crtc_t *crtc)
1370 printf ("crtc %d: disable\n", crtc->crtc.index);
1373 return RRSetConfigSuccess;
1374 return XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1375 0, 0, None, RR_Rotate_0, NULL, 0);
1379 crtc_set_transform (crtc_t *crtc, transform_t *transform)
1383 XRRQueryVersion (dpy, &major, &minor);
1384 if (major > 1 || (major == 1 && minor >= 3))
1385 XRRSetCrtcTransform (dpy, crtc->crtc.xid,
1386 &transform->transform,
1389 transform->nparams);
1393 crtc_revert (crtc_t *crtc)
1395 XRRCrtcInfo *crtc_info = crtc->crtc_info;
1398 printf ("crtc %d: revert\n", crtc->crtc.index);
1401 return RRSetConfigSuccess;
1403 if (!equal_transform (&crtc->current_transform, &crtc->pending_transform))
1404 crtc_set_transform (crtc, &crtc->current_transform);
1405 return XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1406 crtc_info->x, crtc_info->y,
1407 crtc_info->mode, crtc_info->rotation,
1408 crtc_info->outputs, crtc_info->noutput);
1412 crtc_apply (crtc_t *crtc)
1414 RROutput *rr_outputs;
1419 if (!crtc->changing || !crtc->mode_info)
1420 return RRSetConfigSuccess;
1422 rr_outputs = calloc (crtc->noutput, sizeof (RROutput));
1425 for (o = 0; o < crtc->noutput; o++)
1426 rr_outputs[o] = crtc->outputs[o]->output.xid;
1427 mode = crtc->mode_info->id;
1429 printf ("crtc %d: %12s %6.1f +%d+%d", crtc->crtc.index,
1430 crtc->mode_info->name, mode_refresh (crtc->mode_info),
1432 for (o = 0; o < crtc->noutput; o++)
1433 printf (" \"%s\"", crtc->outputs[o]->output.string);
1438 s = RRSetConfigSuccess;
1441 if (!equal_transform (&crtc->current_transform, &crtc->pending_transform))
1442 crtc_set_transform (crtc, &crtc->pending_transform);
1443 s = XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1444 crtc->x, crtc->y, mode, crtc->rotation,
1445 rr_outputs, crtc->noutput);
1446 if (s == RRSetConfigSuccess && crtc->panning_info) {
1448 s = XRRSetPanning (dpy, res, crtc->crtc.xid, crtc->panning_info);
1450 fatal ("panning needs RandR 1.3\n");
1458 screen_revert (void)
1461 printf ("screen %d: revert\n", screen);
1465 XRRSetScreenSize (dpy, root,
1466 DisplayWidth (dpy, screen),
1467 DisplayHeight (dpy, screen),
1468 DisplayWidthMM (dpy, screen),
1469 DisplayHeightMM (dpy, screen));
1475 if (fb_width == DisplayWidth (dpy, screen) &&
1476 fb_height == DisplayHeight (dpy, screen) &&
1477 fb_width_mm == DisplayWidthMM (dpy, screen) &&
1478 fb_height_mm == DisplayHeightMM (dpy, screen))
1483 printf ("screen %d: %dx%d %dx%d mm %6.2fdpi\n", screen,
1484 fb_width, fb_height, fb_width_mm, fb_height_mm, dpi);
1487 XRRSetScreenSize (dpy, root, fb_width, fb_height,
1488 fb_width_mm, fb_height_mm);
1496 /* first disable all crtcs */
1497 for (c = 0; c < res->ncrtc; c++)
1498 crtc_disable (&crtcs[c]);
1499 /* next reset screen size */
1501 /* now restore all crtcs */
1502 for (c = 0; c < res->ncrtc; c++)
1503 crtc_revert (&crtcs[c]);
1507 * uh-oh, something bad happened in the middle of changing
1508 * the configuration. Revert to the previous configuration
1512 panic (Status s, crtc_t *crtc)
1514 int c = crtc->crtc.index;
1518 case RRSetConfigSuccess: message = "succeeded"; break;
1519 case BadAlloc: message = "out of memory"; break;
1520 case RRSetConfigFailed: message = "failed"; break;
1521 case RRSetConfigInvalidConfigTime: message = "invalid config time"; break;
1522 case RRSetConfigInvalidTime: message = "invalid time"; break;
1523 default: message = "unknown failure"; break;
1526 fprintf (stderr, "%s: Configure crtc %d %s\n", program_name, c, message);
1538 * Hold the server grabbed while messing with
1539 * the screen so that apps which notice the resize
1540 * event and ask for xinerama information from the server
1541 * receive up-to-date information
1547 * Turn off any crtcs which are to be disabled or which are
1548 * larger than the target size
1550 for (c = 0; c < res->ncrtc; c++)
1552 crtc_t *crtc = &crtcs[c];
1553 XRRCrtcInfo *crtc_info = crtc->crtc_info;
1555 /* if this crtc is already disabled, skip it */
1556 if (crtc_info->mode == None)
1560 * If this crtc is to be left enabled, make
1561 * sure the old size fits then new screen
1563 if (crtc->mode_info)
1565 XRRModeInfo *old_mode = find_mode_by_xid (crtc_info->mode);
1570 panic (RRSetConfigFailed, crtc);
1572 /* old position and size information */
1573 mode_geometry (old_mode, crtc_info->rotation,
1574 &crtc->current_transform.transform,
1577 x = crtc_info->x + bounds.x1;
1578 y = crtc_info->y + bounds.y1;
1579 w = bounds.x2 - bounds.x1;
1580 h = bounds.y2 - bounds.y1;
1582 /* if it fits, skip it */
1583 if (x + w <= fb_width && y + h <= fb_height)
1585 crtc->changing = True;
1587 s = crtc_disable (crtc);
1588 if (s != RRSetConfigSuccess)
1593 * Set the screen size
1601 for (c = 0; c < res->ncrtc; c++)
1603 crtc_t *crtc = &crtcs[c];
1605 s = crtc_apply (crtc);
1606 if (s != RRSetConfigSuccess)
1613 * Release the server grab and let all clients
1614 * respond to the updated state
1617 XUngrabServer (dpy);
1621 * Use current output state to complete the output list
1629 for (o = 0; o < res->noutput; o++)
1631 XRROutputInfo *output_info = XRRGetOutputInfo (dpy, res, res->outputs[o]);
1634 if (!output_info) fatal ("could not get output 0x%x information\n", res->outputs[o]);
1635 set_name_xid (&output_name, res->outputs[o]);
1636 set_name_index (&output_name, o);
1637 set_name_string (&output_name, output_info->name);
1638 output = find_output (&output_name);
1641 output = add_output ();
1642 set_name_all (&output->output, &output_name);
1644 * When global --automatic mode is set, turn on connected but off
1645 * outputs, turn off disconnected but on outputs
1649 switch (output_info->connection) {
1651 if (!output_info->crtc) {
1652 output->changes |= changes_automatic;
1653 output->automatic = True;
1656 case RR_Disconnected:
1657 if (output_info->crtc)
1659 output->changes |= changes_automatic;
1660 output->automatic = True;
1666 output->found = True;
1669 * Automatic mode -- track connection state and enable/disable outputs
1672 if (output->automatic)
1674 switch (output_info->connection) {
1676 case RR_UnknownConnection:
1677 if ((!(output->changes & changes_mode)))
1679 set_name_preferred (&output->mode);
1680 output->changes |= changes_mode;
1683 case RR_Disconnected:
1684 if ((!(output->changes & changes_mode)))
1686 set_name_xid (&output->mode, None);
1687 set_name_xid (&output->crtc, None);
1688 output->changes |= changes_mode;
1689 output->changes |= changes_crtc;
1695 set_output_info (output, res->outputs[o], output_info);
1697 for (q = outputs; q; q = q->next)
1701 fprintf(stderr, "warning: output %s not found; ignoring\n",
1708 mark_changing_crtcs (void)
1712 for (c = 0; c < num_crtcs; c++)
1714 crtc_t *crtc = &crtcs[c];
1718 /* walk old output list (to catch disables) */
1719 for (o = 0; o < crtc->crtc_info->noutput; o++)
1721 output = find_output_by_xid (crtc->crtc_info->outputs[o]);
1722 if (!output) fatal ("cannot find output 0x%x\n",
1723 crtc->crtc_info->outputs[o]);
1724 if (output->changes)
1725 crtc->changing = True;
1727 /* walk new output list */
1728 for (o = 0; o < crtc->noutput; o++)
1730 output = crtc->outputs[o];
1731 if (output->changes)
1732 crtc->changing = True;
1738 * Test whether 'crtc' can be used for 'output'
1741 check_crtc_for_output (crtc_t *crtc, output_t *output)
1747 for (c = 0; c < output->output_info->ncrtc; c++)
1748 if (output->output_info->crtcs[c] == crtc->crtc.xid)
1750 if (c == output->output_info->ncrtc)
1752 for (other = outputs; other; other = other->next)
1754 if (other == output)
1757 if (other->mode_info == NULL)
1760 if (other->crtc_info != crtc)
1763 /* see if the output connected to the crtc can clone to this output */
1764 for (l = 0; l < output->output_info->nclone; l++)
1765 if (output->output_info->clones[l] == other->output.xid)
1767 /* not on the list, can't clone */
1768 if (l == output->output_info->nclone)
1774 /* make sure the state matches */
1775 if (crtc->mode_info != output->mode_info)
1777 if (crtc->x != output->x)
1779 if (crtc->y != output->y)
1781 if (crtc->rotation != output->rotation)
1783 if (!equal_transform (&crtc->current_transform, &output->transform))
1786 else if (crtc->crtc_info->noutput)
1788 /* make sure the state matches the already used state */
1789 XRRModeInfo *mode = find_mode_by_xid (crtc->crtc_info->mode);
1791 if (mode != output->mode_info)
1793 if (crtc->crtc_info->x != output->x)
1795 if (crtc->crtc_info->y != output->y)
1797 if (crtc->crtc_info->rotation != output->rotation)
1804 find_crtc_for_output (output_t *output)
1808 for (c = 0; c < output->output_info->ncrtc; c++)
1812 crtc = find_crtc_by_xid (output->output_info->crtcs[c]);
1813 if (!crtc) fatal ("cannot find crtc 0x%x\n", output->output_info->crtcs[c]);
1815 if (check_crtc_for_output (crtc, output))
1822 set_positions (void)
1833 for (output = outputs; output; output = output->next)
1836 name_t relation_name;
1838 if (!(output->changes & changes_relation)) continue;
1840 if (output->mode_info == NULL) continue;
1842 init_name (&relation_name);
1843 set_name_string (&relation_name, output->relative_to);
1844 relation = find_output (&relation_name);
1845 if (!relation) fatal ("cannot find output \"%s\"\n", output->relative_to);
1847 if (relation->mode_info == NULL)
1851 output->changes |= changes_position;
1856 * Make sure the dependent object has been set in place
1858 if ((relation->changes & changes_relation) &&
1859 !(relation->changes & changes_position))
1865 switch (output->relation) {
1866 case relation_left_of:
1867 output->y = relation->y;
1868 output->x = relation->x - mode_width (output->mode_info, output->rotation);
1870 case relation_right_of:
1871 output->y = relation->y;
1872 output->x = relation->x + mode_width (relation->mode_info, relation->rotation);
1874 case relation_above:
1875 output->x = relation->x;
1876 output->y = relation->y - mode_height (output->mode_info, output->rotation);
1878 case relation_below:
1879 output->x = relation->x;
1880 output->y = relation->y + mode_height (relation->mode_info, relation->rotation);
1882 case relation_same_as:
1883 output->x = relation->x;
1884 output->y = relation->y;
1886 output->changes |= changes_position;
1892 fatal ("loop in relative position specifications\n");
1896 * Now normalize positions so the upper left corner of all outputs is at 0,0
1900 for (output = outputs; output; output = output->next)
1902 if (output->mode_info == NULL) continue;
1904 if (output->x < min_x) min_x = output->x;
1905 if (output->y < min_y) min_y = output->y;
1909 /* move all outputs */
1910 for (output = outputs; output; output = output->next)
1912 if (output->mode_info == NULL) continue;
1916 output->changes |= changes_position;
1922 set_screen_size (void)
1925 Bool fb_specified = fb_width != 0 && fb_height != 0;
1927 for (output = outputs; output; output = output->next)
1929 XRRModeInfo *mode_info = output->mode_info;
1933 if (!mode_info) continue;
1935 mode_geometry (mode_info, output->rotation,
1936 &output->transform.transform,
1938 x = output->x + bounds.x1;
1939 y = output->y + bounds.y1;
1940 w = bounds.x2 - bounds.x1;
1941 h = bounds.y2 - bounds.y1;
1942 /* make sure output fits in specified size */
1945 if (x + w > fb_width || y + h > fb_height)
1946 warning ("specified screen %dx%d not large enough for output %s (%dx%d+%d+%d)\n",
1947 fb_width, fb_height, output->output.string, w, h, x, y);
1949 /* fit fb to output */
1953 if (x + w > fb_width)
1955 if (y + h > fb_height)
1957 if (output->changes & changes_panning)
1958 pan = &output->panning;
1960 pan = output->crtc_info ? output->crtc_info->panning_info : NULL;
1961 if (pan && pan->left + pan->width > fb_width)
1962 fb_width = pan->left + pan->width;
1963 if (pan && pan->top + pan->height > fb_height)
1964 fb_height = pan->top + pan->height;
1968 if (fb_width > maxWidth || fb_height > maxHeight)
1969 fatal ("screen cannot be larger than %dx%d (desired size %dx%d)\n",
1970 maxWidth, maxHeight, fb_width, fb_height);
1973 if (fb_width < minWidth || fb_height < minHeight)
1974 fatal ("screen must be at least %dx%d\n", minWidth, minHeight);
1978 if (fb_width < minWidth) fb_width = minWidth;
1979 if (fb_height < minHeight) fb_height = minHeight;
1985 disable_outputs (output_t *outputs)
1989 outputs->crtc_info = NULL;
1990 outputs = outputs->next;
1995 * find the best mapping from output to crtc available
1998 pick_crtcs_score (output_t *outputs)
2011 outputs = outputs->next;
2013 * Score with this output disabled
2015 output->crtc_info = NULL;
2016 best_score = pick_crtcs_score (outputs);
2017 if (output->mode_info == NULL)
2022 * Now score with this output any valid crtc
2024 for (c = 0; c < output->output_info->ncrtc; c++)
2028 crtc = find_crtc_by_xid (output->output_info->crtcs[c]);
2030 fatal ("cannot find crtc 0x%x\n", output->output_info->crtcs[c]);
2032 /* reset crtc allocation for following outputs */
2033 disable_outputs (outputs);
2034 if (!check_crtc_for_output (crtc, output))
2038 /* slight preference for existing connections */
2039 if (crtc == output->current_crtc_info)
2042 output->crtc_info = crtc;
2043 score = my_score + pick_crtcs_score (outputs);
2044 if (score > best_score)
2050 if (output->crtc_info != best_crtc)
2051 output->crtc_info = best_crtc;
2053 * Reset other outputs based on this one using the best crtc
2055 (void) pick_crtcs_score (outputs);
2061 * Pick crtcs for any changing outputs that don't have one
2069 * First try to match up newly enabled outputs with spare crtcs
2071 for (output = outputs; output; output = output->next)
2073 if (output->changes && output->mode_info)
2075 if (output->crtc_info) {
2076 if (output->crtc_info->crtc_info->noutput > 0 &&
2077 (output->crtc_info->crtc_info->noutput > 1 ||
2078 output != find_output_by_xid (output->crtc_info->crtc_info->outputs[0])))
2081 output->crtc_info = find_crtc_for_output (output);
2082 if (!output->crtc_info)
2093 * When the simple way fails, see if there is a way
2094 * to swap crtcs around and make things work
2096 for (output = outputs; output; output = output->next)
2097 output->current_crtc_info = output->crtc_info;
2098 pick_crtcs_score (outputs);
2099 for (output = outputs; output; output = output->next)
2101 if (output->mode_info && !output->crtc_info)
2102 fatal ("cannot find crtc for output %s\n", output->output.string);
2103 if (!output->changes && output->crtc_info != output->current_crtc_info)
2104 output->changes |= changes_crtc;
2109 check_strtol(char *s)
2112 int result = strtol(s, &endptr, 10);
2119 check_strtod(char *s)
2122 double result = strtod(s, &endptr);
2129 main (int argc, char **argv)
2131 XRRScreenSize *sizes;
2132 XRRScreenConfiguration *sc;
2136 Status status = RRSetConfigFailed;
2139 int action_requested = False;
2140 Rotation rotation, current_rotation, rotations;
2142 XRRScreenChangeNotifyEvent *sce;
2143 char *display_name = NULL;
2145 SizeID current_size;
2151 Bool version = False;
2152 int event_base, error_base;
2154 int width = 0, height = 0;
2155 Bool have_pixel_size = False;
2157 output_t *output = NULL;
2158 Bool setit_1_2 = False;
2159 Bool query_1_2 = False;
2160 Bool modeit = False;
2161 Bool propit = False;
2162 Bool query_1 = False;
2164 Bool current = False;
2166 program_name = argv[0];
2167 for (i = 1; i < argc; i++) {
2168 if (!strcmp ("-display", argv[i]) || !strcmp ("-d", argv[i])) {
2169 if (++i>=argc) usage ();
2170 display_name = argv[i];
2173 if (!strcmp("-help", argv[i])) {
2175 action_requested = True;
2178 if (!strcmp ("--verbose", argv[i])) {
2182 if (!strcmp ("--dryrun", argv[i])) {
2187 if (!strcmp ("--nograb", argv[i])) {
2188 grab_server = False;
2191 if (!strcmp("--current", argv[i])) {
2196 if (!strcmp ("-s", argv[i]) || !strcmp ("--size", argv[i])) {
2197 if (++i>=argc) usage ();
2198 if (sscanf (argv[i], "%dx%d", &width, &height) == 2) {
2199 have_pixel_size = True;
2201 size = check_strtol(argv[i]);
2202 if (size < 0) usage();
2205 action_requested = True;
2209 if (!strcmp ("-r", argv[i]) ||
2210 !strcmp ("--rate", argv[i]) ||
2211 !strcmp ("--refresh", argv[i]))
2213 if (++i>=argc) usage ();
2214 rate = check_strtod(argv[i]);
2218 output->refresh = rate;
2219 output->changes |= changes_refresh;
2222 action_requested = True;
2226 if (!strcmp ("-v", argv[i]) || !strcmp ("--version", argv[i])) {
2228 action_requested = True;
2232 if (!strcmp ("-x", argv[i])) {
2233 reflection |= RR_Reflect_X;
2235 action_requested = True;
2238 if (!strcmp ("-y", argv[i])) {
2239 reflection |= RR_Reflect_Y;
2241 action_requested = True;
2244 if (!strcmp ("--screen", argv[i])) {
2245 if (++i>=argc) usage ();
2246 screen = check_strtol(argv[i]);
2247 if (screen < 0) usage();
2250 if (!strcmp ("-q", argv[i]) || !strcmp ("--query", argv[i])) {
2254 if (!strcmp ("-o", argv[i]) || !strcmp ("--orientation", argv[i])) {
2256 if (++i>=argc) usage ();
2257 dirind = strtol(argv[i], &endptr, 10);
2258 if (argv[i] == endptr) {
2259 for (dirind = 0; dirind < 4; dirind++) {
2260 if (strcmp (direction[dirind], argv[i]) == 0) break;
2262 if ((dirind < 0) || (dirind > 3)) usage();
2266 action_requested = True;
2269 if (!strcmp ("--prop", argv[i]) ||
2270 !strcmp ("--props", argv[i]) ||
2271 !strcmp ("--madprops", argv[i]) ||
2272 !strcmp ("--properties", argv[i]))
2276 action_requested = True;
2279 if (!strcmp ("--output", argv[i])) {
2280 if (++i >= argc) usage();
2282 output = find_output_by_name (argv[i]);
2284 output = add_output ();
2285 set_name (&output->output, argv[i], name_string|name_xid);
2289 action_requested = True;
2292 if (!strcmp ("--crtc", argv[i])) {
2293 if (++i >= argc) usage();
2294 if (!output) usage();
2295 set_name (&output->crtc, argv[i], name_xid|name_index);
2296 output->changes |= changes_crtc;
2299 if (!strcmp ("--mode", argv[i])) {
2300 if (++i >= argc) usage();
2301 if (!output) usage();
2302 set_name (&output->mode, argv[i], name_string|name_xid);
2303 output->changes |= changes_mode;
2306 if (!strcmp ("--preferred", argv[i])) {
2307 if (!output) usage();
2308 set_name_preferred (&output->mode);
2309 output->changes |= changes_mode;
2312 if (!strcmp ("--pos", argv[i])) {
2313 if (++i>=argc) usage ();
2314 if (!output) usage();
2315 if (sscanf (argv[i], "%dx%d",
2316 &output->x, &output->y) != 2)
2318 output->changes |= changes_position;
2321 if (!strcmp ("--rotation", argv[i]) || !strcmp ("--rotate", argv[i])) {
2322 if (++i>=argc) usage ();
2323 if (!output) usage();
2324 for (dirind = 0; dirind < 4; dirind++) {
2325 if (strcmp (direction[dirind], argv[i]) == 0) break;
2329 output->rotation &= ~0xf;
2330 output->rotation |= 1 << dirind;
2331 output->changes |= changes_rotation;
2334 if (!strcmp ("--reflect", argv[i]) || !strcmp ("--reflection", argv[i])) {
2335 if (++i>=argc) usage ();
2336 if (!output) usage();
2337 for (dirind = 0; dirind < 4; dirind++) {
2338 if (strcmp (reflections[dirind], argv[i]) == 0) break;
2342 output->rotation &= ~(RR_Reflect_X|RR_Reflect_Y);
2343 output->rotation |= dirind * RR_Reflect_X;
2344 output->changes |= changes_reflection;
2347 if (!strcmp ("--left-of", argv[i])) {
2348 if (++i>=argc) usage ();
2349 if (!output) usage();
2350 output->relation = relation_left_of;
2351 output->relative_to = argv[i];
2352 output->changes |= changes_relation;
2355 if (!strcmp ("--right-of", argv[i])) {
2356 if (++i>=argc) usage ();
2357 if (!output) usage();
2358 output->relation = relation_right_of;
2359 output->relative_to = argv[i];
2360 output->changes |= changes_relation;
2363 if (!strcmp ("--above", argv[i])) {
2364 if (++i>=argc) usage ();
2365 if (!output) usage();
2366 output->relation = relation_above;
2367 output->relative_to = argv[i];
2368 output->changes |= changes_relation;
2371 if (!strcmp ("--below", argv[i])) {
2372 if (++i>=argc) usage ();
2373 if (!output) usage();
2374 output->relation = relation_below;
2375 output->relative_to = argv[i];
2376 output->changes |= changes_relation;
2379 if (!strcmp ("--same-as", argv[i])) {
2380 if (++i>=argc) usage ();
2381 if (!output) usage();
2382 output->relation = relation_same_as;
2383 output->relative_to = argv[i];
2384 output->changes |= changes_relation;
2387 if (!strcmp ("--panning", argv[i])) {
2389 if (++i>=argc) usage ();
2390 if (!output) usage();
2391 pan = &output->panning;
2392 switch (sscanf (argv[i], "%dx%d+%d+%d/%dx%d+%d+%d/%d/%d/%d/%d",
2393 &pan->width, &pan->height, &pan->left, &pan->top,
2394 &pan->track_width, &pan->track_height,
2395 &pan->track_left, &pan->track_top,
2396 &pan->border_left, &pan->border_top,
2397 &pan->border_right, &pan->border_bottom)) {
2399 pan->left = pan->top = 0;
2402 pan->track_left = pan->track_top =
2403 pan->track_width = pan->track_height = 0;
2406 pan->border_left = pan->border_top =
2407 pan->border_right = pan->border_bottom = 0;
2414 output->changes |= changes_panning;
2417 if (!strcmp ("--gamma", argv[i])) {
2418 if (!output) usage();
2419 if (++i>=argc) usage ();
2420 if (sscanf(argv[i], "%f:%f:%f", &output->gamma.red,
2421 &output->gamma.green, &output->gamma.blue) != 3)
2423 output->changes |= changes_gamma;
2427 if (!strcmp ("--brightness", argv[i])) {
2428 if (!output) usage();
2429 if (++i>=argc) usage();
2430 if (sscanf(argv[i], "%f", &output->brightness) != 1)
2432 output->changes |= changes_gamma;
2436 if (!strcmp ("--primary", argv[i])) {
2437 if (!output) usage();
2438 output->changes |= changes_primary;
2439 output->primary = True;
2443 if (!strcmp ("--noprimary", argv[i])) {
2448 if (!strcmp ("--set", argv[i])) {
2449 output_prop_t *prop;
2450 if (!output) usage();
2451 prop = malloc (sizeof (output_prop_t));
2452 prop->next = output->props;
2453 output->props = prop;
2454 if (++i>=argc) usage ();
2455 prop->name = argv[i];
2456 if (++i>=argc) usage ();
2457 prop->value = argv[i];
2459 output->changes |= changes_property;
2463 if (!strcmp ("--scale", argv[i]))
2466 if (!output) usage();
2467 if (++i>=argc) usage();
2468 if (sscanf (argv[i], "%lfx%lf", &sx, &sy) != 2)
2470 init_transform (&output->transform);
2471 output->transform.transform.matrix[0][0] = XDoubleToFixed (sx);
2472 output->transform.transform.matrix[1][1] = XDoubleToFixed (sy);
2473 output->transform.transform.matrix[2][2] = XDoubleToFixed (1.0);
2474 if (sx != 1 || sy != 1)
2475 output->transform.filter = "bilinear";
2477 output->transform.filter = "nearest";
2478 output->transform.nparams = 0;
2479 output->transform.params = NULL;
2480 output->changes |= changes_transform;
2483 if (!strcmp ("--transform", argv[i])) {
2484 double transform[3][3];
2486 if (!output) usage();
2487 if (++i>=argc) usage ();
2488 init_transform (&output->transform);
2489 if (strcmp (argv[i], "none") != 0)
2491 if (sscanf(argv[i], "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf",
2492 &transform[0][0],&transform[0][1],&transform[0][2],
2493 &transform[1][0],&transform[1][1],&transform[1][2],
2494 &transform[2][0],&transform[2][1],&transform[2][2])
2497 init_transform (&output->transform);
2498 for (k = 0; k < 3; k++)
2499 for (l = 0; l < 3; l++) {
2500 output->transform.transform.matrix[k][l] = XDoubleToFixed (transform[k][l]);
2502 output->transform.filter = "bilinear";
2503 output->transform.nparams = 0;
2504 output->transform.params = NULL;
2506 output->changes |= changes_transform;
2509 if (!strcmp ("--off", argv[i])) {
2510 if (!output) usage();
2511 set_name_xid (&output->mode, None);
2512 set_name_xid (&output->crtc, None);
2513 output->changes |= changes_mode;
2516 if (!strcmp ("--fb", argv[i])) {
2517 if (++i>=argc) usage ();
2518 if (sscanf (argv[i], "%dx%d",
2519 &fb_width, &fb_height) != 2)
2522 action_requested = True;
2525 if (!strcmp ("--fbmm", argv[i])) {
2526 if (++i>=argc) usage ();
2527 if (sscanf (argv[i], "%dx%d",
2528 &fb_width_mm, &fb_height_mm) != 2)
2531 action_requested = True;
2534 if (!strcmp ("--dpi", argv[i])) {
2536 if (++i>=argc) usage ();
2537 dpi = strtod(argv[i], &strtod_error);
2538 if (argv[i] == strtod_error)
2541 dpi_output = argv[i];
2544 action_requested = True;
2547 if (!strcmp ("--auto", argv[i])) {
2550 output->automatic = True;
2551 output->changes |= changes_automatic;
2556 action_requested = True;
2559 if (!strcmp ("--q12", argv[i]))
2564 if (!strcmp ("--q1", argv[i]))
2569 if (!strcmp ("--newmode", argv[i]))
2571 umode_t *m = malloc (sizeof (umode_t));
2575 if (i + 9 >= argc) usage ();
2576 m->mode.name = argv[i];
2577 m->mode.nameLength = strlen (argv[i]);
2579 clock = check_strtod(argv[i++]);
2580 m->mode.dotClock = clock * 1e6;
2582 m->mode.width = check_strtol(argv[i++]);
2583 m->mode.hSyncStart = check_strtol(argv[i++]);
2584 m->mode.hSyncEnd = check_strtol(argv[i++]);
2585 m->mode.hTotal = check_strtol(argv[i++]);
2586 m->mode.height = check_strtol(argv[i++]);
2587 m->mode.vSyncStart = check_strtol(argv[i++]);
2588 m->mode.vSyncEnd = check_strtol(argv[i++]);
2589 m->mode.vTotal = check_strtol(argv[i++]);
2590 m->mode.modeFlags = 0;
2594 for (f = 0; mode_flags[f].string; f++)
2595 if (!strcasecmp (mode_flags[f].string, argv[i]))
2598 if (!mode_flags[f].string)
2600 m->mode.modeFlags |= mode_flags[f].flag;
2604 m->action = umode_create;
2607 action_requested = True;
2610 if (!strcmp ("--rmmode", argv[i]))
2612 umode_t *m = malloc (sizeof (umode_t));
2614 if (++i>=argc) usage ();
2615 set_name (&m->name, argv[i], name_string|name_xid);
2616 m->action = umode_destroy;
2620 action_requested = True;
2623 if (!strcmp ("--addmode", argv[i]))
2625 umode_t *m = malloc (sizeof (umode_t));
2627 if (++i>=argc) usage ();
2628 set_name (&m->output, argv[i], name_string|name_xid);
2629 if (++i>=argc) usage();
2630 set_name (&m->name, argv[i], name_string|name_xid);
2631 m->action = umode_add;
2635 action_requested = True;
2638 if (!strcmp ("--delmode", argv[i]))
2640 umode_t *m = malloc (sizeof (umode_t));
2642 if (++i>=argc) usage ();
2643 set_name (&m->output, argv[i], name_string|name_xid);
2644 if (++i>=argc) usage();
2645 set_name (&m->name, argv[i], name_string|name_xid);
2646 m->action = umode_delete;
2650 action_requested = True;
2655 if (!action_requested)
2660 if (setit && !setit_1_2)
2664 printf("xrandr program version " VERSION "\n");
2666 dpy = XOpenDisplay (display_name);
2669 fprintf (stderr, "Can't open display %s\n", XDisplayName(display_name));
2673 screen = DefaultScreen (dpy);
2674 if (screen >= ScreenCount (dpy)) {
2675 fprintf (stderr, "Invalid screen number %d (display has %d)\n",
2676 screen, ScreenCount (dpy));
2680 root = RootWindow (dpy, screen);
2682 if (!XRRQueryExtension (dpy, &event_base, &error_base) ||
2683 !XRRQueryVersion (dpy, &major, &minor))
2685 fprintf (stderr, "RandR extension missing\n");
2688 if (major > 1 || (major == 1 && minor >= 2))
2690 if (major > 1 || (major == 1 && minor >= 3))
2693 if (has_1_2 && modeit)
2697 get_screen (current);
2701 for (m = umodes; m; m = m->next)
2706 switch (m->action) {
2708 XRRCreateMode (dpy, root, &m->mode);
2711 e = find_mode (&m->name, 0);
2713 fatal ("cannot find mode \"%s\"\n", m->name.string);
2714 XRRDestroyMode (dpy, e->id);
2717 o = find_output (&m->output);
2719 fatal ("cannot find output \"%s\"\n", m->output.string);
2720 e = find_mode (&m->name, 0);
2722 fatal ("cannot find mode \"%s\"\n", m->name.string);
2723 XRRAddOutputMode (dpy, o->output.xid, e->id);
2726 o = find_output (&m->output);
2728 fatal ("cannot find output \"%s\"\n", m->output.string);
2729 e = find_mode (&m->name, 0);
2731 fatal ("cannot find mode \"%s\"\n", m->name.string);
2732 XRRDeleteOutputMode (dpy, o->output.xid, e->id);
2742 if (has_1_2 && propit)
2745 get_screen (current);
2749 for (output = outputs; output; output = output->next)
2751 output_prop_t *prop;
2753 for (prop = output->props; prop; prop = prop->next)
2755 Atom name = XInternAtom (dpy, prop->name, False);
2758 unsigned char *data;
2761 unsigned long ulong_value;
2762 unsigned char *prop_data;
2764 unsigned long nitems, bytes_after;
2766 XRRPropertyInfo *propinfo;
2768 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;
2801 else if ((type == XA_STRING || type == AnyPropertyType))
2804 data = (unsigned char *) prop->value;
2805 nelements = strlen (prop->value);
2810 XRRChangeOutputProperty (dpy, output->output.xid,
2811 name, type, format, PropModeReplace,
2823 get_screen (current);
2832 * Assign outputs to crtcs
2837 * Mark changing crtcs
2839 mark_changing_crtcs ();
2842 * If an output was specified to track dpi, use it
2846 output_t *output = find_output_by_name (dpi_output);
2847 XRROutputInfo *output_info;
2848 XRRModeInfo *mode_info;
2850 fatal ("Cannot find output %s\n", dpi_output);
2851 output_info = output->output_info;
2852 mode_info = output->mode_info;
2853 if (output_info && mode_info && output_info->mm_height)
2856 * When this output covers the whole screen, just use
2857 * the known physical size
2859 if (fb_width == mode_info->width &&
2860 fb_height == mode_info->height)
2862 fb_width_mm = output_info->mm_width;
2863 fb_height_mm = output_info->mm_height;
2867 dpi = (25.4 * mode_info->height) / output_info->mm_height;
2873 * Compute physical screen size
2875 if (fb_width_mm == 0 || fb_height_mm == 0)
2877 if (fb_width != DisplayWidth (dpy, screen) ||
2878 fb_height != DisplayHeight (dpy, screen) || dpi != 0.0)
2881 dpi = (25.4 * DisplayHeight (dpy, screen)) / DisplayHeightMM(dpy, screen);
2883 fb_width_mm = (25.4 * fb_width) / dpi;
2884 fb_height_mm = (25.4 * fb_height) / dpi;
2888 fb_width_mm = DisplayWidthMM (dpy, screen);
2889 fb_height_mm = DisplayHeightMM (dpy, screen);
2899 * Set gamma on crtc's that belong to the outputs.
2904 * Now apply all of the changes
2911 if (query_1_2 || (query && has_1_2 && !query_1))
2916 #define ModeShown 0x80000000
2918 get_screen (current);
2922 printf ("Screen %d: minimum %d x %d, current %d x %d, maximum %d x %d\n",
2923 screen, minWidth, minHeight,
2924 DisplayWidth (dpy, screen), DisplayHeight(dpy, screen),
2925 maxWidth, maxHeight);
2927 for (output = outputs; output; output = output->next)
2929 XRROutputInfo *output_info = output->output_info;
2930 crtc_t *crtc = output->crtc_info;
2931 XRRCrtcInfo *crtc_info = crtc ? crtc->crtc_info : NULL;
2932 XRRModeInfo *mode = output->mode_info;
2936 Rotation rotations = output_rotations (output);
2938 printf ("%s %s", output_info->name, connection[output_info->connection]);
2942 printf (" %dx%d+%d+%d",
2943 crtc_info->width, crtc_info->height,
2944 crtc_info->x, crtc_info->y);
2946 printf (" %dx%d+%d+%d",
2947 mode->width, mode->height, output->x, output->y);
2950 printf (" (0x%x)", (int)mode->id);
2951 if (output->rotation != RR_Rotate_0 || verbose)
2954 rotation_name (output->rotation));
2955 if (output->rotation & (RR_Reflect_X|RR_Reflect_Y))
2956 printf (" %s", reflection_name (output->rotation));
2959 if (rotations != RR_Rotate_0 || verbose)
2963 for (i = 0; i < 4; i ++) {
2964 if ((rotations >> i) & 1) {
2965 if (!first) printf (" "); first = False;
2966 printf("%s", direction[i]);
2970 if (rotations & RR_Reflect_X)
2972 if (!first) printf (" "); first = False;
2975 if (rotations & RR_Reflect_Y)
2977 if (!first) printf (" "); first = False;
2985 printf (" %dmm x %dmm",
2986 (int)output_info->mm_width, (int)output_info->mm_height);
2989 if (crtc && crtc->panning_info && crtc->panning_info->width > 0)
2991 XRRPanning *pan = crtc->panning_info;
2992 printf (" panning %dx%d+%d+%d",
2993 pan->width, pan->height, pan->left, pan->top);
2994 if ((pan->track_width != 0 &&
2995 (pan->track_left != pan->left ||
2996 pan->track_width != pan->width ||
2997 pan->border_left != 0 ||
2998 pan->border_right != 0)) ||
2999 (pan->track_height != 0 &&
3000 (pan->track_top != pan->top ||
3001 pan->track_height != pan->height ||
3002 pan->border_top != 0 ||
3003 pan->border_bottom != 0)))
3004 printf (" tracking %dx%d+%d+%d border %d/%d/%d/%d",
3005 pan->track_width, pan->track_height,
3006 pan->track_left, pan->track_top,
3007 pan->border_left, pan->border_top,
3008 pan->border_right, pan->border_bottom);
3014 printf ("\tIdentifier: 0x%x\n", (int)output->output.xid);
3015 printf ("\tTimestamp: %d\n", (int)output_info->timestamp);
3016 printf ("\tSubpixel: %s\n", order[output_info->subpixel_order]);
3017 if (output->gamma.red != 0.0 && output->gamma.green != 0.0 && output->gamma.blue != 0.0) {
3018 printf ("\tGamma: %#.2g:%#.2g:%#.2g\n",
3019 output->gamma.red, output->gamma.green, output->gamma.blue);
3020 printf ("\tBrightness: %#.2g\n", output->brightness);
3022 printf ("\tClones: ");
3023 for (j = 0; j < output_info->nclone; j++)
3025 output_t *clone = find_output_by_xid (output_info->clones[j]);
3027 if (clone) printf (" %s", clone->output.string);
3030 if (output->crtc_info)
3031 printf ("\tCRTC: %d\n", output->crtc_info->crtc.index);
3032 printf ("\tCRTCs: ");
3033 for (j = 0; j < output_info->ncrtc; j++)
3035 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[j]);
3037 printf (" %d", crtc->crtc.index);
3040 if (output->crtc_info && output->crtc_info->panning_info) {
3041 XRRPanning *pan = output->crtc_info->panning_info;
3042 printf ("\tPanning: %dx%d+%d+%d\n",
3043 pan->width, pan->height, pan->left, pan->top);
3044 printf ("\tTracking: %dx%d+%d+%d\n",
3045 pan->track_width, pan->track_height,
3046 pan->track_left, pan->track_top);
3047 printf ("\tBorder: %d/%d/%d/%d\n",
3048 pan->border_left, pan->border_top,
3049 pan->border_right, pan->border_bottom);
3056 printf ("\tTransform: ");
3057 for (y = 0; y < 3; y++)
3059 for (x = 0; x < 3; x++)
3060 printf (" %f", XFixedToDouble (output->transform.transform.matrix[y][x]));
3064 if (output->transform.filter)
3065 printf ("\n\t filter: %s", output->transform.filter);
3068 if (verbose || properties)
3070 props = XRRListOutputProperties (dpy, output->output.xid,
3072 for (j = 0; j < nprop; j++) {
3073 unsigned char *prop;
3075 unsigned long nitems, bytes_after;
3077 XRRPropertyInfo *propinfo;
3079 XRRGetOutputProperty (dpy, output->output.xid, props[j],
3080 0, 100, False, False,
3082 &actual_type, &actual_format,
3083 &nitems, &bytes_after, &prop);
3085 propinfo = XRRQueryOutputProperty(dpy, output->output.xid,
3088 if (actual_type == XA_INTEGER && actual_format == 8) {
3091 printf("\t%s:\n", XGetAtomName (dpy, props[j]));
3092 for (k = 0; k < nitems; k++) {
3095 printf("%02x", (unsigned char)prop[k]);
3099 } else if (actual_type == XA_INTEGER &&
3100 actual_format == 32)
3102 printf("\t%s: ", XGetAtomName (dpy, props[j]));
3103 for (k = 0; k < nitems; k++) {
3105 printf ("\n\t\t\t");
3106 printf("%d (0x%08x)",
3107 (int)((INT32 *)prop)[k], (int)((INT32 *)prop)[k]);
3110 if (propinfo->range && propinfo->num_values > 0) {
3113 printf("\trange%s: ",
3114 (propinfo->num_values == 2) ? "" : "s");
3116 for (k = 0; k < propinfo->num_values / 2; k++)
3117 printf(" (%d,%d)", (int)propinfo->values[k * 2],
3118 (int)propinfo->values[k * 2 + 1]);
3122 } else if (actual_type == XA_ATOM &&
3123 actual_format == 32)
3125 printf("\t%s:", XGetAtomName (dpy, props[j]));
3126 for (k = 0; k < nitems; k++) {
3127 if (k > 0 && (k & 1) == 0)
3129 printf("\t%s", XGetAtomName (dpy, ((Atom *)prop)[k]));
3132 if (!propinfo->range && propinfo->num_values > 0) {
3133 printf("\n\t\tsupported:");
3135 for (k = 0; k < propinfo->num_values; k++)
3137 printf(" %-12.12s", XGetAtomName (dpy,
3138 propinfo->values[k]));
3139 if (k % 4 == 3 && k < propinfo->num_values - 1)
3144 } else if (actual_format == 8) {
3145 printf ("\t%s: %s%s\n", XGetAtomName (dpy, props[j]),
3146 prop, bytes_after ? "..." : "");
3148 char *type = actual_type ? XGetAtomName (dpy, actual_type) : "none";
3149 printf ("\t%s: %s(%d) (format %d items %d) ????\n",
3150 XGetAtomName (dpy, props[j]),
3151 type, (int)actual_type, actual_format, (int)nitems);
3160 for (j = 0; j < output_info->nmode; j++)
3162 XRRModeInfo *mode = find_mode_by_xid (output_info->modes[j]);
3165 printf (" %s (0x%x) %6.1fMHz",
3166 mode->name, (int)mode->id,
3167 (double)mode->dotClock / 1000000.0);
3168 for (f = 0; mode_flags[f].flag; f++)
3169 if (mode->modeFlags & mode_flags[f].flag)
3170 printf (" %s", mode_flags[f].string);
3171 if (mode == output->mode_info)
3172 printf (" *current");
3173 if (j < output_info->npreferred)
3174 printf (" +preferred");
3176 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
3177 mode->width, mode->hSyncStart, mode->hSyncEnd,
3178 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
3179 printf (" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
3180 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
3181 mode_refresh (mode));
3182 mode->modeFlags |= ModeShown;
3187 mode_shown = calloc (output_info->nmode, sizeof (Bool));
3188 if (!mode_shown) fatal ("out of memory\n");
3189 for (j = 0; j < output_info->nmode; j++)
3191 XRRModeInfo *jmode, *kmode;
3193 if (mode_shown[j]) continue;
3195 jmode = find_mode_by_xid (output_info->modes[j]);
3197 printf (" %-12s", jmode->name);
3198 for (k = j; k < output_info->nmode; k++)
3200 if (mode_shown[k]) continue;
3201 kmode = find_mode_by_xid (output_info->modes[k]);
3202 if (strcmp (jmode->name, kmode->name) != 0) continue;
3203 mode_shown[k] = True;
3204 kmode->modeFlags |= ModeShown;
3205 printf (" %6.1f", mode_refresh (kmode));
3206 if (kmode == output->mode_info)
3210 if (k < output_info->npreferred)
3220 for (m = 0; m < res->nmode; m++)
3222 XRRModeInfo *mode = &res->modes[m];
3224 if (!(mode->modeFlags & ModeShown))
3226 printf (" %s (0x%x) %6.1fMHz\n",
3227 mode->name, (int)mode->id,
3228 (double)mode->dotClock / 1000000.0);
3229 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
3230 mode->width, mode->hSyncStart, mode->hSyncEnd,
3231 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
3232 printf (" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
3233 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
3234 mode_refresh (mode));
3240 sc = XRRGetScreenInfo (dpy, root);
3245 current_size = XRRConfigCurrentConfiguration (sc, ¤t_rotation);
3247 sizes = XRRConfigSizes(sc, &nsize);
3249 if (have_pixel_size) {
3250 for (size = 0; size < nsize; size++)
3252 if (sizes[size].width == width && sizes[size].height == height)
3255 if (size >= nsize) {
3257 "Size %dx%d not found in available modes\n", width, height);
3262 size = current_size;
3263 else if (size >= nsize) {
3265 "Size index %d is too large, there are only %d sizes\n",
3272 for (rot = 0; rot < 4; rot++)
3273 if (1 << rot == (current_rotation & 0xf))
3277 current_rate = XRRConfigCurrentRate (sc);
3281 if (size == current_size)
3282 rate = current_rate;
3288 rates = XRRConfigRates (sc, size, &nrate);
3289 for (i = 0; i < nrate; i++)
3290 if (rate == rates[i])
3293 fprintf (stderr, "Rate %.1f Hz not available for this size\n", rate);
3299 int major_version, minor_version;
3300 XRRQueryVersion (dpy, &major_version, &minor_version);
3301 printf("Server reports RandR version %d.%d\n",
3302 major_version, minor_version);
3305 if (query || query_1) {
3306 printf(" SZ: Pixels Physical Refresh\n");
3307 for (i = 0; i < nsize; i++) {
3308 printf ("%c%-2d %5d x %-5d (%4dmm x%4dmm )",
3309 i == current_size ? '*' : ' ',
3310 i, sizes[i].width, sizes[i].height,
3311 sizes[i].mwidth, sizes[i].mheight);
3312 rates = XRRConfigRates (sc, i, &nrate);
3313 if (nrate) printf (" ");
3314 for (j = 0; j < nrate; j++)
3316 i == current_size && rates[j] == current_rate ? '*' : ' ',
3322 rotations = XRRConfigRotations(sc, ¤t_rotation);
3324 rotation = 1 << rot ;
3326 printf("Current rotation - %s\n",
3327 rotation_name (current_rotation));
3329 printf("Current reflection - %s\n",
3330 reflection_name (current_rotation));
3332 printf ("Rotations possible - ");
3333 for (i = 0; i < 4; i ++) {
3334 if ((rotations >> i) & 1) printf("%s ", direction[i]);
3338 printf ("Reflections possible - ");
3339 if (rotations & (RR_Reflect_X|RR_Reflect_Y))
3341 if (rotations & RR_Reflect_X) printf ("X Axis ");
3342 if (rotations & RR_Reflect_Y) printf ("Y Axis");
3350 printf("Setting size to %d, rotation to %s\n", size, direction[rot]);
3352 printf ("Setting reflection on ");
3355 if (reflection & RR_Reflect_X) printf ("X Axis ");
3356 if (reflection & RR_Reflect_Y) printf ("Y Axis");
3359 printf ("neither axis");
3362 if (reflection & RR_Reflect_X) printf("Setting reflection on X axis\n");
3364 if (reflection & RR_Reflect_Y) printf("Setting reflection on Y axis\n");
3367 /* we should test configureNotify on the root window */
3368 XSelectInput (dpy, root, StructureNotifyMask);
3370 if (setit && !dryrun) XRRSelectInput (dpy, root,
3371 RRScreenChangeNotifyMask);
3372 if (setit && !dryrun) status = XRRSetScreenConfigAndRate (dpy, sc,
3374 (SizeID) size, (Rotation) (rotation | reflection), rate, CurrentTime);
3376 if (setit && !dryrun && status == RRSetConfigFailed) {
3377 printf ("Failed to change the screen configuration!\n");
3381 if (verbose && setit && !dryrun && size != current_size) {
3382 if (status == RRSetConfigSuccess)
3384 Bool seen_screen = False;
3385 while (!seen_screen) {
3387 XNextEvent(dpy, (XEvent *) &event);
3389 printf ("Event received, type = %d\n", event.type);
3390 /* update Xlib's knowledge of the event */
3391 XRRUpdateConfiguration (&event);
3392 if (event.type == ConfigureNotify)
3393 printf("Received ConfigureNotify Event!\n");
3395 switch (event.type - event_base) {
3396 case RRScreenChangeNotify:
3397 sce = (XRRScreenChangeNotifyEvent *) &event;
3399 printf("Got a screen change notify event!\n");
3400 printf(" window = %d\n root = %d\n size_index = %d\n rotation %d\n",
3401 (int) sce->window, (int) sce->root,
3402 sce->size_index, sce->rotation);
3403 printf(" timestamp = %ld, config_timestamp = %ld\n",
3404 sce->timestamp, sce->config_timestamp);
3405 printf(" Rotation = %x\n", sce->rotation);
3406 printf(" %d X %d pixels, %d X %d mm\n",
3407 sce->width, sce->height, sce->mwidth, sce->mheight);
3408 printf("Display width %d, height %d\n",
3409 DisplayWidth(dpy, screen), DisplayHeight(dpy, screen));
3410 printf("Display widthmm %d, heightmm %d\n",
3411 DisplayWidthMM(dpy, screen), DisplayHeightMM(dpy, screen));
3412 spo = sce->subpixel_order;
3413 if ((spo < 0) || (spo > 5))
3414 printf ("Unknown subpixel order, value = %d\n", spo);
3415 else printf ("new Subpixel rendering model is %s\n", order[spo]);
3419 if (event.type != ConfigureNotify)
3420 printf("unknown event received, type = %d!\n", event.type);
3425 XRRFreeScreenConfigInfo(sc);