File: draw.c

package info (click to toggle)
tightvnc 1%3A1.3.10-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,380 kB
  • sloc: ansic: 215,034; cpp: 848; asm: 780; sh: 683; perl: 539; makefile: 213
file content (1876 lines) | stat: -rw-r--r-- 47,925 bytes parent folder | download | duplicates (13)
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
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
/*
 * draw.c - drawing routines for the RFB X server.  This is a set of
 * wrappers around the standard MI/MFB/CFB drawing routines which work out
 * to a fair approximation the region of the screen being modified by the
 * drawing.  If the RFB client is ready then the modified region of the screen
 * is sent to the client, otherwise the modified region will simply grow with
 * each drawing request until the client is ready.
 */

/*
 *  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
 *
 *  This is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This software is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this software; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 *  USA.
 */

/*

Copyright (c) 1989  X Consortium

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 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 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 NONINFRINGEMENT.  IN NO EVENT SHALL THE
X CONSORTIUM 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.

Except as contained in this notice, the name of the X Consortium shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the X Consortium.
*/

#include <stdio.h>
#include "scrnintstr.h"
#include "gcstruct.h"
#include "windowstr.h"
#include "regionstr.h"
#include "dixfontstr.h"
#include "rfb.h"
#include "mfb.h"

extern WindowPtr *WindowTable; /* Why isn't this in a header file? */

int rfbDeferUpdateTime = 40; /* ms */


/****************************************************************************/
/*
 * Macro definitions
 */
/****************************************************************************/

/* SLIGHTLY DIRTY HACK - use Composite Clip region calculated by mfb */

#define WINDOW_CLIP_REGION(_w, _gc) \
  (((mfbPrivGCPtr)((_gc)->devPrivates[mfbGCPrivateIndex].ptr))->pCompositeClip)

#define TRC(x) /* (fprintf x) */

/* ADD_TO_MODIFIED_REGION adds the given region to the modified region for each
   client */

#define ADD_TO_MODIFIED_REGION(pScreen,reg)				      \
  {									      \
      rfbClientPtr cl;							      \
      for (cl = rfbClientHead; cl; cl = cl->next) {			      \
	  REGION_UNION((pScreen),&cl->modifiedRegion,&cl->modifiedRegion,reg);\
      }									      \
  }

/* SCHEDULE_FB_UPDATE is used at the end of each drawing routine to schedule an
   update to be sent to each client if there is one pending and the client is
   ready for it.  */

#define SCHEDULE_FB_UPDATE(pScreen,prfb)				\
  if (!prfb->dontSendFramebufferUpdate) {				\
      rfbClientPtr cl, nextCl;						\
      for (cl = rfbClientHead; cl; cl = nextCl) {			\
	  nextCl = cl->next;						\
	  if (!cl->deferredUpdateScheduled && FB_UPDATE_PENDING(cl) &&	\
	      REGION_NOTEMPTY(pScreen,&cl->requestedRegion))		\
	  {								\
	      rfbScheduleDeferredUpdate(cl);				\
	  }								\
      }									\
  }

/* function prototypes */

static void rfbScheduleDeferredUpdate(rfbClientPtr cl);
static void rfbCopyRegion(ScreenPtr pScreen, rfbClientPtr cl,
			  RegionPtr src, RegionPtr dst, int dx, int dy);
static void PrintRegion(ScreenPtr pScreen, RegionPtr reg);

/* GC funcs */

static void rfbValidateGC(GCPtr, unsigned long /*changes*/, DrawablePtr);
static void rfbChangeGC(GCPtr, unsigned long /*mask*/);
static void rfbCopyGC(GCPtr /*src*/, unsigned long /*mask*/, GCPtr /*dst*/);
static void rfbDestroyGC(GCPtr);
static void rfbChangeClip(GCPtr, int /*type*/, pointer /*pValue*/,
			  int /*nrects*/);
static void rfbDestroyClip(GCPtr);
static void rfbCopyClip(GCPtr /*dst*/, GCPtr /*src*/);

/* GC ops */

static void rfbFillSpans();
static void rfbSetSpans();
static void rfbPutImage();
static RegionPtr rfbCopyArea();
static RegionPtr rfbCopyPlane();
static void rfbPolyPoint();
static void rfbPolylines();
static void rfbPolySegment();
static void rfbPolyRectangle();
static void rfbPolyArc();
static void rfbFillPolygon();
static void rfbPolyFillRect();
static void rfbPolyFillArc();
static int rfbPolyText8();
static int rfbPolyText16();
static void rfbImageText8();
static void rfbImageText16();
static void rfbImageGlyphBlt();
static void rfbPolyGlyphBlt();
static void rfbPushPixels();


static GCFuncs rfbGCFuncs = {
    rfbValidateGC,
    rfbChangeGC,
    rfbCopyGC,
    rfbDestroyGC,
    rfbChangeClip,
    rfbDestroyClip,
    rfbCopyClip,
};


static GCOps rfbGCOps = {
    rfbFillSpans,	rfbSetSpans,	rfbPutImage,	
    rfbCopyArea,	rfbCopyPlane,	rfbPolyPoint,
    rfbPolylines,	rfbPolySegment,	rfbPolyRectangle,
    rfbPolyArc,		rfbFillPolygon,	rfbPolyFillRect,
    rfbPolyFillArc,	rfbPolyText8,	rfbPolyText16,
    rfbImageText8,	rfbImageText16,	rfbImageGlyphBlt,
    rfbPolyGlyphBlt,	rfbPushPixels
};



/****************************************************************************/
/*
 * Screen functions wrapper stuff
 */
/****************************************************************************/

#define SCREEN_PROLOGUE(scrn, field)		\
    ScreenPtr pScreen = scrn;			\
    rfbScreenInfoPtr prfb = &rfbScreen;		\
    pScreen->field = prfb->field;

#define SCREEN_EPILOGUE(field, wrapper) \
    pScreen->field = wrapper;


/*
 * CloseScreen wrapper -- unwrap everything, free the private data
 * and call the wrapped CloseScreen function.
 */

Bool
rfbCloseScreen (i, pScreen)
    int i;
    ScreenPtr	pScreen;
{
    rfbScreenInfoPtr prfb = &rfbScreen;

    pScreen->CloseScreen = prfb->CloseScreen;
    pScreen->CreateGC = prfb->CreateGC;
    pScreen->PaintWindowBackground = prfb->PaintWindowBackground;
    pScreen->PaintWindowBorder = prfb->PaintWindowBorder;
    pScreen->CopyWindow = prfb->CopyWindow;
    pScreen->ClearToBackground = prfb->ClearToBackground;
    pScreen->RestoreAreas = prfb->RestoreAreas;

    TRC((stderr,"Unwrapped screen functions\n"));

    return (*pScreen->CloseScreen) (i, pScreen);
}

/*
 * CreateGC - wrap the GC funcs (the GC ops will be wrapped when the GC
 * func "ValidateGC" is called).
 */

Bool
rfbCreateGC (pGC)
    GCPtr   pGC;
{
    Bool ret;
    rfbGCPtr pGCPriv;

    SCREEN_PROLOGUE(pGC->pScreen,CreateGC);
    
    pGCPriv = (rfbGCPtr)pGC->devPrivates[rfbGCIndex].ptr;

    ret = (*pScreen->CreateGC) (pGC);

    TRC((stderr,"rfbCreateGC called\n"));

    pGCPriv->wrapOps = NULL;
    pGCPriv->wrapFuncs = pGC->funcs;
    pGC->funcs = &rfbGCFuncs;

    SCREEN_EPILOGUE(CreateGC,rfbCreateGC);

    return ret;
}

/*
 * PaintWindowBackground - the region being modified is just the given region.
 */

