xrandr.c 108 KB
Newer Older
1
/* 
2
3
 * Copyright © 2001 Keith Packard, member of The XFree86 Project, Inc.
 * Copyright © 2002 Hewlett Packard Company, Inc.
4
 * Copyright © 2006 Intel Corporation
5
 * Copyright © 2013 NVIDIA Corporation
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
6
7
8
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
9
10
11
12
13
14
15
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
16
 *
17
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
18
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
20
21
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22
23
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
24
 *
25
 * Thanks to Jim Gettys who wrote most of the client side code,
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
26
27
28
29
30
31
32
 * and part of the server code for randr.
 */

#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xlibint.h>
#include <X11/Xproto.h>
33
#include <X11/Xatom.h>
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
34
35
#include <X11/extensions/Xrandr.h>
#include <X11/extensions/Xrender.h>	/* we share subpixel information */
36
#include <strings.h>
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
37
38
#include <string.h>
#include <stdlib.h>
39
#include <stdint.h>
40
#include <inttypes.h>
41
#include <stdarg.h>
42
#include <math.h>
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
43

Thomas Klausner's avatar
Thomas Klausner committed
44
#ifdef HAVE_CONFIG_H
45
#include "config.h"
Thomas Klausner's avatar
Thomas Klausner committed
46
#endif
47

48
49
50
51
static char	*program_name;
static Display	*dpy;
static Window	root;
static int	screen = -1;
52
53
static Bool	verbose = False;
static Bool	automatic = False;
54
static Bool	properties = False;
Keith Packard's avatar
Keith Packard committed
55
static Bool	grab_server = True;
Keith Packard's avatar
Keith Packard committed
56
static Bool	no_primary = False;
57
58
59
60
61
static int	filter_type = -1;

static const char *filter_names[2] = {
    "bilinear",
    "nearest"};
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
62

63
static const char *direction[5] = {
Keith Packard's avatar
Keith Packard committed
64
65
66
67
68
    "normal", 
    "left", 
    "inverted", 
    "right",
    "\n"};
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
69

70
static const char *reflections[5] = {
71
72
73
74
75
76
    "normal", 
    "x", 
    "y", 
    "xy",
    "\n"};

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
77
/* subpixel order */
78
static const char *order[6] = {
Keith Packard's avatar
Keith Packard committed
79
80
81
82
83
84
    "unknown",
    "horizontal rgb",
    "horizontal bgr",
    "vertical rgb",
    "vertical bgr",
    "no subpixels"};
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
85

86
static const struct {
87
    const char	    *string;
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    unsigned long   flag;
} mode_flags[] = {
    { "+HSync", RR_HSyncPositive },
    { "-HSync", RR_HSyncNegative },
    { "+VSync", RR_VSyncPositive },
    { "-VSync", RR_VSyncNegative },
    { "Interlace", RR_Interlace },
    { "DoubleScan", RR_DoubleScan },
    { "CSync",	    RR_CSync },
    { "+CSync",	    RR_CSyncPositive },
    { "-CSync",	    RR_CSyncNegative },
    { NULL,	    0 }
};

