wmgeneral: Add proper copyright headers; fix changelog formatting.
[dockapps.git] / wmkeys / wmgeneral / wmgeneral.c
blobace55a4447aa0999a31ae159f721af138811728e
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 14/09/1998 (Dave Clark, clarkd@skyia.com)
32 * Updated createXBMfromXPM routine
33 * Now supports >256 colors
34 11/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
35 * Removed a bug from parse_rcfile. You could
36 not use "start" in a command if a label was
37 also start.
38 * Changed the needed geometry string.
39 We don't use window size, and don't support
40 negative positions.
41 03/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
42 * Added parse_rcfile2
43 02/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
44 * Added -geometry support (untested)
45 28/08/1998 (Martijn Pieterse, pieterse@xs4all.nl)
46 * Added createXBMfromXPM routine
47 * Saves a lot of work with changing xpm's.
48 02/05/1998 (Martijn Pieterse, pieterse@xs4all.nl)
49 * changed the read_rc_file to parse_rcfile, as suggested by
50 Marcelo E. Magallon
51 * debugged the parse_rc file.
52 30/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
53 * Ripped similar code from all the wm* programs,
54 and put them in a single file.
58 #include "wmgeneral.h"
59 #include <X11/Xlib.h> /* for XCopyArea, etc */
60 #include <X11/Xutil.h> /* for XSizeHints, XWMHints, etc */
61 #include <X11/extensions/shape.h> /* for XShapeCombineMask */
62 #include <X11/extensions/shapeconst.h> /* for ShapeBounding, ShapeSet */
63 #include <X11/xpm.h> /* for XpmAttributes, Pixel, etc */
64 #include <stddef.h> /* for size_t */
65 #include <stdio.h> /* for fprintf, stderr, NULL, etc */
66 #include <stdlib.h> /* for exit, free */
67 #include <string.h> /* for strcmp, strdup, strcspn, etc */
69 /*****************/
70 /* X11 Variables */
71 /*****************/
73 Window Root;
74 int screen;
75 int x_fd;
76 int d_depth;
77 XSizeHints mysizehints;
78 XWMHints mywmhints;
79 Pixel back_pix, fore_pix;
80 char *Geometry = "";
81 Window iconwin, win;
82 GC NormalGC;
83 XpmIcon wmgen;
84 Pixmap pixmask;
86 /*****************/
87 /* Mouse Regions */
88 /*****************/
90 typedef struct {
91 int enable;
92 int top;
93 int bottom;
94 int left;
95 int right;
96 } MOUSE_REGION;
98 MOUSE_REGION mouse_region[MAX_MOUSE_REGION];
100 /***********************/
101 /* Function Prototypes */
102 /***********************/
104 static void GetXPM(XpmIcon *, char **);
105 static Pixel GetColor(char *);
106 void RedrawWindow(void);
107 void AddMouseRegion(int, int, int, int, int);
108 int CheckMouseRegion(int, int);
110 /*******************************************************************************\
111 |* parse_rcfile *|
112 \*******************************************************************************/
114 void parse_rcfile(const char *filename, rckeys *keys) {
116 char *p;
117 FILE *fp;
119 fp = fopen(filename, "r");
120 if (fp) {
121 char temp[128];
123 while (fgets(temp, 128, fp)) {
124 char *q;
125 char *tokens = " :\t\n";
126 int key;
128 key = 0;
129 q = strdup(temp);
130 q = strtok(q, tokens);
131 while (key >= 0 && keys[key].label) {
132 if ((!strcmp(q, keys[key].label))) {
133 int i;
135 p = strstr(temp, keys[key].label);
136 p += strlen(keys[key].label);
137 p += strspn(p, tokens);
138 if ((i = strcspn(p, "#\n"))) p[i] = 0;
139 free(*keys[key].var);
140 *keys[key].var = strdup(p);
141 key = -1;
142 } else key++;
144 free(q);
146 fclose(fp);
150 /*******************************************************************************\
151 |* parse_rcfile2 *|
152 \*******************************************************************************/
154 void parse_rcfile2(const char *filename, rckeys2 *keys) {
156 char *p;
157 char *line = NULL;
158 size_t line_size = 0;
159 FILE *fp;
160 char *family = NULL;
162 fp = fopen(filename, "r");
163 if (fp) {
164 while (getline(&line, &line_size, fp) >= 0) {
165 int key;
167 key = 0;
168 while (key >= 0 && keys[key].label) {
169 if ((p = strstr(line, keys[key].label))) {
170 char *tokens = " :\t\n";
171 int i;
173 p += strlen(keys[key].label);
174 p += strspn(p, tokens);
175 if ((i = strcspn(p, "#\n"))) p[i] = 0;
176 free(*keys[key].var);
177 *keys[key].var = strdup(p);
178 key = -1;
179 } else key++;
182 fclose(fp);
184 free(family);
188 /*******************************************************************************\
189 |* GetXPM *|
190 \*******************************************************************************/
192 static void GetXPM(XpmIcon *wmgen, char *pixmap_bytes[]) {
194 XWindowAttributes attributes;
195 int err;
197 /* For the colormap */
198 XGetWindowAttributes(display, Root, &attributes);
200 wmgen->attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
202 err = XpmCreatePixmapFromData(display, Root, pixmap_bytes, &(wmgen->pixmap),
203 &(wmgen->mask), &(wmgen->attributes));
205 if (err != XpmSuccess) {
206 fprintf(stderr, "Not enough free colorcells.\n");
207 exit(1);
211 /*******************************************************************************\
212 |* GetColor *|
213 \*******************************************************************************/
215 static Pixel GetColor(char *name) {
217 XColor color;
218 XWindowAttributes attributes;
220 XGetWindowAttributes(display, Root, &attributes);
222 color.pixel = 0;
223 if (!XParseColor(display, attributes.colormap, name, &color)) {
224 fprintf(stderr, "wm.app: can't parse %s.\n", name);
225 } else if (!XAllocColor(display, attributes.colormap, &color)) {
226 fprintf(stderr, "wm.app: can't allocate %s.\n", name);
228 return color.pixel;
231 /*******************************************************************************\
232 |* flush_expose *|
233 \*******************************************************************************/
235 static int flush_expose(Window w) {
237 XEvent dummy;
238 int i=0;
240 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
241 i++;
243 return i;
246 /*******************************************************************************\
247 |* RedrawWindow *|
248 \*******************************************************************************/
250 void RedrawWindow(void) {
252 flush_expose(iconwin);
253 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
254 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
255 flush_expose(win);
256 XCopyArea(display, wmgen.pixmap, win, NormalGC,
257 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
260 /*******************************************************************************\
261 |* RedrawWindowXY *|
262 \*******************************************************************************/
264 void RedrawWindowXY(int x, int y) {
266 flush_expose(iconwin);
267 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
268 x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
269 flush_expose(win);
270 XCopyArea(display, wmgen.pixmap, win, NormalGC,
271 x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
274 /*******************************************************************************\
275 |* AddMouseRegion *|
276 \*******************************************************************************/
278 void AddMouseRegion(int index, int left, int top, int right, int bottom) {
280 if (index < MAX_MOUSE_REGION) {
281 mouse_region[index].enable = 1;
282 mouse_region[index].top = top;
283 mouse_region[index].left = left;
284 mouse_region[index].bottom = bottom;
285 mouse_region[index].right = right;
289 /*******************************************************************************\
290 |* CheckMouseRegion *|
291 \*******************************************************************************/
293 int CheckMouseRegion(int x, int y) {
295 int i;
296 int found;
298 found = 0;
300 for (i=0; i<MAX_MOUSE_REGION && !found; i++) {
301 if (mouse_region[i].enable &&
302 x <= mouse_region[i].right &&
303 x >= mouse_region[i].left &&
304 y <= mouse_region[i].bottom &&
305 y >= mouse_region[i].top)
306 found = 1;
308 if (!found) return -1;
309 return (i-1);
312 /*******************************************************************************\
313 |* createXBMfromXPM *|
314 \*******************************************************************************/
315 void createXBMfromXPM(char *xbm, char **xpm, int sx, int sy) {
317 int i,j,k;
318 int width, height, numcol, depth;
319 int zero=0;
320 int curpixel;
322 sscanf(*xpm, "%10d %10d %10d %10d", &width, &height, &numcol, &depth);
325 for (k=0; k!=depth; k++)
327 zero <<=8;
328 zero |= xpm[1][k];
331 for (i=numcol+1; i < numcol+sy+1; i++) {
332 unsigned char bwrite;
333 int bcount;
335 bcount = 0;
336 bwrite = 0;
337 for (j=0; j<sx*depth; j+=depth) {
338 bwrite >>= 1;
340 curpixel=0;
341 for (k=0; k!=depth; k++)
343 curpixel <<=8;
344 curpixel |= xpm[i][j+k];
347 if ( curpixel != zero ) {
348 bwrite += 128;
350 bcount++;
351 if (bcount == 8) {
352 *xbm = bwrite;
353 xbm++;
354 bcount = 0;
355 bwrite = 0;
361 /*******************************************************************************\
362 |* copyXPMArea *|
363 \*******************************************************************************/
365 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
367 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
371 /*******************************************************************************\
372 |* copyXBMArea *|
373 \*******************************************************************************/
375 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
377 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
381 /*******************************************************************************\
382 |* setMaskXY *|
383 \*******************************************************************************/
385 void setMaskXY(int x, int y) {
387 XShapeCombineMask(display, win, ShapeBounding, x, y, pixmask, ShapeSet);
388 XShapeCombineMask(display, iconwin, ShapeBounding, x, y, pixmask, ShapeSet);
391 /*******************************************************************************\
392 |* openXwindow *|
393 \*******************************************************************************/
394 void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits, int pixmask_width, int pixmask_height) {
396 unsigned int borderwidth = 1;
397 XClassHint classHint;
398 char *display_name = NULL;
399 char *wname = argv[0];
400 XTextProperty name;
402 XGCValues gcv;
403 unsigned long gcm;
405 char *geometry = NULL;
407 int dummy=0;
408 int i, wx, wy;
410 for (i=1; argv[i]; i++) {
411 if (!strcmp(argv[i], "-display"))
412 display_name = argv[++i];
413 else if (!strcmp(argv[i], "-geometry"))
414 geometry = argv[++i];
417 if (!(display = XOpenDisplay(display_name))) {
418 fprintf(stderr, "%s: can't open display %s\n",
419 wname, XDisplayName(display_name));
420 exit(1);
422 screen = DefaultScreen(display);
423 Root = RootWindow(display, screen);
424 d_depth = DefaultDepth(display, screen);
425 x_fd = XConnectionNumber(display);
427 /* Convert XPM to XImage */
428 GetXPM(&wmgen, pixmap_bytes);
430 /* Create a window to hold the stuff */
431 mysizehints.flags = USSize | USPosition;
432 mysizehints.x = 0;
433 mysizehints.y = 0;
435 back_pix = GetColor("white");
436 fore_pix = GetColor("black");
438 XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
439 &mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
440 if (geometry)
441 XParseGeometry(geometry, &mysizehints.x, &mysizehints.y,
442 (unsigned int *) &mysizehints.width, (unsigned int *) &mysizehints.height);
444 mysizehints.width = 64;
445 mysizehints.height = 64;
447 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
448 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
450 iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
451 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
453 /* Activate hints */
454 XSetWMNormalHints(display, win, &mysizehints);
455 classHint.res_name = wname;
456 classHint.res_class = wname;
457 XSetClassHint(display, win, &classHint);
459 XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
460 XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
462 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
463 fprintf(stderr, "%s: can't allocate window name\n", wname);
464 exit(1);
467 XSetWMName(display, win, &name);
469 /* Create GC for drawing */
471 gcm = GCForeground | GCBackground | GCGraphicsExposures;
472 gcv.foreground = fore_pix;
473 gcv.background = back_pix;
474 gcv.graphics_exposures = 0;
475 NormalGC = XCreateGC(display, Root, gcm, &gcv);
477 /* ONLYSHAPE ON */
479 pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
481 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
482 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
484 /* ONLYSHAPE OFF */
486 mywmhints.initial_state = WithdrawnState;
487 mywmhints.icon_window = iconwin;
488 mywmhints.icon_x = mysizehints.x;
489 mywmhints.icon_y = mysizehints.y;
490 mywmhints.window_group = win;
491 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
493 XSetWMHints(display, win, &mywmhints);
495 XSetCommand(display, win, argv, argc);
496 XMapWindow(display, win);
498 if (geometry) {
499 if (sscanf(geometry, "+%10d+%10d", &wx, &wy) != 2) {
500 fprintf(stderr, "Bad geometry string.\n");
501 exit(1);
503 XMoveWindow(display, win, wx, wy);