summaryrefslogtreecommitdiff
path: root/hw/dmx/dmxcursor.c
blob: e74a05215d333755768ae8e5e075fb62bb6257bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
/* $XFree86$ */
/*
 * Copyright 2001-2004 Red Hat Inc., Durham, North Carolina.
 *
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation on the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/*
 * Authors:
 *   David H. Dawes <dawes@xfree86.org>
 *   Kevin E. Martin <kem@redhat.com>
 *   Rickard E. (Rik) Faith <faith@redhat.com>
 *
 */

/** \file
 * This file contains code than supports cursor movement, including the
 * code that initializes and reinitializes the screen positions and
 * computes screen overlap.
 *
 * "This code is based very closely on the XFree86 equivalent
 * (xfree86/common/xf86Cursor.c)."  --David Dawes.
 *
 * "This code was then extensively re-written, as explained here."
 * --Rik Faith
 *
 * The code in xf86Cursor.c used edge lists to implement the
 * CursorOffScreen function.  The edge list computation was complex
 * (especially in the face of arbitrarily overlapping screens) compared
 * with the speed savings in the CursorOffScreen function.  The new
 * implementation has erred on the side of correctness, readability, and
 * maintainability over efficiency.  For the common (non-edge) case, the
 * dmxCursorOffScreen function does avoid a loop over all the screens.
 * When the cursor has left the screen, all the screens are searched,
 * and the first screen (in dmxScreens order) containing the cursor will
 * be returned.  If run-time profiling shows that this routing is a
 * performance bottle-neck, then an edge list may have to be
 * reimplemented.  An edge list algorithm is O(edges) whereas the new
 * algorithm is O(dmxNumScreens).  Since edges is usually 1-3 and
 * dmxNumScreens may be 30-60 for large backend walls, this trade off
 * may be compelling.
 *
 * The xf86InitOrigins routine uses bit masks during the computation and
 * is therefore limited to the length of a word (e.g., 32 or 64 bits)
 * screens.  Because Xdmx is expected to be used with a large number of
 * backend displays, this limitation was removed.  The new
 * implementation has erred on the side of readability over efficiency,
 * using the dmxSL* routines to manage a screen list instead of a
 * bitmap, and a function call to decrease the length of the main
 * routine.  Both algorithms are of the same order, and both are called
 * only at server generation time, so trading clarity and long-term
 * maintainability for efficiency does not seem justified in this case.
 */

#ifdef HAVE_DMX_CONFIG_H
#include <dmx-config.h>
#endif

#define DMX_CURSOR_DEBUG 0

#include "dmx.h"
#include "dmxsync.h"
#include "dmxcursor.h"
#include "dmxlog.h"
#include "dmxprop.h"
#include "dmxinput.h"

#include "mipointer.h"
#include "windowstr.h"
#include "globals.h"
#include "cursorstr.h"
#include "dixevents.h"          /* For GetSpriteCursor() */

#if DMX_CURSOR_DEBUG
#define DMXDBG0(f)               dmxLog(dmxDebug,f)
#define DMXDBG1(f,a)             dmxLog(dmxDebug,f,a)
#define DMXDBG2(f,a,b)           dmxLog(dmxDebug,f,a,b)
#define DMXDBG3(f,a,b,c)         dmxLog(dmxDebug,f,a,b,c)
#define DMXDBG4(f,a,b,c,d)       dmxLog(dmxDebug,f,a,b,c,d)
#define DMXDBG5(f,a,b,c,d,e)     dmxLog(dmxDebug,f,a,b,c,d,e)
#define DMXDBG6(f,a,b,c,d,e,g)   dmxLog(dmxDebug,f,a,b,c,d,e,g)
#define DMXDBG7(f,a,b,c,d,e,g,h) dmxLog(dmxDebug,f,a,b,c,d,e,g,h)
#else
#define DMXDBG0(f)
#define DMXDBG1(f,a)
#define DMXDBG2(f,a,b)
#define DMXDBG3(f,a,b,c)
#define DMXDBG4(f,a,b,c,d)
#define DMXDBG5(f,a,b,c,d,e)
#define DMXDBG6(f,a,b,c,d,e,g)
#define DMXDBG7(f,a,b,c,d,e,g,h)
#endif

static int dmxCursorDoMultiCursors = 1;

/** Turn off support for displaying multiple cursors on overlapped
    back-end displays.  See #dmxCursorDoMultiCursors. */
void dmxCursorNoMulti(void)
{
    dmxCursorDoMultiCursors = 0;
}

