aboutsummaryrefslogtreecommitdiff
path: root/doc/nx-X11_vs_XOrg69_patches/nx-X11_lib_X11_XlibInt.c.X.original
blob: b76e169eea67e3fb64d464ec49f1896b04bbe3d6 (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
--- ./nx-X11/lib/X11/XlibInt.c.X.original	2015-02-13 14:03:44.624443872 +0100
+++ ./nx-X11/lib/X11/XlibInt.c	2015-02-10 19:13:12.800723493 +0100
@@ -26,6 +26,24 @@
 from The Open Group.
 
 */
+
+/**************************************************************************/
+/*                                                                        */
+/* Copyright (c) 2001, 2011 NoMachine, http://www.nomachine.com/.         */
+/*                                                                        */
+/* NX-X11, 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.                                                   */
+/*                                                                        */
+/**************************************************************************/
+
 /* $XFree86: xc/lib/X11/XlibInt.c,v 3.38 2003/10/30 21:55:05 alanh Exp $ */
 
 /*
@@ -100,6 +118,34 @@
 
 #endif /* XTHREADS else */ 
 
+#include "NX.h"
+
+#ifdef NX_TRANS_SOCKET
+
+#include "NX.h"
+#include "NXvars.h"
+
+static struct timeval retry;
+
+/*
+ * From Xtranssock.c. Presently the congestion state
+ * is reported by the proxy to the application, by
+ * invoking the callback directly. The function will
+ * be possibly used in the future, to be able to track
+ * the bandwidth usage even when the NX transport is
+ * not running. Note that in this sample implementation
+ * the congestion state is checked very often and can
+ * be surely optimized.
+ */
+
+#ifdef NX_TRANS_CHANGE
+
+extern int _X11TransSocketCongestionChange(XtransConnInfo, int *);
+
+#endif
+
+#endif /* #ifdef NX_TRANS_SOCKET */
+
 /* check for both EAGAIN and EWOULDBLOCK, because some supposedly POSIX
  * systems are broken and return EWOULDBLOCK when they should return EAGAIN
  */
@@ -219,6 +265,100 @@
 	0, 0, 0
 };
 
