aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/lib/dps/cslibext.c
blob: 831f0ad65b31e831cf417903544063d3019be356 (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
/*
 *  cslibext.c -- DPSCAP client Xlib extension hookup
 *
 * (c) Copyright 1991-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: xc/lib/dps/cslibext.c,v 1.4tsi Exp $ */

#include <stdio.h>
#include <stdlib.h>

#include <sys/param.h>		/* for MAXHOSTNAMELEN */
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xlibint.h>
#include "cslibint.h"
#include <DPS/XDPS.h>
#include <DPS/XDPSproto.h>
#include <DPS/dpsXclient.h>
#include <DPS/dpsNXargs.h>
#include "DPSCAPClient.h"
#include "dpsassert.h"
#include <DPS/XDPSlib.h>

#include "publictypes.h"
#include "dpsXpriv.h"

/* === DEFINES === */

#define DPSNXSYNCGCMODE_FLUSH	0
#define DPSNXSYNCGCMODE_SYNC	1
#define DPSNXSYNCGCMODE_DELAYED	2
#define DPSNXSYNCGCMODE_DEFAULT	DPSNXSYNCGCMODE_DELAYED

/* === GLOBALS === */

DPSCAPGlobals gCSDPS = NULL;

#ifdef DPSLNKL
#define ANXCYAN
#define ANXMAGENTA
#define ANXYELLOW
#define ANXBLACK
#define ANXSPOT
#include "dpslnkl.inc"
#endif /* DPSLNKL */

int gNXSyncGCMode = DPSNXSYNCGCMODE_DEFAULT;

/* === PUBLIC PROCS === */

#ifdef MAHALO
static int
DPSCAPFlushAfterProc(Display *agent)
{
    LockDisplay(agent);
    N_XFlush(agent);
    UnlockDisplay(agent);
}
#endif

int
CSDPSInit(
    Display *dpy,
    int *numberType,		/* RETURN */
    char **floatingName)	/* RETURN */
{
    register Display *agent;
    register xCAPConnReplyPrefix *p;
    register char *c;
    DPSCAPData my;
    xCAPConnSetupReq setup;
    union {
        xCAPConnSuccess good;
        xCAPConnFailed bad;
    } reply;
    XExtData *extData;
    XExtCodes *codes;
    int indian;
    int rest;
    Window clientWindow;
    char fullDisplayName[MAXHOSTNAMELEN+10];

    if (gCSDPS == NULL)
        DPSCAPStartUp();

    /* To fix dps-nx #68, Motif too slow on HP */
    if ((c = getenv("DPSNXGCMODE")) != 0)
        {
	gNXSyncGCMode = atoi(c);
	if (gNXSyncGCMode < DPSNXSYNCGCMODE_FLUSH
	  || gNXSyncGCMode > DPSNXSYNCGCMODE_DELAYED)
	    gNXSyncGCMode = DPSNXSYNCGCMODE_DEFAULT;
	}

    /* We may have already called this routine via XDPSExtensionPresent,
       so don't do it again! */

    if ((codes = XDPSLGetCodes(dpy))
        && (agent = XDPSLGetShunt(dpy))
        && agent != dpy
	&& codes->major_opcode == DPSXOPCODEBASE)
        return(DPSCAPSUCCESS);

    /* Try to create a window for ClientMessage communication */

    clientWindow = XCreateSimpleWindow(
      dpy,
      DefaultRootWindow(dpy),
      0, 0,
      1, 1,
      0,
      BlackPixel(dpy, DefaultScreen(dpy)),
      WhitePixel(dpy, DefaultScreen(dpy)));
    if (clientWindow == None)
        return(DPSCAPFAILED);

    /* Try to open a connection to an agent */

    if ((extData = DPSCAPOpenAgent(dpy, fullDisplayName)) == NULL)
        {
        XDestroyWindow(dpy, clientWindow);
        return(DPSCAPFAILED);
        }

    /* DPSCAPOpenAgent only partially fills in extData, so finish it */

    codes = XAddExtension(dpy);
    codes->major_opcode = DPSXOPCODEBASE;
    codes->first_event = 0;  /* REQUIRED */
    codes->first_error = FirstExtensionError;
    extData->number = codes->extension;
    extData->free_private = DPSCAPDestroy;
    my = (DPSCAPData) extData->private_data;
    my->codes = codes;
    agent = my->agent;
    /* +++ Is this all we have to do here? */

    /* Send opening handshake */

    indian = 1;
    if (*(char *) &indian)
        setup.byteorder = 'l';
    else
    	setup.byteorder = 'B';
    setup.dpscapVersion = DPSCAPPROTOVERSION;
    setup.flags = DPSCAPNONEFLAG;
    setup.libraryversion = DPSPROTOCOLVERSION;
    setup.authProtoNameLength = 0;
    setup.authProtoDataLength = 0;
    setup.displayStringLength = strlen(fullDisplayName);
    setup.nodeStringLength = 0;
    setup.transportStringLength = 0;
    setup.display = 0;
    setup.screen = 0;
    setup.reserved = 0;
    setup.clientWindow = clientWindow;

#ifndef DPSLNKL
    DPSCAPWrite(agent, (char *)&setup, sizeof(xCAPConnSetupReq), dpscap_nopad,dpscap_insert);
    DPSCAPWrite(agent, fullDisplayName, setup.displayStringLength, dpscap_pad, dpscap_append);
    N_XFlush(agent);
#else /* DPSLNKL */
    if (CSDPSConfirmDisplay(agent, dpy, &setup, &reply, fullDisplayName) == 1)
        {
	p = (xCAPConnReplyPrefix *)&reply.good;
        goto skip_read;
	}
    /* Read normal reply */
#endif /* DPSLNKL */

    /* Read common reply prefix */

    p = (xCAPConnReplyPrefix *)&reply.good;
    N_XRead(agent, (char *)p, (long)sizeof(xCAPConnReplyPrefix));
#ifdef DPSLNKL
skip_read:
#endif
    if (!p->success)
        {
	char mbuf[512];
        /* read the rest */
        c = (char *)&reply.bad.serverVersion;
        N_XRead(agent, c, sz_xCAPConnFailed - sz_xCAPConnReplyPrefix);
	sprintf(mbuf, "DPS NX: connection to \"%s\" refused by agent.", DisplayString(agent));
	DPSWarnProc(NULL, mbuf);
        c = (char *)Xmalloc(reply.bad.reasonLength);
        if (!c) return(DPSCAPFAILED);
        N_XReadPad(agent, c, (long)reply.bad.reasonLength);
	if (!reply.bad.reasonLength)
	    sprintf(mbuf, "DPS NX: (no reason given)\n");
	else
            {
	    strcpy(mbuf, "DPS NX: ");
	    strncat(mbuf, c, reply.bad.reasonLength);
	    mbuf[reply.bad.reasonLength+7] = '\0';
	    }
	DPSWarnProc(NULL, mbuf);
        Xfree(c);
        DPSCAPDestroy(extData);
        Xfree(extData);
        XDestroyWindow(dpy, clientWindow);
        return(DPSCAPFAILED);
        }

    /* read the rest of the fixed length reply */
    c = (char *)&reply.good.serverVersion;
    rest = sizeof(xCAPConnSuccess) - sizeof(xCAPConnReplyPrefix);
    N_XRead(agent, c, rest);

    /* verify */

    if (reply.good.serverVersion < DPSPROTOCOLVERSION)
        {
	/* Fine, we downgrade the client */
	char qbuf[256];
	sprintf(qbuf, "NX: server version %ld older than expected %d, client will downgrade", (long)reply.good.serverVersion, DPSPROTOCOLVERSION);
	DPSWarnProc(NULL, qbuf);
	}
    my->dpscapVersion = reply.good.dpscapVersion;
    if (my->dpscapVersion < DPSCAPPROTOVERSION)
        {
	/* Fine, we downgrade the client */
	char kbuf[256];
	sprintf(kbuf, "NX: agent version %d older than expected %d, client will downgrade", my->dpscapVersion, DPSCAPPROTOVERSION);
	DPSWarnProc(NULL, kbuf);
#ifdef XXX
        /* Saving this code as a reminder about what needs to be
	   cleaned up if we exit here */
        DPSCAPDestroy(extData);
        Xfree(extData);
        XDestroyWindow(clientWindow);
        return(DPSCAPFAILED);
#endif
	}

    if (numberType)
        *numberType = reply.good.preferredNumberFormat;

    /* read additional data */

    c = (char *)Xmalloc(reply.good.floatingNameLength + 1);
    N_XReadPad(agent, c, reply.good.floatingNameLength);
    c[reply.good.floatingNameLength] = 0;
    if (floatingName)
	*floatingName = c;
    else
	Xfree(c);

    /* set library extension data */

    XDPSLSetVersion(agent, reply.good.serverVersion);
    XDPSLSetVersion(dpy, reply.good.serverVersion);
    XDPSLSetShunt(dpy, agent);
    XDPSLSetCodes(dpy, codes);
    if (XDPSLGetSyncMask(dpy) == DPSCAP_SYNCMASK_NONE)
        XDPSLSetSyncMask(dpy, DPSCAP_SYNCMASK_DFLT);
    my->agentWindow = reply.good.agentWindow;
    XDPSLSetGCFlushMode(dpy, XDPSNX_GC_UPDATES_SLOW); /* default */

    /* Hook my extension data on the dpy */

    my->extData = extData;
    XAddToExtensionList(CSDPSHeadOfDpyExt(dpy), extData);
    (void) XESetCloseDisplay(dpy, codes->extension, DPSCAPCloseDisplayProc);
    (void) XESetCopyGC(dpy, codes->extension, DPSCAPCopyGCProc);
    (void) XESetFreeGC(dpy, codes->extension, DPSCAPFreeGCProc);
    (void) XESetFlushGC(dpy, codes->extension, DPSCAPFlushGCProc);
    XDPSLSetClientMessageHandler(dpy);

    /* Chain my data on global list */

    my->next = gCSDPS->head;
    gCSDPS->head = my;

#ifdef MAHALO
    /* Set function that is called after every Xlib protocol proc */
    XDPSLSetAfterProc(dpy);

    /* All CSDPS protocol is auto-flushed */
    (void) XSetAfterFunction(agent, DPSCAPFlushAfterProc);
#endif /* MAHALO */

    /* set agent arguments, if needed */
    /* must follow setting of ShuntMap at least, so safest to
       do here when everything has been setup */
    XDPSLUpdateAgentArgs(dpy);

    return(DPSCAPSUCCESS);
}


XExtData **
CSDPSHeadOfDpyExt(
   Display *dpy)
{
   XEDataObject object;

   object.display = dpy;
   return(XEHeadOfExtensionList(object));
}

void
XDPSSyncGCClip(
    register Display *dpy,
    register GC gc)
{
    /* The primary utility of this function is for DPS NX correctness,
       but it also helps DPS/X do less work in tracking GC changes. */
    XDPSLSyncGCClip(dpy, gc);
}

void
XDPSReconcileRequests(
    register DPSContext ctxt)
{
    Display *dpy;
    register ContextXID cxid;
    register DPSContext curCtxt;

    for (curCtxt = ctxt; curCtxt; curCtxt = curCtxt->chainChild)
        {
	cxid = XDPSXIDFromContext(&dpy, curCtxt);
	if (dpy == (Display *)NULL || cxid == None)
	    break;  /* Skip text contexts */
	XDPSLReconcileRequests(dpy, cxid);
        }
}

Status
XDPSNXSetAgentArg(
    Display *dpy,
    int arg, int val)
{
    if (!dpy || arg >= 0 || arg < AGENTLASTARG)
        return(!Success);
    else
        return(XDPSLSetAgentArg(dpy, arg, val));
}

/* New for DPS NX 2.0 */
void
XDPSFlushGC(
    Display *dpy,
    GC gc)
{
    if (dpy && gc)
        XDPSLFlushGC(dpy, gc);
}

/* === SUPPORT PROCS === */

void
DPSCAPChangeGC(
    register Display *agent,
    GC gc,
    unsigned long valuemask,
    XGCValues *values)
{
    register xChangeGCReq *req;
    unsigned long oldDirty = gc->dirty;

    /* ASSERT: called from within LockDisplay section */

    /* Always include the clip_mask */
    valuemask |= GCClipMask;
    /* +++ HACK!  Hide the gc->rects flag in arc_mode */
    valuemask |= GCArcMode;
    valuemask &= (1L << (GCLastBit + 1)) - 1;

    /* Stupid macro insists on Display being called 'dpy' */
    {
    Display *dpy = agent;
    Display *xdpy = (Display *)NULL;
    NXMacroGetReq(ChangeGC, req);
    }
    req->gc = XGContextFromGC(gc);
    gc->dirty = req->mask = valuemask;
    {
/* Derived from MIT XCrGC.c, _XGenerateGCList:
Copyright 1985, 1986, 1987, 1988, 1989 by the
Massachusetts Institute of Technology

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation, and that the name of M.I.T. not be used in advertising or
publicity pertaining to distribution of the software without specific,
written prior permission.  M.I.T. makes no representations about the
suitability of this software for any purpose.  It is provided "as is"
without express or implied warranty.
*/
    /* Warning!  This code assumes that "unsigned int" is 32-bits wide */

    unsigned int vals[32];
    register unsigned int *value = vals;
    long nvalues;
    register XGCValues *gv = values;
    register unsigned long dirty = gc->dirty;

    /*
     * Note: The order of these tests are critical; the order must be the
     * same as the GC mask bits in the word.
     */
    if (dirty & GCFunction)          *value++ = gv->function;
    if (dirty & GCPlaneMask)         *value++ = gv->plane_mask;
    if (dirty & GCForeground)        *value++ = gv->foreground;
    if (dirty & GCBackground)        *value++ = gv->background;
    if (dirty & GCLineWidth)         *value++ = gv->line_width;
    if (dirty & GCLineStyle)         *value++ = gv->line_style;
    if (dirty & GCCapStyle)          *value++ = gv->cap_style;
    if (dirty & GCJoinStyle)         *value++ = gv->join_style;
    if (dirty & GCFillStyle)         *value++ = gv->fill_style;
    if (dirty & GCFillRule)          *value++ = gv->fill_rule;
    if (dirty & GCTile)              *value++ = gv->tile;
    if (dirty & GCStipple)           *value++ = gv->stipple;
    if (dirty & GCTileStipXOrigin)   *value++ = gv->ts_x_origin;
    if (dirty & GCTileStipYOrigin)   *value++ = gv->ts_y_origin;
    if (dirty & GCFont)              *value++ = gv->font;
    if (dirty & GCSubwindowMode)     *value++ = gv->subwindow_mode;
    if (dirty & GCGraphicsExposures) *value++ = gv->graphics_exposures;
    if (dirty & GCClipXOrigin)       *value++ = gv->clip_x_origin;
    if (dirty & GCClipYOrigin)       *value++ = gv->clip_y_origin;
    if (dirty & GCClipMask)          *value++ = gv->clip_mask;
    if (dirty & GCDashOffset)        *value++ = gv->dash_offset;
    if (dirty & GCDashList)          *value++ = gv->dashes;
    /* +++ HACK!  Hide gc->rects flag in GCArcMode */
    if (dirty & GCArcMode)           *value++ = gc->rects;

    req->length += (nvalues = value - vals);

    /*
     * note: Data is a macro that uses its arguments multiple
     * times, so "nvalues" is changed in a separate assignment
     * statement
     */

    nvalues <<= 2;
    Data32 (agent, (long *) vals, nvalues);
    }

    gc->dirty = oldDirty;

    /* ASSERT: SyncHandle called by caller */
}


DPSCAPData
DPSCAPCreate(
    Display *dpy, Display *agent)
{
    register DPSCAPData my = (DPSCAPData)Xcalloc(1, sizeof(DPSCAPDataRec));

    if (my == (DPSCAPData)NULL) return(NULL);
    my->dpy = dpy;
    my->agent = agent;
    my->typePSOutput = XInternAtom(
      dpy,
      DPSCAP_TYPE_PSOUTPUT,
      False);
    my->typePSOutputWithLen = XInternAtom(
      dpy,
      DPSCAP_TYPE_PSOUTPUT_LEN,
      False);
    my->typePSStatus = XInternAtom(
      dpy,
      DPSCAP_TYPE_PSSTATUS,
      False);
    my->typeNoop = XInternAtom(
      dpy,
      DPSCAP_TYPE_NOOP,
      False);
    my->typeSync = XInternAtom(
      dpy,
      DPSCAP_TYPE_SYNC,
      False);
    my->typeXError = XInternAtom(
      dpy,
      DPSCAP_TYPE_XERROR,
      False);
    my->typePSReady = XInternAtom(
      dpy,
      DPSCAP_TYPE_PSREADY,
      False);
    my->typeResume = XInternAtom(
      dpy,
      DPSCAP_TYPE_RESUME,
      False);
    return(my);
}

int
DPSCAPDestroy(
    XExtData *extData)
{
    register DPSCAPData my = (DPSCAPData) extData->private_data;
    register DPSCAPData n;

    if (my == (DPSCAPData)NULL) return(0);
    DPSCAPCloseAgent(my->agent);
    my->agent = NULL;
    /* my->extData->private_data = NIL; ???? +++ */
    if (my == gCSDPS->head)
        gCSDPS->head = my->next;
    else for (n = gCSDPS->head; n != NULL; n = n->next)
        if (n->next == my)
            {
            n->next = my->next;
            break;
            }
    Xfree(my);
    /* extData freed by caller (e.g., _XFreeExtData) */
    return(0);
}

void
DPSCAPStartUp(void)
{
    gCSDPS = (DPSCAPGlobals)Xcalloc(1, sizeof(DPSCAPGlobalsRec));
}


static unsigned char padAdd[] = {0, 3, 2, 1};

void
DPSCAPWrite(
    Display *agent,
    char *buf,
    unsigned len,
    DPSCAPIOFlags writePad,
    DPSCAPIOFlags bufMode)
{
    int pad = padAdd[len & 3];
    unsigned fullLen = (writePad == dpscap_pad) ? len + pad  : len;

    if (agent->bufptr + fullLen > agent->bufmax)
        N_XFlush(agent);
    if (agent->max_request_size && fullLen > agent->max_request_size)
        {
	DPSWarnProc(NULL, "DPS Client Library: request length exceeds max request size. Truncated.\n");
	len = agent->max_request_size;
	pad = 0;
	}
    if (bufMode == dpscap_insert)
        {
        agent->last_req = agent->bufptr;
        agent->request++;
        }
    bcopy(buf, agent->bufptr, len);
    agent->bufptr += len;
    if (writePad == dpscap_pad && pad)
        {
        bcopy((char *) padAdd, agent->bufptr, pad);
        agent->bufptr += pad;
        }
}


/* === EXT CALLBACK HOOKS === */

int
DPSCAPCloseDisplayProc(
    Display *dpy,
    XExtCodes *codes)
{
#ifdef CSDPS
    fprintf(stderr, "NX: Closing agent \"%s\"\n", dpy->display_name);
#endif

    /* Although it seems that we should free codes here, we can't
       because Xlib owns the storage */

    XDPSLSetShunt(dpy, (Display *) NULL);
    XDPSLSetCodes(dpy, (XExtCodes *) NULL);
    XDPSLSetSyncMask(dpy, DPSCAP_SYNCMASK_NONE);
    XDPSLCleanAll(dpy);
    XDPSPrivZapDpy(dpy);
    return(0);
}


int
DPSCAPCopyGCProc(
    Display *dpy,
    GC gc,
    XExtCodes *codes)
{
    XGCValues values;
    DPSCAPData my;
    XExtData *extData = XFindOnExtensionList(
      CSDPSHeadOfDpyExt(dpy),
      codes->extension);

    if (extData)
        my = (DPSCAPData) extData->private_data;
    else
        return(0);

    /* We change the GC unconditionally, since friggin' XCopyGC
       clears the dirty bits of the values that are copied! */

    DPSAssertWarn(XGetGCValues(dpy, gc, DPSGCBITS & ~(GCClipMask), &values),
	NULL, "DPS NX: XGetGCValues returned False\n");
    values.clip_mask = gc->values.clip_mask;
    DPSCAPChangeGC(my->agent, gc, DPSGCBITS, &values);
    /* We have to make sure that the agent completely processes
	the change to the GC.  If we allow the agent to update the
	GC in its own sweet time, the stupid client may delete the
	GC after the agent has already queued a request to, e.g.,
	copy the GC, but before the request is flushed. */
    XDPSLSync(dpy);
    return(1);
}

int
DPSCAPFreeGCProc(
    Display *pdpy,
    GC gc,
    XExtCodes *codes)
{
    register xCAPNotifyReq *req;
    DPSCAPData my;
    Display *dpy = pdpy;  /* Stupid macros insists on Display being 'dpy' */
    XExtData *extData = XFindOnExtensionList(
      CSDPSHeadOfDpyExt(dpy),
      codes->extension);

    if (extData)
        my = (DPSCAPData) extData->private_data;
    else
        return(0);

    /* Notify the agent that the client deleted a GC.  Let the
       agent figure out if it cares. */

    /* ASSERT: called from within LockDisplay section */

    dpy = my->agent;
    if (dpy == (Display *)NULL || dpy == pdpy)
        return(0);

    /* May need to sync changes to GC */
    if (gNXSyncGCMode == DPSNXSYNCGCMODE_DELAYED)
        XDPSLSync(pdpy);

    {
    Display *xdpy = pdpy;			    /* pdpy is X server */
    NXMacroGetReq(CAPNotify, req);
    }
    req->reqType = DPSCAPOPCODEBASE;
    req->type = X_CAPNotify;
    req->cxid = 0;
    req->notification = DPSCAPNOTE_FREEGC;
    req->data = XGContextFromGC(gc);
    req->extra = 0;
    /* Guarantee that everyone sees GC go away */
    XSync(pdpy, False);                             /* pdpy is X server */
    if (gNXSyncGCMode == DPSNXSYNCGCMODE_FLUSH)
        {
        LockDisplay(dpy);                           /* dpy means agent here */
        N_XFlush(dpy);
        UnlockDisplay(dpy);
	}
    else
        XDPSLSync(pdpy);

    /* ASSERT: SynchHandle called by caller */
    return(1);
}

#ifdef CSDPSDEBUG
static unsigned int gcCountFlushedClean = 0;
static unsigned int gcCountFlushedDirty = 0;
#endif /* CSDPSDEBUG */

int
DPSCAPFlushGCProc(
    Display *dpy,
    GC gc,
    XExtCodes *codes)
{
    XGCValues values;
    DPSCAPData my;
    XExtData *extData;
#ifdef CSDPSDEBUG
    unsigned long int dirty;
#endif /* CSDPSDEBUG */

    /* When GC is created, it is flushed with no dirty bits set,
       so we have to notice that situation. */

    if (gc->dirty)
        {
	if (XDPSLGetGCFlushMode(dpy) == XDPSNX_GC_UPDATES_FAST
	   || !(gc->dirty & DPSGCBITS))
	    return(0);
	}
    extData = XFindOnExtensionList(CSDPSHeadOfDpyExt(dpy), codes->extension);
    if (extData)
        my = (DPSCAPData) extData->private_data;
    else
        return(0);
    /* HERE IF (gc->dirty & DPSGCBITS || !gc->dirty) */
#ifdef CSDPSDEBUG
    dirty = gc->dirty;
#endif /* CSDPSDEBUG */
    DPSAssertWarn(XGetGCValues(dpy, gc, DPSGCBITS & ~(GCClipMask), &values),
	NULL, "NX: XGetGCValues returned False\n");
    values.clip_mask = gc->values.clip_mask;
    /* Must guarantee that gc change is registered by X server
       before notification is sent to agent. */
    XSync(dpy, False);
    DPSCAPChangeGC(my->agent, gc, DPSGCBITS, &values);
    /* We have to make sure that the agent completely processes
       the change to the GC.  If we allow the agent to update the
       GC in its own sweet time, the stupid client may delete the
       GC after the agent has already queued a request to, e.g.,
       copy the GC, but before the request is flushed. */
    if (gNXSyncGCMode == DPSNXSYNCGCMODE_SYNC)
        XDPSLSync(dpy);
    else
        XDPSLFlush(dpy);
#ifdef CSDPSDEBUG
    if (dirty)
        ++gcCountFlushedDirty;
    else
        ++gcCountFlushedClean;
#endif /* CSDPSDEBUG */
    return(1);
}