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
;
599 dest
->params
= malloc(nparams
* sizeof(XFixed
));
600 memcpy(dest
->params
, params
, nparams
* sizeof(XFixed
));
603 static void copy_transform(transform_t
* dest
, transform_t
* src
)
605 set_transform(dest
, &src
->transform
, src
->filter
, src
->params
, src
->nparams
);
608 static Bool
equal_transform(transform_t
* a
, transform_t
* b
)
610 if (memcmp(&a
->transform
, &b
->transform
, sizeof(XTransform
)) != 0)
612 if (strcmp(a
->filter
, b
->filter
) != 0)
614 if (a
->nparams
!= b
->nparams
)
616 if (memcmp(a
->params
, b
->params
, a
->nparams
* sizeof(XFixed
)) != 0)
621 static output_t
* add_output(void)
623 output_t
* output
= calloc(1, sizeof(output_t
));
626 fatal("out of memory\n");
628 output
->found
= False
;
629 output
->brightness
= 1.0;
630 *outputs_tail
= output
;
631 outputs_tail
= &output
->next
;
635 static output_t
* find_output(name_t
* name
)
639 for (output
= outputs
; output
; output
= output
->next
)
641 name_kind_t common
= name
->kind
& output
->output
.kind
;
643 if ((common
& name_xid
) && name
->xid
== output
->output
.xid
)
645 if ((common
& name_string
) && !strcmp(name
->string
, output
->output
.string
))
647 if ((common
& name_index
) && name
->index
== output
->output
.index
)
653 static output_t
* find_output_by_xid(RROutput output
)
657 init_name(&output_name
);
658 set_name_xid(&output_name
, output
);
659 return find_output(&output_name
);
662 static output_t
* find_output_by_name(char* name
)
666 init_name(&output_name
);
667 set_name_string(&output_name
, name
);
668 return find_output(&output_name
);
671 static crtc_t
* find_crtc(name_t
* name
)
676 for (c
= 0; c
< num_crtcs
; c
++)
681 common
= name
->kind
& crtc
->crtc
.kind
;
683 if ((common
& name_xid
) && name
->xid
== crtc
->crtc
.xid
)
685 if ((common
& name_string
) && !strcmp(name
->string
, crtc
->crtc
.string
))
687 if ((common
& name_index
) && name
->index
== crtc
->crtc
.index
)
694 static crtc_t
* find_crtc_by_xid(RRCrtc crtc
)
698 init_name(&crtc_name
);
699 set_name_xid(&crtc_name
, crtc
);
700 return find_crtc(&crtc_name
);
703 static XRRModeInfo
* find_mode(name_t
* name
, double refresh
)
706 XRRModeInfo
* best
= NULL
;
709 for (m
= 0; m
< res
->nmode
; m
++)
711 XRRModeInfo
* mode
= &res
->modes
[m
];
712 if ((name
->kind
& name_xid
) && name
->xid
== mode
->id
)
717 if ((name
->kind
& name_string
) && !strcmp(name
->string
, mode
->name
))
722 dist
= fabs(mode_refresh(mode
) - refresh
);
725 if (!best
|| dist
< bestDist
)
735 static XRRModeInfo
* find_mode_by_xid(RRMode mode
)
739 init_name(&mode_name
);
740 set_name_xid(&mode_name
, mode
);
741 return find_mode(&mode_name
, 0);
746 find_mode_by_name (char *name
)
749 init_name (&mode_name
);
750 set_name_string (&mode_name
, name
);
751 return find_mode (&mode_name
, 0);
755 static XRRModeInfo
* find_mode_for_output(output_t
* output
, name_t
* name
)
757 XRROutputInfo
* output_info
= output
->output_info
;
759 XRRModeInfo
* best
= NULL
;
762 for (m
= 0; m
< output_info
->nmode
; m
++)
766 mode
= find_mode_by_xid(output_info
->modes
[m
]);
769 if ((name
->kind
& name_xid
) && name
->xid
== mode
->id
)
774 if ((name
->kind
& name_string
) && !strcmp(name
->string
, mode
->name
))
778 /* Stay away from doublescan modes unless refresh rate is specified. */
779 if (!output
->refresh
&& (mode
->modeFlags
& RR_DoubleScan
))
783 dist
= fabs(mode_refresh(mode
) - output
->refresh
);
786 if (!best
|| dist
< bestDist
)
796 static XRRModeInfo
* preferred_mode(output_t
* output
)
798 XRROutputInfo
* output_info
= output
->output_info
;
805 for (m
= 0; m
< output_info
->nmode
; m
++)
807 XRRModeInfo
* mode_info
= find_mode_by_xid(output_info
->modes
[m
]);
810 if (m
< output_info
->npreferred
)
812 else if (output_info
->mm_height
)
813 dist
= (1000 * DisplayHeight(dpy
, screen
) / DisplayHeightMM(dpy
, screen
) -
814 1000 * mode_info
->height
/ output_info
->mm_height
);
816 dist
= DisplayHeight(dpy
, screen
) - mode_info
->height
;
820 if (!best
|| dist
< bestDist
)
829 static Bool
output_can_use_crtc(output_t
* output
, crtc_t
* crtc
)
831 XRROutputInfo
* output_info
= output
->output_info
;
834 for (c
= 0; c
< output_info
->ncrtc
; c
++)
835 if (output_info
->crtcs
[c
] == crtc
->crtc
.xid
)
840 static Bool
output_can_use_mode(output_t
* output
, XRRModeInfo
* mode
)
842 XRROutputInfo
* output_info
= output
->output_info
;
845 for (m
= 0; m
< output_info
->nmode
; m
++)
846 if (output_info
->modes
[m
] == mode
->id
)
851 static Bool
crtc_can_use_rotation(crtc_t
* crtc
, Rotation rotation
)
853 Rotation rotations
= crtc
->crtc_info
->rotations
;
854 Rotation dir
= rotation
& (RR_Rotate_0
| RR_Rotate_90
| RR_Rotate_180
| RR_Rotate_270
);
855 Rotation reflect
= rotation
& (RR_Reflect_X
| RR_Reflect_Y
);
856 if (((rotations
& dir
) != 0) && ((rotations
& reflect
) == reflect
))
863 crtc_can_use_transform (crtc_t
*crtc
, XTransform
*transform
)
867 XRRQueryVersion (dpy
, &major
, &minor
);
868 if (major
> 1 || (major
== 1 && minor
>= 3))
874 * Report only rotations that are supported by all crtcs
877 output_rotations (output_t
*output
)
880 Rotation rotation
= RR_Rotate_0
;
881 XRROutputInfo
*output_info
= output
->output_info
;
884 for (c
= 0; c
< output_info
->ncrtc
; c
++)
886 crtc_t
*crtc
= find_crtc_by_xid (output_info
->crtcs
[c
]);
890 rotation
= crtc
->crtc_info
->rotations
;
893 rotation
&= crtc
->crtc_info
->rotations
;
900 static Bool
output_can_use_rotation(output_t
* output
, Rotation rotation
)
902 XRROutputInfo
* output_info
= output
->output_info
;
905 /* make sure all of the crtcs can use this rotation.
906 * yes, this is not strictly necessary, but it is
907 * simpler,and we expect most drivers to either
908 * support rotation everywhere or nowhere
910 for (c
= 0; c
< output_info
->ncrtc
; c
++)
912 crtc_t
* crtc
= find_crtc_by_xid(output_info
->crtcs
[c
]);
913 if (crtc
&& !crtc_can_use_rotation(crtc
, rotation
))
919 static Bool
output_is_primary(output_t
* output
)
922 return XRRGetOutputPrimary(dpy
, root
) == output
->output
.xid
;
926 /* Returns the index of the last value in an array < 0xffff */
927 static int find_last_non_clamped(CARD16 array
[], int size
)
930 for (i
= size
- 1; i
> 0; i
--)
932 if (array
[i
] < 0xffff)
938 static void set_gamma_info(output_t
* output
)
941 double i1
, v1
, i2
, v2
;
942 int size
, middle
, last_best
, last_red
, last_green
, last_blue
;
945 if (!output
->crtc_info
)
948 size
= XRRGetCrtcGammaSize(dpy
, output
->crtc_info
->crtc
.xid
);
951 warning("Failed to get size of gamma for output %s\n", output
->output
.string
);
955 gamma
= XRRGetCrtcGamma(dpy
, output
->crtc_info
->crtc
.xid
);
958 warning("Failed to get gamma for output %s\n", output
->output
.string
);
963 * Here is a bit tricky because gamma is a whole curve for each
964 * color. So, typically, we need to represent 3 * 256 values as 3 + 1
965 * values. Therefore, we approximate the gamma curve (v) by supposing
966 * it always follows the way we set it: a power function (i^g)
967 * multiplied by a brightness (b).
969 * so g = (ln(v) - ln(b))/ln(i)
970 * and b can be found using two points (v1,i1) and (v2, i2):
971 * b = e^((ln(v2)*ln(i1) - ln(v1)*ln(i2))/ln(i1/i2))
972 * For the best resolution, we select i2 at the highest place not
973 * clamped and i1 at i2/2. Note that if i2 = 1 (as in most normal
974 * cases), then b = v2.
976 last_red
= find_last_non_clamped(gamma
->red
, size
);
977 last_green
= find_last_non_clamped(gamma
->green
, size
);
978 last_blue
= find_last_non_clamped(gamma
->blue
, size
);
979 best_array
= gamma
->red
;
980 last_best
= last_red
;
981 if (last_green
> last_best
)
983 last_best
= last_green
;
984 best_array
= gamma
->green
;
986 if (last_blue
> last_best
)
988 last_best
= last_blue
;
989 best_array
= gamma
->blue
;
994 middle
= last_best
/ 2;
995 i1
= (double)(middle
+ 1) / size
;
996 v1
= (double)(best_array
[middle
]) / 65535;
997 i2
= (double)(last_best
+ 1) / size
;
998 v2
= (double)(best_array
[last_best
]) / 65535;
1000 { /* The screen is black */
1001 output
->brightness
= 0;
1002 output
->gamma
.red
= 1;
1003 output
->gamma
.green
= 1;
1004 output
->gamma
.blue
= 1;
1008 if ((last_best
+ 1) == size
)
1009 output
->brightness
= v2
;
1011 output
->brightness
= exp((log(v2
) * log(i1
) - log(v1
) * log(i2
)) / log(i1
/ i2
));
1012 output
->gamma
.red
= logf((gamma
->red
[last_red
/ 2]) / output
->brightness
/ 65535) /
1013 logf(((last_red
/ 2) + 1) / size
);
1014 output
->gamma
.green
= logf((gamma
->green
[last_green
/ 2]) / output
->brightness
/ 65535) /
1015 logf(((last_green
/ 2) + 1) / size
);
1016 output
->gamma
.blue
= logf((gamma
->blue
[last_blue
/ 2]) / output
->brightness
/ 65535) /
1017 logf(((last_blue
/ 2) + 1) / size
);
1020 XRRFreeGamma(gamma
);
1023 static void set_output_info(output_t
* output
, RROutput xid
, XRROutputInfo
* output_info
)
1025 /* sanity check output info */
1026 if (output_info
->connection
!= RR_Disconnected
&& !output_info
->nmode
)
1027 warning("Output %s is not disconnected but has no modes\n", output_info
->name
);
1029 /* set output name and info */
1030 if (!(output
->output
.kind
& name_xid
))
1031 set_name_xid(&output
->output
, xid
);
1032 if (!(output
->output
.kind
& name_string
))
1033 set_name_string(&output
->output
, output_info
->name
);
1034 output
->output_info
= output_info
;
1036 /* set crtc name and info */
1037 if (!(output
->changes
& changes_crtc
))
1038 set_name_xid(&output
->crtc
, output_info
->crtc
);
1040 if (output
->crtc
.kind
== name_xid
&& output
->crtc
.xid
== None
)
1041 output
->crtc_info
= NULL
;
1044 output
->crtc_info
= find_crtc(&output
->crtc
);
1045 if (!output
->crtc_info
)
1047 if (output
->crtc
.kind
& name_xid
)
1048 fatal("cannot find crtc 0x%x\n", output
->crtc
.xid
);
1049 if (output
->crtc
.kind
& name_index
)
1050 fatal("cannot find crtc %d\n", output
->crtc
.index
);
1052 if (!output_can_use_crtc(output
, output
->crtc_info
))
1053 fatal("output %s cannot use crtc 0x%x\n", output
->output
.string
, output
->crtc_info
->crtc
.xid
);
1056 /* set mode name and info */
1057 if (!(output
->changes
& changes_mode
))
1059 crtc_t
* crtc
= NULL
;
1061 if (output_info
->crtc
)
1062 crtc
= find_crtc_by_xid(output_info
->crtc
);
1063 if (crtc
&& crtc
->crtc_info
)
1064 set_name_xid(&output
->mode
, crtc
->crtc_info
->mode
);
1065 else if (output
->crtc_info
)
1066 set_name_xid(&output
->mode
, output
->crtc_info
->crtc_info
->mode
);
1068 set_name_xid(&output
->mode
, None
);
1069 if (output
->mode
.xid
)
1071 output
->mode_info
= find_mode_by_xid(output
->mode
.xid
);
1072 if (!output
->mode_info
)
1073 fatal("server did not report mode 0x%x for output %s\n", output
->mode
.xid
,
1074 output
->output
.string
);
1077 output
->mode_info
= NULL
;
1079 else if (output
->mode
.kind
== name_xid
&& output
->mode
.xid
== None
)
1080 output
->mode_info
= NULL
;
1083 if (output
->mode
.kind
== name_preferred
)
1084 output
->mode_info
= preferred_mode(output
);
1086 output
->mode_info
= find_mode_for_output(output
, &output
->mode
);
1087 if (!output
->mode_info
)
1089 if (output
->mode
.kind
& name_preferred
)
1090 fatal("cannot find preferred mode\n");
1091 if (output
->mode
.kind
& name_string
)
1092 fatal("cannot find mode %s\n", output
->mode
.string
);
1093 if (output
->mode
.kind
& name_xid
)
1094 fatal("cannot find mode 0x%x\n", output
->mode
.xid
);
1096 if (!output_can_use_mode(output
, output
->mode_info
))
1097 fatal("output %s cannot use mode %s\n", output
->output
.string
, output
->mode_info
->name
);
1101 if (!(output
->changes
& changes_position
))
1103 if (output
->crtc_info
)
1105 output
->x
= output
->crtc_info
->crtc_info
->x
;
1106 output
->y
= output
->crtc_info
->crtc_info
->y
;
1116 if (!(output
->changes
& changes_rotation
))
1118 output
->rotation
&= ~0xf;
1119 if (output
->crtc_info
)
1120 output
->rotation
|= (output
->crtc_info
->crtc_info
->rotation
& 0xf);
1122 output
->rotation
= RR_Rotate_0
;
1124 if (!(output
->changes
& changes_reflection
))
1126 output
->rotation
&= ~(RR_Reflect_X
| RR_Reflect_Y
);
1127 if (output
->crtc_info
)
1128 output
->rotation
|= (output
->crtc_info
->crtc_info
->rotation
& (RR_Reflect_X
| RR_Reflect_Y
));
1130 if (!output_can_use_rotation(output
, output
->rotation
))
1131 fatal("output %s cannot use rotation \"%s\" reflection \"%s\"\n", output
->output
.string
,
1132 rotation_name(output
->rotation
), reflection_name(output
->rotation
));
1135 if (!(output
->changes
& changes_gamma
))
1136 set_gamma_info(output
);
1138 /* set transformation */
1139 if (!(output
->changes
& changes_transform
))
1141 if (output
->crtc_info
)
1142 copy_transform(&output
->transform
, &output
->crtc_info
->current_transform
);
1144 init_transform(&output
->transform
);
1148 /* transform was already set for --scale or --transform */
1150 /* for --scale-from, figure out the mode size and compute the transform
1151 * for the target framebuffer area */
1152 if (output
->scale_from_w
> 0 && output
->mode_info
)
1154 double sx
= (double)output
->scale_from_w
/ output
->mode_info
->width
;
1155 double sy
= (double)output
->scale_from_h
/ output
->mode_info
->height
;
1157 printf("scaling %s by %lfx%lf\n", output
->output
.string
, sx
, sy
);
1158 init_transform(&output
->transform
);
1159 output
->transform
.transform
.matrix
[0][0] = XDoubleToFixed(sx
);
1160 output
->transform
.transform
.matrix
[1][1] = XDoubleToFixed(sy
);
1161 output
->transform
.transform
.matrix
[2][2] = XDoubleToFixed(1.0);
1162 if (sx
!= 1 || sy
!= 1)
1163 output
->transform
.filter
= "bilinear";
1165 output
->transform
.filter
= "nearest";
1166 output
->transform
.nparams
= 0;
1167 output
->transform
.params
= NULL
;
1172 if (!(output
->changes
& changes_primary
))
1173 output
->primary
= output_is_primary(output
);
1176 static void get_screen(Bool current
)
1179 fatal("Server RandR version before 1.2\n");
1181 XRRGetScreenSizeRange(dpy
, root
, &minWidth
, &minHeight
, &maxWidth
, &maxHeight
);
1184 res
= XRRGetScreenResourcesCurrent(dpy
, root
);
1186 res
= XRRGetScreenResources(dpy
, root
);
1188 fatal("could not get screen resources");
1191 static void get_crtcs(void)
1195 num_crtcs
= res
->ncrtc
;
1196 crtcs
= calloc(num_crtcs
, sizeof(crtc_t
));
1198 fatal("out of memory\n");
1200 for (c
= 0; c
< res
->ncrtc
; c
++)
1202 XRRCrtcInfo
* crtc_info
= XRRGetCrtcInfo(dpy
, res
, res
->crtcs
[c
]);
1203 XRRCrtcTransformAttributes
* attr
;
1204 XRRPanning
* panning_info
= NULL
;
1209 memset(&zero
, 0, sizeof(zero
));
1210 panning_info
= XRRGetPanning(dpy
, res
, res
->crtcs
[c
]);
1211 zero
.timestamp
= panning_info
->timestamp
;
1212 if (!memcmp(panning_info
, &zero
, sizeof(zero
)))
1214 Xfree(panning_info
);
1215 panning_info
= NULL
;
1219 set_name_xid(&crtcs
[c
].crtc
, res
->crtcs
[c
]);
1220 set_name_index(&crtcs
[c
].crtc
, c
);
1222 fatal("could not get crtc 0x%x information\n", res
->crtcs
[c
]);
1223 crtcs
[c
].crtc_info
= crtc_info
;
1224 crtcs
[c
].panning_info
= panning_info
;
1225 if (crtc_info
->mode
== None
)
1227 crtcs
[c
].mode_info
= NULL
;
1230 crtcs
[c
].rotation
= RR_Rotate_0
;
1232 if (XRRGetCrtcTransform(dpy
, res
->crtcs
[c
], &attr
) && attr
)
1234 set_transform(&crtcs
[c
].current_transform
, &attr
->currentTransform
, attr
->currentFilter
,
1235 attr
->currentParams
, attr
->currentNparams
);
1240 init_transform(&crtcs
[c
].current_transform
);
1242 copy_transform(&crtcs
[c
].pending_transform
, &crtcs
[c
].current_transform
);
1246 static void crtc_add_output(crtc_t
* crtc
, output_t
* output
)
1249 crtc
->outputs
= realloc(crtc
->outputs
, (crtc
->noutput
+ 1) * sizeof(output_t
*));
1252 crtc
->outputs
= malloc(sizeof(output_t
*));
1253 crtc
->x
= output
->x
;
1254 crtc
->y
= output
->y
;
1255 crtc
->rotation
= output
->rotation
;
1256 crtc
->mode_info
= output
->mode_info
;
1257 copy_transform(&crtc
->pending_transform
, &output
->transform
);
1260 fatal("out of memory\n");
1261 crtc
->outputs
[crtc
->noutput
++] = output
;
1264 static void set_crtcs(void)
1268 for (output
= outputs
; output
; output
= output
->next
)
1270 if (!output
->mode_info
)
1272 crtc_add_output(output
->crtc_info
, output
);
1276 static void set_panning(void)
1280 for (output
= outputs
; output
; output
= output
->next
)
1282 if (!output
->crtc_info
)
1284 if (!(output
->changes
& changes_panning
))
1286 if (!output
->crtc_info
->panning_info
)
1287 output
->crtc_info
->panning_info
= malloc(sizeof(XRRPanning
));
1288 memcpy(output
->crtc_info
->panning_info
, &output
->panning
, sizeof(XRRPanning
));
1289 output
->crtc_info
->changing
= 1;
1293 static void set_gamma(void)
1297 for (output
= outputs
; output
; output
= output
->next
)
1301 XRRCrtcGamma
* gamma
;
1303 if (!(output
->changes
& changes_gamma
))
1306 if (!output
->crtc_info
)
1308 fatal("Need crtc to set gamma on.\n");
1312 crtc
= output
->crtc_info
;
1314 size
= XRRGetCrtcGammaSize(dpy
, crtc
->crtc
.xid
);
1318 fatal("Gamma size is 0.\n");
1322 gamma
= XRRAllocGamma(size
);
1325 fatal("Gamma allocation failed.\n");
1329 if (output
->gamma
.red
== 0.0f
&& output
->gamma
.green
== 0.0f
&& output
->gamma
.blue
== 0.0f
)
1330 output
->gamma
.red
= output
->gamma
.green
= output
->gamma
.blue
= 1.0f
;
1332 for (i
= 0; i
< size
; i
++)
1334 if (output
->gamma
.red
== 1.0f
&& output
->brightness
== 1.0f
)
1335 gamma
->red
[i
] = (i
<< 8) + i
;
1338 min(powf((float)i
/ (float)(size
- 1), output
->gamma
.red
) * output
->brightness
, 1.0f
) *
1341 if (output
->gamma
.green
== 1.0f
&& output
->brightness
== 1.0f
)
1342 gamma
->green
[i
] = (i
<< 8) + i
;
1345 min(powf((float)i
/ (float)(size
- 1), output
->gamma
.green
) * output
->brightness
,
1349 if (output
->gamma
.blue
== 1.0f
&& output
->brightness
== 1.0f
)
1350 gamma
->blue
[i
] = (i
<< 8) + i
;
1353 min(powf((float)i
/ (float)(size
- 1), output
->gamma
.blue
) * output
->brightness
, 1.0f
) *
1357 XRRSetCrtcGamma(dpy
, crtc
->crtc
.xid
, gamma
);
1363 static void set_primary(void)
1369 XRRSetOutputPrimary(dpy
, root
, None
);
1373 for (output
= outputs
; output
; output
= output
->next
)
1375 if (!(output
->changes
& changes_primary
))
1377 if (output
->primary
)
1378 XRRSetOutputPrimary(dpy
, root
, output
->output
.xid
);
1383 static Status
crtc_disable(crtc_t
* crtc
)
1386 printf("crtc %d: disable\n", crtc
->crtc
.index
);
1389 return RRSetConfigSuccess
;
1390 return XRRSetCrtcConfig(dpy
, res
, crtc
->crtc
.xid
, CurrentTime
, 0, 0, None
, RR_Rotate_0
, NULL
, 0);
1393 static void crtc_set_transform(crtc_t
* crtc
, transform_t
* transform
)
1397 XRRQueryVersion(dpy
, &major
, &minor
);
1398 if (major
> 1 || (major
== 1 && minor
>= 3))
1399 XRRSetCrtcTransform(dpy
, crtc
->crtc
.xid
, &transform
->transform
, transform
->filter
,
1400 transform
->params
, transform
->nparams
);
1403 static Status
crtc_revert(crtc_t
* crtc
)
1405 XRRCrtcInfo
* crtc_info
= crtc
->crtc_info
;
1408 printf("crtc %d: revert\n", crtc
->crtc
.index
);
1411 return RRSetConfigSuccess
;
1413 if (!equal_transform(&crtc
->current_transform
, &crtc
->pending_transform
))
1414 crtc_set_transform(crtc
, &crtc
->current_transform
);
1415 return XRRSetCrtcConfig(dpy
, res
, crtc
->crtc
.xid
, CurrentTime
, crtc_info
->x
, crtc_info
->y
,
1416 crtc_info
->mode
, crtc_info
->rotation
, crtc_info
->outputs
,
1417 crtc_info
->noutput
);
1420 static Status
crtc_apply(crtc_t
* crtc
)
1422 RROutput
* rr_outputs
;
1427 if (!crtc
->changing
|| !crtc
->mode_info
)
1428 return RRSetConfigSuccess
;
1430 rr_outputs
= calloc(crtc
->noutput
, sizeof(RROutput
));
1433 for (o
= 0; o
< crtc
->noutput
; o
++)
1434 rr_outputs
[o
] = crtc
->outputs
[o
]->output
.xid
;
1435 mode
= crtc
->mode_info
->id
;
1438 printf("crtc %d: %12s %6.1f +%d+%d", crtc
->crtc
.index
, crtc
->mode_info
->name
,
1439 mode_refresh(crtc
->mode_info
), crtc
->x
, crtc
->y
);
1440 for (o
= 0; o
< crtc
->noutput
; o
++)
1441 printf(" \"%s\"", crtc
->outputs
[o
]->output
.string
);
1446 s
= RRSetConfigSuccess
;
1449 if (!equal_transform(&crtc
->current_transform
, &crtc
->pending_transform
))
1450 crtc_set_transform(crtc
, &crtc
->pending_transform
);
1451 s
= XRRSetCrtcConfig(dpy
, res
, crtc
->crtc
.xid
, CurrentTime
, crtc
->x
, crtc
->y
, mode
,
1452 crtc
->rotation
, rr_outputs
, crtc
->noutput
);
1453 if (s
== RRSetConfigSuccess
&& crtc
->panning_info
)
1456 s
= XRRSetPanning(dpy
, res
, crtc
->crtc
.xid
, crtc
->panning_info
);
1458 fatal("panning needs RandR 1.3\n");
1465 static void screen_revert(void)
1468 printf("screen %d: revert\n", screen
);
1472 XRRSetScreenSize(dpy
, root
, DisplayWidth(dpy
, screen
), DisplayHeight(dpy
, screen
),
1473 DisplayWidthMM(dpy
, screen
), DisplayHeightMM(dpy
, screen
));
1476 static void screen_apply(void)
1478 if (fb_width
== DisplayWidth(dpy
, screen
) && fb_height
== DisplayHeight(dpy
, screen
) &&
1479 fb_width_mm
== DisplayWidthMM(dpy
, screen
) && fb_height_mm
== DisplayHeightMM(dpy
, screen
))
1484 printf("screen %d: %dx%d %dx%d mm %6.2fdpi\n", screen
, fb_width
, fb_height
, fb_width_mm
,
1488 XRRSetScreenSize(dpy
, root
, fb_width
, fb_height
, fb_width_mm
, fb_height_mm
);
1491 static void revert(void)
1495 /* first disable all crtcs */
1496 for (c
= 0; c
< res
->ncrtc
; c
++)
1497 crtc_disable(&crtcs
[c
]);
1498 /* next reset screen size */
1500 /* now restore all crtcs */
1501 for (c
= 0; c
< res
->ncrtc
; c
++)
1502 crtc_revert(&crtcs
[c
]);
1506 * uh-oh, something bad happened in the middle of changing
1507 * the configuration. Revert to the previous configuration
1510 static void _X_NORETURN
panic(Status s
, crtc_t
* crtc
)
1512 int c
= crtc
->crtc
.index
;
1517 case RRSetConfigSuccess
:
1518 message
= "succeeded";
1521 message
= "out of memory";
1523 case RRSetConfigFailed
:
1526 case RRSetConfigInvalidConfigTime
:
1527 message
= "invalid config time";
1529 case RRSetConfigInvalidTime
:
1530 message
= "invalid time";
1533 message
= "unknown failure";
1537 fprintf(stderr
, "%s: Configure crtc %d %s\n", program_name
, c
, message
);
1542 static void apply(void)
1548 * Hold the server grabbed while messing with
1549 * the screen so that apps which notice the resize
1550 * event and ask for xinerama information from the server
1551 * receive up-to-date information
1557 * Turn off any crtcs which are to be disabled or which are
1558 * larger than the target size
1560 for (c
= 0; c
< res
->ncrtc
; c
++)
1562 crtc_t
* crtc
= &crtcs
[c
];
1563 XRRCrtcInfo
* crtc_info
= crtc
->crtc_info
;
1565 /* if this crtc is already disabled, skip it */
1566 if (crtc_info
->mode
== None
)
1570 * If this crtc is to be left enabled, make
1571 * sure the old size fits then new screen
1573 if (crtc
->mode_info
)
1575 XRRModeInfo
* old_mode
= find_mode_by_xid(crtc_info
->mode
);
1580 panic(RRSetConfigFailed
, crtc
);
1582 /* old position and size information */
1583 mode_geometry(old_mode
, crtc_info
->rotation
, &crtc
->current_transform
.transform
, &bounds
);
1585 x
= crtc_info
->x
+ bounds
.x1
;
1586 y
= crtc_info
->y
+ bounds
.y1
;
1587 w
= bounds
.x2
- bounds
.x1
;
1588 h
= bounds
.y2
- bounds
.y1
;
1590 /* if it fits, skip it */
1591 if (x
+ w
<= fb_width
&& y
+ h
<= fb_height
)
1593 crtc
->changing
= True
;
1595 s
= crtc_disable(crtc
);
1596 if (s
!= RRSetConfigSuccess
)
1601 * Set the screen size
1609 for (c
= 0; c
< res
->ncrtc
; c
++)
1611 crtc_t
* crtc
= &crtcs
[c
];
1613 s
= crtc_apply(crtc
);
1614 if (s
!= RRSetConfigSuccess
)
1621 * Release the server grab and let all clients
1622 * respond to the updated state
1629 * Use current output state to complete the output list
1631 static void get_outputs(void)
1636 for (o
= 0; o
< res
->noutput
; o
++)
1638 XRROutputInfo
* output_info
= XRRGetOutputInfo(dpy
, res
, res
->outputs
[o
]);
1642 fatal("could not get output 0x%x information\n", res
->outputs
[o
]);
1643 init_name(&output_name
);
1644 set_name_xid(&output_name
, res
->outputs
[o
]);
1645 set_name_index(&output_name
, o
);
1646 set_name_string(&output_name
, output_info
->name
);
1647 output
= find_output(&output_name
);
1650 output
= add_output();
1651 set_name_all(&output
->output
, &output_name
);
1653 * When global --automatic mode is set, turn on connected but off
1654 * outputs, turn off disconnected but on outputs
1658 switch (output_info
->connection
)
1661 if (!output_info
->crtc
)
1663 output
->changes
|= changes_automatic
;
1664 output
->automatic
= True
;
1667 case RR_Disconnected
:
1668 if (output_info
->crtc
)
1670 output
->changes
|= changes_automatic
;
1671 output
->automatic
= True
;
1677 output
->found
= True
;
1680 * Automatic mode -- track connection state and enable/disable outputs
1683 if (output
->automatic
)
1685 switch (output_info
->connection
)
1688 case RR_UnknownConnection
:
1689 if ((!(output
->changes
& changes_mode
)))
1691 set_name_preferred(&output
->mode
);
1692 output
->changes
|= changes_mode
;
1695 case RR_Disconnected
:
1696 if ((!(output
->changes
& changes_mode
)))
1698 set_name_xid(&output
->mode
, None
);
1699 set_name_xid(&output
->crtc
, None
);
1700 output
->changes
|= changes_mode
;
1701 output
->changes
|= changes_crtc
;
1707 set_output_info(output
, res
->outputs
[o
], output_info
);
1709 for (q
= outputs
; q
; q
= q
->next
)
1713 fprintf(stderr
, "warning: output %s not found; ignoring\n", q
->output
.string
);
1718 static void mark_changing_crtcs(void)
1722 for (c
= 0; c
< num_crtcs
; c
++)
1724 crtc_t
* crtc
= &crtcs
[c
];
1728 /* walk old output list (to catch disables) */
1729 for (o
= 0; o
< crtc
->crtc_info
->noutput
; o
++)
1731 output
= find_output_by_xid(crtc
->crtc_info
->outputs
[o
]);
1733 fatal("cannot find output 0x%x\n", crtc
->crtc_info
->outputs
[o
]);
1734 if (output
->changes
)
1735 crtc
->changing
= True
;
1737 /* walk new output list */
1738 for (o
= 0; o
< crtc
->noutput
; o
++)
1740 output
= crtc
->outputs
[o
];
1741 if (output
->changes
)
1742 crtc
->changing
= True
;
1748 * Test whether 'crtc' can be used for 'output'
1750 static Bool
check_crtc_for_output(crtc_t
* crtc
, output_t
* output
)
1756 for (c
= 0; c
< output
->output_info
->ncrtc
; c
++)
1757 if (output
->output_info
->crtcs
[c
] == crtc
->crtc
.xid
)
1759 if (c
== output
->output_info
->ncrtc
)
1761 for (other
= outputs
; other
; other
= other
->next
)
1763 if (other
== output
)
1766 if (other
->mode_info
== NULL
)
1769 if (other
->crtc_info
!= crtc
)
1772 /* see if the output connected to the crtc can clone to this output */
1773 for (l
= 0; l
< output
->output_info
->nclone
; l
++)
1774 if (output
->output_info
->clones
[l
] == other
->output
.xid
)
1776 /* not on the list, can't clone */
1777 if (l
== output
->output_info
->nclone
)
1783 /* make sure the state matches */
1784 if (crtc
->mode_info
!= output
->mode_info
)
1786 if (crtc
->x
!= output
->x
)
1788 if (crtc
->y
!= output
->y
)
1790 if (crtc
->rotation
!= output
->rotation
)
1792 if (!equal_transform(&crtc
->current_transform
, &output
->transform
))
1795 else if (crtc
->crtc_info
->noutput
)
1797 /* make sure the state matches the already used state */
1798 XRRModeInfo
* mode
= find_mode_by_xid(crtc
->crtc_info
->mode
);
1800 if (mode
!= output
->mode_info
)
1802 if (crtc
->crtc_info
->x
!= output
->x
)
1804 if (crtc
->crtc_info
->y
!= output
->y
)
1806 if (crtc
->crtc_info
->rotation
!= output
->rotation
)
1812 static crtc_t
* find_crtc_for_output(output_t
* output
)
1816 for (c
= 0; c
< output
->output_info
->ncrtc
; c
++)
1820 crtc
= find_crtc_by_xid(output
->output_info
->crtcs
[c
]);
1822 fatal("cannot find crtc 0x%x\n", output
->output_info
->crtcs
[c
]);
1824 if (check_crtc_for_output(crtc
, output
))
1830 static void set_positions(void)
1841 for (output
= outputs
; output
; output
= output
->next
)
1844 name_t relation_name
;
1846 if (!(output
->changes
& changes_relation
))
1849 if (output
->mode_info
== NULL
)
1852 init_name(&relation_name
);
1853 set_name_string(&relation_name
, output
->relative_to
);
1854 relation
= find_output(&relation_name
);
1856 fatal("cannot find output \"%s\"\n", output
->relative_to
);
1858 if (relation
->mode_info
== NULL
)
1862 output
->changes
|= changes_position
;
1867 * Make sure the dependent object has been set in place
1869 if ((relation
->changes
& changes_relation
) && !(relation
->changes
& changes_position
))
1875 switch (output
->relation
)
1877 case relation_left_of
:
1878 output
->y
= relation
->y
;
1879 output
->x
= relation
->x
- mode_width(output
->mode_info
, output
->rotation
);
1881 case relation_right_of
:
1882 output
->y
= relation
->y
;
1883 output
->x
= relation
->x
+ mode_width(relation
->mode_info
, relation
->rotation
);
1885 case relation_above
:
1886 output
->x
= relation
->x
;
1887 output
->y
= relation
->y
- mode_height(output
->mode_info
, output
->rotation
);
1889 case relation_below
:
1890 output
->x
= relation
->x
;
1891 output
->y
= relation
->y
+ mode_height(relation
->mode_info
, relation
->rotation
);
1893 case relation_same_as
:
1894 output
->x
= relation
->x
;
1895 output
->y
= relation
->y
;
1897 output
->changes
|= changes_position
;
1903 fatal("loop in relative position specifications\n");
1907 * Now normalize positions so the upper left corner of all outputs is at 0,0
1911 for (output
= outputs
; output
; output
= output
->next
)
1913 if (output
->mode_info
== NULL
)
1916 if (output
->x
< min_x
)
1918 if (output
->y
< min_y
)
1923 /* move all outputs */
1924 for (output
= outputs
; output
; output
= output
->next
)
1926 if (output
->mode_info
== NULL
)
1931 output
->changes
|= changes_position
;
1936 static void set_screen_size(void)
1939 Bool fb_specified
= fb_width
!= 0 && fb_height
!= 0;
1941 for (output
= outputs
; output
; output
= output
->next
)
1943 XRRModeInfo
* mode_info
= output
->mode_info
;
1950 mode_geometry(mode_info
, output
->rotation
, &output
->transform
.transform
, &bounds
);
1951 x
= output
->x
+ bounds
.x1
;
1952 y
= output
->y
+ bounds
.y1
;
1953 w
= bounds
.x2
- bounds
.x1
;
1954 h
= bounds
.y2
- bounds
.y1
;
1955 /* make sure output fits in specified size */
1958 if (x
+ w
> fb_width
|| y
+ h
> fb_height
)
1959 warning("specified screen %dx%d not large enough for output %s (%dx%d+%d+%d)\n", fb_width
,
1960 fb_height
, output
->output
.string
, w
, h
, x
, y
);
1962 /* fit fb to output */
1966 if (x
+ w
> fb_width
)
1968 if (y
+ h
> fb_height
)
1970 if (output
->changes
& changes_panning
)
1971 pan
= &output
->panning
;
1973 pan
= output
->crtc_info
? output
->crtc_info
->panning_info
: NULL
;
1974 if (pan
&& pan
->left
+ pan
->width
> (unsigned int)fb_width
)
1975 fb_width
= pan
->left
+ pan
->width
;
1976 if (pan
&& pan
->top
+ pan
->height
> (unsigned int)fb_height
)
1977 fb_height
= pan
->top
+ pan
->height
;
1981 if (fb_width
> maxWidth
|| fb_height
> maxHeight
)
1982 fatal("screen cannot be larger than %dx%d (desired size %dx%d)\n", maxWidth
, maxHeight
,
1983 fb_width
, fb_height
);
1986 if (fb_width
< minWidth
|| fb_height
< minHeight
)
1987 fatal("screen must be at least %dx%d\n", minWidth
, minHeight
);
1991 if (fb_width
< minWidth
)
1992 fb_width
= minWidth
;
1993 if (fb_height
< minHeight
)
1994 fb_height
= minHeight
;
1999 static void disable_outputs(output_t
* outputs
)
2003 outputs
->crtc_info
= NULL
;
2004 outputs
= outputs
->next
;
2009 * find the best mapping from output to crtc available
2011 static int pick_crtcs_score(output_t
* outputs
)
2024 outputs
= outputs
->next
;
2026 * Score with this output disabled
2028 output
->crtc_info
= NULL
;
2029 best_score
= pick_crtcs_score(outputs
);
2030 if (output
->mode_info
== NULL
)
2035 * Now score with this output any valid crtc
2037 for (c
= 0; c
< output
->output_info
->ncrtc
; c
++)
2041 crtc
= find_crtc_by_xid(output
->output_info
->crtcs
[c
]);
2043 fatal("cannot find crtc 0x%x\n", output
->output_info
->crtcs
[c
]);
2045 /* reset crtc allocation for following outputs */
2046 disable_outputs(outputs
);
2047 if (!check_crtc_for_output(crtc
, output
))
2051 /* slight preference for existing connections */
2052 if (crtc
== output
->current_crtc_info
)
2055 output
->crtc_info
= crtc
;
2056 score
= my_score
+ pick_crtcs_score(outputs
);
2057 if (score
> best_score
)
2063 if (output
->crtc_info
!= best_crtc
)
2064 output
->crtc_info
= best_crtc
;
2066 * Reset other outputs based on this one using the best crtc
2068 (void)pick_crtcs_score(outputs
);
2074 * Pick crtcs for any changing outputs that don't have one
2076 static void pick_crtcs(void)
2081 * First try to match up newly enabled outputs with spare crtcs
2083 for (output
= outputs
; output
; output
= output
->next
)
2085 if (output
->changes
&& output
->mode_info
)
2087 if (output
->crtc_info
)
2089 if (output
->crtc_info
->crtc_info
->noutput
> 0 &&
2090 (output
->crtc_info
->crtc_info
->noutput
> 1 ||
2091 output
!= find_output_by_xid(output
->crtc_info
->crtc_info
->outputs
[0])))
2096 output
->crtc_info
= find_crtc_for_output(output
);
2097 if (!output
->crtc_info
)
2108 * When the simple way fails, see if there is a way
2109 * to swap crtcs around and make things work
2111 for (output
= outputs
; output
; output
= output
->next
)
2112 output
->current_crtc_info
= output
->crtc_info
;
2113 pick_crtcs_score(outputs
);
2114 for (output
= outputs
; output
; output
= output
->next
)
2116 if (output
->mode_info
&& !output
->crtc_info
)
2117 fatal("cannot find crtc for output %s\n", output
->output
.string
);
2118 if (!output
->changes
&& output
->crtc_info
!= output
->current_crtc_info
)
2119 output
->changes
|= changes_crtc
;
2123 static int check_strtol(char* s
)
2126 int result
= strtol(s
, &endptr
, 10);
2132 static double check_strtod(char* s
)
2135 double result
= strtod(s
, &endptr
);
2141 int main(int argc
, char** argv
)
2143 XRRScreenSize
* sizes
;
2144 XRRScreenConfiguration
* sc
;
2148 Status status
= RRSetConfigFailed
;
2151 int action_requested
= False
;
2152 Rotation rotation
, current_rotation
, rotations
;
2153 XRRScreenChangeNotifyEvent event
;
2154 XRRScreenChangeNotifyEvent
* sce
;
2155 char* display_name
= NULL
;
2157 SizeID current_size
;
2163 Bool version
= False
;
2164 int event_base
, error_base
;
2166 int width
= 0, height
= 0;
2167 Bool have_pixel_size
= False
;
2169 output_t
* output
= NULL
;
2170 Bool setit_1_2
= False
;
2171 Bool query_1_2
= False
;
2172 Bool modeit
= False
;
2173 Bool propit
= False
;
2174 Bool query_1
= False
;
2176 Bool current
= False
;
2178 program_name
= argv
[0];
2179 for (i
= 1; i
< argc
; i
++)
2181 if (!strcmp("-display", argv
[i
]) || !strcmp("-d", argv
[i
]))
2185 display_name
= argv
[i
];
2188 if (!strcmp("-help", argv
[i
]))
2191 action_requested
= True
;
2194 if (!strcmp("--verbose", argv
[i
]))
2199 if (!strcmp("--dryrun", argv
[i
]))
2205 if (!strcmp("--nograb", argv
[i
]))
2207 grab_server
= False
;
2210 if (!strcmp("--current", argv
[i
]))
2216 if (!strcmp("-s", argv
[i
]) || !strcmp("--size", argv
[i
]))
2220 if (sscanf(argv
[i
], "%dx%d", &width
, &height
) == 2)
2222 have_pixel_size
= True
;
2226 size
= check_strtol(argv
[i
]);
2231 action_requested
= True
;
2235 if (!strcmp("-r", argv
[i
]) || !strcmp("--rate", argv
[i
]) || !strcmp("--refresh", argv
[i
]))
2239 rate
= check_strtod(argv
[i
]);
2243 output
->refresh
= rate
;
2244 output
->changes
|= changes_refresh
;
2247 action_requested
= True
;
2251 if (!strcmp("-v", argv
[i
]) || !strcmp("--version", argv
[i
]))
2254 action_requested
= True
;
2258 if (!strcmp("-x", argv
[i
]))
2260 reflection
|= RR_Reflect_X
;
2262 action_requested
= True
;
2265 if (!strcmp("-y", argv
[i
]))
2267 reflection
|= RR_Reflect_Y
;
2269 action_requested
= True
;
2272 if (!strcmp("--screen", argv
[i
]))
2276 screen
= check_strtol(argv
[i
]);
2281 if (!strcmp("-q", argv
[i
]) || !strcmp("--query", argv
[i
]))
2286 if (!strcmp("-o", argv
[i
]) || !strcmp("--orientation", argv
[i
]))
2291 dirind
= strtol(argv
[i
], &endptr
, 10);
2292 if (argv
[i
] == endptr
)
2294 for (dirind
= 0; dirind
< 4; dirind
++)
2296 if (strcmp(direction
[dirind
], argv
[i
]) == 0)
2299 if ((dirind
< 0) || (dirind
> 3))
2304 action_requested
= True
;
2307 if (!strcmp("--prop", argv
[i
]) || !strcmp("--props", argv
[i
]) ||
2308 !strcmp("--madprops", argv
[i
]) || !strcmp("--properties", argv
[i
]))
2312 action_requested
= True
;
2315 if (!strcmp("--output", argv
[i
]))
2320 output
= find_output_by_name(argv
[i
]);
2323 output
= add_output();
2324 set_name(&output
->output
, argv
[i
], name_string
| name_xid
);
2328 action_requested
= True
;
2331 if (!strcmp("--crtc", argv
[i
]))
2337 set_name(&output
->crtc
, argv
[i
], name_xid
| name_index
);
2338 output
->changes
|= changes_crtc
;
2341 if (!strcmp("--mode", argv
[i
]))
2347 set_name(&output
->mode
, argv
[i
], name_string
| name_xid
);
2348 output
->changes
|= changes_mode
;
2351 if (!strcmp("--preferred", argv
[i
]))
2355 set_name_preferred(&output
->mode
);
2356 output
->changes
|= changes_mode
;
2359 if (!strcmp("--pos", argv
[i
]))
2365 if (sscanf(argv
[i
], "%dx%d", &output
->x
, &output
->y
) != 2)
2367 output
->changes
|= changes_position
;
2370 if (!strcmp("--rotation", argv
[i
]) || !strcmp("--rotate", argv
[i
]))
2376 for (dirind
= 0; dirind
< 4; dirind
++)
2378 if (strcmp(direction
[dirind
], argv
[i
]) == 0)
2383 output
->rotation
&= ~0xf;
2384 output
->rotation
|= 1 << dirind
;
2385 output
->changes
|= changes_rotation
;
2388 if (!strcmp("--reflect", argv
[i
]) || !strcmp("--reflection", argv
[i
]))
2394 for (dirind
= 0; dirind
< 4; dirind
++)
2396 if (strcmp(reflections
[dirind
], argv
[i
]) == 0)
2401 output
->rotation
&= ~(RR_Reflect_X
| RR_Reflect_Y
);
2402 output
->rotation
|= dirind
* RR_Reflect_X
;
2403 output
->changes
|= changes_reflection
;
2406 if (!strcmp("--left-of", argv
[i
]))
2412 output
->relation
= relation_left_of
;
2413 output
->relative_to
= argv
[i
];
2414 output
->changes
|= changes_relation
;
2417 if (!strcmp("--right-of", argv
[i
]))
2423 output
->relation
= relation_right_of
;
2424 output
->relative_to
= argv
[i
];
2425 output
->changes
|= changes_relation
;
2428 if (!strcmp("--above", argv
[i
]))
2434 output
->relation
= relation_above
;
2435 output
->relative_to
= argv
[i
];
2436 output
->changes
|= changes_relation
;
2439 if (!strcmp("--below", argv
[i
]))
2445 output
->relation
= relation_below
;
2446 output
->relative_to
= argv
[i
];
2447 output
->changes
|= changes_relation
;
2450 if (!strcmp("--same-as", argv
[i
]))
2456 output
->relation
= relation_same_as
;
2457 output
->relative_to
= argv
[i
];
2458 output
->changes
|= changes_relation
;
2461 if (!strcmp("--panning", argv
[i
]))
2468 pan
= &output
->panning
;
2469 switch (sscanf(argv
[i
], "%dx%d+%d+%d/%dx%d+%d+%d/%d/%d/%d/%d", &pan
->width
, &pan
->height
,
2470 &pan
->left
, &pan
->top
, &pan
->track_width
, &pan
->track_height
, &pan
->track_left
,
2471 &pan
->track_top
, &pan
->border_left
, &pan
->border_top
, &pan
->border_right
,
2472 &pan
->border_bottom
))
2475 pan
->left
= pan
->top
= 0;
2477 __attribute__((fallthrough
));
2479 pan
->track_left
= pan
->track_top
= pan
->track_width
= pan
->track_height
= 0;
2481 __attribute__((fallthrough
));
2483 pan
->border_left
= pan
->border_top
= pan
->border_right
= pan
->border_bottom
= 0;
2485 __attribute__((fallthrough
));
2491 output
->changes
|= changes_panning
;
2494 if (!strcmp("--gamma", argv
[i
]))
2500 if (sscanf(argv
[i
], "%f:%f:%f", &output
->gamma
.red
, &output
->gamma
.green
,
2501 &output
->gamma
.blue
) != 3)
2503 output
->changes
|= changes_gamma
;
2507 if (!strcmp("--brightness", argv
[i
]))
2513 if (sscanf(argv
[i
], "%f", &output
->brightness
) != 1)
2515 output
->changes
|= changes_gamma
;
2519 if (!strcmp("--primary", argv
[i
]))
2523 output
->changes
|= changes_primary
;
2524 output
->primary
= True
;
2528 if (!strcmp("--noprimary", argv
[i
]))
2534 if (!strcmp("--set", argv
[i
]))
2536 output_prop_t
* prop
;
2539 prop
= malloc(sizeof(output_prop_t
));
2540 prop
->next
= output
->props
;
2541 output
->props
= prop
;
2544 prop
->name
= argv
[i
];
2547 prop
->value
= argv
[i
];
2549 output
->changes
|= changes_property
;
2553 if (!strcmp("--scale", argv
[i
]))
2560 if (sscanf(argv
[i
], "%lfx%lf", &sx
, &sy
) != 2)
2562 init_transform(&output
->transform
);
2563 output
->transform
.transform
.matrix
[0][0] = XDoubleToFixed(sx
);
2564 output
->transform
.transform
.matrix
[1][1] = XDoubleToFixed(sy
);
2565 output
->transform
.transform
.matrix
[2][2] = XDoubleToFixed(1.0);
2566 if (sx
!= 1 || sy
!= 1)
2567 output
->transform
.filter
= "bilinear";
2569 output
->transform
.filter
= "nearest";
2570 output
->transform
.nparams
= 0;
2571 output
->transform
.params
= NULL
;
2572 output
->changes
|= changes_transform
;
2575 if (!strcmp("--scale-from", argv
[i
]))
2582 if (sscanf(argv
[i
], "%dx%d", &w
, &h
) != 2)
2584 if (w
<= 0 || h
<= 0)
2586 output
->scale_from_w
= w
;
2587 output
->scale_from_h
= h
;
2588 output
->changes
|= changes_transform
;
2591 if (!strcmp("--transform", argv
[i
]))
2593 double transform
[3][3];
2599 init_transform(&output
->transform
);
2600 if (strcmp(argv
[i
], "none") != 0)
2602 if (sscanf(argv
[i
], "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf", &transform
[0][0],
2603 &transform
[0][1], &transform
[0][2], &transform
[1][0], &transform
[1][1],
2604 &transform
[1][2], &transform
[2][0], &transform
[2][1], &transform
[2][2]) != 9)
2606 init_transform(&output
->transform
);
2607 for (k
= 0; k
< 3; k
++)
2608 for (l
= 0; l
< 3; l
++)
2610 output
->transform
.transform
.matrix
[k
][l
] = XDoubleToFixed(transform
[k
][l
]);
2612 output
->transform
.filter
= "bilinear";
2613 output
->transform
.nparams
= 0;
2614 output
->transform
.params
= NULL
;
2616 output
->changes
|= changes_transform
;
2619 if (!strcmp("--off", argv
[i
]))
2623 set_name_xid(&output
->mode
, None
);
2624 set_name_xid(&output
->crtc
, None
);
2625 output
->changes
|= changes_mode
;
2628 if (!strcmp("--fb", argv
[i
]))
2632 if (sscanf(argv
[i
], "%dx%d", &fb_width
, &fb_height
) != 2)
2635 action_requested
= True
;
2638 if (!strcmp("--fbmm", argv
[i
]))
2642 if (sscanf(argv
[i
], "%dx%d", &fb_width_mm
, &fb_height_mm
) != 2)
2645 action_requested
= True
;
2648 if (!strcmp("--dpi", argv
[i
]))
2653 dpi
= strtod(argv
[i
], &strtod_error
);
2654 if (argv
[i
] == strtod_error
)
2657 dpi_output
= argv
[i
];
2660 action_requested
= True
;
2663 if (!strcmp("--auto", argv
[i
]))
2667 output
->automatic
= True
;
2668 output
->changes
|= changes_automatic
;
2673 action_requested
= True
;
2676 if (!strcmp("--q12", argv
[i
]))
2681 if (!strcmp("--q1", argv
[i
]))
2686 if (!strcmp("--newmode", argv
[i
]))
2688 umode_t
* m
= malloc(sizeof(umode_t
));
2694 m
->mode
.name
= argv
[i
];
2695 m
->mode
.nameLength
= strlen(argv
[i
]);
2697 clock
= check_strtod(argv
[i
++]);
2698 m
->mode
.dotClock
= clock
* 1e6
;
2700 m
->mode
.width
= check_strtol(argv
[i
++]);
2701 m
->mode
.hSyncStart
= check_strtol(argv
[i
++]);
2702 m
->mode
.hSyncEnd
= check_strtol(argv
[i
++]);
2703 m
->mode
.hTotal
= check_strtol(argv
[i
++]);
2704 m
->mode
.height
= check_strtol(argv
[i
++]);
2705 m
->mode
.vSyncStart
= check_strtol(argv
[i
++]);
2706 m
->mode
.vSyncEnd
= check_strtol(argv
[i
++]);
2707 m
->mode
.vTotal
= check_strtol(argv
[i
++]);
2708 m
->mode
.modeFlags
= 0;
2713 for (f
= 0; mode_flags
[f
].string
; f
++)
2714 if (!strcasecmp(mode_flags
[f
].string
, argv
[i
]))
2717 if (!mode_flags
[f
].string
)
2719 m
->mode
.modeFlags
|= mode_flags
[f
].flag
;
2723 m
->action
= umode_create
;
2726 action_requested
= True
;
2729 if (!strcmp("--rmmode", argv
[i
]))
2731 umode_t
* m
= malloc(sizeof(umode_t
));
2735 set_name(&m
->name
, argv
[i
], name_string
| name_xid
);
2736 m
->action
= umode_destroy
;
2740 action_requested
= True
;
2743 if (!strcmp("--addmode", argv
[i
]))
2745 umode_t
* m
= malloc(sizeof(umode_t
));
2749 set_name(&m
->output
, argv
[i
], name_string
| name_xid
);
2752 set_name(&m
->name
, argv
[i
], name_string
| name_xid
);
2753 m
->action
= umode_add
;
2757 action_requested
= True
;
2760 if (!strcmp("--delmode", argv
[i
]))
2762 umode_t
* m
= malloc(sizeof(umode_t
));
2766 set_name(&m
->output
, argv
[i
], name_string
| name_xid
);
2769 set_name(&m
->name
, argv
[i
], name_string
| name_xid
);
2770 m
->action
= umode_delete
;
2774 action_requested
= True
;
2779 if (!action_requested
)
2784 if (setit
&& !setit_1_2
)
2790 printf("xrandr program version " VERSION "\n");
2793 dpy
= XOpenDisplay(display_name
);
2797 fprintf(stderr
, "Can't open display %s\n", XDisplayName(display_name
));
2801 screen
= DefaultScreen(dpy
);
2802 if (screen
>= ScreenCount(dpy
))
2804 fprintf(stderr
, "Invalid screen number %d (display has %d)\n", screen
, ScreenCount(dpy
));
2808 root
= RootWindow(dpy
, screen
);
2810 if (!XRRQueryExtension(dpy
, &event_base
, &error_base
) || !XRRQueryVersion(dpy
, &major
, &minor
))
2812 fprintf(stderr
, "RandR extension missing\n");
2815 if (major
> 1 || (major
== 1 && minor
>= 2))
2817 if (major
> 1 || (major
== 1 && minor
>= 3))
2820 if (has_1_2
&& modeit
)
2824 get_screen(current
);
2828 for (m
= umodes
; m
; m
= m
->next
)
2836 XRRCreateMode(dpy
, root
, &m
->mode
);
2839 e
= find_mode(&m
->name
, 0);
2841 fatal("cannot find mode \"%s\"\n", m
->name
.string
);
2842 XRRDestroyMode(dpy
, e
->id
);
2845 o
= find_output(&m
->output
);
2847 fatal("cannot find output \"%s\"\n", m
->output
.string
);
2848 e
= find_mode(&m
->name
, 0);
2850 fatal("cannot find mode \"%s\"\n", m
->name
.string
);
2851 XRRAddOutputMode(dpy
, o
->output
.xid
, e
->id
);
2854 o
= find_output(&m
->output
);
2856 fatal("cannot find output \"%s\"\n", m
->output
.string
);
2857 e
= find_mode(&m
->name
, 0);
2859 fatal("cannot find mode \"%s\"\n", m
->name
.string
);
2860 XRRDeleteOutputMode(dpy
, o
->output
.xid
, e
->id
);
2870 if (has_1_2
&& propit
)
2873 get_screen(current
);
2877 for (output
= outputs
; output
; output
= output
->next
)
2879 output_prop_t
* prop
;
2881 for (prop
= output
->props
; prop
; prop
= prop
->next
)
2883 Atom name
= XInternAtom(dpy
, prop
->name
, False
);
2886 unsigned char* data
;
2889 unsigned long ulong_value
;
2890 unsigned char* prop_data
;
2892 unsigned long nitems
, bytes_after
;
2894 XRRPropertyInfo
* propinfo
;
2896 type
= AnyPropertyType
;
2898 if (XRRGetOutputProperty(dpy
, output
->output
.xid
, name
, 0, 100, False
, False
,
2899 AnyPropertyType
, &actual_type
, &actual_format
, &nitems
,
2900 &bytes_after
, &prop_data
) == Success
&&
2902 (propinfo
= XRRQueryOutputProperty(dpy
, output
->output
.xid
, name
)))
2905 format
= actual_format
;
2908 if ((type
== XA_INTEGER
|| type
== AnyPropertyType
) &&
2909 (sscanf(prop
->value
, "%d", &int_value
) == 1 ||
2910 sscanf(prop
->value
, "0x%x", &int_value
) == 1))
2913 ulong_value
= int_value
;
2914 data
= (unsigned char*)&ulong_value
;
2918 else if (type
== XA_ATOM
)
2920 ulong_value
= XInternAtom(dpy
, prop
->value
, False
);
2921 data
= (unsigned char*)&ulong_value
;
2924 else if ((type
== XA_STRING
|| type
== AnyPropertyType
))
2927 data
= (unsigned char*)prop
->value
;
2928 nelements
= strlen(prop
->value
);
2933 XRRChangeOutputProperty(dpy
, output
->output
.xid
, name
, type
, format
, PropModeReplace
, data
,
2945 get_screen(current
);
2954 * Assign outputs to crtcs
2959 * Mark changing crtcs
2961 mark_changing_crtcs();
2964 * If an output was specified to track dpi, use it
2968 output_t
* output
= find_output_by_name(dpi_output
);
2969 XRROutputInfo
* output_info
;
2970 XRRModeInfo
* mode_info
;
2972 fatal("Cannot find output %s\n", dpi_output
);
2973 output_info
= output
->output_info
;
2974 mode_info
= output
->mode_info
;
2975 if (output_info
&& mode_info
&& output_info
->mm_height
)
2978 * When this output covers the whole screen, just use
2979 * the known physical size
2981 if ((unsigned int)fb_width
== mode_info
->width
&&
2982 (unsigned int)fb_height
== mode_info
->height
)
2984 fb_width_mm
= output_info
->mm_width
;
2985 fb_height_mm
= output_info
->mm_height
;
2989 dpi
= (25.4 * mode_info
->height
) / output_info
->mm_height
;
2995 * Compute physical screen size
2997 if (fb_width_mm
== 0 || fb_height_mm
== 0)
2999 if (fb_width
!= DisplayWidth(dpy
, screen
) || fb_height
!= DisplayHeight(dpy
, screen
) ||
3003 dpi
= (25.4 * DisplayHeight(dpy
, screen
)) / DisplayHeightMM(dpy
, screen
);
3005 fb_width_mm
= (25.4 * fb_width
) / dpi
;
3006 fb_height_mm
= (25.4 * fb_height
) / dpi
;
3010 fb_width_mm
= DisplayWidthMM(dpy
, screen
);
3011 fb_height_mm
= DisplayHeightMM(dpy
, screen
);
3021 * Set gamma on crtc's that belong to the outputs.
3026 * Now apply all of the changes
3033 if (query_1_2
|| (query
&& has_1_2
&& !query_1
))
3037 #define ModeShown 0x80000000
3039 get_screen(current
);
3043 printf("<screen id=\"%d\" minimum_w=\"%d\" minimum_h=\"%d\" current_w=\"%d\" current_h=\"%d\" "
3044 "maximum_w=\"%d\" maximum_h=\"%d\">\n",
3045 screen
, minWidth
, minHeight
, DisplayWidth(dpy
, screen
), DisplayHeight(dpy
, screen
),
3046 maxWidth
, maxHeight
);
3048 for (output
= outputs
; output
; output
= output
->next
)
3050 XRROutputInfo
* output_info
= output
->output_info
;
3051 crtc_t
* crtc
= output
->crtc_info
;
3052 XRRCrtcInfo
* crtc_info
= crtc
? crtc
->crtc_info
: NULL
;
3053 XRRModeInfo
* mode
= output
->mode_info
;
3057 // Rotation rotations = output_rotations (output);
3059 printf(" <output name=\"%s\" connected=\"%s\"", output_info
->name
,
3060 connection
[output_info
->connection
]);
3065 printf(" w=\"%d\" h=\"%d\" x=\"%d\" y=\"%d\" crtc=\"%d\"", crtc_info
->width
,
3066 crtc_info
->height
, crtc_info
->x
, crtc_info
->y
, crtc
->crtc
.index
);
3070 printf(" w=\"%d\" h=\"%d\" x=\"%d\" y=\"%d\"", mode
->width
, mode
->height
, output
->x
,
3074 printf(" id=\"%lx\"", mode
->id
);
3075 if (output
->rotation
!= RR_Rotate_0
|| verbose
)
3077 printf(" rotation=\"%s\"", rotation_name(output
->rotation
));
3078 if (output
->rotation
& (RR_Reflect_X
| RR_Reflect_Y
))
3079 printf(" reflection=\"%s\"", reflection_name(output
->rotation
));
3083 if (rotations != RR_Rotate_0 || verbose)
3087 for (i = 0; i < 4; i ++) {
3088 if ((rotations >> i) & 1) {
3089 if (!first) printf (" "); first = False;
3090 printf("%s", direction[i]);
3093 if (rotations & RR_Reflect_X)
3095 if (!first) printf (" "); first = False;
3098 if (rotations & RR_Reflect_Y)
3100 if (!first) printf (" ");
3109 printf(" wmm=\"%d\" hmm=\"%d\"", (int)output_info
->mm_width
, (int)output_info
->mm_height
);
3112 if (crtc
&& crtc
->panning_info
&& crtc
->panning_info
->width
> 0)
3114 XRRPanning
* pan
= crtc
->panning_info
;
3115 printf(" panning %dx%d+%d+%d", pan
->width
, pan
->height
, pan
->left
, pan
->top
);
3116 if ((pan
->track_width
!= 0 &&
3117 (pan
->track_left
!= pan
->left
|| pan
->track_width
!= pan
->width
||
3118 pan
->border_left
!= 0 || pan
->border_right
!= 0)) ||
3119 (pan
->track_height
!= 0 &&
3120 (pan
->track_top
!= pan
->top
|| pan
->track_height
!= pan
->height
||
3121 pan
->border_top
!= 0 || pan
->border_bottom
!= 0)))
3122 printf(" tracking %dx%d+%d+%d border %d/%d/%d/%d", pan
->track_width
, pan
->track_height
,
3123 pan
->track_left
, pan
->track_top
, pan
->border_left
, pan
->border_top
,
3124 pan
->border_right
, pan
->border_bottom
);
3130 printf("\tIdentifier: 0x%x\n", (int)output
->output
.xid
);
3131 printf("\tTimestamp: %d\n", (int)output_info
->timestamp
);
3132 printf("\tSubpixel: %s\n", order
[output_info
->subpixel_order
]);
3133 if (output
->gamma
.red
!= 0.0f
&& output
->gamma
.green
!= 0.0f
&& output
->gamma
.blue
!= 0.0f
)
3135 printf("\tGamma: %#.2g:%#.2g:%#.2g\n", (double)output
->gamma
.red
,
3136 (double)output
->gamma
.green
, (double)output
->gamma
.blue
);
3137 printf("\tBrightness: %#.2g\n", (double)output
->brightness
);
3139 printf("\tClones: ");
3140 for (j
= 0; j
< output_info
->nclone
; j
++)
3142 output_t
* clone
= find_output_by_xid(output_info
->clones
[j
]);
3145 printf(" %s", clone
->output
.string
);
3148 if (output
->crtc_info
)
3149 printf("\tCRTC: %d\n", output
->crtc_info
->crtc
.index
);
3150 printf("\tCRTCs: ");
3151 for (j
= 0; j
< output_info
->ncrtc
; j
++)
3153 crtc_t
* crtc
= find_crtc_by_xid(output_info
->crtcs
[j
]);
3155 printf(" %d", crtc
->crtc
.index
);
3158 if (output
->crtc_info
&& output
->crtc_info
->panning_info
)
3160 XRRPanning
* pan
= output
->crtc_info
->panning_info
;
3161 printf("\tPanning: %dx%d+%d+%d\n", pan
->width
, pan
->height
, pan
->left
, pan
->top
);
3162 printf("\tTracking: %dx%d+%d+%d\n", pan
->track_width
, pan
->track_height
,
3163 pan
->track_left
, pan
->track_top
);
3164 printf("\tBorder: %d/%d/%d/%d\n", pan
->border_left
, pan
->border_top
,
3165 pan
->border_right
, pan
->border_bottom
);
3172 printf("\tTransform: ");
3173 for (y
= 0; y
< 3; y
++)
3175 for (x
= 0; x
< 3; x
++)
3176 printf(" %f", XFixedToDouble(output
->transform
.transform
.matrix
[y
][x
]));
3180 if (output
->transform
.filter
)
3181 printf("\n\t filter: %s", output
->transform
.filter
);
3184 if (verbose
|| properties
)
3186 props
= XRRListOutputProperties(dpy
, output
->output
.xid
, &nprop
);
3187 for (j
= 0; j
< nprop
; j
++)
3189 unsigned char* prop
;
3191 unsigned long nitems
, bytes_after
;
3193 XRRPropertyInfo
* propinfo
;
3195 XRRGetOutputProperty(dpy
, output
->output
.xid
, props
[j
], 0, 100, False
, False
,
3196 AnyPropertyType
, &actual_type
, &actual_format
, &nitems
, &bytes_after
,
3199 propinfo
= XRRQueryOutputProperty(dpy
, output
->output
.xid
, props
[j
]);
3201 if (actual_type
== XA_INTEGER
&& actual_format
== 8)
3205 printf("\t%s:\n", XGetAtomName(dpy
, props
[j
]));
3206 for (k
= 0; k
< (long)nitems
; k
++)
3210 printf("%02x", (unsigned char)prop
[k
]);
3215 else if (actual_type
== XA_INTEGER
&& actual_format
== 32)
3217 printf("\t%s: ", XGetAtomName(dpy
, props
[j
]));
3218 for (k
= 0; k
< (long)nitems
; k
++)
3222 printf("%d (0x%08x)", (int)((INT32
*)prop
)[k
], (int)((INT32
*)prop
)[k
]);
3225 if (propinfo
->range
&& propinfo
->num_values
> 0)
3229 printf("\trange%s: ", (propinfo
->num_values
== 2) ? "" : "s");
3231 for (k
= 0; k
< propinfo
->num_values
/ 2; k
++)
3232 printf(" (%d,%d)", (int)propinfo
->values
[k
* 2], (int)propinfo
->values
[k
* 2 + 1]);
3237 else if (actual_type
== XA_ATOM
&& actual_format
== 32)
3239 printf("\t%s:", XGetAtomName(dpy
, props
[j
]));
3240 for (k
= 0; k
< (long)nitems
; k
++)
3242 if (k
> 0 && (k
& 1) == 0)
3244 printf("\t%s", XGetAtomName(dpy
, ((Atom
*)prop
)[k
]));
3247 if (!propinfo
->range
&& propinfo
->num_values
> 0)
3249 printf("\n\t\tsupported:");
3251 for (k
= 0; k
< propinfo
->num_values
; k
++)
3253 printf(" %-12.12s", XGetAtomName(dpy
, propinfo
->values
[k
]));
3254 if (k
% 4 == 3 && k
< propinfo
->num_values
- 1)
3260 else if (actual_format
== 8)
3262 printf("\t%s: %s%s\n", XGetAtomName(dpy
, props
[j
]), prop
, bytes_after
? "..." : "");
3266 char* type
= actual_type
? XGetAtomName(dpy
, actual_type
) : "none";
3267 printf("\t%s: %s(%d) (format %d items %d) ????\n", XGetAtomName(dpy
, props
[j
]), type
,
3268 (int)actual_type
, actual_format
, (int)nitems
);
3277 for (j
= 0; j
< output_info
->nmode
; j
++)
3279 XRRModeInfo
* mode
= find_mode_by_xid(output_info
->modes
[j
]);
3282 printf(" %s (0x%x) %6.1fMHz", mode
->name
, (int)mode
->id
,
3283 (double)mode
->dotClock
/ 1000000.0);
3284 for (f
= 0; mode_flags
[f
].flag
; f
++)
3285 if (mode
->modeFlags
& mode_flags
[f
].flag
)
3286 printf(" %s", mode_flags
[f
].string
);
3287 if (mode
== output
->mode_info
)
3288 printf(" *current");
3289 if (j
< output_info
->npreferred
)
3290 printf(" +preferred");
3292 printf(" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
3293 mode
->width
, mode
->hSyncStart
, mode
->hSyncEnd
, mode
->hTotal
, mode
->hSkew
,
3294 mode_hsync(mode
) / 1000);
3295 printf(" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
3296 mode
->height
, mode
->vSyncStart
, mode
->vSyncEnd
, mode
->vTotal
, mode_refresh(mode
));
3297 mode
->modeFlags
|= ModeShown
;
3302 mode_shown
= calloc(output_info
->nmode
, sizeof(Bool
));
3304 fatal("out of memory\n");
3305 for (j
= 0; j
< output_info
->nmode
; j
++)
3307 XRRModeInfo
*jmode
, *kmode
;
3312 jmode
= find_mode_by_xid(output_info
->modes
[j
]);
3313 for (k
= j
; k
< output_info
->nmode
; k
++)
3317 kmode
= find_mode_by_xid(output_info
->modes
[k
]);
3318 if (strcmp(jmode
->name
, kmode
->name
) != 0)
3320 mode_shown
[k
] = True
;
3321 kmode
->modeFlags
|= ModeShown
;
3322 printf(" <mode id=\"0x%lx\" name=\"%s\" w=\"%d\" h=\"%d\" hz=\"%.5f\"", kmode
->id
,
3323 kmode
->name
, kmode
->width
, kmode
->height
, mode_refresh(kmode
));
3324 if (kmode
== output
->mode_info
)
3325 printf(" current=\"true\"");
3327 printf(" current=\"false\"");
3328 if (k
< output_info
->npreferred
)
3329 printf(" preferred=\"true\"");
3331 printf(" preferred=\"false\"");
3337 printf(" </output>\n");
3341 for (m = 0; m < res->nmode; m++)
3343 XRRModeInfo *mode = &res->modes[m];
3345 if (!(mode->modeFlags & ModeShown))
3347 printf (" %s (0x%x) %6.1fMHz\n",
3348 mode->name, (int)mode->id,
3349 (double)mode->dotClock / 1000000.0);
3350 printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n",
3351 mode->width, mode->hSyncStart, mode->hSyncEnd,
3352 mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
3353 printf (" v: height %4d start %4d end %4d total %4d clock %6.1fHz\n",
3354 mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
3355 mode_refresh (mode));
3359 printf("</screen>\n");
3363 sc
= XRRGetScreenInfo(dpy
, root
);
3368 current_size
= XRRConfigCurrentConfiguration(sc
, ¤t_rotation
);
3370 sizes
= XRRConfigSizes(sc
, &nsize
);
3372 if (have_pixel_size
)
3374 for (size
= 0; size
< nsize
; size
++)
3376 if (sizes
[size
].width
== width
&& sizes
[size
].height
== height
)
3381 fprintf(stderr
, "Size %dx%d not found in available modes\n", width
, height
);
3386 size
= current_size
;
3387 else if (size
>= nsize
)
3389 fprintf(stderr
, "Size index %d is too large, there are only %d sizes\n", size
, nsize
);
3395 for (rot
= 0; rot
< 4; rot
++)
3396 if (1 << rot
== (current_rotation
& 0xf))
3400 current_rate
= XRRConfigCurrentRate(sc
);
3404 if (size
== current_size
)
3405 rate
= current_rate
;
3411 rates
= XRRConfigRates(sc
, size
, &nrate
);
3412 for (i
= 0; i
< nrate
; i
++)
3413 if (rate
== rates
[i
])
3417 fprintf(stderr
, "Rate %.1f Hz not available for this size\n", rate
);
3424 int major_version
, minor_version
;
3425 XRRQueryVersion(dpy
, &major_version
, &minor_version
);
3426 printf("Server reports RandR version %d.%d\n", major_version
, minor_version
);
3429 if (query
|| query_1
)
3431 printf(" SZ: Pixels Physical Refresh\n");
3432 for (i
= 0; i
< nsize
; i
++)
3434 printf("%c%-2d %5d x %-5d (%4dmm x%4dmm )", i
== current_size
? '*' : ' ', i
, sizes
[i
].width
,
3435 sizes
[i
].height
, sizes
[i
].mwidth
, sizes
[i
].mheight
);
3436 rates
= XRRConfigRates(sc
, i
, &nrate
);
3439 for (j
= 0; j
< nrate
; j
++)
3440 printf("%c%-4d", i
== current_size
&& rates
[j
] == current_rate
? '*' : ' ', rates
[j
]);
3445 rotations
= XRRConfigRotations(sc
, ¤t_rotation
);
3447 rotation
= 1 << rot
;
3450 printf("Current rotation - %s\n", rotation_name(current_rotation
));
3452 printf("Current reflection - %s\n", reflection_name(current_rotation
));
3454 printf("Rotations possible - ");
3455 for (i
= 0; i
< 4; i
++)
3457 if ((rotations
>> i
) & 1)
3458 printf("%s ", direction
[i
]);
3462 printf("Reflections possible - ");
3463 if (rotations
& (RR_Reflect_X
| RR_Reflect_Y
))
3465 if (rotations
& RR_Reflect_X
)
3467 if (rotations
& RR_Reflect_Y
)
3477 printf("Setting size to %d, rotation to %s\n", size
, direction
[rot
]);
3479 printf("Setting reflection on ");
3482 if (reflection
& RR_Reflect_X
)
3484 if (reflection
& RR_Reflect_Y
)
3488 printf("neither axis");
3491 if (reflection
& RR_Reflect_X
)
3492 printf("Setting reflection on X axis\n");
3494 if (reflection
& RR_Reflect_Y
)
3495 printf("Setting reflection on Y axis\n");
3498 /* we should test configureNotify on the root window */
3499 XSelectInput(dpy
, root
, StructureNotifyMask
);
3501 if (setit
&& !dryrun
)
3502 XRRSelectInput(dpy
, root
, RRScreenChangeNotifyMask
);
3503 if (setit
&& !dryrun
)
3504 status
= XRRSetScreenConfigAndRate(dpy
, sc
, root
, (SizeID
)size
,
3505 (Rotation
)(rotation
| reflection
), rate
, CurrentTime
);
3507 if (setit
&& !dryrun
&& status
== RRSetConfigFailed
)
3509 printf("Failed to change the screen configuration!\n");
3513 if (verbose
&& setit
&& !dryrun
&& size
!= current_size
)
3515 if (status
== RRSetConfigSuccess
)
3517 Bool seen_screen
= False
;
3518 while (!seen_screen
)
3521 XNextEvent(dpy
, (XEvent
*)&event
);
3523 printf("Event received, type = %d\n", event
.type
);
3524 /* update Xlib's knowledge of the event */
3525 XRRUpdateConfiguration((XEvent
*)&event
);
3526 if (event
.type
== ConfigureNotify
)
3527 printf("Received ConfigureNotify Event!\n");
3529 switch (event
.type
- event_base
)
3531 case RRScreenChangeNotify
:
3532 sce
= (XRRScreenChangeNotifyEvent
*)&event
;
3534 printf("Got a screen change notify event!\n");
3535 printf(" window = %d\n root = %d\n size_index = %d\n rotation %d\n", (int)sce
->window
,
3536 (int)sce
->root
, sce
->size_index
, sce
->rotation
);
3537 printf(" timestamp = %ld, config_timestamp = %ld\n", sce
->timestamp
,
3538 sce
->config_timestamp
);
3539 printf(" Rotation = %x\n", sce
->rotation
);
3540 printf(" %d X %d pixels, %d X %d mm\n", sce
->width
, sce
->height
, sce
->mwidth
,
3542 printf("Display width %d, height %d\n", DisplayWidth(dpy
, screen
),
3543 DisplayHeight(dpy
, screen
));
3544 printf("Display widthmm %d, heightmm %d\n", DisplayWidthMM(dpy
, screen
),
3545 DisplayHeightMM(dpy
, screen
));
3546 spo
= sce
->subpixel_order
;
3547 if ((spo
< 0) || (spo
> 5))
3548 printf("Unknown subpixel order, value = %d\n", spo
);
3550 printf("new Subpixel rendering model is %s\n", order
[spo
]);
3554 if (event
.type
!= ConfigureNotify
)
3555 printf("unknown event received, type = %d!\n", event
.type
);
3560 XRRFreeScreenConfigInfo(sc
);