prop.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* $Xorg: prop.c,v 1.4 2001/02/09 02:06:01 xorgcvs Exp $ */
  2. /******************************************************************************
  3. Copyright 1993, 1998 The Open Group
  4. Permission to use, copy, modify, distribute, and sell this software and its
  5. documentation for any purpose is hereby granted without fee, provided that
  6. the above copyright notice appear in all copies and that both that
  7. copyright notice and this permission notice appear in supporting
  8. documentation.
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  15. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. Except as contained in this notice, the name of The Open Group shall not be
  18. used in advertising or otherwise to promote the sale, use or other dealings
  19. in this Software without prior written authorization from The Open Group.
  20. ******************************************************************************/
  21. /* $XFree86: xc/programs/xsm/prop.c,v 1.5tsi Exp $ */
  22. #include "xsm.h"
  23. #include "info.h"
  24. #include "prop.h"
  25. #include <X11/Xaw/List.h>
  26. void
  27. FreePropValues(List *propValues)
  28. {
  29. List *pv;
  30. PropValue *pval;
  31. for (pv = ListFirst (propValues); pv; pv = ListNext (pv))
  32. {
  33. pval = (PropValue *) pv->thing;
  34. XtFree ((char *) pval->value);
  35. XtFree ((char *) pval);
  36. }
  37. ListFreeAll (propValues);
  38. }
  39. void
  40. FreeProp(Prop *prop)
  41. {
  42. FreePropValues (prop->values);
  43. XtFree (prop->name);
  44. XtFree (prop->type);
  45. XtFree ((char *) prop);
  46. }
  47. void
  48. SetInitialProperties(ClientRec *client, List *props)
  49. {
  50. List *pl;
  51. if (verbose)
  52. printf("Setting initial properties for %s\n", client->clientId);
  53. if (client->props)
  54. {
  55. /*
  56. * The only way client->props could be non-NULL is if the list
  57. * was initialized, but nothing was added yet. So we just free
  58. * the head of the list.
  59. */
  60. XtFree ((char *) client->props);
  61. }
  62. client->props = props;
  63. for (pl = ListFirst (props); pl; pl = ListNext (pl))
  64. {
  65. Prop *pprop;
  66. PropValue *pval;
  67. List *vl;
  68. pprop = (Prop *) pl->thing;
  69. if (strcmp (pprop->name, SmDiscardCommand) == 0)
  70. {
  71. if (client->discardCommand)
  72. XtFree (client->discardCommand);
  73. vl = ListFirst (pprop->values);
  74. pval = (PropValue *) vl->thing;
  75. client->discardCommand = (char *) XtNewString (
  76. (char *) pval->value);
  77. }
  78. else if (strcmp (pprop->name, SmRestartStyleHint) == 0)
  79. {
  80. int hint;
  81. vl = ListFirst (pprop->values);
  82. pval = (PropValue *) vl->thing;
  83. hint = (int) *((char *) (pval->value));
  84. if (hint == SmRestartIfRunning || hint == SmRestartAnyway ||
  85. hint == SmRestartImmediately || hint == SmRestartNever)
  86. {
  87. client->restartHint = hint;
  88. }
  89. }
  90. }
  91. }
  92. void
  93. SetProperty(ClientRec *client, SmProp *theProp, Bool freeIt)
  94. {
  95. List *pl;
  96. Prop *pprop = NULL;
  97. int found = 0, i;
  98. /*
  99. * If the property exists, delete the property values. We can
  100. * re-use the actual property header.
  101. */
  102. for (pl = ListFirst (client->props); pl; pl = ListNext (pl))
  103. {
  104. pprop = (Prop *) pl->thing;
  105. if (strcmp (theProp->name, pprop->name) == 0 &&
  106. strcmp (theProp->type, pprop->type) == 0)
  107. {
  108. FreePropValues (pprop->values);
  109. found = 1;
  110. break;
  111. }
  112. }
  113. /*
  114. * Add the new property
  115. */
  116. if (!found)
  117. {
  118. pprop = (Prop *) XtMalloc (sizeof (Prop));
  119. pprop->name = XtNewString (theProp->name);
  120. pprop->type = XtNewString (theProp->type);
  121. }
  122. pprop->values = ListInit ();
  123. for (i = 0; i < theProp->num_vals; i++)
  124. {
  125. PropValue *pval = (PropValue *) XtMalloc (sizeof (PropValue));
  126. pval->length = theProp->vals[i].length;
  127. pval->value = (XtPointer) XtMalloc (theProp->vals[i].length + 1);
  128. memcpy (pval->value, theProp->vals[i].value, theProp->vals[i].length);
  129. ((char *) pval->value)[theProp->vals[i].length] = '\0';
  130. ListAddLast (pprop->values, (char *) pval);
  131. }
  132. if (pl)
  133. pl->thing = (char *) pprop;
  134. else
  135. ListAddLast (client->props, (char *) pprop);
  136. if (strcmp (theProp->name, SmDiscardCommand) == 0)
  137. {
  138. if (saveInProgress)
  139. {
  140. /*
  141. * We are in the middle of a save yourself. We save the
  142. * discard command we get now, and make it the current discard
  143. * command when the save is over.
  144. */
  145. if (client->saveDiscardCommand)
  146. XtFree (client->saveDiscardCommand);
  147. client->saveDiscardCommand =
  148. (char *) XtNewString ((char *) theProp->vals[0].value);
  149. client->receivedDiscardCommand = True;
  150. }
  151. else
  152. {
  153. if (client->discardCommand)
  154. XtFree (client->discardCommand);
  155. client->discardCommand =
  156. (char *) XtNewString ((char *) theProp->vals[0].value);
  157. }
  158. }
  159. else if (strcmp (theProp->name, SmRestartStyleHint) == 0)
  160. {
  161. int hint = (int) *((char *) (theProp->vals[0].value));
  162. if (hint == SmRestartIfRunning || hint == SmRestartAnyway ||
  163. hint == SmRestartImmediately || hint == SmRestartNever)
  164. {
  165. client->restartHint = hint;
  166. }
  167. }
  168. if (freeIt)
  169. SmFreeProperty (theProp);
  170. }
  171. void
  172. DeleteProperty(ClientRec *client, char *propname)
  173. {
  174. List *pl;
  175. for (pl = ListFirst (client->props); pl; pl = ListNext (pl))
  176. {
  177. Prop *pprop = (Prop *) pl->thing;
  178. if (strcmp (pprop->name, propname) == 0)
  179. {
  180. FreeProp (pprop);
  181. ListFreeOne (pl);
  182. if (strcmp (propname, SmDiscardCommand) == 0)
  183. {
  184. if (client->discardCommand)
  185. {
  186. XtFree (client->discardCommand);
  187. client->discardCommand = NULL;
  188. }
  189. if (client->saveDiscardCommand)
  190. {
  191. XtFree (client->saveDiscardCommand);
  192. client->saveDiscardCommand = NULL;
  193. }
  194. }
  195. break;
  196. }
  197. }
  198. }
  199. void
  200. SetPropertiesProc(SmsConn smsConn, SmPointer managerData, int numProps,
  201. SmProp **props)
  202. {
  203. ClientRec *client = (ClientRec *) managerData;
  204. int updateList, i;
  205. if (verbose)
  206. {
  207. printf ("Client Id = %s, received SET PROPERTIES ", client->clientId);
  208. printf ("[Num props = %d]\n", numProps);
  209. }
  210. updateList = (ListCount (client->props) == 0) &&
  211. numProps > 0 && client_info_visible;
  212. for (i = 0; i < numProps; i++)
  213. {
  214. SetProperty (client, props[i], True /* free it */);
  215. }
  216. free ((char *) props);
  217. if (updateList)
  218. {
  219. /*
  220. * We have enough info from the client to display it in our list.
  221. */
  222. UpdateClientList ();
  223. XawListHighlight (clientListWidget, current_client_selected);
  224. }
  225. else if (client_prop_visible && clientListRecs &&
  226. clientListRecs[current_client_selected] == client)
  227. {
  228. DisplayProps (client);
  229. }
  230. }
  231. void
  232. DeletePropertiesProc(SmsConn smsConn, SmPointer managerData,
  233. int numProps, char **propNames)
  234. {
  235. ClientRec *client = (ClientRec *) managerData;
  236. int i;
  237. if (verbose) {
  238. printf ("Client Id = %s, received DELETE PROPERTIES ",
  239. client->clientId);
  240. printf ("[Num props = %d]\n", numProps);
  241. }
  242. for (i = 0; i < numProps; i++)
  243. {
  244. if (verbose)
  245. printf (" Name: %s\n", propNames[i]);
  246. DeleteProperty (client, propNames[i]);
  247. free (propNames[i]);
  248. }
  249. free ((char *) propNames);
  250. }
  251. void
  252. GetPropertiesProc(SmsConn smsConn, SmPointer managerData)
  253. {
  254. ClientRec *client = (ClientRec *) managerData;
  255. SmProp **propsRet, *propRet;
  256. SmPropValue *propValRet;
  257. Prop *pprop;
  258. PropValue *pval;
  259. List *pl, *pj;
  260. int numProps;
  261. int index, i;
  262. if (verbose)
  263. {
  264. printf ("Client Id = %s, received GET PROPERTIES\n", client->clientId);
  265. printf ("\n");
  266. }
  267. /*
  268. * Unfortunately, we store the properties in a format different
  269. * from the one required by SMlib.
  270. */
  271. numProps = ListCount (client->props);
  272. propsRet = (SmProp **) XtMalloc (numProps * sizeof (SmProp *));
  273. index = 0;
  274. for (pl = ListFirst (client->props); pl; pl = ListNext (pl))
  275. {
  276. propsRet[index] = propRet = (SmProp *) XtMalloc (sizeof (SmProp));
  277. pprop = (Prop *) pl->thing;
  278. propRet->name = XtNewString (pprop->name);
  279. propRet->type = XtNewString (pprop->type);
  280. propRet->num_vals = ListCount (pprop->values);
  281. propRet->vals = propValRet = (SmPropValue *) XtMalloc (
  282. propRet->num_vals * sizeof (SmPropValue));
  283. for (pj = ListFirst (pprop->values); pj; pj = ListNext (pj))
  284. {
  285. pval = (PropValue *) pj->thing;
  286. propValRet->length = pval->length;
  287. propValRet->value = (SmPointer) XtMalloc (pval->length);
  288. memcpy (propValRet->value, pval->value, pval->length);
  289. propValRet++;
  290. }
  291. index++;
  292. }
  293. SmsReturnProperties (smsConn, numProps, propsRet);
  294. if (verbose)
  295. {
  296. printf ("Client Id = %s, sent PROPERTIES REPLY [Num props = %d]\n",
  297. client->clientId, numProps);
  298. }
  299. for (i = 0; i < numProps; i++)
  300. SmFreeProperty (propsRet[i]);
  301. XtFree ((char *) propsRet);
  302. }