home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / extensions / lib / XShape.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-11  |  13.1 KB  |  469 lines

  1. /*
  2.  * $XConsortium: XShape.c,v 1.22 91/01/12 11:19:44 rws Exp $
  3.  *
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Keith Packard, MIT X Consortium
  24.  */
  25. #define NEED_EVENTS
  26. #define NEED_REPLIES
  27. #include <X11/Xlibint.h>
  28. #include <X11/Xutil.h>
  29. #include "region.h"            /* in Xlib sources */
  30. #include "Xext.h"            /* in ../include */
  31. #include "extutil.h"            /* in ../include */
  32. #include "shapestr.h"            /* in ../include */
  33.  
  34. static XExtensionInfo _shape_info_data;
  35. static XExtensionInfo *shape_info = &_shape_info_data;
  36. static /* const */ char *shape_extension_name = SHAPENAME;
  37.  
  38. #define ShapeCheckExtension(dpy,i,val) \
  39.   XextCheckExtension (dpy, i, shape_extension_name, val)
  40. #define ShapeSimpleCheckExtension(dpy,i) \
  41.   XextSimpleCheckExtension (dpy, i, shape_extension_name)
  42.  
  43.  
  44. /*****************************************************************************
  45.  *                                                                           *
  46.  *               private utility routines                          *
  47.  *                                                                           *
  48.  *****************************************************************************/
  49.  
  50. static int close_display();
  51. static Bool wire_to_event();
  52. static Status event_to_wire();
  53. static /* const */ XExtensionHooks shape_extension_hooks = {
  54.     NULL,                /* create_gc */
  55.     NULL,                /* copy_gc */
  56.     NULL,                /* flush_gc */
  57.     NULL,                /* free_gc */
  58.     NULL,                /* create_font */
  59.     NULL,                /* free_font */
  60.     close_display,            /* close_display */
  61.     wire_to_event,            /* wire_to_event */
  62.     event_to_wire,            /* event_to_wire */
  63.     NULL,                /* error */
  64.     NULL,                /* error_string */
  65. };
  66.  
  67. static XEXT_GENERATE_FIND_DISPLAY (find_display, shape_info,
  68.                    shape_extension_name, 
  69.                    &shape_extension_hooks,
  70.                    ShapeNumberEvents, NULL)
  71.  
  72. static XEXT_GENERATE_CLOSE_DISPLAY (close_display, shape_info)
  73.  
  74.  
  75. static Bool wire_to_event (dpy, re, event)
  76.     Display *dpy;
  77.     XEvent  *re;
  78.     xEvent  *event;
  79. {
  80.     XExtDisplayInfo *info = find_display (dpy);
  81.     XShapeEvent        *se;
  82.     xShapeNotifyEvent    *sevent;
  83.  
  84.     ShapeCheckExtension (dpy, info, False);
  85.  
  86.     switch ((event->u.u.type & 0x7f) - info->codes->first_event) {
  87.     case ShapeNotify:
  88.         se = (XShapeEvent *) re;
  89.     sevent = (xShapeNotifyEvent *) event;
  90.         se->type = sevent->type & 0x7f;
  91.         se->serial = _XSetLastRequestRead(dpy,(xGenericReply *) event);
  92.         se->send_event = (sevent->type & 0x80) != 0;
  93.         se->display = dpy;
  94.         se->window = sevent->window;
  95.         se->kind = sevent->kind;
  96.         se->x = cvtINT16toInt (sevent->x);
  97.         se->y = cvtINT16toInt (sevent->y);
  98.         se->width = sevent->width;
  99.         se->height = sevent->height;
  100.     se->time = sevent->time;
  101.     se->shaped = True;
  102.     if (sevent->shaped == xFalse)
  103.         se->shaped = False;
  104.         return True;
  105.     }
  106.     return False;
  107. }
  108.  
  109. static Status event_to_wire (dpy, re, event)
  110.     Display *dpy;
  111.     XEvent  *re;
  112.     xEvent  *event;
  113. {
  114.     XExtDisplayInfo *info = find_display (dpy);
  115.     XShapeEvent        *se;
  116.     xShapeNotifyEvent    *sevent;
  117.  
  118.     ShapeCheckExtension (dpy, info, 0);
  119.  
  120.     switch ((re->type & 0x7f) - info->codes->first_event) {
  121.     case ShapeNotify:
  122.         se = (XShapeEvent *) re;
  123.     sevent = (xShapeNotifyEvent *) event;
  124.         sevent->type = se->type | (se->send_event ? 0x80 : 0);
  125.         sevent->sequenceNumber = se->serial & 0xffff;
  126.         sevent->window = se->window;
  127.         sevent->kind = se->kind;
  128.         sevent->x = se->x;
  129.         sevent->y = se->y;
  130.         sevent->width = se->width;
  131.         sevent->height = se->height;
  132.     sevent->time = se->time;
  133.         return 1;
  134.     }
  135.     return 0;
  136. }
  137.  
  138.  
  139. /****************************************************************************
  140.  *                                                                          *
  141.  *                Shape public interfaces                         *
  142.  *                                                                          *
  143.  ****************************************************************************/
  144.  
  145. Bool XShapeQueryExtension (dpy, event_basep, error_basep)
  146.     Display *dpy;
  147.     int *event_basep, *error_basep;
  148. {
  149.     XExtDisplayInfo *info = find_display (dpy);
  150.  
  151.     if (XextHasExtension(info)) {
  152.     *event_basep = info->codes->first_event;
  153.     *error_basep = info->codes->first_error;
  154.     return True;
  155.     } else {
  156.     return False;
  157.     }
  158. }
  159.  
  160.  
  161. Status XShapeQueryVersion(dpy, major_versionp, minor_versionp)
  162.     Display *dpy;
  163.     int        *major_versionp, *minor_versionp;
  164. {
  165.     XExtDisplayInfo *info = find_display (dpy);
  166.     xShapeQueryVersionReply        rep;
  167.     register xShapeQueryVersionReq  *req;
  168.  
  169.     ShapeCheckExtension (dpy, info, 0);
  170.  
  171.     LockDisplay (dpy);
  172.     GetReq (ShapeQueryVersion, req);
  173.     req->reqType = info->codes->major_opcode;
  174.     req->shapeReqType = X_ShapeQueryVersion;
  175.     if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) {
  176.     UnlockDisplay (dpy);
  177.     SyncHandle ();
  178.     return 0;
  179.     }
  180.     *major_versionp = rep.majorVersion;
  181.     *minor_versionp = rep.minorVersion;
  182.     UnlockDisplay (dpy);
  183.     SyncHandle ();
  184.     return 1;
  185. }
  186.  
  187. void XShapeCombineRegion(dpy, dest, destKind, xOff, yOff, r, op)
  188. register Display    *dpy;
  189. Window            dest;
  190. int            destKind, op, xOff, yOff;
  191. register REGION        *r;
  192. {
  193.     register int i;
  194.     register XRectangle *xr, *pr;
  195.     register BOX *pb;
  196.  
  197.     xr = (XRectangle *) 
  198.         _XAllocScratch(dpy, (unsigned long)(r->numRects * sizeof (XRectangle)));
  199.     for (pr = xr, pb = r->rects, i = r->numRects; --i >= 0; pr++, pb++) {
  200.         pr->x = pb->x1;
  201.     pr->y = pb->y1;
  202.     pr->width = pb->x2 - pb->x1;
  203.     pr->height = pb->y2 - pb->y1;
  204.      }
  205.      XShapeCombineRectangles (dpy, dest, destKind, xOff, yOff,
  206.                   xr, r->numRects, op, YXBanded);
  207. }
  208.  
  209.  
  210. void XShapeCombineRectangles (dpy, dest, destKind, xOff, yOff,
  211.                   rects, n_rects, op, ordering)
  212. register Display *dpy;
  213. XID dest;
  214. int op, xOff, yOff, ordering;
  215. XRectangle  *rects;
  216. int n_rects;
  217. {
  218.     XExtDisplayInfo *info = find_display (dpy);
  219.     register xShapeRectanglesReq *req;
  220.     register long nbytes;
  221.  
  222.     ShapeSimpleCheckExtension (dpy, info);
  223.  
  224.     LockDisplay(dpy);
  225.     GetReq(ShapeRectangles, req);
  226.     req->reqType = info->codes->major_opcode;
  227.     req->shapeReqType = X_ShapeRectangles;
  228.     req->op = op;
  229.     req->ordering = ordering;
  230.     req->destKind = destKind;
  231.     req->dest = dest;
  232.     req->xOff = xOff;
  233.     req->yOff = yOff;
  234.  
  235.     /* SIZEOF(xRectangle) will be a multiple of 4 */
  236.     req->length += n_rects * (SIZEOF(xRectangle) / 4);
  237.  
  238.     nbytes = n_rects * sizeof(xRectangle);
  239.  
  240.     Data16 (dpy, (short *) rects, nbytes);
  241.     UnlockDisplay(dpy);
  242.     SyncHandle();
  243. }
  244.  
  245.  
  246. void XShapeCombineMask (dpy, dest, destKind, xOff, yOff, src, op)
  247. register Display *dpy;
  248. int destKind;
  249. XID dest;
  250. Pixmap    src;
  251. int op, xOff, yOff;
  252. {
  253.     XExtDisplayInfo *info = find_display (dpy);
  254.     register xShapeMaskReq *req;
  255.  
  256.     ShapeSimpleCheckExtension (dpy, info);
  257.  
  258.     LockDisplay(dpy);
  259.     GetReq(ShapeMask, req);
  260.     req->reqType = info->codes->major_opcode;
  261.     req->shapeReqType = X_ShapeMask;
  262.     req->op = op;
  263.     req->destKind = destKind;
  264.     req->dest = dest;
  265.     req->xOff = xOff;
  266.     req->yOff = yOff;
  267.     req->src = src;
  268.     UnlockDisplay(dpy);
  269.     SyncHandle();
  270. }
  271.  
  272. void XShapeCombineShape (dpy, dest, destKind, xOff, yOff, src, srcKind, op)
  273. register Display *dpy;
  274. int destKind;
  275. XID dest;
  276. int srcKind;
  277. XID src;
  278. int op, xOff, yOff;
  279. {
  280.     XExtDisplayInfo *info = find_display (dpy);
  281.     register xShapeCombineReq *req;
  282.  
  283.     ShapeSimpleCheckExtension (dpy, info);
  284.  
  285.     LockDisplay(dpy);
  286.     GetReq(ShapeCombine, req);
  287.     req->reqType = info->codes->major_opcode;
  288.     req->shapeReqType = X_ShapeCombine;
  289.     req->op = op;
  290.     req->destKind = destKind;
  291.     req->srcKind = srcKind;
  292.     req->dest = dest;
  293.     req->xOff = xOff;
  294.     req->yOff = yOff;
  295.     req->src = src;
  296.     UnlockDisplay(dpy);
  297.     SyncHandle();
  298. }
  299.  
  300. void XShapeOffsetShape (dpy, dest, destKind, xOff, yOff)
  301. register Display *dpy;
  302. int destKind;
  303. XID dest;
  304. int xOff, yOff;
  305. {
  306.     XExtDisplayInfo *info = find_display (dpy);
  307.     register xShapeOffsetReq *req;
  308.  
  309.     ShapeSimpleCheckExtension (dpy, info);
  310.  
  311.     LockDisplay(dpy);
  312.     GetReq(ShapeOffset, req);
  313.     req->reqType = info->codes->major_opcode;
  314.     req->shapeReqType = X_ShapeOffset;
  315.     req->destKind = destKind;
  316.     req->dest = dest;
  317.     req->xOff = xOff;
  318.     req->yOff = yOff;
  319.     UnlockDisplay(dpy);
  320.     SyncHandle();
  321. }
  322.  
  323. Status XShapeQueryExtents (dpy, window,
  324.                bShaped, xbs, ybs, wbs, hbs,
  325.                cShaped, xcs, ycs, wcs, hcs)    
  326.     register Display    *dpy;
  327.     Window            window;
  328.     int                *bShaped, *cShaped;        /* RETURN */
  329.     int                *xbs, *ybs, *xcs, *ycs; /* RETURN */
  330.     unsigned int        *wbs, *hbs, *wcs, *hcs; /* RETURN */
  331. {
  332.     XExtDisplayInfo *info = find_display (dpy);
  333.     xShapeQueryExtentsReply        rep;
  334.     register xShapeQueryExtentsReq *req;
  335.     
  336.     ShapeCheckExtension (dpy, info, 0);
  337.  
  338.     LockDisplay (dpy);
  339.     GetReq (ShapeQueryExtents, req);
  340.     req->reqType = info->codes->major_opcode;
  341.     req->shapeReqType = X_ShapeQueryExtents;
  342.     req->window = window;
  343.     if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) {
  344.     UnlockDisplay (dpy);
  345.     SyncHandle ();
  346.     return 0;
  347.     }
  348.     *bShaped = rep.boundingShaped;
  349.     *cShaped = rep.clipShaped;
  350.     *xbs = cvtINT16toInt (rep.xBoundingShape);
  351.     *ybs = cvtINT16toInt (rep.yBoundingShape);
  352.     *wbs = rep.widthBoundingShape;
  353.     *hbs = rep.heightBoundingShape;
  354.     *xcs = cvtINT16toInt (rep.xClipShape);
  355.     *ycs = cvtINT16toInt (rep.yClipShape);
  356.     *wcs = rep.widthClipShape;
  357.     *hcs = rep.heightClipShape;
  358.     UnlockDisplay (dpy);
  359.     SyncHandle ();
  360.     return 1;
  361. }
  362.  
  363.  
  364. void XShapeSelectInput (dpy, window, mask)
  365.     register Display    *dpy;
  366.     Window        window;
  367.     unsigned long    mask;
  368. {
  369.     XExtDisplayInfo *info = find_display (dpy);
  370.     register xShapeSelectInputReq   *req;
  371.  
  372.     ShapeSimpleCheckExtension (dpy, info);
  373.  
  374.     LockDisplay (dpy);
  375.     GetReq (ShapeSelectInput, req);
  376.     req->reqType = info->codes->major_opcode;
  377.     req->shapeReqType = X_ShapeSelectInput;
  378.     req->window = window;
  379.     if (mask & ShapeNotifyMask)
  380.     req->enable = xTrue;
  381.     else
  382.     req->enable = xFalse;
  383.     UnlockDisplay (dpy);
  384.     SyncHandle ();
  385. }
  386.  
  387. unsigned long XShapeInputSelected (dpy, window)
  388.     register Display    *dpy;
  389.     Window        window;
  390. {
  391.     XExtDisplayInfo *info = find_display (dpy);
  392.     register xShapeInputSelectedReq *req;
  393.     xShapeInputSelectedReply        rep;
  394.  
  395.     ShapeCheckExtension (dpy, info, False);
  396.  
  397.     LockDisplay (dpy);
  398.     GetReq (ShapeInputSelected, req);
  399.     req->reqType = info->codes->major_opcode;
  400.     req->shapeReqType = X_ShapeInputSelected;
  401.     req->window = window;
  402.     if (!_XReply (dpy, (xReply *) &rep, 0, xFalse)) {
  403.     UnlockDisplay (dpy);
  404.     SyncHandle ();
  405.     return False;
  406.     }
  407.     UnlockDisplay (dpy);
  408.     SyncHandle ();
  409.     return rep.enabled ? ShapeNotifyMask : 0L;
  410. }
  411.  
  412.  
  413. XRectangle *XShapeGetRectangles (dpy, window, kind, count, ordering)
  414.     register Display    *dpy;
  415.     Window        window;
  416.     int            kind;
  417.     int            *count;    /* RETURN */
  418.     int            *ordering; /* RETURN */
  419. {
  420.     XExtDisplayInfo *info = find_display (dpy);
  421.     register xShapeGetRectanglesReq   *req;
  422.     xShapeGetRectanglesReply        rep;
  423.     XRectangle                *rects;
  424.     xRectangle                *xrects;
  425.     int                    i;
  426.  
  427.     ShapeCheckExtension (dpy, info, (XRectangle *)NULL);
  428.  
  429.     LockDisplay (dpy);
  430.     GetReq (ShapeGetRectangles, req);
  431.     req->reqType = info->codes->major_opcode;
  432.     req->shapeReqType = X_ShapeGetRectangles;
  433.     req->window = window;
  434.     req->kind = kind;
  435.     if (!_XReply (dpy, (xReply *) &rep, 0, xFalse)) {
  436.     UnlockDisplay (dpy);
  437.     SyncHandle ();
  438.     return (XRectangle *)NULL;
  439.     }
  440.     *count = rep.nrects;
  441.     *ordering = rep.ordering;
  442.     rects = 0;
  443.     if (*count) {
  444.     xrects = (xRectangle *) Xmalloc (*count * sizeof (xRectangle));
  445.     rects = (XRectangle *) Xmalloc (*count * sizeof (XRectangle));
  446.     if (!xrects || !rects) {
  447.         if (xrects)
  448.         Xfree (xrects);
  449.         if (rects)
  450.         Xfree (rects);
  451.         _XEatData (dpy, *count * sizeof (xRectangle));
  452.         rects = 0;
  453.         *count = 0;
  454.     } else {
  455.         _XRead (dpy, (char *) xrects, *count * sizeof (xRectangle));
  456.         for (i = 0; i < *count; i++) {
  457.             rects[i].x = (short) cvtINT16toInt (xrects[i].x);
  458.             rects[i].y = (short) cvtINT16toInt (xrects[i].y);
  459.             rects[i].width = xrects[i].width;
  460.             rects[i].height = xrects[i].height;
  461.         }
  462.         Xfree (xrects);
  463.     }
  464.     }
  465.     UnlockDisplay (dpy);
  466.     SyncHandle ();
  467.     return rects;
  468. }
  469.