void
rfbPaintWindowBackground (pWin, pRegion, what)
    WindowPtr	pWin;
    RegionPtr	pRegion;
    int		what;
{
    SCREEN_PROLOGUE(pWin->drawable.pScreen,PaintWindowBackground);

    TRC((stderr,"rfbPaintWindowBackground called\n"));

    ADD_TO_MODIFIED_REGION(pScreen,pRegion);

    (*pScreen->PaintWindowBackground) (pWin, pRegion, what);

    SCHEDULE_FB_UPDATE(pScreen, prfb);

    SCREEN_EPILOGUE(PaintWindowBackground,rfbPaintWindowBackground);
}

/*
 * PaintWindowBorder - the region being modified is just the given region.
 */

void
rfbPaintWindowBorder (pWin, pRegion, what)
    WindowPtr	pWin;
    RegionPtr	pRegion;
    int		what;
{
    SCREEN_PROLOGUE(pWin->drawable.pScreen,PaintWindowBorder);

    TRC((stderr,"rfbPaintWindowBorder called\n"));

    ADD_TO_MODIFIED_REGION(pScreen,pRegion);

    (*pScreen->PaintWindowBorder) (pWin, pRegion, what);

    SCHEDULE_FB_UPDATE(pScreen, prfb);

    SCREEN_EPILOGUE(PaintWindowBorder,rfbPaintWindowBorder);
}

/*
 * CopyWindow - the region being modified is the translation of the old
 * region, clipped to the border clip region of the window.  Note that any
 * parts of the window which have become newly-visible will not be affected by
 * this call - a separate PaintWindowBackground/Border will be called to do
 * that.  If the client will accept CopyRect messages then use rfbCopyRegion to
 * optimise the pending screen changes into a single "copy region" plus the
 * ordinary modified region.
 */

void
rfbCopyWindow (pWin, ptOldOrg, pOldRegion)
    WindowPtr	pWin;
    DDXPointRec	ptOldOrg;
    RegionPtr	pOldRegion;
{
    rfbClientPtr cl;
    RegionRec srcRegion, dstRegion;
    SCREEN_PROLOGUE(pWin->drawable.pScreen,CopyWindow);

    TRC((stderr,"rfbCopyWindow called\n"));

    REGION_INIT(pScreen,&dstRegion,NullBox,0);
    REGION_COPY(pScreen,&dstRegion,pOldRegion);
    REGION_TRANSLATE(pWin->drawable.pScreen, &dstRegion,
		     pWin->drawable.x - ptOldOrg.x,
		     pWin->drawable.y - ptOldOrg.y);
    REGION_INTERSECT(pWin->drawable.pScreen, &dstRegion, &dstRegion,
		     &pWin->borderClip);

    for (cl = rfbClientHead; cl; cl = cl->next) {
	if (cl->useCopyRect) {
	    REGION_INIT(pScreen,&srcRegion,NullBox,0);
	    REGION_COPY(pScreen,&srcRegion,pOldRegion);

	    rfbCopyRegion(pScreen, cl, &srcRegion, &dstRegion,
			  pWin->drawable.x - ptOldOrg.x,
			  pWin->drawable.y - ptOldOrg.y);

	    REGION_UNINIT(pSrc->pScreen, &srcRegion);

	} else {

	    REGION_UNION(pScreen, &cl->modifiedRegion, &cl->modifiedRegion,
			 &dstRegion);
	}
    }

    REGION_UNINIT(pSrc->pScreen, &dstRegion);

    (*pScreen->CopyWindow) (pWin, ptOldOrg, pOldRegion);

    SCHEDULE_FB_UPDATE(pScreen, prfb);

    SCREEN_EPILOGUE(CopyWindow,rfbCopyWindow);
}

/*
 * ClearToBackground - when generateExposures is false, the region being
 * modified is the given rectangle (clipped to the "window clip region").
 */

void
rfbClearToBackground (pWin, x, y, w, h, generateExposures)
    WindowPtr pWin;
    int x,y,w,h;
    Bool generateExposures;
{
    RegionRec tmpRegion;
    BoxRec box;
    SCREEN_PROLOGUE(pWin->drawable.pScreen,ClearToBackground);

    TRC((stderr,"rfbClearToBackground called\n"));

    if (!generateExposures) {
	box.x1 = x + pWin->drawable.x;
	box.y1 = y + pWin->drawable.y;
	box.x2 = w ? (box.x1 + w) : (pWin->drawable.x + pWin->drawable.width);
	box.y2 = h ? (box.y1 + h) : (pWin->drawable.y + pWin->drawable.height);

	SAFE_REGION_INIT(pScreen, &tmpRegion, &box, 0);

	REGION_INTERSECT(pScreen, &tmpRegion, &tmpRegion, &pWin->clipList);

	ADD_TO_MODIFIED_REGION(pScreen, &tmpRegion);

	REGION_UNINIT(pScreen, &tmpRegion);
    }

    (*pScreen->ClearToBackground) (pWin, x, y, w, h, generateExposures);

    if (!generateExposures) {
	SCHEDULE_FB_UPDATE(pScreen, prfb);
    }

    SCREEN_EPILOGUE(ClearToBackground,rfbClearToBackground);
}

/*
 * RestoreAreas - just be safe here - the region being modified is the whole
 * exposed region.
 */

RegionPtr
rfbRestoreAreas (pWin, prgnExposed)
    WindowPtr	pWin;
    RegionPtr	prgnExposed;
{
    RegionPtr result;
    SCREEN_PROLOGUE(pWin->drawable.pScreen,RestoreAreas);

    TRC((stderr,"rfbRestoreAreas called\n"));

    ADD_TO_MODIFIED_REGION(pScreen, prgnExposed);

    result = (*pScreen->RestoreAreas) (pWin, prgnExposed);

    SCHEDULE_FB_UPDATE(pScreen, prfb);

    SCREEN_EPILOGUE(RestoreAreas,rfbRestoreAreas);

    return result;
}



/****************************************************************************/
/*
 * GC funcs wrapper stuff
 *
 * We only really want to wrap the GC ops, but to do this we need to wrap
 * ValidateGC and so all the other GC funcs must be wrapped as well.
 */
/****************************************************************************/

#define GC_FUNC_PROLOGUE(pGC)						\
    rfbGCPtr pGCPriv = (rfbGCPtr) (pGC)->devPrivates[rfbGCIndex].ptr;	\
    (pGC)->funcs = pGCPriv->wrapFuncs;					\
    if (pGCPriv->wrapOps)						\
	(pGC)->ops = pGCPriv->wrapOps;

#define GC_FUNC_EPILOGUE(pGC)		\
    pGCPriv->wrapFuncs = (pGC)->funcs;	\
    (pGC)->funcs = &rfbGCFuncs;		\
    if (pGCPriv->wrapOps) {		\
	pGCPriv->wrapOps = (pGC)->ops;	\
	(pGC)->ops = &rfbGCOps;		\
    }


/*
 * ValidateGC - call the wrapped ValidateGC, then wrap the resulting GC ops if
 * the drawing will be to a viewable window.
 */

static void
rfbValidateGC (pGC, changes, pDrawable)
    GCPtr	pGC;
    unsigned long changes;
    DrawablePtr	pDrawable;
{
    GC_FUNC_PROLOGUE(pGC);

    TRC((stderr,"rfbValidateGC called\n"));

    (*pGC->funcs->ValidateGC) (pGC, changes, pDrawable);
    
    pGCPriv->wrapOps = NULL;
    if (pDrawable->type == DRAWABLE_WINDOW && ((WindowPtr)pDrawable)->viewable)
    {
	WindowPtr   pWin = (WindowPtr) pDrawable;
	RegionPtr   pRegion = &pWin->clipList;

	if (pGC->subWindowMode == IncludeInferiors)
	    pRegion = &pWin->borderClip;
	if (REGION_NOTEMPTY(pDrawable->pScreen, pRegion)) {
	    pGCPriv->wrapOps = pGC->ops;
	    TRC((stderr,"rfbValidateGC: wrapped GC ops\n"));
	}
    }

    GC_FUNC_EPILOGUE(pGC);
}

/*
 * All other GC funcs simply unwrap the GC funcs and ops, call the wrapped
 * function and then rewrap the funcs and ops.
 */

