summaryrefslogtreecommitdiff
path: root/lib/libXtst/src/XRecord.c
blob: 45c8b431043cf1edff209399acc3ecbfe40eaa26 (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
/*
XRecord.c - client-side library for RECORD extension

Copyright 1995, 1998  The Open Group

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.

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

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

*/
/***************************************************************************
 * Copyright 1995 Network Computing Devices
 *
 * 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 Network Computing Devices
 * not be used in advertising or publicity pertaining to distribution
 * of the software without specific, written prior permission.
 *
 * NETWORK COMPUTING DEVICES DISCLAIMs ALL WARRANTIES WITH REGARD TO
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES BE LIABLE
 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 **************************************************************************/
/*
 * By Stephen Gildea, X Consortium, and Martha Zimet, NCD.
 */

#include <stdio.h>
#include <assert.h>
#include <X11/Xlibint.h>
#include <X11/extensions/Xext.h>
#include <X11/extensions/extutil.h>
#include <X11/extensions/recordproto.h>
#include <X11/extensions/record.h>

static XExtensionInfo _xrecord_info_data;
static XExtensionInfo *xrecord_info = &_xrecord_info_data;
static /* const */ char *xrecord_extension_name = RECORD_NAME;

#define XRecordCheckExtension(dpy,i,val) \
    XextCheckExtension(dpy, i, xrecord_extension_name, val)

/**************************************************************************
 *                                                                        *
 *			   private utility routines                       *
 *                                                                        *
 **************************************************************************/

static XExtDisplayInfo *find_display(Display *dpy);

/*
 * A reply buffer holds a reply from RecordEnableContext.
 * Pieces of that buffer are passed to the XRecordEnableContext callback.
 * ref_count is incremented each time we do that.
 * ref_count is decremented each time XRecordFreeData is called on
 * the buffer.  When ref_count is 0, we can free or reuse the buffer.
 */
struct reply_buffer
{
    struct reply_buffer *next;	/* next in list or NULL */
    unsigned char *buf;		/* pointer to malloc'd buffer */
    int nbytes;			/* size of buf */
    int ref_count;		/* callback uses pending */
};


/*
 * There's some extra information the implementation finds useful
 * to attach to an XRecordInterceptData packet to handle memory
 * management.  So we really allocate one of these.
 */
struct intercept_queue
{
    /* this struct gets passed to the user as an XRecordInterceptData,
       so the data field must come first so we can cast the address
       back and forth */
    XRecordInterceptData data;
    struct intercept_queue *next; /* next in free list or NULL */
    struct mem_cache_str *cache; /* contains head of free list */
};

/*
 * per-display pointers to cache of malloc'd but unused memory
 */
struct mem_cache_str
{
    struct intercept_queue *inter_data;	/* free structs only */
    struct reply_buffer *reply_buffers;	/* all reply buffers */
    int inter_data_count;	/* total allocated, free and in use */
    Bool display_closed;	/* so we know when to free ourself */
};

static int close_display(
    Display *dpy,
    XExtCodes *codes)		/* not used */
{
    XExtDisplayInfo *info = find_display (dpy);

    LockDisplay(dpy);
    if (info && info->data) {
	struct mem_cache_str *cache = (struct mem_cache_str *)info->data;
	struct intercept_queue *iq, *iq_next;
	struct reply_buffer *rbp, **rbp_next_p;

	for (iq=cache->inter_data; iq; iq=iq_next) {
	    iq_next = iq->next;
	    XFree(iq);
	    cache->inter_data_count--;
	}

	/* this is a little trickier, because some of these
	   might still be in use */
	for (rbp_next_p = &cache->reply_buffers; *rbp_next_p; ) {
	    rbp = *rbp_next_p;
	    if (rbp->ref_count == 0) {
		*rbp_next_p = rbp->next;
		XFree(rbp->buf);
		XFree(rbp);
	    } else {
		rbp_next_p = &rbp->next;
	    }
	}

	if (cache->reply_buffers == NULL  &&  cache->inter_data_count == 0) {
	    /* every thing has been freed, can free ourselves, too */
	    XFree(cache);
	} else {
	    cache->display_closed = True;
	    cache->inter_data = NULL; /* neatness only; won't be used */
	}
    }
    UnlockDisplay(dpy);
    return XextRemoveDisplay(xrecord_info, dpy);
}

static XPointer alloc_mem_cache(void)
{
    struct mem_cache_str *cache;
    
    /* note that an error will go unnoticed */
    cache = (struct mem_cache_str *) Xmalloc(sizeof(struct mem_cache_str));
    if (cache) {
	cache->display_closed = False;
	cache->inter_data = NULL;
	cache->inter_data_count = 0;
	cache->reply_buffers = NULL;
    }
    return (XPointer) cache;
}

static const char *xrecord_error_list[] = {
    "XRecordBadContext (Not a defined RECORD context)",
};

static XEXT_GENERATE_ERROR_STRING (error_string, xrecord_extension_name,
                                   RecordNumErrors, xrecord_error_list)

static XExtensionHooks xrecord_extension_hooks = {
    NULL,                               /* create_gc */
    NULL,                               /* copy_gc */
    NULL,                               /* flush_gc */
    NULL,                               /* free_gc */
    NULL,                               /* create_font */
    NULL,                               /* free_font */
    close_display,                      /* close_display */
    NULL,                      		/* wire_to_event */
    NULL,                      		/* event_to_wire */
    NULL,                               /* error */
    error_string                        /* error_string */
};

static XEXT_GENERATE_FIND_DISPLAY (find_display, xrecord_info,
	xrecord_extension_name, &xrecord_extension_hooks, RecordNumEvents,
	alloc_mem_cache())

/**************************************************************************
 *                                                                        *
 *			   private library routines                       *
 *                                                                        *
 **************************************************************************/

static void
SendRange(
    Display 	*dpy,
    XRecordRange **range_item,
    int   	nranges)
{
    int 		rlen = SIZEOF(xRecordRange);
    while(nranges--)
    {
       xRecordRange xrange;

       xrange.coreRequestsFirst = (*range_item)->core_requests.first;
       xrange.coreRequestsLast = (*range_item)->core_requests.last;
       xrange.coreRepliesFirst = (*range_item)->core_replies.first;
       xrange.coreRepliesLast = (*range_item)->core_replies.last;
       xrange.extRequestsMajorFirst = (*range_item)->ext_requests.ext_major.first;
       xrange.extRequestsMajorLast = (*range_item)->ext_requests.ext_major.last;
       xrange.extRequestsMinorFirst = (*range_item)->ext_requests.ext_minor.first;
       xrange.extRequestsMinorLast = (*range_item)->ext_requests.ext_minor.last;
       xrange.extRepliesMajorFirst = (*range_item)->ext_replies.ext_major.first;
       xrange.extRepliesMajorLast = (*range_item)->ext_replies.ext_major.last;
       xrange.extRepliesMinorFirst = (*range_item)->ext_replies.ext_minor.first;
       xrange.extRepliesMinorLast = (*range_item)->ext_replies.ext_minor.last;
       xrange.deliveredEventsFirst = (*range_item)->delivered_events.first;
       xrange.deliveredEventsLast = (*range_item)->delivered_events.last;
       xrange.deviceEventsFirst = (*range_item)->device_events.first;
       xrange.deviceEventsLast = (*range_item)->device_events.last;
       xrange.errorsFirst = (*range_item)->errors.first;
       xrange.errorsLast = (*range_item)->errors.last;
       xrange.clientStarted = (*range_item)->client_started;
       xrange.clientDied = (*range_item)->client_died;
 
       Data(dpy, (char *)&xrange, rlen);
       range_item++;
    }
}

/**************************************************************************
 *                                                                        *
 *		    public routines               			  *
 *                                                                        *
 **************************************************************************/

XID
XRecordIdBaseMask(Display *dpy)
{
    return 0x1fffffff & ~dpy->resource_mask;
}

Status
XRecordQueryVersion(Display *dpy, int *cmajor_return, int *cminor_return)
{
    XExtDisplayInfo *info = find_display (dpy);
    register xRecordQueryVersionReq   	*req;
    xRecordQueryVersionReply 		rep;

    XRecordCheckExtension (dpy, info, False);

    LockDisplay(dpy);
    GetReq(RecordQueryVersion, req);
    req->reqType = info->codes->major_opcode;
    req->recordReqType = X_RecordQueryVersion;
    req->majorVersion = RECORD_MAJOR_VERSION;
    req->minorVersion = RECORD_MINOR_VERSION;
    if (!_XReply(dpy,(xReply *)&rep, 0, True)) {
	UnlockDisplay(dpy);
	SyncHandle();
	return False;
    }
    UnlockDisplay(dpy);
    SyncHandle();
    *cmajor_return = rep.majorVersion;
    *cminor_return = rep.minorVersion;
    return ((rep.majorVersion == RECORD_MAJOR_VERSION) &&
	    (rep.minorVersion >= RECORD_LOWEST_MINOR_VERSION));
}

XRecordContext
XRecordCreateContext(Display *dpy, int datum_flags,
		     XRecordClientSpec *clients, int nclients,
		     XRecordRange **ranges, int nranges)
{
    XExtDisplayInfo 	*info = find_display (dpy);
    register xRecordCreateContextReq 	*req;
    int			clen = 4 * nclients;

    XRecordCheckExtension (dpy, info, 0);
    LockDisplay(dpy);
    GetReq(RecordCreateContext, req);

    req->reqType = info->codes->major_opcode;
    req->recordReqType = X_RecordCreateContext;
    req->context = XAllocID(dpy);
    req->length += (nclients * 4 +
		    nranges * SIZEOF(xRecordRange)) >> 2;
    req->elementHeader = datum_flags;
    req->nClients = nclients;
    req->nRanges = nranges;

    Data32(dpy, (long *)clients, clen);
    SendRange(dpy, ranges, nranges);

    UnlockDisplay(dpy);
    SyncHandle();
    return req->context;
}

XRecordRange *
XRecordAllocRange(void)
{
    return (XRecordRange*)Xcalloc(1, sizeof(XRecordRange));
}

Status
XRecordRegisterClients(Display *dpy, XRecordContext context, int datum_flags,
		       XRecordClientSpec *clients, int nclients,
		       XRecordRange **ranges, int nranges)
{
    XExtDisplayInfo *info = find_display (dpy);
    register xRecordRegisterClientsReq 	*req;
    int			clen = 4 * nclients;

    XRecordCheckExtension (dpy, info, 0);
    LockDisplay(dpy);
    GetReq(RecordRegisterClients, req);

    req->reqType = info->codes->major_opcode;
    req->recordReqType = X_RecordRegisterClients;
    req->context =  context;
    req->length += (nclients * 4 +
		    nranges * SIZEOF(xRecordRange)) >> 2;
    req->elementHeader = datum_flags;
    req->nClients = nclients;
    req->nRanges = nranges;

    Data32(dpy, (long *)clients, clen);
    SendRange(dpy, ranges, nranges);

    UnlockDisplay(dpy);
    SyncHandle();
    return 1;
}

Status
XRecordUnregisterClients(Display *dpy, XRecordContext context,
			 XRecordClientSpec *clients, int nclients)
{
    XExtDisplayInfo *info = find_display (dpy);
    register xRecordUnregisterClientsReq 	*req;
    int			clen = 4 * nclients;

    XRecordCheckExtension (dpy, info, 0);
    LockDisplay(dpy);
    GetReq(RecordUnregisterClients, req);

    req->reqType = info->codes->major_opcode;
    req->recordReqType = X_RecordUnregisterClients;
    req->context = context;
    req->length += nclients;
    req->nClients = nclients;

    Data32(dpy, (long *)clients, clen);

    UnlockDisplay(dpy);
    SyncHandle();
    return 1;
}

static void
WireToLibRange(
    xRecordRange *wire_range,
    XRecordRange *lib_range)
{
    lib_range->core_requests.first = wire_range->coreRequestsFirst;
    lib_range->core_requests.last = wire_range->coreRequestsLast;
    lib_range->core_replies.first = wire_range->coreRepliesFirst;
    lib_range->core_replies.last = wire_range->coreRepliesLast;
    lib_range->ext_requests.ext_major.first = wire_range->extRequestsMajorFirst;
    lib_range->ext_requests.ext_major.last = wire_range->extRequestsMajorLast;
    lib_range->ext_requests.ext_minor.first = wire_range->extRequestsMinorFirst;
    lib_range->ext_requests.ext_minor.last = wire_range->extRequestsMinorLast;
    lib_range->ext_replies.ext_major.first = wire_range->extRepliesMajorFirst;
    lib_range->ext_replies.ext_major.last = wire_range->extRepliesMajorLast;
    lib_range->ext_replies.ext_minor.first = wire_range->extRepliesMinorFirst;
    lib_range->ext_replies.ext_minor.last = wire_range->extRepliesMinorLast;
    lib_range->delivered_events.first = wire_range->deliveredEventsFirst;
    lib_range->delivered_events.last = wire_range->deliveredEventsLast;
    lib_range->device_events.first = wire_range->deviceEventsFirst;
    lib_range->device_events.last = wire_range->deviceEventsLast;
    lib_range->errors.first = wire_range->errorsFirst;
    lib_range->errors.last = wire_range->errorsLast;
    lib_range->client_started = wire_range->clientStarted;
    lib_range->client_died = wire_range->clientDied;
}

Status
XRecordGetContext(Display *dpy, XRecordContext context,
		  XRecordState **state_return)
{
    XExtDisplayInfo 	*info = find_display (dpy);
    register 		xRecordGetContextReq   	*req;
    xRecordGetContextReply 	rep;
    int			count, i, rn;
    xRecordRange   	xrange;
    XRecordRange	*ranges = NULL;
    xRecordClientInfo   xclient_inf;
    XRecordClientInfo	**client_inf, *client_inf_str = NULL;
    XRecordState	*ret;

    XRecordCheckExtension (dpy, info, 0);
    LockDisplay(dpy);
    GetReq(RecordGetContext, req);
    req->reqType = info->codes->major_opcode;
    req->recordReqType = X_RecordGetContext;
    req->context = context;
    if (!_XReply(dpy,(xReply *)&rep, 0, False)) {
	UnlockDisplay(dpy);
	SyncHandle();
	return 0;
    }
    count = rep.nClients;

    ret = (XRecordState*)Xmalloc(sizeof(XRecordState));
    if (!ret) {
	/* XXX - eat data */
	UnlockDisplay(dpy);
	SyncHandle();
	return 0;
    }

    ret->enabled = rep.enabled;
    ret->datum_flags = rep.elementHeader;
    ret->nclients = count;

    if (count)
    {
     	client_inf = (XRecordClientInfo **) Xcalloc(count, sizeof(XRecordClientInfo*));
	ret->client_info = client_inf;
	if (client_inf != NULL) {
	    client_inf_str = (XRecordClientInfo *) Xmalloc(count*sizeof(XRecordClientInfo));
	}
        if (!client_inf || !client_inf_str)
        {
           for(i = 0; i < count; i++)
           {
	        _XEatData (dpy, sizeof(xRecordClientInfo));
                _XEatData (dpy, SIZEOF(xRecordRange)); /* XXX - don't know how many */
           }
	   UnlockDisplay(dpy);
	   XRecordFreeState(ret);
	   SyncHandle();
	   return 0;
        }
        for(i = 0; i < count; i++)
        {
	    client_inf[i] = &(client_inf_str[i]);
            _XRead(dpy, (char *)&xclient_inf, (long)sizeof(xRecordClientInfo));
            client_inf_str[i].client = xclient_inf.clientResource;
            client_inf_str[i].nranges = xclient_inf.nRanges;

	    if (xclient_inf.nRanges)
	    {
		client_inf_str[i].ranges = (XRecordRange**) Xcalloc(xclient_inf.nRanges, sizeof(XRecordRange*));
		if (client_inf_str[i].ranges != NULL) {
		    ranges = (XRecordRange*)
			Xmalloc(xclient_inf.nRanges * sizeof(XRecordRange));
		}
		if (!client_inf_str[i].ranges || !ranges) {
		    /* XXX eat data */
		    UnlockDisplay(dpy);
		    XRecordFreeState(ret);
		    SyncHandle();
		    return 0;
		}
		for (rn=0; rn<xclient_inf.nRanges; rn++) {
		    client_inf_str[i].ranges[rn] = &(ranges[rn]);
		    _XRead(dpy, (char *)&xrange, (long)sizeof(xRecordRange));
		    WireToLibRange(&xrange, &(ranges[rn]));
		}
	    } else {
		client_inf_str[i].ranges = NULL;
	    }
        }
    } else {
	ret->client_info = NULL;
    }

    *state_return = ret;

    UnlockDisplay(dpy);
    SyncHandle();
    return 1;
}

void
XRecordFreeState(XRecordState *state)
{
    int i;

    for(i=0; i<state->nclients; i++) {
	if (state->client_info[i]->ranges) {
	    if (state->client_info[i]->ranges[0])
		Xfree(state->client_info[i]->ranges[0]);
	    Xfree(state->client_info[i]->ranges);
	}
    }
    if (state->client_info) {
	if (state->client_info[0])
	    Xfree(state->client_info[0]);
	Xfree(state->client_info);
    }
    Xfree(state);
}

static struct reply_buffer *alloc_reply_buffer(
    XExtDisplayInfo *info,
    int nbytes)
{
    struct mem_cache_str *cache = (struct mem_cache_str *)info->data;
    struct reply_buffer *rbp;
    struct reply_buffer *saved_rb = NULL;
    /*
     * First look for an allocated buffer that is not in use.
     * If we have a big enough buffer, use that, otherwise
     * realloc an existing one.
     */
    for (rbp = cache->reply_buffers; rbp; rbp = rbp->next) {
	if (rbp->ref_count == 0) {
	    if (rbp->nbytes >= nbytes)
		return rbp;
	    else
		saved_rb = rbp;
	}
    }
    if (saved_rb) {
	saved_rb->buf = (unsigned char *)Xrealloc(saved_rb->buf, nbytes);
	if (!saved_rb->buf) {
	    saved_rb->nbytes = 0;
	    return NULL;
	}
	saved_rb->nbytes = nbytes;
	return saved_rb;
    }

    /*
     * nothing available; malloc a new struct
     */
    rbp = (struct reply_buffer *)Xmalloc(sizeof(struct reply_buffer));
    if (!rbp)
	return NULL;
    rbp->buf = (unsigned char *)Xmalloc(nbytes);
    if (!rbp->buf) {
	Xfree(rbp);
	return NULL;
    }
    rbp->nbytes = nbytes;
    rbp->ref_count = 0;
    rbp->next = cache->reply_buffers;
    cache->reply_buffers = rbp;
    return rbp;
}

static XRecordInterceptData *alloc_inter_data(XExtDisplayInfo *info)
{
    struct mem_cache_str *cache = (struct mem_cache_str *)info->data;
    struct intercept_queue *iq;

    /* if there is one on the free list, pop it */
    if (cache->inter_data) {
	iq = cache->inter_data;
	cache->inter_data = iq->next;
	return &iq->data;
    }
    /* allocate a new one */
    iq = (struct intercept_queue *)Xmalloc(sizeof(struct intercept_queue));
    if (!iq)
	return NULL;
    iq->cache = cache;
    cache->inter_data_count++;
    return &iq->data;
}

void
XRecordFreeData(XRecordInterceptData *data)
{
    /* we can do this cast because that is what we really allocated */
    struct intercept_queue *iq = (struct intercept_queue *)data;
    struct reply_buffer *rbp = NULL;
    struct mem_cache_str *cache = iq->cache;

    /*
     * figure out what reply_buffer this points at
     * and decrement its ref_count.
     */
    if (data->data) {

	for (rbp = cache->reply_buffers; rbp; rbp = rbp->next) {
	    if (data->data >= rbp->buf
		&& data->data < rbp->buf + rbp->nbytes)
	    {
		assert(rbp->ref_count > 0);
		rbp->ref_count--;
		break;
	    }
	}
	/* it's an error if we didn't find something to free */
	assert(rbp);
    }
    /*
     * If the display is still open, put this back on the free queue.
     *
     * Otherwise the display is closed and we won't reuse this, so free it.
     * See if we can free the reply buffer, too.
     * If we can, see if this is the last reply buffer and if so
     * free the list of reply buffers.
     */
    if (cache->display_closed == False) {
	iq->next = cache->inter_data;
	cache->inter_data = iq;
    } else {
	if (rbp && rbp->ref_count == 0) {
	    struct reply_buffer *rbp2, **rbp_next_p;

	    /* Have to search the list again to find the prev element.
	       This is not the common case, so don't slow down the code
	       above by doing it then. */
	    for (rbp_next_p = &cache->reply_buffers; *rbp_next_p; ) {
		rbp2 = *rbp_next_p;
		if (rbp == rbp2) {
		    *rbp_next_p = rbp2->next;
		    break;
		} else {
		    rbp_next_p = &rbp2->next;
		}
	    }
	    XFree(rbp->buf);
	    XFree(rbp);
	}

	XFree(iq);
	cache->inter_data_count--;

	if (cache->reply_buffers == NULL  &&  cache->inter_data_count == 0) {
	    XFree(cache); /* all finished */
	}
    }
}

/* the EXTRACT macros are adapted from ICElibint.h */

#ifndef WORD64

#define EXTRACT_CARD16(swap, src, dst) \
{ \
    (dst) = *((CARD16 *) (src)); \
    if (swap) \
        (dst) = lswaps (dst); \
}

#define EXTRACT_CARD32(swap, src, dst) \
{ \
    (dst) = *((CARD32 *) (src)); \
    if (swap) \
        (dst) = lswapl (dst); \
}

#else /* WORD64 */

#define EXTRACT_CARD16(swap, src, dst) \
{ \
    (dst) = *((src) + 0); \
    (dst) <<= 8; \
    (dst) |= *((src) + 1); \
    if (swap) \
        (dst) = lswaps (dst); \
}

#define EXTRACT_CARD32(swap, src, dst) \
{ \
    (dst) = *((src) + 0); \
    (dst) <<= 8; \
    (dst) |= *((src) + 1); \
    (dst) <<= 8; \
    (dst) |= *((src) + 2); \
    (dst) <<= 8; \
    (dst) |= *((src) + 3); \
    if (swap) \
        (dst) = lswapl (dst); \
}

#endif /* WORD64 */

/* byte swapping macros from xfs/include/misc.h */

/* byte swap a long literal */
#define lswapl(x) ((((x) & 0xff) << 24) |\
		   (((x) & 0xff00) << 8) |\
		   (((x) & 0xff0000) >> 8) |\
		   (((x) >> 24) & 0xff))