static Bool dmxCursorOffScreen(ScreenPtr *ppScreen, int *x, int *y)
{
    DMXScreenInfo *dmxScreen;
    int           i;
    int           localX = *x;
    int           localY = *y;
    int           globalX;
    int           globalY;
    
    if (screenInfo.numScreens == 1) return FALSE;

                                /* On current screen? */
    dmxScreen = &dmxScreens[(*ppScreen)->myNum];
    if (localX >= 0
        && localX < dmxScreen->rootWidth
        && localY >= 0
        && localY < dmxScreen->rootHeight) return FALSE;

                                /* Convert to global coordinate space */
    globalX = dmxScreen->rootXOrigin + localX;
    globalY = dmxScreen->rootYOrigin + localY;

                                /* Is cursor on the current screen?
                                 * This efficiently exits this routine
                                 * for the most common case. */
    if (ppScreen && *ppScreen) {
        dmxScreen = &dmxScreens[(*ppScreen)->myNum];
        if (globalX >= dmxScreen->rootXOrigin
            && globalX < dmxScreen->rootXOrigin + dmxScreen->rootWidth
            && globalY >= dmxScreen->rootYOrigin
            && globalY < dmxScreen->rootYOrigin + dmxScreen->rootHeight)
            return FALSE;
    }

                                /* Find first screen cursor is on */
    for (i = 0; i < dmxNumScreens; i++) {
        dmxScreen = &dmxScreens[i];
        if (globalX >= dmxScreen->rootXOrigin
            && globalX < dmxScreen->rootXOrigin + dmxScreen->rootWidth
            && globalY >= dmxScreen->rootYOrigin
            && globalY < dmxScreen->rootYOrigin + dmxScreen->rootHeight) {
            if (dmxScreen->index == (*ppScreen)->myNum) return FALSE;
            *ppScreen = screenInfo.screens[dmxScreen->index];
            *x        = globalX - dmxScreen->rootXOrigin;
            *y        = globalY - dmxScreen->rootYOrigin;
            return TRUE;
        }
    }
    return FALSE;
}

static void dmxCrossScreen(ScreenPtr pScreen, Bool entering)
{
}

static void dmxWarpCursor(ScreenPtr pScreen, int x, int y)
{
    DMXDBG3("dmxWarpCursor(%d,%d,%d)\n", pScreen->myNum, x, y);
    miPointerWarpCursor(pScreen, x, y);
}

miPointerScreenFuncRec dmxPointerCursorFuncs =
{
    dmxCursorOffScreen,
    dmxCrossScreen,
    dmxWarpCursor,
    dmxeqEnqueue,
    dmxeqSwitchScreen
};


/** Create a list of screens that we'll manipulate. */
static int *dmxSLCreate(void)
{
    int *list = malloc(dmxNumScreens * sizeof(*list));
    int i;
    
    for (i = 0; i < dmxNumScreens; i++) list[i] = 1;
    return list;
}

/** Free list. */
static void dmxSLFree(int *list)
{
    free(list);
}

/** Find next uninitialized entry in list. */
static int dmxSLFindNext(int *list)
{
    int i;
    for (i = 0; i < dmxNumScreens; i++) if (list[i]) return i;
    return -1;
}

/** Make one pass over all the screens and return the number updated. */
static int dmxTryComputeScreenOrigins(int *screensLeft)
{
    ScreenPtr       pScreen;
    DMXScreenInfo   *screen;
    int             i, ref;
    int             changed = 0;

    for (i = 0; i < dmxNumScreens; i++) {
        if (!screensLeft[i]) continue;
        screen  = &dmxScreens[i];
        switch (screen->where) {
        case PosAbsolute:
            dixScreenOrigins[i].x = screen->whereX;
            dixScreenOrigins[i].y = screen->whereY;
            ++changed, screensLeft[i] = 0;
            break;
        case PosRelative:
            ref = screen->whereRefScreen;
            if (screensLeft[ref]) break;
            dixScreenOrigins[i].x = dixScreenOrigins[ref].x + screen->whereX;
            dixScreenOrigins[i].y = dixScreenOrigins[ref].y + screen->whereY;
            ++changed, screensLeft[i] = 0;
            break;
        case PosRightOf:
            ref = screen->whereRefScreen;
            if (screensLeft[ref]) break;
            pScreen = screenInfo.screens[ref];
            dixScreenOrigins[i].x = dixScreenOrigins[ref].x + pScreen->width;
            dixScreenOrigins[i].y = dixScreenOrigins[ref].y;
            ++changed, screensLeft[i] = 0;
            break;
        case PosLeftOf:
            ref = screen->whereRefScreen;
            if (screensLeft[ref]) break;
            pScreen = screenInfo.screens[i];
            dixScreenOrigins[i].x = dixScreenOrigins[ref].x - pScreen->width;
            dixScreenOrigins[i].y = dixScreenOrigins[ref].y;
            ++changed, screensLeft[i] = 0;
            break;
        case PosBelow:
            ref = screen->whereRefScreen;
            if (screensLeft[ref]) break;
            pScreen = screenInfo.screens[ref];
            dixScreenOrigins[i].x = dixScreenOrigins[ref].x;
            dixScreenOrigins[i].y = dixScreenOrigins[ref].y + pScreen->height;
            ++changed, screensLeft[i] = 0;
            break;
        case PosAbove:
            ref = screen->whereRefScreen;
            if (screensLeft[ref]) break;
            pScreen = screenInfo.screens[i];
            dixScreenOrigins[i].x = dixScreenOrigins[ref].x;
            dixScreenOrigins[i].y = dixScreenOrigins[ref].y - pScreen->height;
            ++changed, screensLeft[i] = 0;
            break;
        case PosNone:
            dmxLog(dmxFatal, "No position information for screen %d\n", i);
        }
    }
    return changed;
}

