wmclockmon: update change-log
[dockapps.git] / libdockapp / src / wmgeneral.c
blob9f3e1fa467a6862ada0d9345d8c29e4ce4d97202
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 #define _POSIX_C_SOURCE 200809L
62 #include "wmgeneral.h"
63 #include <X11/Xlib.h> /* for XCopyArea, etc */
64 #include <X11/Xutil.h> /* for XSizeHints, XWMHints, etc */
65 #include <X11/extensions/shape.h> /* for XShapeCombineMask */
66 #include <X11/extensions/shapeconst.h> /* for ShapeBounding, ShapeSet */
67 #include <X11/xpm.h> /* for XpmAttributes, Pixel, etc */
68 #include <stddef.h> /* for size_t */
69 #include <stdio.h> /* for fprintf, stderr, NULL, etc */
70 #include <stdlib.h> /* for exit, free */
71 #include <string.h> /* for strcmp, strdup, strcspn, etc */
72 #include <libgen.h> /* for basename */
74 /*****************/
75 /* X11 Variables */
76 /*****************/
78 Display *display;
79 Window Root;
80 int screen;
81 int x_fd;
82 int d_depth;
83 XSizeHints mysizehints;
84 XWMHints mywmhints;
85 Pixel back_pix, fore_pix;
86 Window iconwin, win;
87 GC NormalGC;
88 XpmIcon wmgen;
89 Pixmap pixmask;
91 /*****************/
92 /* Mouse Regions */
93 /*****************/
95 typedef struct {
96 int enable;
97 int top;
98 int bottom;
99 int left;
100 int right;
101 } MOUSE_REGION;
103 MOUSE_REGION mouse_region[MAX_MOUSE_REGION];
105 /***********************/
106 /* Function Prototypes */
107 /***********************/
109 static void GetXPM(XpmIcon *, char **);
110 static Pixel GetColor(char *);
111 void RedrawWindow(void);
112 void AddMouseRegion(unsigned, int, int, int, int);
113 int CheckMouseRegion(int, int);
115 /*******************************************************************************\
116 |* parse_rcfile *|
117 \*******************************************************************************/
119 void parse_rcfile(const char *filename, rckeys *keys) {
121 char *p;
122 FILE *fp;
124 fp = fopen(filename, "r");
125 if (fp) {
126 char temp[128];
128 while (fgets(temp, 128, fp)) {
129 char *q, *saveptr;
130 char *tokens = " :\t\n";
131 int key;
133 key = 0;
134 q = strdup(temp);
135 q = strtok_r(q, tokens, &saveptr);
136 if(!q)
137 continue;
138 while (key >= 0 && keys[key].label) {
139 if ((!strcmp(q, keys[key].label))) {
140 int i;
142 p = strstr(temp, keys[key].label);
143 p += strlen(keys[key].label);
144 p += strspn(p, tokens);
145 if ((i = strcspn(p, "#\n"))) p[i] = '\0';
146 *keys[key].var = strdup(p);
147 key = -1;
148 } else key++;
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;
166 fp = fopen(filename, "r");
167 if (fp) {
168 while (getline(&line, &line_size, fp) >= 0) {
169 int key;
171 key = 0;
172 while (key >= 0 && keys[key].label) {
173 if ((p = strstr(line, keys[key].label))) {
174 char *tokens = " :\t\n";
175 int i;
177 p += strlen(keys[key].label);
178 p += strspn(p, tokens);
179 if ((i = strcspn(p, "#\n"))) p[i] = 0;
180 *keys[key].var = strdup(p);
181 key = -1;
182 } else key++;
185 fclose(fp);
190 /*******************************************************************************\
191 |* GetXPM *|
192 \*******************************************************************************/
194 static void GetXPM(XpmIcon *wmgen, char *pixmap_bytes[]) {
196 XWindowAttributes attributes;
197 int err;
199 /* For the colormap */
200 XGetWindowAttributes(display, Root, &attributes);
202 wmgen->attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
204 err = XpmCreatePixmapFromData(display, Root, pixmap_bytes, &(wmgen->pixmap),
205 &(wmgen->mask), &(wmgen->attributes));
207 if (err != XpmSuccess) {
208 fprintf(stderr, "Not enough free colorcells.\n");
209 exit(1);
213 /*******************************************************************************\
214 |* GetColor *|
215 \*******************************************************************************/
217 static Pixel GetColor(char *name) {
219 XColor color;
220 XWindowAttributes attributes;
222 XGetWindowAttributes(display, Root, &attributes);
224 color.pixel = 0;
225 if (!XParseColor(display, attributes.colormap, name, &color)) {
226 fprintf(stderr, "wm.app: can't parse %s.\n", name);
227 } else if (!XAllocColor(display, attributes.colormap, &color)) {
228 fprintf(stderr, "wm.app: can't allocate %s.\n", name);
230 return color.pixel;
233 /*******************************************************************************\
234 |* flush_expose *|
235 \*******************************************************************************/
237 static int flush_expose(Window w) {
239 XEvent dummy;
240 int i=0;
242 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
243 i++;
245 return i;
248 /*******************************************************************************\
249 |* RedrawWindow *|
250 \*******************************************************************************/
252 void RedrawWindow(void) {
254 flush_expose(iconwin);
255 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
256 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
257 flush_expose(win);
258 XCopyArea(display, wmgen.pixmap, win, NormalGC,
259 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
262 /*******************************************************************************\
263 |* RedrawWindowXY *|
264 \*******************************************************************************/
266 void RedrawWindowXY(int x, int y) {
268 flush_expose(iconwin);
269 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
270 x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
271 flush_expose(win);
272 XCopyArea(display, wmgen.pixmap, win, NormalGC,
273 x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
276 /*******************************************************************************\
277 |* AddMouseRegion *|
278 \*******************************************************************************/
280 void AddMouseRegion(unsigned index, int left, int top, int right, int bottom) {
282 if (index < MAX_MOUSE_REGION) {
283 mouse_region[index].enable = 1;
284 mouse_region[index].top = top;
285 mouse_region[index].left = left;
286 mouse_region[index].bottom = bottom;
287 mouse_region[index].right = right;
291 /*******************************************************************************\
292 |* CheckMouseRegion *|
293 \*******************************************************************************/
295 int CheckMouseRegion(int x, int y) {
297 int i;
298 int found;
300 found = 0;
302 for (i=0; i<MAX_MOUSE_REGION && !found; i++) {
303 if (mouse_region[i].enable &&
304 x <= mouse_region[i].right &&
305 x >= mouse_region[i].left &&
306 y <= mouse_region[i].bottom &&
307 y >= mouse_region[i].top)
308 found = 1;
310 if (!found) return -1;
311 return (i-1);
314 /*******************************************************************************\
315 |* createXBMfromXPM *|
316 \*******************************************************************************/
317 void createXBMfromXPM(char *xbm, char **xpm, int sx, int sy) {
319 int i,j,k;
320 int width, height, numcol, depth;
321 int zero=0;
322 int curpixel;
324 sscanf(*xpm, "%10d %10d %10d %10d", &width, &height, &numcol, &depth);
327 for (k=0; k!=depth; k++)
329 zero <<=8;
330 zero |= xpm[1][k];
333 for (i=numcol+1; i < numcol+sy+1; i++) {
334 unsigned char bwrite;
335 int bcount;
337 bcount = 0;
338 bwrite = 0;
339 for (j=0; j<sx*depth; j+=depth) {
340 bwrite >>= 1;
342 curpixel=0;
343 for (k=0; k!=depth; k++)
345 curpixel <<=8;
346 curpixel |= xpm[i][j+k];
349 if ( curpixel != zero ) {
350 bwrite += 128;
352 bcount++;
353 if (bcount == 8) {
354 *xbm = bwrite;
355 xbm++;
356 bcount = 0;
357 bwrite = 0;
363 /*******************************************************************************\
364 |* copyXPMArea *|
365 \*******************************************************************************/
367 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
369 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
373 /*******************************************************************************\
374 |* copyXBMArea *|
375 \*******************************************************************************/
377 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
379 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
383 /*******************************************************************************\
384 |* setMaskXY *|
385 \*******************************************************************************/
387 void setMaskXY(int x, int y) {
389 XShapeCombineMask(display, win, ShapeBounding, x, y, pixmask, ShapeSet);
390 XShapeCombineMask(display, iconwin, ShapeBounding, x, y, pixmask, ShapeSet);
393 /*******************************************************************************\
394 |* openXwindow *|
395 \*******************************************************************************/
396 void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits, int pixmask_width, int pixmask_height) {
398 unsigned int borderwidth = 1;
399 XClassHint classHint;
400 char *display_name = NULL;
401 char *wname = basename (argv[0]);
402 XTextProperty name;
404 XGCValues gcv;
405 unsigned long gcm;
407 char *geometry = NULL;
409 int dummy=0;
410 int i;
412 for (i=1; argv[i]; i++) {
413 if (!strcmp(argv[i], "-display"))
414 display_name = argv[++i];
415 else if (!strcmp(argv[i], "-geometry"))
416 geometry = argv[++i];
419 if (!(display = XOpenDisplay(display_name))) {
420 fprintf(stderr, "%s: can't open display %s\n",
421 wname, XDisplayName(display_name));
422 exit(1);
424 screen = DefaultScreen(display);
425 Root = RootWindow(display, screen);
426 d_depth = DefaultDepth(display, screen);
427 x_fd = XConnectionNumber(display);
429 /* Convert XPM to XImage */
430 GetXPM(&wmgen, pixmap_bytes);
432 /* Create a window to hold the stuff */
433 mysizehints.flags = USSize | USPosition;
434 mysizehints.x = 0;
435 mysizehints.y = 0;
437 back_pix = GetColor("white");
438 fore_pix = GetColor("black");
440 XWMGeometry(display, screen, geometry, NULL, borderwidth, &mysizehints,
441 &mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
443 mysizehints.width = 64;
444 mysizehints.height = 64;
446 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
447 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
449 iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
450 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
452 /* Activate hints */
453 XSetWMNormalHints(display, win, &mysizehints);
454 classHint.res_name = wname;
455 classHint.res_class = wname;
456 XSetClassHint(display, win, &classHint);
458 XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
459 XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
461 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
462 fprintf(stderr, "%s: can't allocate window name\n", wname);
463 exit(1);
466 XSetWMName(display, win, &name);
468 /* Create GC for drawing */
470 gcm = GCForeground | GCBackground | GCGraphicsExposures;
471 gcv.foreground = fore_pix;
472 gcv.background = back_pix;
473 gcv.graphics_exposures = 0;
474 NormalGC = XCreateGC(display, Root, gcm, &gcv);
476 /* ONLYSHAPE ON */
478 pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
480 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
481 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
483 /* ONLYSHAPE OFF */
485 mywmhints.initial_state = WithdrawnState;
486 mywmhints.icon_window = iconwin;
487 mywmhints.icon_x = mysizehints.x;
488 mywmhints.icon_y = mysizehints.y;
489 mywmhints.window_group = win;
490 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
492 XSetWMHints(display, win, &mywmhints);
494 XSetCommand(display, win, argv, argc);
495 XMapWindow(display, win);