static void
rfbChangeGC (pGC, mask)
    GCPtr	    pGC;
    unsigned long   mask;
{
    GC_FUNC_PROLOGUE(pGC);
    (*pGC->funcs->ChangeGC) (pGC, mask);
    GC_FUNC_EPILOGUE(pGC);
}

static void
rfbCopyGC (pGCSrc, mask, pGCDst)
    GCPtr	    pGCSrc, pGCDst;
    unsigned long   mask;
{
    GC_FUNC_PROLOGUE(pGCDst);
    (*pGCDst->funcs->CopyGC) (pGCSrc, mask, pGCDst);
    GC_FUNC_EPILOGUE(pGCDst);
}

static void
rfbDestroyGC (pGC)
    GCPtr   pGC;
{
    GC_FUNC_PROLOGUE(pGC);
    (*pGC->funcs->DestroyGC) (pGC);
    GC_FUNC_EPILOGUE(pGC);
}

static void
rfbChangeClip (pGC, type, pvalue, nrects)
    GCPtr   pGC;
    int		type;
    pointer	pvalue;
    int		nrects;
{
    GC_FUNC_PROLOGUE(pGC);
    (*pGC->funcs->ChangeClip) (pGC, type, pvalue, nrects);
    GC_FUNC_EPILOGUE(pGC);
}

static void
rfbDestroyClip(pGC)
    GCPtr	pGC;
{
    GC_FUNC_PROLOGUE(pGC);
    (* pGC->funcs->DestroyClip)(pGC);
    GC_FUNC_EPILOGUE(pGC);
}

static void
rfbCopyClip(pgcDst, pgcSrc)
    GCPtr pgcDst, pgcSrc;
{
    GC_FUNC_PROLOGUE(pgcDst);
    (* pgcDst->funcs->CopyClip)(pgcDst, pgcSrc);
    GC_FUNC_EPILOGUE(pgcDst);
}


/****************************************************************************/
/*
 * GC ops wrapper stuff
 *
 * Note that these routines will only have been wrapped for drawing to
 * viewable windows so we don't need to check each time that the drawable
 * is a viewable window.
 */
/****************************************************************************/

#define GC_OP_PROLOGUE(pDrawable,pGC) \
    rfbScreenInfoPtr prfb = &rfbScreen; \
    rfbGCPtr pGCPrivate = (rfbGCPtr) (pGC)->devPrivates[rfbGCIndex].ptr; \
    GCFuncs *oldFuncs = pGC->funcs; \
    (pGC)->funcs = pGCPrivate->wrapFuncs; \
    (pGC)->ops = pGCPrivate->wrapOps;

#define GC_OP_EPILOGUE(pGC) \
    pGCPrivate->wrapOps = (pGC)->ops; \
    (pGC)->funcs = oldFuncs; \
    (pGC)->ops = &rfbGCOps;


/*
 * FillSpans - being very safe - the region being modified is the border clip
 * region of the window.
 */

static void
rfbFillSpans(pDrawable, pGC, nInit, pptInit, pwidthInit, fSorted)
    DrawablePtr pDrawable;
    GCPtr	pGC;
    int		nInit;			/* number of spans to fill */
    DDXPointPtr pptInit;		/* pointer to list of start points */
    int		*pwidthInit;		/* pointer to list of n widths */
    int 	fSorted;
{
    GC_OP_PROLOGUE(pDrawable,pGC);

    TRC((stderr,"rfbFillSpans called\n"));

    ADD_TO_MODIFIED_REGION(pDrawable->pScreen,
			   &((WindowPtr)pDrawable)->borderClip);

    (*pGC->ops->FillSpans) (pDrawable, pGC, nInit, pptInit,pwidthInit,fSorted);

    SCHEDULE_FB_UPDATE(pDrawable->pScreen, prfb);

    GC_OP_EPILOGUE(pGC);
}

/*
 * SetSpans - being very safe - the region being modified is the border clip
 * region of the window.
 */

static void
rfbSetSpans(pDrawable, pGC, psrc, ppt, pwidth, nspans, fSorted)
    DrawablePtr		pDrawable;
    GCPtr		pGC;
    char		*psrc;
    register DDXPointPtr ppt;
    int			*pwidth;
    int			nspans;
    int			fSorted;
{
    GC_OP_PROLOGUE(pDrawable,pGC);

    TRC((stderr,"rfbSetSpans called\n"));

    ADD_TO_MODIFIED_REGION(pDrawable->pScreen,
			   &((WindowPtr)pDrawable)->borderClip);

    (*pGC->ops->SetSpans) (pDrawable, pGC, psrc, ppt, pwidth, nspans, fSorted);

    SCHEDULE_FB_UPDATE(pDrawable->pScreen, prfb);

    GC_OP_EPILOGUE(pGC);
}

/*
 * PutImage - the region being modified is the rectangle of the
 * PutImage (clipped to the window clip region).
 */

static void
rfbPutImage(pDrawable, pGC, depth, x, y, w, h, leftPad, format, pBits)
    DrawablePtr	  pDrawable;
    GCPtr   	  pGC;
    int		  depth;
    int	    	  x;
    int	    	  y;
    int	    	  w;
    int	    	  h;
    int		  leftPad;
    int	    	  format;
    char    	  *pBits;
{
    RegionRec tmpRegion;
    BoxRec box;
    GC_OP_PROLOGUE(pDrawable, pGC);

    TRC((stderr,"rfbPutImage called\n"));

    box.x1 = x + pDrawable->x;
    box.y1 = y + pDrawable->y;
    box.x2 = box.x1 + w;
    box.y2 = box.y1 + h;

    SAFE_REGION_INIT(pDrawable->pScreen, &tmpRegion, &box, 0);

    REGION_INTERSECT(pDrawable->pScreen, &tmpRegion, &tmpRegion,
		     WINDOW_CLIP_REGION((WindowPtr)pDrawable,pGC));

    ADD_TO_MODIFIED_REGION(pDrawable->pScreen, &tmpRegion);

    REGION_UNINIT(pDrawable->pScreen, &tmpRegion);

    (*pGC->ops->PutImage) (pDrawable, pGC, depth, x, y, w, h,
			   leftPad, format, pBits);

    SCHEDULE_FB_UPDATE(pDrawable->pScreen, prfb);

    GC_OP_EPILOGUE(pGC);
}

/*
 * CopyArea - the region being modified is the destination rectangle (clipped
 * to the window clip region).
 * If the client will accept CopyRect messages then use rfbCopyRegion
 * to optimise the pending screen changes into a single "copy region" plus
 * the ordinary modified region.
 */

static RegionPtr
rfbCopyArea (pSrc, pDst, pGC, srcx, srcy, w, h, dstx, dsty)
    DrawablePtr	  pSrc;
    DrawablePtr	  pDst;
    GCPtr   	  pGC;
    int	    	  srcx;
    int	    	  srcy;
    int	    	  w;
    int	    	  h;
    int	    	  dstx;
    int	    	  dsty;
{
    rfbClientPtr cl;
    RegionPtr rgn;
    RegionRec srcRegion, dstRegion;
    BoxRec box;
    GC_OP_PROLOGUE(pDst, pGC);

    TRC((stderr,"rfbCopyArea called\n"));

    box.x1 = dstx + pDst->x;
    box.y1 = dsty + pDst->y;
    box.x2 = box.x1 + w;
    box.y2 = box.y1 + h;

    SAFE_REGION_INIT(pDst->pScreen, &dstRegion, &box, 0);
    REGION_INTERSECT(pDst->pScreen, &dstRegion, &dstRegion,
		     WINDOW_CLIP_REGION((WindowPtr)pDst,pGC));

    if ((pSrc->type == DRAWABLE_WINDOW) && (pSrc->pScreen == pDst->pScreen)) {
	box.x1 = srcx + pSrc->x;
	box.y1 = srcy + pSrc->y;
	box.x2 = box.x1 + w;
	box.y2 = box.y1 + h;

	for (cl = rfbClientHead; cl; cl = cl->next) {
	    if (cl->useCopyRect) {
		SAFE_REGION_INIT(pSrc->pScreen, &srcRegion, &box, 0);
		REGION_INTERSECT(pSrc->pScreen, &srcRegion, &srcRegion,
				 &((WindowPtr)pSrc)->clipList);

		rfbCopyRegion(pSrc->pScreen, cl, &srcRegion, &dstRegion,
			      dstx + pDst->x - srcx - pSrc->x,
			      dsty + pDst->y - srcy - pSrc->y);

		REGION_UNINIT(pSrc->pScreen, &srcRegion);

	    } else {

		REGION_UNION(pScreen, &cl->modifiedRegion, &cl->modifiedRegion,
			     &dstRegion);
	    }
	}

    } else {

	ADD_TO_MODIFIED_REGION(pDst->pScreen, &dstRegion);
    }

    REGION_UNINIT(pDst->pScreen, &dstRegion);

    rgn = (*pGC->ops->CopyArea) (pSrc, pDst, pGC, srcx, srcy, w, h,
				 dstx, dsty);

    SCHEDULE_FB_UPDATE(pDst->pScreen, prfb);

    GC_OP_EPILOGUE(pGC);

    return rgn;
}


