home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / dix / property.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-09  |  15.7 KB  |  572 lines

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