summaryrefslogtreecommitdiff
path: root/xc/workInProgress/lbx/programs/lbxproxy/di/props.c
blob: e2895bea962542d8604bfaaa31ff6fc1d165cb7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/* $XConsortium: props.c,v 1.9 95/05/30 18:58:43 mor Exp $ */
/*
 * Copyright 1994 Network Computing Devices, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name Network Computing Devices, Inc. not be
 * used in advertising or publicity pertaining to distribution of this
 * software without specific, written prior permission.
 *
 * THIS SOFTWARE IS PROVIDED `AS-IS'.  NETWORK COMPUTING DEVICES, INC.,
 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT
 * LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NONINFRINGEMENT.  IN NO EVENT SHALL NETWORK
 * COMPUTING DEVICES, INC., BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUDING
 * SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, INCLUDING LOSS OF USE, DATA,
 * OR PROFITS, EVEN IF ADVISED OF THE POSSIBILITY THEREOF, AND REGARDLESS OF
 * WHETHER IN AN ACTION IN CONTRACT, TORT OR NEGLIGENCE, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * $NCDId: @(#)props.c,v 1.13 1994/09/07 00:43:39 lemke Exp $
 */
/*
 * property handling
 */


#include	<stdio.h>
#include	"misc.h"
#include	"assert.h"
#include	"lbx.h"
#include	"util.h"
#include	"tags.h"
#include	"resource.h"
#include	"wire.h"
#include	"swap.h"

/*
 * XXX
 *
 * GetProperty notes
 *
 * GetProperty is handled by using X_LbxGetProperty to get the
 * entire property.  if the client asked for a subset, we only
 * ship that subset
 *
 * XXX is this a good idea?  will we transfer data that never gets
 * used?  or is it so useful that offset & length fields aren't
 * required in X_LbxGetProperty?
 */

/*
 * wrapper for tags
 *
 * since this stuff has pointers, have to package it for
 * tag cache
 */
static Bool
propTagStoreData(tid, dlen, swapit, ptdp)
    XID         tid;
    unsigned long dlen;
    Bool	swapit;
    PropertyTagDataPtr ptdp;
{
    PropertyTagDataPtr new,
                tmp;
    Bool        ret;

/* XXX this whole thing is a mess.  too many copies, too much mangling */
    new = (PropertyTagDataPtr) xalloc(dlen + sizeof(PropertyTagDataRec));
    if (!new)
	return FALSE;
    *new = *ptdp;
    new->data = (pointer) (new + 1);
    memcpy((char *) new->data, (char *) ptdp->data, dlen);
    /* save data in proxy format */
    if (swapit) {
	switch (new->format) {
	case 32:
	    SwapLongs((CARD32 *) new->data, dlen / 4);
	    break;
	case 16:
	    SwapShorts((short *) new->data, dlen / 2);
	    break;
	default:
	    break;
	}
    }
    ret = TagStoreData(prop_cache, tid,
		       (dlen + sizeof(PropertyTagDataRec)),
		       LbxTagTypeProperty, new);
    /*
     * XXX barf.  since the saved tag has a pointer to its own stuff, have to
     * tweak it after saving
     */
    if (ret) {
	tmp = (PropertyTagDataPtr) TagGetData(prop_cache, tid);
	tmp->data = (pointer) (tmp + 1);
    }
    xfree(new);
    return TRUE;
}

/*
 * lots of brains need to live here.  this decides whether or not
 * to use an LbxChangeProperty
 *
 * heuristics:
 *
 *	- don't rewrite if its 'small' -- roundtrip swamps bandwidth
 *	- don't rewrite if its a WM property and the WM isn't sharing
 *	  the proxy
 *	- don't rewrite if the prop is on a window owned by a non-proxy
 *	  client
 *
 *	- do rewrite if its a prop on one of the client's windows
 *	- do rewrite if its a Selection property (hope is that
 *	  receiver is also using proxy)
 */
static Bool
rewrite_change_prop(req)
    xChangePropertyReq *req;
{
    extern Bool lbxUseTags;

    /* if tags are turned off, don't try */
    if (!lbxUseTags)
	return FALSE;
    /* XXX don't even try append for now */
    if (req->mode != PropModeReplace)
	return FALSE;
    return TRUE;		/* XXX */
}

