1 /*
2  *   xutils.c - A collection of X-windows utilties for creating WindowMAker
3  *		DockApps.
4  *
5  *     This file contains alot of the lower-level X windows routines. Origins with wmppp
6  *     (by  Martijn Pieterse (pieterse@xs4all.nl)), but its been hacked up quite a bit
7  *     and passed on from one new DockApp to the next.
8  *
9  *
10  *
11  *
12  *      This program is free software; you can redistribute it and/or modify
13  *      it under the terms of the GNU General Public License as published by
14  *      the Free Software Foundation; either version 2, or (at your option)
15  *      any later version.
16  *
17  *      This program is distributed in the hope that it will be useful,
18  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *      GNU General Public License for more details.
21  *
22  *      You should have received a copy of the GNU General Public License
23  *      along with this program (see the file COPYING); if not, write to the
24  *      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25  *      Boston, MA  02111-1307, USA
26  *
27  *
28  *
29  *
30  */
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <ctype.h>
36 #include <stdarg.h>
37 #include <X11/Xlib.h>
38 #include <X11/xpm.h>
39 #include <X11/extensions/shape.h>
40 #include "xutils.h"
41 
42 
43 /*
44  *   Global variable
45  */
46 Display		*display;
47 Window		Root;
48 Window		iconwin, win;
49 int		screen;
50 int		DisplayDepth;
51 GC		NormalGC;
52 
53 
54 /*
55  *   X11 Variables
56  */
57 int		x_fd;
58 XSizeHints	mysizehints;
59 XWMHints	mywmhints;
60 Pixel		back_pix, fore_pix;
61 char		*Geometry = "";
62 XpmIcon		wmgen;
63 Pixmap		pixmask;
64 
65 
66 
67 
68 
69 /*
70  *   flush_expose
71  */
flush_expose(Window w)72 static int flush_expose(Window w) {
73 
74     XEvent 	dummy;
75     int		i=0;
76 
77     while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
78     i++;
79 
80     return i;
81 
82 }
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 /*
93  *   RedrawWindow
94  *   RedrawWindowXY
95  */
RedrawWindow(void)96 void RedrawWindow(void) {
97 
98     flush_expose(iconwin);
99     XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, 0,0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
100 
101     flush_expose(win);
102     XCopyArea(display, wmgen.pixmap, win, NormalGC, 0,0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
103 
104 }
105 
RedrawWindowXY(int x,int y)106 void RedrawWindowXY(int x, int y) {
107 
108     flush_expose(iconwin);
109     XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, x,y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
110 
111     flush_expose(win);
112     XCopyArea(display, wmgen.pixmap, win, NormalGC, x,y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
113 
114 }
115 
116 
117 
getGC()118 GC getGC()
119 {
120 
121 return NormalGC;
122 }
123 
124 
125 
126 
127 /*
128  *   copyXPMArea
129  *   copyXBMArea
130  */
copyXPMArea(int x,int y,int sx,int sy,int dx,int dy)131 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
132 	XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
133 }
134 
copyXBMArea(int x,int y,int sx,int sy,int dx,int dy)135 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
136 
137 	XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
138 }
139 
140 
141 
142 /*
143  *   initXwindow
144  */
initXwindow(int argc,char * argv[])145 void initXwindow(int argc, char *argv[]){
146 
147     int		 i;
148     char	*display_name = NULL;
149 
150     for (i=1; argv[i]; ++i) {
151         if (!strcmp(argv[i], "-display")) display_name = argv[i+1];
152     }
153 
154 
155     if (!(display = XOpenDisplay(display_name))) {
156         fprintf(stderr, "%s: can't open display %s\n",
157                 argv[0], XDisplayName(display_name));
158         exit(1);
159     }
160 
161 
162     screen  	 = DefaultScreen(display);
163     Root    	 = RootWindow(display, screen);
164     DisplayDepth = DefaultDepth(display, screen);
165     x_fd    	 = XConnectionNumber(display);
166 
167 }
168 
169 
170 
171 
172 
173 /*
174  *   openXwindow
175  */
openXwindow(int argc,char * argv[],char * pixmap_bytes[],char * pixmask_bits,int pixmask_width,int pixmask_height,char * BackColor,char * LabelColor,char * WindGustColor,char * DataColor,char * StationTimeColor)176 Window openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits,
177     int pixmask_width, int pixmask_height, char *BackColor, char *LabelColor,
178     char *WindGustColor, char *DataColor, char *StationTimeColor) {
179 
180     unsigned int	borderwidth = 1;
181     XClassHint		classHint;
182     char		*wname = argv[0];
183     XTextProperty	name;
184     XGCValues		gcv;
185     unsigned long	gcm;
186     int			dummy=0;
187     XpmColorSymbol 	cols[5]={	{"BackColor", NULL, 0},
188 					{"LabelColor", NULL, 0},
189     					{"DataColor", NULL, 0},
190 					{"WindGustColor", NULL, 0},
191 					{"StationTimeColor", NULL, 0}   };
192 
193 
194 
195 
196     /*
197      *  Create Pixmap
198      */
199     cols[0].pixel		  = getColor(BackColor, 1.0);
200     cols[1].pixel		  = getColor(LabelColor, 1.0);
201     cols[2].pixel		  = getColor(DataColor, 1.0);
202     cols[3].pixel		  = getColor(WindGustColor, 1.0);
203     cols[4].pixel		  = getColor(StationTimeColor, 1.0);
204     wmgen.attributes.numsymbols   = 5;
205     wmgen.attributes.colorsymbols = cols;
206     wmgen.attributes.exactColors  = False;
207     wmgen.attributes.closeness    = 40000;
208     wmgen.attributes.valuemask    = XpmReturnPixels | XpmReturnExtensions | XpmColorSymbols
209 							| XpmExactColors | XpmCloseness | XpmSize;
210     if (XpmCreatePixmapFromData(display, Root, pixmap_bytes,
211       &(wmgen.pixmap), &(wmgen.mask), &(wmgen.attributes)) != XpmSuccess){
212 	fprintf(stderr, "Not enough free colorcells.\n");
213 	exit(1);
214     }
215 
216 
217 
218 
219     /*
220      *  Create a window
221      */
222     mysizehints.flags = USSize | USPosition;
223     mysizehints.x = 0;
224     mysizehints.y = 0;
225 
226     back_pix = getColor("white", 1.0);
227     fore_pix = getColor("black", 1.0);
228 
229     XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
230 				&mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
231 
232     mysizehints.width = 64;
233     mysizehints.height = 64;
234 
235 
236 
237     win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
238 				mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
239 
240     iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
241 				mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
242 
243 
244 
245     /*
246      *  Activate hints
247      */
248     XSetWMNormalHints(display, win, &mysizehints);
249     classHint.res_name = wname;
250     classHint.res_class = wname;
251     XSetClassHint(display, win, &classHint);
252 
253 
254 
255     /*
256      *  Set up the xevents that you want the relevent windows to inherit
257      *  Currently, its seems that setting KeyPress events here has no
258      *  effect. I.e. for some you will need to Grab the focus and then return
259      *  it after you are done...
260      */
261     XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask
262 		| PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
263 						| KeyPressMask | KeyReleaseMask);
264     XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask
265 		| PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
266 						| KeyPressMask | KeyReleaseMask);
267 
268 
269     if (XStringListToTextProperty(&wname, 1, &name) == 0) {
270         fprintf(stderr, "%s: can't allocate window name\n", wname);
271         exit(1);
272     }
273 
274 
275     XSetWMName(display, win, &name);
276 
277     /*
278      *   Create Graphics Context (GC) for drawing
279      */
280     gcm = GCForeground | GCBackground | GCGraphicsExposures;
281     gcv.foreground = fore_pix;
282     gcv.background = back_pix;
283     gcv.graphics_exposures = 0;
284     NormalGC = XCreateGC(display, Root, gcm, &gcv);
285 
286 
287 
288     pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
289     XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
290     XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
291 
292 
293     mywmhints.initial_state = WithdrawnState;
294     mywmhints.icon_window = iconwin;
295     mywmhints.icon_x = mysizehints.x;
296     mywmhints.icon_y = mysizehints.y;
297     mywmhints.window_group = win;
298     mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
299 
300 
301     XSetWMHints(display, win, &mywmhints);
302 
303 
304     XSetCommand(display, win, argv, argc);
305     XMapWindow(display, win);
306 
307 return win;
308 }
309 
getColor(char * ColorName,float fac)310 unsigned long getColor(char *ColorName, float fac) {
311 
312      XColor 		Color;
313      XWindowAttributes 	Attributes;
314 
315      XGetWindowAttributes(display, Root, &Attributes);
316      Color.pixel = 0;
317 
318      XParseColor(display, Attributes.colormap, ColorName, &Color);
319      Color.red 	 = (unsigned short)(Color.red/fac);
320      Color.blue  = (unsigned short)(Color.blue/fac);
321      Color.green = (unsigned short)(Color.green/fac);
322      Color.flags = DoRed | DoGreen | DoBlue;
323      XAllocColor(display, Attributes.colormap, &Color);
324 
325      return Color.pixel;
326 
327 }
328 
329