2 * Copyright © 2001 Keith Packard, member of The XFree86 Project, Inc.
3 * Copyright © 2002 Hewlett Packard Company, Inc.
4 * Copyright © 2006 Intel Corporation
5 * Copyright © 2013 NVIDIA Corporation
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that copyright
10 * notice and this permission notice appear in supporting documentation, and
11 * that the name of the copyright holders not be used in advertising or
12 * publicity pertaining to distribution of the software without specific,
13 * written prior permission. The copyright holders make no representations
14 * about the suitability of this software for any purpose. It is provided "as
15 * is" without express or implied warranty.
17 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
25 * Thanks to Jim Gettys who wrote most of the client side code,
26 * and part of the server code for randr.
31 #include <X11/Xlibint.h>
32 #include <X11/Xproto.h>
33 #include <X11/Xatom.h>
34 #include <X11/extensions/Xrandr.h>
35 #include <X11/extensions/Xrender.h> /* we share subpixel information */
48 static char *program_name;
51 static int screen = -1;
52 static Bool verbose = False;
53 static Bool automatic = False;
54 static Bool properties = False;
55 static Bool grab_server = True;
56 static Bool no_primary = False;
57 static int filter_type = -1;
59 static const char *filter_names[2] = {
63 static const char *direction[5] = {
70 static const char *reflections[5] = {
78 static const char *order[6] = {
90 { "+HSync", RR_HSyncPositive },
91 { "-HSync", RR_HSyncNegative },
92 { "+VSync", RR_VSyncPositive },
93 { "-VSync", RR_VSyncNegative },
94 { "Interlace", RR_Interlace },
95 { "DoubleScan", RR_DoubleScan },
96 { "CSync", RR_CSync },
97 { "+CSync", RR_CSyncPositive },
98 { "-CSync", RR_CSyncNegative },
105 printf("usage: %s [options]\n%s", program_name,
106 " where options are:\n"
107 " --display <display> or -d <display>\n"
109 " -o <normal,inverted,left,right,0,1,2,3>\n"
110 " or --orientation <normal,inverted,left,right,0,1,2,3>\n"
112 " -s <size>/<width>x<height> or --size <size>/<width>x<height>\n"
113 " -r <rate> or --rate <rate> or --refresh <rate>\n"
115 " -x (reflect in x)\n"
116 " -y (reflect in y)\n"
117 " --screen <screen>\n"
122 " --prop or --properties\n"
123 " --fb <width>x<height>\n"
124 " --fbmm <width>x<height>\n"
125 " --dpi <dpi>/<output>\n"
126 " --output <output>\n"
131 " --rate <rate> or --refresh <rate>\n"
132 " --reflect normal,x,y,xy\n"
133 " --rotate normal,inverted,left,right\n"
134 " --left-of <output>\n"
135 " --right-of <output>\n"
136 " --above <output>\n"
137 " --below <output>\n"
138 " --same-as <output>\n"
139 " --set <property> <value>\n"
141 " --scale-from <w>x<h>\n"
142 " --transform <a>,<b>,<c>,<d>,<e>,<f>,<g>,<h>,<i>\n"
143 " --filter nearest,bilinear\n"
146 " --panning <w>x<h>[+<x>+<y>[/<track:w>x<h>+<x>+<y>[/<border:l>/<t>/<r>/<b>]]]\n"
147 " --gamma <r>:<g>:<b>\n"
148 " --brightness <value>\n"
151 " --newmode <name> <clock MHz>\n"
152 " <hdisp> <hsync-start> <hsync-end> <htotal>\n"
153 " <vdisp> <vsync-start> <vsync-end> <vtotal>\n"
155 " Valid flags: +HSync -HSync +VSync -VSync\n"
156 " +CSync -CSync CSync Interlace DoubleScan\n"
158 " --addmode <output> <name>\n"
159 " --delmode <output> <name>\n"
161 " --setprovideroutputsource <prov-xid> <source-xid>\n"
162 " --setprovideroffloadsink <prov-xid> <sink-xid>\n"
164 " --listactivemonitors\n"
165 " --setmonitor <name> {auto|<w>/<mmw>x<h>/<mmh>+<x>+<y>} {none|<output>,<output>,...}\n"
166 " --delmonitor <name>\n");
169 static void _X_NORETURN _X_ATTRIBUTE_PRINTF(1,2)
170 fatal (const char *format, ...)
174 va_start (ap, format);
175 fprintf (stderr, "%s: ", program_name);
176 vfprintf (stderr, format, ap);
182 static void _X_ATTRIBUTE_PRINTF(1,2)
183 warning (const char *format, ...)
187 va_start (ap, format);
188 fprintf (stderr, "%s: ", program_name);
189 vfprintf (stderr, format, ap);
193 static void _X_NORETURN _X_ATTRIBUTE_PRINTF(1,2)
194 argerr (const char *format, ...)
198 va_start (ap, format);
199 fprintf (stderr, "%s: ", program_name);
200 vfprintf (stderr, format, ap);
201 fprintf (stderr, "Try '%s --help' for more information.\n", program_name);
207 /* Because fmin requires C99 suppport */
208 static inline double dmin (double x, double y)
210 return x < y ? x : y;
214 rotation_name (Rotation rotation)
218 if ((rotation & 0xf) == 0)
220 for (i = 0; i < 4; i++)
221 if (rotation & (1 << i))
223 return "invalid rotation";
227 reflection_name (Rotation rotation)
229 rotation &= (RR_Reflect_X|RR_Reflect_Y);
237 case RR_Reflect_X|RR_Reflect_Y:
238 return "X and Y axis";
240 return "invalid reflection";
244 capability_name (int cap_bit)
247 case RR_Capability_SourceOutput:
248 return "Source Output";
249 case RR_Capability_SinkOutput:
250 return "Sink Output";
251 case RR_Capability_SourceOffload:
252 return "Source Offload";
253 case RR_Capability_SinkOffload:
254 return "Sink Offload";
256 return "invalid capability";
259 typedef enum _relation {
268 int x, y, width, height;
279 typedef enum _changes {
281 changes_crtc = (1 << 0),
282 changes_mode = (1 << 1),
283 changes_relation = (1 << 2),
284 changes_position = (1 << 3),
285 changes_rotation = (1 << 4),
286 changes_reflection = (1 << 5),
287 changes_automatic = (1 << 6),
288 changes_refresh = (1 << 7),
289 changes_property = (1 << 8),
290 changes_transform = (1 << 9),
291 changes_panning = (1 << 10),
292 changes_gamma = (1 << 11),
293 changes_primary = (1 << 12),
294 changes_filter = (1 << 13),
297 typedef enum _name_kind {
299 name_string = (1 << 0),
301 name_index = (1 << 2),
302 name_preferred = (1 << 3),
312 typedef struct _crtc crtc_t;
313 typedef struct _output output_t;
314 typedef struct _transform transform_t;
315 typedef struct _umode umode_t;
316 typedef struct _output_prop output_prop_t;
317 typedef struct _provider provider_t;
318 typedef struct _monitors monitors_t;
319 typedef struct _umonitor umonitor_t;
322 XTransform transform;
331 XRRCrtcInfo *crtc_info;
333 XRRModeInfo *mode_info;
334 XRRPanning *panning_info;
340 transform_t current_transform, pending_transform;
343 struct _output_prop {
344 struct _output_prop *next;
350 struct _output *next;
354 output_prop_t *props;
357 XRROutputInfo *output_info;
361 crtc_t *current_crtc_info;
365 XRRModeInfo *mode_info;
378 int scale_from_w, scale_from_h;
379 transform_t transform;
394 typedef enum _umode_action {
395 umode_create, umode_destroy, umode_add, umode_delete
402 umode_action_t action;
410 XRRProviderInfo *info;
415 XRRMonitorInfo *monitors;
419 struct _umonitor *next;
423 int x, y, width, height;
424 int mmwidth, mmheight;
429 static const char *connection[3] = {
432 "unknown connection"};
434 #define OUTPUT_NAME 1
438 #define CRTC_INDEX 0x40000000
447 static output_t *all_outputs = NULL;
448 static output_t **all_outputs_tail = &all_outputs;
449 static crtc_t *crtcs;
450 static provider_t *providers;
451 static umode_t *umodes;
452 static int num_crtcs, num_providers;
453 static XRRScreenResources *res;
454 static int fb_width = 0, fb_height = 0;
455 static int fb_width_mm = 0, fb_height_mm = 0;
456 static double dpi = 0;
457 static char *dpi_output_name = NULL;
458 static Bool dryrun = False;
459 static int minWidth, maxWidth, minHeight, maxHeight;
460 static Bool has_1_2 = False;
461 static Bool has_1_3 = False;
462 static Bool has_1_4 = False;
463 static Bool has_1_5 = False;
464 static name_t provider_name, output_source_provider_name, offload_sink_provider_name;
465 static monitors_t *monitors;
466 static umonitor_t *umonitors;
469 mode_height (XRRModeInfo *mode_info, Rotation rotation)
471 switch (rotation & 0xf) {
474 return mode_info->height;
477 return mode_info->width;
484 mode_width (XRRModeInfo *mode_info, Rotation rotation)
486 switch (rotation & 0xf) {
489 return mode_info->width;
492 return mode_info->height;
499 transform_point (XTransform *transform, double *xp, double *yp)
509 for (j = 0; j < 3; j++)
512 for (i = 0; i < 3; i++)
513 v += (XFixedToDouble (transform->matrix[j][i]) * vector[i]);
518 for (j = 0; j < 2; j++) {
519 vector[j] = result[j] / result[2];
520 if (vector[j] > 32767 || vector[j] < -32767)
529 path_bounds (XTransform *transform, point_t *points, int npoints, box_t *box)
534 for (i = 0; i < npoints; i++) {
538 transform_point (transform, &x, &y);
539 point.x1 = floor (x);
540 point.y1 = floor (y);
546 if (point.x1 < box->x1) box->x1 = point.x1;
547 if (point.y1 < box->y1) box->y1 = point.y1;
548 if (point.x2 > box->x2) box->x2 = point.x2;
549 if (point.y2 > box->y2) box->y2 = point.y2;
555 mode_geometry (XRRModeInfo *mode_info, Rotation rotation,
556 XTransform *transform,
560 int width = mode_width (mode_info, rotation);
561 int height = mode_height (mode_info, rotation);
571 path_bounds (transform, rect, 4, bounds);
574 /* v refresh frequency in Hz */
576 mode_refresh (const XRRModeInfo *mode_info)
579 double vTotal = mode_info->vTotal;
581 if (mode_info->modeFlags & RR_DoubleScan) {
582 /* doublescan doubles the number of lines */
586 if (mode_info->modeFlags & RR_Interlace) {
587 /* interlace splits the frame into two fields */
588 /* the field rate is what is typically reported by monitors */
592 if (mode_info->hTotal && vTotal)
593 rate = ((double) mode_info->dotClock /
594 ((double) mode_info->hTotal * (double) vTotal));
600 /* h sync frequency in Hz */
602 mode_hsync (const XRRModeInfo *mode_info)
606 if (mode_info->hTotal)
607 rate = (double) mode_info->dotClock / (double) mode_info->hTotal;
614 print_verbose_mode (const XRRModeInfo *mode, Bool current, Bool preferred)
618 printf (" %s (0x%x) %6.3fMHz",
619 mode->name, (int)mode->id,
620 (double)mode->dotClock / 1000000.0);
621 for (f = 0; mode_flags[f].flag; f++)
622 if (mode->modeFlags & mode_flags[f].flag)
623 printf (" %s", mode_flags[f].string);
625 printf (" *current");
627 printf (" +preferred");
629 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.2fKHz\n",
630 mode->width, mode->hSyncStart, mode->hSyncEnd,
631 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
632 printf (" v: height %4d start %4d end %4d total %4d clock %6.2fHz\n",
633 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
634 mode_refresh (mode));
638 init_name (name_t *name)
640 name->kind = name_none;
644 set_name_string (name_t *name, char *string)
646 name->kind |= name_string;
647 name->string = string;
651 set_name_xid (name_t *name, XID xid)
653 name->kind |= name_xid;
658 set_name_index (name_t *name, int idx)
660 name->kind |= name_index;
665 set_name_preferred (name_t *name)
667 name->kind |= name_preferred;
671 set_name_all (name_t *name, name_t *old)
673 if (old->kind & name_xid)
674 name->xid = old->xid;
675 if (old->kind & name_string)
676 name->string = old->string;
677 if (old->kind & name_index)
678 name->index = old->index;
679 name->kind |= old->kind;
683 set_name (name_t *name, char *string, name_kind_t valid)
685 unsigned int xid; /* don't make it XID (which is unsigned long):
686 scanf() takes unsigned int */
689 if ((valid & name_xid) && sscanf (string, "0x%x", &xid) == 1)
690 set_name_xid (name, xid);
691 else if ((valid & name_index) && sscanf (string, "%d", &idx) == 1)
692 set_name_index (name, idx);
693 else if (valid & name_string)
694 set_name_string (name, string);
696 argerr ("invalid name '%s'\n", string);
700 print_name (const name_t *name)
702 name_kind_t kind = name->kind;
704 if ((kind & name_xid)) return printf("XID 0x%x", (unsigned int)name->xid);
705 else if ((kind & name_string)) return printf("name %s", name->string);
706 else if ((kind & name_index)) return printf("index %d", name->index);
707 else return printf("unknown name");
711 init_transform (transform_t *transform)
714 memset (&transform->transform, '\0', sizeof (transform->transform));
715 for (x = 0; x < 3; x++)
716 transform->transform.matrix[x][x] = XDoubleToFixed (1.0);
717 transform->filter = "";
718 transform->nparams = 0;
719 transform->params = NULL;
723 set_transform (transform_t *dest,
724 XTransform *transform,
729 dest->transform = *transform;
730 /* note: this string is leaked */
731 dest->filter = strdup (filter);
732 dest->nparams = nparams;
733 dest->params = malloc (nparams * sizeof (XFixed));
734 memcpy (dest->params, params, nparams * sizeof (XFixed));
738 copy_transform (transform_t *dest, transform_t *src)
740 set_transform (dest, &src->transform,
741 src->filter, src->params, src->nparams);
745 equal_transform (transform_t *a, transform_t *b)
747 if (memcmp (&a->transform, &b->transform, sizeof (XTransform)) != 0)
749 if (strcmp (a->filter, b->filter) != 0)
751 if (a->nparams != b->nparams)
753 if (memcmp (a->params, b->params, a->nparams * sizeof (XFixed)) != 0)
761 output_t *output = calloc (1, sizeof (output_t));
764 fatal ("out of memory\n");
766 output->found = False;
767 output->brightness = 1.0;
768 *all_outputs_tail = output;
769 all_outputs_tail = &output->next;
774 find_output (name_t *name)
778 for (output = all_outputs; output; output = output->next)
780 name_kind_t common = name->kind & output->output.kind;
782 if ((common & name_xid) && name->xid == output->output.xid)
784 if ((common & name_string) && !strcmp (name->string, output->output.string))
786 if ((common & name_index) && name->index == output->output.index)
793 find_output_by_xid (RROutput output)
797 init_name (&output_name);
798 set_name_xid (&output_name, output);
799 return find_output (&output_name);
803 find_output_by_name (char *name)
807 init_name (&output_name);
808 set_name_string (&output_name, name);
809 return find_output (&output_name);
813 find_crtc (name_t *name)
818 for (c = 0; c < num_crtcs; c++)
823 common = name->kind & crtc->crtc.kind;
825 if ((common & name_xid) && name->xid == crtc->crtc.xid)
827 if ((common & name_string) && !strcmp (name->string, crtc->crtc.string))
829 if ((common & name_index) && name->index == crtc->crtc.index)
837 find_crtc_by_xid (RRCrtc crtc)
841 init_name (&crtc_name);
842 set_name_xid (&crtc_name, crtc);
843 return find_crtc (&crtc_name);
847 find_mode (name_t *name, double refresh)
850 XRRModeInfo *best = NULL;
853 for (m = 0; m < res->nmode; m++)
855 XRRModeInfo *mode = &res->modes[m];
856 if ((name->kind & name_xid) && name->xid == mode->id)
861 if ((name->kind & name_string) && !strcmp (name->string, mode->name))
866 dist = fabs (mode_refresh (mode) - refresh);
869 if (!best || dist < bestDist)
880 find_mode_by_xid (RRMode mode)
884 init_name (&mode_name);
885 set_name_xid (&mode_name, mode);
886 return find_mode (&mode_name, 0);
891 find_mode_by_name (char *name)
894 init_name (&mode_name);
895 set_name_string (&mode_name, name);
896 return find_mode (&mode_name, 0);
902 find_mode_for_output (output_t *output, name_t *name)
904 XRROutputInfo *output_info = output->output_info;
906 XRRModeInfo *best = NULL;
909 for (m = 0; m < output_info->nmode; m++)
913 mode = find_mode_by_xid (output_info->modes[m]);
915 if ((name->kind & name_xid) && name->xid == mode->id)
920 if ((name->kind & name_string) && !strcmp (name->string, mode->name))
924 /* Stay away from doublescan modes unless refresh rate is specified. */
925 if (!output->refresh && (mode->modeFlags & RR_DoubleScan))
929 dist = fabs (mode_refresh (mode) - output->refresh);
932 if (!best || dist < bestDist)
943 preferred_mode (output_t *output)
945 XRROutputInfo *output_info = output->output_info;
952 for (m = 0; m < output_info->nmode; m++)
954 XRRModeInfo *mode_info = find_mode_by_xid (output_info->modes[m]);
957 if (m < output_info->npreferred)
959 else if (output_info->mm_height)
960 dist = (1000 * DisplayHeight(dpy, screen) / DisplayHeightMM(dpy, screen) -
961 1000 * mode_info->height / output_info->mm_height);
963 dist = DisplayHeight(dpy, screen) - mode_info->height;
965 if (dist < 0) dist = -dist;
966 if (!best || dist < bestDist)
976 output_can_use_crtc (output_t *output, crtc_t *crtc)
978 XRROutputInfo *output_info = output->output_info;
981 for (c = 0; c < output_info->ncrtc; c++)
982 if (output_info->crtcs[c] == crtc->crtc.xid)
988 output_can_use_mode (output_t *output, XRRModeInfo *mode)
990 XRROutputInfo *output_info = output->output_info;
993 for (m = 0; m < output_info->nmode; m++)
994 if (output_info->modes[m] == mode->id)
1000 crtc_can_use_rotation (crtc_t *crtc, Rotation rotation)
1002 Rotation rotations = crtc->crtc_info->rotations;
1003 Rotation dir = rotation & (RR_Rotate_0|RR_Rotate_90|RR_Rotate_180|RR_Rotate_270);
1004 Rotation reflect = rotation & (RR_Reflect_X|RR_Reflect_Y);
1005 if (((rotations & dir) != 0) && ((rotations & reflect) == reflect))
1012 crtc_can_use_transform (crtc_t *crtc, XTransform *transform)
1016 XRRQueryVersion (dpy, &major, &minor);
1017 if (major > 1 || (major == 1 && minor >= 3))
1024 * Report only rotations that are supported by all crtcs
1027 output_rotations (output_t *output)
1030 Rotation rotation = RR_Rotate_0;
1031 XRROutputInfo *output_info = output->output_info;
1034 for (c = 0; c < output_info->ncrtc; c++)
1036 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
1040 rotation = crtc->crtc_info->rotations;
1043 rotation &= crtc->crtc_info->rotations;
1050 output_can_use_rotation (output_t *output, Rotation rotation)
1052 XRROutputInfo *output_info = output->output_info;
1055 /* make sure all of the crtcs can use this rotation.
1056 * yes, this is not strictly necessary, but it is
1057 * simpler,and we expect most drivers to either
1058 * support rotation everywhere or nowhere
1060 for (c = 0; c < output_info->ncrtc; c++)
1062 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
1063 if (crtc && !crtc_can_use_rotation (crtc, rotation))
1070 output_is_primary(output_t *output)
1073 return XRRGetOutputPrimary(dpy, root) == output->output.xid;
1077 /* Returns the index of the last value in an array < 0xffff */
1079 find_last_non_clamped(CARD16 array[], int size) {
1081 for (i = size - 1; i > 0; i--) {
1082 if (array[i] < 0xffff)
1089 set_gamma_info(output_t *output)
1091 XRRCrtcGamma *crtc_gamma;
1092 double i1, v1, i2, v2;
1093 int size, middle, last_best, last_red, last_green, last_blue;
1096 if (!output->crtc_info)
1099 size = XRRGetCrtcGammaSize(dpy, output->crtc_info->crtc.xid);
1101 warning("Failed to get size of gamma for output %s\n", output->output.string);
1105 crtc_gamma = XRRGetCrtcGamma(dpy, output->crtc_info->crtc.xid);
1107 warning("Failed to get gamma for output %s\n", output->output.string);
1112 * Here is a bit tricky because gamma is a whole curve for each
1113 * color. So, typically, we need to represent 3 * 256 values as 3 + 1
1114 * values. Therefore, we approximate the gamma curve (v) by supposing
1115 * it always follows the way we set it: a power function (i^g)
1116 * multiplied by a brightness (b).
1118 * so g = (ln(v) - ln(b))/ln(i)
1119 * and b can be found using two points (v1,i1) and (v2, i2):
1120 * b = e^((ln(v2)*ln(i1) - ln(v1)*ln(i2))/ln(i1/i2))
1121 * For the best resolution, we select i2 at the highest place not
1122 * clamped and i1 at i2/2. Note that if i2 = 1 (as in most normal
1123 * cases), then b = v2.
1125 last_red = find_last_non_clamped(crtc_gamma->red, size);
1126 last_green = find_last_non_clamped(crtc_gamma->green, size);
1127 last_blue = find_last_non_clamped(crtc_gamma->blue, size);
1128 best_array = crtc_gamma->red;
1129 last_best = last_red;
1130 if (last_green > last_best) {
1131 last_best = last_green;
1132 best_array = crtc_gamma->green;
1134 if (last_blue > last_best) {
1135 last_best = last_blue;
1136 best_array = crtc_gamma->blue;
1141 middle = last_best / 2;
1142 i1 = (double)(middle + 1) / size;
1143 v1 = (double)(best_array[middle]) / 65535;
1144 i2 = (double)(last_best + 1) / size;
1145 v2 = (double)(best_array[last_best]) / 65535;
1146 if (v2 < 0.0001) { /* The screen is black */
1147 output->brightness = 0;
1148 output->gamma.red = 1;
1149 output->gamma.green = 1;
1150 output->gamma.blue = 1;
1152 if ((last_best + 1) == size)
1153 output->brightness = v2;
1155 output->brightness = exp((log(v2)*log(i1) - log(v1)*log(i2))/log(i1/i2));
1156 output->gamma.red = log((double)(crtc_gamma->red[last_red / 2]) / output->brightness
1157 / 65535) / log((double)((last_red / 2) + 1) / size);
1158 output->gamma.green = log((double)(crtc_gamma->green[last_green / 2]) / output->brightness
1159 / 65535) / log((double)((last_green / 2) + 1) / size);
1160 output->gamma.blue = log((double)(crtc_gamma->blue[last_blue / 2]) / output->brightness
1161 / 65535) / log((double)((last_blue / 2) + 1) / size);
1164 XRRFreeGamma(crtc_gamma);
1168 set_output_info (output_t *output, RROutput xid, XRROutputInfo *output_info)
1170 /* sanity check output info */
1171 if (output_info->connection != RR_Disconnected && !output_info->nmode)
1172 warning ("Output %s is not disconnected but has no modes\n",
1175 /* set output name and info */
1176 if (!(output->output.kind & name_xid))
1177 set_name_xid (&output->output, xid);
1178 if (!(output->output.kind & name_string))
1179 set_name_string (&output->output, output_info->name);
1180 output->output_info = output_info;
1182 /* set crtc name and info */
1183 if (!(output->changes & changes_crtc))
1184 set_name_xid (&output->crtc, output_info->crtc);
1186 if (output->crtc.kind == name_xid && output->crtc.xid == None)
1187 output->crtc_info = NULL;
1190 output->crtc_info = find_crtc (&output->crtc);
1191 if (!output->crtc_info)
1193 if (output->crtc.kind & name_xid)
1194 fatal ("cannot find crtc 0x%lx\n", output->crtc.xid);
1195 if (output->crtc.kind & name_index)
1196 fatal ("cannot find crtc %d\n", output->crtc.index);
1198 if (!output_can_use_crtc (output, output->crtc_info))
1199 fatal ("output %s cannot use crtc 0x%lx\n", output->output.string,
1200 output->crtc_info->crtc.xid);
1203 /* set mode name and info */
1204 if (!(output->changes & changes_mode))
1206 crtc_t *crtc = NULL;
1208 if (output_info->crtc)
1209 crtc = find_crtc_by_xid(output_info->crtc);
1210 if (crtc && crtc->crtc_info)
1211 set_name_xid (&output->mode, crtc->crtc_info->mode);
1212 else if (output->crtc_info)
1213 set_name_xid (&output->mode, output->crtc_info->crtc_info->mode);
1215 set_name_xid (&output->mode, None);
1216 if (output->mode.xid)
1218 output->mode_info = find_mode_by_xid (output->mode.xid);
1219 if (!output->mode_info)
1220 fatal ("server did not report mode 0x%lx for output %s\n",
1221 output->mode.xid, output->output.string);
1224 output->mode_info = NULL;
1226 else if (output->mode.kind == name_xid && output->mode.xid == None)
1227 output->mode_info = NULL;
1230 if (output->mode.kind == name_preferred)
1231 output->mode_info = preferred_mode (output);
1233 output->mode_info = find_mode_for_output (output, &output->mode);
1234 if (!output->mode_info)
1236 if (output->mode.kind & name_preferred)
1237 fatal ("cannot find preferred mode\n");
1238 if (output->mode.kind & name_string)
1239 fatal ("cannot find mode %s\n", output->mode.string);
1240 if (output->mode.kind & name_xid)
1241 fatal ("cannot find mode 0x%lx\n", output->mode.xid);
1243 if (!output_can_use_mode (output, output->mode_info))
1244 fatal ("output %s cannot use mode %s\n", output->output.string,
1245 output->mode_info->name);
1249 if (!(output->changes & changes_position))
1251 if (output->crtc_info)
1253 output->x = output->crtc_info->crtc_info->x;
1254 output->y = output->crtc_info->crtc_info->y;
1264 if (!(output->changes & changes_rotation))
1266 output->rotation &= ~0xf;
1267 if (output->crtc_info)
1268 output->rotation |= (output->crtc_info->crtc_info->rotation & 0xf);
1270 output->rotation = RR_Rotate_0;
1272 if (!(output->changes & changes_reflection))
1274 output->rotation &= ~(RR_Reflect_X|RR_Reflect_Y);
1275 if (output->crtc_info)
1276 output->rotation |= (output->crtc_info->crtc_info->rotation &
1277 (RR_Reflect_X|RR_Reflect_Y));
1279 if (!output_can_use_rotation (output, output->rotation))
1280 fatal ("output %s cannot use rotation \"%s\" reflection \"%s\"\n",
1281 output->output.string,
1282 rotation_name (output->rotation),
1283 reflection_name (output->rotation));
1286 if (!(output->changes & changes_gamma))
1287 set_gamma_info(output);
1289 /* set transformation */
1290 if (!(output->changes & changes_transform))
1292 if (output->crtc_info)
1293 copy_transform (&output->transform, &output->crtc_info->current_transform);
1295 init_transform (&output->transform);
1297 /* transform was already set for --scale or --transform */
1299 /* for --scale-from, figure out the mode size and compute the transform
1300 * for the target framebuffer area */
1301 if (output->scale_from_w > 0 && output->mode_info) {
1302 double sx = (double)output->scale_from_w /
1303 output->mode_info->width;
1304 double sy = (double)output->scale_from_h /
1305 output->mode_info->height;
1307 printf("scaling %s by %lfx%lf\n", output->output.string, sx,
1309 init_transform (&output->transform);
1310 output->transform.transform.matrix[0][0] = XDoubleToFixed (sx);
1311 output->transform.transform.matrix[1][1] = XDoubleToFixed (sy);
1312 output->transform.transform.matrix[2][2] = XDoubleToFixed (1.0);
1313 if (sx != 1 || sy != 1)
1314 output->transform.filter = "bilinear";
1316 output->transform.filter = "nearest";
1317 output->transform.nparams = 0;
1318 output->transform.params = NULL;
1321 if (output->changes & changes_filter)
1323 output->transform.filter = filter_names[filter_type];
1327 if (!(output->changes & changes_primary))
1328 output->primary = output_is_primary(output);
1332 get_screen (Bool current)
1335 fatal ("Server RandR version before 1.2\n");
1340 XRRGetScreenSizeRange (dpy, root, &minWidth, &minHeight,
1341 &maxWidth, &maxHeight);
1344 res = XRRGetScreenResourcesCurrent (dpy, root);
1346 res = XRRGetScreenResources (dpy, root);
1347 if (!res) fatal ("could not get screen resources");
1355 num_crtcs = res->ncrtc;
1356 crtcs = calloc (num_crtcs, sizeof (crtc_t));
1357 if (!crtcs) fatal ("out of memory\n");
1359 for (c = 0; c < res->ncrtc; c++)
1361 XRRCrtcInfo *crtc_info = XRRGetCrtcInfo (dpy, res, res->crtcs[c]);
1362 XRRCrtcTransformAttributes *attr;
1363 XRRPanning *panning_info = NULL;
1367 memset(&zero, 0, sizeof(zero));
1368 panning_info = XRRGetPanning (dpy, res, res->crtcs[c]);
1369 zero.timestamp = panning_info->timestamp;
1370 if (!memcmp(panning_info, &zero, sizeof(zero))) {
1371 Xfree(panning_info);
1372 panning_info = NULL;
1376 set_name_xid (&crtcs[c].crtc, res->crtcs[c]);
1377 set_name_index (&crtcs[c].crtc, c);
1378 if (!crtc_info) fatal ("could not get crtc 0x%lx information\n", res->crtcs[c]);
1379 crtcs[c].crtc_info = crtc_info;
1380 crtcs[c].panning_info = panning_info;
1381 if (crtc_info->mode == None)
1383 crtcs[c].mode_info = NULL;
1386 crtcs[c].rotation = RR_Rotate_0;
1388 if (XRRGetCrtcTransform (dpy, res->crtcs[c], &attr) && attr) {
1389 set_transform (&crtcs[c].current_transform,
1390 &attr->currentTransform,
1391 attr->currentFilter,
1392 attr->currentParams,
1393 attr->currentNparams);
1398 init_transform (&crtcs[c].current_transform);
1400 copy_transform (&crtcs[c].pending_transform, &crtcs[c].current_transform);
1405 crtc_add_output (crtc_t *crtc, output_t *output)
1408 crtc->outputs = realloc (crtc->outputs, (crtc->noutput + 1) * sizeof (output_t *));
1411 crtc->outputs = malloc (sizeof (output_t *));
1412 crtc->x = output->x;
1413 crtc->y = output->y;
1414 crtc->rotation = output->rotation;
1415 crtc->mode_info = output->mode_info;
1416 copy_transform (&crtc->pending_transform, &output->transform);
1418 if (!crtc->outputs) fatal ("out of memory\n");
1419 crtc->outputs[crtc->noutput++] = output;
1427 for (output = all_outputs; output; output = output->next)
1429 if (!output->mode_info) continue;
1430 crtc_add_output (output->crtc_info, output);
1439 for (output = all_outputs; output; output = output->next)
1441 if (! output->crtc_info)
1443 if (! (output->changes & changes_panning))
1445 if (! output->crtc_info->panning_info)
1446 output->crtc_info->panning_info = malloc (sizeof(XRRPanning));
1447 memcpy (output->crtc_info->panning_info, &output->panning, sizeof(XRRPanning));
1448 output->crtc_info->changing = 1;
1457 for (output = all_outputs; output; output = output->next) {
1460 XRRCrtcGamma *crtc_gamma;
1465 if (!(output->changes & changes_gamma))
1468 if (!output->crtc_info) {
1469 fatal("Need crtc to set gamma on.\n");
1473 crtc = output->crtc_info;
1475 size = XRRGetCrtcGammaSize(dpy, crtc->crtc.xid);
1478 fatal("Gamma size is 0.\n");
1483 * The gamma-correction lookup table managed through XRR[GS]etCrtcGamma
1484 * is 2^n in size, where 'n' is the number of significant bits in
1485 * the X Color. Because an X Color is 16 bits, size cannot be larger
1489 fatal("Gamma correction table is impossibly large.\n");
1493 crtc_gamma = XRRAllocGamma(size);
1495 fatal("Gamma allocation failed.\n");
1499 if (output->gamma.red == 0.0)
1500 output->gamma.red = 1.0;
1501 if (output->gamma.green == 0.0)
1502 output->gamma.green = 1.0;
1503 if (output->gamma.blue == 0.0)
1504 output->gamma.blue = 1.0;
1506 gammaRed = 1.0 / output->gamma.red;
1507 gammaGreen = 1.0 / output->gamma.green;
1508 gammaBlue = 1.0 / output->gamma.blue;
1510 for (i = 0; i < size; i++) {
1511 if (gammaRed == 1.0 && output->brightness == 1.0)
1512 crtc_gamma->red[i] = (double)i / (double)(size - 1) * 65535.0;
1514 crtc_gamma->red[i] = dmin(pow((double)i/(double)(size - 1),
1515 gammaRed) * output->brightness,
1518 if (gammaGreen == 1.0 && output->brightness == 1.0)
1519 crtc_gamma->green[i] = (double)i / (double)(size - 1) * 65535.0;
1521 crtc_gamma->green[i] = dmin(pow((double)i/(double)(size - 1),
1522 gammaGreen) * output->brightness,
1525 if (gammaBlue == 1.0 && output->brightness == 1.0)
1526 crtc_gamma->blue[i] = (double)i / (double)(size - 1) * 65535.0;
1528 crtc_gamma->blue[i] = dmin(pow((double)i/(double)(size - 1),
1529 gammaBlue) * output->brightness,
1533 XRRSetCrtcGamma(dpy, crtc->crtc.xid, crtc_gamma);
1545 XRRSetOutputPrimary(dpy, root, None);
1547 for (output = all_outputs; output; output = output->next) {
1548 if (!(output->changes & changes_primary))
1550 if (output->primary)
1551 XRRSetOutputPrimary(dpy, root, output->output.xid);
1557 crtc_disable (crtc_t *crtc)
1560 printf ("crtc %d: disable\n", crtc->crtc.index);
1563 return RRSetConfigSuccess;
1564 return XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1565 0, 0, None, RR_Rotate_0, NULL, 0);
1569 crtc_set_transform (crtc_t *crtc, transform_t *transform)
1573 XRRQueryVersion (dpy, &major, &minor);
1574 if (major > 1 || (major == 1 && minor >= 3))
1575 XRRSetCrtcTransform (dpy, crtc->crtc.xid,
1576 &transform->transform,
1579 transform->nparams);
1583 crtc_revert (crtc_t *crtc)
1585 XRRCrtcInfo *crtc_info = crtc->crtc_info;
1588 printf ("crtc %d: revert\n", crtc->crtc.index);
1591 return RRSetConfigSuccess;
1593 if (!equal_transform (&crtc->current_transform, &crtc->pending_transform))
1594 crtc_set_transform (crtc, &crtc->current_transform);
1595 return XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1596 crtc_info->x, crtc_info->y,
1597 crtc_info->mode, crtc_info->rotation,
1598 crtc_info->outputs, crtc_info->noutput);
1602 crtc_apply (crtc_t *crtc)
1604 RROutput *rr_outputs;
1609 if (!crtc->changing || !crtc->mode_info)
1610 return RRSetConfigSuccess;
1612 rr_outputs = calloc (crtc->noutput, sizeof (RROutput));
1615 for (o = 0; o < crtc->noutput; o++)
1616 rr_outputs[o] = crtc->outputs[o]->output.xid;
1617 mode = crtc->mode_info->id;
1619 printf ("crtc %d: %12s %6.2f +%d+%d", crtc->crtc.index,
1620 crtc->mode_info->name, mode_refresh (crtc->mode_info),
1622 for (o = 0; o < crtc->noutput; o++)
1623 printf (" \"%s\"", crtc->outputs[o]->output.string);
1628 s = RRSetConfigSuccess;
1631 if (!equal_transform (&crtc->current_transform, &crtc->pending_transform))
1632 crtc_set_transform (crtc, &crtc->pending_transform);
1633 s = XRRSetCrtcConfig (dpy, res, crtc->crtc.xid, CurrentTime,
1634 crtc->x, crtc->y, mode, crtc->rotation,
1635 rr_outputs, crtc->noutput);
1636 if (s == RRSetConfigSuccess && crtc->panning_info) {
1638 s = XRRSetPanning (dpy, res, crtc->crtc.xid, crtc->panning_info);
1640 fatal ("panning needs RandR 1.3\n");
1648 screen_revert (void)
1651 printf ("screen %d: revert\n", screen);
1655 XRRSetScreenSize (dpy, root,
1656 DisplayWidth (dpy, screen),
1657 DisplayHeight (dpy, screen),
1658 DisplayWidthMM (dpy, screen),
1659 DisplayHeightMM (dpy, screen));
1665 if (fb_width == DisplayWidth (dpy, screen) &&
1666 fb_height == DisplayHeight (dpy, screen) &&
1667 fb_width_mm == DisplayWidthMM (dpy, screen) &&
1668 fb_height_mm == DisplayHeightMM (dpy, screen))
1673 printf ("screen %d: %dx%d %dx%d mm %6.2fdpi\n", screen,
1674 fb_width, fb_height, fb_width_mm, fb_height_mm, dpi);
1677 XRRSetScreenSize (dpy, root, fb_width, fb_height,
1678 fb_width_mm, fb_height_mm);
1686 /* first disable all crtcs */
1687 for (c = 0; c < res->ncrtc; c++)
1688 crtc_disable (&crtcs[c]);
1689 /* next reset screen size */
1691 /* now restore all crtcs */
1692 for (c = 0; c < res->ncrtc; c++)
1693 crtc_revert (&crtcs[c]);
1697 * uh-oh, something bad happened in the middle of changing
1698 * the configuration. Revert to the previous configuration
1701 static void _X_NORETURN
1702 panic (Status s, crtc_t *crtc)
1704 int c = crtc->crtc.index;
1705 const char *message;
1708 case RRSetConfigSuccess: message = "succeeded"; break;
1709 case BadAlloc: message = "out of memory"; break;
1710 case RRSetConfigFailed: message = "failed"; break;
1711 case RRSetConfigInvalidConfigTime: message = "invalid config time"; break;
1712 case RRSetConfigInvalidTime: message = "invalid time"; break;
1713 default: message = "unknown failure"; break;
1716 fprintf (stderr, "%s: Configure crtc %d %s\n", program_name, c, message);
1728 * Hold the server grabbed while messing with
1729 * the screen so that apps which notice the resize
1730 * event and ask for xinerama information from the server
1731 * receive up-to-date information
1737 * Turn off any crtcs which are to be disabled or which are
1738 * larger than the target size
1740 for (c = 0; c < res->ncrtc; c++)
1742 crtc_t *crtc = &crtcs[c];
1743 XRRCrtcInfo *crtc_info = crtc->crtc_info;
1745 /* if this crtc is already disabled, skip it */
1746 if (crtc_info->mode == None)
1750 * If this crtc is to be left enabled, make
1751 * sure the old size fits then new screen
1753 if (crtc->mode_info)
1755 XRRModeInfo *old_mode = find_mode_by_xid (crtc_info->mode);
1760 panic (RRSetConfigFailed, crtc);
1762 /* old position and size information */
1763 mode_geometry (old_mode, crtc_info->rotation,
1764 &crtc->current_transform.transform,
1767 x = crtc_info->x + bounds.x1;
1768 y = crtc_info->y + bounds.y1;
1769 w = bounds.x2 - bounds.x1;
1770 h = bounds.y2 - bounds.y1;
1772 /* if it fits, skip it */
1773 if (x + w <= fb_width && y + h <= fb_height)
1775 crtc->changing = True;
1777 s = crtc_disable (crtc);
1778 if (s != RRSetConfigSuccess)
1783 * Set the screen size
1791 for (c = 0; c < res->ncrtc; c++)
1793 crtc_t *crtc = &crtcs[c];
1795 s = crtc_apply (crtc);
1796 if (s != RRSetConfigSuccess)
1803 * Release the server grab and let all clients
1804 * respond to the updated state
1807 XUngrabServer (dpy);
1811 * Use current output state to complete the output list
1819 for (o = 0; o < res->noutput; o++)
1821 XRROutputInfo *output_info = XRRGetOutputInfo (dpy, res, res->outputs[o]);
1824 if (!output_info) fatal ("could not get output 0x%lx information\n", res->outputs[o]);
1825 set_name_xid (&output_name, res->outputs[o]);
1826 set_name_index (&output_name, o);
1827 set_name_string (&output_name, output_info->name);
1828 output = find_output (&output_name);
1831 output = add_output ();
1832 set_name_all (&output->output, &output_name);
1834 * When global --automatic mode is set, turn on connected but off
1835 * outputs, turn off disconnected but on outputs
1839 switch (output_info->connection) {
1841 if (!output_info->crtc) {
1842 output->changes |= changes_automatic;
1843 output->automatic = True;
1846 case RR_Disconnected:
1847 if (output_info->crtc)
1849 output->changes |= changes_automatic;
1850 output->automatic = True;
1856 output->found = True;
1859 * Automatic mode -- track connection state and enable/disable outputs
1862 if (output->automatic)
1864 switch (output_info->connection) {
1866 case RR_UnknownConnection:
1867 if ((!(output->changes & changes_mode)))
1869 set_name_preferred (&output->mode);
1870 output->changes |= changes_mode;
1873 case RR_Disconnected:
1874 if ((!(output->changes & changes_mode)))
1876 set_name_xid (&output->mode, None);
1877 set_name_xid (&output->crtc, None);
1878 output->changes |= changes_mode;
1879 output->changes |= changes_crtc;
1885 set_output_info (output, res->outputs[o], output_info);
1887 for (q = all_outputs; q; q = q->next)
1891 fprintf(stderr, "warning: output %s not found; ignoring\n",
1898 mark_changing_crtcs (void)
1902 for (c = 0; c < num_crtcs; c++)
1904 crtc_t *crtc = &crtcs[c];
1908 /* walk old output list (to catch disables) */
1909 for (o = 0; o < crtc->crtc_info->noutput; o++)
1911 output = find_output_by_xid (crtc->crtc_info->outputs[o]);
1912 if (!output) fatal ("cannot find output 0x%lx\n",
1913 crtc->crtc_info->outputs[o]);
1914 if (output->changes)
1915 crtc->changing = True;
1917 /* walk new output list */
1918 for (o = 0; o < crtc->noutput; o++)
1920 output = crtc->outputs[o];
1921 if (output->changes)
1922 crtc->changing = True;
1928 * Test whether 'crtc' can be used for 'output'
1931 check_crtc_for_output (crtc_t *crtc, output_t *output)
1937 for (c = 0; c < output->output_info->ncrtc; c++)
1938 if (output->output_info->crtcs[c] == crtc->crtc.xid)
1940 if (c == output->output_info->ncrtc)
1942 for (other = all_outputs; other; other = other->next)
1944 if (other == output)
1947 if (other->mode_info == NULL)
1950 if (other->crtc_info != crtc)
1953 /* see if the output connected to the crtc can clone to this output */
1954 for (l = 0; l < output->output_info->nclone; l++)
1955 if (output->output_info->clones[l] == other->output.xid)
1957 /* not on the list, can't clone */
1958 if (l == output->output_info->nclone)
1964 /* make sure the state matches */
1965 if (crtc->mode_info != output->mode_info)
1967 if (crtc->x != output->x)
1969 if (crtc->y != output->y)
1971 if (crtc->rotation != output->rotation)
1973 if (!equal_transform (&crtc->current_transform, &output->transform))
1976 else if (crtc->crtc_info->noutput)
1978 /* make sure the state matches the already used state */
1979 XRRModeInfo *mode = find_mode_by_xid (crtc->crtc_info->mode);
1981 if (mode != output->mode_info)
1983 if (crtc->crtc_info->x != output->x)
1985 if (crtc->crtc_info->y != output->y)
1987 if (crtc->crtc_info->rotation != output->rotation)
1994 find_crtc_for_output (output_t *output)
1998 for (c = 0; c < output->output_info->ncrtc; c++)
2002 crtc = find_crtc_by_xid (output->output_info->crtcs[c]);
2003 if (!crtc) fatal ("cannot find crtc 0x%lx\n", output->output_info->crtcs[c]);
2005 if (check_crtc_for_output (crtc, output))
2012 set_positions (void)
2023 for (output = all_outputs; output; output = output->next)
2026 name_t relation_name;
2028 if (!(output->changes & changes_relation)) continue;
2030 if (output->mode_info == NULL) continue;
2032 init_name (&relation_name);
2033 set_name_string (&relation_name, output->relative_to);
2034 relation = find_output (&relation_name);
2035 if (!relation) fatal ("cannot find output \"%s\"\n", output->relative_to);
2037 if (relation->mode_info == NULL)
2041 output->changes |= changes_position;
2046 * Make sure the dependent object has been set in place
2048 if ((relation->changes & changes_relation) &&
2049 !(relation->changes & changes_position))
2055 switch (output->relation) {
2056 case relation_left_of:
2057 output->y = relation->y;
2058 output->x = relation->x - mode_width (output->mode_info, output->rotation);
2060 case relation_right_of:
2061 output->y = relation->y;
2062 output->x = relation->x + mode_width (relation->mode_info, relation->rotation);
2064 case relation_above:
2065 output->x = relation->x;
2066 output->y = relation->y - mode_height (output->mode_info, output->rotation);
2068 case relation_below:
2069 output->x = relation->x;
2070 output->y = relation->y + mode_height (relation->mode_info, relation->rotation);
2072 case relation_same_as:
2073 output->x = relation->x;
2074 output->y = relation->y;
2076 output->changes |= changes_position;
2082 fatal ("loop in relative position specifications\n");
2086 * Now normalize positions so the upper left corner of all outputs is at 0,0
2090 for (output = all_outputs; output; output = output->next)
2092 if (output->mode_info == NULL) continue;
2094 if (output->x < min_x) min_x = output->x;
2095 if (output->y < min_y) min_y = output->y;
2099 /* move all outputs */
2100 for (output = all_outputs; output; output = output->next)
2102 if (output->mode_info == NULL) continue;
2106 output->changes |= changes_position;
2112 set_screen_size (void)
2115 Bool fb_specified = fb_width != 0 && fb_height != 0;
2117 for (output = all_outputs; output; output = output->next)
2119 XRRModeInfo *mode_info = output->mode_info;
2123 if (!mode_info) continue;
2125 mode_geometry (mode_info, output->rotation,
2126 &output->transform.transform,
2128 x = output->x + bounds.x1;
2129 y = output->y + bounds.y1;
2130 w = bounds.x2 - bounds.x1;
2131 h = bounds.y2 - bounds.y1;
2132 /* make sure output fits in specified size */
2135 if (x + w > fb_width || y + h > fb_height)
2136 warning ("specified screen %dx%d not large enough for output %s (%dx%d+%d+%d)\n",
2137 fb_width, fb_height, output->output.string, w, h, x, y);
2139 /* fit fb to output */
2143 if (x + w > fb_width)
2145 if (y + h > fb_height)
2147 if (output->changes & changes_panning)
2148 pan = &output->panning;
2150 pan = output->crtc_info ? output->crtc_info->panning_info : NULL;
2151 if (pan && pan->left + pan->width > fb_width)
2152 fb_width = pan->left + pan->width;
2153 if (pan && pan->top + pan->height > fb_height)
2154 fb_height = pan->top + pan->height;
2158 if (fb_width > maxWidth || fb_height > maxHeight)
2159 fatal ("screen cannot be larger than %dx%d (desired size %dx%d)\n",
2160 maxWidth, maxHeight, fb_width, fb_height);
2163 if (fb_width < minWidth || fb_height < minHeight)
2164 fatal ("screen must be at least %dx%d\n", minWidth, minHeight);
2168 if (fb_width < minWidth) fb_width = minWidth;
2169 if (fb_height < minHeight) fb_height = minHeight;
2175 disable_outputs (output_t *outputs)
2179 outputs->crtc_info = NULL;
2180 outputs = outputs->next;
2185 * find the best mapping from output to crtc available
2188 pick_crtcs_score (output_t *outputs)
2201 outputs = outputs->next;
2203 * Score with this output disabled
2205 output->crtc_info = NULL;
2206 best_score = pick_crtcs_score (outputs);
2207 if (output->mode_info == NULL)
2212 * Now score with this output any valid crtc
2214 for (c = 0; c < output->output_info->ncrtc; c++)
2218 crtc = find_crtc_by_xid (output->output_info->crtcs[c]);
2220 fatal ("cannot find crtc 0x%lx\n", output->output_info->crtcs[c]);
2222 /* reset crtc allocation for following outputs */
2223 disable_outputs (outputs);
2224 if (!check_crtc_for_output (crtc, output))
2228 /* slight preference for existing connections */
2229 if (crtc == output->current_crtc_info)
2232 output->crtc_info = crtc;
2233 score = my_score + pick_crtcs_score (outputs);
2234 if (score > best_score)
2240 if (output->crtc_info != best_crtc)
2241 output->crtc_info = best_crtc;
2243 * Reset other outputs based on this one using the best crtc
2245 (void) pick_crtcs_score (outputs);
2251 * Pick crtcs for any changing outputs that don't have one
2257 int saved_crtc_noutput[num_crtcs];
2261 * First try to match up newly enabled outputs with spare crtcs
2263 for (output = all_outputs; output; output = output->next)
2265 if (output->changes && output->mode_info)
2267 if (output->crtc_info) {
2268 if (output->crtc_info->crtc_info->noutput > 0 &&
2269 (output->crtc_info->crtc_info->noutput > 1 ||
2270 output != find_output_by_xid (output->crtc_info->crtc_info->outputs[0])))
2273 output->crtc_info = find_crtc_for_output (output);
2274 if (!output->crtc_info)
2285 * When the simple way fails, see if there is a way
2286 * to swap crtcs around and make things work
2288 for (output = all_outputs; output; output = output->next)
2289 output->current_crtc_info = output->crtc_info;
2291 /* Mark all CRTC as currently unused */
2292 for (n = 0; n < num_crtcs; n++) {
2293 saved_crtc_noutput[n] = crtcs[n].crtc_info->noutput;
2294 crtcs[n].crtc_info->noutput = 0;
2297 pick_crtcs_score (all_outputs);
2299 for (n = 0; n < num_crtcs; n++)
2300 crtcs[n].crtc_info->noutput = saved_crtc_noutput[n];
2302 for (output = all_outputs; output; output = output->next)
2304 if (output->mode_info && !output->crtc_info)
2305 fatal ("cannot find crtc for output %s\n", output->output.string);
2306 if (!output->changes && output->crtc_info != output->current_crtc_info)
2307 output->changes |= changes_crtc;
2312 check_strtol(char *s)
2315 int result = strtol(s, &endptr, 10);
2317 argerr ("failed to parse '%s' as a number\n", s);
2322 check_strtod(char *s)
2325 double result = strtod(s, &endptr);
2327 argerr ("failed to parse '%s' as a number\n", s);
2333 property_values_from_string(const char *str, const Atom type, const int format,
2334 int *returned_nitems)
2337 void *returned_bytes = NULL;
2338 int nitems = 0, bytes_per_item;
2340 if (type != XA_INTEGER && type != XA_CARDINAL)
2343 /* compute memory needed for Xlib datatype (sigh) */
2346 bytes_per_item = sizeof(char);
2349 bytes_per_item = sizeof(short);
2352 bytes_per_item = sizeof(long);
2360 for (token = strtok (tmp, ","); token; token = strtok (NULL, ","))
2363 long int val = strtol (token, &endptr, 0);
2365 if (token == endptr || *endptr != '\0')
2367 argerr ("failed to parse '%s' as a number\n", token);
2370 returned_bytes = realloc (returned_bytes, (nitems + 1) * bytes_per_item);
2372 if (type == XA_INTEGER && format == 8)
2374 signed char *ptr = returned_bytes;
2375 ptr[nitems] = (char) val;
2377 else if (type == XA_INTEGER && format == 16)
2379 short *ptr = returned_bytes;
2380 ptr[nitems] = (short) val;
2382 else if (type == XA_INTEGER && format == 32)
2384 long *ptr = returned_bytes;
2385 ptr[nitems] = (long) val;
2387 else if (type == XA_CARDINAL && format == 8)
2389 unsigned char *ptr = returned_bytes;
2390 ptr[nitems] = (unsigned char) val;
2392 else if (type == XA_CARDINAL && format == 16)
2394 unsigned short *ptr = returned_bytes;
2395 ptr[nitems] = (unsigned short) val;
2397 else if (type == XA_CARDINAL && format == 32)
2399 unsigned long *ptr = returned_bytes;
2400 ptr[nitems] = (unsigned long) val;
2405 free (returned_bytes);
2414 *returned_nitems = nitems;
2415 return returned_bytes;
2420 print_output_property_value(int value_format, /* 8, 16, 32 */
2421 Atom value_type, /* XA_{ATOM,INTEGER,CARDINAL} */
2422 const void *value_bytes)
2424 if (value_type == XA_ATOM && value_format == 32)
2426 const Atom *val = value_bytes;
2427 char *str = XGetAtomName (dpy, *val);
2436 if (value_type == XA_INTEGER)
2438 if (value_format == 8)
2440 const signed char *val = value_bytes;
2441 printf ("%d", *val);
2444 if (value_format == 16)
2446 const short *val = value_bytes;
2447 printf ("%d", *val);
2450 if (value_format == 32)
2452 const long *val = value_bytes;
2453 printf ("%ld", *val);
2458 if (value_type == XA_CARDINAL)
2460 if (value_format == 8)
2462 const unsigned char *val = value_bytes;
2463 printf ("%u", *val);
2466 if (value_format == 16)
2468 const unsigned short *val = value_bytes;
2469 printf ("%u", *val);
2472 if (value_format == 32)
2474 const unsigned long *val = value_bytes;
2475 printf ("%lu", *val);
2484 print_edid(int nitems, const unsigned char *prop)
2490 for (k = 0; k < nitems; k++)
2492 if (k != 0 && (k % 16) == 0)
2497 printf("%02" PRIx8, prop[k]);
2504 print_guid(const unsigned char *prop)
2510 for (k = 0; k < 16; k++)
2512 printf("%02" PRIX8, prop[k]);
2513 if (k == 3 || k == 5 || k == 7 || k == 9)
2523 print_output_property(const char *atom_name,
2527 const unsigned char *prop)
2532 switch (value_format) {
2534 bytes_per_item = sizeof(char);
2537 bytes_per_item = sizeof(short);
2540 bytes_per_item = sizeof(long);
2546 * Check for properties that need special formatting.
2548 if (strcmp (atom_name, "EDID") == 0 && value_format == 8 &&
2549 value_type == XA_INTEGER)
2551 print_edid (nitems, prop);
2554 else if (strcmp (atom_name, "GUID") == 0 && value_format == 8 &&
2555 value_type == XA_INTEGER && nitems == 16)
2561 for (k = 0; k < nitems; k++)
2570 print_output_property_value (value_format, value_type,
2571 prop + (k * bytes_per_item));
2579 get_providers (void)
2581 XRRProviderResources *pr;
2584 if (!has_1_4 || providers)
2587 pr = XRRGetProviderResources(dpy, root);
2588 num_providers = pr->nproviders;
2589 providers = calloc (num_providers, sizeof (provider_t));
2591 fatal ("out of memory\n");
2593 for (i = 0; i < num_providers; i++) {
2594 provider_t *provider = &providers[i];
2595 name_t *name = &provider->provider;
2596 XRRProviderInfo *info = XRRGetProviderInfo(dpy, res, pr->providers[i]);
2598 provider->info = info;
2599 set_name_xid (name, pr->providers[i]);
2600 set_name_index (name, i);
2601 set_name_string (name, info->name);
2604 XRRFreeProviderResources(pr);
2608 find_provider (name_t *name)
2612 if ((name->kind & name_xid) && name->xid == 0)
2614 for (i = 0; i < num_providers; i++) {
2615 provider_t *p = &providers[i];
2616 name_kind_t common = name->kind & p->provider.kind;
2618 if ((common & name_xid) && name->xid == p->provider.xid)
2620 if ((common & name_string) && !strcmp (name->string, p->provider.string))
2622 if ((common & name_index) && name->index == p->provider.index)
2626 printf ("Could not find provider with ");
2633 get_monitors(Bool get_active)
2638 if (!has_1_5 || monitors)
2641 m = XRRGetMonitors(dpy, root, get_active, &n);
2643 fatal("get monitors failed\n");
2644 monitors = calloc(1, sizeof (monitors_t));
2646 monitors->monitors = m;
2650 main (int argc, char **argv)
2652 XRRScreenSize *sizes;
2653 XRRScreenConfiguration *sc;
2657 Status status = RRSetConfigFailed;
2660 int action_requested = False;
2661 Rotation current_rotation;
2663 XRRScreenChangeNotifyEvent *sce;
2664 char *display_name = NULL;
2666 SizeID current_size;
2672 Bool version = False;
2673 int event_base, error_base;
2675 int width = 0, height = 0;
2676 Bool have_pixel_size = False;
2678 output_t *config_output = NULL;
2679 Bool setit_1_2 = False;
2680 Bool query_1_2 = False;
2681 Bool modeit = False;
2682 Bool propit = False;
2683 Bool query_1 = False;
2684 Bool list_providers = False;
2685 Bool provsetoutsource = False;
2686 Bool provsetoffsink = False;
2687 Bool monitorit = False;
2688 Bool list_monitors = False;
2689 Bool list_active_monitors = False;
2691 Bool current = False;
2692 Bool toggle_x = False;
2693 Bool toggle_y = False;
2695 program_name = argv[0];
2696 for (i = 1; i < argc; i++) {
2697 if (!strcmp ("-display", argv[i]) || !strcmp ("--display", argv[i]) ||
2698 !strcmp ("-d", argv[i])) {
2699 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2700 display_name = argv[i];
2703 if (!strcmp("-help", argv[i]) || !strcmp("--help", argv[i])) {
2707 if (!strcmp ("--verbose", argv[i])) {
2711 if (!strcmp ("--dryrun", argv[i])) {
2716 if (!strcmp ("--nograb", argv[i])) {
2717 grab_server = False;
2720 if (!strcmp("--current", argv[i])) {
2725 if (!strcmp ("-s", argv[i]) || !strcmp ("--size", argv[i])) {
2726 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2727 if (sscanf (argv[i], "%dx%d", &width, &height) == 2) {
2728 have_pixel_size = True;
2730 size = check_strtol(argv[i]);
2731 if (size < 0) argerr ("--size argument must be nonnegative\n");
2734 action_requested = True;
2738 if (!strcmp ("-r", argv[i]) ||
2739 !strcmp ("--rate", argv[i]) ||
2740 !strcmp ("--refresh", argv[i]))
2742 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2743 rate = check_strtod(argv[i]);
2747 config_output->refresh = rate;
2748 config_output->changes |= changes_refresh;
2751 action_requested = True;
2755 if (!strcmp ("-v", argv[i]) || !strcmp ("--version", argv[i])) {
2757 action_requested = True;
2761 if (!strcmp ("-x", argv[i])) {
2764 action_requested = True;
2767 if (!strcmp ("-y", argv[i])) {
2770 action_requested = True;
2773 if (!strcmp ("--screen", argv[i])) {
2774 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2775 screen = check_strtol(argv[i]);
2776 if (screen < 0) argerr ("--screen argument must be nonnegative\n");
2779 if (!strcmp ("-q", argv[i]) || !strcmp ("--query", argv[i])) {
2783 if (!strcmp ("-o", argv[i]) || !strcmp ("--orientation", argv[i])) {
2785 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2786 dirind = strtol(argv[i], &endptr, 10);
2787 if (argv[i] == endptr) {
2788 for (dirind = 0; dirind < 4; dirind++) {
2789 if (strcmp (direction[dirind], argv[i]) == 0) break;
2792 if ((dirind < 0) || (dirind > 3))
2793 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2796 action_requested = True;
2799 if (!strcmp ("--prop", argv[i]) ||
2800 !strcmp ("--props", argv[i]) ||
2801 !strcmp ("--madprops", argv[i]) ||
2802 !strcmp ("--properties", argv[i]))
2806 action_requested = True;
2809 if (!strcmp ("--output", argv[i])) {
2810 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2812 config_output = find_output_by_name (argv[i]);
2813 if (!config_output) {
2814 config_output = add_output ();
2815 set_name (&config_output->output, argv[i], name_string|name_xid);
2819 action_requested = True;
2822 if (!strcmp("--filter", argv[i])) {
2825 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2826 if (++i >= argc) argerr("%s requires an argument\n", argv[i-1]);
2829 for (t = 0; t < sizeof(filter_names) / sizeof(filter_names[0]); t++)
2831 if (!strcmp(filter_names[t], argv[i]))
2838 if (filter_type == -1) argerr("Bad argument: %s, for a filter\n", argv[i]);
2840 config_output->changes |= changes_filter;
2841 action_requested = True;
2844 if (!strcmp ("--crtc", argv[i])) {
2845 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2846 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2847 set_name (&config_output->crtc, argv[i], name_xid|name_index);
2848 config_output->changes |= changes_crtc;
2851 if (!strcmp ("--mode", argv[i])) {
2852 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2853 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2854 set_name (&config_output->mode, argv[i], name_string|name_xid);
2855 config_output->changes |= changes_mode;
2858 if (!strcmp ("--preferred", argv[i])) {
2859 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2860 set_name_preferred (&config_output->mode);
2861 config_output->changes |= changes_mode;
2864 if (!strcmp ("--pos", argv[i])) {
2865 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2866 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2867 if (sscanf (argv[i], "%dx%d",
2868 &config_output->x, &config_output->y) != 2)
2869 argerr ("failed to parse '%s' as a position\n", argv[i]);
2870 config_output->changes |= changes_position;
2873 if (!strcmp ("--rotation", argv[i]) || !strcmp ("--rotate", argv[i])) {
2874 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2875 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2876 for (dirind = 0; dirind < 4; dirind++) {
2877 if (strcmp (direction[dirind], argv[i]) == 0) break;
2880 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2881 config_output->rotation &= ~0xf;
2882 config_output->rotation |= 1 << dirind;
2883 config_output->changes |= changes_rotation;
2886 if (!strcmp ("--reflect", argv[i]) || !strcmp ("--reflection", argv[i])) {
2887 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2888 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2889 for (dirind = 0; dirind < 4; dirind++) {
2890 if (strcmp (reflections[dirind], argv[i]) == 0) break;
2893 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2894 config_output->rotation &= ~(RR_Reflect_X|RR_Reflect_Y);
2895 config_output->rotation |= dirind * RR_Reflect_X;
2896 config_output->changes |= changes_reflection;
2899 if (!strcmp ("--left-of", argv[i])) {
2900 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2901 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2902 config_output->relation = relation_left_of;
2903 config_output->relative_to = argv[i];
2904 config_output->changes |= changes_relation;
2907 if (!strcmp ("--right-of", argv[i])) {
2908 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2909 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2910 config_output->relation = relation_right_of;
2911 config_output->relative_to = argv[i];
2912 config_output->changes |= changes_relation;
2915 if (!strcmp ("--above", argv[i])) {
2916 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2917 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2918 config_output->relation = relation_above;
2919 config_output->relative_to = argv[i];
2920 config_output->changes |= changes_relation;
2923 if (!strcmp ("--below", argv[i])) {
2924 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2925 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2926 config_output->relation = relation_below;
2927 config_output->relative_to = argv[i];
2928 config_output->changes |= changes_relation;
2931 if (!strcmp ("--same-as", argv[i])) {
2932 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2933 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2934 config_output->relation = relation_same_as;
2935 config_output->relative_to = argv[i];
2936 config_output->changes |= changes_relation;
2939 if (!strcmp ("--panning", argv[i])) {
2941 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2942 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2943 pan = &config_output->panning;
2944 switch (sscanf (argv[i], "%dx%d+%d+%d/%dx%d+%d+%d/%d/%d/%d/%d",
2945 &pan->width, &pan->height, &pan->left, &pan->top,
2946 &pan->track_width, &pan->track_height,
2947 &pan->track_left, &pan->track_top,
2948 &pan->border_left, &pan->border_top,
2949 &pan->border_right, &pan->border_bottom)) {
2951 pan->left = pan->top = 0;
2954 pan->track_left = pan->track_top =
2955 pan->track_width = pan->track_height = 0;
2958 pan->border_left = pan->border_top =
2959 pan->border_right = pan->border_bottom = 0;
2964 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2966 config_output->changes |= changes_panning;
2969 if (!strcmp ("--gamma", argv[i])) {
2970 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2971 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2972 if (sscanf(argv[i], "%f:%f:%f", &config_output->gamma.red,
2973 &config_output->gamma.green, &config_output->gamma.blue) != 3)
2974 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2975 config_output->changes |= changes_gamma;
2979 if (!strcmp ("--brightness", argv[i])) {
2980 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2981 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
2982 if (sscanf(argv[i], "%f", &config_output->brightness) != 1)
2983 argerr ("%s: invalid argument '%s'\n", argv[i-1], argv[i]);
2984 config_output->changes |= changes_gamma;
2988 if (!strcmp ("--primary", argv[i])) {
2989 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
2990 config_output->changes |= changes_primary;
2991 config_output->primary = True;
2995 if (!strcmp ("--noprimary", argv[i])) {
3000 if (!strcmp ("--set", argv[i])) {
3001 output_prop_t *prop;
3002 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
3003 if (i+2 >= argc) argerr ("%s requires two arguments\n", argv[i]);
3004 prop = malloc (sizeof (output_prop_t));
3005 prop->next = config_output->props;
3006 config_output->props = prop;
3007 prop->name = argv[++i];
3008 prop->value = argv[++i];
3010 config_output->changes |= changes_property;
3014 if (!strcmp ("--scale", argv[i]))
3017 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
3018 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
3019 if (sscanf (argv[i], "%lfx%lf", &sx, &sy) != 2)
3020 argerr ("failed to parse '%s' as a scaling factor\n", argv[i]);
3021 init_transform (&config_output->transform);
3022 config_output->transform.transform.matrix[0][0] = XDoubleToFixed (sx);
3023 config_output->transform.transform.matrix[1][1] = XDoubleToFixed (sy);
3024 config_output->transform.transform.matrix[2][2] = XDoubleToFixed (1.0);
3025 if (sx != 1 || sy != 1)
3026 config_output->transform.filter = "bilinear";
3028 config_output->transform.filter = "nearest";
3029 config_output->transform.nparams = 0;
3030 config_output->transform.params = NULL;
3031 config_output->changes |= changes_transform;
3034 if (!strcmp ("--scale-from", argv[i]))
3037 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
3038 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
3039 if (sscanf (argv[i], "%dx%d", &w, &h) != 2)
3040 argerr ("failed to parse '%s' as a scale-from size\n", argv[i]);
3041 if (w <=0 || h <= 0)
3042 argerr ("--scale-from dimensions must be nonnegative\n");
3043 config_output->scale_from_w = w;
3044 config_output->scale_from_h = h;
3045 config_output->changes |= changes_transform;
3048 if (!strcmp ("--transform", argv[i])) {
3049 double transform[3][3];
3051 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
3052 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
3053 init_transform (&config_output->transform);
3054 if (strcmp (argv[i], "none") != 0)
3056 if (sscanf(argv[i], "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf",
3057 &transform[0][0],&transform[0][1],&transform[0][2],
3058 &transform[1][0],&transform[1][1],&transform[1][2],
3059 &transform[2][0],&transform[2][1],&transform[2][2])
3061 argerr ("failed to parse '%s' as a transformation\n", argv[i]);
3062 init_transform (&config_output->transform);
3063 for (k = 0; k < 3; k++)
3064 for (l = 0; l < 3; l++) {
3065 config_output->transform.transform.matrix[k][l] = XDoubleToFixed (transform[k][l]);
3067 config_output->transform.filter = "bilinear";
3068 config_output->transform.nparams = 0;
3069 config_output->transform.params = NULL;
3071 config_output->changes |= changes_transform;
3074 if (!strcmp ("--off", argv[i])) {
3075 if (!config_output) argerr ("%s must be used after --output\n", argv[i]);
3076 set_name_xid (&config_output->mode, None);
3077 set_name_xid (&config_output->crtc, None);
3078 config_output->changes |= changes_mode | changes_crtc;
3081 if (!strcmp ("--fb", argv[i])) {
3082 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
3083 if (sscanf (argv[i], "%dx%d",
3084 &fb_width, &fb_height) != 2)
3085 argerr ("failed to parse '%s' as a framebuffer size\n", argv[i]);
3087 action_requested = True;
3090 if (!strcmp ("--fbmm", argv[i])) {
3091 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
3092 if (sscanf (argv[i], "%dx%d",
3093 &fb_width_mm, &fb_height_mm) != 2)
3094 argerr ("failed to parse '%s' as a physical size\n", argv[i]);
3096 action_requested = True;
3099 if (!strcmp ("--dpi", argv[i])) {
3101 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
3102 dpi = strtod(argv[i], &strtod_error);
3103 if (argv[i] == strtod_error)
3106 dpi_output_name = argv[i];
3109 action_requested = True;
3112 if (!strcmp ("--auto", argv[i])) {
3115 config_output->automatic = True;
3116 config_output->changes |= changes_automatic;
3121 action_requested = True;
3124 if (!strcmp ("--q12", argv[i]))
3129 if (!strcmp ("--q1", argv[i]))
3134 if (!strcmp ("--newmode", argv[i]))
3136 umode_t *m = calloc (1, sizeof (umode_t));
3141 argerr ("failed to parse '%s' as a mode specification\n", argv[i]);
3142 m->mode.name = argv[i];
3143 m->mode.nameLength = strlen (argv[i]);
3145 clock = check_strtod(argv[i++]);
3146 m->mode.dotClock = clock * 1e6;
3148 m->mode.width = check_strtol(argv[i++]);
3149 m->mode.hSyncStart = check_strtol(argv[i++]);
3150 m->mode.hSyncEnd = check_strtol(argv[i++]);
3151 m->mode.hTotal = check_strtol(argv[i++]);
3152 m->mode.height = check_strtol(argv[i++]);
3153 m->mode.vSyncStart = check_strtol(argv[i++]);
3154 m->mode.vSyncEnd = check_strtol(argv[i++]);
3155 m->mode.vTotal = check_strtol(argv[i++]);
3156 m->mode.modeFlags = 0;
3160 for (f = 0; mode_flags[f].string; f++)
3161 if (!strcasecmp (mode_flags[f].string, argv[i]))
3164 if (!mode_flags[f].string)
3166 m->mode.modeFlags |= mode_flags[f].flag;
3170 m->action = umode_create;
3173 action_requested = True;
3176 if (!strcmp ("--rmmode", argv[i]))
3178 umode_t *m = calloc (1, sizeof (umode_t));
3180 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
3181 set_name (&m->name, argv[i], name_string|name_xid);
3182 m->action = umode_destroy;
3186 action_requested = True;
3189 if (!strcmp ("--addmode", argv[i]))
3191 umode_t *m = calloc (1, sizeof (umode_t));
3193 if (i+2 >= argc) argerr ("%s requires two arguments\n", argv[i]);
3194 set_name (&m->output, argv[++i], name_string|name_xid);
3195 set_name (&m->name, argv[++i], name_string|name_xid);
3196 m->action = umode_add;
3200 action_requested = True;
3203 if (!strcmp ("--delmode", argv[i]))
3205 umode_t *m = calloc (1, sizeof (umode_t));
3207 if (i+2 >= argc) argerr ("%s requires two arguments\n", argv[i]);
3208 set_name (&m->output, argv[++i], name_string|name_xid);
3209 set_name (&m->name, argv[++i], name_string|name_xid);
3210 m->action = umode_delete;
3214 action_requested = True;
3217 if (!strcmp ("--listproviders", argv[i]))
3219 list_providers = True;
3220 action_requested = True;
3223 if (!strcmp("--setprovideroutputsource", argv[i]))
3225 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
3226 set_name (&provider_name, argv[i], name_string|name_xid|name_index);
3228 set_name_xid (&output_source_provider_name, 0);
3230 set_name (&output_source_provider_name, argv[i], name_string|name_xid|name_index);
3231 action_requested = True;
3232 provsetoutsource = True;
3235 if (!strcmp("--setprovideroffloadsink", argv[i]))
3237 if (++i >= argc) argerr ("%s requires an argument\n", argv[i-1]);
3238 set_name (&provider_name, argv[i], name_string|name_xid|name_index);
3240 set_name_xid (&offload_sink_provider_name, 0);
3242 set_name (&offload_sink_provider_name, argv[i], name_string|name_xid|name_index);
3243 action_requested = True;
3244 provsetoffsink = True;
3247 if (!strcmp("--listmonitors", argv[i]))
3249 list_monitors = True;
3250 action_requested = True;
3253 if (!strcmp("--listactivemonitors", argv[i]))
3255 list_active_monitors = True;
3256 action_requested = True;
3259 if (!strcmp("--setmonitor", argv[i]))
3261 umonitor_t *m = calloc(1, sizeof (umonitor_t)), **l;
3267 if (i+3 >= argc) argerr("%s requires three argument\n", argv[i]);
3277 if (strncmp (geom, "auto", 4) != 0) {
3278 if (sscanf(geom, "%d/%dx%d/%d+%d+%d",
3279 &m->width, &m->mmwidth, &m->height, &m->mmheight, &m->x, &m->y) != 6)
3280 argerr ("failed to parse '%s' as monitor geometry\n", argv[i]);
3284 if (strcmp(o, "none") != 0) {
3285 printf ("output list %s\n", o);
3286 for (; (t = strtok(o, ",")) != NULL; o = NULL) {
3287 m->outputs = realloc(m->outputs, (m->noutput + 1) * sizeof (name_t));
3288 printf ("add monitor %s\n", t);
3289 set_name(&m->outputs[m->noutput++], t, name_string|name_xid|name_index);
3290 printf ("output name %s\n", m->outputs[m->noutput-1].string);
3293 for (l = &umonitors; *l; l = &((*l)->next));
3295 action_requested = True;
3299 if (!strcmp("--delmonitor", argv[i]))
3301 umonitor_t *m = calloc(1, sizeof (umonitor_t)), **l;
3303 if (++i >= argc) argerr("%s requires an argument\n", argv[i-1]);
3307 for (l = &umonitors; *l; l = &((*l)->next));
3309 action_requested = True;
3314 argerr ("unrecognized option '%s'\n", argv[i]);
3316 if (!action_requested)
3321 if (setit && !setit_1_2)
3325 printf("xrandr program version " VERSION "\n");
3327 dpy = XOpenDisplay (display_name);
3330 fprintf (stderr, "Can't open display %s\n", XDisplayName(display_name));
3334 screen = DefaultScreen (dpy);
3335 if (screen >= ScreenCount (dpy)) {
3336 fprintf (stderr, "Invalid screen number %d (display has %d)\n",
3337 screen, ScreenCount (dpy));
3341 root = RootWindow (dpy, screen);
3343 if (!XRRQueryExtension (dpy, &event_base, &error_base) ||
3344 !XRRQueryVersion (dpy, &major, &minor))
3346 fprintf (stderr, "RandR extension missing\n");
3349 if (major > 1 || (major == 1 && minor >= 2))
3351 if (major > 1 || (major == 1 && minor >= 3))
3353 if (major > 1 || (major == 1 && minor >= 4))
3355 if (major > 1 || (major == 1 && minor >= 5))
3357 if (has_1_2 && modeit)
3365 for (m = umodes; m; m = m->next)
3370 switch (m->action) {
3372 XRRCreateMode (dpy, root, &m->mode);
3375 e = find_mode (&m->name, 0);
3377 fatal ("cannot find mode \"%s\"\n", m->name.string);
3378 XRRDestroyMode (dpy, e->id);
3381 o = find_output (&m->output);
3383 fatal ("cannot find output \"%s\"\n", m->output.string);
3384 e = find_mode (&m->name, 0);
3386 fatal ("cannot find mode \"%s\"\n", m->name.string);
3387 XRRAddOutputMode (dpy, o->output.xid, e->id);
3390 o = find_output (&m->output);
3392 fatal ("cannot find output \"%s\"\n", m->output.string);
3393 e = find_mode (&m->name, 0);
3395 fatal ("cannot find mode \"%s\"\n", m->name.string);
3396 XRRDeleteOutputMode (dpy, o->output.xid, e->id);
3400 if (!propit && !setit_1_2 && !monitorit)
3406 if (has_1_2 && propit)
3414 for (output = all_outputs; output; output = output->next)
3416 output_prop_t *prop;
3418 for (prop = output->props; prop; prop = prop->next)
3420 Atom name = XInternAtom (dpy, prop->name, False);
3423 unsigned char *data, *malloced_data = NULL;
3426 unsigned long ulong_value;
3427 unsigned char *prop_data;
3429 unsigned long nitems, bytes_after;
3431 XRRPropertyInfo *propinfo;
3433 type = AnyPropertyType;
3435 if (XRRGetOutputProperty (dpy, output->output.xid, name,
3436 0, 100, False, False,
3438 &actual_type, &actual_format,
3439 &nitems, &bytes_after, &prop_data) == Success &&
3441 (propinfo = XRRQueryOutputProperty(dpy, output->output.xid,
3445 format = actual_format;
3448 malloced_data = property_values_from_string
3449 (prop->value, type, actual_format, &nelements);
3453 data = malloced_data;
3455 format = actual_format;
3457 else if (type == AnyPropertyType &&
3458 (sscanf (prop->value, "%d", &int_value) == 1 ||
3459 sscanf (prop->value, "0x%x", &int_value) == 1))
3462 ulong_value = int_value;
3463 data = (unsigned char *) &ulong_value;
3467 else if (type == XA_ATOM)
3469 ulong_value = XInternAtom (dpy, prop->value, False);
3470 data = (unsigned char *) &ulong_value;
3473 else if (type == XA_STRING || type == AnyPropertyType)
3476 data = (unsigned char *) prop->value;
3477 nelements = strlen (prop->value);
3482 XRRChangeOutputProperty (dpy, output->output.xid,
3483 name, type, format, PropModeReplace,
3485 free (malloced_data);
3494 if (provsetoutsource)
3496 provider_t *provider, *source;
3499 fatal ("--setprovideroutputsource requires RandR 1.4\n");
3504 provider = find_provider (&provider_name);
3505 source = find_provider(&output_source_provider_name);
3507 XRRSetProviderOutputSource(dpy, provider->provider.xid, source ? source->provider.xid : 0);
3511 provider_t *provider, *sink;
3514 fatal ("--setprovideroffloadsink requires RandR 1.4\n");
3519 provider = find_provider (&provider_name);
3520 sink = find_provider(&offload_sink_provider_name);
3522 XRRSetProviderOffloadSink(dpy, provider->provider.xid, sink ? sink->provider.xid : 0);
3535 * Assign outputs to crtcs
3540 * Mark changing crtcs
3542 mark_changing_crtcs ();
3545 * If an output was specified to track dpi, use it
3547 if (dpi_output_name)
3549 output_t *dpi_output = find_output_by_name (dpi_output_name);
3550 XRROutputInfo *output_info;
3551 XRRModeInfo *mode_info;
3553 fatal ("Cannot find output %s\n", dpi_output_name);
3554 output_info = dpi_output->output_info;
3555 mode_info = dpi_output->mode_info;
3556 if (output_info && mode_info && output_info->mm_height)
3559 * When this output covers the whole screen, just use
3560 * the known physical size
3562 if (fb_width == mode_info->width &&
3563 fb_height == mode_info->height)
3565 fb_width_mm = output_info->mm_width;
3566 fb_height_mm = output_info->mm_height;
3570 dpi = (25.4 * mode_info->height) / output_info->mm_height;
3576 * Compute physical screen size
3578 if (fb_width_mm == 0 || fb_height_mm == 0)
3580 if (fb_width != DisplayWidth (dpy, screen) ||
3581 fb_height != DisplayHeight (dpy, screen) || dpi != 0.0)
3584 dpi = (25.4 * DisplayHeight (dpy, screen)) / DisplayHeightMM(dpy, screen);
3586 fb_width_mm = (25.4 * fb_width) / dpi;
3587 fb_height_mm = (25.4 * fb_height) / dpi;
3591 fb_width_mm = DisplayWidthMM (dpy, screen);
3592 fb_height_mm = DisplayHeightMM (dpy, screen);
3602 * Set gamma on crtc's that belong to the outputs.
3607 * Now apply all of the changes
3621 printf("RandR 1.5 not supported\n");
3630 for (u = umonitors; u; u = u->next) {
3635 name = XInternAtom(dpy, u->name, False);
3636 m = XRRAllocateMonitor(dpy, u->noutput);
3639 m->primary = u->primary;
3642 m->width = u->width;
3643 m->height = u->height;
3644 m->mwidth = u->mmwidth;
3645 m->mheight = u->mmheight;
3646 for (o = 0; o < u->noutput; o++) {
3647 output_t *output = find_output(&u->outputs[o]);
3649 fatal("cannot find output\n");
3650 m->outputs[o] = output->output.xid;
3653 XRRSetMonitor(dpy, root, m);
3659 name = XInternAtom(dpy, u->name, True);
3661 printf("No monitor named '%s'\n", u->name);
3664 printf ("No monitors\n");
3666 for (m = 0; m < monitors->n; m++) {
3667 if (monitors->monitors[m].name == name)
3670 if (m == monitors->n)
3671 printf("No monitor named '%s'\n", u->name);
3673 XRRDeleteMonitor(dpy, root, name);
3681 if (query_1_2 || (query && has_1_2 && !query_1))
3686 #define ModeShown 0x80000000
3688 get_screen (current);
3692 printf ("Screen %d: minimum %d x %d, current %d x %d, maximum %d x %d\n",
3693 screen, minWidth, minHeight,
3694 DisplayWidth (dpy, screen), DisplayHeight(dpy, screen),
3695 maxWidth, maxHeight);
3697 for (output = all_outputs; output; output = output->next)
3699 XRROutputInfo *output_info = output->output_info;
3700 crtc_t *cur_crtc = output->crtc_info;
3701 XRRCrtcInfo *crtc_info = cur_crtc ? cur_crtc->crtc_info : NULL;
3702 XRRModeInfo *cur_mode = output->mode_info;
3706 Rotation rotations = output_rotations (output);
3708 printf ("%s %s", output_info->name, connection[output_info->connection]);
3709 if (output->primary) {
3715 printf (" %dx%d+%d+%d",
3716 crtc_info->width, crtc_info->height,
3717 crtc_info->x, crtc_info->y);
3719 printf (" %dx%d+%d+%d",
3720 cur_mode->width, cur_mode->height, output->x,
3724 printf (" (0x%x)", (int)cur_mode->id);
3725 if (output->rotation != RR_Rotate_0 || verbose)
3728 rotation_name (output->rotation));
3729 if (output->rotation & (RR_Reflect_X|RR_Reflect_Y))
3730 printf (" %s", reflection_name (output->rotation));
3733 if (rotations != RR_Rotate_0 || verbose)
3737 for (i = 0; i < 4; i ++) {
3738 if ((rotations >> i) & 1) {
3739 if (!first) printf (" ");
3740 printf("%s", direction[i]);
3744 if (rotations & RR_Reflect_X)
3746 if (!first) printf (" ");
3750 if (rotations & RR_Reflect_Y)
3752 if (!first) printf (" ");
3760 printf (" %dmm x %dmm",
3761 (int)output_info->mm_width, (int)output_info->mm_height);
3764 if (cur_crtc && cur_crtc->panning_info &&
3765 cur_crtc->panning_info->width > 0)
3767 XRRPanning *pan = cur_crtc->panning_info;
3768 printf (" panning %dx%d+%d+%d",
3769 pan->width, pan->height, pan->left, pan->top);
3770 if ((pan->track_width != 0 &&
3771 (pan->track_left != pan->left ||
3772 pan->track_width != pan->width ||
3773 pan->border_left != 0 ||
3774 pan->border_right != 0)) ||
3775 (pan->track_height != 0 &&
3776 (pan->track_top != pan->top ||
3777 pan->track_height != pan->height ||
3778 pan->border_top != 0 ||
3779 pan->border_bottom != 0)))
3780 printf (" tracking %dx%d+%d+%d border %d/%d/%d/%d",
3781 pan->track_width, pan->track_height,
3782 pan->track_left, pan->track_top,
3783 pan->border_left, pan->border_top,
3784 pan->border_right, pan->border_bottom);
3790 printf ("\tIdentifier: 0x%x\n", (int)output->output.xid);
3791 printf ("\tTimestamp: %d\n", (int)output_info->timestamp);
3792 printf ("\tSubpixel: %s\n", order[output_info->subpixel_order]);
3793 if (output->gamma.red != 0.0 && output->gamma.green != 0.0 && output->gamma.blue != 0.0) {
3794 printf ("\tGamma: %#.2g:%#.2g:%#.2g\n",
3795 output->gamma.red, output->gamma.green, output->gamma.blue);
3796 printf ("\tBrightness: %#.2g\n", output->brightness);
3798 printf ("\tClones: ");
3799 for (j = 0; j < output_info->nclone; j++)
3801 output_t *clone = find_output_by_xid (output_info->clones[j]);
3803 if (clone) printf (" %s", clone->output.string);
3806 if (output->crtc_info)
3807 printf ("\tCRTC: %d\n", output->crtc_info->crtc.index);
3808 printf ("\tCRTCs: ");
3809 for (j = 0; j < output_info->ncrtc; j++)
3811 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[j]);
3813 printf (" %d", crtc->crtc.index);
3816 if (output->crtc_info && output->crtc_info->panning_info) {
3817 XRRPanning *pan = output->crtc_info->panning_info;
3818 printf ("\tPanning: %dx%d+%d+%d\n",
3819 pan->width, pan->height, pan->left, pan->top);
3820 printf ("\tTracking: %dx%d+%d+%d\n",
3821 pan->track_width, pan->track_height,
3822 pan->track_left, pan->track_top);
3823 printf ("\tBorder: %d/%d/%d/%d\n",
3824 pan->border_left, pan->border_top,
3825 pan->border_right, pan->border_bottom);
3832 printf ("\tTransform: ");
3833 for (y = 0; y < 3; y++)
3835 for (x = 0; x < 3; x++)
3836 printf (" %f", XFixedToDouble (output->transform.transform.matrix[y][x]));
3840 if (output->transform.filter)
3841 printf ("\n\t filter: %s", output->transform.filter);
3844 if (verbose || properties)
3846 props = XRRListOutputProperties (dpy, output->output.xid,
3848 for (j = 0; j < nprop; j++) {
3849 unsigned char *prop;
3851 unsigned long nitems, bytes_after;
3853 XRRPropertyInfo *propinfo;
3854 char *atom_name = XGetAtomName (dpy, props[j]);
3857 XRRGetOutputProperty (dpy, output->output.xid, props[j],
3858 0, 100, False, False,
3860 &actual_type, &actual_format,
3861 &nitems, &bytes_after, &prop);
3863 propinfo = XRRQueryOutputProperty(dpy, output->output.xid,
3866 printf ("\t%s: ", atom_name);
3868 print_output_property(atom_name, actual_format,
3869 actual_type, nitems, prop);
3871 if (propinfo->range && propinfo->num_values > 0)
3873 printf ("\t\trange%s: ",
3874 (propinfo->num_values == 2) ? "" : "s");
3875 for (k = 0; k < propinfo->num_values / 2; k++)
3878 print_output_property_value (32, actual_type,
3879 (unsigned char *) &(propinfo->values[k * 2]));
3881 print_output_property_value (32, actual_type,
3882 (unsigned char *) &(propinfo->values[k * 2 + 1]));
3884 if (k < propinfo->num_values / 2 - 1)
3889 if (!propinfo->range && propinfo->num_values > 0)
3891 printf ("\t\tsupported: ");
3892 for (k = 0; k < propinfo->num_values; k++)
3894 print_output_property_value (32, actual_type,
3895 (unsigned char *) &(propinfo->values[k]));
3896 if (k < propinfo->num_values - 1)
3908 for (j = 0; j < output_info->nmode; j++)
3910 XRRModeInfo *mode = find_mode_by_xid (output_info->modes[j]);
3912 print_verbose_mode (mode, mode == output->mode_info,
3913 j < output_info->npreferred);
3914 mode->modeFlags |= ModeShown;
3919 mode_shown = calloc (output_info->nmode, sizeof (Bool));
3920 if (!mode_shown) fatal ("out of memory\n");
3921 for (j = 0; j < output_info->nmode; j++)
3923 XRRModeInfo *jmode, *kmode;
3926 if (mode_shown[j]) continue;
3928 jmode = find_mode_by_xid (output_info->modes[j]);
3930 printf (" %-12s", jmode->name);
3931 for (k = j; k < output_info->nmode; k++)
3933 if (mode_shown[k]) continue;
3934 kmode = find_mode_by_xid (output_info->modes[k]);
3935 if (strcmp (jmode->name, kmode->name) != 0) continue;
3936 mode_shown[k] = True;
3937 kmode->modeFlags |= ModeShown;
3938 printf (" %6.2f", mode_refresh (kmode));
3939 if (kmode == output->mode_info)
3943 if (k < output_info->npreferred)
3953 for (m = 0; m < res->nmode; m++)
3955 XRRModeInfo *mode = &res->modes[m];
3957 if (!(mode->modeFlags & ModeShown))
3958 print_verbose_mode(mode, False, False);
3962 if (list_providers) {
3966 printf ("RandR 1.4 not supported\n");
3970 get_screen (current);
3976 printf("Providers: number : %d\n", num_providers);
3978 for (j = 0; j < num_providers; j++) {
3979 provider_t *provider = &providers[j];
3980 XRRProviderInfo *info = provider->info;
3982 printf("Provider %d: id: 0x%x cap: 0x%x", j, (int)provider->provider.xid, info->capabilities);
3983 for (k = 0; k < 4; k++)
3984 if (info->capabilities & (1 << k))
3985 printf(", %s", capability_name(1<<k));
3987 printf(" crtcs: %d outputs: %d associated providers: %d name:%s\n", info->ncrtcs, info->noutputs, info->nassociatedproviders, info->name);
3991 if (list_monitors || list_active_monitors) {
3994 printf("RandR 1.5 not supported\n");
3998 get_screen(current);
3999 get_monitors(list_active_monitors ? True : False);
4006 printf("Monitors: %d\n", monitors->n);
4008 for (m = 0; m < monitors->n; m++) {
4009 printf (" %d: %s%s%s %d/%dx%d/%d+%d+%d ",
4011 monitors->monitors[m].automatic ? "+" : "",
4012 monitors->monitors[m].primary ? "*" : "",
4013 XGetAtomName(dpy, monitors->monitors[m].name),
4014 monitors->monitors[m].width,
4015 monitors->monitors[m].mwidth,
4016 monitors->monitors[m].height,
4017 monitors->monitors[m].mheight,
4018 monitors->monitors[m].x,
4019 monitors->monitors[m].y);
4020 for (o = 0; o < monitors->monitors[m].noutput; o++) {
4021 output_t *output = find_output_by_xid(monitors->monitors[m].outputs[o]);
4023 printf (" %s", output->output.string);
4025 printf (" unknown output 0x%x\n", (CARD32) monitors->monitors[m].outputs[o]);
4032 sc = XRRGetScreenInfo (dpy, root);
4037 current_size = XRRConfigCurrentConfiguration (sc, ¤t_rotation);
4039 sizes = XRRConfigSizes(sc, &nsize);
4041 if (have_pixel_size) {
4042 for (size = 0; size < nsize; size++)
4044 if (sizes[size].width == width && sizes[size].height == height)
4047 if (size >= nsize) {
4049 "Size %dx%d not found in available modes\n", width, height);
4054 size = current_size;
4055 else if (size >= nsize) {
4057 "Size index %d is too large, there are only %d sizes\n",
4064 for (rot = 0; rot < 4; rot++)
4065 if (1 << rot == (current_rotation & 0xf))
4069 current_rate = XRRConfigCurrentRate (sc);
4073 if (size == current_size)
4074 rate = current_rate;
4080 rates = XRRConfigRates (sc, size, &nrate);
4081 for (i = 0; i < nrate; i++)
4082 if (rate == rates[i])
4085 fprintf (stderr, "Rate %.2f Hz not available for this size\n", rate);
4091 int major_version, minor_version;
4092 XRRQueryVersion (dpy, &major_version, &minor_version);
4093 printf("Server reports RandR version %d.%d\n",
4094 major_version, minor_version);
4097 if (query || query_1) {
4098 printf(" SZ: Pixels Physical Refresh\n");
4099 for (i = 0; i < nsize; i++) {
4102 printf ("%c%-2d %5d x %-5d (%4dmm x%4dmm )",
4103 i == current_size ? '*' : ' ',
4104 i, sizes[i].width, sizes[i].height,
4105 sizes[i].mwidth, sizes[i].mheight);
4106 rates = XRRConfigRates (sc, i, &nrate);
4107 if (nrate) printf (" ");
4108 for (j = 0; j < nrate; j++)
4110 i == current_size && rates[j] == current_rate ? '*' : ' ',
4117 Rotation rotations = XRRConfigRotations(sc, ¤t_rotation);
4119 if (toggle_x && !(current_rotation & RR_Reflect_X)) reflection |= RR_Reflect_X;
4120 if (toggle_y && !(current_rotation & RR_Reflect_Y)) reflection |= RR_Reflect_Y;
4123 printf("Current rotation - %s\n",
4124 rotation_name (current_rotation));
4126 printf("Current reflection - %s\n",
4127 reflection_name (current_rotation));
4129 printf ("Rotations possible - ");
4130 for (i = 0; i < 4; i ++) {
4131 if ((rotations >> i) & 1) printf("%s ", direction[i]);
4135 printf ("Reflections possible - ");
4136 if (rotations & (RR_Reflect_X|RR_Reflect_Y))
4138 if (rotations & RR_Reflect_X) printf ("X Axis ");
4139 if (rotations & RR_Reflect_Y) printf ("Y Axis");
4148 printf("Setting size to %d, rotation to %s\n", size, direction[rot]);
4150 printf ("Setting reflection on ");
4153 if (reflection & RR_Reflect_X) printf ("X Axis ");
4154 if (reflection & RR_Reflect_Y) printf ("Y Axis");
4157 printf ("neither axis");
4161 /* we should test configureNotify on the root window */
4162 XSelectInput (dpy, root, StructureNotifyMask);
4164 if (setit && !dryrun) XRRSelectInput (dpy, root,
4165 RRScreenChangeNotifyMask);
4166 if (setit && !dryrun) {
4167 Rotation rotation = 1 << rot;
4168 status = XRRSetScreenConfigAndRate (dpy, sc, root, (SizeID) size,
4169 (Rotation) (rotation | reflection),
4173 if (setit && !dryrun && status == RRSetConfigFailed) {
4174 printf ("Failed to change the screen configuration!\n");
4178 if (verbose && setit && !dryrun && size != current_size) {
4179 if (status == RRSetConfigSuccess)
4181 Bool seen_screen = False;
4182 while (!seen_screen) {
4184 XNextEvent(dpy, (XEvent *) &event);
4186 printf ("Event received, type = %d\n", event.type);
4187 /* update Xlib's knowledge of the event */
4188 XRRUpdateConfiguration (&event);
4189 if (event.type == ConfigureNotify)
4190 printf("Received ConfigureNotify Event!\n");
4192 switch (event.type - event_base) {
4193 case RRScreenChangeNotify:
4194 sce = (XRRScreenChangeNotifyEvent *) &event;
4196 printf("Got a screen change notify event!\n");
4197 printf(" window = %d\n root = %d\n size_index = %d\n rotation %d\n",
4198 (int) sce->window, (int) sce->root,
4199 sce->size_index, sce->rotation);
4200 printf(" timestamp = %ld, config_timestamp = %ld\n",
4201 sce->timestamp, sce->config_timestamp);
4202 printf(" Rotation = %x\n", sce->rotation);
4203 printf(" %d X %d pixels, %d X %d mm\n",
4204 sce->width, sce->height, sce->mwidth, sce->mheight);
4205 printf("Display width %d, height %d\n",
4206 DisplayWidth(dpy, screen), DisplayHeight(dpy, screen));
4207 printf("Display widthmm %d, heightmm %d\n",
4208 DisplayWidthMM(dpy, screen), DisplayHeightMM(dpy, screen));
4209 spo = sce->subpixel_order;
4210 if ((spo < 0) || (spo > 5))
4211 printf ("Unknown subpixel order, value = %d\n", spo);
4212 else printf ("new Subpixel rendering model is %s\n", order[spo]);
4216 if (event.type != ConfigureNotify)
4217 printf("unknown event received, type = %d!\n", event.type);
4222 XRRFreeScreenConfigInfo(sc);