+#ifdef NX_TRANS_SOCKET
+
+/*
+ * Replace the standard Select with a version giving NX a
+ * chance to check its own descriptors. This doesn't cover
+ * the cases where the system is using poll or when system-
+ * specific defines override the Select definition (OS/2).
+ */
+
+int _XSelect(int maxfds, fd_set *readfds, fd_set *writefds,
+                 fd_set *exceptfds, struct timeval *timeout)
+{
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "_XSelect: Called with [%d][%p][%p][%p][%p].\n",
+                maxfds, (void *) readfds, (void *) writefds, (void *) exceptfds,
+                    (void *) timeout);
+#endif
+
+    if (NXTransRunning(NX_FD_ANY))
+    {
+        fd_set t_readfds, t_writefds;
+        struct timeval t_timeout;
+
+        int n, r, e;
+
+#ifdef NX_TRANS_TEST
+
+        if (exceptfds != NULL)
+        {
+            fprintf(stderr, "_XSelect: WARNING! Can't handle exception fds in select.\n");
+        }
+
+#endif
+
+        if (readfds == NULL)
+        {
+            FD_ZERO(&t_readfds);
+
+            readfds = &t_readfds;
+        }
+
+        if (writefds == NULL)
+        {
+            FD_ZERO(&t_writefds);
+
+            writefds = &t_writefds;
+        }
+
+        if (timeout == NULL)
+        {
+            t_timeout.tv_sec  = 10;
+            t_timeout.tv_usec = 0;
+
+            timeout = &t_timeout;
+        }
+
+        n = maxfds;
+
+        /*
+         * If the transport is gone avoid
+         * sleeping until the timeout.
+         */
+
+        if (NXTransPrepare(&n, readfds, writefds, timeout) != 0)
+        {
+          NXTransSelect(&r, &e, &n, readfds, writefds, timeout);
+
+          NXTransExecute(&r, &e, &n, readfds, writefds, timeout);
+
+          errno = e;
+
+          return r;
+        }
+        else
+        {
+          return 0;
+        }
+    }
+    else
+    {
+        return select(maxfds, readfds, writefds, exceptfds, timeout);
+    }
+}
+
+#else /* #ifdef NX_TRANS_SOCKET */
+
+int _XSelect(int maxfds, fd_set *readfds, fd_set *writefds,
+                 fd_set *exceptfds, struct timeval *timeout)
+{
+    return select(maxfds, readfds, writefds, exceptfds, timeout);
+}
+
+#endif /* #ifdef NX_TRANS_SOCKET */
+
 /*
  * This is an OS dependent routine which:
  * 1) returns as soon as the connection can be written on....
@@ -242,6 +382,18 @@
 #endif
     int nfound;
 
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_CHANGE)
+    int congestion;
+#endif
+
+#ifdef NX_TRANS_SOCKET
+
+    if (_XGetIOError(dpy)) {
+        return;
+    }
+
+#endif
+
 #ifdef USE_POLL
     filedes.fd = dpy->fd;
     filedes.events = 0;
@@ -276,6 +428,8 @@
 	     (!dpy->lock->reply_awaiters ||
 	      dpy->lock->reply_awaiters->cv == cv)))
 #endif
+
+#ifndef NX_TRANS_SOCKET
 #ifdef USE_POLL
 	    filedes.events = POLLIN;
 	filedes.events |= POLLOUT;
@@ -283,17 +437,109 @@
 	FD_SET(dpy->fd, &r_mask);
         FD_SET(dpy->fd, &w_mask);
 #endif
+#endif /* #ifndef NX_TRANS_SOCKET */
 
 	do {
+#ifdef NX_TRANS_SOCKET
+            /*
+             * Give a chance to the registered client to perform
+             * any needed operation before entering the select.
+             */
+
+#ifdef NX_TRANS_TEST
+            fprintf(stderr, "_XWaitForWritable: WAIT! Waiting for the display to become writable.\n");
+#endif
+            NXTransFlush(dpy->fd);
+
+            if (_NXDisplayBlockFunction != NULL) {
+                    (*_NXDisplayBlockFunction)(dpy, NXBlockWrite);
+            }
+
+            /*
+             * Need to set again the descriptors as we could have
+             * run multiple selects before having the possibility
+             * to read or write to the X connection.
+             */
+
+#ifdef USE_POLL
+            filedes.events = POLLIN;
+            filedes.events |= POLLOUT;
+#else
+            FD_SET(dpy->fd, &r_mask);
+            FD_SET(dpy->fd, &w_mask);
+#endif
+#endif /* #ifdef NX_TRANS_SOCKET */
+
 	    UnlockDisplay(dpy);
 #ifdef USE_POLL
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+            fprintf(stderr, "_XWaitForWritable: Calling poll().\n");
+#endif
 	    nfound = poll (&filedes, 1, -1);
 #else
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+            fprintf(stderr, "_XWaitForWritable: Calling select() after [%ld] ms.\n",
+                        NXTransTime());
+#endif
+#ifdef NX_TRANS_SOCKET
+            /*
+             * Give a chance to the callback to detect
+             * the failure of the display even if we
+             * miss the interrupt inside the select.
+             */
+
+            if (_NXDisplayErrorFunction != NULL) {
+                retry.tv_sec  = 5;
+                retry.tv_usec = 0;
+                nfound = Select (dpy->fd + 1, &r_mask, &w_mask, NULL, &retry);
+            } else {
+                nfound = Select (dpy->fd + 1, &r_mask, &w_mask, NULL, NULL);
+            }
+#else
 	    nfound = Select (dpy->fd + 1, &r_mask, &w_mask, NULL, NULL);
 #endif
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+            fprintf(stderr, "_XWaitForWritable: Out of select() with [%d] after [%ld] ms.\n",
+                        nfound, NXTransTime());
+
+            if (FD_ISSET(dpy->fd, &r_mask))
+            {
+                BytesReadable_t pend;
+
+                _X11TransBytesReadable(dpy->trans_conn, &pend);
+
+                fprintf(stderr, "_XWaitForWritable: Descriptor [%d] is ready with [%ld] bytes to read.\n",
+                            dpy->fd, pend);
+            }
+
+            if (FD_ISSET(dpy->fd, &w_mask))
+            {
+              fprintf(stderr, "_XWaitForWritable: Descriptor [%d] has become writable.\n\n",
+                          dpy->fd);
+            }
+#endif
+#endif
 	    InternalLockDisplay(dpy, cv != NULL);
-	    if (nfound < 0 && !ECHECK(EINTR))
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_CHANGE)
+            if (_NXDisplayCongestionFunction != NULL &&
+                    _X11TransSocketCongestionChange(dpy->trans_conn, &congestion) == 1) {
+                (*_NXDisplayCongestionFunction)(dpy, congestion);
+            }
+#endif
+
+#ifdef NX_TRANS_SOCKET
+            if (nfound <= 0) {
+                if ((nfound == -1 && !ECHECK(EINTR)) ||
+                        (_NXDisplayErrorFunction != NULL &&
+                            (*_NXDisplayErrorFunction)(dpy, _XGetIOError(dpy)))) {
+                    _XIOError(dpy);
+                    return;
+                }
+              }
+#else
+          if (nfound < 0 && !ECHECK(EINTR))
 		_XIOError(dpy);