static void dmxComputeScreenOrigins(void)
{
    int             *screensLeft;
    int             i, ref;
    int             minX, minY;

                                /* Compute origins based on
                                 * configuration information. */
    screensLeft = dmxSLCreate();
    while ((i = dmxSLFindNext(screensLeft)) >= 0) {
        while (dmxTryComputeScreenOrigins(screensLeft));
        if ((i = dmxSLFindNext(screensLeft)) >= 0) {
	    /* All of the remaining screens are referencing each other.
	     * Assign a value to one of them and go through again.  This
	     * guarantees that we will eventually terminate.
	     */
	    ref                     = dmxScreens[i].whereRefScreen;
	    dixScreenOrigins[ref].x = dixScreenOrigins[ref].y = 0;
            screensLeft[ref]        = 0;
	}
    }
    dmxSLFree(screensLeft);


                                /* Justify the topmost and leftmost to
                                 * (0,0). */
    minX = dixScreenOrigins[0].x;
    minY = dixScreenOrigins[0].y;
    for (i = 1; i < dmxNumScreens; i++) { /* Compute minX, minY */
	if (dixScreenOrigins[i].x < minX) minX = dixScreenOrigins[i].x;
	if (dixScreenOrigins[i].y < minY) minY = dixScreenOrigins[i].y;
    }
    if (minX || minY) {
	for (i = 0; i < dmxNumScreens; i++) {
	    dixScreenOrigins[i].x -= minX;
	    dixScreenOrigins[i].y -= minY;
	}
    }
}

/** Recompute origin information in the #dmxScreens list.  This is
 * either called from #dmxInitOrigins() or from #dmxReconfig(). */
void dmxReInitOrigins(void)
{
    int        i;

    if (dmxNumScreens > MAXSCREENS)
        dmxLog(dmxFatal, "dmxNumScreens = %d > MAXSCREENS = %d\n",
               dmxNumScreens, MAXSCREENS);

    for (i = 0; i < dmxNumScreens; i++) {
        DMXScreenInfo    *dmxScreen  = &dmxScreens[i];
        dmxLogOutput(dmxScreen,
                     "s=%dx%d%+d%+d r=%dx%d%+d%+d @%d,%d"
                     " (be=%dx%d depth=%d bpp=%d)\n",
                     dmxScreen->scrnWidth, dmxScreen->scrnHeight,
                     dmxScreen->scrnX, dmxScreen->scrnY,

                     dmxScreen->rootWidth, dmxScreen->rootHeight,
                     dmxScreen->rootX, dmxScreen->rootY,
                     
                     dmxScreen->rootXOrigin, dmxScreen->rootYOrigin,
                     dmxScreen->beWidth, dmxScreen->beHeight,
                     dmxScreen->beDepth, dmxScreen->beBPP);
    }
}

/** Initialize screen origins (and relative position).  This is called
 * for each server generation.  For dynamic reconfiguration, use
 * #dmxReInitOrigins() instead. */
