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.
35 #include <X11/Xatom.h>
37 #include <X11/Xlibint.h>
38 #include <X11/Xproto.h>
39 #include <X11/extensions/Xrandr.h>
40 #include <X11/extensions/Xrender.h> /* we share subpixel information */
44 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)
45 #define _X_NORETURN __attribute((noreturn))
51 static char* program_name
;
54 static int screen
= -1;
55 static Bool verbose
= False
;
56 static Bool automatic
= False
;
57 static Bool properties
= False
;
58 static Bool grab_server
= True
;
59 static Bool no_primary
= False
;
61 static char* direction
[5] = {"normal", "left", "inverted", "right", "\n"};
63 static char* reflections
[5] = {"normal", "x", "y", "xy", "\n"};
66 static char* order
[6] = {"unknown", "horizontal rgb", "horizontal bgr",
67 "vertical rgb", "vertical bgr", "no subpixels"};
73 } mode_flags
[] = {{"+HSync", RR_HSyncPositive
}, {"-HSync", RR_HSyncNegative
},
74 {"+VSync", RR_VSyncPositive
}, {"-VSync", RR_VSyncNegative
},
75 {"Interlace", RR_Interlace
}, {"DoubleScan", RR_DoubleScan
},
76 {"CSync", RR_CSync
}, {"+CSync", RR_CSyncPositive
},
77 {"-CSync", RR_CSyncNegative
}, {NULL
, 0}};
79 static void _X_NORETURN
usage(void)
81 fprintf(stderr
, "usage: %s [options]\n", program_name
);
82 fprintf(stderr
, " where options are:\n");
83 fprintf(stderr
, " -display <display> or -d <display>\n");
84 fprintf(stderr
, " -help\n");
85 fprintf(stderr
, " -o <normal,inverted,left,right,0,1,2,3>\n");
86 fprintf(stderr
, " or --orientation <normal,inverted,left,right,0,1,2,3>\n");
87 fprintf(stderr
, " -q or --query\n");
88 fprintf(stderr
, " -s <size>/<width>x<height> or --size <size>/<width>x<height>\n");
89 fprintf(stderr
, " -r <rate> or --rate <rate> or --refresh <rate>\n");
90 fprintf(stderr
, " -v or --version\n");
91 fprintf(stderr
, " -x (reflect in x)\n");
92 fprintf(stderr
, " -y (reflect in y)\n");
93 fprintf(stderr
, " --screen <screen>\n");
94 fprintf(stderr
, " --verbose\n");
95 fprintf(stderr
, " --current\n");
96 fprintf(stderr
, " --dryrun\n");
97 fprintf(stderr
, " --nograb\n");
98 fprintf(stderr
, " --prop or --properties\n");
99 fprintf(stderr
, " --fb <width>x<height>\n");
100 fprintf(stderr
, " --fbmm <width>x<height>\n");
101 fprintf(stderr
, " --dpi <dpi>/<output>\n");
102 fprintf(stderr
, " --output <output>\n");
103 fprintf(stderr
, " --auto\n");
104 fprintf(stderr
, " --mode <mode>\n");
105 fprintf(stderr
, " --preferred\n");
106 fprintf(stderr
, " --pos <x>x<y>\n");
107 fprintf(stderr
, " --rate <rate> or --refresh <rate>\n");
108 fprintf(stderr
, " --reflect normal,x,y,xy\n");
109 fprintf(stderr
, " --rotate normal,inverted,left,right\n");
110 fprintf(stderr
, " --left-of <output>\n");
111 fprintf(stderr
, " --right-of <output>\n");
112 fprintf(stderr
, " --above <output>\n");
113 fprintf(stderr
, " --below <output>\n");
114 fprintf(stderr
, " --same-as <output>\n");
115 fprintf(stderr
, " --set <property> <value>\n");
116 fprintf(stderr
, " --scale <x>x<y>\n");
117 fprintf(stderr
, " --scale-from <w>x<h>\n");
118 fprintf(stderr
, " --transform <a>,<b>,<c>,<d>,<e>,<f>,<g>,<h>,<i>\n");
119 fprintf(stderr
, " --off\n");
120 fprintf(stderr
, " --crtc <crtc>\n");
122 " --panning <w>x<h>[+<x>+<y>[/<track:w>x<h>+<x>+<y>[/<border:l>/<t>/<r>/<b>]]]\n");
123 fprintf(stderr
, " --gamma <r>:<g>:<b>\n");
124 fprintf(stderr
, " --primary\n");
125 fprintf(stderr
, " --noprimary\n");
126 fprintf(stderr
, " --newmode <name> <clock MHz>\n");
127 fprintf(stderr
, " <hdisp> <hsync-start> <hsync-end> <htotal>\n");
128 fprintf(stderr
, " <vdisp> <vsync-start> <vsync-end> <vtotal>\n");
129 fprintf(stderr
, " [flags...]\n");
130 fprintf(stderr
, " Valid flags: +HSync -HSync +VSync -VSync\n");
131 fprintf(stderr
, " +CSync -CSync CSync Interlace DoubleScan\n");
132 fprintf(stderr
, " --rmmode <name>\n");
133 fprintf(stderr
, " --addmode <output> <name>\n");
134 fprintf(stderr
, " --delmode <output> <name>\n");
140 static void _X_NORETURN
fatal(const char* format
, ...)
144 va_start(ap
, format
);
145 fprintf(stderr
, "%s: ", program_name
);
146 vfprintf(stderr
, format
, ap
);
152 static void warning(const char* format
, ...)
156 va_start(ap
, format
);
157 fprintf(stderr
, "%s: ", program_name
);
158 vfprintf(stderr
, format
, ap
);
162 static char* rotation_name(Rotation rotation
)
166 if ((rotation
& 0xf) == 0)
168 for (i
= 0; i
< 4; i
++)
169 if (rotation
& (1 << i
))
171 return "invalid rotation";
174 static char* reflection_name(Rotation rotation
)
176 rotation
&= (RR_Reflect_X
| RR_Reflect_Y
);
185 case RR_Reflect_X
| RR_Reflect_Y
:
186 return "X and Y axis";
188 return "invalid reflection";
191 typedef enum _relation
202 int x
, y
, width
, height
;
215 typedef enum _changes
218 changes_crtc
= (1 << 0),
219 changes_mode
= (1 << 1),
220 changes_relation
= (1 << 2),
221 changes_position
= (1 << 3),
222 changes_rotation
= (1 << 4),
223 changes_reflection
= (1 << 5),
224 changes_automatic
= (1 << 6),
225 changes_refresh
= (1 << 7),
226 changes_property
= (1 << 8),
227 changes_transform
= (1 << 9),
228 changes_panning
= (1 << 10),
229 changes_gamma
= (1 << 11),
230 changes_primary
= (1 << 12),
233 typedef enum _name_kind
236 name_string
= (1 << 0),
238 name_index
= (1 << 2),
239 name_preferred
= (1 << 3),
250 typedef struct _crtc crtc_t
;
251 typedef struct _output output_t
;
252 typedef struct _transform transform_t
;
253 typedef struct _umode umode_t
;
254 typedef struct _output_prop output_prop_t
;
258 XTransform transform
;
268 XRRCrtcInfo
* crtc_info
;
270 XRRModeInfo
* mode_info
;
271 XRRPanning
* panning_info
;
277 transform_t current_transform
, pending_transform
;
282 struct _output_prop
* next
;
289 struct _output
* next
;
293 output_prop_t
* props
;
296 XRROutputInfo
* output_info
;
300 crtc_t
* current_crtc_info
;
304 XRRModeInfo
* mode_info
;
317 int scale_from_w
, scale_from_h
;
318 transform_t transform
;
334 typedef enum _umode_action
347 umode_action_t action
;
355 static char *connection[3] = {
358 "unknown connection"};
362 static char* connection
[3] = {"true", "false", "unknown"};
364 #define OUTPUT_NAME 1
368 #define CRTC_INDEX 0x40000000
377 static output_t
* outputs
= NULL
;
378 static output_t
** outputs_tail
= &outputs
;
379 static crtc_t
* crtcs
;
380 static umode_t
* umodes
;
381 static int num_crtcs
;
382 static XRRScreenResources
* res
;
383 static int fb_width
= 0, fb_height
= 0;
384 static int fb_width_mm
= 0, fb_height_mm
= 0;
385 static double dpi
= 0;
386 static char* dpi_output
= NULL
;
387 static Bool dryrun
= False
;
388 static int minWidth
, maxWidth
, minHeight
, maxHeight
;
389 static Bool has_1_2
= False
;
390 static Bool has_1_3
= False
;
392 static int mode_height(XRRModeInfo
* mode_info
, Rotation rotation
)
394 switch (rotation
& 0xf)
398 return mode_info
->height
;
401 return mode_info
->width
;
407 static int mode_width(XRRModeInfo
* mode_info
, Rotation rotation
)
409 switch (rotation
& 0xf)
413 return mode_info
->width
;
416 return mode_info
->height
;
422 static Bool
transform_point(XTransform
* transform
, double* xp
, double* yp
)
432 for (j
= 0; j
< 3; j
++)
435 for (i
= 0; i
< 3; i
++)
436 v
+= (XFixedToDouble(transform
->matrix
[j
][i
]) * vector
[i
]);
441 for (j
= 0; j
< 2; j
++)
443 vector
[j
] = result
[j
] / result
[2];
444 if (vector
[j
] > 32767 || vector
[j
] < -32767)
452 static void path_bounds(XTransform
* transform
, point_t
* points
, int npoints
, box_t
* box
)
457 for (i
= 0; i
< npoints
; i
++)
462 transform_point(transform
, &x
, &y
);
471 if (point
.x1
< box
->x1
)
473 if (point
.y1
< box
->y1
)
475 if (point
.x2
> box
->x2
)
477 if (point
.y2
> box
->y2
)
483 static void mode_geometry(XRRModeInfo
* mode_info
,
485 XTransform
* transform
,
489 int width
= mode_width(mode_info
, rotation
);
490 int height
= mode_height(mode_info
, rotation
);
500 path_bounds(transform
, rect
, 4, bounds
);
503 /* v refresh frequency in Hz */
504 static double mode_refresh(XRRModeInfo
* mode_info
)
508 if (mode_info
->hTotal
&& mode_info
->vTotal
)
509 rate
= ((double)mode_info
->dotClock
/ ((double)mode_info
->hTotal
* (double)mode_info
->vTotal
));
515 /* h sync frequency in Hz */
516 static double mode_hsync(XRRModeInfo
* mode_info
)
520 if (mode_info
->hTotal
)
521 rate
= (double)mode_info
->dotClock
/ (double)mode_info
->hTotal
;
527 static void init_name(name_t
* name
)
529 name
->kind
= name_none
;
532 static void set_name_string(name_t
* name
, char* string
)
534 name
->kind
|= name_string
;
535 name
->string
= string
;
538 static void set_name_xid(name_t
* name
, XID xid
)
540 name
->kind
|= name_xid
;
544 static void set_name_index(name_t
* name
, int index
)
546 name
->kind
|= name_index
;
550 static void set_name_preferred(name_t
* name
)
552 name
->kind
|= name_preferred
;
555 static void set_name_all(name_t
* name
, name_t
* old
)
557 if (old
->kind
& name_xid
)
558 name
->xid
= old
->xid
;
559 if (old
->kind
& name_string
)
560 name
->string
= old
->string
;
561 if (old
->kind
& name_index
)
562 name
->index
= old
->index
;
563 name
->kind
|= old
->kind
;
566 static void set_name(name_t
* name
, char* string
, name_kind_t valid
)
568 unsigned int xid
; /* don't make it XID (which is unsigned long):
569 scanf() takes unsigned int */
572 if ((valid
& name_xid
) && sscanf(string
, "0x%x", &xid
) == 1)
573 set_name_xid(name
, xid
);
574 else if ((valid
& name_index
) && sscanf(string
, "%d", &index
) == 1)
575 set_name_index(name
, index
);
576 else if (valid
& name_string
)
577 set_name_string(name
, string
);
582 static void init_transform(transform_t
* transform
)
585 memset(&transform
->transform
, '\0', sizeof(transform
->transform
));
586 for (x
= 0; x
< 3; x
++)
587 transform
->transform
.matrix
[x
][x
] = XDoubleToFixed(1.0);
588 transform
->filter
= "";
589 transform
->nparams
= 0;
590 transform
->params
= NULL
;
593 static void set_transform(
594 transform_t
* dest
, XTransform
* transform
, char* filter
, XFixed
* params
, int nparams
)
596 dest
->transform
= *transform
;
597 dest
->filter
= strdup(filter
);
598 dest
->nparams
= nparams
;
601 dest
->params
= malloc(nparams
* sizeof(XFixed
));
602 memcpy(dest
->params
, params
, nparams
* sizeof(XFixed
));
608 static void copy_transform(transform_t
* dest
, transform_t
* src
)
610 set_transform(dest
, &src
->transform
, src
->filter
, src
->params
, src
->nparams
);
613 static Bool
equal_transform(transform_t
* a
, transform_t
* b
)
615 if (memcmp(&a
->transform
, &b
->transform
, sizeof(XTransform
)) != 0)
617 if (strcmp(a
->filter
, b
->filter
) != 0)
619 if (a
->nparams
!= b
->nparams
)
621 if (memcmp(a
->params
, b
->params
, a
->nparams
* sizeof(XFixed
)) != 0)
626 static output_t
* add_output(void)
628 output_t
* output
= calloc(1, sizeof(output_t
));
631 fatal("out of memory\n");
633 output
->found
= False
;
634 output
->brightness
= 1.0;
635 *outputs_tail
= output
;
636 outputs_tail
= &output
->next
;
640 static output_t
* find_output(name_t
* name
)
644 for (output
= outputs
; output
; output
= output
->next
)
646 name_kind_t common
= name
->kind
& output
->output
.kind
;
648 if ((common
& name_xid
) && name
->xid
== output
->output
.xid
)
650 if ((common
& name_string
) && !strcmp(name
->string
, output
->output
.string
))
652 if ((common
& name_index
) && name
->index
== output
->output
.index
)
658 static output_t
* find_output_by_xid(RROutput output
)
662 init_name(&output_name
);
663 set_name_xid(&output_name
, output
);
664 return find_output(&output_name
);
667 static output_t
* find_output_by_name(char* name
)
671 init_name(&output_name
);
672 set_name_string(&output_name
, name
);
673 return find_output(&output_name
);
676 static crtc_t
* find_crtc(name_t
* name
)
681 for (c
= 0; c
< num_crtcs
; c
++)
686 common
= name
->kind
& crtc
->crtc
.kind
;
688 if ((common
& name_xid
) && name
->xid
== crtc
->crtc
.xid
)
690 if ((common
& name_string
) && !strcmp(name
->string
, crtc
->crtc
.string
))
692 if ((common
& name_index
) && name
->index
== crtc
->crtc
.index
)
699 static crtc_t
* find_crtc_by_xid(RRCrtc crtc
)
703 init_name(&crtc_name
);
704 set_name_xid(&crtc_name
, crtc
);
705 return find_crtc(&crtc_name
);
708 static XRRModeInfo
* find_mode(name_t
* name
, double refresh
)
711 XRRModeInfo
* best
= NULL
;
714 for (m
= 0; m
< res
->nmode
; m
++)
716 XRRModeInfo
* mode
= &res
->modes
[m
];
717 if ((name
->kind
& name_xid
) && name
->xid
== mode
->id
)
722 if ((name
->kind
& name_string
) && !strcmp(name
->string
, mode
->name
))
727 dist
= fabs(mode_refresh(mode
) - refresh
);
730 if (!best
|| dist
< bestDist
)
740 static XRRModeInfo
* find_mode_by_xid(RRMode mode
)
744 init_name(&mode_name
);
745 set_name_xid(&mode_name
, mode
);
746 return find_mode(&mode_name
, 0);
751 find_mode_by_name (char *name
)
754 init_name (&mode_name
);
755 set_name_string (&mode_name
, name
);
756 return find_mode (&mode_name
, 0);
760 static XRRModeInfo
* find_mode_for_output(output_t
* output
, name_t
* name
)
762 XRROutputInfo
* output_info
= output
->output_info
;
764 XRRModeInfo
* best
= NULL
;
767 for (m
= 0; m
< output_info
->nmode
; m
++)
771 mode
= find_mode_by_xid(output_info
->modes
[m
]);
774 if ((name
->kind
& name_xid
) && name
->xid
== mode
->id
)
779 if ((name
->kind
& name_string
) && !strcmp(name
->string
, mode
->name
))
783 /* Stay away from doublescan modes unless refresh rate is specified. */
784 if (!output
->refresh
&& (mode
->modeFlags
& RR_DoubleScan
))
788 dist
= fabs(mode_refresh(mode
) - output
->refresh
);
791 if (!best
|| dist
< bestDist
)
801 static XRRModeInfo
* preferred_mode(output_t
* output
)
803 XRROutputInfo
* output_info
= output
->output_info
;
810 for (m
= 0; m
< output_info
->nmode
; m
++)
812 XRRModeInfo
* mode_info
= find_mode_by_xid(output_info
->modes
[m
]);
815 if (m
< output_info
->npreferred
)
817 else if (output_info
->mm_height
)
818 dist
= (1000 * DisplayHeight(dpy
, screen
) / DisplayHeightMM(dpy
, screen
) -
819 1000 * mode_info
->height
/ output_info
->mm_height
);
821 dist
= DisplayHeight(dpy
, screen
) - mode_info
->height
;
825 if (!best
|| dist
< bestDist
)
834 static Bool
output_can_use_crtc(output_t
* output
, crtc_t
* crtc
)
836 XRROutputInfo
* output_info
= output
->output_info
;
839 for (c
= 0; c
< output_info
->ncrtc
; c
++)
840 if (output_info
->crtcs
[c
] == crtc
->crtc
.xid
)
845 static Bool
output_can_use_mode(output_t
* output
, XRRModeInfo
* mode
)
847 XRROutputInfo
* output_info
= output
->output_info
;
850 for (m
= 0; m
< output_info
->nmode
; m
++)
851 if (output_info
->modes
[m
] == mode
->id
)
856 static Bool
crtc_can_use_rotation(crtc_t
* crtc
, Rotation rotation
)
858 Rotation rotations
= crtc
->crtc_info
->rotations
;
859 Rotation dir
= rotation
& (RR_Rotate_0
| RR_Rotate_90
| RR_Rotate_180
| RR_Rotate_270
);
860 Rotation reflect
= rotation
& (RR_Reflect_X
| RR_Reflect_Y
);
861 if (((rotations
& dir
) != 0) && ((rotations
& reflect
) == reflect
))
868 crtc_can_use_transform (crtc_t
*crtc
, XTransform
*transform
)
872 XRRQueryVersion (dpy
, &major
, &minor
);
873 if (major
> 1 || (major
== 1 && minor
>= 3))
879 * Report only rotations that are supported by all crtcs
882 output_rotations (output_t
*output
)
885 Rotation rotation
= RR_Rotate_0
;
886 XRROutputInfo
*output_info
= output
->output_info
;
889 for (c
= 0; c
< output_info
->ncrtc
; c
++)
891 crtc_t
*crtc
= find_crtc_by_xid (output_info
->crtcs
[c
]);
895 rotation
= crtc
->crtc_info
->rotations
;
898 rotation
&= crtc
->crtc_info
->rotations
;
905 static Bool
output_can_use_rotation(output_t
* output
, Rotation rotation
)
907 XRROutputInfo
* output_info
= output
->output_info
;
910 /* make sure all of the crtcs can use this rotation.
911 * yes, this is not strictly necessary, but it is
912 * simpler,and we expect most drivers to either
913 * support rotation everywhere or nowhere
915 for (c
= 0; c
< output_info
->ncrtc
; c
++)
917 crtc_t
* crtc
= find_crtc_by_xid(output_info
->crtcs
[c
]);
918 if (crtc
&& !crtc_can_use_rotation(crtc
, rotation
))
924 static Bool
output_is_primary(output_t
* output
)
927 return XRRGetOutputPrimary(dpy
, root
) == output
->output
.xid
;
931 /* Returns the index of the last value in an array < 0xffff */
932 static int find_last_non_clamped(CARD16 array
[], int size
)
935 for (i
= size
- 1; i
> 0; i
--)
937 if (array
[i
] < 0xffff)
943 static void set_gamma_info(output_t
* output
)
946 double i1
, v1
, i2
, v2
;
947 int size
, middle
, last_best
, last_red
, last_green
, last_blue
;
950 if (!output
->crtc_info
)
953 size
= XRRGetCrtcGammaSize(dpy
, output
->crtc_info
->crtc
.xid
);
956 warning("Failed to get size of gamma for output %s\n", output
->output
.string
);
960 gamma
= XRRGetCrtcGamma(dpy
, output
->crtc_info
->crtc
.xid
);
963 warning("Failed to get gamma for output %s\n", output
->output
.string
);
968 * Here is a bit tricky because gamma is a whole curve for each
969 * color. So, typically, we need to represent 3 * 256 values as 3 + 1
970 * values. Therefore, we approximate the gamma curve (v) by supposing
971 * it always follows the way we set it: a power function (i^g)
972 * multiplied by a brightness (b).
974 * so g = (ln(v) - ln(b))/ln(i)
975 * and b can be found using two points (v1,i1) and (v2, i2):
976 * b = e^((ln(v2)*ln(i1) - ln(v1)*ln(i2))/ln(i1/i2))
977 * For the best resolution, we select i2 at the highest place not
978 * clamped and i1 at i2/2. Note that if i2 = 1 (as in most normal
979 * cases), then b = v2.
981 last_red
= find_last_non_clamped(gamma
->red
, size
);
982 last_green
= find_last_non_clamped(gamma
->green
, size
);
983 last_blue
= find_last_non_clamped(gamma
->blue
, size
);
984 best_array
= gamma
->red
;
985 last_best
= last_red
;
986 if (last_green
> last_best
)
988 last_best
= last_green
;
989 best_array
= gamma
->green
;
991 if (last_blue
> last_best
)
993 last_best
= last_blue
;
994 best_array
= gamma
->blue
;
999 middle
= last_best
/ 2;
1000 i1
= (double)(middle
+ 1) / size
;
1001 v1
= (double)(best_array
[middle
]) / 65535;
1002 i2
= (double)(last_best
+ 1) / size
;
1003 v2
= (double)(best_array
[last_best
]) / 65535;
1005 { /* The screen is black */
1006 output
->brightness
= 0;
1007 output
->gamma
.red
= 1;
1008 output
->gamma
.green
= 1;
1009 output
->gamma
.blue
= 1;
1013 if ((last_best
+ 1) == size
)
1014 output
->brightness
= v2
;
1016 output
->brightness
= exp((log(v2
) * log(i1
) - log(v1
) * log(i2
)) / log(i1
/ i2
));
1017 output
->gamma
.red
= logf((gamma
->red
[last_red
/ 2]) / output
->brightness
/ 65535) /
1018 logf(((last_red
/ 2) + 1) / size
);
1019 output
->gamma
.green
= logf((gamma
->green
[last_green
/ 2]) / output
->brightness
/ 65535) /
1020 logf(((last_green
/ 2) + 1) / size
);
1021 output
->gamma
.blue
= logf((gamma
->blue
[last_blue
/ 2]) / output
->brightness
/ 65535) /
1022 logf(((last_blue
/ 2) + 1) / size
);
1025 XRRFreeGamma(gamma
);
1028 static void set_output_info(output_t
* output
, RROutput xid
, XRROutputInfo
* output_info
)
1030 /* sanity check output info */
1031 if (output_info
->connection
!= RR_Disconnected
&& !output_info
->nmode
)
1032 warning("Output %s is not disconnected but has no modes\n", output_info
->name
);
1034 /* set output name and info */
1035 if (!(output
->output
.kind
& name_xid
))
1036 set_name_xid(&output
->output
, xid
);
1037 if (!(output
->output
.kind
& name_string
))
1038 set_name_string(&output
->output
, output_info
->name
);
1039 output
->output_info
= output_info
;
1041 /* set crtc name and info */
1042 if (!(output
->changes
& changes_crtc
))
1043 set_name_xid(&output
->crtc
, output_info
->crtc
);
1045 if (output
->crtc
.kind
== name_xid
&& output
->crtc
.xid
== None
)
1046 output
->crtc_info
= NULL
;
1049 output
->crtc_info
= find_crtc(&output
->crtc
);
1050 if (!output
->crtc_info
)
1052 if (output
->crtc
.kind
& name_xid
)
1053 fatal("cannot find crtc 0x%x\n", output
->crtc
.xid
);
1054 if (output
->crtc
.kind
& name_index
)
1055 fatal("cannot find crtc %d\n", output
->crtc
.index
);
1057 if (!output_can_use_crtc(output
, output
->crtc_info
))
1058 fatal("output %s cannot use crtc 0x%x\n", output
->output
.string
, output
->crtc_info
->crtc
.xid
);
1061 /* set mode name and info */
1062 if (!(output
->changes
& changes_mode
))
1064 crtc_t
* crtc
= NULL
;
1066 if (output_info
->crtc
)
1067 crtc
= find_crtc_by_xid(output_info
->crtc
);
1068 if (crtc
&& crtc
->crtc_info
)
1069 set_name_xid(&output
->mode
, crtc
->crtc_info
->mode
);
1070 else if (output
->crtc_info
)
1071 set_name_xid(&output
->mode
, output
->crtc_info
->crtc_info
->mode
);
1073 set_name_xid(&output
->mode
, None
);
1074 if (output
->mode
.xid
)
1076 output
->mode_info
= find_mode_by_xid(output
->mode
.xid
);
1077 if (!output
->mode_info
)
1078 fatal("server did not report mode 0x%x for output %s\n", output
->mode
.xid
,
1079 output
->output
.string
);
1082 output
->mode_info
= NULL
;
1084 else if (output
->mode
.kind
== name_xid
&& output
->mode
.xid
== None
)
1085 output
->mode_info
= NULL
;
1088 if (output
->mode
.kind
== name_preferred
)
1089 output
->mode_info
= preferred_mode(output
);
1091 output
->mode_info
= find_mode_for_output(output
, &output
->mode
);
1092 if (!output
->mode_info
)
1094 if (output
->mode
.kind
& name_preferred
)
1095 fatal("cannot find preferred mode\n");
1096 if (output
->mode
.kind
& name_string
)
1097 fatal("cannot find mode %s\n", output
->mode
.string
);
1098 if (output
->mode
.kind
& name_xid
)
1099 fatal("cannot find mode 0x%x\n", output
->mode
.xid
);
1101 if (!output_can_use_mode(output
, output
->mode_info
))
1102 fatal("output %s cannot use mode %s\n", output
->output
.string
, output
->mode_info
->name
);
1106 if (!(output
->changes
& changes_position
))
1108 if (output
->crtc_info
)
1110 output
->x
= output
->crtc_info
->crtc_info
->x
;
1111 output
->y
= output
->crtc_info
->crtc_info
->y
;
1121 if (!(output
->changes
& changes_rotation
))
1123 output
->rotation
&= ~0xf;
1124 if (output
->crtc_info
)
1125 output
->rotation
|= (output
->crtc_info
->crtc_info
->rotation
& 0xf);
1127 output
->rotation
= RR_Rotate_0
;
1129 if (!(output
->changes
& changes_reflection
))
1131 output
->rotation
&= ~(RR_Reflect_X
| RR_Reflect_Y
);
1132 if (output
->crtc_info
)
1133 output
->rotation
|= (output
->crtc_info
->crtc_info
->rotation
& (RR_Reflect_X
| RR_Reflect_Y
));
1135 if (!output_can_use_rotation(output
, output
->rotation
))
1136 fatal("output %s cannot use rotation \"%s\" reflection \"%s\"\n", output
->output
.string
,
1137 rotation_name(output
->rotation
), reflection_name(output
->rotation
));
1140 if (!(output
->changes
& changes_gamma
))
1141 set_gamma_info(output
);
1143 /* set transformation */
1144 if (!(output
->changes
& changes_transform
))
1146 if (output
->crtc_info
)
1147 copy_transform(&output
->transform
, &output
->crtc_info
->current_transform
);
1149 init_transform(&output
->transform
);
1153 /* transform was already set for --scale or --transform */
1155 /* for --scale-from, figure out the mode size and compute the transform
1156 * for the target framebuffer area */
1157 if (output
->scale_from_w
> 0 && output
->mode_info
)
1159 double sx
= (double)output
->scale_from_w
/ output
->mode_info
->width
;
1160 double sy
= (double)output
->scale_from_h
/ output
->mode_info
->height
;
1162 printf("scaling %s by %lfx%lf\n", output
->output
.string
, sx
, sy
);
1163 init_transform(&output
->transform
);
1164 output
->transform
.transform
.matrix
[0][0] = XDoubleToFixed(sx
);
1165 output
->transform
.transform
.matrix
[1][1] = XDoubleToFixed(sy
);
1166 output
->transform
.transform
.matrix
[2][2] = XDoubleToFixed(1.0);
1167 if (sx
!= 1 || sy
!= 1)
1168 output
->transform
.filter
= "bilinear";
1170 output
->transform
.filter
= "nearest";
1171 output
->transform
.nparams
= 0;
1172 output
->transform
.params
= NULL
;
1177 if (!(output
->changes
& changes_primary
))
1178 output
->primary
= output_is_primary(output
);
1181 static void get_screen(Bool current
)
1184 fatal("Server RandR version before 1.2\n");
1186 XRRGetScreenSizeRange(dpy
, root
, &minWidth
, &minHeight
, &maxWidth
, &maxHeight
);
1189 res
= XRRGetScreenResourcesCurrent(dpy
, root
);
1191 res
= XRRGetScreenResources(dpy
, root
);
1193 fatal("could not get screen resources");
1196 static void get_crtcs(void)
1200 num_crtcs
= res
->ncrtc
;
1201 crtcs
= calloc(num_crtcs
, sizeof(crtc_t
));
1203 fatal("out of memory\n");
1205 for (c
= 0; c
< res
->ncrtc
; c
++)
1207 XRRCrtcInfo
* crtc_info
= XRRGetCrtcInfo(dpy
, res
, res
->crtcs
[c
]);
1208 XRRCrtcTransformAttributes
* attr
;
1209 XRRPanning
* panning_info
= NULL
;
1214 memset(&zero
, 0, sizeof(zero
));
1215 panning_info
= XRRGetPanning(dpy
, res
, res
->crtcs
[c
]);
1216 zero
.timestamp
= panning_info
->timestamp
;
1217 if (!memcmp(panning_info
, &zero
, sizeof(zero
)))
1219 Xfree(panning_info
);
1220 panning_info
= NULL
;
1224 set_name_xid(&crtcs
[c
].crtc
, res
->crtcs
[c
]);
1225 set_name_index(&crtcs
[c
].crtc
, c
);
1227 fatal("could not get crtc 0x%x information\n", res
->crtcs
[c
]);
1228 crtcs
[c
].crtc_info
= crtc_info
;
1229 crtcs
[c
].panning_info
= panning_info
;
1230 if (crtc_info
->mode
== None
)
1232 crtcs
[c
].mode_info
= NULL
;
1235 crtcs
[c
].rotation
= RR_Rotate_0
;
1237 if (XRRGetCrtcTransform(dpy
, res
->crtcs
[c
], &attr
) && attr
)
1239 set_transform(&crtcs
[c
].current_transform
, &attr
->currentTransform
, attr
->currentFilter
,
1240 attr
->currentParams
, attr
->currentNparams
);
1245 init_transform(&crtcs
[c
].current_transform
);
1247 copy_transform(&crtcs
[c
].pending_transform
, &crtcs
[c
].current_transform
);
1251 static void crtc_add_output(crtc_t
* crtc
, output_t
* output
)
1254 crtc
->outputs
= realloc(crtc
->outputs
, (crtc
->noutput
+ 1) * sizeof(output_t
*));
1257 crtc
->outputs
= malloc(sizeof(output_t
*));
1258 crtc
->x
= output
->x
;
1259 crtc
->y
= output
->y
;
1260 crtc
->rotation
= output
->rotation
;
1261 crtc
->mode_info
= output
->mode_info
;
1262 copy_transform(&crtc
->pending_transform
, &output
->transform
);
1265 fatal("out of memory\n");
1266 crtc
->outputs
[crtc
->noutput
++] = output
;
1269 static void set_crtcs(void)
1273 for (output
= outputs
; output
; output
= output
->next
)
1275 if (!output
->mode_info
)
1277 crtc_add_output(output
->crtc_info
, output
);
1281 static void set_panning(void)
1285 for (output
= outputs
; output
; output
= output
->next
)
1287 if (!output
->crtc_info
)
1289 if (!(output
->changes
& changes_panning
))
1291 if (!output
->crtc_info
->panning_info
)
1292 output
->crtc_info
->panning_info
= malloc(sizeof(XRRPanning
));
1293 memcpy(output
->crtc_info
->panning_info
, &output
->panning
, sizeof(XRRPanning
));
1294 output
->crtc_info
->changing
= 1;
1298 static void set_gamma(void)
1302 for (output
= outputs
; output
; output
= output
->next
)
1306 XRRCrtcGamma
* gamma
;
1308 if (!(output
->changes
& changes_gamma
))
1311 if (!output
->crtc_info
)
1313 fatal("Need crtc to set gamma on.\n");
1317 crtc
= output
->crtc_info
;
1319 size
= XRRGetCrtcGammaSize(dpy
, crtc
->crtc
.xid
);
1323 fatal("Gamma size is 0.\n");
1327 gamma
= XRRAllocGamma(size
);
1330 fatal("Gamma allocation failed.\n");
1334 if (output
->gamma
.red
== 0.0f
&& output
->gamma
.green
== 0.0f
&& output
->gamma
.blue
== 0.0f
)
1335 output
->gamma
.red
= output
->gamma
.green
= output
->gamma
.blue
= 1.0f
;
1337 for (i
= 0; i
< size
; i
++)
1339 if (output
->gamma
.red
== 1.0f
&& output
->brightness
== 1.0f
)
1340 gamma
->red
[i
] = (i
<< 8) + i
;
1343 min(powf((float)i
/ (float)(size
- 1), output
->gamma
.red
) * output
->brightness
, 1.0f
) *
1346 if (output
->gamma
.green
== 1.0f
&& output
->brightness
== 1.0f
)
1347 gamma
->green
[i
] = (i
<< 8) + i
;
1350 min(powf((float)i
/ (float)(size
- 1), output
->gamma
.green
) * output
->brightness
,
1354 if (output
->gamma
.blue
== 1.0f
&& output
->brightness
== 1.0f
)
1355 gamma
->blue
[i
] = (i
<< 8) + i
;
1358 min(powf((float)i
/ (float)(size
- 1), output
->gamma
.blue
) * output
->brightness
, 1.0f
) *
1362 XRRSetCrtcGamma(dpy
, crtc
->crtc
.xid
, gamma
);
1368 static void set_primary(void)
1374 XRRSetOutputPrimary(dpy
, root
, None
);
1378 for (output
= outputs
; output
; output
= output
->next
)
1380 if (!(output
->changes
& changes_primary
))
1382 if (output
->primary
)
1383 XRRSetOutputPrimary(dpy
, root
, output
->output
.xid
);
1388 static Status
crtc_disable(crtc_t
* crtc
)
1391 printf("crtc %d: disable\n", crtc
->crtc
.index
);
1394 return RRSetConfigSuccess
;
1395 return XRRSetCrtcConfig(dpy
, res
, crtc
->crtc
.xid
, CurrentTime
, 0, 0, None
, RR_Rotate_0
, NULL
, 0);
1398 static void crtc_set_transform(crtc_t
* crtc
, transform_t
* transform
)
1402 XRRQueryVersion(dpy
, &major
, &minor
);
1403 if (major
> 1 || (major
== 1 && minor
>= 3))
1404 XRRSetCrtcTransform(dpy
, crtc
->crtc
.xid
, &transform
->transform
, transform
->filter
,
1405 transform
->params
, transform
->nparams
);
1408 static Status
crtc_revert(crtc_t
* crtc
)
1410 XRRCrtcInfo
* crtc_info
= crtc
->crtc_info
;
1413 printf("crtc %d: revert\n", crtc
->crtc
.index
);
1416 return RRSetConfigSuccess
;
1418 if (!equal_transform(&crtc
->current_transform
, &crtc
->pending_transform
))
1419 crtc_set_transform(crtc
, &crtc
->current_transform
);
1420 return XRRSetCrtcConfig(dpy
, res
, crtc
->crtc
.xid
, CurrentTime
, crtc_info
->x
, crtc_info
->y
,
1421 crtc_info
->mode
, crtc_info
->rotation
, crtc_info
->outputs
,
1422 crtc_info
->noutput
);
1425 static Status
crtc_apply(crtc_t
* crtc
)
1427 RROutput
* rr_outputs
;
1432 if (!crtc
->changing
|| !crtc
->mode_info
)
1433 return RRSetConfigSuccess
;
1435 rr_outputs
= calloc(crtc
->noutput
, sizeof(RROutput
));
1438 for (o
= 0; o
< crtc
->noutput
; o
++)
1439 rr_outputs
[o
] = crtc
->outputs
[o
]->output
.xid
;
1440 mode
= crtc
->mode_info
->id
;
1443 printf("crtc %d: %12s %6.1f +%d+%d", crtc
->crtc
.index
, crtc
->mode_info
->name
,
1444 mode_refresh(crtc
->mode_info
), crtc
->x
, crtc
->y
);
1445 for (o
= 0; o
< crtc
->noutput
; o
++)
1446 printf(" \"%s\"", crtc
->outputs
[o
]->output
.string
);
1451 s
= RRSetConfigSuccess
;
1454 if (!equal_transform(&crtc
->current_transform
, &crtc
->pending_transform
))
1455 crtc_set_transform(crtc
, &crtc
->pending_transform
);
1456 s
= XRRSetCrtcConfig(dpy
, res
, crtc
->crtc
.xid
, CurrentTime
, crtc
->x
, crtc
->y
, mode
,
1457 crtc
->rotation
, rr_outputs
, crtc
->noutput
);
1458 if (s
== RRSetConfigSuccess
&& crtc
->panning_info
)
1461 s
= XRRSetPanning(dpy
, res
, crtc
->crtc
.xid
, crtc
->panning_info
);
1463 fatal("panning needs RandR 1.3\n");
1470 static void screen_revert(void)
1473 printf("screen %d: revert\n", screen
);
1477 XRRSetScreenSize(dpy
, root
, DisplayWidth(dpy
, screen
), DisplayHeight(dpy
, screen
),
1478 DisplayWidthMM(dpy
, screen
), DisplayHeightMM(dpy
, screen
));
1481 static void screen_apply(void)
1483 if (fb_width
== DisplayWidth(dpy
, screen
) && fb_height
== DisplayHeight(dpy
, screen
) &&
1484 fb_width_mm
== DisplayWidthMM(dpy
, screen
) && fb_height_mm
== DisplayHeightMM(dpy
, screen
))
1489 printf("screen %d: %dx%d %dx%d mm %6.2fdpi\n", screen
, fb_width
, fb_height
, fb_width_mm
,
1493 XRRSetScreenSize(dpy
, root
, fb_width
, fb_height
, fb_width_mm
, fb_height_mm
);
1496 static void revert(void)
1500 /* first disable all crtcs */
1501 for (c
= 0; c
< res
->ncrtc
; c
++)
1502 crtc_disable(&crtcs
[c
]);
1503 /* next reset screen size */
1505 /* now restore all crtcs */
1506 for (c
= 0; c
< res
->ncrtc
; c
++)
1507 crtc_revert(&crtcs
[c
]);
1511 * uh-oh, something bad happened in the middle of changing
1512 * the configuration. Revert to the previous configuration
1515 static void _X_NORETURN
panic(Status s
, crtc_t
* crtc
)
1517 int c
= crtc
->crtc
.index
;
1522 case RRSetConfigSuccess
:
1523 message
= "succeeded";
1526 message
= "out of memory";
1528 case RRSetConfigFailed
:
1531 case RRSetConfigInvalidConfigTime
:
1532 message
= "invalid config time";
1534 case RRSetConfigInvalidTime
:
1535 message
= "invalid time";
1538 message
= "unknown failure";
1542 fprintf(stderr
, "%s: Configure crtc %d %s\n", program_name
, c
, message
);
1547 static void apply(void)
1553 * Hold the server grabbed while messing with
1554 * the screen so that apps which notice the resize
1555 * event and ask for xinerama information from the server
1556 * receive up-to-date information
1562 * Turn off any crtcs which are to be disabled or which are
1563 * larger than the target size
1565 for (c
= 0; c
< res
->ncrtc
; c
++)
1567 crtc_t
* crtc
= &crtcs
[c
];
1568 XRRCrtcInfo
* crtc_info
= crtc
->crtc_info
;
1570 /* if this crtc is already disabled, skip it */
1571 if (crtc_info
->mode
== None
)
1575 * If this crtc is to be left enabled, make
1576 * sure the old size fits then new screen
1578 if (crtc
->mode_info
)
1580 XRRModeInfo
* old_mode
= find_mode_by_xid(crtc_info
->mode
);
1585 panic(RRSetConfigFailed
, crtc
);
1587 /* old position and size information */
1588 mode_geometry(old_mode
, crtc_info
->rotation
, &crtc
->current_transform
.transform
, &bounds
);
1590 x
= crtc_info
->x
+ bounds
.x1
;
1591 y
= crtc_info
->y
+ bounds
.y1
;
1592 w
= bounds
.x2
- bounds
.x1
;
1593 h
= bounds
.y2
- bounds
.y1
;
1595 /* if it fits, skip it */
1596 if (x
+ w
<= fb_width
&& y
+ h
<= fb_height
)
1598 crtc
->changing
= True
;
1600 s
= crtc_disable(crtc
);
1601 if (s
!= RRSetConfigSuccess
)
1606 * Set the screen size
1614 for (c
= 0; c
< res
->ncrtc
; c
++)
1616 crtc_t
* crtc
= &crtcs
[c
];
1618 s
= crtc_apply(crtc
);
1619 if (s
!= RRSetConfigSuccess
)
1626 * Release the server grab and let all clients
1627 * respond to the updated state
1634 * Use current output state to complete the output list
1636 static void get_outputs(void)
1641 for (o
= 0; o
< res
->noutput
; o
++)
1643 XRROutputInfo
* output_info
= XRRGetOutputInfo(dpy
, res
, res
->outputs
[o
]);
1647 fatal("could not get output 0x%x information\n", res
->outputs
[o
]);
1648 init_name(&output_name
);
1649 set_name_xid(&output_name
, res
->outputs
[o
]);
1650 set_name_index(&output_name
, o
);
1651 set_name_string(&output_name
, output_info
->name
);
1652 output
= find_output(&output_name
);
1655 output
= add_output();
1656 set_name_all(&output
->output
, &output_name
);
1658 * When global --automatic mode is set, turn on connected but off
1659 * outputs, turn off disconnected but on outputs
1663 switch (output_info
->connection
)
1666 if (!output_info
->crtc
)
1668 output
->changes
|= changes_automatic
;
1669 output
->automatic
= True
;
1672 case RR_Disconnected
:
1673 if (output_info
->crtc
)
1675 output
->changes
|= changes_automatic
;
1676 output
->automatic
= True
;
1682 output
->found
= True
;
1685 * Automatic mode -- track connection state and enable/disable outputs
1688 if (output
->automatic
)
1690 switch (output_info
->connection
)
1693 case RR_UnknownConnection
:
1694 if ((!(output
->changes
& changes_mode
)))
1696 set_name_preferred(&output
->mode
);
1697 output
->changes
|= changes_mode
;
1700 case RR_Disconnected
:
1701 if ((!(output
->changes
& changes_mode
)))
1703 set_name_xid(&output
->mode
, None
);
1704 set_name_xid(&output
->crtc
, None
);
1705 output
->changes
|= changes_mode
;
1706 output
->changes
|= changes_crtc
;
1712 set_output_info(output
, res
->outputs
[o
], output_info
);
1714 for (q
= outputs
; q
; q
= q
->next
)
1718 fprintf(stderr
, "warning: output %s not found; ignoring\n", q
->output
.string
);
1723 static void mark_changing_crtcs(void)
1727 for (c
= 0; c
< num_crtcs
; c
++)
1729 crtc_t
* crtc
= &crtcs
[c
];
1733 /* walk old output list (to catch disables) */
1734 for (o
= 0; o
< crtc
->crtc_info
->noutput
; o
++)
1736 output
= find_output_by_xid(crtc
->crtc_info
->outputs
[o
]);
1738 fatal("cannot find output 0x%x\n", crtc
->crtc_info
->outputs
[o
]);
1739 if (output
->changes
)
1740 crtc
->changing
= True
;
1742 /* walk new output list */
1743 for (o
= 0; o
< crtc
->noutput
; o
++)
1745 output
= crtc
->outputs
[o
];
1746 if (output
->changes
)
1747 crtc
->changing
= True
;
1753 * Test whether 'crtc' can be used for 'output'
1755 static Bool
check_crtc_for_output(crtc_t
* crtc
, output_t
* output
)
1761 for (c
= 0; c
< output
->output_info
->ncrtc
; c
++)
1762 if (output
->output_info
->crtcs
[c
] == crtc
->crtc
.xid
)
1764 if (c
== output
->output_info
->ncrtc
)
1766 for (other
= outputs
; other
; other
= other
->next
)
1768 if (other
== output
)
1771 if (other
->mode_info
== NULL
)
1774 if (other
->crtc_info
!= crtc
)
1777 /* see if the output connected to the crtc can clone to this output */
1778 for (l
= 0; l
< output
->output_info
->nclone
; l
++)
1779 if (output
->output_info
->clones
[l
] == other
->output
.xid
)
1781 /* not on the list, can't clone */
1782 if (l
== output
->output_info
->nclone
)
1788 /* make sure the state matches */
1789 if (crtc
->mode_info
!= output
->mode_info
)
1791 if (crtc
->x
!= output
->x
)
1793 if (crtc
->y
!= output
->y
)
1795 if (crtc
->rotation
!= output
->rotation
)
1797 if (!equal_transform(&crtc
->current_transform
, &output
->transform
))
1800 else if (crtc
->crtc_info
->noutput
)
1802 /* make sure the state matches the already used state */
1803 XRRModeInfo
* mode
= find_mode_by_xid(crtc
->crtc_info
->mode
);
1805 if (mode
!= output
->mode_info
)
1807 if (crtc
->crtc_info
->x
!= output
->x
)
1809 if (crtc
->crtc_info
->y
!= output
->y
)
1811 if (crtc
->crtc_info
->rotation
!= output
->rotation
)
1817 static crtc_t
* find_crtc_for_output(output_t
* output
)
1821 for (c
= 0; c
< output
->output_info
->ncrtc
; c
++)
1825 crtc
= find_crtc_by_xid(output
->output_info
->crtcs
[c
]);
1827 fatal("cannot find crtc 0x%x\n", output
->output_info
->crtcs
[c
]);
1829 if (check_crtc_for_output(crtc
, output
))
1835 static void set_positions(void)
1846 for (output
= outputs
; output
; output
= output
->next
)
1849 name_t relation_name
;
1851 if (!(output
->changes
& changes_relation
))
1854 if (output
->mode_info
== NULL
)
1857 init_name(&relation_name
);
1858 set_name_string(&relation_name
, output
->relative_to
);
1859 relation
= find_output(&relation_name
);
1861 fatal("cannot find output \"%s\"\n", output
->relative_to
);
1863 if (relation
->mode_info
== NULL
)
1867 output
->changes
|= changes_position
;
1872 * Make sure the dependent object has been set in place
1874 if ((relation
->changes
& changes_relation
) && !(relation
->changes
& changes_position
))
1880 switch (output
->relation
)
1882 case relation_left_of
:
1883 output
->y
= relation
->y
;
1884 output
->x
= relation
->x
- mode_width(output
->mode_info
, output
->rotation
);
1886 case relation_right_of
:
1887 output
->y
= relation
->y
;
1888 output
->x
= relation
->x
+ mode_width(relation
->mode_info
, relation
->rotation
);
1890 case relation_above
:
1891 output
->x
= relation
->x
;
1892 output
->y
= relation
->y
- mode_height(output
->mode_info
, output
->rotation
);
1894 case relation_below
:
1895 output
->x
= relation
->x
;
1896 output
->y
= relation
->y
+ mode_height(relation
->mode_info
, relation
->rotation
);
1898 case relation_same_as
:
1899 output
->x
= relation
->x
;
1900 output
->y
= relation
->y
;
1902 output
->changes
|= changes_position
;
1908 fatal("loop in relative position specifications\n");
1912 * Now normalize positions so the upper left corner of all outputs is at 0,0
1916 for (output
= outputs
; output
; output
= output
->next
)
1918 if (output
->mode_info
== NULL
)
1921 if (output
->x
< min_x
)
1923 if (output
->y
< min_y
)
1928 /* move all outputs */
1929 for (output
= outputs
; output
; output
= output
->next
)
1931 if (output
->mode_info
== NULL
)
1936 output
->changes
|= changes_position
;
1941 static void set_screen_size(void)
1944 Bool fb_specified
= fb_width
!= 0 && fb_height
!= 0;
1946 for (output
= outputs
; output
; output
= output
->next
)
1948 XRRModeInfo
* mode_info
= output
->mode_info
;
1955 mode_geometry(mode_info
, output
->rotation
, &output
->transform
.transform
, &bounds
);
1956 x
= output
->x
+ bounds
.x1
;
1957 y
= output
->y
+ bounds
.y1
;
1958 w
= bounds
.x2
- bounds
.x1
;
1959 h
= bounds
.y2
- bounds
.y1
;
1960 /* make sure output fits in specified size */
1963 if (x
+ w
> fb_width
|| y
+ h
> fb_height
)
1964 warning("specified screen %dx%d not large enough for output %s (%dx%d+%d+%d)\n", fb_width
,
1965 fb_height
, output
->output
.string
, w
, h
, x
, y
);
1967 /* fit fb to output */
1971 if (x
+ w
> fb_width
)
1973 if (y
+ h
> fb_height
)
1975 if (output
->changes
& changes_panning
)
1976 pan
= &output
->panning
;
1978 pan
= output
->crtc_info
? output
->crtc_info
->panning_info
: NULL
;
1979 if (pan
&& pan
->left
+ pan
->width
> (unsigned int)fb_width
)
1980 fb_width
= pan
->left
+ pan
->width
;
1981 if (pan
&& pan
->top
+ pan
->height
> (unsigned int)fb_height
)
1982 fb_height
= pan
->top
+ pan
->height
;
1986 if (fb_width
> maxWidth
|| fb_height
> maxHeight
)
1987 fatal("screen cannot be larger than %dx%d (desired size %dx%d)\n", maxWidth
, maxHeight
,
1988 fb_width
, fb_height
);
1991 if (fb_width
< minWidth
|| fb_height
< minHeight
)
1992 fatal("screen must be at least %dx%d\n", minWidth
, minHeight
);
1996 if (fb_width
< minWidth
)
1997 fb_width
= minWidth
;
1998 if (fb_height
< minHeight
)
1999 fb_height
= minHeight
;
2004 static void disable_outputs(output_t
* outputs
)
2008 outputs
->crtc_info
= NULL
;
2009 outputs
= outputs
->next
;
2014 * find the best mapping from output to crtc available
2016 static int pick_crtcs_score(output_t
* outputs
)
2029 outputs
= outputs
->next
;
2031 * Score with this output disabled
2033 output
->crtc_info
= NULL
;
2034 best_score
= pick_crtcs_score(outputs
);
2035 if (output
->mode_info
== NULL
)
2040 * Now score with this output any valid crtc
2042 for (c
= 0; c
< output
->output_info
->ncrtc
; c
++)
2046 crtc
= find_crtc_by_xid(output
->output_info
->crtcs
[c
]);
2048 fatal("cannot find crtc 0x%x\n", output
->output_info
->crtcs
[c
]);
2050 /* reset crtc allocation for following outputs */
2051 disable_outputs(outputs
);
2052 if (!check_crtc_for_output(crtc
, output
))
2056 /* slight preference for existing connections */
2057 if (crtc
== output
->current_crtc_info
)
2060 output
->crtc_info
= crtc
;
2061 score
= my_score
+ pick_crtcs_score(outputs
);
2062 if (score
> best_score
)
2068 if (output
->crtc_info
!= best_crtc
)
2069 output
->crtc_info
= best_crtc
;
2071 * Reset other outputs based on this one using the best crtc
2073 (void)pick_crtcs_score(outputs
);
2079 * Pick crtcs for any changing outputs that don't have one
2081 static void pick_crtcs(void)
2086 * First try to match up newly enabled outputs with spare crtcs
2088 for (output
= outputs
; output
; output
= output
->next
)
2090 if (output
->changes
&& output
->mode_info
)
2092 if (output
->crtc_info
)
2094 if (output
->crtc_info
->crtc_info
->noutput
> 0 &&
2095 (output
->crtc_info
->crtc_info
->noutput
> 1 ||
2096 output
!= find_output_by_xid(output
->crtc_info
->crtc_info
->outputs
[0])))
2101 output
->crtc_info
= find_crtc_for_output(output
);
2102 if (!output
->crtc_info
)
2113 * When the simple way fails, see if there is a way
2114 * to swap crtcs around and make things work
2116 for (output
= outputs
; output
; output
= output
->next
)
2117 output
->current_crtc_info
= output
->crtc_info
;
2118 pick_crtcs_score(outputs
);
2119 for (output
= outputs
; output
; output
= output
->next
)
2121 if (output
->mode_info
&& !output
->crtc_info
)
2122 fatal("cannot find crtc for output %s\n", output
->output
.string
);
2123 if (!output
->changes
&& output
->crtc_info
!= output
->current_crtc_info
)
2124 output
->changes
|= changes_crtc
;
2128 static int check_strtol(char* s
)
2131 int result
= strtol(s
, &endptr
, 10);
2137 static double check_strtod(char* s
)
2140 double result
= strtod(s
, &endptr
);
2146 int main(int argc
, char** argv
)
2148 XRRScreenSize
* sizes
;
2149 XRRScreenConfiguration
* sc
;
2153 Status status
= RRSetConfigFailed
;
2156 int action_requested
= False
;
2157 Rotation rotation
, current_rotation
, rotations
;
2158 XRRScreenChangeNotifyEvent event
;
2159 XRRScreenChangeNotifyEvent
* sce
;
2160 char* display_name
= NULL
;
2162 SizeID current_size
;
2168 Bool version
= False
;
2169 int event_base
, error_base
;
2171 int width
= 0, height
= 0;
2172 Bool have_pixel_size
= False
;
2174 output_t
* output
= NULL
;
2175 Bool setit_1_2
= False
;
2176 Bool query_1_2
= False
;
2177 Bool modeit
= False
;
2178 Bool propit
= False
;
2179 Bool query_1
= False
;
2181 Bool current
= False
;
2183 program_name
= argv
[0];
2184 for (i
= 1; i
< argc
; i
++)
2186 if (!strcmp("-display", argv
[i
]) || !strcmp("-d", argv
[i
]))
2190 display_name
= argv
[i
];
2193 if (!strcmp("-help", argv
[i
]))
2196 action_requested
= True
;
2199 if (!strcmp("--verbose", argv
[i
]))
2204 if (!strcmp("--dryrun", argv
[i
]))
2210 if (!strcmp("--nograb", argv
[i
]))
2212 grab_server
= False
;
2215 if (!strcmp("--current", argv
[i
]))
2221 if (!strcmp("-s", argv
[i
]) || !strcmp("--size", argv
[i
]))
2225 if (sscanf(argv
[i
], "%dx%d", &width
, &height
) == 2)
2227 have_pixel_size
= True
;
2231 size
= check_strtol(argv
[i
]);
2236 action_requested
= True
;
2240 if (!strcmp("-r", argv
[i
]) || !strcmp("--rate", argv
[i
]) || !strcmp("--refresh", argv
[i
]))
2244 rate
= check_strtod(argv
[i
]);
2248 output
->refresh
= rate
;
2249 output
->changes
|= changes_refresh
;
2252 action_requested
= True
;
2256 if (!strcmp("-v", argv
[i
]) || !strcmp("--version", argv
[i
]))
2259 action_requested
= True
;
2263 if (!strcmp("-x", argv
[i
]))
2265 reflection
|= RR_Reflect_X
;
2267 action_requested
= True
;
2270 if (!strcmp("-y", argv
[i
]))
2272 reflection
|= RR_Reflect_Y
;
2274 action_requested
= True
;
2277 if (!strcmp("--screen", argv
[i
]))
2281 screen
= check_strtol(argv
[i
]);
2286 if (!strcmp("-q", argv
[i
]) || !strcmp("--query", argv
[i
]))
2291 if (!strcmp("-o", argv
[i
]) || !strcmp("--orientation", argv
[i
]))
2296 dirind
= strtol(argv
[i
], &endptr
, 10);
2297 if (argv
[i
] == endptr
)
2299 for (dirind
= 0; dirind
< 4; dirind
++)
2301 if (strcmp(direction
[dirind
], argv
[i
]) == 0)
2304 if ((dirind
< 0) || (dirind
> 3))
2309 action_requested
= True
;
2312 if (!strcmp("--prop", argv
[i
]) || !strcmp("--props", argv
[i
]) ||
2313 !strcmp("--madprops", argv
[i
]) || !strcmp("--properties", argv
[i
]))
2317 action_requested
= True
;
2320 if (!strcmp("--output", argv
[i
]))
2325 output
= find_output_by_name(argv
[i
]);
2328 output
= add_output();
2329 set_name(&output
->output
, argv
[i
], name_string
| name_xid
);
2333 action_requested
= True
;
2336 if (!strcmp("--crtc", argv
[i
]))
2342 set_name(&output
->crtc
, argv
[i
], name_xid
| name_index
);
2343 output
->changes
|= changes_crtc
;
2346 if (!strcmp("--mode", argv
[i
]))
2352 set_name(&output
->mode
, argv
[i
], name_string
| name_xid
);
2353 output
->changes
|= changes_mode
;
2356 if (!strcmp("--preferred", argv
[i
]))
2360 set_name_preferred(&output
->mode
);
2361 output
->changes
|= changes_mode
;
2364 if (!strcmp("--pos", argv
[i
]))
2370 if (sscanf(argv
[i
], "%dx%d", &output
->x
, &output
->y
) != 2)
2372 output
->changes
|= changes_position
;
2375 if (!strcmp("--rotation", argv
[i
]) || !strcmp("--rotate", argv
[i
]))
2381 for (dirind
= 0; dirind
< 4; dirind
++)
2383 if (strcmp(direction
[dirind
], argv
[i
]) == 0)
2388 output
->rotation
&= ~0xf;
2389 output
->rotation
|= 1 << dirind
;
2390 output
->changes
|= changes_rotation
;
2393 if (!strcmp("--reflect", argv
[i
]) || !strcmp("--reflection", argv
[i
]))
2399 for (dirind
= 0; dirind
< 4; dirind
++)
2401 if (strcmp(reflections
[dirind
], argv
[i
]) == 0)
2406 output
->rotation
&= ~(RR_Reflect_X
| RR_Reflect_Y
);
2407 output
->rotation
|= dirind
* RR_Reflect_X
;
2408 output
->changes
|= changes_reflection
;
2411 if (!strcmp("--left-of", argv
[i
]))
2417 output
->relation
= relation_left_of
;
2418 output
->relative_to
= argv
[i
];
2419 output
->changes
|= changes_relation
;
2422 if (!strcmp("--right-of", argv
[i
]))
2428 output
->relation
= relation_right_of
;
2429 output
->relative_to
= argv
[i
];
2430 output
->changes
|= changes_relation
;
2433 if (!strcmp("--above", argv
[i
]))
2439 output
->relation
= relation_above
;
2440 output
->relative_to
= argv
[i
];
2441 output
->changes
|= changes_relation
;
2444 if (!strcmp("--below", argv
[i
]))
2450 output
->relation
= relation_below
;
2451 output
->relative_to
= argv
[i
];
2452 output
->changes
|= changes_relation
;
2455 if (!strcmp("--same-as", argv
[i
]))
2461 output
->relation
= relation_same_as
;
2462 output
->relative_to
= argv
[i
];
2463 output
->changes
|= changes_relation
;
2466 if (!strcmp("--panning", argv
[i
]))
2473 pan
= &output
->panning
;
2474 switch (sscanf(argv
[i
], "%dx%d+%d+%d/%dx%d+%d+%d/%d/%d/%d/%d", &pan
->width
, &pan
->height
,
2475 &pan
->left
, &pan
->top
, &pan
->track_width
, &pan
->track_height
, &pan
->track_left
,
2476 &pan
->track_top
, &pan
->border_left
, &pan
->border_top
, &pan
->border_right
,
2477 &pan
->border_bottom
))
2480 pan
->left
= pan
->top
= 0;
2482 __attribute__((fallthrough
));
2484 pan
->track_left
= pan
->track_top
= pan
->track_width
= pan
->track_height
= 0;
2486 __attribute__((fallthrough
));
2488 pan
->border_left
= pan
->border_top
= pan
->border_right
= pan
->border_bottom
= 0;
2490 __attribute__((fallthrough
));
2496 output
->changes
|= changes_panning
;
2499 if (!strcmp("--gamma", argv
[i
]))
2505 if (sscanf(argv
[i
], "%f:%f:%f", &output
->gamma
.red
, &output
->gamma
.green
,
2506 &output
->gamma
.blue
) != 3)
2508 output
->changes
|= changes_gamma
;
2512 if (!strcmp("--brightness", argv
[i
]))
2518 if (sscanf(argv
[i
], "%f", &output
->brightness
) != 1)
2520 output
->changes
|= changes_gamma
;
2524 if (!strcmp("--primary", argv
[i
]))
2528 output
->changes
|= changes_primary
;
2529 output
->primary
= True
;
2533 if (!strcmp("--noprimary", argv
[i
]))
2539 if (!strcmp("--set", argv
[i
]))
2541 output_prop_t
* prop
;
2544 prop
= malloc(sizeof(output_prop_t
));
2545 prop
->next
= output
->props
;
2546 output
->props
= prop
;
2549 prop
->name
= argv
[i
];
2552 prop
->value
= argv
[i
];
2554 output
->changes
|= changes_property
;
2558 if (!strcmp("--scale", argv
[i
]))
2565 if (sscanf(argv
[i
], "%lfx%lf", &sx
, &sy
) != 2)
2567 init_transform(&output
->transform
);
2568 output
->transform
.transform
.matrix
[0][0] = XDoubleToFixed(sx
);
2569 output
->transform
.transform
.matrix
[1][1] = XDoubleToFixed(sy
);
2570 output
->transform
.transform
.matrix
[2][2] = XDoubleToFixed(1.0);
2571 if (sx
!= 1 || sy
!= 1)
2572 output
->transform
.filter
= "bilinear";
2574 output
->transform
.filter
= "nearest";
2575 output
->transform
.nparams
= 0;
2576 output
->transform
.params
= NULL
;
2577 output
->changes
|= changes_transform
;
2580 if (!strcmp("--scale-from", argv
[i
]))
2587 if (sscanf(argv
[i
], "%dx%d", &w
, &h
) != 2)
2589 if (w
<= 0 || h
<= 0)
2591 output
->scale_from_w
= w
;
2592 output
->scale_from_h
= h
;
2593 output
->changes
|= changes_transform
;
2596 if (!strcmp("--transform", argv
[i
]))
2598 double transform
[3][3];
2604 init_transform(&output
->transform
);
2605 if (strcmp(argv
[i
], "none") != 0)
2607 if (sscanf(argv
[i
], "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf", &transform
[0][0],
2608 &transform
[0][1], &transform
[0][2], &transform
[1][0], &transform
[1][1],
2609 &transform
[1][2], &transform
[2][0], &transform
[2][1], &transform
[2][2]) != 9)
2611 init_transform(&output
->transform
);
2612 for (k
= 0; k
< 3; k
++)
2613 for (l
= 0; l
< 3; l
++)
2615 output
->transform
.transform
.matrix
[k
][l
] = XDoubleToFixed(transform
[k
][l
]);
2617 output
->transform
.filter
= "bilinear";
2618 output
->transform
.nparams
= 0;
2619 output
->transform
.params
= NULL
;
2621 output
->changes
|= changes_transform
;
2624 if (!strcmp("--off", argv
[i
]))
2628 set_name_xid(&output
->mode
, None
);
2629 set_name_xid(&output
->crtc
, None
);
2630 output
->changes
|= changes_mode
;
2633 if (!strcmp("--fb", argv
[i
]))
2637 if (sscanf(argv
[i
], "%dx%d", &fb_width
, &fb_height
) != 2)
2640 action_requested
= True
;
2643 if (!strcmp("--fbmm", argv
[i
]))
2647 if (sscanf(argv
[i
], "%dx%d", &fb_width_mm
, &fb_height_mm
) != 2)
2650 action_requested
= True
;
2653 if (!strcmp("--dpi", argv
[i
]))
2658 dpi
= strtod(argv
[i
], &strtod_error
);
2659 if (argv
[i
] == strtod_error
)
2662 dpi_output
= argv
[i
];
2665 action_requested
= True
;
2668 if (!strcmp("--auto", argv
[i
]))
2672 output
->automatic
= True
;
2673 output
->changes
|= changes_automatic
;
2678 action_requested
= True
;
2681 if (!strcmp("--q12", argv
[i
]))
2686 if (!strcmp("--q1", argv
[i
]))
2691 if (!strcmp("--newmode", argv
[i
]))
2693 umode_t
* m
= malloc(sizeof(umode_t
));
2699 m
->mode
.name
= argv
[i
];
2700 m
->mode
.nameLength
= strlen(argv
[i
]);
2702 clock
= check_strtod(argv
[i
++]);
2703 m
->mode
.dotClock
= clock
* 1e6
;
2705 m
->mode
.width
= check_strtol(argv
[i
++]);
2706 m
->mode
.hSyncStart
= check_strtol(argv
[i
++]);
2707 m
->mode
.hSyncEnd
= check_strtol(argv
[i
++]);
2708 m
->mode
.hTotal
= check_strtol(argv
[i
++]);
2709 m
->mode
.height
= check_strtol(argv
[i
++]);
2710 m
->mode
.vSyncStart
= check_strtol(argv
[i
++]);
2711 m
->mode
.vSyncEnd
= check_strtol(argv
[i
++]);
2712 m
->mode
.vTotal
= check_strtol(argv
[i
++]);
2713 m
->mode
.modeFlags
= 0;
2718 for (f
= 0; mode_flags
[f
].string
; f
++)
2719 if (!strcasecmp(mode_flags
[f
].string
, argv
[i
]))
2722 if (!mode_flags
[f
].string
)
2724 m
->mode
.modeFlags
|= mode_flags
[f
].flag
;
2728 m
->action
= umode_create
;
2731 action_requested
= True
;
2734 if (!strcmp("--rmmode", argv
[i
]))
2736 umode_t
* m
= malloc(sizeof(umode_t
));
2740 set_name(&m
->name
, argv
[i
], name_string
| name_xid
);
2741 m
->action
= umode_destroy
;
2745 action_requested
= True
;
2748 if (!strcmp("--addmode", argv
[i
]))
2750 umode_t
* m
= malloc(sizeof(umode_t
));
2754 set_name(&m
->output
, argv
[i
], name_string
| name_xid
);
2757 set_name(&m
->name
, argv
[i
], name_string
| name_xid
);
2758 m
->action
= umode_add
;
2762 action_requested
= True
;
2765 if (!strcmp("--delmode", argv
[i
]))
2767 umode_t
* m
= malloc(sizeof(umode_t
));
2771 set_name(&m
->output
, argv
[i
], name_string
| name_xid
);
2774 set_name(&m
->name
, argv
[i
], name_string
| name_xid
);
2775 m
->action
= umode_delete
;
2779 action_requested
= True
;
2784 if (!action_requested
)
2789 if (setit
&& !setit_1_2
)
2795 printf("xrandr program version " VERSION "\n");
2798 dpy
= XOpenDisplay(display_name
);
2802 fprintf(stderr
, "Can't open display %s\n", XDisplayName(display_name
));
2806 screen
= DefaultScreen(dpy
);
2807 if (screen
>= ScreenCount(dpy
))
2809 fprintf(stderr
, "Invalid screen number %d (display has %d)\n", screen
, ScreenCount(dpy
));
2813 root
= RootWindow(dpy
, screen
);
2815 if (!XRRQueryExtension(dpy
, &event_base
, &error_base
) || !XRRQueryVersion(dpy
, &major
, &minor
))
2817 fprintf(stderr
, "RandR extension missing\n");
2820 if (major
> 1 || (major
== 1 && minor
>= 2))
2822 if (major
> 1 || (major
== 1 && minor
>= 3))
2825 if (has_1_2
&& modeit
)
2829 get_screen(current
);
2833 for (m
= umodes
; m
; m
= m
->next
)
2841 XRRCreateMode(dpy
, root
, &m
->mode
);
2844 e
= find_mode(&m
->name
, 0);
2846 fatal("cannot find mode \"%s\"\n", m
->name
.string
);
2847 XRRDestroyMode(dpy
, e
->id
);
2850 o
= find_output(&m
->output
);
2852 fatal("cannot find output \"%s\"\n", m
->output
.string
);
2853 e
= find_mode(&m
->name
, 0);
2855 fatal("cannot find mode \"%s\"\n", m
->name
.string
);
2856 XRRAddOutputMode(dpy
, o
->output
.xid
, e
->id
);
2859 o
= find_output(&m
->output
);
2861 fatal("cannot find output \"%s\"\n", m
->output
.string
);
2862 e
= find_mode(&m
->name
, 0);
2864 fatal("cannot find mode \"%s\"\n", m
->name
.string
);
2865 XRRDeleteOutputMode(dpy
, o
->output
.xid
, e
->id
);
2875 if (has_1_2
&& propit
)
2878 get_screen(current
);
2882 for (output
= outputs
; output
; output
= output
->next
)
2884 output_prop_t
* prop
;
2886 for (prop
= output
->props
; prop
; prop
= prop
->next
)
2888 Atom name
= XInternAtom(dpy
, prop
->name
, False
);
2891 unsigned char* data
;
2894 unsigned long ulong_value
;
2895 unsigned char* prop_data
;
2897 unsigned long nitems
, bytes_after
;
2899 XRRPropertyInfo
* propinfo
;
2901 type
= AnyPropertyType
;
2903 if (XRRGetOutputProperty(dpy
, output
->output
.xid
, name
, 0, 100, False
, False
,
2904 AnyPropertyType
, &actual_type
, &actual_format
, &nitems
,
2905 &bytes_after
, &prop_data
) == Success
&&
2907 (propinfo
= XRRQueryOutputProperty(dpy
, output
->output
.xid
, name
)))
2910 format
= actual_format
;
2913 if ((type
== XA_INTEGER
|| type
== AnyPropertyType
) &&
2914 (sscanf(prop
->value
, "%d", &int_value
) == 1 ||
2915 sscanf(prop
->value
, "0x%x", &int_value
) == 1))
2918 ulong_value
= int_value
;
2919 data
= (unsigned char*)&ulong_value
;
2923 else if (type
== XA_ATOM
)
2925 ulong_value
= XInternAtom(dpy
, prop
->value
, False
);
2926 data
= (unsigned char*)&ulong_value
;
2929 else if ((type
== XA_STRING
|| type
== AnyPropertyType
))
2932 data
= (unsigned char*)prop
->value
;
2933 nelements
= strlen(prop
->value
);
2938 XRRChangeOutputProperty(dpy
, output
->output
.xid
, name
, type
, format
, PropModeReplace
, data
,
2950 get_screen(current
);
2959 * Assign outputs to crtcs
2964 * Mark changing crtcs
2966 mark_changing_crtcs();
2969 * If an output was specified to track dpi, use it
2973 output_t
* output
= find_output_by_name(dpi_output
);
2974 XRROutputInfo
* output_info
;
2975 XRRModeInfo
* mode_info
;
2977 fatal("Cannot find output %s\n", dpi_output
);
2978 output_info
= output
->output_info
;
2979 mode_info
= output
->mode_info
;
2980 if (output_info
&& mode_info
&& output_info
->mm_height
)
2983 * When this output covers the whole screen, just use
2984 * the known physical size
2986 if ((unsigned int)fb_width
== mode_info
->width
&&
2987 (unsigned int)fb_height
== mode_info
->height
)
2989 fb_width_mm
= output_info
->mm_width
;
2990 fb_height_mm
= output_info
->mm_height
;
2994 dpi
= (25.4 * mode_info
->height
) / output_info
->mm_height
;
3000 * Compute physical screen size
3002 if (fb_width_mm
== 0 || fb_height_mm
== 0)
3004 if (fb_width
!= DisplayWidth(dpy
, screen
) || fb_height
!= DisplayHeight(dpy
, screen
) ||
3008 dpi
= (25.4 * DisplayHeight(dpy
, screen
)) / DisplayHeightMM(dpy
, screen
);
3010 fb_width_mm
= (25.4 * fb_width
) / dpi
;
3011 fb_height_mm
= (25.4 * fb_height
) / dpi
;
3015 fb_width_mm
= DisplayWidthMM(dpy
, screen
);
3016 fb_height_mm
= DisplayHeightMM(dpy
, screen
);
3026 * Set gamma on crtc's that belong to the outputs.
3031 * Now apply all of the changes
3038 if (query_1_2
|| (query
&& has_1_2
&& !query_1
))
3042 #define ModeShown 0x80000000
3044 get_screen(current
);
3048 printf("<screen id=\"%d\" minimum_w=\"%d\" minimum_h=\"%d\" current_w=\"%d\" current_h=\"%d\" "
3049 "maximum_w=\"%d\" maximum_h=\"%d\">\n",
3050 screen
, minWidth
, minHeight
, DisplayWidth(dpy
, screen
), DisplayHeight(dpy
, screen
),
3051 maxWidth
, maxHeight
);
3053 for (output
= outputs
; output
; output
= output
->next
)
3055 XRROutputInfo
* output_info
= output
->output_info
;
3056 crtc_t
* crtc
= output
->crtc_info
;
3057 XRRCrtcInfo
* crtc_info
= crtc
? crtc
->crtc_info
: NULL
;
3058 XRRModeInfo
* mode
= output
->mode_info
;
3062 // Rotation rotations = output_rotations (output);
3064 printf(" <output name=\"%s\" connected=\"%s\"", output_info
->name
,
3065 connection
[output_info
->connection
]);
3070 printf(" w=\"%d\" h=\"%d\" x=\"%d\" y=\"%d\" crtc=\"%d\"", crtc_info
->width
,
3071 crtc_info
->height
, crtc_info
->x
, crtc_info
->y
, crtc
->crtc
.index
);
3075 printf(" w=\"%d\" h=\"%d\" x=\"%d\" y=\"%d\"", mode
->width
, mode
->height
, output
->x
,
3079 printf(" id=\"%lx\"", mode
->id
);
3080 if (output
->rotation
!= RR_Rotate_0
|| verbose
)
3082 printf(" rotation=\"%s\"", rotation_name(output
->rotation
));
3083 if (output
->rotation
& (RR_Reflect_X
| RR_Reflect_Y
))
3084 printf(" reflection=\"%s\"", reflection_name(output
->rotation
));
3088 if (rotations != RR_Rotate_0 || verbose)
3092 for (i = 0; i < 4; i ++) {
3093 if ((rotations >> i) & 1) {
3094 if (!first) printf (" "); first = False;
3095 printf("%s", direction[i]);
3098 if (rotations & RR_Reflect_X)
3100 if (!first) printf (" "); first = False;
3103 if (rotations & RR_Reflect_Y)
3105 if (!first) printf (" ");
3114 printf(" wmm=\"%d\" hmm=\"%d\"", (int)output_info
->mm_width
, (int)output_info
->mm_height
);
3117 if (crtc
&& crtc
->panning_info
&& crtc
->panning_info
->width
> 0)
3119 XRRPanning
* pan
= crtc
->panning_info
;
3120 printf(" panning %dx%d+%d+%d", pan
->width
, pan
->height
, pan
->left
, pan
->top
);
3121 if ((pan
->track_width
!= 0 &&
3122 (pan
->track_left
!= pan
->left
|| pan
->track_width
!= pan
->width
||
3123 pan
->border_left
!= 0 || pan
->border_right
!= 0)) ||
3124 (pan
->track_height
!= 0 &&
3125 (pan
->track_top
!= pan
->top
|| pan
->track_height
!= pan
->height
||
3126 pan
->border_top
!= 0 || pan
->border_bottom
!= 0)))
3127 printf(" tracking %dx%d+%d+%d border %d/%d/%d/%d", pan
->track_width
, pan
->track_height
,
3128 pan
->track_left
, pan
->track_top
, pan
->border_left
, pan
->border_top
,
3129 pan
->border_right
, pan
->border_bottom
);
3135 printf("\tIdentifier: 0x%x\n", (int)output
->output
.xid
);
3136 printf("\tTimestamp: %d\n", (int)output_info
->timestamp
);
3137 printf("\tSubpixel: %s\n", order
[output_info
->subpixel_order
]);
3138 if (output
->gamma
.red
!= 0.0f
&& output
->gamma
.green
!= 0.0f
&& output
->gamma
.blue
!= 0.0f
)
3140 printf("\tGamma: %#.2g:%#.2g:%#.2g\n", (double)output
->gamma
.red
,
3141 (double)output
->gamma
.green
, (double)output
->gamma
.blue
);
3142 printf("\tBrightness: %#.2g\n", (double)output
->brightness
);
3144 printf("\tClones: ");
3145 for (j
= 0; j
< output_info
->nclone
; j
++)
3147 output_t
* clone
= find_output_by_xid(output_info
->clones
[j
]);
3150 printf(" %s", clone
->output
.string
);
3153 if (output
->crtc_info
)
3154 printf("\tCRTC: %d\n", output
->crtc_info
->crtc
.index
);
3155 printf("\tCRTCs: ");
3156 for (j
= 0; j
< output_info
->ncrtc
; j
++)
3158 crtc_t
* crtc
= find_crtc_by_xid(output_info
->crtcs
[j
]);
3160 printf(" %d", crtc
->crtc
.index
);
3163 if (output
->crtc_info
&& output
->crtc_info
->panning_info
)
3165 XRRPanning
* pan
= output
->crtc_info
->panning_info
;
3166 printf("\tPanning: %dx%d+%d+%d\n", pan
->width
, pan
->height
, pan
->left
, pan
->top
);
3167 printf("\tTracking: %dx%d+%d+%d\n", pan
->track_width
, pan
->track_height
,
3168 pan
->track_left
, pan
->track_top
);
3169 printf("\tBorder: %d/%d/%d/%d\n", pan
->border_left
, pan
->border_top
,
3170 pan
->border_right
, pan
->border_bottom
);
3177 printf("\tTransform: ");
3178 for (y
= 0; y
< 3; y
++)
3180 for (x
= 0; x
< 3; x
++)
3181 printf(" %f", XFixedToDouble(output
->transform
.transform
.matrix
[y
][x
]));
3185 if (output
->transform
.filter
)
3186 printf("\n\t filter: %s", output
->transform
.filter
);
3189 if (verbose
|| properties
)
3191 props
= XRRListOutputProperties(dpy
, output
->output
.xid
, &nprop
);
3192 for (j
= 0; j
< nprop
; j
++)
3194 unsigned char* prop
;
3196 unsigned long nitems
, bytes_after
;
3198 XRRPropertyInfo
* propinfo
;
3200 XRRGetOutputProperty(dpy
, output
->output
.xid
, props
[j
], 0, 100, False
, False
,
3201 AnyPropertyType
, &actual_type
, &actual_format
, &nitems
, &bytes_after
,
3204 propinfo
= XRRQueryOutputProperty(dpy
, output
->output
.xid
, props
[j
]);
3206 if (actual_type
== XA_INTEGER
&& actual_format
== 8)
3210 printf("\t%s:\n", XGetAtomName(dpy
, props
[j
]));
3211 for (k
= 0; k
< (long)nitems
; k
++)
3215 printf("%02x", (unsigned char)prop
[k
]);
3220 else if (actual_type
== XA_INTEGER
&& actual_format
== 32)
3222 printf("\t%s: ", XGetAtomName(dpy
, props
[j
]));
3223 for (k
= 0; k
< (long)nitems
; k
++)
3227 printf("%d (0x%08x)", (int)((INT32
*)prop
)[k
], (int)((INT32
*)prop
)[k
]);
3230 if (propinfo
->range
&& propinfo
->num_values
> 0)
3234 printf("\trange%s: ", (propinfo
->num_values
== 2) ? "" : "s");
3236 for (k
= 0; k
< propinfo
->num_values
/ 2; k
++)
3237 printf(" (%d,%d)", (int)propinfo
->values
[k
* 2], (int)propinfo
->values
[k
* 2 + 1]);
3242 else if (actual_type
== XA_ATOM
&& actual_format
== 32)
3244 printf("\t%s:", XGetAtomName(dpy
, props
[j
]));
3245 for (k
= 0; k
< (long)nitems
; k
++)
3247 if (k
> 0 && (k
& 1) == 0)
3249 printf("\t%s", XGetAtomName(dpy
, ((Atom
*)prop
)[k
]));
3252 if (!propinfo
->range
&& propinfo
->num_values
> 0)
3254 printf("\n\t\tsupported:");
3256 for (k
= 0; k
< propinfo
->num_values
; k
++)
3258 printf(" %-12.12s", XGetAtomName(dpy
, propinfo
->values
[k
]));
3259 if (k
% 4 == 3 && k
< propinfo
->num_values
- 1)
3265 else if (actual_format
== 8)
3267 printf("\t%s: %s%s\n", XGetAtomName(dpy
, props
[j
]), prop
, bytes_after
? "..." : "");
3271 char* type
= actual_type
? XGetAtomName(dpy
, actual_type
) : "none";
3272 printf("\t%s: %s(%d) (format %d items %d) ????\n", XGetAtomName(dpy
, props
[j
]), type
,
3273 (int)actual_type
, actual_format
, (int)nitems
);
3282 for (j
= 0; j
< output_info
->nmode
; j
++)
3284 XRRModeInfo
* mode
= find_mode_by_xid(output_info
->modes
[j
]);
3287 printf(" %s (0x%x) %6.1fMHz", mode
->name
, (int)mode
->id
,
3288 (double)mode
->dotClock
/ 1000000.0);
3289 for (f
= 0; mode_flags
[f
].flag
; f
++)
3290 if (mode
->modeFlags
& mode_flags
[f
].flag
)
3291 printf(" %s", mode_flags
[f
].string
);
3292 if (mode
== output
->mode_info
)
3293 printf(" *current");
3294 if (j
< output_info
->npreferred
)
3295 printf(" +preferred");
3297 printf(" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
3298 mode
->width
, mode
->hSyncStart
, mode
->hSyncEnd
, mode
->hTotal
, mode
->hSkew
,
3299 mode_hsync(mode
) / 1000);
3300 printf(" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
3301 mode
->height
, mode
->vSyncStart
, mode
->vSyncEnd
, mode
->vTotal
, mode_refresh(mode
));
3302 mode
->modeFlags
|= ModeShown
;
3307 mode_shown
= calloc(output_info
->nmode
, sizeof(Bool
));
3309 fatal("out of memory\n");
3310 for (j
= 0; j
< output_info
->nmode
; j
++)
3312 XRRModeInfo
*jmode
, *kmode
;
3317 jmode
= find_mode_by_xid(output_info
->modes
[j
]);
3318 for (k
= j
; k
< output_info
->nmode
; k
++)
3322 kmode
= find_mode_by_xid(output_info
->modes
[k
]);
3323 if (strcmp(jmode
->name
, kmode
->name
) != 0)
3325 mode_shown
[k
] = True
;
3326 kmode
->modeFlags
|= ModeShown
;
3327 printf(" <mode id=\"0x%lx\" name=\"%s\" w=\"%d\" h=\"%d\" hz=\"%.5f\"", kmode
->id
,
3328 kmode
->name
, kmode
->width
, kmode
->height
, mode_refresh(kmode
));
3329 if (kmode
== output
->mode_info
)
3330 printf(" current=\"true\"");
3332 printf(" current=\"false\"");
3333 if (k
< output_info
->npreferred
)
3334 printf(" preferred=\"true\"");
3336 printf(" preferred=\"false\"");
3342 printf(" </output>\n");
3346 for (m = 0; m < res->nmode; m++)
3348 XRRModeInfo *mode = &res->modes[m];
3350 if (!(mode->modeFlags & ModeShown))
3352 printf (" %s (0x%x) %6.1fMHz\n",
3353 mode->name, (int)mode->id,
3354 (double)mode->dotClock / 1000000.0);
3355 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
3356 mode->width, mode->hSyncStart, mode->hSyncEnd,
3357 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
3358 printf (" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
3359 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
3360 mode_refresh (mode));
3364 printf("</screen>\n");
3368 sc
= XRRGetScreenInfo(dpy
, root
);
3373 current_size
= XRRConfigCurrentConfiguration(sc
, ¤t_rotation
);
3375 sizes
= XRRConfigSizes(sc
, &nsize
);
3377 if (have_pixel_size
)
3379 for (size
= 0; size
< nsize
; size
++)
3381 if (sizes
[size
].width
== width
&& sizes
[size
].height
== height
)
3386 fprintf(stderr
, "Size %dx%d not found in available modes\n", width
, height
);
3391 size
= current_size
;
3392 else if (size
>= nsize
)
3394 fprintf(stderr
, "Size index %d is too large, there are only %d sizes\n", size
, nsize
);
3400 for (rot
= 0; rot
< 4; rot
++)
3401 if (1 << rot
== (current_rotation
& 0xf))
3405 current_rate
= XRRConfigCurrentRate(sc
);
3409 if (size
== current_size
)
3410 rate
= current_rate
;
3416 rates
= XRRConfigRates(sc
, size
, &nrate
);
3417 for (i
= 0; i
< nrate
; i
++)
3418 if (rate
== rates
[i
])
3422 fprintf(stderr
, "Rate %.1f Hz not available for this size\n", rate
);
3429 int major_version
, minor_version
;
3430 XRRQueryVersion(dpy
, &major_version
, &minor_version
);
3431 printf("Server reports RandR version %d.%d\n", major_version
, minor_version
);
3434 if (query
|| query_1
)
3436 printf(" SZ: Pixels Physical Refresh\n");
3437 for (i
= 0; i
< nsize
; i
++)
3439 printf("%c%-2d %5d x %-5d (%4dmm x%4dmm )", i
== current_size
? '*' : ' ', i
, sizes
[i
].width
,
3440 sizes
[i
].height
, sizes
[i
].mwidth
, sizes
[i
].mheight
);
3441 rates
= XRRConfigRates(sc
, i
, &nrate
);
3444 for (j
= 0; j
< nrate
; j
++)
3445 printf("%c%-4d", i
== current_size
&& rates
[j
] == current_rate
? '*' : ' ', rates
[j
]);
3450 rotations
= XRRConfigRotations(sc
, ¤t_rotation
);
3452 rotation
= 1 << rot
;
3455 printf("Current rotation - %s\n", rotation_name(current_rotation
));
3457 printf("Current reflection - %s\n", reflection_name(current_rotation
));
3459 printf("Rotations possible - ");
3460 for (i
= 0; i
< 4; i
++)
3462 if ((rotations
>> i
) & 1)
3463 printf("%s ", direction
[i
]);
3467 printf("Reflections possible - ");
3468 if (rotations
& (RR_Reflect_X
| RR_Reflect_Y
))
3470 if (rotations
& RR_Reflect_X
)
3472 if (rotations
& RR_Reflect_Y
)
3482 printf("Setting size to %d, rotation to %s\n", size
, direction
[rot
]);
3484 printf("Setting reflection on ");
3487 if (reflection
& RR_Reflect_X
)
3489 if (reflection
& RR_Reflect_Y
)
3493 printf("neither axis");
3496 if (reflection
& RR_Reflect_X
)
3497 printf("Setting reflection on X axis\n");
3499 if (reflection
& RR_Reflect_Y
)
3500 printf("Setting reflection on Y axis\n");
3503 /* we should test configureNotify on the root window */
3504 XSelectInput(dpy
, root
, StructureNotifyMask
);
3506 if (setit
&& !dryrun
)
3507 XRRSelectInput(dpy
, root
, RRScreenChangeNotifyMask
);
3508 if (setit
&& !dryrun
)
3509 status
= XRRSetScreenConfigAndRate(dpy
, sc
, root
, (SizeID
)size
,
3510 (Rotation
)(rotation
| reflection
), rate
, CurrentTime
);
3512 if (setit
&& !dryrun
&& status
== RRSetConfigFailed
)
3514 printf("Failed to change the screen configuration!\n");
3518 if (verbose
&& setit
&& !dryrun
&& size
!= current_size
)
3520 if (status
== RRSetConfigSuccess
)
3522 Bool seen_screen
= False
;
3523 while (!seen_screen
)
3526 XNextEvent(dpy
, (XEvent
*)&event
);
3528 printf("Event received, type = %d\n", event
.type
);
3529 /* update Xlib's knowledge of the event */
3530 XRRUpdateConfiguration((XEvent
*)&event
);
3531 if (event
.type
== ConfigureNotify
)
3532 printf("Received ConfigureNotify Event!\n");
3534 switch (event
.type
- event_base
)
3536 case RRScreenChangeNotify
:
3537 sce
= (XRRScreenChangeNotifyEvent
*)&event
;
3539 printf("Got a screen change notify event!\n");
3540 printf(" window = %d\n root = %d\n size_index = %d\n rotation %d\n", (int)sce
->window
,
3541 (int)sce
->root
, sce
->size_index
, sce
->rotation
);
3542 printf(" timestamp = %ld, config_timestamp = %ld\n", sce
->timestamp
,
3543 sce
->config_timestamp
);
3544 printf(" Rotation = %x\n", sce
->rotation
);
3545 printf(" %d X %d pixels, %d X %d mm\n", sce
->width
, sce
->height
, sce
->mwidth
,
3547 printf("Display width %d, height %d\n", DisplayWidth(dpy
, screen
),
3548 DisplayHeight(dpy
, screen
));
3549 printf("Display widthmm %d, heightmm %d\n", DisplayWidthMM(dpy
, screen
),
3550 DisplayHeightMM(dpy
, screen
));
3551 spo
= sce
->subpixel_order
;
3552 if ((spo
< 0) || (spo
> 5))
3553 printf("Unknown subpixel order, value = %d\n", spo
);
3555 printf("new Subpixel rendering model is %s\n", order
[spo
]);
3559 if (event
.type
!= ConfigureNotify
)
3560 printf("unknown event received, type = %d!\n", event
.type
);
3565 XRRFreeScreenConfigInfo(sc
);