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 */
40 #if RANDR_MAJOR > 1 || (RANDR_MAJOR == 1 && RANDR_MINOR >= 2)
41 #define HAS_RANDR_1_2 1
44 static char *program_name;
47 static int screen = -1;
48 static Bool verbose = False;
49 static Bool automatic = False;
50 static Bool properties = 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");
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");
115 fprintf(stderr, " --clone\n");
116 fprintf(stderr, " --extend\n");
118 fprintf(stderr, " --output <output>\n");
119 fprintf(stderr, " --auto\n");
120 fprintf(stderr, " --mode <mode>\n");
121 fprintf(stderr, " --preferred\n");
122 fprintf(stderr, " --pos <x>x<y>\n");
123 fprintf(stderr, " --rate <rate> or --refresh <rate>\n");
124 fprintf(stderr, " --reflect normal,x,y,xy\n");
125 fprintf(stderr, " --rotate normal,inverted,left,right\n");
126 fprintf(stderr, " --left-of <output>\n");
127 fprintf(stderr, " --right-of <output>\n");
128 fprintf(stderr, " --above <output>\n");
129 fprintf(stderr, " --below <output>\n");
130 fprintf(stderr, " --same-as <output>\n");
131 fprintf(stderr, " --set <property> <value>\n");
132 fprintf(stderr, " --off\n");
133 fprintf(stderr, " --crtc <crtc>\n");
134 fprintf(stderr, " --newmode <name> <clock MHz>\n");
135 fprintf(stderr, " <hdisp> <hsync-start> <hsync-end> <htotal>\n");
136 fprintf(stderr, " <vdisp> <vsync-start> <vsync-end> <vtotal>\n");
137 fprintf(stderr, " [+HSync] [-HSync] [+VSync] [-VSync]\n");
138 fprintf(stderr, " --rmmode <name>\n");
139 fprintf(stderr, " --addmode <output> <name>\n");
140 fprintf(stderr, " --delmode <output> <name>\n");
148 fatal (const char *format, ...)
152 va_start (ap, format);
153 fprintf (stderr, "%s: ", program_name);
154 vfprintf (stderr, format, ap);
161 warning (const char *format, ...)
165 va_start (ap, format);
166 fprintf (stderr, "%s: ", program_name);
167 vfprintf (stderr, format, ap);
172 rotation_name (Rotation rotation)
176 if ((rotation & 0xf) == 0)
178 for (i = 0; i < 4; i++)
179 if (rotation & (1 << i))
181 return "invalid rotation";
185 reflection_name (Rotation rotation)
187 rotation &= (RR_Reflect_X|RR_Reflect_Y);
195 case RR_Reflect_X|RR_Reflect_Y:
196 return "X and Y axis";
198 return "invalid reflection";
202 typedef enum _policy {
206 typedef enum _relation {
207 left_of, right_of, above, below, same_as,
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),
236 typedef enum _name_kind {
238 name_string = (1 << 0),
240 name_index = (1 << 2),
241 name_preferred = (1 << 3),
251 typedef struct _crtc crtc_t;
252 typedef struct _output output_t;
253 typedef struct _transform transform_t;
254 typedef struct _umode umode_t;
255 typedef struct _output_prop output_prop_t;
258 XTransform transform;
267 XRRCrtcInfo *crtc_info;
269 XRRModeInfo *mode_info;
275 transform_t current_transform, pending_transform;
278 struct _output_prop {
279 struct _output_prop *next;
285 struct _output *next;
289 output_prop_t *props;
292 XRROutputInfo *output_info;
296 crtc_t *current_crtc_info;
300 XRRModeInfo *mode_info;
311 transform_t transform;
314 typedef enum _umode_action {
315 umode_create, umode_destroy, umode_add, umode_delete
322 umode_action_t action;
328 static char *connection[3] = {
331 "unknown connection"};
333 #define OUTPUT_NAME 1
337 #define CRTC_INDEX 0x40000000
346 static output_t *outputs = NULL;
347 static output_t **outputs_tail = &outputs;
348 static crtc_t *crtcs;
349 static umode_t *umodes;
350 static int num_crtcs;
351 static XRRScreenResources *res;
352 static int fb_width = 0, fb_height = 0;
353 static int fb_width_mm = 0, fb_height_mm = 0;
354 static float dpi = 0;
355 static char *dpi_output = NULL;
356 static Bool dryrun = False;
357 static int minWidth, maxWidth, minHeight, maxHeight;
358 static Bool has_1_2 = False;
361 mode_height (XRRModeInfo *mode_info, Rotation rotation)
363 switch (rotation & 0xf) {
366 return mode_info->height;
369 return mode_info->width;
376 mode_width (XRRModeInfo *mode_info, Rotation rotation)
378 switch (rotation & 0xf) {
381 return mode_info->width;
384 return mode_info->height;
391 transform_point (XTransform *transform, double *xp, double *yp)
401 for (j = 0; j < 3; j++)
404 for (i = 0; i < 3; i++)
405 v += (XFixedToDouble (transform->matrix[j][i]) * vector[i]);
406 if (v > 32767 || v < -32767)
412 for (j = 0; j < 2; j++)
413 vector[j] = result[j] / result[2];
420 path_bounds (XTransform *transform, point_t *points, int npoints, box_t *box)
425 for (i = 0; i < npoints; i++) {
429 transform_point (transform, &x, &y);
430 point.x1 = floor (x);
431 point.y1 = floor (y);
437 if (point.x1 < box->x1) box->x1 = point.x1;
438 if (point.y1 < box->y1) box->y1 = point.y1;
439 if (point.x2 > box->x2) box->x2 = point.x2;
440 if (point.y2 > box->y2) box->y2 = point.y2;
446 mode_geometry (XRRModeInfo *mode_info, Rotation rotation,
447 XTransform *transform,
451 int width = mode_width (mode_info, rotation);
452 int height = mode_height (mode_info, rotation);
462 path_bounds (transform, rect, 4, bounds);
465 /* v refresh frequency in Hz */
467 mode_refresh (XRRModeInfo *mode_info)
471 if (mode_info->hTotal && mode_info->vTotal)
472 rate = ((float) mode_info->dotClock /
473 ((float) mode_info->hTotal * (float) mode_info->vTotal));
479 /* h sync frequency in Hz */
481 mode_hsync (XRRModeInfo *mode_info)
485 if (mode_info->hTotal)
486 rate = (float) mode_info->dotClock / (float) mode_info->hTotal;
493 init_name (name_t *name)
495 name->kind = name_none;
499 set_name_string (name_t *name, char *string)
501 name->kind |= name_string;
502 name->string = string;
506 set_name_xid (name_t *name, XID xid)
508 name->kind |= name_xid;
513 set_name_index (name_t *name, int index)
515 name->kind |= name_index;
520 set_name_preferred (name_t *name)
522 name->kind |= name_preferred;
526 set_name_all (name_t *name, name_t *old)
528 if (old->kind & name_xid)
529 name->xid = old->xid;
530 if (old->kind & name_string)
531 name->string = old->string;
532 if (old->kind & name_index)
533 name->index = old->index;
534 name->kind |= old->kind;
538 set_name (name_t *name, char *string, name_kind_t valid)
543 if ((valid & name_xid) && sscanf (string, "0x%x", &xid) == 1)
544 set_name_xid (name, xid);
545 else if ((valid & name_index) && sscanf (string, "%d", &index) == 1)
546 set_name_index (name, index);
547 else if (valid & name_string)
548 set_name_string (name, string);
554 init_transform (transform_t *transform)
557 memset (&transform->transform, '\0', sizeof (transform->transform));
558 for (x = 0; x < 3; x++)
559 transform->transform.matrix[x][x] = XDoubleToFixed (1.0);
560 transform->filter = "";
561 transform->nparams = 0;
562 transform->params = NULL;
566 set_transform (transform_t *dest,
567 XTransform *transform,
572 dest->transform = *transform;
573 dest->filter = strdup (filter);
574 dest->nparams = nparams;
575 dest->params = malloc (nparams * sizeof (XFixed));
576 memcpy (dest->params, params, nparams * sizeof (XFixed));
580 copy_transform (transform_t *dest, transform_t *src)
582 set_transform (dest, &src->transform,
583 src->filter, src->params, src->nparams);
587 equal_transform (transform_t *a, transform_t *b)
589 if (memcmp (&a->transform, &b->transform, sizeof (XTransform)) != 0)
591 if (strcmp (a->filter, b->filter) != 0)
593 if (a->nparams != b->nparams)
595 if (memcmp (a->params, b->params, a->nparams * sizeof (XFixed)) != 0)
603 output_t *output = calloc (1, sizeof (output_t));
606 fatal ("out of memory");
608 *outputs_tail = output;
609 outputs_tail = &output->next;
614 find_output (name_t *name)
618 for (output = outputs; output; output = output->next)
620 name_kind_t common = name->kind & output->output.kind;
622 if ((common & name_xid) && name->xid == output->output.xid)
624 if ((common & name_string) && !strcmp (name->string, output->output.string))
626 if ((common & name_index) && name->index == output->output.index)
633 find_output_by_xid (RROutput output)
637 init_name (&output_name);
638 set_name_xid (&output_name, output);
639 return find_output (&output_name);
643 find_output_by_name (char *name)
647 init_name (&output_name);
648 set_name_string (&output_name, name);
649 return find_output (&output_name);
653 find_crtc (name_t *name)
658 for (c = 0; c < num_crtcs; c++)
663 common = name->kind & crtc->crtc.kind;
665 if ((common & name_xid) && name->xid == crtc->crtc.xid)
667 if ((common & name_string) && !strcmp (name->string, crtc->crtc.string))
669 if ((common & name_index) && name->index == crtc->crtc.index)
677 find_crtc_by_xid (RRCrtc crtc)
681 init_name (&crtc_name);
682 set_name_xid (&crtc_name, crtc);
683 return find_crtc (&crtc_name);
687 find_mode (name_t *name, float refresh)
690 XRRModeInfo *best = NULL;
693 for (m = 0; m < res->nmode; m++)
695 XRRModeInfo *mode = &res->modes[m];
696 if ((name->kind & name_xid) && name->xid == mode->id)
701 if ((name->kind & name_string) && !strcmp (name->string, mode->name))
706 dist = fabs (mode_refresh (mode) - refresh);
709 if (!best || dist < bestDist)
721 find_mode_by_xid (RRMode mode)
725 init_name (&mode_name);
726 set_name_xid (&mode_name, mode);
727 return find_mode (&mode_name, 0);
731 find_mode_by_name (char *name)
734 init_name (&mode_name);
735 set_name_string (&mode_name, name);
736 return find_mode (&mode_name, 0);
741 find_mode_for_output (output_t *output, name_t *name)
743 XRROutputInfo *output_info = output->output_info;
745 XRRModeInfo *best = NULL;
748 for (m = 0; m < output_info->nmode; m++)
752 mode = find_mode_by_xid (output_info->modes[m]);
754 if ((name->kind & name_xid) && name->xid == mode->id)
759 if ((name->kind & name_string) && !strcmp (name->string, mode->name))
764 dist = fabs (mode_refresh (mode) - output->refresh);
767 if (!best || dist < bestDist)
778 preferred_mode (output_t *output)
780 XRROutputInfo *output_info = output->output_info;
787 for (m = 0; m < output_info->nmode; m++)
789 XRRModeInfo *mode_info = find_mode_by_xid (output_info->modes[m]);
792 if (m < output_info->npreferred)
794 else if (output_info->mm_height)
795 dist = (1000 * DisplayHeight(dpy, screen) / DisplayHeightMM(dpy, screen) -
796 1000 * mode_info->height / output_info->mm_height);
798 dist = DisplayHeight(dpy, screen) - mode_info->height;
800 if (dist < 0) dist = -dist;
801 if (!best || dist < bestDist)
811 output_can_use_crtc (output_t *output, crtc_t *crtc)
813 XRROutputInfo *output_info = output->output_info;
816 for (c = 0; c < output_info->ncrtc; c++)
817 if (output_info->crtcs[c] == crtc->crtc.xid)
823 output_can_use_mode (output_t *output, XRRModeInfo *mode)
825 XRROutputInfo *output_info = output->output_info;
828 for (m = 0; m < output_info->nmode; m++)
829 if (output_info->modes[m] == mode->id)
835 crtc_can_use_rotation (crtc_t *crtc, Rotation rotation)
837 Rotation rotations = crtc->crtc_info->rotations;
838 Rotation dir = rotation & (RR_Rotate_0|RR_Rotate_90|RR_Rotate_180|RR_Rotate_270);
839 Rotation reflect = rotation & (RR_Reflect_X|RR_Reflect_Y);
840 if (((rotations & dir) != 0) && ((rotations & reflect) == reflect))
846 crtc_can_use_transform (crtc_t *crtc, XTransform *transform)
850 XRRQueryVersion (dpy, &major, &minor);
851 if (major > 1 || (major == 1 && minor >= 3))
857 * Report only rotations that are supported by all crtcs
860 output_rotations (output_t *output)
863 Rotation rotation = RR_Rotate_0;
864 XRROutputInfo *output_info = output->output_info;
867 for (c = 0; c < output_info->ncrtc; c++)
869 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
873 rotation = crtc->crtc_info->rotations;
876 rotation &= crtc->crtc_info->rotations;
883 output_can_use_rotation (output_t *output, Rotation rotation)
885 XRROutputInfo *output_info = output->output_info;
888 /* make sure all of the crtcs can use this rotation.
889 * yes, this is not strictly necessary, but it is
890 * simpler,and we expect most drivers to either
891 * support rotation everywhere or nowhere
893 for (c = 0; c < output_info->ncrtc; c++)
895 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
896 if (crtc && !crtc_can_use_rotation (crtc, rotation))
903 set_output_info (output_t *output, RROutput xid, XRROutputInfo *output_info)
905 /* sanity check output info */
906 if (output_info->connection != RR_Disconnected && !output_info->nmode)
907 warning ("Output %s is not disconnected but has no modes\n",
910 /* set output name and info */
911 if (!(output->output.kind & name_xid))
912 set_name_xid (&output->output, xid);
913 if (!(output->output.kind & name_string))
914 set_name_string (&output->output, output_info->name);
915 output->output_info = output_info;
917 /* set crtc name and info */
918 if (!(output->changes & changes_crtc))
919 set_name_xid (&output->crtc, output_info->crtc);
921 if (output->crtc.kind == name_xid && output->crtc.xid == None)
922 output->crtc_info = NULL;
925 output->crtc_info = find_crtc (&output->crtc);
926 if (!output->crtc_info)
928 if (output->crtc.kind & name_xid)
929 fatal ("cannot find crtc 0x%x\n", output->crtc.xid);
930 if (output->crtc.kind & name_index)
931 fatal ("cannot find crtc %d\n", output->crtc.index);
933 if (!output_can_use_crtc (output, output->crtc_info))
934 fatal ("output %s cannot use crtc 0x%x\n", output->output.string,
935 output->crtc_info->crtc.xid);
938 /* set mode name and info */
939 if (!(output->changes & changes_mode))
941 if (output->crtc_info)
942 set_name_xid (&output->mode, output->crtc_info->crtc_info->mode);
944 set_name_xid (&output->mode, None);
945 if (output->mode.xid)
947 output->mode_info = find_mode_by_xid (output->mode.xid);
948 if (!output->mode_info)
949 fatal ("server did not report mode 0x%x for output %s\n",
950 output->mode.xid, output->output.string);
953 output->mode_info = NULL;
955 else if (output->mode.kind == name_xid && output->mode.xid == None)
956 output->mode_info = NULL;
959 if (output->mode.kind == name_preferred)
960 output->mode_info = preferred_mode (output);
962 output->mode_info = find_mode_for_output (output, &output->mode);
963 if (!output->mode_info)
965 if (output->mode.kind & name_preferred)
966 fatal ("cannot find preferred mode\n");
967 if (output->mode.kind & name_string)
968 fatal ("cannot find mode %s\n", output->mode.string);
969 if (output->mode.kind & name_xid)
970 fatal ("cannot find mode 0x%x\n", output->mode.xid);
972 if (!output_can_use_mode (output, output->mode_info))
973 fatal ("output %s cannot use mode %s\n", output->output.string,
974 output->mode_info->name);
978 if (!(output->changes & changes_position))
980 if (output->crtc_info)
982 output->x = output->crtc_info->crtc_info->x;
983 output->y = output->crtc_info->crtc_info->y;
993 if (!(output->changes & changes_rotation))
995 output->rotation &= ~0xf;
996 if (output->crtc_info)
997 output->rotation |= (output->crtc_info->crtc_info->rotation & 0xf);
999 output->rotation = RR_Rotate_0;
1001 if (!(output->changes & changes_reflection))
1003 output->rotation &= ~(RR_Reflect_X|RR_Reflect_Y);
1004 if (output->crtc_info)
1005 output->rotation |= (output->crtc_info->crtc_info->rotation &
1006 (RR_Reflect_X|RR_Reflect_Y));
1008 if (!output_can_use_rotation (output, output->rotation))
1009 fatal ("output %s cannot use rotation \"%s\" reflection \"%s\"\n",
1010 output->output.string,
1011 rotation_name (output->rotation),
1012 reflection_name (output->rotation));
1014 /* set transformation */
1015 if (!(output->changes & changes_transform))
1017 if (output->crtc_info)
1018 copy_transform (&output->transform, &output->crtc_info->current_transform);
1020 init_transform (&output->transform);
1028 fatal ("Server RandR version before 1.2\n");
1030 XRRGetScreenSizeRange (dpy, root, &minWidth, &minHeight,
1031 &maxWidth, &maxHeight);
1033 res = XRRGetScreenResources (dpy, root);
1034 if (!res) fatal ("could not get screen resources");
1042 num_crtcs = res->ncrtc;
1043 crtcs = calloc (num_crtcs, sizeof (crtc_t));
1044 if (!crtcs) fatal ("out of memory");
1046 for (c = 0; c < res->ncrtc; c++)
1048 XRRCrtcInfo *crtc_info = XRRGetCrtcInfo (dpy, res, res->crtcs[c]);
1049 #if RANDR_MAJOR > 1 || RANDR_MINOR >= 3
1050 XRRCrtcTransformAttributes *attr;
1053 set_name_xid (&crtcs[c].crtc, res->crtcs[c]);
1054 set_name_index (&crtcs[c].crtc, c);
1055 if (!crtc_info) fatal ("could not get crtc 0x%x information", res->crtcs[c]);
1056 crtcs[c].crtc_info = crtc_info;
1057 if (crtc_info->mode == None)
1059 crtcs[c].mode_info = NULL;
1062 crtcs[c].rotation = RR_Rotate_0;
1064 #if RANDR_MAJOR > 1 || RANDR_MINOR >= 3
1065 XRRGetCrtcTransform (dpy, res->crtcs[c], &attr);
1067 set_transform (&crtcs[c].current_transform,
1068 &attr->currentTransform,
1069 attr->currentFilter,
1070 attr->currentParams,
1071 attr->currentNparams);
1077 init_transform (&crtcs[c].current_transform);
1079 copy_transform (&crtcs[c].pending_transform, &crtcs[c].current_transform);
1084 crtc_add_output (crtc_t *crtc, output_t *output)
1087 crtc->outputs = realloc (crtc->outputs, (crtc->noutput + 1) * sizeof (output_t *));
1090 crtc->outputs = malloc (sizeof (output_t *));
1091 crtc->x = output->x;
1092 crtc->y = output->y;
1093 crtc->rotation = output->rotation;
1094 crtc->mode_info = output->mode_info;
1095 copy_transform (&crtc->pending_transform, &output->transform);
1097 if (!crtc->outputs) fatal ("out of memory");
1098 crtc->outputs[crtc->noutput++] = output;
1106 for (output = outputs; output; output = output->next)
1108 if (!output->mode_info) continue;
1109 crtc_add_output (output->crtc_info, output);
1114 crtc_disable (crtc_t *crtc)
1117 printf ("crtc %d: disable\n", crtc->crtc.index);
1120 return RRSetConfigSuccess;
1121 return XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1122 0, 0, None, RR_Rotate_0, NULL, 0);
1126 crtc_set_transform (crtc_t *crtc, transform_t *transform)
1130 XRRQueryVersion (dpy, &major, &minor);
1131 if (major > 1 || (major == 1 && minor >= 3))
1132 XRRSetCrtcTransform (dpy, crtc->crtc.xid,
1133 &transform->transform,
1136 transform->nparams);
1140 crtc_revert (crtc_t *crtc)
1142 XRRCrtcInfo *crtc_info = crtc->crtc_info;
1145 printf ("crtc %d: revert\n", crtc->crtc.index);
1148 return RRSetConfigSuccess;
1150 crtc_set_transform (crtc, &crtc->current_transform);
1151 return XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1152 crtc_info->x, crtc_info->y,
1153 crtc_info->mode, crtc_info->rotation,
1154 crtc_info->outputs, crtc_info->noutput);
1158 crtc_apply (crtc_t *crtc)
1160 RROutput *rr_outputs;
1165 if (!crtc->changing || !crtc->mode_info)
1166 return RRSetConfigSuccess;
1168 rr_outputs = calloc (crtc->noutput, sizeof (RROutput));
1171 for (o = 0; o < crtc->noutput; o++)
1172 rr_outputs[o] = crtc->outputs[o]->output.xid;
1173 mode = crtc->mode_info->id;
1175 printf ("crtc %d: %12s %6.1f +%d+%d", crtc->crtc.index,
1176 crtc->mode_info->name, mode_refresh (crtc->mode_info),
1178 for (o = 0; o < crtc->noutput; o++)
1179 printf (" \"%s\"", crtc->outputs[o]->output.string);
1184 s = RRSetConfigSuccess;
1187 crtc_set_transform (crtc, &crtc->pending_transform);
1188 s = XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1189 crtc->x, crtc->y, mode, crtc->rotation,
1190 rr_outputs, crtc->noutput);
1197 screen_revert (void)
1200 printf ("screen %d: revert\n", screen);
1204 XRRSetScreenSize (dpy, root,
1205 DisplayWidth (dpy, screen),
1206 DisplayHeight (dpy, screen),
1207 DisplayWidthMM (dpy, screen),
1208 DisplayHeightMM (dpy, screen));
1214 if (fb_width == DisplayWidth (dpy, screen) &&
1215 fb_height == DisplayHeight (dpy, screen) &&
1216 fb_width_mm == DisplayWidthMM (dpy, screen) &&
1217 fb_height_mm == DisplayHeightMM (dpy, screen))
1222 printf ("screen %d: %dx%d %dx%d mm %6.2fdpi\n", screen,
1223 fb_width, fb_height, fb_width_mm, fb_height_mm, dpi);
1226 XRRSetScreenSize (dpy, root, fb_width, fb_height,
1227 fb_width_mm, fb_height_mm);
1235 /* first disable all crtcs */
1236 for (c = 0; c < res->ncrtc; c++)
1237 crtc_disable (&crtcs[c]);
1238 /* next reset screen size */
1240 /* now restore all crtcs */
1241 for (c = 0; c < res->ncrtc; c++)
1242 crtc_revert (&crtcs[c]);
1246 * uh-oh, something bad happened in the middle of changing
1247 * the configuration. Revert to the previous configuration
1251 panic (Status s, crtc_t *crtc)
1253 int c = crtc->crtc.index;
1257 case RRSetConfigSuccess: message = "succeeded"; break;
1258 case BadAlloc: message = "out of memory"; break;
1259 case RRSetConfigFailed: message = "failed"; break;
1260 case RRSetConfigInvalidConfigTime: message = "invalid config time"; break;
1261 case RRSetConfigInvalidTime: message = "invalid time"; break;
1262 default: message = "unknown failure"; break;
1265 fprintf (stderr, "%s: Configure crtc %d %s\n", program_name, c, message);
1277 * Turn off any crtcs which are to be disabled or which are
1278 * larger than the target size
1280 for (c = 0; c < res->ncrtc; c++)
1282 crtc_t *crtc = &crtcs[c];
1283 XRRCrtcInfo *crtc_info = crtc->crtc_info;
1285 /* if this crtc is already disabled, skip it */
1286 if (crtc_info->mode == None)
1290 * If this crtc is to be left enabled, make
1291 * sure the old size fits then new screen
1293 if (crtc->mode_info)
1295 XRRModeInfo *old_mode = find_mode_by_xid (crtc_info->mode);
1300 panic (RRSetConfigFailed, crtc);
1302 /* old position and size information */
1303 mode_geometry (old_mode, crtc_info->rotation,
1304 &crtc->current_transform.transform,
1307 x = crtc_info->x + bounds.x1;
1308 y = crtc_info->y + bounds.y1;
1309 w = bounds.x2 - bounds.x1;
1310 h = bounds.y2 - bounds.y1;
1312 /* if it fits, skip it */
1313 if (x + w <= fb_width && y + h <= fb_height)
1315 crtc->changing = True;
1317 s = crtc_disable (crtc);
1318 if (s != RRSetConfigSuccess)
1323 * Hold the server grabbed while messing with
1324 * the screen so that apps which notice the resize
1325 * event and ask for xinerama information from the server
1326 * receive up-to-date information
1331 * Set the screen size
1339 for (c = 0; c < res->ncrtc; c++)
1341 crtc_t *crtc = &crtcs[c];
1343 s = crtc_apply (crtc);
1344 if (s != RRSetConfigSuccess)
1348 * Release the server grab and let all clients
1349 * respond to the updated state
1351 XUngrabServer (dpy);
1355 * Use current output state to complete the output list
1362 for (o = 0; o < res->noutput; o++)
1364 XRROutputInfo *output_info = XRRGetOutputInfo (dpy, res, res->outputs[o]);
1367 if (!output_info) fatal ("could not get output 0x%x information", res->outputs[o]);
1368 set_name_xid (&output_name, res->outputs[o]);
1369 set_name_index (&output_name, o);
1370 set_name_string (&output_name, output_info->name);
1371 output = find_output (&output_name);
1374 output = add_output ();
1375 set_name_all (&output->output, &output_name);
1377 * When global --automatic mode is set, turn on connected but off
1378 * outputs, turn off disconnected but on outputs
1382 switch (output_info->connection) {
1384 if (!output_info->crtc) {
1385 output->changes |= changes_automatic;
1386 output->automatic = True;
1389 case RR_Disconnected:
1390 if (output_info->crtc)
1392 output->changes |= changes_automatic;
1393 output->automatic = True;
1401 * Automatic mode -- track connection state and enable/disable outputs
1404 if (output->automatic)
1406 switch (output_info->connection) {
1408 case RR_UnknownConnection:
1409 if ((!(output->changes & changes_mode)))
1411 set_name_preferred (&output->mode);
1412 output->changes |= changes_mode;
1415 case RR_Disconnected:
1416 if ((!(output->changes & changes_mode)))
1418 set_name_xid (&output->mode, None);
1419 set_name_xid (&output->crtc, None);
1420 output->changes |= changes_mode;
1421 output->changes |= changes_crtc;
1427 set_output_info (output, res->outputs[o], output_info);
1432 mark_changing_crtcs (void)
1436 for (c = 0; c < num_crtcs; c++)
1438 crtc_t *crtc = &crtcs[c];
1442 /* walk old output list (to catch disables) */
1443 for (o = 0; o < crtc->crtc_info->noutput; o++)
1445 output = find_output_by_xid (crtc->crtc_info->outputs[o]);
1446 if (!output) fatal ("cannot find output 0x%x\n",
1447 crtc->crtc_info->outputs[o]);
1448 if (output->changes)
1449 crtc->changing = True;
1451 /* walk new output list */
1452 for (o = 0; o < crtc->noutput; o++)
1454 output = crtc->outputs[o];
1455 if (output->changes)
1456 crtc->changing = True;
1462 * Test whether 'crtc' can be used for 'output'
1465 check_crtc_for_output (crtc_t *crtc, output_t *output, Bool ignore_state)
1471 for (c = 0; c < output->output_info->ncrtc; c++)
1472 if (output->output_info->crtcs[c] == crtc->crtc.xid)
1474 if (c == output->output_info->ncrtc)
1476 for (other = outputs; other; other = other->next)
1478 if (other == output)
1481 if (other->mode_info == NULL)
1484 if (other->crtc_info != crtc)
1487 /* see if the output connected to the crtc can clone to this output */
1488 for (l = 0; l < output->output_info->nclone; l++)
1489 if (output->output_info->clones[l] == other->output.xid)
1491 /* not on the list, can't clone */
1492 if (l == output->output_info->nclone)
1501 /* make sure the state matches */
1502 if (crtc->mode_info != output->mode_info)
1504 if (crtc->x != output->x)
1506 if (crtc->y != output->y)
1508 if (crtc->rotation != output->rotation)
1510 if (!equal_transform (&crtc->current_transform, &output->transform))
1513 else if (crtc->crtc_info->noutput)
1515 /* make sure the state matches the already used state */
1516 XRRModeInfo *mode = find_mode_by_xid (crtc->crtc_info->mode);
1518 if (mode != output->mode_info)
1520 if (crtc->crtc_info->x != output->x)
1522 if (crtc->crtc_info->y != output->y)
1524 if (crtc->crtc_info->rotation != output->rotation)
1531 find_crtc_for_output (output_t *output)
1535 for (c = 0; c < output->output_info->ncrtc; c++)
1539 crtc = find_crtc_by_xid (output->output_info->crtcs[c]);
1540 if (!crtc) fatal ("cannot find crtc 0x%x\n", output->output_info->crtcs[c]);
1542 if (check_crtc_for_output (crtc, output, False))
1549 set_positions (void)
1560 for (output = outputs; output; output = output->next)
1563 name_t relation_name;
1565 if (!(output->changes & changes_relation)) continue;
1567 if (output->mode_info == NULL) continue;
1569 init_name (&relation_name);
1570 set_name_string (&relation_name, output->relative_to);
1571 relation = find_output (&relation_name);
1572 if (!relation) fatal ("cannot find output \"%s\"\n", output->relative_to);
1574 if (relation->mode_info == NULL)
1578 output->changes |= changes_position;
1583 * Make sure the dependent object has been set in place
1585 if ((relation->changes & changes_relation) &&
1586 !(relation->changes & changes_position))
1592 switch (output->relation) {
1594 output->y = relation->y;
1595 output->x = relation->x - mode_width (output->mode_info, output->rotation);
1598 output->y = relation->y;
1599 output->x = relation->x + mode_width (relation->mode_info, relation->rotation);
1602 output->x = relation->x;
1603 output->y = relation->y - mode_height (output->mode_info, output->rotation);
1606 output->x = relation->x;
1607 output->y = relation->y + mode_height (relation->mode_info, relation->rotation);
1610 output->x = relation->x;
1611 output->y = relation->y;
1613 output->changes |= changes_position;
1619 fatal ("loop in relative position specifications\n");
1623 * Now normalize positions so the upper left corner of all outputs is at 0,0
1627 for (output = outputs; output; output = output->next)
1629 if (output->mode_info == NULL) continue;
1631 if (output->x < min_x) min_x = output->x;
1632 if (output->y < min_y) min_y = output->y;
1636 /* move all outputs */
1637 for (output = outputs; output; output = output->next)
1639 if (output->mode_info == NULL) continue;
1643 output->changes |= changes_position;
1649 set_screen_size (void)
1652 Bool fb_specified = fb_width != 0 && fb_height != 0;
1654 for (output = outputs; output; output = output->next)
1656 XRRModeInfo *mode_info = output->mode_info;
1660 if (!mode_info) continue;
1662 mode_geometry (mode_info, output->rotation,
1663 &output->transform.transform,
1665 x = output->x + bounds.x1;
1666 y = output->y + bounds.y1;
1667 w = bounds.x2 - bounds.x1;
1668 h = bounds.y2 - bounds.y1;
1669 /* make sure output fits in specified size */
1672 if (x + w > fb_width || y + h > fb_height)
1673 fatal ("specified screen %dx%d not large enough for output %s (%dx%d+%d+%d)\n",
1674 fb_width, fb_height, output->output.string, w, h, x, y);
1676 /* fit fb to output */
1679 if (x + w > fb_width) fb_width = x + w;
1680 if (y + h > fb_height) fb_height = y + h;
1684 if (fb_width > maxWidth || fb_height > maxHeight)
1685 fatal ("screen cannot be larger than %dx%d (desired size %dx%d)\n",
1686 maxWidth, maxHeight, fb_width, fb_height);
1689 if (fb_width < minWidth || fb_height < minHeight)
1690 fatal ("screen must be at least %dx%d\n", minWidth, minHeight);
1694 if (fb_width < minWidth) fb_width = minWidth;
1695 if (fb_height < minHeight) fb_height = minHeight;
1702 disable_outputs (output_t *outputs)
1706 outputs->crtc_info = NULL;
1707 outputs = outputs->next;
1712 * find the best mapping from output to crtc available
1715 pick_crtcs_score (output_t *outputs)
1728 outputs = outputs->next;
1730 * Score with this output disabled
1732 output->crtc_info = NULL;
1733 best_score = pick_crtcs_score (outputs);
1734 if (output->mode_info == NULL)
1739 * Now score with this output any valid crtc
1741 for (c = 0; c < output->output_info->ncrtc; c++)
1745 crtc = find_crtc_by_xid (output->output_info->crtcs[c]);
1747 fatal ("cannot find crtc 0x%x\n", output->output_info->crtcs[c]);
1749 /* reset crtc allocation for following outputs */
1750 disable_outputs (outputs);
1751 if (!check_crtc_for_output (crtc, output, True))
1755 /* slight preference for existing connections */
1756 if (crtc == output->current_crtc_info)
1759 output->crtc_info = crtc;
1760 score = my_score + pick_crtcs_score (outputs);
1761 if (score > best_score)
1767 if (output->crtc_info != best_crtc)
1768 output->crtc_info = best_crtc;
1770 * Reset other outputs based on this one using the best crtc
1772 (void) pick_crtcs_score (outputs);
1778 * Pick crtcs for any changing outputs that don't have one
1786 * First try to match up newly enabled outputs with spare crtcs
1788 for (output = outputs; output; output = output->next)
1790 if (output->changes && output->mode_info)
1792 if (output->crtc_info) {
1793 if (output->crtc_info->crtc_info->noutput > 0 &&
1794 (output->crtc_info->crtc_info->noutput > 1 ||
1795 output != find_output_by_xid (output->crtc_info->crtc_info->outputs[0])))
1798 output->crtc_info = find_crtc_for_output (output);
1799 if (!output->crtc_info)
1810 * When the simple way fails, see if there is a way
1811 * to swap crtcs around and make things work
1813 for (output = outputs; output; output = output->next)
1814 output->current_crtc_info = output->crtc_info;
1815 pick_crtcs_score (outputs);
1816 for (output = outputs; output; output = output->next)
1818 if (output->mode_info && !output->crtc_info)
1819 fatal ("cannot find crtc for output %s\n", output->output.string);
1820 if (!output->changes && output->crtc_info != output->current_crtc_info)
1821 output->changes |= changes_crtc;
1826 main (int argc, char **argv)
1828 XRRScreenSize *sizes;
1829 XRRScreenConfiguration *sc;
1833 Status status = RRSetConfigFailed;
1836 Rotation rotation, current_rotation, rotations;
1838 XRRScreenChangeNotifyEvent *sce;
1839 char *display_name = NULL;
1841 SizeID current_size;
1847 Bool version = False;
1848 int event_base, error_base;
1850 int width = 0, height = 0;
1851 Bool have_pixel_size = False;
1854 output_t *output = NULL;
1855 policy_t policy = clone;
1856 Bool setit_1_2 = False;
1857 Bool query_1_2 = False;
1858 Bool modeit = False;
1859 Bool propit = False;
1860 Bool query_1 = False;
1864 program_name = argv[0];
1865 if (argc == 1) query = True;
1866 for (i = 1; i < argc; i++) {
1867 if (!strcmp ("-display", argv[i]) || !strcmp ("-d", argv[i])) {
1868 if (++i>=argc) usage ();
1869 display_name = argv[i];
1872 if (!strcmp("-help", argv[i])) {
1876 if (!strcmp ("--verbose", argv[i])) {
1880 if (!strcmp ("--dryrun", argv[i])) {
1886 if (!strcmp ("-s", argv[i]) || !strcmp ("--size", argv[i])) {
1887 if (++i>=argc) usage ();
1888 if (sscanf (argv[i], "%dx%d", &width, &height) == 2)
1889 have_pixel_size = True;
1891 size = atoi (argv[i]);
1892 if (size < 0) usage();
1898 if (!strcmp ("-r", argv[i]) ||
1899 !strcmp ("--rate", argv[i]) ||
1900 !strcmp ("--refresh", argv[i]))
1902 if (++i>=argc) usage ();
1903 if (sscanf (argv[i], "%f", &rate) != 1)
1909 output->refresh = rate;
1910 output->changes |= changes_refresh;
1917 if (!strcmp ("-v", argv[i]) || !strcmp ("--version", argv[i])) {
1922 if (!strcmp ("-x", argv[i])) {
1923 reflection |= RR_Reflect_X;
1927 if (!strcmp ("-y", argv[i])) {
1928 reflection |= RR_Reflect_Y;
1932 if (!strcmp ("--screen", argv[i])) {
1933 if (++i>=argc) usage ();
1934 screen = atoi (argv[i]);
1935 if (screen < 0) usage();
1938 if (!strcmp ("-q", argv[i]) || !strcmp ("--query", argv[i])) {
1942 if (!strcmp ("-o", argv[i]) || !strcmp ("--orientation", argv[i])) {
1944 if (++i>=argc) usage ();
1945 dirind = strtol(argv[i], &endptr, 0);
1946 if (*endptr != '\0') {
1947 for (dirind = 0; dirind < 4; dirind++) {
1948 if (strcmp (direction[dirind], argv[i]) == 0) break;
1950 if ((dirind < 0) || (dirind > 3)) usage();
1957 if (!strcmp ("--prop", argv[i]) || !strcmp ("--properties", argv[i]))
1963 if (!strcmp ("--output", argv[i])) {
1964 if (++i >= argc) usage();
1966 output = find_output_by_name (argv[i]);
1968 output = add_output ();
1969 set_name (&output->output, argv[i], name_string|name_xid);
1975 if (!strcmp ("--crtc", argv[i])) {
1976 if (++i >= argc) usage();
1977 if (!output) usage();
1978 set_name (&output->crtc, argv[i], name_xid|name_index);
1979 output->changes |= changes_crtc;
1982 if (!strcmp ("--mode", argv[i])) {
1983 if (++i >= argc) usage();
1984 if (!output) usage();
1985 set_name (&output->mode, argv[i], name_string|name_xid);
1986 output->changes |= changes_mode;
1989 if (!strcmp ("--preferred", argv[i])) {
1990 if (!output) usage();
1991 set_name_preferred (&output->mode);
1992 output->changes |= changes_mode;
1995 if (!strcmp ("--pos", argv[i])) {
1996 if (++i>=argc) usage ();
1997 if (!output) usage();
1998 if (sscanf (argv[i], "%dx%d",
1999 &output->x, &output->y) != 2)
2001 output->changes |= changes_position;
2004 if (!strcmp ("--rotation", argv[i]) || !strcmp ("--rotate", argv[i])) {
2005 if (++i>=argc) usage ();
2006 if (!output) usage();
2007 for (dirind = 0; dirind < 4; dirind++) {
2008 if (strcmp (direction[dirind], argv[i]) == 0) break;
2012 output->rotation &= ~0xf;
2013 output->rotation |= 1 << dirind;
2014 output->changes |= changes_rotation;
2017 if (!strcmp ("--reflect", argv[i]) || !strcmp ("--reflection", argv[i])) {
2018 if (++i>=argc) usage ();
2019 if (!output) usage();
2020 for (dirind = 0; dirind < 4; dirind++) {
2021 if (strcmp (reflections[dirind], argv[i]) == 0) break;
2025 output->rotation &= ~(RR_Reflect_X|RR_Reflect_Y);
2026 output->rotation |= dirind * RR_Reflect_X;
2027 output->changes |= changes_reflection;
2030 if (!strcmp ("--left-of", argv[i])) {
2031 if (++i>=argc) usage ();
2032 if (!output) usage();
2033 output->relation = left_of;
2034 output->relative_to = argv[i];
2035 output->changes |= changes_relation;
2038 if (!strcmp ("--right-of", argv[i])) {
2039 if (++i>=argc) usage ();
2040 if (!output) usage();
2041 output->relation = right_of;
2042 output->relative_to = argv[i];
2043 output->changes |= changes_relation;
2046 if (!strcmp ("--above", argv[i])) {
2047 if (++i>=argc) usage ();
2048 if (!output) usage();
2049 output->relation = above;
2050 output->relative_to = argv[i];
2051 output->changes |= changes_relation;
2054 if (!strcmp ("--below", argv[i])) {
2055 if (++i>=argc) usage ();
2056 if (!output) usage();
2057 output->relation = below;
2058 output->relative_to = argv[i];
2059 output->changes |= changes_relation;
2062 if (!strcmp ("--same-as", argv[i])) {
2063 if (++i>=argc) usage ();
2064 if (!output) usage();
2065 output->relation = same_as;
2066 output->relative_to = argv[i];
2067 output->changes |= changes_relation;
2070 if (!strcmp ("--set", argv[i])) {
2071 output_prop_t *prop;
2072 if (!output) usage();
2073 prop = malloc (sizeof (output_prop_t));
2074 prop->next = output->props;
2075 output->props = prop;
2076 if (++i>=argc) usage ();
2077 prop->name = argv[i];
2078 if (++i>=argc) usage ();
2079 prop->value = argv[i];
2081 output->changes |= changes_property;
2085 if (!strcmp ("--scale", argv[i]))
2088 if (++i>=argc) usage();
2089 if (sscanf (argv[i], "%lfx%lf", &sx, &sy) != 2)
2091 init_transform (&output->transform);
2092 output->transform.transform.matrix[0][0] = XDoubleToFixed (sx);
2093 output->transform.transform.matrix[1][1] = XDoubleToFixed (sy);
2094 output->transform.transform.matrix[2][2] = XDoubleToFixed (1.0);
2095 if (sx != 1 || sy != 1)
2096 output->transform.filter = "bilinear";
2098 output->transform.filter = "nearest";
2099 output->transform.nparams = 0;
2100 output->transform.params = NULL;
2101 output->changes |= changes_transform;
2104 if (!strcmp ("--transform", argv[i])) {
2105 double transform[3][3];
2107 if (++i>=argc) usage ();
2108 if (sscanf(argv[i], "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf",
2109 &transform[0][0],&transform[0][1],&transform[0][2],
2110 &transform[1][0],&transform[1][1],&transform[1][2],
2111 &transform[2][0],&transform[2][1],&transform[2][2])
2114 init_transform (&output->transform);
2115 for (k = 0; k < 3; k++)
2116 for (l = 0; l < 3; l++) {
2117 output->transform.transform.matrix[k][l] = XDoubleToFixed (transform[k][l]);
2119 output->transform.filter = "bilinear";
2120 output->transform.nparams = 0;
2121 output->transform.params = NULL;
2122 output->changes |= changes_transform;
2125 if (!strcmp ("--off", argv[i])) {
2126 if (!output) usage();
2127 set_name_xid (&output->mode, None);
2128 set_name_xid (&output->crtc, None);
2129 output->changes |= changes_mode;
2132 if (!strcmp ("--fb", argv[i])) {
2133 if (++i>=argc) usage ();
2134 if (sscanf (argv[i], "%dx%d",
2135 &fb_width, &fb_height) != 2)
2140 if (!strcmp ("--fbmm", argv[i])) {
2141 if (++i>=argc) usage ();
2142 if (sscanf (argv[i], "%dx%d",
2143 &fb_width_mm, &fb_height_mm) != 2)
2148 if (!strcmp ("--dpi", argv[i])) {
2149 if (++i>=argc) usage ();
2150 if (sscanf (argv[i], "%f", &dpi) != 1)
2153 dpi_output = argv[i];
2158 if (!strcmp ("--clone", argv[i])) {
2163 if (!strcmp ("--extend", argv[i])) {
2168 if (!strcmp ("--auto", argv[i])) {
2171 output->automatic = True;
2172 output->changes |= changes_automatic;
2179 if (!strcmp ("--q12", argv[i]))
2184 if (!strcmp ("--q1", argv[i]))
2189 if (!strcmp ("--newmode", argv[i]))
2191 umode_t *m = malloc (sizeof (umode_t));
2195 if (i + 9 >= argc) usage ();
2196 m->mode.name = argv[i];
2197 m->mode.nameLength = strlen (argv[i]);
2199 if (sscanf (argv[i++], "%f", &clock) != 1)
2201 m->mode.dotClock = clock * 1e6;
2203 if (sscanf (argv[i++], "%d", &m->mode.width) != 1) usage();
2204 if (sscanf (argv[i++], "%d", &m->mode.hSyncStart) != 1) usage();
2205 if (sscanf (argv[i++], "%d", &m->mode.hSyncEnd) != 1) usage();
2206 if (sscanf (argv[i++], "%d", &m->mode.hTotal) != 1) usage();
2207 if (sscanf (argv[i++], "%d", &m->mode.height) != 1) usage();
2208 if (sscanf (argv[i++], "%d", &m->mode.vSyncStart) != 1) usage();
2209 if (sscanf (argv[i++], "%d", &m->mode.vSyncEnd) != 1) usage();
2210 if (sscanf (argv[i++], "%d", &m->mode.vTotal) != 1) usage();
2211 m->mode.modeFlags = 0;
2215 for (f = 0; mode_flags[f].string; f++)
2216 if (!strcasecmp (mode_flags[f].string, argv[i]))
2219 if (!mode_flags[f].string)
2221 m->mode.modeFlags |= mode_flags[f].flag;
2225 m->action = umode_create;
2230 if (!strcmp ("--rmmode", argv[i]))
2232 umode_t *m = malloc (sizeof (umode_t));
2234 if (++i>=argc) usage ();
2235 set_name (&m->name, argv[i], name_string|name_xid);
2236 m->action = umode_destroy;
2242 if (!strcmp ("--addmode", argv[i]))
2244 umode_t *m = malloc (sizeof (umode_t));
2246 if (++i>=argc) usage ();
2247 set_name (&m->output, argv[i], name_string|name_xid);
2248 if (++i>=argc) usage();
2249 set_name (&m->name, argv[i], name_string|name_xid);
2250 m->action = umode_add;
2256 if (!strcmp ("--delmode", argv[i]))
2258 umode_t *m = malloc (sizeof (umode_t));
2260 if (++i>=argc) usage ();
2261 set_name (&m->output, argv[i], name_string|name_xid);
2262 if (++i>=argc) usage();
2263 set_name (&m->name, argv[i], name_string|name_xid);
2264 m->action = umode_delete;
2276 if (setit && !setit_1_2)
2280 dpy = XOpenDisplay (display_name);
2283 fprintf (stderr, "Can't open display %s\n", XDisplayName(display_name));
2287 screen = DefaultScreen (dpy);
2288 if (screen >= ScreenCount (dpy)) {
2289 fprintf (stderr, "Invalid screen number %d (display has %d)\n",
2290 screen, ScreenCount (dpy));
2294 root = RootWindow (dpy, screen);
2297 if (!XRRQueryVersion (dpy, &major, &minor))
2299 fprintf (stderr, "RandR extension missing\n");
2302 if (major > 1 || (major == 1 && minor >= 2))
2305 if (has_1_2 && modeit)
2313 for (m = umodes; m; m = m->next)
2318 switch (m->action) {
2320 XRRCreateMode (dpy, root, &m->mode);
2323 e = find_mode (&m->name, 0);
2325 fatal ("cannot find mode \"%s\"\n", m->name.string);
2326 XRRDestroyMode (dpy, e->id);
2329 o = find_output (&m->output);
2331 fatal ("cannot find output \"%s\"\n", m->output.string);
2332 e = find_mode (&m->name, 0);
2334 fatal ("cannot find mode \"%s\"\n", m->name.string);
2335 XRRAddOutputMode (dpy, o->output.xid, e->id);
2338 o = find_output (&m->output);
2340 fatal ("cannot find output \"%s\"\n", m->output.string);
2341 e = find_mode (&m->name, 0);
2343 fatal ("cannot find mode \"%s\"\n", m->name.string);
2344 XRRDeleteOutputMode (dpy, o->output.xid, e->id);
2354 if (has_1_2 && propit)
2361 for (output = outputs; output; output = output->next)
2363 output_prop_t *prop;
2365 for (prop = output->props; prop; prop = prop->next)
2367 Atom name = XInternAtom (dpy, prop->name, False);
2370 unsigned char *data;
2373 unsigned long ulong_value;
2374 unsigned char *prop_data;
2376 unsigned long nitems, bytes_after;
2378 XRRPropertyInfo *propinfo;
2380 type = AnyPropertyType;
2383 if (XRRGetOutputProperty (dpy, output->output.xid, name,
2384 0, 100, False, False,
2386 &actual_type, &actual_format,
2387 &nitems, &bytes_after, &prop_data) == Success &&
2389 (propinfo = XRRQueryOutputProperty(dpy, output->output.xid,
2393 format = actual_format;
2396 if ((type == XA_INTEGER || type == AnyPropertyType) &&
2397 (sscanf (prop->value, "%d", &int_value) == 1 ||
2398 sscanf (prop->value, "0x%x", &int_value) == 1))
2401 ulong_value = int_value;
2402 data = (unsigned char *) &ulong_value;
2406 else if ((type == XA_ATOM))
2408 ulong_value = XInternAtom (dpy, prop->value, False);
2409 data = (unsigned char *) &ulong_value;
2413 else if ((type == XA_STRING || type == AnyPropertyType))
2416 data = (unsigned char *) prop->value;
2417 nelements = strlen (prop->value);
2420 XRRChangeOutputProperty (dpy, output->output.xid,
2421 name, type, format, PropModeReplace,
2442 * Assign outputs to crtcs
2447 * Mark changing crtcs
2449 mark_changing_crtcs ();
2452 * If an output was specified to track dpi, use it
2456 output_t *output = find_output_by_name (dpi_output);
2457 XRROutputInfo *output_info;
2458 XRRModeInfo *mode_info;
2460 fatal ("Cannot find output %s\n", dpi_output);
2461 output_info = output->output_info;
2462 mode_info = output->mode_info;
2463 if (output_info && mode_info && output_info->mm_height)
2466 * When this output covers the whole screen, just use
2467 * the known physical size
2469 if (fb_width == mode_info->width &&
2470 fb_height == mode_info->height)
2472 fb_width_mm = output_info->mm_width;
2473 fb_height_mm = output_info->mm_height;
2477 dpi = (25.4 * mode_info->height) / output_info->mm_height;
2483 * Compute physical screen size
2485 if (fb_width_mm == 0 || fb_height_mm == 0)
2487 if (fb_width != DisplayWidth (dpy, screen) ||
2488 fb_height != DisplayHeight (dpy, screen) || dpi != 0.0)
2491 dpi = (25.4 * DisplayHeight (dpy, screen)) / DisplayHeightMM(dpy, screen);
2493 fb_width_mm = (25.4 * fb_width) / dpi;
2494 fb_height_mm = (25.4 * fb_height) / dpi;
2498 fb_width_mm = DisplayWidthMM (dpy, screen);
2499 fb_height_mm = DisplayHeightMM (dpy, screen);
2504 * Now apply all of the changes
2511 if (query_1_2 || (query && has_1_2 && !query_1))
2516 #define ModeShown 0x80000000
2522 printf ("Screen %d: minimum %d x %d, current %d x %d, maximum %d x %d\n",
2523 screen, minWidth, minHeight,
2524 DisplayWidth (dpy, screen), DisplayHeight(dpy, screen),
2525 maxWidth, maxHeight);
2527 for (output = outputs; output; output = output->next)
2529 XRROutputInfo *output_info = output->output_info;
2530 crtc_t *crtc = output->crtc_info;
2531 XRRCrtcInfo *crtc_info = crtc ? crtc->crtc_info : NULL;
2532 XRRModeInfo *mode = output->mode_info;
2536 Rotation rotations = output_rotations (output);
2538 printf ("%s %s", output_info->name, connection[output_info->connection]);
2542 printf (" %dx%d+%d+%d",
2543 crtc_info->width, crtc_info->height,
2544 crtc_info->x, crtc_info->y);
2546 printf (" %dx%d+%d+%d",
2547 mode->width, mode->height, output->x, output->y);
2550 printf (" (0x%x)", mode->id);
2551 if (output->rotation != RR_Rotate_0 || verbose)
2554 rotation_name (output->rotation));
2555 if (output->rotation & (RR_Reflect_X|RR_Reflect_Y))
2556 printf (" %s", reflection_name (output->rotation));
2559 if (rotations != RR_Rotate_0 || verbose)
2563 for (i = 0; i < 4; i ++) {
2564 if ((rotations >> i) & 1) {
2565 if (!first) printf (" "); first = False;
2566 printf("%s", direction[i]);
2570 if (rotations & RR_Reflect_X)
2572 if (!first) printf (" "); first = False;
2575 if (rotations & RR_Reflect_Y)
2577 if (!first) printf (" "); first = False;
2585 printf (" %dmm x %dmm",
2586 output_info->mm_width, output_info->mm_height);
2592 printf ("\tIdentifier: 0x%x\n", output->output.xid);
2593 printf ("\tTimestamp: %d\n", output_info->timestamp);
2594 printf ("\tSubpixel: %s\n", order[output_info->subpixel_order]);
2595 printf ("\tClones: ");
2596 for (j = 0; j < output_info->nclone; j++)
2598 output_t *clone = find_output_by_xid (output_info->clones[j]);
2600 if (clone) printf (" %s", clone->output.string);
2603 if (output->crtc_info)
2604 printf ("\tCRTC: %d\n", output->crtc_info->crtc.index);
2605 printf ("\tCRTCs: ");
2606 for (j = 0; j < output_info->ncrtc; j++)
2608 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[j]);
2610 printf (" %d", crtc->crtc.index);
2618 printf ("\tTransform: ");
2619 for (y = 0; y < 3; y++)
2621 for (x = 0; x < 3; x++)
2622 printf (" %f", XFixedToDouble (output->transform.transform.matrix[y][x]));
2626 if (output->transform.filter)
2627 printf ("\n\t filter: %s", output->transform.filter);
2630 if (verbose || properties)
2632 props = XRRListOutputProperties (dpy, output->output.xid,
2634 for (j = 0; j < nprop; j++) {
2635 unsigned char *prop;
2637 unsigned long nitems, bytes_after;
2639 XRRPropertyInfo *propinfo;
2641 XRRGetOutputProperty (dpy, output->output.xid, props[j],
2642 0, 100, False, False,
2644 &actual_type, &actual_format,
2645 &nitems, &bytes_after, &prop);
2647 propinfo = XRRQueryOutputProperty(dpy, output->output.xid,
2650 if (actual_type == XA_INTEGER && actual_format == 8) {
2653 printf("\t%s:\n", XGetAtomName (dpy, props[j]));
2654 for (k = 0; k < nitems; k++) {
2657 printf("%02x", (unsigned char)prop[k]);
2661 } else if (actual_type == XA_INTEGER &&
2662 actual_format == 32)
2664 printf("\t%s: %d (0x%08x)",
2665 XGetAtomName (dpy, props[j]),
2666 *(INT32 *)prop, *(INT32 *)prop);
2668 if (propinfo->range && propinfo->num_values > 0) {
2669 printf(" range%s: ",
2670 (propinfo->num_values == 2) ? "" : "s");
2672 for (k = 0; k < propinfo->num_values / 2; k++)
2673 printf(" (%d,%d)", propinfo->values[k * 2],
2674 propinfo->values[k * 2 + 1]);
2678 } else if (actual_type == XA_ATOM &&
2679 actual_format == 32)
2682 XGetAtomName (dpy, props[j]),
2683 XGetAtomName (dpy, *(Atom *)prop));
2685 if (!propinfo->range && propinfo->num_values > 0) {
2686 printf("\n\t\tsupported:");
2688 for (k = 0; k < propinfo->num_values; k++)
2690 printf(" %-12.12s", XGetAtomName (dpy,
2691 propinfo->values[k]));
2692 if (k % 4 == 3 && k < propinfo->num_values - 1)
2697 } else if (actual_format == 8) {
2698 printf ("\t\t%s: %s%s\n", XGetAtomName (dpy, props[j]),
2699 prop, bytes_after ? "..." : "");
2701 char *type = actual_type ? XGetAtomName (dpy, actual_type) : "none";
2702 printf ("\t\t%s: %s(%d) (format %d items %d) ????\n",
2703 XGetAtomName (dpy, props[j]),
2704 type, actual_type, actual_format, nitems);
2713 for (j = 0; j < output_info->nmode; j++)
2715 XRRModeInfo *mode = find_mode_by_xid (output_info->modes[j]);
2718 printf (" %s (0x%x) %6.1fMHz",
2719 mode->name, mode->id,
2720 (float)mode->dotClock / 1000000.0);
2721 for (f = 0; mode_flags[f].flag; f++)
2722 if (mode->modeFlags & mode_flags[f].flag)
2723 printf (" %s", mode_flags[f].string);
2724 if (mode == output->mode_info)
2725 printf (" *current");
2726 if (j < output_info->npreferred)
2727 printf (" +preferred");
2729 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
2730 mode->width, mode->hSyncStart, mode->hSyncEnd,
2731 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
2732 printf (" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
2733 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
2734 mode_refresh (mode));
2735 mode->modeFlags |= ModeShown;
2740 mode_shown = calloc (output_info->nmode, sizeof (Bool));
2741 if (!mode_shown) fatal ("out of memory\n");
2742 for (j = 0; j < output_info->nmode; j++)
2744 XRRModeInfo *jmode, *kmode;
2746 if (mode_shown[j]) continue;
2748 jmode = find_mode_by_xid (output_info->modes[j]);
2750 printf (" %-12s", jmode->name);
2751 for (k = j; k < output_info->nmode; k++)
2753 if (mode_shown[k]) continue;
2754 kmode = find_mode_by_xid (output_info->modes[k]);
2755 if (strcmp (jmode->name, kmode->name) != 0) continue;
2756 mode_shown[k] = True;
2757 kmode->modeFlags |= ModeShown;
2758 printf (" %6.1f", mode_refresh (kmode));
2759 if (kmode == output->mode_info)
2763 if (k < output_info->npreferred)
2773 for (m = 0; m < res->nmode; m++)
2775 XRRModeInfo *mode = &res->modes[m];
2777 if (!(mode->modeFlags & ModeShown))
2779 printf (" %s (0x%x) %6.1fMHz\n",
2780 mode->name, mode->id,
2781 (float)mode->dotClock / 1000000.0);
2782 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
2783 mode->width, mode->hSyncStart, mode->hSyncEnd,
2784 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
2785 printf (" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
2786 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
2787 mode_refresh (mode));
2794 sc = XRRGetScreenInfo (dpy, root);
2799 current_size = XRRConfigCurrentConfiguration (sc, ¤t_rotation);
2801 sizes = XRRConfigSizes(sc, &nsize);
2803 if (have_pixel_size) {
2804 for (size = 0; size < nsize; size++)
2806 if (sizes[size].width == width && sizes[size].height == height)
2809 if (size >= nsize) {
2811 "Size %dx%d not found in available modes\n", width, height);
2816 size = current_size;
2817 else if (size >= nsize) {
2819 "Size index %d is too large, there are only %d sizes\n",
2826 for (rot = 0; rot < 4; rot++)
2827 if (1 << rot == (current_rotation & 0xf))
2831 current_rate = XRRConfigCurrentRate (sc);
2835 if (size == current_size)
2836 rate = current_rate;
2842 rates = XRRConfigRates (sc, size, &nrate);
2843 for (i = 0; i < nrate; i++)
2844 if (rate == rates[i])
2847 fprintf (stderr, "Rate %.1f Hz not available for this size\n", rate);
2853 int major_version, minor_version;
2854 XRRQueryVersion (dpy, &major_version, &minor_version);
2855 printf("Server reports RandR version %d.%d\n",
2856 major_version, minor_version);
2859 if (query || query_1) {
2860 printf(" SZ: Pixels Physical Refresh\n");
2861 for (i = 0; i < nsize; i++) {
2862 printf ("%c%-2d %5d x %-5d (%4dmm x%4dmm )",
2863 i == current_size ? '*' : ' ',
2864 i, sizes[i].width, sizes[i].height,
2865 sizes[i].mwidth, sizes[i].mheight);
2866 rates = XRRConfigRates (sc, i, &nrate);
2867 if (nrate) printf (" ");
2868 for (j = 0; j < nrate; j++)
2870 i == current_size && rates[j] == current_rate ? '*' : ' ',
2876 rotations = XRRConfigRotations(sc, ¤t_rotation);
2878 rotation = 1 << rot ;
2880 printf("Current rotation - %s\n",
2881 rotation_name (current_rotation));
2883 printf("Current reflection - %s\n",
2884 reflection_name (current_rotation));
2886 printf ("Rotations possible - ");
2887 for (i = 0; i < 4; i ++) {
2888 if ((rotations >> i) & 1) printf("%s ", direction[i]);
2892 printf ("Reflections possible - ");
2893 if (rotations & (RR_Reflect_X|RR_Reflect_Y))
2895 if (rotations & RR_Reflect_X) printf ("X Axis ");
2896 if (rotations & RR_Reflect_Y) printf ("Y Axis");
2904 printf("Setting size to %d, rotation to %s\n", size, direction[rot]);
2906 printf ("Setting reflection on ");
2909 if (reflection & RR_Reflect_X) printf ("X Axis ");
2910 if (reflection & RR_Reflect_Y) printf ("Y Axis");
2913 printf ("neither axis");
2916 if (reflection & RR_Reflect_X) printf("Setting reflection on X axis\n");
2918 if (reflection & RR_Reflect_Y) printf("Setting reflection on Y axis\n");
2921 /* we should test configureNotify on the root window */
2922 XSelectInput (dpy, root, StructureNotifyMask);
2924 if (setit && !dryrun) XRRSelectInput (dpy, root,
2925 RRScreenChangeNotifyMask);
2926 if (setit && !dryrun) status = XRRSetScreenConfigAndRate (dpy, sc,
2927 DefaultRootWindow (dpy),
2928 (SizeID) size, (Rotation) (rotation | reflection), rate, CurrentTime);
2930 XRRQueryExtension(dpy, &event_base, &error_base);
2932 if (setit && !dryrun && status == RRSetConfigFailed) {
2933 printf ("Failed to change the screen configuration!\n");
2937 if (verbose && setit && !dryrun && size != current_size) {
2938 if (status == RRSetConfigSuccess)
2940 Bool seen_screen = False;
2941 while (!seen_screen) {
2943 XNextEvent(dpy, (XEvent *) &event);
2945 printf ("Event received, type = %d\n", event.type);
2946 /* update Xlib's knowledge of the event */
2947 XRRUpdateConfiguration (&event);
2948 if (event.type == ConfigureNotify)
2949 printf("Received ConfigureNotify Event!\n");
2951 switch (event.type - event_base) {
2952 case RRScreenChangeNotify:
2953 sce = (XRRScreenChangeNotifyEvent *) &event;
2955 printf("Got a screen change notify event!\n");
2956 printf(" window = %d\n root = %d\n size_index = %d\n rotation %d\n",
2957 (int) sce->window, (int) sce->root,
2958 sce->size_index, sce->rotation);
2959 printf(" timestamp = %ld, config_timestamp = %ld\n",
2960 sce->timestamp, sce->config_timestamp);
2961 printf(" Rotation = %x\n", sce->rotation);
2962 printf(" %d X %d pixels, %d X %d mm\n",
2963 sce->width, sce->height, sce->mwidth, sce->mheight);
2964 printf("Display width %d, height %d\n",
2965 DisplayWidth(dpy, screen), DisplayHeight(dpy, screen));
2966 printf("Display widthmm %d, heightmm %d\n",
2967 DisplayWidthMM(dpy, screen), DisplayHeightMM(dpy, screen));
2968 spo = sce->subpixel_order;
2969 if ((spo < 0) || (spo > 5))
2970 printf ("Unknown subpixel order, value = %d\n", spo);
2971 else printf ("new Subpixel rendering model is %s\n", order[spo]);
2975 if (event.type != ConfigureNotify)
2976 printf("unknown event received, type = %d!\n", event.type);
2981 XRRFreeScreenConfigInfo(sc);