rrmode.c revision 05b261ec
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#include "randrstr.h"
24
25RESTYPE	RRModeType;
26
27static Bool
28RRModeEqual (xRRModeInfo *a, xRRModeInfo *b)
29{
30    if (a->width != b->width) return FALSE;
31    if (a->height != b->height) return FALSE;
32    if (a->dotClock != b->dotClock) return FALSE;
33    if (a->hSyncStart != b->hSyncStart) return FALSE;
34    if (a->hSyncEnd != b->hSyncEnd) return FALSE;
35    if (a->hTotal != b->hTotal) return FALSE;
36    if (a->hSkew != b->hSkew) return FALSE;
37    if (a->vSyncStart != b->vSyncStart) return FALSE;
38    if (a->vSyncEnd != b->vSyncEnd) return FALSE;
39    if (a->vTotal != b->vTotal) return FALSE;
40    if (a->nameLength != b->nameLength) return FALSE;
41    if (a->modeFlags != b->modeFlags) return FALSE;
42    return TRUE;
43}
44
45/*
46 * Keep a list so it's easy to find modes in the resource database.
47 */
48static int	    num_modes;
49static RRModePtr    *modes;
50
51static RRModePtr
52RRModeCreate (xRRModeInfo   *modeInfo,
53	      const char    *name,
54	      ScreenPtr	    userScreen)
55{
56    RRModePtr	mode, *newModes;
57
58    if (!RRInit ())
59	return NULL;
60
61    mode = xalloc (sizeof (RRModeRec) + modeInfo->nameLength + 1);
62    if (!mode)
63	return NULL;
64    mode->refcnt = 1;
65    mode->mode = *modeInfo;
66    mode->name = (char *) (mode + 1);
67    memcpy (mode->name, name, modeInfo->nameLength);
68    mode->name[modeInfo->nameLength] = '\0';
69    mode->userScreen = userScreen;
70
71    if (num_modes)
72	newModes = xrealloc (modes, (num_modes + 1) * sizeof (RRModePtr));
73    else
74	newModes = xalloc (sizeof (RRModePtr));
75
76    if (!newModes)
77    {
78	xfree (mode);
79	return NULL;
80    }
81
82    mode->mode.id = FakeClientID(0);
83    if (!AddResource (mode->mode.id, RRModeType, (pointer) mode))
84	return NULL;
85    modes = newModes;
86    modes[num_modes++] = mode;
87
88    /*
89     * give the caller a reference to this mode
90     */
91    ++mode->refcnt;
92    return mode;
93}
94
95static RRModePtr
96RRModeFindByName (const char	*name,
97		  CARD16    	nameLength)
98{
99    int		i;
100    RRModePtr	mode;
101
102    for (i = 0; i < num_modes; i++)
103    {
104	mode = modes[i];
105	if (mode->mode.nameLength == nameLength &&
106	    !memcmp (name, mode->name, nameLength))
107	{
108	    return mode;
109	}
110    }
111    return NULL;
112}
113
114RRModePtr
115RRModeGet (xRRModeInfo	*modeInfo,
116	   const char	*name)
117{
118    int	i;
119
120    for (i = 0; i < num_modes; i++)
121    {
122	RRModePtr   mode = modes[i];
123	if (RRModeEqual (&mode->mode, modeInfo) &&
124	    !memcmp (name, mode->name, modeInfo->nameLength))
125	{
126	    ++mode->refcnt;
127	    return mode;
128	}
129    }
130
131    return RRModeCreate (modeInfo, name, NULL);
132}
133
134static RRModePtr
135RRModeCreateUser (ScreenPtr	pScreen,
136		  xRRModeInfo	*modeInfo,
137		  const char	*name,
138		  int		*error)
139{
140    RRModePtr	mode;
141
142    mode = RRModeFindByName (name, modeInfo->nameLength);
143    if (mode)
144    {
145	*error = BadName;
146	return NULL;
147    }
148
149    mode = RRModeCreate (modeInfo, name, pScreen);
150    if (!mode)
151    {
152	*error = BadAlloc;
153	return NULL;
154    }
155    *error = Success;
156    return mode;
157}
158
159RRModePtr *
160RRModesForScreen (ScreenPtr pScreen, int *num_ret)
161{
162    rrScrPriv(pScreen);
163    int		o, c, m;
164    RRModePtr	*screen_modes;
165    int		num_screen_modes = 0;
166
167    screen_modes = xalloc ((num_modes ? num_modes : 1) * sizeof (RRModePtr));
168
169    /*
170     * Add modes from all outputs
171     */
172    for (o = 0; o < pScrPriv->numOutputs; o++)
173    {
174	RROutputPtr	output = pScrPriv->outputs[o];
175	int		m, n;
176
177	for (m = 0; m < output->numModes + output->numUserModes; m++)
178	{
179	    RRModePtr   mode = (m < output->numModes ?
180				output->modes[m] :
181				output->userModes[m-output->numModes]);
182	    for (n = 0; n < num_screen_modes; n++)
183		if (screen_modes[n] == mode)
184		    break;
185	    if (n == num_screen_modes)
186		screen_modes[num_screen_modes++] = mode;
187	}
188    }
189    /*
190     * Add modes from all crtcs. The goal is to
191     * make sure all available and active modes
192     * are visible to the client
193     */
194    for (c = 0; c < pScrPriv->numCrtcs; c++)
195    {
196	RRCrtcPtr	crtc = pScrPriv->crtcs[c];
197	RRModePtr	mode = crtc->mode;
198	int		n;
199
200	if (!mode) continue;
201	for (n = 0; n < num_screen_modes; n++)
202	    if (screen_modes[n] == mode)
203		break;
204	if (n == num_screen_modes)
205	    screen_modes[num_screen_modes++] = mode;
206    }
207    /*
208     * Add all user modes for this screen
209     */
210    for (m = 0; m < num_modes; m++)
211    {
212	RRModePtr	mode = modes[m];
213	int		n;
214
215	if (mode->userScreen != pScreen)
216	    continue;
217	for (n = 0; n < num_screen_modes; n++)
218	    if (screen_modes[n] == mode)
219		break;
220	if (n == num_screen_modes)
221	    screen_modes[num_screen_modes++] = mode;
222    }
223
224    *num_ret = num_screen_modes;
225    return screen_modes;
226}
227
228void
229RRModeDestroy (RRModePtr mode)
230{
231    int	m;
232
233    if (--mode->refcnt > 0)
234	return;
235    for (m = 0; m < num_modes; m++)
236    {
237	if (modes[m] == mode)
238	{
239	    memmove (modes + m, modes + m + 1,
240		     (num_modes - m - 1) * sizeof (RRModePtr));
241	    num_modes--;
242	    if (!num_modes)
243	    {
244		xfree (modes);
245		modes = NULL;
246	    }
247	    break;
248	}
249    }
250
251    xfree (mode);
252}
253
254static int
255RRModeDestroyResource (pointer value, XID pid)
256{
257    RRModeDestroy ((RRModePtr) value);
258    return 1;
259}
260
261Bool
262RRModeInit (void)
263{
264    assert (num_modes == 0);
265    assert (modes == NULL);
266    RRModeType = CreateNewResourceType (RRModeDestroyResource);
267    if (!RRModeType)
268	return FALSE;
269#ifdef XResExtension
270    RegisterResourceName (RRModeType, "MODE");
271#endif
272    return TRUE;
273}
274
275int
276ProcRRCreateMode (ClientPtr client)
277{
278    REQUEST(xRRCreateModeReq);
279    xRRCreateModeReply	rep;
280    WindowPtr		pWin;
281    ScreenPtr		pScreen;
282    rrScrPrivPtr	pScrPriv;
283    xRRModeInfo		*modeInfo;
284    long		units_after;
285    char		*name;
286    int			error, rc;
287    RRModePtr		mode;
288
289    REQUEST_AT_LEAST_SIZE (xRRCreateModeReq);
290    rc = dixLookupWindow(&pWin, stuff->window, client, DixReadAccess);
291    if (rc != Success)
292	return rc;
293
294    pScreen = pWin->drawable.pScreen;
295    pScrPriv = rrGetScrPriv(pScreen);
296
297    modeInfo = &stuff->modeInfo;
298    name = (char *) (stuff + 1);
299    units_after = (stuff->length - (sizeof (xRRCreateModeReq) >> 2));
300
301    /* check to make sure requested name fits within the data provided */
302    if ((int) (modeInfo->nameLength + 3) >> 2 > units_after)
303	return BadLength;
304
305    mode = RRModeCreateUser (pScreen, modeInfo, name, &error);
306    if (!mode)
307	return error;
308
309    rep.type = X_Reply;
310    rep.pad0 = 0;
311    rep.sequenceNumber = client->sequence;
312    rep.length = 0;
313    rep.mode = mode->mode.id;
314    if (client->swapped)
315    {
316	int n;
317    	swaps(&rep.sequenceNumber, n);
318    	swapl(&rep.length, n);
319	swapl(&rep.mode, n);
320    }
321    WriteToClient(client, sizeof(xRRCreateModeReply), (char *)&rep);
322
323    return client->noClientException;
324}
325
326int
327ProcRRDestroyMode (ClientPtr client)
328{
329    REQUEST(xRRDestroyModeReq);
330    RRModePtr	mode;
331
332    REQUEST_SIZE_MATCH(xRRDestroyModeReq);
333    mode = LookupIDByType (stuff->mode, RRModeType);
334    if (!mode)
335    {
336	client->errorValue = stuff->mode;
337	return RRErrorBase + BadRRMode;
338    }
339    if (!mode->userScreen)
340	return BadMatch;
341    if (mode->refcnt > 1)
342	return BadAccess;
343    FreeResource (stuff->mode, 0);
344    return Success;
345}
346
347int
348ProcRRAddOutputMode (ClientPtr client)
349{
350    REQUEST(xRRAddOutputModeReq);
351    RRModePtr	mode;
352    RROutputPtr	output;
353
354    REQUEST_SIZE_MATCH(xRRAddOutputModeReq);
355    output = LookupOutput(client, stuff->output, DixReadAccess);
356
357    if (!output)
358    {
359	client->errorValue = stuff->output;
360	return RRErrorBase + BadRROutput;
361    }
362
363    mode = LookupIDByType (stuff->mode, RRModeType);
364    if (!mode)
365    {
366	client->errorValue = stuff->mode;
367	return RRErrorBase + BadRRMode;
368    }
369
370    return RROutputAddUserMode (output, mode);
371}
372
373int
374ProcRRDeleteOutputMode (ClientPtr client)
375{
376    REQUEST(xRRDeleteOutputModeReq);
377    RRModePtr	mode;
378    RROutputPtr	output;
379
380    REQUEST_SIZE_MATCH(xRRDeleteOutputModeReq);
381    output = LookupOutput(client, stuff->output, DixReadAccess);
382
383    if (!output)
384    {
385	client->errorValue = stuff->output;
386	return RRErrorBase + BadRROutput;
387    }
388
389    mode = LookupIDByType (stuff->mode, RRModeType);
390    if (!mode)
391    {
392	client->errorValue = stuff->mode;
393	return RRErrorBase + BadRRMode;
394    }
395
396    return RROutputDeleteUserMode (output, mode);
397}
398