/*
 * CopyPlane - the region being modified is the destination rectangle (clipped
 * to the window clip region).
 */

static RegionPtr
rfbCopyPlane (pSrc, pDst, pGC, srcx, srcy, w, h, dstx, dsty, plane)
    DrawablePtr	  pSrc;
    DrawablePtr	  pDst;
    register GCPtr pGC;
    int     	  srcx,
		  srcy;
    int     	  w,
		  h;
    int     	  dstx,
		  dsty;
    unsigned long  plane;
{
    RegionPtr rgn;
    RegionRec tmpRegion;
    BoxRec box;
    GC_OP_PROLOGUE(pDst, pGC);

    TRC((stderr,"rfbCopyPlane called\n"));

    box.x1 = dstx + pDst->x;
    box.y1 = dsty + pDst->y;
    box.x2 = box.x1 + w;
    box.y2 = box.y1 + h;

    SAFE_REGION_INIT(pDst->pScreen, &tmpRegion, &box, 0);

    REGION_INTERSECT(pDst->pScreen, &tmpRegion, &tmpRegion,
		     WINDOW_CLIP_REGION((WindowPtr)pDst,pGC));

    ADD_TO_MODIFIED_REGION(pDst->pScreen, &tmpRegion);

    REGION_UNINIT(pDst->pScreen, &tmpRegion);

    rgn = (*pGC->ops->CopyPlane) (pSrc, pDst, pGC, srcx, srcy, w, h,
				  dstx, dsty, plane);

    SCHEDULE_FB_UPDATE(pDst->pScreen, prfb);

    GC_OP_EPILOGUE(pGC);

    return rgn;
}

/*
 * PolyPoint - find the smallest rectangle which encloses the points drawn
 * (and clip).
 */

static void
rfbPolyPoint (pDrawable, pGC, mode, npt, pts)
    DrawablePtr pDrawable;
    GCPtr	pGC;
    int		mode;		/* Origin or Previous */
    int		npt;
    xPoint 	*pts;
{
    int i;
    RegionRec tmpRegion;
    BoxRec box;
    GC_OP_PROLOGUE(pDrawable, pGC);

    TRC((stderr,"rfbPolyPoint called\n"));

    if (npt) {
	int minX = pts[0].x, maxX = pts[0].x;
	int minY = pts[0].y, maxY = pts[0].y;

	if (mode == CoordModePrevious)
	{
	    int x = pts[0].x, y = pts[0].y;

	    for (i = 1; i < npt; i++) {
		x += pts[i].x;
		y += pts[i].y;
		if (x < minX) minX = x;
		if (x > maxX) maxX = x;
		if (y < minY) minY = y;
		if (y > maxY) maxY = y;
	    }
	}
	else
	{
	    for (i = 1; i < npt; i++) {
		if (pts[i].x < minX) minX = pts[i].x;
		if (pts[i].x > maxX) maxX = pts[i].x;
		if (pts[i].y < minY) minY = pts[i].y;
		if (pts[i].y > maxY) maxY = pts[i].y;
	    }
	}

	box.x1 = minX + pDrawable->x;
	box.y1 = minY + pDrawable->y;
	box.x2 = maxX + 1 + pDrawable->x;
	box.y2 = maxY + 1 + pDrawable->y;

	SAFE_REGION_INIT(pDrawable->pScreen, &tmpRegion, &box, 0);

	REGION_INTERSECT(pDrawable->pScreen, &tmpRegion, &tmpRegion,
			 WINDOW_CLIP_REGION(((WindowPtr)pDrawable),pGC));

	ADD_TO_MODIFIED_REGION(pDrawable->pScreen, &tmpRegion);

	REGION_UNINIT(pDrawable->pScreen, &tmpRegion);
    }

    (*pGC->ops->PolyPoint) (pDrawable, pGC, mode, npt, pts);

    if (npt) {
	SCHEDULE_FB_UPDATE(pDrawable->pScreen, prfb);
    }

    GC_OP_EPILOGUE(pGC);
}

/*
 * PolyLines - take the union of bounding boxes around each line (and clip).
 */

static void
rfbPolylines (pDrawable, pGC, mode, npt, ppts)
    DrawablePtr	  pDrawable;
    GCPtr   	  pGC;
    int	    	  mode;
    int	    	  npt;
    DDXPointPtr	  ppts;
{
    RegionPtr tmpRegion;
    xRectangle *rects;
    int i, extra, nlines, lw;
    int x1, x2, y1, y2;
    GC_OP_PROLOGUE(pDrawable, pGC);

    TRC((stderr,"rfbPolylines called\n"));

    if (npt) {
	lw = pGC->lineWidth;
	if (lw == 0)
	    lw = 1;

	if (npt == 1)
	{
	    nlines = 1;
	    rects = (xRectangle *)xalloc(sizeof(xRectangle));
	    if (!rects) {
		FatalError("rfbPolylines: xalloc failed\n");
	    }

	    rects[0].x = ppts[0].x - lw + pDrawable->x; /* being safe here */
	    rects[0].y = ppts[0].y - lw + pDrawable->y;
	    rects[0].width = 2*lw;
	    rects[0].height = 2*lw;
	}
	else
	{
	    nlines = npt - 1;
	    rects = (xRectangle *)xalloc(nlines*sizeof(xRectangle));
	    if (!rects) {
		FatalError("rfbPolylines: xalloc failed\n");
	    }

	    /*
	     * mitered joins can project quite a way from
	     * the line end; the 11 degree miter limit limits
	     * this extension to lw / (2 * tan(11/2)), rounded up
	     * and converted to int yields 6 * lw
	     */

	    if (pGC->joinStyle == JoinMiter) {
		extra = 6 * lw;
	    } else {
		extra = lw / 2;
	    }

	    x1 = ppts[0].x + pDrawable->x;
	    y1 = ppts[0].y + pDrawable->y;

	    for (i = 0; i < nlines; i++) {
		if (mode == CoordModeOrigin) {
		    x2 = pDrawable->x + ppts[i+1].x;
		    y2 = pDrawable->y + ppts[i+1].y;
		} else {
		    x2 = x1 + ppts[i+1].x;
		    y2 = y1 + ppts[i+1].y;
		}

		if (x1 > x2) {
		    rects[i].x = x2 - extra;
		    rects[i].width = x1 - x2 + 1 + 2 * extra;
		} else {
		    rects[i].x = x1 - extra;
		    rects[i].width = x2 - x1 + 1 + 2 * extra;
		}

		if (y1 > y2) {
		    rects[i].y = y2 - extra;
		    rects[i].height = y1 - y2 + 1 + 2 * extra;
		} else {
		    rects[i].y = y1 - extra;
		    rects[i].height = y2 - y1 + 1 + 2 * extra;
		}

		x1 = x2;
		y1 = y2;
	    }
	}
	tmpRegion = RECTS_TO_REGION(pDrawable->pScreen, nlines, rects,CT_NONE);
	REGION_INTERSECT(pDrawable->pScreen, tmpRegion, tmpRegion,
			 WINDOW_CLIP_REGION((WindowPtr)pDrawable,pGC));

	ADD_TO_MODIFIED_REGION(pDrawable->pScreen, tmpRegion);

	REGION_DESTROY(pDrawable->pScreen, tmpRegion);
	xfree((char *)rects);
    }

    (*pGC->ops->Polylines) (pDrawable, pGC, mode, npt, ppts);

    if (npt) {
	SCHEDULE_FB_UPDATE(pDrawable->pScreen, prfb);
    }

    GC_OP_EPILOGUE(pGC);
}