static int
change_property_req(client, data)
    ClientPtr   client;
    char       *data;
{
    ReplyStuffPtr nr;
    xChangePropertyReq *req;
    int         size;
    pointer     datacopy;
    char	n;
    Atom	type, property;
    Window	win;
    CARD32	nUnits;

    req = (xChangePropertyReq *) data;

    win = req->window;
    property = req->property;
    type = req->type;
    nUnits = req->nUnits;

    if (client->swapped) {
    	swapl(&win, n);
    	swapl(&property, n);
    	swapl(&type, n);
    	swapl(&nUnits, n);
    }

    size = nUnits * req->format / 8;
    datacopy = (pointer) xalloc(size);
    if (!datacopy)
	return REQ_NOCHANGE;
    memcpy((char *) datacopy, (char *) &req[1], size);

    nr = NewReply(client);
    if (!nr) {
	xfree(datacopy);
	return REQ_NOCHANGE;
    }
    nr->sequenceNumber = LBXSequenceNumber(client);
    nr->request = X_LbxChangeProperty;
    nr->lbx_req = TRUE;
    nr->extension = client->server->lbxReq;
    nr->request_info.lbxchangeprop.ptd.length = size;
    nr->request_info.lbxchangeprop.ptd.type = type;
    nr->request_info.lbxchangeprop.ptd.format = req->format;
    nr->request_info.lbxchangeprop.ptd.data = datacopy;

    /*
     * this expects a reply.  since we write the data here, we have to be sure
     * the seq number is in sync first
     */
    ForceSequenceUpdate(client);
    SendChangeProperty(client, win, property, type,
		       req->format, req->mode, nUnits);

    return REQ_REPLACE;
}

int
ProcLBXChangeProperty(client)
    ClientPtr   client;
{
    int         yank;

    REQUEST(xChangePropertyReq);

    /* we may want to leave it as X_ChangeProperty */
    if (!rewrite_change_prop(stuff)) {
	return ProcStandardRequest(client);
    }
    yank = change_property_req(client, client->requestBuffer);
    return FinishLBXRequest(client, yank);
}

Bool
GetLbxChangePropertyReply(client, data)
    ClientPtr   client;
    char       *data;
{
    xLbxChangePropertyReply *rep;
    ReplyStuffPtr nr;
    PropertyTagDataPtr ptdp;
    char	n;

    rep = (xLbxChangePropertyReply *) data;
    if (client->swapped) {
    	swapl(&rep->tag, n);
    }

    nr = GetMatchingReply(client, rep->sequenceNumber);
    assert(nr);
    ptdp = &nr->request_info.lbxchangeprop.ptd;
    if (rep->tag) {
	if (!propTagStoreData(rep->tag, (unsigned long)ptdp->length,
			      client->swapped, ptdp)) {
	    SendInvalidateTag(client, rep->tag);
/* XXX is this good enough?  or should we try to send the data on to
 * the server?
 */
	    SendErrorToClient(client, X_ChangeProperty, 0, ptdp->length, BadAlloc);
	}
    }
    xfree(ptdp->data);		/* propStore gets its own copy */
    return TRUE;
}

static int
get_property_req(client, data)
    ClientPtr   client;
    char       *data;
{
    ReplyStuffPtr nr;
    xGetPropertyReq *req;

    req = (xGetPropertyReq *) data;
    nr = NewReply(client);
    if (nr) {
	if (client->swapped) {
	    char        n;

	    swapl(&req->window, n);
	    swapl(&req->property, n);
	    swapl(&req->type, n);
	    swapl(&req->longOffset, n);
	    swapl(&req->longLength, n);
	}
	nr->sequenceNumber = LBXSequenceNumber(client);
	nr->request = X_LbxGetProperty;
	nr->lbx_req = TRUE;
	nr->extension = client->server->lbxReq;
	nr->request_info.lbxgetprop.offset = req->longOffset;
	nr->request_info.lbxgetprop.length = req->longLength;

	/*
	 * this expects a reply.  since we write the data here, we have to be
	 * sure the seq number is in sync first
	 */
	ForceSequenceUpdate(client);
	/*
	 * note we ask for the whole damn thing -- any truncating is done when
	 * we pass it on
	 */
	SendGetProperty(client, req->window, req->property, req->type,
			req->delete, 0, 1000000);

	return REQ_REPLACE;
    } else
	return REQ_NOCHANGE;
}

void
FinishGetPropertyReply(client, seqnum, offset, length, ptdp, pdata)
    ClientPtr   client;
    int         seqnum;
    CARD32      offset;
    CARD32      length;
    PropertyTagDataPtr ptdp;
    pointer     pdata;
{
    xGetPropertyReply reply;
    int         bytesafter,
                t,
                dlen;

    t = ptdp->length - (4 * offset);
    dlen = min(t, (4 * length));
    bytesafter = ptdp->length - (dlen + 4 * offset);

    if (!pdata)
	pdata = ptdp->data;

    pdata = (pointer) ((char *) pdata + (4 * offset));

    reply.type = X_Reply;
    reply.sequenceNumber = seqnum;
    reply.format = ptdp->format;
    reply.length = (dlen + 3) >> 2;;
    reply.propertyType = ptdp->type;
    reply.bytesAfter = bytesafter;
    if (dlen && ptdp->format)	/* avoid div-by-zero if no data */
	reply.nItems = dlen / (ptdp->format / 8);
    else
	reply.nItems = 0;

    if (client->swapped) {
	switch (reply.format) {
	case 32:
	    SwapLongs((CARD32 *) pdata, dlen / 4);
	    break;
	case 16:
	    SwapShorts((short *) pdata, dlen / 2);
	    break;
	default:
	    break;
	}
	SwapGetPropertyReply(&reply);
    }
    WriteToClient(client, sizeof(xGetPropertyReply), (char *) &reply);
    if (dlen)
	WriteToClient(client, dlen, pdata);
}

