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

  1. /* $Header: xchgptr.c,v 1.14 91/05/05 17:36:57 rws Exp $ */
  2.  
  3. /************************************************************
  4. Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, California, and the 
  5. Massachusetts Institute of Technology, Cambridge, Massachusetts.
  6.  
  7.             All Rights Reserved
  8.  
  9. Permission to use, copy, modify, and distribute this software and its
  10. documentation for any purpose and without fee is hereby granted,
  11. provided that the above copyright notice appear in all copies and that
  12. both that copyright notice and this permission notice appear in
  13. supporting documentation, and that the names of Hewlett-Packard or MIT not be
  14. used in advertising or publicity pertaining to distribution of the
  15. software without specific, written prior permission.
  16.  
  17. HEWLETT-PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  18. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  19. HEWLETT-PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  20. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23. SOFTWARE.
  24.  
  25. ********************************************************/
  26.  
  27. /***********************************************************************
  28.  *
  29.  * Extension function to change the pointer device.
  30.  *
  31.  */
  32.  
  33. #define     NEED_EVENTS
  34. #define     NEED_REPLIES
  35. #include "X.h"                /* for inputstr.h    */
  36. #include "Xproto.h"            /* Request macro     */
  37. #include "XI.h"
  38. #include "XIproto.h"
  39. #include "inputstr.h"            /* DeviceIntPtr         */
  40. #include "windowstr.h"            /* window structure  */
  41. #include "scrnintstr.h"            /* screen structure  */
  42.  
  43. extern    int         IReqCode;
  44. extern    int         BadDevice;
  45. extern    int         ChangeDeviceNotify;
  46. extern    Mask        ChangeDeviceNotifyMask;
  47. extern    InputInfo    inputInfo;
  48. extern    ScreenInfo    screenInfo;
  49. extern    WindowPtr    *WindowTable;
  50. extern    void        (* ReplySwapVector[256]) ();
  51. DeviceIntPtr        LookupDeviceIntRec();
  52.  
  53. /***********************************************************************
  54.  *
  55.  * This procedure is invoked to swap the request bytes if the server and
  56.  * client have a different byte order.
  57.  *
  58.  */
  59.  
  60. int
  61. SProcXChangePointerDevice(client)
  62.     register ClientPtr client;
  63.     {
  64.     register char n;
  65.  
  66.     REQUEST(xChangePointerDeviceReq);
  67.     swaps(&stuff->length, n);
  68.     return(ProcXChangePointerDevice(client));
  69.     }
  70.  
  71. /***********************************************************************
  72.  *
  73.  * This procedure changes the device used as the X pointer.
  74.  *
  75.  */
  76.  
  77. ProcXChangePointerDevice (client)
  78.     register ClientPtr client;
  79.     {
  80.     DeviceIntPtr     xptr = inputInfo.pointer;
  81.     DeviceIntPtr     dev;
  82.     ValuatorClassPtr     v;
  83.     xChangePointerDeviceReply    rep;
  84.     changeDeviceNotify    ev;
  85.  
  86.     REQUEST(xChangePointerDeviceReq);
  87.     REQUEST_SIZE_MATCH(xChangePointerDeviceReq);
  88.  
  89.     rep.repType = X_Reply;
  90.     rep.RepType = X_ChangePointerDevice;
  91.     rep.length = 0;
  92.     rep.sequenceNumber = client->sequence;
  93.  
  94.     dev = LookupDeviceIntRec (stuff->deviceid);
  95.     if (dev == NULL)
  96.     {
  97.     rep.status = -1;
  98.     SendErrorToClient(client, IReqCode, X_ChangePointerDevice, 0, 
  99.         BadDevice);
  100.     return Success;
  101.     }
  102.  
  103.     v = dev->valuator;
  104.     if (v == NULL || v->numAxes < 2 || 
  105.     stuff->xaxis >= v->numAxes ||
  106.     stuff->yaxis >= v->numAxes)
  107.     {
  108.     rep.status = -1;
  109.     SendErrorToClient(client, IReqCode, X_ChangePointerDevice, 0, BadMatch);
  110.     return Success;
  111.     }
  112.  
  113.     if (((dev->grab) && !SameClient(dev->grab, client)) ||
  114.         ((xptr->grab) && !SameClient(xptr->grab, client)))
  115.     rep.status = AlreadyGrabbed;
  116.     else if ((dev->sync.frozen &&
  117.          dev->sync.other && !SameClient(dev->sync.other, client)) ||
  118.          (xptr->sync.frozen &&
  119.           xptr->sync.other && !SameClient(xptr->sync.other, client)))
  120.     rep.status = GrabFrozen;
  121.     else
  122.     {
  123.     if (ChangePointerDevice (
  124.         xptr, dev, stuff->xaxis, stuff->yaxis) != Success)
  125.         {
  126.         SendErrorToClient(client, IReqCode, X_ChangePointerDevice, 0, 
  127.         BadDevice);
  128.         return Success;
  129.         }
  130.     if (dev->focus)
  131.         DeleteFocusClassDeviceStruct(dev);
  132.     RegisterOtherDevice (xptr);
  133.     RegisterPointerDevice (dev);
  134.  
  135.     ev.type = ChangeDeviceNotify;
  136.     ev.deviceid = stuff->deviceid;
  137.     ev.time = currentTime.milliseconds;
  138.     ev.request = NewPointer;
  139.  
  140.     SendEventToAllWindows (dev, ChangeDeviceNotifyMask, &ev, 1);
  141.     SendMappingNotify (MappingPointer, 0, 0);
  142.  
  143.     rep.status = 0;
  144.     }
  145.  
  146.     WriteReplyToClient (client, sizeof (xChangePointerDeviceReply), 
  147.     &rep);
  148.     return Success;
  149.     }
  150.  
  151. DeleteFocusClassDeviceStruct(dev)
  152.     DeviceIntPtr dev;
  153.     {
  154.     xfree(dev->focus->trace);
  155.     xfree(dev->focus);
  156.     dev->focus = NULL;
  157.     }
  158.  
  159. /***********************************************************************
  160.  *
  161.  * Send an event to interested clients in all windows on all screens.
  162.  *
  163.  */
  164.  
  165. SendEventToAllWindows (dev, mask, ev, count)
  166.     DeviceIntPtr dev;
  167.     Mask mask;
  168.     xEvent *ev;
  169.     int count;
  170.     {
  171.     int i;
  172.     WindowPtr pWin, p1;
  173.  
  174.     for (i=0; i<screenInfo.numScreens; i++)
  175.     {
  176.     pWin = WindowTable[i];
  177.     (void)DeliverEventsToWindow(pWin, ev, count, mask, NullGrab, dev->id);
  178.     p1 = pWin->firstChild;
  179.     FindInterestedChildren (dev, p1, mask, ev, count);
  180.     }
  181.     }
  182.  
  183. /***********************************************************************
  184.  *
  185.  * Walk through the window tree, finding all clients that want to know
  186.  * about the ChangeDeviceNotify Event.
  187.  *
  188.  */
  189.  
  190. FindInterestedChildren (dev, p1, mask, ev, count)
  191.     DeviceIntPtr    dev;
  192.     WindowPtr         p1;
  193.     Mask        mask;
  194.     xEvent        *ev;
  195.     int            count;
  196.     {
  197.     WindowPtr p2;
  198.  
  199.     while (p1)
  200.         {
  201.         p2 = p1->firstChild;
  202.     (void)DeliverEventsToWindow(p1, ev, count, mask, NullGrab, dev->id);
  203.     FindInterestedChildren (dev, p2, mask, ev, count);
  204.     p1 = p1->nextSib;
  205.         }
  206.     }
  207.  
  208. /***********************************************************************
  209.  *
  210.  * This procedure writes the reply for the XChangePointerDevice 
  211.  * function, if the client and server have a different byte ordering.
  212.  *
  213.  */
  214.  
  215. SRepXChangePointerDevice (client, size, rep)
  216.     ClientPtr    client;
  217.     int        size;
  218.     xChangePointerDeviceReply    *rep;
  219.     {
  220.     register char n;
  221.  
  222.     swaps(&rep->sequenceNumber, n);
  223.     swapl(&rep->length, n);
  224.     WriteToClient(client, size, rep);
  225.     }
  226.