/*
 * PolySegment - take the union of bounding boxes around each segment (and
 * clip).
 */

static void
rfbPolySegment(pDrawable, pGC, nseg, segs)
    DrawablePtr pDrawable;
    GCPtr 	pGC;
    int		nseg;
    xSegment	*segs;
{
    RegionPtr tmpRegion;
    xRectangle *rects;
    int i, extra, lw;

    GC_OP_PROLOGUE(pDrawable, pGC);

    TRC((stderr,"rfbPolySegment called\n"));

    if (nseg) {
	rects = (xRectangle *)xalloc(nseg*sizeof(xRectangle));
	if (!rects) {
	    FatalError("rfbPolySegment: xalloc failed\n");
	}

	lw = pGC->lineWidth;
	if (lw == 0)
	    lw = 1;

	extra = lw / 2;

	for (i = 0; i < nseg; i++)
	{
	    if (segs[i].x1 > segs[i].x2) {
		rects[i].x = segs[i].x2 - extra + pDrawable->x;
		rects[i].width = segs[i].x1 - segs[i].x2 + 1 + 2 * extra;
	    } else {
		rects[i].x = segs[i].x1 - extra + pDrawable->x;
		rects[i].width = segs[i].x2 - segs[i].x1 + 1 + 2 * extra;
	    }

	    if (segs[i].y1 > segs[i].y2) {
		rects[i].y = segs[i].y2 - extra + pDrawable->y;
		rects[i].height = segs[i].y1 - segs[i].y2 + 1 + 2 * extra;
	    } else {
		rects[i].y = segs[i].y1 - extra + pDrawable->y;
		rects[i].height = segs[i].y2 - segs[i].y1 + 1 + 2 * extra;
	    }
	}

	tmpRegion = RECTS_TO_REGION(pDrawable->pScreen, nseg, rects, CT_NONE);
	REGION_INTERSECT(pDrawable->pScreen, tmpRegion, tmpRegion,
			 WINDOW_CLIP_REGION((WindowPtr)pDrawable,pGC));

	ADD_TO_MODIFIED_REGION(pDrawable->pScreen, tmpRegion);

	REGION_DESTROY(pDrawable->pScreen, tmpRegion);
	xfree((char *)rects);
    }

    (*pGC->ops->PolySegment) (pDrawable, pGC, nseg, segs);

    if (nseg) {
	SCHEDULE_FB_UPDATE(pDrawable->pScreen, prfb);
    }

    GC_OP_EPILOGUE(pGC);
}

/*
 * PolyRectangle (rectangle outlines) - take the union of bounding boxes
 * around each line (and clip).
 */

static void
rfbPolyRectangle(pDrawable, pGC, nrects, rects)
    DrawablePtr	pDrawable;
    GCPtr	pGC;
    int		nrects;
    xRectangle	*rects;
{
    int i, extra, lw;
    RegionPtr tmpRegion;
    xRectangle *regRects;
    GC_OP_PROLOGUE(pDrawable, pGC);

    TRC((stderr,"rfbPolyRectangle called\n"));

    if (nrects) {
	regRects = (xRectangle *)xalloc(nrects*4*sizeof(xRectangle));
	if (!regRects) {
	    FatalError("rfbPolyRectangle: xalloc failed\n");
	}

	lw = pGC->lineWidth;
	if (lw == 0)
	    lw = 1;

	extra = lw / 2;

	for (i = 0; i < nrects; i++)
	{
	    regRects[i*4].x = rects[i].x - extra + pDrawable->x;
	    regRects[i*4].y = rects[i].y - extra + pDrawable->y;
	    regRects[i*4].width = rects[i].width + 1 + 2 * extra;
	    regRects[i*4].height = 1 + 2 * extra;

	    regRects[i*4+1].x = rects[i].x - extra + pDrawable->x;
	    regRects[i*4+1].y = rects[i].y - extra + pDrawable->y;
	    regRects[i*4+1].width = 1 + 2 * extra;
	    regRects[i*4+1].height = rects[i].height + 1 + 2 * extra;

	    regRects[i*4+2].x
		= rects[i].x + rects[i].width - extra + pDrawable->x;
	    regRects[i*4+2].y = rects[i].y - extra + pDrawable->y;
	    regRects[i*4+2].width = 1 + 2 * extra;
	    regRects[i*4+2].height = rects[i].height + 1 + 2 * extra;

	    regRects[i*4+3].x = rects[i].x - extra + pDrawable->x;
	    regRects[i*4+3].y
		= rects[i].y + rects[i].height - extra + pDrawable->y;
	    regRects[i*4+3].width = rects[i].width + 1 + 2 * extra;
	    regRects[i*4+3].height = 1 + 2 * extra;
	}

	tmpRegion = RECTS_TO_REGION(pDrawable->pScreen, nrects*4,
				    regRects, CT_NONE);
	REGION_INTERSECT(pDrawable->pScreen, tmpRegion, tmpRegion,
			 WINDOW_CLIP_REGION((WindowPtr)pDrawable,pGC));

	ADD_TO_MODIFIED_REGION(pDrawable->pScreen, tmpRegion);

	REGION_DESTROY(pDrawable->pScreen, tmpRegion);
	xfree((char *)regRects);
    }

    (*pGC->ops->PolyRectangle) (pDrawable, pGC, nrects, rects);

    if (nrects) {
	SCHEDULE_FB_UPDATE(pDrawable->pScreen, prfb);
    }

    GC_OP_EPILOGUE(pGC);
}

/*
 * PolyArc - take the union of bounding boxes around each arc (and clip).
 * Bounding boxes assume each is a full circle / ellipse.
 */

static void
rfbPolyArc(pDrawable, pGC, narcs, arcs)
    DrawablePtr	pDrawable;
    register GCPtr	pGC;
    int		narcs;
    xArc	*arcs;
{
    int i, extra, lw;
    RegionPtr tmpRegion;
    xRectangle *rects;
    GC_OP_PROLOGUE(pDrawable, pGC);

    TRC((stderr,"rfbPolyArc called\n"));

    if (narcs) {
	rects = (xRectangle *)xalloc(narcs*sizeof(xRectangle));
	if (!rects) {
	    FatalError("rfbPolyArc: xalloc failed\n");
	}

	lw = pGC->lineWidth;
	if (lw == 0)
	    lw = 1;

	extra = lw / 2;

	for (i = 0; i < narcs; i++)
	{
	    rects[i].x = arcs[i].x - extra + pDrawable->x;
	    rects[i].y = arcs[i].y - extra + pDrawable->y;
	    rects[i].width = arcs[i].width + lw;
	    rects[i].height = arcs[i].height + lw;
	}

	tmpRegion = RECTS_TO_REGION(pDrawable->pScreen, narcs, rects, CT_NONE);
	REGION_INTERSECT(pDrawable->pScreen, tmpRegion, tmpRegion,
			 WINDOW_CLIP_REGION((WindowPtr)pDrawable,pGC));

	ADD_TO_MODIFIED_REGION(pDrawable->pScreen, tmpRegion);

	REGION_DESTROY(pDrawable->pScreen, tmpRegion);
	xfree((char *)rects);
    }

    (*pGC->ops->PolyArc) (pDrawable, pGC, narcs, arcs);

    if (narcs) {
	SCHEDULE_FB_UPDATE(pDrawable->pScreen, prfb);
    }

    GC_OP_EPILOGUE(pGC);
}