/* byte swap a short literal */
#define lswaps(x) ((((x) & 0xff) << 8) | (((x) >> 8) & 0xff))

enum parser_return { Continue, End, Error };

static enum parser_return
parse_reply_call_callback(
    Display *dpy,
    XExtDisplayInfo *info,
    xRecordEnableContextReply *rep,
    struct reply_buffer *reply,
    XRecordInterceptProc callback,
    XPointer		 closure)
{
    int current_index;
    int datum_bytes = 0;
    XRecordInterceptData *data;

    /* call the callback for each protocol element in the reply */
    current_index = 0;
    do {
	data = alloc_inter_data(info);
	if (!data)
	    return Error;
	
	data->id_base = rep->idBase;
	data->category = rep->category;
	data->client_swapped = rep->clientSwapped;
	data->server_time = rep->serverTime;
	data->client_seq = rep->recordedSequenceNumber;
	/*
	 * compute the size of this protocol element.
	 */
	switch (rep->category) {
	case XRecordFromServer:
	    if (rep->elementHeader&XRecordFromServerTime) {
		EXTRACT_CARD32(rep->clientSwapped,
			       reply->buf+current_index,
			       data->server_time);
		current_index += 4;
	    }
	    switch (reply->buf[current_index]) {
	    case X_Reply: /* reply */
		EXTRACT_CARD32(rep->clientSwapped,
			       reply->buf+current_index+4, datum_bytes);
		datum_bytes = (datum_bytes+8) << 2;
		break;
	    default: /* error or event */
		datum_bytes = 32;
	    }
	    break;
	case XRecordFromClient:
	    if (rep->elementHeader&XRecordFromClientTime) {
		EXTRACT_CARD32(rep->clientSwapped,
			       reply->buf+current_index,
			       data->server_time);
		current_index += 4;
	    }
	    if (rep->elementHeader&XRecordFromClientSequence) {
		EXTRACT_CARD32(rep->clientSwapped,
			       reply->buf+current_index,
			       data->client_seq);
		current_index += 4;
	    }
	    if (reply->buf[current_index+2] == 0
		&& reply->buf[current_index+3] == 0) /* needn't swap 0 */
	    {	/* BIG-REQUESTS */
		EXTRACT_CARD32(rep->clientSwapped,
			       reply->buf+current_index+4, datum_bytes);
	    } else {
		EXTRACT_CARD16(rep->clientSwapped,
			       reply->buf+current_index+2, datum_bytes);
	    }
	    datum_bytes <<= 2;
	    break;
	case XRecordClientStarted:
	    EXTRACT_CARD16(rep->clientSwapped,
			   reply->buf+current_index+6, datum_bytes);
	    datum_bytes = (datum_bytes+2) << 2;
	    break;
	case XRecordClientDied:
	    if (rep->elementHeader&XRecordFromClientSequence) {
		EXTRACT_CARD32(rep->clientSwapped,
			       reply->buf+current_index,
			       data->client_seq);
		current_index += 4;
	    }
	    /* fall through */
	case XRecordStartOfData:
	case XRecordEndOfData:
	    datum_bytes = 0;
	}
	
	if (datum_bytes > 0) {
	    if (current_index + datum_bytes > rep->length << 2)
		fprintf(stderr,
			"XRecord: %lu-byte reply claims %d-byte element (seq %lu)\n",
			(long)rep->length << 2, current_index + datum_bytes,
			dpy->last_request_read);
	    /*
	     * This assignment (and indeed the whole buffer sharing
	     * scheme) assumes arbitrary 4-byte boundaries are
	     * addressable. 
	     */
	    data->data = reply->buf+current_index;
	    reply->ref_count++;
	} else {
	    data->data = NULL;
	}
	data->data_len = datum_bytes >> 2;
	
	(*callback)(closure, data);
	
	current_index += datum_bytes;
    } while (current_index<rep->length<<2);

    if (rep->category == XRecordEndOfData)
	return End;

    return Continue;
}

