wmgeneral: Merge 2003-10-10 version from wmmon and wmtime.
[dockapps.git] / wmppp.app / wmgeneral / wmgeneral.c
blob739003d279a14039265b32b1e9e7f6ff079b8d24
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 10/10/2003 (Simon Law, sfllaw@debian.org)
16 * changed the parse_rcfile function to use getline instead of fgets.
17 14/09/1998 (Dave Clark, clarkd@skyia.com)
18 * Updated createXBMfromXPM routine
19 * Now supports >256 colors
20 11/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
21 * Removed a bug from parse_rcfile. You could
22 not use "start" in a command if a label was
23 also start.
24 * Changed the needed geometry string.
25 We don't use window size, and don't support
26 negative positions.
27 03/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
28 * Added parse_rcfile2
29 02/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
30 * Added -geometry support (untested)
31 28/08/1998 (Martijn Pieterse, pieterse@xs4all.nl)
32 * Added createXBMfromXPM routine
33 * Saves a lot of work with changing xpm's.
34 02/05/1998 (Martijn Pieterse, pieterse@xs4all.nl)
35 * changed the read_rc_file to parse_rcfile, as suggested by Marcelo E. Magallon
36 * debugged the parse_rc file.
37 30/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
38 * Ripped similar code from all the wm* programs,
39 and put them in a single file.
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <ctype.h>
48 #include <stdarg.h>
50 #include <X11/Xlib.h>
51 #include <X11/xpm.h>
52 #include <X11/extensions/shape.h>
54 #include "wmgeneral.h"
56 /*****************/
57 /* X11 Variables */
58 /*****************/
60 Window Root;
61 int screen;
62 int x_fd;
63 int d_depth;
64 XSizeHints mysizehints;
65 XWMHints mywmhints;
66 Pixel back_pix, fore_pix;
67 char *Geometry = "";
68 Window iconwin, win;
69 GC NormalGC;
70 XpmIcon wmgen;
71 Pixmap pixmask;
73 /*****************/
74 /* Mouse Regions */
75 /*****************/
77 typedef struct {
78 int enable;
79 int top;
80 int bottom;
81 int left;
82 int right;
83 } MOUSE_REGION;
85 MOUSE_REGION mouse_region[MAX_MOUSE_REGION];
87 /***********************/
88 /* Function Prototypes */
89 /***********************/
91 static void GetXPM(XpmIcon *, char **);
92 static Pixel GetColor(char *);
93 void RedrawWindow(void);
94 void AddMouseRegion(int, int, int, int, int);
95 int CheckMouseRegion(int, int);
97 /*******************************************************************************\
98 |* parse_rcfile *|
99 \*******************************************************************************/
101 void parse_rcfile(const char *filename, rckeys *keys) {
103 char *p,*q;
104 char temp[128];
105 char *tokens = " :\t\n";
106 FILE *fp;
107 int i,key;
109 fp = fopen(filename, "r");
110 if (fp) {
111 while (fgets(temp, 128, fp)) {
112 key = 0;
113 q = strdup(temp);
114 q = strtok(q, tokens);
115 while (key >= 0 && keys[key].label) {
116 if ((!strcmp(q, keys[key].label))) {
117 p = strstr(temp, keys[key].label);
118 p += strlen(keys[key].label);
119 p += strspn(p, tokens);
120 if ((i = strcspn(p, "#\n"))) p[i] = 0;
121 free(*keys[key].var);
122 *keys[key].var = strdup(p);
123 key = -1;
124 } else key++;
126 free(q);
128 fclose(fp);
132 /*******************************************************************************\
133 |* parse_rcfile2 *|
134 \*******************************************************************************/
136 void parse_rcfile2(const char *filename, rckeys2 *keys) {
138 char *p;
139 char *line = NULL;
140 size_t line_size = 0;
141 char *tokens = " :\t\n";
142 FILE *fp;
143 int i,key;
144 char *family = NULL;
146 fp = fopen(filename, "r");
147 if (fp) {
148 while (getline(&line, &line_size, fp) >= 0) {
149 key = 0;
150 while (key >= 0 && keys[key].label) {
151 if ((p = strstr(line, keys[key].label))) {
152 p += strlen(keys[key].label);
153 p += strspn(p, tokens);
154 if ((i = strcspn(p, "#\n"))) p[i] = 0;
155 free(*keys[key].var);
156 *keys[key].var = strdup(p);
157 key = -1;
158 } else key++;
161 fclose(fp);
163 free(family);
167 /*******************************************************************************\
168 |* GetXPM *|
169 \*******************************************************************************/
171 static void GetXPM(XpmIcon *wmgen, char *pixmap_bytes[]) {
173 XWindowAttributes attributes;
174 int err;
176 /* For the colormap */
177 XGetWindowAttributes(display, Root, &attributes);
179 wmgen->attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
181 err = XpmCreatePixmapFromData(display, Root, pixmap_bytes, &(wmgen->pixmap),
182 &(wmgen->mask), &(wmgen->attributes));
184 if (err != XpmSuccess) {
185 fprintf(stderr, "Not enough free colorcells.\n");
186 exit(1);
190 /*******************************************************************************\
191 |* GetColor *|
192 \*******************************************************************************/
194 static Pixel GetColor(char *name) {
196 XColor color;
197 XWindowAttributes attributes;
199 XGetWindowAttributes(display, Root, &attributes);
201 color.pixel = 0;
202 if (!XParseColor(display, attributes.colormap, name, &color)) {
203 fprintf(stderr, "wm.app: can't parse %s.\n", name);
204 } else if (!XAllocColor(display, attributes.colormap, &color)) {
205 fprintf(stderr, "wm.app: can't allocate %s.\n", name);
207 return color.pixel;
210 /*******************************************************************************\
211 |* flush_expose *|
212 \*******************************************************************************/
214 static int flush_expose(Window w) {
216 XEvent dummy;
217 int i=0;
219 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
220 i++;
222 return i;
225 /*******************************************************************************\
226 |* RedrawWindow *|
227 \*******************************************************************************/
229 void RedrawWindow(void) {
231 flush_expose(iconwin);
232 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
233 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
234 flush_expose(win);
235 XCopyArea(display, wmgen.pixmap, win, NormalGC,
236 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
239 /*******************************************************************************\
240 |* RedrawWindowXY *|
241 \*******************************************************************************/
243 void RedrawWindowXY(int x, int y) {
245 flush_expose(iconwin);
246 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
247 x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
248 flush_expose(win);
249 XCopyArea(display, wmgen.pixmap, win, NormalGC,
250 x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
253 /*******************************************************************************\
254 |* AddMouseRegion *|
255 \*******************************************************************************/
257 void AddMouseRegion(int index, int left, int top, int right, int bottom) {
259 if (index < MAX_MOUSE_REGION) {
260 mouse_region[index].enable = 1;
261 mouse_region[index].top = top;
262 mouse_region[index].left = left;
263 mouse_region[index].bottom = bottom;
264 mouse_region[index].right = right;
268 /*******************************************************************************\
269 |* CheckMouseRegion *|
270 \*******************************************************************************/
272 int CheckMouseRegion(int x, int y) {
274 int i;
275 int found;
277 found = 0;
279 for (i=0; i<MAX_MOUSE_REGION && !found; i++) {
280 if (mouse_region[i].enable &&
281 x <= mouse_region[i].right &&
282 x >= mouse_region[i].left &&
283 y <= mouse_region[i].bottom &&
284 y >= mouse_region[i].top)
285 found = 1;
287 if (!found) return -1;
288 return (i-1);
291 /*******************************************************************************\
292 |* createXBMfromXPM *|
293 \*******************************************************************************/
294 void createXBMfromXPM(char *xbm, char **xpm, int sx, int sy) {
296 int i,j,k;
297 int width, height, numcol, depth;
298 int zero=0;
299 unsigned char bwrite;
300 int bcount;
301 int curpixel;
303 sscanf(*xpm, "%d %d %d %d", &width, &height, &numcol, &depth);
306 for (k=0; k!=depth; k++)
308 zero <<=8;
309 zero |= xpm[1][k];
312 for (i=numcol+1; i < numcol+sy+1; i++) {
313 bcount = 0;
314 bwrite = 0;
315 for (j=0; j<sx*depth; j+=depth) {
316 bwrite >>= 1;
318 curpixel=0;
319 for (k=0; k!=depth; k++)
321 curpixel <<=8;
322 curpixel |= xpm[i][j+k];
325 if ( curpixel != zero ) {
326 bwrite += 128;
328 bcount++;
329 if (bcount == 8) {
330 *xbm = bwrite;
331 xbm++;
332 bcount = 0;
333 bwrite = 0;
339 /*******************************************************************************\
340 |* copyXPMArea *|
341 \*******************************************************************************/
343 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
345 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
349 /*******************************************************************************\
350 |* copyXBMArea *|
351 \*******************************************************************************/
353 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
355 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
359 /*******************************************************************************\
360 |* setMaskXY *|
361 \*******************************************************************************/
363 void setMaskXY(int x, int y) {
365 XShapeCombineMask(display, win, ShapeBounding, x, y, pixmask, ShapeSet);
366 XShapeCombineMask(display, iconwin, ShapeBounding, x, y, pixmask, ShapeSet);
369 /*******************************************************************************\
370 |* openXwindow *|
371 \*******************************************************************************/
372 void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits, int pixmask_width, int pixmask_height) {
374 unsigned int borderwidth = 1;
375 XClassHint classHint;
376 char *display_name = NULL;
377 char *wname = argv[0];
378 XTextProperty name;
380 XGCValues gcv;
381 unsigned long gcm;
383 char *geometry = NULL;
385 int dummy=0;
386 int i, wx, wy;
388 for (i=1; argv[i]; i++) {
389 if (!strcmp(argv[i], "-display"))
390 display_name = argv[++i];
391 else if (!strcmp(argv[i], "-geometry"))
392 geometry = argv[++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);
418 if (geometry)
419 XParseGeometry(geometry, &mysizehints.x, &mysizehints.y,
420 (unsigned int *) &mysizehints.width, (unsigned int *) &mysizehints.height);
422 mysizehints.width = 64;
423 mysizehints.height = 64;
425 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
426 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
428 iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
429 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
431 /* Activate hints */
432 XSetWMNormalHints(display, win, &mysizehints);
433 classHint.res_name = wname;
434 classHint.res_class = wname;
435 XSetClassHint(display, win, &classHint);
437 XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
438 XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
440 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
441 fprintf(stderr, "%s: can't allocate window name\n", wname);
442 exit(1);
445 XSetWMName(display, win, &name);
447 /* Create GC for drawing */
449 gcm = GCForeground | GCBackground | GCGraphicsExposures;
450 gcv.foreground = fore_pix;
451 gcv.background = back_pix;
452 gcv.graphics_exposures = 0;
453 NormalGC = XCreateGC(display, Root, gcm, &gcv);
455 /* ONLYSHAPE ON */
457 pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
459 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
460 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
462 /* ONLYSHAPE OFF */
464 mywmhints.initial_state = WithdrawnState;
465 mywmhints.icon_window = iconwin;
466 mywmhints.icon_x = mysizehints.x;
467 mywmhints.icon_y = mysizehints.y;
468 mywmhints.window_group = win;
469 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
471 XSetWMHints(display, win, &mywmhints);
473 XSetCommand(display, win, argv, argc);
474 XMapWindow(display, win);
476 if (geometry) {
477 if (sscanf(geometry, "+%d+%d", &wx, &wy) != 2) {
478 fprintf(stderr, "Bad geometry string.\n");
479 exit(1);
481 XMoveWindow(display, win, wx, wy);