summaryrefslogtreecommitdiff
path: root/src/XrrScreen.c
blob: 54bc2cafc12b213a0865bb76be303efc1ef54e00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 * Copyright © 2006 Keith Packard
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * 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.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <X11/Xlib.h>
/* we need to be able to manipulate the Display structure on events */
#include <X11/Xlibint.h>
#include <X11/extensions/render.h>
#include <X11/extensions/Xrender.h>
#include "Xrandrint.h"

/*
 * this is cheating on the knowledge that the two requests are identical
 * but for the request number.
 */
static XRRScreenResources *
doGetScreenResources (Display *dpy, Window window, int poll)
{
    XExtDisplayInfo		*info = XRRFindDisplay(dpy);
    xRRGetScreenResourcesReply  rep;
    xRRGetScreenResourcesReq	*req;
    _XAsyncHandler		async;
    _XRRVersionState		async_state;
    int				nbytes, nbytesRead, rbytes;
    int				i;
    xRRQueryVersionReq		*vreq;
    XRRScreenResources		*xrsr;
    char			*names;
    char			*wire_names, *wire_name;
    Bool			getting_version = False;
    XRandRInfo			*xrri;

    RRCheckExtension (dpy, info, NULL);

    LockDisplay (dpy);
    xrri = (XRandRInfo *) info->data;

    if (xrri->major_version == -1)
    {
	/* hide a version query in the request */
	GetReq (RRQueryVersion, vreq);
	vreq->reqType = info->codes->major_opcode;
	vreq->randrReqType = X_RRQueryVersion;
	vreq->majorVersion = RANDR_MAJOR;
	vreq->minorVersion = RANDR_MINOR;
    
	async_state.version_seq = dpy->request;
	async_state.error = False;
	async.next = dpy->async_handlers;
	async.handler = _XRRVersionHandler;
	async.data = (XPointer) &async_state;
	dpy->async_handlers = &async;

	getting_version = True;
    }

    GetReq (RRGetScreenResources, req);
    req->reqType = info->codes->major_opcode;
    req->randrReqType = poll ? X_RRGetScreenResources
			     : X_RRGetScreenResourcesCurrent;
    req->window = window;

    if (!_XReply (dpy, (xReply *) &rep, 0, xFalse))
    {
	if (getting_version)
	    DeqAsyncHandler (dpy, &async);
	UnlockDisplay (dpy);
	SyncHandle ();
	return NULL;
    }
    if (getting_version)
    {
	DeqAsyncHandler (dpy, &async);
	if (async_state.error)
	{
	    UnlockDisplay (dpy);
	    SyncHandle();
	    LockDisplay (dpy);
	}
	xrri->major_version = async_state.major_version;
	xrri->minor_version = async_state.minor_version;
	xrri->has_rates = _XRRHasRates (xrri->minor_version, xrri->major_version);
    }

    nbytes = (long) rep.length << 2;

    nbytesRead = (long) (rep.nCrtcs * 4 +
			 rep.nOutputs * 4 +
			 rep.nModes * SIZEOF (xRRModeInfo) +
			 ((rep.nbytesNames + 3) & ~3));

    /* 
     * first we must compute how much space to allocate for 
     * randr library's use; we'll allocate the structures in a single
     * allocation, on cleanlyness grounds.
     */

    rbytes = (sizeof (XRRScreenResources) +
	      rep.nCrtcs * sizeof (RRCrtc) +
	      rep.nOutputs * sizeof (RROutput) +
	      rep.nModes * sizeof (XRRModeInfo) +
	      rep.nbytesNames + rep.nModes);	/* '\0' terminate names */

    xrsr = (XRRScreenResources *) Xmalloc(rbytes);
    wire_names = (char *) Xmalloc (rep.nbytesNames);
    if (xrsr == NULL || wire_names == NULL) {
	if (xrsr) Xfree (xrsr);
	if (wire_names) Xfree (wire_names);
	_XEatData (dpy, (unsigned long) nbytes);
	UnlockDisplay (dpy);
	SyncHandle ();
	return NULL;
    }

    xrsr->timestamp = rep.timestamp;
    xrsr->configTimestamp = rep.configTimestamp;
    xrsr->ncrtc = rep.nCrtcs;
    xrsr->crtcs = (RRCrtc *) (xrsr + 1);
    xrsr->noutput = rep.nOutputs;
    xrsr->outputs = (RROutput *) (xrsr->crtcs + rep.nCrtcs);
    xrsr->nmode = rep.nModes;
    xrsr->modes = (XRRModeInfo *) (xrsr->outputs + rep.nOutputs);
    names = (char *) (xrsr->modes + rep.nModes);

    _XRead32 (dpy, xrsr->crtcs, rep.nCrtcs << 2);
    _XRead32 (dpy, xrsr->outputs, rep.nOutputs << 2);
    
    for (i = 0; i < rep.nModes; i++)  {
	xRRModeInfo modeInfo;
	
	_XReadPad (dpy, (char *) &modeInfo, SIZEOF (xRRModeInfo));
	xrsr->modes[i].id = modeInfo.id;
	xrsr->modes[i].width = modeInfo.width;
	xrsr->modes[i].height = modeInfo.height;
	xrsr->modes[i].dotClock = modeInfo.dotClock;
	xrsr->modes[i].hSyncStart = modeInfo.hSyncStart;
	xrsr->modes[i].hSyncEnd = modeInfo.hSyncEnd;
	xrsr->modes[i].hTotal = modeInfo.hTotal;
	xrsr->modes[i].hSkew = modeInfo.hSkew;
	xrsr->modes[i].vSyncStart = modeInfo.vSyncStart;
	xrsr->modes[i].vSyncEnd = modeInfo.vSyncEnd;
	xrsr->modes[i].vTotal = modeInfo.vTotal;
	xrsr->modes[i].nameLength = modeInfo.nameLength;
	xrsr->modes[i].modeFlags = modeInfo.modeFlags;
    }

    /*
     * Read names and '\0' pad each one
     */
    _XReadPad (dpy, wire_names, rep.nbytesNames);
    wire_name = wire_names;
    for (i = 0; i < rep.nModes; i++)  {
	xrsr->modes[i].name = names;
	memcpy (names, wire_name, xrsr->modes[i].nameLength);
	names[xrsr->modes[i].nameLength] = '\0';
	names += xrsr->modes[i].nameLength + 1;
	wire_name += xrsr->modes[i].nameLength;
    }
    Xfree (wire_names);
    
    /*
     * Skip any extra data
     */
    if (nbytes > nbytesRead)
	_XEatData (dpy, (unsigned long) (nbytes - nbytesRead));
    
    UnlockDisplay (dpy);
    SyncHandle();
    return (XRRScreenResources *) xrsr;
}