Status
XRecordEnableContext(Display *dpy, XRecordContext context,
		     XRecordInterceptProc callback, XPointer closure)
{
    XExtDisplayInfo *info = find_display (dpy);
    register xRecordEnableContextReq   	*req;
    xRecordEnableContextReply 	rep;
    struct reply_buffer *reply;
    enum parser_return status;

    XRecordCheckExtension (dpy, info, 0);
    LockDisplay(dpy);
    GetReq(RecordEnableContext, req);

    req->reqType = info->codes->major_opcode;
    req->recordReqType = X_RecordEnableContext;
    req->context = context;

    while (1)
    {
	/* This code should match that in XRecordEnableContextAsync */
	if (!_XReply (dpy, (xReply *)&rep, 0, xFalse))
	{
	    UnlockDisplay(dpy);
	    SyncHandle();
	    return 0;
	}

	if (rep.length > 0) {
	    reply = alloc_reply_buffer(info, rep.length<<2);
	    if (!reply) {
		UnlockDisplay(dpy);
		SyncHandle();
		return 0;
	    }
	    _XRead (dpy, (char *)reply->buf, rep.length<<2);
	} else {
	    reply = NULL;
	}

	status = parse_reply_call_callback(dpy, info, &rep, reply,
					   callback, closure);
	switch (status) {
	case Continue:
	    break;
	case End:
	    UnlockDisplay(dpy);
	    SyncHandle();
	    return 1;
	case Error:
	    UnlockDisplay(dpy);
	    SyncHandle();
	    return 0;
	}
    }
}


