aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/lib/dps/dpsXclient.c
blob: 212030ecc25c5854847923b9961db05cd6c9a944 (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
/*
 * dpsXclient.c -- Implementation of the Display PostScript Client Library.
 *
 * (c) Copyright 1988-1994 Adobe Systems Incorporated.
 * All rights reserved.
 * 
 * Permission to use, copy, modify, distribute, and sublicense this software
 * and its documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notices appear in all copies and that
 * both those copyright notices and this permission notice appear in
 * supporting documentation and that the name of Adobe Systems Incorporated
 * not be used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  No trademark license
 * to use the Adobe trademarks is hereby granted.  If the Adobe trademark
 * "Display PostScript"(tm) is used to describe this software, its
 * functionality or for any other purpose, such use shall be limited to a
 * statement that this software works in conjunction with the Display
 * PostScript system.  Proper trademark attribution to reflect Adobe's
 * ownership of the trademark shall be given whenever any such reference to
 * the Display PostScript system is made.
 * 
 * ADOBE MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THE SOFTWARE FOR
 * ANY PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
 * ADOBE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON- INFRINGEMENT OF THIRD PARTY RIGHTS.  IN NO EVENT SHALL ADOBE BE LIABLE
 * TO YOU OR ANY OTHER PARTY FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE, STRICT LIABILITY OR ANY OTHER ACTION ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  ADOBE WILL NOT
 * PROVIDE ANY TRAINING OR OTHER SUPPORT FOR THE SOFTWARE.
 * 
 * Adobe, PostScript, and Display PostScript are trademarks of Adobe Systems
 * Incorporated which may be registered in certain jurisdictions
 * 
 * Author:  Adobe Systems Incorporated
 */
/* $XFree86$ */

#include <stdlib.h>
#include <unistd.h>	/* sleep() */
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#ifdef VMS
/* Xlib does not like UNIX defined to any value under VMS. */
#undef UNIX
#include <decw$include/X.h>
#include <decw$include/Xlib.h>

#else /* VMS */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#endif /* VMS */

#include "DPS/XDPSlib.h"
#include "DPS/XDPS.h"

#include "publictypes.h"
#include "DPS/dpsclient.h"
#include "dpsprivate.h"

#include "dpsXpriv.h"
#include "DPS/dpsXclient.h"

#include "dpsdict.h"
#include "DPS/dpsexcept.h"

#include "dpsXint.h"

static DPSPrivContext FindPrivContext (
  Display  *	dpy,
  long int	cid)
{
  DPSPrivSpace ss;
  DPSPrivContext cc;

  for (ss = spaces;  ss != NIL;  ss = ss->next)
    for (cc = ss->firstContext;  cc != NIL;  cc = cc->next)
      if (cc->cid == cid && ((XDPSPrivContext) cc->wh)->dpy == dpy)
	return (cc);
  return (NIL);
}

DPSContext XDPSFindContext (
  Display  *	dpy,
  int		cid)
{
  return ((DPSContext) FindPrivContext (dpy, cid));
}

DPSContext DPSContextFromContextID(
  DPSContext ctxt,
  int contextID,
  DPSTextProc textProc,
  DPSErrorProc errorProc)
{
  DPSPrivSpace ss;
  Display *dpy = ((XDPSPrivContext) ((DPSPrivContext) ctxt)->wh)->dpy;
  DPSPrivContext c, cc = FindPrivContext (dpy, contextID);
  DPSPrivContext oldc = (DPSPrivContext)ctxt;
  
  if (cc != NIL) return (DPSContext)cc;

  c = (DPSPrivContext)DPScalloc(sizeof(DPSPrivContextRec), 1);
  if (!c) return NIL;
  *c = *oldc;
  ss = (DPSPrivSpace)c->space;
  
  if (textProc) c->textProc = textProc;
  if (errorProc) c->errorProc = errorProc;

  c->eofReceived = false;
  c->cid = contextID;
  c->buf = c->outBuf = c->objBuf = NIL;
  c->chainParent = c->chainChild = NIL;
  
  c->nBufChars = c->nOutBufChars = c->nObjBufChars = 0;

  c->next = ss->firstContext;
  DPSAssert(c->next != c);
  ss->firstContext = c;

  /* Note: there's no way to determine whether the new context id was obtained
  ** as a result of a fork operation or from another application.  so, it must
  ** be assumed that the application is the creator of the new context.
  ** Otherwise, it would have called the XDPSContextFromSharedID.
  */
  c->creator = true;
  c->zombie = false;
  c->numstringOffsets = NULL;

  DPSIncludePrivContext(
    (XDPSPrivContext) c->wh, (DPSContext)c, c->cid, ss->sid, DPSclientPrintProc);

  return (DPSContext)c;
}

boolean DPSPrivateCheckWait(
    DPSContext ctxt)
{
    DPSPrivContext cc = (DPSPrivContext) ctxt;

    if (!cc->creator || cc->zombie) {
	DPSSafeSetLastNameIndex(ctxt);
	if (cc->errorProc != NIL) {
	    (*cc->errorProc) (ctxt, cc->zombie ? dps_err_deadContext :
			                         dps_err_invalidAccess,
			      (unsigned long) ctxt, 0);
	}
	return true;
    }
    return false;
}

static void procFlushContext(
  DPSContext ctxt)
{
  DPSPrivContext c = (DPSPrivContext) ctxt;
  XDPSLFlush (((XDPSPrivContext) c->wh)->dpy);
  if (ctxt->chainChild != NIL) DPSFlushContext(ctxt->chainChild);
}

/* ARGSUSED */
static Bool FindDPSEvent(
  Display *dpy,
  XEvent *event,
  char *arg)
{
  return XDPSIsDPSEvent(event);
}

static void procAwaitReturnValues(DPSContext ctxt)
{
  DPSPrivContext c = (DPSPrivContext)ctxt;
 
  XDPSPrivContext xwh = (XDPSPrivContext) c->wh;
  XEvent ev;

  /* Output currently goes only to creator! */
  if (!c->creator)
    {
    DPSSafeSetLastNameIndex(ctxt);
    c->resultTable = NIL;
    c->resultTableLength = 0;
    if (c->errorProc != NIL)
      (*c->errorProc) (ctxt, dps_err_invalidAccess, 0, 0);
    return;
    }
  if (c->resultTable != NIL)
    {
    DPSCheckInitClientGlobals();

    if (XDPSLGetWrapWaitingFlag(xwh->dpy)) {
	DPSSafeSetLastNameIndex(ctxt);
	c->resultTable = NIL;
	c->resultTableLength = 0;
	if (c->errorProc != NIL)
		(*c->errorProc) (ctxt, dps_err_recursiveWait,
				 (unsigned long) xwh->dpy, 0);
	return;
    }
    XDPSLSetWrapWaitingFlag(xwh->dpy, True);

  DURING
    DPSFlushContext(ctxt);
    while (c->resultTable != NIL)
      {
      /* We may block indefinitely if the context is frozen or it
	 somehow needs more input. */
      if (c->zombie)
	{
	DPSSafeSetLastNameIndex(ctxt);
	c->resultTable = NIL;
	c->resultTableLength = 0;
	if (c->errorProc != NIL)
	  (*c->errorProc) (ctxt, dps_err_deadContext, (unsigned long) c, 0);
	XDPSLSetWrapWaitingFlag(xwh->dpy, False);
	E_RTRN_VOID;
	}
      
      /* Someone could conceivably change the event delivery mode in the
	 middle of this...best to check every time */

      if (XDPSLGetPassEventsFlag(xwh->dpy)) {
	  XIfEvent(xwh->dpy, &ev, FindDPSEvent, (char *) NULL);
	  if (!XDPSDispatchEvent(&ev)) DPSCantHappen();
      } else DPSSendPostScript((XDPSPrivContext) c->wh, DPSclientPrintProc,
			       c->cid, NIL, 0, NIL);
    }
  HANDLER
    XDPSLSetWrapWaitingFlag(xwh->dpy, False);
    RERAISE;
  END_HANDLER

  XDPSLSetWrapWaitingFlag(xwh->dpy, False);

  }

 /* update space's name map.
       space->lastNameIndex is the highest index known to be known to the
	 server for this space.
       c->lastNameIndex is the highest index sent so far to the context
 */

  if (((DPSPrivSpace)(c->space))->lastNameIndex < c->lastNameIndex)
    ((DPSPrivSpace)(c->space))->lastNameIndex = c->lastNameIndex;
}

void DPSinnerProcWriteData(
  DPSContext ctxt,
  char *buf,
  unsigned int count)
{
  DPSPrivContext c = (DPSPrivContext) ctxt;
  
  /* ASSERT: safe to call with chain */

  /* No local buffering */
  DPSSendPostScript ((XDPSPrivContext) c->wh, DPSclientPrintProc,
		     c->cid, buf, count, NIL);
} /* DPSinnerProcWriteData */

static void procResetContext(DPSContext ctxt)
{
  DPSPrivContext c = (DPSPrivContext) ctxt;
  int currStatus;
  register XDPSPrivContext xwh = (XDPSPrivContext) c->wh;
  int retries = 0;
  int backoff = 2;
  
  /* First make sure context isn't frozen, try to unfreeze. */

#define DPS_SLEEP_SECS 2

  while((currStatus = XDPSLGetStatus(xwh->dpy, xwh->cxid)) == PSFROZEN)
    {
    XDPSLNotifyContext(xwh->dpy, xwh->cxid, PSUNFREEZE);
    sleep(DPS_SLEEP_SECS);
      /* Okay if context is PSRUNNING, since the EOF will
         be handled at the next PSNEEDSINPUT */
    }

  /* Remove events from Xlib Qs before sending the reset request. */
  XDPSForceEvents (xwh->dpy);
  
  if (currStatus == PSSTATUSERROR)
    /* +++ report error? */;
  else  /* Didn't become zombie. */
    {
    currStatus = 0;
    XDPSLReset(xwh->dpy, xwh->cxid);
    XDPSLFlush(xwh->dpy);
    /* Be optmistic for the first try. Assume the app set up a status mask
       correctly, we should get a status event without asking the
       server for status. */
    
    XDPSForceEvents(xwh->dpy);
    currStatus = c->statusFromEvent;

    while (currStatus != PSNEEDSINPUT && currStatus != PSZOMBIE)
      {
      if (currStatus == PSFROZEN)
          XDPSLNotifyContext(xwh->dpy, xwh->cxid, PSUNFREEZE);
      if (retries > backoff)
        {
        /* Optimism failed.  App probably didn't set up a status mask.
           Ask the server for status. */
        currStatus = XDPSLGetStatus(xwh->dpy, xwh->cxid);
        retries = 0;
        backoff = (backoff > 30) ? 2 : backoff + 1;
        continue;
        }
      else
        ++retries;
      sleep(DPS_SLEEP_SECS);
      XDPSForceEvents(xwh->dpy);
      currStatus = c->statusFromEvent;
      }
    }
  
  c->eofReceived = false;
}

void DPSPrivateDestroyContext(DPSContext ctxt)
{
  DPSPrivContext c = (DPSPrivContext)ctxt;
  DPSPrivSpace s = (DPSPrivSpace) c->space;

  if (c->creator)
    DPSSendTerminate((XDPSPrivContext) c->wh, c->cid, DPSclientPrintProc);
  else 
    XDPSSetStatusMask(ctxt, 0, XDPSL_ALL_EVENTS, 0);  /* Stop status events */
  /* Don't free the space's wh out from under it */
  if (c->wh != s->wh) free(c->wh);
}

void DPSPrivateDestroySpace(DPSSpace space)
{
    DPSPrivSpace ss = (DPSPrivSpace) space;

    if (ss->creator) DPSSendDestroySpace((XDPSPrivContext) ss->wh, ss->sid,
					 DPSclientPrintProc);

    free (ss->wh);
}

boolean DPSCheckShared(DPSPrivContext ctxt)
{
  return ctxt->creator == false && ctxt->resultTable != NIL;
  /* let procAwaitReturnValues generate error */
}

/* ARGSUSED */
void DPSServicePostScript(boolean (*returnControl)(void))
{
} /* DPSServicePostScript */

void DPSHandleBogusError(DPSContext ctxt, char *prefix, char *suffix)
{
    char *buf = "bogus error output from context";
    DPSDefaultPrivateHandler(ctxt, dps_err_warning,
			     (long unsigned int)buf, 0, prefix, suffix);
}

void DPSDefaultPrivateHandler(
  DPSContext ctxt,
  DPSErrorCode errorCode,
  long unsigned int arg1,
  long unsigned int arg2,
  char *prefix,
  char *suffix)
{

  DPSTextProc textProc = DPSGetCurrentTextBackstop();

  switch (errorCode) {
    case dps_err_invalidAccess:
      if (textProc != NIL)
	{
	char m[100];
	(void) sprintf (m, "%sInvalid context access.%s", prefix, suffix);
	(*textProc) (ctxt, m, strlen (m));
	}
	break;
    case dps_err_encodingCheck:
      if (textProc != NIL)
	{
	char m[100];
	(void) sprintf (m, "%sInvalid name/program encoding: %d/%d.%s",
			prefix, (int) arg1, (int) arg2, suffix);
	(*textProc) (ctxt, m, strlen (m));
	}
	break;
    case dps_err_closedDisplay:
      if (textProc != NIL)
	{
	char m[100];
	(void) sprintf (m, "%sBroken display connection %d.%s",
			prefix, (int) arg1, suffix);
	(*textProc) (ctxt, m, strlen (m));
	}
	break;
    case dps_err_deadContext:
      if (textProc != NIL)
	{
	char m[100];
	(void) sprintf (m, "%sDead context 0x0%x.%s", prefix,
			(int) arg1, suffix);
	(*textProc) (ctxt, m, strlen (m));
	}
	break;
    case dps_err_warning:
      if (textProc != NIL)
        {
        char *warn = (char *)arg1;
        char *msg = "%% DPS Client Library Warning:\n   ";
        (*textProc)(ctxt, msg, strlen(msg));
        (*textProc)(ctxt, warn, strlen(warn));
        msg = "\n";
        (*textProc)(ctxt, msg, strlen(msg));
        /* flush convention */
        (*textProc)(ctxt, msg, 0);
        }
        break;
    case dps_err_fatal:
      if (textProc != NIL)
        {
        char *fatal = (char *)arg1;
        char *msg = "%% DPS Client Library Fatal Internal Error:\n   ";
        (*textProc)(ctxt, msg, strlen(msg));
        (*textProc)(ctxt, fatal, strlen(fatal));
        msg = ".\nAborting ...\n";
        (*textProc)(ctxt, msg, strlen(msg));
        /* flush convention */
        (*textProc)(ctxt, msg, 0);
        abort();
        }
        break;
    case dps_err_recursiveWait:
      if (textProc != NIL)
	{
	char m[100];
	(void) sprintf (m,
			"%sRecursive wait for return values, display 0x%x.%s",
			prefix, (int) arg1, suffix);
	(*textProc) (ctxt, m, strlen (m));
	}
	break;
  }
}

void DPSInitPrivateSpaceFields(DPSPrivSpace s)
{
    s->creator = true;
}

void DPSInitPrivateContextFields(DPSPrivContext c, DPSPrivSpace s)
{
    c->creator = true;
    c->zombie = false;
    if (!s->creator) {
	c->procs = XDPSconvProcs;
	c->nameEncoding = dps_strings;
    }
}

void  DPSInitPrivateTextContextFields(DPSPrivContext c, DPSPrivSpace s)
{
    c->creator = true;
    c->zombie = false;
    c->space = (DPSSpace) s;
    c->next = s->firstContext;
    s->firstContext = c;
}
  
long int DPSLastUserObjectIndex = 0;

long int DPSNewUserObjectIndex (void)
{
  return (DPSLastUserObjectIndex++);
}

void XDPSSetProcs (void)
{
  DPSCheckInitClientGlobals ();
  if (!textCtxProcs)
    {
    textCtxProcs = (DPSProcs) DPScalloc (sizeof (DPSProcsRec), 1);
    DPSInitCommonTextContextProcs(textCtxProcs);
    DPSInitSysNames();
    }
  if (!ctxProcs)
    {
    ctxProcs = (DPSProcs) DPScalloc (sizeof (DPSProcsRec), 1);
    DPSInitCommonContextProcs(ctxProcs);
    DPSInitPrivateContextProcs(ctxProcs);
    }
  if (!XDPSconvProcs)
    XDPSconvProcs = (DPSProcs) DPScalloc (sizeof (DPSProcsRec), 1);
  if (!XDPSrawProcs)
    XDPSrawProcs = ctxProcs;
  *XDPSconvProcs = *ctxProcs;
  XDPSconvProcs->BinObjSeqWrite = textCtxProcs->BinObjSeqWrite;
  XDPSconvProcs->WriteStringChars = textCtxProcs->WriteStringChars;
  XDPSconvProcs->WritePostScript = textCtxProcs->WritePostScript;
  XDPSconvProcs->WriteNumString = textCtxProcs->WriteNumString;
}

void DPSInitPrivateContextProcs(DPSProcs p)
{
    p->FlushContext = procFlushContext;
    p->ResetContext = procResetContext;
    p->AwaitReturnValues = procAwaitReturnValues;
}

DPSContext XDPSCreateSimpleContext (
  Display	*dpy,
  Drawable	draw,
  GC		gc,
  int		x,
  int		y,
  DPSTextProc	textProc,
  DPSErrorProc	errorProc,
  DPSSpace	space)
{
  XDPSPrivContext xwh = XDPSCreatePrivContextRec (dpy, draw, gc, x, y,
  						  0, DefaultStdCMap,
						  DefaultStdCMap, 0, false);
  DPSContext newCtxt;
  
  if (xwh == NIL)
    return (NIL);
  else
    {
    newCtxt = DPSCreateContext ((char *) xwh, textProc, errorProc, space);
    if (newCtxt == NIL)
      free ((char *) xwh);
    return (newCtxt);
    }
}


DPSContext XDPSCreateContext (
  Display		*dpy,
  Drawable		draw,
  GC			gc,
  int			x,
  int			y,
  unsigned int		eventmask,
  XStandardColormap	*grayramp,
  XStandardColormap	*ccube,
  int			actual,
  DPSTextProc		textProc,
  DPSErrorProc		errorProc,
  DPSSpace		space)
{
  XDPSPrivContext xwh = XDPSCreatePrivContextRec (dpy, draw, gc, x, y,
  						  eventmask, grayramp,
						  ccube, actual, false);
  DPSContext newCtxt;
  
  if (xwh == NIL)
    return (NIL);
  else
    {
    newCtxt = DPSCreateContext ((char *) xwh, textProc, errorProc, space);
    if (newCtxt == NIL)
      free ((char *) xwh);
    return (newCtxt);
    }
}

DPSContext XDPSCreateSecureContext (
  Display		*dpy,
  Drawable		draw,
  GC			gc,
  int			x,
  int			y,
  unsigned int		eventmask,
  XStandardColormap	*grayramp,
  XStandardColormap	*ccube,
  int			actual,
  DPSTextProc		textProc,
  DPSErrorProc		errorProc,
  DPSSpace		space)
{
  XDPSPrivContext xwh = XDPSCreatePrivContextRec (dpy, draw, gc, x, y,
  						  eventmask, grayramp,
						  ccube, actual, true);
  DPSContext newCtxt;
  
  if (xwh == NIL)
    return (NIL);
  else
    {
    newCtxt = DPSCreateContext ((char *) xwh, textProc, errorProc, space);
    if (newCtxt == NIL)
      free ((char *) xwh);
    return (newCtxt);
    }
}


DPSContext XDPSContextFromSharedID (dpy, cid, textProc, errorProc)
  Display	  *dpy;
  ContextPSID	  cid;
  DPSTextProc	  textProc;
  DPSErrorProc	  errorProc;
{
  DPSPrivContext	c;
  DPSPrivSpace		s; 
  ContextXID		cxid;
  SpaceXID		sxid;
  XDPSPrivContext	xwh;

  if (DPSInitialize () != 0)
    return (NIL);

  c = FindPrivContext (dpy, cid);
  if (c != NIL)
    return ((DPSContext) c);

  xwh = XDPSCreatePrivContextRec (dpy, 0, 0, 0, 0, 0, NIL, NIL, 0, false);
  if (xwh == NIL)
    return (NIL);
  else if (XDPSLIDFromContext (dpy, cid, &cxid, &sxid) != 1)
    {
    free ((char *) xwh);
    return (NIL);
    }
  xwh->cxid = cxid;

  if (spaceProcs == NIL)
    {
    spaceProcs = (DPSSpaceProcs) DPScalloc (sizeof (DPSSpaceProcsRec), 1);
    DPSInitCommonSpaceProcs(spaceProcs);
    }

  s = spaces;
  while (s != NIL)
    if ((SpaceXID)s->sid == sxid && ((XDPSPrivContext) s->wh)->dpy == dpy)
      break;
    else
      s = s->next;

  if (s == NIL)	  /* Create new space record. */
    {
    s = (DPSPrivSpace) DPScalloc (sizeof (DPSPrivSpaceRec), 1);
    s->procs = spaceProcs;
    s->lastNameIndex = -1;
    s->sid = sxid;
    s->wh = (char *) xwh;
    s->creator = false;
    s->next = spaces;
    spaces = s;
    }

  c = (DPSPrivContext) DPScalloc (sizeof (DPSPrivContextRec), 1);
  c->space = (DPSSpace) s;
  c->procs = XDPSconvProcs;
  c->textProc = textProc;
  c->errorProc = errorProc;
  c->programEncoding = DPSDefaultProgramEncoding;
  c->nameEncoding = dps_strings;
  c->next = s->firstContext;
  s->firstContext = c;
  c->lastNameIndex = s->lastNameIndex;
  c->cid = cid;
  c->numstringOffsets = NULL;
  c->creator = false;
  c->zombie = false;
  c->numFormat = XDPSNumFormat (dpy);
  c->wh = (char *) xwh;
  
  xwh->ctxt = (DPSContext) c;

  return ((DPSContext) c);
}


void DPSChangeEncoding (ctxt, newProgEncoding, newNameEncoding)
  DPSContext	     ctxt;
  DPSProgramEncoding newProgEncoding;
  DPSNameEncoding    newNameEncoding;
{
  if (ctxt->programEncoding != newProgEncoding ||
      ctxt->nameEncoding != newNameEncoding)
    {
      DPSPrivContext cc = (DPSPrivContext) ctxt;
      DPSPrivSpace   ss = (DPSPrivSpace) (cc->space);
      
      if ((!cc->creator || !ss->creator) && newNameEncoding != dps_strings)
	{
	DPSSafeSetLastNameIndex(ctxt);
	if (cc->errorProc != NIL)
	  (*cc->errorProc) (ctxt, dps_err_encodingCheck,
			    (unsigned long) newNameEncoding,
			    (unsigned long) newProgEncoding);
	return;
	}
      if (ctxt->procs == textCtxProcs)
        {
        ctxt->programEncoding = newProgEncoding;
        ctxt->nameEncoding = newNameEncoding;
        }
      else
        XDPSSetContextEncoding (ctxt, newProgEncoding, newNameEncoding);
    }
}


DPSSpace XDPSSpaceFromSharedID (dpy, sid)
  Display	*dpy;
  SpaceXID	sid;
{
  DPSPrivSpace		s;
  XDPSPrivContext	xwh;

  if (DPSInitialize () != 0)
    return (NIL);

  if (spaceProcs == NIL)
    {
    spaceProcs = (DPSSpaceProcs) DPScalloc (sizeof (DPSSpaceProcsRec), 1);
    DPSInitCommonSpaceProcs(spaceProcs);
    }

  s = spaces;
  while (s != NIL)
    if ((SpaceXID)s->sid == sid && ((XDPSPrivContext) s->wh)->dpy == dpy)
      break;
    else
      s = s->next;

  if (s == NIL)	  /* Create new space record. */
    {
    xwh = XDPSCreatePrivContextRec (dpy, 0, 0, 0, 0, 0, NIL, NIL, 0, false);
    if (xwh == NIL)
      return (NIL);

    s = (DPSPrivSpace) DPScalloc (sizeof (DPSPrivSpaceRec), 1);
    s->procs = spaceProcs;
    s->lastNameIndex = -1;
    s->sid = sid;
    s->wh = (char *) xwh;
    s->creator = false;
    s->next = spaces;
    spaces = s;
    }

  return ((DPSSpace) s);
}


void XDPSUnfreezeContext (ctxt)
  DPSContext ctxt;
{
  XDPSPrivContext wh = (XDPSPrivContext) (((DPSPrivContext) ctxt)->wh);
  
  if (wh != NIL && wh->cxid != 0)
    XDPSSendUnfreeze (wh->dpy, wh->cxid);
}


ContextXID XDPSXIDFromContext (Pdpy, ctxt)
  Display    **Pdpy;
  DPSContext ctxt;
{
  XDPSPrivContext  xwh = (XDPSPrivContext) (((DPSPrivContext) ctxt)->wh);
  
  if (xwh == NIL || xwh->cxid == 0)
    {
    *Pdpy = NULL;
    return (0);
    }
  else
    {
    *Pdpy = xwh->dpy;
    return (xwh->cxid);
    }
}


SpaceXID XDPSXIDFromSpace (Pdpy, space)
  Display    **Pdpy;
  DPSSpace   space;
{
  DPSPrivSpace	   ss = (DPSPrivSpace) space;
  XDPSPrivContext  xwh = (XDPSPrivContext) ss->wh;
  
  if (xwh != NIL && xwh->dpy != NULL)
    {
    *Pdpy = xwh->dpy;
    return (ss->sid);
    }
  else
    {
    *Pdpy = NULL;
    return (0);
    }
}


DPSContext XDPSContextFromXID (dpy, cxid)
  Display    *dpy;
  ContextXID cxid;
{
  DPSPrivContext c;
  DPSPrivSpace	 ss;
  
  for (ss = spaces;  ss != NIL;  ss = ss->next)
    if (((XDPSPrivContext) ss->wh)->dpy == dpy)
      for (c = ss->firstContext;  c != NIL;  c = c->next)
	if (((XDPSPrivContext) c->wh)->cxid == cxid)
	  return ((DPSContext) c);

  return (NIL);
}


DPSSpace XDPSSpaceFromXID (dpy, sxid)
  Display    *dpy;
  SpaceXID   sxid;
{
  DPSPrivSpace  ss;
  
  for (ss = spaces;  ss != NIL;  ss = ss->next)
    if ((SpaceXID)ss->sid == sxid && ((XDPSPrivContext) ss->wh)->dpy == dpy)
      return ((DPSSpace) ss);

  return (NIL);
}


XDPSStatusProc XDPSRegisterStatusProc (ctxt, statusProc)
  DPSContext	 ctxt;
  XDPSStatusProc statusProc;
{
  DPSPrivContext c = (DPSPrivContext) ctxt;
  XDPSStatusProc old = c->statusProc;
  
  if (c->wh != NIL) c->statusProc = statusProc;
  return old;
}


XDPSReadyProc XDPSRegisterReadyProc (ctxt, readyProc)
  DPSContext	 ctxt;
  XDPSReadyProc readyProc;
{
  DPSPrivContext c = (DPSPrivContext) ctxt;
  XDPSReadyProc old = c->readyProc;
  
  if (c->wh != NIL) c->readyProc = readyProc;
  return old;
}


void XDPSSetStatusMask(ctxt, enableMask, disableMask, nextMask)
  DPSContext ctxt;
  unsigned long enableMask, disableMask, nextMask;
{
  XDPSPrivContext xwh = (XDPSPrivContext) (((DPSPrivContext) ctxt)->wh);
  
  if (xwh != NIL && xwh->cxid != 0)
    XDPSLSetStatusMask(xwh->dpy, xwh->cxid, enableMask, disableMask, nextMask);
}


int XDPSGetContextStatus(ctxt)
  DPSContext ctxt;
{
  DPSPrivContext  c = (DPSPrivContext) ctxt;
  XDPSPrivContext xwh = (XDPSPrivContext) c->wh;
  
  if (xwh != NIL && xwh->cxid != 0)
    return (XDPSLGetStatus(xwh->dpy, xwh->cxid));
  else
    return (0);
}

void XDPSNotifyWhenReady(ctxt, i0, i1, i2, i3)
    DPSContext ctxt;
    int i0, i1, i2, i3;
{
    DPSPrivContext  c = (DPSPrivContext) ctxt;
    XDPSPrivContext xwh = (XDPSPrivContext) c->wh;
    int i[4];

    i[0] = i0;
    i[1] = i1;
    i[2] = i2;
    i[3] = i3;
    
    XDPSLNotifyWhenReady(xwh->dpy, xwh->cxid, i);
}

void XDPSStatusEventHandler (e)
  XDPSLStatusEvent *e;
{
  DPSPrivContext  c = (DPSPrivContext) XDPSContextFromXID(e->display, e->cxid);
  
  if (c == NIL)
    return;
  
  c->statusFromEvent = e->status;
  if (e->status == PSZOMBIE)
    {
    c->zombie = true;
    if (c->resultTable != NIL)  /* Currently waiting for results */
      XDPSQuitBlocking = true;
    }
  
  if (c->statusProc != NIL)
    (*(c->statusProc)) ((DPSContext) c, e->status);
}

void XDPSReadyEventHandler (e)
  XDPSLReadyEvent *e;
{
  DPSPrivContext  c = (DPSPrivContext) XDPSContextFromXID(e->display, e->cxid);
  
  if (c == NIL)
    return;
  
  if (c->readyProc != NIL)
    (*(c->readyProc)) ((DPSContext) c, e->val);
}



void DPSWarnProc(
    DPSContext ctxt,
    char *msg)
{
    DPSErrorProc ep;

    if (DPSInitialize() != 0) return;
    ep = DPSGetCurrentErrorBackstop();
    if (ep == NULL) ep = DPSDefaultErrorProc;
    (*ep)(ctxt, dps_err_warning, (long unsigned int)msg, 0);
}

void DPSFatalProc(
    DPSContext ctxt,
    char *msg)
{
    DPSErrorProc ep;

    if (DPSInitialize() != 0) return;
    ep = DPSGetCurrentErrorBackstop();
    if (ep == NULL) ep = DPSDefaultErrorProc;
    (*ep)(ctxt, dps_err_fatal, (long unsigned int)msg, 0);
}

void DPSCantHappen(void)
{
  static int locked = 0;
  char *msg = "assertion failure or DPSCantHappen";
  if (locked > 0) abort();
  ++locked;
  DPSFatalProc((DPSContext)NULL, msg);
  /* Fatal proc shouldn't return, but client can override and do anything. */
  --locked;
}


/* Procedures for delayed event dispatching */

DPSEventDelivery XDPSSetEventDelivery(
    Display *dpy,
    DPSEventDelivery newMode)
{
    Bool old = XDPSLGetPassEventsFlag(dpy);

    switch (newMode) {
	case dps_event_pass_through:
	    XDPSLSetPassEventsFlag(dpy, True);
	    break;
	case dps_event_internal_dispatch:
	    XDPSLSetPassEventsFlag(dpy, False);
	    break;
	default:
	    break;
    }

    if (old) return dps_event_pass_through;
    else return dps_event_internal_dispatch;
}

Bool XDPSIsStatusEvent(
    XEvent *event,
    DPSContext *ctxt,
    int *status)
{
    Display *d = event->xany.display;
    XExtCodes *c = XDPSLGetCodes(d);
    XDPSLStatusEvent *se = (XDPSLStatusEvent *) event;

    if (c == NULL) return False;	/* Not inited on that display;
					   must be False */

    if (!c->first_event)		/* Check CSDPS first */
        {
	if (XDPSLGetCSDPSFakeEventType(d, event) == csdps_status)
	    {				/* Check CSDPS first */
	    XDPSLGetCSDPSStatus(d, event, (void **)ctxt, status);
	    return True;
	    }
	else
	    return False;
	}

    if (event->type != c->first_event + PSEVENTSTATUS) return False;

    if (ctxt != NULL) *ctxt = XDPSContextFromXID(d, se->cxid);
    if (status != NULL) *status = se->status;
    return True;
}

Bool XDPSIsOutputEvent(
    XEvent *event)
{
    Display *d = event->xany.display;
    XExtCodes *c = XDPSLGetCodes(d);
    CSDPSFakeEventTypes t;

    if (c == NULL) return False;	/* Not inited on that display;
					   must be False */

    if (!c->first_event)		/* Check CSDPS first */
        {
	if ((t = XDPSLGetCSDPSFakeEventType(d, event)) == csdps_output
	  || t == csdps_output_with_len)
	    return True;
	else
	    return False;
        }

    return event->type == c->first_event + PSEVENTOUTPUT;
}

Bool XDPSIsDPSEvent(
    XEvent *event)
{
    Display *d = event->xany.display;
    XExtCodes *c = XDPSLGetCodes(d);

    if (c == NULL) return False;	/* Not inited on that display;
					   must be False */

    if (!c->first_event)		/* Check CSDPS first */
        {
	if (XDPSLGetCSDPSFakeEventType(d, event) != csdps_not)
	    return True;
	else
	    return False;
	}

    return event->type >= c->first_event &&
	   event->type <  c->first_event + NPSEVENTS;
}

Bool XDPSDispatchEvent(
    XEvent *event)
{
    Display *d = event->xany.display;
    XExtCodes *c = XDPSLGetCodes(d);
    CSDPSFakeEventTypes t;

    if (c == NULL) return False;	/* Not inited on that display;
					   must be False */

    if (!c->first_event)		/* Check CSDPS first */
        {
	if ((t = XDPSLGetCSDPSFakeEventType(d, event)) != csdps_not) 
	    return(XDPSLDispatchCSDPSFakeEvent(d, event, t));
	else
	    return False;
        }

    if (event->type == c->first_event + PSEVENTSTATUS) {
	XDPSLCallStatusEventHandler(d, event);
    } else if (event->type == c->first_event + PSEVENTOUTPUT) {
	XDPSLCallOutputEventHandler(d, event);
    } else if (event->type == c->first_event + PSEVENTREADY) {
	XDPSLCallReadyEventHandler(d, event);
    } else return False;
    return True;
}

/* L2-DPS/PROTO 9 addition */
Bool XDPSIsReadyEvent(
    XEvent *event,
    DPSContext *ctxt,
    int *val)
{
    Display *d = event->xany.display;
    XExtCodes *c = XDPSLGetCodes(d);
    XDPSLReadyEvent *re = (XDPSLReadyEvent *) event;

    if (c == NULL) return False;        /* Not inited on that display;
                                           must be False */

    if (!c->first_event)		/* Check CSDPS first */
        {
	if (XDPSLGetCSDPSFakeEventType(d, event) == csdps_ready)
	    {
	    XDPSLGetCSDPSReady(d, event, (void **)ctxt, val);
	    return True;
	    }
	else
	    return False;
	}

    if (event->type != c->first_event + PSEVENTREADY) return False;

    if (ctxt != NULL) *ctxt = XDPSContextFromXID(d, re->cxid);
    if (val != NULL) {
        val[0] = re->val[0];
        val[1] = re->val[1];
        val[2] = re->val[2];
        val[4] = re->val[3];
    }
    return True;
}

int XDPSGetProtocolVersion(
    Display *dpy)
{
    return XDPSLGetVersion(dpy);
}