1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright © 2006 Keith Packard
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Permission to use, copy, modify, distribute, and sell this software and its
5*4882a593Smuzhiyun * documentation for any purpose is hereby granted without fee, provided that
6*4882a593Smuzhiyun * the above copyright notice appear in all copies and that both that copyright
7*4882a593Smuzhiyun * notice and this permission notice appear in supporting documentation, and
8*4882a593Smuzhiyun * that the name of the copyright holders not be used in advertising or
9*4882a593Smuzhiyun * publicity pertaining to distribution of the software without specific,
10*4882a593Smuzhiyun * written prior permission. The copyright holders make no representations
11*4882a593Smuzhiyun * about the suitability of this software for any purpose. It is provided "as
12*4882a593Smuzhiyun * is" without express or implied warranty.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15*4882a593Smuzhiyun * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16*4882a593Smuzhiyun * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17*4882a593Smuzhiyun * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18*4882a593Smuzhiyun * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19*4882a593Smuzhiyun * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20*4882a593Smuzhiyun * OF THIS SOFTWARE.
21*4882a593Smuzhiyun */
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #include "randrstr.h"
24*4882a593Smuzhiyun #include "inputstr.h"
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun /*
27*4882a593Smuzhiyun * When the pointer moves, check to see if the specified position is outside
28*4882a593Smuzhiyun * any of theavailable CRTCs and move it to a 'sensible' place if so, where
29*4882a593Smuzhiyun * sensible is the closest monitor to the departing edge.
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * Returns whether the position was adjusted
32*4882a593Smuzhiyun */
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun static Bool
RRCrtcContainsPosition(RRCrtcPtr crtc,int x,int y)35*4882a593Smuzhiyun RRCrtcContainsPosition(RRCrtcPtr crtc, int x, int y)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun RRModePtr mode = crtc->mode;
38*4882a593Smuzhiyun int scan_width, scan_height;
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun if (!mode)
41*4882a593Smuzhiyun return FALSE;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun RRCrtcGetScanoutSize(crtc, &scan_width, &scan_height);
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun if (crtc->x <= x && x < crtc->x + scan_width &&
46*4882a593Smuzhiyun crtc->y <= y && y < crtc->y + scan_height)
47*4882a593Smuzhiyun return TRUE;
48*4882a593Smuzhiyun return FALSE;
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun /*
52*4882a593Smuzhiyun * Find the CRTC nearest the specified position, ignoring 'skip'
53*4882a593Smuzhiyun */
54*4882a593Smuzhiyun static void
RRPointerToNearestCrtc(DeviceIntPtr pDev,ScreenPtr pScreen,int x,int y,RRCrtcPtr skip)55*4882a593Smuzhiyun RRPointerToNearestCrtc(DeviceIntPtr pDev, ScreenPtr pScreen, int x, int y,
56*4882a593Smuzhiyun RRCrtcPtr skip)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun rrScrPriv(pScreen);
59*4882a593Smuzhiyun int c;
60*4882a593Smuzhiyun RRCrtcPtr nearest = NULL;
61*4882a593Smuzhiyun int best = 0;
62*4882a593Smuzhiyun int best_dx = 0, best_dy = 0;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun for (c = 0; c < pScrPriv->numCrtcs; c++) {
65*4882a593Smuzhiyun RRCrtcPtr crtc = pScrPriv->crtcs[c];
66*4882a593Smuzhiyun RRModePtr mode = crtc->mode;
67*4882a593Smuzhiyun int dx, dy;
68*4882a593Smuzhiyun int dist;
69*4882a593Smuzhiyun int scan_width, scan_height;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun if (!mode)
72*4882a593Smuzhiyun continue;
73*4882a593Smuzhiyun if (crtc == skip)
74*4882a593Smuzhiyun continue;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun RRCrtcGetScanoutSize(crtc, &scan_width, &scan_height);
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun if (x < crtc->x)
79*4882a593Smuzhiyun dx = crtc->x - x;
80*4882a593Smuzhiyun else if (x > crtc->x + scan_width - 1)
81*4882a593Smuzhiyun dx = crtc->x + (scan_width - 1) - x;
82*4882a593Smuzhiyun else
83*4882a593Smuzhiyun dx = 0;
84*4882a593Smuzhiyun if (y < crtc->y)
85*4882a593Smuzhiyun dy = crtc->y - y;
86*4882a593Smuzhiyun else if (y > crtc->y + scan_height - 1)
87*4882a593Smuzhiyun dy = crtc->y + (scan_height - 1) - y;
88*4882a593Smuzhiyun else
89*4882a593Smuzhiyun dy = 0;
90*4882a593Smuzhiyun dist = dx * dx + dy * dy;
91*4882a593Smuzhiyun if (!nearest || dist < best) {
92*4882a593Smuzhiyun nearest = crtc;
93*4882a593Smuzhiyun best_dx = dx;
94*4882a593Smuzhiyun best_dy = dy;
95*4882a593Smuzhiyun best = dist;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun if (best_dx || best_dy)
99*4882a593Smuzhiyun (*pScreen->SetCursorPosition) (pDev, pScreen, x + best_dx, y + best_dy,
100*4882a593Smuzhiyun TRUE);
101*4882a593Smuzhiyun pScrPriv->pointerCrtc = nearest;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun void
RRPointerMoved(ScreenPtr pScreen,int x,int y)105*4882a593Smuzhiyun RRPointerMoved(ScreenPtr pScreen, int x, int y)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun rrScrPriv(pScreen);
108*4882a593Smuzhiyun RRCrtcPtr pointerCrtc = pScrPriv->pointerCrtc;
109*4882a593Smuzhiyun int c;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /* Check last known CRTC */
112*4882a593Smuzhiyun if (pointerCrtc && RRCrtcContainsPosition(pointerCrtc, x, y))
113*4882a593Smuzhiyun return;
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun /* Check all CRTCs */
116*4882a593Smuzhiyun for (c = 0; c < pScrPriv->numCrtcs; c++) {
117*4882a593Smuzhiyun RRCrtcPtr crtc = pScrPriv->crtcs[c];
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun if (RRCrtcContainsPosition(crtc, x, y)) {
120*4882a593Smuzhiyun /* Remember containing CRTC */
121*4882a593Smuzhiyun pScrPriv->pointerCrtc = crtc;
122*4882a593Smuzhiyun return;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun /* None contain pointer, find nearest */
127*4882a593Smuzhiyun ErrorF("RRPointerMoved: Untested, may cause \"bogus pointer event\"\n");
128*4882a593Smuzhiyun RRPointerToNearestCrtc(inputInfo.pointer, pScreen, x, y, pointerCrtc);
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun /*
132*4882a593Smuzhiyun * When the screen is reconfigured, move all pointers to the nearest
133*4882a593Smuzhiyun * CRTC
134*4882a593Smuzhiyun */
135*4882a593Smuzhiyun void
RRPointerScreenConfigured(ScreenPtr pScreen)136*4882a593Smuzhiyun RRPointerScreenConfigured(ScreenPtr pScreen)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun WindowPtr pRoot;
139*4882a593Smuzhiyun ScreenPtr pCurrentScreen;
140*4882a593Smuzhiyun int x, y;
141*4882a593Smuzhiyun DeviceIntPtr pDev;
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun for (pDev = inputInfo.devices; pDev; pDev = pDev->next) {
144*4882a593Smuzhiyun if (IsPointerDevice(pDev)) {
145*4882a593Smuzhiyun pRoot = GetCurrentRootWindow(pDev);
146*4882a593Smuzhiyun pCurrentScreen = pRoot ? pRoot->drawable.pScreen : NULL;
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun if (pScreen == pCurrentScreen) {
149*4882a593Smuzhiyun GetSpritePosition(pDev, &x, &y);
150*4882a593Smuzhiyun RRPointerToNearestCrtc(pDev, pScreen, x, y, NULL);
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun }
155