void dmxInitOrigins(void)
{
    int  i;

    if (dmxNumScreens > MAXSCREENS)
        dmxLog(dmxFatal, "dmxNumScreens = %d > MAXSCREENS = %d\n",
               dmxNumScreens, MAXSCREENS);

    for (i = 0; i < dmxNumScreens; i++) {
        DMXScreenInfo    *dmxScreen  = &dmxScreens[i];
        dmxLogOutput(dmxScreen,
                     "(request) s=%dx%d%+d%+d r=%dx%d%+d%+d @%d,%d (%d)"
                     " (be=%dx%d depth=%d bpp=%d)\n",
                     dmxScreen->scrnWidth, dmxScreen->scrnHeight,
                     dmxScreen->scrnX, dmxScreen->scrnY,

                     dmxScreen->rootWidth, dmxScreen->rootHeight,
                     dmxScreen->rootX, dmxScreen->rootY,

                     dmxScreen->whereX, dmxScreen->whereY,
                     dmxScreen->where,

                     dmxScreen->beWidth, dmxScreen->beHeight,
                     dmxScreen->beDepth, dmxScreen->beBPP);
    }

    dmxComputeScreenOrigins();

    for (i = 0; i < dmxNumScreens; i++) {
        DMXScreenInfo  *dmxScreen = &dmxScreens[i];
        dmxScreen->rootXOrigin = dixScreenOrigins[i].x;
        dmxScreen->rootYOrigin = dixScreenOrigins[i].y;
    }

    dmxReInitOrigins();
}

/** Returns non-zero if the global \a x, \a y coordinate is on the
 * screen window of the \a dmxScreen. */
int dmxOnScreen(int x, int y, DMXScreenInfo *dmxScreen)
{
#if DMX_CURSOR_DEBUG > 1
    dmxLog(dmxDebug,
           "dmxOnScreen %d %d,%d (r=%dx%d%+d%+d@%d,%d s=%dx%d%+d%+d)\n",
           dmxScreen->index, x, y,
           dmxScreen->rootWidth, dmxScreen->rootHeight,
           dmxScreen->rootX, dmxScreen->rootY,
           dmxScreen->rootXOrigin, dmxScreen->rootYOrigin,
           dmxScreen->scrnWidth, dmxScreen->scrnHeight,
           dmxScreen->scrnX, dmxScreen->scrnY);
#endif
    if (x >= dmxScreen->rootXOrigin
        && x < dmxScreen->rootXOrigin + dmxScreen->rootWidth
        && y >= dmxScreen->rootYOrigin
        && y < dmxScreen->rootYOrigin + dmxScreen->rootHeight) return 1;
    return 0;
}

/** Returns non-zero if \a a overlaps \a b. */
static int dmxDoesOverlap(DMXScreenInfo *a, DMXScreenInfo *b)
{
    if (dmxOnScreen(a->rootXOrigin,
                    a->rootYOrigin,                 b)) return 1;

    if (dmxOnScreen(a->rootXOrigin,
                    a->rootYOrigin + a->scrnWidth,  b)) return 1;

    if (dmxOnScreen(a->rootXOrigin + a->scrnHeight,
                    a->rootYOrigin,                 b)) return 1;

    if (dmxOnScreen(a->rootXOrigin + a->scrnHeight,
                    a->rootYOrigin + a->scrnWidth,  b)) return 1;

    if (dmxOnScreen(b->rootXOrigin,
                    b->rootYOrigin,                 a)) return 1;

    if (dmxOnScreen(b->rootXOrigin,
                    b->rootYOrigin + b->scrnWidth,  a)) return 1;

    if (dmxOnScreen(b->rootXOrigin + b->scrnHeight,
                    b->rootYOrigin,                 a)) return 1;

    if (dmxOnScreen(b->rootXOrigin + b->scrnHeight,
                    b->rootYOrigin + b->scrnWidth,  a)) return 1;

    return 0;
}

/** Used with #dmxInterateOverlap to print out a list of screens which
 * overlap each other. */
static void *dmxPrintOverlap(DMXScreenInfo *dmxScreen, void *closure)
{
    DMXScreenInfo *a = closure;
    if (dmxScreen != a) {
        if (dmxScreen->cursorNotShared)
            dmxLogOutputCont(a, " [%d/%s]", dmxScreen->index, dmxScreen->name);
        else
            dmxLogOutputCont(a, " %d/%s", dmxScreen->index, dmxScreen->name);
    }
    return NULL;
}

/** Iterate over the screens which overlap with the \a start screen,
 * calling \a f with the \a closure for each argument.  Often used with
 * #dmxPrintOverlap. */