+#endif
 	} while (nfound <= 0);
 
 	if (
@@ -311,7 +557,15 @@
 
 	    /* find out how much data can be read */
 	    if (_X11TransBytesReadable(dpy->trans_conn, &pend) < 0)
+#ifdef NX_TRANS_SOCKET
+            {
+                _XIOError(dpy);
+
+                return;
+            }
+#else
 		_XIOError(dpy);
+#endif
 	    len = pend;
 
 	    /* must read at least one xEvent; if none is pending, then
@@ -464,6 +718,15 @@
     int highest_fd = fd;
 #endif
 
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_CHANGE)
+    int congestion;
+#endif
+#ifdef NX_TRANS_SOCKET
+    if (_XGetIOError(dpy)) {
+        return -1;
+    }
+#endif
+
 #ifdef USE_POLL
     if (dpy->im_fd_length + 1 > POLLFD_CACHE_SIZE
 	&& !(dpy->flags & XlibDisplayProcConni)) {
@@ -495,16 +758,68 @@
 #endif
 	UnlockDisplay(dpy);
 #ifdef USE_POLL
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+        fprintf(stderr, "_XWaitForReadable: Calling poll().\n");
+#endif
 	result = poll(filedes,
 		      (dpy->flags & XlibDisplayProcConni) ? 1 : 1+dpy->im_fd_length,
 		      -1);
 #else
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+        fprintf(stderr, "_XWaitForReadable: Calling select().\n");
+#endif
+#ifdef NX_TRANS_SOCKET
+        /*
+         * Give a chance to the registered application
+         * to perform any needed operation.
+         */
+
+#ifdef NX_TRANS_TEST
+        fprintf(stderr, "_XWaitForReadable: WAIT! Waiting for the display to become readable.\n");
+#endif
+        NXTransFlush(dpy->fd);
+
+        if (_NXDisplayBlockFunction != NULL) {
+            (*_NXDisplayBlockFunction)(dpy, NXBlockRead);
+        }
+
+        if (_NXDisplayErrorFunction != NULL) {
+            retry.tv_sec  = 5;
+            retry.tv_usec = 0;
+            result = Select(highest_fd + 1, &r_mask, NULL, NULL, &retry);
+        } else {
+            result = Select(highest_fd + 1, &r_mask, NULL, NULL, NULL);
+        }
+#else
 	result = Select(highest_fd + 1, &r_mask, NULL, NULL, NULL);
 #endif
+#endif
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+        fprintf(stderr, "_XWaitForReadable: Out of select with result [%d] and errno [%d].\n",
+                    result, (result < 0 ? errno : 0));
+#endif
 	InternalLockDisplay(dpy, dpy->flags & XlibDisplayReply);
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_CHANGE)
+        if (_NXDisplayCongestionFunction != NULL &&
+                _X11TransSocketCongestionChange(dpy->trans_conn, &congestion) == 1) {
+            (*_NXDisplayCongestionFunction)(dpy, congestion);
+        }
+#endif
+#ifdef NX_TRANS_SOCKET
+        if (result <= 0) {
+            if ((result == -1 && !ECHECK(EINTR)) ||
+                    (_NXDisplayErrorFunction != NULL &&
+                        (*_NXDisplayErrorFunction)(dpy, _XGetIOError(dpy)))) {
+                _XIOError(dpy);
+                return -1;
+            }
+            continue;
+        }
+#else
 	if (result == -1 && !ECHECK(EINTR)) _XIOError(dpy);
 	if (result <= 0)
 	    continue;
+#endif
 #ifdef USE_POLL
 	if (filedes[0].revents & (POLLIN|POLLHUP|POLLERR))
 #else
@@ -562,6 +877,19 @@
     xGetInputFocusReply rep;
     register xReq *req;
 
+#ifdef NX_TRANS_SOCKET
+#ifdef NX_TRANS_DEBUG
+    fprintf(stderr, "_XSeqSyncFunction: Going to synchronize the display.\n");
+#endif
+    if (dpy->flags & XlibDisplayIOError)
+    {
+#ifdef NX_TRANS_DEBUG
+        fprintf(stderr, "_XSeqSyncFunction: Returning 0 with I/O error detected.\n");
+#endif
+        return 0;
+    }
+#endif
+
     LockDisplay(dpy);
     if ((dpy->request - dpy->last_request_read) >= (BUFSIZE / SIZEOF(xReq))) {
 	GetEmptyReq(GetInputFocus, req);
@@ -611,7 +939,14 @@
 	register int write_stat;
 	register char *bufindex;
 	_XExtension *ext;
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_CHANGE)
+        int congestion;
+#endif
 
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+        fprintf(stderr, "_XFlushInt: Entering flush with [%d] bytes to write.\n",
+                    (dpy->bufptr - dpy->buffer));
+#endif
 	/* This fix resets the bufptr to the front of the buffer so
 	 * additional appends to the bufptr will not corrupt memory. Since
 	 * the server is down, these appends are no-op's anyway but 
@@ -619,13 +954,23 @@
 	 */
 	if (dpy->flags & XlibDisplayIOError)
 	{
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+        fprintf(stderr, "_XFlushInt: Returning with I/O error detected.\n");
+#endif
 	    dpy->bufptr = dpy->buffer;
 	    dpy->last_req = (char *)&_dummy_request;
 	    return;
 	}
 
 #ifdef XTHREADS
