wmgeneral: Fix segfault if newline encountered in parse_rcfile.
[dockapps.git] / wmtz / wmgeneral / wmgeneral.c
blob436b196e01a094edcd9c643ed170287ae387117c
1 /*
2 wmgeneral was taken from wmppp.
4 It has a lot of routines which most of the wm* programs use.
6 ------------------------------------------------------------
8 Copyright (C) 1998 Martijn Pieterse (pieterse@xs4all.nl)
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 02110-1301, USA.
25 ---
26 CHANGES:
27 ---
28 10/10/2003 (Simon Law, sfllaw@debian.org)
29 * changed the parse_rcfile function to use getline instead of
30 fgets.
31 10/14/2000 (Chris Gray, cgray@tribsoft.com)
32 * Removed a bug from parse_rcfile. An extra
33 newline would cause a segfault.
34 14/09/1998 (Dave Clark, clarkd@skyia.com)
35 * Updated createXBMfromXPM routine
36 * Now supports >256 colors
37 11/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
38 * Removed a bug from parse_rcfile. You could
39 not use "start" in a command if a label was
40 also start.
41 * Changed the needed geometry string.
42 We don't use window size, and don't support
43 negative positions.
44 03/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
45 * Added parse_rcfile2
46 02/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
47 * Added -geometry support (untested)
48 28/08/1998 (Martijn Pieterse, pieterse@xs4all.nl)
49 * Added createXBMfromXPM routine
50 * Saves a lot of work with changing xpm's.
51 02/05/1998 (Martijn Pieterse, pieterse@xs4all.nl)
52 * changed the read_rc_file to parse_rcfile, as suggested by
53 Marcelo E. Magallon
54 * debugged the parse_rc file.
55 30/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
56 * Ripped similar code from all the wm* programs,
57 and put them in a single file.
61 #include "wmgeneral.h"
62 #include <X11/Xlib.h> /* for XCopyArea, etc */
63 #include <X11/Xutil.h> /* for XSizeHints, XWMHints, etc */
64 #include <X11/extensions/shape.h> /* for XShapeCombineMask */
65 #include <X11/extensions/shapeconst.h> /* for ShapeBounding, ShapeSet */
66 #include <X11/xpm.h> /* for XpmAttributes, Pixel, etc */
67 #include <stddef.h> /* for size_t */
68 #include <stdio.h> /* for fprintf, stderr, NULL, etc */
69 #include <stdlib.h> /* for exit, free */
70 #include <string.h> /* for strcmp, strdup, strcspn, etc */
72 /*****************/
73 /* X11 Variables */
74 /*****************/
76 Window Root;
77 int screen;
78 int x_fd;
79 int d_depth;
80 XSizeHints mysizehints;
81 XWMHints mywmhints;
82 Pixel back_pix, fore_pix;
83 char *Geometry = "";
84 Window iconwin, win;
85 GC NormalGC;
86 XpmIcon wmgen;
87 Pixmap pixmask;
89 /*****************/
90 /* Mouse Regions */
91 /*****************/
93 typedef struct {
94 int enable;
95 int top;
96 int bottom;
97 int left;
98 int right;
99 } MOUSE_REGION;
101 MOUSE_REGION mouse_region[MAX_MOUSE_REGION];
103 /***********************/
104 /* Function Prototypes */
105 /***********************/
107 static void GetXPM(XpmIcon *, char **);
108 static Pixel GetColor(char *);
109 void RedrawWindow(void);
110 void AddMouseRegion(int, int, int, int, int);
111 int CheckMouseRegion(int, int);
113 /*******************************************************************************\
114 |* parse_rcfile *|
115 \*******************************************************************************/
117 void parse_rcfile(const char *filename, rckeys *keys) {
119 char *p;
120 FILE *fp;
122 fp = fopen(filename, "r");
123 if (fp) {
124 char temp[128];
126 while (fgets(temp, 128, fp)) {
127 char *q;
128 char *tokens = " :\t\n";
129 int key;
131 key = 0;
132 q = strdup(temp);
133 q = strtok(q, tokens);
134 if(!q)
135 continue;
136 while (key >= 0 && keys[key].label) {
137 if ((!strcmp(q, keys[key].label))) {
138 int i;
140 p = strstr(temp, keys[key].label);
141 p += strlen(keys[key].label);
142 p += strspn(p, tokens);
143 if ((i = strcspn(p, "#\n"))) p[i] = '\0';
144 free(*keys[key].var);
145 *keys[key].var = strdup(p);
146 key = -1;
147 } else key++;
149 free(q);
151 fclose(fp);
155 /*******************************************************************************\
156 |* parse_rcfile2 *|
157 \*******************************************************************************/
159 void parse_rcfile2(const char *filename, rckeys2 *keys) {
161 char *p;
162 char *line = NULL;
163 size_t line_size = 0;
164 FILE *fp;
165 char *family = NULL;
167 fp = fopen(filename, "r");
168 if (fp) {
169 while (getline(&line, &line_size, fp) >= 0) {
170 int key;
172 key = 0;
173 while (key >= 0 && keys[key].label) {
174 if ((p = strstr(line, keys[key].label))) {
175 char *tokens = " :\t\n";
176 int i;
178 p += strlen(keys[key].label);
179 p += strspn(p, tokens);
180 if ((i = strcspn(p, "#\n"))) p[i] = 0;
181 free(*keys[key].var);
182 *keys[key].var = strdup(p);
183 key = -1;
184 } else key++;
187 fclose(fp);
189 free(family);
193 /*******************************************************************************\
194 |* GetXPM *|
195 \*******************************************************************************/
197 static void GetXPM(XpmIcon *wmgen, char *pixmap_bytes[]) {
199 XWindowAttributes attributes;
200 int err;
202 /* For the colormap */
203 XGetWindowAttributes(display, Root, &attributes);
205 wmgen->attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
207 err = XpmCreatePixmapFromData(display, Root, pixmap_bytes, &(wmgen->pixmap),
208 &(wmgen->mask), &(wmgen->attributes));
210 if (err != XpmSuccess) {
211 fprintf(stderr, "Not enough free colorcells.\n");
212 exit(1);
216 /*******************************************************************************\
217 |* GetColor *|
218 \*******************************************************************************/
220 static Pixel GetColor(char *name) {
222 XColor color;
223 XWindowAttributes attributes;
225 XGetWindowAttributes(display, Root, &attributes);
227 color.pixel = 0;
228 if (!XParseColor(display, attributes.colormap, name, &color)) {
229 fprintf(stderr, "wm.app: can't parse %s.\n", name);
230 } else if (!XAllocColor(display, attributes.colormap, &color)) {
231 fprintf(stderr, "wm.app: can't allocate %s.\n", name);
233 return color.pixel;
236 /*******************************************************************************\
237 |* flush_expose *|
238 \*******************************************************************************/
240 static int flush_expose(Window w) {
242 XEvent dummy;
243 int i=0;
245 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
246 i++;
248 return i;
251 /*******************************************************************************\
252 |* RedrawWindow *|
253 \*******************************************************************************/
255 void RedrawWindow(void) {
257 flush_expose(iconwin);
258 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
259 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
260 flush_expose(win);
261 XCopyArea(display, wmgen.pixmap, win, NormalGC,
262 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
265 /*******************************************************************************\
266 |* RedrawWindowXY *|
267 \*******************************************************************************/
269 void RedrawWindowXY(int x, int y) {
271 flush_expose(iconwin);
272 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
273 x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
274 flush_expose(win);
275 XCopyArea(display, wmgen.pixmap, win, NormalGC,
276 x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
279 /*******************************************************************************\
280 |* AddMouseRegion *|
281 \*******************************************************************************/
283 void AddMouseRegion(int index, int left, int top, int right, int bottom) {
285 if (index < MAX_MOUSE_REGION) {
286 mouse_region[index].enable = 1;
287 mouse_region[index].top = top;
288 mouse_region[index].left = left;
289 mouse_region[index].bottom = bottom;
290 mouse_region[index].right = right;
294 /*******************************************************************************\
295 |* CheckMouseRegion *|
296 \*******************************************************************************/
298 int CheckMouseRegion(int x, int y) {
300 int i;
301 int found;
303 found = 0;
305 for (i=0; i<MAX_MOUSE_REGION && !found; i++) {
306 if (mouse_region[i].enable &&
307 x <= mouse_region[i].right &&
308 x >= mouse_region[i].left &&
309 y <= mouse_region[i].bottom &&
310 y >= mouse_region[i].top)
311 found = 1;
313 if (!found) return -1;
314 return (i-1);
317 /*******************************************************************************\
318 |* createXBMfromXPM *|
319 \*******************************************************************************/
320 void createXBMfromXPM(char *xbm, char **xpm, int sx, int sy) {
322 int i,j,k;
323 int width, height, numcol, depth;
324 int zero=0;
325 int curpixel;
327 sscanf(*xpm, "%10d %10d %10d %10d", &width, &height, &numcol, &depth);
330 for (k=0; k!=depth; k++)
332 zero <<=8;
333 zero |= xpm[1][k];
336 for (i=numcol+1; i < numcol+sy+1; i++) {
337 unsigned char bwrite;
338 int bcount;
340 bcount = 0;
341 bwrite = 0;
342 for (j=0; j<sx*depth; j+=depth) {
343 bwrite >>= 1;
345 curpixel=0;
346 for (k=0; k!=depth; k++)
348 curpixel <<=8;
349 curpixel |= xpm[i][j+k];
352 if ( curpixel != zero ) {
353 bwrite += 128;
355 bcount++;
356 if (bcount == 8) {
357 *xbm = bwrite;
358 xbm++;
359 bcount = 0;
360 bwrite = 0;
366 /*******************************************************************************\
367 |* copyXPMArea *|
368 \*******************************************************************************/
370 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
372 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
376 /*******************************************************************************\
377 |* copyXBMArea *|
378 \*******************************************************************************/
380 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
382 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
386 /*******************************************************************************\
387 |* setMaskXY *|
388 \*******************************************************************************/
390 void setMaskXY(int x, int y) {
392 XShapeCombineMask(display, win, ShapeBounding, x, y, pixmask, ShapeSet);
393 XShapeCombineMask(display, iconwin, ShapeBounding, x, y, pixmask, ShapeSet);
396 /*******************************************************************************\
397 |* openXwindow *|
398 \*******************************************************************************/
399 void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits, int pixmask_width, int pixmask_height) {
401 unsigned int borderwidth = 1;
402 XClassHint classHint;
403 char *display_name = NULL;
404 char *wname = argv[0];
405 XTextProperty name;
407 XGCValues gcv;
408 unsigned long gcm;
410 char *geometry = NULL;
412 int dummy=0;
413 int i, wx, wy;
415 for (i=1; argv[i]; i++) {
416 if (!strcmp(argv[i], "-display"))
417 display_name = argv[++i];
418 else if (!strcmp(argv[i], "-geometry"))
419 geometry = argv[++i];
422 if (!(display = XOpenDisplay(display_name))) {
423 fprintf(stderr, "%s: can't open display %s\n",
424 wname, XDisplayName(display_name));
425 exit(1);
427 screen = DefaultScreen(display);
428 Root = RootWindow(display, screen);
429 d_depth = DefaultDepth(display, screen);
430 x_fd = XConnectionNumber(display);
432 /* Convert XPM to XImage */
433 GetXPM(&wmgen, pixmap_bytes);
435 /* Create a window to hold the stuff */
436 mysizehints.flags = USSize | USPosition;
437 mysizehints.x = 0;
438 mysizehints.y = 0;
440 back_pix = GetColor("white");
441 fore_pix = GetColor("black");
443 XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
444 &mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
445 if (geometry)
446 XParseGeometry(geometry, &mysizehints.x, &mysizehints.y,
447 (unsigned int *) &mysizehints.width, (unsigned int *) &mysizehints.height);
449 mysizehints.width = 64;
450 mysizehints.height = 64;
452 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
453 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
455 iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
456 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
458 /* Activate hints */
459 XSetWMNormalHints(display, win, &mysizehints);
460 classHint.res_name = wname;
461 classHint.res_class = wname;
462 XSetClassHint(display, win, &classHint);
464 XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
465 XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
467 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
468 fprintf(stderr, "%s: can't allocate window name\n", wname);
469 exit(1);
472 XSetWMName(display, win, &name);
474 /* Create GC for drawing */
476 gcm = GCForeground | GCBackground | GCGraphicsExposures;
477 gcv.foreground = fore_pix;
478 gcv.background = back_pix;
479 gcv.graphics_exposures = 0;
480 NormalGC = XCreateGC(display, Root, gcm, &gcv);
482 /* ONLYSHAPE ON */
484 pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
486 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
487 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
489 /* ONLYSHAPE OFF */
491 mywmhints.initial_state = WithdrawnState;
492 mywmhints.icon_window = iconwin;
493 mywmhints.icon_x = mysizehints.x;
494 mywmhints.icon_y = mysizehints.y;
495 mywmhints.window_group = win;
496 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
498 XSetWMHints(display, win, &mywmhints);
500 XSetCommand(display, win, argv, argc);
501 XMapWindow(display, win);
503 if (geometry) {
504 if (sscanf(geometry, "+%10d+%10d", &wx, &wy) != 2) {
505 fprintf(stderr, "Bad geometry string.\n");
506 exit(1);
508 XMoveWindow(display, win, wx, wy);