typedef struct _record_async_state
{
    unsigned long enable_seq;
    _XAsyncHandler *async;
    _XAsyncErrorState *error_state;
    XExtDisplayInfo *info;
    XRecordInterceptProc callback;
    XPointer closure;
} record_async_state;

static Bool
record_async_handler(
    register Display *dpy,
    register xReply *rep,
    char *buf,
    int len,
    XPointer adata)
{
    register record_async_state *state = (record_async_state *)adata;
    struct reply_buffer *reply;
    enum parser_return status;

    if (dpy->last_request_read != state->enable_seq)
    {
	if (dpy->last_request_read > state->enable_seq) {
	    /* it is an error that we are still on the handler list */
	    fprintf(stderr, "XRecord: handler for seq %lu never saw XRecordEndOfData.  (seq now %lu)\n",
		    state->enable_seq, dpy->last_request_read);
	    DeqAsyncHandler(dpy, state->async);
	    Xfree(state->async);
	}
	return False;
    }
    if (rep->generic.type == X_Error)
    {
	DeqAsyncHandler(dpy, state->async);
	Xfree(state->async);
	return False;
    }

    if (rep->generic.length > 0) {
	reply = alloc_reply_buffer(state->info, rep->generic.length<<2);

	if (!reply) {
	    DeqAsyncHandler(dpy, state->async);
	    Xfree(state->async);
	    return False;
	}
	
	_XGetAsyncData(dpy, (char *)reply->buf, buf, len,
		       SIZEOF(xRecordEnableContextReply),
		       rep->generic.length << 2, 0);
    } else {
	reply = NULL;
    }

    status = parse_reply_call_callback(dpy, state->info, 
				       (xRecordEnableContextReply*) rep, 
				       reply, state->callback, state->closure);

    if (status != Continue)
    {
	DeqAsyncHandler(dpy, state->async);
	Xfree(state->async);
	if (status == Error)
	    return False;
    }

    return True;
}

