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