~ubuntu-branches/ubuntu/intrepid/xserver-xgl/intrepid

« back to all changes in this revision

Viewing changes to hw/dmx/dmxcursor.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthew Garrett
  • Date: 2006-02-13 14:21:43 UTC
  • Revision ID: james.westby@ubuntu.com-20060213142143-mad6z9xzem7hzxz9
Tags: upstream-7.0.0
ImportĀ upstreamĀ versionĀ 7.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $XFree86$ */
 
2
/*
 
3
 * Copyright 2001-2004 Red Hat Inc., Durham, North Carolina.
 
4
 *
 
5
 * All Rights Reserved.
 
6
 *
 
7
 * Permission is hereby granted, free of charge, to any person obtaining
 
8
 * a copy of this software and associated documentation files (the
 
9
 * "Software"), to deal in the Software without restriction, including
 
10
 * without limitation on the rights to use, copy, modify, merge,
 
11
 * publish, distribute, sublicense, and/or sell copies of the Software,
 
12
 * and to permit persons to whom the Software is furnished to do so,
 
13
 * subject to the following conditions:
 
14
 *
 
15
 * The above copyright notice and this permission notice (including the
 
16
 * next paragraph) shall be included in all copies or substantial
 
17
 * portions of the Software.
 
18
 *
 
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
20
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
21
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
22
 * NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
 
23
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 
24
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
25
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 
26
 * SOFTWARE.
 
27
 */
 
28
 
 
29
/*
 
30
 * Authors:
 
31
 *   David H. Dawes <dawes@xfree86.org>
 
32
 *   Kevin E. Martin <kem@redhat.com>
 
33
 *   Rickard E. (Rik) Faith <faith@redhat.com>
 
34
 *
 
35
 */
 
36
 
 
37
/** \file
 
38
 * This file contains code than supports cursor movement, including the
 
39
 * code that initializes and reinitializes the screen positions and
 
40
 * computes screen overlap.
 
41
 *
 
42
 * "This code is based very closely on the XFree86 equivalent
 
43
 * (xfree86/common/xf86Cursor.c)."  --David Dawes.
 
44
 *
 
45
 * "This code was then extensively re-written, as explained here."
 
46
 * --Rik Faith
 
47
 *
 
48
 * The code in xf86Cursor.c used edge lists to implement the
 
49
 * CursorOffScreen function.  The edge list computation was complex
 
50
 * (especially in the face of arbitrarily overlapping screens) compared
 
51
 * with the speed savings in the CursorOffScreen function.  The new
 
52
 * implementation has erred on the side of correctness, readability, and
 
53
 * maintainability over efficiency.  For the common (non-edge) case, the
 
54
 * dmxCursorOffScreen function does avoid a loop over all the screens.
 
55
 * When the cursor has left the screen, all the screens are searched,
 
56
 * and the first screen (in dmxScreens order) containing the cursor will
 
57
 * be returned.  If run-time profiling shows that this routing is a
 
58
 * performance bottle-neck, then an edge list may have to be
 
59
 * reimplemented.  An edge list algorithm is O(edges) whereas the new
 
60
 * algorithm is O(dmxNumScreens).  Since edges is usually 1-3 and
 
61
 * dmxNumScreens may be 30-60 for large backend walls, this trade off
 
62
 * may be compelling.
 
63
 *
 
64
 * The xf86InitOrigins routine uses bit masks during the computation and
 
65
 * is therefore limited to the length of a word (e.g., 32 or 64 bits)
 
66
 * screens.  Because Xdmx is expected to be used with a large number of
 
67
 * backend displays, this limitation was removed.  The new
 
68
 * implementation has erred on the side of readability over efficiency,
 
69
 * using the dmxSL* routines to manage a screen list instead of a
 
70
 * bitmap, and a function call to decrease the length of the main
 
71
 * routine.  Both algorithms are of the same order, and both are called
 
72
 * only at server generation time, so trading clarity and long-term
 
73
 * maintainability for efficiency does not seem justified in this case.
 
74
 */
 
75
 
 
76
#ifdef HAVE_DMX_CONFIG_H
 
77
#include <dmx-config.h>
 
78
#endif
 
79
 
 
80
#define DMX_CURSOR_DEBUG 0
 
81
 
 
82
#include "dmx.h"
 
83
#include "dmxsync.h"
 
84
#include "dmxcursor.h"
 
85
#include "dmxlog.h"
 
86
#include "dmxprop.h"
 
87
#include "dmxinput.h"
 
88
 
 
89
#include "mipointer.h"
 
90
#include "windowstr.h"
 
91
#include "globals.h"
 
92
#include "cursorstr.h"
 
93
#include "dixevents.h"          /* For GetSpriteCursor() */
 
94
 
 
95
#if DMX_CURSOR_DEBUG
 
96
#define DMXDBG0(f)               dmxLog(dmxDebug,f)
 
97
#define DMXDBG1(f,a)             dmxLog(dmxDebug,f,a)
 
98
#define DMXDBG2(f,a,b)           dmxLog(dmxDebug,f,a,b)
 
99
#define DMXDBG3(f,a,b,c)         dmxLog(dmxDebug,f,a,b,c)
 
100
#define DMXDBG4(f,a,b,c,d)       dmxLog(dmxDebug,f,a,b,c,d)
 
101
#define DMXDBG5(f,a,b,c,d,e)     dmxLog(dmxDebug,f,a,b,c,d,e)
 
102
#define DMXDBG6(f,a,b,c,d,e,g)   dmxLog(dmxDebug,f,a,b,c,d,e,g)
 
103
#define DMXDBG7(f,a,b,c,d,e,g,h) dmxLog(dmxDebug,f,a,b,c,d,e,g,h)
 
104
#else
 
105
#define DMXDBG0(f)
 
106
#define DMXDBG1(f,a)
 
107
#define DMXDBG2(f,a,b)
 