static void *dmxIterateOverlap(DMXScreenInfo *start,
                               void *(*f)(DMXScreenInfo *dmxScreen, void *),
                               void *closure)
{
    DMXScreenInfo *pt;

    if (!start->over) return f(start, closure);

    for (pt = start->over; /* condition at end of loop */; pt = pt->over) {
        void *retval;
        if ((retval = f(pt, closure))) return retval;
        if (pt == start) break;
    }
    return NULL;
}

/** Used with #dmxPropertyIterate to determine if screen \a a is the
 * same as the screen \a closure. */
static void *dmxTestSameDisplay(DMXScreenInfo *a, void *closure)
{
    DMXScreenInfo *b = closure;

    if (a == b) return a;
    return NULL;
}

/** Detects overlapping dmxScreens and creates circular lists.  This
 * uses an O(dmxNumScreens^2) algorithm, but dmxNumScreens is < 100 and
 * the computation only needs to be performed for every server
 * generation or dynamic reconfiguration . */
void dmxInitOverlap(void)
{
    int           i, j;
    DMXScreenInfo *a, *b, *pt;

    for (i = 0; i < dmxNumScreens; i++) dmxScreens[i].over = NULL;

    for (i = 0; i < dmxNumScreens; i++) {
        a = &dmxScreens[i];
        
        for (j = i+1; j < dmxNumScreens; j++) {
            b = &dmxScreens[j];
            if (b->over) continue;
            
            if (dmxDoesOverlap(a, b)) {
                DMXDBG6("%d overlaps %d: a=%p %p b=%p %p\n",
                        a->index, b->index, a, a->over, b, b->over);
                b->over = (a->over ? a->over : a);
                a->over = b;
            }
        }
    }

    for (i = 0; i < dmxNumScreens; i++) {
        a = &dmxScreens[i];
        
        if (!a->over) continue;
        
                                /* Flag all pairs that are on same display */
        for (pt = a->over; pt != a; pt = pt->over) {
            if (dmxPropertyIterate(a, dmxTestSameDisplay, pt)) {
                /* The ->over sets contain the transitive set of screens
                 * that overlap.  For screens that are on the same
                 * backend display, we only want to exclude pairs of
                 * screens that mutually overlap on the backend display,
                 * so we call dmxDoesOverlap, which is stricter than the
                 * ->over set. */
                if (!dmxDoesOverlap(a, pt)) continue;
                a->cursorNotShared  = 1;
                pt->cursorNotShared = 1;
                dmxLog(dmxInfo,
                       "Screen %d and %d overlap on %s\n",
                       a->index, pt->index, a->name);
            }
        }
    }

    for (i = 0; i < dmxNumScreens; i++) {
        a = &dmxScreens[i];
        
        if (a->over) {
            dmxLogOutput(a, "Overlaps");
            dmxIterateOverlap(a, dmxPrintOverlap, a);
            dmxLogOutputCont(a, "\n");
        }
    }
}

/** Create \a pCursor on the back-end associated with \a pScreen. */
void dmxBECreateCursor(ScreenPtr pScreen, CursorPtr pCursor)
{
    DMXScreenInfo    *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxCursorPrivPtr  pCursorPriv = DMX_GET_CURSOR_PRIV(pCursor, pScreen);
    CursorBitsPtr     pBits = pCursor->bits;
    Pixmap            src, msk;
    XColor            fg, bg;
    XImage           *img;
    XlibGC            gc = NULL;
    XGCValues         v;
    unsigned long     m;
    int               i;

    if (!pCursorPriv)
	return;

    m = GCFunction | GCPlaneMask | GCForeground | GCBackground | GCClipMask;
    v.function = GXcopy;
    v.plane_mask = AllPlanes;
    v.foreground = 1L;
    v.background = 0L;
    v.clip_mask = None;

    for (i = 0; i < dmxScreen->beNumPixmapFormats; i++) {
	if (dmxScreen->bePixmapFormats[i].depth == 1) {
	    /* Create GC in the back-end servers */
	    gc = XCreateGC(dmxScreen->beDisplay, dmxScreen->scrnDefDrawables[i],
			   m, &v);
	    break;
	}
    }
    if (!gc)
        dmxLog(dmxFatal, "dmxRealizeCursor: gc not initialized\n");

    src = XCreatePixmap(dmxScreen->beDisplay, dmxScreen->scrnWin,
			pBits->width, pBits->height, 1);
    msk = XCreatePixmap(dmxScreen->beDisplay, dmxScreen->scrnWin,
			pBits->width, pBits->height, 1);

    img = XCreateImage(dmxScreen->beDisplay,
		       dmxScreen->beVisuals[dmxScreen->beDefVisualIndex].visual,
		       1, XYBitmap, 0, (char *)pBits->source,
		       pBits->width, pBits->height,
		       BitmapPad(dmxScreen->beDisplay), 0);

    XPutImage(dmxScreen->beDisplay, src, gc, img, 0, 0, 0, 0,
	      pBits->width, pBits->height);

    XFree(img);
  
    img = XCreateImage(dmxScreen->beDisplay,
		       dmxScreen->beVisuals[dmxScreen->beDefVisualIndex].visual,
		       1, XYBitmap, 0, (char *)pBits->mask,
		       pBits->width, pBits->height,
		       BitmapPad(dmxScreen->beDisplay), 0);

    XPutImage(dmxScreen->beDisplay, msk, gc, img, 0, 0, 0, 0,
	      pBits->width, pBits->height);

    XFree(img);

    fg.red   = pCursor->foreRed;
    fg.green = pCursor->foreGreen;
    fg.blue  = pCursor->foreBlue;

    bg.red   = pCursor->backRed;
    bg.green = pCursor->backGreen;
    bg.blue  = pCursor->backBlue;

    pCursorPriv->cursor = XCreatePixmapCursor(dmxScreen->beDisplay,
					      src, msk,
					      &fg, &bg,
					      pBits->xhot, pBits->yhot);

    XFreePixmap(dmxScreen->beDisplay, src);
    XFreePixmap(dmxScreen->beDisplay, msk);
    XFreeGC(dmxScreen->beDisplay, gc);

    dmxSync(dmxScreen, FALSE);
}