/*
 * FillPolygon - take bounding box around polygon (and clip).
 */

static void
rfbFillPolygon(pDrawable, pGC, shape, mode, count, pts)
    register DrawablePtr pDrawable;
    register GCPtr	pGC;
    int			shape, mode;
    int			count;
    DDXPointPtr		pts;
{
    int i;
    RegionRec tmpRegion;
    BoxRec box;
    GC_OP_PROLOGUE(pDrawable, pGC);

    TRC((stderr,"rfbFillPolygon called\n"));

    if (count) {
	int minX = pts[0].x, maxX = pts[0].x;
	int minY = pts[0].y, maxY = pts[0].y;

	if (mode == CoordModePrevious)
	{
	    int x = pts[0].x, y = pts[0].y;

	    for (i = 1; i < count; i++) {
		x += pts[i].x;
		y += pts[i].y;
		if (x < minX) minX = x;
		if (x > maxX) maxX = x;
		if (y < minY) minY = y;
		if (y > maxY) maxY = y;
	    }
	}
	else
	{
	    for (i = 1; i < count; i++) {
		if (pts[i].x < minX) minX = pts[i].x;
		if (pts[i].x > maxX) maxX = pts[i].x;
		if (pts[i].y < minY) minY = pts[i].y;
		if (pts[i].y > maxY) maxY = pts[i].y;
	    }
	}

	box.x1 = minX + pDrawable->x;
	box.y1 = minY + pDrawable->y;
	box.x2 = maxX + 1 + pDrawable->x;
	box.y2 = maxY + 1 + pDrawable->y;

	SAFE_REGION_INIT(pDrawable->pScreen, &tmpRegion, &box, 0);

	REGION_INTERSECT(pDrawable->pScreen, &tmpRegion, &tmpRegion,
			 WINDOW_CLIP_REGION(((WindowPtr)pDrawable),pGC));

	ADD_TO_MODIFIED_REGION(pDrawable->pScreen, &tmpRegion);

	REGION_UNINIT(pDrawable->pScreen, &tmpRegion);
    }

    (*pGC->ops->FillPolygon) (pDrawable, pGC, shape, mode, count, pts);

    if (count) {
	SCHEDULE_FB_UPDATE(pDrawable->pScreen, prfb);
    }

    GC_OP_EPILOGUE(pGC);
}

/*
 * PolyFillRect - take the union of the given rectangles (and clip).
 */

static void
rfbPolyFillRect(pDrawable, pGC, nrects, rects)
    DrawablePtr pDrawable;
    GCPtr	pGC;
    int		nrects;
    xRectangle	*rects;
{
    RegionPtr tmpRegion;
    xRectangle *regRects;
    int i;
    GC_OP_PROLOGUE(pDrawable, pGC);

    TRC((stderr,"rfbPolyFillRect called\n"));

    if (nrects) {
	regRects = (xRectangle *)xalloc(nrects*sizeof(xRectangle));
	if (!regRects) {
	    FatalError("rfbPolyFillRect: xalloc failed\n");
	}

	for (i = 0; i < nrects; i++) {
	    regRects[i].x = rects[i].x + pDrawable->x;
	    regRects[i].y = rects[i].y + pDrawable->y;
	    regRects[i].width = rects[i].width;
	    regRects[i].height = rects[i].height;
	}

	tmpRegion = RECTS_TO_REGION(pDrawable->pScreen, nrects, regRects,
				    CT_NONE);
	REGION_INTERSECT(pDrawable->pScreen, tmpRegion, tmpRegion,
			 WINDOW_CLIP_REGION((WindowPtr)pDrawable,pGC));

	ADD_TO_MODIFIED_REGION(pDrawable->pScreen, tmpRegion);

	REGION_DESTROY(pDrawable->pScreen, tmpRegion);
	xfree((char *)regRects);
    }

    (*pGC->ops->PolyFillRect) (pDrawable, pGC, nrects, rects);

    if (nrects) {
	SCHEDULE_FB_UPDATE(pDrawable->pScreen, prfb);
    }

    GC_OP_EPILOGUE(pGC);
}

/*
 * PolyFillArc - take the union of bounding boxes around each arc (and clip).
 * Bounding boxes assume each is a full circle / ellipse.
 */

static void
rfbPolyFillArc(pDrawable, pGC, narcs, arcs)
    DrawablePtr	pDrawable;
    GCPtr	pGC;
    int		narcs;
    xArc	*arcs;
{
    int i, extra, lw;
    RegionPtr tmpRegion;
    xRectangle *rects;
    GC_OP_PROLOGUE(pDrawable, pGC);

    TRC((stderr,"rfbPolyFillArc called\n"));

    if (narcs) {
	rects = (xRectangle *)xalloc(narcs*sizeof(xRectangle));
	if (!rects) {
	    FatalError("rfbPolyFillArc: xalloc failed\n");
	}

	lw = pGC->lineWidth;
	if (lw == 0)
	    lw = 1;

	extra = lw / 2;

	for (i = 0; i < narcs; i++)
	{
	    rects[i].x = arcs[i].x - extra + pDrawable->x;
	    rects[i].y = arcs[i].y - extra + pDrawable->y;
	    rects[i].width = arcs[i].width + lw;
	    rects[i].height = arcs[i].height + lw;
	}

	tmpRegion = RECTS_TO_REGION(pDrawable->pScreen, narcs, rects, CT_NONE);
	REGION_INTERSECT(pDrawable->pScreen, tmpRegion, tmpRegion,
			 WINDOW_CLIP_REGION((WindowPtr)pDrawable,pGC));

	ADD_TO_MODIFIED_REGION(pDrawable->pScreen, tmpRegion);

	REGION_DESTROY(pDrawable->pScreen, tmpRegion);
	xfree((char *)rects);
    }

    (*pGC->ops->PolyFillArc) (pDrawable, pGC, narcs, arcs);

    if (narcs) {
	SCHEDULE_FB_UPDATE(pDrawable->pScreen, prfb);
    }

    GC_OP_EPILOGUE(pGC);
}

/*
 * Get a rough bounding box around n characters of the given font.
 */

static void GetTextBoundingBox(pDrawable, font, x, y, n, pbox)
    DrawablePtr pDrawable;
    FontPtr font;
    int x, y, n;
    BoxPtr pbox;
{
    int maxAscent, maxDescent, maxCharWidth;

    if (FONTASCENT(font) > FONTMAXBOUNDS(font,ascent))
	maxAscent = FONTASCENT(font);
    else
	maxAscent = FONTMAXBOUNDS(font,ascent);

    if (FONTDESCENT(font) > FONTMAXBOUNDS(font,descent))
	maxDescent = FONTDESCENT(font);
    else
	maxDescent = FONTMAXBOUNDS(font,descent);

    if (FONTMAXBOUNDS(font,rightSideBearing) > FONTMAXBOUNDS(font,characterWidth))
	maxCharWidth = FONTMAXBOUNDS(font,rightSideBearing);
    else
	maxCharWidth = FONTMAXBOUNDS(font,characterWidth);

    pbox->x1 = pDrawable->x + x;
    pbox->y1 = pDrawable->y + y - maxAscent;
    pbox->x2 = pbox->x1 + maxCharWidth * n;
    pbox->y2 = pbox->y1 + maxAscent + maxDescent;

    if (FONTMINBOUNDS(font,leftSideBearing) < 0) {
	pbox->x1 += FONTMINBOUNDS(font,leftSideBearing);
    }
}


/*
 * PolyText8 - use rough bounding box.
 */

