wmpop3lb: Add version 2.4.2 to repository.
[dockapps.git] / wmpop3lb / wmgeneral / wmgeneral.c
blob63649566bef85123b6873f7c9021a3ec0bb2e85e
1 /*
2 Best viewed with vim5, using ts=4
4 wmgeneral was taken from wmppp.
6 It has a lot of routines which most of the wm* programs use.
8 ------------------------------------------------------------
10 Author: Martijn Pieterse (pieterse@xs4all.nl)
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.
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <ctype.h>
46 #include <stdarg.h>
48 #include <X11/Xlib.h>
49 #include <X11/xpm.h>
50 #include <X11/extensions/shape.h>
52 #include "wmgeneral.h"
54 /*****************/
55 /* X11 Variables */
56 /*****************/
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;
71 /*****************/
72 /* Mouse Regions */
73 /*****************/
75 typedef struct {
76 int enable;
77 int top;
78 int bottom;
79 int left;
80 int right;
81 } MOUSE_REGION;
83 MOUSE_REGION mouse_region[MAX_MOUSE_REGION];
85 /***********************/
86 /* Function Prototypes */
87 /***********************/
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);
95 /*******************************************************************************\
96 |* parse_rcfile *|
97 \*******************************************************************************/
99 void parse_rcfile(const char *filename, rckeys *keys) {
101 char *p,*q;
102 char temp[128];
103 char *tokens = " :\t\n";
104 FILE *fp;
105 int i,key;
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++;
124 free(q);
126 fclose(fp);
130 /*******************************************************************************\
131 |* parse_rcfile2 *|
132 \*******************************************************************************/
134 void parse_rcfile2(const char *filename, rckeys2 *keys) {
136 char *p;
137 char temp[128];
138 char *tokens = " :\t\n";
139 FILE *fp;
140 int i,key;
141 char *family = NULL;
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++;
158 fclose(fp);
160 free(family);
164 /*******************************************************************************\
165 |* GetXPM *|
166 \*******************************************************************************/
168 static void GetXPM(XpmIcon *wmgen, char *pixmap_bytes[]) {
170 XWindowAttributes attributes; int err;
172 /* For the colormap */
173 XGetWindowAttributes(display, Root, &attributes);
175 wmgen->attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
177 err = XpmCreatePixmapFromData(display, Root, pixmap_bytes, &(wmgen->pixmap),
178 &(wmgen->mask), &(wmgen->attributes));
180 if (err != XpmSuccess) {
181 fprintf(stderr, "Not enough free colorcells.\n");
182 exit(1);
186 /*******************************************************************************\
187 |* GetColor *|
188 \*******************************************************************************/
190 static Pixel GetColor(char *name) {
192 XColor color;
193 XWindowAttributes attributes;
195 XGetWindowAttributes(display, Root, &attributes);
197 color.pixel = 0;
198 if (!XParseColor(display, attributes.colormap, name, &color)) {
199 fprintf(stderr, "wm.app: can't parse %s.\n", name);
200 } else if (!XAllocColor(display, attributes.colormap, &color)) {
201 fprintf(stderr, "wm.app: can't allocate %s.\n", name);
203 return color.pixel;
206 /*******************************************************************************\
207 |* flush_expose *|
208 \*******************************************************************************/
210 static int flush_expose(Window w) {
212 XEvent dummy;
213 int i=0;
215 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
216 i++;
218 return i;
221 /*******************************************************************************\
222 |* RedrawWindow *|
223 \*******************************************************************************/
225 void RedrawWindow(void) {
227 flush_expose(iconwin);
228 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
229 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
230 flush_expose(win);
231 XCopyArea(display, wmgen.pixmap, win, NormalGC,
232 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
235 /*******************************************************************************\
236 |* RedrawWindowXY *|
237 \*******************************************************************************/
239 void RedrawWindowXY(int x, int y) {
241 flush_expose(iconwin);
242 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
243 x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
244 flush_expose(win);
245 XCopyArea(display, wmgen.pixmap, win, NormalGC,
246 x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
249 /*******************************************************************************\
250 |* AddMouseRegion *|
251 \*******************************************************************************/
253 void AddMouseRegion(int index, int left, int top, int right, int bottom) {
255 if (index < MAX_MOUSE_REGION) {
256 mouse_region[index].enable = 1;
257 mouse_region[index].top = top;
258 mouse_region[index].left = left;
259 mouse_region[index].bottom = bottom;
260 mouse_region[index].right = right;
264 /*******************************************************************************\
265 |* CheckMouseRegion *|
266 \*******************************************************************************/
268 int CheckMouseRegion(int x, int y) {
270 int i;
271 int found;
273 found = 0;
275 for (i=0; i<MAX_MOUSE_REGION && !found; i++) {
276 if (mouse_region[i].enable &&
277 x <= mouse_region[i].right &&
278 x >= mouse_region[i].left &&
279 y <= mouse_region[i].bottom &&
280 y >= mouse_region[i].top)
281 found = 1;
283 if (!found) return -1;
284 return (i-1);
287 /*******************************************************************************\
288 |* createXBMfromXPM *|
289 \*******************************************************************************/
290 void createXBMfromXPM(char *xbm, char **xpm, int sx, int sy) {
292 int i,j,k;
293 int width, height, numcol, depth;
294 int zero=0;
295 unsigned char bwrite;
296 int bcount;
297 int curpixel;
299 sscanf(*xpm, "%d %d %d %d", &width, &height, &numcol, &depth);
302 for (k=0; k!=depth; k++)
304 zero <<=8;
305 zero |= xpm[1][k];
308 for (i=numcol+1; i < numcol+sy+1; i++) {
309 bcount = 0;
310 bwrite = 0;
311 for (j=0; j<sx*depth; j+=depth) {
312 bwrite >>= 1;
314 curpixel=0;
315 for (k=0; k!=depth; k++)
317 curpixel <<=8;
318 curpixel |= xpm[i][j+k];
321 if ( curpixel != zero ) {
322 bwrite += 128;
324 bcount++;
325 if (bcount == 8) {
326 *xbm = bwrite;
327 xbm++;
328 bcount = 0;
329 bwrite = 0;
335 /*******************************************************************************\
336 |* copyXPMArea *|
337 \*******************************************************************************/
339 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
341 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
345 /*******************************************************************************\
346 |* copyXBMArea *|
347 \*******************************************************************************/
349 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
351 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
355 /*******************************************************************************\
356 |* setMaskXY *|
357 \*******************************************************************************/
359 void setMaskXY(int x, int y) {
361 XShapeCombineMask(display, win, ShapeBounding, x, y, pixmask, ShapeSet);
362 XShapeCombineMask(display, iconwin, ShapeBounding, x, y, pixmask, ShapeSet);
365 /*******************************************************************************\
366 |* openXwindow *|
367 \*******************************************************************************/
368 void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits, int pixmask_width, int pixmask_height) {
370 unsigned int borderwidth = 1;
371 XClassHint classHint;
372 char *display_name = NULL;
373 char *wname = argv[0];
374 XTextProperty name;
376 XGCValues gcv;
377 unsigned long gcm;
379 char *geometry = NULL;
381 int dummy=0;
382 int i, wx, wy;
384 for (i=1; argv[i]; i++) {
385 if (!strcmp(argv[i], "-display")) {
386 display_name = argv[i+1];
387 i++;
389 if (!strcmp(argv[i], "-geometry")) {
390 geometry = argv[i+1];
391 i++;
395 if (!(display = XOpenDisplay(display_name))) {
396 fprintf(stderr, "%s: can't open display %s\n",
397 wname, XDisplayName(display_name));
398 exit(1);
400 screen = DefaultScreen(display);
401 Root = RootWindow(display, screen);
402 d_depth = DefaultDepth(display, screen);
403 x_fd = XConnectionNumber(display);
405 /* Convert XPM to XImage */
406 GetXPM(&wmgen, pixmap_bytes);
408 /* Create a window to hold the stuff */
409 mysizehints.flags = USSize | USPosition;
410 mysizehints.x = 0;
411 mysizehints.y = 0;
413 back_pix = GetColor("white");
414 fore_pix = GetColor("black");
416 XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
417 &mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
419 mysizehints.width = 64;
420 mysizehints.height = 64;
422 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
423 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
425 iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
426 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
428 /* Activate hints */
429 XSetWMNormalHints(display, win, &mysizehints);
430 classHint.res_name = wname;
431 classHint.res_class = wname;
432 XSetClassHint(display, win, &classHint);
434 XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
435 XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
437 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
438 fprintf(stderr, "%s: can't allocate window name\n", wname);
439 exit(1);
442 XSetWMName(display, win, &name);
444 /* Create GC for drawing */
446 gcm = GCForeground | GCBackground | GCGraphicsExposures;
447 gcv.foreground = fore_pix;
448 gcv.background = back_pix;
449 gcv.graphics_exposures = 0;
450 NormalGC = XCreateGC(display, Root, gcm, &gcv);
452 /* ONLYSHAPE ON */
454 pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
456 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
457 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
459 /* ONLYSHAPE OFF */
461 mywmhints.initial_state = WithdrawnState;
462 mywmhints.icon_window = iconwin;
463 mywmhints.icon_x = mysizehints.x;
464 mywmhints.icon_y = mysizehints.y;
465 mywmhints.window_group = win;
466 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
468 XSetWMHints(display, win, &mywmhints);
470 XSetCommand(display, win, argv, argc);
471 XMapWindow(display, win);
473 if (geometry) {
474 if (sscanf(geometry, "+%d+%d", &wx, &wy) != 2) {
475 fprintf(stderr, "Bad geometry string.\n");
476 exit(1);
478 XMoveWindow(display, win, wx, wy);