rrinfo.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
25#ifdef RANDR_10_INTERFACE
26static RRModePtr
27RROldModeAdd (RROutputPtr output, RRScreenSizePtr size, int refresh)
28{
29    ScreenPtr	pScreen = output->pScreen;
30    rrScrPriv(pScreen);
31    xRRModeInfo	modeInfo;
32    char	name[100];
33    RRModePtr	mode;
34    int		i;
35    RRModePtr   *modes;
36
37    memset (&modeInfo, '\0', sizeof (modeInfo));
38    sprintf (name, "%dx%d", size->width, size->height);
39
40    modeInfo.width = size->width;
41    modeInfo.height = size->height;
42    modeInfo.hTotal = size->width;
43    modeInfo.vTotal = size->height;
44    modeInfo.dotClock = ((CARD32) size->width * (CARD32) size->height *
45			 (CARD32) refresh);
46    modeInfo.nameLength = strlen (name);
47    mode = RRModeGet (&modeInfo, name);
48    if (!mode)
49	return NULL;
50    for (i = 0; i < output->numModes; i++)
51	if (output->modes[i] == mode)
52	{
53	    RRModeDestroy (mode);
54	    return mode;
55	}
56
57    if (output->numModes)
58	modes = xrealloc (output->modes,
59			  (output->numModes + 1) * sizeof (RRModePtr));
60    else
61	modes = xalloc (sizeof (RRModePtr));
62    if (!modes)
63    {
64	RRModeDestroy (mode);
65	FreeResource (mode->mode.id, 0);
66	return NULL;
67    }
68    modes[output->numModes++] = mode;
69    output->modes = modes;
70    output->changed = TRUE;
71    pScrPriv->changed = TRUE;
72    pScrPriv->configChanged = TRUE;
73    return mode;
74}
75
76static void
77RRScanOldConfig (ScreenPtr pScreen, Rotation rotations)
78{
79    rrScrPriv(pScreen);
80    RROutputPtr	output;
81    RRCrtcPtr	crtc;
82    RRModePtr	mode, newMode = NULL;
83    int		i;
84    CARD16	minWidth = MAXSHORT, minHeight = MAXSHORT;
85    CARD16	maxWidth = 0, maxHeight = 0;
86
87    /*
88     * First time through, create a crtc and output and hook
89     * them together
90     */
91    if (pScrPriv->numOutputs == 0 &&
92	pScrPriv->numCrtcs == 0)
93    {
94	crtc = RRCrtcCreate (pScreen, NULL);
95	if (!crtc)
96	    return;
97	output = RROutputCreate (pScreen, "default", 7, NULL);
98	if (!output)
99	    return;
100	RROutputSetCrtcs (output, &crtc, 1);
101	RROutputSetConnection (output, RR_Connected);
102#ifdef RENDER
103	RROutputSetSubpixelOrder (output, PictureGetSubpixelOrder (pScreen));
104#endif
105    }
106
107    output = pScrPriv->outputs[0];
108    if (!output)
109	return;
110    crtc = pScrPriv->crtcs[0];
111    if (!crtc)
112	return;
113
114    /* check rotations */
115    if (rotations != crtc->rotations)
116    {
117        crtc->rotations = rotations;
118	crtc->changed = TRUE;
119	pScrPriv->changed = TRUE;
120    }
121
122    /* regenerate mode list */
123    for (i = 0; i < pScrPriv->nSizes; i++)
124    {
125	RRScreenSizePtr	size = &pScrPriv->pSizes[i];
126	int		r;
127
128	if (size->nRates)
129	{
130	    for (r = 0; r < size->nRates; r++)
131	    {
132		mode = RROldModeAdd (output, size, size->pRates[r].rate);
133		if (i == pScrPriv->size &&
134		    size->pRates[r].rate == pScrPriv->rate)
135		{
136		    newMode = mode;
137		}
138	    }
139	    xfree (size->pRates);
140	}
141	else
142	{
143	    mode = RROldModeAdd (output, size, 0);
144	    if (i == pScrPriv->size)
145		newMode = mode;
146	}
147    }
148    if (pScrPriv->nSizes)
149	xfree (pScrPriv->pSizes);
150    pScrPriv->pSizes = NULL;
151    pScrPriv->nSizes = 0;
152
153    /* find size bounds */
154    for (i = 0; i < output->numModes + output->numUserModes; i++)
155    {
156	RRModePtr   mode = (i < output->numModes ?
157			    output->modes[i] :
158			    output->userModes[i-output->numModes]);
159        CARD16	    width = mode->mode.width;
160        CARD16	    height = mode->mode.height;
161
162	if (width < minWidth) minWidth = width;
163	if (width > maxWidth) maxWidth = width;
164	if (height < minHeight) minHeight = height;
165	if (height > maxHeight) maxHeight = height;
166    }
167
168    RRScreenSetSizeRange (pScreen, minWidth, minHeight, maxWidth, maxHeight);
169
170    /* notice current mode */
171    if (newMode)
172	RRCrtcNotify (crtc, newMode, 0, 0, pScrPriv->rotation,
173		      1, &output);
174}
175#endif
176
177/*
178 * Poll the driver for changed information
179 */
180Bool
181RRGetInfo (ScreenPtr pScreen)
182{
183    rrScrPriv (pScreen);
184    Rotation	    rotations;
185    int		    i;
186
187    for (i = 0; i < pScrPriv->numOutputs; i++)
188	pScrPriv->outputs[i]->changed = FALSE;
189    for (i = 0; i < pScrPriv->numCrtcs; i++)
190	pScrPriv->crtcs[i]->changed = FALSE;
191
192    rotations = 0;
193    pScrPriv->changed = FALSE;
194    pScrPriv->configChanged = FALSE;
195
196    if (!(*pScrPriv->rrGetInfo) (pScreen, &rotations))
197	return FALSE;
198
199#if RANDR_10_INTERFACE
200    if (pScrPriv->nSizes)
201	RRScanOldConfig (pScreen, rotations);
202#endif
203    RRTellChanged (pScreen);
204    return TRUE;
205}
206
207/*
208 * Register the range of sizes for the screen
209 */
210void
211RRScreenSetSizeRange (ScreenPtr	pScreen,
212		      CARD16	minWidth,
213		      CARD16	minHeight,
214		      CARD16	maxWidth,
215		      CARD16	maxHeight)
216{
217    rrScrPriv (pScreen);
218
219    if (!pScrPriv)
220	return;
221    if (pScrPriv->minWidth == minWidth && pScrPriv->minHeight == minHeight &&
222	pScrPriv->maxWidth == maxWidth && pScrPriv->maxHeight == maxHeight)
223    {
224	return;
225    }
226
227    pScrPriv->minWidth  = minWidth;
228    pScrPriv->minHeight = minHeight;
229    pScrPriv->maxWidth  = maxWidth;
230    pScrPriv->maxHeight = maxHeight;
231    pScrPriv->changed = TRUE;
232    pScrPriv->configChanged = TRUE;
233}
234
235#ifdef RANDR_10_INTERFACE
236static Bool
237RRScreenSizeMatches (RRScreenSizePtr  a,
238		   RRScreenSizePtr  b)
239{
240    if (a->width != b->width)
241	return FALSE;
242    if (a->height != b->height)
243	return FALSE;
244    if (a->mmWidth != b->mmWidth)
245	return FALSE;
246    if (a->mmHeight != b->mmHeight)
247	return FALSE;
248    return TRUE;
249}
250
251RRScreenSizePtr
252RRRegisterSize (ScreenPtr	    pScreen,
253		short		    width,
254		short		    height,
255		short		    mmWidth,
256		short		    mmHeight)
257{
258    rrScrPriv (pScreen);
259    int		    i;
260    RRScreenSize    tmp;
261    RRScreenSizePtr pNew;
262
263    if (!pScrPriv)
264	return 0;
265
266    tmp.id = 0;
267    tmp.width = width;
268    tmp.height= height;
269    tmp.mmWidth = mmWidth;
270    tmp.mmHeight = mmHeight;
271    tmp.pRates = 0;
272    tmp.nRates = 0;
273    for (i = 0; i < pScrPriv->nSizes; i++)
274	if (RRScreenSizeMatches (&tmp, &pScrPriv->pSizes[i]))
275	    return &pScrPriv->pSizes[i];
276    pNew = xrealloc (pScrPriv->pSizes,
277		     (pScrPriv->nSizes + 1) * sizeof (RRScreenSize));
278    if (!pNew)
279	return 0;
280    pNew[pScrPriv->nSizes++] = tmp;
281    pScrPriv->pSizes = pNew;
282    return &pNew[pScrPriv->nSizes-1];
283}
284
285Bool RRRegisterRate (ScreenPtr		pScreen,
286		     RRScreenSizePtr	pSize,
287		     int		rate)
288{
289    rrScrPriv(pScreen);
290    int		    i;
291    RRScreenRatePtr pNew, pRate;
292
293    if (!pScrPriv)
294	return FALSE;
295
296    for (i = 0; i < pSize->nRates; i++)
297	if (pSize->pRates[i].rate == rate)
298	    return TRUE;
299
300    pNew = xrealloc (pSize->pRates,
301		     (pSize->nRates + 1) * sizeof (RRScreenRate));
302    if (!pNew)
303	return FALSE;
304    pRate = &pNew[pSize->nRates++];
305    pRate->rate = rate;
306    pSize->pRates = pNew;
307    return TRUE;
308}
309
310Rotation
311RRGetRotation(ScreenPtr pScreen)
312{
313    RROutputPtr	output = RRFirstOutput (pScreen);
314
315    if (!output)
316	return RR_Rotate_0;
317
318    return output->crtc->rotation;
319}
320
321void
322RRSetCurrentConfig (ScreenPtr		pScreen,
323		    Rotation		rotation,
324		    int			rate,
325		    RRScreenSizePtr	pSize)
326{
327    rrScrPriv (pScreen);
328
329    if (!pScrPriv)
330	return;
331    pScrPriv->size = pSize - pScrPriv->pSizes;
332    pScrPriv->rotation = rotation;
333    pScrPriv->rate = rate;
334}
335#endif
336