108
#define DMXDBG3(f,a,b,c)
 
109
#define DMXDBG4(f,a,b,c,d)
 
110
#define DMXDBG5(f,a,b,c,d,e)
 
111
#define DMXDBG6(f,a,b,c,d,e,g)
 
112
#define DMXDBG7(f,a,b,c,d,e,g,h)
 
113
#endif
 
114
 
 
115
static int dmxCursorDoMultiCursors = 1;
 
116
 
 
117
/** Turn off support for displaying multiple cursors on overlapped
 
118
    back-end displays.  See #dmxCursorDoMultiCursors. */
 
119
void dmxCursorNoMulti(void)
 
120
{
 
121
    dmxCursorDoMultiCursors = 0;
 
122
}
 
123
 
 
124
static Bool dmxCursorOffScreen(ScreenPtr *ppScreen, int *x, int *y)
 
125
{
 
126
    DMXScreenInfo *dmxScreen;
 
127
    int           i;
 
128
    int           localX = *x;
 
129
    int           localY = *y;
 
130
    int           globalX;
 
131
    int           globalY;
 
132
    
 
133
    if (screenInfo.numScreens == 1) return FALSE;
 
134
 
 
135
                                /* On current screen? */
 
136
    dmxScreen = &dmxScreens[(*ppScreen)->myNum];
 
137
    if (localX >= 0
 
138
        && localX < dmxScreen->rootWidth
 
139
        && localY >= 0
 
140
        && localY < dmxScreen->rootHeight) return FALSE;
 
141
 
 
142
                                /* Convert to global coordinate space */
 
143
    globalX = dmxScreen->rootXOrigin + localX;
 
144
    globalY = dmxScreen->rootYOrigin + localY;
 
145
 
 
146
                                /* Is cursor on the current screen?
 
147
                                 * This efficiently exits this routine
 
148
                                 * for the most common case. */
 
149
    if (ppScreen && *ppScreen) {
 
150
        dmxScreen = &dmxScreens[(*ppScreen)->myNum];
 
151
        if (globalX >= dmxScreen->rootXOrigin
 
152
            && globalX < dmxScreen->rootXOrigin + dmxScreen->rootWidth
 
153
            && globalY >= dmxScreen->rootYOrigin
 
154
            && globalY < dmxScreen->rootYOrigin + dmxScreen->rootHeight)
 
155
            return FALSE;
 
156
    }
 
157
 
 
158
                                /* Find first screen cursor is on */
 
159
    for (i = 0; i < dmxNumScreens; i++) {
 
160
        dmxScreen = &dmxScreens[i];
 
161
        if (globalX >= dmxScreen->rootXOrigin
 
162
            && globalX < dmxScreen->rootXOrigin + dmxScreen->rootWidth
 
163
            && globalY >= dmxScreen->rootYOrigin
 
164
            && globalY < dmxScreen->rootYOrigin + dmxScreen->rootHeight) {
 
165
            if (dmxScreen->index == (*ppScreen)->myNum) return FALSE;
 
166
            *ppScreen = screenInfo.screens[dmxScreen->index];
 
167
            *x        = globalX - dmxScreen->rootXOrigin;
 
168
            *y        = globalY - dmxScreen->rootYOrigin;
 
169
            return TRUE;
 
170
        }
 
171
    }
 
172
    return FALSE;
 
173
}
 
174
 
 
175
static void dmxCrossScreen(ScreenPtr pScreen, Bool entering)
 
176
{
 
177
}
 
178
 
 
179
static void dmxWarpCursor(ScreenPtr pScreen, int x, int y)
 
180
{
 
181
    DMXDBG3("dmxWarpCursor(%d,%d,%d)\n", pScreen->myNum, x, y);
 
182
    miPointerWarpCursor(pScreen, x, y);
 
183
}
 
184
 
 
185
miPointerScreenFuncRec dmxPointerCursorFuncs =
 
186
{
 
187
    dmxCursorOffScreen,
 
188
    dmxCrossScreen,
 
189
    dmxWarpCursor,
 
190
    dmxeqEnqueue,
 
191
    dmxeqSwitchScreen
 
192
};
 
193
 
 
194
 
 
195
/** Create a list of screens that we'll manipulate. */
 
196
static int *dmxSLCreate(void)
 
197
{
 
198
    int *list = malloc(dmxNumScreens * sizeof(*list));
 
199
    int i;
 
200
    
 
201
    for (i = 0; i < dmxNumScreens; i++) list[i] = 1;
 
202
    return list;
 
203
}
 
204
 
 
205
/** Free list. */
 
206
static void dmxSLFree(int *list)
 
207
{
 
208
    free(list);
 
209
}
 
210
 
 
211
/** Find next uninitialized entry in list. */
 
212
static int dmxSLFindNext(int *list)
 
213
{
 
214
    int i;
 
215
    for (i = 0; i < dmxNumScreens; i++) if (list[i]) return i;
 
216
    return -1;
 
217
}
 
218
 
 
219
/** Make one pass over all the screens and return the number updated. */
 
220
static int dmxTryComputeScreenOrigins(int *screensLeft)
 
