property.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /***********************************************************
  2. Copyright 1987, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
  20. All Rights Reserved
  21. Permission to use, copy, modify, and distribute this software and its
  22. documentation for any purpose and without fee is hereby granted,
  23. provided that the above copyright notice appear in all copies and that
  24. both that copyright notice and this permission notice appear in
  25. supporting documentation, and that the name of Digital not be
  26. used in advertising or publicity pertaining to distribution of the
  27. software without specific, written prior permission.
  28. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  29. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  30. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  31. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  32. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  33. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  34. SOFTWARE.
  35. ******************************************************************/
  36. #ifdef HAVE_DIX_CONFIG_H
  37. #include <dix-config.h>
  38. #endif
  39. #include <X11/X.h>
  40. #include <X11/Xproto.h>
  41. #include "windowstr.h"
  42. #include "propertyst.h"
  43. #include "dixstruct.h"
  44. #include "dispatch.h"
  45. #include "swaprep.h"
  46. /*****************************************************************
  47. * Property Stuff
  48. *
  49. * ChangeProperty, DeleteProperty, GetProperties,
  50. * ListProperties
  51. *
  52. * Properties below to windows. A allocate slots each time
  53. * a property is added. No fancy searching done.
  54. *
  55. *****************************************************************/
  56. #ifdef notdef
  57. static void
  58. PrintPropertys(WindowPtr pWin)
  59. {
  60. PropertyPtr pProp;
  61. int j;
  62. pProp = pWin->userProps;
  63. while (pProp) {
  64. ErrorF("%x %x\n", pProp->propertyName, pProp->type);
  65. ErrorF("property format: %d\n", pProp->format);
  66. ErrorF("property data: \n");
  67. for (j = 0; j < (pProp->format / 8) * pProp->size; j++)
  68. ErrorF("%c\n", pProp->data[j]);
  69. pProp = pProp->next;
  70. }
  71. }
  72. #endif
  73. int
  74. ProcRotateProperties(ClientPtr client)
  75. {
  76. int i, j, delta;
  77. REQUEST(xRotatePropertiesReq);
  78. WindowPtr pWin;
  79. Atom *atoms;
  80. PropertyPtr *props; /* array of pointer */
  81. PropertyPtr pProp;
  82. xEvent event;
  83. REQUEST_FIXED_SIZE(xRotatePropertiesReq, stuff->nAtoms << 2);
  84. UpdateCurrentTime();
  85. pWin = (WindowPtr) SecurityLookupWindow(stuff->window, client,
  86. SecurityWriteAccess);
  87. if (!pWin)
  88. return (BadWindow);
  89. if (!stuff->nAtoms)
  90. return (Success);
  91. atoms = (Atom *) &stuff[1];
  92. props = (PropertyPtr *) ALLOCATE_LOCAL(stuff->nAtoms * sizeof(PropertyPtr));
  93. if (!props)
  94. return (BadAlloc);
  95. for (i = 0; i < stuff->nAtoms; i++) {
  96. if (!ValidAtom(atoms[i])
  97. ) {
  98. DEALLOCATE_LOCAL(props);
  99. client->errorValue = atoms[i];
  100. return BadAtom;
  101. }
  102. for (j = i + 1; j < stuff->nAtoms; j++)
  103. if (atoms[j] == atoms[i]) {
  104. DEALLOCATE_LOCAL(props);
  105. return BadMatch;
  106. }
  107. pProp = wUserProps(pWin);
  108. while (pProp) {
  109. if (pProp->propertyName == atoms[i])
  110. goto found;
  111. pProp = pProp->next;
  112. }
  113. DEALLOCATE_LOCAL(props);
  114. return BadMatch;
  115. found:
  116. props[i] = pProp;
  117. }
  118. delta = stuff->nPositions;
  119. /* If the rotation is a complete 360 degrees, then moving the properties
  120. around and generating PropertyNotify events should be skipped. */
  121. if ((stuff->nAtoms != 0) && (abs(delta) % stuff->nAtoms) != 0) {
  122. while (delta < 0) /* faster if abs value is small */
  123. delta += stuff->nAtoms;
  124. for (i = 0; i < stuff->nAtoms; i++) {
  125. /* Generate a PropertyNotify event for each property whose value
  126. is changed in the order in which they appear in the request. */
  127. event.u.u.type = PropertyNotify;
  128. event.u.property.window = pWin->drawable.id;
  129. event.u.property.state = PropertyNewValue;
  130. event.u.property.atom = props[i]->propertyName;
  131. event.u.property.time = currentTime.milliseconds;
  132. DeliverEvents(pWin, &event, 1, (WindowPtr) NULL);
  133. props[i]->propertyName = atoms[(i + delta) % stuff->nAtoms];
  134. }
  135. }
  136. DEALLOCATE_LOCAL(props);
  137. return Success;
  138. }
  139. int
  140. ProcChangeProperty(ClientPtr client)
  141. {
  142. WindowPtr pWin;
  143. char format, mode;
  144. unsigned long len;
  145. int sizeInBytes;
  146. int totalSize;
  147. int err;
  148. REQUEST(xChangePropertyReq);
  149. REQUEST_AT_LEAST_SIZE(xChangePropertyReq);
  150. UpdateCurrentTime();
  151. format = stuff->format;
  152. mode = stuff->mode;
  153. if ((mode != PropModeReplace) && (mode != PropModeAppend) &&
  154. (mode != PropModePrepend)) {
  155. client->errorValue = mode;
  156. return BadValue;
  157. }
  158. if ((format != 8) && (format != 16) && (format != 32)) {
  159. client->errorValue = format;
  160. return BadValue;
  161. }
  162. len = stuff->nUnits;
  163. if (len > ((0xffffffff - sizeof(xChangePropertyReq)) >> 2))
  164. return BadLength;
  165. sizeInBytes = format >> 3;
  166. totalSize = len * sizeInBytes;
  167. REQUEST_FIXED_SIZE(xChangePropertyReq, totalSize);
  168. pWin = (WindowPtr) SecurityLookupWindow(stuff->window, client,
  169. SecurityWriteAccess);
  170. if (!pWin)
  171. return (BadWindow);
  172. if (!ValidAtom(stuff->property)) {
  173. client->errorValue = stuff->property;
  174. return (BadAtom);
  175. }
  176. if (!ValidAtom(stuff->type)) {
  177. client->errorValue = stuff->type;
  178. return (BadAtom);
  179. }
  180. err = ChangeWindowProperty(pWin, stuff->property, stuff->type, (int) format,
  181. (int) mode, len, (pointer) &stuff[1], TRUE);
  182. if (err != Success)
  183. return err;
  184. else
  185. return client->noClientException;
  186. }
  187. _X_EXPORT int
  188. ChangeWindowProperty(WindowPtr pWin, Atom property, Atom type, int format,
  189. int mode, unsigned long len, pointer value, Bool sendevent)
  190. {
  191. PropertyPtr pProp;
  192. xEvent event;
  193. int sizeInBytes;
  194. int totalSize;
  195. pointer data;
  196. sizeInBytes = format >> 3;
  197. totalSize = len * sizeInBytes;
  198. /* first see if property already exists */
  199. pProp = wUserProps(pWin);
  200. while (pProp) {
  201. if (pProp->propertyName == property)
  202. break;
  203. pProp = pProp->next;
  204. }
  205. if (!pProp) { /* just add to list */
  206. if (!pWin->optional && !MakeWindowOptional(pWin))
  207. return (BadAlloc);
  208. pProp = malloc(sizeof(PropertyRec));
  209. if (!pProp)
  210. return (BadAlloc);
  211. data = malloc(totalSize);
  212. if (!data && len) {
  213. free(pProp);
  214. return (BadAlloc);
  215. }
  216. pProp->propertyName = property;
  217. pProp->type = type;
  218. pProp->format = format;
  219. pProp->data = data;
  220. if (len)
  221. memmove((char *) data, (char *) value, totalSize);
  222. pProp->size = len;
  223. pProp->next = pWin->optional->userProps;
  224. pWin->optional->userProps = pProp;
  225. }
  226. else {
  227. /* To append or prepend to a property the request format and type
  228. must match those of the already defined property. The
  229. existing format and type are irrelevant when using the mode
  230. "PropModeReplace" since they will be written over. */
  231. if ((format != pProp->format) && (mode != PropModeReplace))
  232. return (BadMatch);
  233. if ((pProp->type != type) && (mode != PropModeReplace))
  234. return (BadMatch);
  235. if (mode == PropModeReplace) {
  236. if (totalSize != pProp->size * (pProp->format >> 3)) {
  237. data = (pointer) realloc(pProp->data, totalSize);
  238. if (!data && len)
  239. return (BadAlloc);
  240. pProp->data = data;
  241. }
  242. if (len)
  243. memmove((char *) pProp->data, (char *) value, totalSize);
  244. pProp->size = len;
  245. pProp->type = type;
  246. pProp->format = format;
  247. }
  248. else if (len == 0) {
  249. /* do nothing */
  250. }
  251. else if (mode == PropModeAppend) {
  252. data = (pointer) realloc(pProp->data,
  253. sizeInBytes * (len + pProp->size));
  254. if (!data)
  255. return (BadAlloc);
  256. pProp->data = data;
  257. memmove(&((char *) data)[pProp->size * sizeInBytes],
  258. (char *) value, totalSize);
  259. pProp->size += len;
  260. }
  261. else if (mode == PropModePrepend) {
  262. data = malloc(sizeInBytes * (len + pProp->size));
  263. if (!data)
  264. return (BadAlloc);
  265. memmove(&((char *) data)[totalSize], (char *) pProp->data,
  266. (int) (pProp->size * sizeInBytes));
  267. memmove((char *) data, (char *) value, totalSize);
  268. free(pProp->data);
  269. pProp->data = data;
  270. pProp->size += len;
  271. }
  272. }
  273. if (sendevent) {
  274. event.u.u.type = PropertyNotify;
  275. event.u.property.window = pWin->drawable.id;
  276. event.u.property.state = PropertyNewValue;
  277. event.u.property.atom = pProp->propertyName;
  278. event.u.property.time = currentTime.milliseconds;
  279. DeliverEvents(pWin, &event, 1, (WindowPtr) NULL);
  280. }
  281. return (Success);
  282. }
  283. int
  284. DeleteProperty(WindowPtr pWin, Atom propName)
  285. {
  286. PropertyPtr pProp, prevProp;
  287. xEvent event;
  288. if (!(pProp = wUserProps(pWin)))
  289. return (Success);
  290. prevProp = (PropertyPtr) NULL;
  291. while (pProp) {
  292. if (pProp->propertyName == propName)
  293. break;
  294. prevProp = pProp;
  295. pProp = pProp->next;
  296. }
  297. if (pProp) {
  298. if (prevProp == (PropertyPtr) NULL) { /* takes care of head */
  299. if (!(pWin->optional->userProps = pProp->next))
  300. CheckWindowOptionalNeed(pWin);
  301. }
  302. else {
  303. prevProp->next = pProp->next;
  304. }
  305. event.u.u.type = PropertyNotify;
  306. event.u.property.window = pWin->drawable.id;
  307. event.u.property.state = PropertyDelete;
  308. event.u.property.atom = pProp->propertyName;
  309. event.u.property.time = currentTime.milliseconds;
  310. DeliverEvents(pWin, &event, 1, (WindowPtr) NULL);
  311. free(pProp->data);
  312. free(pProp);
  313. }
  314. return (Success);
  315. }
  316. void
  317. DeleteAllWindowProperties(WindowPtr pWin)
  318. {
  319. PropertyPtr pProp, pNextProp;
  320. xEvent event;
  321. pProp = wUserProps(pWin);
  322. while (pProp) {
  323. event.u.u.type = PropertyNotify;
  324. event.u.property.window = pWin->drawable.id;
  325. event.u.property.state = PropertyDelete;
  326. event.u.property.atom = pProp->propertyName;
  327. event.u.property.time = currentTime.milliseconds;
  328. DeliverEvents(pWin, &event, 1, (WindowPtr) NULL);
  329. pNextProp = pProp->next;
  330. free(pProp->data);
  331. free(pProp);
  332. pProp = pNextProp;
  333. }
  334. }
  335. static int
  336. NullPropertyReply(ClientPtr client,
  337. ATOM propertyType, int format, xGetPropertyReply * reply)
  338. {
  339. reply->nItems = 0;
  340. reply->length = 0;
  341. reply->bytesAfter = 0;
  342. reply->propertyType = propertyType;
  343. reply->format = format;
  344. WriteReplyToClient(client, sizeof(xGenericReply), reply);
  345. return (client->noClientException);
  346. }
  347. /*****************
  348. * GetProperty
  349. * If type Any is specified, returns the property from the specified
  350. * window regardless of its type. If a type is specified, returns the
  351. * property only if its type equals the specified type.
  352. * If delete is True and a property is returned, the property is also
  353. * deleted from the window and a PropertyNotify event is generated on the
  354. * window.
  355. *****************/
  356. int
  357. ProcGetProperty(ClientPtr client)
  358. {
  359. PropertyPtr pProp, prevProp;
  360. unsigned long n, len, ind;
  361. WindowPtr pWin;
  362. xGetPropertyReply reply = {0};
  363. REQUEST(xGetPropertyReq);
  364. REQUEST_SIZE_MATCH(xGetPropertyReq);
  365. if (stuff->delete)
  366. UpdateCurrentTime();
  367. pWin = (WindowPtr) SecurityLookupWindow(stuff->window, client,
  368. SecurityReadAccess);
  369. if (!pWin)
  370. return BadWindow;
  371. if (!ValidAtom(stuff->property)) {
  372. client->errorValue = stuff->property;
  373. return (BadAtom);
  374. }
  375. if ((stuff->delete != xTrue) && (stuff->delete != xFalse)) {
  376. client->errorValue = stuff->delete;
  377. return (BadValue);
  378. }
  379. if ((stuff->type != AnyPropertyType) && !ValidAtom(stuff->type)) {
  380. client->errorValue = stuff->type;
  381. return (BadAtom);
  382. }
  383. pProp = wUserProps(pWin);
  384. prevProp = (PropertyPtr) NULL;
  385. while (pProp) {
  386. if (pProp->propertyName == stuff->property)
  387. break;
  388. prevProp = pProp;
  389. pProp = pProp->next;
  390. }
  391. reply.type = X_Reply;
  392. reply.sequenceNumber = client->sequence;
  393. if (!pProp)
  394. return NullPropertyReply(client, None, 0, &reply);
  395. /* If the request type and actual type don't match. Return the
  396. property information, but not the data. */
  397. if (((stuff->type != pProp->type) && (stuff->type != AnyPropertyType))
  398. ) {
  399. reply.bytesAfter = pProp->size;
  400. reply.format = pProp->format;
  401. reply.length = 0;
  402. reply.nItems = 0;
  403. reply.propertyType = pProp->type;
  404. WriteReplyToClient(client, sizeof(xGenericReply), &reply);
  405. return (Success);
  406. }
  407. /*
  408. * Return type, format, value to client
  409. */
  410. n = (pProp->format / 8) * pProp->size; /* size (bytes) of prop */
  411. ind = stuff->longOffset << 2;
  412. /* If longOffset is invalid such that it causes "len" to
  413. be negative, it's a value error. */
  414. if (n < ind) {
  415. client->errorValue = stuff->longOffset;
  416. return BadValue;
  417. }
  418. len = min(n - ind, 4 * stuff->longLength);
  419. reply.bytesAfter = n - (ind + len);
  420. reply.format = pProp->format;
  421. reply.length = (len + 3) >> 2;
  422. reply.nItems = len / (pProp->format / 8);
  423. reply.propertyType = pProp->type;
  424. if (stuff->delete && (reply.bytesAfter == 0)) { /* send the event */
  425. xEvent event;
  426. event.u.u.type = PropertyNotify;
  427. event.u.property.window = pWin->drawable.id;
  428. event.u.property.state = PropertyDelete;
  429. event.u.property.atom = pProp->propertyName;
  430. event.u.property.time = currentTime.milliseconds;
  431. DeliverEvents(pWin, &event, 1, (WindowPtr) NULL);
  432. }
  433. WriteReplyToClient(client, sizeof(xGenericReply), &reply);
  434. if (len) {
  435. switch (reply.format) {
  436. case 32:
  437. client->pSwapReplyFunc = (ReplySwapPtr) CopySwap32Write;
  438. break;
  439. case 16:
  440. client->pSwapReplyFunc = (ReplySwapPtr) CopySwap16Write;
  441. break;
  442. default:
  443. client->pSwapReplyFunc = (ReplySwapPtr) WriteToClient;
  444. break;
  445. }
  446. WriteSwappedDataToClient(client, len, (char *) pProp->data + ind);
  447. }
  448. if (stuff->delete && (reply.bytesAfter == 0)) { /* delete the Property */
  449. if (prevProp == (PropertyPtr) NULL) { /* takes care of head */
  450. if (!(pWin->optional->userProps = pProp->next))
  451. CheckWindowOptionalNeed(pWin);
  452. }
  453. else
  454. prevProp->next = pProp->next;
  455. free(pProp->data);
  456. free(pProp);
  457. }
  458. return (client->noClientException);
  459. }
  460. int
  461. ProcListProperties(ClientPtr client)
  462. {
  463. Atom *pAtoms = NULL, *temppAtoms;
  464. xListPropertiesReply xlpr;
  465. int numProps = 0;
  466. WindowPtr pWin;
  467. PropertyPtr pProp;
  468. REQUEST(xResourceReq);
  469. REQUEST_SIZE_MATCH(xResourceReq);
  470. pWin = (WindowPtr) SecurityLookupWindow(stuff->id, client,
  471. SecurityReadAccess);
  472. if (!pWin)
  473. return (BadWindow);
  474. pProp = wUserProps(pWin);
  475. while (pProp) {
  476. pProp = pProp->next;
  477. numProps++;
  478. }
  479. if (numProps)
  480. if (!(pAtoms = (Atom *) ALLOCATE_LOCAL(numProps * sizeof(Atom))))
  481. return (BadAlloc);
  482. xlpr.type = X_Reply;
  483. xlpr.nProperties = numProps;
  484. xlpr.length = (numProps * sizeof(Atom)) >> 2;
  485. xlpr.sequenceNumber = client->sequence;
  486. pProp = wUserProps(pWin);
  487. temppAtoms = pAtoms;
  488. while (pProp) {
  489. *temppAtoms++ = pProp->propertyName;
  490. pProp = pProp->next;
  491. }
  492. WriteReplyToClient(client, sizeof(xGenericReply), &xlpr);
  493. if (numProps) {
  494. client->pSwapReplyFunc = (ReplySwapPtr) Swap32Write;
  495. WriteSwappedDataToClient(client, numProps * sizeof(Atom), pAtoms);
  496. DEALLOCATE_LOCAL(pAtoms);
  497. }
  498. return (client->noClientException);
  499. }
  500. int
  501. ProcDeleteProperty(register ClientPtr client)
  502. {
  503. WindowPtr pWin;
  504. REQUEST(xDeletePropertyReq);
  505. int result;
  506. REQUEST_SIZE_MATCH(xDeletePropertyReq);
  507. UpdateCurrentTime();
  508. pWin = (WindowPtr) SecurityLookupWindow(stuff->window, client,
  509. SecurityWriteAccess);
  510. if (!pWin)
  511. return (BadWindow);
  512. if (!ValidAtom(stuff->property)) {
  513. client->errorValue = stuff->property;
  514. return (BadAtom);
  515. }
  516. result = DeleteProperty(pWin, stuff->property);
  517. if (client->noClientException != Success)
  518. return (client->noClientException);
  519. else
  520. return (result);
  521. }