+#ifdef NX_TRANS_SOCKET
+        while (dpy->flags & XlibDisplayWriting) {
+            if (_XGetIOError(dpy)) {
+                return;
+            }
+#else
 	while (dpy->flags & XlibDisplayWriting) {
+#endif
 	    if (dpy->lock) {
 		ConditionWait(dpy, dpy->lock->writers);
 	    } else {
@@ -653,6 +998,17 @@
 	    write_stat = _X11TransWrite(dpy->trans_conn,
 					bufindex, (int) todo);
 	    if (write_stat >= 0) {
+#ifdef NX_TRANS_SOCKET
+                if (_NXDisplayWriteFunction != NULL) {
+                    (*_NXDisplayWriteFunction)(dpy, write_stat);
+                }
+#ifdef NX_TRANS_CHANGE
+                if (_NXDisplayCongestionFunction != NULL &&
+                        _X11TransSocketCongestionChange(dpy->trans_conn, &congestion) == 1) {
+                    (*_NXDisplayCongestionFunction)(dpy, congestion);
+                }
+#endif
+#endif
 		size -= write_stat;
 		todo = size;
 		bufindex += write_stat;
@@ -682,11 +1038,25 @@
 				      );
 		}
 #endif
+#ifdef NX_TRANS_SOCKET
+            } else if (!ECHECK(EINTR) ||
+                (_NXDisplayErrorFunction != NULL &&
+                    (*_NXDisplayErrorFunction)(dpy, _XGetIOError(dpy)))) {
+                _XIOError(dpy);
+                return;
+            }
+#else
 	    } else if (!ECHECK(EINTR)) {
 		/* Write failed! */
 		/* errno set by write system call. */
 		_XIOError(dpy);
 	    }
+#endif
+#ifdef NX_TRANS_SOCKET
+            if (_XGetIOError(dpy)) {
+                return;
+            }
+#endif
 	}
 	dpy->last_req = (char *)&_dummy_request;
 	if ((dpy->request - dpy->last_request_read) >= SEQLIMIT &&
@@ -727,6 +1097,12 @@
 	    if (dpy->qlen)
 		return(dpy->qlen);
 	}
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+        if (dpy->flags & XlibDisplayIOError) {
+            fprintf(stderr, "_XEventsQueued: Returning [%d] after display failure.\n",
+                        dpy->qlen);
+        }
+#endif
 	if (dpy->flags & XlibDisplayIOError) return(dpy->qlen);
 
 #ifdef XTHREADS
@@ -767,8 +1143,19 @@
 	}
 #endif /* XTHREADS*/
 
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+        fprintf(stderr, "_XEventsQueued: Checking bytes readable.\n");
+#endif
 	if (_X11TransBytesReadable(dpy->trans_conn, &pend) < 0)
+#ifdef NX_TRANS_SOCKET
+        {
+            _XIOError(dpy);
+
+            return (dpy->qlen);
+        }
+#else
 	    _XIOError(dpy);
+#endif
 #ifdef XCONN_CHECK_FREQ
 	/* This is a crock, required because FIONREAD or equivalent is
 	 * not guaranteed to detect a broken connection.
@@ -785,10 +1172,16 @@
 
 	    dpy->conn_checker = 0;
 #ifdef USE_POLL
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+            fprintf(stderr, "_XEventsQueued: Calling poll().\n");
+#endif
 	    filedes.fd = dpy->fd;
 	    filedes.events = POLLIN;
 	    if ((result = poll(&filedes, 1, 0)))
 #else
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+            fprintf(stderr, "_XEventsQueued: Calling select().\n");
+#endif
 	    FD_ZERO(&r_mask);
 	    FD_SET(dpy->fd, &r_mask);
 	    if ((result = Select(dpy->fd + 1, &r_mask, NULL, NULL, &zero_time)))
@@ -797,13 +1190,32 @@
 		if (result > 0)
 		{
 		    if (_X11TransBytesReadable(dpy->trans_conn, &pend) < 0)
+#ifdef NX_TRANS_SOCKET
+                    {
+                        _XIOError(dpy);
+
+                        return (dpy->qlen);
+                    }
+#else
 			_XIOError(dpy);
+#endif
 		    /* we should not get zero, if we do, force a read */
 		    if (!pend)
 			pend = SIZEOF(xReply);
 		}