/*
 * reads the first reply, StartOfData, synchronously,
 * then returns allowing the app to call XRecordProcessReplies
 * to get the rest.
 */
Status
XRecordEnableContextAsync(Display *dpy, XRecordContext context,
			  XRecordInterceptProc callback, XPointer closure)
{
    XExtDisplayInfo *info = find_display (dpy);
    register xRecordEnableContextReq *req;
    xRecordEnableContextReply rep;
    struct reply_buffer *reply;
    enum parser_return status;
    _XAsyncHandler *async;
    record_async_state *async_state;

    XRecordCheckExtension (dpy, info, 0);
    async = (_XAsyncHandler *)Xmalloc(sizeof(_XAsyncHandler) +
	sizeof(record_async_state));
    if (!async)
	return 0;
    async_state = (record_async_state *)(async + 1);

    LockDisplay(dpy);
    GetReq(RecordEnableContext, req);

    req->reqType = info->codes->major_opcode;
    req->recordReqType = X_RecordEnableContext;
    req->context = context;

    /* Get the StartOfData reply. */
    /* This code should match that in XRecordEnableContext */
    if (!_XReply (dpy, (xReply *)&rep, 0, xFalse))
    {
	UnlockDisplay(dpy);
	SyncHandle();
	Xfree(async);
	return 0;
    }

    /* this had better be a StartOfData, which has no extra data. */
    if (rep.length != 0) {
	fprintf(stderr, "XRecord: malformed StartOfData for sequence %lu\n",
		dpy->last_request_read);
    }
    reply = NULL;

    status = parse_reply_call_callback(dpy, info, &rep, reply,
				       callback, closure);
    if (status != Continue)
    {
	UnlockDisplay(dpy);
	Xfree(async);
	return 0;
    }

    /* hook in the async handler for the rest of the replies */
    async_state->enable_seq = dpy->request;
    async_state->async = async;
    async_state->info = info;
    async_state->callback = callback;
    async_state->closure = closure;

    async->next = dpy->async_handlers;
    async->handler = record_async_handler;
    async->data = (XPointer)async_state;
    dpy->async_handlers = async;

    UnlockDisplay(dpy);
    /* Don't invoke SyncHandle here, since this is an async
       function.  Does this break XSetAfterFunction() ? */
    return 1;
}

void
XRecordProcessReplies(Display *dpy)
{
    (void) XPending(dpy);
}

Status
XRecordDisableContext(Display *dpy, XRecordContext context)
{
    XExtDisplayInfo *info = find_display (dpy);
    register xRecordDisableContextReq 	*req;

    XRecordCheckExtension (dpy, info, 0);
    LockDisplay(dpy);
    GetReq(RecordDisableContext, req);
    req->reqType = info->codes->major_opcode;
    req->recordReqType = X_RecordDisableContext;
    req->context =  context;

    UnlockDisplay(dpy);
    SyncHandle();
    return 1;
}

Status
XRecordFreeContext(Display *dpy, XRecordContext context)
{
    XExtDisplayInfo *info = find_display (dpy);
    register xRecordFreeContextReq 	*req;

    XRecordCheckExtension (dpy, info, 0);

    LockDisplay(dpy);
    GetReq(RecordFreeContext, req);
    req->reqType = info->codes->major_opcode;
    req->recordReqType = X_RecordFreeContext;
    req->context = context;

    UnlockDisplay(dpy);
    SyncHandle();
    return 1;
}