1 /*
2  * Copyright (C) 2007-2020 Kim Woelders
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies of the Software, its documentation and marketing & publicity
13  * materials, and acknowledgment shall be given in the documentation, materials
14  * and software packages that this Software was used.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #include "config.h"
24 
25 #include <X11/Xlib.h>
26 
27 #include "E.h"
28 #include "ewins.h"
29 #include "shapewin.h"
30 #include "xwin.h"
31 
32 #define _SHAPE_SET_RECT(rl, _x, _y, _w, _h) \
33   do { \
34     rl[0].x = (_x);        rl[0].y = (_y);        rl[0].width = (_w); rl[0].height = 1; \
35     rl[1].x = (_x);        rl[1].y = (_y)+(_h)-1; rl[1].width = (_w); rl[1].height = 1; \
36     rl[2].x = (_x);        rl[2].y = (_y)+1;      rl[2].width = 1;    rl[2].height = (_h)-2; \
37     rl[3].x = (_x)+(_w)-1; rl[3].y = (_y)+1;      rl[3].width = 1;    rl[3].height = (_h)-2; \
38   } while(0)
39 
40 static unsigned int
_ShapeGetColor(void)41 _ShapeGetColor(void)
42 {
43    static char         color_valid = 0;
44    static unsigned int color_value = 0;
45    static unsigned int color_pixel;
46 
47    if (color_valid && color_value == Conf.movres.color)
48       goto done;
49 
50    color_value = Conf.movres.color;
51    color_pixel = EAllocColor(WinGetCmap(VROOT), color_value);
52    color_valid = 1;
53 
54  done:
55    return color_pixel;
56 }
57 
58 void
ShapewinDestroy(ShapeWin * sw)59 ShapewinDestroy(ShapeWin * sw)
60 {
61    if (!sw)
62       return;
63 
64    EoUnmap(sw);
65    EoFini(sw);
66    EXFreeGC(sw->gc);
67    if (sw->mask != NoXID)
68       EFreePixmap(sw->mask);
69    Efree(sw);
70 }
71 
72 ShapeWin           *
ShapewinCreate(int md)73 ShapewinCreate(int md)
74 {
75    ShapeWin           *sw;
76 
77    sw = ECALLOC(ShapeWin, 1);
78    if (!sw)
79       return NULL;
80 
81    EoInit(sw, EOBJ_TYPE_MISC, NoXID,
82 	  0, 0, WinGetW(VROOT), WinGetH(VROOT), 2, "Wires");
83    if (!EoGetWin(sw))
84       goto bail_out;
85 
86    EoSetFloating(sw, 1);
87    EoSetLayer(sw, 18);
88    ESetWindowBackground(EoGetWin(sw), _ShapeGetColor());
89 #ifdef ShapeInput		/* Should really check server too */
90    XShapeCombineRectangles(disp, EoGetXwin(sw),
91 			   ShapeInput, 0, 0, NULL, 0, ShapeSet, Unsorted);
92 #endif
93 
94    if ((md == MR_TECHNICAL) || (md == MR_TECH_OPAQUE))
95      {
96 	sw->mask =
97 	   ECreatePixmap(EoGetWin(sw), WinGetW(VROOT), WinGetH(VROOT), 1);
98 	sw->gc = EXCreateGC(sw->mask, 0, NULL);
99 	if (sw->mask == NoXID || !sw->gc)
100 	   goto bail_out;
101      }
102 
103    return sw;
104 
105  bail_out:
106    ShapewinDestroy(sw);
107    return NULL;
108 }
109 
110 void
ShapewinShapeSet(ShapeWin * sw,int md,int x,int y,int w,int h,int bl,int br,int bt,int bb)111 ShapewinShapeSet(ShapeWin * sw, int md, int x, int y, int w, int h,
112 		 int bl, int br, int bt, int bb)
113 {
114    int                 w2, h2;
115 
116    w2 = w + bl + br;
117    h2 = h + bt + bb;
118 
119    if ((md == MR_TECHNICAL) || (md == MR_TECH_OPAQUE))
120      {
121 	XSetForeground(disp, sw->gc, 0);
122 	XFillRectangle(disp, sw->mask, sw->gc,
123 		       0, 0, WinGetW(VROOT), WinGetH(VROOT));
124 	XSetForeground(disp, sw->gc, 1);
125 	do_draw_technical(sw->mask, sw->gc, x, y, w, h, bl, br, bt, bb);
126 
127 	EShapeSetMask(EoGetWin(sw), 0, 0, sw->mask);
128      }
129    else
130      {
131 	XRectangle          rl[8];
132 
133 	_SHAPE_SET_RECT((&rl[0]), x, y, w2, h2);
134 	w = (w > 5) ? w - 2 : 3;
135 	h = (h > 5) ? h - 2 : 3;
136 	_SHAPE_SET_RECT((&rl[4]), x + bl + 1, y + bt + 1, w, h);
137 
138 	EShapeSetRects(EoGetWin(sw), 0, 0, rl, 8);
139      }
140    EoShapeUpdate(sw, 0);
141 }
142