wmgtemp: Add verson 1.1 to repository.
[dockapps.git] / wmgtemp / src / wmgeneral / wmgeneral.c
blob30c39bf35595191a9a828973fb61d82c22a05b3b
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, *t;
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 t = strtok(q, tokens);
113 if(t) {
114 while (key >= 0 && keys[key].label) {
115 if ((!strcmp(t, keys[key].label))) {
116 p = strstr(temp, keys[key].label);
117 p += strlen(keys[key].label);
118 p += strspn(p, tokens);
119 if ((i = strcspn(p, "#\n"))) p[i] = 0;
120 free(*keys[key].var);
121 *keys[key].var = strdup(p);
122 key = -1;
123 } 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 temp[128];
140 char *tokens = " :\t\n";
141 FILE *fp;
142 int i,key;
143 char *family = NULL;
145 fp = fopen(filename, "r");
146 if (fp) {
147 while (fgets(temp, 128, fp)) {
148 key = 0;
149 while (key >= 0 && keys[key].label) {
150 if ((p = strstr(temp, keys[key].label))) {
151 p += strlen(keys[key].label);
152 p += strspn(p, tokens);
153 if ((i = strcspn(p, "#\n"))) p[i] = 0;
154 free(*keys[key].var);
155 *keys[key].var = strdup(p);
156 key = -1;
157 } else key++;
160 fclose(fp);
162 free(family);
166 /*******************************************************************************\
167 |* GetXPM *|
168 \*******************************************************************************/
170 static void GetXPM(XpmIcon *wmgen, char *pixmap_bytes[]) {
172 XWindowAttributes attributes;
173 int err;
175 /* For the colormap */
176 XGetWindowAttributes(display, Root, &attributes);
178 wmgen->attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
180 err = XpmCreatePixmapFromData(display, Root, pixmap_bytes, &(wmgen->pixmap),
181 &(wmgen->mask), &(wmgen->attributes));
183 if (err != XpmSuccess) {
184 fprintf(stderr, "Not enough free colorcells.\n");
185 exit(1);
189 /*******************************************************************************\
190 |* GetColor *|
191 \*******************************************************************************/
193 static Pixel GetColor(char *name) {
195 XColor color;
196 XWindowAttributes attributes;
198 XGetWindowAttributes(display, Root, &attributes);
200 color.pixel = 0;
201 if (!XParseColor(display, attributes.colormap, name, &color)) {
202 fprintf(stderr, "wm.app: can't parse %s.\n", name);
203 } else if (!XAllocColor(display, attributes.colormap, &color)) {
204 fprintf(stderr, "wm.app: can't allocate %s.\n", name);
206 return color.pixel;
209 /*******************************************************************************\
210 |* flush_expose *|
211 \*******************************************************************************/
213 static int flush_expose(Window w) {
215 XEvent dummy;
216 int i=0;
218 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
219 i++;
221 return i;
224 /*******************************************************************************\
225 |* RedrawWindow *|
226 \*******************************************************************************/
228 void RedrawWindow(void) {
230 flush_expose(iconwin);
231 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
232 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
233 flush_expose(win);
234 XCopyArea(display, wmgen.pixmap, win, NormalGC,
235 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
238 /*******************************************************************************\
239 |* RedrawWindowXY *|
240 \*******************************************************************************/
242 void RedrawWindowXY(int x, int y) {
244 flush_expose(iconwin);
245 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
246 x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
247 flush_expose(win);
248 XCopyArea(display, wmgen.pixmap, win, NormalGC,
249 x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
252 /*******************************************************************************\
253 |* AddMouseRegion *|
254 \*******************************************************************************/
256 void AddMouseRegion(int index, int left, int top, int right, int bottom) {
258 if (index < MAX_MOUSE_REGION) {
259 mouse_region[index].enable = 1;
260 mouse_region[index].top = top;
261 mouse_region[index].left = left;
262 mouse_region[index].bottom = bottom;
263 mouse_region[index].right = right;
267 /*******************************************************************************\
268 |* CheckMouseRegion *|
269 \*******************************************************************************/
271 int CheckMouseRegion(int x, int y) {
273 int i;
274 int found;
276 found = 0;
278 for (i=0; i<MAX_MOUSE_REGION && !found; i++) {
279 if (mouse_region[i].enable &&
280 x <= mouse_region[i].right &&
281 x >= mouse_region[i].left &&
282 y <= mouse_region[i].bottom &&
283 y >= mouse_region[i].top)
284 found = 1;
286 if (!found) return -1;
287 return (i-1);
290 /*******************************************************************************\
291 |* createXBMfromXPM *|
292 \*******************************************************************************/
293 void createXBMfromXPM(char *xbm, char **xpm, int sx, int sy) {
295 int i,j,k;
296 int width, height, numcol, depth;
297 int zero=0;
298 unsigned char bwrite;
299 int bcount;
300 int curpixel;
302 sscanf(*xpm, "%d %d %d %d", &width, &height, &numcol, &depth);
305 for (k=0; k!=depth; k++)
307 zero <<=8;
308 zero |= xpm[1][k];
311 for (i=numcol+1; i < numcol+sy+1; i++) {
312 bcount = 0;
313 bwrite = 0;
314 for (j=0; j<sx*depth; j+=depth) {
315 bwrite >>= 1;
317 curpixel=0;
318 for (k=0; k!=depth; k++)
320 curpixel <<=8;
321 curpixel |= xpm[i][j+k];
324 if ( curpixel != zero ) {
325 bwrite += 128;
327 bcount++;
328 if (bcount == 8) {
329 *xbm = bwrite;
330 xbm++;
331 bcount = 0;
332 bwrite = 0;
338 /*******************************************************************************\
339 |* copyXPMArea *|
340 \*******************************************************************************/
342 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
344 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
348 /*******************************************************************************\
349 |* copyXBMArea *|
350 \*******************************************************************************/
352 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
354 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
358 /*******************************************************************************\
359 |* setMaskXY *|
360 \*******************************************************************************/
362 void setMaskXY(int x, int y) {
364 XShapeCombineMask(display, win, ShapeBounding, x, y, pixmask, ShapeSet);
365 XShapeCombineMask(display, iconwin, ShapeBounding, x, y, pixmask, ShapeSet);
368 /*******************************************************************************\
369 |* openXwindow *|
370 \*******************************************************************************/
371 void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits, int pixmask_width, int pixmask_height) {
373 unsigned int borderwidth = 1;
374 XClassHint classHint;
375 char *display_name = NULL;
376 char *wname = argv[0];
377 XTextProperty name;
379 XGCValues gcv;
380 unsigned long gcm;
382 char *geometry = NULL;
384 int dummy=0;
385 int i, wx, wy;
387 for (i=1; argv[i]; i++) {
388 if (!strcmp(argv[i], "-display")) {
389 display_name = argv[i+1];
390 i++;
392 if (!strcmp(argv[i], "-geometry")) {
393 geometry = argv[i+1];
394 i++;
398 if (!(display = XOpenDisplay(display_name))) {
399 fprintf(stderr, "%s: can't open display %s\n",
400 wname, XDisplayName(display_name));
401 exit(1);
403 screen = DefaultScreen(display);
404 Root = RootWindow(display, screen);
405 d_depth = DefaultDepth(display, screen);
406 x_fd = XConnectionNumber(display);
408 /* Convert XPM to XImage */
409 GetXPM(&wmgen, pixmap_bytes);
411 /* Create a window to hold the stuff */
412 mysizehints.flags = USSize | USPosition;
413 mysizehints.x = 0;
414 mysizehints.y = 0;
416 back_pix = GetColor("white");
417 fore_pix = GetColor("black");
419 XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
420 &mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
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);