static Bool _dmxRealizeCursor(ScreenPtr pScreen, CursorPtr pCursor)
{
    DMXScreenInfo    *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxCursorPrivPtr  pCursorPriv;

    DMXDBG2("_dmxRealizeCursor(%d,%p)\n", pScreen->myNum, pCursor);

    pCursor->devPriv[pScreen->myNum] = xalloc(sizeof(*pCursorPriv));
    if (!pCursor->devPriv[pScreen->myNum])
	return FALSE;

    pCursorPriv = DMX_GET_CURSOR_PRIV(pCursor, pScreen);
    pCursorPriv->cursor = (Cursor)0;

    if (!dmxScreen->beDisplay)
	return TRUE;

    dmxBECreateCursor(pScreen, pCursor);
    return TRUE;
}

/** Free \a pCursor on the back-end associated with \a pScreen. */
Bool dmxBEFreeCursor(ScreenPtr pScreen, CursorPtr pCursor)
{
    DMXScreenInfo    *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxCursorPrivPtr  pCursorPriv = DMX_GET_CURSOR_PRIV(pCursor, pScreen);

    if (pCursorPriv) {
	XFreeCursor(dmxScreen->beDisplay, pCursorPriv->cursor);
	pCursorPriv->cursor = (Cursor)0;
	return TRUE;
    }

    return FALSE;
}

static Bool _dmxUnrealizeCursor(ScreenPtr pScreen, CursorPtr pCursor)
{
    DMXScreenInfo    *dmxScreen = &dmxScreens[pScreen->myNum];

    DMXDBG2("_dmxUnrealizeCursor(%d,%p)\n",
            pScreen->myNum, pCursor);

    if (dmxScreen->beDisplay) {
	if (dmxBEFreeCursor(pScreen, pCursor))
	    xfree(pCursor->devPriv[pScreen->myNum]);
    }
    pCursor->devPriv[pScreen->myNum] = NULL;

    return TRUE;
}

static void _dmxMoveCursor(ScreenPtr pScreen, int x, int y)
{
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
    int           newX       = x + dmxScreen->rootX;
    int           newY       = y + dmxScreen->rootY;

    if (newX < 0) newX = 0;
    if (newY < 0) newY = 0;

    DMXDBG5("_dmxMoveCursor(%d,%d,%d) -> %d,%d\n",
            pScreen->myNum, x, y, newX, newY);
    if (dmxScreen->beDisplay) {
	XWarpPointer(dmxScreen->beDisplay, None, dmxScreen->scrnWin,
		     0, 0, 0, 0, newX, newY);
	dmxSync(dmxScreen, TRUE);
    }
}

