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 = {
75 /* let miPointerInitialize take care of these */
80 static xf86ScreenLayoutRec xf86ScreenLayout[MAXSCREENS];
82 static Bool HardEdges;
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.
92 xf86InitViewport(ScrnInfoPtr pScr)
95 pScr->PointerMoved = xf86PointerMoved;
98 * Compute the initial Viewport if necessary
101 if (pScr->display->frameX0 < 0) {
102 pScr->frameX0 = (pScr->virtualX - pScr->modes->HDisplay) / 2;
103 pScr->frameY0 = (pScr->virtualY - pScr->modes->VDisplay) / 2;
106 pScr->frameX0 = pScr->display->frameX0;
107 pScr->frameY0 = pScr->display->frameY0;
111 pScr->frameX1 = pScr->frameX0 + pScr->modes->HDisplay - 1;
112 pScr->frameY1 = pScr->frameY0 + pScr->modes->VDisplay - 1;
115 * Now adjust the initial Viewport, so it lies within the virtual area
117 if (pScr->frameX1 >= pScr->virtualX) {
118 pScr->frameX0 = pScr->virtualX - pScr->modes->HDisplay;
119 pScr->frameX1 = pScr->frameX0 + pScr->modes->HDisplay - 1;
122 if (pScr->frameY1 >= pScr->virtualY) {
123 pScr->frameY0 = pScr->virtualY - pScr->modes->VDisplay;
124 pScr->frameY1 = pScr->frameY0 + pScr->modes->VDisplay - 1;
130 * Scroll the visual part of the screen so the pointer is visible.
134 xf86SetViewport(ScreenPtr pScreen, int x, int y)
136 ScrnInfoPtr pScr = xf86ScreenToScrn(pScreen);
138 (*pScr->PointerMoved) (pScr, x, y);
142 xf86PointerMoved(ScrnInfoPtr pScr, int x, int y)
144 Bool frameChanged = FALSE;
147 * check wether (x,y) belongs to the visual part of the screen
148 * if not, change the base of the displayed frame accoring
150 if (pScr->frameX0 > x) {
152 pScr->frameX1 = x + pScr->currentMode->HDisplay - 1;
156 if (pScr->frameX1 < x) {
157 pScr->frameX1 = x + 1;
158 pScr->frameX0 = x - pScr->currentMode->HDisplay + 1;
162 if (pScr->frameY0 > y) {
164 pScr->frameY1 = y + pScr->currentMode->VDisplay - 1;
168 if (pScr->frameY1 < y) {
170 pScr->frameY0 = y - pScr->currentMode->VDisplay + 1;
174 if (frameChanged && pScr->AdjustFrame != NULL)
175 pScr->AdjustFrame(pScr, pScr->frameX0, pScr->frameY0);
180 * Enable/disable ZoomViewport
184 xf86LockZoom(ScreenPtr pScreen, Bool lock)
186 ScrnInfoPtr pScr = xf86ScreenToScrn(pScreen);
187 pScr->zoomLocked = lock;
192 * This is called by both keyboard processing and the VidMode extension to
197 xf86SwitchMode(ScreenPtr pScreen, DisplayModePtr mode)
199 ScrnInfoPtr pScr = xf86ScreenToScrn(pScreen);
200 ScreenPtr pCursorScreen;
203 DeviceIntPtr dev, it;
205 if (!pScr->vtSema || !mode || !pScr->SwitchMode)
209 if (DGAActive(pScr->scrnIndex))
213 if (mode == pScr->currentMode)
216 if (mode->HDisplay > pScr->virtualX || mode->VDisplay > pScr->virtualY)
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.
222 for (dev = inputInfo.devices; dev; dev = dev->next) {
223 if (IsPointerDevice(dev) && dev->spriteInfo->spriteOwner)
227 pCursorScreen = miPointerGetScreen(dev);
228 if (pScreen == pCursorScreen)
229 miPointerGetPosition(dev, &px, &py);
232 Switched = (*pScr->SwitchMode) (pScr, mode);
234 pScr->currentMode = mode;
237 * Adjust frame for new display size.
238 * Frame is centered around cursor position if cursor is on same screen.
240 if (pScreen == pCursorScreen)
241 pScr->frameX0 = px - (mode->HDisplay / 2) + 1;
244 (pScr->frameX0 + pScr->frameX1 + 1 - mode->HDisplay) / 2;
246 if (pScr->frameX0 < 0)
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;
255 if (pScreen == pCursorScreen)
256 pScr->frameY0 = py - (mode->VDisplay / 2) + 1;
259 (pScr->frameY0 + pScr->frameY1 + 1 - mode->VDisplay) / 2;
261 if (pScr->frameY0 < 0)
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;
272 if (pScr->AdjustFrame)
273 (*pScr->AdjustFrame) (pScr, pScr->frameX0, pScr->frameY0);
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).
281 if (pScreen == pCursorScreen) {
282 xf86WarpCursor(dev, pScreen, px, py);
285 for (it = inputInfo.devices; it; it = it->next) {
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)
295 else if (px > pScr->frameX1)
298 if (py < pScr->frameY0)
300 else if (py > pScr->frameY1)
303 xf86WarpCursor(it, pScreen, px, py);
312 * xf86ZoomViewport --
313 * Reinitialize the visual part of the screen for another mode.
317 xf86ZoomViewport(ScreenPtr pScreen, int zoom)
319 ScrnInfoPtr pScr = xf86ScreenToScrn(pScreen);
322 if (pScr->zoomLocked || !(mode = pScr->currentMode))
330 } while (mode != pScr->currentMode && !(mode->type & M_T_USERDEF));
332 (void) xf86SwitchMode(pScreen, mode);
336 FindEdge(xf86EdgePtr edge, int val)
338 while (edge && (edge->end <= val))
341 if (edge && (edge->start <= val))
348 * xf86CursorOffScreen --
349 * Check whether it is necessary to switch to another screen
353 xf86CursorOffScreen(ScreenPtr *pScreen, int *x, int *y)
358 if (screenInfo.numScreens == 1)
365 if (tmp >= (*pScreen)->height)
366 tmp = (*pScreen)->height - 1;
368 if ((edge = xf86ScreenLayout[(*pScreen)->myNum].left))
369 edge = FindEdge(edge, tmp);
374 *x += edge->offset.x;
375 *y += edge->offset.y;
376 *pScreen = xf86Screens[edge->screen]->pScreen;
380 if (*x >= (*pScreen)->width) {
384 if (tmp >= (*pScreen)->height)
385 tmp = (*pScreen)->height - 1;
387 if ((edge = xf86ScreenLayout[(*pScreen)->myNum].right))
388 edge = FindEdge(edge, tmp);
391 *x = (*pScreen)->width - 1;
393 *x += edge->offset.x;
394 *y += edge->offset.y;
395 *pScreen = xf86Screens[edge->screen]->pScreen;
403 if (tmp >= (*pScreen)->width)
404 tmp = (*pScreen)->width - 1;
406 if ((edge = xf86ScreenLayout[(*pScreen)->myNum].up))
407 edge = FindEdge(edge, tmp);
412 *x += edge->offset.x;
413 *y += edge->offset.y;
414 *pScreen = xf86Screens[edge->screen]->pScreen;
418 if (*y >= (*pScreen)->height) {
422 if (tmp >= (*pScreen)->width)
423 tmp = (*pScreen)->width - 1;
425 if ((edge = xf86ScreenLayout[(*pScreen)->myNum].down))
426 edge = FindEdge(edge, tmp);
429 *y = (*pScreen)->height - 1;
431 *x += edge->offset.x;
432 *y += edge->offset.y;
433 (*pScreen) = xf86Screens[edge->screen]->pScreen;
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);
452 * Switch to another screen
454 * Currently nothing special happens, but mi assumes the CrossScreen
459 xf86CrossScreen(ScreenPtr pScreen, Bool entering)
465 * Warp possible to another screen
470 xf86WarpCursor(DeviceIntPtr pDev, ScreenPtr pScreen, int x, int y)
473 miPointerWarpCursor(pDev, pScreen, x, y);
475 xf86Info.currentScreen = pScreen;
480 xf86GetPointerScreenFuncs(void)
482 return (void *) &xf86PointerScreenFuncs;
486 AddEdge(xf86EdgePtr edge,
487 short min, short max, short dx, short dy, short screen)
489 xf86EdgePtr pEdge = edge, pPrev = NULL, pNew;
492 while (pEdge && (min >= pEdge->end)) {
498 if (!(pNew = malloc(sizeof(xf86EdgeRec))))
501 pNew->screen = screen;
515 else if (min < pEdge->start) {
516 if (!(pNew = malloc(sizeof(xf86EdgeRec))))
519 pNew->screen = screen;
530 if (max <= pEdge->start) {
535 pNew->end = pEdge->start;
553 FillOutEdge(xf86EdgePtr pEdge, int limit)
558 if (pEdge->start > 0)
561 while ((pNext = pEdge->next)) {
562 diff = pNext->start - pEdge->end;
564 pEdge->end += diff >> 1;
565 pNext->start -= diff - (diff >> 1);
570 if (pEdge->end < limit)
575 * xf86InitOrigins() can deal with a maximum of 32 screens
576 * on 32 bit architectures, 64 on 64 bit architectures.
580 xf86InitOrigins(void)
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;
590 /* need to have this set up with a config file option */
593 memset(xf86ScreenLayout, 0, MAXSCREENS * sizeof(xf86ScreenLayoutRec));
595 screensLeft = prevScreensLeft = (1 << xf86NumScreens) - 1;
598 for (mask = screensLeft, i = 0; mask; mask >>= 1, i++) {
602 screen = &xf86ConfigLayout.screens[i];
604 if (screen->refscreen != NULL &&
605 screen->refscreen->screennum >= xf86NumScreens) {
606 screensLeft &= ~(1 << i);
608 "Not including screen \"%s\" in origins calculation.\n",
613 pScreen = xf86Screens[i]->pScreen;
614 switch (screen->where) {
616 OldStyleConfig = TRUE;
617 pLayout = &xf86ScreenLayout[i];
618 /* force edge lists */
620 ref = screen->left->screennum;
621 if (!xf86Screens[ref] || !xf86Screens[ref]->pScreen) {
622 ErrorF("Referenced uninitialized screen in Layout!\n");
625 pLayout->left = AddEdge(pLayout->left,
627 xf86Screens[ref]->pScreen->width, 0,
631 ref = screen->right->screennum;
632 if (!xf86Screens[ref] || !xf86Screens[ref]->pScreen) {
633 ErrorF("Referenced uninitialized screen in Layout!\n");
636 pLayout->right = AddEdge(pLayout->right,
638 -pScreen->width, 0, ref);
641 ref = screen->top->screennum;
642 if (!xf86Screens[ref] || !xf86Screens[ref]->pScreen) {
643 ErrorF("Referenced uninitialized screen in Layout!\n");
646 pLayout->up = AddEdge(pLayout->up,
648 0, xf86Screens[ref]->pScreen->height,
651 if (screen->bottom) {
652 ref = screen->bottom->screennum;
653 if (!xf86Screens[ref] || !xf86Screens[ref]->pScreen) {
654 ErrorF("Referenced uninitialized screen in Layout!\n");
657 pLayout->down = AddEdge(pLayout->down,
658 0, pScreen->width, 0,
659 -pScreen->height, ref);
661 /* we could also try to place it based on those
662 relative locations if we wanted to */
663 screen->x = screen->y = 0;
666 pScreen->x = screen->x;
667 pScreen->y = screen->y;
668 screensLeft &= ~(1 << i);
671 ref = screen->refscreen->screennum;
672 if (!xf86Screens[ref] || !xf86Screens[ref]->pScreen) {
673 ErrorF("Referenced uninitialized screen in Layout!\n");
676 if (screensLeft & (1 << ref))
678 refScreen = xf86Screens[ref]->pScreen;
679 pScreen->x = refScreen->x + screen->x;
680 pScreen->y = refScreen->y + screen->y;
681 screensLeft &= ~(1 << i);
684 ref = screen->refscreen->screennum;
685 if (!xf86Screens[ref] || !xf86Screens[ref]->pScreen) {
686 ErrorF("Referenced uninitialized screen in Layout!\n");
689 if (screensLeft & (1 << ref))
691 refScreen = xf86Screens[ref]->pScreen;
692 pScreen->x = refScreen->x + refScreen->width;
693 pScreen->y = refScreen->y;
694 screensLeft &= ~(1 << i);
697 ref = screen->refscreen->screennum;
698 if (!xf86Screens[ref] || !xf86Screens[ref]->pScreen) {
699 ErrorF("Referenced uninitialized screen in Layout!\n");
702 if (screensLeft & (1 << ref))
704 refScreen = xf86Screens[ref]->pScreen;
705 pScreen->x = refScreen->x - pScreen->width;
706 pScreen->y = refScreen->y;
707 screensLeft &= ~(1 << i);
710 ref = screen->refscreen->screennum;
711 if (!xf86Screens[ref] || !xf86Screens[ref]->pScreen) {
712 ErrorF("Referenced uninitialized screen in Layout!\n");
715 if (screensLeft & (1 << ref))
717 refScreen = xf86Screens[ref]->pScreen;
718 pScreen->x = refScreen->x;
719 pScreen->y = refScreen->y + refScreen->height;
720 screensLeft &= ~(1 << i);
723 ref = screen->refscreen->screennum;
724 if (!xf86Screens[ref] || !xf86Screens[ref]->pScreen) {
725 ErrorF("Referenced uninitialized screen in Layout!\n");
728 if (screensLeft & (1 << ref))
730 refScreen = xf86Screens[ref]->pScreen;
731 pScreen->x = refScreen->x;
732 pScreen->y = refScreen->y - pScreen->height;
733 screensLeft &= ~(1 << i);
736 ErrorF("Illegal placement keyword in Layout!\n");
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 */
749 while (!((1 << i) & screensLeft)) {
753 ref = xf86ConfigLayout.screens[i].refscreen->screennum;
754 xf86Screens[ref]->pScreen->x = xf86Screens[ref]->pScreen->y = 0;
755 screensLeft &= ~(1 << ref);
758 prevScreensLeft = screensLeft;
761 /* justify the topmost and leftmost to (0,0) */
762 minX = xf86Screens[0]->pScreen->x;
763 minY = xf86Screens[0]->pScreen->y;
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;
773 for (i = 0; i < xf86NumScreens; i++) {
774 xf86Screens[i]->pScreen->x -= minX;
775 xf86Screens[i]->pScreen->y -= minY;
779 /* Create the edge lists */
781 if (!OldStyleConfig) {
782 for (i = 0; i < xf86NumScreens; i++) {
783 pLayout = &xf86ScreenLayout[i];
785 pScreen = xf86Screens[i]->pScreen;
788 right = left + pScreen->width;
790 bottom = top + pScreen->height;
792 for (j = 0; j < xf86NumScreens; j++) {
796 refScreen = xf86Screens[j]->pScreen;
799 x2 = x1 + refScreen->width;
801 y2 = y1 + refScreen->height;
803 if ((bottom > y1) && (top < y2)) {
807 max = pScreen->height - (bottom - y2);
808 if (max > pScreen->height)
809 max = pScreen->height;
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);
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);
822 if ((left < x2) && (right > x1)) {
826 max = pScreen->width - (right - x2);
827 if (max > pScreen->width)
828 max = pScreen->width;
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);
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);
844 if (!HardEdges && !OldStyleConfig) {
845 for (i = 0; i < xf86NumScreens; i++) {
846 pLayout = &xf86ScreenLayout[i];
847 pScreen = xf86Screens[i]->pScreen;
849 FillOutEdge(pLayout->left, pScreen->height);
851 FillOutEdge(pLayout->right, pScreen->height);
853 FillOutEdge(pLayout->up, pScreen->width);
855 FillOutEdge(pLayout->down, pScreen->width);
859 update_desktop_dimensions();
863 xf86ReconfigureLayout(void)
867 for (i = 0; i < MAXSCREENS; i++) {
868 xf86ScreenLayoutPtr sl = &xf86ScreenLayout[i];
870 /* we don't have to zero these, xf86InitOrigins() takes care of that */