+#ifdef NX_TRANS_SOCKET
+                if (result <= 0) {
+                    if ((result == -1 && !ECHECK(EINTR)) ||
+                            (_NXDisplayErrorFunction != NULL &&
+                                (*_NXDisplayErrorFunction)(dpy, _XGetIOError(dpy)))) {
+                        _XIOError(dpy);
+                        return (dpy->qlen);
+                    }
+                }
+#else
 		else if (result < 0 && !ECHECK(EINTR))
 		    _XIOError(dpy);
+#endif
 	    }
 	}
 #endif /* XCONN_CHECK_FREQ */
@@ -815,6 +1227,10 @@
 	    {
 		UnlockNextEventReader(dpy);
 	    }
+
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+            fprintf(stderr, "_XEventsQueued: Returning [%d].\n", dpy->qlen);
+#endif
 	    return(dpy->qlen);
 	}
       /* Force a read if there is not enough data.  Otherwise,
@@ -847,6 +1263,11 @@
 
 	(void) _XRead (dpy, read_buf, (long) len);
 	
+#ifdef NX_TRANS_SOCKET
+        if (_XGetIOError(dpy)) {
+            return(dpy->qlen);
+        }
+#endif
 #ifdef XTHREADS
 	/* what did we actually read: reply or event? */
 	if (dpy->lock && dpy->lock->reply_awaiters) {
@@ -945,7 +1366,15 @@
 #endif /* XTHREADS */
 	    /* find out how much data can be read */
 	    if (_X11TransBytesReadable(dpy->trans_conn, &pend) < 0)
+#ifdef NX_TRANS_SOCKET
+            {
+                _XIOError(dpy);
+
+                return;
+            }
+#else
 	    	_XIOError(dpy);
+#endif
 	    len = pend;
 
 	    /* must read at least one xEvent; if none is pending, then
@@ -995,6 +1424,15 @@
 	    dpy->flags |= XlibDisplayReadEvents;
 	    i = _XRead (dpy, read_buf, (long) len);
 	    dpy->flags &= ~XlibDisplayReadEvents;
+#ifdef NX_TRANS_SOCKET
+            if (dpy->flags & XlibDisplayIOError)
+            {
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+                fprintf(stderr, "_XReadEvents: Returning with I/O error detected.\n");
+#endif
+                return;
+            }
+#endif
 	    if (i == -2) {
 		/* special flag from _XRead to say that internal connection has
 		   done XPutBackEvent.  Which we can use so we're done. */
@@ -1065,12 +1503,33 @@
 #ifdef XTHREADS
 	int original_size = size;
 #endif
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_CHANGE)
+        int congestion;
+#endif
 
 	if ((dpy->flags & XlibDisplayIOError) || size == 0)
 	    return 0;
 	ESET(0);
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_CHANGE)
+        while (1) {
+                /*
+                 * Need to check the congestion state
+                 * after the read so split the statement
+                 * in multiple blocks.
+                 */
+
+                bytes_read = _X11TransRead(dpy->trans_conn, data, (int)size);
+                if (_NXDisplayCongestionFunction != NULL &&
+                        _X11TransSocketCongestionChange(dpy->trans_conn, &congestion) == 1) {
+                    (*_NXDisplayCongestionFunction)(dpy, congestion);
+                }
+                if (bytes_read == size) {
+                    break;
+                }
+#else
 	while ((bytes_read = _X11TransRead(dpy->trans_conn, data, (int)size))
 		!= size) {
+#endif
 
 	    	if (bytes_read > 0) {
 		    size -= bytes_read;
@@ -1090,14 +1549,34 @@
 		else if (bytes_read == 0) {
 		    /* Read failed because of end of file! */
 		    ESET(EPIPE);
+#ifdef NX_TRANS_SOCKET
+                    _XIOError(dpy);
+
+                    return -1;
+#else
 		    _XIOError(dpy);
+#endif
 		    }
 
 		else  /* bytes_read is less than 0; presumably -1 */ {
 		    /* If it's a system call interrupt, it's not an error. */
+#ifdef NX_TRANS_SOCKET
+                    if (!ECHECK(EINTR) ||
+                        (_NXDisplayErrorFunction != NULL &&
+                            (*_NXDisplayErrorFunction)(dpy, _XGetIOError(dpy)))) {
+                        _XIOError(dpy);
+                        return -1;
+                    }
+#else
 		    if (!ECHECK(EINTR))
 		    	_XIOError(dpy);
+#endif
 		    }
+#ifdef NX_TRANS_SOCKET
+                if (_XGetIOError(dpy)) {
+                    return -1;
+                }
+#endif
 	    	 }
 #ifdef XTHREADS
        if (dpy->lock && dpy->lock->reply_bytes_left > 0)
@@ -1268,6 +1747,9 @@
 #ifdef XTHREADS
         int original_size;
 #endif
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_CHANGE)
+        int congestion;
+#endif
 
 	if ((dpy->flags & XlibDisplayIOError) || size == 0) return;
 	iov[0].iov_len = (int)size;
@@ -1285,7 +1767,19 @@
 	original_size = size;
 #endif
 	ESET(0);
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_CHANGE)
+        while (1) {
+            bytes_read = _X11TransReadv (dpy->trans_conn, iov, 2);
+            if (_NXDisplayCongestionFunction != NULL &&
+                    _X11TransSocketCongestionChange(dpy->trans_conn, &congestion) == 1) {
+                (*_NXDisplayCongestionFunction)(dpy, congestion);
+            }
+            if (bytes_read == size) {
+                break;
+            }
+#else
 	while ((bytes_read = _X11TransReadv (dpy->trans_conn, iov, 2)) != size) {
+#endif
 
 	    if (bytes_read > 0) {
 		size -= bytes_read;
@@ -1313,14 +1807,34 @@
 	    else if (bytes_read == 0) {
 		/* Read failed because of end of file! */
 		ESET(EPIPE);
+#ifdef NX_TRANS_SOCKET
+                _XIOError(dpy);
+
+                return;
+#else
 		_XIOError(dpy);
+#endif
 		}
 	    
 	    else  /* bytes_read is less than 0; presumably -1 */ {
 		/* If it's a system call interrupt, it's not an error. */
+#ifdef NX_TRANS_SOCKET
+		if (!ECHECK(EINTR) ||
+                        (_NXDisplayErrorFunction != NULL &&
+                            (*_NXDisplayErrorFunction)(dpy, _XGetIOError(dpy)))) {
+                    _XIOError(dpy);
+                    return;
+                }
+#else
 		if (!ECHECK(EINTR))
 		    _XIOError(dpy);
+#endif
 		}
+#ifdef NX_TRANS_SOCKET
+            if (_XGetIOError(dpy)) {
+                return;
+            }
+#endif
 	    }
 #ifdef XTHREADS
        if (dpy->lock && dpy->lock->reply_bytes_left > 0)
@@ -1351,8 +1865,31 @@
 
 	long skip, dbufsize, padsize, total, todo;
 	_XExtension *ext;
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_CHANGE)
+        int congestion;
+#endif
+
+#ifdef NX_TRANS_SOCKET
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+    fprintf(stderr, "_XSend: Sending data with [%d] bytes to write.\n",
+                (dpy->bufptr - dpy->buffer));
+#endif
+        if (!size || (dpy->flags & XlibDisplayIOError))
+        {
+            if (dpy->flags & XlibDisplayIOError)
+            {
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+                fprintf(stderr, "_XSend: Returning with I/O error detected.\n");
+#endif
+	        dpy->bufptr = dpy->buffer;
+	        dpy->last_req = (char *)&_dummy_request;
+            }
 
+	    return;
+	}
+#else
 	if (!size || (dpy->flags & XlibDisplayIOError)) return;
+#endif
 	dbufsize = dpy->bufptr - dpy->buffer;
 #ifdef XTHREADS
 	dpy->flags |= XlibDisplayWriting;
@@ -1418,6 +1955,17 @@
     
 	    ESET(0);
 	    if ((len = _X11TransWritev(dpy->trans_conn, iov, i)) >= 0) {
+#ifdef NX_TRANS_SOCKET
+                if (_NXDisplayWriteFunction != NULL) {
+                    (*_NXDisplayWriteFunction)(dpy, len);
+                }
+#ifdef NX_TRANS_CHANGE
+                if (_NXDisplayCongestionFunction != NULL &&
+                        _X11TransSocketCongestionChange(dpy->trans_conn, &congestion) == 1) {
+                    (*_NXDisplayCongestionFunction)(dpy, congestion);
+                }
+#endif
+#endif
 		skip += len;
 		total -= len;
 		todo = total;
@@ -1447,9 +1995,23 @@
 				      );
 		}
 #endif
