Merge pull request #25959 from neo1973/TagLib_deprecation_warnings
[xbmc.git] / xbmc-xrandr.c
blob8a95799db1442bb87b379fdd7ad47636ac70f643
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 static char* rotation_name(Rotation rotation)
164 int i;
166 if ((rotation & 0xf) == 0)
167 return "normal";
168 for (i = 0; i < 4; i++)
169 if (rotation & (1 << i))
170 return direction[i];
171 return "invalid rotation";
174 static char* reflection_name(Rotation rotation)
176 rotation &= (RR_Reflect_X | RR_Reflect_Y);
177 switch (rotation)
179 case 0:
180 return "none";
181 case RR_Reflect_X:
182 return "X axis";
183 case RR_Reflect_Y:
184 return "Y axis";
185 case RR_Reflect_X | RR_Reflect_Y:
186 return "X and Y axis";
188 return "invalid reflection";
191 typedef enum _relation
193 relation_left_of,
194 relation_right_of,
195 relation_above,
196 relation_below,
197 relation_same_as,
198 } relation_t;
200 typedef struct
202 int x, y, width, height;
203 } rectangle_t;
205 typedef struct
207 int x1, y1, x2, y2;
208 } box_t;
210 typedef struct
212 int x, y;
213 } point_t;
215 typedef enum _changes
217 changes_none = 0,
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),
231 } changes_t;
233 typedef enum _name_kind
235 name_none = 0,
236 name_string = (1 << 0),
237 name_xid = (1 << 1),
238 name_index = (1 << 2),
239 name_preferred = (1 << 3),
240 } name_kind_t;
242 typedef struct
244 name_kind_t kind;
245 char* string;
246 XID xid;
247 int index;
248 } name_t;
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;
256 struct _transform
258 XTransform transform;
259 char* filter;
260 int nparams;
261 XFixed* params;
264 struct _crtc
266 name_t crtc;
267 Bool changing;
268 XRRCrtcInfo* crtc_info;
270 XRRModeInfo* mode_info;
271 XRRPanning* panning_info;
272 int x;
273 int y;
274 Rotation rotation;
275 output_t** outputs;
276 int noutput;
277 transform_t current_transform, pending_transform;
280 struct _output_prop
282 struct _output_prop* next;
283 char* name;
284 char* value;
287 struct _output
289 struct _output* next;
291 changes_t changes;
293 output_prop_t* props;
295 name_t output;
296 XRROutputInfo* output_info;
298 name_t crtc;
299 crtc_t* crtc_info;
300 crtc_t* current_crtc_info;
302 name_t mode;
303 double refresh;
304 XRRModeInfo* mode_info;
306 name_t addmode;
308 relation_t relation;
309 char* relative_to;
311 int x, y;
312 Rotation rotation;
314 XRRPanning panning;
316 Bool automatic;
317 int scale_from_w, scale_from_h;
318 transform_t transform;
320 struct
322 float red;
323 float green;
324 float blue;
325 } gamma;
327 float brightness;
329 Bool primary;
331 Bool found;
334 typedef enum _umode_action
336 umode_create,
337 umode_destroy,
338 umode_add,
339 umode_delete
340 } umode_action_t;
343 struct _umode
345 struct _umode* next;
347 umode_action_t action;
348 XRRModeInfo mode;
349 name_t output;
350 name_t name;
355 static char *connection[3] = {
356 "connected",
357 "disconnected",
358 "unknown connection"};
362 static char* connection[3] = {"true", "false", "unknown"};
364 #define OUTPUT_NAME 1
366 #define CRTC_OFF 2
367 #define CRTC_UNSET 3
368 #define CRTC_INDEX 0x40000000
370 #define MODE_NAME 1
371 #define MODE_OFF 2
372 #define MODE_UNSET 3
373 #define MODE_PREF 4
375 #define POS_UNSET -1
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)
396 case RR_Rotate_0:
397 case RR_Rotate_180:
398 return mode_info->height;
399 case RR_Rotate_90:
400 case RR_Rotate_270:
401 return mode_info->width;
402 default:
403 return 0;
407 static int mode_width(XRRModeInfo* mode_info, Rotation rotation)
409 switch (rotation & 0xf)
411 case RR_Rotate_0:
412 case RR_Rotate_180:
413 return mode_info->width;
414 case RR_Rotate_90:
415 case RR_Rotate_270:
416 return mode_info->height;
417 default:
418 return 0;
422 static Bool transform_point(XTransform* transform, double* xp, double* yp)
424 double vector[3];
425 double result[3];
426 int i, j;
427 double v;
429 vector[0] = *xp;
430 vector[1] = *yp;
431 vector[2] = 1;
432 for (j = 0; j < 3; j++)
434 v = 0;
435 for (i = 0; i < 3; i++)
436 v += (XFixedToDouble(transform->matrix[j][i]) * vector[i]);
437 result[j] = v;
439 if (!result[2])
440 return False;
441 for (j = 0; j < 2; j++)
443 vector[j] = result[j] / result[2];
444 if (vector[j] > 32767 || vector[j] < -32767)
445 return False;
447 *xp = vector[0];
448 *yp = vector[1];
449 return True;
452 static void path_bounds(XTransform* transform, point_t* points, int npoints, box_t* box)
454 int i;
455 box_t point;
457 for (i = 0; i < npoints; i++)
459 double x, y;
460 x = points[i].x;
461 y = points[i].y;
462 transform_point(transform, &x, &y);
463 point.x1 = floor(x);
464 point.y1 = floor(y);
465 point.x2 = ceil(x);
466 point.y2 = ceil(y);
467 if (i == 0)
468 *box = point;
469 else
471 if (point.x1 < box->x1)
472 box->x1 = point.x1;
473 if (point.y1 < box->y1)
474 box->y1 = point.y1;
475 if (point.x2 > box->x2)
476 box->x2 = point.x2;
477 if (point.y2 > box->y2)
478 box->y2 = point.y2;
483 static void mode_geometry(XRRModeInfo* mode_info,
484 Rotation rotation,
485 XTransform* transform,
486 box_t* bounds)
488 point_t rect[4];
489 int width = mode_width(mode_info, rotation);
490 int height = mode_height(mode_info, rotation);
492 rect[0].x = 0;
493 rect[0].y = 0;
494 rect[1].x = width;
495 rect[1].y = 0;
496 rect[2].x = width;
497 rect[2].y = height;
498 rect[3].x = 0;
499 rect[3].y = height;
500 path_bounds(transform, rect, 4, bounds);
503 /* v refresh frequency in Hz */
504 static double mode_refresh(XRRModeInfo* mode_info)
506 double rate;
508 if (mode_info->hTotal && mode_info->vTotal)
509 rate = ((double)mode_info->dotClock / ((double)mode_info->hTotal * (double)mode_info->vTotal));
510 else
511 rate = 0;
512 return rate;
515 /* h sync frequency in Hz */
516 static double mode_hsync(XRRModeInfo* mode_info)
518 double rate;
520 if (mode_info->hTotal)
521 rate = (double)mode_info->dotClock / (double)mode_info->hTotal;
522 else
523 rate = 0;
524 return rate;
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;
541 name->xid = xid;
544 static void set_name_index(name_t* name, int index)
546 name->kind |= name_index;
547 name->index = 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 */
570 int index;
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);
578 else
579 usage();
582 static void init_transform(transform_t* transform)
584 int x;
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 if (nparams)
601 dest->params = malloc(nparams * sizeof(XFixed));
602 memcpy(dest->params, params, nparams * sizeof(XFixed));
604 else
605 dest->params = NULL;
608 static void copy_transform(transform_t* dest, transform_t* src)
610 set_transform(dest, &src->transform, src->filter, src->params, src->nparams);
613 static Bool equal_transform(transform_t* a, transform_t* b)
615 if (memcmp(&a->transform, &b->transform, sizeof(XTransform)) != 0)
616 return False;
617 if (strcmp(a->filter, b->filter) != 0)
618 return False;
619 if (a->nparams != b->nparams)
620 return False;
621 if (memcmp(a->params, b->params, a->nparams * sizeof(XFixed)) != 0)
622 return False;
623 return True;
626 static output_t* add_output(void)
628 output_t* output = calloc(1, sizeof(output_t));
630 if (!output)
631 fatal("out of memory\n");
632 output->next = NULL;
633 output->found = False;
634 output->brightness = 1.0;
635 *outputs_tail = output;
636 outputs_tail = &output->next;
637 return output;
640 static output_t* find_output(name_t* name)
642 output_t* output;
644 for (output = outputs; output; output = output->next)
646 name_kind_t common = name->kind & output->output.kind;
648 if ((common & name_xid) && name->xid == output->output.xid)
649 break;
650 if ((common & name_string) && !strcmp(name->string, output->output.string))
651 break;
652 if ((common & name_index) && name->index == output->output.index)
653 break;
655 return output;
658 static output_t* find_output_by_xid(RROutput output)
660 name_t output_name;
662 init_name(&output_name);
663 set_name_xid(&output_name, output);
664 return find_output(&output_name);
667 static output_t* find_output_by_name(char* name)
669 name_t output_name;
671 init_name(&output_name);
672 set_name_string(&output_name, name);
673 return find_output(&output_name);
676 static crtc_t* find_crtc(name_t* name)
678 int c;
679 crtc_t* crtc = NULL;
681 for (c = 0; c < num_crtcs; c++)
683 name_kind_t common;
685 crtc = &crtcs[c];
686 common = name->kind & crtc->crtc.kind;
688 if ((common & name_xid) && name->xid == crtc->crtc.xid)
689 break;
690 if ((common & name_string) && !strcmp(name->string, crtc->crtc.string))
691 break;
692 if ((common & name_index) && name->index == crtc->crtc.index)
693 break;
694 crtc = NULL;
696 return crtc;
699 static crtc_t* find_crtc_by_xid(RRCrtc crtc)
701 name_t crtc_name;
703 init_name(&crtc_name);
704 set_name_xid(&crtc_name, crtc);
705 return find_crtc(&crtc_name);
708 static XRRModeInfo* find_mode(name_t* name, double refresh)
710 int m;
711 XRRModeInfo* best = NULL;
712 double bestDist = 0;
714 for (m = 0; m < res->nmode; m++)
716 XRRModeInfo* mode = &res->modes[m];
717 if ((name->kind & name_xid) && name->xid == mode->id)
719 best = mode;
720 break;
722 if ((name->kind & name_string) && !strcmp(name->string, mode->name))
724 double dist;
726 if (refresh)
727 dist = fabs(mode_refresh(mode) - refresh);
728 else
729 dist = 0;
730 if (!best || dist < bestDist)
732 bestDist = dist;
733 best = mode;
737 return best;
740 static XRRModeInfo* find_mode_by_xid(RRMode mode)
742 name_t mode_name;
744 init_name(&mode_name);
745 set_name_xid(&mode_name, mode);
746 return find_mode(&mode_name, 0);
749 #if 0
750 static XRRModeInfo *
751 find_mode_by_name (char *name)
753 name_t mode_name;
754 init_name (&mode_name);
755 set_name_string (&mode_name, name);
756 return find_mode (&mode_name, 0);
758 #endif
760 static XRRModeInfo* find_mode_for_output(output_t* output, name_t* name)
762 XRROutputInfo* output_info = output->output_info;
763 int m;
764 XRRModeInfo* best = NULL;
765 double bestDist = 0;
767 for (m = 0; m < output_info->nmode; m++)
769 XRRModeInfo* mode;
771 mode = find_mode_by_xid(output_info->modes[m]);
772 if (!mode)
773 continue;
774 if ((name->kind & name_xid) && name->xid == mode->id)
776 best = mode;
777 break;
779 if ((name->kind & name_string) && !strcmp(name->string, mode->name))
781 double dist;
783 /* Stay away from doublescan modes unless refresh rate is specified. */
784 if (!output->refresh && (mode->modeFlags & RR_DoubleScan))
785 continue;
787 if (output->refresh)
788 dist = fabs(mode_refresh(mode) - output->refresh);
789 else
790 dist = 0;
791 if (!best || dist < bestDist)
793 bestDist = dist;
794 best = mode;
798 return best;
801 static XRRModeInfo* preferred_mode(output_t* output)
803 XRROutputInfo* output_info = output->output_info;
804 int m;
805 XRRModeInfo* best;
806 int bestDist;
808 best = NULL;
809 bestDist = 0;
810 for (m = 0; m < output_info->nmode; m++)
812 XRRModeInfo* mode_info = find_mode_by_xid(output_info->modes[m]);
813 int dist;
815 if (m < output_info->npreferred)
816 dist = 0;
817 else if (output_info->mm_height)
818 dist = (1000 * DisplayHeight(dpy, screen) / DisplayHeightMM(dpy, screen) -
819 1000 * mode_info->height / output_info->mm_height);
820 else
821 dist = DisplayHeight(dpy, screen) - mode_info->height;
823 if (dist < 0)
824 dist = -dist;
825 if (!best || dist < bestDist)
827 best = mode_info;
828 bestDist = dist;
831 return best;
834 static Bool output_can_use_crtc(output_t* output, crtc_t* crtc)
836 XRROutputInfo* output_info = output->output_info;
837 int c;
839 for (c = 0; c < output_info->ncrtc; c++)
840 if (output_info->crtcs[c] == crtc->crtc.xid)
841 return True;
842 return False;
845 static Bool output_can_use_mode(output_t* output, XRRModeInfo* mode)
847 XRROutputInfo* output_info = output->output_info;
848 int m;
850 for (m = 0; m < output_info->nmode; m++)
851 if (output_info->modes[m] == mode->id)
852 return True;
853 return False;
856 static Bool crtc_can_use_rotation(crtc_t* crtc, Rotation rotation)
858 Rotation rotations = crtc->crtc_info->rotations;
859 Rotation dir = rotation & (RR_Rotate_0 | RR_Rotate_90 | RR_Rotate_180 | RR_Rotate_270);
860 Rotation reflect = rotation & (RR_Reflect_X | RR_Reflect_Y);
861 if (((rotations & dir) != 0) && ((rotations & reflect) == reflect))
862 return True;
863 return False;
866 #if 0
867 static Bool
868 crtc_can_use_transform (crtc_t *crtc, XTransform *transform)
870 int major, minor;
872 XRRQueryVersion (dpy, &major, &minor);
873 if (major > 1 || (major == 1 && minor >= 3))
874 return True;
875 return False;
879 * Report only rotations that are supported by all crtcs
881 static Rotation
882 output_rotations (output_t *output)
884 Bool found = False;
885 Rotation rotation = RR_Rotate_0;
886 XRROutputInfo *output_info = output->output_info;
887 int c;
889 for (c = 0; c < output_info->ncrtc; c++)
891 crtc_t *crtc = find_crtc_by_xid (output_info->crtcs[c]);
892 if (crtc)
894 if (!found) {
895 rotation = crtc->crtc_info->rotations;
896 found = True;
897 } else
898 rotation &= crtc->crtc_info->rotations;
901 return rotation;
903 #endif
905 static Bool output_can_use_rotation(output_t* output, Rotation rotation)
907 XRROutputInfo* output_info = output->output_info;
908 int c;
910 /* make sure all of the crtcs can use this rotation.
911 * yes, this is not strictly necessary, but it is
912 * simpler,and we expect most drivers to either
913 * support rotation everywhere or nowhere
915 for (c = 0; c < output_info->ncrtc; c++)
917 crtc_t* crtc = find_crtc_by_xid(output_info->crtcs[c]);
918 if (crtc && !crtc_can_use_rotation(crtc, rotation))
919 return False;
921 return True;
924 static Bool output_is_primary(output_t* output)
926 if (has_1_3)
927 return XRRGetOutputPrimary(dpy, root) == output->output.xid;
928 return False;
931 /* Returns the index of the last value in an array < 0xffff */
932 static int find_last_non_clamped(CARD16 array[], int size)
934 int i;
935 for (i = size - 1; i > 0; i--)
937 if (array[i] < 0xffff)
938 return i;
940 return 0;
943 static void set_gamma_info(output_t* output)
945 XRRCrtcGamma* gamma;
946 double i1, v1, i2, v2;
947 int size, middle, last_best, last_red, last_green, last_blue;
948 CARD16* best_array;
950 if (!output->crtc_info)
951 return;
953 size = XRRGetCrtcGammaSize(dpy, output->crtc_info->crtc.xid);
954 if (!size)
956 warning("Failed to get size of gamma for output %s\n", output->output.string);
957 return;
960 gamma = XRRGetCrtcGamma(dpy, output->crtc_info->crtc.xid);
961 if (!gamma)
963 warning("Failed to get gamma for output %s\n", output->output.string);
964 return;
968 * Here is a bit tricky because gamma is a whole curve for each
969 * color. So, typically, we need to represent 3 * 256 values as 3 + 1
970 * values. Therefore, we approximate the gamma curve (v) by supposing
971 * it always follows the way we set it: a power function (i^g)
972 * multiplied by a brightness (b).
973 * v = i^g * b
974 * so g = (ln(v) - ln(b))/ln(i)
975 * and b can be found using two points (v1,i1) and (v2, i2):
976 * b = e^((ln(v2)*ln(i1) - ln(v1)*ln(i2))/ln(i1/i2))
977 * For the best resolution, we select i2 at the highest place not
978 * clamped and i1 at i2/2. Note that if i2 = 1 (as in most normal
979 * cases), then b = v2.
981 last_red = find_last_non_clamped(gamma->red, size);
982 last_green = find_last_non_clamped(gamma->green, size);
983 last_blue = find_last_non_clamped(gamma->blue, size);
984 best_array = gamma->red;
985 last_best = last_red;
986 if (last_green > last_best)
988 last_best = last_green;
989 best_array = gamma->green;
991 if (last_blue > last_best)
993 last_best = last_blue;
994 best_array = gamma->blue;
996 if (last_best == 0)
997 last_best = 1;
999 middle = last_best / 2;
1000 i1 = (double)(middle + 1) / size;
1001 v1 = (double)(best_array[middle]) / 65535;
1002 i2 = (double)(last_best + 1) / size;
1003 v2 = (double)(best_array[last_best]) / 65535;
1004 if (v2 < 0.0001)
1005 { /* The screen is black */
1006 output->brightness = 0;
1007 output->gamma.red = 1;
1008 output->gamma.green = 1;
1009 output->gamma.blue = 1;
1011 else
1013 if ((last_best + 1) == size)
1014 output->brightness = v2;
1015 else
1016 output->brightness = exp((log(v2) * log(i1) - log(v1) * log(i2)) / log(i1 / i2));
1017 output->gamma.red = logf((gamma->red[last_red / 2]) / output->brightness / 65535) /
1018 logf(((last_red / 2) + 1) / size);
1019 output->gamma.green = logf((gamma->green[last_green / 2]) / output->brightness / 65535) /
1020 logf(((last_green / 2) + 1) / size);
1021 output->gamma.blue = logf((gamma->blue[last_blue / 2]) / output->brightness / 65535) /
1022 logf(((last_blue / 2) + 1) / size);
1025 XRRFreeGamma(gamma);
1028 static void set_output_info(output_t* output, RROutput xid, XRROutputInfo* output_info)
1030 /* sanity check output info */
1031 if (output_info->connection != RR_Disconnected && !output_info->nmode)
1032 warning("Output %s is not disconnected but has no modes\n", output_info->name);
1034 /* set output name and info */
1035 if (!(output->output.kind & name_xid))
1036 set_name_xid(&output->output, xid);
1037 if (!(output->output.kind & name_string))
1038 set_name_string(&output->output, output_info->name);
1039 output->output_info = output_info;
1041 /* set crtc name and info */
1042 if (!(output->changes & changes_crtc))
1043 set_name_xid(&output->crtc, output_info->crtc);
1045 if (output->crtc.kind == name_xid && output->crtc.xid == None)
1046 output->crtc_info = NULL;
1047 else
1049 output->crtc_info = find_crtc(&output->crtc);
1050 if (!output->crtc_info)
1052 if (output->crtc.kind & name_xid)
1053 fatal("cannot find crtc 0x%x\n", output->crtc.xid);
1054 if (output->crtc.kind & name_index)
1055 fatal("cannot find crtc %d\n", output->crtc.index);
1057 if (!output_can_use_crtc(output, output->crtc_info))
1058 fatal("output %s cannot use crtc 0x%x\n", output->output.string, output->crtc_info->crtc.xid);
1061 /* set mode name and info */
1062 if (!(output->changes & changes_mode))
1064 crtc_t* crtc = NULL;
1066 if (output_info->crtc)
1067 crtc = find_crtc_by_xid(output_info->crtc);
1068 if (crtc && crtc->crtc_info)
1069 set_name_xid(&output->mode, crtc->crtc_info->mode);
1070 else if (output->crtc_info)
1071 set_name_xid(&output->mode, output->crtc_info->crtc_info->mode);
1072 else
1073 set_name_xid(&output->mode, None);
1074 if (output->mode.xid)
1076 output->mode_info = find_mode_by_xid(output->mode.xid);
1077 if (!output->mode_info)
1078 fatal("server did not report mode 0x%x for output %s\n", output->mode.xid,
1079 output->output.string);
1081 else
1082 output->mode_info = NULL;
1084 else if (output->mode.kind == name_xid && output->mode.xid == None)
1085 output->mode_info = NULL;
1086 else
1088 if (output->mode.kind == name_preferred)
1089 output->mode_info = preferred_mode(output);
1090 else
1091 output->mode_info = find_mode_for_output(output, &output->mode);
1092 if (!output->mode_info)
1094 if (output->mode.kind & name_preferred)
1095 fatal("cannot find preferred mode\n");
1096 if (output->mode.kind & name_string)
1097 fatal("cannot find mode %s\n", output->mode.string);
1098 if (output->mode.kind & name_xid)
1099 fatal("cannot find mode 0x%x\n", output->mode.xid);
1101 if (!output_can_use_mode(output, output->mode_info))
1102 fatal("output %s cannot use mode %s\n", output->output.string, output->mode_info->name);
1105 /* set position */
1106 if (!(output->changes & changes_position))
1108 if (output->crtc_info)
1110 output->x = output->crtc_info->crtc_info->x;
1111 output->y = output->crtc_info->crtc_info->y;
1113 else
1115 output->x = 0;
1116 output->y = 0;
1120 /* set rotation */
1121 if (!(output->changes & changes_rotation))
1123 output->rotation &= ~0xf;
1124 if (output->crtc_info)
1125 output->rotation |= (output->crtc_info->crtc_info->rotation & 0xf);
1126 else
1127 output->rotation = RR_Rotate_0;
1129 if (!(output->changes & changes_reflection))
1131 output->rotation &= ~(RR_Reflect_X | RR_Reflect_Y);
1132 if (output->crtc_info)
1133 output->rotation |= (output->crtc_info->crtc_info->rotation & (RR_Reflect_X | RR_Reflect_Y));
1135 if (!output_can_use_rotation(output, output->rotation))
1136 fatal("output %s cannot use rotation \"%s\" reflection \"%s\"\n", output->output.string,
1137 rotation_name(output->rotation), reflection_name(output->rotation));
1139 /* set gamma */
1140 if (!(output->changes & changes_gamma))
1141 set_gamma_info(output);
1143 /* set transformation */
1144 if (!(output->changes & changes_transform))
1146 if (output->crtc_info)
1147 copy_transform(&output->transform, &output->crtc_info->current_transform);
1148 else
1149 init_transform(&output->transform);
1151 else
1153 /* transform was already set for --scale or --transform */
1155 /* for --scale-from, figure out the mode size and compute the transform
1156 * for the target framebuffer area */
1157 if (output->scale_from_w > 0 && output->mode_info)
1159 double sx = (double)output->scale_from_w / output->mode_info->width;
1160 double sy = (double)output->scale_from_h / output->mode_info->height;
1161 if (verbose)
1162 printf("scaling %s by %lfx%lf\n", output->output.string, sx, sy);
1163 init_transform(&output->transform);
1164 output->transform.transform.matrix[0][0] = XDoubleToFixed(sx);
1165 output->transform.transform.matrix[1][1] = XDoubleToFixed(sy);
1166 output->transform.transform.matrix[2][2] = XDoubleToFixed(1.0);
1167 if (sx != 1 || sy != 1)
1168 output->transform.filter = "bilinear";
1169 else
1170 output->transform.filter = "nearest";
1171 output->transform.nparams = 0;
1172 output->transform.params = NULL;
1176 /* set primary */
1177 if (!(output->changes & changes_primary))
1178 output->primary = output_is_primary(output);
1181 static void get_screen(Bool current)
1183 if (!has_1_2)
1184 fatal("Server RandR version before 1.2\n");
1186 XRRGetScreenSizeRange(dpy, root, &minWidth, &minHeight, &maxWidth, &maxHeight);
1188 if (current)
1189 res = XRRGetScreenResourcesCurrent(dpy, root);
1190 else
1191 res = XRRGetScreenResources(dpy, root);
1192 if (!res)
1193 fatal("could not get screen resources");
1196 static void get_crtcs(void)
1198 int c;
1200 num_crtcs = res->ncrtc;
1201 crtcs = calloc(num_crtcs, sizeof(crtc_t));
1202 if (!crtcs)
1203 fatal("out of memory\n");
1205 for (c = 0; c < res->ncrtc; c++)
1207 XRRCrtcInfo* crtc_info = XRRGetCrtcInfo(dpy, res, res->crtcs[c]);
1208 XRRCrtcTransformAttributes* attr;
1209 XRRPanning* panning_info = NULL;
1211 if (has_1_3)
1213 XRRPanning zero;
1214 memset(&zero, 0, sizeof(zero));
1215 panning_info = XRRGetPanning(dpy, res, res->crtcs[c]);
1216 zero.timestamp = panning_info->timestamp;
1217 if (!memcmp(panning_info, &zero, sizeof(zero)))
1219 Xfree(panning_info);
1220 panning_info = NULL;
1224 set_name_xid(&crtcs[c].crtc, res->crtcs[c]);
1225 set_name_index(&crtcs[c].crtc, c);
1226 if (!crtc_info)
1227 fatal("could not get crtc 0x%x information\n", res->crtcs[c]);
1228 crtcs[c].crtc_info = crtc_info;
1229 crtcs[c].panning_info = panning_info;
1230 if (crtc_info->mode == None)
1232 crtcs[c].mode_info = NULL;
1233 crtcs[c].x = 0;
1234 crtcs[c].y = 0;
1235 crtcs[c].rotation = RR_Rotate_0;
1237 if (XRRGetCrtcTransform(dpy, res->crtcs[c], &attr) && attr)
1239 set_transform(&crtcs[c].current_transform, &attr->currentTransform, attr->currentFilter,
1240 attr->currentParams, attr->currentNparams);
1241 XFree(attr);
1243 else
1245 init_transform(&crtcs[c].current_transform);
1247 copy_transform(&crtcs[c].pending_transform, &crtcs[c].current_transform);
1251 static void crtc_add_output(crtc_t* crtc, output_t* output)
1253 if (crtc->outputs)
1254 crtc->outputs = realloc(crtc->outputs, (crtc->noutput + 1) * sizeof(output_t*));
1255 else
1257 crtc->outputs = malloc(sizeof(output_t*));
1258 crtc->x = output->x;
1259 crtc->y = output->y;
1260 crtc->rotation = output->rotation;
1261 crtc->mode_info = output->mode_info;
1262 copy_transform(&crtc->pending_transform, &output->transform);
1264 if (!crtc->outputs)
1265 fatal("out of memory\n");
1266 crtc->outputs[crtc->noutput++] = output;
1269 static void set_crtcs(void)
1271 output_t* output;
1273 for (output = outputs; output; output = output->next)
1275 if (!output->mode_info)
1276 continue;
1277 crtc_add_output(output->crtc_info, output);
1281 static void set_panning(void)
1283 output_t* output;
1285 for (output = outputs; output; output = output->next)
1287 if (!output->crtc_info)
1288 continue;
1289 if (!(output->changes & changes_panning))
1290 continue;
1291 if (!output->crtc_info->panning_info)
1292 output->crtc_info->panning_info = malloc(sizeof(XRRPanning));
1293 memcpy(output->crtc_info->panning_info, &output->panning, sizeof(XRRPanning));
1294 output->crtc_info->changing = 1;
1298 static void set_gamma(void)
1300 output_t* output;
1302 for (output = outputs; output; output = output->next)
1304 int i, size;
1305 crtc_t* crtc;
1306 XRRCrtcGamma* gamma;
1308 if (!(output->changes & changes_gamma))
1309 continue;
1311 if (!output->crtc_info)
1313 fatal("Need crtc to set gamma on.\n");
1314 continue;
1317 crtc = output->crtc_info;
1319 size = XRRGetCrtcGammaSize(dpy, crtc->crtc.xid);
1321 if (!size)
1323 fatal("Gamma size is 0.\n");
1324 continue;
1327 gamma = XRRAllocGamma(size);
1328 if (!gamma)
1330 fatal("Gamma allocation failed.\n");
1331 continue;
1334 if (output->gamma.red == 0.0f && output->gamma.green == 0.0f && output->gamma.blue == 0.0f)
1335 output->gamma.red = output->gamma.green = output->gamma.blue = 1.0f;
1337 for (i = 0; i < size; i++)
1339 if (output->gamma.red == 1.0f && output->brightness == 1.0f)
1340 gamma->red[i] = (i << 8) + i;
1341 else
1342 gamma->red[i] =
1343 min(powf((float)i / (float)(size - 1), output->gamma.red) * output->brightness, 1.0f) *
1344 65535.0f;
1346 if (output->gamma.green == 1.0f && output->brightness == 1.0f)
1347 gamma->green[i] = (i << 8) + i;
1348 else
1349 gamma->green[i] =
1350 min(powf((float)i / (float)(size - 1), output->gamma.green) * output->brightness,
1351 1.0f) *
1352 65535.0f;
1354 if (output->gamma.blue == 1.0f && output->brightness == 1.0f)
1355 gamma->blue[i] = (i << 8) + i;
1356 else
1357 gamma->blue[i] =
1358 min(powf((float)i / (float)(size - 1), output->gamma.blue) * output->brightness, 1.0f) *
1359 65535.0f;
1362 XRRSetCrtcGamma(dpy, crtc->crtc.xid, gamma);
1364 free(gamma);
1368 static void set_primary(void)
1370 output_t* output;
1372 if (no_primary)
1374 XRRSetOutputPrimary(dpy, root, None);
1376 else
1378 for (output = outputs; output; output = output->next)
1380 if (!(output->changes & changes_primary))
1381 continue;
1382 if (output->primary)
1383 XRRSetOutputPrimary(dpy, root, output->output.xid);
1388 static Status crtc_disable(crtc_t* crtc)
1390 if (verbose)
1391 printf("crtc %d: disable\n", crtc->crtc.index);
1393 if (dryrun)
1394 return RRSetConfigSuccess;
1395 return XRRSetCrtcConfig(dpy, res, crtc->crtc.xid, CurrentTime, 0, 0, None, RR_Rotate_0, NULL, 0);
1398 static void crtc_set_transform(crtc_t* crtc, transform_t* transform)
1400 int major, minor;
1402 XRRQueryVersion(dpy, &major, &minor);
1403 if (major > 1 || (major == 1 && minor >= 3))
1404 XRRSetCrtcTransform(dpy, crtc->crtc.xid, &transform->transform, transform->filter,
1405 transform->params, transform->nparams);
1408 static Status crtc_revert(crtc_t* crtc)
1410 XRRCrtcInfo* crtc_info = crtc->crtc_info;
1412 if (verbose)
1413 printf("crtc %d: revert\n", crtc->crtc.index);
1415 if (dryrun)
1416 return RRSetConfigSuccess;
1418 if (!equal_transform(&crtc->current_transform, &crtc->pending_transform))
1419 crtc_set_transform(crtc, &crtc->current_transform);
1420 return XRRSetCrtcConfig(dpy, res, crtc->crtc.xid, CurrentTime, crtc_info->x, crtc_info->y,
1421 crtc_info->mode, crtc_info->rotation, crtc_info->outputs,
1422 crtc_info->noutput);
1425 static Status crtc_apply(crtc_t* crtc)
1427 RROutput* rr_outputs;
1428 int o;
1429 Status s;
1430 RRMode mode = None;
1432 if (!crtc->changing || !crtc->mode_info)
1433 return RRSetConfigSuccess;
1435 rr_outputs = calloc(crtc->noutput, sizeof(RROutput));
1436 if (!rr_outputs)
1437 return BadAlloc;
1438 for (o = 0; o < crtc->noutput; o++)
1439 rr_outputs[o] = crtc->outputs[o]->output.xid;
1440 mode = crtc->mode_info->id;
1441 if (verbose)
1443 printf("crtc %d: %12s %6.1f +%d+%d", crtc->crtc.index, crtc->mode_info->name,
1444 mode_refresh(crtc->mode_info), crtc->x, crtc->y);
1445 for (o = 0; o < crtc->noutput; o++)
1446 printf(" \"%s\"", crtc->outputs[o]->output.string);
1447 printf("\n");
1450 if (dryrun)
1451 s = RRSetConfigSuccess;
1452 else
1454 if (!equal_transform(&crtc->current_transform, &crtc->pending_transform))
1455 crtc_set_transform(crtc, &crtc->pending_transform);
1456 s = XRRSetCrtcConfig(dpy, res, crtc->crtc.xid, CurrentTime, crtc->x, crtc->y, mode,
1457 crtc->rotation, rr_outputs, crtc->noutput);
1458 if (s == RRSetConfigSuccess && crtc->panning_info)
1460 if (has_1_3)
1461 s = XRRSetPanning(dpy, res, crtc->crtc.xid, crtc->panning_info);
1462 else
1463 fatal("panning needs RandR 1.3\n");
1466 free(rr_outputs);
1467 return s;
1470 static void screen_revert(void)
1472 if (verbose)
1473 printf("screen %d: revert\n", screen);
1475 if (dryrun)
1476 return;
1477 XRRSetScreenSize(dpy, root, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
1478 DisplayWidthMM(dpy, screen), DisplayHeightMM(dpy, screen));
1481 static void screen_apply(void)
1483 if (fb_width == DisplayWidth(dpy, screen) && fb_height == DisplayHeight(dpy, screen) &&
1484 fb_width_mm == DisplayWidthMM(dpy, screen) && fb_height_mm == DisplayHeightMM(dpy, screen))
1486 return;
1488 if (verbose)
1489 printf("screen %d: %dx%d %dx%d mm %6.2fdpi\n", screen, fb_width, fb_height, fb_width_mm,
1490 fb_height_mm, dpi);
1491 if (dryrun)
1492 return;
1493 XRRSetScreenSize(dpy, root, fb_width, fb_height, fb_width_mm, fb_height_mm);
1496 static void revert(void)
1498 int c;
1500 /* first disable all crtcs */
1501 for (c = 0; c < res->ncrtc; c++)
1502 crtc_disable(&crtcs[c]);
1503 /* next reset screen size */
1504 screen_revert();
1505 /* now restore all crtcs */
1506 for (c = 0; c < res->ncrtc; c++)
1507 crtc_revert(&crtcs[c]);
1511 * uh-oh, something bad happened in the middle of changing
1512 * the configuration. Revert to the previous configuration
1513 * and bail
1515 static void _X_NORETURN panic(Status s, crtc_t* crtc)
1517 int c = crtc->crtc.index;
1518 char* message;
1520 switch (s)
1522 case RRSetConfigSuccess:
1523 message = "succeeded";
1524 break;
1525 case BadAlloc:
1526 message = "out of memory";
1527 break;
1528 case RRSetConfigFailed:
1529 message = "failed";
1530 break;
1531 case RRSetConfigInvalidConfigTime:
1532 message = "invalid config time";
1533 break;
1534 case RRSetConfigInvalidTime:
1535 message = "invalid time";
1536 break;
1537 default:
1538 message = "unknown failure";
1539 break;
1542 fprintf(stderr, "%s: Configure crtc %d %s\n", program_name, c, message);
1543 revert();
1544 exit(1);
1547 static void apply(void)
1549 Status s;
1550 int c;
1553 * Hold the server grabbed while messing with
1554 * the screen so that apps which notice the resize
1555 * event and ask for xinerama information from the server
1556 * receive up-to-date information
1558 if (grab_server)
1559 XGrabServer(dpy);
1562 * Turn off any crtcs which are to be disabled or which are
1563 * larger than the target size
1565 for (c = 0; c < res->ncrtc; c++)
1567 crtc_t* crtc = &crtcs[c];
1568 XRRCrtcInfo* crtc_info = crtc->crtc_info;
1570 /* if this crtc is already disabled, skip it */
1571 if (crtc_info->mode == None)
1572 continue;
1575 * If this crtc is to be left enabled, make
1576 * sure the old size fits then new screen
1578 if (crtc->mode_info)
1580 XRRModeInfo* old_mode = find_mode_by_xid(crtc_info->mode);
1581 int x, y, w, h;
1582 box_t bounds;
1584 if (!old_mode)
1585 panic(RRSetConfigFailed, crtc);
1587 /* old position and size information */
1588 mode_geometry(old_mode, crtc_info->rotation, &crtc->current_transform.transform, &bounds);
1590 x = crtc_info->x + bounds.x1;
1591 y = crtc_info->y + bounds.y1;
1592 w = bounds.x2 - bounds.x1;
1593 h = bounds.y2 - bounds.y1;
1595 /* if it fits, skip it */
1596 if (x + w <= fb_width && y + h <= fb_height)
1597 continue;
1598 crtc->changing = True;
1600 s = crtc_disable(crtc);
1601 if (s != RRSetConfigSuccess)
1602 panic(s, crtc);
1606 * Set the screen size
1608 screen_apply();
1611 * Set crtcs
1614 for (c = 0; c < res->ncrtc; c++)
1616 crtc_t* crtc = &crtcs[c];
1618 s = crtc_apply(crtc);
1619 if (s != RRSetConfigSuccess)
1620 panic(s, crtc);
1623 set_primary();
1626 * Release the server grab and let all clients
1627 * respond to the updated state
1629 if (grab_server)
1630 XUngrabServer(dpy);
1634 * Use current output state to complete the output list
1636 static void get_outputs(void)
1638 int o;
1639 output_t* q;
1641 for (o = 0; o < res->noutput; o++)
1643 XRROutputInfo* output_info = XRRGetOutputInfo(dpy, res, res->outputs[o]);
1644 output_t* output;
1645 name_t output_name;
1646 if (!output_info)
1647 fatal("could not get output 0x%x information\n", res->outputs[o]);
1648 init_name(&output_name);
1649 set_name_xid(&output_name, res->outputs[o]);
1650 set_name_index(&output_name, o);
1651 set_name_string(&output_name, output_info->name);
1652 output = find_output(&output_name);
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);