221
{
 
222
    ScreenPtr       pScreen;
 
223
    DMXScreenInfo   *screen;
 
224
    int             i, ref;
 
225
    int             changed = 0;
 
226
 
 
227
    for (i = 0; i < dmxNumScreens; i++) {
 
228
        if (!screensLeft[i]) continue;
 
229
        screen  = &dmxScreens[i];
 
230
        switch (screen->where) {
 
231
        case PosAbsolute:
 
232
            dixScreenOrigins[i].x = screen->whereX;
 
233
            dixScreenOrigins[i].y = screen->whereY;
 
234
            ++changed, screensLeft[i] = 0;
 
235
            break;
 
236
        case PosRelative:
 
237
            ref = screen->whereRefScreen;
 
238
            if (screensLeft[ref]) break;
 
239
            dixScreenOrigins[i].x = dixScreenOrigins[ref].x + screen->whereX;
 
240
            dixScreenOrigins[i].y = dixScreenOrigins[ref].y + screen->whereY;
 
241
            ++changed, screensLeft[i] = 0;
 
242
            break;
 
243
        case PosRightOf:
 
244
            ref = screen->whereRefScreen;
 
245
            if (screensLeft[ref]) break;
 
246
            pScreen = screenInfo.screens[ref];
 
247
            dixScreenOrigins[i].x = dixScreenOrigins[ref].x + pScreen->width;
 
248
            dixScreenOrigins[i].y = dixScreenOrigins[ref].y;
 
249
            ++changed, screensLeft[i] = 0;
 
250
            break;
 
251
        case PosLeftOf:
 
252
            ref = screen->whereRefScreen;
 
253
            if (screensLeft[ref]) break;
 
254
            pScreen = screenInfo.screens[i];
 
255
            dixScreenOrigins[i].x = dixScreenOrigins[ref].x - pScreen->width;
 
256
            dixScreenOrigins[i].y = dixScreenOrigins[ref].y;
 
257
            ++changed, screensLeft[i] = 0;
 
258
            break;
 
259
        case PosBelow:
 
260
            ref = screen->whereRefScreen;
 
261
            if (screensLeft[ref]) break;
 
262
            pScreen = screenInfo.screens[ref];
 
263
            dixScreenOrigins[i].x = dixScreenOrigins[ref].x;
 
264
            dixScreenOrigins[i].y = dixScreenOrigins[ref].y + pScreen->height;
 
265
            ++changed, screensLeft[i] = 0;
 
266
            break;
 
267
        case PosAbove:
 
268
            ref = screen->whereRefScreen;
 
269
            if (screensLeft[ref]) break;
 
270
            pScreen = screenInfo.screens[i];
 
271
            dixScreenOrigins[i].x = dixScreenOrigins[ref].x;
 
272
            dixScreenOrigins[i].y = dixScreenOrigins[ref].y - pScreen->height;
 
273
            ++changed, screensLeft[i] = 0;
 
274
            break;
 
275
        case PosNone:
 
276
            dmxLog(dmxFatal, "No position information for screen %d\n", i);
 
277
        }
 
278
    }
 
279
    return changed;
 
280
}
 
281
 
 
282
static void dmxComputeScreenOrigins(void)
 
283
{
 
284
    int             *screensLeft;
 
285
    int             i, ref;
 
286
    int             minX, minY;
 
287
 
 
288
                                /* Compute origins based on
 
289
                                 * configuration information. */
 
290
    screensLeft = dmxSLCreate();
 
291
    while ((i = dmxSLFindNext(screensLeft)) >= 0) {
 
292
        while (dmxTryComputeScreenOrigins(screensLeft));
 
293
        if ((i = dmxSLFindNext(screensLeft)) >= 0) {
 
294
            /* All of the remaining screens are referencing each other.
 
295
             * Assign a value to one of them and go through again.  This
 
296
             * guarantees that we will eventually terminate.
 
297
             */
 
298
            ref                     = dmxScreens[i].whereRefScreen;
 
299
            dixScreenOrigins[ref].x = dixScreenOrigins[ref].y = 0;
 
300
            screensLeft[ref]        = 0;
 
301
        }
 
302
    }
 
303
    dmxSLFree(screensLeft);
 
304
 
 
305
 
 
306
                                /* Justify the topmost and leftmost to
 
307
                                 * (0,0). */
 
308
    minX = dixScreenOrigins[0].x;
 
309
    minY = dixScreenOrigins[0].y;
 
310
    for (i = 1; i < dmxNumScreens; i++) { /* Compute minX, minY */
 
311
        if (dixScreenOrigins[i].x < minX) minX = dixScreenOrigins[i].x;
 
312
        if (dixScreenOrigins[i].y < minY) minY = dixScreenOrigins[i].y;
 
313
    }
 
314
    if (minX || minY) {
 
315
        for (i = 0; i < dmxNumScreens; i++) {
 
316
            dixScreenOrigins[i].x -= minX;
 
317
            dixScreenOrigins[i].y -= minY;
 
318
        }
 
319
    }
 
320
}
 
321
 
 
322
/** Recompute origin information in the #dmxScreens list.  This is
 
323
 * either called from #dmxInitOrigins() or from #dmxReconfig(). */
 
324
void dmxReInitOrigins(void)
 
325
{
 
326
    int        i;
 
327
 
 
328
    if (dmxNumScreens > MAXSCREENS)
 
329
        dmxLog(dmxFatal, "dmxNumScreens = %d > MAXSCREENS = %d\n",
 
330
               dmxNumScreens, MAXSCREENS);
 
331
 
 
332
    for (i = 0; i < dmxNumScreens; i++) {
 
333
        DMXScreenInfo    *dmxScreen  = &dmxScreens[i];
 
334
        dmxLogOutput(dmxScreen,
 
335
                     "s=%dx%d%+d%+d r=%dx%d%+d%+d @%d,%d"
 
336
                     " (be=%dx%d depth=%d bpp=%d)\n",
 
337
                     dmxScreen->scrnWidth, dmxScreen->scrnHeight,
 
338
                     dmxScreen->scrnX, dmxScreen->scrnY,
 
339
 
 
340
                     dmxScreen->rootWidth, dmxScreen->rootHeight,
 
341
                     dmxScreen->rootX, dmxScreen->rootY,
 
342
                     
 
343
                     dmxScreen->rootXOrigin, dmxScreen->rootYOrigin,
 
344
                     dmxScreen->beWidth, dmxScreen->beHeight,
 
345
                     dmxScreen->beDepth, dmxScreen->beBPP);
 
346
    }
 
347
}
 