Bool
GetLbxGetPropertyReply(client, data)
    ClientPtr   client;
    char       *data;
{
    xLbxGetPropertyReply *rep;
    unsigned long len;
    ReplyStuffPtr nr;
    PropertyTagDataRec ptd;
    PropertyTagDataPtr ptdp;
    QueryTagRec qt;
    pointer     pdata = NULL;
    char        n;
    CARD32      tag,
                nItems;

    rep = (xLbxGetPropertyReply *) data;

    nr = GetMatchingReply(client, rep->sequenceNumber);
    assert(nr);

    ptd.type = rep->propertyType;
    ptd.format = rep->format;
    ptd.data = NULL;
    len = rep->length << 2;
    tag = rep->tag;
    nItems = rep->nItems;
    if (client->swapped) {
	swapl(&tag, n);
	swapl(&nItems, n);
	swapl(&ptd.type, n);
    }
    if (len)
	ptd.length = nItems * (rep->format / 8);
    else
	ptd.length = 0;
    ptdp = &ptd;

    if (tag) {
	if (rep->length) {	/* first time, set tag */

#ifdef LBX_STATS
	    getprop_full++;
#endif

	    ptd.data = (pointer) &rep[1];
	    if (!propTagStoreData(tag, len, client->swapped, &ptd)) {
		/* tell server we lost it */
		SendInvalidateTag(client, tag);
	    }
	} else {

	    ptdp = (PropertyTagDataPtr) TagGetData(prop_cache, tag);

	    if (!ptdp) {
		/* lost data -- ask again for tag value */

		qt.tag = tag;
		qt.tagtype = LbxTagTypeProperty;
		qt.typedata.getprop.offset =
		    nr->request_info.lbxgetprop.offset;
		qt.typedata.getprop.length =
		    nr->request_info.lbxgetprop.length;
		qt.typedata.getprop.ptd = ptd;
		QueryTag(client, &qt);

		/* XXX what is the right way to stack Queries? */
		return TRUE;

	    } else if (client->swapped) {
		/*
		 * Make a copy, because we will need to swap the property data
		 * and we don't want to alter the tag database.
		 */
		pdata = (pointer) ALLOCATE_LOCAL(ptdp->length);
		memcpy((char *) pdata, (char *) ptdp->data, ptdp->length);
	    }

#ifdef LBX_STATS
	    getprop_tag++;
	    tag_bytes_unsent += ptdp->length;
#endif

	}
    } else {

#ifdef LBX_STATS
	getprop_full++;
#endif

	/*
	 * server didn't send us a tag -- either can't store it or no prop
	 * data
	 */
	if (rep->length)
	    ptd.data = (pointer) &rep[1];
	if (client->swapped) {
	    pdata = (pointer) ALLOCATE_LOCAL(ptdp->length);
	    memcpy((char *) pdata, (char *) ptd.data, ptdp->length);
	    if (pdata) {
		switch (rep->format) {
		case 32:
		    SwapLongs((CARD32 *) pdata, ptdp->length / 4);
		    break;
		case 16:
		    SwapShorts((short *) pdata, ptdp->length / 2);
		    break;
		default:
		    break;
		}
	    }
	}
    }

    FinishGetPropertyReply(client, rep->sequenceNumber,
			   nr->request_info.lbxgetprop.offset,
			   nr->request_info.lbxgetprop.length,
			   ptdp, pdata);
    if (pdata)
	DEALLOCATE_LOCAL(pdata);

    return TRUE;
}

int
ProcLBXGetProperty(client)
    ClientPtr   client;
{
    int         yank;

    REQUEST(xGetPropertyReq);

    /* just pass through 'delete' messages */
    if (stuff->delete) {

#ifdef DEBUG
	fprintf(stderr, "Got GetProperty with 'delete' set -- passing through\n");
#endif

	return ProcStandardRequest(client);
    }
    yank = get_property_req(client, client->requestBuffer);
    return FinishLBXRequest(client, yank);
}