summaryrefslogtreecommitdiff
path: root/prop.c
blob: b5ebac84b59c2c6490c0a7311da4e82e201e0e7d (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
/* $Xorg: prop.c,v 1.4 2001/02/09 02:06:01 xorgcvs Exp $ */
/******************************************************************************

Copyright 1993, 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.
******************************************************************************/
/* $XFree86: xc/programs/xsm/prop.c,v 1.5tsi Exp $ */

#include "xsm.h"
#include "info.h"
#include "prop.h"
#include <X11/Xaw/List.h>


void
FreePropValues(List *propValues)
{
    List	*pv;
    PropValue	*pval;

    for (pv = ListFirst (propValues); pv; pv = ListNext (pv))
    {
	pval = (PropValue *) pv->thing;
	XtFree ((char *) pval->value);
	XtFree ((char *) pval);
    }

    ListFreeAll (propValues);
}



void
FreeProp(Prop *prop)
{
    FreePropValues (prop->values);
    XtFree (prop->name);
    XtFree (prop->type);
    XtFree ((char *) prop);
}



void
SetInitialProperties(ClientRec *client, List *props)
{
    List *pl;

    if (verbose)
	printf("Setting initial properties for %s\n", client->clientId);

    if (client->props)
    {
	/*
	 * The only way client->props could be non-NULL is if the list
	 * was initialized, but nothing was added yet.  So we just free
	 * the head of the list.
	 */

	XtFree ((char *) client->props);
    }

    client->props = props;

    for (pl = ListFirst (props); pl; pl = ListNext (pl))
    {
	Prop		*pprop;
	PropValue	*pval;
	List		*vl;

	pprop = (Prop *) pl->thing;

	if (strcmp (pprop->name, SmDiscardCommand) == 0)
	{
	    if (client->discardCommand)
		XtFree (client->discardCommand);

	    vl = ListFirst (pprop->values);
	    pval = (PropValue *) vl->thing;

	    client->discardCommand = (char *) XtNewString (
		(char *) pval->value);
	}
	else if (strcmp (pprop->name, SmRestartStyleHint) == 0)
	{
	    int hint;

	    vl = ListFirst (pprop->values);
	    pval = (PropValue *) vl->thing;

	    hint = (int) *((char *) (pval->value));

	    if (hint == SmRestartIfRunning || hint == SmRestartAnyway ||
		hint == SmRestartImmediately || hint == SmRestartNever)
	    {
		client->restartHint = hint;
	    }
	}
    }
}



void
SetProperty(ClientRec *client, SmProp *theProp, Bool freeIt)
{
    List 	*pl;
    Prop	*pprop = NULL;
    int		found = 0, i;

    /*
     * If the property exists, delete the property values.  We can
     * re-use the actual property header.
     */

    for (pl = ListFirst (client->props); pl; pl = ListNext (pl))
    {
	pprop = (Prop *) pl->thing;

	if (strcmp (theProp->name, pprop->name) == 0 &&
	    strcmp (theProp->type, pprop->type) == 0)
	{
	    FreePropValues (pprop->values);
	    found = 1;
	    break;
	}
    }


    /*
     * Add the new property
     */

    if (!found)
    {
	pprop = (Prop *) XtMalloc (sizeof (Prop));
	pprop->name = XtNewString (theProp->name);
	pprop->type = XtNewString (theProp->type);
    }

    pprop->values = ListInit ();

    for (i = 0; i < theProp->num_vals; i++)
    {
	PropValue *pval = (PropValue *) XtMalloc (sizeof (PropValue));

	pval->length = theProp->vals[i].length;
	pval->value = (XtPointer) XtMalloc (theProp->vals[i].length + 1);
	memcpy (pval->value, theProp->vals[i].value, theProp->vals[i].length);
	((char *) pval->value)[theProp->vals[i].length] = '\0';

	ListAddLast (pprop->values, (char *) pval);
    }

    if (pl)
	pl->thing = (char *) pprop;
    else
	ListAddLast (client->props, (char *) pprop);

    if (strcmp (theProp->name, SmDiscardCommand) == 0)
    {
	if (saveInProgress)
	{
	    /*
	     * We are in the middle of a save yourself.  We save the
	     * discard command we get now, and make it the current discard
	     * command when the save is over.
	     */

	    if (client->saveDiscardCommand)
		XtFree (client->saveDiscardCommand);
	    client->saveDiscardCommand =
		(char *) XtNewString ((char *) theProp->vals[0].value);

	    client->receivedDiscardCommand = True;
	}
	else
	{
	    if (client->discardCommand)
		XtFree (client->discardCommand);
	    client->discardCommand =
		(char *) XtNewString ((char *) theProp->vals[0].value);
	}
    }
    else if (strcmp (theProp->name, SmRestartStyleHint) == 0)
    {
	int hint = (int) *((char *) (theProp->vals[0].value));

	if (hint == SmRestartIfRunning || hint == SmRestartAnyway ||
	    hint == SmRestartImmediately || hint == SmRestartNever)
	{
	    client->restartHint = hint;
	}
    }

    if (freeIt)
	SmFreeProperty (theProp);
}



void
DeleteProperty(ClientRec *client, char *propname)
{
    List *pl;

    for (pl = ListFirst (client->props); pl; pl = ListNext (pl))
    {
	Prop *pprop = (Prop *) pl->thing;

	if (strcmp (pprop->name, propname) == 0)
	{
	    FreeProp (pprop);
	    ListFreeOne (pl);

	    if (strcmp (propname, SmDiscardCommand) == 0)
	    {
		if (client->discardCommand)
		{
		    XtFree (client->discardCommand);
		    client->discardCommand = NULL;
		}

		if (client->saveDiscardCommand)
		{
		    XtFree (client->saveDiscardCommand);
		    client->saveDiscardCommand = NULL;
		}
	    }
	    break;
	}
    }
}



void
SetPropertiesProc(SmsConn smsConn, SmPointer managerData, int numProps, 
		  SmProp **props)
{
    ClientRec	*client = (ClientRec *) managerData;
    int		updateList, i;

    if (verbose)
    {
	printf ("Client Id = %s, received SET PROPERTIES ", client->clientId);
	printf ("[Num props = %d]\n", numProps);
    }

    updateList = (ListCount (client->props) == 0) &&
	numProps > 0 && client_info_visible;

    for (i = 0; i < numProps; i++)
    {
	SetProperty (client, props[i], True /* free it */);
    }

    free ((char *) props);

    if (updateList)
    {
	/*
	 * We have enough info from the client to display it in our list.
	 */

	UpdateClientList ();
	XawListHighlight (clientListWidget, current_client_selected);
    }
    else if (client_prop_visible && clientListRecs &&
	clientListRecs[current_client_selected] == client)
    {
	DisplayProps (client);
    }
}



void
DeletePropertiesProc(SmsConn smsConn, SmPointer managerData, 
		     int numProps, char **propNames)

{
    ClientRec	*client = (ClientRec *) managerData;
    int		i;

    if (verbose) {
	printf ("Client Id = %s, received DELETE PROPERTIES ",
	    client->clientId);
	printf ("[Num props = %d]\n", numProps);
    }

    for (i = 0; i < numProps; i++)
    {
	if (verbose)
	    printf ("   Name:	%s\n", propNames[i]);

	DeleteProperty (client, propNames[i]);

	free (propNames[i]);
    }

    free ((char *) propNames);
}



void
GetPropertiesProc(SmsConn smsConn, SmPointer managerData)
{
    ClientRec	*client = (ClientRec *) managerData;
    SmProp	**propsRet, *propRet;
    SmPropValue *propValRet;
    Prop	*pprop;
    PropValue	*pval;
    List	*pl, *pj;
    int		numProps;
    int		index, i;

    if (verbose)
    {
	printf ("Client Id = %s, received GET PROPERTIES\n", client->clientId);
	printf ("\n");
    }

    /*
     * Unfortunately, we store the properties in a format different
     * from the one required by SMlib.
     */

    numProps = ListCount (client->props);
    propsRet = (SmProp **) XtMalloc (numProps * sizeof (SmProp *));

    index = 0;
    for (pl = ListFirst (client->props); pl; pl = ListNext (pl))
    {
	propsRet[index] = propRet = (SmProp *) XtMalloc (sizeof (SmProp));

	pprop = (Prop *) pl->thing;

	propRet->name = XtNewString (pprop->name);
	propRet->type = XtNewString (pprop->type);
	propRet->num_vals = ListCount (pprop->values);
	propRet->vals = propValRet = (SmPropValue *) XtMalloc (
	    propRet->num_vals * sizeof (SmPropValue));

	for (pj = ListFirst (pprop->values); pj; pj = ListNext (pj))
	{
	    pval = (PropValue *) pj->thing;

	    propValRet->length = pval->length;
	    propValRet->value = (SmPointer) XtMalloc (pval->length);
	    memcpy (propValRet->value, pval->value, pval->length);

	    propValRet++;
	}

	index++;
    }

    SmsReturnProperties (smsConn, numProps, propsRet);

    if (verbose)
    {
	printf ("Client Id = %s, sent PROPERTIES REPLY [Num props = %d]\n",
		client->clientId, numProps);
    }

    for (i = 0; i < numProps; i++)
	SmFreeProperty (propsRet[i]);
    XtFree ((char *) propsRet);
}