static int
rfbPolyText8(pDrawable, pGC, x, y, count, chars)
    DrawablePtr pDrawable;
    GCPtr	pGC;
    int		x, y;
    int 	count;
    char	*chars;
{
    int	ret;
    RegionRec tmpRegion;
    BoxRec box;
    GC_OP_PROLOGUE(pDrawable, pGC);

    TRC((stderr,"rfbPolyText8 called '%.*s'\n",count,chars));

    if (count) {
	GetTextBoundingBox(pDrawable, pGC->font, x, y, count, &box);

	SAFE_REGION_INIT(pDrawable->pScreen, &tmpRegion, &box, 0);

	REGION_INTERSECT(pDrawable->pScreen, &tmpRegion, &tmpRegion,
			 WINDOW_CLIP_REGION(((WindowPtr)pDrawable),pGC));

	ADD_TO_MODIFIED_REGION(pDrawable->pScreen, &tmpRegion);

	REGION_UNINIT(pDrawable->pScreen, &tmpRegion);
    }

    ret = (*pGC->ops->PolyText8) (pDrawable, pGC, x, y, count, chars);

    if (count) {
	SCHEDULE_FB_UPDATE(pDrawable->pScreen, prfb);
    }

    GC_OP_EPILOGUE(pGC);
    return ret;
}

/*
 * PolyText16 - use rough bounding box.
 */

static int
rfbPolyText16(pDrawable, pGC, x, y, count, chars)
    DrawablePtr pDrawable;
    GCPtr	pGC;
    int		x, y;
    int		count;
    unsigned short *chars;
{
    int	ret;
    RegionRec tmpRegion;
    BoxRec box;
    GC_OP_PROLOGUE(pDrawable, pGC);

    TRC((stderr,"rfbPolyText16 called\n"));

    if (count) {
	GetTextBoundingBox(pDrawable, pGC->font, x, y, count, &box);

	SAFE_REGION_INIT(pDrawable->pScreen, &tmpRegion, &box, 0);

	REGION_INTERSECT(pDrawable->pScreen, &tmpRegion, &tmpRegion,
			 WINDOW_CLIP_REGION(((WindowPtr)pDrawable),pGC));

	ADD_TO_MODIFIED_REGION(pDrawable->pScreen, &tmpRegion);

	REGION_UNINIT(pDrawable->pScreen, &tmpRegion);
    }

    ret = (*pGC->ops->PolyText16) (pDrawable, pGC, x, y, count, chars);

    if (count) {
	SCHEDULE_FB_UPDATE(pDrawable->pScreen, prfb);
    }

    GC_OP_EPILOGUE(pGC);
    return ret;
}

/*
 * ImageText8 - use rough bounding box.
 */

static void
rfbImageText8(pDrawable, pGC, x, y, count, chars)
    DrawablePtr pDrawable;
    GCPtr	pGC;
    int		x, y;
    int		count;
    char	*chars;
{
    RegionRec tmpRegion;
    BoxRec box;
    GC_OP_PROLOGUE(pDrawable, pGC);

    TRC((stderr,"rfbImageText8 called '%.*s'\n",count,chars));

    if (count) {
	GetTextBoundingBox(pDrawable, pGC->font, x, y, count, &box);

	SAFE_REGION_INIT(pDrawable->pScreen, &tmpRegion, &box, 0);

	REGION_INTERSECT(pDrawable->pScreen, &tmpRegion, &tmpRegion,
			 WINDOW_CLIP_REGION(((WindowPtr)pDrawable),pGC));

	ADD_TO_MODIFIED_REGION(pDrawable->pScreen, &tmpRegion);

	REGION_UNINIT(pDrawable->pScreen, &tmpRegion);
    }

    (*pGC->ops->ImageText8) (pDrawable, pGC, x, y, count, chars);

    if (count) {
	SCHEDULE_FB_UPDATE(pDrawable->pScreen, prfb);
    }

    GC_OP_EPILOGUE(pGC);
}

/*
 * ImageText16 - use rough bounding box.
 */

static void
rfbImageText16(pDrawable, pGC, x, y, count, chars)
    DrawablePtr pDrawable;
    GCPtr	pGC;
    int		x, y;
    int		count;
    unsigned short *chars;
{
    RegionRec tmpRegion;
    BoxRec box;
    GC_OP_PROLOGUE(pDrawable, pGC);

    TRC((stderr,"rfbImageText16 called\n"));

    if (count) {
	GetTextBoundingBox(pDrawable, pGC->font, x, y, count, &box);

	SAFE_REGION_INIT(pDrawable->pScreen, &tmpRegion, &box, 0);

	REGION_INTERSECT(pDrawable->pScreen, &tmpRegion, &tmpRegion,
			 WINDOW_CLIP_REGION(((WindowPtr)pDrawable),pGC));

	ADD_TO_MODIFIED_REGION(pDrawable->pScreen, &tmpRegion);

	REGION_UNINIT(pDrawable->pScreen, &tmpRegion);
    }

    (*pGC->ops->ImageText16) (pDrawable, pGC, x, y, count, chars);

    if (count) {
	SCHEDULE_FB_UPDATE(pDrawable->pScreen, prfb);
    }

    GC_OP_EPILOGUE(pGC);
}

/*
 * ImageGlyphBlt - use rough bounding box.
 */

static void
rfbImageGlyphBlt(pDrawable, pGC, x, y, nglyph, ppci, pglyphBase)
    DrawablePtr pDrawable;
    GCPtr 	pGC;
    int 	x, y;
    unsigned int nglyph;
    CharInfoPtr *ppci;		/* array of character info */
    pointer 	pglyphBase;	/* start of array of glyphs */
{
    RegionRec tmpRegion;
    BoxRec box;
    GC_OP_PROLOGUE(pDrawable, pGC);

    TRC((stderr,"rfbImageGlyphBlt called\n"));

    if (nglyph) {
	GetTextBoundingBox(pDrawable, pGC->font, x, y, nglyph, &box);

	SAFE_REGION_INIT(pDrawable->pScreen, &tmpRegion, &box, 0);

	REGION_INTERSECT(pDrawable->pScreen, &tmpRegion, &tmpRegion,
			 WINDOW_CLIP_REGION(((WindowPtr)pDrawable),pGC));

	ADD_TO_MODIFIED_REGION(pDrawable->pScreen, &tmpRegion);

	REGION_UNINIT(pDrawable->pScreen, &tmpRegion);
    }

    (*pGC->ops->ImageGlyphBlt) (pDrawable, pGC, x, y, nglyph, ppci,pglyphBase);

    if (nglyph) {
	SCHEDULE_FB_UPDATE(pDrawable->pScreen, prfb);
    }

    GC_OP_EPILOGUE(pGC);
}

/*
 * PolyGlyphBlt - use rough bounding box.
 */

static void
rfbPolyGlyphBlt(pDrawable, pGC, x, y, nglyph, ppci, pglyphBase)
    DrawablePtr pDrawable;
    GCPtr	pGC;
    int 	x, y;
    unsigned int nglyph;
    CharInfoPtr *ppci;		/* array of character info */
    pointer	pglyphBase;	/* start of array of glyphs */
{
    RegionRec tmpRegion;
    BoxRec box;
    GC_OP_PROLOGUE(pDrawable, pGC);

    TRC((stderr,"rfbPolyGlyphBlt called\n"));

    if (nglyph) {
	GetTextBoundingBox(pDrawable, pGC->font, x, y, nglyph, &box);

	SAFE_REGION_INIT(pDrawable->pScreen, &tmpRegion, &box, 0);

	REGION_INTERSECT(pDrawable->pScreen, &tmpRegion, &tmpRegion,
			 WINDOW_CLIP_REGION(((WindowPtr)pDrawable),pGC));

	ADD_TO_MODIFIED_REGION(pDrawable->pScreen, &tmpRegion);

	REGION_UNINIT(pDrawable->pScreen, &tmpRegion);
    }

    (*pGC->ops->PolyGlyphBlt) (pDrawable, pGC, x, y, nglyph, ppci, pglyphBase);

    if (nglyph) {
	SCHEDULE_FB_UPDATE(pDrawable->pScreen, prfb);
    }

    GC_OP_EPILOGUE(pGC);
}

/*
 * PushPixels - be fairly safe - region modified is intersection of the given
 * rectangle with the window clip region.
 */

