xref: /OK3568_Linux_fs/external/xserver/hw/xfree86/modes/xf86Crtc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright © 2006 Keith Packard
3*4882a593Smuzhiyun  * Copyright © 2008 Red Hat, Inc.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Permission to use, copy, modify, distribute, and sell this software and its
6*4882a593Smuzhiyun  * documentation for any purpose is hereby granted without fee, provided that
7*4882a593Smuzhiyun  * the above copyright notice appear in all copies and that both that copyright
8*4882a593Smuzhiyun  * notice and this permission notice appear in supporting documentation, and
9*4882a593Smuzhiyun  * that the name of the copyright holders not be used in advertising or
10*4882a593Smuzhiyun  * publicity pertaining to distribution of the software without specific,
11*4882a593Smuzhiyun  * written prior permission.  The copyright holders make no representations
12*4882a593Smuzhiyun  * about the suitability of this software for any purpose.  It is provided "as
13*4882a593Smuzhiyun  * is" without express or implied warranty.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16*4882a593Smuzhiyun  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17*4882a593Smuzhiyun  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18*4882a593Smuzhiyun  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19*4882a593Smuzhiyun  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20*4882a593Smuzhiyun  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21*4882a593Smuzhiyun  * OF THIS SOFTWARE.
22*4882a593Smuzhiyun  */
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #ifdef HAVE_XORG_CONFIG_H
25*4882a593Smuzhiyun #include <xorg-config.h>
26*4882a593Smuzhiyun #endif
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #include <stddef.h>
29*4882a593Smuzhiyun #include <string.h>
30*4882a593Smuzhiyun #include <stdio.h>
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #include "xf86.h"
33*4882a593Smuzhiyun #include "xf86DDC.h"
34*4882a593Smuzhiyun #include "xf86Crtc.h"
35*4882a593Smuzhiyun #include "xf86Modes.h"
36*4882a593Smuzhiyun #include "xf86Priv.h"
37*4882a593Smuzhiyun #include "xf86RandR12.h"
38*4882a593Smuzhiyun #include "X11/extensions/render.h"
39*4882a593Smuzhiyun #include "X11/extensions/dpmsconst.h"
40*4882a593Smuzhiyun #include "X11/Xatom.h"
41*4882a593Smuzhiyun #include "picturestr.h"
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #ifdef XV
44*4882a593Smuzhiyun #include "xf86xv.h"
45*4882a593Smuzhiyun #endif
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #define NO_OUTPUT_DEFAULT_WIDTH 1024
48*4882a593Smuzhiyun #define NO_OUTPUT_DEFAULT_HEIGHT 768
49*4882a593Smuzhiyun /*
50*4882a593Smuzhiyun  * Initialize xf86CrtcConfig structure
51*4882a593Smuzhiyun  */
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun int xf86CrtcConfigPrivateIndex = -1;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun void
xf86CrtcConfigInit(ScrnInfoPtr scrn,const xf86CrtcConfigFuncsRec * funcs)56*4882a593Smuzhiyun xf86CrtcConfigInit(ScrnInfoPtr scrn, const xf86CrtcConfigFuncsRec * funcs)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun     xf86CrtcConfigPtr config;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun     if (xf86CrtcConfigPrivateIndex == -1)
61*4882a593Smuzhiyun         xf86CrtcConfigPrivateIndex = xf86AllocateScrnInfoPrivateIndex();
62*4882a593Smuzhiyun     config = xnfcalloc(1, sizeof(xf86CrtcConfigRec));
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun     config->funcs = funcs;
65*4882a593Smuzhiyun     config->compat_output = -1;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun     scrn->privates[xf86CrtcConfigPrivateIndex].ptr = config;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun void
xf86CrtcSetSizeRange(ScrnInfoPtr scrn,int minWidth,int minHeight,int maxWidth,int maxHeight)71*4882a593Smuzhiyun xf86CrtcSetSizeRange(ScrnInfoPtr scrn,
72*4882a593Smuzhiyun                      int minWidth, int minHeight, int maxWidth, int maxHeight)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun     config->minWidth = minWidth;
77*4882a593Smuzhiyun     config->minHeight = minHeight;
78*4882a593Smuzhiyun     config->maxWidth = maxWidth;
79*4882a593Smuzhiyun     config->maxHeight = maxHeight;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun /*
83*4882a593Smuzhiyun  * Crtc functions
84*4882a593Smuzhiyun  */
85*4882a593Smuzhiyun xf86CrtcPtr
xf86CrtcCreate(ScrnInfoPtr scrn,const xf86CrtcFuncsRec * funcs)86*4882a593Smuzhiyun xf86CrtcCreate(ScrnInfoPtr scrn, const xf86CrtcFuncsRec * funcs)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun     xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
89*4882a593Smuzhiyun     xf86CrtcPtr crtc, *crtcs;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun     crtc = calloc(sizeof(xf86CrtcRec), 1);
92*4882a593Smuzhiyun     if (!crtc)
93*4882a593Smuzhiyun         return NULL;
94*4882a593Smuzhiyun     crtc->version = XF86_CRTC_VERSION;
95*4882a593Smuzhiyun     crtc->scrn = scrn;
96*4882a593Smuzhiyun     crtc->funcs = funcs;
97*4882a593Smuzhiyun #ifdef RANDR_12_INTERFACE
98*4882a593Smuzhiyun     crtc->randr_crtc = NULL;
99*4882a593Smuzhiyun #endif
100*4882a593Smuzhiyun     crtc->rotation = RR_Rotate_0;
101*4882a593Smuzhiyun     crtc->desiredRotation = RR_Rotate_0;
102*4882a593Smuzhiyun     pixman_transform_init_identity(&crtc->crtc_to_framebuffer);
103*4882a593Smuzhiyun     pixman_f_transform_init_identity(&crtc->f_crtc_to_framebuffer);
104*4882a593Smuzhiyun     pixman_f_transform_init_identity(&crtc->f_framebuffer_to_crtc);
105*4882a593Smuzhiyun     crtc->filter = NULL;
106*4882a593Smuzhiyun     crtc->params = NULL;
107*4882a593Smuzhiyun     crtc->nparams = 0;
108*4882a593Smuzhiyun     crtc->filter_width = 0;
109*4882a593Smuzhiyun     crtc->filter_height = 0;
110*4882a593Smuzhiyun     crtc->transform_in_use = FALSE;
111*4882a593Smuzhiyun     crtc->transformPresent = FALSE;
112*4882a593Smuzhiyun     crtc->desiredTransformPresent = FALSE;
113*4882a593Smuzhiyun     memset(&crtc->bounds, '\0', sizeof(crtc->bounds));
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun     /* Preallocate gamma at a sensible size. */
116*4882a593Smuzhiyun     crtc->gamma_size = 256;
117*4882a593Smuzhiyun     crtc->gamma_red = xallocarray(crtc->gamma_size, 3 * sizeof(CARD16));
118*4882a593Smuzhiyun     if (!crtc->gamma_red) {
119*4882a593Smuzhiyun         free(crtc);
120*4882a593Smuzhiyun         return NULL;
121*4882a593Smuzhiyun     }
122*4882a593Smuzhiyun     crtc->gamma_green = crtc->gamma_red + crtc->gamma_size;
123*4882a593Smuzhiyun     crtc->gamma_blue = crtc->gamma_green + crtc->gamma_size;
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun     if (xf86_config->crtc)
126*4882a593Smuzhiyun         crtcs = reallocarray(xf86_config->crtc,
127*4882a593Smuzhiyun                              xf86_config->num_crtc + 1, sizeof(xf86CrtcPtr));
128*4882a593Smuzhiyun     else
129*4882a593Smuzhiyun         crtcs = xallocarray(xf86_config->num_crtc + 1, sizeof(xf86CrtcPtr));
130*4882a593Smuzhiyun     if (!crtcs) {
131*4882a593Smuzhiyun         free(crtc->gamma_red);
132*4882a593Smuzhiyun         free(crtc);
133*4882a593Smuzhiyun         return NULL;
134*4882a593Smuzhiyun     }
135*4882a593Smuzhiyun     xf86_config->crtc = crtcs;
136*4882a593Smuzhiyun     xf86_config->crtc[xf86_config->num_crtc++] = crtc;
137*4882a593Smuzhiyun     return crtc;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun void
xf86CrtcDestroy(xf86CrtcPtr crtc)141*4882a593Smuzhiyun xf86CrtcDestroy(xf86CrtcPtr crtc)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun     xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(crtc->scrn);
144*4882a593Smuzhiyun     int c;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun     (*crtc->funcs->destroy) (crtc);
147*4882a593Smuzhiyun     for (c = 0; c < xf86_config->num_crtc; c++)
148*4882a593Smuzhiyun         if (xf86_config->crtc[c] == crtc) {
149*4882a593Smuzhiyun             memmove(&xf86_config->crtc[c],
150*4882a593Smuzhiyun                     &xf86_config->crtc[c + 1],
151*4882a593Smuzhiyun                     ((xf86_config->num_crtc - (c + 1)) * sizeof(void *)));
152*4882a593Smuzhiyun             xf86_config->num_crtc--;
153*4882a593Smuzhiyun             break;
154*4882a593Smuzhiyun         }
155*4882a593Smuzhiyun     free(crtc->params);
156*4882a593Smuzhiyun     free(crtc->gamma_red);
157*4882a593Smuzhiyun     free(crtc);
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun /**
161*4882a593Smuzhiyun  * Return whether any outputs are connected to the specified pipe
162*4882a593Smuzhiyun  */
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun Bool
xf86CrtcInUse(xf86CrtcPtr crtc)165*4882a593Smuzhiyun xf86CrtcInUse(xf86CrtcPtr crtc)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun     ScrnInfoPtr pScrn = crtc->scrn;
168*4882a593Smuzhiyun     xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
169*4882a593Smuzhiyun     int o;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun     for (o = 0; o < xf86_config->num_output; o++)
172*4882a593Smuzhiyun         if (xf86_config->output[o]->crtc == crtc)
173*4882a593Smuzhiyun             return TRUE;
174*4882a593Smuzhiyun     return FALSE;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun /**
178*4882a593Smuzhiyun  * Return whether the crtc is leased by a client
179*4882a593Smuzhiyun  */
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun static Bool
xf86CrtcIsLeased(xf86CrtcPtr crtc)182*4882a593Smuzhiyun xf86CrtcIsLeased(xf86CrtcPtr crtc)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun     /* If the DIX structure hasn't been created, it can't have been leased */
185*4882a593Smuzhiyun     if (!crtc->randr_crtc)
186*4882a593Smuzhiyun         return FALSE;
187*4882a593Smuzhiyun     return RRCrtcIsLeased(crtc->randr_crtc);
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun /**
191*4882a593Smuzhiyun  * Return whether the output is leased by a client
192*4882a593Smuzhiyun  */
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun static Bool
xf86OutputIsLeased(xf86OutputPtr output)195*4882a593Smuzhiyun xf86OutputIsLeased(xf86OutputPtr output)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun     /* If the DIX structure hasn't been created, it can't have been leased */
198*4882a593Smuzhiyun     if (!output->randr_output)
199*4882a593Smuzhiyun         return FALSE;
200*4882a593Smuzhiyun     return RROutputIsLeased(output->randr_output);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun void
xf86CrtcSetScreenSubpixelOrder(ScreenPtr pScreen)204*4882a593Smuzhiyun xf86CrtcSetScreenSubpixelOrder(ScreenPtr pScreen)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun     int subpixel_order = SubPixelUnknown;
207*4882a593Smuzhiyun     Bool has_none = FALSE;
208*4882a593Smuzhiyun     ScrnInfoPtr scrn = xf86ScreenToScrn(pScreen);
209*4882a593Smuzhiyun     xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
210*4882a593Smuzhiyun     int icrtc, o;
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun     for (icrtc = 0; icrtc < xf86_config->num_crtc; icrtc++) {
213*4882a593Smuzhiyun         xf86CrtcPtr crtc = xf86_config->crtc[icrtc];
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun         for (o = 0; o < xf86_config->num_output; o++) {
216*4882a593Smuzhiyun             xf86OutputPtr output = xf86_config->output[o];
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun             if (output->crtc == crtc) {
219*4882a593Smuzhiyun                 switch (output->subpixel_order) {
220*4882a593Smuzhiyun                 case SubPixelNone:
221*4882a593Smuzhiyun                     has_none = TRUE;
222*4882a593Smuzhiyun                     break;
223*4882a593Smuzhiyun                 case SubPixelUnknown:
224*4882a593Smuzhiyun                     break;
225*4882a593Smuzhiyun                 default:
226*4882a593Smuzhiyun                     subpixel_order = output->subpixel_order;
227*4882a593Smuzhiyun                     break;
228*4882a593Smuzhiyun                 }
229*4882a593Smuzhiyun             }
230*4882a593Smuzhiyun             if (subpixel_order != SubPixelUnknown)
231*4882a593Smuzhiyun                 break;
232*4882a593Smuzhiyun         }
233*4882a593Smuzhiyun         if (subpixel_order != SubPixelUnknown) {
234*4882a593Smuzhiyun             static const int circle[4] = {
235*4882a593Smuzhiyun                 SubPixelHorizontalRGB,
236*4882a593Smuzhiyun                 SubPixelVerticalRGB,
237*4882a593Smuzhiyun                 SubPixelHorizontalBGR,
238*4882a593Smuzhiyun                 SubPixelVerticalBGR,
239*4882a593Smuzhiyun             };
240*4882a593Smuzhiyun             int rotate;
241*4882a593Smuzhiyun             int sc;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun             for (rotate = 0; rotate < 4; rotate++)
244*4882a593Smuzhiyun                 if (crtc->rotation & (1 << rotate))
245*4882a593Smuzhiyun                     break;
246*4882a593Smuzhiyun             for (sc = 0; sc < 4; sc++)
247*4882a593Smuzhiyun                 if (circle[sc] == subpixel_order)
248*4882a593Smuzhiyun                     break;
249*4882a593Smuzhiyun             sc = (sc + rotate) & 0x3;
250*4882a593Smuzhiyun             if ((crtc->rotation & RR_Reflect_X) && !(sc & 1))
251*4882a593Smuzhiyun                 sc ^= 2;
252*4882a593Smuzhiyun             if ((crtc->rotation & RR_Reflect_Y) && (sc & 1))
253*4882a593Smuzhiyun                 sc ^= 2;
254*4882a593Smuzhiyun             subpixel_order = circle[sc];
255*4882a593Smuzhiyun             break;
256*4882a593Smuzhiyun         }
257*4882a593Smuzhiyun     }
258*4882a593Smuzhiyun     if (subpixel_order == SubPixelUnknown && has_none)
259*4882a593Smuzhiyun         subpixel_order = SubPixelNone;
260*4882a593Smuzhiyun     PictureSetSubpixelOrder(pScreen, subpixel_order);
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun /**
264*4882a593Smuzhiyun  * Sets the given video mode on the given crtc
265*4882a593Smuzhiyun  */
266*4882a593Smuzhiyun Bool
xf86CrtcSetModeTransform(xf86CrtcPtr crtc,DisplayModePtr mode,Rotation rotation,RRTransformPtr transform,int x,int y)267*4882a593Smuzhiyun xf86CrtcSetModeTransform(xf86CrtcPtr crtc, DisplayModePtr mode,
268*4882a593Smuzhiyun                          Rotation rotation, RRTransformPtr transform, int x,
269*4882a593Smuzhiyun                          int y)
270*4882a593Smuzhiyun {
271*4882a593Smuzhiyun     ScrnInfoPtr scrn = crtc->scrn;
272*4882a593Smuzhiyun     xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
273*4882a593Smuzhiyun     int i;
274*4882a593Smuzhiyun     Bool ret = FALSE;
275*4882a593Smuzhiyun     Bool didLock = FALSE;
276*4882a593Smuzhiyun     DisplayModePtr adjusted_mode;
277*4882a593Smuzhiyun     DisplayModeRec saved_mode;
278*4882a593Smuzhiyun     int saved_x, saved_y;
279*4882a593Smuzhiyun     Rotation saved_rotation;
280*4882a593Smuzhiyun     RRTransformRec saved_transform;
281*4882a593Smuzhiyun     Bool saved_transform_present;
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun     crtc->enabled = xf86CrtcInUse(crtc) && !xf86CrtcIsLeased(crtc);
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun     /* We only hit this if someone explicitly sends a "disabled" modeset. */
286*4882a593Smuzhiyun     if (!crtc->enabled) {
287*4882a593Smuzhiyun         /* Check everything for stuff that should be off. */
288*4882a593Smuzhiyun         xf86DisableUnusedFunctions(scrn);
289*4882a593Smuzhiyun         return TRUE;
290*4882a593Smuzhiyun     }
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun     adjusted_mode = xf86DuplicateMode(mode);
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun     saved_mode = crtc->mode;
295*4882a593Smuzhiyun     saved_x = crtc->x;
296*4882a593Smuzhiyun     saved_y = crtc->y;
297*4882a593Smuzhiyun     saved_rotation = crtc->rotation;
298*4882a593Smuzhiyun     if (crtc->transformPresent) {
299*4882a593Smuzhiyun         RRTransformInit(&saved_transform);
300*4882a593Smuzhiyun         RRTransformCopy(&saved_transform, &crtc->transform);
301*4882a593Smuzhiyun     }
302*4882a593Smuzhiyun     saved_transform_present = crtc->transformPresent;
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun     /* Update crtc values up front so the driver can rely on them for mode
305*4882a593Smuzhiyun      * setting.
306*4882a593Smuzhiyun      */
307*4882a593Smuzhiyun     crtc->mode = *mode;
308*4882a593Smuzhiyun     crtc->x = x;
309*4882a593Smuzhiyun     crtc->y = y;
310*4882a593Smuzhiyun     crtc->rotation = rotation;
311*4882a593Smuzhiyun     if (transform) {
312*4882a593Smuzhiyun         RRTransformCopy(&crtc->transform, transform);
313*4882a593Smuzhiyun         crtc->transformPresent = TRUE;
314*4882a593Smuzhiyun     }
315*4882a593Smuzhiyun     else
316*4882a593Smuzhiyun         crtc->transformPresent = FALSE;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun     if (crtc->funcs->set_mode_major) {
319*4882a593Smuzhiyun         ret = crtc->funcs->set_mode_major(crtc, mode, rotation, x, y);
320*4882a593Smuzhiyun         goto done;
321*4882a593Smuzhiyun     }
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun     didLock = crtc->funcs->lock(crtc);
324*4882a593Smuzhiyun     /* Pass our mode to the outputs and the CRTC to give them a chance to
325*4882a593Smuzhiyun      * adjust it according to limitations or output properties, and also
326*4882a593Smuzhiyun      * a chance to reject the mode entirely.
327*4882a593Smuzhiyun      */
328*4882a593Smuzhiyun     for (i = 0; i < xf86_config->num_output; i++) {
329*4882a593Smuzhiyun         xf86OutputPtr output = xf86_config->output[i];
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun         if (output->crtc != crtc)
332*4882a593Smuzhiyun             continue;
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun         if (!output->funcs->mode_fixup(output, mode, adjusted_mode)) {
335*4882a593Smuzhiyun             goto done;
336*4882a593Smuzhiyun         }
337*4882a593Smuzhiyun     }
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun     if (!crtc->funcs->mode_fixup(crtc, mode, adjusted_mode)) {
340*4882a593Smuzhiyun         goto done;
341*4882a593Smuzhiyun     }
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun     if (!xf86CrtcRotate(crtc))
344*4882a593Smuzhiyun         goto done;
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun     /* Prepare the outputs and CRTCs before setting the mode. */
347*4882a593Smuzhiyun     for (i = 0; i < xf86_config->num_output; i++) {
348*4882a593Smuzhiyun         xf86OutputPtr output = xf86_config->output[i];
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun         if (output->crtc != crtc)
351*4882a593Smuzhiyun             continue;
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun         /* Disable the output as the first thing we do. */
354*4882a593Smuzhiyun         output->funcs->prepare(output);
355*4882a593Smuzhiyun     }
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun     crtc->funcs->prepare(crtc);
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun     /* Set up the DPLL and any output state that needs to adjust or depend
360*4882a593Smuzhiyun      * on the DPLL.
361*4882a593Smuzhiyun      */
362*4882a593Smuzhiyun     crtc->funcs->mode_set(crtc, mode, adjusted_mode, crtc->x, crtc->y);
363*4882a593Smuzhiyun     for (i = 0; i < xf86_config->num_output; i++) {
364*4882a593Smuzhiyun         xf86OutputPtr output = xf86_config->output[i];
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun         if (output->crtc == crtc)
367*4882a593Smuzhiyun             output->funcs->mode_set(output, mode, adjusted_mode);
368*4882a593Smuzhiyun     }
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun     /* Only upload when needed, to avoid unneeded delays. */
371*4882a593Smuzhiyun     if (!crtc->active && crtc->funcs->gamma_set)
372*4882a593Smuzhiyun         crtc->funcs->gamma_set(crtc, crtc->gamma_red, crtc->gamma_green,
373*4882a593Smuzhiyun                                crtc->gamma_blue, crtc->gamma_size);
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun     /* Now, enable the clocks, plane, pipe, and outputs that we set up. */
376*4882a593Smuzhiyun     crtc->funcs->commit(crtc);
377*4882a593Smuzhiyun     for (i = 0; i < xf86_config->num_output; i++) {
378*4882a593Smuzhiyun         xf86OutputPtr output = xf86_config->output[i];
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun         if (output->crtc == crtc)
381*4882a593Smuzhiyun             output->funcs->commit(output);
382*4882a593Smuzhiyun     }
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun     ret = TRUE;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun  done:
387*4882a593Smuzhiyun     if (ret) {
388*4882a593Smuzhiyun         crtc->active = TRUE;
389*4882a593Smuzhiyun         if (scrn->pScreen)
390*4882a593Smuzhiyun             xf86CrtcSetScreenSubpixelOrder(scrn->pScreen);
391*4882a593Smuzhiyun         if (scrn->ModeSet)
392*4882a593Smuzhiyun             scrn->ModeSet(scrn);
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun         /* Make sure the HW cursor is hidden if it's supposed to be, in case
395*4882a593Smuzhiyun          * it was hidden while the CRTC was disabled
396*4882a593Smuzhiyun          */
397*4882a593Smuzhiyun         if (!xf86_config->cursor_on)
398*4882a593Smuzhiyun             xf86_hide_cursors(scrn);
399*4882a593Smuzhiyun     }
400*4882a593Smuzhiyun     else {
401*4882a593Smuzhiyun         crtc->x = saved_x;
402*4882a593Smuzhiyun         crtc->y = saved_y;
403*4882a593Smuzhiyun         crtc->rotation = saved_rotation;
404*4882a593Smuzhiyun         crtc->mode = saved_mode;
405*4882a593Smuzhiyun         if (saved_transform_present)
406*4882a593Smuzhiyun             RRTransformCopy(&crtc->transform, &saved_transform);
407*4882a593Smuzhiyun         crtc->transformPresent = saved_transform_present;
408*4882a593Smuzhiyun     }
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun     free((void *) adjusted_mode->name);
411*4882a593Smuzhiyun     free(adjusted_mode);
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun     if (didLock)
414*4882a593Smuzhiyun         crtc->funcs->unlock(crtc);
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun     return ret;
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun /**
420*4882a593Smuzhiyun  * Sets the given video mode on the given crtc, but without providing
421*4882a593Smuzhiyun  * a transform
422*4882a593Smuzhiyun  */
423*4882a593Smuzhiyun Bool
xf86CrtcSetMode(xf86CrtcPtr crtc,DisplayModePtr mode,Rotation rotation,int x,int y)424*4882a593Smuzhiyun xf86CrtcSetMode(xf86CrtcPtr crtc, DisplayModePtr mode, Rotation rotation,
425*4882a593Smuzhiyun                 int x, int y)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun     return xf86CrtcSetModeTransform(crtc, mode, rotation, NULL, x, y);
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun /**
431*4882a593Smuzhiyun  * Pans the screen, does not change the mode
432*4882a593Smuzhiyun  */
433*4882a593Smuzhiyun void
xf86CrtcSetOrigin(xf86CrtcPtr crtc,int x,int y)434*4882a593Smuzhiyun xf86CrtcSetOrigin(xf86CrtcPtr crtc, int x, int y)
435*4882a593Smuzhiyun {
436*4882a593Smuzhiyun     ScrnInfoPtr scrn = crtc->scrn;
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun     crtc->x = x;
439*4882a593Smuzhiyun     crtc->y = y;
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun     if (xf86CrtcIsLeased(crtc))
442*4882a593Smuzhiyun         return;
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun     if (crtc->funcs->set_origin) {
445*4882a593Smuzhiyun         if (!xf86CrtcRotate(crtc))
446*4882a593Smuzhiyun             return;
447*4882a593Smuzhiyun         crtc->funcs->set_origin(crtc, x, y);
448*4882a593Smuzhiyun         if (scrn->ModeSet)
449*4882a593Smuzhiyun             scrn->ModeSet(scrn);
450*4882a593Smuzhiyun     }
451*4882a593Smuzhiyun     else
452*4882a593Smuzhiyun         xf86CrtcSetMode(crtc, &crtc->mode, crtc->rotation, x, y);
453*4882a593Smuzhiyun }
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun /*
456*4882a593Smuzhiyun  * Output functions
457*4882a593Smuzhiyun  */
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun extern XF86ConfigPtr xf86configptr;
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun typedef enum {
462*4882a593Smuzhiyun     OPTION_PREFERRED_MODE,
463*4882a593Smuzhiyun     OPTION_ZOOM_MODES,
464*4882a593Smuzhiyun     OPTION_POSITION,
465*4882a593Smuzhiyun     OPTION_BELOW,
466*4882a593Smuzhiyun     OPTION_RIGHT_OF,
467*4882a593Smuzhiyun     OPTION_ABOVE,
468*4882a593Smuzhiyun     OPTION_LEFT_OF,
469*4882a593Smuzhiyun     OPTION_ENABLE,
470*4882a593Smuzhiyun     OPTION_DISABLE,
471*4882a593Smuzhiyun     OPTION_MIN_CLOCK,
472*4882a593Smuzhiyun     OPTION_MAX_CLOCK,
473*4882a593Smuzhiyun     OPTION_IGNORE,
474*4882a593Smuzhiyun     OPTION_ROTATE,
475*4882a593Smuzhiyun     OPTION_PANNING,
476*4882a593Smuzhiyun     OPTION_PRIMARY,
477*4882a593Smuzhiyun     OPTION_DEFAULT_MODES,
478*4882a593Smuzhiyun } OutputOpts;
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun static OptionInfoRec xf86OutputOptions[] = {
481*4882a593Smuzhiyun     {OPTION_PREFERRED_MODE, "PreferredMode", OPTV_STRING, {0}, FALSE},
482*4882a593Smuzhiyun     {OPTION_ZOOM_MODES, "ZoomModes", OPTV_STRING, {0}, FALSE },
483*4882a593Smuzhiyun     {OPTION_POSITION, "Position", OPTV_STRING, {0}, FALSE},
484*4882a593Smuzhiyun     {OPTION_BELOW, "Below", OPTV_STRING, {0}, FALSE},
485*4882a593Smuzhiyun     {OPTION_RIGHT_OF, "RightOf", OPTV_STRING, {0}, FALSE},
486*4882a593Smuzhiyun     {OPTION_ABOVE, "Above", OPTV_STRING, {0}, FALSE},
487*4882a593Smuzhiyun     {OPTION_LEFT_OF, "LeftOf", OPTV_STRING, {0}, FALSE},
488*4882a593Smuzhiyun     {OPTION_ENABLE, "Enable", OPTV_BOOLEAN, {0}, FALSE},
489*4882a593Smuzhiyun     {OPTION_DISABLE, "Disable", OPTV_BOOLEAN, {0}, FALSE},
490*4882a593Smuzhiyun     {OPTION_MIN_CLOCK, "MinClock", OPTV_FREQ, {0}, FALSE},
491*4882a593Smuzhiyun     {OPTION_MAX_CLOCK, "MaxClock", OPTV_FREQ, {0}, FALSE},
492*4882a593Smuzhiyun     {OPTION_IGNORE, "Ignore", OPTV_BOOLEAN, {0}, FALSE},
493*4882a593Smuzhiyun     {OPTION_ROTATE, "Rotate", OPTV_STRING, {0}, FALSE},
494*4882a593Smuzhiyun     {OPTION_PANNING, "Panning", OPTV_STRING, {0}, FALSE},
495*4882a593Smuzhiyun     {OPTION_PRIMARY, "Primary", OPTV_BOOLEAN, {0}, FALSE},
496*4882a593Smuzhiyun     {OPTION_DEFAULT_MODES, "DefaultModes", OPTV_BOOLEAN, {0}, FALSE},
497*4882a593Smuzhiyun     {-1, NULL, OPTV_NONE, {0}, FALSE},
498*4882a593Smuzhiyun };
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun enum {
501*4882a593Smuzhiyun     OPTION_MODEDEBUG,
502*4882a593Smuzhiyun     OPTION_PREFER_CLONEMODE,
503*4882a593Smuzhiyun };
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun static OptionInfoRec xf86DeviceOptions[] = {
506*4882a593Smuzhiyun     {OPTION_MODEDEBUG, "ModeDebug", OPTV_BOOLEAN, {0}, FALSE},
507*4882a593Smuzhiyun     {OPTION_PREFER_CLONEMODE, "PreferCloneMode", OPTV_BOOLEAN, {0}, FALSE},
508*4882a593Smuzhiyun     {-1, NULL, OPTV_NONE, {0}, FALSE},
509*4882a593Smuzhiyun };
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun static void
xf86OutputSetMonitor(xf86OutputPtr output)512*4882a593Smuzhiyun xf86OutputSetMonitor(xf86OutputPtr output)
513*4882a593Smuzhiyun {
514*4882a593Smuzhiyun     char *option_name;
515*4882a593Smuzhiyun     const char *monitor;
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun     if (!output->name)
518*4882a593Smuzhiyun         return;
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun     free(output->options);
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun     output->options = xnfalloc(sizeof(xf86OutputOptions));
523*4882a593Smuzhiyun     memcpy(output->options, xf86OutputOptions, sizeof(xf86OutputOptions));
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun     XNFasprintf(&option_name, "monitor-%s", output->name);
526*4882a593Smuzhiyun     monitor = xf86findOptionValue(output->scrn->options, option_name);
527*4882a593Smuzhiyun     if (!monitor)
528*4882a593Smuzhiyun         monitor = output->name;
529*4882a593Smuzhiyun     else
530*4882a593Smuzhiyun         xf86MarkOptionUsedByName(output->scrn->options, option_name);
531*4882a593Smuzhiyun     free(option_name);
532*4882a593Smuzhiyun     output->conf_monitor = xf86findMonitor(monitor,
533*4882a593Smuzhiyun                                            xf86configptr->conf_monitor_lst);
534*4882a593Smuzhiyun     /*
535*4882a593Smuzhiyun      * Find the monitor section of the screen and use that
536*4882a593Smuzhiyun      */
537*4882a593Smuzhiyun     if (!output->conf_monitor && output->use_screen_monitor)
538*4882a593Smuzhiyun         output->conf_monitor = xf86findMonitor(output->scrn->monitor->id,
539*4882a593Smuzhiyun                                                xf86configptr->conf_monitor_lst);
540*4882a593Smuzhiyun     if (output->conf_monitor) {
541*4882a593Smuzhiyun         xf86DrvMsg(output->scrn->scrnIndex, X_INFO,
542*4882a593Smuzhiyun                    "Output %s using monitor section %s\n",
543*4882a593Smuzhiyun                    output->name, output->conf_monitor->mon_identifier);
544*4882a593Smuzhiyun         xf86ProcessOptions(output->scrn->scrnIndex,
545*4882a593Smuzhiyun                            output->conf_monitor->mon_option_lst,
546*4882a593Smuzhiyun                            output->options);
547*4882a593Smuzhiyun     }
548*4882a593Smuzhiyun     else
549*4882a593Smuzhiyun         xf86DrvMsg(output->scrn->scrnIndex, X_INFO,
550*4882a593Smuzhiyun                    "Output %s has no monitor section\n", output->name);
551*4882a593Smuzhiyun }
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun static Bool
xf86OutputEnabled(xf86OutputPtr output,Bool strict)554*4882a593Smuzhiyun xf86OutputEnabled(xf86OutputPtr output, Bool strict)
555*4882a593Smuzhiyun {
556*4882a593Smuzhiyun     Bool enable, disable;
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun     /* check to see if this output was enabled in the config file */
559*4882a593Smuzhiyun     if (xf86GetOptValBool(output->options, OPTION_ENABLE, &enable) && enable) {
560*4882a593Smuzhiyun         xf86DrvMsg(output->scrn->scrnIndex, X_INFO,
561*4882a593Smuzhiyun                    "Output %s enabled by config file\n", output->name);
562*4882a593Smuzhiyun         return TRUE;
563*4882a593Smuzhiyun     }
564*4882a593Smuzhiyun     /* or if this output was disabled in the config file */
565*4882a593Smuzhiyun     if (xf86GetOptValBool(output->options, OPTION_DISABLE, &disable) && disable) {
566*4882a593Smuzhiyun         xf86DrvMsg(output->scrn->scrnIndex, X_INFO,
567*4882a593Smuzhiyun                    "Output %s disabled by config file\n", output->name);
568*4882a593Smuzhiyun         return FALSE;
569*4882a593Smuzhiyun     }
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun     /* If not, try to only light up the ones we know are connected which are supposed to be on the desktop */
572*4882a593Smuzhiyun     if (strict) {
573*4882a593Smuzhiyun         enable = output->status == XF86OutputStatusConnected && !output->non_desktop;
574*4882a593Smuzhiyun     }
575*4882a593Smuzhiyun     /* But if that fails, try to light up even outputs we're unsure of */
576*4882a593Smuzhiyun     else {
577*4882a593Smuzhiyun         enable = output->status != XF86OutputStatusDisconnected;
578*4882a593Smuzhiyun     }
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun     xf86DrvMsg(output->scrn->scrnIndex, X_INFO,
581*4882a593Smuzhiyun                "Output %s %sconnected\n", output->name, enable ? "" : "dis");
582*4882a593Smuzhiyun     return enable;
583*4882a593Smuzhiyun }
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun static Bool
xf86OutputIgnored(xf86OutputPtr output)586*4882a593Smuzhiyun xf86OutputIgnored(xf86OutputPtr output)
587*4882a593Smuzhiyun {
588*4882a593Smuzhiyun     return xf86ReturnOptValBool(output->options, OPTION_IGNORE, FALSE);
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun static const char *direction[4] = {
592*4882a593Smuzhiyun     "normal",
593*4882a593Smuzhiyun     "left",
594*4882a593Smuzhiyun     "inverted",
595*4882a593Smuzhiyun     "right"
596*4882a593Smuzhiyun };
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun static Rotation
xf86OutputInitialRotation(xf86OutputPtr output)599*4882a593Smuzhiyun xf86OutputInitialRotation(xf86OutputPtr output)
600*4882a593Smuzhiyun {
601*4882a593Smuzhiyun     const char *rotate_name = xf86GetOptValString(output->options,
602*4882a593Smuzhiyun                                                   OPTION_ROTATE);
603*4882a593Smuzhiyun     int i;
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun     if (!rotate_name) {
606*4882a593Smuzhiyun         if (output->initial_rotation)
607*4882a593Smuzhiyun             return output->initial_rotation;
608*4882a593Smuzhiyun         return RR_Rotate_0;
609*4882a593Smuzhiyun     }
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun     for (i = 0; i < 4; i++)
612*4882a593Smuzhiyun         if (xf86nameCompare(direction[i], rotate_name) == 0)
613*4882a593Smuzhiyun             return 1 << i;
614*4882a593Smuzhiyun     return RR_Rotate_0;
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun xf86OutputPtr
xf86OutputCreate(ScrnInfoPtr scrn,const xf86OutputFuncsRec * funcs,const char * name)618*4882a593Smuzhiyun xf86OutputCreate(ScrnInfoPtr scrn,
619*4882a593Smuzhiyun                  const xf86OutputFuncsRec * funcs, const char *name)
620*4882a593Smuzhiyun {
621*4882a593Smuzhiyun     xf86OutputPtr output, *outputs;
622*4882a593Smuzhiyun     xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
623*4882a593Smuzhiyun     int len;
624*4882a593Smuzhiyun     Bool primary;
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun     if (name)
627*4882a593Smuzhiyun         len = strlen(name) + 1;
628*4882a593Smuzhiyun     else
629*4882a593Smuzhiyun         len = 0;
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun     output = calloc(sizeof(xf86OutputRec) + len, 1);
632*4882a593Smuzhiyun     if (!output)
633*4882a593Smuzhiyun         return NULL;
634*4882a593Smuzhiyun     output->scrn = scrn;
635*4882a593Smuzhiyun     output->funcs = funcs;
636*4882a593Smuzhiyun     if (name) {
637*4882a593Smuzhiyun         output->name = (char *) (output + 1);
638*4882a593Smuzhiyun         strcpy(output->name, name);
639*4882a593Smuzhiyun     }
640*4882a593Smuzhiyun     output->subpixel_order = SubPixelUnknown;
641*4882a593Smuzhiyun     /*
642*4882a593Smuzhiyun      * Use the old per-screen monitor section for the first output
643*4882a593Smuzhiyun      */
644*4882a593Smuzhiyun     output->use_screen_monitor = (xf86_config->num_output == 0);
645*4882a593Smuzhiyun #ifdef RANDR_12_INTERFACE
646*4882a593Smuzhiyun     output->randr_output = NULL;
647*4882a593Smuzhiyun #endif
648*4882a593Smuzhiyun     if (name) {
649*4882a593Smuzhiyun         xf86OutputSetMonitor(output);
650*4882a593Smuzhiyun         if (xf86OutputIgnored(output)) {
651*4882a593Smuzhiyun             free(output);
652*4882a593Smuzhiyun             return FALSE;
653*4882a593Smuzhiyun         }
654*4882a593Smuzhiyun     }
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun     if (xf86_config->output)
657*4882a593Smuzhiyun         outputs = reallocarray(xf86_config->output,
658*4882a593Smuzhiyun                                xf86_config->num_output + 1,
659*4882a593Smuzhiyun                                sizeof(xf86OutputPtr));
660*4882a593Smuzhiyun     else
661*4882a593Smuzhiyun         outputs = xallocarray(xf86_config->num_output + 1,
662*4882a593Smuzhiyun                               sizeof(xf86OutputPtr));
663*4882a593Smuzhiyun     if (!outputs) {
664*4882a593Smuzhiyun         free(output);
665*4882a593Smuzhiyun         return NULL;
666*4882a593Smuzhiyun     }
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun     xf86_config->output = outputs;
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun     if (xf86GetOptValBool(output->options, OPTION_PRIMARY, &primary) && primary) {
671*4882a593Smuzhiyun         memmove(xf86_config->output + 1, xf86_config->output,
672*4882a593Smuzhiyun                 xf86_config->num_output * sizeof(xf86OutputPtr));
673*4882a593Smuzhiyun         xf86_config->output[0] = output;
674*4882a593Smuzhiyun     }
675*4882a593Smuzhiyun     else {
676*4882a593Smuzhiyun         xf86_config->output[xf86_config->num_output] = output;
677*4882a593Smuzhiyun     }
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun     xf86_config->num_output++;
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun     return output;
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun Bool
xf86OutputRename(xf86OutputPtr output,const char * name)685*4882a593Smuzhiyun xf86OutputRename(xf86OutputPtr output, const char *name)
686*4882a593Smuzhiyun {
687*4882a593Smuzhiyun     char *newname = strdup(name);
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun     if (!newname)
690*4882a593Smuzhiyun         return FALSE;           /* so sorry... */
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun     if (output->name && output->name != (char *) (output + 1))
693*4882a593Smuzhiyun         free(output->name);
694*4882a593Smuzhiyun     output->name = newname;
695*4882a593Smuzhiyun     xf86OutputSetMonitor(output);
696*4882a593Smuzhiyun     if (xf86OutputIgnored(output))
697*4882a593Smuzhiyun         return FALSE;
698*4882a593Smuzhiyun     return TRUE;
699*4882a593Smuzhiyun }
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun void
xf86OutputUseScreenMonitor(xf86OutputPtr output,Bool use_screen_monitor)702*4882a593Smuzhiyun xf86OutputUseScreenMonitor(xf86OutputPtr output, Bool use_screen_monitor)
703*4882a593Smuzhiyun {
704*4882a593Smuzhiyun     if (use_screen_monitor != output->use_screen_monitor) {
705*4882a593Smuzhiyun         output->use_screen_monitor = use_screen_monitor;
706*4882a593Smuzhiyun         xf86OutputSetMonitor(output);
707*4882a593Smuzhiyun     }
708*4882a593Smuzhiyun }
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun void
xf86OutputDestroy(xf86OutputPtr output)711*4882a593Smuzhiyun xf86OutputDestroy(xf86OutputPtr output)
712*4882a593Smuzhiyun {
713*4882a593Smuzhiyun     ScrnInfoPtr scrn = output->scrn;
714*4882a593Smuzhiyun     xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
715*4882a593Smuzhiyun     int o;
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun     (*output->funcs->destroy) (output);
718*4882a593Smuzhiyun     while (output->probed_modes)
719*4882a593Smuzhiyun         xf86DeleteMode(&output->probed_modes, output->probed_modes);
720*4882a593Smuzhiyun     for (o = 0; o < xf86_config->num_output; o++)
721*4882a593Smuzhiyun         if (xf86_config->output[o] == output) {
722*4882a593Smuzhiyun             memmove(&xf86_config->output[o],
723*4882a593Smuzhiyun                     &xf86_config->output[o + 1],
724*4882a593Smuzhiyun                     ((xf86_config->num_output - (o + 1)) * sizeof(void *)));
725*4882a593Smuzhiyun             xf86_config->num_output--;
726*4882a593Smuzhiyun             break;
727*4882a593Smuzhiyun         }
728*4882a593Smuzhiyun     if (output->name && output->name != (char *) (output + 1))
729*4882a593Smuzhiyun         free(output->name);
730*4882a593Smuzhiyun     free(output);
731*4882a593Smuzhiyun }
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun /*
734*4882a593Smuzhiyun  * Called during CreateScreenResources to hook up RandR
735*4882a593Smuzhiyun  */
736*4882a593Smuzhiyun static Bool
xf86CrtcCreateScreenResources(ScreenPtr screen)737*4882a593Smuzhiyun xf86CrtcCreateScreenResources(ScreenPtr screen)
738*4882a593Smuzhiyun {
739*4882a593Smuzhiyun     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
740*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun     screen->CreateScreenResources = config->CreateScreenResources;
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun     if (!(*screen->CreateScreenResources) (screen))
745*4882a593Smuzhiyun         return FALSE;
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun     if (!xf86RandR12CreateScreenResources(screen))
748*4882a593Smuzhiyun         return FALSE;
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun     return TRUE;
751*4882a593Smuzhiyun }
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun /*
754*4882a593Smuzhiyun  * Clean up config on server reset
755*4882a593Smuzhiyun  */
756*4882a593Smuzhiyun static Bool
xf86CrtcCloseScreen(ScreenPtr screen)757*4882a593Smuzhiyun xf86CrtcCloseScreen(ScreenPtr screen)
758*4882a593Smuzhiyun {
759*4882a593Smuzhiyun     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
760*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
761*4882a593Smuzhiyun     int o, c;
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun     /* The randr_output and randr_crtc pointers are already invalid as
764*4882a593Smuzhiyun      * the DIX resources were freed when the associated resources were
765*4882a593Smuzhiyun      * freed. Clear them now; referencing through them during the rest
766*4882a593Smuzhiyun      * of the CloseScreen sequence will not end well.
767*4882a593Smuzhiyun      */
768*4882a593Smuzhiyun     for (o = 0; o < config->num_output; o++) {
769*4882a593Smuzhiyun         xf86OutputPtr output = config->output[o];
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun         output->randr_output = NULL;
772*4882a593Smuzhiyun     }
773*4882a593Smuzhiyun     for (c = 0; c < config->num_crtc; c++) {
774*4882a593Smuzhiyun         xf86CrtcPtr crtc = config->crtc[c];
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun         crtc->randr_crtc = NULL;
777*4882a593Smuzhiyun     }
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun     screen->CloseScreen = config->CloseScreen;
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun     xf86RotateCloseScreen(screen);
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun     xf86RandR12CloseScreen(screen);
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun     screen->CloseScreen(screen);
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun     /* detach any providers */
788*4882a593Smuzhiyun     if (config->randr_provider) {
789*4882a593Smuzhiyun         RRProviderDestroy(config->randr_provider);
790*4882a593Smuzhiyun         config->randr_provider = NULL;
791*4882a593Smuzhiyun     }
792*4882a593Smuzhiyun     return TRUE;
793*4882a593Smuzhiyun }
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun /*
796*4882a593Smuzhiyun  * Called at ScreenInit time to set up
797*4882a593Smuzhiyun  */
798*4882a593Smuzhiyun #ifdef RANDR_13_INTERFACE
799*4882a593Smuzhiyun int
800*4882a593Smuzhiyun #else
801*4882a593Smuzhiyun Bool
802*4882a593Smuzhiyun #endif
xf86CrtcScreenInit(ScreenPtr screen)803*4882a593Smuzhiyun xf86CrtcScreenInit(ScreenPtr screen)
804*4882a593Smuzhiyun {
805*4882a593Smuzhiyun     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
806*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
807*4882a593Smuzhiyun     int c;
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun     /* Rotation */
810*4882a593Smuzhiyun     xf86RandR12Init(screen);
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun     /* support all rotations if every crtc has the shadow alloc funcs */
813*4882a593Smuzhiyun     for (c = 0; c < config->num_crtc; c++) {
814*4882a593Smuzhiyun         xf86CrtcPtr crtc = config->crtc[c];
815*4882a593Smuzhiyun 
816*4882a593Smuzhiyun         if (!crtc->funcs->shadow_allocate || !crtc->funcs->shadow_create)
817*4882a593Smuzhiyun             break;
818*4882a593Smuzhiyun     }
819*4882a593Smuzhiyun     if (c == config->num_crtc) {
820*4882a593Smuzhiyun         xf86RandR12SetRotations(screen, RR_Rotate_0 | RR_Rotate_90 |
821*4882a593Smuzhiyun                                 RR_Rotate_180 | RR_Rotate_270 |
822*4882a593Smuzhiyun                                 RR_Reflect_X | RR_Reflect_Y);
823*4882a593Smuzhiyun         xf86RandR12SetTransformSupport(screen, TRUE);
824*4882a593Smuzhiyun     }
825*4882a593Smuzhiyun     else {
826*4882a593Smuzhiyun         xf86RandR12SetRotations(screen, RR_Rotate_0);
827*4882a593Smuzhiyun         xf86RandR12SetTransformSupport(screen, FALSE);
828*4882a593Smuzhiyun     }
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun     /* Wrap CreateScreenResources so we can initialize the RandR code */
831*4882a593Smuzhiyun     config->CreateScreenResources = screen->CreateScreenResources;
832*4882a593Smuzhiyun     screen->CreateScreenResources = xf86CrtcCreateScreenResources;
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun     config->CloseScreen = screen->CloseScreen;
835*4882a593Smuzhiyun     screen->CloseScreen = xf86CrtcCloseScreen;
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun     /* This might still be marked wrapped from a previous generation */
838*4882a593Smuzhiyun     config->BlockHandler = NULL;
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun #ifdef XFreeXDGA
841*4882a593Smuzhiyun     _xf86_di_dga_init_internal(screen);
842*4882a593Smuzhiyun #endif
843*4882a593Smuzhiyun #ifdef RANDR_13_INTERFACE
844*4882a593Smuzhiyun     return RANDR_INTERFACE_VERSION;
845*4882a593Smuzhiyun #else
846*4882a593Smuzhiyun     return TRUE;
847*4882a593Smuzhiyun #endif
848*4882a593Smuzhiyun }
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun static DisplayModePtr
xf86DefaultMode(xf86OutputPtr output,int width,int height)851*4882a593Smuzhiyun xf86DefaultMode(xf86OutputPtr output, int width, int height)
852*4882a593Smuzhiyun {
853*4882a593Smuzhiyun     DisplayModePtr target_mode = NULL;
854*4882a593Smuzhiyun     DisplayModePtr mode;
855*4882a593Smuzhiyun     int target_diff = 0;
856*4882a593Smuzhiyun     int target_preferred = 0;
857*4882a593Smuzhiyun     int mm_height;
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun     mm_height = output->mm_height;
860*4882a593Smuzhiyun     if (!mm_height)
861*4882a593Smuzhiyun         mm_height = (768 * 25.4) / DEFAULT_DPI;
862*4882a593Smuzhiyun     /*
863*4882a593Smuzhiyun      * Pick a mode closest to DEFAULT_DPI
864*4882a593Smuzhiyun      */
865*4882a593Smuzhiyun     for (mode = output->probed_modes; mode; mode = mode->next) {
866*4882a593Smuzhiyun         int dpi;
867*4882a593Smuzhiyun         int preferred = (((mode->type & M_T_PREFERRED) != 0) +
868*4882a593Smuzhiyun                          ((mode->type & M_T_USERPREF) != 0));
869*4882a593Smuzhiyun         int diff;
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun         if (xf86ModeWidth(mode, output->initial_rotation) > width ||
872*4882a593Smuzhiyun             xf86ModeHeight(mode, output->initial_rotation) > height)
873*4882a593Smuzhiyun             continue;
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun         /* yes, use VDisplay here, not xf86ModeHeight */
876*4882a593Smuzhiyun         dpi = (mode->VDisplay * 254) / (mm_height * 10);
877*4882a593Smuzhiyun         diff = dpi - DEFAULT_DPI;
878*4882a593Smuzhiyun         diff = diff < 0 ? -diff : diff;
879*4882a593Smuzhiyun         if (target_mode == NULL || (preferred > target_preferred) ||
880*4882a593Smuzhiyun             (preferred == target_preferred && diff < target_diff)) {
881*4882a593Smuzhiyun             target_mode = mode;
882*4882a593Smuzhiyun             target_diff = diff;
883*4882a593Smuzhiyun             target_preferred = preferred;
884*4882a593Smuzhiyun         }
885*4882a593Smuzhiyun     }
886*4882a593Smuzhiyun     return target_mode;
887*4882a593Smuzhiyun }
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun static DisplayModePtr
xf86ClosestMode(xf86OutputPtr output,DisplayModePtr match,Rotation match_rotation,int width,int height)890*4882a593Smuzhiyun xf86ClosestMode(xf86OutputPtr output,
891*4882a593Smuzhiyun                 DisplayModePtr match, Rotation match_rotation,
892*4882a593Smuzhiyun                 int width, int height)
893*4882a593Smuzhiyun {
894*4882a593Smuzhiyun     DisplayModePtr target_mode = NULL;
895*4882a593Smuzhiyun     DisplayModePtr mode;
896*4882a593Smuzhiyun     int target_diff = 0;
897*4882a593Smuzhiyun 
898*4882a593Smuzhiyun     /*
899*4882a593Smuzhiyun      * Pick a mode closest to the specified mode
900*4882a593Smuzhiyun      */
901*4882a593Smuzhiyun     for (mode = output->probed_modes; mode; mode = mode->next) {
902*4882a593Smuzhiyun         int dx, dy;
903*4882a593Smuzhiyun         int diff;
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun         if (xf86ModeWidth(mode, output->initial_rotation) > width ||
906*4882a593Smuzhiyun             xf86ModeHeight(mode, output->initial_rotation) > height)
907*4882a593Smuzhiyun             continue;
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun         /* exact matches are preferred */
910*4882a593Smuzhiyun         if (output->initial_rotation == match_rotation &&
911*4882a593Smuzhiyun             xf86ModesEqual(mode, match))
912*4882a593Smuzhiyun             return mode;
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun         dx = xf86ModeWidth(match, match_rotation) - xf86ModeWidth(mode,
915*4882a593Smuzhiyun                                                                   output->
916*4882a593Smuzhiyun                                                                   initial_rotation);
917*4882a593Smuzhiyun         dy = xf86ModeHeight(match, match_rotation) - xf86ModeHeight(mode,
918*4882a593Smuzhiyun                                                                     output->
919*4882a593Smuzhiyun                                                                     initial_rotation);
920*4882a593Smuzhiyun         diff = dx * dx + dy * dy;
921*4882a593Smuzhiyun         if (target_mode == NULL || diff < target_diff) {
922*4882a593Smuzhiyun             target_mode = mode;
923*4882a593Smuzhiyun             target_diff = diff;
924*4882a593Smuzhiyun         }
925*4882a593Smuzhiyun     }
926*4882a593Smuzhiyun     return target_mode;
927*4882a593Smuzhiyun }
928*4882a593Smuzhiyun 
929*4882a593Smuzhiyun static DisplayModePtr
xf86OutputHasPreferredMode(xf86OutputPtr output,int width,int height)930*4882a593Smuzhiyun xf86OutputHasPreferredMode(xf86OutputPtr output, int width, int height)
931*4882a593Smuzhiyun {
932*4882a593Smuzhiyun     DisplayModePtr mode;
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun     for (mode = output->probed_modes; mode; mode = mode->next) {
935*4882a593Smuzhiyun         if (xf86ModeWidth(mode, output->initial_rotation) > width ||
936*4882a593Smuzhiyun             xf86ModeHeight(mode, output->initial_rotation) > height)
937*4882a593Smuzhiyun             continue;
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun         if (mode->type & M_T_PREFERRED)
940*4882a593Smuzhiyun             return mode;
941*4882a593Smuzhiyun     }
942*4882a593Smuzhiyun     return NULL;
943*4882a593Smuzhiyun }
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun static DisplayModePtr
xf86OutputHasUserPreferredMode(xf86OutputPtr output)946*4882a593Smuzhiyun xf86OutputHasUserPreferredMode(xf86OutputPtr output)
947*4882a593Smuzhiyun {
948*4882a593Smuzhiyun     DisplayModePtr mode, first = output->probed_modes;
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun     for (mode = first; mode && mode->next != first; mode = mode->next)
951*4882a593Smuzhiyun         if (mode->type & M_T_USERPREF)
952*4882a593Smuzhiyun             return mode;
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun     return NULL;
955*4882a593Smuzhiyun }
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun static int
xf86PickCrtcs(ScrnInfoPtr scrn,xf86CrtcPtr * best_crtcs,DisplayModePtr * modes,int n,int width,int height)958*4882a593Smuzhiyun xf86PickCrtcs(ScrnInfoPtr scrn,
959*4882a593Smuzhiyun               xf86CrtcPtr * best_crtcs,
960*4882a593Smuzhiyun               DisplayModePtr * modes, int n, int width, int height)
961*4882a593Smuzhiyun {
962*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
963*4882a593Smuzhiyun     int c, o;
964*4882a593Smuzhiyun     xf86OutputPtr output;
965*4882a593Smuzhiyun     xf86CrtcPtr crtc;
966*4882a593Smuzhiyun     xf86CrtcPtr *crtcs;
967*4882a593Smuzhiyun     int best_score;
968*4882a593Smuzhiyun     int score;
969*4882a593Smuzhiyun     int my_score;
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun     if (n == config->num_output)
972*4882a593Smuzhiyun         return 0;
973*4882a593Smuzhiyun     output = config->output[n];
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun     /*
976*4882a593Smuzhiyun      * Compute score with this output disabled
977*4882a593Smuzhiyun      */
978*4882a593Smuzhiyun     best_crtcs[n] = NULL;
979*4882a593Smuzhiyun     best_score = xf86PickCrtcs(scrn, best_crtcs, modes, n + 1, width, height);
980*4882a593Smuzhiyun     if (modes[n] == NULL)
981*4882a593Smuzhiyun         return best_score;
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun     crtcs = xallocarray(config->num_output, sizeof(xf86CrtcPtr));
984*4882a593Smuzhiyun     if (!crtcs)
985*4882a593Smuzhiyun         return best_score;
986*4882a593Smuzhiyun 
987*4882a593Smuzhiyun     my_score = 1;
988*4882a593Smuzhiyun     /* Score outputs that are known to be connected higher */
989*4882a593Smuzhiyun     if (output->status == XF86OutputStatusConnected)
990*4882a593Smuzhiyun         my_score++;
991*4882a593Smuzhiyun     /* Score outputs with preferred modes higher */
992*4882a593Smuzhiyun     if (xf86OutputHasPreferredMode(output, width, height))
993*4882a593Smuzhiyun         my_score++;
994*4882a593Smuzhiyun     /*
995*4882a593Smuzhiyun      * Select a crtc for this output and
996*4882a593Smuzhiyun      * then attempt to configure the remaining
997*4882a593Smuzhiyun      * outputs
998*4882a593Smuzhiyun      */
999*4882a593Smuzhiyun     for (c = 0; c < config->num_crtc; c++) {
1000*4882a593Smuzhiyun         if ((output->possible_crtcs & (1 << c)) == 0)
1001*4882a593Smuzhiyun             continue;
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun         crtc = config->crtc[c];
1004*4882a593Smuzhiyun         /*
1005*4882a593Smuzhiyun          * Check to see if some other output is
1006*4882a593Smuzhiyun          * using this crtc
1007*4882a593Smuzhiyun          */
1008*4882a593Smuzhiyun         for (o = 0; o < n; o++)
1009*4882a593Smuzhiyun             if (best_crtcs[o] == crtc)
1010*4882a593Smuzhiyun                 break;
1011*4882a593Smuzhiyun         if (o < n) {
1012*4882a593Smuzhiyun             /*
1013*4882a593Smuzhiyun              * If the two outputs desire the same mode,
1014*4882a593Smuzhiyun              * see if they can be cloned
1015*4882a593Smuzhiyun              */
1016*4882a593Smuzhiyun             if (xf86ModesEqual(modes[o], modes[n]) &&
1017*4882a593Smuzhiyun                 config->output[o]->initial_rotation ==
1018*4882a593Smuzhiyun                 config->output[n]->initial_rotation &&
1019*4882a593Smuzhiyun                 config->output[o]->initial_x == config->output[n]->initial_x &&
1020*4882a593Smuzhiyun                 config->output[o]->initial_y == config->output[n]->initial_y) {
1021*4882a593Smuzhiyun                 if ((output->possible_clones & (1 << o)) == 0)
1022*4882a593Smuzhiyun                     continue;   /* nope, try next CRTC */
1023*4882a593Smuzhiyun             }
1024*4882a593Smuzhiyun             else
1025*4882a593Smuzhiyun                 continue;       /* different modes, can't clone */
1026*4882a593Smuzhiyun         }
1027*4882a593Smuzhiyun         crtcs[n] = crtc;
1028*4882a593Smuzhiyun         memcpy(crtcs, best_crtcs, n * sizeof(xf86CrtcPtr));
1029*4882a593Smuzhiyun         score =
1030*4882a593Smuzhiyun             my_score + xf86PickCrtcs(scrn, crtcs, modes, n + 1, width, height);
1031*4882a593Smuzhiyun         if (score > best_score) {
1032*4882a593Smuzhiyun             best_score = score;
1033*4882a593Smuzhiyun             memcpy(best_crtcs, crtcs, config->num_output * sizeof(xf86CrtcPtr));
1034*4882a593Smuzhiyun         }
1035*4882a593Smuzhiyun     }
1036*4882a593Smuzhiyun     free(crtcs);
1037*4882a593Smuzhiyun     return best_score;
1038*4882a593Smuzhiyun }
1039*4882a593Smuzhiyun 
1040*4882a593Smuzhiyun /*
1041*4882a593Smuzhiyun  * Compute the virtual size necessary to place all of the available
1042*4882a593Smuzhiyun  * crtcs in the specified configuration.
1043*4882a593Smuzhiyun  *
1044*4882a593Smuzhiyun  * canGrow indicates that the driver can make the screen larger than its initial
1045*4882a593Smuzhiyun  * configuration.  If FALSE, this function will enlarge the screen to include
1046*4882a593Smuzhiyun  * the largest available mode.
1047*4882a593Smuzhiyun  */
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun static void
xf86DefaultScreenLimits(ScrnInfoPtr scrn,int * widthp,int * heightp,Bool canGrow)1050*4882a593Smuzhiyun xf86DefaultScreenLimits(ScrnInfoPtr scrn, int *widthp, int *heightp,
1051*4882a593Smuzhiyun                         Bool canGrow)
1052*4882a593Smuzhiyun {
1053*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
1054*4882a593Smuzhiyun     int width = 0, height = 0;
1055*4882a593Smuzhiyun     int o;
1056*4882a593Smuzhiyun     int c;
1057*4882a593Smuzhiyun     int s;
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun     for (c = 0; c < config->num_crtc; c++) {
1060*4882a593Smuzhiyun         int crtc_width = 0, crtc_height = 0;
1061*4882a593Smuzhiyun         xf86CrtcPtr crtc = config->crtc[c];
1062*4882a593Smuzhiyun 
1063*4882a593Smuzhiyun         if (crtc->enabled) {
1064*4882a593Smuzhiyun             crtc_width =
1065*4882a593Smuzhiyun                 crtc->desiredX + xf86ModeWidth(&crtc->desiredMode,
1066*4882a593Smuzhiyun                                                crtc->desiredRotation);
1067*4882a593Smuzhiyun             crtc_height =
1068*4882a593Smuzhiyun                 crtc->desiredY + xf86ModeHeight(&crtc->desiredMode,
1069*4882a593Smuzhiyun                                                 crtc->desiredRotation);
1070*4882a593Smuzhiyun         }
1071*4882a593Smuzhiyun         if (!canGrow) {
1072*4882a593Smuzhiyun             for (o = 0; o < config->num_output; o++) {
1073*4882a593Smuzhiyun                 xf86OutputPtr output = config->output[o];
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun                 for (s = 0; s < config->num_crtc; s++)
1076*4882a593Smuzhiyun                     if (output->possible_crtcs & (1 << s)) {
1077*4882a593Smuzhiyun                         DisplayModePtr mode;
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun                         for (mode = output->probed_modes; mode;
1080*4882a593Smuzhiyun                              mode = mode->next) {
1081*4882a593Smuzhiyun                             if (mode->HDisplay > crtc_width)
1082*4882a593Smuzhiyun                                 crtc_width = mode->HDisplay;
1083*4882a593Smuzhiyun                             if (mode->VDisplay > crtc_width)
1084*4882a593Smuzhiyun                                 crtc_width = mode->VDisplay;
1085*4882a593Smuzhiyun                             if (mode->VDisplay > crtc_height)
1086*4882a593Smuzhiyun                                 crtc_height = mode->VDisplay;
1087*4882a593Smuzhiyun                             if (mode->HDisplay > crtc_height)
1088*4882a593Smuzhiyun                                 crtc_height = mode->HDisplay;
1089*4882a593Smuzhiyun                         }
1090*4882a593Smuzhiyun                     }
1091*4882a593Smuzhiyun             }
1092*4882a593Smuzhiyun         }
1093*4882a593Smuzhiyun         if (crtc_width > width)
1094*4882a593Smuzhiyun             width = crtc_width;
1095*4882a593Smuzhiyun         if (crtc_height > height)
1096*4882a593Smuzhiyun             height = crtc_height;
1097*4882a593Smuzhiyun     }
1098*4882a593Smuzhiyun     if (config->maxWidth && width > config->maxWidth)
1099*4882a593Smuzhiyun         width = config->maxWidth;
1100*4882a593Smuzhiyun     if (config->maxHeight && height > config->maxHeight)
1101*4882a593Smuzhiyun         height = config->maxHeight;
1102*4882a593Smuzhiyun     if (config->minWidth && width < config->minWidth)
1103*4882a593Smuzhiyun         width = config->minWidth;
1104*4882a593Smuzhiyun     if (config->minHeight && height < config->minHeight)
1105*4882a593Smuzhiyun         height = config->minHeight;
1106*4882a593Smuzhiyun     *widthp = width;
1107*4882a593Smuzhiyun     *heightp = height;
1108*4882a593Smuzhiyun }
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun #define POSITION_UNSET	-100000
1111*4882a593Smuzhiyun 
1112*4882a593Smuzhiyun /*
1113*4882a593Smuzhiyun  * check if the user configured any outputs at all
1114*4882a593Smuzhiyun  * with either a position or a relative setting or a mode.
1115*4882a593Smuzhiyun  */
1116*4882a593Smuzhiyun static Bool
xf86UserConfiguredOutputs(ScrnInfoPtr scrn,DisplayModePtr * modes)1117*4882a593Smuzhiyun xf86UserConfiguredOutputs(ScrnInfoPtr scrn, DisplayModePtr * modes)
1118*4882a593Smuzhiyun {
1119*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
1120*4882a593Smuzhiyun     int o;
1121*4882a593Smuzhiyun     Bool user_conf = FALSE;
1122*4882a593Smuzhiyun 
1123*4882a593Smuzhiyun     for (o = 0; o < config->num_output; o++) {
1124*4882a593Smuzhiyun         xf86OutputPtr output = config->output[o];
1125*4882a593Smuzhiyun         const char *position;
1126*4882a593Smuzhiyun         const char *relative_name;
1127*4882a593Smuzhiyun         OutputOpts relation;
1128*4882a593Smuzhiyun         int r;
1129*4882a593Smuzhiyun 
1130*4882a593Smuzhiyun         static const OutputOpts relations[] = {
1131*4882a593Smuzhiyun             OPTION_BELOW, OPTION_RIGHT_OF, OPTION_ABOVE, OPTION_LEFT_OF
1132*4882a593Smuzhiyun         };
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun         position = xf86GetOptValString(output->options, OPTION_POSITION);
1135*4882a593Smuzhiyun         if (position)
1136*4882a593Smuzhiyun             user_conf = TRUE;
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun         relation = 0;
1139*4882a593Smuzhiyun         relative_name = NULL;
1140*4882a593Smuzhiyun         for (r = 0; r < 4; r++) {
1141*4882a593Smuzhiyun             relation = relations[r];
1142*4882a593Smuzhiyun             relative_name = xf86GetOptValString(output->options, relation);
1143*4882a593Smuzhiyun             if (relative_name)
1144*4882a593Smuzhiyun                 break;
1145*4882a593Smuzhiyun         }
1146*4882a593Smuzhiyun         if (relative_name)
1147*4882a593Smuzhiyun             user_conf = TRUE;
1148*4882a593Smuzhiyun 
1149*4882a593Smuzhiyun         modes[o] = xf86OutputHasUserPreferredMode(output);
1150*4882a593Smuzhiyun         if (modes[o])
1151*4882a593Smuzhiyun             user_conf = TRUE;
1152*4882a593Smuzhiyun     }
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun     return user_conf;
1155*4882a593Smuzhiyun }
1156*4882a593Smuzhiyun 
1157*4882a593Smuzhiyun static Bool
xf86InitialOutputPositions(ScrnInfoPtr scrn,DisplayModePtr * modes)1158*4882a593Smuzhiyun xf86InitialOutputPositions(ScrnInfoPtr scrn, DisplayModePtr * modes)
1159*4882a593Smuzhiyun {
1160*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
1161*4882a593Smuzhiyun     int o;
1162*4882a593Smuzhiyun     int min_x, min_y;
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun     /* check for initial right-of heuristic */
1165*4882a593Smuzhiyun     for (o = 0; o < config->num_output; o++)
1166*4882a593Smuzhiyun     {
1167*4882a593Smuzhiyun         xf86OutputPtr output = config->output[o];
1168*4882a593Smuzhiyun 
1169*4882a593Smuzhiyun         if (output->initial_x || output->initial_y)
1170*4882a593Smuzhiyun             return TRUE;
1171*4882a593Smuzhiyun     }
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun     for (o = 0; o < config->num_output; o++) {
1174*4882a593Smuzhiyun         xf86OutputPtr output = config->output[o];
1175*4882a593Smuzhiyun 
1176*4882a593Smuzhiyun         output->initial_x = output->initial_y = POSITION_UNSET;
1177*4882a593Smuzhiyun     }
1178*4882a593Smuzhiyun 
1179*4882a593Smuzhiyun     /*
1180*4882a593Smuzhiyun      * Loop until all outputs are set
1181*4882a593Smuzhiyun      */
1182*4882a593Smuzhiyun     for (;;) {
1183*4882a593Smuzhiyun         Bool any_set = FALSE;
1184*4882a593Smuzhiyun         Bool keep_going = FALSE;
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun         for (o = 0; o < config->num_output; o++) {
1187*4882a593Smuzhiyun             static const OutputOpts relations[] = {
1188*4882a593Smuzhiyun                 OPTION_BELOW, OPTION_RIGHT_OF, OPTION_ABOVE, OPTION_LEFT_OF
1189*4882a593Smuzhiyun             };
1190*4882a593Smuzhiyun             xf86OutputPtr output = config->output[o];
1191*4882a593Smuzhiyun             xf86OutputPtr relative;
1192*4882a593Smuzhiyun             const char *relative_name;
1193*4882a593Smuzhiyun             const char *position;
1194*4882a593Smuzhiyun             OutputOpts relation;
1195*4882a593Smuzhiyun             int r;
1196*4882a593Smuzhiyun 
1197*4882a593Smuzhiyun             if (output->initial_x != POSITION_UNSET)
1198*4882a593Smuzhiyun                 continue;
1199*4882a593Smuzhiyun             position = xf86GetOptValString(output->options, OPTION_POSITION);
1200*4882a593Smuzhiyun             /*
1201*4882a593Smuzhiyun              * Absolute position wins
1202*4882a593Smuzhiyun              */
1203*4882a593Smuzhiyun             if (position) {
1204*4882a593Smuzhiyun                 int x, y;
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun                 if (sscanf(position, "%d %d", &x, &y) == 2) {
1207*4882a593Smuzhiyun                     output->initial_x = x;
1208*4882a593Smuzhiyun                     output->initial_y = y;
1209*4882a593Smuzhiyun                 }
1210*4882a593Smuzhiyun                 else {
1211*4882a593Smuzhiyun                     xf86DrvMsg(scrn->scrnIndex, X_ERROR,
1212*4882a593Smuzhiyun                                "Output %s position not of form \"x y\"\n",
1213*4882a593Smuzhiyun                                output->name);
1214*4882a593Smuzhiyun                     output->initial_x = output->initial_y = 0;
1215*4882a593Smuzhiyun                 }
1216*4882a593Smuzhiyun                 any_set = TRUE;
1217*4882a593Smuzhiyun                 continue;
1218*4882a593Smuzhiyun             }
1219*4882a593Smuzhiyun             /*
1220*4882a593Smuzhiyun              * Next comes relative positions
1221*4882a593Smuzhiyun              */
1222*4882a593Smuzhiyun             relation = 0;
1223*4882a593Smuzhiyun             relative_name = NULL;
1224*4882a593Smuzhiyun             for (r = 0; r < 4; r++) {
1225*4882a593Smuzhiyun                 relation = relations[r];
1226*4882a593Smuzhiyun                 relative_name = xf86GetOptValString(output->options, relation);
1227*4882a593Smuzhiyun                 if (relative_name)
1228*4882a593Smuzhiyun                     break;
1229*4882a593Smuzhiyun             }
1230*4882a593Smuzhiyun             if (relative_name) {
1231*4882a593Smuzhiyun                 int or;
1232*4882a593Smuzhiyun 
1233*4882a593Smuzhiyun                 relative = NULL;
1234*4882a593Smuzhiyun                 for (or = 0; or < config->num_output; or++) {
1235*4882a593Smuzhiyun                     xf86OutputPtr out_rel = config->output[or];
1236*4882a593Smuzhiyun                     XF86ConfMonitorPtr rel_mon = out_rel->conf_monitor;
1237*4882a593Smuzhiyun 
1238*4882a593Smuzhiyun                     if (rel_mon) {
1239*4882a593Smuzhiyun                         if (xf86nameCompare(rel_mon->mon_identifier,
1240*4882a593Smuzhiyun                                             relative_name) == 0) {
1241*4882a593Smuzhiyun                             relative = config->output[or];
1242*4882a593Smuzhiyun                             break;
1243*4882a593Smuzhiyun                         }
1244*4882a593Smuzhiyun                     }
1245*4882a593Smuzhiyun                     if (strcmp(out_rel->name, relative_name) == 0) {
1246*4882a593Smuzhiyun                         relative = config->output[or];
1247*4882a593Smuzhiyun                         break;
1248*4882a593Smuzhiyun                     }
1249*4882a593Smuzhiyun                 }
1250*4882a593Smuzhiyun                 if (!relative) {
1251*4882a593Smuzhiyun                     xf86DrvMsg(scrn->scrnIndex, X_ERROR,
1252*4882a593Smuzhiyun                                "Cannot position output %s relative to unknown output %s\n",
1253*4882a593Smuzhiyun                                output->name, relative_name);
1254*4882a593Smuzhiyun                     output->initial_x = 0;
1255*4882a593Smuzhiyun                     output->initial_y = 0;
1256*4882a593Smuzhiyun                     any_set = TRUE;
1257*4882a593Smuzhiyun                     continue;
1258*4882a593Smuzhiyun                 }
1259*4882a593Smuzhiyun                 if (!modes[or]) {
1260*4882a593Smuzhiyun                     xf86DrvMsg(scrn->scrnIndex, X_ERROR,
1261*4882a593Smuzhiyun                                "Cannot position output %s relative to output %s without modes\n",
1262*4882a593Smuzhiyun                                output->name, relative_name);
1263*4882a593Smuzhiyun                     output->initial_x = 0;
1264*4882a593Smuzhiyun                     output->initial_y = 0;
1265*4882a593Smuzhiyun                     any_set = TRUE;
1266*4882a593Smuzhiyun                     continue;
1267*4882a593Smuzhiyun                 }
1268*4882a593Smuzhiyun                 if (relative->initial_x == POSITION_UNSET) {
1269*4882a593Smuzhiyun                     keep_going = TRUE;
1270*4882a593Smuzhiyun                     continue;
1271*4882a593Smuzhiyun                 }
1272*4882a593Smuzhiyun                 output->initial_x = relative->initial_x;
1273*4882a593Smuzhiyun                 output->initial_y = relative->initial_y;
1274*4882a593Smuzhiyun                 switch (relation) {
1275*4882a593Smuzhiyun                 case OPTION_BELOW:
1276*4882a593Smuzhiyun                     output->initial_y +=
1277*4882a593Smuzhiyun                         xf86ModeHeight(modes[or], relative->initial_rotation);
1278*4882a593Smuzhiyun                     break;
1279*4882a593Smuzhiyun                 case OPTION_RIGHT_OF:
1280*4882a593Smuzhiyun                     output->initial_x +=
1281*4882a593Smuzhiyun                         xf86ModeWidth(modes[or], relative->initial_rotation);
1282*4882a593Smuzhiyun                     break;
1283*4882a593Smuzhiyun                 case OPTION_ABOVE:
1284*4882a593Smuzhiyun                     if (modes[o])
1285*4882a593Smuzhiyun                         output->initial_y -=
1286*4882a593Smuzhiyun                             xf86ModeHeight(modes[o], output->initial_rotation);
1287*4882a593Smuzhiyun                     break;
1288*4882a593Smuzhiyun                 case OPTION_LEFT_OF:
1289*4882a593Smuzhiyun                     if (modes[o])
1290*4882a593Smuzhiyun                         output->initial_x -=
1291*4882a593Smuzhiyun                             xf86ModeWidth(modes[o], output->initial_rotation);
1292*4882a593Smuzhiyun                     break;
1293*4882a593Smuzhiyun                 default:
1294*4882a593Smuzhiyun                     break;
1295*4882a593Smuzhiyun                 }
1296*4882a593Smuzhiyun                 any_set = TRUE;
1297*4882a593Smuzhiyun                 continue;
1298*4882a593Smuzhiyun             }
1299*4882a593Smuzhiyun 
1300*4882a593Smuzhiyun             /* Nothing set, just stick them at 0,0 */
1301*4882a593Smuzhiyun             output->initial_x = 0;
1302*4882a593Smuzhiyun             output->initial_y = 0;
1303*4882a593Smuzhiyun             any_set = TRUE;
1304*4882a593Smuzhiyun         }
1305*4882a593Smuzhiyun         if (!keep_going)
1306*4882a593Smuzhiyun             break;
1307*4882a593Smuzhiyun         if (!any_set) {
1308*4882a593Smuzhiyun             for (o = 0; o < config->num_output; o++) {
1309*4882a593Smuzhiyun                 xf86OutputPtr output = config->output[o];
1310*4882a593Smuzhiyun 
1311*4882a593Smuzhiyun                 if (output->initial_x == POSITION_UNSET) {
1312*4882a593Smuzhiyun                     xf86DrvMsg(scrn->scrnIndex, X_ERROR,
1313*4882a593Smuzhiyun                                "Output position loop. Moving %s to 0,0\n",
1314*4882a593Smuzhiyun                                output->name);
1315*4882a593Smuzhiyun                     output->initial_x = output->initial_y = 0;
1316*4882a593Smuzhiyun                     break;
1317*4882a593Smuzhiyun                 }
1318*4882a593Smuzhiyun             }
1319*4882a593Smuzhiyun         }
1320*4882a593Smuzhiyun     }
1321*4882a593Smuzhiyun 
1322*4882a593Smuzhiyun     /*
1323*4882a593Smuzhiyun      * normalize positions
1324*4882a593Smuzhiyun      */
1325*4882a593Smuzhiyun     min_x = 1000000;
1326*4882a593Smuzhiyun     min_y = 1000000;
1327*4882a593Smuzhiyun     for (o = 0; o < config->num_output; o++) {
1328*4882a593Smuzhiyun         xf86OutputPtr output = config->output[o];
1329*4882a593Smuzhiyun 
1330*4882a593Smuzhiyun         if (output->initial_x < min_x)
1331*4882a593Smuzhiyun             min_x = output->initial_x;
1332*4882a593Smuzhiyun         if (output->initial_y < min_y)
1333*4882a593Smuzhiyun             min_y = output->initial_y;
1334*4882a593Smuzhiyun     }
1335*4882a593Smuzhiyun 
1336*4882a593Smuzhiyun     for (o = 0; o < config->num_output; o++) {
1337*4882a593Smuzhiyun         xf86OutputPtr output = config->output[o];
1338*4882a593Smuzhiyun 
1339*4882a593Smuzhiyun         output->initial_x -= min_x;
1340*4882a593Smuzhiyun         output->initial_y -= min_y;
1341*4882a593Smuzhiyun     }
1342*4882a593Smuzhiyun     return TRUE;
1343*4882a593Smuzhiyun }
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun static void
xf86InitialPanning(ScrnInfoPtr scrn)1346*4882a593Smuzhiyun xf86InitialPanning(ScrnInfoPtr scrn)
1347*4882a593Smuzhiyun {
1348*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
1349*4882a593Smuzhiyun     int o;
1350*4882a593Smuzhiyun 
1351*4882a593Smuzhiyun     for (o = 0; o < config->num_output; o++) {
1352*4882a593Smuzhiyun         xf86OutputPtr output = config->output[o];
1353*4882a593Smuzhiyun         const char *panning = xf86GetOptValString(output->options, OPTION_PANNING);
1354*4882a593Smuzhiyun         int width, height, left, top;
1355*4882a593Smuzhiyun         int track_width, track_height, track_left, track_top;
1356*4882a593Smuzhiyun         int brdr[4];
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun         memset(&output->initialTotalArea, 0, sizeof(BoxRec));
1359*4882a593Smuzhiyun         memset(&output->initialTrackingArea, 0, sizeof(BoxRec));
1360*4882a593Smuzhiyun         memset(output->initialBorder, 0, 4 * sizeof(INT16));
1361*4882a593Smuzhiyun 
1362*4882a593Smuzhiyun         if (!panning)
1363*4882a593Smuzhiyun             continue;
1364*4882a593Smuzhiyun 
1365*4882a593Smuzhiyun         switch (sscanf(panning, "%dx%d+%d+%d/%dx%d+%d+%d/%d/%d/%d/%d",
1366*4882a593Smuzhiyun                        &width, &height, &left, &top,
1367*4882a593Smuzhiyun                        &track_width, &track_height, &track_left, &track_top,
1368*4882a593Smuzhiyun                        &brdr[0], &brdr[1], &brdr[2], &brdr[3])) {
1369*4882a593Smuzhiyun         case 12:
1370*4882a593Smuzhiyun             output->initialBorder[0] = brdr[0];
1371*4882a593Smuzhiyun             output->initialBorder[1] = brdr[1];
1372*4882a593Smuzhiyun             output->initialBorder[2] = brdr[2];
1373*4882a593Smuzhiyun             output->initialBorder[3] = brdr[3];
1374*4882a593Smuzhiyun             /* fall through */
1375*4882a593Smuzhiyun         case 8:
1376*4882a593Smuzhiyun             output->initialTrackingArea.x1 = track_left;
1377*4882a593Smuzhiyun             output->initialTrackingArea.y1 = track_top;
1378*4882a593Smuzhiyun             output->initialTrackingArea.x2 = track_left + track_width;
1379*4882a593Smuzhiyun             output->initialTrackingArea.y2 = track_top + track_height;
1380*4882a593Smuzhiyun             /* fall through */
1381*4882a593Smuzhiyun         case 4:
1382*4882a593Smuzhiyun             output->initialTotalArea.x1 = left;
1383*4882a593Smuzhiyun             output->initialTotalArea.y1 = top;
1384*4882a593Smuzhiyun             /* fall through */
1385*4882a593Smuzhiyun         case 2:
1386*4882a593Smuzhiyun             output->initialTotalArea.x2 = output->initialTotalArea.x1 + width;
1387*4882a593Smuzhiyun             output->initialTotalArea.y2 = output->initialTotalArea.y1 + height;
1388*4882a593Smuzhiyun             break;
1389*4882a593Smuzhiyun         default:
1390*4882a593Smuzhiyun             xf86DrvMsg(scrn->scrnIndex, X_ERROR,
1391*4882a593Smuzhiyun                        "Broken panning specification '%s' for output %s in config file\n",
1392*4882a593Smuzhiyun                        panning, output->name);
1393*4882a593Smuzhiyun         }
1394*4882a593Smuzhiyun     }
1395*4882a593Smuzhiyun }
1396*4882a593Smuzhiyun 
1397*4882a593Smuzhiyun /** Return - 0 + if a should be earlier, same or later than b in list
1398*4882a593Smuzhiyun  */
1399*4882a593Smuzhiyun static int
xf86ModeCompare(DisplayModePtr a,DisplayModePtr b)1400*4882a593Smuzhiyun xf86ModeCompare(DisplayModePtr a, DisplayModePtr b)
1401*4882a593Smuzhiyun {
1402*4882a593Smuzhiyun     int diff;
1403*4882a593Smuzhiyun 
1404*4882a593Smuzhiyun     diff = ((b->type & M_T_PREFERRED) != 0) - ((a->type & M_T_PREFERRED) != 0);
1405*4882a593Smuzhiyun     if (diff)
1406*4882a593Smuzhiyun         return diff;
1407*4882a593Smuzhiyun     diff = b->HDisplay * b->VDisplay - a->HDisplay * a->VDisplay;
1408*4882a593Smuzhiyun     if (diff)
1409*4882a593Smuzhiyun         return diff;
1410*4882a593Smuzhiyun     diff = b->Clock - a->Clock;
1411*4882a593Smuzhiyun     return diff;
1412*4882a593Smuzhiyun }
1413*4882a593Smuzhiyun 
1414*4882a593Smuzhiyun /**
1415*4882a593Smuzhiyun  * Insertion sort input in-place and return the resulting head
1416*4882a593Smuzhiyun  */
1417*4882a593Smuzhiyun static DisplayModePtr
xf86SortModes(DisplayModePtr input)1418*4882a593Smuzhiyun xf86SortModes(DisplayModePtr input)
1419*4882a593Smuzhiyun {
1420*4882a593Smuzhiyun     DisplayModePtr output = NULL, i, o, n, *op, prev;
1421*4882a593Smuzhiyun 
1422*4882a593Smuzhiyun     /* sort by preferred status and pixel area */
1423*4882a593Smuzhiyun     while (input) {
1424*4882a593Smuzhiyun         i = input;
1425*4882a593Smuzhiyun         input = input->next;
1426*4882a593Smuzhiyun         for (op = &output; (o = *op); op = &o->next)
1427*4882a593Smuzhiyun             if (xf86ModeCompare(o, i) > 0)
1428*4882a593Smuzhiyun                 break;
1429*4882a593Smuzhiyun         i->next = *op;
1430*4882a593Smuzhiyun         *op = i;
1431*4882a593Smuzhiyun     }
1432*4882a593Smuzhiyun     /* prune identical modes */
1433*4882a593Smuzhiyun     for (o = output; o && (n = o->next); o = n) {
1434*4882a593Smuzhiyun         if (!strcmp(o->name, n->name) && xf86ModesEqual(o, n)) {
1435*4882a593Smuzhiyun             o->next = n->next;
1436*4882a593Smuzhiyun             free((void *) n->name);
1437*4882a593Smuzhiyun             free(n);
1438*4882a593Smuzhiyun             n = o;
1439*4882a593Smuzhiyun         }
1440*4882a593Smuzhiyun     }
1441*4882a593Smuzhiyun     /* hook up backward links */
1442*4882a593Smuzhiyun     prev = NULL;
1443*4882a593Smuzhiyun     for (o = output; o; o = o->next) {
1444*4882a593Smuzhiyun         o->prev = prev;
1445*4882a593Smuzhiyun         prev = o;
1446*4882a593Smuzhiyun     }
1447*4882a593Smuzhiyun     return output;
1448*4882a593Smuzhiyun }
1449*4882a593Smuzhiyun 
1450*4882a593Smuzhiyun static const char *
preferredMode(ScrnInfoPtr pScrn,xf86OutputPtr output)1451*4882a593Smuzhiyun preferredMode(ScrnInfoPtr pScrn, xf86OutputPtr output)
1452*4882a593Smuzhiyun {
1453*4882a593Smuzhiyun     const char *preferred_mode = NULL;
1454*4882a593Smuzhiyun 
1455*4882a593Smuzhiyun     /* Check for a configured preference for a particular mode */
1456*4882a593Smuzhiyun     preferred_mode = xf86GetOptValString(output->options,
1457*4882a593Smuzhiyun                                          OPTION_PREFERRED_MODE);
1458*4882a593Smuzhiyun     if (preferred_mode)
1459*4882a593Smuzhiyun         return preferred_mode;
1460*4882a593Smuzhiyun 
1461*4882a593Smuzhiyun     if (pScrn->display->modes && *pScrn->display->modes)
1462*4882a593Smuzhiyun         preferred_mode = *pScrn->display->modes;
1463*4882a593Smuzhiyun 
1464*4882a593Smuzhiyun     return preferred_mode;
1465*4882a593Smuzhiyun }
1466*4882a593Smuzhiyun 
1467*4882a593Smuzhiyun /** identify a token
1468*4882a593Smuzhiyun  * args
1469*4882a593Smuzhiyun  *   *src     a string with zero or more tokens, e.g. "tok0 tok1",
1470*4882a593Smuzhiyun  *   **token  stores a pointer to the first token character,
1471*4882a593Smuzhiyun  *   *len     stores the token length.
1472*4882a593Smuzhiyun  * return
1473*4882a593Smuzhiyun  *   a pointer into src[] at the token terminating character, or
1474*4882a593Smuzhiyun  *   NULL if no token is found.
1475*4882a593Smuzhiyun  */
1476*4882a593Smuzhiyun static const char *
gettoken(const char * src,const char ** token,int * len)1477*4882a593Smuzhiyun gettoken(const char *src, const char **token, int *len)
1478*4882a593Smuzhiyun {
1479*4882a593Smuzhiyun     const char *delim = " \t";
1480*4882a593Smuzhiyun     int skip;
1481*4882a593Smuzhiyun 
1482*4882a593Smuzhiyun     if (!src)
1483*4882a593Smuzhiyun         return NULL;
1484*4882a593Smuzhiyun 
1485*4882a593Smuzhiyun     skip = strspn(src, delim);
1486*4882a593Smuzhiyun     *token = &src[skip];
1487*4882a593Smuzhiyun 
1488*4882a593Smuzhiyun     *len = strcspn(*token, delim);
1489*4882a593Smuzhiyun     /* Support for backslash escaped delimiters could be implemented
1490*4882a593Smuzhiyun      * here.
1491*4882a593Smuzhiyun      */
1492*4882a593Smuzhiyun 
1493*4882a593Smuzhiyun     /* (*token)[0] != '\0'  <==>  *len > 0 */
1494*4882a593Smuzhiyun     if (*len > 0)
1495*4882a593Smuzhiyun         return &(*token)[*len];
1496*4882a593Smuzhiyun     else
1497*4882a593Smuzhiyun         return NULL;
1498*4882a593Smuzhiyun }
1499*4882a593Smuzhiyun 
1500*4882a593Smuzhiyun /** Check for a user configured zoom mode list, Option "ZoomModes":
1501*4882a593Smuzhiyun  *
1502*4882a593Smuzhiyun  * Section "Monitor"
1503*4882a593Smuzhiyun  *   Identifier "a21inch"
1504*4882a593Smuzhiyun  *   Option "ZoomModes" "1600x1200 1280x1024 1280x1024 640x480"
1505*4882a593Smuzhiyun  * EndSection
1506*4882a593Smuzhiyun  *
1507*4882a593Smuzhiyun  * Each user mode name is searched for independently so the list
1508*4882a593Smuzhiyun  * specification order is free.  An output mode is matched at most
1509*4882a593Smuzhiyun  * once, a mode with an already set M_T_USERDEF type bit is skipped.
1510*4882a593Smuzhiyun  * Thus a repeat mode name specification matches the next output mode
1511*4882a593Smuzhiyun  * with the same name.
1512*4882a593Smuzhiyun  *
1513*4882a593Smuzhiyun  * Ctrl+Alt+Keypad-{Plus,Minus} zooms {in,out} by selecting the
1514*4882a593Smuzhiyun  * {next,previous} M_T_USERDEF mode in the screen modes list, itself
1515*4882a593Smuzhiyun  * sorted toward lower dot area or lower dot clock frequency, see
1516*4882a593Smuzhiyun  *   modes/xf86Crtc.c: xf86SortModes() xf86SetScrnInfoModes(), and
1517*4882a593Smuzhiyun  *   common/xf86Cursor.c: xf86ZoomViewport().
1518*4882a593Smuzhiyun  */
1519*4882a593Smuzhiyun static int
processZoomModes(xf86OutputPtr output)1520*4882a593Smuzhiyun processZoomModes(xf86OutputPtr output)
1521*4882a593Smuzhiyun {
1522*4882a593Smuzhiyun     const char *zoom_modes;
1523*4882a593Smuzhiyun     int count = 0;
1524*4882a593Smuzhiyun 
1525*4882a593Smuzhiyun     zoom_modes = xf86GetOptValString(output->options, OPTION_ZOOM_MODES);
1526*4882a593Smuzhiyun 
1527*4882a593Smuzhiyun     if (zoom_modes) {
1528*4882a593Smuzhiyun         const char *token, *next;
1529*4882a593Smuzhiyun         int len;
1530*4882a593Smuzhiyun 
1531*4882a593Smuzhiyun         next = gettoken(zoom_modes, &token, &len);
1532*4882a593Smuzhiyun         while (next) {
1533*4882a593Smuzhiyun             DisplayModePtr mode;
1534*4882a593Smuzhiyun 
1535*4882a593Smuzhiyun             for (mode = output->probed_modes; mode; mode = mode->next)
1536*4882a593Smuzhiyun                 if (!strncmp(token, mode->name, len)  /* prefix match */
1537*4882a593Smuzhiyun                     && mode->name[len] == '\0'        /* equal length */
1538*4882a593Smuzhiyun                     && !(mode->type & M_T_USERDEF)) { /* no rematch */
1539*4882a593Smuzhiyun                     mode->type |= M_T_USERDEF;
1540*4882a593Smuzhiyun                     break;
1541*4882a593Smuzhiyun                 }
1542*4882a593Smuzhiyun 
1543*4882a593Smuzhiyun             count++;
1544*4882a593Smuzhiyun             next = gettoken(next, &token, &len);
1545*4882a593Smuzhiyun         }
1546*4882a593Smuzhiyun     }
1547*4882a593Smuzhiyun 
1548*4882a593Smuzhiyun     return count;
1549*4882a593Smuzhiyun }
1550*4882a593Smuzhiyun 
1551*4882a593Smuzhiyun static void
GuessRangeFromModes(MonPtr mon,DisplayModePtr mode)1552*4882a593Smuzhiyun GuessRangeFromModes(MonPtr mon, DisplayModePtr mode)
1553*4882a593Smuzhiyun {
1554*4882a593Smuzhiyun     if (!mon || !mode)
1555*4882a593Smuzhiyun         return;
1556*4882a593Smuzhiyun 
1557*4882a593Smuzhiyun     mon->nHsync = 1;
1558*4882a593Smuzhiyun     mon->hsync[0].lo = 1024.0;
1559*4882a593Smuzhiyun     mon->hsync[0].hi = 0.0;
1560*4882a593Smuzhiyun 
1561*4882a593Smuzhiyun     mon->nVrefresh = 1;
1562*4882a593Smuzhiyun     mon->vrefresh[0].lo = 1024.0;
1563*4882a593Smuzhiyun     mon->vrefresh[0].hi = 0.0;
1564*4882a593Smuzhiyun 
1565*4882a593Smuzhiyun     while (mode) {
1566*4882a593Smuzhiyun         if (!mode->HSync)
1567*4882a593Smuzhiyun             mode->HSync = ((float) mode->Clock) / ((float) mode->HTotal);
1568*4882a593Smuzhiyun 
1569*4882a593Smuzhiyun         if (!mode->VRefresh)
1570*4882a593Smuzhiyun             mode->VRefresh = (1000.0 * ((float) mode->Clock)) /
1571*4882a593Smuzhiyun                 ((float) (mode->HTotal * mode->VTotal));
1572*4882a593Smuzhiyun 
1573*4882a593Smuzhiyun         if (mode->HSync < mon->hsync[0].lo)
1574*4882a593Smuzhiyun             mon->hsync[0].lo = mode->HSync;
1575*4882a593Smuzhiyun 
1576*4882a593Smuzhiyun         if (mode->HSync > mon->hsync[0].hi)
1577*4882a593Smuzhiyun             mon->hsync[0].hi = mode->HSync;
1578*4882a593Smuzhiyun 
1579*4882a593Smuzhiyun         if (mode->VRefresh < mon->vrefresh[0].lo)
1580*4882a593Smuzhiyun             mon->vrefresh[0].lo = mode->VRefresh;
1581*4882a593Smuzhiyun 
1582*4882a593Smuzhiyun         if (mode->VRefresh > mon->vrefresh[0].hi)
1583*4882a593Smuzhiyun             mon->vrefresh[0].hi = mode->VRefresh;
1584*4882a593Smuzhiyun 
1585*4882a593Smuzhiyun         mode = mode->next;
1586*4882a593Smuzhiyun     }
1587*4882a593Smuzhiyun 
1588*4882a593Smuzhiyun     /* stretch out the bottom to fit 640x480@60 */
1589*4882a593Smuzhiyun     if (mon->hsync[0].lo > 31.0)
1590*4882a593Smuzhiyun         mon->hsync[0].lo = 31.0;
1591*4882a593Smuzhiyun     if (mon->vrefresh[0].lo > 58.0)
1592*4882a593Smuzhiyun         mon->vrefresh[0].lo = 58.0;
1593*4882a593Smuzhiyun }
1594*4882a593Smuzhiyun 
1595*4882a593Smuzhiyun enum det_monrec_source {
1596*4882a593Smuzhiyun     sync_config, sync_edid, sync_default
1597*4882a593Smuzhiyun };
1598*4882a593Smuzhiyun 
1599*4882a593Smuzhiyun struct det_monrec_parameter {
1600*4882a593Smuzhiyun     MonRec *mon_rec;
1601*4882a593Smuzhiyun     int *max_clock;
1602*4882a593Smuzhiyun     Bool set_hsync;
1603*4882a593Smuzhiyun     Bool set_vrefresh;
1604*4882a593Smuzhiyun     enum det_monrec_source *sync_source;
1605*4882a593Smuzhiyun };
1606*4882a593Smuzhiyun 
1607*4882a593Smuzhiyun static void
handle_detailed_monrec(struct detailed_monitor_section * det_mon,void * data)1608*4882a593Smuzhiyun handle_detailed_monrec(struct detailed_monitor_section *det_mon, void *data)
1609*4882a593Smuzhiyun {
1610*4882a593Smuzhiyun     struct det_monrec_parameter *p;
1611*4882a593Smuzhiyun 
1612*4882a593Smuzhiyun     p = (struct det_monrec_parameter *) data;
1613*4882a593Smuzhiyun 
1614*4882a593Smuzhiyun     if (det_mon->type == DS_RANGES) {
1615*4882a593Smuzhiyun         struct monitor_ranges *ranges = &det_mon->section.ranges;
1616*4882a593Smuzhiyun 
1617*4882a593Smuzhiyun         if (p->set_hsync && ranges->max_h) {
1618*4882a593Smuzhiyun             p->mon_rec->hsync[p->mon_rec->nHsync].lo = ranges->min_h;
1619*4882a593Smuzhiyun             p->mon_rec->hsync[p->mon_rec->nHsync].hi = ranges->max_h;
1620*4882a593Smuzhiyun             p->mon_rec->nHsync++;
1621*4882a593Smuzhiyun             if (*p->sync_source == sync_default)
1622*4882a593Smuzhiyun                 *p->sync_source = sync_edid;
1623*4882a593Smuzhiyun         }
1624*4882a593Smuzhiyun         if (p->set_vrefresh && ranges->max_v) {
1625*4882a593Smuzhiyun             p->mon_rec->vrefresh[p->mon_rec->nVrefresh].lo = ranges->min_v;
1626*4882a593Smuzhiyun             p->mon_rec->vrefresh[p->mon_rec->nVrefresh].hi = ranges->max_v;
1627*4882a593Smuzhiyun             p->mon_rec->nVrefresh++;
1628*4882a593Smuzhiyun             if (*p->sync_source == sync_default)
1629*4882a593Smuzhiyun                 *p->sync_source = sync_edid;
1630*4882a593Smuzhiyun         }
1631*4882a593Smuzhiyun         if (ranges->max_clock * 1000 > *p->max_clock)
1632*4882a593Smuzhiyun             *p->max_clock = ranges->max_clock * 1000;
1633*4882a593Smuzhiyun     }
1634*4882a593Smuzhiyun }
1635*4882a593Smuzhiyun 
1636*4882a593Smuzhiyun void
xf86ProbeOutputModes(ScrnInfoPtr scrn,int maxX,int maxY)1637*4882a593Smuzhiyun xf86ProbeOutputModes(ScrnInfoPtr scrn, int maxX, int maxY)
1638*4882a593Smuzhiyun {
1639*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
1640*4882a593Smuzhiyun     int o;
1641*4882a593Smuzhiyun 
1642*4882a593Smuzhiyun     /* When canGrow was TRUE in the initial configuration we have to
1643*4882a593Smuzhiyun      * compare against the maximum values so that we don't drop modes.
1644*4882a593Smuzhiyun      * When canGrow was FALSE, the maximum values would have been clamped
1645*4882a593Smuzhiyun      * anyway.
1646*4882a593Smuzhiyun      */
1647*4882a593Smuzhiyun     if (maxX == 0 || maxY == 0) {
1648*4882a593Smuzhiyun         maxX = config->maxWidth;
1649*4882a593Smuzhiyun         maxY = config->maxHeight;
1650*4882a593Smuzhiyun     }
1651*4882a593Smuzhiyun 
1652*4882a593Smuzhiyun     /* Probe the list of modes for each output. */
1653*4882a593Smuzhiyun     for (o = 0; o < config->num_output; o++) {
1654*4882a593Smuzhiyun         xf86OutputPtr output = config->output[o];
1655*4882a593Smuzhiyun         DisplayModePtr mode;
1656*4882a593Smuzhiyun         DisplayModePtr config_modes = NULL, output_modes, default_modes = NULL;
1657*4882a593Smuzhiyun         const char *preferred_mode;
1658*4882a593Smuzhiyun         xf86MonPtr edid_monitor;
1659*4882a593Smuzhiyun         XF86ConfMonitorPtr conf_monitor;
1660*4882a593Smuzhiyun         MonRec mon_rec;
1661*4882a593Smuzhiyun         int min_clock = 0;
1662*4882a593Smuzhiyun         int max_clock = 0;
1663*4882a593Smuzhiyun         double clock;
1664*4882a593Smuzhiyun         Bool add_default_modes;
1665*4882a593Smuzhiyun         Bool debug_modes = config->debug_modes || xf86Initialising;
1666*4882a593Smuzhiyun         enum det_monrec_source sync_source = sync_default;
1667*4882a593Smuzhiyun 
1668*4882a593Smuzhiyun         while (output->probed_modes != NULL)
1669*4882a593Smuzhiyun             xf86DeleteMode(&output->probed_modes, output->probed_modes);
1670*4882a593Smuzhiyun 
1671*4882a593Smuzhiyun         /*
1672*4882a593Smuzhiyun          * Check connection status
1673*4882a593Smuzhiyun          */
1674*4882a593Smuzhiyun         output->status = (*output->funcs->detect) (output);
1675*4882a593Smuzhiyun 
1676*4882a593Smuzhiyun         if (output->status == XF86OutputStatusDisconnected &&
1677*4882a593Smuzhiyun             !xf86ReturnOptValBool(output->options, OPTION_ENABLE, FALSE)) {
1678*4882a593Smuzhiyun             xf86OutputSetEDID(output, NULL);
1679*4882a593Smuzhiyun             continue;
1680*4882a593Smuzhiyun         }
1681*4882a593Smuzhiyun 
1682*4882a593Smuzhiyun         memset(&mon_rec, '\0', sizeof(mon_rec));
1683*4882a593Smuzhiyun 
1684*4882a593Smuzhiyun         conf_monitor = output->conf_monitor;
1685*4882a593Smuzhiyun 
1686*4882a593Smuzhiyun         if (conf_monitor) {
1687*4882a593Smuzhiyun             int i;
1688*4882a593Smuzhiyun 
1689*4882a593Smuzhiyun             for (i = 0; i < conf_monitor->mon_n_hsync; i++) {
1690*4882a593Smuzhiyun                 mon_rec.hsync[mon_rec.nHsync].lo =
1691*4882a593Smuzhiyun                     conf_monitor->mon_hsync[i].lo;
1692*4882a593Smuzhiyun                 mon_rec.hsync[mon_rec.nHsync].hi =
1693*4882a593Smuzhiyun                     conf_monitor->mon_hsync[i].hi;
1694*4882a593Smuzhiyun                 mon_rec.nHsync++;
1695*4882a593Smuzhiyun                 sync_source = sync_config;
1696*4882a593Smuzhiyun             }
1697*4882a593Smuzhiyun             for (i = 0; i < conf_monitor->mon_n_vrefresh; i++) {
1698*4882a593Smuzhiyun                 mon_rec.vrefresh[mon_rec.nVrefresh].lo =
1699*4882a593Smuzhiyun                     conf_monitor->mon_vrefresh[i].lo;
1700*4882a593Smuzhiyun                 mon_rec.vrefresh[mon_rec.nVrefresh].hi =
1701*4882a593Smuzhiyun                     conf_monitor->mon_vrefresh[i].hi;
1702*4882a593Smuzhiyun                 mon_rec.nVrefresh++;
1703*4882a593Smuzhiyun                 sync_source = sync_config;
1704*4882a593Smuzhiyun             }
1705*4882a593Smuzhiyun             config_modes = xf86GetMonitorModes(scrn, conf_monitor);
1706*4882a593Smuzhiyun         }
1707*4882a593Smuzhiyun 
1708*4882a593Smuzhiyun         output_modes = (*output->funcs->get_modes) (output);
1709*4882a593Smuzhiyun 
1710*4882a593Smuzhiyun         /*
1711*4882a593Smuzhiyun          * If the user has a preference, respect it.
1712*4882a593Smuzhiyun          * Otherwise, don't second-guess the driver.
1713*4882a593Smuzhiyun          */
1714*4882a593Smuzhiyun         if (!xf86GetOptValBool(output->options, OPTION_DEFAULT_MODES,
1715*4882a593Smuzhiyun                                &add_default_modes))
1716*4882a593Smuzhiyun             add_default_modes = (output_modes == NULL);
1717*4882a593Smuzhiyun 
1718*4882a593Smuzhiyun         edid_monitor = output->MonInfo;
1719*4882a593Smuzhiyun 
1720*4882a593Smuzhiyun         if (edid_monitor) {
1721*4882a593Smuzhiyun             struct det_monrec_parameter p;
1722*4882a593Smuzhiyun             struct cea_data_block *hdmi_db;
1723*4882a593Smuzhiyun 
1724*4882a593Smuzhiyun             /* if display is not continuous-frequency, don't add default modes */
1725*4882a593Smuzhiyun             if (!gtf_supported(edid_monitor))
1726*4882a593Smuzhiyun                 add_default_modes = FALSE;
1727*4882a593Smuzhiyun 
1728*4882a593Smuzhiyun             p.mon_rec = &mon_rec;
1729*4882a593Smuzhiyun             p.max_clock = &max_clock;
1730*4882a593Smuzhiyun             p.set_hsync = mon_rec.nHsync == 0;
1731*4882a593Smuzhiyun             p.set_vrefresh = mon_rec.nVrefresh == 0;
1732*4882a593Smuzhiyun             p.sync_source = &sync_source;
1733*4882a593Smuzhiyun 
1734*4882a593Smuzhiyun             xf86ForEachDetailedBlock(edid_monitor, handle_detailed_monrec, &p);
1735*4882a593Smuzhiyun 
1736*4882a593Smuzhiyun             /* Look at the CEA HDMI vendor block for the max TMDS freq */
1737*4882a593Smuzhiyun             hdmi_db = xf86MonitorFindHDMIBlock(edid_monitor);
1738*4882a593Smuzhiyun             if (hdmi_db && hdmi_db->len >= 7) {
1739*4882a593Smuzhiyun                 int tmds_freq = hdmi_db->u.vendor.hdmi.max_tmds_clock * 5000;
1740*4882a593Smuzhiyun                 xf86DrvMsg(scrn->scrnIndex, X_PROBED,
1741*4882a593Smuzhiyun                            "HDMI max TMDS frequency %dKHz\n", tmds_freq);
1742*4882a593Smuzhiyun                 if (tmds_freq > max_clock)
1743*4882a593Smuzhiyun                     max_clock = tmds_freq;
1744*4882a593Smuzhiyun             }
1745*4882a593Smuzhiyun         }
1746*4882a593Smuzhiyun 
1747*4882a593Smuzhiyun         if (xf86GetOptValFreq(output->options, OPTION_MIN_CLOCK,
1748*4882a593Smuzhiyun                               OPTUNITS_KHZ, &clock))
1749*4882a593Smuzhiyun             min_clock = (int) clock;
1750*4882a593Smuzhiyun         if (xf86GetOptValFreq(output->options, OPTION_MAX_CLOCK,
1751*4882a593Smuzhiyun                               OPTUNITS_KHZ, &clock))
1752*4882a593Smuzhiyun             max_clock = (int) clock;
1753*4882a593Smuzhiyun 
1754*4882a593Smuzhiyun         /* If we still don't have a sync range, guess wildly */
1755*4882a593Smuzhiyun         if (!mon_rec.nHsync || !mon_rec.nVrefresh)
1756*4882a593Smuzhiyun             GuessRangeFromModes(&mon_rec, output_modes);
1757*4882a593Smuzhiyun 
1758*4882a593Smuzhiyun         /*
1759*4882a593Smuzhiyun          * These limits will end up setting a 1024x768@60Hz mode by default,
1760*4882a593Smuzhiyun          * which seems like a fairly good mode to use when nothing else is
1761*4882a593Smuzhiyun          * specified
1762*4882a593Smuzhiyun          */
1763*4882a593Smuzhiyun         if (mon_rec.nHsync == 0) {
1764*4882a593Smuzhiyun             mon_rec.hsync[0].lo = 31.0;
1765*4882a593Smuzhiyun             mon_rec.hsync[0].hi = 55.0;
1766*4882a593Smuzhiyun             mon_rec.nHsync = 1;
1767*4882a593Smuzhiyun         }
1768*4882a593Smuzhiyun         if (mon_rec.nVrefresh == 0) {
1769*4882a593Smuzhiyun             mon_rec.vrefresh[0].lo = 58.0;
1770*4882a593Smuzhiyun             mon_rec.vrefresh[0].hi = 62.0;
1771*4882a593Smuzhiyun             mon_rec.nVrefresh = 1;
1772*4882a593Smuzhiyun         }
1773*4882a593Smuzhiyun 
1774*4882a593Smuzhiyun         if (add_default_modes)
1775*4882a593Smuzhiyun             default_modes = xf86GetDefaultModes();
1776*4882a593Smuzhiyun 
1777*4882a593Smuzhiyun         /*
1778*4882a593Smuzhiyun          * If this is not an RB monitor, remove RB modes from the default
1779*4882a593Smuzhiyun          * pool.  RB modes from the config or the monitor itself are fine.
1780*4882a593Smuzhiyun          */
1781*4882a593Smuzhiyun         if (!mon_rec.reducedblanking)
1782*4882a593Smuzhiyun             xf86ValidateModesReducedBlanking(scrn, default_modes);
1783*4882a593Smuzhiyun 
1784*4882a593Smuzhiyun         if (sync_source == sync_config) {
1785*4882a593Smuzhiyun             /*
1786*4882a593Smuzhiyun              * Check output and config modes against sync range from config file
1787*4882a593Smuzhiyun              */
1788*4882a593Smuzhiyun             xf86ValidateModesSync(scrn, output_modes, &mon_rec);
1789*4882a593Smuzhiyun             xf86ValidateModesSync(scrn, config_modes, &mon_rec);
1790*4882a593Smuzhiyun         }
1791*4882a593Smuzhiyun         /*
1792*4882a593Smuzhiyun          * Check default modes against sync range
1793*4882a593Smuzhiyun          */
1794*4882a593Smuzhiyun         xf86ValidateModesSync(scrn, default_modes, &mon_rec);
1795*4882a593Smuzhiyun         /*
1796*4882a593Smuzhiyun          * Check default modes against monitor max clock
1797*4882a593Smuzhiyun          */
1798*4882a593Smuzhiyun         if (max_clock) {
1799*4882a593Smuzhiyun             xf86ValidateModesClocks(scrn, default_modes,
1800*4882a593Smuzhiyun                                     &min_clock, &max_clock, 1);
1801*4882a593Smuzhiyun             xf86ValidateModesClocks(scrn, output_modes,
1802*4882a593Smuzhiyun                                     &min_clock, &max_clock, 1);
1803*4882a593Smuzhiyun         }
1804*4882a593Smuzhiyun 
1805*4882a593Smuzhiyun         output->probed_modes = NULL;
1806*4882a593Smuzhiyun         output->probed_modes = xf86ModesAdd(output->probed_modes, config_modes);
1807*4882a593Smuzhiyun         output->probed_modes = xf86ModesAdd(output->probed_modes, output_modes);
1808*4882a593Smuzhiyun         output->probed_modes =
1809*4882a593Smuzhiyun             xf86ModesAdd(output->probed_modes, default_modes);
1810*4882a593Smuzhiyun 
1811*4882a593Smuzhiyun         /*
1812*4882a593Smuzhiyun          * Check all modes against max size, interlace, and doublescan
1813*4882a593Smuzhiyun          */
1814*4882a593Smuzhiyun         if (maxX && maxY)
1815*4882a593Smuzhiyun             xf86ValidateModesSize(scrn, output->probed_modes, maxX, maxY, 0);
1816*4882a593Smuzhiyun 
1817*4882a593Smuzhiyun         {
1818*4882a593Smuzhiyun             int flags = (output->interlaceAllowed ? V_INTERLACE : 0) |
1819*4882a593Smuzhiyun                 (output->doubleScanAllowed ? V_DBLSCAN : 0);
1820*4882a593Smuzhiyun             xf86ValidateModesFlags(scrn, output->probed_modes, flags);
1821*4882a593Smuzhiyun         }
1822*4882a593Smuzhiyun 
1823*4882a593Smuzhiyun         /*
1824*4882a593Smuzhiyun          * Check all modes against output
1825*4882a593Smuzhiyun          */
1826*4882a593Smuzhiyun         for (mode = output->probed_modes; mode != NULL; mode = mode->next)
1827*4882a593Smuzhiyun             if (mode->status == MODE_OK)
1828*4882a593Smuzhiyun                 mode->status = (*output->funcs->mode_valid) (output, mode);
1829*4882a593Smuzhiyun 
1830*4882a593Smuzhiyun         xf86PruneInvalidModes(scrn, &output->probed_modes, debug_modes);
1831*4882a593Smuzhiyun 
1832*4882a593Smuzhiyun         output->probed_modes = xf86SortModes(output->probed_modes);
1833*4882a593Smuzhiyun 
1834*4882a593Smuzhiyun         /* Check for a configured preference for a particular mode */
1835*4882a593Smuzhiyun         preferred_mode = preferredMode(scrn, output);
1836*4882a593Smuzhiyun 
1837*4882a593Smuzhiyun         if (preferred_mode) {
1838*4882a593Smuzhiyun             for (mode = output->probed_modes; mode; mode = mode->next) {
1839*4882a593Smuzhiyun                 if (!strcmp(preferred_mode, mode->name)) {
1840*4882a593Smuzhiyun                     if (mode != output->probed_modes) {
1841*4882a593Smuzhiyun                         if (mode->prev)
1842*4882a593Smuzhiyun                             mode->prev->next = mode->next;
1843*4882a593Smuzhiyun                         if (mode->next)
1844*4882a593Smuzhiyun                             mode->next->prev = mode->prev;
1845*4882a593Smuzhiyun                         mode->next = output->probed_modes;
1846*4882a593Smuzhiyun                         output->probed_modes->prev = mode;
1847*4882a593Smuzhiyun                         mode->prev = NULL;
1848*4882a593Smuzhiyun                         output->probed_modes = mode;
1849*4882a593Smuzhiyun                     }
1850*4882a593Smuzhiyun                     mode->type |= (M_T_PREFERRED | M_T_USERPREF);
1851*4882a593Smuzhiyun                     break;
1852*4882a593Smuzhiyun                 }
1853*4882a593Smuzhiyun             }
1854*4882a593Smuzhiyun         }
1855*4882a593Smuzhiyun 
1856*4882a593Smuzhiyun         /* Ctrl+Alt+Keypad-{Plus,Minus} zoom mode: M_T_USERDEF mode type */
1857*4882a593Smuzhiyun         processZoomModes(output);
1858*4882a593Smuzhiyun 
1859*4882a593Smuzhiyun         output->initial_rotation = xf86OutputInitialRotation(output);
1860*4882a593Smuzhiyun 
1861*4882a593Smuzhiyun         if (debug_modes) {
1862*4882a593Smuzhiyun             if (output->probed_modes != NULL) {
1863*4882a593Smuzhiyun                 xf86DrvMsg(scrn->scrnIndex, X_INFO,
1864*4882a593Smuzhiyun                            "Printing probed modes for output %s\n",
1865*4882a593Smuzhiyun                            output->name);
1866*4882a593Smuzhiyun             }
1867*4882a593Smuzhiyun             else {
1868*4882a593Smuzhiyun                 xf86DrvMsg(scrn->scrnIndex, X_INFO,
1869*4882a593Smuzhiyun                            "No remaining probed modes for output %s\n",
1870*4882a593Smuzhiyun                            output->name);
1871*4882a593Smuzhiyun             }
1872*4882a593Smuzhiyun         }
1873*4882a593Smuzhiyun         for (mode = output->probed_modes; mode != NULL; mode = mode->next) {
1874*4882a593Smuzhiyun             /* The code to choose the best mode per pipe later on will require
1875*4882a593Smuzhiyun              * VRefresh to be set.
1876*4882a593Smuzhiyun              */
1877*4882a593Smuzhiyun             mode->VRefresh = xf86ModeVRefresh(mode);
1878*4882a593Smuzhiyun             xf86SetModeCrtc(mode, INTERLACE_HALVE_V);
1879*4882a593Smuzhiyun 
1880*4882a593Smuzhiyun             if (debug_modes)
1881*4882a593Smuzhiyun                 xf86PrintModeline(scrn->scrnIndex, mode);
1882*4882a593Smuzhiyun         }
1883*4882a593Smuzhiyun     }
1884*4882a593Smuzhiyun }
1885*4882a593Smuzhiyun 
1886*4882a593Smuzhiyun /**
1887*4882a593Smuzhiyun  * Copy one of the output mode lists to the ScrnInfo record
1888*4882a593Smuzhiyun  */
1889*4882a593Smuzhiyun 
1890*4882a593Smuzhiyun static DisplayModePtr
biggestMode(DisplayModePtr a,DisplayModePtr b)1891*4882a593Smuzhiyun biggestMode(DisplayModePtr a, DisplayModePtr b)
1892*4882a593Smuzhiyun {
1893*4882a593Smuzhiyun     int A, B;
1894*4882a593Smuzhiyun 
1895*4882a593Smuzhiyun     if (!a)
1896*4882a593Smuzhiyun         return b;
1897*4882a593Smuzhiyun     if (!b)
1898*4882a593Smuzhiyun         return a;
1899*4882a593Smuzhiyun 
1900*4882a593Smuzhiyun     A = a->HDisplay * a->VDisplay;
1901*4882a593Smuzhiyun     B = b->HDisplay * b->VDisplay;
1902*4882a593Smuzhiyun 
1903*4882a593Smuzhiyun     if (A > B)
1904*4882a593Smuzhiyun         return a;
1905*4882a593Smuzhiyun 
1906*4882a593Smuzhiyun     return b;
1907*4882a593Smuzhiyun }
1908*4882a593Smuzhiyun 
1909*4882a593Smuzhiyun static xf86OutputPtr
SetCompatOutput(xf86CrtcConfigPtr config)1910*4882a593Smuzhiyun SetCompatOutput(xf86CrtcConfigPtr config)
1911*4882a593Smuzhiyun {
1912*4882a593Smuzhiyun     xf86OutputPtr output = NULL, test = NULL;
1913*4882a593Smuzhiyun     DisplayModePtr maxmode = NULL, testmode, mode;
1914*4882a593Smuzhiyun     int o, compat = -1, count, mincount = 0;
1915*4882a593Smuzhiyun 
1916*4882a593Smuzhiyun     if (config->num_output == 0)
1917*4882a593Smuzhiyun         return NULL;
1918*4882a593Smuzhiyun 
1919*4882a593Smuzhiyun     /* Look for one that's definitely connected */
1920*4882a593Smuzhiyun     for (o = 0; o < config->num_output; o++) {
1921*4882a593Smuzhiyun         test = config->output[o];
1922*4882a593Smuzhiyun         if (!test->crtc)
1923*4882a593Smuzhiyun             continue;
1924*4882a593Smuzhiyun         if (test->status != XF86OutputStatusConnected)
1925*4882a593Smuzhiyun             continue;
1926*4882a593Smuzhiyun         if (!test->probed_modes)
1927*4882a593Smuzhiyun             continue;
1928*4882a593Smuzhiyun 
1929*4882a593Smuzhiyun         testmode = mode = test->probed_modes;
1930*4882a593Smuzhiyun         for (count = 0; mode; mode = mode->next, count++)
1931*4882a593Smuzhiyun             testmode = biggestMode(testmode, mode);
1932*4882a593Smuzhiyun 
1933*4882a593Smuzhiyun         if (!output) {
1934*4882a593Smuzhiyun             output = test;
1935*4882a593Smuzhiyun             compat = o;
1936*4882a593Smuzhiyun             maxmode = testmode;
1937*4882a593Smuzhiyun             mincount = count;
1938*4882a593Smuzhiyun         }
1939*4882a593Smuzhiyun         else if (maxmode == biggestMode(maxmode, testmode)) {
1940*4882a593Smuzhiyun             output = test;
1941*4882a593Smuzhiyun             compat = o;
1942*4882a593Smuzhiyun             maxmode = testmode;
1943*4882a593Smuzhiyun             mincount = count;
1944*4882a593Smuzhiyun         }
1945*4882a593Smuzhiyun         else if ((maxmode->HDisplay == testmode->HDisplay) &&
1946*4882a593Smuzhiyun                  (maxmode->VDisplay == testmode->VDisplay) &&
1947*4882a593Smuzhiyun                  count <= mincount) {
1948*4882a593Smuzhiyun             output = test;
1949*4882a593Smuzhiyun             compat = o;
1950*4882a593Smuzhiyun             maxmode = testmode;
1951*4882a593Smuzhiyun             mincount = count;
1952*4882a593Smuzhiyun         }
1953*4882a593Smuzhiyun     }
1954*4882a593Smuzhiyun 
1955*4882a593Smuzhiyun     /* If we didn't find one, take anything we can get */
1956*4882a593Smuzhiyun     if (!output) {
1957*4882a593Smuzhiyun         for (o = 0; o < config->num_output; o++) {
1958*4882a593Smuzhiyun             test = config->output[o];
1959*4882a593Smuzhiyun             if (!test->crtc)
1960*4882a593Smuzhiyun                 continue;
1961*4882a593Smuzhiyun             if (!test->probed_modes)
1962*4882a593Smuzhiyun                 continue;
1963*4882a593Smuzhiyun 
1964*4882a593Smuzhiyun             if (!output) {
1965*4882a593Smuzhiyun                 output = test;
1966*4882a593Smuzhiyun                 compat = o;
1967*4882a593Smuzhiyun             }
1968*4882a593Smuzhiyun             else if (test->probed_modes->HDisplay <
1969*4882a593Smuzhiyun                      output->probed_modes->HDisplay) {
1970*4882a593Smuzhiyun                 output = test;
1971*4882a593Smuzhiyun                 compat = o;
1972*4882a593Smuzhiyun             }
1973*4882a593Smuzhiyun         }
1974*4882a593Smuzhiyun     }
1975*4882a593Smuzhiyun 
1976*4882a593Smuzhiyun     if (compat >= 0) {
1977*4882a593Smuzhiyun         config->compat_output = compat;
1978*4882a593Smuzhiyun     }
1979*4882a593Smuzhiyun     else if (config->compat_output >= 0 && config->compat_output < config->num_output) {
1980*4882a593Smuzhiyun         /* Don't change the compat output when no valid outputs found */
1981*4882a593Smuzhiyun         output = config->output[config->compat_output];
1982*4882a593Smuzhiyun     }
1983*4882a593Smuzhiyun 
1984*4882a593Smuzhiyun     /* All outputs are disconnected, select one to fake */
1985*4882a593Smuzhiyun     if (!output && config->num_output) {
1986*4882a593Smuzhiyun         config->compat_output = 0;
1987*4882a593Smuzhiyun         output = config->output[config->compat_output];
1988*4882a593Smuzhiyun     }
1989*4882a593Smuzhiyun 
1990*4882a593Smuzhiyun     return output;
1991*4882a593Smuzhiyun }
1992*4882a593Smuzhiyun 
1993*4882a593Smuzhiyun void
xf86SetScrnInfoModes(ScrnInfoPtr scrn)1994*4882a593Smuzhiyun xf86SetScrnInfoModes(ScrnInfoPtr scrn)
1995*4882a593Smuzhiyun {
1996*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
1997*4882a593Smuzhiyun     xf86OutputPtr output;
1998*4882a593Smuzhiyun     xf86CrtcPtr crtc;
1999*4882a593Smuzhiyun     DisplayModePtr last, mode = NULL;
2000*4882a593Smuzhiyun 
2001*4882a593Smuzhiyun     output = SetCompatOutput(config);
2002*4882a593Smuzhiyun 
2003*4882a593Smuzhiyun     if (!output)
2004*4882a593Smuzhiyun         return;                 /* punt */
2005*4882a593Smuzhiyun 
2006*4882a593Smuzhiyun     crtc = output->crtc;
2007*4882a593Smuzhiyun 
2008*4882a593Smuzhiyun     /* Clear any existing modes from scrn->modes */
2009*4882a593Smuzhiyun     while (scrn->modes != NULL)
2010*4882a593Smuzhiyun         xf86DeleteMode(&scrn->modes, scrn->modes);
2011*4882a593Smuzhiyun 
2012*4882a593Smuzhiyun     /* Set scrn->modes to the mode list for the 'compat' output */
2013*4882a593Smuzhiyun     scrn->modes = xf86DuplicateModes(scrn, output->probed_modes);
2014*4882a593Smuzhiyun 
2015*4882a593Smuzhiyun     if (crtc) {
2016*4882a593Smuzhiyun         for (mode = scrn->modes; mode; mode = mode->next)
2017*4882a593Smuzhiyun             if (xf86ModesEqual(mode, &crtc->desiredMode))
2018*4882a593Smuzhiyun                 break;
2019*4882a593Smuzhiyun     }
2020*4882a593Smuzhiyun 
2021*4882a593Smuzhiyun     if (!scrn->modes) {
2022*4882a593Smuzhiyun         scrn->modes = xf86ModesAdd(scrn->modes,
2023*4882a593Smuzhiyun                                    xf86CVTMode(scrn->display->virtualX,
2024*4882a593Smuzhiyun                                                scrn->display->virtualY,
2025*4882a593Smuzhiyun                                                60, 0, 0));
2026*4882a593Smuzhiyun     }
2027*4882a593Smuzhiyun 
2028*4882a593Smuzhiyun     /* For some reason, scrn->modes is circular, unlike the other mode
2029*4882a593Smuzhiyun      * lists.  How great is that?
2030*4882a593Smuzhiyun      */
2031*4882a593Smuzhiyun     for (last = scrn->modes; last && last->next; last = last->next);
2032*4882a593Smuzhiyun     last->next = scrn->modes;
2033*4882a593Smuzhiyun     scrn->modes->prev = last;
2034*4882a593Smuzhiyun     if (mode) {
2035*4882a593Smuzhiyun         while (scrn->modes != mode)
2036*4882a593Smuzhiyun             scrn->modes = scrn->modes->next;
2037*4882a593Smuzhiyun     }
2038*4882a593Smuzhiyun 
2039*4882a593Smuzhiyun     scrn->currentMode = scrn->modes;
2040*4882a593Smuzhiyun #ifdef XFreeXDGA
2041*4882a593Smuzhiyun     if (scrn->pScreen)
2042*4882a593Smuzhiyun         _xf86_di_dga_reinit_internal(scrn->pScreen);
2043*4882a593Smuzhiyun #endif
2044*4882a593Smuzhiyun }
2045*4882a593Smuzhiyun 
2046*4882a593Smuzhiyun static Bool
xf86CollectEnabledOutputs(ScrnInfoPtr scrn,xf86CrtcConfigPtr config,Bool * enabled)2047*4882a593Smuzhiyun xf86CollectEnabledOutputs(ScrnInfoPtr scrn, xf86CrtcConfigPtr config,
2048*4882a593Smuzhiyun                           Bool *enabled)
2049*4882a593Smuzhiyun {
2050*4882a593Smuzhiyun     Bool any_enabled = FALSE;
2051*4882a593Smuzhiyun     int o;
2052*4882a593Smuzhiyun 
2053*4882a593Smuzhiyun     /*
2054*4882a593Smuzhiyun      * Don't bother enabling outputs on GPU screens: a client needs to attach
2055*4882a593Smuzhiyun      * it to a source provider before setting a mode that scans out a shared
2056*4882a593Smuzhiyun      * pixmap.
2057*4882a593Smuzhiyun      */
2058*4882a593Smuzhiyun     if (scrn->is_gpu)
2059*4882a593Smuzhiyun         return FALSE;
2060*4882a593Smuzhiyun 
2061*4882a593Smuzhiyun     for (o = 0; o < config->num_output; o++)
2062*4882a593Smuzhiyun         any_enabled |= enabled[o] = xf86OutputEnabled(config->output[o], TRUE);
2063*4882a593Smuzhiyun 
2064*4882a593Smuzhiyun     if (!any_enabled) {
2065*4882a593Smuzhiyun         xf86DrvMsg(scrn->scrnIndex, X_WARNING,
2066*4882a593Smuzhiyun                    "No outputs definitely connected, trying again...\n");
2067*4882a593Smuzhiyun 
2068*4882a593Smuzhiyun         for (o = 0; o < config->num_output; o++)
2069*4882a593Smuzhiyun             any_enabled |= enabled[o] =
2070*4882a593Smuzhiyun                 xf86OutputEnabled(config->output[o], FALSE);
2071*4882a593Smuzhiyun     }
2072*4882a593Smuzhiyun 
2073*4882a593Smuzhiyun     return any_enabled;
2074*4882a593Smuzhiyun }
2075*4882a593Smuzhiyun 
2076*4882a593Smuzhiyun static Bool
nextEnabledOutput(xf86CrtcConfigPtr config,Bool * enabled,int * index)2077*4882a593Smuzhiyun nextEnabledOutput(xf86CrtcConfigPtr config, Bool *enabled, int *index)
2078*4882a593Smuzhiyun {
2079*4882a593Smuzhiyun     int o = *index;
2080*4882a593Smuzhiyun 
2081*4882a593Smuzhiyun     for (o++; o < config->num_output; o++) {
2082*4882a593Smuzhiyun         if (enabled[o]) {
2083*4882a593Smuzhiyun             *index = o;
2084*4882a593Smuzhiyun             return TRUE;
2085*4882a593Smuzhiyun         }
2086*4882a593Smuzhiyun     }
2087*4882a593Smuzhiyun 
2088*4882a593Smuzhiyun     return FALSE;
2089*4882a593Smuzhiyun }
2090*4882a593Smuzhiyun 
2091*4882a593Smuzhiyun static Bool
aspectMatch(float a,float b)2092*4882a593Smuzhiyun aspectMatch(float a, float b)
2093*4882a593Smuzhiyun {
2094*4882a593Smuzhiyun     return fabs(1 - (a / b)) < 0.05;
2095*4882a593Smuzhiyun }
2096*4882a593Smuzhiyun 
2097*4882a593Smuzhiyun static DisplayModePtr
nextAspectMode(xf86OutputPtr o,DisplayModePtr last,float aspect)2098*4882a593Smuzhiyun nextAspectMode(xf86OutputPtr o, DisplayModePtr last, float aspect)
2099*4882a593Smuzhiyun {
2100*4882a593Smuzhiyun     DisplayModePtr m = NULL;
2101*4882a593Smuzhiyun 
2102*4882a593Smuzhiyun     if (!o)
2103*4882a593Smuzhiyun         return NULL;
2104*4882a593Smuzhiyun 
2105*4882a593Smuzhiyun     if (!last)
2106*4882a593Smuzhiyun         m = o->probed_modes;
2107*4882a593Smuzhiyun     else
2108*4882a593Smuzhiyun         m = last->next;
2109*4882a593Smuzhiyun 
2110*4882a593Smuzhiyun     for (; m; m = m->next)
2111*4882a593Smuzhiyun         if (aspectMatch(aspect, (float) m->HDisplay / (float) m->VDisplay))
2112*4882a593Smuzhiyun             return m;
2113*4882a593Smuzhiyun 
2114*4882a593Smuzhiyun     return NULL;
2115*4882a593Smuzhiyun }
2116*4882a593Smuzhiyun 
2117*4882a593Smuzhiyun static DisplayModePtr
bestModeForAspect(xf86CrtcConfigPtr config,Bool * enabled,float aspect)2118*4882a593Smuzhiyun bestModeForAspect(xf86CrtcConfigPtr config, Bool *enabled, float aspect)
2119*4882a593Smuzhiyun {
2120*4882a593Smuzhiyun     int o = -1, p;
2121*4882a593Smuzhiyun     DisplayModePtr mode = NULL, test = NULL, match = NULL;
2122*4882a593Smuzhiyun 
2123*4882a593Smuzhiyun     if (!nextEnabledOutput(config, enabled, &o))
2124*4882a593Smuzhiyun         return NULL;
2125*4882a593Smuzhiyun     while ((mode = nextAspectMode(config->output[o], mode, aspect))) {
2126*4882a593Smuzhiyun         test = mode;
2127*4882a593Smuzhiyun         for (p = o; nextEnabledOutput(config, enabled, &p);) {
2128*4882a593Smuzhiyun             test = xf86OutputFindClosestMode(config->output[p], mode);
2129*4882a593Smuzhiyun             if (!test)
2130*4882a593Smuzhiyun                 break;
2131*4882a593Smuzhiyun             if (test->HDisplay != mode->HDisplay ||
2132*4882a593Smuzhiyun                 test->VDisplay != mode->VDisplay) {
2133*4882a593Smuzhiyun                 test = NULL;
2134*4882a593Smuzhiyun                 break;
2135*4882a593Smuzhiyun             }
2136*4882a593Smuzhiyun         }
2137*4882a593Smuzhiyun 
2138*4882a593Smuzhiyun         /* if we didn't match it on all outputs, try the next one */
2139*4882a593Smuzhiyun         if (!test)
2140*4882a593Smuzhiyun             continue;
2141*4882a593Smuzhiyun 
2142*4882a593Smuzhiyun         /* if it's bigger than the last one, save it */
2143*4882a593Smuzhiyun         if (!match || (test->HDisplay > match->HDisplay))
2144*4882a593Smuzhiyun             match = test;
2145*4882a593Smuzhiyun     }
2146*4882a593Smuzhiyun 
2147*4882a593Smuzhiyun     /* return the biggest one found */
2148*4882a593Smuzhiyun     return match;
2149*4882a593Smuzhiyun }
2150*4882a593Smuzhiyun 
2151*4882a593Smuzhiyun static int
numEnabledOutputs(xf86CrtcConfigPtr config,Bool * enabled)2152*4882a593Smuzhiyun numEnabledOutputs(xf86CrtcConfigPtr config, Bool *enabled)
2153*4882a593Smuzhiyun {
2154*4882a593Smuzhiyun     int i = 0, p;
2155*4882a593Smuzhiyun 
2156*4882a593Smuzhiyun     for (i = 0, p = -1; nextEnabledOutput(config, enabled, &p); i++) ;
2157*4882a593Smuzhiyun 
2158*4882a593Smuzhiyun     return i;
2159*4882a593Smuzhiyun }
2160*4882a593Smuzhiyun 
2161*4882a593Smuzhiyun static Bool
xf86TargetRightOf(ScrnInfoPtr scrn,xf86CrtcConfigPtr config,DisplayModePtr * modes,Bool * enabled,int width,int height)2162*4882a593Smuzhiyun xf86TargetRightOf(ScrnInfoPtr scrn, xf86CrtcConfigPtr config,
2163*4882a593Smuzhiyun                   DisplayModePtr *modes, Bool *enabled,
2164*4882a593Smuzhiyun                   int width, int height)
2165*4882a593Smuzhiyun {
2166*4882a593Smuzhiyun     int o;
2167*4882a593Smuzhiyun     int w = 0;
2168*4882a593Smuzhiyun     Bool has_tile = FALSE;
2169*4882a593Smuzhiyun     uint32_t configured_outputs;
2170*4882a593Smuzhiyun 
2171*4882a593Smuzhiyun     xf86GetOptValBool(config->options, OPTION_PREFER_CLONEMODE,
2172*4882a593Smuzhiyun                       &scrn->preferClone);
2173*4882a593Smuzhiyun     if (scrn->preferClone)
2174*4882a593Smuzhiyun         return FALSE;
2175*4882a593Smuzhiyun 
2176*4882a593Smuzhiyun     if (numEnabledOutputs(config, enabled) < 2)
2177*4882a593Smuzhiyun         return FALSE;
2178*4882a593Smuzhiyun 
2179*4882a593Smuzhiyun     for (o = -1; nextEnabledOutput(config, enabled, &o); ) {
2180*4882a593Smuzhiyun         DisplayModePtr mode =
2181*4882a593Smuzhiyun             xf86OutputHasPreferredMode(config->output[o], width, height);
2182*4882a593Smuzhiyun 
2183*4882a593Smuzhiyun         if (!mode)
2184*4882a593Smuzhiyun             return FALSE;
2185*4882a593Smuzhiyun 
2186*4882a593Smuzhiyun         w += mode->HDisplay;
2187*4882a593Smuzhiyun     }
2188*4882a593Smuzhiyun 
2189*4882a593Smuzhiyun     if (w > width)
2190*4882a593Smuzhiyun         return FALSE;
2191*4882a593Smuzhiyun 
2192*4882a593Smuzhiyun     w = 0;
2193*4882a593Smuzhiyun     configured_outputs = 0;
2194*4882a593Smuzhiyun 
2195*4882a593Smuzhiyun     for (o = -1; nextEnabledOutput(config, enabled, &o); ) {
2196*4882a593Smuzhiyun         DisplayModePtr mode =
2197*4882a593Smuzhiyun             xf86OutputHasPreferredMode(config->output[o], width, height);
2198*4882a593Smuzhiyun 
2199*4882a593Smuzhiyun         if (configured_outputs & (1 << o))
2200*4882a593Smuzhiyun             continue;
2201*4882a593Smuzhiyun 
2202*4882a593Smuzhiyun         if (config->output[o]->tile_info.group_id) {
2203*4882a593Smuzhiyun             has_tile = TRUE;
2204*4882a593Smuzhiyun             continue;
2205*4882a593Smuzhiyun         }
2206*4882a593Smuzhiyun 
2207*4882a593Smuzhiyun         config->output[o]->initial_x = w;
2208*4882a593Smuzhiyun         w += mode->HDisplay;
2209*4882a593Smuzhiyun 
2210*4882a593Smuzhiyun         configured_outputs |= (1 << o);
2211*4882a593Smuzhiyun         modes[o] = mode;
2212*4882a593Smuzhiyun     }
2213*4882a593Smuzhiyun 
2214*4882a593Smuzhiyun     if (has_tile) {
2215*4882a593Smuzhiyun         for (o = -1; nextEnabledOutput(config, enabled, &o); ) {
2216*4882a593Smuzhiyun             int ht, vt, ot;
2217*4882a593Smuzhiyun             int add_x, cur_x = w;
2218*4882a593Smuzhiyun             struct xf86CrtcTileInfo *tile_info = &config->output[o]->tile_info, *this_tile;
2219*4882a593Smuzhiyun             if (configured_outputs & (1 << o))
2220*4882a593Smuzhiyun                 continue;
2221*4882a593Smuzhiyun             if (!tile_info->group_id)
2222*4882a593Smuzhiyun                 continue;
2223*4882a593Smuzhiyun 
2224*4882a593Smuzhiyun             if (tile_info->tile_h_loc != 0 && tile_info->tile_v_loc != 0)
2225*4882a593Smuzhiyun                 continue;
2226*4882a593Smuzhiyun 
2227*4882a593Smuzhiyun             for (ht = 0; ht < tile_info->num_h_tile; ht++) {
2228*4882a593Smuzhiyun                 int cur_y = 0;
2229*4882a593Smuzhiyun                 add_x = 0;
2230*4882a593Smuzhiyun                 for (vt = 0; vt < tile_info->num_v_tile; vt++) {
2231*4882a593Smuzhiyun 
2232*4882a593Smuzhiyun                     for (ot = -1; nextEnabledOutput(config, enabled, &ot); ) {
2233*4882a593Smuzhiyun 
2234*4882a593Smuzhiyun                         DisplayModePtr mode =
2235*4882a593Smuzhiyun                             xf86OutputHasPreferredMode(config->output[ot], width, height);
2236*4882a593Smuzhiyun                         if (!config->output[ot]->tile_info.group_id)
2237*4882a593Smuzhiyun                             continue;
2238*4882a593Smuzhiyun 
2239*4882a593Smuzhiyun                         this_tile = &config->output[ot]->tile_info;
2240*4882a593Smuzhiyun                         if (this_tile->group_id != tile_info->group_id)
2241*4882a593Smuzhiyun                             continue;
2242*4882a593Smuzhiyun 
2243*4882a593Smuzhiyun                         if (this_tile->tile_h_loc != ht ||
2244*4882a593Smuzhiyun                             this_tile->tile_v_loc != vt)
2245*4882a593Smuzhiyun                             continue;
2246*4882a593Smuzhiyun 
2247*4882a593Smuzhiyun                         config->output[ot]->initial_x = cur_x;
2248*4882a593Smuzhiyun                         config->output[ot]->initial_y = cur_y;
2249*4882a593Smuzhiyun 
2250*4882a593Smuzhiyun                         if (vt == 0)
2251*4882a593Smuzhiyun                             add_x = this_tile->tile_h_size;
2252*4882a593Smuzhiyun                         cur_y += this_tile->tile_v_size;
2253*4882a593Smuzhiyun                         configured_outputs |= (1 << ot);
2254*4882a593Smuzhiyun                         modes[ot] = mode;
2255*4882a593Smuzhiyun                     }
2256*4882a593Smuzhiyun                 }
2257*4882a593Smuzhiyun                 cur_x += add_x;
2258*4882a593Smuzhiyun             }
2259*4882a593Smuzhiyun             w = cur_x;
2260*4882a593Smuzhiyun         }
2261*4882a593Smuzhiyun     }
2262*4882a593Smuzhiyun     return TRUE;
2263*4882a593Smuzhiyun }
2264*4882a593Smuzhiyun 
2265*4882a593Smuzhiyun static Bool
xf86TargetPreferred(ScrnInfoPtr scrn,xf86CrtcConfigPtr config,DisplayModePtr * modes,Bool * enabled,int width,int height)2266*4882a593Smuzhiyun xf86TargetPreferred(ScrnInfoPtr scrn, xf86CrtcConfigPtr config,
2267*4882a593Smuzhiyun                     DisplayModePtr * modes, Bool *enabled,
2268*4882a593Smuzhiyun                     int width, int height)
2269*4882a593Smuzhiyun {
2270*4882a593Smuzhiyun     int o, p;
2271*4882a593Smuzhiyun     int max_pref_width = 0, max_pref_height = 0;
2272*4882a593Smuzhiyun     DisplayModePtr *preferred, *preferred_match;
2273*4882a593Smuzhiyun     Bool ret = FALSE;
2274*4882a593Smuzhiyun 
2275*4882a593Smuzhiyun     preferred = xnfcalloc(config->num_output, sizeof(DisplayModePtr));
2276*4882a593Smuzhiyun     preferred_match = xnfcalloc(config->num_output, sizeof(DisplayModePtr));
2277*4882a593Smuzhiyun 
2278*4882a593Smuzhiyun     /* Check if the preferred mode is available on all outputs */
2279*4882a593Smuzhiyun     for (p = -1; nextEnabledOutput(config, enabled, &p);) {
2280*4882a593Smuzhiyun         Rotation r = config->output[p]->initial_rotation;
2281*4882a593Smuzhiyun         DisplayModePtr mode;
2282*4882a593Smuzhiyun 
2283*4882a593Smuzhiyun         if ((preferred[p] = xf86OutputHasPreferredMode(config->output[p],
2284*4882a593Smuzhiyun                                                        width, height))) {
2285*4882a593Smuzhiyun             int pref_width = xf86ModeWidth(preferred[p], r);
2286*4882a593Smuzhiyun             int pref_height = xf86ModeHeight(preferred[p], r);
2287*4882a593Smuzhiyun             Bool all_match = TRUE;
2288*4882a593Smuzhiyun 
2289*4882a593Smuzhiyun             for (o = -1; nextEnabledOutput(config, enabled, &o);) {
2290*4882a593Smuzhiyun                 Bool match = FALSE;
2291*4882a593Smuzhiyun                 xf86OutputPtr output = config->output[o];
2292*4882a593Smuzhiyun 
2293*4882a593Smuzhiyun                 if (o == p)
2294*4882a593Smuzhiyun                     continue;
2295*4882a593Smuzhiyun 
2296*4882a593Smuzhiyun                 /*
2297*4882a593Smuzhiyun                  * First see if the preferred mode matches on the next
2298*4882a593Smuzhiyun                  * output as well.  This catches the common case of identical
2299*4882a593Smuzhiyun                  * monitors and makes sure they all have the same timings
2300*4882a593Smuzhiyun                  * and refresh.  If that fails, we fall back to trying to
2301*4882a593Smuzhiyun                  * match just width & height.
2302*4882a593Smuzhiyun                  */
2303*4882a593Smuzhiyun                 mode = xf86OutputHasPreferredMode(output, pref_width,
2304*4882a593Smuzhiyun                                                   pref_height);
2305*4882a593Smuzhiyun                 if (mode && xf86ModesEqual(mode, preferred[p])) {
2306*4882a593Smuzhiyun                     preferred[o] = mode;
2307*4882a593Smuzhiyun                     match = TRUE;
2308*4882a593Smuzhiyun                 }
2309*4882a593Smuzhiyun                 else {
2310*4882a593Smuzhiyun                     for (mode = output->probed_modes; mode; mode = mode->next) {
2311*4882a593Smuzhiyun                         Rotation ir = output->initial_rotation;
2312*4882a593Smuzhiyun 
2313*4882a593Smuzhiyun                         if (xf86ModeWidth(mode, ir) == pref_width &&
2314*4882a593Smuzhiyun                             xf86ModeHeight(mode, ir) == pref_height) {
2315*4882a593Smuzhiyun                             preferred[o] = mode;
2316*4882a593Smuzhiyun                             match = TRUE;
2317*4882a593Smuzhiyun                         }
2318*4882a593Smuzhiyun                     }
2319*4882a593Smuzhiyun                 }
2320*4882a593Smuzhiyun 
2321*4882a593Smuzhiyun                 all_match &= match;
2322*4882a593Smuzhiyun             }
2323*4882a593Smuzhiyun 
2324*4882a593Smuzhiyun             if (all_match &&
2325*4882a593Smuzhiyun                 (pref_width * pref_height > max_pref_width * max_pref_height)) {
2326*4882a593Smuzhiyun                 for (o = -1; nextEnabledOutput(config, enabled, &o);)
2327*4882a593Smuzhiyun                     preferred_match[o] = preferred[o];
2328*4882a593Smuzhiyun                 max_pref_width = pref_width;
2329*4882a593Smuzhiyun                 max_pref_height = pref_height;
2330*4882a593Smuzhiyun                 ret = TRUE;
2331*4882a593Smuzhiyun             }
2332*4882a593Smuzhiyun         }
2333*4882a593Smuzhiyun     }
2334*4882a593Smuzhiyun 
2335*4882a593Smuzhiyun     /*
2336*4882a593Smuzhiyun      * If there's no preferred mode, but only one monitor, pick the
2337*4882a593Smuzhiyun      * biggest mode for its aspect ratio or 4:3, assuming one exists.
2338*4882a593Smuzhiyun      */
2339*4882a593Smuzhiyun     if (!ret)
2340*4882a593Smuzhiyun         do {
2341*4882a593Smuzhiyun             float aspect = 0.0;
2342*4882a593Smuzhiyun             DisplayModePtr a = NULL, b = NULL;
2343*4882a593Smuzhiyun 
2344*4882a593Smuzhiyun             if (numEnabledOutputs(config, enabled) != 1)
2345*4882a593Smuzhiyun                 break;
2346*4882a593Smuzhiyun 
2347*4882a593Smuzhiyun             p = -1;
2348*4882a593Smuzhiyun             nextEnabledOutput(config, enabled, &p);
2349*4882a593Smuzhiyun             if (config->output[p]->mm_height)
2350*4882a593Smuzhiyun                 aspect = (float) config->output[p]->mm_width /
2351*4882a593Smuzhiyun                     (float) config->output[p]->mm_height;
2352*4882a593Smuzhiyun 
2353*4882a593Smuzhiyun             a = bestModeForAspect(config, enabled, 4.0/3.0);
2354*4882a593Smuzhiyun             if (aspect)
2355*4882a593Smuzhiyun                 b = bestModeForAspect(config, enabled, aspect);
2356*4882a593Smuzhiyun 
2357*4882a593Smuzhiyun             preferred_match[p] = biggestMode(a, b);
2358*4882a593Smuzhiyun 
2359*4882a593Smuzhiyun             if (preferred_match[p])
2360*4882a593Smuzhiyun                 ret = TRUE;
2361*4882a593Smuzhiyun 
2362*4882a593Smuzhiyun         } while (0);
2363*4882a593Smuzhiyun 
2364*4882a593Smuzhiyun     if (ret) {
2365*4882a593Smuzhiyun         /* oh good, there is a match.  stash the selected modes and return. */
2366*4882a593Smuzhiyun         memcpy(modes, preferred_match,
2367*4882a593Smuzhiyun                config->num_output * sizeof(DisplayModePtr));
2368*4882a593Smuzhiyun     }
2369*4882a593Smuzhiyun 
2370*4882a593Smuzhiyun     free(preferred);
2371*4882a593Smuzhiyun     free(preferred_match);
2372*4882a593Smuzhiyun     return ret;
2373*4882a593Smuzhiyun }
2374*4882a593Smuzhiyun 
2375*4882a593Smuzhiyun static Bool
xf86TargetAspect(ScrnInfoPtr scrn,xf86CrtcConfigPtr config,DisplayModePtr * modes,Bool * enabled,int width,int height)2376*4882a593Smuzhiyun xf86TargetAspect(ScrnInfoPtr scrn, xf86CrtcConfigPtr config,
2377*4882a593Smuzhiyun                  DisplayModePtr * modes, Bool *enabled, int width, int height)
2378*4882a593Smuzhiyun {
2379*4882a593Smuzhiyun     int o;
2380*4882a593Smuzhiyun     float aspect = 0.0, *aspects;
2381*4882a593Smuzhiyun     xf86OutputPtr output;
2382*4882a593Smuzhiyun     Bool ret = FALSE;
2383*4882a593Smuzhiyun     DisplayModePtr guess = NULL, aspect_guess = NULL, base_guess = NULL;
2384*4882a593Smuzhiyun 
2385*4882a593Smuzhiyun     aspects = xnfcalloc(config->num_output, sizeof(float));
2386*4882a593Smuzhiyun 
2387*4882a593Smuzhiyun     /* collect the aspect ratios */
2388*4882a593Smuzhiyun     for (o = -1; nextEnabledOutput(config, enabled, &o);) {
2389*4882a593Smuzhiyun         output = config->output[o];
2390*4882a593Smuzhiyun         if (output->mm_height)
2391*4882a593Smuzhiyun             aspects[o] = (float) output->mm_width / (float) output->mm_height;
2392*4882a593Smuzhiyun         else
2393*4882a593Smuzhiyun             aspects[o] = 4.0 / 3.0;
2394*4882a593Smuzhiyun     }
2395*4882a593Smuzhiyun 
2396*4882a593Smuzhiyun     /* check that they're all the same */
2397*4882a593Smuzhiyun     for (o = -1; nextEnabledOutput(config, enabled, &o);) {
2398*4882a593Smuzhiyun         output = config->output[o];
2399*4882a593Smuzhiyun         if (!aspect) {
2400*4882a593Smuzhiyun             aspect = aspects[o];
2401*4882a593Smuzhiyun         }
2402*4882a593Smuzhiyun         else if (!aspectMatch(aspect, aspects[o])) {
2403*4882a593Smuzhiyun             goto no_aspect_match;
2404*4882a593Smuzhiyun         }
2405*4882a593Smuzhiyun     }
2406*4882a593Smuzhiyun 
2407*4882a593Smuzhiyun     /* if they're all 4:3, just skip ahead and save effort */
2408*4882a593Smuzhiyun     if (!aspectMatch(aspect, 4.0 / 3.0))
2409*4882a593Smuzhiyun         aspect_guess = bestModeForAspect(config, enabled, aspect);
2410*4882a593Smuzhiyun 
2411*4882a593Smuzhiyun  no_aspect_match:
2412*4882a593Smuzhiyun     base_guess = bestModeForAspect(config, enabled, 4.0 / 3.0);
2413*4882a593Smuzhiyun 
2414*4882a593Smuzhiyun     guess = biggestMode(base_guess, aspect_guess);
2415*4882a593Smuzhiyun 
2416*4882a593Smuzhiyun     if (!guess)
2417*4882a593Smuzhiyun         goto out;
2418*4882a593Smuzhiyun 
2419*4882a593Smuzhiyun     /* found a mode that works everywhere, now apply it */
2420*4882a593Smuzhiyun     for (o = -1; nextEnabledOutput(config, enabled, &o);) {
2421*4882a593Smuzhiyun         modes[o] = xf86OutputFindClosestMode(config->output[o], guess);
2422*4882a593Smuzhiyun     }
2423*4882a593Smuzhiyun     ret = TRUE;
2424*4882a593Smuzhiyun 
2425*4882a593Smuzhiyun  out:
2426*4882a593Smuzhiyun     free(aspects);
2427*4882a593Smuzhiyun     return ret;
2428*4882a593Smuzhiyun }
2429*4882a593Smuzhiyun 
2430*4882a593Smuzhiyun static Bool
xf86TargetFallback(ScrnInfoPtr scrn,xf86CrtcConfigPtr config,DisplayModePtr * modes,Bool * enabled,int width,int height)2431*4882a593Smuzhiyun xf86TargetFallback(ScrnInfoPtr scrn, xf86CrtcConfigPtr config,
2432*4882a593Smuzhiyun                    DisplayModePtr * modes, Bool *enabled, int width, int height)
2433*4882a593Smuzhiyun {
2434*4882a593Smuzhiyun     DisplayModePtr target_mode = NULL;
2435*4882a593Smuzhiyun     Rotation target_rotation = RR_Rotate_0;
2436*4882a593Smuzhiyun     DisplayModePtr default_mode;
2437*4882a593Smuzhiyun     int default_preferred, target_preferred = 0, o;
2438*4882a593Smuzhiyun 
2439*4882a593Smuzhiyun     /* User preferred > preferred > other modes */
2440*4882a593Smuzhiyun     for (o = -1; nextEnabledOutput(config, enabled, &o);) {
2441*4882a593Smuzhiyun         default_mode = xf86DefaultMode(config->output[o], width, height);
2442*4882a593Smuzhiyun         if (!default_mode)
2443*4882a593Smuzhiyun             continue;
2444*4882a593Smuzhiyun 
2445*4882a593Smuzhiyun         default_preferred = (((default_mode->type & M_T_PREFERRED) != 0) +
2446*4882a593Smuzhiyun                              ((default_mode->type & M_T_USERPREF) != 0));
2447*4882a593Smuzhiyun 
2448*4882a593Smuzhiyun         if (default_preferred > target_preferred || !target_mode) {
2449*4882a593Smuzhiyun             target_mode = default_mode;
2450*4882a593Smuzhiyun             target_preferred = default_preferred;
2451*4882a593Smuzhiyun             target_rotation = config->output[o]->initial_rotation;
2452*4882a593Smuzhiyun             config->compat_output = o;
2453*4882a593Smuzhiyun         }
2454*4882a593Smuzhiyun     }
2455*4882a593Smuzhiyun 
2456*4882a593Smuzhiyun     if (target_mode)
2457*4882a593Smuzhiyun         modes[config->compat_output] = target_mode;
2458*4882a593Smuzhiyun 
2459*4882a593Smuzhiyun     /* Fill in other output modes */
2460*4882a593Smuzhiyun     for (o = -1; nextEnabledOutput(config, enabled, &o);) {
2461*4882a593Smuzhiyun         if (!modes[o])
2462*4882a593Smuzhiyun             modes[o] = xf86ClosestMode(config->output[o], target_mode,
2463*4882a593Smuzhiyun                                        target_rotation, width, height);
2464*4882a593Smuzhiyun     }
2465*4882a593Smuzhiyun 
2466*4882a593Smuzhiyun     return target_mode != NULL;
2467*4882a593Smuzhiyun }
2468*4882a593Smuzhiyun 
2469*4882a593Smuzhiyun static Bool
xf86TargetUserpref(ScrnInfoPtr scrn,xf86CrtcConfigPtr config,DisplayModePtr * modes,Bool * enabled,int width,int height)2470*4882a593Smuzhiyun xf86TargetUserpref(ScrnInfoPtr scrn, xf86CrtcConfigPtr config,
2471*4882a593Smuzhiyun                    DisplayModePtr * modes, Bool *enabled, int width, int height)
2472*4882a593Smuzhiyun {
2473*4882a593Smuzhiyun     int o;
2474*4882a593Smuzhiyun 
2475*4882a593Smuzhiyun     if (xf86UserConfiguredOutputs(scrn, modes))
2476*4882a593Smuzhiyun         return xf86TargetFallback(scrn, config, modes, enabled, width, height);
2477*4882a593Smuzhiyun 
2478*4882a593Smuzhiyun     for (o = -1; nextEnabledOutput(config, enabled, &o);)
2479*4882a593Smuzhiyun         if (xf86OutputHasUserPreferredMode(config->output[o]))
2480*4882a593Smuzhiyun             return
2481*4882a593Smuzhiyun                 xf86TargetFallback(scrn, config, modes, enabled, width, height);
2482*4882a593Smuzhiyun 
2483*4882a593Smuzhiyun     return FALSE;
2484*4882a593Smuzhiyun }
2485*4882a593Smuzhiyun 
2486*4882a593Smuzhiyun /**
2487*4882a593Smuzhiyun  * Construct default screen configuration
2488*4882a593Smuzhiyun  *
2489*4882a593Smuzhiyun  * Given auto-detected (and, eventually, configured) values,
2490*4882a593Smuzhiyun  * construct a usable configuration for the system
2491*4882a593Smuzhiyun  *
2492*4882a593Smuzhiyun  * canGrow indicates that the driver can resize the screen to larger than its
2493*4882a593Smuzhiyun  * initially configured size via the config->funcs->resize hook.  If TRUE, this
2494*4882a593Smuzhiyun  * function will set virtualX and virtualY to match the initial configuration
2495*4882a593Smuzhiyun  * and leave config->max{Width,Height} alone.  If FALSE, it will bloat
2496*4882a593Smuzhiyun  * virtual[XY] to include the largest modes and set config->max{Width,Height}
2497*4882a593Smuzhiyun  * accordingly.
2498*4882a593Smuzhiyun  */
2499*4882a593Smuzhiyun 
2500*4882a593Smuzhiyun Bool
xf86InitialConfiguration(ScrnInfoPtr scrn,Bool canGrow)2501*4882a593Smuzhiyun xf86InitialConfiguration(ScrnInfoPtr scrn, Bool canGrow)
2502*4882a593Smuzhiyun {
2503*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
2504*4882a593Smuzhiyun     int o, c;
2505*4882a593Smuzhiyun     xf86CrtcPtr *crtcs;
2506*4882a593Smuzhiyun     DisplayModePtr *modes;
2507*4882a593Smuzhiyun     Bool *enabled;
2508*4882a593Smuzhiyun     int width, height;
2509*4882a593Smuzhiyun     int i = scrn->scrnIndex;
2510*4882a593Smuzhiyun     Bool have_outputs = TRUE;
2511*4882a593Smuzhiyun     Bool ret;
2512*4882a593Smuzhiyun     Bool success = FALSE;
2513*4882a593Smuzhiyun 
2514*4882a593Smuzhiyun     /* Set up the device options */
2515*4882a593Smuzhiyun     config->options = xnfalloc(sizeof(xf86DeviceOptions));
2516*4882a593Smuzhiyun     memcpy(config->options, xf86DeviceOptions, sizeof(xf86DeviceOptions));
2517*4882a593Smuzhiyun     xf86ProcessOptions(scrn->scrnIndex, scrn->options, config->options);
2518*4882a593Smuzhiyun     config->debug_modes = xf86ReturnOptValBool(config->options,
2519*4882a593Smuzhiyun                                                OPTION_MODEDEBUG, FALSE);
2520*4882a593Smuzhiyun 
2521*4882a593Smuzhiyun     if (scrn->display->virtualX && !scrn->is_gpu)
2522*4882a593Smuzhiyun         width = scrn->display->virtualX;
2523*4882a593Smuzhiyun     else
2524*4882a593Smuzhiyun         width = config->maxWidth;
2525*4882a593Smuzhiyun     if (scrn->display->virtualY && !scrn->is_gpu)
2526*4882a593Smuzhiyun         height = scrn->display->virtualY;
2527*4882a593Smuzhiyun     else
2528*4882a593Smuzhiyun         height = config->maxHeight;
2529*4882a593Smuzhiyun 
2530*4882a593Smuzhiyun     xf86ProbeOutputModes(scrn, width, height);
2531*4882a593Smuzhiyun 
2532*4882a593Smuzhiyun     crtcs = xnfcalloc(config->num_output, sizeof(xf86CrtcPtr));
2533*4882a593Smuzhiyun     modes = xnfcalloc(config->num_output, sizeof(DisplayModePtr));
2534*4882a593Smuzhiyun     enabled = xnfcalloc(config->num_output, sizeof(Bool));
2535*4882a593Smuzhiyun 
2536*4882a593Smuzhiyun     ret = xf86CollectEnabledOutputs(scrn, config, enabled);
2537*4882a593Smuzhiyun     if (ret == FALSE && canGrow) {
2538*4882a593Smuzhiyun         if (!scrn->is_gpu)
2539*4882a593Smuzhiyun             xf86DrvMsg(i, X_WARNING,
2540*4882a593Smuzhiyun 		       "Unable to find connected outputs - setting %dx%d "
2541*4882a593Smuzhiyun                        "initial framebuffer\n",
2542*4882a593Smuzhiyun                        NO_OUTPUT_DEFAULT_WIDTH, NO_OUTPUT_DEFAULT_HEIGHT);
2543*4882a593Smuzhiyun         have_outputs = FALSE;
2544*4882a593Smuzhiyun     }
2545*4882a593Smuzhiyun     else {
2546*4882a593Smuzhiyun         if (xf86TargetUserpref(scrn, config, modes, enabled, width, height))
2547*4882a593Smuzhiyun             xf86DrvMsg(i, X_INFO, "Using user preference for initial modes\n");
2548*4882a593Smuzhiyun         else if (xf86TargetRightOf(scrn, config, modes, enabled, width, height))
2549*4882a593Smuzhiyun             xf86DrvMsg(i, X_INFO, "Using spanning desktop for initial modes\n");
2550*4882a593Smuzhiyun         else if (xf86TargetPreferred
2551*4882a593Smuzhiyun                  (scrn, config, modes, enabled, width, height))
2552*4882a593Smuzhiyun             xf86DrvMsg(i, X_INFO, "Using exact sizes for initial modes\n");
2553*4882a593Smuzhiyun         else if (xf86TargetAspect(scrn, config, modes, enabled, width, height))
2554*4882a593Smuzhiyun             xf86DrvMsg(i, X_INFO,
2555*4882a593Smuzhiyun                        "Using fuzzy aspect match for initial modes\n");
2556*4882a593Smuzhiyun         else if (xf86TargetFallback
2557*4882a593Smuzhiyun                  (scrn, config, modes, enabled, width, height))
2558*4882a593Smuzhiyun             xf86DrvMsg(i, X_INFO, "Using sloppy heuristic for initial modes\n");
2559*4882a593Smuzhiyun         else
2560*4882a593Smuzhiyun             xf86DrvMsg(i, X_WARNING, "Unable to find initial modes\n");
2561*4882a593Smuzhiyun     }
2562*4882a593Smuzhiyun 
2563*4882a593Smuzhiyun     for (o = -1; nextEnabledOutput(config, enabled, &o);) {
2564*4882a593Smuzhiyun         if (!modes[o])
2565*4882a593Smuzhiyun             xf86DrvMsg(scrn->scrnIndex, X_ERROR,
2566*4882a593Smuzhiyun                        "Output %s enabled but has no modes\n",
2567*4882a593Smuzhiyun                        config->output[o]->name);
2568*4882a593Smuzhiyun         else
2569*4882a593Smuzhiyun             xf86DrvMsg (scrn->scrnIndex, X_INFO,
2570*4882a593Smuzhiyun                         "Output %s using initial mode %s +%d+%d\n",
2571*4882a593Smuzhiyun                         config->output[o]->name, modes[o]->name,
2572*4882a593Smuzhiyun                         config->output[o]->initial_x,
2573*4882a593Smuzhiyun                         config->output[o]->initial_y);
2574*4882a593Smuzhiyun     }
2575*4882a593Smuzhiyun 
2576*4882a593Smuzhiyun     /*
2577*4882a593Smuzhiyun      * Set the position of each output
2578*4882a593Smuzhiyun      */
2579*4882a593Smuzhiyun     if (!xf86InitialOutputPositions(scrn, modes))
2580*4882a593Smuzhiyun         goto bailout;
2581*4882a593Smuzhiyun 
2582*4882a593Smuzhiyun     /*
2583*4882a593Smuzhiyun      * Set initial panning of each output
2584*4882a593Smuzhiyun      */
2585*4882a593Smuzhiyun     xf86InitialPanning(scrn);
2586*4882a593Smuzhiyun 
2587*4882a593Smuzhiyun     /*
2588*4882a593Smuzhiyun      * Assign CRTCs to fit output configuration
2589*4882a593Smuzhiyun      */
2590*4882a593Smuzhiyun     if (have_outputs && !xf86PickCrtcs(scrn, crtcs, modes, 0, width, height))
2591*4882a593Smuzhiyun         goto bailout;
2592*4882a593Smuzhiyun 
2593*4882a593Smuzhiyun     /* XXX override xf86 common frame computation code */
2594*4882a593Smuzhiyun 
2595*4882a593Smuzhiyun     if (!scrn->is_gpu) {
2596*4882a593Smuzhiyun         scrn->display->frameX0 = 0;
2597*4882a593Smuzhiyun         scrn->display->frameY0 = 0;
2598*4882a593Smuzhiyun     }
2599*4882a593Smuzhiyun 
2600*4882a593Smuzhiyun     for (c = 0; c < config->num_crtc; c++) {
2601*4882a593Smuzhiyun         xf86CrtcPtr crtc = config->crtc[c];
2602*4882a593Smuzhiyun 
2603*4882a593Smuzhiyun         crtc->enabled = FALSE;
2604*4882a593Smuzhiyun         memset(&crtc->desiredMode, '\0', sizeof(crtc->desiredMode));
2605*4882a593Smuzhiyun     }
2606*4882a593Smuzhiyun 
2607*4882a593Smuzhiyun     /*
2608*4882a593Smuzhiyun      * Set initial configuration
2609*4882a593Smuzhiyun      */
2610*4882a593Smuzhiyun     for (o = 0; o < config->num_output; o++) {
2611*4882a593Smuzhiyun         xf86OutputPtr output = config->output[o];
2612*4882a593Smuzhiyun         DisplayModePtr mode = modes[o];
2613*4882a593Smuzhiyun         xf86CrtcPtr crtc = crtcs[o];
2614*4882a593Smuzhiyun 
2615*4882a593Smuzhiyun         if (mode && crtc) {
2616*4882a593Smuzhiyun             xf86SaveModeContents(&crtc->desiredMode, mode);
2617*4882a593Smuzhiyun             crtc->desiredRotation = output->initial_rotation;
2618*4882a593Smuzhiyun             crtc->desiredX = output->initial_x;
2619*4882a593Smuzhiyun             crtc->desiredY = output->initial_y;
2620*4882a593Smuzhiyun             crtc->desiredTransformPresent = FALSE;
2621*4882a593Smuzhiyun             crtc->enabled = TRUE;
2622*4882a593Smuzhiyun             memcpy(&crtc->panningTotalArea, &output->initialTotalArea,
2623*4882a593Smuzhiyun                    sizeof(BoxRec));
2624*4882a593Smuzhiyun             memcpy(&crtc->panningTrackingArea, &output->initialTrackingArea,
2625*4882a593Smuzhiyun                    sizeof(BoxRec));
2626*4882a593Smuzhiyun             memcpy(crtc->panningBorder, output->initialBorder,
2627*4882a593Smuzhiyun                    4 * sizeof(INT16));
2628*4882a593Smuzhiyun             output->crtc = crtc;
2629*4882a593Smuzhiyun         }
2630*4882a593Smuzhiyun         else {
2631*4882a593Smuzhiyun             output->crtc = NULL;
2632*4882a593Smuzhiyun         }
2633*4882a593Smuzhiyun     }
2634*4882a593Smuzhiyun 
2635*4882a593Smuzhiyun     if (scrn->display->virtualX == 0 || scrn->is_gpu) {
2636*4882a593Smuzhiyun         /*
2637*4882a593Smuzhiyun          * Expand virtual size to cover the current config and potential mode
2638*4882a593Smuzhiyun          * switches, if the driver can't enlarge the screen later.
2639*4882a593Smuzhiyun          */
2640*4882a593Smuzhiyun         xf86DefaultScreenLimits(scrn, &width, &height, canGrow);
2641*4882a593Smuzhiyun 
2642*4882a593Smuzhiyun         if (have_outputs == FALSE) {
2643*4882a593Smuzhiyun             if (width < NO_OUTPUT_DEFAULT_WIDTH &&
2644*4882a593Smuzhiyun                 height < NO_OUTPUT_DEFAULT_HEIGHT) {
2645*4882a593Smuzhiyun                 width = NO_OUTPUT_DEFAULT_WIDTH;
2646*4882a593Smuzhiyun                 height = NO_OUTPUT_DEFAULT_HEIGHT;
2647*4882a593Smuzhiyun             }
2648*4882a593Smuzhiyun         }
2649*4882a593Smuzhiyun 
2650*4882a593Smuzhiyun 	if (!scrn->is_gpu) {
2651*4882a593Smuzhiyun             scrn->display->virtualX = width;
2652*4882a593Smuzhiyun             scrn->display->virtualY = height;
2653*4882a593Smuzhiyun 	}
2654*4882a593Smuzhiyun     }
2655*4882a593Smuzhiyun 
2656*4882a593Smuzhiyun     if (width > scrn->virtualX)
2657*4882a593Smuzhiyun         scrn->virtualX = width;
2658*4882a593Smuzhiyun     if (height > scrn->virtualY)
2659*4882a593Smuzhiyun         scrn->virtualY = height;
2660*4882a593Smuzhiyun 
2661*4882a593Smuzhiyun     /*
2662*4882a593Smuzhiyun      * Make sure the configuration isn't too small.
2663*4882a593Smuzhiyun      */
2664*4882a593Smuzhiyun     if (width < config->minWidth || height < config->minHeight)
2665*4882a593Smuzhiyun         goto bailout;
2666*4882a593Smuzhiyun 
2667*4882a593Smuzhiyun     /*
2668*4882a593Smuzhiyun      * Limit the crtc config to virtual[XY] if the driver can't grow the
2669*4882a593Smuzhiyun      * desktop.
2670*4882a593Smuzhiyun      */
2671*4882a593Smuzhiyun     if (!canGrow) {
2672*4882a593Smuzhiyun         xf86CrtcSetSizeRange(scrn, config->minWidth, config->minHeight,
2673*4882a593Smuzhiyun                              width, height);
2674*4882a593Smuzhiyun     }
2675*4882a593Smuzhiyun 
2676*4882a593Smuzhiyun     xf86SetScrnInfoModes(scrn);
2677*4882a593Smuzhiyun 
2678*4882a593Smuzhiyun     success = TRUE;
2679*4882a593Smuzhiyun  bailout:
2680*4882a593Smuzhiyun     free(crtcs);
2681*4882a593Smuzhiyun     free(modes);
2682*4882a593Smuzhiyun     free(enabled);
2683*4882a593Smuzhiyun     return success;
2684*4882a593Smuzhiyun }
2685*4882a593Smuzhiyun 
2686*4882a593Smuzhiyun /* Turn a CRTC off, using the DPMS function and disabling the cursor */
2687*4882a593Smuzhiyun static void
xf86DisableCrtc(xf86CrtcPtr crtc)2688*4882a593Smuzhiyun xf86DisableCrtc(xf86CrtcPtr crtc)
2689*4882a593Smuzhiyun {
2690*4882a593Smuzhiyun     if (xf86CrtcIsLeased(crtc))
2691*4882a593Smuzhiyun         return;
2692*4882a593Smuzhiyun 
2693*4882a593Smuzhiyun     crtc->funcs->dpms(crtc, DPMSModeOff);
2694*4882a593Smuzhiyun     xf86_crtc_hide_cursor(crtc);
2695*4882a593Smuzhiyun }
2696*4882a593Smuzhiyun 
2697*4882a593Smuzhiyun /*
2698*4882a593Smuzhiyun  * Check the CRTC we're going to map each output to vs. it's current
2699*4882a593Smuzhiyun  * CRTC.  If they don't match, we have to disable the output and the CRTC
2700*4882a593Smuzhiyun  * since the driver will have to re-route things.
2701*4882a593Smuzhiyun  */
2702*4882a593Smuzhiyun static void
xf86PrepareOutputs(ScrnInfoPtr scrn)2703*4882a593Smuzhiyun xf86PrepareOutputs(ScrnInfoPtr scrn)
2704*4882a593Smuzhiyun {
2705*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
2706*4882a593Smuzhiyun     int o;
2707*4882a593Smuzhiyun 
2708*4882a593Smuzhiyun     for (o = 0; o < config->num_output; o++) {
2709*4882a593Smuzhiyun         xf86OutputPtr output = config->output[o];
2710*4882a593Smuzhiyun 
2711*4882a593Smuzhiyun         if (xf86OutputIsLeased(output))
2712*4882a593Smuzhiyun             continue;
2713*4882a593Smuzhiyun 
2714*4882a593Smuzhiyun #if RANDR_GET_CRTC_INTERFACE
2715*4882a593Smuzhiyun         /* Disable outputs that are unused or will be re-routed */
2716*4882a593Smuzhiyun         if (!output->funcs->get_crtc ||
2717*4882a593Smuzhiyun             output->crtc != (*output->funcs->get_crtc) (output) ||
2718*4882a593Smuzhiyun             output->crtc == NULL)
2719*4882a593Smuzhiyun #endif
2720*4882a593Smuzhiyun             (*output->funcs->dpms) (output, DPMSModeOff);
2721*4882a593Smuzhiyun     }
2722*4882a593Smuzhiyun }
2723*4882a593Smuzhiyun 
2724*4882a593Smuzhiyun static void
xf86PrepareCrtcs(ScrnInfoPtr scrn)2725*4882a593Smuzhiyun xf86PrepareCrtcs(ScrnInfoPtr scrn)
2726*4882a593Smuzhiyun {
2727*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
2728*4882a593Smuzhiyun     int c;
2729*4882a593Smuzhiyun 
2730*4882a593Smuzhiyun     for (c = 0; c < config->num_crtc; c++) {
2731*4882a593Smuzhiyun         xf86CrtcPtr crtc = config->crtc[c];
2732*4882a593Smuzhiyun #if RANDR_GET_CRTC_INTERFACE
2733*4882a593Smuzhiyun         xf86OutputPtr output = NULL;
2734*4882a593Smuzhiyun         uint32_t desired_outputs = 0, current_outputs = 0;
2735*4882a593Smuzhiyun         int o;
2736*4882a593Smuzhiyun 
2737*4882a593Smuzhiyun         if (xf86CrtcIsLeased(crtc))
2738*4882a593Smuzhiyun             continue;
2739*4882a593Smuzhiyun 
2740*4882a593Smuzhiyun         for (o = 0; o < config->num_output; o++) {
2741*4882a593Smuzhiyun             output = config->output[o];
2742*4882a593Smuzhiyun             if (output->crtc == crtc)
2743*4882a593Smuzhiyun                 desired_outputs |= (1 << o);
2744*4882a593Smuzhiyun             /* If we can't tell where it's mapped, force it off */
2745*4882a593Smuzhiyun             if (!output->funcs->get_crtc) {
2746*4882a593Smuzhiyun                 desired_outputs = 0;
2747*4882a593Smuzhiyun                 break;
2748*4882a593Smuzhiyun             }
2749*4882a593Smuzhiyun             if ((*output->funcs->get_crtc) (output) == crtc)
2750*4882a593Smuzhiyun                 current_outputs |= (1 << o);
2751*4882a593Smuzhiyun         }
2752*4882a593Smuzhiyun 
2753*4882a593Smuzhiyun         /*
2754*4882a593Smuzhiyun          * If mappings are different or the CRTC is unused,
2755*4882a593Smuzhiyun          * we need to disable it
2756*4882a593Smuzhiyun          */
2757*4882a593Smuzhiyun         if (desired_outputs != current_outputs || !desired_outputs)
2758*4882a593Smuzhiyun             xf86DisableCrtc(crtc);
2759*4882a593Smuzhiyun #else
2760*4882a593Smuzhiyun         if (xf86CrtcIsLeased(crtc))
2761*4882a593Smuzhiyun             continue;
2762*4882a593Smuzhiyun 
2763*4882a593Smuzhiyun         xf86DisableCrtc(crtc);
2764*4882a593Smuzhiyun #endif
2765*4882a593Smuzhiyun     }
2766*4882a593Smuzhiyun }
2767*4882a593Smuzhiyun 
2768*4882a593Smuzhiyun /*
2769*4882a593Smuzhiyun  * Using the desired mode information in each crtc, set
2770*4882a593Smuzhiyun  * modes (used in EnterVT functions, or at server startup)
2771*4882a593Smuzhiyun  */
2772*4882a593Smuzhiyun 
2773*4882a593Smuzhiyun Bool
xf86SetDesiredModes(ScrnInfoPtr scrn)2774*4882a593Smuzhiyun xf86SetDesiredModes(ScrnInfoPtr scrn)
2775*4882a593Smuzhiyun {
2776*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
2777*4882a593Smuzhiyun     xf86CrtcPtr crtc = config->crtc[0];
2778*4882a593Smuzhiyun     int enabled = 0, failed = 0;
2779*4882a593Smuzhiyun     int c;
2780*4882a593Smuzhiyun 
2781*4882a593Smuzhiyun     /* A driver with this hook will take care of this */
2782*4882a593Smuzhiyun     if (!crtc->funcs->set_mode_major) {
2783*4882a593Smuzhiyun         xf86PrepareOutputs(scrn);
2784*4882a593Smuzhiyun         xf86PrepareCrtcs(scrn);
2785*4882a593Smuzhiyun     }
2786*4882a593Smuzhiyun 
2787*4882a593Smuzhiyun     for (c = 0; c < config->num_crtc; c++) {
2788*4882a593Smuzhiyun         xf86OutputPtr output = NULL;
2789*4882a593Smuzhiyun         int o;
2790*4882a593Smuzhiyun         RRTransformPtr transform;
2791*4882a593Smuzhiyun 
2792*4882a593Smuzhiyun         crtc = config->crtc[c];
2793*4882a593Smuzhiyun 
2794*4882a593Smuzhiyun         /* Skip disabled CRTCs */
2795*4882a593Smuzhiyun         if (!crtc->enabled)
2796*4882a593Smuzhiyun             continue;
2797*4882a593Smuzhiyun 
2798*4882a593Smuzhiyun         if (xf86CompatOutput(scrn) && xf86CompatCrtc(scrn) == crtc)
2799*4882a593Smuzhiyun             output = xf86CompatOutput(scrn);
2800*4882a593Smuzhiyun         else {
2801*4882a593Smuzhiyun             for (o = 0; o < config->num_output; o++)
2802*4882a593Smuzhiyun                 if (config->output[o]->crtc == crtc) {
2803*4882a593Smuzhiyun                     output = config->output[o];
2804*4882a593Smuzhiyun                     break;
2805*4882a593Smuzhiyun                 }
2806*4882a593Smuzhiyun         }
2807*4882a593Smuzhiyun         /* paranoia */
2808*4882a593Smuzhiyun         if (!output)
2809*4882a593Smuzhiyun             continue;
2810*4882a593Smuzhiyun 
2811*4882a593Smuzhiyun         /* Mark that we'll need to re-set the mode for sure */
2812*4882a593Smuzhiyun         memset(&crtc->mode, 0, sizeof(crtc->mode));
2813*4882a593Smuzhiyun         if (!crtc->desiredMode.CrtcHDisplay) {
2814*4882a593Smuzhiyun             DisplayModePtr mode =
2815*4882a593Smuzhiyun                 xf86OutputFindClosestMode(output, scrn->currentMode);
2816*4882a593Smuzhiyun 
2817*4882a593Smuzhiyun             if (!mode)
2818*4882a593Smuzhiyun                 return FALSE;
2819*4882a593Smuzhiyun             xf86SaveModeContents(&crtc->desiredMode, mode);
2820*4882a593Smuzhiyun             crtc->desiredRotation = RR_Rotate_0;
2821*4882a593Smuzhiyun             crtc->desiredTransformPresent = FALSE;
2822*4882a593Smuzhiyun             crtc->desiredX = 0;
2823*4882a593Smuzhiyun             crtc->desiredY = 0;
2824*4882a593Smuzhiyun         }
2825*4882a593Smuzhiyun 
2826*4882a593Smuzhiyun         if (crtc->desiredTransformPresent)
2827*4882a593Smuzhiyun             transform = &crtc->desiredTransform;
2828*4882a593Smuzhiyun         else
2829*4882a593Smuzhiyun             transform = NULL;
2830*4882a593Smuzhiyun         if (xf86CrtcSetModeTransform
2831*4882a593Smuzhiyun             (crtc, &crtc->desiredMode, crtc->desiredRotation, transform,
2832*4882a593Smuzhiyun              crtc->desiredX, crtc->desiredY)) {
2833*4882a593Smuzhiyun             ++enabled;
2834*4882a593Smuzhiyun         } else {
2835*4882a593Smuzhiyun             for (o = 0; o < config->num_output; o++)
2836*4882a593Smuzhiyun                 if (config->output[o]->crtc == crtc)
2837*4882a593Smuzhiyun                     config->output[o]->crtc = NULL;
2838*4882a593Smuzhiyun             crtc->enabled = FALSE;
2839*4882a593Smuzhiyun             ++failed;
2840*4882a593Smuzhiyun 	}
2841*4882a593Smuzhiyun     }
2842*4882a593Smuzhiyun 
2843*4882a593Smuzhiyun     xf86DisableUnusedFunctions(scrn);
2844*4882a593Smuzhiyun     return enabled != 0 || failed == 0;
2845*4882a593Smuzhiyun }
2846*4882a593Smuzhiyun 
2847*4882a593Smuzhiyun /**
2848*4882a593Smuzhiyun  * In the current world order, there are lists of modes per output, which may
2849*4882a593Smuzhiyun  * or may not include the mode that was asked to be set by XFree86's mode
2850*4882a593Smuzhiyun  * selection.  Find the closest one, in the following preference order:
2851*4882a593Smuzhiyun  *
2852*4882a593Smuzhiyun  * - Equality
2853*4882a593Smuzhiyun  * - Closer in size to the requested mode, but no larger
2854*4882a593Smuzhiyun  * - Closer in refresh rate to the requested mode.
2855*4882a593Smuzhiyun  */
2856*4882a593Smuzhiyun 
2857*4882a593Smuzhiyun DisplayModePtr
xf86OutputFindClosestMode(xf86OutputPtr output,DisplayModePtr desired)2858*4882a593Smuzhiyun xf86OutputFindClosestMode(xf86OutputPtr output, DisplayModePtr desired)
2859*4882a593Smuzhiyun {
2860*4882a593Smuzhiyun     DisplayModePtr best = NULL, scan = NULL;
2861*4882a593Smuzhiyun 
2862*4882a593Smuzhiyun     for (scan = output->probed_modes; scan != NULL; scan = scan->next) {
2863*4882a593Smuzhiyun         /* If there's an exact match, we're done. */
2864*4882a593Smuzhiyun         if (xf86ModesEqual(scan, desired)) {
2865*4882a593Smuzhiyun             best = desired;
2866*4882a593Smuzhiyun             break;
2867*4882a593Smuzhiyun         }
2868*4882a593Smuzhiyun 
2869*4882a593Smuzhiyun         /* Reject if it's larger than the desired mode. */
2870*4882a593Smuzhiyun         if (scan->HDisplay > desired->HDisplay ||
2871*4882a593Smuzhiyun             scan->VDisplay > desired->VDisplay) {
2872*4882a593Smuzhiyun             continue;
2873*4882a593Smuzhiyun         }
2874*4882a593Smuzhiyun 
2875*4882a593Smuzhiyun         /*
2876*4882a593Smuzhiyun          * If we haven't picked a best mode yet, use the first
2877*4882a593Smuzhiyun          * one in the size range
2878*4882a593Smuzhiyun          */
2879*4882a593Smuzhiyun         if (best == NULL) {
2880*4882a593Smuzhiyun             best = scan;
2881*4882a593Smuzhiyun             continue;
2882*4882a593Smuzhiyun         }
2883*4882a593Smuzhiyun 
2884*4882a593Smuzhiyun         /* Find if it's closer to the right size than the current best
2885*4882a593Smuzhiyun          * option.
2886*4882a593Smuzhiyun          */
2887*4882a593Smuzhiyun         if ((scan->HDisplay > best->HDisplay &&
2888*4882a593Smuzhiyun              scan->VDisplay >= best->VDisplay) ||
2889*4882a593Smuzhiyun             (scan->HDisplay >= best->HDisplay &&
2890*4882a593Smuzhiyun              scan->VDisplay > best->VDisplay)) {
2891*4882a593Smuzhiyun             best = scan;
2892*4882a593Smuzhiyun             continue;
2893*4882a593Smuzhiyun         }
2894*4882a593Smuzhiyun 
2895*4882a593Smuzhiyun         /* Find if it's still closer to the right refresh than the current
2896*4882a593Smuzhiyun          * best resolution.
2897*4882a593Smuzhiyun          */
2898*4882a593Smuzhiyun         if (scan->HDisplay == best->HDisplay &&
2899*4882a593Smuzhiyun             scan->VDisplay == best->VDisplay &&
2900*4882a593Smuzhiyun             (fabs(scan->VRefresh - desired->VRefresh) <
2901*4882a593Smuzhiyun              fabs(best->VRefresh - desired->VRefresh))) {
2902*4882a593Smuzhiyun             best = scan;
2903*4882a593Smuzhiyun         }
2904*4882a593Smuzhiyun     }
2905*4882a593Smuzhiyun     return best;
2906*4882a593Smuzhiyun }
2907*4882a593Smuzhiyun 
2908*4882a593Smuzhiyun /**
2909*4882a593Smuzhiyun  * When setting a mode through XFree86-VidModeExtension or XFree86-DGA,
2910*4882a593Smuzhiyun  * take the specified mode and apply it to the crtc connected to the compat
2911*4882a593Smuzhiyun  * output. Then, find similar modes for the other outputs, as with the
2912*4882a593Smuzhiyun  * InitialConfiguration code above. The goal is to clone the desired
2913*4882a593Smuzhiyun  * mode across all outputs that are currently active.
2914*4882a593Smuzhiyun  */
2915*4882a593Smuzhiyun 
2916*4882a593Smuzhiyun Bool
xf86SetSingleMode(ScrnInfoPtr pScrn,DisplayModePtr desired,Rotation rotation)2917*4882a593Smuzhiyun xf86SetSingleMode(ScrnInfoPtr pScrn, DisplayModePtr desired, Rotation rotation)
2918*4882a593Smuzhiyun {
2919*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
2920*4882a593Smuzhiyun     Bool ok = TRUE;
2921*4882a593Smuzhiyun     xf86OutputPtr compat_output;
2922*4882a593Smuzhiyun     DisplayModePtr compat_mode = NULL;
2923*4882a593Smuzhiyun     int c;
2924*4882a593Smuzhiyun 
2925*4882a593Smuzhiyun     /*
2926*4882a593Smuzhiyun      * Let the compat output drive the final mode selection
2927*4882a593Smuzhiyun      */
2928*4882a593Smuzhiyun     compat_output = xf86CompatOutput(pScrn);
2929*4882a593Smuzhiyun     if (compat_output)
2930*4882a593Smuzhiyun         compat_mode = xf86OutputFindClosestMode(compat_output, desired);
2931*4882a593Smuzhiyun     if (compat_mode)
2932*4882a593Smuzhiyun         desired = compat_mode;
2933*4882a593Smuzhiyun 
2934*4882a593Smuzhiyun     for (c = 0; c < config->num_crtc; c++) {
2935*4882a593Smuzhiyun         xf86CrtcPtr crtc = config->crtc[c];
2936*4882a593Smuzhiyun         DisplayModePtr crtc_mode = NULL;
2937*4882a593Smuzhiyun         int o;
2938*4882a593Smuzhiyun 
2939*4882a593Smuzhiyun         if (!crtc->enabled)
2940*4882a593Smuzhiyun             continue;
2941*4882a593Smuzhiyun 
2942*4882a593Smuzhiyun         for (o = 0; o < config->num_output; o++) {
2943*4882a593Smuzhiyun             xf86OutputPtr output = config->output[o];
2944*4882a593Smuzhiyun             DisplayModePtr output_mode;
2945*4882a593Smuzhiyun 
2946*4882a593Smuzhiyun             /* skip outputs not on this crtc */
2947*4882a593Smuzhiyun             if (output->crtc != crtc)
2948*4882a593Smuzhiyun                 continue;
2949*4882a593Smuzhiyun 
2950*4882a593Smuzhiyun             if (crtc_mode) {
2951*4882a593Smuzhiyun                 output_mode = xf86OutputFindClosestMode(output, crtc_mode);
2952*4882a593Smuzhiyun                 if (output_mode != crtc_mode)
2953*4882a593Smuzhiyun                     output->crtc = NULL;
2954*4882a593Smuzhiyun             }
2955*4882a593Smuzhiyun             else
2956*4882a593Smuzhiyun                 crtc_mode = xf86OutputFindClosestMode(output, desired);
2957*4882a593Smuzhiyun         }
2958*4882a593Smuzhiyun         if (!crtc_mode) {
2959*4882a593Smuzhiyun             crtc->enabled = FALSE;
2960*4882a593Smuzhiyun             continue;
2961*4882a593Smuzhiyun         }
2962*4882a593Smuzhiyun         if (!xf86CrtcSetModeTransform(crtc, crtc_mode, rotation, NULL, 0, 0))
2963*4882a593Smuzhiyun             ok = FALSE;
2964*4882a593Smuzhiyun         else {
2965*4882a593Smuzhiyun             xf86SaveModeContents(&crtc->desiredMode, crtc_mode);
2966*4882a593Smuzhiyun             crtc->desiredRotation = rotation;
2967*4882a593Smuzhiyun             crtc->desiredTransformPresent = FALSE;
2968*4882a593Smuzhiyun             crtc->desiredX = 0;
2969*4882a593Smuzhiyun             crtc->desiredY = 0;
2970*4882a593Smuzhiyun         }
2971*4882a593Smuzhiyun     }
2972*4882a593Smuzhiyun     xf86DisableUnusedFunctions(pScrn);
2973*4882a593Smuzhiyun #ifdef RANDR_12_INTERFACE
2974*4882a593Smuzhiyun     xf86RandR12TellChanged(pScrn->pScreen);
2975*4882a593Smuzhiyun #endif
2976*4882a593Smuzhiyun     return ok;
2977*4882a593Smuzhiyun }
2978*4882a593Smuzhiyun 
2979*4882a593Smuzhiyun /**
2980*4882a593Smuzhiyun  * Set the DPMS power mode of all outputs and CRTCs.
2981*4882a593Smuzhiyun  *
2982*4882a593Smuzhiyun  * If the new mode is off, it will turn off outputs and then CRTCs.
2983*4882a593Smuzhiyun  * Otherwise, it will affect CRTCs before outputs.
2984*4882a593Smuzhiyun  */
2985*4882a593Smuzhiyun void
xf86DPMSSet(ScrnInfoPtr scrn,int mode,int flags)2986*4882a593Smuzhiyun xf86DPMSSet(ScrnInfoPtr scrn, int mode, int flags)
2987*4882a593Smuzhiyun {
2988*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
2989*4882a593Smuzhiyun     int i;
2990*4882a593Smuzhiyun 
2991*4882a593Smuzhiyun     if (!scrn->vtSema)
2992*4882a593Smuzhiyun         return;
2993*4882a593Smuzhiyun 
2994*4882a593Smuzhiyun     if (mode == DPMSModeOff) {
2995*4882a593Smuzhiyun         for (i = 0; i < config->num_output; i++) {
2996*4882a593Smuzhiyun             xf86OutputPtr output = config->output[i];
2997*4882a593Smuzhiyun 
2998*4882a593Smuzhiyun             if (!xf86OutputIsLeased(output) && output->crtc != NULL)
2999*4882a593Smuzhiyun                 (*output->funcs->dpms) (output, mode);
3000*4882a593Smuzhiyun         }
3001*4882a593Smuzhiyun     }
3002*4882a593Smuzhiyun 
3003*4882a593Smuzhiyun     for (i = 0; i < config->num_crtc; i++) {
3004*4882a593Smuzhiyun         xf86CrtcPtr crtc = config->crtc[i];
3005*4882a593Smuzhiyun 
3006*4882a593Smuzhiyun         if (crtc->enabled)
3007*4882a593Smuzhiyun             (*crtc->funcs->dpms) (crtc, mode);
3008*4882a593Smuzhiyun     }
3009*4882a593Smuzhiyun 
3010*4882a593Smuzhiyun     if (mode != DPMSModeOff) {
3011*4882a593Smuzhiyun         for (i = 0; i < config->num_output; i++) {
3012*4882a593Smuzhiyun             xf86OutputPtr output = config->output[i];
3013*4882a593Smuzhiyun 
3014*4882a593Smuzhiyun             if (!xf86OutputIsLeased(output) && output->crtc != NULL)
3015*4882a593Smuzhiyun                 (*output->funcs->dpms) (output, mode);
3016*4882a593Smuzhiyun         }
3017*4882a593Smuzhiyun     }
3018*4882a593Smuzhiyun }
3019*4882a593Smuzhiyun 
3020*4882a593Smuzhiyun /**
3021*4882a593Smuzhiyun  * Implement the screensaver by just calling down into the driver DPMS hooks.
3022*4882a593Smuzhiyun  *
3023*4882a593Smuzhiyun  * Even for monitors with no DPMS support, by the definition of our DPMS hooks,
3024*4882a593Smuzhiyun  * the outputs will still get disabled (blanked).
3025*4882a593Smuzhiyun  */
3026*4882a593Smuzhiyun Bool
xf86SaveScreen(ScreenPtr pScreen,int mode)3027*4882a593Smuzhiyun xf86SaveScreen(ScreenPtr pScreen, int mode)
3028*4882a593Smuzhiyun {
3029*4882a593Smuzhiyun     ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
3030*4882a593Smuzhiyun 
3031*4882a593Smuzhiyun     if (xf86IsUnblank(mode))
3032*4882a593Smuzhiyun         xf86DPMSSet(pScrn, DPMSModeOn, 0);
3033*4882a593Smuzhiyun     else
3034*4882a593Smuzhiyun         xf86DPMSSet(pScrn, DPMSModeOff, 0);
3035*4882a593Smuzhiyun 
3036*4882a593Smuzhiyun     return TRUE;
3037*4882a593Smuzhiyun }
3038*4882a593Smuzhiyun 
3039*4882a593Smuzhiyun /**
3040*4882a593Smuzhiyun  * Disable all inactive crtcs and outputs
3041*4882a593Smuzhiyun  */
3042*4882a593Smuzhiyun void
xf86DisableUnusedFunctions(ScrnInfoPtr pScrn)3043*4882a593Smuzhiyun xf86DisableUnusedFunctions(ScrnInfoPtr pScrn)
3044*4882a593Smuzhiyun {
3045*4882a593Smuzhiyun     xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
3046*4882a593Smuzhiyun     int o, c;
3047*4882a593Smuzhiyun 
3048*4882a593Smuzhiyun     for (o = 0; o < xf86_config->num_output; o++) {
3049*4882a593Smuzhiyun         xf86OutputPtr output = xf86_config->output[o];
3050*4882a593Smuzhiyun 
3051*4882a593Smuzhiyun         if (!output->crtc)
3052*4882a593Smuzhiyun             (*output->funcs->dpms) (output, DPMSModeOff);
3053*4882a593Smuzhiyun     }
3054*4882a593Smuzhiyun 
3055*4882a593Smuzhiyun     for (c = 0; c < xf86_config->num_crtc; c++) {
3056*4882a593Smuzhiyun         xf86CrtcPtr crtc = xf86_config->crtc[c];
3057*4882a593Smuzhiyun 
3058*4882a593Smuzhiyun         if (!crtc->enabled) {
3059*4882a593Smuzhiyun             xf86DisableCrtc(crtc);
3060*4882a593Smuzhiyun             memset(&crtc->mode, 0, sizeof(crtc->mode));
3061*4882a593Smuzhiyun             xf86RotateDestroy(crtc);
3062*4882a593Smuzhiyun             crtc->active = FALSE;
3063*4882a593Smuzhiyun         }
3064*4882a593Smuzhiyun     }
3065*4882a593Smuzhiyun     if (pScrn->pScreen)
3066*4882a593Smuzhiyun         xf86_crtc_notify(pScrn->pScreen);
3067*4882a593Smuzhiyun     if (pScrn->ModeSet)
3068*4882a593Smuzhiyun         pScrn->ModeSet(pScrn);
3069*4882a593Smuzhiyun     if (pScrn->pScreen) {
3070*4882a593Smuzhiyun         if (pScrn->pScreen->isGPU)
3071*4882a593Smuzhiyun             xf86CursorResetCursor(pScrn->pScreen->current_master);
3072*4882a593Smuzhiyun         else
3073*4882a593Smuzhiyun             xf86CursorResetCursor(pScrn->pScreen);
3074*4882a593Smuzhiyun     }
3075*4882a593Smuzhiyun }
3076*4882a593Smuzhiyun 
3077*4882a593Smuzhiyun #ifdef RANDR_12_INTERFACE
3078*4882a593Smuzhiyun 
3079*4882a593Smuzhiyun #define EDID_ATOM_NAME		"EDID"
3080*4882a593Smuzhiyun 
3081*4882a593Smuzhiyun /**
3082*4882a593Smuzhiyun  * Set the RandR EDID property
3083*4882a593Smuzhiyun  */
3084*4882a593Smuzhiyun static void
xf86OutputSetEDIDProperty(xf86OutputPtr output,void * data,int data_len)3085*4882a593Smuzhiyun xf86OutputSetEDIDProperty(xf86OutputPtr output, void *data, int data_len)
3086*4882a593Smuzhiyun {
3087*4882a593Smuzhiyun     Atom edid_atom = MakeAtom(EDID_ATOM_NAME, sizeof(EDID_ATOM_NAME) - 1, TRUE);
3088*4882a593Smuzhiyun 
3089*4882a593Smuzhiyun     /* This may get called before the RandR resources have been created */
3090*4882a593Smuzhiyun     if (output->randr_output == NULL)
3091*4882a593Smuzhiyun         return;
3092*4882a593Smuzhiyun 
3093*4882a593Smuzhiyun     if (data_len != 0) {
3094*4882a593Smuzhiyun         RRChangeOutputProperty(output->randr_output, edid_atom, XA_INTEGER, 8,
3095*4882a593Smuzhiyun                                PropModeReplace, data_len, data, FALSE, TRUE);
3096*4882a593Smuzhiyun     }
3097*4882a593Smuzhiyun     else {
3098*4882a593Smuzhiyun         RRDeleteOutputProperty(output->randr_output, edid_atom);
3099*4882a593Smuzhiyun     }
3100*4882a593Smuzhiyun }
3101*4882a593Smuzhiyun 
3102*4882a593Smuzhiyun #define TILE_ATOM_NAME		"TILE"
3103*4882a593Smuzhiyun /* changing this in the future could be tricky as people may hardcode 8 */
3104*4882a593Smuzhiyun #define TILE_PROP_NUM_ITEMS		8
3105*4882a593Smuzhiyun static void
xf86OutputSetTileProperty(xf86OutputPtr output)3106*4882a593Smuzhiyun xf86OutputSetTileProperty(xf86OutputPtr output)
3107*4882a593Smuzhiyun {
3108*4882a593Smuzhiyun     Atom tile_atom = MakeAtom(TILE_ATOM_NAME, sizeof(TILE_ATOM_NAME) - 1, TRUE);
3109*4882a593Smuzhiyun 
3110*4882a593Smuzhiyun     /* This may get called before the RandR resources have been created */
3111*4882a593Smuzhiyun     if (output->randr_output == NULL)
3112*4882a593Smuzhiyun         return;
3113*4882a593Smuzhiyun 
3114*4882a593Smuzhiyun     if (output->tile_info.group_id != 0) {
3115*4882a593Smuzhiyun         RRChangeOutputProperty(output->randr_output, tile_atom, XA_INTEGER, 32,
3116*4882a593Smuzhiyun                                PropModeReplace, TILE_PROP_NUM_ITEMS, (uint32_t *)&output->tile_info, FALSE, TRUE);
3117*4882a593Smuzhiyun     }
3118*4882a593Smuzhiyun     else {
3119*4882a593Smuzhiyun         RRDeleteOutputProperty(output->randr_output, tile_atom);
3120*4882a593Smuzhiyun     }
3121*4882a593Smuzhiyun }
3122*4882a593Smuzhiyun 
3123*4882a593Smuzhiyun #endif
3124*4882a593Smuzhiyun 
3125*4882a593Smuzhiyun /* Pull out a phyiscal size from a detailed timing if available. */
3126*4882a593Smuzhiyun struct det_phySize_parameter {
3127*4882a593Smuzhiyun     xf86OutputPtr output;
3128*4882a593Smuzhiyun     ddc_quirk_t quirks;
3129*4882a593Smuzhiyun     Bool ret;
3130*4882a593Smuzhiyun };
3131*4882a593Smuzhiyun 
3132*4882a593Smuzhiyun static void
handle_detailed_physical_size(struct detailed_monitor_section * det_mon,void * data)3133*4882a593Smuzhiyun handle_detailed_physical_size(struct detailed_monitor_section
3134*4882a593Smuzhiyun                               *det_mon, void *data)
3135*4882a593Smuzhiyun {
3136*4882a593Smuzhiyun     struct det_phySize_parameter *p;
3137*4882a593Smuzhiyun 
3138*4882a593Smuzhiyun     p = (struct det_phySize_parameter *) data;
3139*4882a593Smuzhiyun 
3140*4882a593Smuzhiyun     if (p->ret == TRUE)
3141*4882a593Smuzhiyun         return;
3142*4882a593Smuzhiyun 
3143*4882a593Smuzhiyun     xf86DetTimingApplyQuirks(det_mon, p->quirks,
3144*4882a593Smuzhiyun                              p->output->MonInfo->features.hsize,
3145*4882a593Smuzhiyun                              p->output->MonInfo->features.vsize);
3146*4882a593Smuzhiyun     if (det_mon->type == DT &&
3147*4882a593Smuzhiyun         det_mon->section.d_timings.h_size != 0 &&
3148*4882a593Smuzhiyun         det_mon->section.d_timings.v_size != 0) {
3149*4882a593Smuzhiyun         /* some sanity checking for aspect ratio:
3150*4882a593Smuzhiyun            assume any h / v (or v / h) > 2.4 to be bogus.
3151*4882a593Smuzhiyun            This would even include cinemascope */
3152*4882a593Smuzhiyun         if (((det_mon->section.d_timings.h_size * 5) <
3153*4882a593Smuzhiyun              (det_mon->section.d_timings.v_size * 12)) &&
3154*4882a593Smuzhiyun             ((det_mon->section.d_timings.v_size * 5) <
3155*4882a593Smuzhiyun              (det_mon->section.d_timings.h_size * 12))) {
3156*4882a593Smuzhiyun             p->output->mm_width = det_mon->section.d_timings.h_size;
3157*4882a593Smuzhiyun             p->output->mm_height = det_mon->section.d_timings.v_size;
3158*4882a593Smuzhiyun             p->ret = TRUE;
3159*4882a593Smuzhiyun         } else
3160*4882a593Smuzhiyun             xf86DrvMsg(p->output->scrn->scrnIndex, X_WARNING,
3161*4882a593Smuzhiyun                        "Output %s: Strange aspect ratio (%i/%i), "
3162*4882a593Smuzhiyun                        "consider adding a quirk\n", p->output->name,
3163*4882a593Smuzhiyun                        det_mon->section.d_timings.h_size,
3164*4882a593Smuzhiyun                        det_mon->section.d_timings.v_size);
3165*4882a593Smuzhiyun     }
3166*4882a593Smuzhiyun }
3167*4882a593Smuzhiyun 
3168*4882a593Smuzhiyun Bool
xf86OutputParseKMSTile(const char * tile_data,int tile_length,struct xf86CrtcTileInfo * tile_info)3169*4882a593Smuzhiyun xf86OutputParseKMSTile(const char *tile_data, int tile_length,
3170*4882a593Smuzhiyun                        struct xf86CrtcTileInfo *tile_info)
3171*4882a593Smuzhiyun {
3172*4882a593Smuzhiyun     int ret;
3173*4882a593Smuzhiyun 
3174*4882a593Smuzhiyun     ret = sscanf(tile_data, "%d:%d:%d:%d:%d:%d:%d:%d",
3175*4882a593Smuzhiyun                  &tile_info->group_id,
3176*4882a593Smuzhiyun                  &tile_info->flags,
3177*4882a593Smuzhiyun                  &tile_info->num_h_tile,
3178*4882a593Smuzhiyun                  &tile_info->num_v_tile,
3179*4882a593Smuzhiyun                  &tile_info->tile_h_loc,
3180*4882a593Smuzhiyun                  &tile_info->tile_v_loc,
3181*4882a593Smuzhiyun                  &tile_info->tile_h_size,
3182*4882a593Smuzhiyun                  &tile_info->tile_v_size);
3183*4882a593Smuzhiyun     if (ret != 8)
3184*4882a593Smuzhiyun         return FALSE;
3185*4882a593Smuzhiyun     return TRUE;
3186*4882a593Smuzhiyun }
3187*4882a593Smuzhiyun 
3188*4882a593Smuzhiyun void
xf86OutputSetTile(xf86OutputPtr output,struct xf86CrtcTileInfo * tile_info)3189*4882a593Smuzhiyun xf86OutputSetTile(xf86OutputPtr output, struct xf86CrtcTileInfo *tile_info)
3190*4882a593Smuzhiyun {
3191*4882a593Smuzhiyun     if (tile_info)
3192*4882a593Smuzhiyun         output->tile_info = *tile_info;
3193*4882a593Smuzhiyun     else
3194*4882a593Smuzhiyun         memset(&output->tile_info, 0, sizeof(output->tile_info));
3195*4882a593Smuzhiyun #ifdef RANDR_12_INTERFACE
3196*4882a593Smuzhiyun     xf86OutputSetTileProperty(output);
3197*4882a593Smuzhiyun #endif
3198*4882a593Smuzhiyun }
3199*4882a593Smuzhiyun 
3200*4882a593Smuzhiyun /**
3201*4882a593Smuzhiyun  * Set the EDID information for the specified output
3202*4882a593Smuzhiyun  */
3203*4882a593Smuzhiyun void
xf86OutputSetEDID(xf86OutputPtr output,xf86MonPtr edid_mon)3204*4882a593Smuzhiyun xf86OutputSetEDID(xf86OutputPtr output, xf86MonPtr edid_mon)
3205*4882a593Smuzhiyun {
3206*4882a593Smuzhiyun     ScrnInfoPtr scrn = output->scrn;
3207*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
3208*4882a593Smuzhiyun     Bool debug_modes = config->debug_modes || xf86Initialising;
3209*4882a593Smuzhiyun 
3210*4882a593Smuzhiyun #ifdef RANDR_12_INTERFACE
3211*4882a593Smuzhiyun     int size;
3212*4882a593Smuzhiyun #endif
3213*4882a593Smuzhiyun 
3214*4882a593Smuzhiyun     free(output->MonInfo);
3215*4882a593Smuzhiyun 
3216*4882a593Smuzhiyun     output->MonInfo = edid_mon;
3217*4882a593Smuzhiyun     output->mm_width = 0;
3218*4882a593Smuzhiyun     output->mm_height = 0;
3219*4882a593Smuzhiyun 
3220*4882a593Smuzhiyun     if (debug_modes) {
3221*4882a593Smuzhiyun         xf86DrvMsg(scrn->scrnIndex, X_INFO, "EDID for output %s\n",
3222*4882a593Smuzhiyun                    output->name);
3223*4882a593Smuzhiyun         xf86PrintEDID(edid_mon);
3224*4882a593Smuzhiyun     }
3225*4882a593Smuzhiyun 
3226*4882a593Smuzhiyun     /* Set the DDC properties for the 'compat' output */
3227*4882a593Smuzhiyun     /* GPU screens don't have a root window */
3228*4882a593Smuzhiyun     if (output == xf86CompatOutput(scrn) && !scrn->is_gpu)
3229*4882a593Smuzhiyun         xf86SetDDCproperties(scrn, edid_mon);
3230*4882a593Smuzhiyun 
3231*4882a593Smuzhiyun #ifdef RANDR_12_INTERFACE
3232*4882a593Smuzhiyun     /* Set the RandR output properties */
3233*4882a593Smuzhiyun     size = 0;
3234*4882a593Smuzhiyun     if (edid_mon) {
3235*4882a593Smuzhiyun         if (edid_mon->ver.version == 1) {
3236*4882a593Smuzhiyun             size = 128;
3237*4882a593Smuzhiyun             if (edid_mon->flags & EDID_COMPLETE_RAWDATA)
3238*4882a593Smuzhiyun                 size += edid_mon->no_sections * 128;
3239*4882a593Smuzhiyun         }
3240*4882a593Smuzhiyun         else if (edid_mon->ver.version == 2)
3241*4882a593Smuzhiyun             size = 256;
3242*4882a593Smuzhiyun     }
3243*4882a593Smuzhiyun     xf86OutputSetEDIDProperty(output, edid_mon ? edid_mon->rawData : NULL,
3244*4882a593Smuzhiyun                               size);
3245*4882a593Smuzhiyun #endif
3246*4882a593Smuzhiyun 
3247*4882a593Smuzhiyun     if (edid_mon) {
3248*4882a593Smuzhiyun 
3249*4882a593Smuzhiyun         struct det_phySize_parameter p;
3250*4882a593Smuzhiyun 
3251*4882a593Smuzhiyun         p.output = output;
3252*4882a593Smuzhiyun         p.quirks = xf86DDCDetectQuirks(scrn->scrnIndex, edid_mon, FALSE);
3253*4882a593Smuzhiyun         p.ret = FALSE;
3254*4882a593Smuzhiyun         xf86ForEachDetailedBlock(edid_mon, handle_detailed_physical_size, &p);
3255*4882a593Smuzhiyun 
3256*4882a593Smuzhiyun         /* if no mm size is available from a detailed timing, check the max size field */
3257*4882a593Smuzhiyun         if ((!output->mm_width || !output->mm_height) &&
3258*4882a593Smuzhiyun             (edid_mon->features.hsize && edid_mon->features.vsize)) {
3259*4882a593Smuzhiyun             output->mm_width = edid_mon->features.hsize * 10;
3260*4882a593Smuzhiyun             output->mm_height = edid_mon->features.vsize * 10;
3261*4882a593Smuzhiyun         }
3262*4882a593Smuzhiyun     }
3263*4882a593Smuzhiyun }
3264*4882a593Smuzhiyun 
3265*4882a593Smuzhiyun /**
3266*4882a593Smuzhiyun  * Return the list of modes supported by the EDID information
3267*4882a593Smuzhiyun  * stored in 'output'
3268*4882a593Smuzhiyun  */
3269*4882a593Smuzhiyun DisplayModePtr
xf86OutputGetEDIDModes(xf86OutputPtr output)3270*4882a593Smuzhiyun xf86OutputGetEDIDModes(xf86OutputPtr output)
3271*4882a593Smuzhiyun {
3272*4882a593Smuzhiyun     ScrnInfoPtr scrn = output->scrn;
3273*4882a593Smuzhiyun     xf86MonPtr edid_mon = output->MonInfo;
3274*4882a593Smuzhiyun 
3275*4882a593Smuzhiyun     if (!edid_mon)
3276*4882a593Smuzhiyun         return NULL;
3277*4882a593Smuzhiyun     return xf86DDCGetModes(scrn->scrnIndex, edid_mon);
3278*4882a593Smuzhiyun }
3279*4882a593Smuzhiyun 
3280*4882a593Smuzhiyun /* maybe we should care about DDC1?  meh. */
3281*4882a593Smuzhiyun xf86MonPtr
xf86OutputGetEDID(xf86OutputPtr output,I2CBusPtr pDDCBus)3282*4882a593Smuzhiyun xf86OutputGetEDID(xf86OutputPtr output, I2CBusPtr pDDCBus)
3283*4882a593Smuzhiyun {
3284*4882a593Smuzhiyun     ScrnInfoPtr scrn = output->scrn;
3285*4882a593Smuzhiyun     xf86MonPtr mon;
3286*4882a593Smuzhiyun 
3287*4882a593Smuzhiyun     mon = xf86DoEEDID(scrn, pDDCBus, TRUE);
3288*4882a593Smuzhiyun     if (mon)
3289*4882a593Smuzhiyun         xf86DDCApplyQuirks(scrn->scrnIndex, mon);
3290*4882a593Smuzhiyun 
3291*4882a593Smuzhiyun     return mon;
3292*4882a593Smuzhiyun }
3293*4882a593Smuzhiyun 
3294*4882a593Smuzhiyun static const char *_xf86ConnectorNames[] = {
3295*4882a593Smuzhiyun     "None", "VGA", "DVI-I", "DVI-D",
3296*4882a593Smuzhiyun     "DVI-A", "Composite", "S-Video",
3297*4882a593Smuzhiyun     "Component", "LFP", "Proprietary",
3298*4882a593Smuzhiyun     "HDMI", "DisplayPort",
3299*4882a593Smuzhiyun };
3300*4882a593Smuzhiyun 
3301*4882a593Smuzhiyun const char *
xf86ConnectorGetName(xf86ConnectorType connector)3302*4882a593Smuzhiyun xf86ConnectorGetName(xf86ConnectorType connector)
3303*4882a593Smuzhiyun {
3304*4882a593Smuzhiyun     return _xf86ConnectorNames[connector];
3305*4882a593Smuzhiyun }
3306*4882a593Smuzhiyun 
3307*4882a593Smuzhiyun #ifdef XV
3308*4882a593Smuzhiyun static void
x86_crtc_box_intersect(BoxPtr dest,BoxPtr a,BoxPtr b)3309*4882a593Smuzhiyun x86_crtc_box_intersect(BoxPtr dest, BoxPtr a, BoxPtr b)
3310*4882a593Smuzhiyun {
3311*4882a593Smuzhiyun     dest->x1 = a->x1 > b->x1 ? a->x1 : b->x1;
3312*4882a593Smuzhiyun     dest->x2 = a->x2 < b->x2 ? a->x2 : b->x2;
3313*4882a593Smuzhiyun     dest->y1 = a->y1 > b->y1 ? a->y1 : b->y1;
3314*4882a593Smuzhiyun     dest->y2 = a->y2 < b->y2 ? a->y2 : b->y2;
3315*4882a593Smuzhiyun 
3316*4882a593Smuzhiyun     if (dest->x1 >= dest->x2 || dest->y1 >= dest->y2)
3317*4882a593Smuzhiyun         dest->x1 = dest->x2 = dest->y1 = dest->y2 = 0;
3318*4882a593Smuzhiyun }
3319*4882a593Smuzhiyun 
3320*4882a593Smuzhiyun static void
x86_crtc_box(xf86CrtcPtr crtc,BoxPtr crtc_box)3321*4882a593Smuzhiyun x86_crtc_box(xf86CrtcPtr crtc, BoxPtr crtc_box)
3322*4882a593Smuzhiyun {
3323*4882a593Smuzhiyun     if (crtc->enabled) {
3324*4882a593Smuzhiyun         crtc_box->x1 = crtc->x;
3325*4882a593Smuzhiyun         crtc_box->x2 = crtc->x + xf86ModeWidth(&crtc->mode, crtc->rotation);
3326*4882a593Smuzhiyun         crtc_box->y1 = crtc->y;
3327*4882a593Smuzhiyun         crtc_box->y2 = crtc->y + xf86ModeHeight(&crtc->mode, crtc->rotation);
3328*4882a593Smuzhiyun     }
3329*4882a593Smuzhiyun     else
3330*4882a593Smuzhiyun         crtc_box->x1 = crtc_box->x2 = crtc_box->y1 = crtc_box->y2 = 0;
3331*4882a593Smuzhiyun }
3332*4882a593Smuzhiyun 
3333*4882a593Smuzhiyun static int
xf86_crtc_box_area(BoxPtr box)3334*4882a593Smuzhiyun xf86_crtc_box_area(BoxPtr box)
3335*4882a593Smuzhiyun {
3336*4882a593Smuzhiyun     return (int) (box->x2 - box->x1) * (int) (box->y2 - box->y1);
3337*4882a593Smuzhiyun }
3338*4882a593Smuzhiyun 
3339*4882a593Smuzhiyun /*
3340*4882a593Smuzhiyun  * Return the crtc covering 'box'. If two crtcs cover a portion of
3341*4882a593Smuzhiyun  * 'box', then prefer 'desired'. If 'desired' is NULL, then prefer the crtc
3342*4882a593Smuzhiyun  * with greater coverage
3343*4882a593Smuzhiyun  */
3344*4882a593Smuzhiyun 
3345*4882a593Smuzhiyun static xf86CrtcPtr
xf86_covering_crtc(ScrnInfoPtr pScrn,BoxPtr box,xf86CrtcPtr desired,BoxPtr crtc_box_ret)3346*4882a593Smuzhiyun xf86_covering_crtc(ScrnInfoPtr pScrn,
3347*4882a593Smuzhiyun                    BoxPtr box, xf86CrtcPtr desired, BoxPtr crtc_box_ret)
3348*4882a593Smuzhiyun {
3349*4882a593Smuzhiyun     xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
3350*4882a593Smuzhiyun     xf86CrtcPtr crtc, best_crtc;
3351*4882a593Smuzhiyun     int coverage, best_coverage;
3352*4882a593Smuzhiyun     int c;
3353*4882a593Smuzhiyun     BoxRec crtc_box, cover_box;
3354*4882a593Smuzhiyun 
3355*4882a593Smuzhiyun     best_crtc = NULL;
3356*4882a593Smuzhiyun     best_coverage = 0;
3357*4882a593Smuzhiyun     crtc_box_ret->x1 = 0;
3358*4882a593Smuzhiyun     crtc_box_ret->x2 = 0;
3359*4882a593Smuzhiyun     crtc_box_ret->y1 = 0;
3360*4882a593Smuzhiyun     crtc_box_ret->y2 = 0;
3361*4882a593Smuzhiyun     for (c = 0; c < xf86_config->num_crtc; c++) {
3362*4882a593Smuzhiyun         crtc = xf86_config->crtc[c];
3363*4882a593Smuzhiyun         x86_crtc_box(crtc, &crtc_box);
3364*4882a593Smuzhiyun         x86_crtc_box_intersect(&cover_box, &crtc_box, box);
3365*4882a593Smuzhiyun         coverage = xf86_crtc_box_area(&cover_box);
3366*4882a593Smuzhiyun         if (coverage && crtc == desired) {
3367*4882a593Smuzhiyun             *crtc_box_ret = crtc_box;
3368*4882a593Smuzhiyun             return crtc;
3369*4882a593Smuzhiyun         }
3370*4882a593Smuzhiyun         else if (coverage > best_coverage) {
3371*4882a593Smuzhiyun             *crtc_box_ret = crtc_box;
3372*4882a593Smuzhiyun             best_crtc = crtc;
3373*4882a593Smuzhiyun             best_coverage = coverage;
3374*4882a593Smuzhiyun         }
3375*4882a593Smuzhiyun     }
3376*4882a593Smuzhiyun     return best_crtc;
3377*4882a593Smuzhiyun }
3378*4882a593Smuzhiyun 
3379*4882a593Smuzhiyun /*
3380*4882a593Smuzhiyun  * For overlay video, compute the relevant CRTC and
3381*4882a593Smuzhiyun  * clip video to that.
3382*4882a593Smuzhiyun  *
3383*4882a593Smuzhiyun  * returning FALSE means there was a memory failure of some kind,
3384*4882a593Smuzhiyun  * not that the video shouldn't be displayed
3385*4882a593Smuzhiyun  */
3386*4882a593Smuzhiyun 
3387*4882a593Smuzhiyun Bool
xf86_crtc_clip_video_helper(ScrnInfoPtr pScrn,xf86CrtcPtr * crtc_ret,xf86CrtcPtr desired_crtc,BoxPtr dst,INT32 * xa,INT32 * xb,INT32 * ya,INT32 * yb,RegionPtr reg,INT32 width,INT32 height)3388*4882a593Smuzhiyun xf86_crtc_clip_video_helper(ScrnInfoPtr pScrn,
3389*4882a593Smuzhiyun                             xf86CrtcPtr * crtc_ret,
3390*4882a593Smuzhiyun                             xf86CrtcPtr desired_crtc,
3391*4882a593Smuzhiyun                             BoxPtr dst,
3392*4882a593Smuzhiyun                             INT32 *xa,
3393*4882a593Smuzhiyun                             INT32 *xb,
3394*4882a593Smuzhiyun                             INT32 *ya,
3395*4882a593Smuzhiyun                             INT32 *yb, RegionPtr reg, INT32 width, INT32 height)
3396*4882a593Smuzhiyun {
3397*4882a593Smuzhiyun     Bool ret;
3398*4882a593Smuzhiyun     RegionRec crtc_region_local;
3399*4882a593Smuzhiyun     RegionPtr crtc_region = reg;
3400*4882a593Smuzhiyun 
3401*4882a593Smuzhiyun     if (crtc_ret) {
3402*4882a593Smuzhiyun         BoxRec crtc_box;
3403*4882a593Smuzhiyun         xf86CrtcPtr crtc = xf86_covering_crtc(pScrn, dst,
3404*4882a593Smuzhiyun                                               desired_crtc,
3405*4882a593Smuzhiyun                                               &crtc_box);
3406*4882a593Smuzhiyun 
3407*4882a593Smuzhiyun         if (crtc) {
3408*4882a593Smuzhiyun             RegionInit(&crtc_region_local, &crtc_box, 1);
3409*4882a593Smuzhiyun             crtc_region = &crtc_region_local;
3410*4882a593Smuzhiyun             RegionIntersect(crtc_region, crtc_region, reg);
3411*4882a593Smuzhiyun         }
3412*4882a593Smuzhiyun         *crtc_ret = crtc;
3413*4882a593Smuzhiyun     }
3414*4882a593Smuzhiyun 
3415*4882a593Smuzhiyun     ret = xf86XVClipVideoHelper(dst, xa, xb, ya, yb,
3416*4882a593Smuzhiyun                                 crtc_region, width, height);
3417*4882a593Smuzhiyun 
3418*4882a593Smuzhiyun     if (crtc_region != reg)
3419*4882a593Smuzhiyun         RegionUninit(&crtc_region_local);
3420*4882a593Smuzhiyun 
3421*4882a593Smuzhiyun     return ret;
3422*4882a593Smuzhiyun }
3423*4882a593Smuzhiyun #endif
3424*4882a593Smuzhiyun 
3425*4882a593Smuzhiyun xf86_crtc_notify_proc_ptr
xf86_wrap_crtc_notify(ScreenPtr screen,xf86_crtc_notify_proc_ptr new)3426*4882a593Smuzhiyun xf86_wrap_crtc_notify(ScreenPtr screen, xf86_crtc_notify_proc_ptr new)
3427*4882a593Smuzhiyun {
3428*4882a593Smuzhiyun     if (xf86CrtcConfigPrivateIndex != -1) {
3429*4882a593Smuzhiyun         ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
3430*4882a593Smuzhiyun         xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
3431*4882a593Smuzhiyun         xf86_crtc_notify_proc_ptr old;
3432*4882a593Smuzhiyun 
3433*4882a593Smuzhiyun         old = config->xf86_crtc_notify;
3434*4882a593Smuzhiyun         config->xf86_crtc_notify = new;
3435*4882a593Smuzhiyun         return old;
3436*4882a593Smuzhiyun     }
3437*4882a593Smuzhiyun     return NULL;
3438*4882a593Smuzhiyun }
3439*4882a593Smuzhiyun 
3440*4882a593Smuzhiyun void
xf86_unwrap_crtc_notify(ScreenPtr screen,xf86_crtc_notify_proc_ptr old)3441*4882a593Smuzhiyun xf86_unwrap_crtc_notify(ScreenPtr screen, xf86_crtc_notify_proc_ptr old)
3442*4882a593Smuzhiyun {
3443*4882a593Smuzhiyun     if (xf86CrtcConfigPrivateIndex != -1) {
3444*4882a593Smuzhiyun         ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
3445*4882a593Smuzhiyun         xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
3446*4882a593Smuzhiyun 
3447*4882a593Smuzhiyun         config->xf86_crtc_notify = old;
3448*4882a593Smuzhiyun     }
3449*4882a593Smuzhiyun }
3450*4882a593Smuzhiyun 
3451*4882a593Smuzhiyun void
xf86_crtc_notify(ScreenPtr screen)3452*4882a593Smuzhiyun xf86_crtc_notify(ScreenPtr screen)
3453*4882a593Smuzhiyun {
3454*4882a593Smuzhiyun     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
3455*4882a593Smuzhiyun     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
3456*4882a593Smuzhiyun 
3457*4882a593Smuzhiyun     if (config->xf86_crtc_notify)
3458*4882a593Smuzhiyun         config->xf86_crtc_notify(screen);
3459*4882a593Smuzhiyun }
3460*4882a593Smuzhiyun 
3461*4882a593Smuzhiyun Bool
xf86_crtc_supports_gamma(ScrnInfoPtr pScrn)3462*4882a593Smuzhiyun xf86_crtc_supports_gamma(ScrnInfoPtr pScrn)
3463*4882a593Smuzhiyun {
3464*4882a593Smuzhiyun     if (xf86CrtcConfigPrivateIndex != -1) {
3465*4882a593Smuzhiyun         xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
3466*4882a593Smuzhiyun         xf86CrtcPtr crtc;
3467*4882a593Smuzhiyun 
3468*4882a593Smuzhiyun         /* for multiple drivers loaded we need this */
3469*4882a593Smuzhiyun         if (!xf86_config)
3470*4882a593Smuzhiyun             return FALSE;
3471*4882a593Smuzhiyun         if (xf86_config->num_crtc == 0)
3472*4882a593Smuzhiyun             return FALSE;
3473*4882a593Smuzhiyun         crtc = xf86_config->crtc[0];
3474*4882a593Smuzhiyun 
3475*4882a593Smuzhiyun         return crtc->funcs->gamma_set != NULL;
3476*4882a593Smuzhiyun     }
3477*4882a593Smuzhiyun 
3478*4882a593Smuzhiyun     return FALSE;
3479*4882a593Smuzhiyun }
3480*4882a593Smuzhiyun 
3481*4882a593Smuzhiyun void
xf86ProviderSetup(ScrnInfoPtr scrn,const xf86ProviderFuncsRec * funcs,const char * name)3482*4882a593Smuzhiyun xf86ProviderSetup(ScrnInfoPtr scrn,
3483*4882a593Smuzhiyun                   const xf86ProviderFuncsRec *funcs, const char *name)
3484*4882a593Smuzhiyun {
3485*4882a593Smuzhiyun     xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
3486*4882a593Smuzhiyun 
3487*4882a593Smuzhiyun     assert(!xf86_config->name);
3488*4882a593Smuzhiyun     assert(name);
3489*4882a593Smuzhiyun 
3490*4882a593Smuzhiyun     xf86_config->name = strdup(name);
3491*4882a593Smuzhiyun     xf86_config->provider_funcs = funcs;
3492*4882a593Smuzhiyun #ifdef RANDR_12_INTERFACE
3493*4882a593Smuzhiyun     xf86_config->randr_provider = NULL;
3494*4882a593Smuzhiyun #endif
3495*4882a593Smuzhiyun }
3496*4882a593Smuzhiyun 
3497*4882a593Smuzhiyun void
xf86DetachAllCrtc(ScrnInfoPtr scrn)3498*4882a593Smuzhiyun xf86DetachAllCrtc(ScrnInfoPtr scrn)
3499*4882a593Smuzhiyun {
3500*4882a593Smuzhiyun         xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
3501*4882a593Smuzhiyun         int i;
3502*4882a593Smuzhiyun 
3503*4882a593Smuzhiyun         for (i = 0; i < xf86_config->num_crtc; i++) {
3504*4882a593Smuzhiyun             xf86CrtcPtr crtc = xf86_config->crtc[i];
3505*4882a593Smuzhiyun 
3506*4882a593Smuzhiyun             if (crtc->randr_crtc)
3507*4882a593Smuzhiyun                 RRCrtcDetachScanoutPixmap(crtc->randr_crtc);
3508*4882a593Smuzhiyun 
3509*4882a593Smuzhiyun             /* dpms off */
3510*4882a593Smuzhiyun             xf86DisableCrtc(crtc);
3511*4882a593Smuzhiyun             /* force a reset the next time its used */
3512*4882a593Smuzhiyun             crtc->randr_crtc->mode = NULL;
3513*4882a593Smuzhiyun             crtc->mode.HDisplay = 0;
3514*4882a593Smuzhiyun             crtc->x = crtc->y = 0;
3515*4882a593Smuzhiyun         }
3516*4882a593Smuzhiyun }
3517