XrrScreen.c revision 8d0bc965
1/*
2 * Copyright © 2006 Keith Packard
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission.  The copyright holders make no representations
11 * about the suitability of this software for any purpose.  It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26
27#include <limits.h>
28#include <stdio.h>
29#include <X11/Xlib.h>
30/* we need to be able to manipulate the Display structure on events */
31#include <X11/Xlibint.h>
32#include <X11/extensions/render.h>
33#include <X11/extensions/Xrender.h>
34#include "Xrandrint.h"
35
36/*
37 * this is cheating on the knowledge that the two requests are identical
38 * but for the request number.
39 */
40static XRRScreenResources *
41doGetScreenResources (Display *dpy, Window window, int poll)
42{
43    XExtDisplayInfo		*info = XRRFindDisplay(dpy);
44    xRRGetScreenResourcesReply  rep;
45    xRRGetScreenResourcesReq	*req;
46    _XAsyncHandler		async;
47    _XRRVersionState		async_state;
48    int				nbytes, nbytesRead, rbytes;
49    int				i;
50    XRRScreenResources		*xrsr;
51    char			*names;
52    char			*wire_names, *wire_name;
53    Bool			getting_version = False;
54    XRandRInfo			*xrri;
55
56    RRCheckExtension (dpy, info, NULL);
57
58    LockDisplay (dpy);
59    xrri = (XRandRInfo *) info->data;
60
61    if (xrri->major_version == -1)
62    {
63	xRRQueryVersionReq		*vreq;
64
65	/* hide a version query in the request */
66	GetReq (RRQueryVersion, vreq);
67	vreq->reqType = info->codes->major_opcode;
68	vreq->randrReqType = X_RRQueryVersion;
69	vreq->majorVersion = RANDR_MAJOR;
70	vreq->minorVersion = RANDR_MINOR;
71
72	async_state.version_seq = dpy->request;
73	async_state.error = False;
74	async.next = dpy->async_handlers;
75	async.handler = _XRRVersionHandler;
76	async.data = (XPointer) &async_state;
77	dpy->async_handlers = &async;
78
79	getting_version = True;
80    }
81
82    GetReq (RRGetScreenResources, req);
83    req->reqType = info->codes->major_opcode;
84    req->randrReqType = poll ? X_RRGetScreenResources
85			     : X_RRGetScreenResourcesCurrent;
86    req->window = window;
87
88    if (!_XReply (dpy, (xReply *) &rep, 0, xFalse))
89    {
90	if (getting_version)
91	    DeqAsyncHandler (dpy, &async);
92	UnlockDisplay (dpy);
93	SyncHandle ();
94	return NULL;
95    }
96    if (getting_version)
97    {
98	DeqAsyncHandler (dpy, &async);
99	if (async_state.error)
100	{
101	    UnlockDisplay (dpy);
102	    SyncHandle();
103	    LockDisplay (dpy);
104	}
105	xrri->major_version = async_state.major_version;
106	xrri->minor_version = async_state.minor_version;
107	xrri->has_rates = _XRRHasRates (xrri->minor_version, xrri->major_version);
108    }
109
110    if (rep.length < INT_MAX >> 2) {
111	nbytes = (long) rep.length << 2;
112
113	nbytesRead = (long) (rep.nCrtcs * 4 +
114			     rep.nOutputs * 4 +
115			     rep.nModes * SIZEOF (xRRModeInfo) +
116			     ((rep.nbytesNames + 3) & ~3));
117
118	/*
119	 * first we must compute how much space to allocate for
120	 * randr library's use; we'll allocate the structures in a single
121	 * allocation, on cleanlyness grounds.
122	 */
123
124	rbytes = (sizeof (XRRScreenResources) +
125		  rep.nCrtcs * sizeof (RRCrtc) +
126		  rep.nOutputs * sizeof (RROutput) +
127		  rep.nModes * sizeof (XRRModeInfo) +
128		  rep.nbytesNames + rep.nModes);    /* '\0' terminate names */
129
130	xrsr = Xmalloc(rbytes);
131	wire_names = Xmalloc (rep.nbytesNames);
132    } else {
133	nbytes = 0;
134	nbytesRead = 0;
135	rbytes = 0;
136	xrsr = NULL;
137	wire_names = NULL;
138    }
139
140    if (xrsr == NULL || wire_names == NULL) {
141	Xfree (xrsr);
142	Xfree (wire_names);
143	_XEatDataWords (dpy, rep.length);
144	UnlockDisplay (dpy);
145	SyncHandle ();
146	return NULL;
147    }
148
149    xrsr->timestamp = rep.timestamp;
150    xrsr->configTimestamp = rep.configTimestamp;
151    xrsr->ncrtc = rep.nCrtcs;
152    xrsr->crtcs = (RRCrtc *) (xrsr + 1);
153    xrsr->noutput = rep.nOutputs;
154    xrsr->outputs = (RROutput *) (xrsr->crtcs + rep.nCrtcs);
155    xrsr->nmode = rep.nModes;
156    xrsr->modes = (XRRModeInfo *) (xrsr->outputs + rep.nOutputs);
157    names = (char *) (xrsr->modes + rep.nModes);
158
159    _XRead32 (dpy, (long *) xrsr->crtcs, rep.nCrtcs << 2);
160    _XRead32 (dpy, (long *) xrsr->outputs, rep.nOutputs << 2);
161
162    for (i = 0; i < rep.nModes; i++)  {
163	xRRModeInfo modeInfo;
164
165	_XReadPad (dpy, (char *) &modeInfo, SIZEOF (xRRModeInfo));
166	xrsr->modes[i].id = modeInfo.id;
167	xrsr->modes[i].width = modeInfo.width;
168	xrsr->modes[i].height = modeInfo.height;
169	xrsr->modes[i].dotClock = modeInfo.dotClock;
170	xrsr->modes[i].hSyncStart = modeInfo.hSyncStart;
171	xrsr->modes[i].hSyncEnd = modeInfo.hSyncEnd;
172	xrsr->modes[i].hTotal = modeInfo.hTotal;
173	xrsr->modes[i].hSkew = modeInfo.hSkew;
174	xrsr->modes[i].vSyncStart = modeInfo.vSyncStart;
175	xrsr->modes[i].vSyncEnd = modeInfo.vSyncEnd;
176	xrsr->modes[i].vTotal = modeInfo.vTotal;
177	xrsr->modes[i].nameLength = modeInfo.nameLength;
178	xrsr->modes[i].modeFlags = modeInfo.modeFlags;
179    }
180
181    /*
182     * Read names and '\0' pad each one
183     */
184    _XReadPad (dpy, wire_names, rep.nbytesNames);
185    wire_name = wire_names;
186    for (i = 0; i < rep.nModes; i++)  {
187	xrsr->modes[i].name = names;
188	if (xrsr->modes[i].nameLength > rep.nbytesNames) {
189	    Xfree (xrsr);
190	    Xfree (wire_names);
191	    UnlockDisplay (dpy);
192	    SyncHandle ();
193	    return NULL;
194	}
195	rep.nbytesNames -= xrsr->modes[i].nameLength;
196	memcpy (names, wire_name, xrsr->modes[i].nameLength);
197	names[xrsr->modes[i].nameLength] = '\0';
198	names += xrsr->modes[i].nameLength + 1;
199	wire_name += xrsr->modes[i].nameLength;
200    }
201    Xfree (wire_names);
202
203    /*
204     * Skip any extra data
205     */
206    if (nbytes > nbytesRead)
207	_XEatData (dpy, (unsigned long) (nbytes - nbytesRead));
208
209    UnlockDisplay (dpy);
210    SyncHandle();
211    return (XRRScreenResources *) xrsr;
212}
213
214XRRScreenResources *
215XRRGetScreenResources(Display *dpy, Window window)
216{
217    return doGetScreenResources(dpy, window, 1);
218}
219
220XRRScreenResources *
221XRRGetScreenResourcesCurrent(Display *dpy, Window window)
222{
223    return doGetScreenResources(dpy, window, 0);
224}
225
226void
227XRRFreeScreenResources (XRRScreenResources *resources)
228{
229    Xfree (resources);
230}
231
232Status
233XRRGetScreenSizeRange (Display *dpy, Window window,
234		       int *minWidth, int *minHeight,
235		       int *maxWidth, int *maxHeight)
236{
237    XExtDisplayInfo		*info = XRRFindDisplay(dpy);
238    xRRGetScreenSizeRangeReq	*req;
239    xRRGetScreenSizeRangeReply	rep;
240
241    RRCheckExtension (dpy, info, 0);
242    LockDisplay (dpy);
243    GetReq (RRGetScreenSizeRange, req);
244    req->reqType = info->codes->major_opcode;
245    req->randrReqType = X_RRGetScreenSizeRange;
246    req->window = window;
247    if (!_XReply (dpy, (xReply *) &rep, 0, xFalse))
248    {
249	UnlockDisplay (dpy);
250	SyncHandle ();
251	return False;
252    }
253    UnlockDisplay (dpy);
254    SyncHandle ();
255    *minWidth = rep.minWidth;
256    *minHeight = rep.minHeight;
257    *maxWidth = rep.maxWidth;
258    *maxHeight = rep.maxHeight;
259   return True;
260}
261
262void
263XRRSetScreenSize (Display *dpy, Window window,
264		  int width, int height,
265		  int mmWidth, int mmHeight)
266{
267    XExtDisplayInfo		*info = XRRFindDisplay(dpy);
268    xRRSetScreenSizeReq		*req;
269
270    RRSimpleCheckExtension (dpy, info);
271    LockDisplay (dpy);
272    GetReq (RRSetScreenSize, req);
273    req->reqType = info->codes->major_opcode;
274    req->randrReqType = X_RRSetScreenSize;
275    req->window = window;
276    req->width = width;
277    req->height = height;
278    req->widthInMillimeters = mmWidth;
279    req->heightInMillimeters = mmHeight;
280    UnlockDisplay (dpy);
281    SyncHandle ();
282}
283