348
 
 
349
/** Initialize screen origins (and relative position).  This is called
 
350
 * for each server generation.  For dynamic reconfiguration, use
 
351
 * #dmxReInitOrigins() instead. */
 
352
void dmxInitOrigins(void)
 
353
{
 
354
    int  i;
 
355
 
 
356
    if (dmxNumScreens > MAXSCREENS)
 
357
        dmxLog(dmxFatal, "dmxNumScreens = %d > MAXSCREENS = %d\n",
 
358
               dmxNumScreens, MAXSCREENS);
 
359
 
 
360
    for (i = 0; i < dmxNumScreens; i++) {
 
361
        DMXScreenInfo    *dmxScreen  = &dmxScreens[i];
 
362
        dmxLogOutput(dmxScreen,
 
363
                     "(request) s=%dx%d%+d%+d r=%dx%d%+d%+d @%d,%d (%d)"
 
364
                     " (be=%dx%d depth=%d bpp=%d)\n",
 
365
                     dmxScreen->scrnWidth, dmxScreen->scrnHeight,
 
366
                     dmxScreen->scrnX, dmxScreen->scrnY,
 
367
 
 
368
                     dmxScreen->rootWidth, dmxScreen->rootHeight,
 
369
                     dmxScreen->rootX, dmxScreen->rootY,
 
370
 
 
371
                     dmxScreen->whereX, dmxScreen->whereY,
 
372
                     dmxScreen->where,
 
373
 
 
374
                     dmxScreen->beWidth, dmxScreen->beHeight,
 
375
                     dmxScreen->beDepth, dmxScreen->beBPP);
 
376
    }
 
377
 
 
378
    dmxComputeScreenOrigins();
 
379
 
 
380
    for (i = 0; i < dmxNumScreens; i++) {
 
381
        DMXScreenInfo  *dmxScreen = &dmxScreens[i];
 
382
        dmxScreen->rootXOrigin = dixScreenOrigins[i].x;
 
383
        dmxScreen->rootYOrigin = dixScreenOrigins[i].y;
 
384
    }
 
385
 
 
386
    dmxReInitOrigins();
 
387
}
 
388
 
 
389
/** Returns non-zero if the global \a x, \a y coordinate is on the
 
390
 * screen window of the \a dmxScreen. */
 
391
int dmxOnScreen(int x, int y, DMXScreenInfo *dmxScreen)
 
392
{
 
393
#if DMX_CURSOR_DEBUG > 1
 
394
    dmxLog(dmxDebug,
 
395
           "dmxOnScreen %d %d,%d (r=%dx%d%+d%+d@%d,%d s=%dx%d%+d%+d)\n",
 
396
           dmxScreen->index, x, y,
 
397
           dmxScreen->rootWidth, dmxScreen->rootHeight,
 
398
           dmxScreen->rootX, dmxScreen->rootY,
 
399
           dmxScreen->rootXOrigin, dmxScreen->rootYOrigin,
 
400
           dmxScreen->scrnWidth, dmxScreen->scrnHeight,
 
401
           dmxScreen->scrnX, dmxScreen->scrnY);
 
402
#endif
 
403
    if (x >= dmxScreen->rootXOrigin
 
404
        && x < dmxScreen->rootXOrigin + dmxScreen->rootWidth
 
405
        && y >= dmxScreen->rootYOrigin
 
406
        && y < dmxScreen->rootYOrigin + dmxScreen->rootHeight) return 1;
 
407
    return 0;
 
408
}
 
409
 
 
410
/** Returns non-zero if \a a overlaps \a b. */
 
411
static int dmxDoesOverlap(DMXScreenInfo *a, DMXScreenInfo *b)
 
412
{
 
413
    if (dmxOnScreen(a->rootXOrigin,
 
414
                    a->rootYOrigin,                 b)) return 1;
 
415
 
 
416
    if (dmxOnScreen(a->rootXOrigin,
 
417
                    a->rootYOrigin + a->scrnWidth,  b)) return 1;
 
418
 
 
419
    if (dmxOnScreen(a->rootXOrigin + a->scrnHeight,
 
420
                    a->rootYOrigin,                 b)) return 1;
 
421
 
 
422
    if (dmxOnScreen(a->rootXOrigin + a->scrnHeight,
 
423
                    a->rootYOrigin + a->scrnWidth,  b)) return 1;
 
424
 
 
425
    if (dmxOnScreen(b->rootXOrigin,
 
426
                    b->rootYOrigin,                 a)) return 1;
 
427
 
 
428
    if (dmxOnScreen(b->rootXOrigin,
 
429
                    b->rootYOrigin + b->scrnWidth,  a)) return 1;
 
430
 
 
431
    if (dmxOnScreen(b->rootXOrigin + b->scrnHeight,
 
432
                    b->rootYOrigin,                 a)) return 1;
 
433
 
 
434
    if (dmxOnScreen(b->rootXOrigin + b->scrnHeight,
 
435
                    b->rootYOrigin + b->scrnWidth,  a)) return 1;
 
436
 
 
437
    return 0;
 
438
}
 
439
 
 
440
/** Used with #dmxInterateOverlap to print out a list of screens which
 
441
 * overlap each other. */
 
442
static void *dmxPrintOverlap(DMXScreenInfo *dmxScreen, void *closure)
 
443
{
 
444
    DMXScreenInfo *a = closure;
 
445
    if (dmxScreen != a) {
 
446
        if (dmxScreen->cursorNotShared)
 
447
            dmxLogOutputCont(a, " [%d/%s]", dmxScreen->index, dmxScreen->name);
 
448
        else
 
449
            dmxLogOutputCont(a, " %d/%s", dmxScreen->index, dmxScreen->name);
 
450
    }
 
451
    return NULL;
 
452
}
 
453
 
 
454
/** Iterate over the screens which overlap with the \a start screen,
 
455
 * calling \a f with the \a closure for each argument.  Often used with
 
456
 * #dmxPrintOverlap. */
 
