-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathrecord.c
2817 lines (2492 loc) · 98.6 KB
/
record.c
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
/*
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.
Author: David P. Wiggins, The Open Group
This work benefited from earlier work done by Martha Zimet of NCD
and Jim Haggerty of Metheus.
*/
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif
#include "dixstruct.h"
#include "extnsionst.h"
#include "extinit.h"
#include <X11/extensions/recordproto.h>
#include "set.h"
#include "swaprep.h"
#include "inputstr.h"
#include "eventconvert.h"
#include "scrnintstr.h"
#include <stdio.h>
#include <assert.h>
#ifdef PANORAMIX
#include "globals.h"
#include "panoramiX.h"
#include "panoramiXsrv.h"
#include "cursor.h"
#endif
#include "protocol-versions.h"
static RESTYPE RTContext; /* internal resource type for Record contexts */
/* How many bytes of protocol data to buffer in a context. Don't set to less
* than 32.
*/
#define REPLY_BUF_SIZE 1024
/* Record Context structure */
typedef struct {
XID id; /* resource id of context */
ClientPtr pRecordingClient; /* client that has context enabled */
struct _RecordClientsAndProtocolRec *pListOfRCAP; /* all registered info */
ClientPtr pBufClient; /* client whose protocol is in replyBuffer */
unsigned int continuedReply:1; /* recording a reply that is split up? */
char elemHeaders; /* element header flags (time/seq no.) */
char bufCategory; /* category of protocol in replyBuffer */
int numBufBytes; /* number of bytes in replyBuffer */
char replyBuffer[REPLY_BUF_SIZE]; /* buffered recorded protocol */
int inFlush; /* are we inside RecordFlushReplyBuffer */
} RecordContextRec, *RecordContextPtr;
/* RecordMinorOpRec - to hold minor opcode selections for extension requests
* and replies
*/
typedef union {
int count; /* first element of array: how many "major" structs to follow */
struct { /* rest of array elements are this */
short first; /* first major opcode */
short last; /* last major opcode */
RecordSetPtr pMinOpSet; /* minor opcode set for above major range */
} major;
} RecordMinorOpRec, *RecordMinorOpPtr;
/* RecordClientsAndProtocolRec, nicknamed RCAP - holds all the client and
* protocol selections passed in a single CreateContext or RegisterClients.
* Generally, a context will have one of these from the create and an
* additional one for each RegisterClients. RCAPs are freed when all their
* clients are unregistered.
*/
typedef struct _RecordClientsAndProtocolRec {
RecordContextPtr pContext; /* context that owns this RCAP */
struct _RecordClientsAndProtocolRec *pNextRCAP; /* next RCAP on context */
RecordSetPtr pRequestMajorOpSet; /* requests to record */
RecordMinorOpPtr pRequestMinOpInfo; /* extension requests to record */
RecordSetPtr pReplyMajorOpSet; /* replies to record */
RecordMinorOpPtr pReplyMinOpInfo; /* extension replies to record */
RecordSetPtr pDeviceEventSet; /* device events to record */
RecordSetPtr pDeliveredEventSet; /* delivered events to record */
RecordSetPtr pErrorSet; /* errors to record */
XID *pClientIDs; /* array of clients to record */
short numClients; /* number of clients in pClientIDs */
short sizeClients; /* size of pClientIDs array */
unsigned int clientStarted:1; /* record new client connections? */
unsigned int clientDied:1; /* record client disconnections? */
unsigned int clientIDsSeparatelyAllocated:1; /* pClientIDs malloced? */
} RecordClientsAndProtocolRec, *RecordClientsAndProtocolPtr;
/* how much bigger to make pRCAP->pClientIDs when reallocing */
#define CLIENT_ARRAY_GROWTH_INCREMENT 4
/* counts the total number of RCAPs belonging to enabled contexts. */
static int numEnabledRCAPs;
/* void VERIFY_CONTEXT(RecordContextPtr, XID, ClientPtr)
* In the spirit of the VERIFY_* macros in dix.h, this macro fills in
* the context pointer if the given ID is a valid Record Context, else it
* returns an error.
*/
#define VERIFY_CONTEXT(_pContext, _contextid, _client) { \
int rc = dixLookupResourceByType((pointer *)&(_pContext), _contextid, \
RTContext, _client, DixUseAccess); \
if (rc != Success) \
return rc; \
}
static int RecordDeleteContext(pointer /*value */ ,
XID /*id */
);
/***************************************************************************/
/* client private stuff */
/* To make declarations less obfuscated, have a typedef for a pointer to a
* Proc function.
*/
typedef int (*ProcFunctionPtr) (ClientPtr /*pClient */
);
/* Record client private. Generally a client only has one of these if
* any of its requests are being recorded.
*/
typedef struct {
/* ptr to client's proc vector before Record stuck its nose in */
ProcFunctionPtr *originalVector;
/* proc vector with pointers for recorded requests redirected to the
* function RecordARequest
*/
ProcFunctionPtr recordVector[256];
} RecordClientPrivateRec, *RecordClientPrivatePtr;
static DevPrivateKeyRec RecordClientPrivateKeyRec;
#define RecordClientPrivateKey (&RecordClientPrivateKeyRec)
/* RecordClientPrivatePtr RecordClientPrivate(ClientPtr)
* gets the client private of the given client. Syntactic sugar.
*/
#define RecordClientPrivate(_pClient) (RecordClientPrivatePtr) \
dixLookupPrivate(&(_pClient)->devPrivates, RecordClientPrivateKey)
/***************************************************************************/
/* global list of all contexts */
static RecordContextPtr *ppAllContexts;
static int numContexts; /* number of contexts in ppAllContexts */
/* number of currently enabled contexts. All enabled contexts are bunched
* up at the front of the ppAllContexts array, from ppAllContexts[0] to
* ppAllContexts[numEnabledContexts-1], to eliminate time spent skipping
* past disabled contexts.
*/
static int numEnabledContexts;
/* RecordFindContextOnAllContexts
*
* Arguments:
* pContext is the context to search for.
*
* Returns:
* The index into the array ppAllContexts at which pContext is stored.
* If pContext is not found in ppAllContexts, returns -1.
*
* Side Effects: none.
*/
static int
RecordFindContextOnAllContexts(RecordContextPtr pContext)
{
int i;
assert(numContexts >= numEnabledContexts);
for (i = 0; i < numContexts; i++) {
if (ppAllContexts[i] == pContext)
return i;
}
return -1;
} /* RecordFindContextOnAllContexts */
/***************************************************************************/
/* RecordFlushReplyBuffer
*
* Arguments:
* pContext is the context to flush.
* data1 is a pointer to additional data, and len1 is its length in bytes.
* data2 is a pointer to additional data, and len2 is its length in bytes.
*
* Returns: nothing.
*
* Side Effects:
* If the context is enabled, any buffered (recorded) protocol is written
* to the recording client, and the number of buffered bytes is set to
* zero. If len1 is not zero, data1/len1 are then written to the
* recording client, and similarly for data2/len2 (written after
* data1/len1).
*/
static void
RecordFlushReplyBuffer(RecordContextPtr pContext,
pointer data1, int len1, pointer data2, int len2)
{
if (!pContext->pRecordingClient || pContext->pRecordingClient->clientGone ||
pContext->inFlush)
return;
++pContext->inFlush;
if (pContext->numBufBytes)
WriteToClient(pContext->pRecordingClient, pContext->numBufBytes,
pContext->replyBuffer);
pContext->numBufBytes = 0;
if (len1)
WriteToClient(pContext->pRecordingClient, len1, data1);
if (len2)
WriteToClient(pContext->pRecordingClient, len2, data2);
--pContext->inFlush;
} /* RecordFlushReplyBuffer */
/* RecordAProtocolElement
*
* Arguments:
* pContext is the context that is recording a protocol element.
* pClient is the client whose protocol is being recorded. For
* device events and EndOfData, pClient is NULL.
* category is the category of the protocol element, as defined
* by the RECORD spec.
* data is a pointer to the protocol data, and datalen - padlen
* is its length in bytes.
* padlen is the number of pad bytes from a zeroed array.
* futurelen is the number of bytes that will be sent in subsequent
* calls to this function to complete this protocol element.
* In those subsequent calls, futurelen will be -1 to indicate
* that the current data is a continuation of the same protocol
* element.
*
* Returns: nothing.
*
* Side Effects:
* The context may be flushed. The new protocol element will be
* added to the context's protocol buffer with appropriate element
* headers prepended (sequence number and timestamp). If the data
* is continuation data (futurelen == -1), element headers won't
* be added. If the protocol element and headers won't fit in
* the context's buffer, it is sent directly to the recording
* client (after any buffered data).
*/
static void
RecordAProtocolElement(RecordContextPtr pContext, ClientPtr pClient,
int category, pointer data, int datalen, int padlen,
int futurelen)
{
CARD32 elemHeaderData[2];
int numElemHeaders = 0;
Bool recordingClientSwapped = pContext->pRecordingClient->swapped;
CARD32 serverTime = 0;
Bool gotServerTime = FALSE;
int replylen;
if (futurelen >= 0) { /* start of new protocol element */
xRecordEnableContextReply *pRep = (xRecordEnableContextReply *)
pContext->replyBuffer;
if (pContext->pBufClient != pClient ||
pContext->bufCategory != category) {
RecordFlushReplyBuffer(pContext, NULL, 0, NULL, 0);
pContext->pBufClient = pClient;
pContext->bufCategory = category;
}
if (!pContext->numBufBytes) {
serverTime = GetTimeInMillis();
gotServerTime = TRUE;
pRep->type = X_Reply;
pRep->category = category;
pRep->sequenceNumber = pContext->pRecordingClient->sequence;
pRep->length = 0;
pRep->elementHeader = pContext->elemHeaders;
pRep->serverTime = serverTime;
if (pClient) {
pRep->clientSwapped =
(pClient->swapped != recordingClientSwapped);
pRep->idBase = pClient->clientAsMask;
pRep->recordedSequenceNumber = pClient->sequence;
}
else { /* it's a device event, StartOfData, or EndOfData */
pRep->clientSwapped = (category != XRecordFromServer) &&
recordingClientSwapped;
pRep->idBase = 0;
pRep->recordedSequenceNumber = 0;
}
if (recordingClientSwapped) {
swaps(&pRep->sequenceNumber);
swapl(&pRep->length);
swapl(&pRep->idBase);
swapl(&pRep->serverTime);
swapl(&pRep->recordedSequenceNumber);
}
pContext->numBufBytes = SIZEOF(xRecordEnableContextReply);
}
/* generate element headers if needed */
if (((pContext->elemHeaders & XRecordFromClientTime)
&& category == XRecordFromClient)
|| ((pContext->elemHeaders & XRecordFromServerTime)
&& category == XRecordFromServer)) {
if (gotServerTime)
elemHeaderData[numElemHeaders] = serverTime;
else
elemHeaderData[numElemHeaders] = GetTimeInMillis();
if (recordingClientSwapped)
swapl(&elemHeaderData[numElemHeaders]);
numElemHeaders++;
}
if ((pContext->elemHeaders & XRecordFromClientSequence)
&& (category == XRecordFromClient || category == XRecordClientDied)) {
elemHeaderData[numElemHeaders] = pClient->sequence;
if (recordingClientSwapped)
swapl(&elemHeaderData[numElemHeaders]);
numElemHeaders++;
}
/* adjust reply length */
replylen = pRep->length;
if (recordingClientSwapped)
swapl(&replylen);
replylen += numElemHeaders + bytes_to_int32(datalen) +
bytes_to_int32(futurelen);
if (recordingClientSwapped)
swapl(&replylen);
pRep->length = replylen;
} /* end if not continued reply */
numElemHeaders *= 4;
/* if space available >= space needed, buffer the data */
if (REPLY_BUF_SIZE - pContext->numBufBytes >= datalen + numElemHeaders) {
if (numElemHeaders) {
memcpy(pContext->replyBuffer + pContext->numBufBytes,
elemHeaderData, numElemHeaders);
pContext->numBufBytes += numElemHeaders;
}
if (datalen) {
static char padBuffer[3]; /* as in FlushClient */
memcpy(pContext->replyBuffer + pContext->numBufBytes,
data, datalen - padlen);
pContext->numBufBytes += datalen - padlen;
memcpy(pContext->replyBuffer + pContext->numBufBytes,
padBuffer, padlen);
pContext->numBufBytes += padlen;
}
}
else {
RecordFlushReplyBuffer(pContext, (pointer) elemHeaderData,
numElemHeaders, (pointer) data,
datalen - padlen);
}
} /* RecordAProtocolElement */
/* RecordFindClientOnContext
*
* Arguments:
* pContext is the context to search.
* clientspec is the resource ID mask identifying the client to search
* for, or XRecordFutureClients.
* pposition is a pointer to an int, or NULL. See Returns.
*
* Returns:
* The RCAP on which clientspec was found, or NULL if not found on
* any RCAP on the given context.
* If pposition was not NULL and the returned RCAP is not NULL,
* *pposition will be set to the index into the returned the RCAP's
* pClientIDs array that holds clientspec.
*
* Side Effects: none.
*/
static RecordClientsAndProtocolPtr
RecordFindClientOnContext(RecordContextPtr pContext,
XID clientspec, int *pposition)
{
RecordClientsAndProtocolPtr pRCAP;
for (pRCAP = pContext->pListOfRCAP; pRCAP; pRCAP = pRCAP->pNextRCAP) {
int i;
for (i = 0; i < pRCAP->numClients; i++) {
if (pRCAP->pClientIDs[i] == clientspec) {
if (pposition)
*pposition = i;
return pRCAP;
}
}
}
return NULL;
} /* RecordFindClientOnContext */
/* RecordABigRequest
*
* Arguments:
* pContext is the recording context.
* client is the client being recorded.
* stuff is a pointer to the big request of client (see the Big Requests
* extension for details.)
*
* Returns: nothing.
*
* Side Effects:
* The big request is recorded with the correct length field re-inserted.
*
* Note: this function exists mainly to make RecordARequest smaller.
*/
static void
RecordABigRequest(RecordContextPtr pContext, ClientPtr client, xReq * stuff)
{
CARD32 bigLength;
int bytesLeft;
/* note: client->req_len has been frobbed by ReadRequestFromClient
* (os/io.c) to discount the extra 4 bytes taken by the extended length
* field in a big request. The actual request length to record is
* client->req_len + 1 (measured in CARD32s).
*/
/* record the request header */
bytesLeft = client->req_len << 2;
RecordAProtocolElement(pContext, client, XRecordFromClient,
(pointer) stuff, SIZEOF(xReq), 0, bytesLeft);
/* reinsert the extended length field that was squished out */
bigLength = client->req_len + bytes_to_int32(sizeof(bigLength));
if (client->swapped)
swapl(&bigLength);
RecordAProtocolElement(pContext, client, XRecordFromClient,
(pointer) &bigLength, sizeof(bigLength), 0,
/* continuation */ -1);
bytesLeft -= sizeof(bigLength);
/* record the rest of the request after the length */
RecordAProtocolElement(pContext, client, XRecordFromClient,
(pointer) (stuff + 1), bytesLeft, 0,
/* continuation */ -1);
} /* RecordABigRequest */
/* RecordARequest
*
* Arguments:
* client is a client that the server has dispatched a request to by
* calling client->requestVector[request opcode] .
* The request is in client->requestBuffer.
*
* Returns:
* Whatever is returned by the "real" Proc function for this request.
* The "real" Proc function is the function that was in
* client->requestVector[request opcode] before it was replaced by
* RecordARequest. (See the function RecordInstallHooks.)
*
* Side Effects:
* The request is recorded by all contexts that have registered this
* request for this client. The real Proc function is called.
*/
static int
RecordARequest(ClientPtr client)
{
RecordContextPtr pContext;
RecordClientsAndProtocolPtr pRCAP;
int i;
RecordClientPrivatePtr pClientPriv;
REQUEST(xReq);
int majorop;
majorop = stuff->reqType;
for (i = 0; i < numEnabledContexts; i++) {
pContext = ppAllContexts[i];
pRCAP = RecordFindClientOnContext(pContext, client->clientAsMask, NULL);
if (pRCAP && pRCAP->pRequestMajorOpSet &&
RecordIsMemberOfSet(pRCAP->pRequestMajorOpSet, majorop)) {
if (majorop <= 127) { /* core request */
if (stuff->length == 0)
RecordABigRequest(pContext, client, stuff);
else
RecordAProtocolElement(pContext, client, XRecordFromClient,
(pointer) stuff,
client->req_len << 2, 0, 0);
}
else { /* extension, check minor opcode */
int minorop = client->minorOp;
int numMinOpInfo;
RecordMinorOpPtr pMinorOpInfo = pRCAP->pRequestMinOpInfo;
assert(pMinorOpInfo);
numMinOpInfo = pMinorOpInfo->count;
pMinorOpInfo++;
assert(numMinOpInfo);
for (; numMinOpInfo; numMinOpInfo--, pMinorOpInfo++) {
if (majorop >= pMinorOpInfo->major.first &&
majorop <= pMinorOpInfo->major.last &&
RecordIsMemberOfSet(pMinorOpInfo->major.pMinOpSet,
minorop)) {
if (stuff->length == 0)
RecordABigRequest(pContext, client, stuff);
else
RecordAProtocolElement(pContext, client,
XRecordFromClient,
(pointer) stuff,
client->req_len << 2, 0, 0);
break;
}
} /* end for each minor op info */
} /* end extension request */
} /* end this RCAP wants this major opcode */
} /* end for each context */
pClientPriv = RecordClientPrivate(client);
assert(pClientPriv);
return (*pClientPriv->originalVector[majorop]) (client);
} /* RecordARequest */
/* RecordAReply
*
* Arguments:
* pcbl is &ReplyCallback.
* nulldata is NULL.
* calldata is a pointer to a ReplyInfoRec (include/os.h)
* which provides information about replies that are being sent
* to clients.
*
* Returns: nothing.
*
* Side Effects:
* The reply is recorded by all contexts that have registered this
* reply type for this client. If more data belonging to the same
* reply is expected, and if the reply is being recorded by any
* context, pContext->continuedReply is set to 1.
* If pContext->continuedReply was already 1 and this is the last
* chunk of data belonging to this reply, it is set to 0.
*/
static void
RecordAReply(CallbackListPtr *pcbl, pointer nulldata, pointer calldata)
{
RecordContextPtr pContext;
RecordClientsAndProtocolPtr pRCAP;
int eci;
ReplyInfoRec *pri = (ReplyInfoRec *) calldata;
ClientPtr client = pri->client;
for (eci = 0; eci < numEnabledContexts; eci++) {
pContext = ppAllContexts[eci];
pRCAP = RecordFindClientOnContext(pContext, client->clientAsMask, NULL);
if (pRCAP) {
int majorop = client->majorOp;
if (pContext->continuedReply) {
RecordAProtocolElement(pContext, client, XRecordFromServer,
(pointer) pri->replyData,
pri->dataLenBytes, pri->padBytes,
/* continuation */ -1);
if (!pri->bytesRemaining)
pContext->continuedReply = 0;
}
else if (pri->startOfReply && pRCAP->pReplyMajorOpSet &&
RecordIsMemberOfSet(pRCAP->pReplyMajorOpSet, majorop)) {
if (majorop <= 127) { /* core reply */
RecordAProtocolElement(pContext, client, XRecordFromServer,
(pointer) pri->replyData,
pri->dataLenBytes, 0,
pri->bytesRemaining);
if (pri->bytesRemaining)
pContext->continuedReply = 1;
}
else { /* extension, check minor opcode */
int minorop = client->minorOp;
int numMinOpInfo;
RecordMinorOpPtr pMinorOpInfo = pRCAP->pReplyMinOpInfo;
assert(pMinorOpInfo);
numMinOpInfo = pMinorOpInfo->count;
pMinorOpInfo++;
assert(numMinOpInfo);
for (; numMinOpInfo; numMinOpInfo--, pMinorOpInfo++) {
if (majorop >= pMinorOpInfo->major.first &&
majorop <= pMinorOpInfo->major.last &&
RecordIsMemberOfSet(pMinorOpInfo->major.pMinOpSet,
minorop)) {
RecordAProtocolElement(pContext, client,
XRecordFromServer,
(pointer) pri->replyData,
pri->dataLenBytes, 0,
pri->bytesRemaining);
if (pri->bytesRemaining)
pContext->continuedReply = 1;
break;
}
} /* end for each minor op info */
} /* end extension reply */
} /* end continued reply vs. start of reply */
} /* end client is registered on this context */
} /* end for each context */
} /* RecordAReply */
/* RecordADeliveredEventOrError
*
* Arguments:
* pcbl is &EventCallback.
* nulldata is NULL.
* calldata is a pointer to a EventInfoRec (include/dix.h)
* which provides information about events that are being sent
* to clients.
*
* Returns: nothing.
*
* Side Effects:
* The event or error is recorded by all contexts that have registered
* it for this client.
*/
static void
RecordADeliveredEventOrError(CallbackListPtr *pcbl, pointer nulldata,
pointer calldata)
{
EventInfoRec *pei = (EventInfoRec *) calldata;
RecordContextPtr pContext;
RecordClientsAndProtocolPtr pRCAP;
int eci; /* enabled context index */
ClientPtr pClient = pei->client;
for (eci = 0; eci < numEnabledContexts; eci++) {
pContext = ppAllContexts[eci];
pRCAP = RecordFindClientOnContext(pContext, pClient->clientAsMask,
NULL);
if (pRCAP && (pRCAP->pDeliveredEventSet || pRCAP->pErrorSet)) {
int ev; /* event index */
xEvent *pev = pei->events;
for (ev = 0; ev < pei->count; ev++, pev++) {
int recordit = 0;
if (pRCAP->pErrorSet) {
recordit = RecordIsMemberOfSet(pRCAP->pErrorSet,
((xError *) (pev))->
errorCode);
}
else if (pRCAP->pDeliveredEventSet) {
recordit = RecordIsMemberOfSet(pRCAP->pDeliveredEventSet,
pev->u.u.type & 0177);
}
if (recordit) {
xEvent swappedEvent;
xEvent *pEvToRecord = pev;
if (pClient->swapped) {
(*EventSwapVector[pev->u.u.type & 0177])
(pev, &swappedEvent);
pEvToRecord = &swappedEvent;
}
RecordAProtocolElement(pContext, pClient,
XRecordFromServer, pEvToRecord,
SIZEOF(xEvent), 0, 0);
}
} /* end for each event */
} /* end this client is on this context */
} /* end for each enabled context */
} /* RecordADeliveredEventOrError */
static void
RecordSendProtocolEvents(RecordClientsAndProtocolPtr pRCAP,
RecordContextPtr pContext, xEvent *pev, int count)
{
int ev; /* event index */
for (ev = 0; ev < count; ev++, pev++) {
if (RecordIsMemberOfSet(pRCAP->pDeviceEventSet, pev->u.u.type & 0177)) {
xEvent swappedEvent;
xEvent *pEvToRecord = pev;
#ifdef PANORAMIX
xEvent shiftedEvent;
if (!noPanoramiXExtension &&
(pev->u.u.type == MotionNotify ||
pev->u.u.type == ButtonPress ||
pev->u.u.type == ButtonRelease ||
pev->u.u.type == KeyPress || pev->u.u.type == KeyRelease)) {
int scr = XineramaGetCursorScreen(inputInfo.pointer);
memcpy(&shiftedEvent, pev, sizeof(xEvent));
shiftedEvent.u.keyButtonPointer.rootX +=
screenInfo.screens[scr]->x - screenInfo.screens[0]->x;
shiftedEvent.u.keyButtonPointer.rootY +=
screenInfo.screens[scr]->y - screenInfo.screens[0]->y;
pEvToRecord = &shiftedEvent;
}
#endif /* PANORAMIX */
if (pContext->pRecordingClient->swapped) {
(*EventSwapVector[pEvToRecord->u.u.type & 0177])
(pEvToRecord, &swappedEvent);
pEvToRecord = &swappedEvent;
}
RecordAProtocolElement(pContext, NULL,
XRecordFromServer, pEvToRecord,
SIZEOF(xEvent), 0, 0);
/* make sure device events get flushed in the absence
* of other client activity
*/
SetCriticalOutputPending();
}
} /* end for each event */
} /* RecordADeviceEvent */
/* RecordADeviceEvent
*
* Arguments:
* pcbl is &DeviceEventCallback.
* nulldata is NULL.
* calldata is a pointer to a DeviceEventInfoRec (include/dix.h)
* which provides information about device events that occur.
*
* Returns: nothing.
*
* Side Effects:
* The device event is recorded by all contexts that have registered
* it for this client.
*/
static void
RecordADeviceEvent(CallbackListPtr *pcbl, pointer nulldata, pointer calldata)
{
DeviceEventInfoRec *pei = (DeviceEventInfoRec *) calldata;
RecordContextPtr pContext;
RecordClientsAndProtocolPtr pRCAP;
int eci; /* enabled context index */
for (eci = 0; eci < numEnabledContexts; eci++) {
pContext = ppAllContexts[eci];
for (pRCAP = pContext->pListOfRCAP; pRCAP; pRCAP = pRCAP->pNextRCAP) {
if (pRCAP->pDeviceEventSet) {
int count;
xEvent *xi_events = NULL;
/* TODO check return values */
if (IsMaster(pei->device)) {
xEvent *core_events;
EventToCore(pei->event, &core_events, &count);
RecordSendProtocolEvents(pRCAP, pContext, core_events,
count);
free(core_events);
}
EventToXI(pei->event, &xi_events, &count);
RecordSendProtocolEvents(pRCAP, pContext, xi_events, count);
free(xi_events);
} /* end this RCAP selects device events */
} /* end for each RCAP on this context */
} /* end for each enabled context */
}
/* RecordFlushAllContexts
*
* Arguments:
* pcbl is &FlushCallback.
* nulldata and calldata are NULL.
*
* Returns: nothing.
*
* Side Effects:
* All buffered reply data of all enabled contexts is written to
* the recording clients.
*/
static void
RecordFlushAllContexts(CallbackListPtr *pcbl,
pointer nulldata, pointer calldata)
{
int eci; /* enabled context index */
RecordContextPtr pContext;
for (eci = 0; eci < numEnabledContexts; eci++) {
pContext = ppAllContexts[eci];
/* In most cases we leave it to RecordFlushReplyBuffer to make
* this check, but this function could be called very often, so we
* check before calling hoping to save the function call cost
* most of the time.
*/
if (pContext->numBufBytes)
RecordFlushReplyBuffer(ppAllContexts[eci], NULL, 0, NULL, 0);
}
} /* RecordFlushAllContexts */
/* RecordInstallHooks
*
* Arguments:
* pRCAP is an RCAP on an enabled or being-enabled context.
* oneclient can be zero or the resource ID mask identifying a client.
*
* Returns: BadAlloc if a memory allocation error occurred, else Success.
*
* Side Effects:
* Recording hooks needed by RCAP are installed.
* If oneclient is zero, recording hooks needed for all clients and
* protocol on the RCAP are installed. If oneclient is non-zero,
* only those hooks needed for the specified client are installed.
*
* Client requestVectors may be altered. numEnabledRCAPs will be
* incremented if oneclient == 0. Callbacks may be added to
* various callback lists.
*/
static int
RecordInstallHooks(RecordClientsAndProtocolPtr pRCAP, XID oneclient)
{
int i = 0;
XID client;
if (oneclient)
client = oneclient;
else
client = pRCAP->numClients ? pRCAP->pClientIDs[i++] : 0;
while (client) {
if (client != XRecordFutureClients) {
if (pRCAP->pRequestMajorOpSet) {
RecordSetIteratePtr pIter = NULL;
RecordSetInterval interval;
ClientPtr pClient = clients[CLIENT_ID(client)];
if (pClient && !RecordClientPrivate(pClient)) {
RecordClientPrivatePtr pClientPriv;
/* no Record proc vector; allocate one */
pClientPriv = (RecordClientPrivatePtr)
malloc(sizeof(RecordClientPrivateRec));
if (!pClientPriv)
return BadAlloc;
/* copy old proc vector to new */
memcpy(pClientPriv->recordVector, pClient->requestVector,
sizeof(pClientPriv->recordVector));
pClientPriv->originalVector = pClient->requestVector;
dixSetPrivate(&pClient->devPrivates,
RecordClientPrivateKey, pClientPriv);
pClient->requestVector = pClientPriv->recordVector;
}
while ((pIter = RecordIterateSet(pRCAP->pRequestMajorOpSet,
pIter, &interval))) {
unsigned int j;
for (j = interval.first; j <= interval.last; j++)
pClient->requestVector[j] = RecordARequest;
}
}
}
if (oneclient)
client = 0;
else
client = (i < pRCAP->numClients) ? pRCAP->pClientIDs[i++] : 0;
}
assert(numEnabledRCAPs >= 0);
if (!oneclient && ++numEnabledRCAPs == 1) { /* we're enabling the first context */
if (!AddCallback(&EventCallback, RecordADeliveredEventOrError, NULL))
return BadAlloc;
if (!AddCallback(&DeviceEventCallback, RecordADeviceEvent, NULL))
return BadAlloc;
if (!AddCallback(&ReplyCallback, RecordAReply, NULL))
return BadAlloc;
if (!AddCallback(&FlushCallback, RecordFlushAllContexts, NULL))
return BadAlloc;
/* Alternate context flushing scheme: delete the line above
* and call RegisterBlockAndWakeupHandlers here passing
* RecordFlushAllContexts. Is this any better?
*/
}
return Success;
} /* RecordInstallHooks */
/* RecordUninstallHooks
*
* Arguments:
* pRCAP is an RCAP on an enabled or being-disabled context.
* oneclient can be zero or the resource ID mask identifying a client.
*
* Returns: nothing.
*
* Side Effects:
* Recording hooks needed by RCAP may be uninstalled.
* If oneclient is zero, recording hooks needed for all clients and
* protocol on the RCAP may be uninstalled. If oneclient is non-zero,
* only those hooks needed for the specified client may be uninstalled.
*
* Client requestVectors may be altered. numEnabledRCAPs will be
* decremented if oneclient == 0. Callbacks may be deleted from
* various callback lists.
*/
static void
RecordUninstallHooks(RecordClientsAndProtocolPtr pRCAP, XID oneclient)
{
int i = 0;
XID client;
if (oneclient)
client = oneclient;
else
client = pRCAP->numClients ? pRCAP->pClientIDs[i++] : 0;
while (client) {
if (client != XRecordFutureClients) {
if (pRCAP->pRequestMajorOpSet) {
ClientPtr pClient = clients[CLIENT_ID(client)];
int c;
Bool otherRCAPwantsProcVector = FALSE;
RecordClientPrivatePtr pClientPriv = NULL;
assert(pClient);
pClientPriv = RecordClientPrivate(pClient);
assert(pClientPriv);
memcpy(pClientPriv->recordVector, pClientPriv->originalVector,
sizeof(pClientPriv->recordVector));
for (c = 0; c < numEnabledContexts; c++) {
RecordClientsAndProtocolPtr pOtherRCAP;
RecordContextPtr pContext = ppAllContexts[c];
if (pContext == pRCAP->pContext)
continue;
pOtherRCAP = RecordFindClientOnContext(pContext, client,
NULL);
if (pOtherRCAP && pOtherRCAP->pRequestMajorOpSet) {
RecordSetIteratePtr pIter = NULL;
RecordSetInterval interval;
otherRCAPwantsProcVector = TRUE;
while ((pIter =
RecordIterateSet(pOtherRCAP->pRequestMajorOpSet,
pIter, &interval))) {
unsigned int j;
for (j = interval.first; j <= interval.last; j++)
pClient->requestVector[j] = RecordARequest;
}
}
}
if (!otherRCAPwantsProcVector) { /* nobody needs it, so free it */
pClient->requestVector = pClientPriv->originalVector;
dixSetPrivate(&pClient->devPrivates,
RecordClientPrivateKey, NULL);
free(pClientPriv);
}
} /* end if this RCAP specifies any requests */
} /* end if not future clients */
if (oneclient)
client = 0;
else
client = (i < pRCAP->numClients) ? pRCAP->pClientIDs[i++] : 0;
}
assert(numEnabledRCAPs >= 1);
if (!oneclient && --numEnabledRCAPs == 0) { /* we're disabling the last context */
DeleteCallback(&EventCallback, RecordADeliveredEventOrError, NULL);