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);
173 rotation_name (Rotation rotation)
177 if ((rotation & 0xf) == 0)
179 for (i = 0; i < 4; i++)
180 if (rotation & (1 << i))
182 return "invalid rotation";
186 reflection_name (Rotation rotation)
188 rotation &= (RR_Reflect_X|RR_Reflect_Y);
196 case RR_Reflect_X|RR_Reflect_Y:
197 return "X and Y axis";
199 return "invalid reflection";
202 typedef enum _relation {
211 int x, y, width, height;
222 typedef enum _changes {
224 changes_crtc = (1 << 0),
225 changes_mode = (1 << 1),
226 changes_relation = (1 << 2),
227 changes_position = (1 << 3),
228 changes_rotation = (1 << 4),
229 changes_reflection = (1 << 5),
230 changes_automatic = (1 << 6),
231 changes_refresh = (1 << 7),
232 changes_property = (1 << 8),
233 changes_transform = (1 << 9),
234 changes_panning = (1 << 10),
235 changes_gamma = (1 << 11),
236 changes_primary = (1 << 12),
239 typedef enum _name_kind {
241 name_string = (1 << 0),
243 name_index = (1 << 2),
244 name_preferred = (1 << 3),
254 typedef struct _crtc crtc_t;
255 typedef struct _output output_t;
256 typedef struct _transform transform_t;
257 typedef struct _umode umode_t;
258 typedef struct _output_prop output_prop_t;
261 XTransform transform;
270 XRRCrtcInfo *crtc_info;
272 XRRModeInfo *mode_info;
273 XRRPanning *panning_info;
279 transform_t current_transform, pending_transform;
282 struct _output_prop {
283 struct _output_prop *next;
289 struct _output *next;
293 output_prop_t *props;
296 XRROutputInfo *output_info;
300 crtc_t *current_crtc_info;
304 XRRModeInfo *mode_info;
317 transform_t transform;
332 typedef enum _umode_action {
333 umode_create, umode_destroy, umode_add, umode_delete
340 umode_action_t action;
346 static char *connection[3] = {
349 "unknown connection"};
351 #define OUTPUT_NAME 1
355 #define CRTC_INDEX 0x40000000
364 static output_t *outputs = NULL;
365 static output_t **outputs_tail = &outputs;
366 static crtc_t *crtcs;
367 static umode_t *umodes;
368 static int num_crtcs;
369 static XRRScreenResources *res;
370 static int fb_width = 0, fb_height = 0;
371 static int fb_width_mm = 0, fb_height_mm = 0;
372 static double dpi = 0;
373 static char *dpi_output = NULL;
374 static Bool dryrun = False;
375 static int minWidth, maxWidth, minHeight, maxHeight;
376 static Bool has_1_2 = False;
377 static Bool has_1_3 = False;
380 mode_height (XRRModeInfo *mode_info, Rotation rotation)
382 switch (rotation & 0xf) {
385 return mode_info->height;
388 return mode_info->width;
395 mode_width (XRRModeInfo *mode_info, Rotation rotation)
397 switch (rotation & 0xf) {
400 return mode_info->width;
403 return mode_info->height;
410 transform_point (XTransform *transform, double *xp, double *yp)
420 for (j = 0; j < 3; j++)
423 for (i = 0; i < 3; i++)
424 v += (XFixedToDouble (transform->matrix[j][i]) * vector[i]);
425 if (v > 32767 || v < -32767)
431 for (j = 0; j < 2; j++)
432 vector[j] = result[j] / result[2];
439 path_bounds (XTransform *transform, point_t *points, int npoints, box_t *box)
444 for (i = 0; i < npoints; i++) {
448 transform_point (transform, &x, &y);
449 point.x1 = floor (x);
450 point.y1 = floor (y);
456 if (point.x1 < box->x1) box->x1 = point.x1;
457 if (point.y1 < box->y1) box->y1 = point.y1;
458 if (point.x2 > box->x2) box->x2 = point.x2;
459 if (point.y2 > box->y2) box->y2 = point.y2;
465 mode_geometry (XRRModeInfo *mode_info, Rotation rotation,
466 XTransform *transform,
470 int width = mode_width (mode_info, rotation);
471 int height = mode_height (mode_info, rotation);
481 path_bounds (transform, rect, 4, bounds);
484 /* v refresh frequency in Hz */
486 mode_refresh (XRRModeInfo *mode_info)
490 if (mode_info->hTotal && mode_info->vTotal)
491 rate = ((double) mode_info->dotClock /
492 ((double) mode_info->hTotal * (double) mode_info->vTotal));
498 /* h sync frequency in Hz */
500 mode_hsync (XRRModeInfo *mode_info)
504 if (mode_info->hTotal)
505 rate = (double) mode_info->dotClock / (double) mode_info->hTotal;
512 init_name (name_t *name)
514 name->kind = name_none;
518 set_name_string (name_t *name, char *string)
520 name->kind |= name_string;
521 name->string = string;
525 set_name_xid (name_t *name, XID xid)
527 name->kind |= name_xid;
532 set_name_index (name_t *name, int index)
534 name->kind |= name_index;
539 set_name_preferred (name_t *name)
541 name->kind |= name_preferred;
545 set_name_all (name_t *name, name_t *old)
547 if (old->kind & name_xid)
548 name->xid = old->xid;
549 if (old->kind & name_string)
550 name->string = old->string;
551 if (old->kind & name_index)
552 name->index = old->index;
553 name->kind |= old->kind;
557 set_name (name_t *name, char *string, name_kind_t valid)
559 unsigned int xid; /* don't make it XID (which is unsigned long):
560 scanf() takes unsigned int */
563 if ((valid & name_xid) && sscanf (string, "0x%x", &xid) == 1)
564 set_name_xid (name, xid);
565 else if ((valid & name_index) && sscanf (string, "%d", &index) == 1)
566 set_name_index (name, index);
567 else if (valid & name_string)
568 set_name_string (name, string);
574 init_transform (transform_t *transform)
577 memset (&transform->transform, '\0', sizeof (transform->transform));
578 for (x = 0; x < 3; x++)
579 transform->transform.matrix[x][x] = XDoubleToFixed (1.0);
580 transform->filter = "";
581 transform->nparams = 0;
582 transform->params = NULL;
586 set_transform (transform_t *dest,
587 XTransform *transform,
592 dest->transform = *transform;
593 dest->filter = strdup (filter);
594 dest->nparams = nparams;
595 dest->params = malloc (nparams * sizeof (XFixed));
596 memcpy (dest->params, params, nparams * sizeof (XFixed));
600 copy_transform (transform_t *dest, transform_t *src)
602 set_transform (dest, &src->transform,
603 src->filter, src->params, src->nparams);
607 equal_transform (transform_t *a, transform_t *b)
609 if (memcmp (&a->transform, &b->transform, sizeof (XTransform)) != 0)
611 if (strcmp (a->filter, b->filter) != 0)
613 if (a->nparams != b->nparams)
615 if (memcmp (a->params, b->params, a->nparams * sizeof (XFixed)) != 0)
623 output_t *output = calloc (1, sizeof (output_t));
626 fatal ("out of memory\n");
628 output->found = False;
629 *outputs_tail = output;
630 outputs_tail = &output->next;
635 find_output (name_t *name)
639 for (output = outputs; output; output = output->next)
641 name_kind_t common = name->kind & output->output.kind;
643 if ((common & name_xid) && name->xid == output->output.xid)
645 if ((common & name_string) && !strcmp (name->string, output->output.string))
647 if ((common & name_index) && name->index == output->output.index)
654 find_output_by_xid (RROutput output)
658 init_name (&output_name);
659 set_name_xid (&output_name, output);
660 return find_output (&output_name);
664 find_output_by_name (char *name)
668 init_name (&output_name);
669 set_name_string (&output_name, name);
670 return find_output (&output_name);
674 find_crtc (name_t *name)
679 for (c = 0; c < num_crtcs; c++)
684 common = name->kind & crtc->crtc.kind;
686 if ((common & name_xid) && name->xid == crtc->crtc.xid)
688 if ((common & name_string) && !strcmp (name->string, crtc->crtc.string))
690 if ((common & name_index) && name->index == crtc->crtc.index)
698 find_crtc_by_xid (RRCrtc crtc)
702 init_name (&crtc_name);
703 set_name_xid (&crtc_name, crtc);
704 return find_crtc (&crtc_name);
708 find_mode (name_t *name, double refresh)
711 XRRModeInfo *best = NULL;
714 for (m = 0; m < res->nmode; m++)
716 XRRModeInfo *mode = &res->modes[m];
717 if ((name->kind & name_xid) && name->xid == mode->id)
722 if ((name->kind & name_string) && !strcmp (name->string, mode->name))
727 dist = fabs (mode_refresh (mode) - refresh);
730 if (!best || dist < bestDist)
742 find_mode_by_xid (RRMode mode)
746 init_name (&mode_name);
747 set_name_xid (&mode_name, mode);
748 return find_mode (&mode_name, 0);
753 find_mode_by_name (char *name)
756 init_name (&mode_name);
757 set_name_string (&mode_name, name);
758 return find_mode (&mode_name, 0);
764 find_mode_for_output (output_t *output, name_t *name)
766 XRROutputInfo *output_info = output->output_info;
768 XRRModeInfo *best = NULL;
771 for (m = 0; m < output_info->nmode; m++)
775 mode = find_mode_by_xid (output_info->modes[m]);
777 if ((name->kind & name_xid) && name->xid == mode->id)
782 if ((name->kind & name_string) && !strcmp (name->string, mode->name))
786 /* Stay away from doublescan modes unless refresh rate is specified. */
787 if (!output->refresh && (mode->modeFlags & RR_DoubleScan))
791 dist = fabs (mode_refresh (mode) - output->refresh);
794 if (!best || dist < bestDist)
805 preferred_mode (output_t *output)
807 XRROutputInfo *output_info = output->output_info;
814 for (m = 0; m < output_info->nmode; m++)
816 XRRModeInfo *mode_info = find_mode_by_xid (output_info->modes[m]);
819 if (m < output_info->npreferred)
821 else if (output_info->mm_height)
822 dist = (1000 * DisplayHeight(dpy, screen) / DisplayHeightMM(dpy, screen) -
823 1000 * mode_info->height / output_info->mm_height);
825 dist = DisplayHeight(dpy, screen) - mode_info->height;
827 if (dist < 0) dist = -dist;
828 if (!best || dist < bestDist)
838 output_can_use_crtc (output_t *output, crtc_t *crtc)
840 XRROutputInfo *output_info = output->output_info;
843 for (c = 0; c < output_info->ncrtc; c++)
844 if (output_info->crtcs[c] == crtc->crtc.xid)
850 output_can_use_mode (output_t *output, XRRModeInfo *mode)
852 XRROutputInfo *output_info = output->output_info;
855 for (m = 0; m < output_info->nmode; m++)
856 if (output_info->modes[m] == mode->id)
862 crtc_can_use_rotation (crtc_t *crtc, Rotation rotation)
864 Rotation rotations = crtc->crtc_info->rotations;
865 Rotation dir = rotation & (RR_Rotate_0|RR_Rotate_90|RR_Rotate_180|RR_Rotate_270);
866 Rotation reflect = rotation & (RR_Reflect_X|RR_Reflect_Y);
867 if (((rotations & dir) != 0) && ((rotations & reflect) == reflect))
874 crtc_can_use_transform (crtc_t *crtc, XTransform *transform)
878 XRRQueryVersion (dpy, &major, &minor);
879 if (major > 1 || (major == 1 && minor >= 3))
886 * Report only rotations that are supported by all crtcs
889 output_rotations (output_t *output)
892 Rotation rotation = RR_Rotate_0;
893 XRROutputInfo *output_info = output->output_info;
896 for (c = 0; c < output_info->ncrtc; c++)
898 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
902 rotation = crtc->crtc_info->rotations;
905 rotation &= crtc->crtc_info->rotations;
912 output_can_use_rotation (output_t *output, Rotation rotation)
914 XRROutputInfo *output_info = output->output_info;
917 /* make sure all of the crtcs can use this rotation.
918 * yes, this is not strictly necessary, but it is
919 * simpler,and we expect most drivers to either
920 * support rotation everywhere or nowhere
922 for (c = 0; c < output_info->ncrtc; c++)
924 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
925 if (crtc && !crtc_can_use_rotation (crtc, rotation))
932 output_is_primary(output_t *output)
935 return XRRGetOutputPrimary(dpy, root) == output->output.xid;
940 set_output_info (output_t *output, RROutput xid, XRROutputInfo *output_info)
942 /* sanity check output info */
943 if (output_info->connection != RR_Disconnected && !output_info->nmode)
944 warning ("Output %s is not disconnected but has no modes\n",
947 /* set output name and info */
948 if (!(output->output.kind & name_xid))
949 set_name_xid (&output->output, xid);
950 if (!(output->output.kind & name_string))
951 set_name_string (&output->output, output_info->name);
952 output->output_info = output_info;
954 /* set crtc name and info */
955 if (!(output->changes & changes_crtc))
956 set_name_xid (&output->crtc, output_info->crtc);
958 if (output->crtc.kind == name_xid && output->crtc.xid == None)
959 output->crtc_info = NULL;
962 output->crtc_info = find_crtc (&output->crtc);
963 if (!output->crtc_info)
965 if (output->crtc.kind & name_xid)
966 fatal ("cannot find crtc 0x%x\n", output->crtc.xid);
967 if (output->crtc.kind & name_index)
968 fatal ("cannot find crtc %d\n", output->crtc.index);
970 if (!output_can_use_crtc (output, output->crtc_info))
971 fatal ("output %s cannot use crtc 0x%x\n", output->output.string,
972 output->crtc_info->crtc.xid);
975 /* set mode name and info */
976 if (!(output->changes & changes_mode))
978 if (output->crtc_info)
979 set_name_xid (&output->mode, output->crtc_info->crtc_info->mode);
981 set_name_xid (&output->mode, None);
982 if (output->mode.xid)
984 output->mode_info = find_mode_by_xid (output->mode.xid);
985 if (!output->mode_info)
986 fatal ("server did not report mode 0x%x for output %s\n",
987 output->mode.xid, output->output.string);
990 output->mode_info = NULL;
992 else if (output->mode.kind == name_xid && output->mode.xid == None)
993 output->mode_info = NULL;
996 if (output->mode.kind == name_preferred)
997 output->mode_info = preferred_mode (output);
999 output->mode_info = find_mode_for_output (output, &output->mode);
1000 if (!output->mode_info)
1002 if (output->mode.kind & name_preferred)
1003 fatal ("cannot find preferred mode\n");
1004 if (output->mode.kind & name_string)
1005 fatal ("cannot find mode %s\n", output->mode.string);
1006 if (output->mode.kind & name_xid)
1007 fatal ("cannot find mode 0x%x\n", output->mode.xid);
1009 if (!output_can_use_mode (output, output->mode_info))
1010 fatal ("output %s cannot use mode %s\n", output->output.string,
1011 output->mode_info->name);
1015 if (!(output->changes & changes_position))
1017 if (output->crtc_info)
1019 output->x = output->crtc_info->crtc_info->x;
1020 output->y = output->crtc_info->crtc_info->y;
1030 if (!(output->changes & changes_rotation))
1032 output->rotation &= ~0xf;
1033 if (output->crtc_info)
1034 output->rotation |= (output->crtc_info->crtc_info->rotation & 0xf);
1036 output->rotation = RR_Rotate_0;
1038 if (!(output->changes & changes_reflection))
1040 output->rotation &= ~(RR_Reflect_X|RR_Reflect_Y);
1041 if (output->crtc_info)
1042 output->rotation |= (output->crtc_info->crtc_info->rotation &
1043 (RR_Reflect_X|RR_Reflect_Y));
1045 if (!output_can_use_rotation (output, output->rotation))
1046 fatal ("output %s cannot use rotation \"%s\" reflection \"%s\"\n",
1047 output->output.string,
1048 rotation_name (output->rotation),
1049 reflection_name (output->rotation));
1051 /* set transformation */
1052 if (!(output->changes & changes_transform))
1054 if (output->crtc_info)
1055 copy_transform (&output->transform, &output->crtc_info->current_transform);
1057 init_transform (&output->transform);
1061 if (!(output->changes & changes_primary))
1062 output->primary = output_is_primary(output);
1066 get_screen (Bool current)
1069 fatal ("Server RandR version before 1.2\n");
1071 XRRGetScreenSizeRange (dpy, root, &minWidth, &minHeight,
1072 &maxWidth, &maxHeight);
1075 res = XRRGetScreenResourcesCurrent (dpy, root);
1077 res = XRRGetScreenResources (dpy, root);
1078 if (!res) fatal ("could not get screen resources");
1086 num_crtcs = res->ncrtc;
1087 crtcs = calloc (num_crtcs, sizeof (crtc_t));
1088 if (!crtcs) fatal ("out of memory\n");
1090 for (c = 0; c < res->ncrtc; c++)
1092 XRRCrtcInfo *crtc_info = XRRGetCrtcInfo (dpy, res, res->crtcs[c]);
1093 XRRCrtcTransformAttributes *attr;
1094 XRRPanning *panning_info = NULL;
1098 memset(&zero, 0, sizeof(zero));
1099 panning_info = XRRGetPanning (dpy, res, res->crtcs[c]);
1100 zero.timestamp = panning_info->timestamp;
1101 if (!memcmp(panning_info, &zero, sizeof(zero))) {
1102 Xfree(panning_info);
1103 panning_info = NULL;
1107 set_name_xid (&crtcs[c].crtc, res->crtcs[c]);
1108 set_name_index (&crtcs[c].crtc, c);
1109 if (!crtc_info) fatal ("could not get crtc 0x%x information\n", res->crtcs[c]);
1110 crtcs[c].crtc_info = crtc_info;
1111 crtcs[c].panning_info = panning_info;
1112 if (crtc_info->mode == None)
1114 crtcs[c].mode_info = NULL;
1117 crtcs[c].rotation = RR_Rotate_0;
1119 if (XRRGetCrtcTransform (dpy, res->crtcs[c], &attr) && attr) {
1120 set_transform (&crtcs[c].current_transform,
1121 &attr->currentTransform,
1122 attr->currentFilter,
1123 attr->currentParams,
1124 attr->currentNparams);
1129 init_transform (&crtcs[c].current_transform);
1131 copy_transform (&crtcs[c].pending_transform, &crtcs[c].current_transform);
1136 crtc_add_output (crtc_t *crtc, output_t *output)
1139 crtc->outputs = realloc (crtc->outputs, (crtc->noutput + 1) * sizeof (output_t *));
1142 crtc->outputs = malloc (sizeof (output_t *));
1143 crtc->x = output->x;
1144 crtc->y = output->y;
1145 crtc->rotation = output->rotation;
1146 crtc->mode_info = output->mode_info;
1147 copy_transform (&crtc->pending_transform, &output->transform);
1149 if (!crtc->outputs) fatal ("out of memory\n");
1150 crtc->outputs[crtc->noutput++] = output;
1158 for (output = outputs; output; output = output->next)
1160 if (!output->mode_info) continue;
1161 crtc_add_output (output->crtc_info, output);
1170 for (output = outputs; output; output = output->next)
1172 if (! output->crtc_info)
1174 if (! (output->changes & changes_panning))
1176 if (! output->crtc_info->panning_info)
1177 output->crtc_info->panning_info = malloc (sizeof(XRRPanning));
1178 memcpy (output->crtc_info->panning_info, &output->panning, sizeof(XRRPanning));
1179 output->crtc_info->changing = 1;
1188 for (output = outputs; output; output = output->next) {
1191 XRRCrtcGamma *gamma;
1193 if (!(output->changes & changes_gamma))
1196 if (!output->crtc_info) {
1197 fatal("Need crtc to set gamma on.\n");
1201 crtc = output->crtc_info;
1203 size = XRRGetCrtcGammaSize(dpy, crtc->crtc.xid);
1206 fatal("Gamma size is 0.\n");
1210 gamma = XRRAllocGamma(size);
1212 fatal("Gamma allocation failed.\n");
1216 if(output->gamma.red == 0.0 && output->gamma.green == 0.0 && output->gamma.blue == 0.0)
1217 output->gamma.red = output->gamma.green = output->gamma.blue = 1.0;
1219 if (output->brightness == 0.0)
1220 output->brightness = 1.0;
1222 for (i = 0; i < size; i++) {
1223 if (output->gamma.red == 1.0 && output->brightness == 1.0)
1224 gamma->red[i] = i << 8;
1226 gamma->red[i] = (CARD16)(pow((double)i/(double)(size - 1),
1227 (double)output->gamma.red) * (double)(size - 1)
1228 * (double)output->brightness * 256);
1230 if (output->gamma.green == 1.0 && output->brightness == 1.0)
1231 gamma->green[i] = i << 8;
1233 gamma->green[i] = (CARD16)(pow((double)i/(double)(size - 1),
1234 (double)output->gamma.green) * (double)(size - 1)
1235 * (double)output->brightness * 256);
1237 if (output->gamma.blue == 1.0 && output->brightness == 1.0)
1238 gamma->blue[i] = i << 8;
1240 gamma->blue[i] = (CARD16)(pow((double)i/(double)(size - 1),
1241 (double)output->gamma.blue) * (double)(size - 1)
1242 * (double)output->brightness * 256);
1245 XRRSetCrtcGamma(dpy, crtc->crtc.xid, gamma);
1257 XRRSetOutputPrimary(dpy, root, None);
1259 for (output = outputs; output; output = output->next) {
1260 if (!(output->changes & changes_primary))
1262 if (output->primary)
1263 XRRSetOutputPrimary(dpy, root, output->output.xid);
1269 crtc_disable (crtc_t *crtc)
1272 printf ("crtc %d: disable\n", crtc->crtc.index);
1275 return RRSetConfigSuccess;
1276 return XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1277 0, 0, None, RR_Rotate_0, NULL, 0);
1281 crtc_set_transform (crtc_t *crtc, transform_t *transform)
1285 XRRQueryVersion (dpy, &major, &minor);
1286 if (major > 1 || (major == 1 && minor >= 3))
1287 XRRSetCrtcTransform (dpy, crtc->crtc.xid,
1288 &transform->transform,
1291 transform->nparams);
1295 crtc_revert (crtc_t *crtc)
1297 XRRCrtcInfo *crtc_info = crtc->crtc_info;
1300 printf ("crtc %d: revert\n", crtc->crtc.index);
1303 return RRSetConfigSuccess;
1305 if (!equal_transform (&crtc->current_transform, &crtc->pending_transform))
1306 crtc_set_transform (crtc, &crtc->current_transform);
1307 return XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1308 crtc_info->x, crtc_info->y,
1309 crtc_info->mode, crtc_info->rotation,
1310 crtc_info->outputs, crtc_info->noutput);
1314 crtc_apply (crtc_t *crtc)
1316 RROutput *rr_outputs;
1321 if (!crtc->changing || !crtc->mode_info)
1322 return RRSetConfigSuccess;
1324 rr_outputs = calloc (crtc->noutput, sizeof (RROutput));
1327 for (o = 0; o < crtc->noutput; o++)
1328 rr_outputs[o] = crtc->outputs[o]->output.xid;
1329 mode = crtc->mode_info->id;
1331 printf ("crtc %d: %12s %6.1f +%d+%d", crtc->crtc.index,
1332 crtc->mode_info->name, mode_refresh (crtc->mode_info),
1334 for (o = 0; o < crtc->noutput; o++)
1335 printf (" \"%s\"", crtc->outputs[o]->output.string);
1340 s = RRSetConfigSuccess;
1343 if (!equal_transform (&crtc->current_transform, &crtc->pending_transform))
1344 crtc_set_transform (crtc, &crtc->pending_transform);
1345 s = XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1346 crtc->x, crtc->y, mode, crtc->rotation,
1347 rr_outputs, crtc->noutput);
1348 if (s == RRSetConfigSuccess && crtc->panning_info) {
1350 s = XRRSetPanning (dpy, res, crtc->crtc.xid, crtc->panning_info);
1352 fatal ("panning needs RandR 1.3\n");
1360 screen_revert (void)
1363 printf ("screen %d: revert\n", screen);
1367 XRRSetScreenSize (dpy, root,
1368 DisplayWidth (dpy, screen),
1369 DisplayHeight (dpy, screen),
1370 DisplayWidthMM (dpy, screen),
1371 DisplayHeightMM (dpy, screen));
1377 if (fb_width == DisplayWidth (dpy, screen) &&
1378 fb_height == DisplayHeight (dpy, screen) &&
1379 fb_width_mm == DisplayWidthMM (dpy, screen) &&
1380 fb_height_mm == DisplayHeightMM (dpy, screen))
1385 printf ("screen %d: %dx%d %dx%d mm %6.2fdpi\n", screen,
1386 fb_width, fb_height, fb_width_mm, fb_height_mm, dpi);
1389 XRRSetScreenSize (dpy, root, fb_width, fb_height,
1390 fb_width_mm, fb_height_mm);
1398 /* first disable all crtcs */
1399 for (c = 0; c < res->ncrtc; c++)
1400 crtc_disable (&crtcs[c]);
1401 /* next reset screen size */
1403 /* now restore all crtcs */
1404 for (c = 0; c < res->ncrtc; c++)
1405 crtc_revert (&crtcs[c]);
1409 * uh-oh, something bad happened in the middle of changing
1410 * the configuration. Revert to the previous configuration
1414 panic (Status s, crtc_t *crtc)
1416 int c = crtc->crtc.index;
1420 case RRSetConfigSuccess: message = "succeeded"; break;
1421 case BadAlloc: message = "out of memory"; break;
1422 case RRSetConfigFailed: message = "failed"; break;
1423 case RRSetConfigInvalidConfigTime: message = "invalid config time"; break;
1424 case RRSetConfigInvalidTime: message = "invalid time"; break;
1425 default: message = "unknown failure"; break;
1428 fprintf (stderr, "%s: Configure crtc %d %s\n", program_name, c, message);
1440 * Hold the server grabbed while messing with
1441 * the screen so that apps which notice the resize
1442 * event and ask for xinerama information from the server
1443 * receive up-to-date information
1449 * Turn off any crtcs which are to be disabled or which are
1450 * larger than the target size
1452 for (c = 0; c < res->ncrtc; c++)
1454 crtc_t *crtc = &crtcs[c];
1455 XRRCrtcInfo *crtc_info = crtc->crtc_info;
1457 /* if this crtc is already disabled, skip it */
1458 if (crtc_info->mode == None)
1462 * If this crtc is to be left enabled, make
1463 * sure the old size fits then new screen
1465 if (crtc->mode_info)
1467 XRRModeInfo *old_mode = find_mode_by_xid (crtc_info->mode);
1472 panic (RRSetConfigFailed, crtc);
1474 /* old position and size information */
1475 mode_geometry (old_mode, crtc_info->rotation,
1476 &crtc->current_transform.transform,
1479 x = crtc_info->x + bounds.x1;
1480 y = crtc_info->y + bounds.y1;
1481 w = bounds.x2 - bounds.x1;
1482 h = bounds.y2 - bounds.y1;
1484 /* if it fits, skip it */
1485 if (x + w <= fb_width && y + h <= fb_height)
1487 crtc->changing = True;
1489 s = crtc_disable (crtc);
1490 if (s != RRSetConfigSuccess)
1495 * Set the screen size
1503 for (c = 0; c < res->ncrtc; c++)
1505 crtc_t *crtc = &crtcs[c];
1507 s = crtc_apply (crtc);
1508 if (s != RRSetConfigSuccess)
1515 * Release the server grab and let all clients
1516 * respond to the updated state
1519 XUngrabServer (dpy);
1523 * Use current output state to complete the output list
1531 for (o = 0; o < res->noutput; o++)
1533 XRROutputInfo *output_info = XRRGetOutputInfo (dpy, res, res->outputs[o]);
1536 if (!output_info) fatal ("could not get output 0x%x information\n", res->outputs[o]);
1537 set_name_xid (&output_name, res->outputs[o]);
1538 set_name_index (&output_name, o);
1539 set_name_string (&output_name, output_info->name);
1540 output = find_output (&output_name);
1543 output = add_output ();
1544 set_name_all (&output->output, &output_name);
1546 * When global --automatic mode is set, turn on connected but off
1547 * outputs, turn off disconnected but on outputs
1551 switch (output_info->connection) {
1553 if (!output_info->crtc) {
1554 output->changes |= changes_automatic;
1555 output->automatic = True;
1558 case RR_Disconnected:
1559 if (output_info->crtc)
1561 output->changes |= changes_automatic;
1562 output->automatic = True;
1568 output->found = True;
1571 * Automatic mode -- track connection state and enable/disable outputs
1574 if (output->automatic)
1576 switch (output_info->connection) {
1578 case RR_UnknownConnection:
1579 if ((!(output->changes & changes_mode)))
1581 set_name_preferred (&output->mode);
1582 output->changes |= changes_mode;
1585 case RR_Disconnected:
1586 if ((!(output->changes & changes_mode)))
1588 set_name_xid (&output->mode, None);
1589 set_name_xid (&output->crtc, None);
1590 output->changes |= changes_mode;
1591 output->changes |= changes_crtc;
1597 set_output_info (output, res->outputs[o], output_info);
1599 for (q = outputs; q; q = q->next)
1603 fprintf(stderr, "warning: output %s not found; ignoring\n",
1610 mark_changing_crtcs (void)
1614 for (c = 0; c < num_crtcs; c++)
1616 crtc_t *crtc = &crtcs[c];
1620 /* walk old output list (to catch disables) */
1621 for (o = 0; o < crtc->crtc_info->noutput; o++)
1623 output = find_output_by_xid (crtc->crtc_info->outputs[o]);
1624 if (!output) fatal ("cannot find output 0x%x\n",
1625 crtc->crtc_info->outputs[o]);
1626 if (output->changes)
1627 crtc->changing = True;
1629 /* walk new output list */
1630 for (o = 0; o < crtc->noutput; o++)
1632 output = crtc->outputs[o];
1633 if (output->changes)
1634 crtc->changing = True;
1640 * Test whether 'crtc' can be used for 'output'
1643 check_crtc_for_output (crtc_t *crtc, output_t *output)
1649 for (c = 0; c < output->output_info->ncrtc; c++)
1650 if (output->output_info->crtcs[c] == crtc->crtc.xid)
1652 if (c == output->output_info->ncrtc)
1654 for (other = outputs; other; other = other->next)
1656 if (other == output)
1659 if (other->mode_info == NULL)
1662 if (other->crtc_info != crtc)
1665 /* see if the output connected to the crtc can clone to this output */
1666 for (l = 0; l < output->output_info->nclone; l++)
1667 if (output->output_info->clones[l] == other->output.xid)
1669 /* not on the list, can't clone */
1670 if (l == output->output_info->nclone)
1676 /* make sure the state matches */
1677 if (crtc->mode_info != output->mode_info)
1679 if (crtc->x != output->x)
1681 if (crtc->y != output->y)
1683 if (crtc->rotation != output->rotation)
1685 if (!equal_transform (&crtc->current_transform, &output->transform))
1688 else if (crtc->crtc_info->noutput)
1690 /* make sure the state matches the already used state */
1691 XRRModeInfo *mode = find_mode_by_xid (crtc->crtc_info->mode);
1693 if (mode != output->mode_info)
1695 if (crtc->crtc_info->x != output->x)
1697 if (crtc->crtc_info->y != output->y)
1699 if (crtc->crtc_info->rotation != output->rotation)
1706 find_crtc_for_output (output_t *output)
1710 for (c = 0; c < output->output_info->ncrtc; c++)
1714 crtc = find_crtc_by_xid (output->output_info->crtcs[c]);
1715 if (!crtc) fatal ("cannot find crtc 0x%x\n", output->output_info->crtcs[c]);
1717 if (check_crtc_for_output (crtc, output))
1724 set_positions (void)
1735 for (output = outputs; output; output = output->next)
1738 name_t relation_name;
1740 if (!(output->changes & changes_relation)) continue;
1742 if (output->mode_info == NULL) continue;
1744 init_name (&relation_name);
1745 set_name_string (&relation_name, output->relative_to);
1746 relation = find_output (&relation_name);
1747 if (!relation) fatal ("cannot find output \"%s\"\n", output->relative_to);
1749 if (relation->mode_info == NULL)
1753 output->changes |= changes_position;
1758 * Make sure the dependent object has been set in place
1760 if ((relation->changes & changes_relation) &&
1761 !(relation->changes & changes_position))
1767 switch (output->relation) {
1768 case relation_left_of:
1769 output->y = relation->y;
1770 output->x = relation->x - mode_width (output->mode_info, output->rotation);
1772 case relation_right_of:
1773 output->y = relation->y;
1774 output->x = relation->x + mode_width (relation->mode_info, relation->rotation);
1776 case relation_above:
1777 output->x = relation->x;
1778 output->y = relation->y - mode_height (output->mode_info, output->rotation);
1780 case relation_below:
1781 output->x = relation->x;
1782 output->y = relation->y + mode_height (relation->mode_info, relation->rotation);
1784 case relation_same_as:
1785 output->x = relation->x;
1786 output->y = relation->y;
1788 output->changes |= changes_position;
1794 fatal ("loop in relative position specifications\n");
1798 * Now normalize positions so the upper left corner of all outputs is at 0,0
1802 for (output = outputs; output; output = output->next)
1804 if (output->mode_info == NULL) continue;
1806 if (output->x < min_x) min_x = output->x;
1807 if (output->y < min_y) min_y = output->y;
1811 /* move all outputs */
1812 for (output = outputs; output; output = output->next)
1814 if (output->mode_info == NULL) continue;
1818 output->changes |= changes_position;
1824 set_screen_size (void)
1827 Bool fb_specified = fb_width != 0 && fb_height != 0;
1829 for (output = outputs; output; output = output->next)
1831 XRRModeInfo *mode_info = output->mode_info;
1835 if (!mode_info) continue;
1837 mode_geometry (mode_info, output->rotation,
1838 &output->transform.transform,
1840 x = output->x + bounds.x1;
1841 y = output->y + bounds.y1;
1842 w = bounds.x2 - bounds.x1;
1843 h = bounds.y2 - bounds.y1;
1844 /* make sure output fits in specified size */
1847 if (x + w > fb_width || y + h > fb_height)
1848 warning ("specified screen %dx%d not large enough for output %s (%dx%d+%d+%d)\n",
1849 fb_width, fb_height, output->output.string, w, h, x, y);
1851 /* fit fb to output */
1855 if (x + w > fb_width)
1857 if (y + h > fb_height)
1859 if (output->changes & changes_panning)
1860 pan = &output->panning;
1862 pan = output->crtc_info ? output->crtc_info->panning_info : NULL;
1863 if (pan && pan->left + pan->width > fb_width)
1864 fb_width = pan->left + pan->width;
1865 if (pan && pan->top + pan->height > fb_height)
1866 fb_height = pan->top + pan->height;
1870 if (fb_width > maxWidth || fb_height > maxHeight)
1871 fatal ("screen cannot be larger than %dx%d (desired size %dx%d)\n",
1872 maxWidth, maxHeight, fb_width, fb_height);
1875 if (fb_width < minWidth || fb_height < minHeight)
1876 fatal ("screen must be at least %dx%d\n", minWidth, minHeight);
1880 if (fb_width < minWidth) fb_width = minWidth;
1881 if (fb_height < minHeight) fb_height = minHeight;
1887 disable_outputs (output_t *outputs)
1891 outputs->crtc_info = NULL;
1892 outputs = outputs->next;
1897 * find the best mapping from output to crtc available
1900 pick_crtcs_score (output_t *outputs)
1913 outputs = outputs->next;
1915 * Score with this output disabled
1917 output->crtc_info = NULL;
1918 best_score = pick_crtcs_score (outputs);
1919 if (output->mode_info == NULL)
1924 * Now score with this output any valid crtc
1926 for (c = 0; c < output->output_info->ncrtc; c++)
1930 crtc = find_crtc_by_xid (output->output_info->crtcs[c]);
1932 fatal ("cannot find crtc 0x%x\n", output->output_info->crtcs[c]);
1934 /* reset crtc allocation for following outputs */
1935 disable_outputs (outputs);
1936 if (!check_crtc_for_output (crtc, output))
1940 /* slight preference for existing connections */
1941 if (crtc == output->current_crtc_info)
1944 output->crtc_info = crtc;
1945 score = my_score + pick_crtcs_score (outputs);
1946 if (score > best_score)
1952 if (output->crtc_info != best_crtc)
1953 output->crtc_info = best_crtc;
1955 * Reset other outputs based on this one using the best crtc
1957 (void) pick_crtcs_score (outputs);
1963 * Pick crtcs for any changing outputs that don't have one
1971 * First try to match up newly enabled outputs with spare crtcs
1973 for (output = outputs; output; output = output->next)
1975 if (output->changes && output->mode_info)
1977 if (output->crtc_info) {
1978 if (output->crtc_info->crtc_info->noutput > 0 &&
1979 (output->crtc_info->crtc_info->noutput > 1 ||
1980 output != find_output_by_xid (output->crtc_info->crtc_info->outputs[0])))
1983 output->crtc_info = find_crtc_for_output (output);
1984 if (!output->crtc_info)
1995 * When the simple way fails, see if there is a way
1996 * to swap crtcs around and make things work
1998 for (output = outputs; output; output = output->next)
1999 output->current_crtc_info = output->crtc_info;
2000 pick_crtcs_score (outputs);
2001 for (output = outputs; output; output = output->next)
2003 if (output->mode_info && !output->crtc_info)
2004 fatal ("cannot find crtc for output %s\n", output->output.string);
2005 if (!output->changes && output->crtc_info != output->current_crtc_info)
2006 output->changes |= changes_crtc;
2011 check_strtol(char *s)
2014 int result = strtol(s, &endptr, 10);
2021 check_strtod(char *s)
2024 double result = strtod(s, &endptr);
2031 main (int argc, char **argv)
2033 XRRScreenSize *sizes;
2034 XRRScreenConfiguration *sc;
2038 Status status = RRSetConfigFailed;
2041 int action_requested = False;
2042 Rotation rotation, current_rotation, rotations;
2044 XRRScreenChangeNotifyEvent *sce;
2045 char *display_name = NULL;
2047 SizeID current_size;
2053 Bool version = False;
2054 int event_base, error_base;
2056 int width = 0, height = 0;
2057 Bool have_pixel_size = False;
2059 output_t *output = NULL;
2060 Bool setit_1_2 = False;
2061 Bool query_1_2 = False;
2062 Bool modeit = False;
2063 Bool propit = False;
2064 Bool query_1 = False;
2066 Bool current = False;
2068 program_name = argv[0];
2069 for (i = 1; i < argc; i++) {
2070 if (!strcmp ("-display", argv[i]) || !strcmp ("-d", argv[i])) {
2071 if (++i>=argc) usage ();
2072 display_name = argv[i];
2075 if (!strcmp("-help", argv[i])) {
2077 action_requested = True;
2080 if (!strcmp ("--verbose", argv[i])) {
2084 if (!strcmp ("--dryrun", argv[i])) {
2089 if (!strcmp ("--nograb", argv[i])) {
2090 grab_server = False;
2093 if (!strcmp("--current", argv[i])) {
2098 if (!strcmp ("-s", argv[i]) || !strcmp ("--size", argv[i])) {
2099 if (++i>=argc) usage ();
2100 if (sscanf (argv[i], "%dx%d", &width, &height) == 2) {
2101 have_pixel_size = True;
2103 size = check_strtol(argv[i]);
2104 if (size < 0) usage();
2107 action_requested = True;
2111 if (!strcmp ("-r", argv[i]) ||
2112 !strcmp ("--rate", argv[i]) ||
2113 !strcmp ("--refresh", argv[i]))
2115 if (++i>=argc) usage ();
2116 rate = check_strtod(argv[i]);
2120 output->refresh = rate;
2121 output->changes |= changes_refresh;
2124 action_requested = True;
2128 if (!strcmp ("-v", argv[i]) || !strcmp ("--version", argv[i])) {
2130 action_requested = True;
2134 if (!strcmp ("-x", argv[i])) {
2135 reflection |= RR_Reflect_X;
2137 action_requested = True;
2140 if (!strcmp ("-y", argv[i])) {
2141 reflection |= RR_Reflect_Y;
2143 action_requested = True;
2146 if (!strcmp ("--screen", argv[i])) {
2147 if (++i>=argc) usage ();
2148 screen = check_strtol(argv[i]);
2149 if (screen < 0) usage();
2152 if (!strcmp ("-q", argv[i]) || !strcmp ("--query", argv[i])) {
2156 if (!strcmp ("-o", argv[i]) || !strcmp ("--orientation", argv[i])) {
2158 if (++i>=argc) usage ();
2159 dirind = strtol(argv[i], &endptr, 10);
2160 if (argv[i] == endptr) {
2161 for (dirind = 0; dirind < 4; dirind++) {
2162 if (strcmp (direction[dirind], argv[i]) == 0) break;
2164 if ((dirind < 0) || (dirind > 3)) usage();
2168 action_requested = True;
2171 if (!strcmp ("--prop", argv[i]) ||
2172 !strcmp ("--props", argv[i]) ||
2173 !strcmp ("--madprops", argv[i]) ||
2174 !strcmp ("--properties", argv[i]))
2178 action_requested = True;
2181 if (!strcmp ("--output", argv[i])) {
2182 if (++i >= argc) usage();
2184 output = find_output_by_name (argv[i]);
2186 output = add_output ();
2187 set_name (&output->output, argv[i], name_string|name_xid);
2191 action_requested = True;
2194 if (!strcmp ("--crtc", argv[i])) {
2195 if (++i >= argc) usage();
2196 if (!output) usage();
2197 set_name (&output->crtc, argv[i], name_xid|name_index);
2198 output->changes |= changes_crtc;
2201 if (!strcmp ("--mode", argv[i])) {
2202 if (++i >= argc) usage();
2203 if (!output) usage();
2204 set_name (&output->mode, argv[i], name_string|name_xid);
2205 output->changes |= changes_mode;
2208 if (!strcmp ("--preferred", argv[i])) {
2209 if (!output) usage();
2210 set_name_preferred (&output->mode);
2211 output->changes |= changes_mode;
2214 if (!strcmp ("--pos", argv[i])) {
2215 if (++i>=argc) usage ();
2216 if (!output) usage();
2217 if (sscanf (argv[i], "%dx%d",
2218 &output->x, &output->y) != 2)
2220 output->changes |= changes_position;
2223 if (!strcmp ("--rotation", argv[i]) || !strcmp ("--rotate", argv[i])) {
2224 if (++i>=argc) usage ();
2225 if (!output) usage();
2226 for (dirind = 0; dirind < 4; dirind++) {
2227 if (strcmp (direction[dirind], argv[i]) == 0) break;
2231 output->rotation &= ~0xf;
2232 output->rotation |= 1 << dirind;
2233 output->changes |= changes_rotation;
2236 if (!strcmp ("--reflect", argv[i]) || !strcmp ("--reflection", argv[i])) {
2237 if (++i>=argc) usage ();
2238 if (!output) usage();
2239 for (dirind = 0; dirind < 4; dirind++) {
2240 if (strcmp (reflections[dirind], argv[i]) == 0) break;
2244 output->rotation &= ~(RR_Reflect_X|RR_Reflect_Y);
2245 output->rotation |= dirind * RR_Reflect_X;
2246 output->changes |= changes_reflection;
2249 if (!strcmp ("--left-of", argv[i])) {
2250 if (++i>=argc) usage ();
2251 if (!output) usage();
2252 output->relation = relation_left_of;
2253 output->relative_to = argv[i];
2254 output->changes |= changes_relation;
2257 if (!strcmp ("--right-of", argv[i])) {
2258 if (++i>=argc) usage ();
2259 if (!output) usage();
2260 output->relation = relation_right_of;
2261 output->relative_to = argv[i];
2262 output->changes |= changes_relation;
2265 if (!strcmp ("--above", argv[i])) {
2266 if (++i>=argc) usage ();
2267 if (!output) usage();
2268 output->relation = relation_above;
2269 output->relative_to = argv[i];
2270 output->changes |= changes_relation;
2273 if (!strcmp ("--below", argv[i])) {
2274 if (++i>=argc) usage ();
2275 if (!output) usage();
2276 output->relation = relation_below;
2277 output->relative_to = argv[i];
2278 output->changes |= changes_relation;
2281 if (!strcmp ("--same-as", argv[i])) {
2282 if (++i>=argc) usage ();
2283 if (!output) usage();
2284 output->relation = relation_same_as;
2285 output->relative_to = argv[i];
2286 output->changes |= changes_relation;
2289 if (!strcmp ("--panning", argv[i])) {
2291 if (++i>=argc) usage ();
2292 if (!output) usage();
2293 pan = &output->panning;
2294 switch (sscanf (argv[i], "%dx%d+%d+%d/%dx%d+%d+%d/%d/%d/%d/%d",
2295 &pan->width, &pan->height, &pan->left, &pan->top,
2296 &pan->track_width, &pan->track_height,
2297 &pan->track_left, &pan->track_top,
2298 &pan->border_left, &pan->border_top,
2299 &pan->border_right, &pan->border_bottom)) {
2301 pan->left = pan->top = 0;
2304 pan->track_left = pan->track_top =
2305 pan->track_width = pan->track_height = 0;
2308 pan->border_left = pan->border_top =
2309 pan->border_right = pan->border_bottom = 0;
2316 output->changes |= changes_panning;
2319 if (!strcmp ("--gamma", argv[i])) {
2320 if (!output) usage();
2321 if (++i>=argc) usage ();
2322 if (sscanf(argv[i], "%f:%f:%f", &output->gamma.red,
2323 &output->gamma.green, &output->gamma.blue) != 3)
2325 output->changes |= changes_gamma;
2329 if (!strcmp ("--brightness", argv[i])) {
2330 if (!output) usage();
2331 if (++i>=argc) usage();
2332 if (sscanf(argv[i], "%f", &output->brightness) != 1)
2334 output->changes |= changes_gamma;
2338 if (!strcmp ("--primary", argv[i])) {
2339 if (!output) usage();
2340 output->changes |= changes_primary;
2341 output->primary = True;
2345 if (!strcmp ("--noprimary", argv[i])) {
2350 if (!strcmp ("--set", argv[i])) {
2351 output_prop_t *prop;
2352 if (!output) usage();
2353 prop = malloc (sizeof (output_prop_t));
2354 prop->next = output->props;
2355 output->props = prop;
2356 if (++i>=argc) usage ();
2357 prop->name = argv[i];
2358 if (++i>=argc) usage ();
2359 prop->value = argv[i];
2361 output->changes |= changes_property;
2365 if (!strcmp ("--scale", argv[i]))
2368 if (!output) usage();
2369 if (++i>=argc) usage();
2370 if (sscanf (argv[i], "%lfx%lf", &sx, &sy) != 2)
2372 init_transform (&output->transform);
2373 output->transform.transform.matrix[0][0] = XDoubleToFixed (sx);
2374 output->transform.transform.matrix[1][1] = XDoubleToFixed (sy);
2375 output->transform.transform.matrix[2][2] = XDoubleToFixed (1.0);
2376 if (sx != 1 || sy != 1)
2377 output->transform.filter = "bilinear";
2379 output->transform.filter = "nearest";
2380 output->transform.nparams = 0;
2381 output->transform.params = NULL;
2382 output->changes |= changes_transform;
2385 if (!strcmp ("--transform", argv[i])) {
2386 double transform[3][3];
2388 if (!output) usage();
2389 if (++i>=argc) usage ();
2390 init_transform (&output->transform);
2391 if (strcmp (argv[i], "none") != 0)
2393 if (sscanf(argv[i], "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf",
2394 &transform[0][0],&transform[0][1],&transform[0][2],
2395 &transform[1][0],&transform[1][1],&transform[1][2],
2396 &transform[2][0],&transform[2][1],&transform[2][2])
2399 init_transform (&output->transform);
2400 for (k = 0; k < 3; k++)
2401 for (l = 0; l < 3; l++) {
2402 output->transform.transform.matrix[k][l] = XDoubleToFixed (transform[k][l]);
2404 output->transform.filter = "bilinear";
2405 output->transform.nparams = 0;
2406 output->transform.params = NULL;
2408 output->changes |= changes_transform;
2411 if (!strcmp ("--off", argv[i])) {
2412 if (!output) usage();
2413 set_name_xid (&output->mode, None);
2414 set_name_xid (&output->crtc, None);
2415 output->changes |= changes_mode;
2418 if (!strcmp ("--fb", argv[i])) {
2419 if (++i>=argc) usage ();
2420 if (sscanf (argv[i], "%dx%d",
2421 &fb_width, &fb_height) != 2)
2424 action_requested = True;
2427 if (!strcmp ("--fbmm", argv[i])) {
2428 if (++i>=argc) usage ();
2429 if (sscanf (argv[i], "%dx%d",
2430 &fb_width_mm, &fb_height_mm) != 2)
2433 action_requested = True;
2436 if (!strcmp ("--dpi", argv[i])) {
2438 if (++i>=argc) usage ();
2439 dpi = strtod(argv[i], &strtod_error);
2440 if (argv[i] == strtod_error)
2443 dpi_output = argv[i];
2446 action_requested = True;
2449 if (!strcmp ("--auto", argv[i])) {
2452 output->automatic = True;
2453 output->changes |= changes_automatic;
2458 action_requested = True;
2461 if (!strcmp ("--q12", argv[i]))
2466 if (!strcmp ("--q1", argv[i]))
2471 if (!strcmp ("--newmode", argv[i]))
2473 umode_t *m = malloc (sizeof (umode_t));
2477 if (i + 9 >= argc) usage ();
2478 m->mode.name = argv[i];
2479 m->mode.nameLength = strlen (argv[i]);
2481 clock = check_strtod(argv[i++]);
2482 m->mode.dotClock = clock * 1e6;
2484 m->mode.width = check_strtol(argv[i++]);
2485 m->mode.hSyncStart = check_strtol(argv[i++]);
2486 m->mode.hSyncEnd = check_strtol(argv[i++]);
2487 m->mode.hTotal = check_strtol(argv[i++]);
2488 m->mode.height = check_strtol(argv[i++]);
2489 m->mode.vSyncStart = check_strtol(argv[i++]);
2490 m->mode.vSyncEnd = check_strtol(argv[i++]);
2491 m->mode.vTotal = check_strtol(argv[i++]);
2492 m->mode.modeFlags = 0;
2496 for (f = 0; mode_flags[f].string; f++)
2497 if (!strcasecmp (mode_flags[f].string, argv[i]))
2500 if (!mode_flags[f].string)
2502 m->mode.modeFlags |= mode_flags[f].flag;
2506 m->action = umode_create;
2509 action_requested = True;
2512 if (!strcmp ("--rmmode", argv[i]))
2514 umode_t *m = malloc (sizeof (umode_t));
2516 if (++i>=argc) usage ();
2517 set_name (&m->name, argv[i], name_string|name_xid);
2518 m->action = umode_destroy;
2522 action_requested = True;
2525 if (!strcmp ("--addmode", argv[i]))
2527 umode_t *m = malloc (sizeof (umode_t));
2529 if (++i>=argc) usage ();
2530 set_name (&m->output, argv[i], name_string|name_xid);
2531 if (++i>=argc) usage();
2532 set_name (&m->name, argv[i], name_string|name_xid);
2533 m->action = umode_add;
2537 action_requested = True;
2540 if (!strcmp ("--delmode", argv[i]))
2542 umode_t *m = malloc (sizeof (umode_t));
2544 if (++i>=argc) usage ();
2545 set_name (&m->output, argv[i], name_string|name_xid);
2546 if (++i>=argc) usage();
2547 set_name (&m->name, argv[i], name_string|name_xid);
2548 m->action = umode_delete;
2552 action_requested = True;
2557 if (!action_requested)
2562 if (setit && !setit_1_2)
2566 printf("xrandr program version " VERSION "\n");
2568 dpy = XOpenDisplay (display_name);
2571 fprintf (stderr, "Can't open display %s\n", XDisplayName(display_name));
2575 screen = DefaultScreen (dpy);
2576 if (screen >= ScreenCount (dpy)) {
2577 fprintf (stderr, "Invalid screen number %d (display has %d)\n",
2578 screen, ScreenCount (dpy));
2582 root = RootWindow (dpy, screen);
2584 if (!XRRQueryVersion (dpy, &major, &minor))
2586 fprintf (stderr, "RandR extension missing\n");
2589 if (major > 1 || (major == 1 && minor >= 2))
2591 if (major > 1 || (major == 1 && minor >= 3))
2594 if (has_1_2 && modeit)
2598 get_screen (current);
2602 for (m = umodes; m; m = m->next)
2607 switch (m->action) {
2609 XRRCreateMode (dpy, root, &m->mode);
2612 e = find_mode (&m->name, 0);
2614 fatal ("cannot find mode \"%s\"\n", m->name.string);
2615 XRRDestroyMode (dpy, e->id);
2618 o = find_output (&m->output);
2620 fatal ("cannot find output \"%s\"\n", m->output.string);
2621 e = find_mode (&m->name, 0);
2623 fatal ("cannot find mode \"%s\"\n", m->name.string);
2624 XRRAddOutputMode (dpy, o->output.xid, e->id);
2627 o = find_output (&m->output);
2629 fatal ("cannot find output \"%s\"\n", m->output.string);
2630 e = find_mode (&m->name, 0);
2632 fatal ("cannot find mode \"%s\"\n", m->name.string);
2633 XRRDeleteOutputMode (dpy, o->output.xid, e->id);
2643 if (has_1_2 && propit)
2646 get_screen (current);
2650 for (output = outputs; output; output = output->next)
2652 output_prop_t *prop;
2654 for (prop = output->props; prop; prop = prop->next)
2656 Atom name = XInternAtom (dpy, prop->name, False);
2659 unsigned char *data;
2662 unsigned long ulong_value;
2663 unsigned char *prop_data;
2665 unsigned long nitems, bytes_after;
2667 XRRPropertyInfo *propinfo;
2669 type = AnyPropertyType;
2672 if (XRRGetOutputProperty (dpy, output->output.xid, name,
2673 0, 100, False, False,
2675 &actual_type, &actual_format,
2676 &nitems, &bytes_after, &prop_data) == Success &&
2678 (propinfo = XRRQueryOutputProperty(dpy, output->output.xid,
2682 format = actual_format;
2685 if ((type == XA_INTEGER || type == AnyPropertyType) &&
2686 (sscanf (prop->value, "%d", &int_value) == 1 ||
2687 sscanf (prop->value, "0x%x", &int_value) == 1))
2690 ulong_value = int_value;
2691 data = (unsigned char *) &ulong_value;
2695 else if ((type == XA_ATOM))
2697 ulong_value = XInternAtom (dpy, prop->value, False);
2698 data = (unsigned char *) &ulong_value;
2702 else if ((type == XA_STRING || type == AnyPropertyType))
2705 data = (unsigned char *) prop->value;
2706 nelements = strlen (prop->value);
2711 XRRChangeOutputProperty (dpy, output->output.xid,
2712 name, type, format, PropModeReplace,
2724 get_screen (current);
2733 * Assign outputs to crtcs
2738 * Mark changing crtcs
2740 mark_changing_crtcs ();
2743 * If an output was specified to track dpi, use it
2747 output_t *output = find_output_by_name (dpi_output);
2748 XRROutputInfo *output_info;
2749 XRRModeInfo *mode_info;
2751 fatal ("Cannot find output %s\n", dpi_output);
2752 output_info = output->output_info;
2753 mode_info = output->mode_info;
2754 if (output_info && mode_info && output_info->mm_height)
2757 * When this output covers the whole screen, just use
2758 * the known physical size
2760 if (fb_width == mode_info->width &&
2761 fb_height == mode_info->height)
2763 fb_width_mm = output_info->mm_width;
2764 fb_height_mm = output_info->mm_height;
2768 dpi = (25.4 * mode_info->height) / output_info->mm_height;
2774 * Compute physical screen size
2776 if (fb_width_mm == 0 || fb_height_mm == 0)
2778 if (fb_width != DisplayWidth (dpy, screen) ||
2779 fb_height != DisplayHeight (dpy, screen) || dpi != 0.0)
2782 dpi = (25.4 * DisplayHeight (dpy, screen)) / DisplayHeightMM(dpy, screen);
2784 fb_width_mm = (25.4 * fb_width) / dpi;
2785 fb_height_mm = (25.4 * fb_height) / dpi;
2789 fb_width_mm = DisplayWidthMM (dpy, screen);
2790 fb_height_mm = DisplayHeightMM (dpy, screen);
2800 * Set gamma on crtc's that belong to the outputs.
2805 * Now apply all of the changes
2812 if (query_1_2 || (query && has_1_2 && !query_1))
2817 #define ModeShown 0x80000000
2819 get_screen (current);
2823 printf ("Screen %d: minimum %d x %d, current %d x %d, maximum %d x %d\n",
2824 screen, minWidth, minHeight,
2825 DisplayWidth (dpy, screen), DisplayHeight(dpy, screen),
2826 maxWidth, maxHeight);
2828 for (output = outputs; output; output = output->next)
2830 XRROutputInfo *output_info = output->output_info;
2831 crtc_t *crtc = output->crtc_info;
2832 XRRCrtcInfo *crtc_info = crtc ? crtc->crtc_info : NULL;
2833 XRRModeInfo *mode = output->mode_info;
2837 Rotation rotations = output_rotations (output);
2839 printf ("%s %s", output_info->name, connection[output_info->connection]);
2843 printf (" %dx%d+%d+%d",
2844 crtc_info->width, crtc_info->height,
2845 crtc_info->x, crtc_info->y);
2847 printf (" %dx%d+%d+%d",
2848 mode->width, mode->height, output->x, output->y);
2851 printf (" (0x%x)", (int)mode->id);
2852 if (output->rotation != RR_Rotate_0 || verbose)
2855 rotation_name (output->rotation));
2856 if (output->rotation & (RR_Reflect_X|RR_Reflect_Y))
2857 printf (" %s", reflection_name (output->rotation));
2860 if (rotations != RR_Rotate_0 || verbose)
2864 for (i = 0; i < 4; i ++) {
2865 if ((rotations >> i) & 1) {
2866 if (!first) printf (" "); first = False;
2867 printf("%s", direction[i]);
2871 if (rotations & RR_Reflect_X)
2873 if (!first) printf (" "); first = False;
2876 if (rotations & RR_Reflect_Y)
2878 if (!first) printf (" "); first = False;
2886 printf (" %dmm x %dmm",
2887 (int)output_info->mm_width, (int)output_info->mm_height);
2890 if (crtc && crtc->panning_info && crtc->panning_info->width > 0)
2892 XRRPanning *pan = crtc->panning_info;
2893 printf (" panning %dx%d+%d+%d",
2894 pan->width, pan->height, pan->left, pan->top);
2895 if ((pan->track_width != 0 &&
2896 (pan->track_left != pan->left ||
2897 pan->track_width != pan->width ||
2898 pan->border_left != 0 ||
2899 pan->border_right != 0)) ||
2900 (pan->track_height != 0 &&
2901 (pan->track_top != pan->top ||
2902 pan->track_height != pan->height ||
2903 pan->border_top != 0 ||
2904 pan->border_bottom != 0)))
2905 printf (" tracking %dx%d+%d+%d border %d/%d/%d/%d",
2906 pan->track_width, pan->track_height,
2907 pan->track_left, pan->track_top,
2908 pan->border_left, pan->border_top,
2909 pan->border_right, pan->border_bottom);
2915 printf ("\tIdentifier: 0x%x\n", (int)output->output.xid);
2916 printf ("\tTimestamp: %d\n", (int)output_info->timestamp);
2917 printf ("\tSubpixel: %s\n", order[output_info->subpixel_order]);
2918 printf ("\tClones: ");
2919 for (j = 0; j < output_info->nclone; j++)
2921 output_t *clone = find_output_by_xid (output_info->clones[j]);
2923 if (clone) printf (" %s", clone->output.string);
2926 if (output->crtc_info)
2927 printf ("\tCRTC: %d\n", output->crtc_info->crtc.index);
2928 printf ("\tCRTCs: ");
2929 for (j = 0; j < output_info->ncrtc; j++)
2931 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[j]);
2933 printf (" %d", crtc->crtc.index);
2936 if (output->crtc_info && output->crtc_info->panning_info) {
2937 XRRPanning *pan = output->crtc_info->panning_info;
2938 printf ("\tPanning: %dx%d+%d+%d\n",
2939 pan->width, pan->height, pan->left, pan->top);
2940 printf ("\tTracking: %dx%d+%d+%d\n",
2941 pan->track_width, pan->track_height,
2942 pan->track_left, pan->track_top);
2943 printf ("\tBorder: %d/%d/%d/%d\n",
2944 pan->border_left, pan->border_top,
2945 pan->border_right, pan->border_bottom);
2952 printf ("\tTransform: ");
2953 for (y = 0; y < 3; y++)
2955 for (x = 0; x < 3; x++)
2956 printf (" %f", XFixedToDouble (output->transform.transform.matrix[y][x]));
2960 if (output->transform.filter)
2961 printf ("\n\t filter: %s", output->transform.filter);
2964 if (verbose || properties)
2966 props = XRRListOutputProperties (dpy, output->output.xid,
2968 for (j = 0; j < nprop; j++) {
2969 unsigned char *prop;
2971 unsigned long nitems, bytes_after;
2973 XRRPropertyInfo *propinfo;
2975 XRRGetOutputProperty (dpy, output->output.xid, props[j],
2976 0, 100, False, False,
2978 &actual_type, &actual_format,
2979 &nitems, &bytes_after, &prop);
2981 propinfo = XRRQueryOutputProperty(dpy, output->output.xid,
2984 if (actual_type == XA_INTEGER && actual_format == 8) {
2987 printf("\t%s:\n", XGetAtomName (dpy, props[j]));
2988 for (k = 0; k < nitems; k++) {
2991 printf("%02x", (unsigned char)prop[k]);
2995 } else if (actual_type == XA_INTEGER &&
2996 actual_format == 32)
2998 printf("\t%s: ", XGetAtomName (dpy, props[j]));
2999 for (k = 0; k < nitems; k++) {
3001 printf ("\n\t\t\t");
3002 printf("%d (0x%08x)",
3003 (int)((INT32 *)prop)[k], (int)((INT32 *)prop)[k]);
3006 if (propinfo->range && propinfo->num_values > 0) {
3009 printf("\trange%s: ",
3010 (propinfo->num_values == 2) ? "" : "s");
3012 for (k = 0; k < propinfo->num_values / 2; k++)
3013 printf(" (%d,%d)", (int)propinfo->values[k * 2],
3014 (int)propinfo->values[k * 2 + 1]);
3018 } else if (actual_type == XA_ATOM &&
3019 actual_format == 32)
3021 printf("\t%s:", XGetAtomName (dpy, props[j]));
3022 for (k = 0; k < nitems; k++) {
3023 if (k > 0 && (k & 1) == 0)
3025 printf("\t%s", XGetAtomName (dpy, ((Atom *)prop)[k]));
3028 if (!propinfo->range && propinfo->num_values > 0) {
3029 printf("\n\t\tsupported:");
3031 for (k = 0; k < propinfo->num_values; k++)
3033 printf(" %-12.12s", XGetAtomName (dpy,
3034 propinfo->values[k]));
3035 if (k % 4 == 3 && k < propinfo->num_values - 1)
3040 } else if (actual_format == 8) {
3041 printf ("\t%s: %s%s\n", XGetAtomName (dpy, props[j]),
3042 prop, bytes_after ? "..." : "");
3044 char *type = actual_type ? XGetAtomName (dpy, actual_type) : "none";
3045 printf ("\t%s: %s(%d) (format %d items %d) ????\n",
3046 XGetAtomName (dpy, props[j]),
3047 type, (int)actual_type, actual_format, (int)nitems);
3056 for (j = 0; j < output_info->nmode; j++)
3058 XRRModeInfo *mode = find_mode_by_xid (output_info->modes[j]);
3061 printf (" %s (0x%x) %6.1fMHz",
3062 mode->name, (int)mode->id,
3063 (double)mode->dotClock / 1000000.0);
3064 for (f = 0; mode_flags[f].flag; f++)
3065 if (mode->modeFlags & mode_flags[f].flag)
3066 printf (" %s", mode_flags[f].string);
3067 if (mode == output->mode_info)
3068 printf (" *current");
3069 if (j < output_info->npreferred)
3070 printf (" +preferred");
3072 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
3073 mode->width, mode->hSyncStart, mode->hSyncEnd,
3074 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
3075 printf (" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
3076 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
3077 mode_refresh (mode));
3078 mode->modeFlags |= ModeShown;
3083 mode_shown = calloc (output_info->nmode, sizeof (Bool));
3084 if (!mode_shown) fatal ("out of memory\n");
3085 for (j = 0; j < output_info->nmode; j++)
3087 XRRModeInfo *jmode, *kmode;
3089 if (mode_shown[j]) continue;
3091 jmode = find_mode_by_xid (output_info->modes[j]);
3093 printf (" %-12s", jmode->name);
3094 for (k = j; k < output_info->nmode; k++)
3096 if (mode_shown[k]) continue;
3097 kmode = find_mode_by_xid (output_info->modes[k]);
3098 if (strcmp (jmode->name, kmode->name) != 0) continue;
3099 mode_shown[k] = True;
3100 kmode->modeFlags |= ModeShown;
3101 printf (" %6.1f", mode_refresh (kmode));
3102 if (kmode == output->mode_info)
3106 if (k < output_info->npreferred)
3116 for (m = 0; m < res->nmode; m++)
3118 XRRModeInfo *mode = &res->modes[m];
3120 if (!(mode->modeFlags & ModeShown))
3122 printf (" %s (0x%x) %6.1fMHz\n",
3123 mode->name, (int)mode->id,
3124 (double)mode->dotClock / 1000000.0);
3125 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
3126 mode->width, mode->hSyncStart, mode->hSyncEnd,
3127 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
3128 printf (" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
3129 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
3130 mode_refresh (mode));
3136 sc = XRRGetScreenInfo (dpy, root);
3141 current_size = XRRConfigCurrentConfiguration (sc, ¤t_rotation);
3143 sizes = XRRConfigSizes(sc, &nsize);
3145 if (have_pixel_size) {
3146 for (size = 0; size < nsize; size++)
3148 if (sizes[size].width == width && sizes[size].height == height)
3151 if (size >= nsize) {
3153 "Size %dx%d not found in available modes\n", width, height);
3158 size = current_size;
3159 else if (size >= nsize) {
3161 "Size index %d is too large, there are only %d sizes\n",
3168 for (rot = 0; rot < 4; rot++)
3169 if (1 << rot == (current_rotation & 0xf))
3173 current_rate = XRRConfigCurrentRate (sc);
3177 if (size == current_size)
3178 rate = current_rate;
3184 rates = XRRConfigRates (sc, size, &nrate);
3185 for (i = 0; i < nrate; i++)
3186 if (rate == rates[i])
3189 fprintf (stderr, "Rate %.1f Hz not available for this size\n", rate);
3195 int major_version, minor_version;
3196 XRRQueryVersion (dpy, &major_version, &minor_version);
3197 printf("Server reports RandR version %d.%d\n",
3198 major_version, minor_version);
3201 if (query || query_1) {
3202 printf(" SZ: Pixels Physical Refresh\n");
3203 for (i = 0; i < nsize; i++) {
3204 printf ("%c%-2d %5d x %-5d (%4dmm x%4dmm )",
3205 i == current_size ? '*' : ' ',
3206 i, sizes[i].width, sizes[i].height,
3207 sizes[i].mwidth, sizes[i].mheight);
3208 rates = XRRConfigRates (sc, i, &nrate);
3209 if (nrate) printf (" ");
3210 for (j = 0; j < nrate; j++)
3212 i == current_size && rates[j] == current_rate ? '*' : ' ',
3218 rotations = XRRConfigRotations(sc, ¤t_rotation);
3220 rotation = 1 << rot ;
3222 printf("Current rotation - %s\n",
3223 rotation_name (current_rotation));
3225 printf("Current reflection - %s\n",
3226 reflection_name (current_rotation));
3228 printf ("Rotations possible - ");
3229 for (i = 0; i < 4; i ++) {
3230 if ((rotations >> i) & 1) printf("%s ", direction[i]);
3234 printf ("Reflections possible - ");
3235 if (rotations & (RR_Reflect_X|RR_Reflect_Y))
3237 if (rotations & RR_Reflect_X) printf ("X Axis ");
3238 if (rotations & RR_Reflect_Y) printf ("Y Axis");
3246 printf("Setting size to %d, rotation to %s\n", size, direction[rot]);
3248 printf ("Setting reflection on ");
3251 if (reflection & RR_Reflect_X) printf ("X Axis ");
3252 if (reflection & RR_Reflect_Y) printf ("Y Axis");
3255 printf ("neither axis");
3258 if (reflection & RR_Reflect_X) printf("Setting reflection on X axis\n");
3260 if (reflection & RR_Reflect_Y) printf("Setting reflection on Y axis\n");
3263 /* we should test configureNotify on the root window */
3264 XSelectInput (dpy, root, StructureNotifyMask);
3266 if (setit && !dryrun) XRRSelectInput (dpy, root,
3267 RRScreenChangeNotifyMask);
3268 if (setit && !dryrun) status = XRRSetScreenConfigAndRate (dpy, sc,
3270 (SizeID) size, (Rotation) (rotation | reflection), rate, CurrentTime);
3272 XRRQueryExtension(dpy, &event_base, &error_base);
3274 if (setit && !dryrun && status == RRSetConfigFailed) {
3275 printf ("Failed to change the screen configuration!\n");
3279 if (verbose && setit && !dryrun && size != current_size) {
3280 if (status == RRSetConfigSuccess)
3282 Bool seen_screen = False;
3283 while (!seen_screen) {
3285 XNextEvent(dpy, (XEvent *) &event);
3287 printf ("Event received, type = %d\n", event.type);
3288 /* update Xlib's knowledge of the event */
3289 XRRUpdateConfiguration (&event);
3290 if (event.type == ConfigureNotify)
3291 printf("Received ConfigureNotify Event!\n");
3293 switch (event.type - event_base) {
3294 case RRScreenChangeNotify:
3295 sce = (XRRScreenChangeNotifyEvent *) &event;
3297 printf("Got a screen change notify event!\n");
3298 printf(" window = %d\n root = %d\n size_index = %d\n rotation %d\n",
3299 (int) sce->window, (int) sce->root,
3300 sce->size_index, sce->rotation);
3301 printf(" timestamp = %ld, config_timestamp = %ld\n",
3302 sce->timestamp, sce->config_timestamp);
3303 printf(" Rotation = %x\n", sce->rotation);
3304 printf(" %d X %d pixels, %d X %d mm\n",
3305 sce->width, sce->height, sce->mwidth, sce->mheight);
3306 printf("Display width %d, height %d\n",
3307 DisplayWidth(dpy, screen), DisplayHeight(dpy, screen));
3308 printf("Display widthmm %d, heightmm %d\n",
3309 DisplayWidthMM(dpy, screen), DisplayHeightMM(dpy, screen));
3310 spo = sce->subpixel_order;
3311 if ((spo < 0) || (spo > 5))
3312 printf ("Unknown subpixel order, value = %d\n", spo);
3313 else printf ("new Subpixel rendering model is %s\n", order[spo]);
3317 if (event.type != ConfigureNotify)
3318 printf("unknown event received, type = %d!\n", event.type);
3323 XRRFreeScreenConfigInfo(sc);