summaryrefslogtreecommitdiff
path: root/di/props.c
blob: 8f2c7b3779e04734d0155cb063ac655c13e83bae (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
/* $Xorg: props.c,v 1.4 2001/02/09 02:05:32 xorgcvs Exp $ */
/*

Copyright 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

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

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

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

*/
/*
 * Copyright 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.
 *
 */
/* $XFree86: xc/programs/lbxproxy/di/props.c,v 1.7 2003/10/24 20:38:12 tsi 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"
#include	"proxyopts.h"
#include	"atomcache.h"

static Bool GetLbxChangePropertyReply();
static Bool GetLbxGetPropertyReply();

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

    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 >> 2);
	    break;
	case 16:
	    SwapShorts((short *) new->data, dlen >> 1);
	    break;
	}
    }
    return TagStoreDataNC(server, server->prop_cache, tid,
			  (dlen + sizeof(PropertyTagDataRec)),
			  LbxTagTypeProperty, new);
}

/*
 * lots of brains need to live here.  this decides whether or not
 * to use an LbxChangeProperty
 *
 * some possible heuristics:
 *
 *	- don't rewrite if it's small -- roundtrip swamps bandwidth
 *	- don't rewrite if it's a WM property and the WM isn't sharing
 *	  the proxy
 *	- don't rewrite if the prop is on a non-root window owned by
 *	  a non-proxy client
 *
 *	- do rewrite if it's a prop on one of the client's windows
 *	- do rewrite if it's a Selection property (hope is that
 *	  receiver is also using proxy)
 */
/*ARGSUSED*/
static Bool
rewrite_change_prop(
    ClientPtr client,
    Window win,
    Atom property,
    Atom type,
    int format,
    int mode,
    unsigned long nUnits
)
{
    unsigned flags;

    /* if tags are turned off, don't try */
    if (!client || !client->server)
	return FALSE;
    if (!client->server->lbxNegOpt.useTags)
	return FALSE;
    /* we aren't nearly clever enough to know what data we have */
    if (mode != PropModeReplace)
	return FALSE;
    if ((nUnits * (format >> 3)) < min_keep_prop_size)
	return FALSE;
    flags = FlagsForAtom(client->server, property);
    if (flags & AtomNoCacheFlag)
	return FALSE;
    if ((flags & AtomWMCacheFlag) && !client->server->wm_running)
	return FALSE;
    return TRUE;
}

int
ProcLBXChangeProperty(client)
    ClientPtr   client;
{
    REQUEST(xChangePropertyReq);
    ReplyStuffPtr nr;
    int         size;
    pointer     datacopy;
    char	n;
    Atom	type, property;
    Window	win;
    CARD32	nUnits;

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

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

    /* we may want to leave it as X_ChangeProperty */
    if (!rewrite_change_prop(client, win, property, type,
			     stuff->format, stuff->mode, nUnits))
	return ProcStandardRequest(client);

    size = nUnits * (stuff->format >> 3);
    datacopy = (pointer) xalloc(size);
    if (!datacopy)
	return ProcStandardRequest(client);
    memcpy((char *) datacopy, (char *) &stuff[1], size);

    nr = NewReply(client, client->server->lbxReq, X_LbxChangeProperty,
		  GetLbxChangePropertyReply);
    if (!nr) {
	xfree(datacopy);
	return ProcStandardRequest(client);
    }
    nr->request_info.lbxchangeprop.ptd.length = size;
    nr->request_info.lbxchangeprop.ptd.type = type;
    nr->request_info.lbxchangeprop.ptd.format = stuff->format;
    nr->request_info.lbxchangeprop.ptd.data = datacopy;

    FinishLBXRequest(client, REQ_REPLACELATE);
    SendChangeProperty(client, win, property, type,
		       stuff->format, stuff->mode, nUnits);
    return Success;
}

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

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

    ptdp = &nr->request_info.lbxchangeprop.ptd;
    if (tag) {
	if (!propTagStoreData(client->server, tag, (unsigned long)ptdp->length,
			      client->swapped, ptdp)) {
	    if (client->swapped) {
		switch (ptdp->format) {
		case 32:
		    SwapLongs((CARD32 *) ptdp->data, ptdp->length >> 2);
		    break;
		case 16:
		    SwapShorts((short *) ptdp->data, ptdp->length >> 1);
		    break;
		}
	    }
	    SendTagData(client, tag, ptdp->length, ptdp->data);
	    SendInvalidateTag(client, tag);
	    WriteError(client, X_ChangeProperty, 0, 0, BadAlloc);
	}
    }
    xfree(ptdp->data);		/* propStore gets its own copy */
    return TRUE;
}