457
static void *dmxIterateOverlap(DMXScreenInfo *start,
 
458
                               void *(*f)(DMXScreenInfo *dmxScreen, void *),
 
459
                               void *closure)
 
460
{
 
461
    DMXScreenInfo *pt;
 
462
 
 
463
    if (!start->over) return f(start, closure);
 
464
 
 
465
    for (pt = start->over; /* condition at end of loop */; pt = pt->over) {
 
466
        void *retval;
 
467
        if ((retval = f(pt, closure))) return retval;
 
468
        if (pt == start) break;
 
469
    }
 
470
    return NULL;
 
471
}
 
472
 
 
473
/** Used with #dmxPropertyIterate to determine if screen \a a is the
 
474
 * same as the screen \a closure. */
 
475
static void *dmxTestSameDisplay(DMXScreenInfo *a, void *closure)
 
476
{
 
477
    DMXScreenInfo *b = closure;
 
478
 
 
479
    if (a == b) return a;
 
480
    return NULL;
 
481
}
 
482
 
 
483
/** Detects overlapping dmxScreens and creates circular lists.  This
 
484
 * uses an O(dmxNumScreens^2) algorithm, but dmxNumScreens is < 100 and
 
485
 * the computation only needs to be performed for every server
 
486
 * generation or dynamic reconfiguration . */
 
487
void dmxInitOverlap(void)
 
488
{
 
489
    int           i, j;
 
490
    DMXScreenInfo *a, *b, *pt;
 
491
 
 
492
    for (i = 0; i < dmxNumScreens; i++) dmxScreens[i].over = NULL;
 
493
 
 
494
    for (i = 0; i < dmxNumScreens; i++) {
 
495
        a = &dmxScreens[i];
 
496
        
 
497
        for (j = i+1; j < dmxNumScreens; j++) {
 
498
            b = &dmxScreens[j];
 
499
            if (b->over) continue;
 
500
            
 
501
            if (dmxDoesOverlap(a, b)) {
 
502
                DMXDBG6("%d overlaps %d: a=%p %p b=%p %p\n",
 
503
                        a->index, b->index, a, a->over, b, b->over);
 
504
                b->over = (a->over ? a->over : a);
 
505
                a->over = b;
 
506
            }
 
507
        }
 
508
    }
 
509
 
 
510
    for (i = 0; i < dmxNumScreens; i++) {
 
511
        a = &dmxScreens[i];
 
512
        
 
513
        if (!a->over) continue;
 
514
        
 
515
                                /* Flag all pairs that are on same display */
 
516
        for (pt = a->over; pt != a; pt = pt->over) {
 
517
            if (dmxPropertyIterate(a, dmxTestSameDisplay, pt)) {
 
518
                /* The ->over sets contain the transitive set of screens
 
519
                 * that overlap.  For screens that are on the same
 
520
                 * backend display, we only want to exclude pairs of
 
521
                 * screens that mutually overlap on the backend display,
 
522
                 * so we call dmxDoesOverlap, which is stricter than the
 
523
                 * ->over set. */
 
524
                if (!dmxDoesOverlap(a, pt)) continue;
 
525
                a->cursorNotShared  = 1;
 
526
                pt->cursorNotShared = 1;
 
527
                dmxLog(dmxInfo,
 
528
                       "Screen %d and %d overlap on %s\n",
 
529
                       a->index, pt->index, a->name);
 
530
            }
 
531
        }
 
532
    }
 
533
 
 
534
    for (i = 0; i < dmxNumScreens; i++) {
 
535
        a = &dmxScreens[i];
 
536
        
 
537
        if (a->over) {
 
538
            dmxLogOutput(a, "Overlaps");
 
539
            dmxIterateOverlap(a, dmxPrintOverlap, a);
 
540
            dmxLogOutputCont(a, "\n");
 
541
        }
 
542
    }
 
543
}
 
544
 
 
545
/** Create \a pCursor on the back-end associated with \a pScreen. */
 
546
void dmxBECreateCursor(ScreenPtr pScreen, CursorPtr pCursor)
 