+#ifdef NX_TRANS_SOCKET
+            } else if (!ECHECK(EINTR) ||
+                (_NXDisplayErrorFunction != NULL &&
+                    (*_NXDisplayErrorFunction)(dpy, _XGetIOError(dpy)))) {
+                _XIOError(dpy);
+                return;
+            }
+#else
 	    } else if (!ECHECK(EINTR)) {
 		_XIOError(dpy);
 	    }
+#endif
+#ifdef NX_TRANS_SOCKET
+            if (_XGetIOError(dpy)) {
+                return;
+            }
+#endif
 	}
 	dpy->last_req = (char *) & _dummy_request;
 	if ((dpy->request - dpy->last_request_read) >= SEQLIMIT &&
@@ -1640,10 +2202,31 @@
     if (newseq < lastseq) {
 	newseq += 0x10000;
 	if (newseq > dpy->request) {
+
+#ifdef NX_TRANS_SOCKET
+
+	    if (_NXLostSequenceFunction != NULL)
+            {
+                (*_NXLostSequenceFunction)(dpy, newseq, dpy->request,
+                                               (unsigned int) rep->type);
+            }
+            else
+            {
+                (void) fprintf (stderr, 
+                "Xlib: sequence lost (0x%lx > 0x%lx) in reply type 0x%x!\n",
+                                newseq, dpy->request, 
+                                (unsigned int) rep->type);
+            }
+
+#else /* #ifdef NX_TRANS_SOCKET */
+
 	    (void) fprintf (stderr, 
 	    "Xlib: sequence lost (0x%lx > 0x%lx) in reply type 0x%x!\n",
 			    newseq, dpy->request, 
 			    (unsigned int) rep->type);
+
+#endif /* #ifdef NX_TRANS_SOCKET */
+
 	    newseq -= 0x10000;
 	}
     }
@@ -1671,9 +2254,22 @@
 #ifdef XTHREADS
     struct _XCVList *cvl;
 #endif
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+    fprintf(stderr, "_XReply: Going to wait for an X reply.\n");
+#endif
 
+#ifdef NX_TRANS_SOCKET
+    if (dpy->flags & XlibDisplayIOError)
+    {
+#ifdef NX_TRANS_DEBUG
+        fprintf(stderr, "_XReply: Returning 0 with I/O error detected.\n");
+#endif
+        return 0;
+    }
+#else
     if (dpy->flags & XlibDisplayIOError)
 	return 0;
+#endif
 
 #ifdef XTHREADS
     /* create our condition variable and append to list */
@@ -1689,6 +2285,9 @@
 	   XThread_Self(), cvl);
 #endif
 
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+    fprintf(stderr, "_XReply: Going to flush the display buffer.\n");
+#endif
     _XFlushInt(dpy, cvl ? cvl->cv : NULL);
     /* if it is not our turn to read a reply off the wire,
      * wait til we're at head of list.  if there is an event waiter,
@@ -1704,6 +2303,20 @@
     _XFlush(dpy);
 #endif
 
+#ifdef NX_TRANS_SOCKET
+    /*
+     * We are going to block waiting for the remote
+     * X server. Be sure that the proxy has flushed
+     * all the data.
+     */
+
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "_XReply: Requesting a flush of the NX transport.\n");
+#endif
+
+    NXTransFlush(dpy->fd);
+#endif
+
     for (;;) {
 #ifdef XTHREADS
 	/* Did another thread's _XReadEvents get our reply by accident? */
@@ -1767,6 +2380,12 @@
 			((long) rep->generic.length) << 2);
 		dpy->flags &= ~XlibDisplayReply;
 		UnlockNextReplyReader(dpy);