XRRScreenResources *
XRRGetScreenResources(Display *dpy, Window window)
{
    return doGetScreenResources(dpy, window, 1);
}

XRRScreenResources *
XRRGetScreenResourcesCurrent(Display *dpy, Window window)
{
    return doGetScreenResources(dpy, window, 0);
}

void
XRRFreeScreenResources (XRRScreenResources *resources)
{
    Xfree (resources);
}

Status
XRRGetScreenSizeRange (Display *dpy, Window window,
		       int *minWidth, int *minHeight,
		       int *maxWidth, int *maxHeight)
{
    XExtDisplayInfo		*info = XRRFindDisplay(dpy);
    xRRGetScreenSizeRangeReq	*req;
    xRRGetScreenSizeRangeReply	rep;

    RRCheckExtension (dpy, info, 0);
    LockDisplay (dpy);
    GetReq (RRGetScreenSizeRange, req);
    req->reqType = info->codes->major_opcode;
    req->randrReqType = X_RRGetScreenSizeRange;
    req->window = window;
    if (!_XReply (dpy, (xReply *) &rep, 0, xFalse))
    {
	UnlockDisplay (dpy);
	SyncHandle ();
	return False;
    }
    UnlockDisplay (dpy);
    SyncHandle ();
    *minWidth = rep.minWidth;
    *minHeight = rep.minHeight;
    *maxWidth = rep.maxWidth;
    *maxHeight = rep.maxHeight;
   return True;
}

void
XRRSetScreenSize (Display *dpy, Window window,
		  int width, int height,
		  int mmWidth, int mmHeight)
{
    XExtDisplayInfo		*info = XRRFindDisplay(dpy);
    xRRSetScreenSizeReq		*req;

    RRSimpleCheckExtension (dpy, info);
    LockDisplay (dpy);
    GetReq (RRSetScreenSize, req);
    req->reqType = info->codes->major_opcode;
    req->randrReqType = X_RRSetScreenSize;
    req->window = window;
    req->width = width;
    req->height = height;
    req->widthInMillimeters = mmWidth;
    req->heightInMillimeters = mmHeight;
    UnlockDisplay (dpy);
    SyncHandle ();
}