2 * Copyright (c) 1994-2003 by The XFree86 Project, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
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).
28 #ifdef HAVE_XORG_CONFIG_H
29 #include <xorg-config.h>
36 #include "mipointer.h"
37 #include "scrnintstr.h"
44 #include "xf86_OSproc.h"
46 #include <X11/extensions/XIproto.h>
47 #include "xf86Xinput.h"
53 typedef struct _xf86EdgeRec
{
58 struct _xf86EdgeRec
*next
;
59 } xf86EdgeRec
, *xf86EdgePtr
;
62 xf86EdgePtr left
, right
, up
, down
;
63 } xf86ScreenLayoutRec
, *xf86ScreenLayoutPtr
;
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
);
69 static void xf86PointerMoved(ScrnInfoPtr pScrn
, int x
, int y
);
71 static miPointerScreenFuncRec xf86PointerScreenFuncs
= {
77 static xf86ScreenLayoutRec xf86ScreenLayout
[MAXSCREENS
];
79 static Bool HardEdges
;
83 * Initialize paning & zooming parameters, so that a driver must only
84 * check what resolutions are possible and whether the virtual area
85 * is valid if specified.
89 xf86InitViewport(ScrnInfoPtr pScr
)
92 pScr
->PointerMoved
= xf86PointerMoved
;
95 * Compute the initial Viewport if necessary
98 if (pScr
->display
->frameX0
< 0) {
99 pScr
->frameX0
= (pScr
->virtualX
- pScr
->modes
->HDisplay
) / 2;
100 pScr
->frameY0
= (pScr
->virtualY
- pScr
->modes
->VDisplay
) / 2;
103 pScr
->frameX0
= pScr
->display
->frameX0
;
104 pScr
->frameY0
= pScr
->display
->frameY0
;
108 pScr
->frameX1
= pScr
->frameX0
+ pScr
->modes
->HDisplay
- 1;
109 pScr
->frameY1
= pScr
->frameY0
+ pScr
->modes
->VDisplay
- 1;
112 * Now adjust the initial Viewport, so it lies within the virtual area
114 if (pScr
->frameX1
>= pScr
->virtualX
) {
115 pScr
->frameX0
= pScr
->virtualX
- pScr
->modes
->HDisplay
;
116 pScr
->frameX1
= pScr
->frameX0
+ pScr
->modes
->HDisplay
- 1;
119 if (pScr
->frameY1
>= pScr
->virtualY
) {
120 pScr
->frameY0
= pScr
->virtualY
- pScr
->modes
->VDisplay
;
121 pScr
->frameY1
= pScr
->frameY0
+ pScr
->modes
->VDisplay
- 1;
127 * Scroll the visual part of the screen so the pointer is visible.
131 xf86SetViewport(ScreenPtr pScreen
, int x
, int y
)
133 ScrnInfoPtr pScr
= xf86ScreenToScrn(pScreen
);
135 (*pScr
->PointerMoved
) (pScr
, x
, y
);
139 xf86PointerMoved(ScrnInfoPtr pScr
, int x
, int y
)
141 Bool frameChanged
= FALSE
;
144 * check wether (x,y) belongs to the visual part of the screen
145 * if not, change the base of the displayed frame accoring
147 if (pScr
->frameX0
> x
) {
149 pScr
->frameX1
= x
+ pScr
->currentMode
->HDisplay
- 1;
153 if (pScr
->frameX1
< x
) {
154 pScr
->frameX1
= x
+ 1;
155 pScr
->frameX0
= x
- pScr
->currentMode
->HDisplay
+ 1;
159 if (pScr
->frameY0
> y
) {
161 pScr
->frameY1
= y
+ pScr
->currentMode
->VDisplay
- 1;
165 if (pScr
->frameY1
< y
) {
167 pScr
->frameY0
= y
- pScr
->currentMode
->VDisplay
+ 1;
171 if (frameChanged
&& pScr
->AdjustFrame
!= NULL
)
172 pScr
->AdjustFrame(pScr
, pScr
->frameX0
, pScr
->frameY0
);
177 * Enable/disable ZoomViewport
181 xf86LockZoom(ScreenPtr pScreen
, Bool lock
)
183 ScrnInfoPtr pScr
= xf86ScreenToScrn(pScreen
);
184 pScr
->zoomLocked
= lock
;
189 * This is called by both keyboard processing and the VidMode extension to
194 xf86SwitchMode(ScreenPtr pScreen
, DisplayModePtr mode
)
196 ScrnInfoPtr pScr
= xf86ScreenToScrn(pScreen
);
197 ScreenPtr pCursorScreen
;
200 DeviceIntPtr dev
, it
;
202 if (!pScr
->vtSema
|| !mode
|| !pScr
->SwitchMode
)
206 if (DGAActive(pScr
->scrnIndex
))
210 if (mode
== pScr
->currentMode
)
213 if (mode
->HDisplay
> pScr
->virtualX
|| mode
->VDisplay
> pScr
->virtualY
)
216 /* Let's take an educated guess for which pointer to take here. And about as
217 educated as it gets is to take the first pointer we find.
219 for (dev
= inputInfo
.devices
; dev
; dev
= dev
->next
) {
220 if (IsPointerDevice(dev
) && dev
->spriteInfo
->spriteOwner
)
224 pCursorScreen
= miPointerGetScreen(dev
);
225 if (pScreen
== pCursorScreen
)
226 miPointerGetPosition(dev
, &px
, &py
);
229 Switched
= (*pScr
->SwitchMode
) (pScr
, mode
);
231 pScr
->currentMode
= mode
;
234 * Adjust frame for new display size.
235 * Frame is centered around cursor position if cursor is on same screen.
237 if (pScreen
== pCursorScreen
)
238 pScr
->frameX0
= px
- (mode
->HDisplay
/ 2) + 1;
241 (pScr
->frameX0
+ pScr
->frameX1
+ 1 - mode
->HDisplay
) / 2;
243 if (pScr
->frameX0
< 0)
246 pScr
->frameX1
= pScr
->frameX0
+ mode
->HDisplay
- 1;
247 if (pScr
->frameX1
>= pScr
->virtualX
) {
248 pScr
->frameX0
= pScr
->virtualX
- mode
->HDisplay
;
249 pScr
->frameX1
= pScr
->virtualX
- 1;
252 if (pScreen
== pCursorScreen
)
253 pScr
->frameY0
= py
- (mode
->VDisplay
/ 2) + 1;
256 (pScr
->frameY0
+ pScr
->frameY1
+ 1 - mode
->VDisplay
) / 2;
258 if (pScr
->frameY0
< 0)
261 pScr
->frameY1
= pScr
->frameY0
+ mode
->VDisplay
- 1;
262 if (pScr
->frameY1
>= pScr
->virtualY
) {
263 pScr
->frameY0
= pScr
->virtualY
- mode
->VDisplay
;
264 pScr
->frameY1
= pScr
->virtualY
- 1;
269 if (pScr
->AdjustFrame
)
270 (*pScr
->AdjustFrame
) (pScr
, pScr
->frameX0
, pScr
->frameY0
);
272 /* The original code centered the frame around the cursor if possible.
273 * Since this is hard to achieve with multiple cursors, we do the following:
274 * - center around the first pointer
275 * - move all other pointers to the nearest edge on the screen (or leave
276 * them unmodified if they are within the boundaries).
278 if (pScreen
== pCursorScreen
) {
279 xf86WarpCursor(dev
, pScreen
, px
, py
);
282 for (it
= inputInfo
.devices
; it
; it
= it
->next
) {
286 if (IsPointerDevice(it
) && it
->spriteInfo
->spriteOwner
) {
287 pCursorScreen
= miPointerGetScreen(it
);
288 if (pScreen
== pCursorScreen
) {
289 miPointerGetPosition(it
, &px
, &py
);
290 if (px
< pScr
->frameX0
)
292 else if (px
> pScr
->frameX1
)
295 if (py
< pScr
->frameY0
)
297 else if (py
> pScr
->frameY1
)
300 xf86WarpCursor(it
, pScreen
, px
, py
);
309 * xf86ZoomViewport --
310 * Reinitialize the visual part of the screen for another mode.
314 xf86ZoomViewport(ScreenPtr pScreen
, int zoom
)
316 ScrnInfoPtr pScr
= xf86ScreenToScrn(pScreen
);
319 if (pScr
->zoomLocked
|| !(mode
= pScr
->currentMode
))
327 } while (mode
!= pScr
->currentMode
&& !(mode
->type
& M_T_USERDEF
));
329 (void) xf86SwitchMode(pScreen
, mode
);
333 FindEdge(xf86EdgePtr edge
, int val
)
335 while (edge
&& (edge
->end
<= val
))
338 if (edge
&& (edge
->start
<= val
))
345 * xf86CursorOffScreen --
346 * Check whether it is necessary to switch to another screen
350 xf86CursorOffScreen(ScreenPtr
*pScreen
, int *x
, int *y
)
355 if (screenInfo
.numScreens
== 1)
362 if (tmp
>= (*pScreen
)->height
)
363 tmp
= (*pScreen
)->height
- 1;
365 if ((edge
= xf86ScreenLayout
[(*pScreen
)->myNum
].left
))
366 edge
= FindEdge(edge
, tmp
);
371 *x
+= edge
->offset
.x
;
372 *y
+= edge
->offset
.y
;
373 *pScreen
= xf86Screens
[edge
->screen
]->pScreen
;
377 if (*x
>= (*pScreen
)->width
) {
381 if (tmp
>= (*pScreen
)->height
)
382 tmp
= (*pScreen
)->height
- 1;
384 if ((edge
= xf86ScreenLayout
[(*pScreen
)->myNum
].right
))
385 edge
= FindEdge(edge
, tmp
);
388 *x
= (*pScreen
)->width
- 1;
390 *x
+= edge
->offset
.x
;
391 *y
+= edge
->offset
.y
;
392 *pScreen
= xf86Screens
[edge
->screen
]->pScreen
;
400 if (tmp
>= (*pScreen
)->width
)
401 tmp
= (*pScreen
)->width
- 1;
403 if ((edge
= xf86ScreenLayout
[(*pScreen
)->myNum
].up
))
404 edge
= FindEdge(edge
, tmp
);
409 *x
+= edge
->offset
.x
;
410 *y
+= edge
->offset
.y
;
411 *pScreen
= xf86Screens
[edge
->screen
]->pScreen
;
415 if (*y
>= (*pScreen
)->height
) {
419 if (tmp
>= (*pScreen
)->width
)
420 tmp
= (*pScreen
)->width
- 1;
422 if ((edge
= xf86ScreenLayout
[(*pScreen
)->myNum
].down
))
423 edge
= FindEdge(edge
, tmp
);
426 *y
= (*pScreen
)->height
- 1;
428 *x
+= edge
->offset
.x
;
429 *y
+= edge
->offset
.y
;
430 (*pScreen
) = xf86Screens
[edge
->screen
]->pScreen
;
435 /* This presents problems for overlapping screens when
436 HardEdges is used. Have to think about the logic more */
437 if ((*x
< 0) || (*x
>= (*pScreen
)->width
) ||
438 (*y
< 0) || (*y
>= (*pScreen
)->height
)) {
439 /* We may have crossed more than one screen */
440 xf86CursorOffScreen(pScreen
, x
, y
);
449 * Switch to another screen
451 * Currently nothing special happens, but mi assumes the CrossScreen
456 xf86CrossScreen(ScreenPtr pScreen
, Bool entering
)
462 * Warp possible to another screen
467 xf86WarpCursor(DeviceIntPtr pDev
, ScreenPtr pScreen
, int x
, int y
)
470 miPointerWarpCursor(pDev
, pScreen
, x
, y
);
472 xf86Info
.currentScreen
= pScreen
;
477 xf86GetPointerScreenFuncs(void)
479 return (void *) &xf86PointerScreenFuncs
;
483 AddEdge(xf86EdgePtr edge
,
484 short min
, short max
, short dx
, short dy
, short screen
)
486 xf86EdgePtr pEdge
= edge
, pPrev
= NULL
, pNew
;
489 while (pEdge
&& (min
>= pEdge
->end
)) {
495 if (!(pNew
= malloc(sizeof(xf86EdgeRec
))))
498 pNew
->screen
= screen
;
512 else if (min
< pEdge
->start
) {
513 if (!(pNew
= malloc(sizeof(xf86EdgeRec
))))
516 pNew
->screen
= screen
;
527 if (max
<= pEdge
->start
) {
532 pNew
->end
= pEdge
->start
;
550 FillOutEdge(xf86EdgePtr pEdge
, int limit
)
555 if (pEdge
->start
> 0)
558 while ((pNext
= pEdge
->next
)) {
559 diff
= pNext
->start
- pEdge
->end
;
561 pEdge
->end
+= diff
>> 1;
562 pNext
->start
-= diff
- (diff
>> 1);
567 if (pEdge
->end
< limit
)
572 * xf86InitOrigins() can deal with a maximum of 32 screens
573 * on 32 bit architectures, 64 on 64 bit architectures.
577 xf86InitOrigins(void)
579 unsigned long screensLeft
, prevScreensLeft
, mask
;
580 screenLayoutPtr screen
;
581 ScreenPtr pScreen
, refScreen
;
582 int x1
, x2
, y1
, y2
, left
, right
, top
, bottom
;
583 int i
, j
, ref
, minX
, minY
, min
, max
;
584 xf86ScreenLayoutPtr pLayout
;
585 Bool OldStyleConfig
= FALSE
;
587 /* need to have this set up with a config file option */
590 memset(xf86ScreenLayout
, 0, MAXSCREENS
* sizeof(xf86ScreenLayoutRec
));
592 screensLeft
= prevScreensLeft
= (1 << xf86NumScreens
) - 1;
595 for (mask
= screensLeft
, i
= 0; mask
; mask
>>= 1, i
++) {
599 screen
= &xf86ConfigLayout
.screens
[i
];
601 if (screen
->refscreen
!= NULL
&&
602 screen
->refscreen
->screennum
>= xf86NumScreens
) {
603 screensLeft
&= ~(1 << i
);
605 "Not including screen \"%s\" in origins calculation.\n",
610 pScreen
= xf86Screens
[i
]->pScreen
;
611 switch (screen
->where
) {
613 OldStyleConfig
= TRUE
;
614 pLayout
= &xf86ScreenLayout
[i
];
615 /* force edge lists */
617 ref
= screen
->left
->screennum
;
618 if (!xf86Screens
[ref
] || !xf86Screens
[ref
]->pScreen
) {
619 ErrorF("Referenced uninitialized screen in Layout!\n");
622 pLayout
->left
= AddEdge(pLayout
->left
,
624 xf86Screens
[ref
]->pScreen
->width
, 0,
628 ref
= screen
->right
->screennum
;
629 if (!xf86Screens
[ref
] || !xf86Screens
[ref
]->pScreen
) {
630 ErrorF("Referenced uninitialized screen in Layout!\n");
633 pLayout
->right
= AddEdge(pLayout
->right
,
635 -pScreen
->width
, 0, ref
);
638 ref
= screen
->top
->screennum
;
639 if (!xf86Screens
[ref
] || !xf86Screens
[ref
]->pScreen
) {
640 ErrorF("Referenced uninitialized screen in Layout!\n");
643 pLayout
->up
= AddEdge(pLayout
->up
,
645 0, xf86Screens
[ref
]->pScreen
->height
,
648 if (screen
->bottom
) {
649 ref
= screen
->bottom
->screennum
;
650 if (!xf86Screens
[ref
] || !xf86Screens
[ref
]->pScreen
) {
651 ErrorF("Referenced uninitialized screen in Layout!\n");
654 pLayout
->down
= AddEdge(pLayout
->down
,
655 0, pScreen
->width
, 0,
656 -pScreen
->height
, ref
);
658 /* we could also try to place it based on those
659 relative locations if we wanted to */
660 screen
->x
= screen
->y
= 0;
663 pScreen
->x
= screen
->x
;
664 pScreen
->y
= screen
->y
;
665 screensLeft
&= ~(1 << i
);
668 ref
= screen
->refscreen
->screennum
;
669 if (!xf86Screens
[ref
] || !xf86Screens
[ref
]->pScreen
) {
670 ErrorF("Referenced uninitialized screen in Layout!\n");
673 if (screensLeft
& (1 << ref
))
675 refScreen
= xf86Screens
[ref
]->pScreen
;
676 pScreen
->x
= refScreen
->x
+ screen
->x
;
677 pScreen
->y
= refScreen
->y
+ screen
->y
;
678 screensLeft
&= ~(1 << i
);
681 ref
= screen
->refscreen
->screennum
;
682 if (!xf86Screens
[ref
] || !xf86Screens
[ref
]->pScreen
) {
683 ErrorF("Referenced uninitialized screen in Layout!\n");
686 if (screensLeft
& (1 << ref
))
688 refScreen
= xf86Screens
[ref
]->pScreen
;
689 pScreen
->x
= refScreen
->x
+ refScreen
->width
;
690 pScreen
->y
= refScreen
->y
;
691 screensLeft
&= ~(1 << i
);
694 ref
= screen
->refscreen
->screennum
;
695 if (!xf86Screens
[ref
] || !xf86Screens
[ref
]->pScreen
) {
696 ErrorF("Referenced uninitialized screen in Layout!\n");
699 if (screensLeft
& (1 << ref
))
701 refScreen
= xf86Screens
[ref
]->pScreen
;
702 pScreen
->x
= refScreen
->x
- pScreen
->width
;
703 pScreen
->y
= refScreen
->y
;
704 screensLeft
&= ~(1 << i
);
707 ref
= screen
->refscreen
->screennum
;
708 if (!xf86Screens
[ref
] || !xf86Screens
[ref
]->pScreen
) {
709 ErrorF("Referenced uninitialized screen in Layout!\n");
712 if (screensLeft
& (1 << ref
))
714 refScreen
= xf86Screens
[ref
]->pScreen
;
715 pScreen
->x
= refScreen
->x
;
716 pScreen
->y
= refScreen
->y
+ refScreen
->height
;
717 screensLeft
&= ~(1 << i
);
720 ref
= screen
->refscreen
->screennum
;
721 if (!xf86Screens
[ref
] || !xf86Screens
[ref
]->pScreen
) {
722 ErrorF("Referenced uninitialized screen in Layout!\n");
725 if (screensLeft
& (1 << ref
))
727 refScreen
= xf86Screens
[ref
]->pScreen
;
728 pScreen
->x
= refScreen
->x
;
729 pScreen
->y
= refScreen
->y
- pScreen
->height
;
730 screensLeft
&= ~(1 << i
);
733 ErrorF("Illegal placement keyword in Layout!\n");
742 if (screensLeft
== prevScreensLeft
) {
743 /* All the remaining screens are referencing each other.
744 Assign a value to one of them and go through again */
746 while (!((1 << i
) & screensLeft
)) {
750 ref
= xf86ConfigLayout
.screens
[i
].refscreen
->screennum
;
751 xf86Screens
[ref
]->pScreen
->x
= xf86Screens
[ref
]->pScreen
->y
= 0;
752 screensLeft
&= ~(1 << ref
);
755 prevScreensLeft
= screensLeft
;
758 /* justify the topmost and leftmost to (0,0) */
759 minX
= xf86Screens
[0]->pScreen
->x
;
760 minY
= xf86Screens
[0]->pScreen
->y
;
762 for (i
= 1; i
< xf86NumScreens
; i
++) {
763 if (xf86Screens
[i
]->pScreen
->x
< minX
)
764 minX
= xf86Screens
[i
]->pScreen
->x
;
765 if (xf86Screens
[i
]->pScreen
->y
< minY
)
766 minY
= xf86Screens
[i
]->pScreen
->y
;
770 for (i
= 0; i
< xf86NumScreens
; i
++) {
771 xf86Screens
[i
]->pScreen
->x
-= minX
;
772 xf86Screens
[i
]->pScreen
->y
-= minY
;
776 /* Create the edge lists */
778 if (!OldStyleConfig
) {
779 for (i
= 0; i
< xf86NumScreens
; i
++) {
780 pLayout
= &xf86ScreenLayout
[i
];
782 pScreen
= xf86Screens
[i
]->pScreen
;
785 right
= left
+ pScreen
->width
;
787 bottom
= top
+ pScreen
->height
;
789 for (j
= 0; j
< xf86NumScreens
; j
++) {
793 refScreen
= xf86Screens
[j
]->pScreen
;
796 x2
= x1
+ refScreen
->width
;
798 y2
= y1
+ refScreen
->height
;
800 if ((bottom
> y1
) && (top
< y2
)) {
804 max
= pScreen
->height
- (bottom
- y2
);
805 if (max
> pScreen
->height
)
806 max
= pScreen
->height
;
808 if (((left
- 1) >= x1
) && ((left
- 1) < x2
))
809 pLayout
->left
= AddEdge(pLayout
->left
, min
, max
,
810 pScreen
->x
- refScreen
->x
,
811 pScreen
->y
- refScreen
->y
, j
);
813 if ((right
>= x1
) && (right
< x2
))
814 pLayout
->right
= AddEdge(pLayout
->right
, min
, max
,
815 pScreen
->x
- refScreen
->x
,
816 pScreen
->y
- refScreen
->y
, j
);
819 if ((left
< x2
) && (right
> x1
)) {
823 max
= pScreen
->width
- (right
- x2
);
824 if (max
> pScreen
->width
)
825 max
= pScreen
->width
;
827 if (((top
- 1) >= y1
) && ((top
- 1) < y2
))
828 pLayout
->up
= AddEdge(pLayout
->up
, min
, max
,
829 pScreen
->x
- refScreen
->x
,
830 pScreen
->y
- refScreen
->y
, j
);
832 if ((bottom
>= y1
) && (bottom
< y2
))
833 pLayout
->down
= AddEdge(pLayout
->down
, min
, max
,
834 pScreen
->x
- refScreen
->x
,
835 pScreen
->y
- refScreen
->y
, j
);
841 if (!HardEdges
&& !OldStyleConfig
) {
842 for (i
= 0; i
< xf86NumScreens
; i
++) {
843 pLayout
= &xf86ScreenLayout
[i
];
844 pScreen
= xf86Screens
[i
]->pScreen
;
846 FillOutEdge(pLayout
->left
, pScreen
->height
);
848 FillOutEdge(pLayout
->right
, pScreen
->height
);
850 FillOutEdge(pLayout
->up
, pScreen
->width
);
852 FillOutEdge(pLayout
->down
, pScreen
->width
);
856 update_desktop_dimensions();
860 xf86ReconfigureLayout(void)
864 for (i
= 0; i
< MAXSCREENS
; i
++) {
865 xf86ScreenLayoutPtr sl
= &xf86ScreenLayout
[i
];
867 /* we don't have to zero these, xf86InitOrigins() takes care of that */