static void
rfbPushPixels(pGC, pBitMap, pDrawable, w, h, x, y)
    GCPtr	pGC;
    PixmapPtr	pBitMap;
    DrawablePtr pDrawable;
    int		w, h, x, y;
{
    RegionRec tmpRegion;
    BoxRec box;
    GC_OP_PROLOGUE(pDrawable, pGC);

    TRC((stderr,"rfbPushPixels called\n"));

    box.x1 = x + pDrawable->x;
    box.y1 = y + pDrawable->y;
    box.x2 = box.x1 + w;
    box.y2 = box.y1 + h;

    SAFE_REGION_INIT(pDrawable->pScreen, &tmpRegion, &box, 0);

    REGION_INTERSECT(pDrawable->pScreen, &tmpRegion, &tmpRegion,
		     WINDOW_CLIP_REGION(((WindowPtr)pDrawable),pGC));

    ADD_TO_MODIFIED_REGION(pDrawable->pScreen, &tmpRegion);

    REGION_UNINIT(pDrawable->pScreen, &tmpRegion);

    (*pGC->ops->PushPixels) (pGC, pBitMap, pDrawable, w, h, x, y);

    SCHEDULE_FB_UPDATE(pDrawable->pScreen, prfb);

    GC_OP_EPILOGUE(pGC);
}



/****************************************************************************/
/*
 * Other functions
 */
/****************************************************************************/

/*
 * rfbCopyRegion.  Args are src and dst regions plus a translation (dx,dy).
 * Takes these args together with the existing modified region and possibly an
 * existing copy region and translation.  Produces a combined modified region
 * plus copy region and translation.  Note that the copy region is the
 * destination of the copy.
 *
 * First we trim parts of src which are invalid (ie in the modified region).
 * Then we see if there is any overlap between the src and the existing copy
 * region.  If not then the two copies cannot be combined, so we choose
 * whichever is bigger to form the basis of a new copy, while the other copy is
 * just done the hard way by being added to the modified region.  So if the
 * existing copy is bigger then we simply add the destination of the new copy
 * to the modified region and we're done.  If the new copy is bigger, we add
 * the old copy region to the modified region and behave as though there is no
 * existing copy region.
 * 
 * At this stage we now know that either the two copies can be combined, or
 * that there is no existing copy.  We temporarily add both the existing copy
 * region and dst to the modified region (this is the entire area of the screen
 * affected in any way).  Finally we calculate the new copy region, and remove
 * it from the modified region.
 *
 * Note:
 *   1. The src region is modified by this routine.
 *   2. When the copy region is empty, copyDX and copyDY MUST be set to zero.
 */

static void
rfbCopyRegion(pScreen, cl, src, dst, dx, dy)
    ScreenPtr pScreen;
    rfbClientPtr cl;
    RegionPtr src;
    RegionPtr dst;
    int dx, dy;
{
    RegionRec tmp;

    /* src = src - modifiedRegion */

    REGION_SUBTRACT(pScreen, src, src, &cl->modifiedRegion);

    if (REGION_NOTEMPTY(pScreen, &cl->copyRegion)) {

	REGION_INIT(pScreen, &tmp, NullBox, 0);
	REGION_INTERSECT(pScreen, &tmp, src, &cl->copyRegion);

	if (REGION_NOTEMPTY(pScreen, &tmp)) {

	    /* if src and copyRegion overlap:
	         src = src intersect copyRegion */

	    REGION_COPY(pScreen, src, &tmp);

	} else {

	    /* if no overlap, find bigger region */

	    int newArea = (((REGION_EXTENTS(pScreen,src))->x2
			    - (REGION_EXTENTS(pScreen,src))->x1)
			   * ((REGION_EXTENTS(pScreen,src))->y2
			      - (REGION_EXTENTS(pScreen,src))->y1));

	    int oldArea = (((REGION_EXTENTS(pScreen,&cl->copyRegion))->x2
			    - (REGION_EXTENTS(pScreen,&cl->copyRegion))->x1)
			   * ((REGION_EXTENTS(pScreen,&cl->copyRegion))->y2
			     - (REGION_EXTENTS(pScreen,&cl->copyRegion))->y1));

	    if (oldArea > newArea) {

		/* existing copy is bigger:
		     modifiedRegion = modifiedRegion union dst
		     copyRegion = copyRegion - dst
		     return */

		REGION_UNION(pScreen, &cl->modifiedRegion, &cl->modifiedRegion,
			     dst);
		REGION_SUBTRACT(pScreen, &cl->copyRegion, &cl->copyRegion,
				dst);
		if (!REGION_NOTEMPTY(pScreen, &cl->copyRegion)) {
		    cl->copyDX = 0;
		    cl->copyDY = 0;
		}
		return;
	    }

	    /* new copy is bigger:
	         modifiedRegion = modifiedRegion union copyRegion
		 copyRegion = empty */

	    REGION_UNION(pScreen, &cl->modifiedRegion, &cl->modifiedRegion,
			 &cl->copyRegion);
	    REGION_EMPTY(pScreen, &cl->copyRegion);
	    cl->copyDX = cl->copyDY = 0;
	}
    }


    /* modifiedRegion = modifiedRegion union dst union copyRegion */

    REGION_UNION(pScreen, &cl->modifiedRegion, &cl->modifiedRegion, dst);
    REGION_UNION(pScreen, &cl->modifiedRegion, &cl->modifiedRegion,
		 &cl->copyRegion);

    /* copyRegion = T(src) intersect dst */

    REGION_TRANSLATE(pScreen, src, dx, dy);
    REGION_INTERSECT(pScreen, &cl->copyRegion, src, dst);

    /* modifiedRegion = modifiedRegion - copyRegion */

    REGION_SUBTRACT(pScreen, &cl->modifiedRegion, &cl->modifiedRegion,
		    &cl->copyRegion);

    /* combine new translation T with existing translation */

    if (REGION_NOTEMPTY(pScreen, &cl->copyRegion)) {
	cl->copyDX += dx;
	cl->copyDY += dy;
    } else {
	cl->copyDX = 0;
	cl->copyDY = 0;
    }
}


/*
 * rfbDeferredUpdateCallback() is called when a client's deferredUpdateTimer
 * goes off.
 */

static CARD32
rfbDeferredUpdateCallback(OsTimerPtr timer, CARD32 now, pointer arg)
{
  rfbClientPtr cl = (rfbClientPtr)arg;

  rfbSendFramebufferUpdate(cl);

  cl->deferredUpdateScheduled = FALSE;
  return 0;
}


/*
 * rfbScheduleDeferredUpdate() is called from the SCHEDULE_FB_UPDATE macro
 * to schedule an update.
 */

static void
rfbScheduleDeferredUpdate(rfbClientPtr cl)
{
    if (rfbDeferUpdateTime != 0) {
	cl->deferredUpdateTimer = TimerSet(cl->deferredUpdateTimer, 0,
					   rfbDeferUpdateTime,
					   rfbDeferredUpdateCallback, cl);
	cl->deferredUpdateScheduled = TRUE;
    } else {
	rfbSendFramebufferUpdate(cl);
    }
}


/*
 * PrintRegion is useful for debugging.
 */

static void
PrintRegion(pScreen,reg)
    ScreenPtr pScreen;
    RegionPtr reg;
{
    int nrects = REGION_NUM_RECTS(reg);
    int i;

    rfbLog("Region num rects %d extents %d,%d %d,%d\n",nrects,
	   (REGION_EXTENTS(pScreen,reg))->x1,
	   (REGION_EXTENTS(pScreen,reg))->y1,
	   (REGION_EXTENTS(pScreen,reg))->x2,
	   (REGION_EXTENTS(pScreen,reg))->y2);

    for (i = 0; i < nrects; i++) {
	rfbLog("    rect %d,%d %dx%d\n",
	       REGION_RECTS(reg)[i].x1,
	       REGION_RECTS(reg)[i].y1,
	       REGION_RECTS(reg)[i].x2-REGION_RECTS(reg)[i].x1,
	       REGION_RECTS(reg)[i].y2-REGION_RECTS(reg)[i].y1);
    }
}