aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/programs/Xserver/hw/nxagent/Split.c
blob: 4cc2ea6070989e46cf28c7c5f61a52f9c59e23fe (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
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
/**************************************************************************/
/*                                                                        */
/* Copyright (c) 2001, 2011 NoMachine, http://www.nomachine.com/.         */
/*                                                                        */
/* NXAGENT, NX protocol compression and NX extensions to this software    */
/* are copyright of NoMachine. Redistribution and use of the present      */
/* software is allowed according to terms specified in the file LICENSE   */
/* which comes in the source distribution.                                */
/*                                                                        */
/* Check http://www.nomachine.com/licensing.html for applicability.       */
/*                                                                        */
/* NX and NoMachine are trademarks of Medialogic S.p.A.                   */
/*                                                                        */
/* All rights reserved.                                                   */
/*                                                                        */
/**************************************************************************/

#include "scrnintstr.h"
#include "pixmapstr.h"
#include "windowstr.h"
#include "gcstruct.h"

#include "Agent.h"
#include "Display.h"
#include "Drawable.h"
#include "Events.h"
#include "GCs.h"

#include "NXlib.h"


/*
 * Set here the required log level.
 */

#define PANIC
#define WARNING
#undef  TEST
#undef  DEBUG

/*
 * This should be a macro but for now
 * we make it a real function to log
 * a warning in the logs.
 */

DrawablePtr nxagentSplitDrawable(DrawablePtr pDrawable)
{
  if (pDrawable -> type == DRAWABLE_PIXMAP &&
          nxagentPixmapIsVirtual((PixmapPtr) pDrawable))
  {
    #ifdef TEST
    fprintf(stderr, "nxagentSplitDrawable: WARNING! The drawable at [%p] is "
                "virtual. Assuming real at [%p].\n", (void *) pDrawable,
                    (void *) nxagentRealPixmap((PixmapPtr) pDrawable));
    #endif

    return (DrawablePtr) nxagentRealPixmap((PixmapPtr) pDrawable);
  }
  else
  {
    return pDrawable;
  }
}

void nxagentInitSplitResources()
{
  int resource;

  #ifdef TEST
  fprintf(stderr, "nxagentInitSplitResources: Initializing the split resources.\n");
  #endif

  for (resource = 0; resource < NXNumberOfResources; resource++)
  {
    SplitResourcePtr pResource = &nxagentSplitResources[resource];

    pResource -> pending  = 0;
    pResource -> commit   = 0;
    pResource -> split    = NXNoResource;
    pResource -> unpack   = NXNoResource;
    pResource -> drawable = NULL;
    pResource -> region   = NullRegion;
    pResource -> gc       = NULL;
  }
}

SplitResourcePtr nxagentAllocSplitResource()
{
  int resource;

  SplitResourcePtr pResource;

  for (;;)
  {
    resource = NXAllocSplit(nxagentDisplay, NXAnyResource);

    if (resource != NXNoResource)
    {
      #ifdef TEST
      fprintf(stderr, "nxagentAllocSplitResource: Reserving resource [%d] for the next split.\n",
                  resource);
      #endif

      break;
    }
    else
    {
      #ifdef PANIC
      fprintf(stderr, "nxagentAllocSplitResource: PANIC! No more resources for the next split.\n");
      #endif
/*
FIXME: Must deal with the case all resources are exausted.
*/
      FatalError("nxagentAllocSplitResource: PANIC! No more resources for the next split.\n");
    }
  }

  pResource = &nxagentSplitResources[resource];

  if (pResource -> pending != 0 || pResource -> split != NXNoResource ||
          pResource -> unpack != NXNoResource || pResource -> drawable != NULL ||
              pResource -> region != NullRegion || pResource -> commit != 0 ||
                  pResource -> gc != NULL)
  {
    /*
     * This is really an unrecoverable error.
     */

    #ifdef PANIC
    fprintf(stderr, "nxagentAllocSplitResource: PANIC! Invalid record for resource [%d] with "
                "pending [%d] split [%d] unpack [%d] drawable [%p] region [%p] commit [%d] gc [%p].\n",
                    resource, pResource -> pending, pResource -> split, pResource -> unpack,
                        (void *) pResource -> drawable, (void *) pResource -> region,
                            pResource -> commit, (void *) pResource -> gc);
    #endif

    FatalError("nxagentAllocSplitResource: PANIC! Invalid record for resource [%d] with "
                   "pending [%d] split [%d] unpack [%d] drawable [%p] region [%p] commit [%d] gc [%p].\n",
                       resource, pResource -> pending, pResource -> split, pResource -> unpack,
                           (void *) pResource -> drawable, (void *) pResource -> region,
                               pResource -> commit, (void *) pResource -> gc);
  }

  pResource -> pending = 1;
  pResource -> split   = resource;

  return pResource;
}

void nxagentFreeSplitResource(SplitResourcePtr pResource)
{
  if (pResource -> pending == 0 || pResource -> split == NXNoResource ||
          pResource -> drawable != NULL || pResource -> region != NullRegion ||
              pResource -> gc != NULL)
  {
    #ifdef PANIC
    fprintf(stderr, "nxagentFreeSplitResource: PANIC! Invalid record provided with values "
                "pending [%d] split [%d] unpack [%d] drawable [%p] region [%p] commit [%d] gc [%p].\n",
                    pResource -> pending, pResource -> split, pResource -> unpack,
                        (void *) pResource -> drawable, (void *) pResource -> region,
                            pResource -> commit, (void *) pResource -> gc);
    #endif

    FatalError("nxagentFreeSplitResource: PANIC! Invalid record provided with values "
                   "pending [%d] split [%d] unpack [%d] drawable [%p] region [%p] commit [%d] gc [%p].\n",
                       pResource -> pending, pResource -> split, pResource -> unpack,
                           (void *) pResource -> drawable, (void *) pResource -> region,
                               pResource -> commit, (void *) pResource -> gc);
  }

  #ifdef TEST
  fprintf(stderr, "nxagentFreeSplitResource: Clearing the record for resource [%d].\n",
              pResource -> split);
  #endif

  NXFreeSplit(nxagentDisplay, pResource -> split);

  pResource -> pending  = 0;
  pResource -> commit   = 0;
  pResource -> split    = NXNoResource;
  pResource -> unpack   = NXNoResource;
  pResource -> drawable = NULL;
  pResource -> region   = NullRegion;
  pResource -> gc       = NULL;
}

void nxagentInitUnpackResources()
{
/*
FIXME: This must be implemented.
*/
}

UnpackResourcePtr nxagentAllocUnpackResource()
{
/*
FIXME: This must be implemented.
*/
  return NULL;
}

void nxagentFreeUnpackResource(UnpackResourcePtr pResource)
{
/*
FIXME: This must be implemented.
*/
}

void nxagentReleaseAllSplits(void)
{
  int resource;

  #ifdef TEST
  fprintf(stderr, "nxagentReleaseAllSplits: Going to release all the split resources.\n");
  #endif

  for (resource = 0; resource < NXNumberOfResources; resource++)
  {
    SplitResourcePtr pResource = &nxagentSplitResources[resource];

    if (pResource != NULL && pResource -> pending == 1)
    {
      DrawablePtr pDrawable = pResource -> drawable;

      if (pDrawable != NULL)
      {
        #ifdef TEST
        fprintf(stderr, "nxagentReleaseAllSplits: Releasing the drawable at [%p] for "
                    "resource [%d].\n", (void *) pDrawable, pResource -> split);
        #endif

        nxagentReleaseSplit(pDrawable);
      }

      #ifdef TEST
      fprintf(stderr, "nxagentReleaseAllSplits: Freeing the resource [%d].\n",
                  resource);
      #endif

      nxagentFreeSplitResource(pResource);
    }
  }
}

/*
 * Check the coherency of the split record.
 */

#ifdef TEST

static void nxagentCheckSplit(DrawablePtr pDrawable, SplitResourcePtr pResource)
{
  if (pResource == NULL)
  {
    #ifdef PANIC
    fprintf(stderr, "nxagentCheckSplit: PANIC! No record associated to drawable at [%p].\n",
              (void *) pDrawable);
    #endif

    FatalError("nxagentCheckSplit: PANIC! No record associated to drawable at [%p].\n",
                   (void *) pDrawable);
  }
  else if (pResource -> drawable != pDrawable ||
               pResource -> split == NXNoResource)
  {
    #ifdef PANIC
    fprintf(stderr, "nxagentCheckSplit: PANIC! The record [%d] doesn't match the drawable at [%p].\n",
                pResource -> split, (void *) pDrawable);
    #endif

    FatalError("nxagentCheckSplit: PANIC! The record [%d] doesn't match the drawable at [%p].\n",
                pResource -> split, (void *) pDrawable);
  }
  else if (pResource -> commit == 1 &&
               pResource -> gc == NULL)
  {
    #ifdef PANIC
    fprintf(stderr, "nxagentCheckSplit: PANIC! The record [%d] doesn't have a valid GC.\n",
                pResource -> split);
    #endif

    FatalError("nxagentCheckSplit: PANIC! The record [%d] doesn't have a valid GC.\n",
                   pResource -> split);
  }
}

static void nxagentCheckResource(SplitResourcePtr pResource, int resource)
{
  if (pResource == NULL)
  {
    #ifdef PANIC
    fprintf(stderr, "nxagentCheckResource: PANIC! No record associated to resource [%d].\n",
                resource);
    #endif

    FatalError("nxagentCheckResource: PANIC! No record associated to resource [%d].\n",
                   resource);
  }
  else if ((pResource -> split != resource || pResource -> pending != 1) ||
               (pResource -> commit == 1 && (pResource -> drawable == NULL ||
                   pResource -> gc == NULL)))
  {
    #ifdef PANIC
    fprintf(stderr, "nxagentCheckResource: PANIC! Invalid record for resource [%d] with "
                "pending [%d] split [%d] unpack [%d] drawable [%p] region [%p] commit [%d] gc [%p].\n",
                    resource, pResource -> pending, pResource -> split, pResource -> unpack,
                        (void *) pResource -> drawable, (void *) pResource -> region,
                            pResource -> commit, (void *) pResource -> gc);
    #endif

    FatalError("nxagentCheckResource: PANIC! Invalid record for resource [%d] with "
                   "pending [%d] split [%d] unpack [%d] drawable [%p] region [%p] commit [%d] gc [%p].\n",
                       resource, pResource -> pending, pResource -> split, pResource -> unpack,
                           (void *) pResource -> drawable, (void *) pResource -> region,
                                pResource -> commit, (void *) pResource -> gc);
  }
}

#endif

int nxagentCreateSplit(DrawablePtr pDrawable, GCPtr *pGC)
{
  SplitResourcePtr pResource;

  pDrawable = nxagentSplitDrawable(pDrawable);

  pResource = nxagentAllocSplitResource();

  if (pDrawable -> type == DRAWABLE_PIXMAP)
  {
    nxagentPixmapPriv((PixmapPtr) pDrawable) -> splitResource = pResource;
  }
  else
  {
    nxagentWindowPriv((WindowPtr) pDrawable) -> splitResource = pResource;
  }

  pResource -> drawable = pDrawable;
  pResource -> commit   = 1;

  /*
   * Make a copy of the GC so the client
   * can safely remove it.
   */

  pResource -> gc = CreateScratchGC(pDrawable -> pScreen, pDrawable -> depth);

/*
FIXME: What do we do here?
*/
  if (pResource -> gc == NULL)
  {
    #ifdef PANIC
    fprintf(stderr, "nxagentCreateSplit: PANIC! Failed to create split GC for resource [%d].\n",
                pResource -> split);
    #endif

    FatalError("nxagentCreateSplit: PANIC! Failed to create split GC for resource [%d].\n",
                   pResource -> split);
  }
  else if (CopyGC(*pGC, pResource -> gc, GCFunction | GCPlaneMask |
                      GCSubwindowMode | GCClipXOrigin | GCClipYOrigin |
                           GCClipMask | GCForeground | GCBackground) != Success)
  {
/*
FIXME: What do we do here?
*/
    #ifdef PANIC
    fprintf(stderr, "nxagentCreateSplit: PANIC! Failed to copy split GC for resource [%d].\n",
                pResource -> split);
    #endif

    FatalError("nxagentCreateSplit: PANIC! Failed to copy split GC for resource [%d].\n",
                   pResource -> split);
  }

  #ifdef TEST
  fprintf(stderr, "nxagentCreateSplit: Associated GC at [%p] to resource [%d] "
              "with id [%lu].\n", (void *) pResource -> gc, pResource -> split,
                  (unsigned long) nxagentGC(pResource -> gc));
  #endif

  *pGC = pResource -> gc;

  #ifdef TEST
  fprintf(stderr, "nxagentCreateSplit: Associated resource [%d] to drawable at [%p].\n",
              pResource -> split, (void *) pResource -> drawable);
  #endif

  return pResource -> split;
}

/*
 * Set the region to be the current
 * streaming region.
 */

void nxagentRegionSplit(DrawablePtr pDrawable, RegionPtr pRegion)
{
  SplitResourcePtr pResource;

  pDrawable = nxagentSplitDrawable(pDrawable);

  pResource = nxagentSplitResource(pDrawable);

  #ifdef TEST

  fprintf(stderr, "nxagentRegionSplit: Associating region to resource [%d] drawable at [%p].\n",
              pResource -> split, (void *) pDrawable);

  nxagentCheckSplit(pDrawable, pResource);

  #endif
  
  if (pResource == NULL)
  {
    #ifdef PANIC
    fprintf(stderr, "nxagentRegionSplit: PANIC! No valid split record for drawable at [%p].\n",
                (void *) pDrawable);
    #endif

    return;
  }

  #ifdef TEST
  fprintf(stderr, "nxagentRegionSplit: Associated region [%d,%d,%d,%d] to drawable at [%p] "
              "with resource [%d].\n", pRegion -> extents.x1, pRegion -> extents.y1,
                  pRegion -> extents.x2, pRegion -> extents.y2, (void *) pDrawable,
                      pResource -> split);
  #endif

  pResource -> region = pRegion;
}

/*
 * Remove the association between the drawable
 * and the split resource. The resource is not
 * deallocated until the end of the split.
 */

void nxagentReleaseSplit(DrawablePtr pDrawable)
{
  SplitResourcePtr pResource;

  pDrawable = nxagentSplitDrawable(pDrawable);

  pResource = nxagentSplitResource(pDrawable);

  if (pResource == NULL)
  {
    #ifdef TEST
    fprintf(stderr, "nxagentReleaseSplit: No split resources for drawable at [%p].\n",
                (void *) pDrawable);
    #endif

    return;
  }

  #ifdef TEST

  fprintf(stderr, "nxagentReleaseSplit: Going to release resource [%d] for drawable at [%p].\n",
              pResource -> split, (void *) pDrawable);

  nxagentCheckSplit(pDrawable, pResource);

  #endif

  if (pResource -> region != NullRegion)
  {
    /*
     * If we have a region the commits
     * had to be still valid. In this
     * case tell the proxy to abort the
     * data transfer.
     */

    #ifdef TEST

    if (pResource -> commit == 0)
    {
      #ifdef PANIC
      fprintf(stderr, "nxagentReleaseSplit: PANIC! Found a region for resource [%d] but the "
                  "commits are invalid.\n", pResource -> split);
      #endif

      FatalError("nxagentCheckSplit: PANIC! Found a region for resource [%d] but the "
                     "commits are invalid.\n", pResource -> split);
    }

    #endif

    #ifdef TEST
    fprintf(stderr, "nxagentValidateSplit: Aborting the data transfer for resource [%d].\n",
                pResource -> split);
    #endif

    NXAbortSplit(nxagentDisplay, pResource -> split);

    #ifdef TEST
    fprintf(stderr, "nxagentReleaseSplit: Freeing the region for drawable at [%p].\n",
                (void *) pDrawable);
    #endif

    REGION_DESTROY(pDrawable -> pScreen, pResource -> region);

    pResource -> region = NullRegion;
  }

  if (pResource -> gc != NULL)
  {
    #ifdef TEST
    fprintf(stderr, "nxagentReleaseSplit: Freeing the split GC for drawable at [%p].\n",
                (void *) pDrawable);
    #endif

    FreeScratchGC(pResource -> gc);

    pResource -> gc = NULL;
  }

  /*
   * Remove the association between the
   * drawable and the resource record.
   */

  #ifdef TEST
  fprintf(stderr, "nxagentReleaseSplit: Removing association to drawable at [%p].\n",
              (void *) pDrawable);
  #endif

  if (pDrawable -> type == DRAWABLE_PIXMAP)
  {
    nxagentPixmapPriv((PixmapPtr) pDrawable) -> splitResource = NULL;
  }
  else
  {
    nxagentWindowPriv((WindowPtr) pDrawable) -> splitResource = NULL;
  }

  /*
   * Invalidate the commits and remove the
   * association between the resource and
   * the drawable.
   */

  pResource -> drawable = NULL;
  pResource -> commit   = 0;
}

void nxagentValidateSplit(DrawablePtr pDrawable, RegionPtr pRegion)
{
  SplitResourcePtr pResource;

  pDrawable = nxagentSplitDrawable(pDrawable);

  pResource = nxagentSplitResource(pDrawable);

  if (pResource == NULL)
  {
    #ifdef TEST
    fprintf(stderr, "nxagentValidateSplit: No split resource for drawable at [%p].\n",
                (void *) pDrawable);
    #endif

    return;
  }
  else if (pResource -> region == NullRegion)
  {
    #ifdef TEST
    fprintf(stderr, "nxagentValidateSplit: No split region yet for drawable at [%p].\n",
                (void *) pDrawable);
    #endif

    return;
  }
  else if (pResource -> commit == 0)
  {
    #ifdef TEST
    fprintf(stderr, "nxagentValidateSplit: WARNING! Split for drawable at [%p] was "
                "already invalidated.\n", (void *) pDrawable);
    #endif

    return;
  }

  #ifdef TEST

  fprintf(stderr, "nxagentValidateSplit: Going to validate resource [%d] drawable at [%p].\n",
              pResource -> split, (void *) pDrawable);

  nxagentCheckSplit(pDrawable, pResource);

  #endif
  
  #ifdef TEST
  fprintf(stderr, "nxagentValidateSplit: Checking the region for resource [%d] "
              "and drawable at [%p].\n", pResource -> split, (void *) pDrawable);
  #endif

  /*
   * If a null region is passed as parameter,
   * we assume that all the commits have to
   * be discarded.
   */

  if (pRegion == NullRegion)
  {
    #ifdef TEST
    fprintf(stderr, "nxagentValidateSplit: Forcing all commits as invalid for "
                "drawable at [%p].\n", (void *) pDrawable);
    #endif

    nxagentReleaseSplit(pDrawable);
  }
  else
  {
    RegionRec tmpRegion;

    /*
     * Check if the provided region overlaps
     * the area covered by the image being
     * streamed.
     */

    REGION_INIT(pDrawable -> pScreen, &tmpRegion, NullBox, 1);

    REGION_INTERSECT(pDrawable -> pScreen, &tmpRegion, pResource -> region, pRegion);

    if (REGION_NIL(&tmpRegion) == 0)
    {
      #ifdef TEST
      fprintf(stderr, "nxagentValidateSplit: Marking the overlapping commits as invalid "
                  "for drawable at [%p].\n", (void *) pDrawable);
      #endif

      nxagentReleaseSplit(pDrawable);
    }
    #ifdef TEST
    else
    {
      fprintf(stderr, "nxagentValidateSplit: Leaving the commits as valid for "
                  "drawable at [%p].\n", (void *) pDrawable);
    }
    #endif

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

void nxagentFreeSplit(int resource)
{
  DrawablePtr pDrawable;

  SplitResourcePtr pResource = &nxagentSplitResources[resource];

  if (pResource == NULL)
  {
    #ifdef PANIC
    fprintf(stderr, "nxagentFreeSplit: PANIC! No valid split record for resource [%d].\n",
                resource);
    #endif

    FatalError("nxagentFreeSplit: PANIC! No valid split record for resource [%d].\n",
                   resource);
  }
  else if (pResource -> split != resource)
  {
    #ifdef PANIC
    fprintf(stderr, "nxagentFreeSplit: PANIC! The record [%d] doesn't match the resource [%d].\n",
                pResource -> split, resource);
    #endif

    FatalError("nxagentFreeSplit: PANIC! The record [%d] doesn't match the resource [%d].\n",
                   pResource -> split, resource);
  }

  pDrawable = pResource -> drawable;

  if (pDrawable != NULL)
  {
    #ifdef TEST
    fprintf(stderr, "nxagentFreeSplit: Removing association to drawable at [%p].\n",
                (void *) pDrawable);
    #endif

    nxagentReleaseSplit(pDrawable);
  }
  #ifdef TEST
  else
  {
    /*
     * The end of the split has come after we have
     * invalidated the operation and removed the
     * association to the drawable. This happens,
     * for example, if the drawable is destroyed.
     */

    fprintf(stderr, "nxagentFreeSplit: WARNING! Releasing invalidated resource [%d].\n",
                pResource -> split);
  }
  #endif

  #ifdef TEST
  fprintf(stderr, "nxagentFreeSplit: Freeing the record for resource [%d].\n",
              pResource -> split);
  #endif

  nxagentFreeSplitResource(pResource);
}

/*
FIXME: This must be enabled when the vanilla
       synchronization procedure is working.
*/
#define USE_FINISH_SPLIT

void nxagentWaitDrawable(DrawablePtr pDrawable)
{
  SplitResourcePtr pResource;

  pDrawable = nxagentSplitDrawable(pDrawable);

  pResource = nxagentSplitResource(pDrawable);

  if (pResource == NULL)
  {
    #ifdef TEST
    fprintf(stderr, "++++++nxagentWaitDrawable: WARNING! The drawable at [%p] is already awake.\n",
	    (void *) pDrawable);
    #endif

    return;
  }

  #ifdef TEST
  fprintf(stderr, "++++++nxagentWaitDrawable: Waiting drawable at [%p] with resource [%d].\n",
              (void *) pDrawable, pResource -> split);
  #endif

  /*
   * Be sure we intercept an I/O error
   * as well as an interrupt.
   */

  #ifdef USE_FINISH_SPLIT

  NXFinishSplit(nxagentDisplay, pResource -> split);

  #endif

  NXFlushDisplay(nxagentDisplay, NXFlushBuffer);

  for (;;)
  {
    /*
     * Handling all the possible events here
     * preempts the queue and makes a better
     * use of the link.
     *
     * We should better use XIfEvent() instead
     * of looping again and again through the
     * event queue.
     */

    nxagentDispatchEvents(NULL);

    /*
     * Wait indefinitely until the resource
     * is released or the display is shut
     * down.
     */

    if (nxagentSplitResource(pDrawable) == NULL ||
            NXDisplayError(nxagentDisplay) == 1)
    {
      #ifdef TEST

      if (NXDisplayError(nxagentDisplay) == 1)
      {
        fprintf(stderr, "++++++nxagentWaitDrawable: WARNING! Display error detected while "
                    "waiting for the drawable.\n");
      }
      else
      {
        fprintf(stderr, "++++++nxagentWaitDrawable: Drawable at [%p] can now be restarted.\n",
		(void *) pDrawable);
      }

      #endif
 
      return;
    }

    #ifdef TEST
    fprintf(stderr, "++++++nxagentWaitDrawable: Yielding control to the NX transport.\n");
    #endif

    nxagentWaitEvents(nxagentDisplay, NULL);
  }
}

static Bool nxagentCommitSplitPredicate(Display *display, XEvent *event, XPointer ptr)
{
  return (event -> type == ClientMessage &&
              event -> xclient.data.l[0] == NXCommitSplitNotify &&
                  event -> xclient.window == 0 && event -> xclient.message_type == 0 &&
                      event -> xclient.format == 32);
}

void nxagentWaitCommitEvent(int resource)
{
  XEvent event;

  /*
   * Check if there is any commit pending. As
   * we are at it, handle any commit, even those
   * commits pertaining to other resources.
   *
   * We can receive some commits even if we'll
   * later receive a no-split event. The proxy,
   * in fact, may have missed to find the image
   * in the memory cache and could have loaded it
   * from disk (so requiring a commit) before we
   * marked the end of the split sequence.
   */

  while (nxagentCheckEvents(nxagentDisplay, &event,
                                nxagentCommitSplitPredicate, NULL) == 1)
  {
    int client   = event.xclient.data.l[1];
    int request  = event.xclient.data.l[2];
    int position = event.xclient.data.l[3];

    #ifdef TEST
    fprintf(stderr, "nxagentWaitCommitEvent: Commit event received with "
                "client [%d] request [%d] and position [%d].\n",
                    client, request, position);
    #endif

    nxagentHandleCommitSplitEvent(client, request, position);
  }
}

static Bool nxagentWaitSplitPredicate(Display *display, XEvent *event, XPointer ptr)
{
  return (event -> type == ClientMessage &&
              (event -> xclient.data.l[0] == NXNoSplitNotify ||
                  event -> xclient.data.l[0] == NXStartSplitNotify) &&
                      event -> xclient.window == 0 && event -> xclient.message_type == 0 &&
                          event -> xclient.format == 32);
}

int nxagentWaitSplitEvent(int resource)
{
  XEvent event;

  int split = 0;

  /*
   * Don't flush the link. We only want to
   * query the NX transport to check whether
   * the operation caused a split.
   */

  NXFlushDisplay(nxagentDisplay, NXFlushBuffer);

  for (;;)
  {
    #ifdef TEST
    fprintf(stderr, "nxagentWaitSplitEvent: Waiting for the split event for resource [%d].\n",
                resource);
    #endif

    XIfEvent(nxagentDisplay, &event, nxagentWaitSplitPredicate, NULL);

    if (NXDisplayError(nxagentDisplay) == 1)
    {
      #ifdef TEST
      fprintf(stderr, "nxagentWaitSplitEvent: WARNING! Error detected reading the event.\n");
      #endif

      nxagentHandleNoSplitEvent(resource);

      break;
    }

    #ifdef TEST
    fprintf(stderr, "nxagentWaitSplitEvent: Going to process the split event.\n");
    #endif

    if (resource != (int) event.xclient.data.l[1])
    {
      #ifdef PANIC
      fprintf(stderr, "nxagentWaitSplitEvent: PANIC! Got event for resource [%d] while "
                  "waiting for resource [%d].\n", (int) event.xclient.data.l[1], resource);

      fprintf(stderr, "nxagentWaitSplitEvent: PANIC! Restarting resource [%d] due to the "
                  "missing split event.\n", resource);
      #endif

      nxagentHandleNoSplitEvent(resource);

      break;
    }
    else if (event.xclient.data.l[0] == NXNoSplitNotify)
    {
      nxagentHandleNoSplitEvent(resource);

      break;
    }
    else
    {
      nxagentHandleStartSplitEvent(resource);

      split = 1;

      break;
    }
  }

  return split;
}

void nxagentHandleNoSplitEvent(int resource)
{
  if (resource >= 0 && resource < NXNumberOfResources)
  {
    #ifdef TEST

    SplitResourcePtr pResource = &nxagentSplitResources[resource];

    fprintf(stderr, "nxagentHandleNoSplitEvent: Received event for resource [%d].\n",
                resource);

    nxagentCheckResource(pResource, resource);

    #endif

    #ifdef TEST
    fprintf(stderr, "nxagentHandleNoSplitEvent: Checking if there is any commit pending.\n");
    #endif

    nxagentWaitCommitEvent(resource);

    #ifdef TEST
    fprintf(stderr, "nxagentHandleNoSplitEvent: No streaming was required with resource [%d] "
                "and drawable at [%p].\n", resource, (void *) pResource -> drawable);
    #endif

    /*
     * Release the resource.
     */

    nxagentFreeSplit(resource);
  }
  #ifdef PANIC
  else
  {
    fprintf(stderr, "nxagentHandleNoSplitEvent: PANIC! Invalid resource identifier [%d] "
                " received in event.\n", resource);
  }
  #endif
}

void nxagentHandleStartSplitEvent(int resource)
{
  if (resource >= 0 && resource < NXNumberOfResources)
  {
    #ifdef TEST

    SplitResourcePtr pResource = &nxagentSplitResources[resource];

    fprintf(stderr, "nxagentHandleStartSplitEvent: Received event for resource [%d].\n",
                resource);

    nxagentCheckResource(pResource, resource);

    #endif

    #ifdef TEST
    fprintf(stderr, "nxagentHandleStartSplitEvent: Checking if there is any commit pending.\n");
    #endif

    nxagentWaitCommitEvent(resource);

    #ifdef TEST
    fprintf(stderr, "nxagentHandleStartSplitEvent: Streaming started with resource [%d] "
                "and drawable at [%p].\n", resource, (void *) pResource -> drawable);
    #endif
  }
  #ifdef PANIC
  else
  {
    fprintf(stderr, "nxagentHandleStartSplitEvent: PANIC! Invalid resource identifier [%d] "
                " received in event.\n", resource);
  }
  #endif
}

void nxagentHandleCommitSplitEvent(int resource, int request, int position)
{
  if (resource >= 0 && resource < NXNumberOfResources &&
          request >= 0 && position >= 0)
  {
    SplitResourcePtr pResource = &nxagentSplitResources[resource];

    #ifdef TEST
    fprintf(stderr, "nxagentHandleCommitSplitEvent: Received event for resource [%d].\n",
                resource);
    #endif

    if (pResource != NULL && pResource -> commit == 1)
    {
      #ifdef TEST

      nxagentCheckResource(pResource, resource);

      fprintf(stderr, "nxagentHandleCommitSplitEvent: Committing request [%d] with "
                  "position [%d] for resource [%d].\n", request, position, resource);

      #endif

      NXCommitSplit(nxagentDisplay, resource, 1, request, position);
    }
    else
    {
      #ifdef TEST
      fprintf(stderr, "nxagentHandleCommitSplitEvent: Discarding request [%d] for "
                  "resource [%d] with position [%d].\n", request, resource, position);
      #endif

      NXCommitSplit(nxagentDisplay, resource, 0, request, position);
    }
  }
  #ifdef PANIC
  else
  {
    fprintf(stderr, "nxagentHandleCommitSplitEvent: PANIC! Invalid commit event with "
                "request [%d] and position [%d] for resource [%d].\n", request,
                    position, resource);
  }
  #endif
}

void nxagentHandleEndSplitEvent(int resource)
{
  if (resource >= 0 && resource < NXNumberOfResources)
  {
    SplitResourcePtr pResource = &nxagentSplitResources[resource];

    #ifdef TEST
    fprintf(stderr, "nxagentHandleEndSplitEvent: Received event for resource [%d].\n",
                resource);

    nxagentCheckResource(pResource, resource);

    #endif

    #ifdef TEST
    fprintf(stderr, "nxagentHandleEndSplitEvent: Checking if there is any commit pending.\n");
    #endif

    nxagentWaitCommitEvent(resource);

    if (pResource != NULL && pResource -> commit == 1)
    {
      #ifdef TEST

      fprintf(stderr, "nxagentHandleEndSplitEvent: Checking the split region at [%p] "
                  "for drawable at [%p].\n", (void *) pResource -> drawable,
                      (void *) pResource -> region);

      if (pResource -> region == NULL)
      {
        #ifdef PANIC
        fprintf(stderr, "nxagentHandleEndSplitEvent: PANIC! Invalid region [%p] for drawable at [%p].\n",
                    (void *) pResource -> region, (void *) pResource -> drawable);
        #endif

        FatalError("nxagentHandleEndSplitEvent: PANIC! Invalid region [%p] for drawable at [%p].\n",
                       (void *) pResource -> region, (void *) pResource -> drawable);
      }
      else if (pResource -> gc == NULL)
      {
        #ifdef PANIC
        fprintf(stderr, "nxagentHandleEndSplitEvent: PANIC! Invalid GC [%p] for drawable at [%p].\n",
                    (void *) pResource -> gc, (void *) pResource -> drawable);
        #endif

        FatalError("nxagentHandleEndSplitEvent: PANIC! Invalid GC [%p] for drawable at [%p].\n",
                       (void *) pResource -> gc, (void *) pResource -> drawable);
      }

      #endif

      if (pResource -> drawable != NULL &&
              pResource -> region != NullRegion)
      {
        if (REGION_NIL(pResource -> region) == 0)
        {
          REGION_SUBTRACT(pResource -> drawable -> pScreen,
                              nxagentCorruptedRegion(pResource -> drawable),
                                  nxagentCorruptedRegion(pResource -> drawable),
                                      pResource -> region);

/*
FIXME: Implementing the valid region policy

          nxagentExpandValidRegion(pResource -> drawable, pResource -> region);
*/

          #ifdef TEST
          fprintf(stderr, "nxagentHandleEndSplitEvent: Synchronized region [%d,%d,%d,%d] "
                      "for drawable at [%p].\n", pResource -> region -> extents.x1,
                          pResource -> region -> extents.y1, pResource -> region -> extents.x2,
                              pResource -> region -> extents.y2, (void *) pResource -> drawable);
          #endif
        }
        else
        {
          #ifdef PANIC
          fprintf(stderr, "nxagentHandleEndSplitEvent: PANIC! The region [%d,%d,%d,%d] for drawable "
                      "at [%p] is empty.\n", pResource -> region -> extents.x1,
                          pResource -> region -> extents.y1, pResource -> region -> extents.x2,
                              pResource -> region -> extents.y2, (void *) pResource -> drawable);
          #endif

          FatalError("nxagentHandleEndSplitEvent: PANIC! The region [%d,%d,%d,%d] for drawable "
                      "at [%p] is empty.\n", pResource -> region -> extents.x1,
                          pResource -> region -> extents.y1, pResource -> region -> extents.x2,
                              pResource -> region -> extents.y2, (void *) pResource -> drawable);
        }
      }
      else
      {
        #ifdef PANIC
        fprintf(stderr, "nxagentHandleEndSplitEvent: PANIC! Invalid record for resource [%d] with "
                    "pending [%d] split [%d] unpack [%d] drawable [%p] region [%p] commit [%d] gc [%p].\n",
                        resource, pResource -> pending, pResource -> split, pResource -> unpack,
                            (void *) pResource -> drawable, (void *) pResource -> region,
                                pResource -> commit, (void *) pResource -> gc);
        #endif

        FatalError("nxagentHandleEndSplitEvent: PANIC! Invalid record for resource [%d] with "
                       "pending [%d] split [%d] unpack [%d] drawable [%p] region [%p] commit [%d] gc [%p].\n",
                           resource, pResource -> pending, pResource -> split, pResource -> unpack,
                               (void *) pResource -> drawable, (void *) pResource -> region,
                                    pResource -> commit, (void *) pResource -> gc);
      }
    }

    /*
     * Release the resource.
     */

    nxagentFreeSplit(resource);
  }
  #ifdef TEST
  else
  {
    fprintf(stderr, "nxagentHandleEndSplitEvent: WARNING! Ignoring split event "
                "for resource [%d].\n", resource);
  }
  #endif
}

void nxagentHandleEmptySplitEvent()
{
/*
FIXME: Should run a consistency check here.
*/
  #ifdef TEST
  fprintf(stderr, "nxagentHandleEmptySplitEvent: No more split event to handle.\n");
  #endif
}

/*
 * The drawable is going to become corrupted.
 */

void nxagentSetCorruptedTimestamp(DrawablePtr pDrawable)
{
  if (nxagentDrawableStatus(pDrawable) == Synchronized)
  {
    if (pDrawable -> type == DRAWABLE_PIXMAP)
    {
      #ifdef TEST
      fprintf(stderr, "nxagentSetCorruptedTimestamp: Corruption timestamp for pixmap at [%p] was [%lu].\n",
                  (void *) pDrawable, nxagentPixmapPriv((PixmapPtr) pDrawable) -> corruptedTimestamp);
      #endif

      nxagentPixmapPriv((PixmapPtr) pDrawable) -> corruptedTimestamp = GetTimeInMillis();

      #ifdef TEST
      fprintf(stderr, "nxagentSetCorruptedTimestamp: New corruption timestamp for pixmap at [%p] is [%lu].\n",
                  (void *) pDrawable, nxagentPixmapPriv((PixmapPtr) pDrawable) -> corruptedTimestamp);
      #endif
    }
    else
    {
      #ifdef TEST
      fprintf(stderr, "nxagentSetCorruptedTimestamp: Corruption timestamp for window at [%p] was [%lu].\n",
                  (void *) pDrawable, nxagentWindowPriv((WindowPtr) pDrawable) -> corruptedTimestamp);
      #endif

      nxagentWindowPriv((WindowPtr) pDrawable) -> corruptedTimestamp = GetTimeInMillis();

      #ifdef TEST
      fprintf(stderr, "nxagentSetCorruptedTimestamp: New corruption timestamp for window at [%p] is [%lu].\n",
                  (void *) pDrawable, nxagentWindowPriv((WindowPtr) pDrawable) -> corruptedTimestamp);
      #endif
    }
  }
}

/*
 * Reset the timestamp taken when the drawable
 * became initially corrupted. The timestamp is
 * reset only after the drawable has been fully
 * synchronized.
 */

void nxagentResetCorruptedTimestamp(DrawablePtr pDrawable)
{
  if (nxagentDrawableStatus(pDrawable) == Synchronized)
  {
    if (pDrawable -> type == DRAWABLE_PIXMAP)
    {
      #ifdef TEST
      fprintf(stderr, "nxagentResetCorruptedTimestamp: Corruption timestamp for pixmap at [%p] was [%lu].\n",
                  (void *) pDrawable, nxagentPixmapPriv((PixmapPtr) pDrawable) -> corruptedTimestamp);
      #endif

      nxagentPixmapPriv((PixmapPtr) pDrawable) -> corruptedTimestamp = 0;
    }
    else
    {
      #ifdef TEST
      fprintf(stderr, "nxagentResetCorruptedTimestamp: Corruption timestamp for window at [%p] was [%lu].\n",
                  (void *) pDrawable, nxagentWindowPriv((WindowPtr) pDrawable) -> corruptedTimestamp);
      #endif

      nxagentWindowPriv((WindowPtr) pDrawable) -> corruptedTimestamp = 0;
    }
  }
}