547
{
 
548
    DMXScreenInfo    *dmxScreen = &dmxScreens[pScreen->myNum];
 
549
    dmxCursorPrivPtr  pCursorPriv = DMX_GET_CURSOR_PRIV(pCursor, pScreen);
 
550
    CursorBitsPtr     pBits = pCursor->bits;
 
551
    Pixmap            src, msk;
 
552
    XColor            fg, bg;
 
553
    XImage           *img;
 
554
    XlibGC            gc = NULL;
 
555
    XGCValues         v;
 
556
    unsigned long     m;
 
557
    int               i;
 
558
 
 
559
    if (!pCursorPriv)
 
560
        return;
 
561
 
 
562
    m = GCFunction | GCPlaneMask | GCForeground | GCBackground | GCClipMask;
 
563
    v.function = GXcopy;
 
564
    v.plane_mask = AllPlanes;
 
565
    v.foreground = 1L;
 
566
    v.background = 0L;
 
567
    v.clip_mask = None;
 
568
 
 
569
    for (i = 0; i < dmxScreen->beNumPixmapFormats; i++) {
 
570
        if (dmxScreen->bePixmapFormats[i].depth == 1) {
 
571
            /* Create GC in the back-end servers */
 
572
            gc = XCreateGC(dmxScreen->beDisplay, dmxScreen->scrnDefDrawables[i],
 
573
                           m, &v);
 
574
            break;
 
575
        }
 
576
    }
 
577
    if (!gc)
 
578
        dmxLog(dmxFatal, "dmxRealizeCursor: gc not initialized\n");
 
579
 
 
580
    src = XCreatePixmap(dmxScreen->beDisplay, dmxScreen->scrnWin,
 
581
                        pBits->width, pBits->height, 1);
 
582
    msk = XCreatePixmap(dmxScreen->beDisplay, dmxScreen->scrnWin,
 
583
                        pBits->width, pBits->height, 1);
 
584
 
 
585
    img = XCreateImage(dmxScreen->beDisplay,
 
586
                       dmxScreen->beVisuals[dmxScreen->beDefVisualIndex].visual,
 
587
                       1, XYBitmap, 0, (char *)pBits->source,
 
588
                       pBits->width, pBits->height,
 
589
                       BitmapPad(dmxScreen->beDisplay), 0);
 
590
 
 
591
    XPutImage(dmxScreen->beDisplay, src, gc, img, 0, 0, 0, 0,
 
592
              pBits->width, pBits->height);
 
593
 
 
594
    XFree(img);
 
595
  
 
596
    img = XCreateImage(dmxScreen->beDisplay,
 
597
                       dmxScreen->beVisuals[dmxScreen->beDefVisualIndex].visual,
 
598
                       1, XYBitmap, 0, (char *)pBits->mask,
 
599
                       pBits->width, pBits->height,
 
600
                       BitmapPad(dmxScreen->beDisplay), 0);
 
601
 
 
602
    XPutImage(dmxScreen->beDisplay, msk, gc, img, 0, 0, 0, 0,
 
603
              pBits->width, pBits->height);
 
604
 
 
605
    XFree(img);
 
606
 
 
607
    fg.red   = pCursor->foreRed;
 
608
    fg.green = pCursor->foreGreen;
 
609
    fg.blue  = pCursor->foreBlue;
 
610
 
 
611
    bg.red   = pCursor->backRed;
 
612
    bg.green = pCursor->backGreen;
 
613
    bg.blue  = pCursor->backBlue;
 
614
 
 
615
    pCursorPriv->cursor = XCreatePixmapCursor(dmxScreen->beDisplay,
 
616
                                              src, msk,
 
617
                                              &fg, &bg,
 
618
                                              pBits->xhot, pBits->yhot);
 
619
 
 
620
    XFreePixmap(dmxScreen->beDisplay, src);
 
621
    XFreePixmap(dmxScreen->beDisplay, msk);
 
622
    XFreeGC(dmxScreen->beDisplay, gc);
 
623
 
 
624
    dmxSync(dmxScreen, FALSE);
 
625
}
 
626
 
 
627
static Bool _dmxRealizeCursor(ScreenPtr pScreen, CursorPtr pCursor)
 
628
{
 
629
    DMXScreenInfo    *dmxScreen = &dmxScreens[pScreen->myNum];
 
630
    dmxCursorPrivPtr  pCursorPriv;
 
631
 
 
632
    DMXDBG2("_dmxRealizeCursor(%d,%p)\n", pScreen->myNum, pCursor);
 
633
 
 
634
    pCursor->devPriv[pScreen->myNum] = xalloc(sizeof(*pCursorPriv));
 
635
    if (!pCursor->devPriv[pScreen->myNum])
 
636
        return FALSE;
 
637
 
 
638
    pCursorPriv = DMX_GET_CURSOR_PRIV(pCursor, pScreen);
 
639
    pCursorPriv->cursor = (Cursor)0;
 
640
 
 
641
    if (!dmxScreen->beDisplay)
 
642
        return TRUE;
 
643
 
 
644
    dmxBECreateCursor(pScreen, pCursor);
 
645
    return TRUE;
 
646
}
 
647
 
 
648
/** Free \a pCursor on the back-end associated with \a pScreen. */
 
649
Bool dmxBEFreeCursor(ScreenPtr pScreen, CursorPtr pCursor)
 
650
{
 
651
    DMXScreenInfo    *dmxScreen = &dmxScreens[pScreen->myNum];
 
652
    dmxCursorPrivPtr  pCursorPriv = DMX_GET_CURSOR_PRIV(pCursor, pScreen);
 
653
 
 
654
    if (pCursorPriv) {
 
655
        XFreeCursor(dmxScreen->beDisplay, pCursorPriv->cursor);
 
656
        pCursorPriv->cursor = (Cursor)0;
 
657
        return TRUE;
 
658
    }
 
659
 
 
660
    return FALSE;
 
661
}
 
662
 
 
663
static Bool _dmxUnrealizeCursor(ScreenPtr pScreen, CursorPtr pCursor)
 
664
{
 
665
    DMXScreenInfo    *dmxScreen = &dmxScreens[pScreen->myNum];
 
666
 
 
667
    DMXDBG3("_dmxUnrealizeCursor(%d,%p) %p\n",
 
668
            pScreen->myNum, pCursor, pCursorPriv);
 
669
 
 
670
    if (dmxScreen->beDisplay) {
 
671
        if (dmxBEFreeCursor(pScreen, pCursor))
 
672
            xfree(pCursor->devPriv[pScreen->myNum]);
 
673
    }
 
674
    pCursor->devPriv[pScreen->myNum] = NULL;
 
675
 
 
676
    return TRUE;
 
677
}
 
678
 
 
679
static void _dmxMoveCursor(ScreenPtr pScreen, int x, int y)
 
680
{
 
681
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
 
682
    int           newX       = x + dmxScreen->rootX;
 
683
    int           newY       = y + dmxScreen->rootY;
 
684
 
 
685
    if (newX < 0) newX = 0;
 
686
    if (newY < 0) newY = 0;
 
687
 
 
688
    DMXDBG5("_dmxMoveCursor(%d,%d,%d) -> %d,%d\n",
 
689
            pScreen->myNum, x, y, newX, newY);
 
690
    if (dmxScreen->beDisplay) {
 
691
        XWarpPointer(dmxScreen->beDisplay, None, dmxScreen->scrnWin,
 
692
                     0, 0, 0, 0, newX, newY);
 
693
        dmxSync(dmxScreen, TRUE);
 
694
    }
 
695
}
 
696
 
 
697
static void _dmxSetCursor(ScreenPtr pScreen, CursorPtr pCursor, int x, int y)
 
