xref: /OK3568_Linux_fs/external/xserver/record/record.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun 
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun Copyright 1995, 1998  The Open Group
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun Permission to use, copy, modify, distribute, and sell this software and its
7*4882a593Smuzhiyun documentation for any purpose is hereby granted without fee, provided that
8*4882a593Smuzhiyun the above copyright notice appear in all copies and that both that
9*4882a593Smuzhiyun copyright notice and this permission notice appear in supporting
10*4882a593Smuzhiyun documentation.
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun The above copyright notice and this permission notice shall be
13*4882a593Smuzhiyun included in all copies or substantial portions of the Software.
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*4882a593Smuzhiyun EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*4882a593Smuzhiyun MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18*4882a593Smuzhiyun IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
19*4882a593Smuzhiyun OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20*4882a593Smuzhiyun ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21*4882a593Smuzhiyun OTHER DEALINGS IN THE SOFTWARE.
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun Except as contained in this notice, the name of The Open Group shall
24*4882a593Smuzhiyun not be used in advertising or otherwise to promote the sale, use or
25*4882a593Smuzhiyun other dealings in this Software without prior written authorization
26*4882a593Smuzhiyun from The Open Group.
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun Author: David P. Wiggins, The Open Group
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun This work benefited from earlier work done by Martha Zimet of NCD
31*4882a593Smuzhiyun and Jim Haggerty of Metheus.
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun */
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #ifdef HAVE_DIX_CONFIG_H
36*4882a593Smuzhiyun #include <dix-config.h>
37*4882a593Smuzhiyun #endif
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #include "dixstruct.h"
40*4882a593Smuzhiyun #include "extnsionst.h"
41*4882a593Smuzhiyun #include "extinit.h"
42*4882a593Smuzhiyun #include <X11/extensions/recordproto.h>
43*4882a593Smuzhiyun #include "set.h"
44*4882a593Smuzhiyun #include "swaprep.h"
45*4882a593Smuzhiyun #include "inputstr.h"
46*4882a593Smuzhiyun #include "eventconvert.h"
47*4882a593Smuzhiyun #include "scrnintstr.h"
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun #include <stdio.h>
50*4882a593Smuzhiyun #include <assert.h>
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun #ifdef PANORAMIX
53*4882a593Smuzhiyun #include "globals.h"
54*4882a593Smuzhiyun #include "panoramiX.h"
55*4882a593Smuzhiyun #include "panoramiXsrv.h"
56*4882a593Smuzhiyun #include "cursor.h"
57*4882a593Smuzhiyun #endif
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun #include "protocol-versions.h"
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun static RESTYPE RTContext;       /* internal resource type for Record contexts */
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun /* How many bytes of protocol data to buffer in a context. Don't set to less
64*4882a593Smuzhiyun  * than 32.
65*4882a593Smuzhiyun  */
66*4882a593Smuzhiyun #define REPLY_BUF_SIZE 1024
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun /* Record Context structure */
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun typedef struct {
71*4882a593Smuzhiyun     XID id;                     /* resource id of context */
72*4882a593Smuzhiyun     ClientPtr pRecordingClient; /* client that has context enabled */
73*4882a593Smuzhiyun     struct _RecordClientsAndProtocolRec *pListOfRCAP;   /* all registered info */
74*4882a593Smuzhiyun     ClientPtr pBufClient;       /* client whose protocol is in replyBuffer */
75*4882a593Smuzhiyun     unsigned int continuedReply:1;      /* recording a reply that is split up? */
76*4882a593Smuzhiyun     char elemHeaders;           /* element header flags (time/seq no.) */
77*4882a593Smuzhiyun     char bufCategory;           /* category of protocol in replyBuffer */
78*4882a593Smuzhiyun     int numBufBytes;            /* number of bytes in replyBuffer */
79*4882a593Smuzhiyun     char replyBuffer[REPLY_BUF_SIZE];   /* buffered recorded protocol */
80*4882a593Smuzhiyun     int inFlush;                /*  are we inside RecordFlushReplyBuffer */
81*4882a593Smuzhiyun } RecordContextRec, *RecordContextPtr;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun /*  RecordMinorOpRec - to hold minor opcode selections for extension requests
84*4882a593Smuzhiyun  *  and replies
85*4882a593Smuzhiyun  */
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun typedef union {
88*4882a593Smuzhiyun     int count;                  /* first element of array: how many "major" structs to follow */
89*4882a593Smuzhiyun     struct {                    /* rest of array elements are this */
90*4882a593Smuzhiyun         short first;            /* first major opcode */
91*4882a593Smuzhiyun         short last;             /* last major opcode */
92*4882a593Smuzhiyun         RecordSetPtr pMinOpSet; /*  minor opcode set for above major range */
93*4882a593Smuzhiyun     } major;
94*4882a593Smuzhiyun } RecordMinorOpRec, *RecordMinorOpPtr;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun /*  RecordClientsAndProtocolRec, nicknamed RCAP - holds all the client and
97*4882a593Smuzhiyun  *  protocol selections passed in a single CreateContext or RegisterClients.
98*4882a593Smuzhiyun  *  Generally, a context will have one of these from the create and an
99*4882a593Smuzhiyun  *  additional one for each RegisterClients.  RCAPs are freed when all their
100*4882a593Smuzhiyun  *  clients are unregistered.
101*4882a593Smuzhiyun  */
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun typedef struct _RecordClientsAndProtocolRec {
104*4882a593Smuzhiyun     RecordContextPtr pContext;  /* context that owns this RCAP */
105*4882a593Smuzhiyun     struct _RecordClientsAndProtocolRec *pNextRCAP;     /* next RCAP on context */
106*4882a593Smuzhiyun     RecordSetPtr pRequestMajorOpSet;    /* requests to record */
107*4882a593Smuzhiyun     RecordMinorOpPtr pRequestMinOpInfo; /* extension requests to record */
108*4882a593Smuzhiyun     RecordSetPtr pReplyMajorOpSet;      /* replies to record */
109*4882a593Smuzhiyun     RecordMinorOpPtr pReplyMinOpInfo;   /* extension replies to record */
110*4882a593Smuzhiyun     RecordSetPtr pDeviceEventSet;       /* device events to record */
111*4882a593Smuzhiyun     RecordSetPtr pDeliveredEventSet;    /* delivered events to record */
112*4882a593Smuzhiyun     RecordSetPtr pErrorSet;     /* errors to record */
113*4882a593Smuzhiyun     XID *pClientIDs;            /* array of clients to record */
114*4882a593Smuzhiyun     short numClients;           /* number of clients in pClientIDs */
115*4882a593Smuzhiyun     short sizeClients;          /* size of pClientIDs array */
116*4882a593Smuzhiyun     unsigned int clientStarted:1;       /* record new client connections? */
117*4882a593Smuzhiyun     unsigned int clientDied:1;  /* record client disconnections? */
118*4882a593Smuzhiyun     unsigned int clientIDsSeparatelyAllocated:1;        /* pClientIDs malloced? */
119*4882a593Smuzhiyun } RecordClientsAndProtocolRec, *RecordClientsAndProtocolPtr;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun /* how much bigger to make pRCAP->pClientIDs when reallocing */
122*4882a593Smuzhiyun #define CLIENT_ARRAY_GROWTH_INCREMENT 4
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun /* counts the total number of RCAPs belonging to enabled contexts. */
125*4882a593Smuzhiyun static int numEnabledRCAPs;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun /*  void VERIFY_CONTEXT(RecordContextPtr, XID, ClientPtr)
128*4882a593Smuzhiyun  *  In the spirit of the VERIFY_* macros in dix.h, this macro fills in
129*4882a593Smuzhiyun  *  the context pointer if the given ID is a valid Record Context, else it
130*4882a593Smuzhiyun  *  returns an error.
131*4882a593Smuzhiyun  */
132*4882a593Smuzhiyun #define VERIFY_CONTEXT(_pContext, _contextid, _client) { \
133*4882a593Smuzhiyun     int rc = dixLookupResourceByType((void **)&(_pContext), _contextid, \
134*4882a593Smuzhiyun                                      RTContext, _client, DixUseAccess); \
135*4882a593Smuzhiyun     if (rc != Success) \
136*4882a593Smuzhiyun 	return rc; \
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun static int RecordDeleteContext(void     *value,
140*4882a593Smuzhiyun                                XID      id);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun /***************************************************************************/
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun /* client private stuff */
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun /*  To make declarations less obfuscated, have a typedef for a pointer to a
147*4882a593Smuzhiyun  *  Proc function.
148*4882a593Smuzhiyun  */
149*4882a593Smuzhiyun typedef int (*ProcFunctionPtr) (ClientPtr       /*pClient */
150*4882a593Smuzhiyun     );
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun /* Record client private.  Generally a client only has one of these if
153*4882a593Smuzhiyun  * any of its requests are being recorded.
154*4882a593Smuzhiyun  */
155*4882a593Smuzhiyun typedef struct {
156*4882a593Smuzhiyun /* ptr to client's proc vector before Record stuck its nose in */
157*4882a593Smuzhiyun     ProcFunctionPtr *originalVector;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun /* proc vector with pointers for recorded requests redirected to the
160*4882a593Smuzhiyun  * function RecordARequest
161*4882a593Smuzhiyun  */
162*4882a593Smuzhiyun     ProcFunctionPtr recordVector[256];
163*4882a593Smuzhiyun } RecordClientPrivateRec, *RecordClientPrivatePtr;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun static DevPrivateKeyRec RecordClientPrivateKeyRec;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun #define RecordClientPrivateKey (&RecordClientPrivateKeyRec)
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun /*  RecordClientPrivatePtr RecordClientPrivate(ClientPtr)
170*4882a593Smuzhiyun  *  gets the client private of the given client.  Syntactic sugar.
171*4882a593Smuzhiyun  */
172*4882a593Smuzhiyun #define RecordClientPrivate(_pClient) (RecordClientPrivatePtr) \
173*4882a593Smuzhiyun     dixLookupPrivate(&(_pClient)->devPrivates, RecordClientPrivateKey)
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun /***************************************************************************/
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun /* global list of all contexts */
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun static RecordContextPtr *ppAllContexts;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun static int numContexts;         /* number of contexts in ppAllContexts */
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun /* number of currently enabled contexts.  All enabled contexts are bunched
184*4882a593Smuzhiyun  * up at the front of the ppAllContexts array, from ppAllContexts[0] to
185*4882a593Smuzhiyun  * ppAllContexts[numEnabledContexts-1], to eliminate time spent skipping
186*4882a593Smuzhiyun  * past disabled contexts.
187*4882a593Smuzhiyun  */
188*4882a593Smuzhiyun static int numEnabledContexts;
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun /* RecordFindContextOnAllContexts
191*4882a593Smuzhiyun  *
192*4882a593Smuzhiyun  * Arguments:
193*4882a593Smuzhiyun  *	pContext is the context to search for.
194*4882a593Smuzhiyun  *
195*4882a593Smuzhiyun  * Returns:
196*4882a593Smuzhiyun  *	The index into the array ppAllContexts at which pContext is stored.
197*4882a593Smuzhiyun  *	If pContext is not found in ppAllContexts, returns -1.
198*4882a593Smuzhiyun  *
199*4882a593Smuzhiyun  * Side Effects: none.
200*4882a593Smuzhiyun  */
201*4882a593Smuzhiyun static int
RecordFindContextOnAllContexts(RecordContextPtr pContext)202*4882a593Smuzhiyun RecordFindContextOnAllContexts(RecordContextPtr pContext)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun     int i;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun     assert(numContexts >= numEnabledContexts);
207*4882a593Smuzhiyun     for (i = 0; i < numContexts; i++) {
208*4882a593Smuzhiyun         if (ppAllContexts[i] == pContext)
209*4882a593Smuzhiyun             return i;
210*4882a593Smuzhiyun     }
211*4882a593Smuzhiyun     return -1;
212*4882a593Smuzhiyun }                               /* RecordFindContextOnAllContexts */
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun /***************************************************************************/
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun /* RecordFlushReplyBuffer
217*4882a593Smuzhiyun  *
218*4882a593Smuzhiyun  * Arguments:
219*4882a593Smuzhiyun  *	pContext is the context to flush.
220*4882a593Smuzhiyun  *	data1 is a pointer to additional data, and len1 is its length in bytes.
221*4882a593Smuzhiyun  *	data2 is a pointer to additional data, and len2 is its length in bytes.
222*4882a593Smuzhiyun  *
223*4882a593Smuzhiyun  * Returns: nothing.
224*4882a593Smuzhiyun  *
225*4882a593Smuzhiyun  * Side Effects:
226*4882a593Smuzhiyun  *	If the context is enabled, any buffered (recorded) protocol is written
227*4882a593Smuzhiyun  *	to the recording client, and the number of buffered bytes is set to
228*4882a593Smuzhiyun  *	zero.  If len1 is not zero, data1/len1 are then written to the
229*4882a593Smuzhiyun  *	recording client, and similarly for data2/len2 (written after
230*4882a593Smuzhiyun  *	data1/len1).
231*4882a593Smuzhiyun  */
232*4882a593Smuzhiyun static void
RecordFlushReplyBuffer(RecordContextPtr pContext,void * data1,int len1,void * data2,int len2)233*4882a593Smuzhiyun RecordFlushReplyBuffer(RecordContextPtr pContext,
234*4882a593Smuzhiyun                        void *data1, int len1, void *data2, int len2)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun     if (!pContext->pRecordingClient || pContext->pRecordingClient->clientGone ||
237*4882a593Smuzhiyun         pContext->inFlush)
238*4882a593Smuzhiyun         return;
239*4882a593Smuzhiyun     ++pContext->inFlush;
240*4882a593Smuzhiyun     if (pContext->numBufBytes)
241*4882a593Smuzhiyun         WriteToClient(pContext->pRecordingClient, pContext->numBufBytes,
242*4882a593Smuzhiyun                       pContext->replyBuffer);
243*4882a593Smuzhiyun     pContext->numBufBytes = 0;
244*4882a593Smuzhiyun     if (len1)
245*4882a593Smuzhiyun         WriteToClient(pContext->pRecordingClient, len1, data1);
246*4882a593Smuzhiyun     if (len2)
247*4882a593Smuzhiyun         WriteToClient(pContext->pRecordingClient, len2, data2);
248*4882a593Smuzhiyun     --pContext->inFlush;
249*4882a593Smuzhiyun }                               /* RecordFlushReplyBuffer */
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun /* RecordAProtocolElement
252*4882a593Smuzhiyun  *
253*4882a593Smuzhiyun  * Arguments:
254*4882a593Smuzhiyun  *	pContext is the context that is recording a protocol element.
255*4882a593Smuzhiyun  *	pClient is the client whose protocol is being recorded.  For
256*4882a593Smuzhiyun  *	  device events and EndOfData, pClient is NULL.
257*4882a593Smuzhiyun  *	category is the category of the protocol element, as defined
258*4882a593Smuzhiyun  *	  by the RECORD spec.
259*4882a593Smuzhiyun  *	data is a pointer to the protocol data, and datalen - padlen
260*4882a593Smuzhiyun  *	  is its length in bytes.
261*4882a593Smuzhiyun  *	padlen is the number of pad bytes from a zeroed array.
262*4882a593Smuzhiyun  *	futurelen is the number of bytes that will be sent in subsequent
263*4882a593Smuzhiyun  *	  calls to this function to complete this protocol element.
264*4882a593Smuzhiyun  *	  In those subsequent calls, futurelen will be -1 to indicate
265*4882a593Smuzhiyun  *	  that the current data is a continuation of the same protocol
266*4882a593Smuzhiyun  *	  element.
267*4882a593Smuzhiyun  *
268*4882a593Smuzhiyun  * Returns: nothing.
269*4882a593Smuzhiyun  *
270*4882a593Smuzhiyun  * Side Effects:
271*4882a593Smuzhiyun  *	The context may be flushed.  The new protocol element will be
272*4882a593Smuzhiyun  *	added to the context's protocol buffer with appropriate element
273*4882a593Smuzhiyun  *	headers prepended (sequence number and timestamp).  If the data
274*4882a593Smuzhiyun  *	is continuation data (futurelen == -1), element headers won't
275*4882a593Smuzhiyun  *	be added.  If the protocol element and headers won't fit in
276*4882a593Smuzhiyun  *	the context's buffer, it is sent directly to the recording
277*4882a593Smuzhiyun  *	client (after any buffered data).
278*4882a593Smuzhiyun  */
279*4882a593Smuzhiyun static void
RecordAProtocolElement(RecordContextPtr pContext,ClientPtr pClient,int category,void * data,int datalen,int padlen,int futurelen)280*4882a593Smuzhiyun RecordAProtocolElement(RecordContextPtr pContext, ClientPtr pClient,
281*4882a593Smuzhiyun                        int category, void *data, int datalen, int padlen,
282*4882a593Smuzhiyun                        int futurelen)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun     CARD32 elemHeaderData[2];
285*4882a593Smuzhiyun     int numElemHeaders = 0;
286*4882a593Smuzhiyun     Bool recordingClientSwapped = pContext->pRecordingClient->swapped;
287*4882a593Smuzhiyun     CARD32 serverTime = 0;
288*4882a593Smuzhiyun     Bool gotServerTime = FALSE;
289*4882a593Smuzhiyun     int replylen;
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun     if (futurelen >= 0) {       /* start of new protocol element */
292*4882a593Smuzhiyun         xRecordEnableContextReply *pRep = (xRecordEnableContextReply *)
293*4882a593Smuzhiyun             pContext->replyBuffer;
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun         if (pContext->pBufClient != pClient ||
296*4882a593Smuzhiyun             pContext->bufCategory != category) {
297*4882a593Smuzhiyun             RecordFlushReplyBuffer(pContext, NULL, 0, NULL, 0);
298*4882a593Smuzhiyun             pContext->pBufClient = pClient;
299*4882a593Smuzhiyun             pContext->bufCategory = category;
300*4882a593Smuzhiyun         }
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun         if (!pContext->numBufBytes) {
303*4882a593Smuzhiyun             serverTime = GetTimeInMillis();
304*4882a593Smuzhiyun             gotServerTime = TRUE;
305*4882a593Smuzhiyun             pRep->type = X_Reply;
306*4882a593Smuzhiyun             pRep->category = category;
307*4882a593Smuzhiyun             pRep->sequenceNumber = pContext->pRecordingClient->sequence;
308*4882a593Smuzhiyun             pRep->length = 0;
309*4882a593Smuzhiyun             pRep->elementHeader = pContext->elemHeaders;
310*4882a593Smuzhiyun             pRep->serverTime = serverTime;
311*4882a593Smuzhiyun             if (pClient) {
312*4882a593Smuzhiyun                 pRep->clientSwapped =
313*4882a593Smuzhiyun                     (pClient->swapped != recordingClientSwapped);
314*4882a593Smuzhiyun                 pRep->idBase = pClient->clientAsMask;
315*4882a593Smuzhiyun                 pRep->recordedSequenceNumber = pClient->sequence;
316*4882a593Smuzhiyun             }
317*4882a593Smuzhiyun             else {              /* it's a device event, StartOfData, or EndOfData */
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun                 pRep->clientSwapped = (category != XRecordFromServer) &&
320*4882a593Smuzhiyun                     recordingClientSwapped;
321*4882a593Smuzhiyun                 pRep->idBase = 0;
322*4882a593Smuzhiyun                 pRep->recordedSequenceNumber = 0;
323*4882a593Smuzhiyun             }
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun             if (recordingClientSwapped) {
326*4882a593Smuzhiyun                 swaps(&pRep->sequenceNumber);
327*4882a593Smuzhiyun                 swapl(&pRep->length);
328*4882a593Smuzhiyun                 swapl(&pRep->idBase);
329*4882a593Smuzhiyun                 swapl(&pRep->serverTime);
330*4882a593Smuzhiyun                 swapl(&pRep->recordedSequenceNumber);
331*4882a593Smuzhiyun             }
332*4882a593Smuzhiyun             pContext->numBufBytes = SIZEOF(xRecordEnableContextReply);
333*4882a593Smuzhiyun         }
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun         /* generate element headers if needed */
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun         if (((pContext->elemHeaders & XRecordFromClientTime)
338*4882a593Smuzhiyun              && category == XRecordFromClient)
339*4882a593Smuzhiyun             || ((pContext->elemHeaders & XRecordFromServerTime)
340*4882a593Smuzhiyun                 && category == XRecordFromServer)) {
341*4882a593Smuzhiyun             if (gotServerTime)
342*4882a593Smuzhiyun                 elemHeaderData[numElemHeaders] = serverTime;
343*4882a593Smuzhiyun             else
344*4882a593Smuzhiyun                 elemHeaderData[numElemHeaders] = GetTimeInMillis();
345*4882a593Smuzhiyun             if (recordingClientSwapped)
346*4882a593Smuzhiyun                 swapl(&elemHeaderData[numElemHeaders]);
347*4882a593Smuzhiyun             numElemHeaders++;
348*4882a593Smuzhiyun         }
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun         if ((pContext->elemHeaders & XRecordFromClientSequence)
351*4882a593Smuzhiyun             && (category == XRecordFromClient || category == XRecordClientDied)) {
352*4882a593Smuzhiyun             elemHeaderData[numElemHeaders] = pClient->sequence;
353*4882a593Smuzhiyun             if (recordingClientSwapped)
354*4882a593Smuzhiyun                 swapl(&elemHeaderData[numElemHeaders]);
355*4882a593Smuzhiyun             numElemHeaders++;
356*4882a593Smuzhiyun         }
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun         /* adjust reply length */
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun         replylen = pRep->length;
361*4882a593Smuzhiyun         if (recordingClientSwapped)
362*4882a593Smuzhiyun             swapl(&replylen);
363*4882a593Smuzhiyun         replylen += numElemHeaders + bytes_to_int32(datalen) +
364*4882a593Smuzhiyun             bytes_to_int32(futurelen);
365*4882a593Smuzhiyun         if (recordingClientSwapped)
366*4882a593Smuzhiyun             swapl(&replylen);
367*4882a593Smuzhiyun         pRep->length = replylen;
368*4882a593Smuzhiyun     }                           /* end if not continued reply */
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun     numElemHeaders *= 4;
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun     /* if space available >= space needed, buffer the data */
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun     if (REPLY_BUF_SIZE - pContext->numBufBytes >= datalen + numElemHeaders) {
375*4882a593Smuzhiyun         if (numElemHeaders) {
376*4882a593Smuzhiyun             memcpy(pContext->replyBuffer + pContext->numBufBytes,
377*4882a593Smuzhiyun                    elemHeaderData, numElemHeaders);
378*4882a593Smuzhiyun             pContext->numBufBytes += numElemHeaders;
379*4882a593Smuzhiyun         }
380*4882a593Smuzhiyun         if (datalen) {
381*4882a593Smuzhiyun             static char padBuffer[3];   /* as in FlushClient */
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun             memcpy(pContext->replyBuffer + pContext->numBufBytes,
384*4882a593Smuzhiyun                    data, datalen - padlen);
385*4882a593Smuzhiyun             pContext->numBufBytes += datalen - padlen;
386*4882a593Smuzhiyun             memcpy(pContext->replyBuffer + pContext->numBufBytes,
387*4882a593Smuzhiyun                    padBuffer, padlen);
388*4882a593Smuzhiyun             pContext->numBufBytes += padlen;
389*4882a593Smuzhiyun         }
390*4882a593Smuzhiyun     }
391*4882a593Smuzhiyun     else {
392*4882a593Smuzhiyun         RecordFlushReplyBuffer(pContext, (void *) elemHeaderData,
393*4882a593Smuzhiyun                                numElemHeaders, (void *) data,
394*4882a593Smuzhiyun                                datalen - padlen);
395*4882a593Smuzhiyun     }
396*4882a593Smuzhiyun }                               /* RecordAProtocolElement */
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun /* RecordFindClientOnContext
399*4882a593Smuzhiyun  *
400*4882a593Smuzhiyun  * Arguments:
401*4882a593Smuzhiyun  *	pContext is the context to search.
402*4882a593Smuzhiyun  *	clientspec is the resource ID mask identifying the client to search
403*4882a593Smuzhiyun  *	  for, or XRecordFutureClients.
404*4882a593Smuzhiyun  *	pposition is a pointer to an int, or NULL.  See Returns.
405*4882a593Smuzhiyun  *
406*4882a593Smuzhiyun  * Returns:
407*4882a593Smuzhiyun  *	The RCAP on which clientspec was found, or NULL if not found on
408*4882a593Smuzhiyun  *	any RCAP on the given context.
409*4882a593Smuzhiyun  *	If pposition was not NULL and the returned RCAP is not NULL,
410*4882a593Smuzhiyun  *	*pposition will be set to the index into the returned the RCAP's
411*4882a593Smuzhiyun  *	pClientIDs array that holds clientspec.
412*4882a593Smuzhiyun  *
413*4882a593Smuzhiyun  * Side Effects: none.
414*4882a593Smuzhiyun  */
415*4882a593Smuzhiyun static RecordClientsAndProtocolPtr
RecordFindClientOnContext(RecordContextPtr pContext,XID clientspec,int * pposition)416*4882a593Smuzhiyun RecordFindClientOnContext(RecordContextPtr pContext,
417*4882a593Smuzhiyun                           XID clientspec, int *pposition)
418*4882a593Smuzhiyun {
419*4882a593Smuzhiyun     RecordClientsAndProtocolPtr pRCAP;
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun     for (pRCAP = pContext->pListOfRCAP; pRCAP; pRCAP = pRCAP->pNextRCAP) {
422*4882a593Smuzhiyun         int i;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun         for (i = 0; i < pRCAP->numClients; i++) {
425*4882a593Smuzhiyun             if (pRCAP->pClientIDs[i] == clientspec) {
426*4882a593Smuzhiyun                 if (pposition)
427*4882a593Smuzhiyun                     *pposition = i;
428*4882a593Smuzhiyun                 return pRCAP;
429*4882a593Smuzhiyun             }
430*4882a593Smuzhiyun         }
431*4882a593Smuzhiyun     }
432*4882a593Smuzhiyun     return NULL;
433*4882a593Smuzhiyun }                               /* RecordFindClientOnContext */
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun /* RecordABigRequest
436*4882a593Smuzhiyun  *
437*4882a593Smuzhiyun  * Arguments:
438*4882a593Smuzhiyun  *	pContext is the recording context.
439*4882a593Smuzhiyun  *	client is the client being recorded.
440*4882a593Smuzhiyun  *	stuff is a pointer to the big request of client (see the Big Requests
441*4882a593Smuzhiyun  *	extension for details.)
442*4882a593Smuzhiyun  *
443*4882a593Smuzhiyun  * Returns: nothing.
444*4882a593Smuzhiyun  *
445*4882a593Smuzhiyun  * Side Effects:
446*4882a593Smuzhiyun  *	The big request is recorded with the correct length field re-inserted.
447*4882a593Smuzhiyun  *
448*4882a593Smuzhiyun  * Note: this function exists mainly to make RecordARequest smaller.
449*4882a593Smuzhiyun  */
450*4882a593Smuzhiyun static void
RecordABigRequest(RecordContextPtr pContext,ClientPtr client,xReq * stuff)451*4882a593Smuzhiyun RecordABigRequest(RecordContextPtr pContext, ClientPtr client, xReq * stuff)
452*4882a593Smuzhiyun {
453*4882a593Smuzhiyun     CARD32 bigLength;
454*4882a593Smuzhiyun     int bytesLeft;
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun     /* note: client->req_len has been frobbed by ReadRequestFromClient
457*4882a593Smuzhiyun      * (os/io.c) to discount the extra 4 bytes taken by the extended length
458*4882a593Smuzhiyun      * field in a big request.  The actual request length to record is
459*4882a593Smuzhiyun      * client->req_len + 1 (measured in CARD32s).
460*4882a593Smuzhiyun      */
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun     /* record the request header */
463*4882a593Smuzhiyun     bytesLeft = client->req_len << 2;
464*4882a593Smuzhiyun     RecordAProtocolElement(pContext, client, XRecordFromClient,
465*4882a593Smuzhiyun                            (void *) stuff, SIZEOF(xReq), 0, bytesLeft);
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun     /* reinsert the extended length field that was squished out */
468*4882a593Smuzhiyun     bigLength = client->req_len + bytes_to_int32(sizeof(bigLength));
469*4882a593Smuzhiyun     if (client->swapped)
470*4882a593Smuzhiyun         swapl(&bigLength);
471*4882a593Smuzhiyun     RecordAProtocolElement(pContext, client, XRecordFromClient,
472*4882a593Smuzhiyun                            (void *) &bigLength, sizeof(bigLength), 0,
473*4882a593Smuzhiyun                            /* continuation */ -1);
474*4882a593Smuzhiyun     bytesLeft -= sizeof(bigLength);
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun     /* record the rest of the request after the length */
477*4882a593Smuzhiyun     RecordAProtocolElement(pContext, client, XRecordFromClient,
478*4882a593Smuzhiyun                            (void *) (stuff + 1), bytesLeft, 0,
479*4882a593Smuzhiyun                            /* continuation */ -1);
480*4882a593Smuzhiyun }                               /* RecordABigRequest */
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun /* RecordARequest
483*4882a593Smuzhiyun  *
484*4882a593Smuzhiyun  * Arguments:
485*4882a593Smuzhiyun  *	client is a client that the server has dispatched a request to by
486*4882a593Smuzhiyun  *	calling client->requestVector[request opcode] .
487*4882a593Smuzhiyun  *	The request is in client->requestBuffer.
488*4882a593Smuzhiyun  *
489*4882a593Smuzhiyun  * Returns:
490*4882a593Smuzhiyun  *	Whatever is returned by the "real" Proc function for this request.
491*4882a593Smuzhiyun  *	The "real" Proc function is the function that was in
492*4882a593Smuzhiyun  *	client->requestVector[request opcode]  before it was replaced by
493*4882a593Smuzhiyun  *	RecordARequest.  (See the function RecordInstallHooks.)
494*4882a593Smuzhiyun  *
495*4882a593Smuzhiyun  * Side Effects:
496*4882a593Smuzhiyun  *	The request is recorded by all contexts that have registered this
497*4882a593Smuzhiyun  *	request for this client.  The real Proc function is called.
498*4882a593Smuzhiyun  */
499*4882a593Smuzhiyun static int
RecordARequest(ClientPtr client)500*4882a593Smuzhiyun RecordARequest(ClientPtr client)
501*4882a593Smuzhiyun {
502*4882a593Smuzhiyun     RecordContextPtr pContext;
503*4882a593Smuzhiyun     RecordClientsAndProtocolPtr pRCAP;
504*4882a593Smuzhiyun     int i;
505*4882a593Smuzhiyun     RecordClientPrivatePtr pClientPriv;
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun     REQUEST(xReq);
508*4882a593Smuzhiyun     int majorop;
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun     majorop = stuff->reqType;
511*4882a593Smuzhiyun     for (i = 0; i < numEnabledContexts; i++) {
512*4882a593Smuzhiyun         pContext = ppAllContexts[i];
513*4882a593Smuzhiyun         pRCAP = RecordFindClientOnContext(pContext, client->clientAsMask, NULL);
514*4882a593Smuzhiyun         if (pRCAP && pRCAP->pRequestMajorOpSet &&
515*4882a593Smuzhiyun             RecordIsMemberOfSet(pRCAP->pRequestMajorOpSet, majorop)) {
516*4882a593Smuzhiyun             if (majorop <= 127) {       /* core request */
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun                 if (stuff->length == 0)
519*4882a593Smuzhiyun                     RecordABigRequest(pContext, client, stuff);
520*4882a593Smuzhiyun                 else
521*4882a593Smuzhiyun                     RecordAProtocolElement(pContext, client, XRecordFromClient,
522*4882a593Smuzhiyun                                            (void *) stuff,
523*4882a593Smuzhiyun                                            client->req_len << 2, 0, 0);
524*4882a593Smuzhiyun             }
525*4882a593Smuzhiyun             else {              /* extension, check minor opcode */
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun                 int minorop = client->minorOp;
528*4882a593Smuzhiyun                 int numMinOpInfo;
529*4882a593Smuzhiyun                 RecordMinorOpPtr pMinorOpInfo = pRCAP->pRequestMinOpInfo;
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun                 assert(pMinorOpInfo);
532*4882a593Smuzhiyun                 numMinOpInfo = pMinorOpInfo->count;
533*4882a593Smuzhiyun                 pMinorOpInfo++;
534*4882a593Smuzhiyun                 assert(numMinOpInfo);
535*4882a593Smuzhiyun                 for (; numMinOpInfo; numMinOpInfo--, pMinorOpInfo++) {
536*4882a593Smuzhiyun                     if (majorop >= pMinorOpInfo->major.first &&
537*4882a593Smuzhiyun                         majorop <= pMinorOpInfo->major.last &&
538*4882a593Smuzhiyun                         RecordIsMemberOfSet(pMinorOpInfo->major.pMinOpSet,
539*4882a593Smuzhiyun                                             minorop)) {
540*4882a593Smuzhiyun                         if (stuff->length == 0)
541*4882a593Smuzhiyun                             RecordABigRequest(pContext, client, stuff);
542*4882a593Smuzhiyun                         else
543*4882a593Smuzhiyun                             RecordAProtocolElement(pContext, client,
544*4882a593Smuzhiyun                                                    XRecordFromClient,
545*4882a593Smuzhiyun                                                    (void *) stuff,
546*4882a593Smuzhiyun                                                    client->req_len << 2, 0, 0);
547*4882a593Smuzhiyun                         break;
548*4882a593Smuzhiyun                     }
549*4882a593Smuzhiyun                 }               /* end for each minor op info */
550*4882a593Smuzhiyun             }                   /* end extension request */
551*4882a593Smuzhiyun         }                       /* end this RCAP wants this major opcode */
552*4882a593Smuzhiyun     }                           /* end for each context */
553*4882a593Smuzhiyun     pClientPriv = RecordClientPrivate(client);
554*4882a593Smuzhiyun     assert(pClientPriv);
555*4882a593Smuzhiyun     return (*pClientPriv->originalVector[majorop]) (client);
556*4882a593Smuzhiyun }                               /* RecordARequest */
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun /* RecordAReply
559*4882a593Smuzhiyun  *
560*4882a593Smuzhiyun  * Arguments:
561*4882a593Smuzhiyun  *	pcbl is &ReplyCallback.
562*4882a593Smuzhiyun  *	nulldata is NULL.
563*4882a593Smuzhiyun  *	calldata is a pointer to a ReplyInfoRec (include/os.h)
564*4882a593Smuzhiyun  *	  which provides information about replies that are being sent
565*4882a593Smuzhiyun  *	  to clients.
566*4882a593Smuzhiyun  *
567*4882a593Smuzhiyun  * Returns: nothing.
568*4882a593Smuzhiyun  *
569*4882a593Smuzhiyun  * Side Effects:
570*4882a593Smuzhiyun  *	The reply is recorded by all contexts that have registered this
571*4882a593Smuzhiyun  *	reply type for this client.  If more data belonging to the same
572*4882a593Smuzhiyun  *	reply is expected, and if the reply is being recorded by any
573*4882a593Smuzhiyun  *	context, pContext->continuedReply is set to 1.
574*4882a593Smuzhiyun  *	If pContext->continuedReply was already 1 and this is the last
575*4882a593Smuzhiyun  *	chunk of data belonging to this reply, it is set to 0.
576*4882a593Smuzhiyun  */
577*4882a593Smuzhiyun static void
RecordAReply(CallbackListPtr * pcbl,void * nulldata,void * calldata)578*4882a593Smuzhiyun RecordAReply(CallbackListPtr *pcbl, void *nulldata, void *calldata)
579*4882a593Smuzhiyun {
580*4882a593Smuzhiyun     RecordContextPtr pContext;
581*4882a593Smuzhiyun     RecordClientsAndProtocolPtr pRCAP;
582*4882a593Smuzhiyun     int eci;
583*4882a593Smuzhiyun     ReplyInfoRec *pri = (ReplyInfoRec *) calldata;
584*4882a593Smuzhiyun     ClientPtr client = pri->client;
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun     for (eci = 0; eci < numEnabledContexts; eci++) {
587*4882a593Smuzhiyun         pContext = ppAllContexts[eci];
588*4882a593Smuzhiyun         pRCAP = RecordFindClientOnContext(pContext, client->clientAsMask, NULL);
589*4882a593Smuzhiyun         if (pRCAP) {
590*4882a593Smuzhiyun             int majorop = client->majorOp;
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun             if (pContext->continuedReply) {
593*4882a593Smuzhiyun                 RecordAProtocolElement(pContext, client, XRecordFromServer,
594*4882a593Smuzhiyun                                        (void *) pri->replyData,
595*4882a593Smuzhiyun                                        pri->dataLenBytes, pri->padBytes,
596*4882a593Smuzhiyun                                        /* continuation */ -1);
597*4882a593Smuzhiyun                 if (!pri->bytesRemaining)
598*4882a593Smuzhiyun                     pContext->continuedReply = 0;
599*4882a593Smuzhiyun             }
600*4882a593Smuzhiyun             else if (pri->startOfReply && pRCAP->pReplyMajorOpSet &&
601*4882a593Smuzhiyun                      RecordIsMemberOfSet(pRCAP->pReplyMajorOpSet, majorop)) {
602*4882a593Smuzhiyun                 if (majorop <= 127) {   /* core reply */
603*4882a593Smuzhiyun                     RecordAProtocolElement(pContext, client, XRecordFromServer,
604*4882a593Smuzhiyun                                            (void *) pri->replyData,
605*4882a593Smuzhiyun                                            pri->dataLenBytes, 0,
606*4882a593Smuzhiyun                                            pri->bytesRemaining);
607*4882a593Smuzhiyun                     if (pri->bytesRemaining)
608*4882a593Smuzhiyun                         pContext->continuedReply = 1;
609*4882a593Smuzhiyun                 }
610*4882a593Smuzhiyun                 else {          /* extension, check minor opcode */
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun                     int minorop = client->minorOp;
613*4882a593Smuzhiyun                     int numMinOpInfo;
614*4882a593Smuzhiyun                     RecordMinorOpPtr pMinorOpInfo = pRCAP->pReplyMinOpInfo;
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun                     assert(pMinorOpInfo);
617*4882a593Smuzhiyun                     numMinOpInfo = pMinorOpInfo->count;
618*4882a593Smuzhiyun                     pMinorOpInfo++;
619*4882a593Smuzhiyun                     assert(numMinOpInfo);
620*4882a593Smuzhiyun                     for (; numMinOpInfo; numMinOpInfo--, pMinorOpInfo++) {
621*4882a593Smuzhiyun                         if (majorop >= pMinorOpInfo->major.first &&
622*4882a593Smuzhiyun                             majorop <= pMinorOpInfo->major.last &&
623*4882a593Smuzhiyun                             RecordIsMemberOfSet(pMinorOpInfo->major.pMinOpSet,
624*4882a593Smuzhiyun                                                 minorop)) {
625*4882a593Smuzhiyun                             RecordAProtocolElement(pContext, client,
626*4882a593Smuzhiyun                                                    XRecordFromServer,
627*4882a593Smuzhiyun                                                    (void *) pri->replyData,
628*4882a593Smuzhiyun                                                    pri->dataLenBytes, 0,
629*4882a593Smuzhiyun                                                    pri->bytesRemaining);
630*4882a593Smuzhiyun                             if (pri->bytesRemaining)
631*4882a593Smuzhiyun                                 pContext->continuedReply = 1;
632*4882a593Smuzhiyun                             break;
633*4882a593Smuzhiyun                         }
634*4882a593Smuzhiyun                     }           /* end for each minor op info */
635*4882a593Smuzhiyun                 }               /* end extension reply */
636*4882a593Smuzhiyun             }                   /* end continued reply vs. start of reply */
637*4882a593Smuzhiyun         }                       /* end client is registered on this context */
638*4882a593Smuzhiyun     }                           /* end for each context */
639*4882a593Smuzhiyun }                               /* RecordAReply */
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun /* RecordADeliveredEventOrError
642*4882a593Smuzhiyun  *
643*4882a593Smuzhiyun  * Arguments:
644*4882a593Smuzhiyun  *	pcbl is &EventCallback.
645*4882a593Smuzhiyun  *	nulldata is NULL.
646*4882a593Smuzhiyun  *	calldata is a pointer to a EventInfoRec (include/dix.h)
647*4882a593Smuzhiyun  *	  which provides information about events that are being sent
648*4882a593Smuzhiyun  *	  to clients.
649*4882a593Smuzhiyun  *
650*4882a593Smuzhiyun  * Returns: nothing.
651*4882a593Smuzhiyun  *
652*4882a593Smuzhiyun  * Side Effects:
653*4882a593Smuzhiyun  *	The event or error is recorded by all contexts that have registered
654*4882a593Smuzhiyun  *	it for this client.
655*4882a593Smuzhiyun  */
656*4882a593Smuzhiyun static void
RecordADeliveredEventOrError(CallbackListPtr * pcbl,void * nulldata,void * calldata)657*4882a593Smuzhiyun RecordADeliveredEventOrError(CallbackListPtr *pcbl, void *nulldata,
658*4882a593Smuzhiyun                              void *calldata)
659*4882a593Smuzhiyun {
660*4882a593Smuzhiyun     EventInfoRec *pei = (EventInfoRec *) calldata;
661*4882a593Smuzhiyun     RecordContextPtr pContext;
662*4882a593Smuzhiyun     RecordClientsAndProtocolPtr pRCAP;
663*4882a593Smuzhiyun     int eci;                    /* enabled context index */
664*4882a593Smuzhiyun     ClientPtr pClient = pei->client;
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun     for (eci = 0; eci < numEnabledContexts; eci++) {
667*4882a593Smuzhiyun         pContext = ppAllContexts[eci];
668*4882a593Smuzhiyun         pRCAP = RecordFindClientOnContext(pContext, pClient->clientAsMask,
669*4882a593Smuzhiyun                                           NULL);
670*4882a593Smuzhiyun         if (pRCAP && (pRCAP->pDeliveredEventSet || pRCAP->pErrorSet)) {
671*4882a593Smuzhiyun             int ev;             /* event index */
672*4882a593Smuzhiyun             xEvent *pev = pei->events;
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun             for (ev = 0; ev < pei->count; ev++, pev++) {
675*4882a593Smuzhiyun                 int recordit = 0;
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun                 if (pRCAP->pErrorSet) {
678*4882a593Smuzhiyun                     recordit = RecordIsMemberOfSet(pRCAP->pErrorSet,
679*4882a593Smuzhiyun                                                    ((xError *) (pev))->
680*4882a593Smuzhiyun                                                    errorCode);
681*4882a593Smuzhiyun                 }
682*4882a593Smuzhiyun                 else if (pRCAP->pDeliveredEventSet) {
683*4882a593Smuzhiyun                     recordit = RecordIsMemberOfSet(pRCAP->pDeliveredEventSet,
684*4882a593Smuzhiyun                                                    pev->u.u.type & 0177);
685*4882a593Smuzhiyun                 }
686*4882a593Smuzhiyun                 if (recordit) {
687*4882a593Smuzhiyun                     xEvent swappedEvent;
688*4882a593Smuzhiyun                     xEvent *pEvToRecord = pev;
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun                     if (pClient->swapped) {
691*4882a593Smuzhiyun                         (*EventSwapVector[pev->u.u.type & 0177])
692*4882a593Smuzhiyun                             (pev, &swappedEvent);
693*4882a593Smuzhiyun                         pEvToRecord = &swappedEvent;
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun                     }
696*4882a593Smuzhiyun                     RecordAProtocolElement(pContext, pClient,
697*4882a593Smuzhiyun                                            XRecordFromServer, pEvToRecord,
698*4882a593Smuzhiyun                                            SIZEOF(xEvent), 0, 0);
699*4882a593Smuzhiyun                 }
700*4882a593Smuzhiyun             }                   /* end for each event */
701*4882a593Smuzhiyun         }                       /* end this client is on this context */
702*4882a593Smuzhiyun     }                           /* end for each enabled context */
703*4882a593Smuzhiyun }                               /* RecordADeliveredEventOrError */
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun static void
RecordSendProtocolEvents(RecordClientsAndProtocolPtr pRCAP,RecordContextPtr pContext,xEvent * pev,int count)706*4882a593Smuzhiyun RecordSendProtocolEvents(RecordClientsAndProtocolPtr pRCAP,
707*4882a593Smuzhiyun                          RecordContextPtr pContext, xEvent *pev, int count)
708*4882a593Smuzhiyun {
709*4882a593Smuzhiyun     int ev;                     /* event index */
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun     for (ev = 0; ev < count; ev++, pev++) {
712*4882a593Smuzhiyun         if (RecordIsMemberOfSet(pRCAP->pDeviceEventSet, pev->u.u.type & 0177)) {
713*4882a593Smuzhiyun             xEvent swappedEvent;
714*4882a593Smuzhiyun             xEvent *pEvToRecord = pev;
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun #ifdef PANORAMIX
717*4882a593Smuzhiyun             xEvent shiftedEvent;
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun             if (!noPanoramiXExtension &&
720*4882a593Smuzhiyun                 (pev->u.u.type == MotionNotify ||
721*4882a593Smuzhiyun                  pev->u.u.type == ButtonPress ||
722*4882a593Smuzhiyun                  pev->u.u.type == ButtonRelease ||
723*4882a593Smuzhiyun                  pev->u.u.type == KeyPress || pev->u.u.type == KeyRelease)) {
724*4882a593Smuzhiyun                 int scr = XineramaGetCursorScreen(inputInfo.pointer);
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun                 memcpy(&shiftedEvent, pev, sizeof(xEvent));
727*4882a593Smuzhiyun                 shiftedEvent.u.keyButtonPointer.rootX +=
728*4882a593Smuzhiyun                     screenInfo.screens[scr]->x - screenInfo.screens[0]->x;
729*4882a593Smuzhiyun                 shiftedEvent.u.keyButtonPointer.rootY +=
730*4882a593Smuzhiyun                     screenInfo.screens[scr]->y - screenInfo.screens[0]->y;
731*4882a593Smuzhiyun                 pEvToRecord = &shiftedEvent;
732*4882a593Smuzhiyun             }
733*4882a593Smuzhiyun #endif                          /* PANORAMIX */
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun             if (pContext->pRecordingClient->swapped) {
736*4882a593Smuzhiyun                 (*EventSwapVector[pEvToRecord->u.u.type & 0177])
737*4882a593Smuzhiyun                     (pEvToRecord, &swappedEvent);
738*4882a593Smuzhiyun                 pEvToRecord = &swappedEvent;
739*4882a593Smuzhiyun             }
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun             RecordAProtocolElement(pContext, NULL,
742*4882a593Smuzhiyun                                    XRecordFromServer, pEvToRecord,
743*4882a593Smuzhiyun                                    SIZEOF(xEvent), 0, 0);
744*4882a593Smuzhiyun             /* make sure device events get flushed in the absence
745*4882a593Smuzhiyun              * of other client activity
746*4882a593Smuzhiyun              */
747*4882a593Smuzhiyun             SetCriticalOutputPending();
748*4882a593Smuzhiyun         }
749*4882a593Smuzhiyun     }                           /* end for each event */
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun }                               /* RecordADeviceEvent */
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun /* RecordADeviceEvent
754*4882a593Smuzhiyun  *
755*4882a593Smuzhiyun  * Arguments:
756*4882a593Smuzhiyun  *	pcbl is &DeviceEventCallback.
757*4882a593Smuzhiyun  *	nulldata is NULL.
758*4882a593Smuzhiyun  *	calldata is a pointer to a DeviceEventInfoRec (include/dix.h)
759*4882a593Smuzhiyun  *	  which provides information about device events that occur.
760*4882a593Smuzhiyun  *
761*4882a593Smuzhiyun  * Returns: nothing.
762*4882a593Smuzhiyun  *
763*4882a593Smuzhiyun  * Side Effects:
764*4882a593Smuzhiyun  *	The device event is recorded by all contexts that have registered
765*4882a593Smuzhiyun  *	it for this client.
766*4882a593Smuzhiyun  */
767*4882a593Smuzhiyun static void
RecordADeviceEvent(CallbackListPtr * pcbl,void * nulldata,void * calldata)768*4882a593Smuzhiyun RecordADeviceEvent(CallbackListPtr *pcbl, void *nulldata, void *calldata)
769*4882a593Smuzhiyun {
770*4882a593Smuzhiyun     DeviceEventInfoRec *pei = (DeviceEventInfoRec *) calldata;
771*4882a593Smuzhiyun     RecordContextPtr pContext;
772*4882a593Smuzhiyun     RecordClientsAndProtocolPtr pRCAP;
773*4882a593Smuzhiyun     int eci;                    /* enabled context index */
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun     for (eci = 0; eci < numEnabledContexts; eci++) {
776*4882a593Smuzhiyun         pContext = ppAllContexts[eci];
777*4882a593Smuzhiyun         for (pRCAP = pContext->pListOfRCAP; pRCAP; pRCAP = pRCAP->pNextRCAP) {
778*4882a593Smuzhiyun             if (pRCAP->pDeviceEventSet) {
779*4882a593Smuzhiyun                 int count;
780*4882a593Smuzhiyun                 xEvent *xi_events = NULL;
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun                 /* TODO check return values */
783*4882a593Smuzhiyun                 if (IsMaster(pei->device)) {
784*4882a593Smuzhiyun                     xEvent *core_events;
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun                     EventToCore(pei->event, &core_events, &count);
787*4882a593Smuzhiyun                     RecordSendProtocolEvents(pRCAP, pContext, core_events,
788*4882a593Smuzhiyun                                              count);
789*4882a593Smuzhiyun                     free(core_events);
790*4882a593Smuzhiyun                 }
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun                 EventToXI(pei->event, &xi_events, &count);
793*4882a593Smuzhiyun                 RecordSendProtocolEvents(pRCAP, pContext, xi_events, count);
794*4882a593Smuzhiyun                 free(xi_events);
795*4882a593Smuzhiyun             }                   /* end this RCAP selects device events */
796*4882a593Smuzhiyun         }                       /* end for each RCAP on this context */
797*4882a593Smuzhiyun     }                           /* end for each enabled context */
798*4882a593Smuzhiyun }
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun /* RecordFlushAllContexts
801*4882a593Smuzhiyun  *
802*4882a593Smuzhiyun  * Arguments:
803*4882a593Smuzhiyun  *	pcbl is &FlushCallback.
804*4882a593Smuzhiyun  *	nulldata and calldata are NULL.
805*4882a593Smuzhiyun  *
806*4882a593Smuzhiyun  * Returns: nothing.
807*4882a593Smuzhiyun  *
808*4882a593Smuzhiyun  * Side Effects:
809*4882a593Smuzhiyun  *	All buffered reply data of all enabled contexts is written to
810*4882a593Smuzhiyun  *	the recording clients.
811*4882a593Smuzhiyun  */
812*4882a593Smuzhiyun static void
RecordFlushAllContexts(CallbackListPtr * pcbl,void * nulldata,void * calldata)813*4882a593Smuzhiyun RecordFlushAllContexts(CallbackListPtr *pcbl,
814*4882a593Smuzhiyun                        void *nulldata, void *calldata)
815*4882a593Smuzhiyun {
816*4882a593Smuzhiyun     int eci;                    /* enabled context index */
817*4882a593Smuzhiyun     RecordContextPtr pContext;
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun     for (eci = 0; eci < numEnabledContexts; eci++) {
820*4882a593Smuzhiyun         pContext = ppAllContexts[eci];
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun         /* In most cases we leave it to RecordFlushReplyBuffer to make
823*4882a593Smuzhiyun          * this check, but this function could be called very often, so we
824*4882a593Smuzhiyun          * check before calling hoping to save the function call cost
825*4882a593Smuzhiyun          * most of the time.
826*4882a593Smuzhiyun          */
827*4882a593Smuzhiyun         if (pContext->numBufBytes)
828*4882a593Smuzhiyun             RecordFlushReplyBuffer(ppAllContexts[eci], NULL, 0, NULL, 0);
829*4882a593Smuzhiyun     }
830*4882a593Smuzhiyun }                               /* RecordFlushAllContexts */
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun /* RecordInstallHooks
833*4882a593Smuzhiyun  *
834*4882a593Smuzhiyun  * Arguments:
835*4882a593Smuzhiyun  *	pRCAP is an RCAP on an enabled or being-enabled context.
836*4882a593Smuzhiyun  *	oneclient can be zero or the resource ID mask identifying a client.
837*4882a593Smuzhiyun  *
838*4882a593Smuzhiyun  * Returns: BadAlloc if a memory allocation error occurred, else Success.
839*4882a593Smuzhiyun  *
840*4882a593Smuzhiyun  * Side Effects:
841*4882a593Smuzhiyun  *	Recording hooks needed by RCAP are installed.
842*4882a593Smuzhiyun  *	If oneclient is zero, recording hooks needed for all clients and
843*4882a593Smuzhiyun  *	protocol on the RCAP are installed.  If oneclient is non-zero,
844*4882a593Smuzhiyun  *	only those hooks needed for the specified client are installed.
845*4882a593Smuzhiyun  *
846*4882a593Smuzhiyun  *	Client requestVectors may be altered.  numEnabledRCAPs will be
847*4882a593Smuzhiyun  *	incremented if oneclient == 0.  Callbacks may be added to
848*4882a593Smuzhiyun  *	various callback lists.
849*4882a593Smuzhiyun  */
850*4882a593Smuzhiyun static int
RecordInstallHooks(RecordClientsAndProtocolPtr pRCAP,XID oneclient)851*4882a593Smuzhiyun RecordInstallHooks(RecordClientsAndProtocolPtr pRCAP, XID oneclient)
852*4882a593Smuzhiyun {
853*4882a593Smuzhiyun     int i = 0;
854*4882a593Smuzhiyun     XID client;
855*4882a593Smuzhiyun 
856*4882a593Smuzhiyun     if (oneclient)
857*4882a593Smuzhiyun         client = oneclient;
858*4882a593Smuzhiyun     else
859*4882a593Smuzhiyun         client = pRCAP->numClients ? pRCAP->pClientIDs[i++] : 0;
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun     while (client) {
862*4882a593Smuzhiyun         if (client != XRecordFutureClients) {
863*4882a593Smuzhiyun             if (pRCAP->pRequestMajorOpSet) {
864*4882a593Smuzhiyun                 RecordSetIteratePtr pIter = NULL;
865*4882a593Smuzhiyun                 RecordSetInterval interval;
866*4882a593Smuzhiyun                 ClientPtr pClient = clients[CLIENT_ID(client)];
867*4882a593Smuzhiyun 
868*4882a593Smuzhiyun                 if (pClient && !RecordClientPrivate(pClient)) {
869*4882a593Smuzhiyun                     RecordClientPrivatePtr pClientPriv;
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun                     /* no Record proc vector; allocate one */
872*4882a593Smuzhiyun                     pClientPriv = (RecordClientPrivatePtr)
873*4882a593Smuzhiyun                         malloc(sizeof(RecordClientPrivateRec));
874*4882a593Smuzhiyun                     if (!pClientPriv)
875*4882a593Smuzhiyun                         return BadAlloc;
876*4882a593Smuzhiyun                     /* copy old proc vector to new */
877*4882a593Smuzhiyun                     memcpy(pClientPriv->recordVector, pClient->requestVector,
878*4882a593Smuzhiyun                            sizeof(pClientPriv->recordVector));
879*4882a593Smuzhiyun                     pClientPriv->originalVector = pClient->requestVector;
880*4882a593Smuzhiyun                     dixSetPrivate(&pClient->devPrivates,
881*4882a593Smuzhiyun                                   RecordClientPrivateKey, pClientPriv);
882*4882a593Smuzhiyun                     pClient->requestVector = pClientPriv->recordVector;
883*4882a593Smuzhiyun                 }
884*4882a593Smuzhiyun                 while ((pIter = RecordIterateSet(pRCAP->pRequestMajorOpSet,
885*4882a593Smuzhiyun                                                  pIter, &interval))) {
886*4882a593Smuzhiyun                     unsigned int j;
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun                     for (j = interval.first; j <= interval.last; j++)
889*4882a593Smuzhiyun                         pClient->requestVector[j] = RecordARequest;
890*4882a593Smuzhiyun                 }
891*4882a593Smuzhiyun             }
892*4882a593Smuzhiyun         }
893*4882a593Smuzhiyun         if (oneclient)
894*4882a593Smuzhiyun             client = 0;
895*4882a593Smuzhiyun         else
896*4882a593Smuzhiyun             client = (i < pRCAP->numClients) ? pRCAP->pClientIDs[i++] : 0;
897*4882a593Smuzhiyun     }
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun     assert(numEnabledRCAPs >= 0);
900*4882a593Smuzhiyun     if (!oneclient && ++numEnabledRCAPs == 1) { /* we're enabling the first context */
901*4882a593Smuzhiyun         if (!AddCallback(&EventCallback, RecordADeliveredEventOrError, NULL))
902*4882a593Smuzhiyun             return BadAlloc;
903*4882a593Smuzhiyun         if (!AddCallback(&DeviceEventCallback, RecordADeviceEvent, NULL))
904*4882a593Smuzhiyun             return BadAlloc;
905*4882a593Smuzhiyun         if (!AddCallback(&ReplyCallback, RecordAReply, NULL))
906*4882a593Smuzhiyun             return BadAlloc;
907*4882a593Smuzhiyun         if (!AddCallback(&FlushCallback, RecordFlushAllContexts, NULL))
908*4882a593Smuzhiyun             return BadAlloc;
909*4882a593Smuzhiyun         /* Alternate context flushing scheme: delete the line above
910*4882a593Smuzhiyun          * and call RegisterBlockAndWakeupHandlers here passing
911*4882a593Smuzhiyun          * RecordFlushAllContexts.  Is this any better?
912*4882a593Smuzhiyun          */
913*4882a593Smuzhiyun     }
914*4882a593Smuzhiyun     return Success;
915*4882a593Smuzhiyun }                               /* RecordInstallHooks */
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun /* RecordUninstallHooks
918*4882a593Smuzhiyun  *
919*4882a593Smuzhiyun  * Arguments:
920*4882a593Smuzhiyun  *	pRCAP is an RCAP on an enabled or being-disabled context.
921*4882a593Smuzhiyun  *	oneclient can be zero or the resource ID mask identifying a client.
922*4882a593Smuzhiyun  *
923*4882a593Smuzhiyun  * Returns: nothing.
924*4882a593Smuzhiyun  *
925*4882a593Smuzhiyun  * Side Effects:
926*4882a593Smuzhiyun  *	Recording hooks needed by RCAP may be uninstalled.
927*4882a593Smuzhiyun  *	If oneclient is zero, recording hooks needed for all clients and
928*4882a593Smuzhiyun  *	protocol on the RCAP may be uninstalled.  If oneclient is non-zero,
929*4882a593Smuzhiyun  *	only those hooks needed for the specified client may be uninstalled.
930*4882a593Smuzhiyun  *
931*4882a593Smuzhiyun  *	Client requestVectors may be altered.  numEnabledRCAPs will be
932*4882a593Smuzhiyun  *	decremented if oneclient == 0.  Callbacks may be deleted from
933*4882a593Smuzhiyun  *	various callback lists.
934*4882a593Smuzhiyun  */
935*4882a593Smuzhiyun static void
RecordUninstallHooks(RecordClientsAndProtocolPtr pRCAP,XID oneclient)936*4882a593Smuzhiyun RecordUninstallHooks(RecordClientsAndProtocolPtr pRCAP, XID oneclient)
937*4882a593Smuzhiyun {
938*4882a593Smuzhiyun     int i = 0;
939*4882a593Smuzhiyun     XID client;
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun     if (oneclient)
942*4882a593Smuzhiyun         client = oneclient;
943*4882a593Smuzhiyun     else
944*4882a593Smuzhiyun         client = pRCAP->numClients ? pRCAP->pClientIDs[i++] : 0;
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun     while (client) {
947*4882a593Smuzhiyun         if (client != XRecordFutureClients) {
948*4882a593Smuzhiyun             if (pRCAP->pRequestMajorOpSet) {
949*4882a593Smuzhiyun                 ClientPtr pClient = clients[CLIENT_ID(client)];
950*4882a593Smuzhiyun                 int c;
951*4882a593Smuzhiyun                 Bool otherRCAPwantsProcVector = FALSE;
952*4882a593Smuzhiyun                 RecordClientPrivatePtr pClientPriv = NULL;
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun                 assert(pClient);
955*4882a593Smuzhiyun                 pClientPriv = RecordClientPrivate(pClient);
956*4882a593Smuzhiyun                 assert(pClientPriv);
957*4882a593Smuzhiyun                 memcpy(pClientPriv->recordVector, pClientPriv->originalVector,
958*4882a593Smuzhiyun                        sizeof(pClientPriv->recordVector));
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun                 for (c = 0; c < numEnabledContexts; c++) {
961*4882a593Smuzhiyun                     RecordClientsAndProtocolPtr pOtherRCAP;
962*4882a593Smuzhiyun                     RecordContextPtr pContext = ppAllContexts[c];
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun                     if (pContext == pRCAP->pContext)
965*4882a593Smuzhiyun                         continue;
966*4882a593Smuzhiyun                     pOtherRCAP = RecordFindClientOnContext(pContext, client,
967*4882a593Smuzhiyun                                                            NULL);
968*4882a593Smuzhiyun                     if (pOtherRCAP && pOtherRCAP->pRequestMajorOpSet) {
969*4882a593Smuzhiyun                         RecordSetIteratePtr pIter = NULL;
970*4882a593Smuzhiyun                         RecordSetInterval interval;
971*4882a593Smuzhiyun 
972*4882a593Smuzhiyun                         otherRCAPwantsProcVector = TRUE;
973*4882a593Smuzhiyun                         while ((pIter =
974*4882a593Smuzhiyun                                 RecordIterateSet(pOtherRCAP->pRequestMajorOpSet,
975*4882a593Smuzhiyun                                                  pIter, &interval))) {
976*4882a593Smuzhiyun                             unsigned int j;
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun                             for (j = interval.first; j <= interval.last; j++)
979*4882a593Smuzhiyun                                 pClient->requestVector[j] = RecordARequest;
980*4882a593Smuzhiyun                         }
981*4882a593Smuzhiyun                     }
982*4882a593Smuzhiyun                 }
983*4882a593Smuzhiyun                 if (!otherRCAPwantsProcVector) {        /* nobody needs it, so free it */
984*4882a593Smuzhiyun                     pClient->requestVector = pClientPriv->originalVector;
985*4882a593Smuzhiyun                     dixSetPrivate(&pClient->devPrivates,
986*4882a593Smuzhiyun                                   RecordClientPrivateKey, NULL);
987*4882a593Smuzhiyun                     free(pClientPriv);
988*4882a593Smuzhiyun                 }
989*4882a593Smuzhiyun             }                   /* end if this RCAP specifies any requests */
990*4882a593Smuzhiyun         }                       /* end if not future clients */
991*4882a593Smuzhiyun         if (oneclient)
992*4882a593Smuzhiyun             client = 0;
993*4882a593Smuzhiyun         else
994*4882a593Smuzhiyun             client = (i < pRCAP->numClients) ? pRCAP->pClientIDs[i++] : 0;
995*4882a593Smuzhiyun     }
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun     assert(numEnabledRCAPs >= 1);
998*4882a593Smuzhiyun     if (!oneclient && --numEnabledRCAPs == 0) { /* we're disabling the last context */
999*4882a593Smuzhiyun         DeleteCallback(&EventCallback, RecordADeliveredEventOrError, NULL);
1000*4882a593Smuzhiyun         DeleteCallback(&DeviceEventCallback, RecordADeviceEvent, NULL);
1001*4882a593Smuzhiyun         DeleteCallback(&ReplyCallback, RecordAReply, NULL);
1002*4882a593Smuzhiyun         DeleteCallback(&FlushCallback, RecordFlushAllContexts, NULL);
1003*4882a593Smuzhiyun         /* Alternate context flushing scheme: delete the line above
1004*4882a593Smuzhiyun          * and call RemoveBlockAndWakeupHandlers here passing
1005*4882a593Smuzhiyun          * RecordFlushAllContexts.  Is this any better?
1006*4882a593Smuzhiyun          */
1007*4882a593Smuzhiyun         /* Having deleted the callback, call it one last time. -gildea */
1008*4882a593Smuzhiyun         RecordFlushAllContexts(&FlushCallback, NULL, NULL);
1009*4882a593Smuzhiyun     }
1010*4882a593Smuzhiyun }                               /* RecordUninstallHooks */
1011*4882a593Smuzhiyun 
1012*4882a593Smuzhiyun /* RecordDeleteClientFromRCAP
1013*4882a593Smuzhiyun  *
1014*4882a593Smuzhiyun  * Arguments:
1015*4882a593Smuzhiyun  *	pRCAP is an RCAP to delete the client from.
1016*4882a593Smuzhiyun  *	position is the index into the array pRCAP->pClientIDs of the
1017*4882a593Smuzhiyun  *	client to delete.
1018*4882a593Smuzhiyun  *
1019*4882a593Smuzhiyun  * Returns: nothing.
1020*4882a593Smuzhiyun  *
1021*4882a593Smuzhiyun  * Side Effects:
1022*4882a593Smuzhiyun  *	Recording hooks needed by client will be uninstalled if the context
1023*4882a593Smuzhiyun  *	is enabled.  The designated client will be removed from the
1024*4882a593Smuzhiyun  *	pRCAP->pClientIDs array.  If it was the only client on the RCAP,
1025*4882a593Smuzhiyun  *	the RCAP is removed from the context and freed.  (Invariant: RCAPs
1026*4882a593Smuzhiyun  *	have at least one client.)
1027*4882a593Smuzhiyun  */
1028*4882a593Smuzhiyun static void
RecordDeleteClientFromRCAP(RecordClientsAndProtocolPtr pRCAP,int position)1029*4882a593Smuzhiyun RecordDeleteClientFromRCAP(RecordClientsAndProtocolPtr pRCAP, int position)
1030*4882a593Smuzhiyun {
1031*4882a593Smuzhiyun     if (pRCAP->pContext->pRecordingClient)
1032*4882a593Smuzhiyun         RecordUninstallHooks(pRCAP, pRCAP->pClientIDs[position]);
1033*4882a593Smuzhiyun     if (position != pRCAP->numClients - 1)
1034*4882a593Smuzhiyun         pRCAP->pClientIDs[position] = pRCAP->pClientIDs[pRCAP->numClients - 1];
1035*4882a593Smuzhiyun     if (--pRCAP->numClients == 0) {     /* no more clients; remove RCAP from context's list */
1036*4882a593Smuzhiyun         RecordContextPtr pContext = pRCAP->pContext;
1037*4882a593Smuzhiyun 
1038*4882a593Smuzhiyun         if (pContext->pRecordingClient)
1039*4882a593Smuzhiyun             RecordUninstallHooks(pRCAP, 0);
1040*4882a593Smuzhiyun         if (pContext->pListOfRCAP == pRCAP)
1041*4882a593Smuzhiyun             pContext->pListOfRCAP = pRCAP->pNextRCAP;
1042*4882a593Smuzhiyun         else {
1043*4882a593Smuzhiyun             RecordClientsAndProtocolPtr prevRCAP;
1044*4882a593Smuzhiyun 
1045*4882a593Smuzhiyun             for (prevRCAP = pContext->pListOfRCAP;
1046*4882a593Smuzhiyun                  prevRCAP->pNextRCAP != pRCAP; prevRCAP = prevRCAP->pNextRCAP);
1047*4882a593Smuzhiyun             prevRCAP->pNextRCAP = pRCAP->pNextRCAP;
1048*4882a593Smuzhiyun         }
1049*4882a593Smuzhiyun         /* free the RCAP */
1050*4882a593Smuzhiyun         if (pRCAP->clientIDsSeparatelyAllocated)
1051*4882a593Smuzhiyun             free(pRCAP->pClientIDs);
1052*4882a593Smuzhiyun         free(pRCAP);
1053*4882a593Smuzhiyun     }
1054*4882a593Smuzhiyun }                               /* RecordDeleteClientFromRCAP */
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun /* RecordAddClientToRCAP
1057*4882a593Smuzhiyun  *
1058*4882a593Smuzhiyun  * Arguments:
1059*4882a593Smuzhiyun  *	pRCAP is an RCAP to add the client to.
1060*4882a593Smuzhiyun  *	clientspec is the resource ID mask identifying a client, or
1061*4882a593Smuzhiyun  *	  XRecordFutureClients.
1062*4882a593Smuzhiyun  *
1063*4882a593Smuzhiyun  * Returns: nothing.
1064*4882a593Smuzhiyun  *
1065*4882a593Smuzhiyun  * Side Effects:
1066*4882a593Smuzhiyun  *	Recording hooks needed by client will be installed if the context
1067*4882a593Smuzhiyun  *	is enabled.  The designated client will be added to the
1068*4882a593Smuzhiyun  *	pRCAP->pClientIDs array, which may be realloced.
1069*4882a593Smuzhiyun  *	pRCAP->clientIDsSeparatelyAllocated may be set to 1 if there
1070*4882a593Smuzhiyun  *	is no more room to hold clients internal to the RCAP.
1071*4882a593Smuzhiyun  */
1072*4882a593Smuzhiyun static void
RecordAddClientToRCAP(RecordClientsAndProtocolPtr pRCAP,XID clientspec)1073*4882a593Smuzhiyun RecordAddClientToRCAP(RecordClientsAndProtocolPtr pRCAP, XID clientspec)
1074*4882a593Smuzhiyun {
1075*4882a593Smuzhiyun     if (pRCAP->numClients == pRCAP->sizeClients) {
1076*4882a593Smuzhiyun         if (pRCAP->clientIDsSeparatelyAllocated) {
1077*4882a593Smuzhiyun             XID *pNewIDs =
1078*4882a593Smuzhiyun                 reallocarray(pRCAP->pClientIDs,
1079*4882a593Smuzhiyun                              pRCAP->sizeClients + CLIENT_ARRAY_GROWTH_INCREMENT,
1080*4882a593Smuzhiyun                              sizeof(XID));
1081*4882a593Smuzhiyun             if (!pNewIDs)
1082*4882a593Smuzhiyun                 return;
1083*4882a593Smuzhiyun             pRCAP->pClientIDs = pNewIDs;
1084*4882a593Smuzhiyun             pRCAP->sizeClients += CLIENT_ARRAY_GROWTH_INCREMENT;
1085*4882a593Smuzhiyun         }
1086*4882a593Smuzhiyun         else {
1087*4882a593Smuzhiyun             XID *pNewIDs =
1088*4882a593Smuzhiyun                 xallocarray(pRCAP->sizeClients + CLIENT_ARRAY_GROWTH_INCREMENT,
1089*4882a593Smuzhiyun                             sizeof(XID));
1090*4882a593Smuzhiyun             if (!pNewIDs)
1091*4882a593Smuzhiyun                 return;
1092*4882a593Smuzhiyun             memcpy(pNewIDs, pRCAP->pClientIDs, pRCAP->numClients * sizeof(XID));
1093*4882a593Smuzhiyun             pRCAP->pClientIDs = pNewIDs;
1094*4882a593Smuzhiyun             pRCAP->sizeClients += CLIENT_ARRAY_GROWTH_INCREMENT;
1095*4882a593Smuzhiyun             pRCAP->clientIDsSeparatelyAllocated = 1;
1096*4882a593Smuzhiyun         }
1097*4882a593Smuzhiyun     }
1098*4882a593Smuzhiyun     pRCAP->pClientIDs[pRCAP->numClients++] = clientspec;
1099*4882a593Smuzhiyun     if (pRCAP->pContext->pRecordingClient)
1100*4882a593Smuzhiyun         RecordInstallHooks(pRCAP, clientspec);
1101*4882a593Smuzhiyun }                               /* RecordDeleteClientFromRCAP */
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun /* RecordDeleteClientFromContext
1104*4882a593Smuzhiyun  *
1105*4882a593Smuzhiyun  * Arguments:
1106*4882a593Smuzhiyun  *	pContext is the context to delete from.
1107*4882a593Smuzhiyun  *	clientspec is the resource ID mask identifying a client, or
1108*4882a593Smuzhiyun  *	  XRecordFutureClients.
1109*4882a593Smuzhiyun  *
1110*4882a593Smuzhiyun  * Returns: nothing.
1111*4882a593Smuzhiyun  *
1112*4882a593Smuzhiyun  * Side Effects:
1113*4882a593Smuzhiyun  *	If clientspec is on any RCAP of the context, it is deleted from that
1114*4882a593Smuzhiyun  *	RCAP.  (A given clientspec can only be on one RCAP of a context.)
1115*4882a593Smuzhiyun  */
1116*4882a593Smuzhiyun static void
RecordDeleteClientFromContext(RecordContextPtr pContext,XID clientspec)1117*4882a593Smuzhiyun RecordDeleteClientFromContext(RecordContextPtr pContext, XID clientspec)
1118*4882a593Smuzhiyun {
1119*4882a593Smuzhiyun     RecordClientsAndProtocolPtr pRCAP;
1120*4882a593Smuzhiyun     int position;
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun     if ((pRCAP = RecordFindClientOnContext(pContext, clientspec, &position)))
1123*4882a593Smuzhiyun         RecordDeleteClientFromRCAP(pRCAP, position);
1124*4882a593Smuzhiyun }                               /* RecordDeleteClientFromContext */
1125*4882a593Smuzhiyun 
1126*4882a593Smuzhiyun /* RecordSanityCheckClientSpecifiers
1127*4882a593Smuzhiyun  *
1128*4882a593Smuzhiyun  * Arguments:
1129*4882a593Smuzhiyun  *	clientspecs is an array of alleged CLIENTSPECs passed by the client.
1130*4882a593Smuzhiyun  *	nspecs is the number of elements in clientspecs.
1131*4882a593Smuzhiyun  *	errorspec, if non-zero, is the resource id base of a client that
1132*4882a593Smuzhiyun  *	  must not appear in clienspecs.
1133*4882a593Smuzhiyun  *
1134*4882a593Smuzhiyun  * Returns: BadMatch if any of the clientspecs are invalid, else Success.
1135*4882a593Smuzhiyun  *
1136*4882a593Smuzhiyun  * Side Effects: none.
1137*4882a593Smuzhiyun  */
1138*4882a593Smuzhiyun static int
RecordSanityCheckClientSpecifiers(ClientPtr client,XID * clientspecs,int nspecs,XID errorspec)1139*4882a593Smuzhiyun RecordSanityCheckClientSpecifiers(ClientPtr client, XID *clientspecs,
1140*4882a593Smuzhiyun                                   int nspecs, XID errorspec)
1141*4882a593Smuzhiyun {
1142*4882a593Smuzhiyun     int i;
1143*4882a593Smuzhiyun     int clientIndex;
1144*4882a593Smuzhiyun     int rc;
1145*4882a593Smuzhiyun     void *value;
1146*4882a593Smuzhiyun 
1147*4882a593Smuzhiyun     for (i = 0; i < nspecs; i++) {
1148*4882a593Smuzhiyun         if (clientspecs[i] == XRecordCurrentClients ||
1149*4882a593Smuzhiyun             clientspecs[i] == XRecordFutureClients ||
1150*4882a593Smuzhiyun             clientspecs[i] == XRecordAllClients)
1151*4882a593Smuzhiyun             continue;
1152*4882a593Smuzhiyun         if (errorspec && (CLIENT_BITS(clientspecs[i]) == errorspec))
1153*4882a593Smuzhiyun             return BadMatch;
1154*4882a593Smuzhiyun         clientIndex = CLIENT_ID(clientspecs[i]);
1155*4882a593Smuzhiyun         if (clientIndex && clients[clientIndex] &&
1156*4882a593Smuzhiyun             clients[clientIndex]->clientState == ClientStateRunning) {
1157*4882a593Smuzhiyun             if (clientspecs[i] == clients[clientIndex]->clientAsMask)
1158*4882a593Smuzhiyun                 continue;
1159*4882a593Smuzhiyun             rc = dixLookupResourceByClass(&value, clientspecs[i], RC_ANY,
1160*4882a593Smuzhiyun                                           client, DixGetAttrAccess);
1161*4882a593Smuzhiyun             if (rc != Success)
1162*4882a593Smuzhiyun                 return rc;
1163*4882a593Smuzhiyun         }
1164*4882a593Smuzhiyun         else
1165*4882a593Smuzhiyun             return BadMatch;
1166*4882a593Smuzhiyun     }
1167*4882a593Smuzhiyun     return Success;
1168*4882a593Smuzhiyun }                               /* RecordSanityCheckClientSpecifiers */
1169*4882a593Smuzhiyun 
1170*4882a593Smuzhiyun /* RecordCanonicalizeClientSpecifiers
1171*4882a593Smuzhiyun  *
1172*4882a593Smuzhiyun  * Arguments:
1173*4882a593Smuzhiyun  *	pClientspecs is an array of CLIENTSPECs that have been sanity
1174*4882a593Smuzhiyun  *	  checked.
1175*4882a593Smuzhiyun  *	pNumClientspecs is a pointer to the number of elements in pClientspecs.
1176*4882a593Smuzhiyun  *	excludespec, if non-zero, is the resource id base of a client that
1177*4882a593Smuzhiyun  *	  should not be included in the expansion of XRecordAllClients or
1178*4882a593Smuzhiyun  *	  XRecordCurrentClients.
1179*4882a593Smuzhiyun  *
1180*4882a593Smuzhiyun  * Returns:
1181*4882a593Smuzhiyun  *	A pointer to an array of CLIENTSPECs that is the same as the
1182*4882a593Smuzhiyun  *	passed array with the following modifications:
1183*4882a593Smuzhiyun  *	  - all but the client id bits of resource IDs are stripped off.
1184*4882a593Smuzhiyun  *	  - duplicates removed.
1185*4882a593Smuzhiyun  *	  - XRecordAllClients expanded to a list of all currently connected
1186*4882a593Smuzhiyun  *	    clients + XRecordFutureClients - excludespec (if non-zero)
1187*4882a593Smuzhiyun  *	  - XRecordCurrentClients expanded to a list of all currently
1188*4882a593Smuzhiyun  *	    connected clients - excludespec (if non-zero)
1189*4882a593Smuzhiyun  *	The returned array may be the passed array modified in place, or
1190*4882a593Smuzhiyun  *	it may be an malloc'ed array.  The caller should keep a pointer to the
1191*4882a593Smuzhiyun  *	original array and free the returned array if it is different.
1192*4882a593Smuzhiyun  *
1193*4882a593Smuzhiyun  *	*pNumClientspecs is set to the number of elements in the returned
1194*4882a593Smuzhiyun  *	array.
1195*4882a593Smuzhiyun  *
1196*4882a593Smuzhiyun  * Side Effects:
1197*4882a593Smuzhiyun  *	pClientspecs may be modified in place.
1198*4882a593Smuzhiyun  */
1199*4882a593Smuzhiyun static XID *
RecordCanonicalizeClientSpecifiers(XID * pClientspecs,int * pNumClientspecs,XID excludespec)1200*4882a593Smuzhiyun RecordCanonicalizeClientSpecifiers(XID *pClientspecs, int *pNumClientspecs,
1201*4882a593Smuzhiyun                                    XID excludespec)
1202*4882a593Smuzhiyun {
1203*4882a593Smuzhiyun     int i;
1204*4882a593Smuzhiyun     int numClients = *pNumClientspecs;
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun     /*  first pass strips off the resource index bits, leaving just the
1207*4882a593Smuzhiyun      *  client id bits.  This makes searching for a particular client simpler
1208*4882a593Smuzhiyun      *  (and faster.)
1209*4882a593Smuzhiyun      */
1210*4882a593Smuzhiyun     for (i = 0; i < numClients; i++) {
1211*4882a593Smuzhiyun         XID cs = pClientspecs[i];
1212*4882a593Smuzhiyun 
1213*4882a593Smuzhiyun         if (cs > XRecordAllClients)
1214*4882a593Smuzhiyun             pClientspecs[i] = CLIENT_BITS(cs);
1215*4882a593Smuzhiyun     }
1216*4882a593Smuzhiyun 
1217*4882a593Smuzhiyun     for (i = 0; i < numClients; i++) {
1218*4882a593Smuzhiyun         if (pClientspecs[i] == XRecordAllClients || pClientspecs[i] == XRecordCurrentClients) { /* expand All/Current */
1219*4882a593Smuzhiyun             int j, nc;
1220*4882a593Smuzhiyun             XID *pCanon = xallocarray(currentMaxClients + 1, sizeof(XID));
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun             if (!pCanon)
1223*4882a593Smuzhiyun                 return NULL;
1224*4882a593Smuzhiyun             for (nc = 0, j = 1; j < currentMaxClients; j++) {
1225*4882a593Smuzhiyun                 ClientPtr client = clients[j];
1226*4882a593Smuzhiyun 
1227*4882a593Smuzhiyun                 if (client != NullClient &&
1228*4882a593Smuzhiyun                     client->clientState == ClientStateRunning &&
1229*4882a593Smuzhiyun                     client->clientAsMask != excludespec) {
1230*4882a593Smuzhiyun                     pCanon[nc++] = client->clientAsMask;
1231*4882a593Smuzhiyun                 }
1232*4882a593Smuzhiyun             }
1233*4882a593Smuzhiyun             if (pClientspecs[i] == XRecordAllClients)
1234*4882a593Smuzhiyun                 pCanon[nc++] = XRecordFutureClients;
1235*4882a593Smuzhiyun             *pNumClientspecs = nc;
1236*4882a593Smuzhiyun             return pCanon;
1237*4882a593Smuzhiyun         }
1238*4882a593Smuzhiyun         else {                  /* not All or Current */
1239*4882a593Smuzhiyun 
1240*4882a593Smuzhiyun             int j;
1241*4882a593Smuzhiyun 
1242*4882a593Smuzhiyun             for (j = i + 1; j < numClients;) {
1243*4882a593Smuzhiyun                 if (pClientspecs[i] == pClientspecs[j]) {
1244*4882a593Smuzhiyun                     pClientspecs[j] = pClientspecs[--numClients];
1245*4882a593Smuzhiyun                 }
1246*4882a593Smuzhiyun                 else
1247*4882a593Smuzhiyun                     j++;
1248*4882a593Smuzhiyun             }
1249*4882a593Smuzhiyun         }
1250*4882a593Smuzhiyun     }                           /* end for each clientspec */
1251*4882a593Smuzhiyun     *pNumClientspecs = numClients;
1252*4882a593Smuzhiyun     return pClientspecs;
1253*4882a593Smuzhiyun }                               /* RecordCanonicalizeClientSpecifiers */
1254*4882a593Smuzhiyun 
1255*4882a593Smuzhiyun /****************************************************************************/
1256*4882a593Smuzhiyun 
1257*4882a593Smuzhiyun /* stuff for RegisterClients */
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun /* RecordPadAlign
1260*4882a593Smuzhiyun  *
1261*4882a593Smuzhiyun  * Arguments:
1262*4882a593Smuzhiyun  *	size is the number of bytes taken by an object.
1263*4882a593Smuzhiyun  *	align is a byte boundary (e.g. 4, 8)
1264*4882a593Smuzhiyun  *
1265*4882a593Smuzhiyun  * Returns:
1266*4882a593Smuzhiyun  *	the number of pad bytes to add at the end of an object of the
1267*4882a593Smuzhiyun  *	given size so that an object placed immediately behind it will
1268*4882a593Smuzhiyun  *	begin on an <align>-byte boundary.
1269*4882a593Smuzhiyun  *
1270*4882a593Smuzhiyun  * Side Effects: none.
1271*4882a593Smuzhiyun  */
1272*4882a593Smuzhiyun static int
RecordPadAlign(int size,int align)1273*4882a593Smuzhiyun RecordPadAlign(int size, int align)
1274*4882a593Smuzhiyun {
1275*4882a593Smuzhiyun     return (align - (size & (align - 1))) & (align - 1);
1276*4882a593Smuzhiyun }                               /* RecordPadAlign */
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun /* RecordSanityCheckRegisterClients
1279*4882a593Smuzhiyun  *
1280*4882a593Smuzhiyun  * Arguments:
1281*4882a593Smuzhiyun  *	pContext is the context being registered on.
1282*4882a593Smuzhiyun  *	client is the client that issued a RecordCreateContext or
1283*4882a593Smuzhiyun  *	  RecordRegisterClients request.
1284*4882a593Smuzhiyun  *	stuff is a pointer to the request.
1285*4882a593Smuzhiyun  *
1286*4882a593Smuzhiyun  * Returns:
1287*4882a593Smuzhiyun  *	Any one of several possible error values if any of the request
1288*4882a593Smuzhiyun  *	arguments are invalid.  Success if everything is OK.
1289*4882a593Smuzhiyun  *
1290*4882a593Smuzhiyun  * Side Effects: none.
1291*4882a593Smuzhiyun  */
1292*4882a593Smuzhiyun static int
RecordSanityCheckRegisterClients(RecordContextPtr pContext,ClientPtr client,xRecordRegisterClientsReq * stuff)1293*4882a593Smuzhiyun RecordSanityCheckRegisterClients(RecordContextPtr pContext, ClientPtr client,
1294*4882a593Smuzhiyun                                  xRecordRegisterClientsReq * stuff)
1295*4882a593Smuzhiyun {
1296*4882a593Smuzhiyun     int err;
1297*4882a593Smuzhiyun     xRecordRange *pRange;
1298*4882a593Smuzhiyun     int i;
1299*4882a593Smuzhiyun     XID recordingClient;
1300*4882a593Smuzhiyun 
1301*4882a593Smuzhiyun     if (((client->req_len << 2) - SIZEOF(xRecordRegisterClientsReq)) !=
1302*4882a593Smuzhiyun         4 * stuff->nClients + SIZEOF(xRecordRange) * stuff->nRanges)
1303*4882a593Smuzhiyun         return BadLength;
1304*4882a593Smuzhiyun 
1305*4882a593Smuzhiyun     if (stuff->elementHeader &
1306*4882a593Smuzhiyun         ~(XRecordFromClientSequence | XRecordFromClientTime |
1307*4882a593Smuzhiyun           XRecordFromServerTime)) {
1308*4882a593Smuzhiyun         client->errorValue = stuff->elementHeader;
1309*4882a593Smuzhiyun         return BadValue;
1310*4882a593Smuzhiyun     }
1311*4882a593Smuzhiyun 
1312*4882a593Smuzhiyun     recordingClient = pContext->pRecordingClient ?
1313*4882a593Smuzhiyun         pContext->pRecordingClient->clientAsMask : 0;
1314*4882a593Smuzhiyun     err = RecordSanityCheckClientSpecifiers(client, (XID *) &stuff[1],
1315*4882a593Smuzhiyun                                             stuff->nClients, recordingClient);
1316*4882a593Smuzhiyun     if (err != Success)
1317*4882a593Smuzhiyun         return err;
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun     pRange = (xRecordRange *) (((XID *) &stuff[1]) + stuff->nClients);
1320*4882a593Smuzhiyun     for (i = 0; i < stuff->nRanges; i++, pRange++) {
1321*4882a593Smuzhiyun         if (pRange->coreRequestsFirst > pRange->coreRequestsLast) {
1322*4882a593Smuzhiyun             client->errorValue = pRange->coreRequestsFirst;
1323*4882a593Smuzhiyun             return BadValue;
1324*4882a593Smuzhiyun         }
1325*4882a593Smuzhiyun         if (pRange->coreRepliesFirst > pRange->coreRepliesLast) {
1326*4882a593Smuzhiyun             client->errorValue = pRange->coreRepliesFirst;
1327*4882a593Smuzhiyun             return BadValue;
1328*4882a593Smuzhiyun         }
1329*4882a593Smuzhiyun         if ((pRange->extRequestsMajorFirst || pRange->extRequestsMajorLast) &&
1330*4882a593Smuzhiyun             (pRange->extRequestsMajorFirst < 128 ||
1331*4882a593Smuzhiyun              pRange->extRequestsMajorLast < 128 ||
1332*4882a593Smuzhiyun              pRange->extRequestsMajorFirst > pRange->extRequestsMajorLast)) {
1333*4882a593Smuzhiyun             client->errorValue = pRange->extRequestsMajorFirst;
1334*4882a593Smuzhiyun             return BadValue;
1335*4882a593Smuzhiyun         }
1336*4882a593Smuzhiyun         if (pRange->extRequestsMinorFirst > pRange->extRequestsMinorLast) {
1337*4882a593Smuzhiyun             client->errorValue = pRange->extRequestsMinorFirst;
1338*4882a593Smuzhiyun             return BadValue;
1339*4882a593Smuzhiyun         }
1340*4882a593Smuzhiyun         if ((pRange->extRepliesMajorFirst || pRange->extRepliesMajorLast) &&
1341*4882a593Smuzhiyun             (pRange->extRepliesMajorFirst < 128 ||
1342*4882a593Smuzhiyun              pRange->extRepliesMajorLast < 128 ||
1343*4882a593Smuzhiyun              pRange->extRepliesMajorFirst > pRange->extRepliesMajorLast)) {
1344*4882a593Smuzhiyun             client->errorValue = pRange->extRepliesMajorFirst;
1345*4882a593Smuzhiyun             return BadValue;
1346*4882a593Smuzhiyun         }
1347*4882a593Smuzhiyun         if (pRange->extRepliesMinorFirst > pRange->extRepliesMinorLast) {
1348*4882a593Smuzhiyun             client->errorValue = pRange->extRepliesMinorFirst;
1349*4882a593Smuzhiyun             return BadValue;
1350*4882a593Smuzhiyun         }
1351*4882a593Smuzhiyun         if ((pRange->deliveredEventsFirst || pRange->deliveredEventsLast) &&
1352*4882a593Smuzhiyun             (pRange->deliveredEventsFirst < 2 ||
1353*4882a593Smuzhiyun              pRange->deliveredEventsLast < 2 ||
1354*4882a593Smuzhiyun              pRange->deliveredEventsFirst > pRange->deliveredEventsLast)) {
1355*4882a593Smuzhiyun             client->errorValue = pRange->deliveredEventsFirst;
1356*4882a593Smuzhiyun             return BadValue;
1357*4882a593Smuzhiyun         }
1358*4882a593Smuzhiyun         if ((pRange->deviceEventsFirst || pRange->deviceEventsLast) &&
1359*4882a593Smuzhiyun             (pRange->deviceEventsFirst < 2 ||
1360*4882a593Smuzhiyun              pRange->deviceEventsLast < 2 ||
1361*4882a593Smuzhiyun              pRange->deviceEventsFirst > pRange->deviceEventsLast)) {
1362*4882a593Smuzhiyun             client->errorValue = pRange->deviceEventsFirst;
1363*4882a593Smuzhiyun             return BadValue;
1364*4882a593Smuzhiyun         }
1365*4882a593Smuzhiyun         if (pRange->errorsFirst > pRange->errorsLast) {
1366*4882a593Smuzhiyun             client->errorValue = pRange->errorsFirst;
1367*4882a593Smuzhiyun             return BadValue;
1368*4882a593Smuzhiyun         }
1369*4882a593Smuzhiyun         if (pRange->clientStarted != xFalse && pRange->clientStarted != xTrue) {
1370*4882a593Smuzhiyun             client->errorValue = pRange->clientStarted;
1371*4882a593Smuzhiyun             return BadValue;
1372*4882a593Smuzhiyun         }
1373*4882a593Smuzhiyun         if (pRange->clientDied != xFalse && pRange->clientDied != xTrue) {
1374*4882a593Smuzhiyun             client->errorValue = pRange->clientDied;
1375*4882a593Smuzhiyun             return BadValue;
1376*4882a593Smuzhiyun         }
1377*4882a593Smuzhiyun     }                           /* end for each range */
1378*4882a593Smuzhiyun     return Success;
1379*4882a593Smuzhiyun }                               /* end RecordSanityCheckRegisterClients */
1380*4882a593Smuzhiyun 
1381*4882a593Smuzhiyun /* This is a tactical structure used to gather information about all the sets
1382*4882a593Smuzhiyun  * (RecordSetPtr) that need to be created for an RCAP in the process of
1383*4882a593Smuzhiyun  * digesting a list of RECORDRANGEs (converting it to the internal
1384*4882a593Smuzhiyun  * representation).
1385*4882a593Smuzhiyun  */
1386*4882a593Smuzhiyun typedef struct {
1387*4882a593Smuzhiyun     int nintervals;             /* number of intervals in following array */
1388*4882a593Smuzhiyun     RecordSetInterval *intervals;       /* array of intervals for this set */
1389*4882a593Smuzhiyun     int size;                   /* size of intevals array; >= nintervals */
1390*4882a593Smuzhiyun     int align;                  /* alignment restriction for set */
1391*4882a593Smuzhiyun     int offset;                 /* where to store set pointer rel. to start of RCAP */
1392*4882a593Smuzhiyun     short first, last;          /* if for extension, major opcode interval */
1393*4882a593Smuzhiyun } SetInfoRec, *SetInfoPtr;
1394*4882a593Smuzhiyun 
1395*4882a593Smuzhiyun #if defined(ERR) && defined(__sun)
1396*4882a593Smuzhiyun #undef ERR /* Avoid conflict with Solaris <sys/regset.h> */
1397*4882a593Smuzhiyun #endif
1398*4882a593Smuzhiyun 
1399*4882a593Smuzhiyun /* These constant are used to index into an array of SetInfoRec. */
1400*4882a593Smuzhiyun enum { REQ,                     /* set info for requests */
1401*4882a593Smuzhiyun     REP,                        /* set info for replies */
1402*4882a593Smuzhiyun     ERR,                        /* set info for errors */
1403*4882a593Smuzhiyun     DEV,                        /* set info for device events */
1404*4882a593Smuzhiyun     DLEV,                       /* set info for delivered events */
1405*4882a593Smuzhiyun     PREDEFSETS
1406*4882a593Smuzhiyun };                              /* number of predefined array entries */
1407*4882a593Smuzhiyun 
1408*4882a593Smuzhiyun /* RecordAllocIntervals
1409*4882a593Smuzhiyun  *
1410*4882a593Smuzhiyun  * Arguments:
1411*4882a593Smuzhiyun  *	psi is a pointer to a SetInfoRec whose intervals pointer is NULL.
1412*4882a593Smuzhiyun  *	nIntervals is the desired size of the intervals array.
1413*4882a593Smuzhiyun  *
1414*4882a593Smuzhiyun  * Returns: BadAlloc if a memory allocation error occurred, else Success.
1415*4882a593Smuzhiyun  *
1416*4882a593Smuzhiyun  * Side Effects:
1417*4882a593Smuzhiyun  *	If Success is returned, psi->intervals is a pointer to size
1418*4882a593Smuzhiyun  *	RecordSetIntervals, all zeroed, and psi->size is set to size.
1419*4882a593Smuzhiyun  */
1420*4882a593Smuzhiyun static int
RecordAllocIntervals(SetInfoPtr psi,int nIntervals)1421*4882a593Smuzhiyun RecordAllocIntervals(SetInfoPtr psi, int nIntervals)
1422*4882a593Smuzhiyun {
1423*4882a593Smuzhiyun     assert(!psi->intervals);
1424*4882a593Smuzhiyun     psi->intervals = xallocarray(nIntervals, sizeof(RecordSetInterval));
1425*4882a593Smuzhiyun     if (!psi->intervals)
1426*4882a593Smuzhiyun         return BadAlloc;
1427*4882a593Smuzhiyun     memset(psi->intervals, 0, nIntervals * sizeof(RecordSetInterval));
1428*4882a593Smuzhiyun     psi->size = nIntervals;
1429*4882a593Smuzhiyun     return Success;
1430*4882a593Smuzhiyun }                               /* end RecordAllocIntervals */
1431*4882a593Smuzhiyun 
1432*4882a593Smuzhiyun /* RecordConvertRangesToIntervals
1433*4882a593Smuzhiyun  *
1434*4882a593Smuzhiyun  * Arguments:
1435*4882a593Smuzhiyun  *	psi is a pointer to the SetInfoRec we are building.
1436*4882a593Smuzhiyun  *	pRanges is an array of xRecordRanges.
1437*4882a593Smuzhiyun  *	nRanges is the number of elements in pRanges.
1438*4882a593Smuzhiyun  *	byteoffset is the offset from the start of an xRecordRange of the
1439*4882a593Smuzhiyun  *	  two bytes (1 for first, 1 for last) we are interested in.
1440*4882a593Smuzhiyun  *	pExtSetInfo, if non-NULL, indicates that the two bytes mentioned
1441*4882a593Smuzhiyun  *	  above are followed by four bytes (2 for first, 2 for last)
1442*4882a593Smuzhiyun  *	  representing a minor opcode range, and this information should be
1443*4882a593Smuzhiyun  *	  stored in one of the SetInfoRecs starting at pExtSetInfo.
1444*4882a593Smuzhiyun  *	pnExtSetInfo is the number of elements in the pExtSetInfo array.
1445*4882a593Smuzhiyun  *
1446*4882a593Smuzhiyun  * Returns:  BadAlloc if a memory allocation error occurred, else Success.
1447*4882a593Smuzhiyun  *
1448*4882a593Smuzhiyun  * Side Effects:
1449*4882a593Smuzhiyun  *	The slice of pRanges indicated by byteoffset is stored in psi.
1450*4882a593Smuzhiyun  *	If pExtSetInfo is non-NULL, minor opcode intervals are stored
1451*4882a593Smuzhiyun  *	in an existing SetInfoRec if the major opcode interval matches, else
1452*4882a593Smuzhiyun  *	they are stored in a new SetInfoRec, and *pnExtSetInfo is
1453*4882a593Smuzhiyun  *	increased accordingly.
1454*4882a593Smuzhiyun  */
1455*4882a593Smuzhiyun static int
RecordConvertRangesToIntervals(SetInfoPtr psi,xRecordRange * pRanges,int nRanges,int byteoffset,SetInfoPtr pExtSetInfo,int * pnExtSetInfo)1456*4882a593Smuzhiyun RecordConvertRangesToIntervals(SetInfoPtr psi,
1457*4882a593Smuzhiyun                                xRecordRange * pRanges,
1458*4882a593Smuzhiyun                                int nRanges,
1459*4882a593Smuzhiyun                                int byteoffset,
1460*4882a593Smuzhiyun                                SetInfoPtr pExtSetInfo, int *pnExtSetInfo)
1461*4882a593Smuzhiyun {
1462*4882a593Smuzhiyun     int i;
1463*4882a593Smuzhiyun     CARD8 *pCARD8;
1464*4882a593Smuzhiyun     int first, last;
1465*4882a593Smuzhiyun     int err;
1466*4882a593Smuzhiyun 
1467*4882a593Smuzhiyun     for (i = 0; i < nRanges; i++, pRanges++) {
1468*4882a593Smuzhiyun         pCARD8 = ((CARD8 *) pRanges) + byteoffset;
1469*4882a593Smuzhiyun         first = pCARD8[0];
1470*4882a593Smuzhiyun         last = pCARD8[1];
1471*4882a593Smuzhiyun         if (first || last) {
1472*4882a593Smuzhiyun             if (!psi->intervals) {
1473*4882a593Smuzhiyun                 err = RecordAllocIntervals(psi, 2 * (nRanges - i));
1474*4882a593Smuzhiyun                 if (err != Success)
1475*4882a593Smuzhiyun                     return err;
1476*4882a593Smuzhiyun             }
1477*4882a593Smuzhiyun             psi->intervals[psi->nintervals].first = first;
1478*4882a593Smuzhiyun             psi->intervals[psi->nintervals].last = last;
1479*4882a593Smuzhiyun             psi->nintervals++;
1480*4882a593Smuzhiyun             assert(psi->nintervals <= psi->size);
1481*4882a593Smuzhiyun             if (pExtSetInfo) {
1482*4882a593Smuzhiyun                 SetInfoPtr pesi = pExtSetInfo;
1483*4882a593Smuzhiyun                 CARD16 *pCARD16 = (CARD16 *) (pCARD8 + 2);
1484*4882a593Smuzhiyun                 int j;
1485*4882a593Smuzhiyun 
1486*4882a593Smuzhiyun                 for (j = 0; j < *pnExtSetInfo; j++, pesi++) {
1487*4882a593Smuzhiyun                     if ((first == pesi->first) && (last == pesi->last))
1488*4882a593Smuzhiyun                         break;
1489*4882a593Smuzhiyun                 }
1490*4882a593Smuzhiyun                 if (j == *pnExtSetInfo) {
1491*4882a593Smuzhiyun                     err = RecordAllocIntervals(pesi, 2 * (nRanges - i));
1492*4882a593Smuzhiyun                     if (err != Success)
1493*4882a593Smuzhiyun                         return err;
1494*4882a593Smuzhiyun                     pesi->first = first;
1495*4882a593Smuzhiyun                     pesi->last = last;
1496*4882a593Smuzhiyun                     (*pnExtSetInfo)++;
1497*4882a593Smuzhiyun                 }
1498*4882a593Smuzhiyun                 pesi->intervals[pesi->nintervals].first = pCARD16[0];
1499*4882a593Smuzhiyun                 pesi->intervals[pesi->nintervals].last = pCARD16[1];
1500*4882a593Smuzhiyun                 pesi->nintervals++;
1501*4882a593Smuzhiyun                 assert(pesi->nintervals <= pesi->size);
1502*4882a593Smuzhiyun             }
1503*4882a593Smuzhiyun         }
1504*4882a593Smuzhiyun     }
1505*4882a593Smuzhiyun     return Success;
1506*4882a593Smuzhiyun }                               /* end RecordConvertRangesToIntervals */
1507*4882a593Smuzhiyun 
1508*4882a593Smuzhiyun #define offset_of(_structure, _field) \
1509*4882a593Smuzhiyun     ((char *)(& (_structure . _field)) - (char *)(&_structure))
1510*4882a593Smuzhiyun 
1511*4882a593Smuzhiyun /* RecordRegisterClients
1512*4882a593Smuzhiyun  *
1513*4882a593Smuzhiyun  * Arguments:
1514*4882a593Smuzhiyun  *	pContext is the context on which to register the clients.
1515*4882a593Smuzhiyun  *	client is the client that issued the RecordCreateContext or
1516*4882a593Smuzhiyun  *	  RecordRegisterClients request.
1517*4882a593Smuzhiyun  *	stuff is a pointer to the request.
1518*4882a593Smuzhiyun  *
1519*4882a593Smuzhiyun  * Returns:
1520*4882a593Smuzhiyun  *	Any one of several possible error values defined by the protocol.
1521*4882a593Smuzhiyun  *	Success if everything is OK.
1522*4882a593Smuzhiyun  *
1523*4882a593Smuzhiyun  * Side Effects:
1524*4882a593Smuzhiyun  *	If different element headers are specified, the context is flushed.
1525*4882a593Smuzhiyun  *	If any of the specified clients are already registered on the
1526*4882a593Smuzhiyun  *	context, they are first unregistered.  A new RCAP is created to
1527*4882a593Smuzhiyun  *	hold the specified protocol and clients, and it is linked onto the
1528*4882a593Smuzhiyun  *	context.  If the context is enabled, appropriate hooks are installed
1529*4882a593Smuzhiyun  *	to record the new clients and protocol.
1530*4882a593Smuzhiyun  */
1531*4882a593Smuzhiyun static int
RecordRegisterClients(RecordContextPtr pContext,ClientPtr client,xRecordRegisterClientsReq * stuff)1532*4882a593Smuzhiyun RecordRegisterClients(RecordContextPtr pContext, ClientPtr client,
1533*4882a593Smuzhiyun                       xRecordRegisterClientsReq * stuff)
1534*4882a593Smuzhiyun {
1535*4882a593Smuzhiyun     int err;
1536*4882a593Smuzhiyun     int i;
1537*4882a593Smuzhiyun     SetInfoPtr si;
1538*4882a593Smuzhiyun     int maxSets;
1539*4882a593Smuzhiyun     int nExtReqSets = 0;
1540*4882a593Smuzhiyun     int nExtRepSets = 0;
1541*4882a593Smuzhiyun     int extReqSetsOffset = 0;
1542*4882a593Smuzhiyun     int extRepSetsOffset = 0;
1543*4882a593Smuzhiyun     SetInfoPtr pExtReqSets, pExtRepSets;
1544*4882a593Smuzhiyun     int clientListOffset;
1545*4882a593Smuzhiyun     XID *pCanonClients;
1546*4882a593Smuzhiyun     int clientStarted = 0, clientDied = 0;
1547*4882a593Smuzhiyun     xRecordRange *pRanges, rr;
1548*4882a593Smuzhiyun     int nClients;
1549*4882a593Smuzhiyun     int sizeClients;
1550*4882a593Smuzhiyun     int totRCAPsize;
1551*4882a593Smuzhiyun     RecordClientsAndProtocolPtr pRCAP;
1552*4882a593Smuzhiyun     int pad;
1553*4882a593Smuzhiyun     XID recordingClient;
1554*4882a593Smuzhiyun 
1555*4882a593Smuzhiyun     /* do all sanity checking up front */
1556*4882a593Smuzhiyun 
1557*4882a593Smuzhiyun     err = RecordSanityCheckRegisterClients(pContext, client, stuff);
1558*4882a593Smuzhiyun     if (err != Success)
1559*4882a593Smuzhiyun         return err;
1560*4882a593Smuzhiyun 
1561*4882a593Smuzhiyun     /* if element headers changed, flush buffer */
1562*4882a593Smuzhiyun 
1563*4882a593Smuzhiyun     if (pContext->elemHeaders != stuff->elementHeader) {
1564*4882a593Smuzhiyun         RecordFlushReplyBuffer(pContext, NULL, 0, NULL, 0);
1565*4882a593Smuzhiyun         pContext->elemHeaders = stuff->elementHeader;
1566*4882a593Smuzhiyun     }
1567*4882a593Smuzhiyun 
1568*4882a593Smuzhiyun     nClients = stuff->nClients;
1569*4882a593Smuzhiyun     if (!nClients)
1570*4882a593Smuzhiyun         /* if empty clients list, we're done. */
1571*4882a593Smuzhiyun         return Success;
1572*4882a593Smuzhiyun 
1573*4882a593Smuzhiyun     recordingClient = pContext->pRecordingClient ?
1574*4882a593Smuzhiyun         pContext->pRecordingClient->clientAsMask : 0;
1575*4882a593Smuzhiyun     pCanonClients = RecordCanonicalizeClientSpecifiers((XID *) &stuff[1],
1576*4882a593Smuzhiyun                                                        &nClients,
1577*4882a593Smuzhiyun                                                        recordingClient);
1578*4882a593Smuzhiyun     if (!pCanonClients)
1579*4882a593Smuzhiyun         return BadAlloc;
1580*4882a593Smuzhiyun 
1581*4882a593Smuzhiyun     /* We may have to create as many as one set for each "predefined"
1582*4882a593Smuzhiyun      * protocol types, plus one per range for extension reuests, plus one per
1583*4882a593Smuzhiyun      * range for extension replies.
1584*4882a593Smuzhiyun      */
1585*4882a593Smuzhiyun     maxSets = PREDEFSETS + 2 * stuff->nRanges;
1586*4882a593Smuzhiyun     si = xallocarray(maxSets, sizeof(SetInfoRec));
1587*4882a593Smuzhiyun     if (!si) {
1588*4882a593Smuzhiyun         err = BadAlloc;
1589*4882a593Smuzhiyun         goto bailout;
1590*4882a593Smuzhiyun     }
1591*4882a593Smuzhiyun     memset(si, 0, sizeof(SetInfoRec) * maxSets);
1592*4882a593Smuzhiyun 
1593*4882a593Smuzhiyun     /* theoretically you must do this because NULL may not be all-bits-zero */
1594*4882a593Smuzhiyun     for (i = 0; i < maxSets; i++)
1595*4882a593Smuzhiyun         si[i].intervals = NULL;
1596*4882a593Smuzhiyun 
1597*4882a593Smuzhiyun     pExtReqSets = si + PREDEFSETS;
1598*4882a593Smuzhiyun     pExtRepSets = pExtReqSets + stuff->nRanges;
1599*4882a593Smuzhiyun 
1600*4882a593Smuzhiyun     pRanges = (xRecordRange *) (((XID *) &stuff[1]) + stuff->nClients);
1601*4882a593Smuzhiyun 
1602*4882a593Smuzhiyun     err = RecordConvertRangesToIntervals(&si[REQ], pRanges, stuff->nRanges,
1603*4882a593Smuzhiyun                                          offset_of(rr, coreRequestsFirst), NULL,
1604*4882a593Smuzhiyun                                          NULL);
1605*4882a593Smuzhiyun     if (err != Success)
1606*4882a593Smuzhiyun         goto bailout;
1607*4882a593Smuzhiyun 
1608*4882a593Smuzhiyun     err = RecordConvertRangesToIntervals(&si[REQ], pRanges, stuff->nRanges,
1609*4882a593Smuzhiyun                                          offset_of(rr, extRequestsMajorFirst),
1610*4882a593Smuzhiyun                                          pExtReqSets, &nExtReqSets);
1611*4882a593Smuzhiyun     if (err != Success)
1612*4882a593Smuzhiyun         goto bailout;
1613*4882a593Smuzhiyun 
1614*4882a593Smuzhiyun     err = RecordConvertRangesToIntervals(&si[REP], pRanges, stuff->nRanges,
1615*4882a593Smuzhiyun                                          offset_of(rr, coreRepliesFirst), NULL,
1616*4882a593Smuzhiyun                                          NULL);
1617*4882a593Smuzhiyun     if (err != Success)
1618*4882a593Smuzhiyun         goto bailout;
1619*4882a593Smuzhiyun 
1620*4882a593Smuzhiyun     err = RecordConvertRangesToIntervals(&si[REP], pRanges, stuff->nRanges,
1621*4882a593Smuzhiyun                                          offset_of(rr, extRepliesMajorFirst),
1622*4882a593Smuzhiyun                                          pExtRepSets, &nExtRepSets);
1623*4882a593Smuzhiyun     if (err != Success)
1624*4882a593Smuzhiyun         goto bailout;
1625*4882a593Smuzhiyun 
1626*4882a593Smuzhiyun     err = RecordConvertRangesToIntervals(&si[ERR], pRanges, stuff->nRanges,
1627*4882a593Smuzhiyun                                          offset_of(rr, errorsFirst), NULL,
1628*4882a593Smuzhiyun                                          NULL);
1629*4882a593Smuzhiyun     if (err != Success)
1630*4882a593Smuzhiyun         goto bailout;
1631*4882a593Smuzhiyun 
1632*4882a593Smuzhiyun     err = RecordConvertRangesToIntervals(&si[DLEV], pRanges, stuff->nRanges,
1633*4882a593Smuzhiyun                                          offset_of(rr, deliveredEventsFirst),
1634*4882a593Smuzhiyun                                          NULL, NULL);
1635*4882a593Smuzhiyun     if (err != Success)
1636*4882a593Smuzhiyun         goto bailout;
1637*4882a593Smuzhiyun 
1638*4882a593Smuzhiyun     err = RecordConvertRangesToIntervals(&si[DEV], pRanges, stuff->nRanges,
1639*4882a593Smuzhiyun                                          offset_of(rr, deviceEventsFirst), NULL,
1640*4882a593Smuzhiyun                                          NULL);
1641*4882a593Smuzhiyun     if (err != Success)
1642*4882a593Smuzhiyun         goto bailout;
1643*4882a593Smuzhiyun 
1644*4882a593Smuzhiyun     /* collect client-started and client-died */
1645*4882a593Smuzhiyun 
1646*4882a593Smuzhiyun     for (i = 0; i < stuff->nRanges; i++) {
1647*4882a593Smuzhiyun         if (pRanges[i].clientStarted)
1648*4882a593Smuzhiyun             clientStarted = TRUE;
1649*4882a593Smuzhiyun         if (pRanges[i].clientDied)
1650*4882a593Smuzhiyun             clientDied = TRUE;
1651*4882a593Smuzhiyun     }
1652*4882a593Smuzhiyun 
1653*4882a593Smuzhiyun     /*  We now have all the information collected to create all the sets,
1654*4882a593Smuzhiyun      * and we can compute the total memory required for the RCAP.
1655*4882a593Smuzhiyun      */
1656*4882a593Smuzhiyun 
1657*4882a593Smuzhiyun     totRCAPsize = sizeof(RecordClientsAndProtocolRec);
1658*4882a593Smuzhiyun 
1659*4882a593Smuzhiyun     /* leave a little room to grow before forcing a separate allocation */
1660*4882a593Smuzhiyun     sizeClients = nClients + CLIENT_ARRAY_GROWTH_INCREMENT;
1661*4882a593Smuzhiyun     pad = RecordPadAlign(totRCAPsize, sizeof(XID));
1662*4882a593Smuzhiyun     clientListOffset = totRCAPsize + pad;
1663*4882a593Smuzhiyun     totRCAPsize += pad + sizeClients * sizeof(XID);
1664*4882a593Smuzhiyun 
1665*4882a593Smuzhiyun     if (nExtReqSets) {
1666*4882a593Smuzhiyun         pad = RecordPadAlign(totRCAPsize, sizeof(RecordSetPtr));
1667*4882a593Smuzhiyun         extReqSetsOffset = totRCAPsize + pad;
1668*4882a593Smuzhiyun         totRCAPsize += pad + (nExtReqSets + 1) * sizeof(RecordMinorOpRec);
1669*4882a593Smuzhiyun     }
1670*4882a593Smuzhiyun     if (nExtRepSets) {
1671*4882a593Smuzhiyun         pad = RecordPadAlign(totRCAPsize, sizeof(RecordSetPtr));
1672*4882a593Smuzhiyun         extRepSetsOffset = totRCAPsize + pad;
1673*4882a593Smuzhiyun         totRCAPsize += pad + (nExtRepSets + 1) * sizeof(RecordMinorOpRec);
1674*4882a593Smuzhiyun     }
1675*4882a593Smuzhiyun 
1676*4882a593Smuzhiyun     for (i = 0; i < maxSets; i++) {
1677*4882a593Smuzhiyun         if (si[i].nintervals) {
1678*4882a593Smuzhiyun             si[i].size =
1679*4882a593Smuzhiyun                 RecordSetMemoryRequirements(si[i].intervals, si[i].nintervals,
1680*4882a593Smuzhiyun                                             &si[i].align);
1681*4882a593Smuzhiyun             pad = RecordPadAlign(totRCAPsize, si[i].align);
1682*4882a593Smuzhiyun             si[i].offset = pad + totRCAPsize;
1683*4882a593Smuzhiyun             totRCAPsize += pad + si[i].size;
1684*4882a593Smuzhiyun         }
1685*4882a593Smuzhiyun     }
1686*4882a593Smuzhiyun 
1687*4882a593Smuzhiyun     /* allocate memory for the whole RCAP */
1688*4882a593Smuzhiyun 
1689*4882a593Smuzhiyun     pRCAP = (RecordClientsAndProtocolPtr) malloc(totRCAPsize);
1690*4882a593Smuzhiyun     if (!pRCAP) {
1691*4882a593Smuzhiyun         err = BadAlloc;
1692*4882a593Smuzhiyun         goto bailout;
1693*4882a593Smuzhiyun     }
1694*4882a593Smuzhiyun 
1695*4882a593Smuzhiyun     /* fill in the RCAP */
1696*4882a593Smuzhiyun 
1697*4882a593Smuzhiyun     pRCAP->pContext = pContext;
1698*4882a593Smuzhiyun     pRCAP->pClientIDs = (XID *) ((char *) pRCAP + clientListOffset);
1699*4882a593Smuzhiyun     pRCAP->numClients = nClients;
1700*4882a593Smuzhiyun     pRCAP->sizeClients = sizeClients;
1701*4882a593Smuzhiyun     pRCAP->clientIDsSeparatelyAllocated = 0;
1702*4882a593Smuzhiyun     for (i = 0; i < nClients; i++) {
1703*4882a593Smuzhiyun         RecordDeleteClientFromContext(pContext, pCanonClients[i]);
1704*4882a593Smuzhiyun         pRCAP->pClientIDs[i] = pCanonClients[i];
1705*4882a593Smuzhiyun     }
1706*4882a593Smuzhiyun 
1707*4882a593Smuzhiyun     /* create all the sets */
1708*4882a593Smuzhiyun 
1709*4882a593Smuzhiyun     if (si[REQ].intervals) {
1710*4882a593Smuzhiyun         pRCAP->pRequestMajorOpSet =
1711*4882a593Smuzhiyun             RecordCreateSet(si[REQ].intervals, si[REQ].nintervals,
1712*4882a593Smuzhiyun                             (RecordSetPtr) ((char *) pRCAP + si[REQ].offset),
1713*4882a593Smuzhiyun                             si[REQ].size);
1714*4882a593Smuzhiyun     }
1715*4882a593Smuzhiyun     else
1716*4882a593Smuzhiyun         pRCAP->pRequestMajorOpSet = NULL;
1717*4882a593Smuzhiyun 
1718*4882a593Smuzhiyun     if (si[REP].intervals) {
1719*4882a593Smuzhiyun         pRCAP->pReplyMajorOpSet =
1720*4882a593Smuzhiyun             RecordCreateSet(si[REP].intervals, si[REP].nintervals,
1721*4882a593Smuzhiyun                             (RecordSetPtr) ((char *) pRCAP + si[REP].offset),
1722*4882a593Smuzhiyun                             si[REP].size);
1723*4882a593Smuzhiyun     }
1724*4882a593Smuzhiyun     else
1725*4882a593Smuzhiyun         pRCAP->pReplyMajorOpSet = NULL;
1726*4882a593Smuzhiyun 
1727*4882a593Smuzhiyun     if (si[ERR].intervals) {
1728*4882a593Smuzhiyun         pRCAP->pErrorSet =
1729*4882a593Smuzhiyun             RecordCreateSet(si[ERR].intervals, si[ERR].nintervals,
1730*4882a593Smuzhiyun                             (RecordSetPtr) ((char *) pRCAP + si[ERR].offset),
1731*4882a593Smuzhiyun                             si[ERR].size);
1732*4882a593Smuzhiyun     }
1733*4882a593Smuzhiyun     else
1734*4882a593Smuzhiyun         pRCAP->pErrorSet = NULL;
1735*4882a593Smuzhiyun 
1736*4882a593Smuzhiyun     if (si[DEV].intervals) {
1737*4882a593Smuzhiyun         pRCAP->pDeviceEventSet =
1738*4882a593Smuzhiyun             RecordCreateSet(si[DEV].intervals, si[DEV].nintervals,
1739*4882a593Smuzhiyun                             (RecordSetPtr) ((char *) pRCAP + si[DEV].offset),
1740*4882a593Smuzhiyun                             si[DEV].size);
1741*4882a593Smuzhiyun     }
1742*4882a593Smuzhiyun     else
1743*4882a593Smuzhiyun         pRCAP->pDeviceEventSet = NULL;
1744*4882a593Smuzhiyun 
1745*4882a593Smuzhiyun     if (si[DLEV].intervals) {
1746*4882a593Smuzhiyun         pRCAP->pDeliveredEventSet =
1747*4882a593Smuzhiyun             RecordCreateSet(si[DLEV].intervals, si[DLEV].nintervals,
1748*4882a593Smuzhiyun                             (RecordSetPtr) ((char *) pRCAP + si[DLEV].offset),
1749*4882a593Smuzhiyun                             si[DLEV].size);
1750*4882a593Smuzhiyun     }
1751*4882a593Smuzhiyun     else
1752*4882a593Smuzhiyun         pRCAP->pDeliveredEventSet = NULL;
1753*4882a593Smuzhiyun 
1754*4882a593Smuzhiyun     if (nExtReqSets) {
1755*4882a593Smuzhiyun         pRCAP->pRequestMinOpInfo = (RecordMinorOpPtr)
1756*4882a593Smuzhiyun             ((char *) pRCAP + extReqSetsOffset);
1757*4882a593Smuzhiyun         pRCAP->pRequestMinOpInfo[0].count = nExtReqSets;
1758*4882a593Smuzhiyun         for (i = 0; i < nExtReqSets; i++, pExtReqSets++) {
1759*4882a593Smuzhiyun             pRCAP->pRequestMinOpInfo[i + 1].major.first = pExtReqSets->first;
1760*4882a593Smuzhiyun             pRCAP->pRequestMinOpInfo[i + 1].major.last = pExtReqSets->last;
1761*4882a593Smuzhiyun             pRCAP->pRequestMinOpInfo[i + 1].major.pMinOpSet =
1762*4882a593Smuzhiyun                 RecordCreateSet(pExtReqSets->intervals,
1763*4882a593Smuzhiyun                                 pExtReqSets->nintervals,
1764*4882a593Smuzhiyun                                 (RecordSetPtr) ((char *) pRCAP +
1765*4882a593Smuzhiyun                                                 pExtReqSets->offset),
1766*4882a593Smuzhiyun                                 pExtReqSets->size);
1767*4882a593Smuzhiyun         }
1768*4882a593Smuzhiyun     }
1769*4882a593Smuzhiyun     else
1770*4882a593Smuzhiyun         pRCAP->pRequestMinOpInfo = NULL;
1771*4882a593Smuzhiyun 
1772*4882a593Smuzhiyun     if (nExtRepSets) {
1773*4882a593Smuzhiyun         pRCAP->pReplyMinOpInfo = (RecordMinorOpPtr)
1774*4882a593Smuzhiyun             ((char *) pRCAP + extRepSetsOffset);
1775*4882a593Smuzhiyun         pRCAP->pReplyMinOpInfo[0].count = nExtRepSets;
1776*4882a593Smuzhiyun         for (i = 0; i < nExtRepSets; i++, pExtRepSets++) {
1777*4882a593Smuzhiyun             pRCAP->pReplyMinOpInfo[i + 1].major.first = pExtRepSets->first;
1778*4882a593Smuzhiyun             pRCAP->pReplyMinOpInfo[i + 1].major.last = pExtRepSets->last;
1779*4882a593Smuzhiyun             pRCAP->pReplyMinOpInfo[i + 1].major.pMinOpSet =
1780*4882a593Smuzhiyun                 RecordCreateSet(pExtRepSets->intervals,
1781*4882a593Smuzhiyun                                 pExtRepSets->nintervals,
1782*4882a593Smuzhiyun                                 (RecordSetPtr) ((char *) pRCAP +
1783*4882a593Smuzhiyun                                                 pExtRepSets->offset),
1784*4882a593Smuzhiyun                                 pExtRepSets->size);
1785*4882a593Smuzhiyun         }
1786*4882a593Smuzhiyun     }
1787*4882a593Smuzhiyun     else
1788*4882a593Smuzhiyun         pRCAP->pReplyMinOpInfo = NULL;
1789*4882a593Smuzhiyun 
1790*4882a593Smuzhiyun     pRCAP->clientStarted = clientStarted;
1791*4882a593Smuzhiyun     pRCAP->clientDied = clientDied;
1792*4882a593Smuzhiyun 
1793*4882a593Smuzhiyun     /* link the RCAP onto the context */
1794*4882a593Smuzhiyun 
1795*4882a593Smuzhiyun     pRCAP->pNextRCAP = pContext->pListOfRCAP;
1796*4882a593Smuzhiyun     pContext->pListOfRCAP = pRCAP;
1797*4882a593Smuzhiyun 
1798*4882a593Smuzhiyun     if (pContext->pRecordingClient)     /* context enabled */
1799*4882a593Smuzhiyun         RecordInstallHooks(pRCAP, 0);
1800*4882a593Smuzhiyun 
1801*4882a593Smuzhiyun  bailout:
1802*4882a593Smuzhiyun     if (si) {
1803*4882a593Smuzhiyun         for (i = 0; i < maxSets; i++)
1804*4882a593Smuzhiyun             free(si[i].intervals);
1805*4882a593Smuzhiyun         free(si);
1806*4882a593Smuzhiyun     }
1807*4882a593Smuzhiyun     if (pCanonClients && pCanonClients != (XID *) &stuff[1])
1808*4882a593Smuzhiyun         free(pCanonClients);
1809*4882a593Smuzhiyun     return err;
1810*4882a593Smuzhiyun }                               /* RecordRegisterClients */
1811*4882a593Smuzhiyun 
1812*4882a593Smuzhiyun /* Proc functions all take a client argument, execute the request in
1813*4882a593Smuzhiyun  * client->requestBuffer, and return a protocol error status.
1814*4882a593Smuzhiyun  */
1815*4882a593Smuzhiyun 
1816*4882a593Smuzhiyun static int
ProcRecordQueryVersion(ClientPtr client)1817*4882a593Smuzhiyun ProcRecordQueryVersion(ClientPtr client)
1818*4882a593Smuzhiyun {
1819*4882a593Smuzhiyun     /* REQUEST(xRecordQueryVersionReq); */
1820*4882a593Smuzhiyun     xRecordQueryVersionReply rep = {
1821*4882a593Smuzhiyun         .type = X_Reply,
1822*4882a593Smuzhiyun         .sequenceNumber = client->sequence,
1823*4882a593Smuzhiyun         .length = 0,
1824*4882a593Smuzhiyun         .majorVersion = SERVER_RECORD_MAJOR_VERSION,
1825*4882a593Smuzhiyun         .minorVersion = SERVER_RECORD_MINOR_VERSION
1826*4882a593Smuzhiyun     };
1827*4882a593Smuzhiyun 
1828*4882a593Smuzhiyun     REQUEST_SIZE_MATCH(xRecordQueryVersionReq);
1829*4882a593Smuzhiyun     if (client->swapped) {
1830*4882a593Smuzhiyun         swaps(&rep.sequenceNumber);
1831*4882a593Smuzhiyun         swaps(&rep.majorVersion);
1832*4882a593Smuzhiyun         swaps(&rep.minorVersion);
1833*4882a593Smuzhiyun     }
1834*4882a593Smuzhiyun     WriteToClient(client, sizeof(xRecordQueryVersionReply), &rep);
1835*4882a593Smuzhiyun     return Success;
1836*4882a593Smuzhiyun }                               /* ProcRecordQueryVersion */
1837*4882a593Smuzhiyun 
1838*4882a593Smuzhiyun static int
ProcRecordCreateContext(ClientPtr client)1839*4882a593Smuzhiyun ProcRecordCreateContext(ClientPtr client)
1840*4882a593Smuzhiyun {
1841*4882a593Smuzhiyun     REQUEST(xRecordCreateContextReq);
1842*4882a593Smuzhiyun     RecordContextPtr pContext;
1843*4882a593Smuzhiyun     RecordContextPtr *ppNewAllContexts = NULL;
1844*4882a593Smuzhiyun     int err = BadAlloc;
1845*4882a593Smuzhiyun 
1846*4882a593Smuzhiyun     REQUEST_AT_LEAST_SIZE(xRecordCreateContextReq);
1847*4882a593Smuzhiyun     LEGAL_NEW_RESOURCE(stuff->context, client);
1848*4882a593Smuzhiyun 
1849*4882a593Smuzhiyun     pContext = (RecordContextPtr) malloc(sizeof(RecordContextRec));
1850*4882a593Smuzhiyun     if (!pContext)
1851*4882a593Smuzhiyun         goto bailout;
1852*4882a593Smuzhiyun 
1853*4882a593Smuzhiyun     /* make sure there is room in ppAllContexts to store the new context */
1854*4882a593Smuzhiyun 
1855*4882a593Smuzhiyun     ppNewAllContexts =
1856*4882a593Smuzhiyun         reallocarray(ppAllContexts, numContexts + 1, sizeof(RecordContextPtr));
1857*4882a593Smuzhiyun     if (!ppNewAllContexts)
1858*4882a593Smuzhiyun         goto bailout;
1859*4882a593Smuzhiyun     ppAllContexts = ppNewAllContexts;
1860*4882a593Smuzhiyun 
1861*4882a593Smuzhiyun     pContext->id = stuff->context;
1862*4882a593Smuzhiyun     pContext->pRecordingClient = NULL;
1863*4882a593Smuzhiyun     pContext->pListOfRCAP = NULL;
1864*4882a593Smuzhiyun     pContext->elemHeaders = 0;
1865*4882a593Smuzhiyun     pContext->bufCategory = 0;
1866*4882a593Smuzhiyun     pContext->numBufBytes = 0;
1867*4882a593Smuzhiyun     pContext->pBufClient = NULL;
1868*4882a593Smuzhiyun     pContext->continuedReply = 0;
1869*4882a593Smuzhiyun     pContext->inFlush = 0;
1870*4882a593Smuzhiyun 
1871*4882a593Smuzhiyun     err = RecordRegisterClients(pContext, client,
1872*4882a593Smuzhiyun                                 (xRecordRegisterClientsReq *) stuff);
1873*4882a593Smuzhiyun     if (err != Success)
1874*4882a593Smuzhiyun         goto bailout;
1875*4882a593Smuzhiyun 
1876*4882a593Smuzhiyun     if (AddResource(pContext->id, RTContext, pContext)) {
1877*4882a593Smuzhiyun         ppAllContexts[numContexts++] = pContext;
1878*4882a593Smuzhiyun         return Success;
1879*4882a593Smuzhiyun     }
1880*4882a593Smuzhiyun     else {
1881*4882a593Smuzhiyun         return BadAlloc;
1882*4882a593Smuzhiyun     }
1883*4882a593Smuzhiyun  bailout:
1884*4882a593Smuzhiyun     free(pContext);
1885*4882a593Smuzhiyun     return err;
1886*4882a593Smuzhiyun }                               /* ProcRecordCreateContext */
1887*4882a593Smuzhiyun 
1888*4882a593Smuzhiyun static int
ProcRecordRegisterClients(ClientPtr client)1889*4882a593Smuzhiyun ProcRecordRegisterClients(ClientPtr client)
1890*4882a593Smuzhiyun {
1891*4882a593Smuzhiyun     RecordContextPtr pContext;
1892*4882a593Smuzhiyun 
1893*4882a593Smuzhiyun     REQUEST(xRecordRegisterClientsReq);
1894*4882a593Smuzhiyun 
1895*4882a593Smuzhiyun     REQUEST_AT_LEAST_SIZE(xRecordRegisterClientsReq);
1896*4882a593Smuzhiyun     VERIFY_CONTEXT(pContext, stuff->context, client);
1897*4882a593Smuzhiyun 
1898*4882a593Smuzhiyun     return RecordRegisterClients(pContext, client, stuff);
1899*4882a593Smuzhiyun }                               /* ProcRecordRegisterClients */
1900*4882a593Smuzhiyun 
1901*4882a593Smuzhiyun static int
ProcRecordUnregisterClients(ClientPtr client)1902*4882a593Smuzhiyun ProcRecordUnregisterClients(ClientPtr client)
1903*4882a593Smuzhiyun {
1904*4882a593Smuzhiyun     RecordContextPtr pContext;
1905*4882a593Smuzhiyun     int err;
1906*4882a593Smuzhiyun 
1907*4882a593Smuzhiyun     REQUEST(xRecordUnregisterClientsReq);
1908*4882a593Smuzhiyun     XID *pCanonClients;
1909*4882a593Smuzhiyun     int nClients;
1910*4882a593Smuzhiyun     int i;
1911*4882a593Smuzhiyun 
1912*4882a593Smuzhiyun     REQUEST_AT_LEAST_SIZE(xRecordUnregisterClientsReq);
1913*4882a593Smuzhiyun     if (INT_MAX / 4 < stuff->nClients ||
1914*4882a593Smuzhiyun         (client->req_len << 2) - SIZEOF(xRecordUnregisterClientsReq) !=
1915*4882a593Smuzhiyun         4 * stuff->nClients)
1916*4882a593Smuzhiyun         return BadLength;
1917*4882a593Smuzhiyun     VERIFY_CONTEXT(pContext, stuff->context, client);
1918*4882a593Smuzhiyun     err = RecordSanityCheckClientSpecifiers(client, (XID *) &stuff[1],
1919*4882a593Smuzhiyun                                             stuff->nClients, 0);
1920*4882a593Smuzhiyun     if (err != Success)
1921*4882a593Smuzhiyun         return err;
1922*4882a593Smuzhiyun 
1923*4882a593Smuzhiyun     nClients = stuff->nClients;
1924*4882a593Smuzhiyun     pCanonClients = RecordCanonicalizeClientSpecifiers((XID *) &stuff[1],
1925*4882a593Smuzhiyun                                                        &nClients, 0);
1926*4882a593Smuzhiyun     if (!pCanonClients)
1927*4882a593Smuzhiyun         return BadAlloc;
1928*4882a593Smuzhiyun 
1929*4882a593Smuzhiyun     for (i = 0; i < nClients; i++) {
1930*4882a593Smuzhiyun         RecordDeleteClientFromContext(pContext, pCanonClients[i]);
1931*4882a593Smuzhiyun     }
1932*4882a593Smuzhiyun     if (pCanonClients != (XID *) &stuff[1])
1933*4882a593Smuzhiyun         free(pCanonClients);
1934*4882a593Smuzhiyun     return Success;
1935*4882a593Smuzhiyun }                               /* ProcRecordUnregisterClients */
1936*4882a593Smuzhiyun 
1937*4882a593Smuzhiyun /****************************************************************************/
1938*4882a593Smuzhiyun 
1939*4882a593Smuzhiyun /* stuff for GetContext */
1940*4882a593Smuzhiyun 
1941*4882a593Smuzhiyun /* This is a tactical structure used to hold the xRecordRanges as they are
1942*4882a593Smuzhiyun  * being reconstituted from the sets in the RCAPs.
1943*4882a593Smuzhiyun  */
1944*4882a593Smuzhiyun 
1945*4882a593Smuzhiyun typedef struct {
1946*4882a593Smuzhiyun     xRecordRange *pRanges;      /* array of xRecordRanges for one RCAP */
1947*4882a593Smuzhiyun     int size;                   /* number of elements in pRanges, >= nRanges */
1948*4882a593Smuzhiyun     int nRanges;                /* number of occupied element of pRanges */
1949*4882a593Smuzhiyun } GetContextRangeInfoRec, *GetContextRangeInfoPtr;
1950*4882a593Smuzhiyun 
1951*4882a593Smuzhiyun /* RecordAllocRanges
1952*4882a593Smuzhiyun  *
1953*4882a593Smuzhiyun  * Arguments:
1954*4882a593Smuzhiyun  *	pri is a pointer to a GetContextRangeInfoRec to allocate for.
1955*4882a593Smuzhiyun  *	nRanges is the number of xRecordRanges desired for pri.
1956*4882a593Smuzhiyun  *
1957*4882a593Smuzhiyun  * Returns: BadAlloc if a memory allocation error occurred, else Success.
1958*4882a593Smuzhiyun  *
1959*4882a593Smuzhiyun  * Side Effects:
1960*4882a593Smuzhiyun  *	If Success is returned, pri->pRanges points to at least nRanges
1961*4882a593Smuzhiyun  *	ranges.  pri->nRanges is set to nRanges.  pri->size is the actual
1962*4882a593Smuzhiyun  *	number of ranges.  Newly allocated ranges are zeroed.
1963*4882a593Smuzhiyun  */
1964*4882a593Smuzhiyun static int
RecordAllocRanges(GetContextRangeInfoPtr pri,int nRanges)1965*4882a593Smuzhiyun RecordAllocRanges(GetContextRangeInfoPtr pri, int nRanges)
1966*4882a593Smuzhiyun {
1967*4882a593Smuzhiyun     int newsize;
1968*4882a593Smuzhiyun     xRecordRange *pNewRange;
1969*4882a593Smuzhiyun 
1970*4882a593Smuzhiyun #define SZINCR 8
1971*4882a593Smuzhiyun 
1972*4882a593Smuzhiyun     newsize = max(pri->size + SZINCR, nRanges);
1973*4882a593Smuzhiyun     pNewRange = reallocarray(pri->pRanges, newsize, sizeof(xRecordRange));
1974*4882a593Smuzhiyun     if (!pNewRange)
1975*4882a593Smuzhiyun         return BadAlloc;
1976*4882a593Smuzhiyun 
1977*4882a593Smuzhiyun     pri->pRanges = pNewRange;
1978*4882a593Smuzhiyun     pri->size = newsize;
1979*4882a593Smuzhiyun     memset(&pri->pRanges[pri->size - SZINCR], 0, SZINCR * sizeof(xRecordRange));
1980*4882a593Smuzhiyun     if (pri->nRanges < nRanges)
1981*4882a593Smuzhiyun         pri->nRanges = nRanges;
1982*4882a593Smuzhiyun     return Success;
1983*4882a593Smuzhiyun }                               /* RecordAllocRanges */
1984*4882a593Smuzhiyun 
1985*4882a593Smuzhiyun /* RecordConvertSetToRanges
1986*4882a593Smuzhiyun  *
1987*4882a593Smuzhiyun  * Arguments:
1988*4882a593Smuzhiyun  *	pSet is the set to be converted.
1989*4882a593Smuzhiyun  *	pri is where the result should be stored.
1990*4882a593Smuzhiyun  *	byteoffset is the offset from the start of an xRecordRange of the
1991*4882a593Smuzhiyun  *	  two vales (first, last) we are interested in.
1992*4882a593Smuzhiyun  *	card8 is TRUE if the vales are one byte each and FALSE if two bytes
1993*4882a593Smuzhiyun  *	  each.
1994*4882a593Smuzhiyun  *	imax is the largest set value to store in pri->pRanges.
1995*4882a593Smuzhiyun  *	pStartIndex, if non-NULL, is the index of the first range in
1996*4882a593Smuzhiyun  *	  pri->pRanges that should be stored to.  If NULL,
1997*4882a593Smuzhiyun  *	  start at index 0.
1998*4882a593Smuzhiyun  *
1999*4882a593Smuzhiyun  * Returns: BadAlloc if a memory allocation error occurred, else Success.
2000*4882a593Smuzhiyun  *
2001*4882a593Smuzhiyun  * Side Effects:
2002*4882a593Smuzhiyun  *	If Success is returned, the slice of pri->pRanges indicated by
2003*4882a593Smuzhiyun  *	byteoffset and card8 is filled in with the intervals from pSet.
2004*4882a593Smuzhiyun  *	if pStartIndex was non-NULL, *pStartIndex is filled in with one
2005*4882a593Smuzhiyun  *	more than the index of the last xRecordRange that was touched.
2006*4882a593Smuzhiyun  */
2007*4882a593Smuzhiyun static int
RecordConvertSetToRanges(RecordSetPtr pSet,GetContextRangeInfoPtr pri,int byteoffset,Bool card8,unsigned int imax,int * pStartIndex)2008*4882a593Smuzhiyun RecordConvertSetToRanges(RecordSetPtr pSet,
2009*4882a593Smuzhiyun                          GetContextRangeInfoPtr pri,
2010*4882a593Smuzhiyun                          int byteoffset,
2011*4882a593Smuzhiyun                          Bool card8, unsigned int imax, int *pStartIndex)
2012*4882a593Smuzhiyun {
2013*4882a593Smuzhiyun     int nRanges;
2014*4882a593Smuzhiyun     RecordSetIteratePtr pIter = NULL;
2015*4882a593Smuzhiyun     RecordSetInterval interval;
2016*4882a593Smuzhiyun     CARD8 *pCARD8;
2017*4882a593Smuzhiyun     CARD16 *pCARD16;
2018*4882a593Smuzhiyun     int err;
2019*4882a593Smuzhiyun 
2020*4882a593Smuzhiyun     if (!pSet)
2021*4882a593Smuzhiyun         return Success;
2022*4882a593Smuzhiyun 
2023*4882a593Smuzhiyun     nRanges = pStartIndex ? *pStartIndex : 0;
2024*4882a593Smuzhiyun     while ((pIter = RecordIterateSet(pSet, pIter, &interval))) {
2025*4882a593Smuzhiyun         if (interval.first > imax)
2026*4882a593Smuzhiyun             break;
2027*4882a593Smuzhiyun         if (interval.last > imax)
2028*4882a593Smuzhiyun             interval.last = imax;
2029*4882a593Smuzhiyun         nRanges++;
2030*4882a593Smuzhiyun         if (nRanges > pri->size) {
2031*4882a593Smuzhiyun             err = RecordAllocRanges(pri, nRanges);
2032*4882a593Smuzhiyun             if (err != Success)
2033*4882a593Smuzhiyun                 return err;
2034*4882a593Smuzhiyun         }
2035*4882a593Smuzhiyun         else
2036*4882a593Smuzhiyun             pri->nRanges = max(pri->nRanges, nRanges);
2037*4882a593Smuzhiyun         if (card8) {
2038*4882a593Smuzhiyun             pCARD8 = ((CARD8 *) &pri->pRanges[nRanges - 1]) + byteoffset;
2039*4882a593Smuzhiyun             *pCARD8++ = interval.first;
2040*4882a593Smuzhiyun             *pCARD8 = interval.last;
2041*4882a593Smuzhiyun         }
2042*4882a593Smuzhiyun         else {
2043*4882a593Smuzhiyun             pCARD16 = (CARD16 *)
2044*4882a593Smuzhiyun                 (((char *) &pri->pRanges[nRanges - 1]) + byteoffset);
2045*4882a593Smuzhiyun             *pCARD16++ = interval.first;
2046*4882a593Smuzhiyun             *pCARD16 = interval.last;
2047*4882a593Smuzhiyun         }
2048*4882a593Smuzhiyun     }
2049*4882a593Smuzhiyun     if (pStartIndex)
2050*4882a593Smuzhiyun         *pStartIndex = nRanges;
2051*4882a593Smuzhiyun     return Success;
2052*4882a593Smuzhiyun }                               /* RecordConvertSetToRanges */
2053*4882a593Smuzhiyun 
2054*4882a593Smuzhiyun /* RecordConvertMinorOpInfoToRanges
2055*4882a593Smuzhiyun  *
2056*4882a593Smuzhiyun  * Arguments:
2057*4882a593Smuzhiyun  *	pMinOpInfo is the minor opcode info to convert to xRecordRanges.
2058*4882a593Smuzhiyun  *	pri is where the result should be stored.
2059*4882a593Smuzhiyun  *	byteoffset is the offset from the start of an xRecordRange of the
2060*4882a593Smuzhiyun  *	  four vales (CARD8 major_first, CARD8 major_last,
2061*4882a593Smuzhiyun  *	  CARD16 minor_first, CARD16 minor_last) we are going to store.
2062*4882a593Smuzhiyun  *
2063*4882a593Smuzhiyun  * Returns: BadAlloc if a memory allocation error occurred, else Success.
2064*4882a593Smuzhiyun  *
2065*4882a593Smuzhiyun  * Side Effects:
2066*4882a593Smuzhiyun  *	If Success is returned, the slice of pri->pRanges indicated by
2067*4882a593Smuzhiyun  *	byteoffset is filled in with the information from pMinOpInfo.
2068*4882a593Smuzhiyun  */
2069*4882a593Smuzhiyun static int
RecordConvertMinorOpInfoToRanges(RecordMinorOpPtr pMinOpInfo,GetContextRangeInfoPtr pri,int byteoffset)2070*4882a593Smuzhiyun RecordConvertMinorOpInfoToRanges(RecordMinorOpPtr pMinOpInfo,
2071*4882a593Smuzhiyun                                  GetContextRangeInfoPtr pri, int byteoffset)
2072*4882a593Smuzhiyun {
2073*4882a593Smuzhiyun     int nsets;
2074*4882a593Smuzhiyun     int start;
2075*4882a593Smuzhiyun     int i;
2076*4882a593Smuzhiyun     int err;
2077*4882a593Smuzhiyun 
2078*4882a593Smuzhiyun     if (!pMinOpInfo)
2079*4882a593Smuzhiyun         return Success;
2080*4882a593Smuzhiyun 
2081*4882a593Smuzhiyun     nsets = pMinOpInfo->count;
2082*4882a593Smuzhiyun     pMinOpInfo++;
2083*4882a593Smuzhiyun     start = 0;
2084*4882a593Smuzhiyun     for (i = 0; i < nsets; i++) {
2085*4882a593Smuzhiyun         int j, s;
2086*4882a593Smuzhiyun 
2087*4882a593Smuzhiyun         s = start;
2088*4882a593Smuzhiyun         err = RecordConvertSetToRanges(pMinOpInfo[i].major.pMinOpSet, pri,
2089*4882a593Smuzhiyun                                        byteoffset + 2, FALSE, 65535, &start);
2090*4882a593Smuzhiyun         if (err != Success)
2091*4882a593Smuzhiyun             return err;
2092*4882a593Smuzhiyun         for (j = s; j < start; j++) {
2093*4882a593Smuzhiyun             CARD8 *pCARD8 = ((CARD8 *) &pri->pRanges[j]) + byteoffset;
2094*4882a593Smuzhiyun 
2095*4882a593Smuzhiyun             *pCARD8++ = pMinOpInfo[i].major.first;
2096*4882a593Smuzhiyun             *pCARD8 = pMinOpInfo[i].major.last;
2097*4882a593Smuzhiyun         }
2098*4882a593Smuzhiyun     }
2099*4882a593Smuzhiyun     return Success;
2100*4882a593Smuzhiyun }                               /* RecordConvertMinorOpInfoToRanges */
2101*4882a593Smuzhiyun 
2102*4882a593Smuzhiyun /* RecordSwapRanges
2103*4882a593Smuzhiyun  *
2104*4882a593Smuzhiyun  * Arguments:
2105*4882a593Smuzhiyun  *	pRanges is an array of xRecordRanges.
2106*4882a593Smuzhiyun  *	nRanges is the number of elements in pRanges.
2107*4882a593Smuzhiyun  *
2108*4882a593Smuzhiyun  * Returns: nothing.
2109*4882a593Smuzhiyun  *
2110*4882a593Smuzhiyun  * Side Effects:
2111*4882a593Smuzhiyun  *	The 16 bit fields of each xRecordRange are byte swapped.
2112*4882a593Smuzhiyun  */
2113*4882a593Smuzhiyun static void
RecordSwapRanges(xRecordRange * pRanges,int nRanges)2114*4882a593Smuzhiyun RecordSwapRanges(xRecordRange * pRanges, int nRanges)
2115*4882a593Smuzhiyun {
2116*4882a593Smuzhiyun     int i;
2117*4882a593Smuzhiyun 
2118*4882a593Smuzhiyun     for (i = 0; i < nRanges; i++, pRanges++) {
2119*4882a593Smuzhiyun         swaps(&pRanges->extRequestsMinorFirst);
2120*4882a593Smuzhiyun         swaps(&pRanges->extRequestsMinorLast);
2121*4882a593Smuzhiyun         swaps(&pRanges->extRepliesMinorFirst);
2122*4882a593Smuzhiyun         swaps(&pRanges->extRepliesMinorLast);
2123*4882a593Smuzhiyun     }
2124*4882a593Smuzhiyun }                               /* RecordSwapRanges */
2125*4882a593Smuzhiyun 
2126*4882a593Smuzhiyun static int
ProcRecordGetContext(ClientPtr client)2127*4882a593Smuzhiyun ProcRecordGetContext(ClientPtr client)
2128*4882a593Smuzhiyun {
2129*4882a593Smuzhiyun     RecordContextPtr pContext;
2130*4882a593Smuzhiyun 
2131*4882a593Smuzhiyun     REQUEST(xRecordGetContextReq);
2132*4882a593Smuzhiyun     xRecordGetContextReply rep;
2133*4882a593Smuzhiyun     RecordClientsAndProtocolPtr pRCAP;
2134*4882a593Smuzhiyun     int nRCAPs = 0;
2135*4882a593Smuzhiyun     GetContextRangeInfoPtr pRangeInfo;
2136*4882a593Smuzhiyun     GetContextRangeInfoPtr pri;
2137*4882a593Smuzhiyun     int i;
2138*4882a593Smuzhiyun     int err;
2139*4882a593Smuzhiyun     CARD32 nClients, length;
2140*4882a593Smuzhiyun 
2141*4882a593Smuzhiyun     REQUEST_SIZE_MATCH(xRecordGetContextReq);
2142*4882a593Smuzhiyun     VERIFY_CONTEXT(pContext, stuff->context, client);
2143*4882a593Smuzhiyun 
2144*4882a593Smuzhiyun     /* how many RCAPs are there on this context? */
2145*4882a593Smuzhiyun 
2146*4882a593Smuzhiyun     for (pRCAP = pContext->pListOfRCAP; pRCAP; pRCAP = pRCAP->pNextRCAP)
2147*4882a593Smuzhiyun         nRCAPs++;
2148*4882a593Smuzhiyun 
2149*4882a593Smuzhiyun     /* allocate and initialize space for record range info */
2150*4882a593Smuzhiyun 
2151*4882a593Smuzhiyun     pRangeInfo = xallocarray(nRCAPs, sizeof(GetContextRangeInfoRec));
2152*4882a593Smuzhiyun     if (!pRangeInfo && nRCAPs > 0)
2153*4882a593Smuzhiyun         return BadAlloc;
2154*4882a593Smuzhiyun     for (i = 0; i < nRCAPs; i++) {
2155*4882a593Smuzhiyun         pRangeInfo[i].pRanges = NULL;
2156*4882a593Smuzhiyun         pRangeInfo[i].size = 0;
2157*4882a593Smuzhiyun         pRangeInfo[i].nRanges = 0;
2158*4882a593Smuzhiyun     }
2159*4882a593Smuzhiyun 
2160*4882a593Smuzhiyun     /* convert the RCAP (internal) representation of the recorded protocol
2161*4882a593Smuzhiyun      * to the wire protocol (external) representation, storing the information
2162*4882a593Smuzhiyun      * for the ith RCAP in pri[i]
2163*4882a593Smuzhiyun      */
2164*4882a593Smuzhiyun 
2165*4882a593Smuzhiyun     for (pRCAP = pContext->pListOfRCAP, pri = pRangeInfo;
2166*4882a593Smuzhiyun          pRCAP; pRCAP = pRCAP->pNextRCAP, pri++) {
2167*4882a593Smuzhiyun         xRecordRange rr;
2168*4882a593Smuzhiyun 
2169*4882a593Smuzhiyun         err = RecordConvertSetToRanges(pRCAP->pRequestMajorOpSet, pri,
2170*4882a593Smuzhiyun                                        offset_of(rr, coreRequestsFirst), TRUE,
2171*4882a593Smuzhiyun                                        127, NULL);
2172*4882a593Smuzhiyun         if (err != Success)
2173*4882a593Smuzhiyun             goto bailout;
2174*4882a593Smuzhiyun 
2175*4882a593Smuzhiyun         err = RecordConvertSetToRanges(pRCAP->pReplyMajorOpSet, pri,
2176*4882a593Smuzhiyun                                        offset_of(rr, coreRepliesFirst), TRUE,
2177*4882a593Smuzhiyun                                        127, NULL);
2178*4882a593Smuzhiyun         if (err != Success)
2179*4882a593Smuzhiyun             goto bailout;
2180*4882a593Smuzhiyun 
2181*4882a593Smuzhiyun         err = RecordConvertSetToRanges(pRCAP->pDeliveredEventSet, pri,
2182*4882a593Smuzhiyun                                        offset_of(rr, deliveredEventsFirst),
2183*4882a593Smuzhiyun                                        TRUE, 255, NULL);
2184*4882a593Smuzhiyun         if (err != Success)
2185*4882a593Smuzhiyun             goto bailout;
2186*4882a593Smuzhiyun 
2187*4882a593Smuzhiyun         err = RecordConvertSetToRanges(pRCAP->pDeviceEventSet, pri,
2188*4882a593Smuzhiyun                                        offset_of(rr, deviceEventsFirst), TRUE,
2189*4882a593Smuzhiyun                                        255, NULL);
2190*4882a593Smuzhiyun         if (err != Success)
2191*4882a593Smuzhiyun             goto bailout;
2192*4882a593Smuzhiyun 
2193*4882a593Smuzhiyun         err = RecordConvertSetToRanges(pRCAP->pErrorSet, pri,
2194*4882a593Smuzhiyun                                        offset_of(rr, errorsFirst), TRUE, 255,
2195*4882a593Smuzhiyun                                        NULL);
2196*4882a593Smuzhiyun         if (err != Success)
2197*4882a593Smuzhiyun             goto bailout;
2198*4882a593Smuzhiyun 
2199*4882a593Smuzhiyun         err = RecordConvertMinorOpInfoToRanges(pRCAP->pRequestMinOpInfo,
2200*4882a593Smuzhiyun                                                pri, offset_of(rr,
2201*4882a593Smuzhiyun                                                               extRequestsMajorFirst));
2202*4882a593Smuzhiyun         if (err != Success)
2203*4882a593Smuzhiyun             goto bailout;
2204*4882a593Smuzhiyun 
2205*4882a593Smuzhiyun         err = RecordConvertMinorOpInfoToRanges(pRCAP->pReplyMinOpInfo,
2206*4882a593Smuzhiyun                                                pri, offset_of(rr,
2207*4882a593Smuzhiyun                                                               extRepliesMajorFirst));
2208*4882a593Smuzhiyun         if (err != Success)
2209*4882a593Smuzhiyun             goto bailout;
2210*4882a593Smuzhiyun 
2211*4882a593Smuzhiyun         if (pRCAP->clientStarted || pRCAP->clientDied) {
2212*4882a593Smuzhiyun             if (pri->nRanges == 0)
2213*4882a593Smuzhiyun                 RecordAllocRanges(pri, 1);
2214*4882a593Smuzhiyun             pri->pRanges[0].clientStarted = pRCAP->clientStarted;
2215*4882a593Smuzhiyun             pri->pRanges[0].clientDied = pRCAP->clientDied;
2216*4882a593Smuzhiyun         }
2217*4882a593Smuzhiyun     }
2218*4882a593Smuzhiyun 
2219*4882a593Smuzhiyun     /* calculate number of clients and reply length */
2220*4882a593Smuzhiyun 
2221*4882a593Smuzhiyun     nClients = 0;
2222*4882a593Smuzhiyun     length = 0;
2223*4882a593Smuzhiyun     for (pRCAP = pContext->pListOfRCAP, pri = pRangeInfo;
2224*4882a593Smuzhiyun          pRCAP; pRCAP = pRCAP->pNextRCAP, pri++) {
2225*4882a593Smuzhiyun         nClients += pRCAP->numClients;
2226*4882a593Smuzhiyun         length += pRCAP->numClients *
2227*4882a593Smuzhiyun             (bytes_to_int32(sizeof(xRecordClientInfo)) +
2228*4882a593Smuzhiyun              pri->nRanges * bytes_to_int32(sizeof(xRecordRange)));
2229*4882a593Smuzhiyun     }
2230*4882a593Smuzhiyun 
2231*4882a593Smuzhiyun     /* write the reply header */
2232*4882a593Smuzhiyun 
2233*4882a593Smuzhiyun     rep = (xRecordGetContextReply) {
2234*4882a593Smuzhiyun         .type = X_Reply,
2235*4882a593Smuzhiyun         .enabled = pContext->pRecordingClient != NULL,
2236*4882a593Smuzhiyun         .sequenceNumber = client->sequence,
2237*4882a593Smuzhiyun         .length = length,
2238*4882a593Smuzhiyun         .elementHeader = pContext->elemHeaders,
2239*4882a593Smuzhiyun         .nClients = nClients
2240*4882a593Smuzhiyun     };
2241*4882a593Smuzhiyun     if (client->swapped) {
2242*4882a593Smuzhiyun         swaps(&rep.sequenceNumber);
2243*4882a593Smuzhiyun         swapl(&rep.length);
2244*4882a593Smuzhiyun         swapl(&rep.nClients);
2245*4882a593Smuzhiyun     }
2246*4882a593Smuzhiyun     WriteToClient(client, sizeof(xRecordGetContextReply), &rep);
2247*4882a593Smuzhiyun 
2248*4882a593Smuzhiyun     /* write all the CLIENT_INFOs */
2249*4882a593Smuzhiyun 
2250*4882a593Smuzhiyun     for (pRCAP = pContext->pListOfRCAP, pri = pRangeInfo;
2251*4882a593Smuzhiyun          pRCAP; pRCAP = pRCAP->pNextRCAP, pri++) {
2252*4882a593Smuzhiyun         xRecordClientInfo rci;
2253*4882a593Smuzhiyun 
2254*4882a593Smuzhiyun         rci.nRanges = pri->nRanges;
2255*4882a593Smuzhiyun         if (client->swapped) {
2256*4882a593Smuzhiyun             swapl(&rci.nRanges);
2257*4882a593Smuzhiyun             RecordSwapRanges(pri->pRanges, pri->nRanges);
2258*4882a593Smuzhiyun         }
2259*4882a593Smuzhiyun         for (i = 0; i < pRCAP->numClients; i++) {
2260*4882a593Smuzhiyun             rci.clientResource = pRCAP->pClientIDs[i];
2261*4882a593Smuzhiyun             if (client->swapped)
2262*4882a593Smuzhiyun                 swapl(&rci.clientResource);
2263*4882a593Smuzhiyun             WriteToClient(client, sizeof(xRecordClientInfo), &rci);
2264*4882a593Smuzhiyun             WriteToClient(client, sizeof(xRecordRange) * pri->nRanges,
2265*4882a593Smuzhiyun                           pri->pRanges);
2266*4882a593Smuzhiyun         }
2267*4882a593Smuzhiyun     }
2268*4882a593Smuzhiyun     err = Success;
2269*4882a593Smuzhiyun 
2270*4882a593Smuzhiyun  bailout:
2271*4882a593Smuzhiyun     for (i = 0; i < nRCAPs; i++) {
2272*4882a593Smuzhiyun         free(pRangeInfo[i].pRanges);
2273*4882a593Smuzhiyun     }
2274*4882a593Smuzhiyun     free(pRangeInfo);
2275*4882a593Smuzhiyun     return err;
2276*4882a593Smuzhiyun }                               /* ProcRecordGetContext */
2277*4882a593Smuzhiyun 
2278*4882a593Smuzhiyun static int
ProcRecordEnableContext(ClientPtr client)2279*4882a593Smuzhiyun ProcRecordEnableContext(ClientPtr client)
2280*4882a593Smuzhiyun {
2281*4882a593Smuzhiyun     RecordContextPtr pContext;
2282*4882a593Smuzhiyun 
2283*4882a593Smuzhiyun     REQUEST(xRecordEnableContextReq);
2284*4882a593Smuzhiyun     int i;
2285*4882a593Smuzhiyun     RecordClientsAndProtocolPtr pRCAP;
2286*4882a593Smuzhiyun 
2287*4882a593Smuzhiyun     REQUEST_SIZE_MATCH(xRecordGetContextReq);
2288*4882a593Smuzhiyun     VERIFY_CONTEXT(pContext, stuff->context, client);
2289*4882a593Smuzhiyun     if (pContext->pRecordingClient)
2290*4882a593Smuzhiyun         return BadMatch;        /* already enabled */
2291*4882a593Smuzhiyun 
2292*4882a593Smuzhiyun     /* install record hooks for each RCAP */
2293*4882a593Smuzhiyun 
2294*4882a593Smuzhiyun     for (pRCAP = pContext->pListOfRCAP; pRCAP; pRCAP = pRCAP->pNextRCAP) {
2295*4882a593Smuzhiyun         int err = RecordInstallHooks(pRCAP, 0);
2296*4882a593Smuzhiyun 
2297*4882a593Smuzhiyun         if (err != Success) {   /* undo the previous installs */
2298*4882a593Smuzhiyun             RecordClientsAndProtocolPtr pUninstallRCAP;
2299*4882a593Smuzhiyun 
2300*4882a593Smuzhiyun             for (pUninstallRCAP = pContext->pListOfRCAP;
2301*4882a593Smuzhiyun                  pUninstallRCAP != pRCAP;
2302*4882a593Smuzhiyun                  pUninstallRCAP = pUninstallRCAP->pNextRCAP) {
2303*4882a593Smuzhiyun                 RecordUninstallHooks(pUninstallRCAP, 0);
2304*4882a593Smuzhiyun             }
2305*4882a593Smuzhiyun             return err;
2306*4882a593Smuzhiyun         }
2307*4882a593Smuzhiyun     }
2308*4882a593Smuzhiyun 
2309*4882a593Smuzhiyun     /* Disallow further request processing on this connection until
2310*4882a593Smuzhiyun      * the context is disabled.
2311*4882a593Smuzhiyun      */
2312*4882a593Smuzhiyun     IgnoreClient(client);
2313*4882a593Smuzhiyun     pContext->pRecordingClient = client;
2314*4882a593Smuzhiyun 
2315*4882a593Smuzhiyun     /* Don't allow the data connection to record itself; unregister it. */
2316*4882a593Smuzhiyun     RecordDeleteClientFromContext(pContext,
2317*4882a593Smuzhiyun                                   pContext->pRecordingClient->clientAsMask);
2318*4882a593Smuzhiyun 
2319*4882a593Smuzhiyun     /* move the newly enabled context to the front part of ppAllContexts,
2320*4882a593Smuzhiyun      * where all the enabled contexts are
2321*4882a593Smuzhiyun      */
2322*4882a593Smuzhiyun     i = RecordFindContextOnAllContexts(pContext);
2323*4882a593Smuzhiyun     assert(i >= numEnabledContexts);
2324*4882a593Smuzhiyun     if (i != numEnabledContexts) {
2325*4882a593Smuzhiyun         ppAllContexts[i] = ppAllContexts[numEnabledContexts];
2326*4882a593Smuzhiyun         ppAllContexts[numEnabledContexts] = pContext;
2327*4882a593Smuzhiyun     }
2328*4882a593Smuzhiyun 
2329*4882a593Smuzhiyun     ++numEnabledContexts;
2330*4882a593Smuzhiyun     assert(numEnabledContexts > 0);
2331*4882a593Smuzhiyun 
2332*4882a593Smuzhiyun     /* send StartOfData */
2333*4882a593Smuzhiyun     RecordAProtocolElement(pContext, NULL, XRecordStartOfData, NULL, 0, 0, 0);
2334*4882a593Smuzhiyun     RecordFlushReplyBuffer(pContext, NULL, 0, NULL, 0);
2335*4882a593Smuzhiyun     return Success;
2336*4882a593Smuzhiyun }                               /* ProcRecordEnableContext */
2337*4882a593Smuzhiyun 
2338*4882a593Smuzhiyun /* RecordDisableContext
2339*4882a593Smuzhiyun  *
2340*4882a593Smuzhiyun  * Arguments:
2341*4882a593Smuzhiyun  *	pContext is the context to disable.
2342*4882a593Smuzhiyun  *	nRanges is the number of elements in pRanges.
2343*4882a593Smuzhiyun  *
2344*4882a593Smuzhiyun  * Returns: nothing.
2345*4882a593Smuzhiyun  *
2346*4882a593Smuzhiyun  * Side Effects:
2347*4882a593Smuzhiyun  *	If the context was enabled, it is disabled.  An EndOfData
2348*4882a593Smuzhiyun  *	message is sent to the recording client.  Recording hooks for
2349*4882a593Smuzhiyun  *	this context are uninstalled.  The context is moved to the
2350*4882a593Smuzhiyun  *	rear part of the ppAllContexts array.  numEnabledContexts is
2351*4882a593Smuzhiyun  *	decremented.  Request processing for the formerly recording client
2352*4882a593Smuzhiyun  *	is resumed.
2353*4882a593Smuzhiyun  */
2354*4882a593Smuzhiyun static void
RecordDisableContext(RecordContextPtr pContext)2355*4882a593Smuzhiyun RecordDisableContext(RecordContextPtr pContext)
2356*4882a593Smuzhiyun {
2357*4882a593Smuzhiyun     RecordClientsAndProtocolPtr pRCAP;
2358*4882a593Smuzhiyun     int i;
2359*4882a593Smuzhiyun 
2360*4882a593Smuzhiyun     if (!pContext->pRecordingClient)
2361*4882a593Smuzhiyun         return;
2362*4882a593Smuzhiyun     if (!pContext->pRecordingClient->clientGone) {
2363*4882a593Smuzhiyun         RecordAProtocolElement(pContext, NULL, XRecordEndOfData, NULL, 0, 0, 0);
2364*4882a593Smuzhiyun         RecordFlushReplyBuffer(pContext, NULL, 0, NULL, 0);
2365*4882a593Smuzhiyun     }
2366*4882a593Smuzhiyun     /* Re-enable request processing on this connection. */
2367*4882a593Smuzhiyun     AttendClient(pContext->pRecordingClient);
2368*4882a593Smuzhiyun 
2369*4882a593Smuzhiyun     for (pRCAP = pContext->pListOfRCAP; pRCAP; pRCAP = pRCAP->pNextRCAP) {
2370*4882a593Smuzhiyun         RecordUninstallHooks(pRCAP, 0);
2371*4882a593Smuzhiyun     }
2372*4882a593Smuzhiyun 
2373*4882a593Smuzhiyun     pContext->pRecordingClient = NULL;
2374*4882a593Smuzhiyun 
2375*4882a593Smuzhiyun     /* move the newly disabled context to the rear part of ppAllContexts,
2376*4882a593Smuzhiyun      * where all the disabled contexts are
2377*4882a593Smuzhiyun      */
2378*4882a593Smuzhiyun     i = RecordFindContextOnAllContexts(pContext);
2379*4882a593Smuzhiyun     assert((i != -1) && (i < numEnabledContexts));
2380*4882a593Smuzhiyun     if (i != (numEnabledContexts - 1)) {
2381*4882a593Smuzhiyun         ppAllContexts[i] = ppAllContexts[numEnabledContexts - 1];
2382*4882a593Smuzhiyun         ppAllContexts[numEnabledContexts - 1] = pContext;
2383*4882a593Smuzhiyun     }
2384*4882a593Smuzhiyun     --numEnabledContexts;
2385*4882a593Smuzhiyun     assert(numEnabledContexts >= 0);
2386*4882a593Smuzhiyun }                               /* RecordDisableContext */
2387*4882a593Smuzhiyun 
2388*4882a593Smuzhiyun static int
ProcRecordDisableContext(ClientPtr client)2389*4882a593Smuzhiyun ProcRecordDisableContext(ClientPtr client)
2390*4882a593Smuzhiyun {
2391*4882a593Smuzhiyun     RecordContextPtr pContext;
2392*4882a593Smuzhiyun 
2393*4882a593Smuzhiyun     REQUEST(xRecordDisableContextReq);
2394*4882a593Smuzhiyun 
2395*4882a593Smuzhiyun     REQUEST_SIZE_MATCH(xRecordDisableContextReq);
2396*4882a593Smuzhiyun     VERIFY_CONTEXT(pContext, stuff->context, client);
2397*4882a593Smuzhiyun     RecordDisableContext(pContext);
2398*4882a593Smuzhiyun     return Success;
2399*4882a593Smuzhiyun }                               /* ProcRecordDisableContext */
2400*4882a593Smuzhiyun 
2401*4882a593Smuzhiyun /* RecordDeleteContext
2402*4882a593Smuzhiyun  *
2403*4882a593Smuzhiyun  * Arguments:
2404*4882a593Smuzhiyun  *	value is the context to delete.
2405*4882a593Smuzhiyun  *	id is its resource ID.
2406*4882a593Smuzhiyun  *
2407*4882a593Smuzhiyun  * Returns: Success.
2408*4882a593Smuzhiyun  *
2409*4882a593Smuzhiyun  * Side Effects:
2410*4882a593Smuzhiyun  *	Disables the context, frees all associated memory, and removes
2411*4882a593Smuzhiyun  *	it from the ppAllContexts array.
2412*4882a593Smuzhiyun  */
2413*4882a593Smuzhiyun static int
RecordDeleteContext(void * value,XID id)2414*4882a593Smuzhiyun RecordDeleteContext(void *value, XID id)
2415*4882a593Smuzhiyun {
2416*4882a593Smuzhiyun     int i;
2417*4882a593Smuzhiyun     RecordContextPtr pContext = (RecordContextPtr) value;
2418*4882a593Smuzhiyun     RecordClientsAndProtocolPtr pRCAP;
2419*4882a593Smuzhiyun 
2420*4882a593Smuzhiyun     RecordDisableContext(pContext);
2421*4882a593Smuzhiyun 
2422*4882a593Smuzhiyun     /*  Remove all the clients from all the RCAPs.
2423*4882a593Smuzhiyun      *  As a result, the RCAPs will be freed.
2424*4882a593Smuzhiyun      */
2425*4882a593Smuzhiyun 
2426*4882a593Smuzhiyun     while ((pRCAP = pContext->pListOfRCAP)) {
2427*4882a593Smuzhiyun         int numClients = pRCAP->numClients;
2428*4882a593Smuzhiyun 
2429*4882a593Smuzhiyun         /* when the last client is deleted, the RCAP will go away. */
2430*4882a593Smuzhiyun         while (numClients--) {
2431*4882a593Smuzhiyun             RecordDeleteClientFromRCAP(pRCAP, numClients);
2432*4882a593Smuzhiyun         }
2433*4882a593Smuzhiyun     }
2434*4882a593Smuzhiyun 
2435*4882a593Smuzhiyun     /* remove context from AllContexts list */
2436*4882a593Smuzhiyun 
2437*4882a593Smuzhiyun     if (-1 != (i = RecordFindContextOnAllContexts(pContext))) {
2438*4882a593Smuzhiyun         ppAllContexts[i] = ppAllContexts[numContexts - 1];
2439*4882a593Smuzhiyun         if (--numContexts == 0) {
2440*4882a593Smuzhiyun             free(ppAllContexts);
2441*4882a593Smuzhiyun             ppAllContexts = NULL;
2442*4882a593Smuzhiyun         }
2443*4882a593Smuzhiyun     }
2444*4882a593Smuzhiyun     free(pContext);
2445*4882a593Smuzhiyun 
2446*4882a593Smuzhiyun     return Success;
2447*4882a593Smuzhiyun }                               /* RecordDeleteContext */
2448*4882a593Smuzhiyun 
2449*4882a593Smuzhiyun static int
ProcRecordFreeContext(ClientPtr client)2450*4882a593Smuzhiyun ProcRecordFreeContext(ClientPtr client)
2451*4882a593Smuzhiyun {
2452*4882a593Smuzhiyun     RecordContextPtr pContext;
2453*4882a593Smuzhiyun 
2454*4882a593Smuzhiyun     REQUEST(xRecordFreeContextReq);
2455*4882a593Smuzhiyun 
2456*4882a593Smuzhiyun     REQUEST_SIZE_MATCH(xRecordFreeContextReq);
2457*4882a593Smuzhiyun     VERIFY_CONTEXT(pContext, stuff->context, client);
2458*4882a593Smuzhiyun     FreeResource(stuff->context, RT_NONE);
2459*4882a593Smuzhiyun     return Success;
2460*4882a593Smuzhiyun }                               /* ProcRecordFreeContext */
2461*4882a593Smuzhiyun 
2462*4882a593Smuzhiyun static int
ProcRecordDispatch(ClientPtr client)2463*4882a593Smuzhiyun ProcRecordDispatch(ClientPtr client)
2464*4882a593Smuzhiyun {
2465*4882a593Smuzhiyun     REQUEST(xReq);
2466*4882a593Smuzhiyun 
2467*4882a593Smuzhiyun     switch (stuff->data) {
2468*4882a593Smuzhiyun     case X_RecordQueryVersion:
2469*4882a593Smuzhiyun         return ProcRecordQueryVersion(client);
2470*4882a593Smuzhiyun     case X_RecordCreateContext:
2471*4882a593Smuzhiyun         return ProcRecordCreateContext(client);
2472*4882a593Smuzhiyun     case X_RecordRegisterClients:
2473*4882a593Smuzhiyun         return ProcRecordRegisterClients(client);
2474*4882a593Smuzhiyun     case X_RecordUnregisterClients:
2475*4882a593Smuzhiyun         return ProcRecordUnregisterClients(client);
2476*4882a593Smuzhiyun     case X_RecordGetContext:
2477*4882a593Smuzhiyun         return ProcRecordGetContext(client);
2478*4882a593Smuzhiyun     case X_RecordEnableContext:
2479*4882a593Smuzhiyun         return ProcRecordEnableContext(client);
2480*4882a593Smuzhiyun     case X_RecordDisableContext:
2481*4882a593Smuzhiyun         return ProcRecordDisableContext(client);
2482*4882a593Smuzhiyun     case X_RecordFreeContext:
2483*4882a593Smuzhiyun         return ProcRecordFreeContext(client);
2484*4882a593Smuzhiyun     default:
2485*4882a593Smuzhiyun         return BadRequest;
2486*4882a593Smuzhiyun     }
2487*4882a593Smuzhiyun }                               /* ProcRecordDispatch */
2488*4882a593Smuzhiyun 
2489*4882a593Smuzhiyun static int _X_COLD
SProcRecordQueryVersion(ClientPtr client)2490*4882a593Smuzhiyun SProcRecordQueryVersion(ClientPtr client)
2491*4882a593Smuzhiyun {
2492*4882a593Smuzhiyun     REQUEST(xRecordQueryVersionReq);
2493*4882a593Smuzhiyun 
2494*4882a593Smuzhiyun     swaps(&stuff->length);
2495*4882a593Smuzhiyun     REQUEST_SIZE_MATCH(xRecordQueryVersionReq);
2496*4882a593Smuzhiyun     swaps(&stuff->majorVersion);
2497*4882a593Smuzhiyun     swaps(&stuff->minorVersion);
2498*4882a593Smuzhiyun     return ProcRecordQueryVersion(client);
2499*4882a593Smuzhiyun }                               /* SProcRecordQueryVersion */
2500*4882a593Smuzhiyun 
2501*4882a593Smuzhiyun static int _X_COLD
SwapCreateRegister(ClientPtr client,xRecordRegisterClientsReq * stuff)2502*4882a593Smuzhiyun SwapCreateRegister(ClientPtr client, xRecordRegisterClientsReq * stuff)
2503*4882a593Smuzhiyun {
2504*4882a593Smuzhiyun     int i;
2505*4882a593Smuzhiyun     XID *pClientID;
2506*4882a593Smuzhiyun 
2507*4882a593Smuzhiyun     swapl(&stuff->context);
2508*4882a593Smuzhiyun     swapl(&stuff->nClients);
2509*4882a593Smuzhiyun     swapl(&stuff->nRanges);
2510*4882a593Smuzhiyun     pClientID = (XID *) &stuff[1];
2511*4882a593Smuzhiyun     if (stuff->nClients >
2512*4882a593Smuzhiyun         client->req_len - bytes_to_int32(sz_xRecordRegisterClientsReq))
2513*4882a593Smuzhiyun         return BadLength;
2514*4882a593Smuzhiyun     for (i = 0; i < stuff->nClients; i++, pClientID++) {
2515*4882a593Smuzhiyun         swapl(pClientID);
2516*4882a593Smuzhiyun     }
2517*4882a593Smuzhiyun     if (stuff->nRanges >
2518*4882a593Smuzhiyun         client->req_len - bytes_to_int32(sz_xRecordRegisterClientsReq)
2519*4882a593Smuzhiyun         - stuff->nClients)
2520*4882a593Smuzhiyun         return BadLength;
2521*4882a593Smuzhiyun     RecordSwapRanges((xRecordRange *) pClientID, stuff->nRanges);
2522*4882a593Smuzhiyun     return Success;
2523*4882a593Smuzhiyun }                               /* SwapCreateRegister */
2524*4882a593Smuzhiyun 
2525*4882a593Smuzhiyun static int _X_COLD
SProcRecordCreateContext(ClientPtr client)2526*4882a593Smuzhiyun SProcRecordCreateContext(ClientPtr client)
2527*4882a593Smuzhiyun {
2528*4882a593Smuzhiyun     REQUEST(xRecordCreateContextReq);
2529*4882a593Smuzhiyun     int status;
2530*4882a593Smuzhiyun 
2531*4882a593Smuzhiyun     swaps(&stuff->length);
2532*4882a593Smuzhiyun     REQUEST_AT_LEAST_SIZE(xRecordCreateContextReq);
2533*4882a593Smuzhiyun     if ((status = SwapCreateRegister(client, (void *) stuff)) != Success)
2534*4882a593Smuzhiyun         return status;
2535*4882a593Smuzhiyun     return ProcRecordCreateContext(client);
2536*4882a593Smuzhiyun }                               /* SProcRecordCreateContext */
2537*4882a593Smuzhiyun 
2538*4882a593Smuzhiyun static int _X_COLD
SProcRecordRegisterClients(ClientPtr client)2539*4882a593Smuzhiyun SProcRecordRegisterClients(ClientPtr client)
2540*4882a593Smuzhiyun {
2541*4882a593Smuzhiyun     REQUEST(xRecordRegisterClientsReq);
2542*4882a593Smuzhiyun     int status;
2543*4882a593Smuzhiyun 
2544*4882a593Smuzhiyun     swaps(&stuff->length);
2545*4882a593Smuzhiyun     REQUEST_AT_LEAST_SIZE(xRecordRegisterClientsReq);
2546*4882a593Smuzhiyun     if ((status = SwapCreateRegister(client, (void *) stuff)) != Success)
2547*4882a593Smuzhiyun         return status;
2548*4882a593Smuzhiyun     return ProcRecordRegisterClients(client);
2549*4882a593Smuzhiyun }                               /* SProcRecordRegisterClients */
2550*4882a593Smuzhiyun 
2551*4882a593Smuzhiyun static int _X_COLD
SProcRecordUnregisterClients(ClientPtr client)2552*4882a593Smuzhiyun SProcRecordUnregisterClients(ClientPtr client)
2553*4882a593Smuzhiyun {
2554*4882a593Smuzhiyun     REQUEST(xRecordUnregisterClientsReq);
2555*4882a593Smuzhiyun 
2556*4882a593Smuzhiyun     swaps(&stuff->length);
2557*4882a593Smuzhiyun     REQUEST_AT_LEAST_SIZE(xRecordUnregisterClientsReq);
2558*4882a593Smuzhiyun     swapl(&stuff->context);
2559*4882a593Smuzhiyun     swapl(&stuff->nClients);
2560*4882a593Smuzhiyun     SwapRestL(stuff);
2561*4882a593Smuzhiyun     return ProcRecordUnregisterClients(client);
2562*4882a593Smuzhiyun }                               /* SProcRecordUnregisterClients */
2563*4882a593Smuzhiyun 
2564*4882a593Smuzhiyun static int _X_COLD
SProcRecordGetContext(ClientPtr client)2565*4882a593Smuzhiyun SProcRecordGetContext(ClientPtr client)
2566*4882a593Smuzhiyun {
2567*4882a593Smuzhiyun     REQUEST(xRecordGetContextReq);
2568*4882a593Smuzhiyun 
2569*4882a593Smuzhiyun     swaps(&stuff->length);
2570*4882a593Smuzhiyun     REQUEST_SIZE_MATCH(xRecordGetContextReq);
2571*4882a593Smuzhiyun     swapl(&stuff->context);
2572*4882a593Smuzhiyun     return ProcRecordGetContext(client);
2573*4882a593Smuzhiyun }                               /* SProcRecordGetContext */
2574*4882a593Smuzhiyun 
2575*4882a593Smuzhiyun static int _X_COLD
SProcRecordEnableContext(ClientPtr client)2576*4882a593Smuzhiyun SProcRecordEnableContext(ClientPtr client)
2577*4882a593Smuzhiyun {
2578*4882a593Smuzhiyun     REQUEST(xRecordEnableContextReq);
2579*4882a593Smuzhiyun 
2580*4882a593Smuzhiyun     swaps(&stuff->length);
2581*4882a593Smuzhiyun     REQUEST_SIZE_MATCH(xRecordEnableContextReq);
2582*4882a593Smuzhiyun     swapl(&stuff->context);
2583*4882a593Smuzhiyun     return ProcRecordEnableContext(client);
2584*4882a593Smuzhiyun }                               /* SProcRecordEnableContext */
2585*4882a593Smuzhiyun 
2586*4882a593Smuzhiyun static int _X_COLD
SProcRecordDisableContext(ClientPtr client)2587*4882a593Smuzhiyun SProcRecordDisableContext(ClientPtr client)
2588*4882a593Smuzhiyun {
2589*4882a593Smuzhiyun     REQUEST(xRecordDisableContextReq);
2590*4882a593Smuzhiyun 
2591*4882a593Smuzhiyun     swaps(&stuff->length);
2592*4882a593Smuzhiyun     REQUEST_SIZE_MATCH(xRecordDisableContextReq);
2593*4882a593Smuzhiyun     swapl(&stuff->context);
2594*4882a593Smuzhiyun     return ProcRecordDisableContext(client);
2595*4882a593Smuzhiyun }                               /* SProcRecordDisableContext */
2596*4882a593Smuzhiyun 
2597*4882a593Smuzhiyun static int _X_COLD
SProcRecordFreeContext(ClientPtr client)2598*4882a593Smuzhiyun SProcRecordFreeContext(ClientPtr client)
2599*4882a593Smuzhiyun {
2600*4882a593Smuzhiyun     REQUEST(xRecordFreeContextReq);
2601*4882a593Smuzhiyun 
2602*4882a593Smuzhiyun     swaps(&stuff->length);
2603*4882a593Smuzhiyun     REQUEST_SIZE_MATCH(xRecordFreeContextReq);
2604*4882a593Smuzhiyun     swapl(&stuff->context);
2605*4882a593Smuzhiyun     return ProcRecordFreeContext(client);
2606*4882a593Smuzhiyun }                               /* SProcRecordFreeContext */
2607*4882a593Smuzhiyun 
2608*4882a593Smuzhiyun static int _X_COLD
SProcRecordDispatch(ClientPtr client)2609*4882a593Smuzhiyun SProcRecordDispatch(ClientPtr client)
2610*4882a593Smuzhiyun {
2611*4882a593Smuzhiyun     REQUEST(xReq);
2612*4882a593Smuzhiyun 
2613*4882a593Smuzhiyun     switch (stuff->data) {
2614*4882a593Smuzhiyun     case X_RecordQueryVersion:
2615*4882a593Smuzhiyun         return SProcRecordQueryVersion(client);
2616*4882a593Smuzhiyun     case X_RecordCreateContext:
2617*4882a593Smuzhiyun         return SProcRecordCreateContext(client);
2618*4882a593Smuzhiyun     case X_RecordRegisterClients:
2619*4882a593Smuzhiyun         return SProcRecordRegisterClients(client);
2620*4882a593Smuzhiyun     case X_RecordUnregisterClients:
2621*4882a593Smuzhiyun         return SProcRecordUnregisterClients(client);
2622*4882a593Smuzhiyun     case X_RecordGetContext:
2623*4882a593Smuzhiyun         return SProcRecordGetContext(client);
2624*4882a593Smuzhiyun     case X_RecordEnableContext:
2625*4882a593Smuzhiyun         return SProcRecordEnableContext(client);
2626*4882a593Smuzhiyun     case X_RecordDisableContext:
2627*4882a593Smuzhiyun         return SProcRecordDisableContext(client);
2628*4882a593Smuzhiyun     case X_RecordFreeContext:
2629*4882a593Smuzhiyun         return SProcRecordFreeContext(client);
2630*4882a593Smuzhiyun     default:
2631*4882a593Smuzhiyun         return BadRequest;
2632*4882a593Smuzhiyun     }
2633*4882a593Smuzhiyun }                               /* SProcRecordDispatch */
2634*4882a593Smuzhiyun 
2635*4882a593Smuzhiyun /* RecordConnectionSetupInfo
2636*4882a593Smuzhiyun  *
2637*4882a593Smuzhiyun  * Arguments:
2638*4882a593Smuzhiyun  *	pContext is an enabled context that specifies recording of
2639*4882a593Smuzhiyun  *	  connection setup info.
2640*4882a593Smuzhiyun  *	pci holds the connection setup info.
2641*4882a593Smuzhiyun  *
2642*4882a593Smuzhiyun  * Returns: nothing.
2643*4882a593Smuzhiyun  *
2644*4882a593Smuzhiyun  * Side Effects:
2645*4882a593Smuzhiyun  *	The connection setup info is sent to the recording client.
2646*4882a593Smuzhiyun  */
2647*4882a593Smuzhiyun static void
RecordConnectionSetupInfo(RecordContextPtr pContext,NewClientInfoRec * pci)2648*4882a593Smuzhiyun RecordConnectionSetupInfo(RecordContextPtr pContext, NewClientInfoRec * pci)
2649*4882a593Smuzhiyun {
2650*4882a593Smuzhiyun     int prefixsize = SIZEOF(xConnSetupPrefix);
2651*4882a593Smuzhiyun     int restsize = pci->prefix->length * 4;
2652*4882a593Smuzhiyun 
2653*4882a593Smuzhiyun     if (pci->client->swapped) {
2654*4882a593Smuzhiyun         char *pConnSetup = (char *) malloc(prefixsize + restsize);
2655*4882a593Smuzhiyun 
2656*4882a593Smuzhiyun         if (!pConnSetup)
2657*4882a593Smuzhiyun             return;
2658*4882a593Smuzhiyun         SwapConnSetupPrefix(pci->prefix, (xConnSetupPrefix *) pConnSetup);
2659*4882a593Smuzhiyun         SwapConnSetupInfo((char *) pci->setup,
2660*4882a593Smuzhiyun                           (char *) (pConnSetup + prefixsize));
2661*4882a593Smuzhiyun         RecordAProtocolElement(pContext, pci->client, XRecordClientStarted,
2662*4882a593Smuzhiyun                                (void *) pConnSetup, prefixsize + restsize, 0,
2663*4882a593Smuzhiyun                                0);
2664*4882a593Smuzhiyun         free(pConnSetup);
2665*4882a593Smuzhiyun     }
2666*4882a593Smuzhiyun     else {
2667*4882a593Smuzhiyun         /* don't alloc and copy as in the swapped case; just send the
2668*4882a593Smuzhiyun          * data in two pieces
2669*4882a593Smuzhiyun          */
2670*4882a593Smuzhiyun         RecordAProtocolElement(pContext, pci->client, XRecordClientStarted,
2671*4882a593Smuzhiyun                                (void *) pci->prefix, prefixsize, 0, restsize);
2672*4882a593Smuzhiyun         RecordAProtocolElement(pContext, pci->client, XRecordClientStarted,
2673*4882a593Smuzhiyun                                (void *) pci->setup, restsize, 0,
2674*4882a593Smuzhiyun                                /* continuation */ -1);
2675*4882a593Smuzhiyun     }
2676*4882a593Smuzhiyun }                               /* RecordConnectionSetupInfo */
2677*4882a593Smuzhiyun 
2678*4882a593Smuzhiyun /* RecordDeleteContext
2679*4882a593Smuzhiyun  *
2680*4882a593Smuzhiyun  * Arguments:
2681*4882a593Smuzhiyun  *	pcbl is &ClientStateCallback.
2682*4882a593Smuzhiyun  *	nullata is NULL.
2683*4882a593Smuzhiyun  *	calldata is a pointer to a NewClientInfoRec (include/dixstruct.h)
2684*4882a593Smuzhiyun  *	which contains information about client state changes.
2685*4882a593Smuzhiyun  *
2686*4882a593Smuzhiyun  * Returns: nothing.
2687*4882a593Smuzhiyun  *
2688*4882a593Smuzhiyun  * Side Effects:
2689*4882a593Smuzhiyun  *	If a new client has connected and any contexts have specified
2690*4882a593Smuzhiyun  *	XRecordFutureClients, the new client is registered on those contexts.
2691*4882a593Smuzhiyun  *	If any of those contexts specify recording of the connection setup
2692*4882a593Smuzhiyun  *	info, it is recorded.
2693*4882a593Smuzhiyun  *
2694*4882a593Smuzhiyun  *	If an existing client has disconnected, it is deleted from any
2695*4882a593Smuzhiyun  *	contexts that it was registered on.  If any of those contexts
2696*4882a593Smuzhiyun  *	specified XRecordClientDied, they record a ClientDied protocol element.
2697*4882a593Smuzhiyun  *	If the disconnectiong client happened to be the data connection of an
2698*4882a593Smuzhiyun  *	enabled context, the context is disabled.
2699*4882a593Smuzhiyun  */
2700*4882a593Smuzhiyun 
2701*4882a593Smuzhiyun static void
RecordAClientStateChange(CallbackListPtr * pcbl,void * nulldata,void * calldata)2702*4882a593Smuzhiyun RecordAClientStateChange(CallbackListPtr *pcbl, void *nulldata,
2703*4882a593Smuzhiyun                          void *calldata)
2704*4882a593Smuzhiyun {
2705*4882a593Smuzhiyun     NewClientInfoRec *pci = (NewClientInfoRec *) calldata;
2706*4882a593Smuzhiyun     int i;
2707*4882a593Smuzhiyun     ClientPtr pClient = pci->client;
2708*4882a593Smuzhiyun     RecordContextPtr *ppAllContextsCopy = NULL;
2709*4882a593Smuzhiyun     int numContextsCopy = 0;
2710*4882a593Smuzhiyun 
2711*4882a593Smuzhiyun     switch (pClient->clientState) {
2712*4882a593Smuzhiyun     case ClientStateRunning:   /* new client */
2713*4882a593Smuzhiyun         for (i = 0; i < numContexts; i++) {
2714*4882a593Smuzhiyun             RecordClientsAndProtocolPtr pRCAP;
2715*4882a593Smuzhiyun             RecordContextPtr pContext = ppAllContexts[i];
2716*4882a593Smuzhiyun 
2717*4882a593Smuzhiyun             if ((pRCAP = RecordFindClientOnContext(pContext,
2718*4882a593Smuzhiyun                                                    XRecordFutureClients, NULL)))
2719*4882a593Smuzhiyun             {
2720*4882a593Smuzhiyun                 RecordAddClientToRCAP(pRCAP, pClient->clientAsMask);
2721*4882a593Smuzhiyun                 if (pContext->pRecordingClient && pRCAP->clientStarted)
2722*4882a593Smuzhiyun                     RecordConnectionSetupInfo(pContext, pci);
2723*4882a593Smuzhiyun             }
2724*4882a593Smuzhiyun         }
2725*4882a593Smuzhiyun         break;
2726*4882a593Smuzhiyun 
2727*4882a593Smuzhiyun     case ClientStateGone:
2728*4882a593Smuzhiyun     case ClientStateRetained:  /* client disconnected */
2729*4882a593Smuzhiyun 
2730*4882a593Smuzhiyun         /* RecordDisableContext modifies contents of ppAllContexts. */
2731*4882a593Smuzhiyun         numContextsCopy = numContexts;
2732*4882a593Smuzhiyun         ppAllContextsCopy = xallocarray(numContextsCopy,
2733*4882a593Smuzhiyun                                         sizeof(RecordContextPtr));
2734*4882a593Smuzhiyun         assert(ppAllContextsCopy);
2735*4882a593Smuzhiyun         memcpy(ppAllContextsCopy, ppAllContexts,
2736*4882a593Smuzhiyun                numContextsCopy * sizeof(RecordContextPtr));
2737*4882a593Smuzhiyun 
2738*4882a593Smuzhiyun         for (i = 0; i < numContextsCopy; i++) {
2739*4882a593Smuzhiyun             RecordClientsAndProtocolPtr pRCAP;
2740*4882a593Smuzhiyun             RecordContextPtr pContext = ppAllContextsCopy[i];
2741*4882a593Smuzhiyun             int pos;
2742*4882a593Smuzhiyun 
2743*4882a593Smuzhiyun             if (pContext->pRecordingClient == pClient)
2744*4882a593Smuzhiyun                 RecordDisableContext(pContext);
2745*4882a593Smuzhiyun             if ((pRCAP = RecordFindClientOnContext(pContext,
2746*4882a593Smuzhiyun                                                    pClient->clientAsMask,
2747*4882a593Smuzhiyun                                                    &pos))) {
2748*4882a593Smuzhiyun                 if (pContext->pRecordingClient && pRCAP->clientDied)
2749*4882a593Smuzhiyun                     RecordAProtocolElement(pContext, pClient,
2750*4882a593Smuzhiyun                                            XRecordClientDied, NULL, 0, 0, 0);
2751*4882a593Smuzhiyun                 RecordDeleteClientFromRCAP(pRCAP, pos);
2752*4882a593Smuzhiyun             }
2753*4882a593Smuzhiyun         }
2754*4882a593Smuzhiyun 
2755*4882a593Smuzhiyun         free(ppAllContextsCopy);
2756*4882a593Smuzhiyun         break;
2757*4882a593Smuzhiyun 
2758*4882a593Smuzhiyun     default:
2759*4882a593Smuzhiyun         break;
2760*4882a593Smuzhiyun     }                           /* end switch on client state */
2761*4882a593Smuzhiyun }                               /* RecordAClientStateChange */
2762*4882a593Smuzhiyun 
2763*4882a593Smuzhiyun /* RecordCloseDown
2764*4882a593Smuzhiyun  *
2765*4882a593Smuzhiyun  * Arguments:
2766*4882a593Smuzhiyun  *	extEntry is the extension information for RECORD.
2767*4882a593Smuzhiyun  *
2768*4882a593Smuzhiyun  * Returns: nothing.
2769*4882a593Smuzhiyun  *
2770*4882a593Smuzhiyun  * Side Effects:
2771*4882a593Smuzhiyun  *	Performs any cleanup needed by RECORD at server shutdown time.
2772*4882a593Smuzhiyun  *
2773*4882a593Smuzhiyun  */
2774*4882a593Smuzhiyun static void
RecordCloseDown(ExtensionEntry * extEntry)2775*4882a593Smuzhiyun RecordCloseDown(ExtensionEntry * extEntry)
2776*4882a593Smuzhiyun {
2777*4882a593Smuzhiyun     DeleteCallback(&ClientStateCallback, RecordAClientStateChange, NULL);
2778*4882a593Smuzhiyun }                               /* RecordCloseDown */
2779*4882a593Smuzhiyun 
2780*4882a593Smuzhiyun /* RecordExtensionInit
2781*4882a593Smuzhiyun  *
2782*4882a593Smuzhiyun  * Arguments: none.
2783*4882a593Smuzhiyun  *
2784*4882a593Smuzhiyun  * Returns: nothing.
2785*4882a593Smuzhiyun  *
2786*4882a593Smuzhiyun  * Side Effects:
2787*4882a593Smuzhiyun  *	Enables the RECORD extension if possible.
2788*4882a593Smuzhiyun  */
2789*4882a593Smuzhiyun void
RecordExtensionInit(void)2790*4882a593Smuzhiyun RecordExtensionInit(void)
2791*4882a593Smuzhiyun {
2792*4882a593Smuzhiyun     ExtensionEntry *extentry;
2793*4882a593Smuzhiyun 
2794*4882a593Smuzhiyun     RTContext = CreateNewResourceType(RecordDeleteContext, "RecordContext");
2795*4882a593Smuzhiyun     if (!RTContext)
2796*4882a593Smuzhiyun         return;
2797*4882a593Smuzhiyun 
2798*4882a593Smuzhiyun     if (!dixRegisterPrivateKey(RecordClientPrivateKey, PRIVATE_CLIENT, 0))
2799*4882a593Smuzhiyun         return;
2800*4882a593Smuzhiyun 
2801*4882a593Smuzhiyun     ppAllContexts = NULL;
2802*4882a593Smuzhiyun     numContexts = numEnabledContexts = numEnabledRCAPs = 0;
2803*4882a593Smuzhiyun 
2804*4882a593Smuzhiyun     if (!AddCallback(&ClientStateCallback, RecordAClientStateChange, NULL))
2805*4882a593Smuzhiyun         return;
2806*4882a593Smuzhiyun 
2807*4882a593Smuzhiyun     extentry = AddExtension(RECORD_NAME, RecordNumEvents, RecordNumErrors,
2808*4882a593Smuzhiyun                             ProcRecordDispatch, SProcRecordDispatch,
2809*4882a593Smuzhiyun                             RecordCloseDown, StandardMinorOpcode);
2810*4882a593Smuzhiyun     if (!extentry) {
2811*4882a593Smuzhiyun         DeleteCallback(&ClientStateCallback, RecordAClientStateChange, NULL);
2812*4882a593Smuzhiyun         return;
2813*4882a593Smuzhiyun     }
2814*4882a593Smuzhiyun     SetResourceTypeErrorValue(RTContext,
2815*4882a593Smuzhiyun                               extentry->errorBase + XRecordBadContext);
2816*4882a593Smuzhiyun 
2817*4882a593Smuzhiyun }                               /* RecordExtensionInit */
2818