1 /*
2 	Best viewed with vim5, using ts=4
3 
4 	wmgeneral was taken from wmppp.
5 
6 	It has a lot of routines which most of the wm* programs use.
7 
8 	------------------------------------------------------------
9 
10 	Author: Martijn Pieterse (pieterse@xs4all.nl)
11 
12 	---
13 	CHANGES:
14     ---
15     14/09/1998 (Dave Clark, clarkd@skyia.com)
16         * Updated createXBMfromXPM routine
17         * Now supports >256 colors
18 	11/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
19 		* Removed a bug from parse_rcfile. You could
20 		  not use "start" in a command if a label was
21 		  also start.
22 		* Changed the needed geometry string.
23 		  We don't use window size, and don't support
24 		  negative positions.
25 	03/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
26 		* Added parse_rcfile2
27 	02/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
28 		* Added -geometry support (untested)
29 	28/08/1998 (Martijn Pieterse, pieterse@xs4all.nl)
30 		* Added createXBMfromXPM routine
31 		* Saves a lot of work with changing xpm's.
32 	02/05/1998 (Martijn Pieterse, pieterse@xs4all.nl)
33 		* changed the read_rc_file to parse_rcfile, as suggested by Marcelo E. Magallon
34 		* debugged the parse_rc file.
35 	30/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
36 		* Ripped similar code from all the wm* programs,
37 		  and put them in a single file.
38 
39 */
40 
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <ctype.h>
46 #include <stdarg.h>
47 
48 #include <X11/Xlib.h>
49 #include <X11/xpm.h>
50 #include <X11/extensions/shape.h>
51 
52 #include "wmgeneral.h"
53 
54   /*****************/
55  /* X11 Variables */
56 /*****************/
57 
58 Window		Root;
59 int			screen;
60 int			x_fd;
61 int			d_depth;
62 XSizeHints	mysizehints;
63 XWMHints	mywmhints;
64 Pixel		back_pix, fore_pix;
65 char		*Geometry = "";
66 Window		iconwin, win;
67 GC			NormalGC;
68 XpmIcon		wmgen;
69 Pixmap		pixmask;
70 
71   /*****************/
72  /* Mouse Regions */
73 /*****************/
74 
75 typedef struct {
76 	int		enable;
77 	int		top;
78 	int		bottom;
79 	int		left;
80 	int		right;
81 } MOUSE_REGION;
82 
83 MOUSE_REGION	mouse_region[MAX_MOUSE_REGION];
84 
85   /***********************/
86  /* Function Prototypes */
87 /***********************/
88 
89 static void GetXPM(XpmIcon *, char **);
90 static Pixel GetColor(char *);
91 void RedrawWindow(void);
92 void AddMouseRegion(int, int, int, int, int);
93 int CheckMouseRegion(int, int);
94 
95 /*******************************************************************************\
96 |* parse_rcfile																   *|
97 \*******************************************************************************/
98 
parse_rcfile(const char * filename,rckeys * keys)99 void parse_rcfile(const char *filename, rckeys *keys) {
100 
101 	char	*p,*q;
102 	char	temp[128];
103 	char	*tokens = " :\t\n";
104 	FILE	*fp;
105 	int		i,key;
106 
107 	fp = fopen(filename, "r");
108 	if (fp) {
109 		while (fgets(temp, 128, fp)) {
110 			key = 0;
111 			q = strdup(temp);
112 			q = strtok(q, tokens);
113 			while (key >= 0 && keys[key].label) {
114 				if ((!strcmp(q, keys[key].label))) {
115 					p = strstr(temp, keys[key].label);
116 					p += strlen(keys[key].label);
117 					p += strspn(p, tokens);
118 					if ((i = strcspn(p, "#\n"))) p[i] = 0;
119 					free(*keys[key].var);
120 					*keys[key].var = strdup(p);
121 					key = -1;
122 				} else key++;
123 			}
124 			free(q);
125 		}
126 		fclose(fp);
127 	}
128 }
129 
130 /*******************************************************************************\
131 |* parse_rcfile2															   *|
132 \*******************************************************************************/
133 
parse_rcfile2(const char * filename,rckeys2 * keys)134 void parse_rcfile2(const char *filename, rckeys2 *keys) {
135 
136 	char	*p;
137 	char	temp[128];
138 	char	*tokens = " :\t\n";
139 	FILE	*fp;
140 	int		i,key;
141 	char	*family = NULL;
142 
143 	fp = fopen(filename, "r");
144 	if (fp) {
145 		while (fgets(temp, 128, fp)) {
146 			key = 0;
147 			while (key >= 0 && keys[key].label) {
148 				if ((p = strstr(temp, keys[key].label))) {
149 					p += strlen(keys[key].label);
150 					p += strspn(p, tokens);
151 					if ((i = strcspn(p, "#\n"))) p[i] = 0;
152 					free(*keys[key].var);
153 					*keys[key].var = strdup(p);
154 					key = -1;
155 				} else key++;
156 			}
157 		}
158 		fclose(fp);
159 	}
160 	free(family);
161 }
162 
163 
164 /*******************************************************************************\
165 |* GetXPM																	   *|
166 \*******************************************************************************/
167 
GetXPM(XpmIcon * wmgen,char * pixmap_bytes[])168 static void GetXPM(XpmIcon *wmgen, char *pixmap_bytes[]) {
169 
170 	XWindowAttributes	attributes;
171 	int					err;
172 
173 	/* For the colormap */
174 	XGetWindowAttributes(display, Root, &attributes);
175 
176 	wmgen->attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
177 
178 	err = XpmCreatePixmapFromData(display, Root, pixmap_bytes, &(wmgen->pixmap),
179 					&(wmgen->mask), &(wmgen->attributes));
180 
181 	if (err != XpmSuccess) {
182 		fprintf(stderr, "Not enough free colorcells.\n");
183 		exit(1);
184 	}
185 }
186 
187 /*******************************************************************************\
188 |* GetColor																	   *|
189 \*******************************************************************************/
190 
GetColor(char * name)191 static Pixel GetColor(char *name) {
192 
193 	XColor				color;
194 	XWindowAttributes	attributes;
195 
196 	XGetWindowAttributes(display, Root, &attributes);
197 
198 	color.pixel = 0;
199 	if (!XParseColor(display, attributes.colormap, name, &color)) {
200 		fprintf(stderr, "wm.app: can't parse %s.\n", name);
201 	} else if (!XAllocColor(display, attributes.colormap, &color)) {
202 		fprintf(stderr, "wm.app: can't allocate %s.\n", name);
203 	}
204 	return color.pixel;
205 }
206 
207 /*******************************************************************************\
208 |* flush_expose																   *|
209 \*******************************************************************************/
210 
flush_expose(Window w)211 static int flush_expose(Window w) {
212 
213 	XEvent 		dummy;
214 	int			i=0;
215 
216 	while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
217 		i++;
218 
219 	return i;
220 }
221 
222 /*******************************************************************************\
223 |* RedrawWindow																   *|
224 \*******************************************************************************/
225 
RedrawWindow(void)226 void RedrawWindow(void) {
227 
228 	flush_expose(iconwin);
229 	XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
230 				0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
231 	flush_expose(win);
232 	XCopyArea(display, wmgen.pixmap, win, NormalGC,
233 				0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
234 }
235 
236 /*******************************************************************************\
237 |* RedrawWindowXY															   *|
238 \*******************************************************************************/
239 
RedrawWindowXY(int x,int y)240 void RedrawWindowXY(int x, int y) {
241 
242 	flush_expose(iconwin);
243 	XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
244 				x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
245 	flush_expose(win);
246 	XCopyArea(display, wmgen.pixmap, win, NormalGC,
247 				x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
248 }
249 
250 /*******************************************************************************\
251 |* AddMouseRegion															   *|
252 \*******************************************************************************/
253 
AddMouseRegion(int index,int left,int top,int right,int bottom)254 void AddMouseRegion(int index, int left, int top, int right, int bottom) {
255 
256 	if (index < MAX_MOUSE_REGION) {
257 		mouse_region[index].enable = 1;
258 		mouse_region[index].top = top;
259 		mouse_region[index].left = left;
260 		mouse_region[index].bottom = bottom;
261 		mouse_region[index].right = right;
262 	}
263 }
264 
265 /*******************************************************************************\
266 |* CheckMouseRegion															   *|
267 \*******************************************************************************/
268 
CheckMouseRegion(int x,int y)269 int CheckMouseRegion(int x, int y) {
270 
271 	int		i;
272 	int		found;
273 
274 	found = 0;
275 
276 	for (i=0; i<MAX_MOUSE_REGION && !found; i++) {
277 		if (mouse_region[i].enable &&
278 			x <= mouse_region[i].right &&
279 			x >= mouse_region[i].left &&
280 			y <= mouse_region[i].bottom &&
281 			y >= mouse_region[i].top)
282 			found = 1;
283 	}
284 	if (!found) return -1;
285 	return (i-1);
286 }
287 
288 /*******************************************************************************\
289 |* createXBMfromXPM															   *|
290 \*******************************************************************************/
createXBMfromXPM(char * xbm,char ** xpm,int sx,int sy)291 void createXBMfromXPM(char *xbm, char **xpm, int sx, int sy) {
292 
293 	int		i,j,k;
294 	int		width, height, numcol, depth;
295     int 	zero=0;
296 	unsigned char	bwrite;
297     int		bcount;
298     int     curpixel;
299 
300 	sscanf(*xpm, "%d %d %d %d", &width, &height, &numcol, &depth);
301 
302 
303     for (k=0; k!=depth; k++)
304     {
305         zero <<=8;
306         zero |= xpm[1][k];
307     }
308 
309 	for (i=numcol+1; i < numcol+sy+1; i++) {
310 		bcount = 0;
311 		bwrite = 0;
312 		for (j=0; j<sx*depth; j+=depth) {
313             bwrite >>= 1;
314 
315             curpixel=0;
316             for (k=0; k!=depth; k++)
317             {
318                 curpixel <<=8;
319                 curpixel |= xpm[i][j+k];
320             }
321 
322             if ( curpixel != zero ) {
323 				bwrite += 128;
324 			}
325 			bcount++;
326 			if (bcount == 8) {
327 				*xbm = bwrite;
328 				xbm++;
329 				bcount = 0;
330 				bwrite = 0;
331 			}
332 		}
333 	}
334 }
335 
336 /*******************************************************************************\
337 |* copyXPMArea																   *|
338 \*******************************************************************************/
339 
copyXPMArea(int x,int y,int sx,int sy,int dx,int dy)340 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
341 
342 	XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
343 
344 }
345 
346 /*******************************************************************************\
347 |* copyXBMArea																   *|
348 \*******************************************************************************/
349 
copyXBMArea(int x,int y,int sx,int sy,int dx,int dy)350 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
351 
352 	XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
353 }
354 
355 
356 /*******************************************************************************\
357 |* setMaskXY																   *|
358 \*******************************************************************************/
359 
setMaskXY(int x,int y)360 void setMaskXY(int x, int y) {
361 
362 	 XShapeCombineMask(display, win, ShapeBounding, x, y, pixmask, ShapeSet);
363 	 XShapeCombineMask(display, iconwin, ShapeBounding, x, y, pixmask, ShapeSet);
364 }
365 
366 /*******************************************************************************\
367 |* openXwindow																   *|
368 \*******************************************************************************/
openXwindow(int argc,char * argv[],char * pixmap_bytes[],char * pixmask_bits,int pixmask_width,int pixmask_height)369 void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits, int pixmask_width, int pixmask_height) {
370 
371 	unsigned int	borderwidth = 1;
372 	XClassHint		classHint;
373 	char			*display_name = NULL;
374 	char			*wname = argv[0];
375 	XTextProperty	name;
376 
377 	XGCValues		gcv;
378 	unsigned long	gcm;
379 
380 	char			*geometry = NULL;
381 
382 	int				dummy=0;
383 	int				i, wx, wy;
384 
385 	for (i=1; argv[i]; i++) {
386 		if (!strcmp(argv[i], "-display")) {
387 			display_name = argv[i+1];
388 			i++;
389 		}
390 		if (!strcmp(argv[i], "-geometry")) {
391 			geometry = argv[i+1];
392 			i++;
393 		}
394 	}
395 
396 	if (!(display = XOpenDisplay(display_name))) {
397 		fprintf(stderr, "%s: can't open display %s\n",
398 						wname, XDisplayName(display_name));
399 		exit(1);
400 	}
401 	screen  = DefaultScreen(display);
402 	Root    = RootWindow(display, screen);
403 	d_depth = DefaultDepth(display, screen);
404 	x_fd    = XConnectionNumber(display);
405 
406 	/* Convert XPM to XImage */
407 	GetXPM(&wmgen, pixmap_bytes);
408 
409 	/* Create a window to hold the stuff */
410 	mysizehints.flags = USSize | USPosition;
411 	mysizehints.x = 0;
412 	mysizehints.y = 0;
413 
414 	back_pix = GetColor("white");
415 	fore_pix = GetColor("black");
416 
417 	XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
418 				&mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
419 
420 	mysizehints.width = 64;
421 	mysizehints.height = 64;
422 
423 	win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
424 				mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
425 
426 	iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
427 				mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
428 
429 	/* Activate hints */
430 	XSetWMNormalHints(display, win, &mysizehints);
431 	classHint.res_name = wname;
432 	classHint.res_class = wname;
433 	XSetClassHint(display, win, &classHint);
434 
435 	XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
436 	XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
437 
438 	if (XStringListToTextProperty(&wname, 1, &name) == 0) {
439 		fprintf(stderr, "%s: can't allocate window name\n", wname);
440 		exit(1);
441 	}
442 
443 	XSetWMName(display, win, &name);
444 
445 	/* Create GC for drawing */
446 
447 	gcm = GCForeground | GCBackground | GCGraphicsExposures;
448 	gcv.foreground = fore_pix;
449 	gcv.background = back_pix;
450 	gcv.graphics_exposures = 0;
451 	NormalGC = XCreateGC(display, Root, gcm, &gcv);
452 
453 	/* ONLYSHAPE ON */
454 
455 	pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
456 
457 	XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
458 	XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
459 
460 	/* ONLYSHAPE OFF */
461 
462 	mywmhints.initial_state = WithdrawnState;
463 	mywmhints.icon_window = iconwin;
464 	mywmhints.icon_x = mysizehints.x;
465 	mywmhints.icon_y = mysizehints.y;
466 	mywmhints.window_group = win;
467 	mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
468 
469 	XSetWMHints(display, win, &mywmhints);
470 
471 	XSetCommand(display, win, argv, argc);
472 	XMapWindow(display, win);
473 
474 	if (geometry) {
475 		if (sscanf(geometry, "+%d+%d", &wx, &wy) != 2) {
476 			fprintf(stderr, "Bad geometry string.\n");
477 			exit(1);
478 		}
479 		XMoveWindow(display, win, wx, wy);
480 	}
481 }
482