698
{
 
699
    DMXScreenInfo    *dmxScreen = &dmxScreens[pScreen->myNum];
 
700
 
 
701
    DMXDBG4("_dmxSetCursor(%d,%p,%d,%d)\n", pScreen->myNum, pCursor, x, y);
 
702
 
 
703
    if (pCursor) {
 
704
        dmxCursorPrivPtr  pCursorPriv = DMX_GET_CURSOR_PRIV(pCursor, pScreen);
 
705
        if (dmxScreen->curCursor != pCursorPriv->cursor) {
 
706
            if (dmxScreen->beDisplay)
 
707
                XDefineCursor(dmxScreen->beDisplay, dmxScreen->scrnWin,
 
708
                              pCursorPriv->cursor);
 
709
            dmxScreen->cursor        = pCursor;
 
710
            dmxScreen->curCursor     = pCursorPriv->cursor;
 
711
            dmxScreen->cursorVisible = 1;
 
712
        }
 
713
        _dmxMoveCursor(pScreen, x, y);
 
714
    } else {
 
715
        if (dmxScreen->beDisplay)
 
716
            XDefineCursor(dmxScreen->beDisplay, dmxScreen->scrnWin,
 
717
                          dmxScreen->noCursor);
 
718
        dmxScreen->cursor        = NULL;
 
719
        dmxScreen->curCursor     = (Cursor)0;
 
720
        dmxScreen->cursorVisible = 0;
 
721
    }
 
722
    if (dmxScreen->beDisplay) dmxSync(dmxScreen, TRUE);
 
723
}
 
724
 
 
725
static Bool dmxRealizeCursor(ScreenPtr pScreen, CursorPtr pCursor)
 
726
{
 
727
    DMXScreenInfo *start = &dmxScreens[pScreen->myNum];
 
728
    DMXScreenInfo *pt;
 
729
 
 
730
    if (!start->over || !dmxCursorDoMultiCursors || start->cursorNotShared)
 
731
        return _dmxRealizeCursor(pScreen, pCursor);
 
732
 
 
733
    for (pt = start->over; /* condition at end of loop */; pt = pt->over) {
 
734
        if (pt->cursorNotShared) continue;
 
735
        _dmxRealizeCursor(screenInfo.screens[pt->index], pCursor);
 
736
        if (pt == start) break;
 
737
    }
 
738
    return TRUE;
 
739
}
 
740
 
 
741
static Bool dmxUnrealizeCursor(ScreenPtr pScreen, CursorPtr pCursor)
 
742
{
 
743
    DMXScreenInfo *start = &dmxScreens[pScreen->myNum];
 
744
    DMXScreenInfo *pt;
 
745
 
 
746
    if (!start->over || !dmxCursorDoMultiCursors || start->cursorNotShared)
 
747
        return _dmxUnrealizeCursor(pScreen, pCursor);
 
748
 
 
749
    for (pt = start->over; /* condition at end of loop */; pt = pt->over) {
 
750
        if (pt->cursorNotShared) continue;
 
751
        _dmxUnrealizeCursor(screenInfo.screens[pt->index], pCursor);
 
752
        if (pt == start) break;
 
753
    }
 
754
    return TRUE;
 
755
}
 
756
 
 
757
static CursorPtr dmxFindCursor(DMXScreenInfo *start)
 
758
{
 
759
    DMXScreenInfo *pt;
 
760
 
 
761
    if (!start || !start->over) return GetSpriteCursor();
 
762
    for (pt = start->over; /* condition at end of loop */; pt = pt->over) {
 
763
        if (pt->cursor) return pt->cursor;
 
764
        if (pt == start) break;
 
765
    }
 
766
    return GetSpriteCursor();
 
767
}
 
768
 
 
769
/** Move the cursor to coordinates (\a x, \a y)on \a pScreen.  This
 
770
 * function is usually called via #dmxPointerSpriteFuncs, except during
 
771
 * reconfiguration when the cursor is repositioned to force an update on
 
772
 * newley overlapping screens and on screens that no longer overlap. */
 
773
void dmxMoveCursor(ScreenPtr pScreen, int x, int y)
 
774
{
 
775
    DMXScreenInfo *start = &dmxScreens[pScreen->myNum];
 
776
    DMXScreenInfo *pt;
 
777
 
 
778
    DMXDBG3("dmxMoveCursor(%d,%d,%d)\n", pScreen->myNum, x, y);
 
779
 
 
780
    if (!start->over || !dmxCursorDoMultiCursors || start->cursorNotShared) {
 
781
        _dmxMoveCursor(pScreen, x, y);
 
782
        return;
 
783
    }
 
784
 
 
785
    for (pt = start->over; /* condition at end of loop */; pt = pt->over) {
 
786
        if (pt->cursorNotShared) continue;
 
787
        if (dmxOnScreen(x + start->rootXOrigin, y + start->rootYOrigin, pt)) {
 
788
            if (/* pt != start && */ !pt->cursorVisible) {
 
789
                if (!pt->cursor) {
 
790
                                /* This only happens during
 
791
                                 * reconfiguration when a new overlap
 
792
                                 * occurs. */
 
793
                    CursorPtr pCursor;
 
794
                    
 
795
                    if ((pCursor = dmxFindCursor(start)))
 
796
                        _dmxRealizeCursor(screenInfo.screens[pt->index],
 
797
                                          pt->cursor = pCursor);
 
798
                    
 
799
                }
 
800
                _dmxSetCursor(screenInfo.screens[pt->index],
 
801
                              pt->cursor,
 
802
                              x + start->rootXOrigin - pt->rootXOrigin,
 
803
                              y + start->rootYOrigin - pt->rootYOrigin);
 
804
            }
 
805
            _dmxMoveCursor(screenInfo.screens[pt->index],
 
806
                           x + start->rootXOrigin - pt->rootXOrigin,
 
807
                           y + start->rootYOrigin - pt->rootYOrigin);
 
808
        } else if (/* pt != start && */ pt->cursorVisible) {
 
809
            _dmxSetCursor(screenInfo.screens[pt->index],
 
810
                          NULL,
 
811
                          x + start->rootXOrigin - pt->rootXOrigin,
 
812
                          y + start->rootYOrigin - pt->rootYOrigin);
 
813
        }
 
814
        if (pt == start) break;
 
815
    }
 
816
}
 