+#ifdef NX_TRANS_SOCKET
+                /*
+                 * The original code has provision
+                 * for returning already.
+                 */
+#endif
 		_XIOError (dpy);
 		return (0);
 
@@ -1830,6 +2449,12 @@
 #endif
 		break;
 	    }
+#ifdef NX_TRANS_SOCKET
+            if (_XGetIOError(dpy)) {
+                UnlockNextReplyReader(dpy);
+                return 0;
+            }
+#endif
 	}
 }   
 
@@ -1849,6 +2474,14 @@
     (void) _XSetLastRequestRead(dpy, &rep->generic);
     len = SIZEOF(xReply) + (rep->generic.length << 2);
     if (len < SIZEOF(xReply)) {
+#ifdef NX_TRANS_SOCKET
+
+        /*
+         * The original code has provision
+         * for returning already.
+         */
+
+#endif
 	_XIOError (dpy);
 	buf += *lenp;
 	*lenp = 0;
@@ -1876,6 +2509,14 @@
     }
     if (len < SIZEOF(xReply))
     {
+#ifdef NX_TRANS_SOCKET
+
+        /*
+         * The original code has provision
+         * for returning already.
+         */
+
+#endif
 	_XIOError (dpy);
 	buf += *lenp;
 	*lenp = 0;
@@ -1944,6 +2585,10 @@
     struct _XConnWatchInfo *watchers;
     XPointer *wd;
 
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+    fprintf(stderr, "_XRegisterInternalConnection: Got called.\n");
+#endif
+
     new_conni = (struct _XConnectionInfo*)Xmalloc(sizeof(struct _XConnectionInfo));
     if (!new_conni)
 	return 0;
@@ -1991,6 +2636,10 @@
     struct _XConnWatchInfo *watch;
     XPointer *wd;
 
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+    fprintf(stderr, "_XUnregisterInternalConnection: Got called.\n");
+#endif
+
     for (prev = &dpy->im_fd_info; (info_list = *prev);
 	 prev = &info_list->next) {
 	if (info_list->fd == fd) {
@@ -2030,6 +2679,10 @@
     struct _XConnectionInfo *info_list;
     int *fd_list;
 
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+    fprintf(stderr, "XInternalConnectionNumbers: Got called.\n");
+#endif
+
     LockDisplay(dpy);
     count = 0;
     for (info_list=dpy->im_fd_info; info_list; info_list=info_list->next)
@@ -2088,6 +2741,10 @@
 {
     struct _XConnectionInfo *info_list;
 
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+    fprintf(stderr, "XProcessInternalConnection: Got called.\n");
+#endif
+
     LockDisplay(dpy);
     for (info_list=dpy->im_fd_info; info_list; info_list=info_list->next) {
 	if (info_list->fd == fd) {
@@ -2116,6 +2773,10 @@
     struct _XConnectionInfo *info_list;
     XPointer *wd_array;
 
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+    fprintf(stderr, "XAddConnectionWatch: Got called.\n");
+#endif
+
     LockDisplay(dpy);
 
     /* allocate new watch data */
@@ -2172,6 +2833,10 @@
     struct _XConnectionInfo *conni;
     int counter = 0;
 
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+    fprintf(stderr, "XRemoveConnectionWatch: Got called.\n");
+#endif
+
     LockDisplay(dpy);
     for (watch=dpy->conn_watchers; watch; watch=watch->next) {
 	if (watch->fn == callback  &&  watch->client_data == client_data) {
@@ -2209,6 +2874,10 @@
 #define SCRATCHSIZE 2048
     char buf[SCRATCHSIZE];
 
+#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
+    fprintf(stderr, "_XEatData: Going to eat [%ld] bytes of data from descriptor [%d].\n",
+                n, dpy->fd);
+#endif
     while (n > 0) {
 	register long bytes_read = (n > SCRATCHSIZE) ? SCRATCHSIZE : n;
 	(void) _XRead (dpy, buf, bytes_read);
@@ -2237,7 +2906,13 @@
 	    (_XQEvent *) Xmalloc((unsigned)sizeof(_XQEvent))) == NULL) {
 		/* Malloc call failed! */
 		ESET(ENOMEM);
+#ifdef NX_TRANS_SOCKET
+                _XIOError(dpy);
+
+                return;
+#else
 		_XIOError(dpy);
+#endif
 	}
 	qelt->next = NULL;
 	/* go call through display to find proper event reformatter */
@@ -2710,7 +3385,29 @@
 			QLength(dpy));
 
 	}
-	exit(1);
+#ifdef NX_TRANS_SOCKET
+        if (_NXHandleDisplayError == 1)
+        {
+#ifdef NX_TRANS_TEST
+            fprintf(stderr, "_XDefaultIOError: Going to return from the error handler.\n");
+#endif
+            return 0;
+        }
+        else
+        {
+#ifdef NX_TRANS_TEST
+            fprintf(stderr, "_XDefaultIOError: Going to exit from the program.\n");
+#endif
+#ifdef NX_TRANS_EXIT
+            NXTransExit(1);
+#else
+            exit(1);
+#endif
+        }
+#else
+        exit(1);
+#endif /* #ifdef NX_TRANS_SOCKET */
+
         return(0); /* dummy - function should never return */
 }
 
@@ -2911,7 +3608,48 @@
 	(*_XIOErrorFunction)(dpy);
     else
 	_XDefaultIOError(dpy);
+#ifdef NX_TRANS_SOCKET
+    /*
+     * Check if we are supposed to return in the case
+     * of a display failure. The client which originated
+     * the X operation will have to check the value of
+     * the XlibDisplayIOError flag and handle appropria-
+     * tely the display disconnection.
+     */
+
+    if (_NXHandleDisplayError == 0)
+    {
+#ifdef NX_TRANS_EXIT
+        NXTransExit(1);
+#else
+        exit(1);
+#endif
+    }
+
+    /*
+     * We are going to return. Reset the display
+     * buffers. Further writes will be discarded.
+     */
+
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "_XIOError: Resetting the display buffer.\n");
+#endif
+
+    dpy->bufptr = dpy->buffer;
+    dpy->last_req = (char *) &_dummy_request;
+
+#ifdef NX_TRANS_TEST
+    fprintf(stderr, "_XIOError: Resetting the display flags.\n");
+#endif
+
+    dpy->flags &= ~XlibDisplayProcConni;
+    dpy->flags &= ~XlibDisplayPrivSync;
+    dpy->flags &= ~XlibDisplayReadEvents;
+    dpy->flags &= ~XlibDisplayWriting;
+    dpy->flags &= ~XlibDisplayReply;
+#else
     exit (1);
+#endif
     return 0;
 }