Region.c revision 3e6c936a
1/*
2 * Copyright © 2003 Keith Packard
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of Keith Packard not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission.  Keith Packard makes no
11 * representations about the suitability of this software for any purpose.  It
12 * is provided "as is" without express or implied warranty.
13 *
14 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26#include "Xfixesint.h"
27
28XserverRegion
29XFixesCreateRegion (Display *dpy, XRectangle *rectangles, int nrectangles)
30{
31    XFixesExtDisplayInfo	*info = XFixesFindDisplay (dpy);
32    xXFixesCreateRegionReq	*req;
33    long    			len;
34    XserverRegion		region;
35
36    XFixesCheckExtension (dpy, info, 0);
37    LockDisplay (dpy);
38    GetReq (XFixesCreateRegion, req);
39    req->reqType = info->codes->major_opcode;
40    req->xfixesReqType = X_XFixesCreateRegion;
41    region = req->region = XAllocID (dpy);
42    len = ((long) nrectangles) << 1;
43    SetReqLen (req, len, len);
44    len <<= 2;
45    Data16 (dpy, (short *) rectangles, len);
46    UnlockDisplay (dpy);
47    SyncHandle();
48    return region;
49}
50
51XserverRegion
52XFixesCreateRegionFromBitmap (Display *dpy, Pixmap bitmap)
53{
54    XFixesExtDisplayInfo		*info = XFixesFindDisplay (dpy);
55    xXFixesCreateRegionFromBitmapReq	*req;
56    XserverRegion			region;
57
58    XFixesCheckExtension (dpy, info, 0);
59    LockDisplay (dpy);
60    GetReq (XFixesCreateRegionFromBitmap, req);
61    req->reqType = info->codes->major_opcode;
62    req->xfixesReqType = X_XFixesCreateRegionFromBitmap;
63    region = req->region = XAllocID (dpy);
64    req->bitmap = bitmap;
65    UnlockDisplay (dpy);
66    SyncHandle();
67    return region;
68}
69
70XserverRegion
71XFixesCreateRegionFromWindow (Display *dpy, Window window, int kind)
72{
73    XFixesExtDisplayInfo		*info = XFixesFindDisplay (dpy);
74    xXFixesCreateRegionFromWindowReq	*req;
75    XserverRegion			region;
76
77    XFixesCheckExtension (dpy, info, 0);
78    LockDisplay (dpy);
79    GetReq (XFixesCreateRegionFromWindow, req);
80    req->reqType = info->codes->major_opcode;
81    req->xfixesReqType = X_XFixesCreateRegionFromWindow;
82    region = req->region = XAllocID (dpy);
83    req->window = window;
84    req->kind = kind;
85    UnlockDisplay (dpy);
86    SyncHandle();
87    return region;
88}
89
90XserverRegion
91XFixesCreateRegionFromGC (Display *dpy, GC gc)
92{
93    XFixesExtDisplayInfo		*info = XFixesFindDisplay (dpy);
94    xXFixesCreateRegionFromGCReq	*req;
95    XserverRegion			region;
96
97    XFixesCheckExtension (dpy, info, 0);
98    LockDisplay (dpy);
99    GetReq (XFixesCreateRegionFromGC, req);
100    req->reqType = info->codes->major_opcode;
101    req->xfixesReqType = X_XFixesCreateRegionFromGC;
102    region = req->region = XAllocID (dpy);
103    req->gc = gc->gid;
104    UnlockDisplay (dpy);
105    SyncHandle();
106    return region;
107}
108
109XserverRegion
110XFixesCreateRegionFromPicture (Display *dpy, XID picture)
111{
112    XFixesExtDisplayInfo		*info = XFixesFindDisplay (dpy);
113    xXFixesCreateRegionFromPictureReq	*req;
114    XserverRegion			region;
115
116    XFixesCheckExtension (dpy, info, 0);
117    LockDisplay (dpy);
118    GetReq (XFixesCreateRegionFromPicture, req);
119    req->reqType = info->codes->major_opcode;
120    req->xfixesReqType = X_XFixesCreateRegionFromPicture;
121    region = req->region = XAllocID (dpy);
122    req->picture = picture;
123    UnlockDisplay (dpy);
124    SyncHandle();
125    return region;
126}
127
128void
129XFixesDestroyRegion (Display *dpy, XserverRegion region)
130{
131    XFixesExtDisplayInfo	*info = XFixesFindDisplay (dpy);
132    xXFixesDestroyRegionReq	*req;
133
134    XFixesSimpleCheckExtension (dpy, info);
135    LockDisplay (dpy);
136    GetReq (XFixesDestroyRegion, req);
137    req->reqType = info->codes->major_opcode;
138    req->xfixesReqType = X_XFixesDestroyRegion;
139    req->region = region;
140    UnlockDisplay (dpy);
141    SyncHandle();
142}
143
144void
145XFixesSetRegion (Display *dpy, XserverRegion region,
146		 XRectangle *rectangles, int nrectangles)
147{
148    XFixesExtDisplayInfo	*info = XFixesFindDisplay (dpy);
149    xXFixesSetRegionReq		*req;
150    long    			len;
151
152    XFixesSimpleCheckExtension (dpy, info);
153    LockDisplay (dpy);
154    GetReq (XFixesSetRegion, req);
155    req->reqType = info->codes->major_opcode;
156    req->xfixesReqType = X_XFixesSetRegion;
157    req->region = region;
158    len = ((long) nrectangles) << 1;
159    SetReqLen (req, len, len);
160    len <<= 2;
161    Data16 (dpy, (short *) rectangles, len);
162    UnlockDisplay (dpy);
163    SyncHandle();
164}
165
166void
167XFixesCopyRegion (Display *dpy, XserverRegion dst, XserverRegion src)
168{
169    XFixesExtDisplayInfo	*info = XFixesFindDisplay (dpy);
170    xXFixesCopyRegionReq	*req;
171
172    XFixesSimpleCheckExtension (dpy, info);
173    LockDisplay (dpy);
174    GetReq (XFixesCopyRegion, req);
175    req->reqType = info->codes->major_opcode;
176    req->xfixesReqType = X_XFixesCopyRegion;
177    req->source = src;
178    req->destination = dst;
179    UnlockDisplay (dpy);
180    SyncHandle();
181}
182
183void
184XFixesUnionRegion (Display *dpy, XserverRegion dst,
185		   XserverRegion src1, XserverRegion src2)
186{
187    XFixesExtDisplayInfo	*info = XFixesFindDisplay (dpy);
188    xXFixesUnionRegionReq	*req;
189
190    XFixesSimpleCheckExtension (dpy, info);
191    LockDisplay (dpy);
192    GetReq (XFixesUnionRegion, req);
193    req->reqType = info->codes->major_opcode;
194    req->xfixesReqType = X_XFixesUnionRegion;
195    req->source1 = src1;
196    req->source2 = src2;
197    req->destination = dst;
198    UnlockDisplay (dpy);
199    SyncHandle();
200}
201
202void
203XFixesIntersectRegion (Display *dpy, XserverRegion dst,
204		       XserverRegion src1, XserverRegion src2)
205{
206    XFixesExtDisplayInfo	*info = XFixesFindDisplay (dpy);
207    xXFixesIntersectRegionReq	*req;
208
209    XFixesSimpleCheckExtension (dpy, info);
210    LockDisplay (dpy);
211    GetReq (XFixesIntersectRegion, req);
212    req->reqType = info->codes->major_opcode;
213    req->xfixesReqType = X_XFixesIntersectRegion;
214    req->source1 = src1;
215    req->source2 = src2;
216    req->destination = dst;
217    UnlockDisplay (dpy);
218    SyncHandle();
219}
220
221void
222XFixesSubtractRegion (Display *dpy, XserverRegion dst,
223		      XserverRegion src1, XserverRegion src2)
224{
225    XFixesExtDisplayInfo	*info = XFixesFindDisplay (dpy);
226    xXFixesSubtractRegionReq	*req;
227
228    XFixesSimpleCheckExtension (dpy, info);
229    LockDisplay (dpy);
230    GetReq (XFixesSubtractRegion, req);
231    req->reqType = info->codes->major_opcode;
232    req->xfixesReqType = X_XFixesSubtractRegion;
233    req->source1 = src1;
234    req->source2 = src2;
235    req->destination = dst;
236    UnlockDisplay (dpy);
237    SyncHandle();
238}
239
240void
241XFixesInvertRegion (Display *dpy, XserverRegion dst,
242		    XRectangle *rect, XserverRegion src)
243{
244    XFixesExtDisplayInfo	*info = XFixesFindDisplay (dpy);
245    xXFixesInvertRegionReq	*req;
246
247    XFixesSimpleCheckExtension (dpy, info);
248    LockDisplay (dpy);
249    GetReq (XFixesInvertRegion, req);
250    req->reqType = info->codes->major_opcode;
251    req->xfixesReqType = X_XFixesInvertRegion;
252    req->x = rect->x;
253    req->y = rect->y;
254    req->width = rect->width;
255    req->height = rect->height;
256    req->source = src;
257    req->destination = dst;
258    UnlockDisplay (dpy);
259    SyncHandle();
260}
261
262void
263XFixesTranslateRegion (Display *dpy, XserverRegion region, int dx, int dy)
264{
265    XFixesExtDisplayInfo	*info = XFixesFindDisplay (dpy);
266    xXFixesTranslateRegionReq	*req;
267
268    XFixesSimpleCheckExtension (dpy, info);
269    LockDisplay (dpy);
270    GetReq (XFixesTranslateRegion, req);
271    req->reqType = info->codes->major_opcode;
272    req->xfixesReqType = X_XFixesTranslateRegion;
273    req->region = region;
274    req->dx = dx;
275    req->dy = dy;
276    UnlockDisplay (dpy);
277    SyncHandle();
278}
279
280void
281XFixesRegionExtents (Display *dpy, XserverRegion dst, XserverRegion src)
282{
283    XFixesExtDisplayInfo	*info = XFixesFindDisplay (dpy);
284    xXFixesRegionExtentsReq	*req;
285
286    XFixesSimpleCheckExtension (dpy, info);
287    LockDisplay (dpy);
288    GetReq (XFixesRegionExtents, req);
289    req->reqType = info->codes->major_opcode;
290    req->xfixesReqType = X_XFixesRegionExtents;
291    req->source = src;
292    req->destination = dst;
293    UnlockDisplay (dpy);
294    SyncHandle();
295}
296
297XRectangle *
298XFixesFetchRegion (Display *dpy, XserverRegion region, int *nrectanglesRet)
299{
300    XRectangle	bounds;
301
302    return XFixesFetchRegionAndBounds (dpy, region, nrectanglesRet, &bounds);
303}
304
305XRectangle *
306XFixesFetchRegionAndBounds (Display	    *dpy,
307			    XserverRegion   region,
308			    int		    *nrectanglesRet,
309			    XRectangle	    *bounds)
310{
311    XFixesExtDisplayInfo	*info = XFixesFindDisplay (dpy);
312    xXFixesFetchRegionReq	*req;
313    xXFixesFetchRegionReply	rep;
314    XRectangle			*rects;
315    int    			nrects;
316    long    			nbytes;
317    long			nread;
318
319    XFixesCheckExtension (dpy, info, NULL);
320    LockDisplay (dpy);
321    GetReq (XFixesFetchRegion, req);
322    req->reqType = info->codes->major_opcode;
323    req->xfixesReqType = X_XFixesFetchRegion;
324    req->region = region;
325    *nrectanglesRet = 0;
326    if (!_XReply (dpy, (xReply *) &rep, 0, xFalse))
327    {
328	UnlockDisplay (dpy);
329	SyncHandle ();
330	return NULL;
331    }
332    bounds->x = rep.x;
333    bounds->y = rep.y;
334    bounds->width = rep.width;
335    bounds->height = rep.height;
336    nbytes = (long) rep.length << 2;
337    nrects = rep.length >> 1;
338    rects = Xmalloc (nrects * sizeof (XRectangle));
339    if (!rects)
340    {
341	_XEatDataWords(dpy, rep.length);
342	UnlockDisplay (dpy);
343	SyncHandle ();
344	return NULL;
345    }
346    nread = nrects << 3;
347    _XRead16 (dpy, (short *) rects, nread);
348    /* skip any padding */
349    if(nbytes > nread)
350    {
351	_XEatData (dpy, (unsigned long) (nbytes - nread));
352    }
353    UnlockDisplay (dpy);
354    SyncHandle();
355    *nrectanglesRet = nrects;
356    return rects;
357}
358
359void
360XFixesSetGCClipRegion (Display *dpy, GC gc,
361		       int clip_x_origin, int clip_y_origin,
362		       XserverRegion region)
363{
364    XFixesExtDisplayInfo	*info = XFixesFindDisplay (dpy);
365    xXFixesSetGCClipRegionReq	    *req;
366
367    XFixesSimpleCheckExtension (dpy, info);
368    LockDisplay (dpy);
369    GetReq (XFixesSetGCClipRegion, req);
370    req->reqType = info->codes->major_opcode;
371    req->xfixesReqType = X_XFixesSetGCClipRegion;
372    req->gc = gc->gid;
373    req->region = region;
374    req->xOrigin = clip_x_origin;
375    req->yOrigin = clip_y_origin;
376    UnlockDisplay (dpy);
377    SyncHandle();
378}
379
380void
381XFixesSetWindowShapeRegion (Display *dpy, Window win, int shape_kind,
382			    int x_off, int y_off, XserverRegion region)
383{
384    XFixesExtDisplayInfo	    *info = XFixesFindDisplay (dpy);
385    xXFixesSetWindowShapeRegionReq  *req;
386
387    XFixesSimpleCheckExtension (dpy, info);
388    LockDisplay (dpy);
389    GetReq (XFixesSetWindowShapeRegion, req);
390    req->reqType = info->codes->major_opcode;
391    req->xfixesReqType = X_XFixesSetWindowShapeRegion;
392    req->dest = win;
393    req->destKind = shape_kind;
394    req->xOff = x_off;
395    req->yOff = y_off;
396    req->region = region;
397    UnlockDisplay (dpy);
398    SyncHandle();
399}
400
401void
402XFixesSetPictureClipRegion (Display *dpy, XID picture,
403			    int clip_x_origin, int clip_y_origin,
404			    XserverRegion region)
405{
406    XFixesExtDisplayInfo	    *info = XFixesFindDisplay (dpy);
407    xXFixesSetPictureClipRegionReq  *req;
408
409    XFixesSimpleCheckExtension (dpy, info);
410    LockDisplay (dpy);
411    GetReq (XFixesSetPictureClipRegion, req);
412    req->reqType = info->codes->major_opcode;
413    req->xfixesReqType = X_XFixesSetPictureClipRegion;
414    req->picture = picture;
415    req->region = region;
416    req->xOrigin = clip_x_origin;
417    req->yOrigin = clip_y_origin;
418    UnlockDisplay (dpy);
419    SyncHandle();
420}
421
422void
423XFixesExpandRegion (Display *dpy, XserverRegion dst, XserverRegion src,
424		    unsigned left, unsigned right,
425		    unsigned top, unsigned bottom)
426{
427    XFixesExtDisplayInfo	*info = XFixesFindDisplay (dpy);
428    xXFixesExpandRegionReq	*req;
429
430    XFixesSimpleCheckExtension (dpy, info);
431    LockDisplay (dpy);
432    GetReq (XFixesExpandRegion, req);
433    req->reqType = info->codes->major_opcode;
434    req->xfixesReqType = X_XFixesExpandRegion;
435    req->source = src;
436    req->destination = dst;
437    req->left = left;
438    req->right = right;
439    req->top = top;
440    req->bottom = bottom;
441    UnlockDisplay (dpy);
442    SyncHandle();
443}
444
445