817
 
 
818
static void dmxSetCursor(ScreenPtr pScreen, CursorPtr pCursor, int x, int y)
 
819
{
 
820
    DMXScreenInfo *start = &dmxScreens[pScreen->myNum];
 
821
    DMXScreenInfo *pt;
 
822
    int           GX, GY, gx, gy;
 
823
 
 
824
    DMXDBG5("dmxSetCursor(%d %p, %p,%d,%d)\n",
 
825
            pScreen->myNum, start, pCursor, x, y);
 
826
 
 
827
                                /* We do this check here because of two cases:
 
828
                                 *
 
829
                                 * 1) if a client calls XWarpPointer()
 
830
                                 * and Xinerama is not running, we can
 
831
                                 * have mi's notion of the pointer
 
832
                                 * position out of phase with DMX's
 
833
                                 * notion.
 
834
                                 *
 
835
                                 * 2) if a down button is held while the
 
836
                                 * cursor moves outside the root window,
 
837
                                 * mi's notion of the pointer position
 
838
                                 * is out of phase with DMX's notion and
 
839
                                 * the cursor can remain visible when it
 
840
                                 * shouldn't be. */
 
841
 
 
842
    dmxGetGlobalPosition(&GX, &GY);
 
843
    gx = start->rootXOrigin + x;
 
844
    gy = start->rootYOrigin + y;
 
845
    if (x && y && (GX != gx || GY != gy))
 
846
        dmxCoreMotion(gx, gy, 0, DMX_NO_BLOCK);
 
847
    
 
848
    if (!start->over || !dmxCursorDoMultiCursors || start->cursorNotShared) {
 
849
        _dmxSetCursor(pScreen, pCursor, x, y);
 
850
        return;
 
851
    }
 
852
 
 
853
    for (pt = start->over; /* condition at end of loop */; pt = pt->over) {
 
854
        if (pt->cursorNotShared) continue;
 
855
        if (dmxOnScreen(x + start->rootXOrigin, y + start->rootYOrigin, pt)) {
 
856
            _dmxSetCursor(screenInfo.screens[pt->index], pCursor,
 
857
                          x + start->rootXOrigin - pt->rootXOrigin,
 
858
                          y + start->rootYOrigin - pt->rootYOrigin);
 
859
        } else {
 
860
            _dmxSetCursor(screenInfo.screens[pt->index], NULL,
 
861
                          x + start->rootXOrigin - pt->rootXOrigin,
 
862
                          y + start->rootYOrigin - pt->rootYOrigin);
 
863
        }
 
864
        if (pt == start) break;
 
865
    }
 
866
}
 
867
 
 
868
 
 
869
/** This routine is used by the backend input routines to hide the
 
870
 * cursor on a screen that is being used for relative input.  \see
 
871
 * dmxbackend.c */
 
872
void dmxHideCursor(DMXScreenInfo *dmxScreen)
 
873
{
 
874
    int       x, y;
 
875
    ScreenPtr pScreen = screenInfo.screens[dmxScreen->index];
 
876
 
 
877
    dmxGetGlobalPosition(&x, &y);
 
878
    _dmxSetCursor(pScreen, NULL, x, y);
 
879
}
 
880
 
 
881
/** This routine is called during reconfiguration to make sure the
 
882
 * cursor is visible. */
 
883
void dmxCheckCursor(void)
 
884
{
 
885
    int           i;
 
886
    int           x, y;
 
887
    ScreenPtr     pScreen;
 
888
    DMXScreenInfo *firstScreen;
 
889
 
 
890
    dmxGetGlobalPosition(&x, &y);
 
891
    firstScreen = dmxFindFirstScreen(x, y);
 
892
 
 
893
    DMXDBG2("dmxCheckCursor %d %d\n", x, y);
 
894
    for (i = 0; i < dmxNumScreens; i++) {
 
895
        DMXScreenInfo *dmxScreen = &dmxScreens[i];
 
896
        pScreen                  = screenInfo.screens[dmxScreen->index];
 
897
 
 
898
        if (!dmxOnScreen(x, y, dmxScreen)) {
 
899
            if (firstScreen && i == miPointerCurrentScreen()->myNum)
 
900
                miPointerSetNewScreen(firstScreen->index, x, y);
 
901
            _dmxSetCursor(pScreen, NULL,
 
902
                          x - dmxScreen->rootXOrigin,
 
903
                          y - dmxScreen->rootYOrigin);
 
904
        } else {
 
905
            if (!dmxScreen->cursor) {
 
906
                CursorPtr pCursor;
 
907
                
 
908
                if ((pCursor = dmxFindCursor(dmxScreen))) {
 
909
                    _dmxRealizeCursor(pScreen, dmxScreen->cursor = pCursor);
 
910
                }
 
911
            }
 
912
            _dmxSetCursor(pScreen, dmxScreen->cursor,
 
913
                          x - dmxScreen->rootXOrigin,
 
914
                          y - dmxScreen->rootYOrigin);
 
915
        }
 
916
    }
 
917
    DMXDBG2("   leave dmxCheckCursor %d %d\n", x, y);
 
918
}
 
919
 
 
920
miPointerSpriteFuncRec dmxPointerSpriteFuncs =
 
921
{
 
922
    dmxRealizeCursor,
 
923
    dmxUnrealizeCursor,
 
924
    dmxSetCursor,
 
925
    dmxMoveCursor,
 
926
};