102
static void
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
103
104
usage(void)
{
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    printf("usage: %s [options]\n%s", program_name,
           "  where options are:\n"
           "  --display <display> or -d <display>\n"
           "  --help\n"
           "  -o <normal,inverted,left,right,0,1,2,3>\n"
           "            or --orientation <normal,inverted,left,right,0,1,2,3>\n"
           "  -q        or --query\n"
           "  -s <size>/<width>x<height> or --size <size>/<width>x<height>\n"
           "  -r <rate> or --rate <rate> or --refresh <rate>\n"
           "  -v        or --version\n"
           "  -x        (reflect in x)\n"
           "  -y        (reflect in y)\n"
           "  --screen <screen>\n"
           "  --verbose\n"
           "  --current\n"
           "  --dryrun\n"
           "  --nograb\n"
           "  --prop or --properties\n"
           "  --fb <width>x<height>\n"
           "  --fbmm <width>x<height>\n"
           "  --dpi <dpi>/<output>\n"
           "  --output <output>\n"
           "      --auto\n"
           "      --mode <mode>\n"
           "      --preferred\n"
           "      --pos <x>x<y>\n"
           "      --rate <rate> or --refresh <rate>\n"
           "      --reflect normal,x,y,xy\n"
           "      --rotate normal,inverted,left,right\n"
           "      --left-of <output>\n"
           "      --right-of <output>\n"
           "      --above <output>\n"
           "      --below <output>\n"
           "      --same-as <output>\n"
           "      --set <property> <value>\n"
140
           "      --scale <x>[x<y>]\n"
141
142
           "      --scale-from <w>x<h>\n"
           "      --transform <a>,<b>,<c>,<d>,<e>,<f>,<g>,<h>,<i>\n"
143
           "      --filter nearest,bilinear\n"
144
145
146
           "      --off\n"
           "      --crtc <crtc>\n"
           "      --panning <w>x<h>[+<x>+<y>[/<track:w>x<h>+<x>+<y>[/<border:l>/<t>/<r>/<b>]]]\n"
147
           "      --gamma <r>[:<g>:<b>]\n"
Aaron Plattner's avatar
Aaron Plattner committed
148
           "      --brightness <value>\n"
149
150
151
152
153
154
155
156
157
158
159
160
161
           "      --primary\n"
           "  --noprimary\n"
           "  --newmode <name> <clock MHz>\n"
           "            <hdisp> <hsync-start> <hsync-end> <htotal>\n"
           "            <vdisp> <vsync-start> <vsync-end> <vtotal>\n"
           "            [flags...]\n"
           "            Valid flags: +HSync -HSync +VSync -VSync\n"
           "                         +CSync -CSync CSync Interlace DoubleScan\n"
           "  --rmmode <name>\n"
           "  --addmode <output> <name>\n"
           "  --delmode <output> <name>\n"
           "  --listproviders\n"
           "  --setprovideroutputsource <prov-xid> <source-xid>\n"
Keith Packard's avatar
Keith Packard committed
162
163
164
165
166
           "  --setprovideroffloadsink <prov-xid> <sink-xid>\n"
	   "  --listmonitors\n"
	   "  --listactivemonitors\n"
	   "  --setmonitor <name> {auto|<w>/<mmw>x<h>/<mmh>+<x>+<y>} {none|<output>,<output>,...}\n"
	   "  --delmonitor <name>\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
167
168
}

169
static void _X_NORETURN _X_ATTRIBUTE_PRINTF(1,2)
170
171
172
173
174
175
176
177
178
179
180
181
fatal (const char *format, ...)
{
    va_list ap;
    
    va_start (ap, format);
    fprintf (stderr, "%s: ", program_name);
    vfprintf (stderr, format, ap);
    va_end (ap);
    exit (1);
    /*NOTREACHED*/
}

182
static void _X_ATTRIBUTE_PRINTF(1,2)
183
184
185
186
187
188
189
190
191
192
warning (const char *format, ...)
{
    va_list ap;
    
    va_start (ap, format);
    fprintf (stderr, "%s: ", program_name);
    vfprintf (stderr, format, ap);
    va_end (ap);
}

193
194
195
196
197
198
199
200
201
202
203
204
205
206
static void _X_NORETURN _X_ATTRIBUTE_PRINTF(1,2)
argerr (const char *format, ...)
{
    va_list ap;

    va_start (ap, format);
    fprintf (stderr, "%s: ", program_name);
    vfprintf (stderr, format, ap);
    fprintf (stderr, "Try '%s --help' for more information.\n", program_name);
    va_end (ap);
    exit (1);
    /*NOTREACHED*/
}

Alan Coopersmith's avatar
Alan Coopersmith committed
207
/* Because fmin requires C99 support */
208
209
210
211
212
static inline double dmin (double x, double y)
{
    return x < y ? x : y;
}

213
static const char *
214
215
216
217
rotation_name (Rotation rotation)
{
    int	i;

218
219
    if ((rotation & 0xf) == 0)
	return "normal";
220
221
222
223
224
225
    for (i = 0; i < 4; i++)
	if (rotation & (1 << i))
	    return direction[i];
    return "invalid rotation";
}

226
static const char *
227
reflection_name (Rotation rotation)
228
229
230
231
{
    rotation &= (RR_Reflect_X|RR_Reflect_Y);
    switch (rotation) {
    case 0:
232
	return "none";
233
234
235
236
237
238
239
    case RR_Reflect_X:
	return "X axis";
    case RR_Reflect_Y:
	return "Y axis";
    case RR_Reflect_X|RR_Reflect_Y:
	return "X and Y axis";
    }
240
    return "invalid reflection";
241
242
}

243
static const char *
Dave Airlie's avatar
Dave Airlie committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
capability_name (int cap_bit)
{
    switch (cap_bit) {
    case RR_Capability_SourceOutput:
	return "Source Output";
    case RR_Capability_SinkOutput:
	return "Sink Output";
    case RR_Capability_SourceOffload:
	return "Source Offload";
    case RR_Capability_SinkOffload:
	return "Sink Offload";
    }
    return "invalid capability";
}

259
typedef enum _relation {
260
261
262
263
264
    relation_left_of,
    relation_right_of,
    relation_above,
    relation_below,
    relation_same_as,
265
266
} relation_t;

267
268
269
270
271
272
273
274
275
276
277
278
typedef struct {
    int	    x, y, width, height;
} rectangle_t;

typedef struct {
    int	    x1, y1, x2, y2;
} box_t;

typedef struct {
    int	    x, y;
} point_t;

279
280
281
282
283
284
285
typedef enum _changes {
    changes_none = 0,
    changes_crtc = (1 << 0),
    changes_mode = (1 << 1),
    changes_relation = (1 << 2),
    changes_position = (1 << 3),
    changes_rotation = (1 << 4),
286
287
288
    changes_reflection = (1 << 5),
    changes_automatic = (1 << 6),
    changes_refresh = (1 << 7),
289
    changes_property = (1 << 8),
290
    changes_transform = (1 << 9),
Matthias Hopf's avatar
Matthias Hopf committed
291
    changes_panning = (1 << 10),
292
    changes_gamma = (1 << 11),
Keith Packard's avatar
Keith Packard committed
293
    changes_primary = (1 << 12),
294
    changes_filter = (1 << 13),
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
} changes_t;

typedef enum _name_kind {
    name_none = 0,
    name_string = (1 << 0),
    name_xid = (1 << 1),
    name_index = (1 << 2),
    name_preferred = (1 << 3),
} name_kind_t;

typedef struct {
    name_kind_t	    kind;
    char    	    *string;
    XID	    	    xid;
    int		    index;
} name_t;

typedef struct _crtc crtc_t;
typedef struct _output	output_t;
314
typedef struct _transform transform_t;
315
typedef struct _umode	umode_t;
316
typedef struct _output_prop output_prop_t;
317
typedef struct _provider provider_t;
Keith Packard's avatar
Keith Packard committed
318
319
typedef struct _monitors monitors_t;
typedef struct _umonitor umonitor_t;
320

321
322
struct _transform {
    XTransform	    transform;
323
    const char	    *filter;
324
325
326
327
    int		    nparams;
    XFixed	    *params;
};

328
329
330
331
332
333
struct _crtc {
    name_t	    crtc;
    Bool	    changing;
    XRRCrtcInfo	    *crtc_info;

    XRRModeInfo	    *mode_info;
Matthias Hopf's avatar
Matthias Hopf committed
334
    XRRPanning      *panning_info;
335
336
337
338
339
    int		    x;
    int		    y;
    Rotation	    rotation;
    output_t	    **outputs;
    int		    noutput;
340
    transform_t	    current_transform, pending_transform;
341
342
};

343
344
345
346
347
348
struct _output_prop {
    struct _output_prop	*next;
    char		*name;
    char		*value;
};

349
350
351
352
353
struct _output {
    struct _output   *next;
    
    changes_t	    changes;
    
354
355
    output_prop_t   *props;

356
357
358
359
360
    name_t	    output;
    XRROutputInfo   *output_info;
    
    name_t	    crtc;
    crtc_t	    *crtc_info;
361
    crtc_t	    *current_crtc_info;
362
363
    
    name_t	    mode;
364
    double	    refresh;
365
366
    XRRModeInfo	    *mode_info;
    
367
368
    name_t	    addmode;

369
370
371
372
373
    relation_t	    relation;
    char	    *relative_to;

    int		    x, y;
    Rotation	    rotation;
374

375
    XRRPanning      panning;
Matthias Hopf's avatar
Matthias Hopf committed
376

377
    Bool    	    automatic;
Aaron Plattner's avatar
Aaron Plattner committed
378
    int     	    scale_from_w, scale_from_h;
379
    transform_t	    transform;
380
381
382
383
384
385

    struct {
	float red;
	float green;
	float blue;
    } gamma;
Keith Packard's avatar
Keith Packard committed
386

Mikhail Gusarov's avatar
Mikhail Gusarov committed
387
388
    float	    brightness;

Keith Packard's avatar
Keith Packard committed
389
    Bool	    primary;
390
391

    Bool	    found;
392
};
393

394
395
396
397
398
typedef enum _umode_action {
    umode_create, umode_destroy, umode_add, umode_delete
} umode_action_t;


399
400
401
struct _umode {
    struct _umode   *next;
    
402
    umode_action_t  action;
403
    XRRModeInfo	    mode;
404
405
    name_t	    output;
    name_t	    name;
406
407
};

408
409
410
411
412
struct _provider {
    name_t		provider;
    XRRProviderInfo	*info;
};

Keith Packard's avatar
Keith Packard committed
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
struct _monitors {
    int			n;
    XRRMonitorInfo	*monitors;
};

struct _umonitor {
    struct _umonitor	*next;
    char		*name;
    Bool		set;
    Bool		primary;
    int			x, y, width, height;
    int			mmwidth, mmheight;
    int			noutput;
    name_t		*outputs;
};

429
static const char *connection[3] = {
430
431
432
433
434
435
436
437
    "connected",
    "disconnected",
    "unknown connection"};

#define OUTPUT_NAME 1

#define CRTC_OFF    2
#define CRTC_UNSET  3
438
#define CRTC_INDEX  0x40000000
439
440
441
442

#define MODE_NAME   1
#define MODE_OFF    2
#define MODE_UNSET  3
443
#define MODE_PREF   4
444
445
446

#define POS_UNSET   -1

447
448
static output_t	*all_outputs = NULL;
static output_t	**all_outputs_tail = &all_outputs;
449
static crtc_t	*crtcs;
450
static provider_t	*providers;
451
static umode_t	*umodes;
452
static int	num_crtcs, num_providers;
453
454
455
static XRRScreenResources  *res;
static int	fb_width = 0, fb_height = 0;
static int	fb_width_mm = 0, fb_height_mm = 0;
456
static double	dpi = 0;
457
static char	*dpi_output_name = NULL;
458
static Bool	dryrun = False;
459
static int	minWidth, maxWidth, minHeight, maxHeight;
460
static Bool    	has_1_2 = False;
Matthias Hopf's avatar
Matthias Hopf committed
461
static Bool    	has_1_3 = False;
462
static Bool    	has_1_4 = False;
Keith Packard's avatar
Keith Packard committed
463
static Bool	has_1_5 = False;
464
static name_t   provider_name, output_source_provider_name, offload_sink_provider_name;
Keith Packard's avatar
Keith Packard committed
465
466
static monitors_t	*monitors;
static umonitor_t	*umonitors;
467

468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
static int
mode_height (XRRModeInfo *mode_info, Rotation rotation)
{
    switch (rotation & 0xf) {
    case RR_Rotate_0:
    case RR_Rotate_180:
	return mode_info->height;
    case RR_Rotate_90:
    case RR_Rotate_270:
	return mode_info->width;
    default:
	return 0;
    }
}

static int
mode_width (XRRModeInfo *mode_info, Rotation rotation)
{
    switch (rotation & 0xf) {
    case RR_Rotate_0:
    case RR_Rotate_180:
	return mode_info->width;
    case RR_Rotate_90:
    case RR_Rotate_270:
	return mode_info->height;
    default:
	return 0;
    }
}

498
static Bool
499
500
501
502
503
transform_point (XTransform *transform, double *xp, double *yp)
{
    double  vector[3];
    double  result[3];
    int	    i, j;
504
    double  v;
505
506
507
508
509
510
511
512
513
514
515
516
517

    vector[0] = *xp;
    vector[1] = *yp;
    vector[2] = 1;
    for (j = 0; j < 3; j++)
    {
	v = 0;
	for (i = 0; i < 3; i++)
	    v += (XFixedToDouble (transform->matrix[j][i]) * vector[i]);
	result[j] = v;
    }
    if (!result[2])
	return False;
518
    for (j = 0; j < 2; j++) {
519
	vector[j] = result[j] / result[2];
520
521
522
	if (vector[j] > 32767 || vector[j] < -32767)
	    return False;
    }
523
524
525
526
527
    *xp = vector[0];
    *yp = vector[1];
    return True;
}

528
static void
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
path_bounds (XTransform *transform, point_t *points, int npoints, box_t *box)
{
    int	    i;
    box_t   point;

    for (i = 0; i < npoints; i++) {
	double	x, y;
	x = points[i].x;
	y = points[i].y;
	transform_point (transform, &x, &y);
	point.x1 = floor (x);
	point.y1 = floor (y);
	point.x2 = ceil (x);
	point.y2 = ceil (y);
	if (i == 0)
	    *box = point;
	else {
	    if (point.x1 < box->x1) box->x1 = point.x1;
	    if (point.y1 < box->y1) box->y1 = point.y1;
	    if (point.x2 > box->x2) box->x2 = point.x2;
	    if (point.y2 > box->y2) box->y2 = point.y2;
	}
    }
}

static void
mode_geometry (XRRModeInfo *mode_info, Rotation rotation,
	       XTransform *transform,
	       box_t *bounds)
{
    point_t rect[4];
    int	width = mode_width (mode_info, rotation);
    int height = mode_height (mode_info, rotation);

    rect[0].x = 0;
    rect[0].y = 0;
    rect[1].x = width;
    rect[1].y = 0;
    rect[2].x = width;
    rect[2].y = height;
    rect[3].x = 0;
    rect[3].y = height;
    path_bounds (transform, rect, 4, bounds);
}

574
/* v refresh frequency in Hz */
575
static double
576
mode_refresh (const XRRModeInfo *mode_info)
577
{
578
    double rate;
579
    double vTotal = mode_info->vTotal;
580
581
582
583
584
585
586
587
588
589
590

    if (mode_info->modeFlags & RR_DoubleScan) {
	/* doublescan doubles the number of lines */
	vTotal *= 2;
    }

    if (mode_info->modeFlags & RR_Interlace) {
	/* interlace splits the frame into two fields */
	/* the field rate is what is typically reported by monitors */
	vTotal /= 2;
    }
591
    
592
    if (mode_info->hTotal && vTotal)
593
	rate = ((double) mode_info->dotClock /
594
		((double) mode_info->hTotal * (double) vTotal));
595
596
597
598
599
600
    else
    	rate = 0;
    return rate;
}

/* h sync frequency in Hz */
601
static double
602
mode_hsync (const XRRModeInfo *mode_info)
603
{
604
    double rate;
605
606
    
    if (mode_info->hTotal)
607
	rate = (double) mode_info->dotClock / (double) mode_info->hTotal;
608
609
610
611
612
    else
    	rate = 0;
    return rate;
}

613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
static void
print_verbose_mode (const XRRModeInfo *mode, Bool current, Bool preferred)
{
    int f;

    printf ("  %s (0x%x) %6.3fMHz",
	    mode->name, (int)mode->id,
	    (double)mode->dotClock / 1000000.0);
    for (f = 0; mode_flags[f].flag; f++)
	if (mode->modeFlags & mode_flags[f].flag)
	    printf (" %s", mode_flags[f].string);
    if (current)
	printf (" *current");
    if (preferred)
	printf (" +preferred");
    printf ("\n");
    printf ("        h: width  %4d start %4d end %4d total %4d skew %4d clock %6.2fKHz\n",
	    mode->width, mode->hSyncStart, mode->hSyncEnd,
	    mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000);
    printf ("        v: height %4d start %4d end %4d total %4d           clock %6.2fHz\n",
	    mode->height, mode->vSyncStart, mode->vSyncEnd, mode->vTotal,
	    mode_refresh (mode));
}

637
638
639
static void
init_name (name_t *name)
{
Peter Hutterer's avatar
Peter Hutterer committed
640
    memset(name, 0, sizeof(*name));
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
    name->kind = name_none;
}

static void
set_name_string (name_t *name, char *string)
{
    name->kind |= name_string;
    name->string = string;
}

static void
set_name_xid (name_t *name, XID xid)
{
    name->kind |= name_xid;
    name->xid = xid;
}

static void
659
set_name_index (name_t *name, int idx)
660
661
{
    name->kind |= name_index;
662
    name->index = idx;
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
}

static void
set_name_preferred (name_t *name)
{
    name->kind |= name_preferred;
}

static void
set_name_all (name_t *name, name_t *old)
{
    if (old->kind & name_xid)
	name->xid = old->xid;
    if (old->kind & name_string)
	name->string = old->string;
    if (old->kind & name_index)
	name->index = old->index;
    name->kind |= old->kind;
}

static void
set_name (name_t *name, char *string, name_kind_t valid)
{
686
687
    unsigned int xid; /* don't make it XID (which is unsigned long):
			 scanf() takes unsigned int */
688
    int idx;
689
690
691

    if ((valid & name_xid) && sscanf (string, "0x%x", &xid) == 1)
	set_name_xid (name, xid);
692
693
    else if ((valid & name_index) && sscanf (string, "%d", &idx) == 1)
	set_name_index (name, idx);
694
695
696
    else if (valid & name_string)
	set_name_string (name, string);
    else
697
	argerr ("invalid name '%s'\n", string);
698
699
}

700
701
702
703
704
static int
print_name (const name_t *name)
{
    name_kind_t kind = name->kind;

705
    if ((kind & name_xid))         return printf("XID 0x%x", (unsigned int)name->xid);
706
707
708
709
710
    else if ((kind & name_string)) return printf("name %s", name->string);
    else if ((kind & name_index))  return printf("index %d", name->index);
    else                           return printf("unknown name");
}

711
712
713
714
715
716
static void
init_transform (transform_t *transform)
{
    int x;
    memset (&transform->transform, '\0', sizeof (transform->transform));
    for (x = 0; x < 3; x++)
717
	transform->transform.matrix[x][x] = XDoubleToFixed (1.0);
718
719
720
721
722
723
724
725
    transform->filter = "";
    transform->nparams = 0;
    transform->params = NULL;
}

static void
set_transform (transform_t  *dest,
	       XTransform   *transform,
726
	       const char   *filter,
727
728
729
730
	       XFixed	    *params,
	       int	    nparams)
{
    dest->transform = *transform;
731
    /* note: this string is leaked */
732
733
734
735
736
737
738
739
740
    dest->filter = strdup (filter);
    dest->nparams = nparams;
    dest->params = malloc (nparams * sizeof (XFixed));
    memcpy (dest->params, params, nparams * sizeof (XFixed));
}

static void
copy_transform (transform_t *dest, transform_t *src)
{
741
    set_transform (dest, &src->transform,
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
		   src->filter, src->params, src->nparams);
}

static Bool
equal_transform (transform_t *a, transform_t *b)
{
    if (memcmp (&a->transform, &b->transform, sizeof (XTransform)) != 0)
	return False;
    if (strcmp (a->filter, b->filter) != 0)
	return False;
    if (a->nparams != b->nparams)
	return False;
    if (memcmp (a->params, b->params, a->nparams * sizeof (XFixed)) != 0)
	return False;
    return True;
}

759
760
761
762
763
764
static output_t *
add_output (void)
{
    output_t *output = calloc (1, sizeof (output_t));

    if (!output)
765
	fatal ("out of memory\n");
766
    output->next = NULL;
767
    output->found = False;
768
    output->brightness = 1.0;
769
770
    *all_outputs_tail = output;
    all_outputs_tail = &output->next;
771
772
773
774
775
776
777
778
    return output;
}

static output_t *
find_output (name_t *name)
{
    output_t *output;

779
    for (output = all_outputs; output; output = output->next)
780
781
782
783
784
785
786
    {
	name_kind_t common = name->kind & output->output.kind;
	
	if ((common & name_xid) && name->xid == output->output.xid)
	    break;
	if ((common & name_string) && !strcmp (name->string, output->output.string))
	    break;
787
	if ((common & name_index) && name->index == output->output.index)
788
789
790
791
792
	    break;
    }
    return output;
}

793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
static output_t *
find_output_by_xid (RROutput output)
{
    name_t  output_name;

    init_name (&output_name);
    set_name_xid (&output_name, output);
    return find_output (&output_name);
}

static output_t *
find_output_by_name (char *name)
{
    name_t  output_name;

    init_name (&output_name);
    set_name_string (&output_name, name);
    return find_output (&output_name);
}
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829

static crtc_t *
find_crtc (name_t *name)
{
    int	    c;
    crtc_t  *crtc = NULL;

    for (c = 0; c < num_crtcs; c++)
    {
	name_kind_t common;
	
	crtc = &crtcs[c];
	common = name->kind & crtc->crtc.kind;
	
	if ((common & name_xid) && name->xid == crtc->crtc.xid)
	    break;
	if ((common & name_string) && !strcmp (name->string, crtc->crtc.string))
	    break;
830
	if ((common & name_index) && name->index == crtc->crtc.index)
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
	    break;
	crtc = NULL;
    }
    return crtc;
}

static crtc_t *
find_crtc_by_xid (RRCrtc crtc)
{
    name_t  crtc_name;

    init_name (&crtc_name);
    set_name_xid (&crtc_name, crtc);
    return find_crtc (&crtc_name);
}

static XRRModeInfo *
848
find_mode (name_t *name, double refresh)
849
850
{
    int		m;
851
    XRRModeInfo	*best = NULL;
852
    double	bestDist = 0;
853
854
855

    for (m = 0; m < res->nmode; m++)
    {
856
	XRRModeInfo *mode = &res->modes[m];
857
	if ((name->kind & name_xid) && name->xid == mode->id)
858
859
	{
	    best = mode;
860
	    break;
861
	}
862
	if ((name->kind & name_string) && !strcmp (name->string, mode->name))
863
	{
864
	    double   dist;
865
866
867
868
869
870
871
872
873
874
875
	    
	    if (refresh)
		dist = fabs (mode_refresh (mode) - refresh);
	    else
		dist = 0;
	    if (!best || dist < bestDist)
	    {
		bestDist = dist;
		best = mode;
	    }
	}
876
    }
877
    return best;
878
879
880
881
882
883
884
885
886
}

static XRRModeInfo *
find_mode_by_xid (RRMode mode)
{
    name_t  mode_name;

    init_name (&mode_name);
    set_name_xid (&mode_name, mode);
887
    return find_mode (&mode_name, 0);
888
889
}

890
#if 0
891
892
893
894
895
896
897
898
static XRRModeInfo *
find_mode_by_name (char *name)
{
    name_t  mode_name;
    init_name (&mode_name);
    set_name_string (&mode_name, name);
    return find_mode (&mode_name, 0);
}
899
#endif
900

901
902
903
904
905
906
static
XRRModeInfo *
find_mode_for_output (output_t *output, name_t *name)
{
    XRROutputInfo   *output_info = output->output_info;
    int		    m;
907
    XRRModeInfo	    *best = NULL;
908
    double	    bestDist = 0;
909
910
911

    for (m = 0; m < output_info->nmode; m++)
    {
912
	XRRModeInfo	    *mode;
913

914
915
916
	mode = find_mode_by_xid (output_info->modes[m]);
	if (!mode) continue;
	if ((name->kind & name_xid) && name->xid == mode->id)
917
918
	{
	    best = mode;
919
	    break;
920
	}
921
	if ((name->kind & name_string) && !strcmp (name->string, mode->name))
922
	{
923
	    double   dist;
924
925
926
927
928

	    /* Stay away from doublescan modes unless refresh rate is specified. */
	    if (!output->refresh && (mode->modeFlags & RR_DoubleScan))
		continue;

929
930
931
932
933
934
935
936
937
938
	    if (output->refresh)
		dist = fabs (mode_refresh (mode) - output->refresh);
	    else
		dist = 0;
	    if (!best || dist < bestDist)
	    {
		bestDist = dist;
		best = mode;
	    }
	}
939
    }
940
    return best;
941
942
}

943
static XRRModeInfo *
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
preferred_mode (output_t *output)
{
    XRROutputInfo   *output_info = output->output_info;
    int		    m;
    XRRModeInfo	    *best;
    int		    bestDist;
    
    best = NULL;
    bestDist = 0;
    for (m = 0; m < output_info->nmode; m++)
    {
	XRRModeInfo *mode_info = find_mode_by_xid (output_info->modes[m]);
	int	    dist;
	
	if (m < output_info->npreferred)
	    dist = 0;
	else if (output_info->mm_height)
	    dist = (1000 * DisplayHeight(dpy, screen) / DisplayHeightMM(dpy, screen) -
		    1000 * mode_info->height / output_info->mm_height);
	else
	    dist = DisplayHeight(dpy, screen) - mode_info->height;

        if (dist < 0) dist = -dist;
	if (!best || dist < bestDist)
	{
	    best = mode_info;
	    bestDist = dist;
	}
    }
    return best;
}

static Bool
output_can_use_crtc (output_t *output, crtc_t *crtc)
{
    XRROutputInfo   *output_info = output->output_info;
    int		    c;

    for (c = 0; c < output_info->ncrtc; c++)
	if (output_info->crtcs[c] == crtc->crtc.xid)
	    return True;
    return False;
}

static Bool
output_can_use_mode (output_t *output, XRRModeInfo *mode)
{
    XRROutputInfo   *output_info = output->output_info;
    int		    m;

    for (m = 0; m < output_info->nmode; m++)
	if (output_info->modes[m] == mode->id)
	    return True;
    return False;
}

static Bool