upgrade for xorg-server 1.12.99.905 (for 1.13 RC)
[framework/uifw/xorg/server/xorg-server.git] / hw / xfree86 / common / xf86Cursor.c
1 /*
2  * Copyright (c) 1994-2003 by The XFree86 Project, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Except as contained in this notice, the name of the copyright holder(s)
23  * and author(s) shall not be used in advertising or otherwise to promote
24  * the sale, use or other dealings in this Software without prior written
25  * authorization from the copyright holder(s) and author(s).
26  */
27
28 #ifdef HAVE_XORG_CONFIG_H
29 #include <xorg-config.h>
30 #endif
31
32 #include <X11/X.h>
33 #include <X11/Xmd.h>
34 #include "input.h"
35 #include "cursor.h"
36 #include "mipointer.h"
37 #include "scrnintstr.h"
38 #include "globals.h"
39
40 #include "compiler.h"
41
42 #include "xf86.h"
43 #include "xf86Priv.h"
44 #include "xf86_OSproc.h"
45
46 #include <X11/extensions/XIproto.h>
47 #include "xf86Xinput.h"
48
49 #ifdef XFreeXDGA
50 #include "dgaproc.h"
51 #endif
52
53 typedef struct _xf86EdgeRec {
54     short screen;
55     short start;
56     short end;
57     DDXPointRec offset;
58     struct _xf86EdgeRec *next;
59 } xf86EdgeRec, *xf86EdgePtr;
60
61 typedef struct {
62     xf86EdgePtr left, right, up, down;
63 } xf86ScreenLayoutRec, *xf86ScreenLayoutPtr;
64
65 static Bool xf86CursorOffScreen(ScreenPtr *pScreen, int *x, int *y);
66 static void xf86CrossScreen(ScreenPtr pScreen, Bool entering);
67 static void xf86WarpCursor(DeviceIntPtr pDev, ScreenPtr pScreen, int x, int y);
68
69 static void xf86PointerMoved(ScrnInfoPtr pScrn, int x, int y);
70
71 static miPointerScreenFuncRec xf86PointerScreenFuncs = {
72     xf86CursorOffScreen,
73     xf86CrossScreen,
74     xf86WarpCursor,
75     /* let miPointerInitialize take care of these */
76     NULL,
77     NULL
78 };
79
80 static xf86ScreenLayoutRec xf86ScreenLayout[MAXSCREENS];
81
82 static Bool HardEdges;
83
84 /*
85  * xf86InitViewport --
86  *      Initialize paning & zooming parameters, so that a driver must only
87  *      check what resolutions are possible and whether the virtual area
88  *      is valid if specified.
89  */
90
91 void
92 xf86InitViewport(ScrnInfoPtr pScr)
93 {
94
95     pScr->PointerMoved = xf86PointerMoved;
96
97     /*
98      * Compute the initial Viewport if necessary
99      */
100     if (pScr->display) {
101         if (pScr->display->frameX0 < 0) {
102             pScr->frameX0 = (pScr->virtualX - pScr->modes->HDisplay) / 2;
103             pScr->frameY0 = (pScr->virtualY - pScr->modes->VDisplay) / 2;
104         }
105         else {
106             pScr->frameX0 = pScr->display->frameX0;
107             pScr->frameY0 = pScr->display->frameY0;
108         }
109     }
110
111     pScr->frameX1 = pScr->frameX0 + pScr->modes->HDisplay - 1;
112     pScr->frameY1 = pScr->frameY0 + pScr->modes->VDisplay - 1;
113
114     /*
115      * Now adjust the initial Viewport, so it lies within the virtual area
116      */
117     if (pScr->frameX1 >= pScr->virtualX) {
118         pScr->frameX0 = pScr->virtualX - pScr->modes->HDisplay;
119         pScr->frameX1 = pScr->frameX0 + pScr->modes->HDisplay - 1;
120     }
121
122     if (pScr->frameY1 >= pScr->virtualY) {
123         pScr->frameY0 = pScr->virtualY - pScr->modes->VDisplay;
124         pScr->frameY1 = pScr->frameY0 + pScr->modes->VDisplay - 1;
125     }
126 }
127
128 /*
129  * xf86SetViewport --
130  *      Scroll the visual part of the screen so the pointer is visible.
131  */
132
133 void
134 xf86SetViewport(ScreenPtr pScreen, int x, int y)
135 {
136     ScrnInfoPtr pScr = xf86ScreenToScrn(pScreen);
137
138     (*pScr->PointerMoved) (pScr, x, y);
139 }
140
141 static void
142 xf86PointerMoved(ScrnInfoPtr pScr, int x, int y)
143 {
144     Bool frameChanged = FALSE;
145
146     /*
147      * check wether (x,y) belongs to the visual part of the screen
148      * if not, change the base of the displayed frame accoring
149      */
150     if (pScr->frameX0 > x) {
151         pScr->frameX0 = x;
152         pScr->frameX1 = x + pScr->currentMode->HDisplay - 1;
153         frameChanged = TRUE;
154     }
155
156     if (pScr->frameX1 < x) {
157         pScr->frameX1 = x + 1;
158         pScr->frameX0 = x - pScr->currentMode->HDisplay + 1;
159         frameChanged = TRUE;
160     }
161
162     if (pScr->frameY0 > y) {
163         pScr->frameY0 = y;
164         pScr->frameY1 = y + pScr->currentMode->VDisplay - 1;
165         frameChanged = TRUE;
166     }
167
168     if (pScr->frameY1 < y) {
169         pScr->frameY1 = y;
170         pScr->frameY0 = y - pScr->currentMode->VDisplay + 1;
171         frameChanged = TRUE;
172     }
173
174     if (frameChanged && pScr->AdjustFrame != NULL)
175         pScr->AdjustFrame(pScr, pScr->frameX0, pScr->frameY0);
176 }
177
178 /*
179  * xf86LockZoom --
180  *      Enable/disable ZoomViewport
181  */
182
183 void
184 xf86LockZoom(ScreenPtr pScreen, Bool lock)
185 {
186     ScrnInfoPtr pScr = xf86ScreenToScrn(pScreen);
187     pScr->zoomLocked = lock;
188 }
189
190 /*
191  * xf86SwitchMode --
192  *      This is called by both keyboard processing and the VidMode extension to
193  *      set a new mode.
194  */
195
196 Bool
197 xf86SwitchMode(ScreenPtr pScreen, DisplayModePtr mode)
198 {
199     ScrnInfoPtr pScr = xf86ScreenToScrn(pScreen);
200     ScreenPtr pCursorScreen;
201     Bool Switched;
202     int px, py;
203     DeviceIntPtr dev, it;
204
205     if (!pScr->vtSema || !mode || !pScr->SwitchMode)
206         return FALSE;
207
208 #ifdef XFreeXDGA
209     if (DGAActive(pScr->scrnIndex))
210         return FALSE;
211 #endif
212
213     if (mode == pScr->currentMode)
214         return TRUE;
215
216     if (mode->HDisplay > pScr->virtualX || mode->VDisplay > pScr->virtualY)
217         return FALSE;
218
219     /* Let's take an educated guess for which pointer to take here. And about as
220        educated as it gets is to take the first pointer we find.
221      */
222     for (dev = inputInfo.devices; dev; dev = dev->next) {
223         if (IsPointerDevice(dev) && dev->spriteInfo->spriteOwner)
224             break;
225     }
226
227     pCursorScreen = miPointerGetScreen(dev);
228     if (pScreen == pCursorScreen)
229         miPointerGetPosition(dev, &px, &py);
230
231     OsBlockSIGIO();
232     Switched = (*pScr->SwitchMode) (pScr, mode);
233     if (Switched) {
234         pScr->currentMode = mode;
235
236         /*
237          * Adjust frame for new display size.
238          * Frame is centered around cursor position if cursor is on same screen.
239          */
240         if (pScreen == pCursorScreen)
241             pScr->frameX0 = px - (mode->HDisplay / 2) + 1;
242         else
243             pScr->frameX0 =
244                 (pScr->frameX0 + pScr->frameX1 + 1 - mode->HDisplay) / 2;
245
246         if (pScr->frameX0 < 0)
247             pScr->frameX0 = 0;
248
249         pScr->frameX1 = pScr->frameX0 + mode->HDisplay - 1;
250         if (pScr->frameX1 >= pScr->virtualX) {
251             pScr->frameX0 = pScr->virtualX - mode->HDisplay;
252             pScr->frameX1 = pScr->virtualX - 1;
253         }
254
255         if (pScreen == pCursorScreen)
256             pScr->frameY0 = py - (mode->VDisplay / 2) + 1;
257         else
258             pScr->frameY0 =
259                 (pScr->frameY0 + pScr->frameY1 + 1 - mode->VDisplay) / 2;
260
261         if (pScr->frameY0 < 0)
262             pScr->frameY0 = 0;
263
264         pScr->frameY1 = pScr->frameY0 + mode->VDisplay - 1;
265         if (pScr->frameY1 >= pScr->virtualY) {
266             pScr->frameY0 = pScr->virtualY - mode->VDisplay;
267             pScr->frameY1 = pScr->virtualY - 1;
268         }
269     }
270     OsReleaseSIGIO();
271
272     if (pScr->AdjustFrame)
273         (*pScr->AdjustFrame) (pScr, pScr->frameX0, pScr->frameY0);
274
275     /* The original code centered the frame around the cursor if possible.
276      * Since this is hard to achieve with multiple cursors, we do the following:
277      *   - center around the first pointer
278      *   - move all other pointers to the nearest edge on the screen (or leave
279      *   them unmodified if they are within the boundaries).
280      */
281     if (pScreen == pCursorScreen) {
282         xf86WarpCursor(dev, pScreen, px, py);
283     }
284
285     for (it = inputInfo.devices; it; it = it->next) {
286         if (it == dev)
287             continue;
288
289         if (IsPointerDevice(it) && it->spriteInfo->spriteOwner) {
290             pCursorScreen = miPointerGetScreen(it);
291             if (pScreen == pCursorScreen) {
292                 miPointerGetPosition(it, &px, &py);
293                 if (px < pScr->frameX0)
294                     px = pScr->frameX0;
295                 else if (px > pScr->frameX1)
296                     px = pScr->frameX1;
297
298                 if (py < pScr->frameY0)
299                     py = pScr->frameY0;
300                 else if (py > pScr->frameY1)
301                     py = pScr->frameY1;
302
303                 xf86WarpCursor(it, pScreen, px, py);
304             }
305         }
306     }
307
308     return Switched;
309 }
310
311 /*
312  * xf86ZoomViewport --
313  *      Reinitialize the visual part of the screen for another mode.
314  */
315
316 void
317 xf86ZoomViewport(ScreenPtr pScreen, int zoom)
318 {
319     ScrnInfoPtr pScr = xf86ScreenToScrn(pScreen);
320     DisplayModePtr mode;
321
322     if (pScr->zoomLocked || !(mode = pScr->currentMode))
323         return;
324
325     do {
326         if (zoom > 0)
327             mode = mode->next;
328         else
329             mode = mode->prev;
330     } while (mode != pScr->currentMode && !(mode->type & M_T_USERDEF));
331
332     (void) xf86SwitchMode(pScreen, mode);
333 }
334
335 static xf86EdgePtr
336 FindEdge(xf86EdgePtr edge, int val)
337 {
338     while (edge && (edge->end <= val))
339         edge = edge->next;
340
341     if (edge && (edge->start <= val))
342         return edge;
343
344     return NULL;
345 }
346
347 /*
348  * xf86CursorOffScreen --
349  *      Check whether it is necessary to switch to another screen
350  */
351
352 static Bool
353 xf86CursorOffScreen(ScreenPtr *pScreen, int *x, int *y)
354 {
355     xf86EdgePtr edge;
356     int tmp;
357
358     if (screenInfo.numScreens == 1)
359         return FALSE;
360
361     if (*x < 0) {
362         tmp = *y;
363         if (tmp < 0)
364             tmp = 0;
365         if (tmp >= (*pScreen)->height)
366             tmp = (*pScreen)->height - 1;
367
368         if ((edge = xf86ScreenLayout[(*pScreen)->myNum].left))
369             edge = FindEdge(edge, tmp);
370
371         if (!edge)
372             *x = 0;
373         else {
374             *x += edge->offset.x;
375             *y += edge->offset.y;
376             *pScreen = xf86Screens[edge->screen]->pScreen;
377         }
378     }
379
380     if (*x >= (*pScreen)->width) {
381         tmp = *y;
382         if (tmp < 0)
383             tmp = 0;
384         if (tmp >= (*pScreen)->height)
385             tmp = (*pScreen)->height - 1;
386
387         if ((edge = xf86ScreenLayout[(*pScreen)->myNum].right))
388             edge = FindEdge(edge, tmp);
389
390         if (!edge)
391             *x = (*pScreen)->width - 1;
392         else {
393             *x += edge->offset.x;
394             *y += edge->offset.y;
395             *pScreen = xf86Screens[edge->screen]->pScreen;
396         }
397     }
398
399     if (*y < 0) {
400         tmp = *x;
401         if (tmp < 0)
402             tmp = 0;
403         if (tmp >= (*pScreen)->width)
404             tmp = (*pScreen)->width - 1;
405
406         if ((edge = xf86ScreenLayout[(*pScreen)->myNum].up))
407             edge = FindEdge(edge, tmp);
408
409         if (!edge)
410             *y = 0;
411         else {
412             *x += edge->offset.x;
413             *y += edge->offset.y;
414             *pScreen = xf86Screens[edge->screen]->pScreen;
415         }
416     }
417
418     if (*y >= (*pScreen)->height) {
419         tmp = *x;
420         if (tmp < 0)
421             tmp = 0;
422         if (tmp >= (*pScreen)->width)
423             tmp = (*pScreen)->width - 1;
424
425         if ((edge = xf86ScreenLayout[(*pScreen)->myNum].down))
426             edge = FindEdge(edge, tmp);
427
428         if (!edge)
429             *y = (*pScreen)->height - 1;
430         else {
431             *x += edge->offset.x;
432             *y += edge->offset.y;
433             (*pScreen) = xf86Screens[edge->screen]->pScreen;
434         }
435     }
436
437 #if 0
438     /* This presents problems for overlapping screens when
439        HardEdges is used.  Have to think about the logic more */
440     if ((*x < 0) || (*x >= (*pScreen)->width) ||
441         (*y < 0) || (*y >= (*pScreen)->height)) {
442         /* We may have crossed more than one screen */
443         xf86CursorOffScreen(pScreen, x, y);
444     }
445 #endif
446
447     return TRUE;
448 }
449
450 /*
451  * xf86CrossScreen --
452  *      Switch to another screen
453  *
454  *      Currently nothing special happens, but mi assumes the CrossScreen
455  *      method exists.
456  */
457
458 static void
459 xf86CrossScreen(ScreenPtr pScreen, Bool entering)
460 {
461 }
462
463 /*
464  * xf86WarpCursor --
465  *      Warp possible to another screen
466  */
467
468 /* ARGSUSED */
469 static void
470 xf86WarpCursor(DeviceIntPtr pDev, ScreenPtr pScreen, int x, int y)
471 {
472     OsBlockSIGIO();
473     miPointerWarpCursor(pDev, pScreen, x, y);
474
475     xf86Info.currentScreen = pScreen;
476     OsReleaseSIGIO();
477 }
478
479 void *
480 xf86GetPointerScreenFuncs(void)
481 {
482     return (void *) &xf86PointerScreenFuncs;
483 }
484
485 static xf86EdgePtr
486 AddEdge(xf86EdgePtr edge,
487         short min, short max, short dx, short dy, short screen)
488 {
489     xf86EdgePtr pEdge = edge, pPrev = NULL, pNew;
490
491     while (1) {
492         while (pEdge && (min >= pEdge->end)) {
493             pPrev = pEdge;
494             pEdge = pEdge->next;
495         }
496
497         if (!pEdge) {
498             if (!(pNew = malloc(sizeof(xf86EdgeRec))))
499                 break;
500
501             pNew->screen = screen;
502             pNew->start = min;
503             pNew->end = max;
504             pNew->offset.x = dx;
505             pNew->offset.y = dy;
506             pNew->next = NULL;
507
508             if (pPrev)
509                 pPrev->next = pNew;
510             else
511                 edge = pNew;
512
513             break;
514         }
515         else if (min < pEdge->start) {
516             if (!(pNew = malloc(sizeof(xf86EdgeRec))))
517                 break;
518
519             pNew->screen = screen;
520             pNew->start = min;
521             pNew->offset.x = dx;
522             pNew->offset.y = dy;
523             pNew->next = pEdge;
524
525             if (pPrev)
526                 pPrev->next = pNew;
527             else
528                 edge = pNew;
529
530             if (max <= pEdge->start) {
531                 pNew->end = max;
532                 break;
533             }
534             else {
535                 pNew->end = pEdge->start;
536                 min = pEdge->end;
537             }
538         }
539         else
540             min = pEdge->end;
541
542         pPrev = pEdge;
543         pEdge = pEdge->next;
544
545         if (max <= min)
546             break;
547     }
548
549     return edge;
550 }
551
552 static void
553 FillOutEdge(xf86EdgePtr pEdge, int limit)
554 {
555     xf86EdgePtr pNext;
556     int diff;
557
558     if (pEdge->start > 0)
559         pEdge->start = 0;
560
561     while ((pNext = pEdge->next)) {
562         diff = pNext->start - pEdge->end;
563         if (diff > 0) {
564             pEdge->end += diff >> 1;
565             pNext->start -= diff - (diff >> 1);
566         }
567         pEdge = pNext;
568     }
569
570     if (pEdge->end < limit)
571         pEdge->end = limit;
572 }
573
574 /*
575  * xf86InitOrigins() can deal with a maximum of 32 screens
576  * on 32 bit architectures, 64 on 64 bit architectures.
577  */
578
579 void
580 xf86InitOrigins(void)
581 {
582     unsigned long screensLeft, prevScreensLeft, mask;
583     screenLayoutPtr screen;
584     ScreenPtr pScreen, refScreen;
585     int x1, x2, y1, y2, left, right, top, bottom;
586     int i, j, ref, minX, minY, min, max;
587     xf86ScreenLayoutPtr pLayout;
588     Bool OldStyleConfig = FALSE;
589
590     /* need to have this set up with a config file option */
591     HardEdges = FALSE;
592
593     memset(xf86ScreenLayout, 0, MAXSCREENS * sizeof(xf86ScreenLayoutRec));
594
595     screensLeft = prevScreensLeft = (1 << xf86NumScreens) - 1;
596
597     while (1) {
598         for (mask = screensLeft, i = 0; mask; mask >>= 1, i++) {
599             if (!(mask & 1L))
600                 continue;
601
602             screen = &xf86ConfigLayout.screens[i];
603
604             if (screen->refscreen != NULL &&
605                 screen->refscreen->screennum >= xf86NumScreens) {
606                 screensLeft &= ~(1 << i);
607                 xf86Msg(X_WARNING,
608                         "Not including screen \"%s\" in origins calculation.\n",
609                         screen->screen->id);
610                 continue;
611             }
612
613             pScreen = xf86Screens[i]->pScreen;
614             switch (screen->where) {
615             case PosObsolete:
616                 OldStyleConfig = TRUE;
617                 pLayout = &xf86ScreenLayout[i];
618                 /* force edge lists */
619                 if (screen->left) {
620                     ref = screen->left->screennum;
621                     if (!xf86Screens[ref] || !xf86Screens[ref]->pScreen) {
622                         ErrorF("Referenced uninitialized screen in Layout!\n");
623                         break;
624                     }
625                     pLayout->left = AddEdge(pLayout->left,
626                                             0, pScreen->height,
627                                             xf86Screens[ref]->pScreen->width, 0,
628                                             ref);
629                 }
630                 if (screen->right) {
631                     ref = screen->right->screennum;
632                     if (!xf86Screens[ref] || !xf86Screens[ref]->pScreen) {
633                         ErrorF("Referenced uninitialized screen in Layout!\n");
634                         break;
635                     }
636                     pLayout->right = AddEdge(pLayout->right,
637                                              0, pScreen->height,
638                                              -pScreen->width, 0, ref);
639                 }
640                 if (screen->top) {
641                     ref = screen->top->screennum;
642                     if (!xf86Screens[ref] || !xf86Screens[ref]->pScreen) {
643                         ErrorF("Referenced uninitialized screen in Layout!\n");
644                         break;
645                     }
646                     pLayout->up = AddEdge(pLayout->up,
647                                           0, pScreen->width,
648                                           0, xf86Screens[ref]->pScreen->height,
649                                           ref);
650                 }
651                 if (screen->bottom) {
652                     ref = screen->bottom->screennum;
653                     if (!xf86Screens[ref] || !xf86Screens[ref]->pScreen) {
654                         ErrorF("Referenced uninitialized screen in Layout!\n");
655                         break;
656                     }
657                     pLayout->down = AddEdge(pLayout->down,
658                                             0, pScreen->width, 0,
659                                             -pScreen->height, ref);
660                 }
661                 /* we could also try to place it based on those
662                    relative locations if we wanted to */
663                 screen->x = screen->y = 0;
664                 /* FALLTHROUGH */
665             case PosAbsolute:
666                 pScreen->x = screen->x;
667                 pScreen->y = screen->y;
668                 screensLeft &= ~(1 << i);
669                 break;
670             case PosRelative:
671                 ref = screen->refscreen->screennum;
672                 if (!xf86Screens[ref] || !xf86Screens[ref]->pScreen) {
673                     ErrorF("Referenced uninitialized screen in Layout!\n");
674                     break;
675                 }
676                 if (screensLeft & (1 << ref))
677                     break;
678                 refScreen = xf86Screens[ref]->pScreen;
679                 pScreen->x = refScreen->x + screen->x;
680                 pScreen->y = refScreen->y + screen->y;
681                 screensLeft &= ~(1 << i);
682                 break;
683             case PosRightOf:
684                 ref = screen->refscreen->screennum;
685                 if (!xf86Screens[ref] || !xf86Screens[ref]->pScreen) {
686                     ErrorF("Referenced uninitialized screen in Layout!\n");
687                     break;
688                 }
689                 if (screensLeft & (1 << ref))
690                     break;
691                 refScreen = xf86Screens[ref]->pScreen;
692                 pScreen->x = refScreen->x + refScreen->width;
693                 pScreen->y = refScreen->y;
694                 screensLeft &= ~(1 << i);
695                 break;
696             case PosLeftOf:
697                 ref = screen->refscreen->screennum;
698                 if (!xf86Screens[ref] || !xf86Screens[ref]->pScreen) {
699                     ErrorF("Referenced uninitialized screen in Layout!\n");
700                     break;
701                 }
702                 if (screensLeft & (1 << ref))
703                     break;
704                 refScreen = xf86Screens[ref]->pScreen;
705                 pScreen->x = refScreen->x - pScreen->width;
706                 pScreen->y = refScreen->y;
707                 screensLeft &= ~(1 << i);
708                 break;
709             case PosBelow:
710                 ref = screen->refscreen->screennum;
711                 if (!xf86Screens[ref] || !xf86Screens[ref]->pScreen) {
712                     ErrorF("Referenced uninitialized screen in Layout!\n");
713                     break;
714                 }
715                 if (screensLeft & (1 << ref))
716                     break;
717                 refScreen = xf86Screens[ref]->pScreen;
718                 pScreen->x = refScreen->x;
719                 pScreen->y = refScreen->y + refScreen->height;
720                 screensLeft &= ~(1 << i);
721                 break;
722             case PosAbove:
723                 ref = screen->refscreen->screennum;
724                 if (!xf86Screens[ref] || !xf86Screens[ref]->pScreen) {
725                     ErrorF("Referenced uninitialized screen in Layout!\n");
726                     break;
727                 }
728                 if (screensLeft & (1 << ref))
729                     break;
730                 refScreen = xf86Screens[ref]->pScreen;
731                 pScreen->x = refScreen->x;
732                 pScreen->y = refScreen->y - pScreen->height;
733                 screensLeft &= ~(1 << i);
734                 break;
735             default:
736                 ErrorF("Illegal placement keyword in Layout!\n");
737                 break;
738             }
739
740         }
741
742         if (!screensLeft)
743             break;
744
745         if (screensLeft == prevScreensLeft) {
746             /* All the remaining screens are referencing each other.
747                Assign a value to one of them and go through again */
748             i = 0;
749             while (!((1 << i) & screensLeft)) {
750                 i++;
751             }
752
753             ref = xf86ConfigLayout.screens[i].refscreen->screennum;
754             xf86Screens[ref]->pScreen->x = xf86Screens[ref]->pScreen->y = 0;
755             screensLeft &= ~(1 << ref);
756         }
757
758         prevScreensLeft = screensLeft;
759     }
760
761     /* justify the topmost and leftmost to (0,0) */
762     minX = xf86Screens[0]->pScreen->x;
763     minY = xf86Screens[0]->pScreen->y;
764
765     for (i = 1; i < xf86NumScreens; i++) {
766         if (xf86Screens[i]->pScreen->x < minX)
767             minX = xf86Screens[i]->pScreen->x;
768         if (xf86Screens[i]->pScreen->y < minY)
769             minY = xf86Screens[i]->pScreen->y;
770     }
771
772     if (minX || minY) {
773         for (i = 0; i < xf86NumScreens; i++) {
774             xf86Screens[i]->pScreen->x -= minX;
775             xf86Screens[i]->pScreen->y -= minY;
776         }
777     }
778
779     /* Create the edge lists */
780
781     if (!OldStyleConfig) {
782         for (i = 0; i < xf86NumScreens; i++) {
783             pLayout = &xf86ScreenLayout[i];
784
785             pScreen = xf86Screens[i]->pScreen;
786
787             left = pScreen->x;
788             right = left + pScreen->width;
789             top = pScreen->y;
790             bottom = top + pScreen->height;
791
792             for (j = 0; j < xf86NumScreens; j++) {
793                 if (i == j)
794                     continue;
795
796                 refScreen = xf86Screens[j]->pScreen;
797
798                 x1 = refScreen->x;
799                 x2 = x1 + refScreen->width;
800                 y1 = refScreen->y;
801                 y2 = y1 + refScreen->height;
802
803                 if ((bottom > y1) && (top < y2)) {
804                     min = y1 - top;
805                     if (min < 0)
806                         min = 0;
807                     max = pScreen->height - (bottom - y2);
808                     if (max > pScreen->height)
809                         max = pScreen->height;
810
811                     if (((left - 1) >= x1) && ((left - 1) < x2))
812                         pLayout->left = AddEdge(pLayout->left, min, max,
813                                                 pScreen->x - refScreen->x,
814                                                 pScreen->y - refScreen->y, j);
815
816                     if ((right >= x1) && (right < x2))
817                         pLayout->right = AddEdge(pLayout->right, min, max,
818                                                  pScreen->x - refScreen->x,
819                                                  pScreen->y - refScreen->y, j);
820                 }
821
822                 if ((left < x2) && (right > x1)) {
823                     min = x1 - left;
824                     if (min < 0)
825                         min = 0;
826                     max = pScreen->width - (right - x2);
827                     if (max > pScreen->width)
828                         max = pScreen->width;
829
830                     if (((top - 1) >= y1) && ((top - 1) < y2))
831                         pLayout->up = AddEdge(pLayout->up, min, max,
832                                               pScreen->x - refScreen->x,
833                                               pScreen->y - refScreen->y, j);
834
835                     if ((bottom >= y1) && (bottom < y2))
836                         pLayout->down = AddEdge(pLayout->down, min, max,
837                                                 pScreen->x - refScreen->x,
838                                                 pScreen->y - refScreen->y, j);
839                 }
840             }
841         }
842     }
843
844     if (!HardEdges && !OldStyleConfig) {
845         for (i = 0; i < xf86NumScreens; i++) {
846             pLayout = &xf86ScreenLayout[i];
847             pScreen = xf86Screens[i]->pScreen;
848             if (pLayout->left)
849                 FillOutEdge(pLayout->left, pScreen->height);
850             if (pLayout->right)
851                 FillOutEdge(pLayout->right, pScreen->height);
852             if (pLayout->up)
853                 FillOutEdge(pLayout->up, pScreen->width);
854             if (pLayout->down)
855                 FillOutEdge(pLayout->down, pScreen->width);
856         }
857     }
858
859     update_desktop_dimensions();
860 }
861
862 void
863 xf86ReconfigureLayout(void)
864 {
865     int i;
866
867     for (i = 0; i < MAXSCREENS; i++) {
868         xf86ScreenLayoutPtr sl = &xf86ScreenLayout[i];
869
870         /* we don't have to zero these, xf86InitOrigins() takes care of that */
871         free(sl->left);
872         free(sl->right);
873         free(sl->up);
874         free(sl->down);
875     }
876
877     xf86InitOrigins();
878 }