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 = realloc(output->modes,
59			  (output->numModes + 1) * sizeof (RRModePtr));
60    else
61	modes = malloc(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	RROutputSetSubpixelOrder (output, PictureGetSubpixelOrder (pScreen));
103    }
104
105    output = pScrPriv->outputs[0];
106    if (!output)
107	return;
108    crtc = pScrPriv->crtcs[0];
109    if (!crtc)
110	return;
111
112    /* check rotations */
113    if (rotations != crtc->rotations)
114    {
115        crtc->rotations = rotations;
116	crtc->changed = TRUE;
117	pScrPriv->changed = TRUE;
118    }
119
120    /* regenerate mode list */
121    for (i = 0; i < pScrPriv->nSizes; i++)
122    {
123	RRScreenSizePtr	size = &pScrPriv->pSizes[i];
124	int		r;
125
126	if (size->nRates)
127	{
128	    for (r = 0; r < size->nRates; r++)
129	    {
130		mode = RROldModeAdd (output, size, size->pRates[r].rate);
131		if (i == pScrPriv->size &&
132		    size->pRates[r].rate == pScrPriv->rate)
133		{
134		    newMode = mode;
135		}
136	    }
137	    free(size->pRates);
138	}
139	else
140	{
141	    mode = RROldModeAdd (output, size, 0);
142	    if (i == pScrPriv->size)
143		newMode = mode;
144	}
145    }
146    if (pScrPriv->nSizes)
147	free(pScrPriv->pSizes);
148    pScrPriv->pSizes = NULL;
149    pScrPriv->nSizes = 0;
150
151    /* find size bounds */
152    for (i = 0; i < output->numModes + output->numUserModes; i++)
153    {
154	RRModePtr   mode = (i < output->numModes ?
155			    output->modes[i] :
156			    output->userModes[i-output->numModes]);
157        CARD16	    width = mode->mode.width;
158        CARD16	    height = mode->mode.height;
159
160	if (width < minWidth) minWidth = width;
161	if (width > maxWidth) maxWidth = width;
162	if (height < minHeight) minHeight = height;
163	if (height > maxHeight) maxHeight = height;
164    }
165
166    RRScreenSetSizeRange (pScreen, minWidth, minHeight, maxWidth, maxHeight);
167
168    /* notice current mode */
169    if (newMode)
170	RRCrtcNotify (crtc, newMode, 0, 0, pScrPriv->rotation,
171		      NULL, 1, &output);
172}
173#endif
174
175/*
176 * Poll the driver for changed information
177 */
178Bool
179RRGetInfo (ScreenPtr pScreen, Bool force_query)
180{
181    rrScrPriv (pScreen);
182    Rotation	    rotations;
183    int		    i;
184
185    /* Return immediately if we don't need to re-query and we already have the
186     * information.
187     */
188    if (!force_query) {
189	if (pScrPriv->numCrtcs != 0 || pScrPriv->numOutputs != 0)
190	    return TRUE;
191    }
192
193    for (i = 0; i < pScrPriv->numOutputs; i++)
194	pScrPriv->outputs[i]->changed = FALSE;
195    for (i = 0; i < pScrPriv->numCrtcs; i++)
196	pScrPriv->crtcs[i]->changed = FALSE;
197
198    rotations = 0;
199    pScrPriv->changed = FALSE;
200    pScrPriv->configChanged = FALSE;
201
202    if (!(*pScrPriv->rrGetInfo) (pScreen, &rotations))
203	return FALSE;
204
205#if RANDR_10_INTERFACE
206    if (pScrPriv->nSizes)
207	RRScanOldConfig (pScreen, rotations);
208#endif
209    RRTellChanged (pScreen);
210    return TRUE;
211}
212
213/*
214 * Register the range of sizes for the screen
215 */
216void
217RRScreenSetSizeRange (ScreenPtr	pScreen,
218		      CARD16	minWidth,
219		      CARD16	minHeight,
220		      CARD16	maxWidth,
221		      CARD16	maxHeight)
222{
223    rrScrPriv (pScreen);
224
225    if (!pScrPriv)
226	return;
227    if (pScrPriv->minWidth == minWidth && pScrPriv->minHeight == minHeight &&
228	pScrPriv->maxWidth == maxWidth && pScrPriv->maxHeight == maxHeight)
229    {
230	return;
231    }
232
233    pScrPriv->minWidth  = minWidth;
234    pScrPriv->minHeight = minHeight;
235    pScrPriv->maxWidth  = maxWidth;
236    pScrPriv->maxHeight = maxHeight;
237    pScrPriv->changed = TRUE;
238    pScrPriv->configChanged = TRUE;
239}
240
241#ifdef RANDR_10_INTERFACE
242static Bool
243RRScreenSizeMatches (RRScreenSizePtr  a,
244		   RRScreenSizePtr  b)
245{
246    if (a->width != b->width)
247	return FALSE;
248    if (a->height != b->height)
249	return FALSE;
250    if (a->mmWidth != b->mmWidth)
251	return FALSE;
252    if (a->mmHeight != b->mmHeight)
253	return FALSE;
254    return TRUE;
255}
256
257RRScreenSizePtr
258RRRegisterSize (ScreenPtr	    pScreen,
259		short		    width,
260		short		    height,
261		short		    mmWidth,
262		short		    mmHeight)
263{
264    rrScrPriv (pScreen);
265    int		    i;
266    RRScreenSize    tmp;
267    RRScreenSizePtr pNew;
268
269    if (!pScrPriv)
270	return 0;
271
272    tmp.id = 0;
273    tmp.width = width;
274    tmp.height= height;
275    tmp.mmWidth = mmWidth;
276    tmp.mmHeight = mmHeight;
277    tmp.pRates = 0;
278    tmp.nRates = 0;
279    for (i = 0; i < pScrPriv->nSizes; i++)
280	if (RRScreenSizeMatches (&tmp, &pScrPriv->pSizes[i]))
281	    return &pScrPriv->pSizes[i];
282    pNew = realloc(pScrPriv->pSizes,
283		     (pScrPriv->nSizes + 1) * sizeof (RRScreenSize));
284    if (!pNew)
285	return 0;
286    pNew[pScrPriv->nSizes++] = tmp;
287    pScrPriv->pSizes = pNew;
288    return &pNew[pScrPriv->nSizes-1];
289}
290
291Bool RRRegisterRate (ScreenPtr		pScreen,
292		     RRScreenSizePtr	pSize,
293		     int		rate)
294{
295    rrScrPriv(pScreen);
296    int		    i;
297    RRScreenRatePtr pNew, pRate;
298
299    if (!pScrPriv)
300	return FALSE;
301
302    for (i = 0; i < pSize->nRates; i++)
303	if (pSize->pRates[i].rate == rate)
304	    return TRUE;
305
306    pNew = realloc(pSize->pRates,
307		     (pSize->nRates + 1) * sizeof (RRScreenRate));
308    if (!pNew)
309	return FALSE;
310    pRate = &pNew[pSize->nRates++];
311    pRate->rate = rate;
312    pSize->pRates = pNew;
313    return TRUE;
314}
315
316Rotation
317RRGetRotation(ScreenPtr pScreen)
318{
319    RROutputPtr	output = RRFirstOutput (pScreen);
320
321    if (!output)
322	return RR_Rotate_0;
323
324    return output->crtc->rotation;
325}
326
327void
328RRSetCurrentConfig (ScreenPtr		pScreen,
329		    Rotation		rotation,
330		    int			rate,
331		    RRScreenSizePtr	pSize)
332{
333    rrScrPriv (pScreen);
334
335    if (!pScrPriv)
336	return;
337    pScrPriv->size = pSize - pScrPriv->pSizes;
338    pScrPriv->rotation = rotation;
339    pScrPriv->rate = rate;
340}
341#endif
342