wmclockmon: update change-log
[dockapps.git] / wmpower / src / dockapp / dockapp.c
bloba5f3c3acbdb929db0e827ccc6668a5abd0d1b1a0
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 02/05/1998 (Martijn Pieterse, pieterse@xs4all.nl)
16 * changed the read_rc_file to parse_rcfile, as suggester by Marcelo E. Magallon
17 * debugged the parse_rc file.
18 30/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
19 * Ripped similar code from all the wm* programs,
20 and put them in a single file.
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <ctype.h>
29 #include <stdarg.h>
31 #include <X11/Xlib.h>
32 #include <X11/xpm.h>
33 #include <X11/extensions/shape.h>
35 #include "dockapp.h"
37 /*****************/
38 /* X11 Variables */
39 /*****************/
41 Window Root;
42 int screen;
43 int x_fd;
44 int d_depth;
45 XSizeHints mysizehints;
46 XWMHints mywmhints;
47 Pixel back_pix, fore_pix;
48 char *Geometry = "";
49 Window iconwin, win;
50 GC NormalGC;
51 XpmIcon wmgen;
52 Pixmap pixmask;
54 /*****************/
55 /* Mouse Regions */
56 /*****************/
58 typedef struct
60 int enable;
61 int top;
62 int bottom;
63 int left;
64 int right;
65 } MOUSE_REGION;
67 #define MAX_MOUSE_REGION (8)
68 MOUSE_REGION mouse_region[MAX_MOUSE_REGION];
70 /***********************/
71 /* Function Prototypes */
72 /***********************/
74 static void GetXPM(XpmIcon *, char **);
75 static Pixel GetColor(char *);
76 void RedrawWindow(void);
77 void AddMouseRegion(unsigned, int, int, int, int);
78 int CheckMouseRegion(int, int);
80 /*******************************************************************************\
81 |* read_rc_file *|
82 \*******************************************************************************/
84 void parse_rcfile(const char *filename, rckeys *keys)
86 char *p;
87 char temp[128];
88 char *tokens = " :\t\n";
89 FILE *fp;
90 int i,key;
92 fp = fopen(filename, "r");
93 if (fp) {
94 while (fgets(temp, 128, fp)) {
95 key = 0;
96 while (key >= 0 && keys[key].label) {
97 if ((p = strstr(temp, keys[key].label))) {
98 p += strlen(keys[key].label);
99 p += strspn(p, tokens);
100 if ((i = strcspn(p, "#\n"))) p[i] = 0;
101 free(*keys[key].var);
102 *keys[key].var = strdup(p);
103 key = -1;
104 } else key++;
107 fclose(fp);
112 /*******************************************************************************\
113 |* GetXPM *|
114 \*******************************************************************************/
116 static void GetXPM(XpmIcon *wmgen, char *pixmap_bytes[])
118 XWindowAttributes attributes;
119 int err;
121 /* For the colormap */
122 XGetWindowAttributes(display, Root, &attributes);
124 wmgen->attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
126 err = XpmCreatePixmapFromData(display, Root, pixmap_bytes, &(wmgen->pixmap),
127 &(wmgen->mask), &(wmgen->attributes));
129 if (err != XpmSuccess) {
130 fprintf(stderr, "Not enough free colorcells.\n");
131 exit(1);
135 /*******************************************************************************\
136 |* GetColor *|
137 \*******************************************************************************/
139 static Pixel GetColor(char *name)
141 XColor color;
142 XWindowAttributes attributes;
144 XGetWindowAttributes(display, Root, &attributes);
146 color.pixel = 0;
147 if (!XParseColor(display, attributes.colormap, name, &color)) {
148 fprintf(stderr, "wm.app: can't parse %s.\n", name);
149 } else if (!XAllocColor(display, attributes.colormap, &color)) {
150 fprintf(stderr, "wm.app: can't allocate %s.\n", name);
152 return color.pixel;
155 /*******************************************************************************\
156 |* flush_expose *|
157 \*******************************************************************************/
159 static int flush_expose(Window w)
161 XEvent dummy;
162 int i=0;
164 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
165 i++;
167 return i;
170 /*******************************************************************************\
171 |* RedrawWindow *|
172 \*******************************************************************************/
174 void RedrawWindow(void)
176 flush_expose(iconwin);
177 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
178 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
179 flush_expose(win);
180 XCopyArea(display, wmgen.pixmap, win, NormalGC,
181 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
184 /*******************************************************************************\
185 |* RedrawWindowXY *|
186 \*******************************************************************************/
188 void RedrawWindowXY(int x, int y)
190 flush_expose(iconwin);
191 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
192 x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
193 flush_expose(win);
194 XCopyArea(display, wmgen.pixmap, win, NormalGC,
195 x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
198 /*******************************************************************************\
199 |* AddMouseRegion *|
200 \*******************************************************************************/
202 void AddMouseRegion(unsigned index, int left, int top, int right, int bottom)
204 if (index < MAX_MOUSE_REGION)
206 mouse_region[index].enable = 1;
207 mouse_region[index].top = top;
208 mouse_region[index].left = left;
209 mouse_region[index].bottom = bottom;
210 mouse_region[index].right = right;
214 /*******************************************************************************\
215 |* CheckMouseRegion *|
216 \*******************************************************************************/
218 int CheckMouseRegion(int x, int y)
220 int i;
221 int found;
223 found = 0;
225 for (i=0; i<MAX_MOUSE_REGION && !found; i++)
227 if (mouse_region[i].enable &&
228 x <= mouse_region[i].right &&
229 x >= mouse_region[i].left &&
230 y <= mouse_region[i].bottom &&
231 y >= mouse_region[i].top)
232 found = 1;
234 if (!found) return -1;
235 return (i-1);
238 /*******************************************************************************\
239 |* copyXPMArea *|
240 \*******************************************************************************/
242 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy)
244 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
247 /*******************************************************************************\
248 |* copyXBMArea *|
249 \*******************************************************************************/
251 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy)
253 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
257 /*******************************************************************************\
258 |* setMaskXY *|
259 \*******************************************************************************/
261 void setMaskXY(int x, int y)
263 XShapeCombineMask(display, win, ShapeBounding, x, y, pixmask, ShapeSet);
264 XShapeCombineMask(display, iconwin, ShapeBounding, x, y, pixmask, ShapeSet);
267 /*******************************************************************************\
268 |* openXwindow *|
269 \*******************************************************************************/
270 void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits, int pixmask_width, int pixmask_height)
272 unsigned int borderwidth = 1;
273 XClassHint classHint;
274 char *display_name = NULL;
275 char *wname = argv[0];
276 XTextProperty name;
277 XGCValues gcv;
278 unsigned long gcm;
280 int dummy=0;
281 int i;
283 for (i=1; argv[i]; i++)
285 if (!strcmp(argv[i], "-display")) display_name = argv[i+1];
288 if (!(display = XOpenDisplay(display_name)))
290 fprintf(stderr, "%s: can't open display %s\n", wname, XDisplayName(display_name));
291 exit(1);
293 screen = DefaultScreen(display);
294 Root = RootWindow(display, screen);
295 d_depth = DefaultDepth(display, screen);
296 x_fd = XConnectionNumber(display);
298 /* Convert XPM to XImage */
299 GetXPM(&wmgen, pixmap_bytes);
301 /* Create a window to hold the stuff */
302 mysizehints.flags = USSize | USPosition;
303 mysizehints.x = 0;
304 mysizehints.y = 0;
306 back_pix = GetColor("white");
307 fore_pix = GetColor("black");
309 XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
310 &mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
312 mysizehints.width = 64;
313 mysizehints.height = 64;
315 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
316 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
318 iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
319 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
321 /* Activate hints */
322 XSetWMNormalHints(display, win, &mysizehints);
323 classHint.res_name = wname;
324 classHint.res_class = wname;
325 XSetClassHint(display, win, &classHint);
327 XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
328 XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
330 if (XStringListToTextProperty(&wname, 1, &name) == 0)
332 fprintf(stderr, "%s: can't allocate window name\n", wname);
333 exit(1);
336 XSetWMName(display, win, &name);
338 /* Create GC for drawing */
340 gcm = GCForeground | GCBackground | GCGraphicsExposures;
341 gcv.foreground = fore_pix;
342 gcv.background = back_pix;
343 gcv.graphics_exposures = 0;
344 NormalGC = XCreateGC(display, Root, gcm, &gcv);
346 /* ONLYSHAPE ON */
348 pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
350 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
351 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
353 /* ONLYSHAPE OFF */
355 mywmhints.initial_state = WithdrawnState;
356 mywmhints.icon_window = iconwin;
357 mywmhints.icon_x = mysizehints.x;
358 mywmhints.icon_y = mysizehints.y;
359 mywmhints.window_group = win;
360 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
362 XSetWMHints(display, win, &mywmhints);
364 XSetCommand(display, win, argv, argc);
365 XMapWindow(display, win);