1b042e37fSmrg/*
2b042e37fSmrg * Copyright © 2006 Keith Packard
38c4a8e55Smrg * Copyright © 2008 Red Hat, Inc.
4b042e37fSmrg *
5b042e37fSmrg * Permission to use, copy, modify, distribute, and sell this software and its
6b042e37fSmrg * documentation for any purpose is hereby granted without fee, provided that
7b042e37fSmrg * the above copyright notice appear in all copies and that both that copyright
8b042e37fSmrg * notice and this permission notice appear in supporting documentation, and
9b042e37fSmrg * that the name of the copyright holders not be used in advertising or
10b042e37fSmrg * publicity pertaining to distribution of the software without specific,
11b042e37fSmrg * written prior permission.  The copyright holders make no representations
12b042e37fSmrg * about the suitability of this software for any purpose.  It is provided "as
13b042e37fSmrg * is" without express or implied warranty.
14b042e37fSmrg *
15b042e37fSmrg * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16b042e37fSmrg * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17b042e37fSmrg * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18b042e37fSmrg * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19b042e37fSmrg * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20b042e37fSmrg * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21b042e37fSmrg * OF THIS SOFTWARE.
22b042e37fSmrg */
23b042e37fSmrg
24b042e37fSmrg#ifdef HAVE_CONFIG_H
25b042e37fSmrg#include <config.h>
26b042e37fSmrg#endif
27b042e37fSmrg
28b242714cSmrg#include <limits.h>
29b042e37fSmrg#include <stdio.h>
30b042e37fSmrg#include <X11/Xlib.h>
31b042e37fSmrg/* we need to be able to manipulate the Display structure on events */
32b042e37fSmrg#include <X11/Xlibint.h>
33b042e37fSmrg#include <X11/extensions/render.h>
34b042e37fSmrg#include <X11/extensions/Xrender.h>
35b042e37fSmrg#include "Xrandrint.h"
36b042e37fSmrg
37b042e37fSmrg#define OutputInfoExtra	(SIZEOF(xRRGetOutputInfoReply) - 32)
380597fb56Smrg
39b042e37fSmrgXRROutputInfo *
40b042e37fSmrgXRRGetOutputInfo (Display *dpy, XRRScreenResources *resources, RROutput output)
41b042e37fSmrg{
42b042e37fSmrg    XExtDisplayInfo		*info = XRRFindDisplay(dpy);
43b042e37fSmrg    xRRGetOutputInfoReply	rep;
44b042e37fSmrg    xRRGetOutputInfoReq		*req;
45b042e37fSmrg    int				nbytes, nbytesRead, rbytes;
46b042e37fSmrg    XRROutputInfo		*xoi;
47b042e37fSmrg
488c4a8e55Smrg    RRCheckExtension (dpy, info, NULL);
49b042e37fSmrg
50b042e37fSmrg    LockDisplay (dpy);
51b042e37fSmrg    GetReq (RRGetOutputInfo, req);
52b042e37fSmrg    req->reqType = info->codes->major_opcode;
53b042e37fSmrg    req->randrReqType = X_RRGetOutputInfo;
54b042e37fSmrg    req->output = output;
55b042e37fSmrg    req->configTimestamp = resources->configTimestamp;
56b042e37fSmrg
57b042e37fSmrg    if (!_XReply (dpy, (xReply *) &rep, OutputInfoExtra >> 2, xFalse))
58b042e37fSmrg    {
59b042e37fSmrg	UnlockDisplay (dpy);
60b042e37fSmrg	SyncHandle ();
61b042e37fSmrg	return NULL;
62b042e37fSmrg    }
63b042e37fSmrg
64b242714cSmrg    if (rep.length > INT_MAX >> 2 || rep.length < (OutputInfoExtra >> 2))
65b242714cSmrg    {
66b242714cSmrg        if (rep.length > (OutputInfoExtra >> 2))
67b242714cSmrg	    _XEatDataWords (dpy, rep.length - (OutputInfoExtra >> 2));
68b242714cSmrg	else
69b242714cSmrg	    _XEatDataWords (dpy, rep.length);
70b242714cSmrg	UnlockDisplay (dpy);
71b242714cSmrg	SyncHandle ();
72b242714cSmrg	return NULL;
73b242714cSmrg    }
74b042e37fSmrg    nbytes = ((long) (rep.length) << 2) - OutputInfoExtra;
75b042e37fSmrg
76b042e37fSmrg    nbytesRead = (long) (rep.nCrtcs * 4 +
77b042e37fSmrg			 rep.nModes * 4 +
78b042e37fSmrg			 rep.nClones * 4 +
79b042e37fSmrg			 ((rep.nameLength + 3) & ~3));
80b042e37fSmrg
810597fb56Smrg    /*
820597fb56Smrg     * first we must compute how much space to allocate for
83b042e37fSmrg     * randr library's use; we'll allocate the structures in a single
84b042e37fSmrg     * allocation, on cleanlyness grounds.
85b042e37fSmrg     */
86b042e37fSmrg
87b042e37fSmrg    rbytes = (sizeof (XRROutputInfo) +
88b042e37fSmrg	      rep.nCrtcs * sizeof (RRCrtc) +
89b042e37fSmrg	      rep.nModes * sizeof (RRMode) +
90b042e37fSmrg	      rep.nClones * sizeof (RROutput) +
91b042e37fSmrg	      rep.nameLength + 1);	    /* '\0' terminate name */
92b042e37fSmrg
938d0bc965Smrg    xoi = Xmalloc(rbytes);
94b042e37fSmrg    if (xoi == NULL) {
958bd17e5fSmrg	_XEatDataWords (dpy, rep.length - (OutputInfoExtra >> 2));
96b042e37fSmrg	UnlockDisplay (dpy);
97b042e37fSmrg	SyncHandle ();
98b042e37fSmrg	return NULL;
99b042e37fSmrg    }
100b042e37fSmrg
101b042e37fSmrg    xoi->timestamp = rep.timestamp;
102b042e37fSmrg    xoi->crtc = rep.crtc;
103b042e37fSmrg    xoi->mm_width = rep.mmWidth;
104b042e37fSmrg    xoi->mm_height = rep.mmHeight;
105b042e37fSmrg    xoi->connection = rep.connection;
106b042e37fSmrg    xoi->subpixel_order = rep.subpixelOrder;
107b042e37fSmrg    xoi->ncrtc = rep.nCrtcs;
108b042e37fSmrg    xoi->crtcs = (RRCrtc *) (xoi + 1);
109b042e37fSmrg    xoi->nmode = rep.nModes;
110b042e37fSmrg    xoi->npreferred = rep.nPreferred;
111b042e37fSmrg    xoi->modes = (RRMode *) (xoi->crtcs + rep.nCrtcs);
112b042e37fSmrg    xoi->nclone = rep.nClones;
113b042e37fSmrg    xoi->clones = (RROutput *) (xoi->modes + rep.nModes);
114b042e37fSmrg    xoi->name = (char *) (xoi->clones + rep.nClones);
115b042e37fSmrg
11667594505Smrg    _XRead32 (dpy, (long *) xoi->crtcs, rep.nCrtcs << 2);
11767594505Smrg    _XRead32 (dpy, (long *) xoi->modes, rep.nModes << 2);
11867594505Smrg    _XRead32 (dpy, (long *) xoi->clones, rep.nClones << 2);
1190597fb56Smrg
120b042e37fSmrg    /*
121b042e37fSmrg     * Read name and '\0' terminate
122b042e37fSmrg     */
123b042e37fSmrg    _XReadPad (dpy, xoi->name, rep.nameLength);
124b042e37fSmrg    xoi->name[rep.nameLength] = '\0';
1250597fb56Smrg    xoi->nameLen = rep.nameLength;
1260597fb56Smrg
127b042e37fSmrg    /*
128b042e37fSmrg     * Skip any extra data
129b042e37fSmrg     */
130b042e37fSmrg    if (nbytes > nbytesRead)
131b042e37fSmrg	_XEatData (dpy, (unsigned long) (nbytes - nbytesRead));
1320597fb56Smrg
133b042e37fSmrg    UnlockDisplay (dpy);
134b042e37fSmrg    SyncHandle ();
135b042e37fSmrg    return (XRROutputInfo *) xoi;
136b042e37fSmrg}
137b042e37fSmrg
138b042e37fSmrgvoid
139b042e37fSmrgXRRFreeOutputInfo (XRROutputInfo *outputInfo)
140b042e37fSmrg{
141b042e37fSmrg    Xfree (outputInfo);
142b042e37fSmrg}
1438c4a8e55Smrg
1448c4a8e55Smrgstatic Bool
1458c4a8e55Smrg_XRRHasOutputPrimary (int major, int minor)
1468c4a8e55Smrg{
1478c4a8e55Smrg    return major > 1 || (major == 1 && minor >= 3);
1488c4a8e55Smrg}
1498c4a8e55Smrg
1508c4a8e55Smrgvoid
1518c4a8e55SmrgXRRSetOutputPrimary(Display *dpy, Window window, RROutput output)
1528c4a8e55Smrg{
1538c4a8e55Smrg    XExtDisplayInfo	    *info = XRRFindDisplay(dpy);
1548c4a8e55Smrg    xRRSetOutputPrimaryReq  *req;
1558c4a8e55Smrg    int			    major_version, minor_version;
1568c4a8e55Smrg
1578c4a8e55Smrg    RRSimpleCheckExtension (dpy, info);
1588c4a8e55Smrg
1590597fb56Smrg    if (!XRRQueryVersion (dpy, &major_version, &minor_version) ||
1608c4a8e55Smrg	!_XRRHasOutputPrimary (major_version, minor_version))
1618c4a8e55Smrg	return;
1628c4a8e55Smrg
1638c4a8e55Smrg    LockDisplay(dpy);
1648c4a8e55Smrg    GetReq (RRSetOutputPrimary, req);
1658c4a8e55Smrg    req->reqType       = info->codes->major_opcode;
1668c4a8e55Smrg    req->randrReqType  = X_RRSetOutputPrimary;
1678c4a8e55Smrg    req->window        = window;
1688c4a8e55Smrg    req->output	       = output;
1698c4a8e55Smrg
1708c4a8e55Smrg    UnlockDisplay (dpy);
1718c4a8e55Smrg    SyncHandle ();
1728c4a8e55Smrg}
1738c4a8e55Smrg
1748c4a8e55SmrgRROutput
1758c4a8e55SmrgXRRGetOutputPrimary(Display *dpy, Window window)
1768c4a8e55Smrg{
1778c4a8e55Smrg    XExtDisplayInfo	    *info = XRRFindDisplay(dpy);
1788c4a8e55Smrg    xRRGetOutputPrimaryReq  *req;
1798c4a8e55Smrg    xRRGetOutputPrimaryReply rep;
1808c4a8e55Smrg    int			    major_version, minor_version;
1818c4a8e55Smrg
1828c4a8e55Smrg    RRCheckExtension (dpy, info, 0);
1838c4a8e55Smrg
1840597fb56Smrg    if (!XRRQueryVersion (dpy, &major_version, &minor_version) ||
1858c4a8e55Smrg	!_XRRHasOutputPrimary (major_version, minor_version))
1868c4a8e55Smrg	return None;
1878c4a8e55Smrg
1888c4a8e55Smrg    LockDisplay(dpy);
1898c4a8e55Smrg    GetReq (RRGetOutputPrimary, req);
1908c4a8e55Smrg    req->reqType	= info->codes->major_opcode;
1918c4a8e55Smrg    req->randrReqType	= X_RRGetOutputPrimary;
1928c4a8e55Smrg    req->window		= window;
1938c4a8e55Smrg
1948c4a8e55Smrg    if (!_XReply (dpy, (xReply *) &rep, 0, xFalse))
1958c4a8e55Smrg	rep.output = None;
1960597fb56Smrg
1978c4a8e55Smrg    UnlockDisplay(dpy);
1988c4a8e55Smrg    SyncHandle();
1998c4a8e55Smrg
2008c4a8e55Smrg    return rep.output;
2018c4a8e55Smrg}
202