static void _dmxSetCursor(ScreenPtr pScreen, CursorPtr pCursor, int x, int y)
{
    DMXScreenInfo    *dmxScreen = &dmxScreens[pScreen->myNum];

    DMXDBG4("_dmxSetCursor(%d,%p,%d,%d)\n", pScreen->myNum, pCursor, x, y);

    if (pCursor) {
	dmxCursorPrivPtr  pCursorPriv = DMX_GET_CURSOR_PRIV(pCursor, pScreen);
	if (dmxScreen->curCursor != pCursorPriv->cursor) {
	    if (dmxScreen->beDisplay)
		XDefineCursor(dmxScreen->beDisplay, dmxScreen->scrnWin,
			      pCursorPriv->cursor);
            dmxScreen->cursor        = pCursor;
	    dmxScreen->curCursor     = pCursorPriv->cursor;
            dmxScreen->cursorVisible = 1;
	}
	_dmxMoveCursor(pScreen, x, y);
    } else {
	if (dmxScreen->beDisplay)
	    XDefineCursor(dmxScreen->beDisplay, dmxScreen->scrnWin,
			  dmxScreen->noCursor);
        dmxScreen->cursor        = NULL;
	dmxScreen->curCursor     = (Cursor)0;
        dmxScreen->cursorVisible = 0;
    }
    if (dmxScreen->beDisplay) dmxSync(dmxScreen, TRUE);
}

static Bool dmxRealizeCursor(ScreenPtr pScreen, CursorPtr pCursor)
{
    DMXScreenInfo *start = &dmxScreens[pScreen->myNum];
    DMXScreenInfo *pt;

    if (!start->over || !dmxCursorDoMultiCursors || start->cursorNotShared)
        return _dmxRealizeCursor(pScreen, pCursor);

    for (pt = start->over; /* condition at end of loop */; pt = pt->over) {
        if (pt->cursorNotShared) continue;
        _dmxRealizeCursor(screenInfo.screens[pt->index], pCursor);
        if (pt == start) break;
    }
    return TRUE;
}

static Bool dmxUnrealizeCursor(ScreenPtr pScreen, CursorPtr pCursor)
{
    DMXScreenInfo *start = &dmxScreens[pScreen->myNum];
    DMXScreenInfo *pt;

    if (!start->over || !dmxCursorDoMultiCursors || start->cursorNotShared)
        return _dmxUnrealizeCursor(pScreen, pCursor);

    for (pt = start->over; /* condition at end of loop */; pt = pt->over) {
        if (pt->cursorNotShared) continue;
        _dmxUnrealizeCursor(screenInfo.screens[pt->index], pCursor);
        if (pt == start) break;
    }
    return TRUE;
}

static CursorPtr dmxFindCursor(DMXScreenInfo *start)
{
    DMXScreenInfo *pt;

    if (!start || !start->over) return GetSpriteCursor();
    for (pt = start->over; /* condition at end of loop */; pt = pt->over) {
        if (pt->cursor) return pt->cursor;
        if (pt == start) break;
    }
    return GetSpriteCursor();
}

/** Move the cursor to coordinates (\a x, \a y)on \a pScreen.  This
 * function is usually called via #dmxPointerSpriteFuncs, except during
 * reconfiguration when the cursor is repositioned to force an update on
 * newley overlapping screens and on screens that no longer overlap. */
void dmxMoveCursor(ScreenPtr pScreen, int x, int y)
{
    DMXScreenInfo *start = &dmxScreens[pScreen->myNum];
    DMXScreenInfo *pt;

    DMXDBG3("dmxMoveCursor(%d,%d,%d)\n", pScreen->myNum, x, y);

    if (!start->over || !dmxCursorDoMultiCursors || start->cursorNotShared) {
        _dmxMoveCursor(pScreen, x, y);
        return;
    }

    for (pt = start->over; /* condition at end of loop */; pt = pt->over) {
        if (pt->cursorNotShared) continue;
        if (dmxOnScreen(x + start->rootXOrigin, y + start->rootYOrigin, pt)) {
            if (/* pt != start && */ !pt->cursorVisible) {
                if (!pt->cursor) {
                                /* This only happens during
                                 * reconfiguration when a new overlap
                                 * occurs. */
                    CursorPtr pCursor;
                    
                    if ((pCursor = dmxFindCursor(start)))
                        _dmxRealizeCursor(screenInfo.screens[pt->index],
                                          pt->cursor = pCursor);
                    
                }
                _dmxSetCursor(screenInfo.screens[pt->index],
                              pt->cursor,
                              x + start->rootXOrigin - pt->rootXOrigin,
                              y + start->rootYOrigin - pt->rootYOrigin);
            }
            _dmxMoveCursor(screenInfo.screens[pt->index],
                           x + start->rootXOrigin - pt->rootXOrigin,
                           y + start->rootYOrigin - pt->rootYOrigin);
        } else if (/* pt != start && */ pt->cursorVisible) {
            _dmxSetCursor(screenInfo.screens[pt->index],
                          NULL,
                          x + start->rootXOrigin - pt->rootXOrigin,
                          y + start->rootYOrigin - pt->rootYOrigin);
        }
        if (pt == start) break;
    }
}

