Merge pull request #21633 from CastagnaIT/vp_mediaflag
[xbmc.git] / xbmc-xrandr.c
blob6b916f083daf8f30047c9fcc695437a7f28edd5b
1 /*
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
22 * OF THIS SOFTWARE.
24 * Thanks to Jim Gettys who wrote most of the client side code,
25 * and part of the server code for randr.
28 #include <math.h>
29 #include <stdarg.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
35 #include <X11/Xatom.h>
36 #include <X11/Xlib.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 */
41 #include <strings.h>
43 #ifndef _X_NORETURN
44 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)
45 #define _X_NORETURN __attribute((noreturn))
46 #else
47 #define _X_NORETURN
48 #endif
49 #endif
51 static char* program_name;
52 static Display* dpy;
53 static Window root;
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"};
65 /* subpixel order */
66 static char* order[6] = {"unknown", "horizontal rgb", "horizontal bgr",
67 "vertical rgb", "vertical bgr", "no subpixels"};
69 static const struct
71 char* string;
72 unsigned long flag;
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");
121 fprintf(stderr,
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");
136 exit(1);
137 /*NOTREACHED*/
140 static void _X_NORETURN fatal(const char* format, ...)
142 va_list ap;
144 va_start(ap, format);
145 fprintf(stderr, "%s: ", program_name);
146 vfprintf(stderr, format, ap);
147 va_end(ap);
148 exit(1);
149 /*NOTREACHED*/
152 static void warning(const char* format, ...)
154 va_list ap;
156 va_start(ap, format);
157 fprintf(stderr, "%s: ", program_name);
158 vfprintf(stderr, format, ap);
159 va_end(ap);
162 /* Because fmin requires C99 support */
163 static inline double dmin(double x, double y)
165 return x < y ? x : y;
168 static char* rotation_name(Rotation rotation)
170 int i;
172 if ((rotation & 0xf) == 0)
173 return "normal";
174 for (i = 0; i < 4; i++)
175 if (rotation & (1 << i))
176 return direction[i];
177 return "invalid rotation";
180 static char* reflection_name(Rotation rotation)
182 rotation &= (RR_Reflect_X | RR_Reflect_Y);
183 switch (rotation)
185 case 0:
186 return "none";
187 case RR_Reflect_X:
188 return "X axis";
189 case RR_Reflect_Y:
190 return "Y axis";
191 case RR_Reflect_X | RR_Reflect_Y:
192 return "X and Y axis";
194 return "invalid reflection";
197 typedef enum _relation
199 relation_left_of,
200 relation_right_of,
201 relation_above,
202 relation_below,
203 relation_same_as,
204 } relation_t;
206 typedef struct
208 int x, y, width, height;
209 } rectangle_t;
211 typedef struct
213 int x1, y1, x2, y2;
214 } box_t;
216 typedef struct
218 int x, y;
219 } point_t;
221 typedef enum _changes
223 changes_none = 0,
224 changes_crtc = (1 << 0),
225 changes_mode = (1 << 1),
226 changes_relation = (1 << 2),
227 changes_position = (1 << 3),
228 changes_rotation = (1 << 4),
229 changes_reflection = (1 << 5),
230 changes_automatic = (1 << 6),
231 changes_refresh = (1 << 7),
232 changes_property = (1 << 8),
233 changes_transform = (1 << 9),
234 changes_panning = (1 << 10),
235 changes_gamma = (1 << 11),
236 changes_primary = (1 << 12),
237 } changes_t;
239 typedef enum _name_kind
241 name_none = 0,
242 name_string = (1 << 0),
243 name_xid = (1 << 1),
244 name_index = (1 << 2),
245 name_preferred = (1 << 3),
246 } name_kind_t;
248 typedef struct
250 name_kind_t kind;
251 char* string;
252 XID xid;
253 int index;
254 } name_t;
256 typedef struct _crtc crtc_t;
257 typedef struct _output output_t;
258 typedef struct _transform transform_t;
259 typedef struct _umode umode_t;
260 typedef struct _output_prop output_prop_t;
262 struct _transform
264 XTransform transform;
265 char* filter;
266 int nparams;
267 XFixed* params;
270 struct _crtc
272 name_t crtc;
273 Bool changing;
274 XRRCrtcInfo* crtc_info;
276 XRRModeInfo* mode_info;
277 XRRPanning* panning_info;
278 int x;
279 int y;
280 Rotation rotation;
281 output_t** outputs;
282 int noutput;
283 transform_t current_transform, pending_transform;
286 struct _output_prop
288 struct _output_prop* next;
289 char* name;
290 char* value;
293 struct _output
295 struct _output* next;
297 changes_t changes;
299 output_prop_t* props;
301 name_t output;
302 XRROutputInfo* output_info;
304 name_t crtc;
305 crtc_t* crtc_info;
306 crtc_t* current_crtc_info;
308 name_t mode;
309 double refresh;
310 XRRModeInfo* mode_info;
312 name_t addmode;
314 relation_t relation;
315 char* relative_to;
317 int x, y;
318 Rotation rotation;
320 XRRPanning panning;
322 Bool automatic;
323 int scale_from_w, scale_from_h;
324 transform_t transform;
326 struct
328 float red;
329 float green;
330 float blue;
331 } gamma;
333 float brightness;
335 Bool primary;
337 Bool found;
340 typedef enum _umode_action
342 umode_create,
343 umode_destroy,
344 umode_add,
345 umode_delete
346 } umode_action_t;
349 struct _umode
351 struct _umode* next;
353 umode_action_t action;
354 XRRModeInfo mode;
355 name_t output;
356 name_t name;
361 static char *connection[3] = {
362 "connected",
363 "disconnected",
364 "unknown connection"};
368 static char* connection[3] = {"true", "false", "unknown"};
370 #define OUTPUT_NAME 1
372 #define CRTC_OFF 2
373 #define CRTC_UNSET 3
374 #define CRTC_INDEX 0x40000000
376 #define MODE_NAME 1
377 #define MODE_OFF 2
378 #define MODE_UNSET 3
379 #define MODE_PREF 4
381 #define POS_UNSET -1
383 static output_t* outputs = NULL;
384 static output_t** outputs_tail = &outputs;
385 static crtc_t* crtcs;
386 static umode_t* umodes;
387 static int num_crtcs;
388 static XRRScreenResources* res;
389 static int fb_width = 0, fb_height = 0;
390 static int fb_width_mm = 0, fb_height_mm = 0;
391 static double dpi = 0;
392 static char* dpi_output = NULL;
393 static Bool dryrun = False;
394 static int minWidth, maxWidth, minHeight, maxHeight;
395 static Bool has_1_2 = False;
396 static Bool has_1_3 = False;
398 static int mode_height(XRRModeInfo* mode_info, Rotation rotation)
400 switch (rotation & 0xf)
402 case RR_Rotate_0:
403 case RR_Rotate_180:
404 return mode_info->height;
405 case RR_Rotate_90:
406 case RR_Rotate_270:
407 return mode_info->width;
408 default:
409 return 0;
413 static int mode_width(XRRModeInfo* mode_info, Rotation rotation)
415 switch (rotation & 0xf)
417 case RR_Rotate_0:
418 case RR_Rotate_180:
419 return mode_info->width;
420 case RR_Rotate_90:
421 case RR_Rotate_270:
422 return mode_info->height;
423 default:
424 return 0;
428 static Bool transform_point(XTransform* transform, double* xp, double* yp)
430 double vector[3];
431 double result[3];
432 int i, j;
433 double v;
435 vector[0] = *xp;
436 vector[1] = *yp;
437 vector[2] = 1;
438 for (j = 0; j < 3; j++)
440 v = 0;
441 for (i = 0; i < 3; i++)
442 v += (XFixedToDouble(transform->matrix[j][i]) * vector[i]);
443 result[j] = v;
445 if (!result[2])
446 return False;
447 for (j = 0; j < 2; j++)
449 vector[j] = result[j] / result[2];
450 if (vector[j] > 32767 || vector[j] < -32767)
451 return False;
453 *xp = vector[0];
454 *yp = vector[1];
455 return True;
458 static void path_bounds(XTransform* transform, point_t* points, int npoints, box_t* box)
460 int i;
461 box_t point;
463 for (i = 0; i < npoints; i++)
465 double x, y;
466 x = points[i].x;
467 y = points[i].y;
468 transform_point(transform, &x, &y);
469 point.x1 = floor(x);
470 point.y1 = floor(y);
471 point.x2 = ceil(x);
472 point.y2 = ceil(y);
473 if (i == 0)
474 *box = point;
475 else
477 if (point.x1 < box->x1)
478 box->x1 = point.x1;
479 if (point.y1 < box->y1)
480 box->y1 = point.y1;
481 if (point.x2 > box->x2)
482 box->x2 = point.x2;
483 if (point.y2 > box->y2)
484 box->y2 = point.y2;
489 static void mode_geometry(XRRModeInfo* mode_info,
490 Rotation rotation,
491 XTransform* transform,
492 box_t* bounds)
494 point_t rect[4];
495 int width = mode_width(mode_info, rotation);
496 int height = mode_height(mode_info, rotation);
498 rect[0].x = 0;
499 rect[0].y = 0;
500 rect[1].x = width;
501 rect[1].y = 0;
502 rect[2].x = width;
503 rect[2].y = height;
504 rect[3].x = 0;
505 rect[3].y = height;
506 path_bounds(transform, rect, 4, bounds);
509 /* v refresh frequency in Hz */
510 static double mode_refresh(XRRModeInfo* mode_info)
512 double rate;
514 if (mode_info->hTotal && mode_info->vTotal)
515 rate = ((double)mode_info->dotClock / ((double)mode_info->hTotal * (double)mode_info->vTotal));
516 else
517 rate = 0;
518 return rate;
521 /* h sync frequency in Hz */
522 static double mode_hsync(XRRModeInfo* mode_info)
524 double rate;
526 if (mode_info->hTotal)
527 rate = (double)mode_info->dotClock / (double)mode_info->hTotal;
528 else
529 rate = 0;
530 return rate;
533 static void init_name(name_t* name)
535 name->kind = name_none;
538 static void set_name_string(name_t* name, char* string)
540 name->kind |= name_string;
541 name->string = string;
544 static void set_name_xid(name_t* name, XID xid)
546 name->kind |= name_xid;
547 name->xid = xid;
550 static void set_name_index(name_t* name, int index)
552 name->kind |= name_index;
553 name->index = index;
556 static void set_name_preferred(name_t* name)
558 name->kind |= name_preferred;
561 static void set_name_all(name_t* name, name_t* old)
563 if (old->kind & name_xid)
564 name->xid = old->xid;
565 if (old->kind & name_string)
566 name->string = old->string;
567 if (old->kind & name_index)
568 name->index = old->index;
569 name->kind |= old->kind;
572 static void set_name(name_t* name, char* string, name_kind_t valid)
574 unsigned int xid; /* don't make it XID (which is unsigned long):
575 scanf() takes unsigned int */
576 int index;
578 if ((valid & name_xid) && sscanf(string, "0x%x", &xid) == 1)
579 set_name_xid(name, xid);
580 else if ((valid & name_index) && sscanf(string, "%d", &index) == 1)
581 set_name_index(name, index);
582 else if (valid & name_string)
583 set_name_string(name, string);
584 else
585 usage();
588 static void init_transform(transform_t* transform)
590 int x;
591 memset(&transform->transform, '\0', sizeof(transform->transform));
592 for (x = 0; x < 3; x++)
593 transform->transform.matrix[x][x] = XDoubleToFixed(1.0);
594 transform->filter = "";
595 transform->nparams = 0;
596 transform->params = NULL;
599 static void set_transform(
600 transform_t* dest, XTransform* transform, char* filter, XFixed* params, int nparams)
602 dest->transform = *transform;
603 dest->filter = strdup(filter);
604 dest->nparams = nparams;
605 dest->params = malloc(nparams * sizeof(XFixed));
606 memcpy(dest->params, params, nparams * sizeof(XFixed));
609 static void copy_transform(transform_t* dest, transform_t* src)
611 set_transform(dest, &src->transform, src->filter, src->params, src->nparams);
614 static Bool equal_transform(transform_t* a, transform_t* b)
616 if (memcmp(&a->transform, &b->transform, sizeof(XTransform)) != 0)
617 return False;
618 if (strcmp(a->filter, b->filter) != 0)
619 return False;
620 if (a->nparams != b->nparams)
621 return False;
622 if (memcmp(a->params, b->params, a->nparams * sizeof(XFixed)) != 0)
623 return False;
624 return True;
627 static output_t* add_output(void)
629 output_t* output = calloc(1, sizeof(output_t));
631 if (!output)
632 fatal("out of memory\n");
633 output->next = NULL;
634 output->found = False;
635 output->brightness = 1.0;
636 *outputs_tail = output;
637 outputs_tail = &output->next;
638 return output;
641 static output_t* find_output(name_t* name)
643 output_t* output;
645 for (output = outputs; output; output = output->next)
647 name_kind_t common = name->kind & output->output.kind;
649 if ((common & name_xid) && name->xid == output->output.xid)
650 break;
651 if ((common & name_string) && !strcmp(name->string, output->output.string))
652 break;
653 if ((common & name_index) && name->index == output->output.index)
654 break;
656 return output;
659 static output_t* find_output_by_xid(RROutput output)
661 name_t output_name;
663 init_name(&output_name);
664 set_name_xid(&output_name, output);
665 return find_output(&output_name);
668 static output_t* find_output_by_name(char* name)
670 name_t output_name;
672 init_name(&output_name);
673 set_name_string(&output_name, name);
674 return find_output(&output_name);
677 static crtc_t* find_crtc(name_t* name)
679 int c;
680 crtc_t* crtc = NULL;
682 for (c = 0; c < num_crtcs; c++)
684 name_kind_t common;
686 crtc = &crtcs[c];
687 common = name->kind & crtc->crtc.kind;
689 if ((common & name_xid) && name->xid == crtc->crtc.xid)
690 break;
691 if ((common & name_string) && !strcmp(name->string, crtc->crtc.string))
692 break;
693 if ((common & name_index) && name->index == crtc->crtc.index)
694 break;
695 crtc = NULL;
697 return crtc;
700 static crtc_t* find_crtc_by_xid(RRCrtc crtc)
702 name_t crtc_name;
704 init_name(&crtc_name);
705 set_name_xid(&crtc_name, crtc);
706 return find_crtc(&crtc_name);
709 static XRRModeInfo* find_mode(name_t* name, double refresh)
711 int m;
712 XRRModeInfo* best = NULL;
713 double bestDist = 0;
715 for (m = 0; m < res->nmode; m++)
717 XRRModeInfo* mode = &res->modes[m];
718 if ((name->kind & name_xid) && name->xid == mode->id)
720 best = mode;
721 break;
723 if ((name->kind & name_string) && !strcmp(name->string, mode->name))
725 double dist;
727 if (refresh)
728 dist = fabs(mode_refresh(mode) - refresh);
729 else
730 dist = 0;
731 if (!best || dist < bestDist)
733 bestDist = dist;
734 best = mode;
738 return best;
741 static XRRModeInfo* find_mode_by_xid(RRMode mode)
743 name_t mode_name;
745 init_name(&mode_name);
746 set_name_xid(&mode_name, mode);
747 return find_mode(&mode_name, 0);
750 #if 0
751 static XRRModeInfo *
752 find_mode_by_name (char *name)
754 name_t mode_name;
755 init_name (&mode_name);
756 set_name_string (&mode_name, name);
757 return find_mode (&mode_name, 0);
759 #endif
761 static XRRModeInfo* find_mode_for_output(output_t* output, name_t* name)
763 XRROutputInfo* output_info = output->output_info;
764 int m;
765 XRRModeInfo* best = NULL;
766 double bestDist = 0;
768 for (m = 0; m < output_info->nmode; m++)
770 XRRModeInfo* mode;
772 mode = find_mode_by_xid(output_info->modes[m]);
773 if (!mode)
774 continue;
775 if ((name->kind & name_xid) && name->xid == mode->id)
777 best = mode;
778 break;
780 if ((name->kind & name_string) && !strcmp(name->string, mode->name))
782 double dist;
784 /* Stay away from doublescan modes unless refresh rate is specified. */
785 if (!output->refresh && (mode->modeFlags & RR_DoubleScan))
786 continue;
788 if (output->refresh)
789 dist = fabs(mode_refresh(mode) - output->refresh);
790 else
791 dist = 0;
792 if (!best || dist < bestDist)
794 bestDist = dist;
795 best = mode;
799 return best;
802 static XRRModeInfo* preferred_mode(output_t* output)
804 XRROutputInfo* output_info = output->output_info;
805 int m;
806 XRRModeInfo* best;
807 int bestDist;
809 best = NULL;
810 bestDist = 0;
811 for (m = 0; m < output_info->nmode; m++)
813 XRRModeInfo* mode_info = find_mode_by_xid(output_info->modes[m]);
814 int dist;
816 if (m < output_info->npreferred)
817 dist = 0;
818 else if (output_info->mm_height)
819 dist = (1000 * DisplayHeight(dpy, screen) / DisplayHeightMM(dpy, screen) -
820 1000 * mode_info->height / output_info->mm_height);
821 else
822 dist = DisplayHeight(dpy, screen) - mode_info->height;
824 if (dist < 0)
825 dist = -dist;
826 if (!best || dist < bestDist)
828 best = mode_info;
829 bestDist = dist;
832 return best;
835 static Bool output_can_use_crtc(output_t* output, crtc_t* crtc)
837 XRROutputInfo* output_info = output->output_info;
838 int c;
840 for (c = 0; c < output_info->ncrtc; c++)
841 if (output_info->crtcs[c] == crtc->crtc.xid)
842 return True;
843 return False;
846 static Bool output_can_use_mode(output_t* output, XRRModeInfo* mode)
848 XRROutputInfo* output_info = output->output_info;
849 int m;
851 for (m = 0; m < output_info->nmode; m++)
852 if (output_info->modes[m] == mode->id)
853 return True;
854 return False;
857 static Bool crtc_can_use_rotation(crtc_t* crtc, Rotation rotation)
859 Rotation rotations = crtc->crtc_info->rotations;
860 Rotation dir = rotation & (RR_Rotate_0 | RR_Rotate_90 | RR_Rotate_180 | RR_Rotate_270);
861 Rotation reflect = rotation & (RR_Reflect_X | RR_Reflect_Y);
862 if (((rotations & dir) != 0) && ((rotations & reflect) == reflect))
863 return True;
864 return False;
867 #if 0
868 static Bool
869 crtc_can_use_transform (crtc_t *crtc, XTransform *transform)
871 int major, minor;
873 XRRQueryVersion (dpy, &major, &minor);
874 if (major > 1 || (major == 1 && minor >= 3))
875 return True;
876 return False;
880 * Report only rotations that are supported by all crtcs
882 static Rotation
883 output_rotations (output_t *output)
885 Bool found = False;
886 Rotation rotation = RR_Rotate_0;
887 XRROutputInfo *output_info = output->output_info;
888 int c;
890 for (c = 0; c < output_info->ncrtc; c++)
892 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
893 if (crtc)
895 if (!found) {
896 rotation = crtc->crtc_info->rotations;
897 found = True;
898 } else
899 rotation &= crtc->crtc_info->rotations;
902 return rotation;
904 #endif
906 static Bool output_can_use_rotation(output_t* output, Rotation rotation)
908 XRROutputInfo* output_info = output->output_info;
909 int c;
911 /* make sure all of the crtcs can use this rotation.
912 * yes, this is not strictly necessary, but it is
913 * simpler,and we expect most drivers to either
914 * support rotation everywhere or nowhere
916 for (c = 0; c < output_info->ncrtc; c++)
918 crtc_t* crtc = find_crtc_by_xid(output_info->crtcs[c]);
919 if (crtc && !crtc_can_use_rotation(crtc, rotation))
920 return False;
922 return True;
925 static Bool output_is_primary(output_t* output)
927 if (has_1_3)
928 return XRRGetOutputPrimary(dpy, root) == output->output.xid;
929 return False;
932 /* Returns the index of the last value in an array < 0xffff */
933 static int find_last_non_clamped(CARD16 array[], int size)
935 int i;
936 for (i = size - 1; i > 0; i--)
938 if (array[i] < 0xffff)
939 return i;
941 return 0;
944 static void set_gamma_info(output_t* output)
946 XRRCrtcGamma* gamma;
947 double i1, v1, i2, v2;
948 int size, middle, last_best, last_red, last_green, last_blue;
949 CARD16* best_array;
951 if (!output->crtc_info)
952 return;
954 size = XRRGetCrtcGammaSize(dpy, output->crtc_info->crtc.xid);
955 if (!size)
957 warning("Failed to get size of gamma for output %s\n", output->output.string);
958 return;
961 gamma = XRRGetCrtcGamma(dpy, output->crtc_info->crtc.xid);
962 if (!gamma)
964 warning("Failed to get gamma for output %s\n", output->output.string);
965 return;
969 * Here is a bit tricky because gamma is a whole curve for each
970 * color. So, typically, we need to represent 3 * 256 values as 3 + 1
971 * values. Therefore, we approximate the gamma curve (v) by supposing
972 * it always follows the way we set it: a power function (i^g)
973 * multiplied by a brightness (b).
974 * v = i^g * b
975 * so g = (ln(v) - ln(b))/ln(i)
976 * and b can be found using two points (v1,i1) and (v2, i2):
977 * b = e^((ln(v2)*ln(i1) - ln(v1)*ln(i2))/ln(i1/i2))
978 * For the best resolution, we select i2 at the highest place not
979 * clamped and i1 at i2/2. Note that if i2 = 1 (as in most normal
980 * cases), then b = v2.
982 last_red = find_last_non_clamped(gamma->red, size);
983 last_green = find_last_non_clamped(gamma->green, size);
984 last_blue = find_last_non_clamped(gamma->blue, size);
985 best_array = gamma->red;
986 last_best = last_red;
987 if (last_green > last_best)
989 last_best = last_green;
990 best_array = gamma->green;
992 if (last_blue > last_best)
994 last_best = last_blue;
995 best_array = gamma->blue;
997 if (last_best == 0)
998 last_best = 1;
1000 middle = last_best / 2;
1001 i1 = (double)(middle + 1) / size;
1002 v1 = (double)(best_array[middle]) / 65535;
1003 i2 = (double)(last_best + 1) / size;
1004 v2 = (double)(best_array[last_best]) / 65535;
1005 if (v2 < 0.0001)
1006 { /* The screen is black */
1007 output->brightness = 0;
1008 output->gamma.red = 1;
1009 output->gamma.green = 1;
1010 output->gamma.blue = 1;
1012 else
1014 if ((last_best + 1) == size)
1015 output->brightness = v2;
1016 else
1017 output->brightness = exp((log(v2) * log(i1) - log(v1) * log(i2)) / log(i1 / i2));
1018 output->gamma.red = logf((gamma->red[last_red / 2]) / output->brightness / 65535) /
1019 logf(((last_red / 2) + 1) / size);
1020 output->gamma.green = logf((gamma->green[last_green / 2]) / output->brightness / 65535) /
1021 logf(((last_green / 2) + 1) / size);
1022 output->gamma.blue = logf((gamma->blue[last_blue / 2]) / output->brightness / 65535) /
1023 logf(((last_blue / 2) + 1) / size);
1026 XRRFreeGamma(gamma);
1029 static void set_output_info(output_t* output, RROutput xid, XRROutputInfo* output_info)
1031 /* sanity check output info */
1032 if (output_info->connection != RR_Disconnected && !output_info->nmode)
1033 warning("Output %s is not disconnected but has no modes\n", output_info->name);
1035 /* set output name and info */
1036 if (!(output->output.kind & name_xid))
1037 set_name_xid(&output->output, xid);
1038 if (!(output->output.kind & name_string))
1039 set_name_string(&output->output, output_info->name);
1040 output->output_info = output_info;
1042 /* set crtc name and info */
1043 if (!(output->changes & changes_crtc))
1044 set_name_xid(&output->crtc, output_info->crtc);
1046 if (output->crtc.kind == name_xid && output->crtc.xid == None)
1047 output->crtc_info = NULL;
1048 else
1050 output->crtc_info = find_crtc(&output->crtc);
1051 if (!output->crtc_info)
1053 if (output->crtc.kind & name_xid)
1054 fatal("cannot find crtc 0x%x\n", output->crtc.xid);
1055 if (output->crtc.kind & name_index)
1056 fatal("cannot find crtc %d\n", output->crtc.index);
1058 if (!output_can_use_crtc(output, output->crtc_info))
1059 fatal("output %s cannot use crtc 0x%x\n", output->output.string, output->crtc_info->crtc.xid);
1062 /* set mode name and info */
1063 if (!(output->changes & changes_mode))
1065 crtc_t* crtc = NULL;
1067 if (output_info->crtc)
1068 crtc = find_crtc_by_xid(output_info->crtc);
1069 if (crtc && crtc->crtc_info)
1070 set_name_xid(&output->mode, crtc->crtc_info->mode);
1071 else if (output->crtc_info)
1072 set_name_xid(&output->mode, output->crtc_info->crtc_info->mode);
1073 else
1074 set_name_xid(&output->mode, None);
1075 if (output->mode.xid)
1077 output->mode_info = find_mode_by_xid(output->mode.xid);
1078 if (!output->mode_info)
1079 fatal("server did not report mode 0x%x for output %s\n", output->mode.xid,
1080 output->output.string);
1082 else
1083 output->mode_info = NULL;
1085 else if (output->mode.kind == name_xid && output->mode.xid == None)
1086 output->mode_info = NULL;
1087 else
1089 if (output->mode.kind == name_preferred)
1090 output->mode_info = preferred_mode(output);
1091 else
1092 output->mode_info = find_mode_for_output(output, &output->mode);
1093 if (!output->mode_info)
1095 if (output->mode.kind & name_preferred)
1096 fatal("cannot find preferred mode\n");
1097 if (output->mode.kind & name_string)
1098 fatal("cannot find mode %s\n", output->mode.string);
1099 if (output->mode.kind & name_xid)
1100 fatal("cannot find mode 0x%x\n", output->mode.xid);
1102 if (!output_can_use_mode(output, output->mode_info))
1103 fatal("output %s cannot use mode %s\n", output->output.string, output->mode_info->name);
1106 /* set position */
1107 if (!(output->changes & changes_position))
1109 if (output->crtc_info)
1111 output->x = output->crtc_info->crtc_info->x;
1112 output->y = output->crtc_info->crtc_info->y;
1114 else
1116 output->x = 0;
1117 output->y = 0;
1121 /* set rotation */
1122 if (!(output->changes & changes_rotation))
1124 output->rotation &= ~0xf;
1125 if (output->crtc_info)
1126 output->rotation |= (output->crtc_info->crtc_info->rotation & 0xf);
1127 else
1128 output->rotation = RR_Rotate_0;
1130 if (!(output->changes & changes_reflection))
1132 output->rotation &= ~(RR_Reflect_X | RR_Reflect_Y);
1133 if (output->crtc_info)
1134 output->rotation |= (output->crtc_info->crtc_info->rotation & (RR_Reflect_X | RR_Reflect_Y));
1136 if (!output_can_use_rotation(output, output->rotation))
1137 fatal("output %s cannot use rotation \"%s\" reflection \"%s\"\n", output->output.string,
1138 rotation_name(output->rotation), reflection_name(output->rotation));
1140 /* set gamma */
1141 if (!(output->changes & changes_gamma))
1142 set_gamma_info(output);
1144 /* set transformation */
1145 if (!(output->changes & changes_transform))
1147 if (output->crtc_info)
1148 copy_transform(&output->transform, &output->crtc_info->current_transform);
1149 else
1150 init_transform(&output->transform);
1152 else
1154 /* transform was already set for --scale or --transform */
1156 /* for --scale-from, figure out the mode size and compute the transform
1157 * for the target framebuffer area */
1158 if (output->scale_from_w > 0 && output->mode_info)
1160 double sx = (double)output->scale_from_w / output->mode_info->width;
1161 double sy = (double)output->scale_from_h / output->mode_info->height;
1162 if (verbose)
1163 printf("scaling %s by %lfx%lf\n", output->output.string, sx, sy);
1164 init_transform(&output->transform);
1165 output->transform.transform.matrix[0][0] = XDoubleToFixed(sx);
1166 output->transform.transform.matrix[1][1] = XDoubleToFixed(sy);
1167 output->transform.transform.matrix[2][2] = XDoubleToFixed(1.0);
1168 if (sx != 1 || sy != 1)
1169 output->transform.filter = "bilinear";
1170 else
1171 output->transform.filter = "nearest";
1172 output->transform.nparams = 0;
1173 output->transform.params = NULL;
1177 /* set primary */
1178 if (!(output->changes & changes_primary))
1179 output->primary = output_is_primary(output);
1182 static void get_screen(Bool current)
1184 if (!has_1_2)
1185 fatal("Server RandR version before 1.2\n");
1187 XRRGetScreenSizeRange(dpy, root, &minWidth, &minHeight, &maxWidth, &maxHeight);
1189 if (current)
1190 res = XRRGetScreenResourcesCurrent(dpy, root);
1191 else
1192 res = XRRGetScreenResources(dpy, root);
1193 if (!res)
1194 fatal("could not get screen resources");
1197 static void get_crtcs(void)
1199 int c;
1201 num_crtcs = res->ncrtc;
1202 crtcs = calloc(num_crtcs, sizeof(crtc_t));
1203 if (!crtcs)
1204 fatal("out of memory\n");
1206 for (c = 0; c < res->ncrtc; c++)
1208 XRRCrtcInfo* crtc_info = XRRGetCrtcInfo(dpy, res, res->crtcs[c]);
1209 XRRCrtcTransformAttributes* attr;
1210 XRRPanning* panning_info = NULL;
1212 if (has_1_3)
1214 XRRPanning zero;
1215 memset(&zero, 0, sizeof(zero));
1216 panning_info = XRRGetPanning(dpy, res, res->crtcs[c]);
1217 zero.timestamp = panning_info->timestamp;
1218 if (!memcmp(panning_info, &zero, sizeof(zero)))
1220 Xfree(panning_info);
1221 panning_info = NULL;
1225 set_name_xid(&crtcs[c].crtc, res->crtcs[c]);
1226 set_name_index(&crtcs[c].crtc, c);
1227 if (!crtc_info)
1228 fatal("could not get crtc 0x%x information\n", res->crtcs[c]);
1229 crtcs[c].crtc_info = crtc_info;
1230 crtcs[c].panning_info = panning_info;
1231 if (crtc_info->mode == None)
1233 crtcs[c].mode_info = NULL;
1234 crtcs[c].x = 0;
1235 crtcs[c].y = 0;
1236 crtcs[c].rotation = RR_Rotate_0;
1238 if (XRRGetCrtcTransform(dpy, res->crtcs[c], &attr) && attr)
1240 set_transform(&crtcs[c].current_transform, &attr->currentTransform, attr->currentFilter,
1241 attr->currentParams, attr->currentNparams);
1242 XFree(attr);
1244 else
1246 init_transform(&crtcs[c].current_transform);
1248 copy_transform(&crtcs[c].pending_transform, &crtcs[c].current_transform);
1252 static void crtc_add_output(crtc_t* crtc, output_t* output)
1254 if (crtc->outputs)
1255 crtc->outputs = realloc(crtc->outputs, (crtc->noutput + 1) * sizeof(output_t*));
1256 else
1258 crtc->outputs = malloc(sizeof(output_t*));
1259 crtc->x = output->x;
1260 crtc->y = output->y;
1261 crtc->rotation = output->rotation;
1262 crtc->mode_info = output->mode_info;
1263 copy_transform(&crtc->pending_transform, &output->transform);
1265 if (!crtc->outputs)
1266 fatal("out of memory\n");
1267 crtc->outputs[crtc->noutput++] = output;
1270 static void set_crtcs(void)
1272 output_t* output;
1274 for (output = outputs; output; output = output->next)
1276 if (!output->mode_info)
1277 continue;
1278 crtc_add_output(output->crtc_info, output);
1282 static void set_panning(void)
1284 output_t* output;
1286 for (output = outputs; output; output = output->next)
1288 if (!output->crtc_info)
1289 continue;
1290 if (!(output->changes & changes_panning))
1291 continue;
1292 if (!output->crtc_info->panning_info)
1293 output->crtc_info->panning_info = malloc(sizeof(XRRPanning));
1294 memcpy(output->crtc_info->panning_info, &output->panning, sizeof(XRRPanning));
1295 output->crtc_info->changing = 1;
1299 static void set_gamma(void)
1301 output_t* output;
1303 for (output = outputs; output; output = output->next)
1305 int i, size;
1306 crtc_t* crtc;
1307 XRRCrtcGamma* gamma;
1309 if (!(output->changes & changes_gamma))
1310 continue;
1312 if (!output->crtc_info)
1314 fatal("Need crtc to set gamma on.\n");
1315 continue;
1318 crtc = output->crtc_info;
1320 size = XRRGetCrtcGammaSize(dpy, crtc->crtc.xid);
1322 if (!size)
1324 fatal("Gamma size is 0.\n");
1325 continue;
1328 gamma = XRRAllocGamma(size);
1329 if (!gamma)
1331 fatal("Gamma allocation failed.\n");
1332 continue;
1335 if (output->gamma.red == 0.0f && output->gamma.green == 0.0f && output->gamma.blue == 0.0f)
1336 output->gamma.red = output->gamma.green = output->gamma.blue = 1.0f;
1338 for (i = 0; i < size; i++)
1340 if (output->gamma.red == 1.0f && output->brightness == 1.0f)
1341 gamma->red[i] = (i << 8) + i;
1342 else
1343 gamma->red[i] =
1344 min(powf((float)i / (float)(size - 1), output->gamma.red) * output->brightness, 1.0f) *
1345 65535.0f;
1347 if (output->gamma.green == 1.0f && output->brightness == 1.0f)
1348 gamma->green[i] = (i << 8) + i;
1349 else
1350 gamma->green[i] =
1351 min(powf((float)i / (float)(size - 1), output->gamma.green) * output->brightness,
1352 1.0f) *
1353 65535.0f;
1355 if (output->gamma.blue == 1.0f && output->brightness == 1.0f)
1356 gamma->blue[i] = (i << 8) + i;
1357 else
1358 gamma->blue[i] =
1359 min(powf((float)i / (float)(size - 1), output->gamma.blue) * output->brightness, 1.0f) *
1360 65535.0f;
1363 XRRSetCrtcGamma(dpy, crtc->crtc.xid, gamma);
1365 free(gamma);
1369 static void set_primary(void)
1371 output_t* output;
1373 if (no_primary)
1375 XRRSetOutputPrimary(dpy, root, None);
1377 else
1379 for (output = outputs; output; output = output->next)
1381 if (!(output->changes & changes_primary))
1382 continue;
1383 if (output->primary)
1384 XRRSetOutputPrimary(dpy, root, output->output.xid);
1389 static Status crtc_disable(crtc_t* crtc)
1391 if (verbose)
1392 printf("crtc %d: disable\n", crtc->crtc.index);
1394 if (dryrun)
1395 return RRSetConfigSuccess;
1396 return XRRSetCrtcConfig(dpy, res, crtc->crtc.xid, CurrentTime, 0, 0, None, RR_Rotate_0, NULL, 0);
1399 static void crtc_set_transform(crtc_t* crtc, transform_t* transform)
1401 int major, minor;
1403 XRRQueryVersion(dpy, &major, &minor);
1404 if (major > 1 || (major == 1 && minor >= 3))
1405 XRRSetCrtcTransform(dpy, crtc->crtc.xid, &transform->transform, transform->filter,
1406 transform->params, transform->nparams);
1409 static Status crtc_revert(crtc_t* crtc)
1411 XRRCrtcInfo* crtc_info = crtc->crtc_info;
1413 if (verbose)
1414 printf("crtc %d: revert\n", crtc->crtc.index);
1416 if (dryrun)
1417 return RRSetConfigSuccess;
1419 if (!equal_transform(&crtc->current_transform, &crtc->pending_transform))
1420 crtc_set_transform(crtc, &crtc->current_transform);
1421 return XRRSetCrtcConfig(dpy, res, crtc->crtc.xid, CurrentTime, crtc_info->x, crtc_info->y,
1422 crtc_info->mode, crtc_info->rotation, crtc_info->outputs,
1423 crtc_info->noutput);
1426 static Status crtc_apply(crtc_t* crtc)
1428 RROutput* rr_outputs;
1429 int o;
1430 Status s;
1431 RRMode mode = None;
1433 if (!crtc->changing || !crtc->mode_info)
1434 return RRSetConfigSuccess;
1436 rr_outputs = calloc(crtc->noutput, sizeof(RROutput));
1437 if (!rr_outputs)
1438 return BadAlloc;
1439 for (o = 0; o < crtc->noutput; o++)
1440 rr_outputs[o] = crtc->outputs[o]->output.xid;
1441 mode = crtc->mode_info->id;
1442 if (verbose)
1444 printf("crtc %d: %12s %6.1f +%d+%d", crtc->crtc.index, crtc->mode_info->name,
1445 mode_refresh(crtc->mode_info), crtc->x, crtc->y);
1446 for (o = 0; o < crtc->noutput; o++)
1447 printf(" \"%s\"", crtc->outputs[o]->output.string);
1448 printf("\n");
1451 if (dryrun)
1452 s = RRSetConfigSuccess;
1453 else
1455 if (!equal_transform(&crtc->current_transform, &crtc->pending_transform))
1456 crtc_set_transform(crtc, &crtc->pending_transform);
1457 s = XRRSetCrtcConfig(dpy, res, crtc->crtc.xid, CurrentTime, crtc->x, crtc->y, mode,
1458 crtc->rotation, rr_outputs, crtc->noutput);
1459 if (s == RRSetConfigSuccess && crtc->panning_info)
1461 if (has_1_3)
1462 s = XRRSetPanning(dpy, res, crtc->crtc.xid, crtc->panning_info);
1463 else
1464 fatal("panning needs RandR 1.3\n");
1467 free(rr_outputs);
1468 return s;
1471 static void screen_revert(void)
1473 if (verbose)
1474 printf("screen %d: revert\n", screen);
1476 if (dryrun)
1477 return;
1478 XRRSetScreenSize(dpy, root, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
1479 DisplayWidthMM(dpy, screen), DisplayHeightMM(dpy, screen));
1482 static void screen_apply(void)
1484 if (fb_width == DisplayWidth(dpy, screen) && fb_height == DisplayHeight(dpy, screen) &&
1485 fb_width_mm == DisplayWidthMM(dpy, screen) && fb_height_mm == DisplayHeightMM(dpy, screen))
1487 return;
1489 if (verbose)
1490 printf("screen %d: %dx%d %dx%d mm %6.2fdpi\n", screen, fb_width, fb_height, fb_width_mm,
1491 fb_height_mm, dpi);
1492 if (dryrun)
1493 return;
1494 XRRSetScreenSize(dpy, root, fb_width, fb_height, fb_width_mm, fb_height_mm);
1497 static void revert(void)
1499 int c;
1501 /* first disable all crtcs */
1502 for (c = 0; c < res->ncrtc; c++)
1503 crtc_disable(&crtcs[c]);
1504 /* next reset screen size */
1505 screen_revert();
1506 /* now restore all crtcs */
1507 for (c = 0; c < res->ncrtc; c++)
1508 crtc_revert(&crtcs[c]);
1512 * uh-oh, something bad happened in the middle of changing
1513 * the configuration. Revert to the previous configuration
1514 * and bail
1516 static void _X_NORETURN panic(Status s, crtc_t* crtc)
1518 int c = crtc->crtc.index;
1519 char* message;
1521 switch (s)
1523 case RRSetConfigSuccess:
1524 message = "succeeded";
1525 break;
1526 case BadAlloc:
1527 message = "out of memory";
1528 break;
1529 case RRSetConfigFailed:
1530 message = "failed";
1531 break;
1532 case RRSetConfigInvalidConfigTime:
1533 message = "invalid config time";
1534 break;
1535 case RRSetConfigInvalidTime:
1536 message = "invalid time";
1537 break;
1538 default:
1539 message = "unknown failure";
1540 break;
1543 fprintf(stderr, "%s: Configure crtc %d %s\n", program_name, c, message);
1544 revert();
1545 exit(1);
1548 static void apply(void)
1550 Status s;
1551 int c;
1554 * Hold the server grabbed while messing with
1555 * the screen so that apps which notice the resize
1556 * event and ask for xinerama information from the server
1557 * receive up-to-date information
1559 if (grab_server)
1560 XGrabServer(dpy);
1563 * Turn off any crtcs which are to be disabled or which are
1564 * larger than the target size
1566 for (c = 0; c < res->ncrtc; c++)
1568 crtc_t* crtc = &crtcs[c];
1569 XRRCrtcInfo* crtc_info = crtc->crtc_info;
1571 /* if this crtc is already disabled, skip it */
1572 if (crtc_info->mode == None)
1573 continue;
1576 * If this crtc is to be left enabled, make
1577 * sure the old size fits then new screen
1579 if (crtc->mode_info)
1581 XRRModeInfo* old_mode = find_mode_by_xid(crtc_info->mode);
1582 int x, y, w, h;
1583 box_t bounds;
1585 if (!old_mode)
1586 panic(RRSetConfigFailed, crtc);
1588 /* old position and size information */
1589 mode_geometry(old_mode, crtc_info->rotation, &crtc->current_transform.transform, &bounds);
1591 x = crtc_info->x + bounds.x1;
1592 y = crtc_info->y + bounds.y1;
1593 w = bounds.x2 - bounds.x1;
1594 h = bounds.y2 - bounds.y1;
1596 /* if it fits, skip it */
1597 if (x + w <= fb_width && y + h <= fb_height)
1598 continue;
1599 crtc->changing = True;
1601 s = crtc_disable(crtc);
1602 if (s != RRSetConfigSuccess)
1603 panic(s, crtc);
1607 * Set the screen size
1609 screen_apply();
1612 * Set crtcs
1615 for (c = 0; c < res->ncrtc; c++)
1617 crtc_t* crtc = &crtcs[c];
1619 s = crtc_apply(crtc);
1620 if (s != RRSetConfigSuccess)
1621 panic(s, crtc);
1624 set_primary();
1627 * Release the server grab and let all clients
1628 * respond to the updated state
1630 if (grab_server)
1631 XUngrabServer(dpy);
1635 * Use current output state to complete the output list
1637 static void get_outputs(void)
1639 int o;
1640 output_t* q;
1642 for (o = 0; o < res->noutput; o++)
1644 XRROutputInfo* output_info = XRRGetOutputInfo(dpy, res, res->outputs[o]);
1645 output_t* output;
1646 name_t output_name;
1647 if (!output_info)
1648 fatal("could not get output 0x%x information\n", res->outputs[o]);
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);
1653 if (!output)
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
1661 if (automatic)
1663 switch (output_info->connection)
1665 case RR_Connected:
1666 if (!output_info->crtc)
1668 output->changes |= changes_automatic;
1669 output->automatic = True;
1671 break;
1672 case RR_Disconnected:
1673 if (output_info->crtc)
1675 output->changes |= changes_automatic;
1676 output->automatic = True;
1678 break;
1682 output->found = True;
1685 * Automatic mode -- track connection state and enable/disable outputs
1686 * as necessary
1688 if (output->automatic)
1690 switch (output_info->connection)
1692 case RR_Connected:
1693 case RR_UnknownConnection:
1694 if ((!(output->changes & changes_mode)))
1696 set_name_preferred(&output->mode);
1697 output->changes |= changes_mode;
1699 break;
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;
1708 break;
1712 set_output_info(output, res->outputs[o], output_info);
1714 for (q = outputs; q; q = q->next)
1716 if (!q->found)
1718 fprintf(stderr, "warning: output %s not found; ignoring\n", q->output.string);
1723 static void mark_changing_crtcs(void)
1725 int c;
1727 for (c = 0; c < num_crtcs; c++)
1729 crtc_t* crtc = &crtcs[c];
1730 int o;
1731 output_t* output;
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]);
1737 if (!output)
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)
1757 int c;
1758 int l;
1759 output_t* other;
1761 for (c = 0; c < output->output_info->ncrtc; c++)
1762 if (output->output_info->crtcs[c] == crtc->crtc.xid)
1763 break;
1764 if (c == output->output_info->ncrtc)
1765 return False;
1766 for (other = outputs; other; other = other->next)
1768 if (other == output)
1769 continue;
1771 if (other->mode_info == NULL)
1772 continue;
1774 if (other->crtc_info != crtc)
1775 continue;
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)
1780 break;
1781 /* not on the list, can't clone */
1782 if (l == output->output_info->nclone)
1783 return False;
1786 if (crtc->noutput)
1788 /* make sure the state matches */
1789 if (crtc->mode_info != output->mode_info)
1790 return False;
1791 if (crtc->x != output->x)
1792 return False;
1793 if (crtc->y != output->y)
1794 return False;
1795 if (crtc->rotation != output->rotation)
1796 return False;
1797 if (!equal_transform(&crtc->current_transform, &output->transform))
1798 return False;
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)
1806 return False;
1807 if (crtc->crtc_info->x != output->x)
1808 return False;
1809 if (crtc->crtc_info->y != output->y)
1810 return False;
1811 if (crtc->crtc_info->rotation != output->rotation)
1812 return False;
1814 return True;
1817 static crtc_t* find_crtc_for_output(output_t* output)
1819 int c;
1821 for (c = 0; c < output->output_info->ncrtc; c++)
1823 crtc_t* crtc;
1825 crtc = find_crtc_by_xid(output->output_info->crtcs[c]);
1826 if (!crtc)
1827 fatal("cannot find crtc 0x%x\n", output->output_info->crtcs[c]);
1829 if (check_crtc_for_output(crtc, output))
1830 return crtc;
1832 return NULL;
1835 static void set_positions(void)
1837 output_t* output;
1838 Bool keep_going;
1839 Bool any_set;
1840 int min_x, min_y;
1842 for (;;)
1844 any_set = False;
1845 keep_going = False;
1846 for (output = outputs; output; output = output->next)
1848 output_t* relation;
1849 name_t relation_name;
1851 if (!(output->changes & changes_relation))
1852 continue;
1854 if (output->mode_info == NULL)
1855 continue;
1857 init_name(&relation_name);
1858 set_name_string(&relation_name, output->relative_to);
1859 relation = find_output(&relation_name);
1860 if (!relation)
1861 fatal("cannot find output \"%s\"\n", output->relative_to);
1863 if (relation->mode_info == NULL)
1865 output->x = 0;
1866 output->y = 0;
1867 output->changes |= changes_position;
1868 any_set = True;
1869 continue;
1872 * Make sure the dependent object has been set in place
1874 if ((relation->changes & changes_relation) && !(relation->changes & changes_position))
1876 keep_going = True;
1877 continue;
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);
1885 break;
1886 case relation_right_of:
1887 output->y = relation->y;
1888 output->x = relation->x + mode_width(relation->mode_info, relation->rotation);
1889 break;
1890 case relation_above:
1891 output->x = relation->x;
1892 output->y = relation->y - mode_height(output->mode_info, output->rotation);
1893 break;
1894 case relation_below:
1895 output->x = relation->x;
1896 output->y = relation->y + mode_height(relation->mode_info, relation->rotation);
1897 break;
1898 case relation_same_as:
1899 output->x = relation->x;
1900 output->y = relation->y;
1902 output->changes |= changes_position;
1903 any_set = True;
1905 if (!keep_going)
1906 break;
1907 if (!any_set)
1908 fatal("loop in relative position specifications\n");
1912 * Now normalize positions so the upper left corner of all outputs is at 0,0
1914 min_x = 32768;
1915 min_y = 32768;
1916 for (output = outputs; output; output = output->next)
1918 if (output->mode_info == NULL)
1919 continue;
1921 if (output->x < min_x)
1922 min_x = output->x;
1923 if (output->y < min_y)
1924 min_y = output->y;
1926 if (min_x || min_y)
1928 /* move all outputs */
1929 for (output = outputs; output; output = output->next)
1931 if (output->mode_info == NULL)
1932 continue;
1934 output->x -= min_x;
1935 output->y -= min_y;
1936 output->changes |= changes_position;
1941 static void set_screen_size(void)
1943 output_t* output;
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;
1949 int x, y, w, h;
1950 box_t bounds;
1952 if (!mode_info)
1953 continue;
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 */
1961 if (fb_specified)
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 */
1968 else
1970 XRRPanning* pan;
1971 if (x + w > fb_width)
1972 fb_width = x + w;
1973 if (y + h > fb_height)
1974 fb_height = y + h;
1975 if (output->changes & changes_panning)
1976 pan = &output->panning;
1977 else
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);
1989 if (fb_specified)
1991 if (fb_width < minWidth || fb_height < minHeight)
1992 fatal("screen must be at least %dx%d\n", minWidth, minHeight);
1994 else
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)
2006 while (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)
2018 output_t* output;
2019 int best_score;
2020 int my_score;
2021 int score;
2022 crtc_t* best_crtc;
2023 int c;
2025 if (!outputs)
2026 return 0;
2028 output = 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)
2036 return best_score;
2038 best_crtc = NULL;
2040 * Now score with this output any valid crtc
2042 for (c = 0; c < output->output_info->ncrtc; c++)
2044 crtc_t* crtc;
2046 crtc = find_crtc_by_xid(output->output_info->crtcs[c]);
2047 if (!crtc)
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))
2053 continue;
2055 my_score = 1000;
2056 /* slight preference for existing connections */
2057 if (crtc == output->current_crtc_info)
2058 my_score++;
2060 output->crtc_info = crtc;
2061 score = my_score + pick_crtcs_score(outputs);
2062 if (score > best_score)
2064 best_crtc = crtc;
2065 best_score = 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);
2075 return best_score;
2079 * Pick crtcs for any changing outputs that don't have one
2081 static void pick_crtcs(void)
2083 output_t* output;
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])))
2097 break;
2099 else
2101 output->crtc_info = find_crtc_for_output(output);
2102 if (!output->crtc_info)
2103 break;
2108 * Everyone is happy
2110 if (!output)
2111 return;
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)
2130 char* endptr;
2131 int result = strtol(s, &endptr, 10);
2132 if (s == endptr)
2133 usage();
2134 return result;
2137 static double check_strtod(char* s)
2139 char* endptr;
2140 double result = strtod(s, &endptr);
2141 if (s == endptr)
2142 usage();
2143 return result;
2146 int main(int argc, char** argv)
2148 XRRScreenSize* sizes;
2149 XRRScreenConfiguration* sc;
2150 int nsize;
2151 int nrate;
2152 short* rates;
2153 Status status = RRSetConfigFailed;
2154 int rot = -1;
2155 int query = False;
2156 int action_requested = False;
2157 Rotation rotation, current_rotation, rotations;
2158 XRRScreenChangeNotifyEvent event;
2159 XRRScreenChangeNotifyEvent* sce;
2160 char* display_name = NULL;
2161 int i, j;
2162 SizeID current_size;
2163 short current_rate;
2164 double rate = -1;
2165 int size = -1;
2166 int dirind = 0;
2167 Bool setit = False;
2168 Bool version = False;
2169 int event_base, error_base;
2170 int reflection = 0;
2171 int width = 0, height = 0;
2172 Bool have_pixel_size = False;
2173 int ret = 0;
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;
2180 int major, minor;
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]))
2188 if (++i >= argc)
2189 usage();
2190 display_name = argv[i];
2191 continue;
2193 if (!strcmp("-help", argv[i]))
2195 usage();
2196 action_requested = True;
2197 continue;
2199 if (!strcmp("--verbose", argv[i]))
2201 verbose = True;
2202 continue;
2204 if (!strcmp("--dryrun", argv[i]))
2206 dryrun = True;
2207 verbose = True;
2208 continue;
2210 if (!strcmp("--nograb", argv[i]))
2212 grab_server = False;
2213 continue;
2215 if (!strcmp("--current", argv[i]))
2217 current = True;
2218 continue;
2221 if (!strcmp("-s", argv[i]) || !strcmp("--size", argv[i]))
2223 if (++i >= argc)
2224 usage();
2225 if (sscanf(argv[i], "%dx%d", &width, &height) == 2)
2227 have_pixel_size = True;
2229 else
2231 size = check_strtol(argv[i]);
2232 if (size < 0)
2233 usage();
2235 setit = True;
2236 action_requested = True;
2237 continue;
2240 if (!strcmp("-r", argv[i]) || !strcmp("--rate", argv[i]) || !strcmp("--refresh", argv[i]))
2242 if (++i >= argc)
2243 usage();
2244 rate = check_strtod(argv[i]);
2245 setit = True;
2246 if (output)
2248 output->refresh = rate;
2249 output->changes |= changes_refresh;
2250 setit_1_2 = True;
2252 action_requested = True;
2253 continue;
2256 if (!strcmp("-v", argv[i]) || !strcmp("--version", argv[i]))
2258 version = True;
2259 action_requested = True;
2260 continue;
2263 if (!strcmp("-x", argv[i]))
2265 reflection |= RR_Reflect_X;
2266 setit = True;
2267 action_requested = True;
2268 continue;
2270 if (!strcmp("-y", argv[i]))
2272 reflection |= RR_Reflect_Y;
2273 setit = True;
2274 action_requested = True;
2275 continue;
2277 if (!strcmp("--screen", argv[i]))
2279 if (++i >= argc)
2280 usage();
2281 screen = check_strtol(argv[i]);
2282 if (screen < 0)
2283 usage();
2284 continue;
2286 if (!strcmp("-q", argv[i]) || !strcmp("--query", argv[i]))
2288 query = True;
2289 continue;
2291 if (!strcmp("-o", argv[i]) || !strcmp("--orientation", argv[i]))
2293 char* endptr;
2294 if (++i >= argc)
2295 usage();
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)
2302 break;
2304 if ((dirind < 0) || (dirind > 3))
2305 usage();
2307 rot = dirind;
2308 setit = True;
2309 action_requested = True;
2310 continue;
2312 if (!strcmp("--prop", argv[i]) || !strcmp("--props", argv[i]) ||
2313 !strcmp("--madprops", argv[i]) || !strcmp("--properties", argv[i]))
2315 query_1_2 = True;
2316 properties = True;
2317 action_requested = True;
2318 continue;
2320 if (!strcmp("--output", argv[i]))
2322 if (++i >= argc)
2323 usage();
2325 output = find_output_by_name(argv[i]);
2326 if (!output)
2328 output = add_output();
2329 set_name(&output->output, argv[i], name_string | name_xid);
2332 setit_1_2 = True;
2333 action_requested = True;
2334 continue;
2336 if (!strcmp("--crtc", argv[i]))
2338 if (++i >= argc)
2339 usage();
2340 if (!output)
2341 usage();
2342 set_name(&output->crtc, argv[i], name_xid | name_index);
2343 output->changes |= changes_crtc;
2344 continue;
2346 if (!strcmp("--mode", argv[i]))
2348 if (++i >= argc)
2349 usage();
2350 if (!output)
2351 usage();
2352 set_name(&output->mode, argv[i], name_string | name_xid);
2353 output->changes |= changes_mode;
2354 continue;
2356 if (!strcmp("--preferred", argv[i]))
2358 if (!output)
2359 usage();
2360 set_name_preferred(&output->mode);
2361 output->changes |= changes_mode;
2362 continue;
2364 if (!strcmp("--pos", argv[i]))
2366 if (++i >= argc)
2367 usage();
2368 if (!output)
2369 usage();
2370 if (sscanf(argv[i], "%dx%d", &output->x, &output->y) != 2)
2371 usage();
2372 output->changes |= changes_position;
2373 continue;
2375 if (!strcmp("--rotation", argv[i]) || !strcmp("--rotate", argv[i]))
2377 if (++i >= argc)
2378 usage();
2379 if (!output)
2380 usage();
2381 for (dirind = 0; dirind < 4; dirind++)
2383 if (strcmp(direction[dirind], argv[i]) == 0)
2384 break;
2386 if (dirind == 4)
2387 usage();
2388 output->rotation &= ~0xf;
2389 output->rotation |= 1 << dirind;
2390 output->changes |= changes_rotation;
2391 continue;
2393 if (!strcmp("--reflect", argv[i]) || !strcmp("--reflection", argv[i]))
2395 if (++i >= argc)
2396 usage();
2397 if (!output)
2398 usage();
2399 for (dirind = 0; dirind < 4; dirind++)
2401 if (strcmp(reflections[dirind], argv[i]) == 0)
2402 break;
2404 if (dirind == 4)
2405 usage();
2406 output->rotation &= ~(RR_Reflect_X | RR_Reflect_Y);
2407 output->rotation |= dirind * RR_Reflect_X;
2408 output->changes |= changes_reflection;
2409 continue;
2411 if (!strcmp("--left-of", argv[i]))
2413 if (++i >= argc)
2414 usage();
2415 if (!output)
2416 usage();
2417 output->relation = relation_left_of;
2418 output->relative_to = argv[i];
2419 output->changes |= changes_relation;
2420 continue;
2422 if (!strcmp("--right-of", argv[i]))
2424 if (++i >= argc)
2425 usage();
2426 if (!output)
2427 usage();
2428 output->relation = relation_right_of;
2429 output->relative_to = argv[i];
2430 output->changes |= changes_relation;
2431 continue;
2433 if (!strcmp("--above", argv[i]))
2435 if (++i >= argc)
2436 usage();
2437 if (!output)
2438 usage();
2439 output->relation = relation_above;
2440 output->relative_to = argv[i];
2441 output->changes |= changes_relation;
2442 continue;
2444 if (!strcmp("--below", argv[i]))
2446 if (++i >= argc)
2447 usage();
2448 if (!output)
2449 usage();
2450 output->relation = relation_below;
2451 output->relative_to = argv[i];
2452 output->changes |= changes_relation;
2453 continue;
2455 if (!strcmp("--same-as", argv[i]))
2457 if (++i >= argc)
2458 usage();
2459 if (!output)
2460 usage();
2461 output->relation = relation_same_as;
2462 output->relative_to = argv[i];
2463 output->changes |= changes_relation;
2464 continue;
2466 if (!strcmp("--panning", argv[i]))
2468 XRRPanning* pan;
2469 if (++i >= argc)
2470 usage();
2471 if (!output)
2472 usage();
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))
2479 case 2:
2480 pan->left = pan->top = 0;
2481 /* fall through */
2482 __attribute__((fallthrough));
2483 case 4:
2484 pan->track_left = pan->track_top = pan->track_width = pan->track_height = 0;
2485 /* fall through */
2486 __attribute__((fallthrough));
2487 case 8:
2488 pan->border_left = pan->border_top = pan->border_right = pan->border_bottom = 0;
2489 /* fall through */
2490 __attribute__((fallthrough));
2491 case 12:
2492 break;
2493 default:
2494 usage();
2496 output->changes |= changes_panning;
2497 continue;
2499 if (!strcmp("--gamma", argv[i]))
2501 if (!output)
2502 usage();
2503 if (++i >= argc)
2504 usage();
2505 if (sscanf(argv[i], "%f:%f:%f", &output->gamma.red, &output->gamma.green,
2506 &output->gamma.blue) != 3)
2507 usage();
2508 output->changes |= changes_gamma;
2509 setit_1_2 = True;
2510 continue;
2512 if (!strcmp("--brightness", argv[i]))
2514 if (!output)
2515 usage();
2516 if (++i >= argc)
2517 usage();
2518 if (sscanf(argv[i], "%f", &output->brightness) != 1)
2519 usage();
2520 output->changes |= changes_gamma;
2521 setit_1_2 = True;
2522 continue;
2524 if (!strcmp("--primary", argv[i]))
2526 if (!output)
2527 usage();
2528 output->changes |= changes_primary;
2529 output->primary = True;
2530 setit_1_2 = True;
2531 continue;
2533 if (!strcmp("--noprimary", argv[i]))
2535 no_primary = True;
2536 setit_1_2 = True;
2537 continue;
2539 if (!strcmp("--set", argv[i]))
2541 output_prop_t* prop;
2542 if (!output)
2543 usage();
2544 prop = malloc(sizeof(output_prop_t));
2545 prop->next = output->props;
2546 output->props = prop;
2547 if (++i >= argc)
2548 usage();
2549 prop->name = argv[i];
2550 if (++i >= argc)
2551 usage();
2552 prop->value = argv[i];
2553 propit = True;
2554 output->changes |= changes_property;
2555 setit_1_2 = True;
2556 continue;
2558 if (!strcmp("--scale", argv[i]))
2560 double sx, sy;
2561 if (!output)
2562 usage();
2563 if (++i >= argc)
2564 usage();
2565 if (sscanf(argv[i], "%lfx%lf", &sx, &sy) != 2)
2566 usage();
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";
2573 else
2574 output->transform.filter = "nearest";
2575 output->transform.nparams = 0;
2576 output->transform.params = NULL;
2577 output->changes |= changes_transform;
2578 continue;
2580 if (!strcmp("--scale-from", argv[i]))
2582 int w, h;
2583 if (!output)
2584 usage();
2585 if (++i >= argc)
2586 usage();
2587 if (sscanf(argv[i], "%dx%d", &w, &h) != 2)
2588 usage();
2589 if (w <= 0 || h <= 0)
2590 usage();
2591 output->scale_from_w = w;
2592 output->scale_from_h = h;
2593 output->changes |= changes_transform;
2594 continue;
2596 if (!strcmp("--transform", argv[i]))
2598 double transform[3][3];
2599 int k, l;
2600 if (!output)
2601 usage();
2602 if (++i >= argc)
2603 usage();
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)
2610 usage();
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;
2622 continue;
2624 if (!strcmp("--off", argv[i]))
2626 if (!output)
2627 usage();
2628 set_name_xid(&output->mode, None);
2629 set_name_xid(&output->crtc, None);
2630 output->changes |= changes_mode;
2631 continue;
2633 if (!strcmp("--fb", argv[i]))
2635 if (++i >= argc)
2636 usage();
2637 if (sscanf(argv[i], "%dx%d", &fb_width, &fb_height) != 2)
2638 usage();
2639 setit_1_2 = True;
2640 action_requested = True;
2641 continue;
2643 if (!strcmp("--fbmm", argv[i]))
2645 if (++i >= argc)
2646 usage();
2647 if (sscanf(argv[i], "%dx%d", &fb_width_mm, &fb_height_mm) != 2)
2648 usage();
2649 setit_1_2 = True;
2650 action_requested = True;
2651 continue;
2653 if (!strcmp("--dpi", argv[i]))
2655 char* strtod_error;
2656 if (++i >= argc)
2657 usage();
2658 dpi = strtod(argv[i], &strtod_error);
2659 if (argv[i] == strtod_error)
2661 dpi = 0.0;
2662 dpi_output = argv[i];
2664 setit_1_2 = True;
2665 action_requested = True;
2666 continue;
2668 if (!strcmp("--auto", argv[i]))
2670 if (output)
2672 output->automatic = True;
2673 output->changes |= changes_automatic;
2675 else
2676 automatic = True;
2677 setit_1_2 = True;
2678 action_requested = True;
2679 continue;
2681 if (!strcmp("--q12", argv[i]))
2683 query_1_2 = True;
2684 continue;
2686 if (!strcmp("--q1", argv[i]))
2688 query_1 = True;
2689 continue;
2691 if (!strcmp("--newmode", argv[i]))
2693 umode_t* m = malloc(sizeof(umode_t));
2694 double clock;
2696 ++i;
2697 if (i + 9 >= argc)
2698 usage();
2699 m->mode.name = argv[i];
2700 m->mode.nameLength = strlen(argv[i]);
2701 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;
2714 while (i < argc)
2716 int f;
2718 for (f = 0; mode_flags[f].string; f++)
2719 if (!strcasecmp(mode_flags[f].string, argv[i]))
2720 break;
2722 if (!mode_flags[f].string)
2723 break;
2724 m->mode.modeFlags |= mode_flags[f].flag;
2725 i++;
2727 m->next = umodes;
2728 m->action = umode_create;
2729 umodes = m;
2730 modeit = True;
2731 action_requested = True;
2732 continue;
2734 if (!strcmp("--rmmode", argv[i]))
2736 umode_t* m = malloc(sizeof(umode_t));
2738 if (++i >= argc)
2739 usage();
2740 set_name(&m->name, argv[i], name_string | name_xid);
2741 m->action = umode_destroy;
2742 m->next = umodes;
2743 umodes = m;
2744 modeit = True;
2745 action_requested = True;
2746 continue;
2748 if (!strcmp("--addmode", argv[i]))
2750 umode_t* m = malloc(sizeof(umode_t));
2752 if (++i >= argc)
2753 usage();
2754 set_name(&m->output, argv[i], name_string | name_xid);
2755 if (++i >= argc)
2756 usage();
2757 set_name(&m->name, argv[i], name_string | name_xid);
2758 m->action = umode_add;
2759 m->next = umodes;
2760 umodes = m;
2761 modeit = True;
2762 action_requested = True;
2763 continue;
2765 if (!strcmp("--delmode", argv[i]))
2767 umode_t* m = malloc(sizeof(umode_t));
2769 if (++i >= argc)
2770 usage();
2771 set_name(&m->output, argv[i], name_string | name_xid);
2772 if (++i >= argc)
2773 usage();
2774 set_name(&m->name, argv[i], name_string | name_xid);
2775 m->action = umode_delete;
2776 m->next = umodes;
2777 umodes = m;
2778 modeit = True;
2779 action_requested = True;
2780 continue;
2782 usage();
2784 if (!action_requested)
2785 query = True;
2786 if (verbose)
2788 query = True;
2789 if (setit && !setit_1_2)
2790 query_1 = True;
2794 if (version)
2795 printf("xrandr program version " VERSION "\n");
2798 dpy = XOpenDisplay(display_name);
2800 if (dpy == NULL)
2802 fprintf(stderr, "Can't open display %s\n", XDisplayName(display_name));
2803 exit(1);
2805 if (screen < 0)
2806 screen = DefaultScreen(dpy);
2807 if (screen >= ScreenCount(dpy))
2809 fprintf(stderr, "Invalid screen number %d (display has %d)\n", screen, ScreenCount(dpy));
2810 exit(1);
2813 root = RootWindow(dpy, screen);
2815 if (!XRRQueryExtension(dpy, &event_base, &error_base) || !XRRQueryVersion(dpy, &major, &minor))
2817 fprintf(stderr, "RandR extension missing\n");
2818 exit(1);
2820 if (major > 1 || (major == 1 && minor >= 2))
2821 has_1_2 = True;
2822 if (major > 1 || (major == 1 && minor >= 3))
2823 has_1_3 = True;
2825 if (has_1_2 && modeit)
2827 umode_t* m;
2829 get_screen(current);
2830 get_crtcs();
2831 get_outputs();
2833 for (m = umodes; m; m = m->next)
2835 XRRModeInfo* e;
2836 output_t* o;
2838 switch (m->action)
2840 case umode_create:
2841 XRRCreateMode(dpy, root, &m->mode);
2842 break;
2843 case umode_destroy:
2844 e = find_mode(&m->name, 0);
2845 if (!e)
2846 fatal("cannot find mode \"%s\"\n", m->name.string);
2847 XRRDestroyMode(dpy, e->id);
2848 break;
2849 case umode_add:
2850 o = find_output(&m->output);
2851 if (!o)
2852 fatal("cannot find output \"%s\"\n", m->output.string);
2853 e = find_mode(&m->name, 0);
2854 if (!e)
2855 fatal("cannot find mode \"%s\"\n", m->name.string);
2856 XRRAddOutputMode(dpy, o->output.xid, e->id);
2857 break;
2858 case umode_delete:
2859 o = find_output(&m->output);
2860 if (!o)
2861 fatal("cannot find output \"%s\"\n", m->output.string);
2862 e = find_mode(&m->name, 0);
2863 if (!e)
2864 fatal("cannot find mode \"%s\"\n", m->name.string);
2865 XRRDeleteOutputMode(dpy, o->output.xid, e->id);
2866 break;
2869 if (!setit_1_2)
2871 XSync(dpy, False);
2872 exit(0);
2875 if (has_1_2 && propit)
2878 get_screen(current);
2879 get_crtcs();
2880 get_outputs();
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);
2889 Atom type;
2890 int format = 0;
2891 unsigned char* data;
2892 int nelements;
2893 int int_value;
2894 unsigned long ulong_value;
2895 unsigned char* prop_data;
2896 int actual_format;
2897 unsigned long nitems, bytes_after;
2898 Atom actual_type;
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)))
2909 type = actual_type;
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))
2917 type = XA_INTEGER;
2918 ulong_value = int_value;
2919 data = (unsigned char*)&ulong_value;
2920 nelements = 1;
2921 format = 32;
2923 else if (type == XA_ATOM)
2925 ulong_value = XInternAtom(dpy, prop->value, False);
2926 data = (unsigned char*)&ulong_value;
2927 nelements = 1;
2929 else if ((type == XA_STRING || type == AnyPropertyType))
2931 type = XA_STRING;
2932 data = (unsigned char*)prop->value;
2933 nelements = strlen(prop->value);
2934 format = 8;
2936 else
2937 continue;
2938 XRRChangeOutputProperty(dpy, output->output.xid, name, type, format, PropModeReplace, data,
2939 nelements);
2942 if (!setit_1_2)
2944 XSync(dpy, False);
2945 exit(0);
2948 if (setit_1_2)
2950 get_screen(current);
2951 get_crtcs();
2952 get_outputs();
2953 set_positions();
2954 set_screen_size();
2956 pick_crtcs();
2959 * Assign outputs to crtcs
2961 set_crtcs();
2964 * Mark changing crtcs
2966 mark_changing_crtcs();
2969 * If an output was specified to track dpi, use it
2971 if (dpi_output)
2973 output_t* output = find_output_by_name(dpi_output);
2974 XRROutputInfo* output_info;
2975 XRRModeInfo* mode_info;
2976 if (!output)
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;
2992 else
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) ||
3005 dpi != 0.0)
3007 if (dpi <= 0)
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;
3013 else
3015 fb_width_mm = DisplayWidthMM(dpy, screen);
3016 fb_height_mm = DisplayHeightMM(dpy, screen);
3021 * Set panning
3023 set_panning();
3026 * Set gamma on crtc's that belong to the outputs.
3028 set_gamma();
3031 * Now apply all of the changes
3033 apply();
3035 XSync(dpy, False);
3036 exit(0);
3038 if (query_1_2 || (query && has_1_2 && !query_1))
3040 output_t* output;
3042 #define ModeShown 0x80000000
3044 get_screen(current);
3045 get_crtcs();
3046 get_outputs();
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;
3059 Atom* props;
3060 int j, k, nprop;
3061 Bool* mode_shown;
3062 // Rotation rotations = output_rotations (output);
3064 printf(" <output name=\"%s\" connected=\"%s\"", output_info->name,
3065 connection[output_info->connection]);
3066 if (mode)
3068 if (crtc_info)
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);
3073 else
3075 printf(" w=\"%d\" h=\"%d\" x=\"%d\" y=\"%d\"", mode->width, mode->height, output->x,
3076 output->y);
3078 if (verbose)
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)
3090 Bool first = True;
3091 printf (" (");
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;
3101 printf ("x axis");
3103 if (rotations & RR_Reflect_Y)
3105 if (!first) printf (" ");
3106 printf ("y axis");
3108 printf (")");
3112 if (mode)
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);
3131 printf(">\n");
3133 if (verbose)
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]);
3149 if (clone)
3150 printf(" %s", clone->output.string);
3152 printf("\n");
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]);
3159 if (crtc)
3160 printf(" %d", crtc->crtc.index);
3162 printf("\n");
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);
3173 if (verbose)
3175 int x, y;
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]));
3182 if (y < 2)
3183 printf("\n\t ");
3185 if (output->transform.filter)
3186 printf("\n\t filter: %s", output->transform.filter);
3187 printf("\n");
3189 if (verbose || properties)
3191 props = XRRListOutputProperties(dpy, output->output.xid, &nprop);
3192 for (j = 0; j < nprop; j++)
3194 unsigned char* prop;
3195 int actual_format;
3196 unsigned long nitems, bytes_after;
3197 Atom actual_type;
3198 XRRPropertyInfo* propinfo;
3200 XRRGetOutputProperty(dpy, output->output.xid, props[j], 0, 100, False, False,
3201 AnyPropertyType, &actual_type, &actual_format, &nitems, &bytes_after,
3202 &prop);
3204 propinfo = XRRQueryOutputProperty(dpy, output->output.xid, props[j]);
3206 if (actual_type == XA_INTEGER && actual_format == 8)
3208 int k;
3210 printf("\t%s:\n", XGetAtomName(dpy, props[j]));
3211 for (k = 0; k < (long)nitems; k++)
3213 if (k % 16 == 0)
3214 printf("\t\t");
3215 printf("%02x", (unsigned char)prop[k]);
3216 if (k % 16 == 15)
3217 printf("\n");
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++)
3225 if (k > 0)
3226 printf("\n\t\t\t");
3227 printf("%d (0x%08x)", (int)((INT32*)prop)[k], (int)((INT32*)prop)[k]);
3230 if (propinfo->range && propinfo->num_values > 0)
3232 if (nitems > 1)
3233 printf("\n\t\t");
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]);
3240 printf("\n");
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)
3248 printf("\n\t\t");
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)
3260 printf("\n\t\t ");
3263 printf("\n");
3265 else if (actual_format == 8)
3267 printf("\t%s: %s%s\n", XGetAtomName(dpy, props[j]), prop, bytes_after ? "..." : "");
3269 else
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);
3276 free(propinfo);
3280 if (verbose)
3282 for (j = 0; j < output_info->nmode; j++)
3284 XRRModeInfo* mode = find_mode_by_xid(output_info->modes[j]);
3285 int f;
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");
3296 printf("\n");
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;
3305 else
3307 mode_shown = calloc(output_info->nmode, sizeof(Bool));
3308 if (!mode_shown)
3309 fatal("out of memory\n");
3310 for (j = 0; j < output_info->nmode; j++)
3312 XRRModeInfo *jmode, *kmode;
3314 if (mode_shown[j])
3315 continue;
3317 jmode = find_mode_by_xid(output_info->modes[j]);
3318 for (k = j; k < output_info->nmode; k++)
3320 if (mode_shown[k])
3321 continue;
3322 kmode = find_mode_by_xid(output_info->modes[k]);
3323 if (strcmp(jmode->name, kmode->name) != 0)
3324 continue;
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\"");
3331 else
3332 printf(" current=\"false\"");
3333 if (k < output_info->npreferred)
3334 printf(" preferred=\"true\"");
3335 else
3336 printf(" preferred=\"false\"");
3337 printf("/>\n");
3340 free(mode_shown);
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");
3365 exit(0);
3368 sc = XRRGetScreenInfo(dpy, root);
3370 if (sc == NULL)
3371 exit(1);
3373 current_size = XRRConfigCurrentConfiguration(sc, &current_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)
3382 break;
3384 if (size >= nsize)
3386 fprintf(stderr, "Size %dx%d not found in available modes\n", width, height);
3387 exit(1);
3390 else if (size < 0)
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);
3395 exit(1);
3398 if (rot < 0)
3400 for (rot = 0; rot < 4; rot++)
3401 if (1 << rot == (current_rotation & 0xf))
3402 break;
3405 current_rate = XRRConfigCurrentRate(sc);
3407 if (rate < 0)
3409 if (size == current_size)
3410 rate = current_rate;
3411 else
3412 rate = 0;
3414 else
3416 rates = XRRConfigRates(sc, size, &nrate);
3417 for (i = 0; i < nrate; i++)
3418 if (rate == rates[i])
3419 break;
3420 if (i == nrate)
3422 fprintf(stderr, "Rate %.1f Hz not available for this size\n", rate);
3423 exit(1);
3427 if (version)
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);
3442 if (nrate)
3443 printf(" ");
3444 for (j = 0; j < nrate; j++)
3445 printf("%c%-4d", i == current_size && rates[j] == current_rate ? '*' : ' ', rates[j]);
3446 printf("\n");
3450 rotations = XRRConfigRotations(sc, &current_rotation);
3452 rotation = 1 << rot;
3453 if (query)
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]);
3465 printf("\n");
3467 printf("Reflections possible - ");
3468 if (rotations & (RR_Reflect_X | RR_Reflect_Y))
3470 if (rotations & RR_Reflect_X)
3471 printf("X Axis ");
3472 if (rotations & RR_Reflect_Y)
3473 printf("Y Axis");
3475 else
3476 printf("none");
3477 printf("\n");
3480 if (verbose)
3482 printf("Setting size to %d, rotation to %s\n", size, direction[rot]);
3484 printf("Setting reflection on ");
3485 if (reflection)
3487 if (reflection & RR_Reflect_X)
3488 printf("X Axis ");
3489 if (reflection & RR_Reflect_Y)
3490 printf("Y Axis");
3492 else
3493 printf("neither axis");
3494 printf("\n");
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");
3515 ret = 1;
3518 if (verbose && setit && !dryrun && size != current_size)
3520 if (status == RRSetConfigSuccess)
3522 Bool seen_screen = False;
3523 while (!seen_screen)
3525 int spo;
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,
3546 sce->mheight);
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);
3554 else
3555 printf("new Subpixel rendering model is %s\n", order[spo]);
3556 seen_screen = True;
3557 break;
3558 default:
3559 if (event.type != ConfigureNotify)
3560 printf("unknown event received, type = %d!\n", event.type);
3565 XRRFreeScreenConfigInfo(sc);
3566 return (ret);