home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / extensions / server / xinput / xqueryst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-17  |  3.9 KB  |  180 lines

  1. /* $Header: xqueryst.c,v 1.7 91/07/17 16:39:03 rws Exp $ */
  2.  
  3. /***********************************************************************
  4.  *
  5.  * Request to query the state of an extension input device.
  6.  *
  7.  */
  8.  
  9. #define     NEED_EVENTS
  10. #define     NEED_REPLIES
  11. #include "X.h"                /* for inputstr.h    */
  12. #include "Xproto.h"            /* Request macro     */
  13. #include "inputstr.h"            /* DeviceIntPtr         */
  14. #include "windowstr.h"            /* window structure  */
  15. #include "XI.h"
  16. #include "XIproto.h"
  17.  
  18. extern    int         IReqCode;
  19. extern    int         BadDevice;
  20. extern    void        (* ReplySwapVector[256]) ();
  21. DeviceIntPtr        LookupDeviceIntRec();
  22.  
  23. /***********************************************************************
  24.  *
  25.  * This procedure allows a client to query the state of a device.
  26.  *
  27.  */
  28.  
  29. int
  30. SProcXQueryDeviceState(client)
  31.     register ClientPtr client;
  32.     {
  33.     register char n;
  34.  
  35.     REQUEST(xQueryDeviceStateReq);
  36.     swaps(&stuff->length, n);
  37.     return(ProcXQueryDeviceState(client));
  38.     }
  39.  
  40. /***********************************************************************
  41.  *
  42.  * This procedure allows frozen events to be routed.
  43.  *
  44.  */
  45.  
  46. int
  47. ProcXQueryDeviceState(client)
  48.     register ClientPtr client;
  49.     {
  50.     register char         n;
  51.     int             i;
  52.     int             num_classes = 0;
  53.     int             total_length = 0;
  54.     char            *buf, *savbuf;
  55.     KeyClassPtr         k;
  56.     xKeyState            *tk;
  57.     ButtonClassPtr         b;
  58.     xButtonState        *tb;
  59.     ValuatorClassPtr         v;
  60.     xValuatorState        *tv;
  61.     xQueryDeviceStateReply    rep;
  62.     DeviceIntPtr        dev;
  63.     int                *values;
  64.  
  65.     REQUEST(xQueryDeviceStateReq);
  66.     REQUEST_SIZE_MATCH(xQueryDeviceStateReq);
  67.  
  68.     rep.repType = X_Reply;
  69.     rep.RepType = X_QueryDeviceState;
  70.     rep.length = 0;
  71.     rep.sequenceNumber = client->sequence;
  72.  
  73.     dev = LookupDeviceIntRec (stuff->deviceid);
  74.     if (dev == NULL)
  75.     {
  76.     SendErrorToClient(client, IReqCode, X_QueryDeviceState, 0, 
  77.         BadDevice);
  78.     return Success;
  79.     }
  80.  
  81.  
  82.     k = dev->key;
  83.     if (k != NULL)
  84.     {
  85.     total_length += sizeof (xKeyState);
  86.     num_classes++;
  87.     }
  88.  
  89.     b = dev->button;
  90.     if (b != NULL)
  91.     {
  92.     total_length += sizeof (xButtonState);
  93.     num_classes++;
  94.     }
  95.  
  96.     v = dev->valuator;
  97.     if (v != NULL)
  98.     {
  99.     total_length += (sizeof(xValuatorState) + 
  100.             (v->numAxes * sizeof(int)));
  101.     num_classes++;
  102.     }
  103.     buf = (char *) Xalloc (total_length);
  104.     if (!buf)
  105.     {
  106.     SendErrorToClient(client, IReqCode, X_QueryDeviceState, 0, 
  107.         BadAlloc);
  108.     return Success;
  109.     }
  110.     savbuf = buf;
  111.  
  112.     if (k != NULL)
  113.     {
  114.     tk = (xKeyState *) buf;
  115.     tk->class = KeyClass;
  116.     tk->length = sizeof (xKeyState);
  117.     tk->num_keys = k->curKeySyms.maxKeyCode - k->curKeySyms.minKeyCode + 1;
  118.     for (i = 0; i<32; i++)
  119.         tk->keys[i] = k->down[i];
  120.     buf += sizeof (xKeyState);
  121.     }
  122.  
  123.     if (b != NULL)
  124.     {
  125.     tb = (xButtonState *) buf;
  126.     tb->class = ButtonClass;
  127.     tb->length = sizeof (xButtonState);
  128.     tb->num_buttons = b->numButtons;
  129.     for (i = 0; i<32; i++)
  130.         tb->buttons[i] = b->down[i];
  131.     buf += sizeof (xButtonState);
  132.     }
  133.  
  134.     if (v != NULL)
  135.     {
  136.     tv = (xValuatorState *) buf;
  137.     tv->class = ValuatorClass;
  138.     tv->length = sizeof (xValuatorState);
  139.     tv->num_valuators = v->numAxes;
  140.     tv->mode = v->mode;
  141.     buf += sizeof(xValuatorState);
  142.     for (i=0, values=v->axisVal; i<v->numAxes; i++)
  143.         {
  144.         *((int *) buf) = *values++;
  145.         if (client->swapped)
  146.         {
  147.         swapl ((int *) buf, n);/* macro - braces needed */
  148.         }
  149.         buf += sizeof(int);
  150.         }
  151.     }
  152.  
  153.     rep.num_classes = num_classes;
  154.     rep.length = (total_length + 3) >> 2;
  155.     WriteReplyToClient (client, sizeof(xQueryDeviceStateReply), &rep);
  156.     if (total_length > 0)
  157.     WriteToClient (client, total_length, savbuf);
  158.     Xfree (savbuf);
  159.     return Success;
  160.     }
  161.  
  162. /***********************************************************************
  163.  *
  164.  * This procedure writes the reply for the XQueryDeviceState function,
  165.  * if the client and server have a different byte ordering.
  166.  *
  167.  */
  168.  
  169. SRepXQueryDeviceState (client, size, rep)
  170.     ClientPtr    client;
  171.     int        size;
  172.     xQueryDeviceStateReply    *rep;
  173.     {
  174.     register char n;
  175.  
  176.     swaps(&rep->sequenceNumber, n);
  177.     swapl(&rep->length, n);
  178.     WriteToClient(client, size, rep);
  179.     }
  180.