wmressel: Use automake.
[dockapps.git] / wmressel / src / wmgeneral / wmgeneral.c
blob0f221d56dccf2537b05ee3357564da015e9c83bd
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 "wmgeneral.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 {
59 int enable;
60 int top;
61 int bottom;
62 int left;
63 int right;
64 } MOUSE_REGION;
66 MOUSE_REGION mouse_region[MAX_MOUSE_REGION];
68 /***********************/
69 /* Function Prototypes */
70 /***********************/
72 static void GetXPM(XpmIcon *, char **);
73 static Pixel GetColor(char *);
74 void RedrawWindow(void);
75 void AddMouseRegion(int, int, int, int, int);
76 int CheckMouseRegion(int, int);
78 /*******************************************************************************\
79 |* read_rc_file *|
80 \*******************************************************************************/
82 void parse_rcfile(const char *filename, rckeys *keys) {
84 char *p;
85 char temp[128];
86 char *tokens = " :\t\n";
87 FILE *fp;
88 int i,key;
90 fp = fopen(filename, "r");
91 if (fp) {
92 while (fgets(temp, 128, fp)) {
93 key = 0;
94 while (key >= 0 && keys[key].label) {
95 if ((p = strstr(temp, keys[key].label))) {
96 p += strlen(keys[key].label);
97 p += strspn(p, tokens);
98 if ((i = strcspn(p, "#\n"))) p[i] = 0;
99 free(*keys[key].var);
100 *keys[key].var = strdup(p);
101 key = -1;
102 } else key++;
105 fclose(fp);
110 /*******************************************************************************\
111 |* GetXPM *|
112 \*******************************************************************************/
114 static void GetXPM(XpmIcon *wmgen, char *pixmap_bytes[]) {
116 XWindowAttributes attributes;
117 int err;
119 /* For the colormap */
120 XGetWindowAttributes(display, Root, &attributes);
122 wmgen->attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
124 err = XpmCreatePixmapFromData(display, Root, pixmap_bytes, &(wmgen->pixmap),
125 &(wmgen->mask), &(wmgen->attributes));
127 if (err != XpmSuccess) {
128 fprintf(stderr, "Not enough free colorcells.\n");
129 exit(1);
133 /*******************************************************************************\
134 |* GetColor *|
135 \*******************************************************************************/
137 static Pixel GetColor(char *name) {
139 XColor color;
140 XWindowAttributes attributes;
142 XGetWindowAttributes(display, Root, &attributes);
144 color.pixel = 0;
145 if (!XParseColor(display, attributes.colormap, name, &color)) {
146 fprintf(stderr, "wm.app: can't parse %s.\n", name);
147 } else if (!XAllocColor(display, attributes.colormap, &color)) {
148 fprintf(stderr, "wm.app: can't allocate %s.\n", name);
150 return color.pixel;
153 /*******************************************************************************\
154 |* flush_expose *|
155 \*******************************************************************************/
157 static int flush_expose(Window w) {
159 XEvent dummy;
160 int i=0;
162 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
163 i++;
165 return i;
168 /*******************************************************************************\
169 |* RedrawWindow *|
170 \*******************************************************************************/
172 void RedrawWindow(void) {
174 flush_expose(iconwin);
175 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
176 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
177 flush_expose(win);
178 XCopyArea(display, wmgen.pixmap, win, NormalGC,
179 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
182 /*******************************************************************************\
183 |* RedrawWindowXY *|
184 \*******************************************************************************/
186 void RedrawWindowXY(int x, int y) {
188 flush_expose(iconwin);
189 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
190 x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
191 flush_expose(win);
192 XCopyArea(display, wmgen.pixmap, win, NormalGC,
193 x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
196 /*******************************************************************************\
197 |* AddMouseRegion *|
198 \*******************************************************************************/
200 void AddMouseRegion(int index, int left, int top, int right, int bottom) {
202 if (index < MAX_MOUSE_REGION) {
203 mouse_region[index].enable = 1;
204 mouse_region[index].top = top;
205 mouse_region[index].left = left;
206 mouse_region[index].bottom = bottom;
207 mouse_region[index].right = right;
211 /*******************************************************************************\
212 |* CheckMouseRegion *|
213 \*******************************************************************************/
215 int CheckMouseRegion(int x, int y) {
217 int i;
218 int found;
220 found = 0;
222 for (i=0; i<MAX_MOUSE_REGION && !found; i++) {
223 if (mouse_region[i].enable &&
224 x <= mouse_region[i].right &&
225 x >= mouse_region[i].left &&
226 y <= mouse_region[i].bottom &&
227 y >= mouse_region[i].top)
228 found = 1;
230 if (!found) return -1;
231 return (i-1);
234 /*******************************************************************************\
235 |* copyXPMArea *|
236 \*******************************************************************************/
238 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
240 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
244 /*******************************************************************************\
245 |* copyXBMArea *|
246 \*******************************************************************************/
248 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
250 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
254 /*******************************************************************************\
255 |* setMaskXY *|
256 \*******************************************************************************/
258 void setMaskXY(int x, int y) {
260 XShapeCombineMask(display, win, ShapeBounding, x, y, pixmask, ShapeSet);
261 XShapeCombineMask(display, iconwin, ShapeBounding, x, y, pixmask, ShapeSet);
264 /*******************************************************************************\
265 |* openXwindow *|
266 \*******************************************************************************/
267 void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits, int pixmask_width, int pixmask_height) {
269 unsigned int borderwidth = 1;
270 XClassHint classHint;
271 char *display_name = NULL;
272 char *wname = argv[0];
273 XTextProperty name;
275 XGCValues gcv;
276 unsigned long gcm;
279 int dummy=0;
280 int i;
282 for (i=1; argv[i]; i++) {
283 if (!strcmp(argv[i], "--display"))
284 display_name = argv[i+1];
287 if (!(display = XOpenDisplay(display_name))) {
288 fprintf(stderr, "%s: can't open display %s\n",
289 wname, XDisplayName(display_name));
290 exit(1);
292 screen = DefaultScreen(display);
293 Root = RootWindow(display, screen);
294 d_depth = DefaultDepth(display, screen);
295 x_fd = XConnectionNumber(display);
297 /* Convert XPM to XImage */
298 GetXPM(&wmgen, pixmap_bytes);
300 /* Create a window to hold the stuff */
301 mysizehints.flags = USSize | USPosition;
302 mysizehints.x = 0;
303 mysizehints.y = 0;
305 back_pix = GetColor("white");
306 fore_pix = GetColor("black");
308 XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
309 &mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
311 mysizehints.width = 64;
312 mysizehints.height = 64;
314 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
315 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
317 iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
318 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
320 /* Activate hints */
321 XSetWMNormalHints(display, win, &mysizehints);
322 classHint.res_name = wname;
323 classHint.res_class = wname;
324 XSetClassHint(display, win, &classHint);
326 XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
327 XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
329 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
330 fprintf(stderr, "%s: can't allocate window name\n", wname);
331 exit(1);
334 XSetWMName(display, win, &name);
336 /* Create GC for drawing */
338 gcm = GCForeground | GCBackground | GCGraphicsExposures;
339 gcv.foreground = fore_pix;
340 gcv.background = back_pix;
341 gcv.graphics_exposures = 0;
342 NormalGC = XCreateGC(display, Root, gcm, &gcv);
344 /* ONLYSHAPE ON */
346 pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
348 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
349 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
351 /* ONLYSHAPE OFF */
353 mywmhints.initial_state = WithdrawnState;
354 mywmhints.icon_window = iconwin;
355 mywmhints.icon_x = mysizehints.x;
356 mywmhints.icon_y = mysizehints.y;
357 mywmhints.window_group = win;
358 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
360 XSetWMHints(display, win, &mywmhints);
362 XSetCommand(display, win, argv, argc);
363 XMapWindow(display, win);