int
ProcLBXGetProperty(client)
    ClientPtr   client;
{
    REQUEST(xGetPropertyReq);
    ReplyStuffPtr nr;

    nr = NewReply(client, client->server->lbxReq, X_LbxGetProperty,
		  GetLbxGetPropertyReply);
    if (!nr)
	return ProcStandardRequest(client);

    if (client->swapped) {
	char        n;

	swapl(&stuff->window, n);
	swapl(&stuff->property, n);
	swapl(&stuff->type, n);
	swapl(&stuff->longOffset, n);
	swapl(&stuff->longLength, n);
    }
    nr->request_info.lbxgetprop.offset = stuff->longOffset;
    nr->request_info.lbxgetprop.length = stuff->longLength;

    FinishLBXRequest(client, REQ_PASSTHROUGH);

    SendGetProperty(client, stuff->window, stuff->property, stuff->type,
		    stuff->delete, stuff->longOffset, stuff->longLength);
    return Success;
}

static Bool
GetLbxGetPropertyReply(client, nr, data)
    ClientPtr   client;
    ReplyStuffPtr nr;
    char       *data;
{
    xLbxGetPropertyReply *rep;
    unsigned long len;
    PropertyTagDataRec ptd;
    PropertyTagDataPtr ptdp;
    pointer     pdata = NULL;
    char	*sdata;
    char        n;
    xGetPropertyReply reply;
    CARD32	tag, nItems, type, bytesAfter;

    rep = (xLbxGetPropertyReply *) data;

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

    if (tag) {
	if (len) {	/* first time, set tag */

#ifdef LBX_STATS
	    getprop_full++;
#endif

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

	    ptdp = (PropertyTagDataPtr) TagGetData(client->server, 
			client->server->prop_cache, tag);

	    if (!ptdp) {
		fprintf(stderr, "no data for property tag 0x%lx\n", (long)tag);
		WriteError(client, X_GetProperty, 0, 0, BadAlloc);
		return TRUE;
	    }
	    if (client->swapped && rep->format != 8) {
		/*
		 * 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);
		switch (rep->format) {
		case 32:
		    SwapLongs((CARD32 *) pdata, ptdp->length >> 2);
		    break;
		case 16:
		    SwapShorts((short *) pdata, ptdp->length >> 1);
		    break;
		}
	    }

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

	}
    } else if (len) {

#ifdef LBX_STATS
	getprop_full++;
#endif

	/*
	 * server didn't send us a tag -- either can't store it or no prop
	 * data
	 */
	ptd.data = (pointer) &rep[1];
    }

    if (pdata)
	sdata = pdata;
    else
	sdata = ptdp->data;
    if (rep->tag) {
	len = ptdp->length;
	reply.bytesAfter = bytesAfter;
    } else {
	len = ptdp->length - (nr->request_info.lbxgetprop.offset << 2);
	len = min(len, nr->request_info.lbxgetprop.length << 2);
	reply.bytesAfter = (ptdp->length -
			    (len + (nr->request_info.lbxgetprop.offset << 2)));
	sdata = sdata + (nr->request_info.lbxgetprop.offset << 2);
    }

    reply.type = X_Reply;
    reply.sequenceNumber = rep->sequenceNumber;
    reply.format = ptdp->format;
    reply.length = (len + 3) >> 2;
    reply.propertyType = ptdp->type;
    if (len)
	reply.nItems = len / (ptdp->format >> 3);
    else
	reply.nItems = 0;

    if (client->swapped)
	SwapGetPropertyReply(&reply);
    WriteToClient(client, sizeof(xGetPropertyReply), (char *) &reply);
    if (len)
	WriteToClient(client, len, sdata);

    if (pdata)
	DEALLOCATE_LOCAL(pdata);

    return TRUE;
}