static void dmxSetCursor(ScreenPtr pScreen, CursorPtr pCursor, int x, int y)
{
    DMXScreenInfo *start = &dmxScreens[pScreen->myNum];
    DMXScreenInfo *pt;
    int           GX, GY, gx, gy;

    DMXDBG5("dmxSetCursor(%d %p, %p,%d,%d)\n",
            pScreen->myNum, start, pCursor, x, y);

                                /* We do this check here because of two cases:
                                 *
                                 * 1) if a client calls XWarpPointer()
                                 * and Xinerama is not running, we can
                                 * have mi's notion of the pointer
                                 * position out of phase with DMX's
                                 * notion.
                                 *
                                 * 2) if a down button is held while the
                                 * cursor moves outside the root window,
                                 * mi's notion of the pointer position
                                 * is out of phase with DMX's notion and
                                 * the cursor can remain visible when it
                                 * shouldn't be. */

    dmxGetGlobalPosition(&GX, &GY);
    gx = start->rootXOrigin + x;
    gy = start->rootYOrigin + y;
    if (x && y && (GX != gx || GY != gy))
        dmxCoreMotion(gx, gy, 0, DMX_NO_BLOCK);
    
    if (!start->over || !dmxCursorDoMultiCursors || start->cursorNotShared) {
        _dmxSetCursor(pScreen, pCursor, x, y);
        return;
    }

    for (pt = start->over; /* condition at end of loop */; pt = pt->over) {
        if (pt->cursorNotShared) continue;
        if (dmxOnScreen(x + start->rootXOrigin, y + start->rootYOrigin, pt)) {
            _dmxSetCursor(screenInfo.screens[pt->index], pCursor,
                          x + start->rootXOrigin - pt->rootXOrigin,
                          y + start->rootYOrigin - pt->rootYOrigin);
        } else {
            _dmxSetCursor(screenInfo.screens[pt->index], NULL,
                          x + start->rootXOrigin - pt->rootXOrigin,
                          y + start->rootYOrigin - pt->rootYOrigin);
        }
        if (pt == start) break;
    }
}


/** This routine is used by the backend input routines to hide the
 * cursor on a screen that is being used for relative input.  \see
 * dmxbackend.c */
void dmxHideCursor(DMXScreenInfo *dmxScreen)
{
    int       x, y;
    ScreenPtr pScreen = screenInfo.screens[dmxScreen->index];

    dmxGetGlobalPosition(&x, &y);
    _dmxSetCursor(pScreen, NULL, x, y);
}

/** This routine is called during reconfiguration to make sure the
 * cursor is visible. */
void dmxCheckCursor(void)
{
    int           i;
    int           x, y;
    ScreenPtr     pScreen;
    DMXScreenInfo *firstScreen;

    dmxGetGlobalPosition(&x, &y);
    firstScreen = dmxFindFirstScreen(x, y);

    DMXDBG2("dmxCheckCursor %d %d\n", x, y);
    for (i = 0; i < dmxNumScreens; i++) {
        DMXScreenInfo *dmxScreen = &dmxScreens[i];
        pScreen                  = screenInfo.screens[dmxScreen->index];

        if (!dmxOnScreen(x, y, dmxScreen)) {
            if (firstScreen && i == miPointerCurrentScreen()->myNum)
                miPointerSetNewScreen(firstScreen->index, x, y);
            _dmxSetCursor(pScreen, NULL,
                          x - dmxScreen->rootXOrigin,
                          y - dmxScreen->rootYOrigin);
        } else {
            if (!dmxScreen->cursor) {
                CursorPtr pCursor;
                
                if ((pCursor = dmxFindCursor(dmxScreen))) {
                    _dmxRealizeCursor(pScreen, dmxScreen->cursor = pCursor);
                }
            }
            _dmxSetCursor(pScreen, dmxScreen->cursor,
                          x - dmxScreen->rootXOrigin,
                          y - dmxScreen->rootYOrigin);
        }
    }
    DMXDBG2("   leave dmxCheckCursor %d %d\n", x, y);
}

miPointerSpriteFuncRec dmxPointerSpriteFuncs =
{
    dmxRealizeCursor,
    dmxUnrealizeCursor,
    dmxSetCursor,
    dmxMoveCursor,
};