wmifs: Update Makefile.
[dockapps.git] / wmkeys / wmgeneral / wmgeneral.c
blob645321c88a1b7d3e302e3ce570a2725fd62d2076
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 #define MAX_MOUSE_REGION (8)
67 MOUSE_REGION mouse_region[MAX_MOUSE_REGION];
69 /***********************/
70 /* Function Prototypes */
71 /***********************/
73 static void GetXPM(XpmIcon *, char **);
74 static Pixel GetColor(char *);
75 void RedrawWindow(void);
76 void AddMouseRegion(int, int, int, int, int);
77 int CheckMouseRegion(int, int);
79 /*******************************************************************************\
80 |* read_rc_file *|
81 \*******************************************************************************/
83 void parse_rcfile(const char *filename, rckeys *keys) {
85 char *p;
86 char temp[128];
87 char *tokens = " :\t\n";
88 FILE *fp;
89 int i,key;
91 fp = fopen(filename, "r");
92 if (fp) {
93 while (fgets(temp, 128, fp)) {
94 key = 0;
95 while (key >= 0 && keys[key].label) {
96 if ((p = strstr(temp, keys[key].label))) {
97 p += strlen(keys[key].label);
98 p += strspn(p, tokens);
99 if ((i = strcspn(p, "#\n"))) p[i] = 0;
100 free(*keys[key].var);
101 *keys[key].var = strdup(p);
102 key = -1;
103 } else key++;
106 fclose(fp);
111 /*******************************************************************************\
112 |* GetXPM *|
113 \*******************************************************************************/
115 static void GetXPM(XpmIcon *wmgen, char *pixmap_bytes[]) {
117 XWindowAttributes attributes;
118 int err;
120 /* For the colormap */
121 XGetWindowAttributes(display, Root, &attributes);
123 wmgen->attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
125 err = XpmCreatePixmapFromData(display, Root, pixmap_bytes, &(wmgen->pixmap),
126 &(wmgen->mask), &(wmgen->attributes));
128 if (err != XpmSuccess) {
129 fprintf(stderr, "Not enough free colorcells.\n");
130 exit(1);
134 /*******************************************************************************\
135 |* GetColor *|
136 \*******************************************************************************/
138 static Pixel GetColor(char *name) {
140 XColor color;
141 XWindowAttributes attributes;
143 XGetWindowAttributes(display, Root, &attributes);
145 color.pixel = 0;
146 if (!XParseColor(display, attributes.colormap, name, &color)) {
147 fprintf(stderr, "wm.app: can't parse %s.\n", name);
148 } else if (!XAllocColor(display, attributes.colormap, &color)) {
149 fprintf(stderr, "wm.app: can't allocate %s.\n", name);
151 return color.pixel;
154 /*******************************************************************************\
155 |* flush_expose *|
156 \*******************************************************************************/
158 static int flush_expose(Window w) {
160 XEvent dummy;
161 int i=0;
163 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
164 i++;
166 return i;
169 /*******************************************************************************\
170 |* RedrawWindow *|
171 \*******************************************************************************/
173 void RedrawWindow(void) {
175 flush_expose(iconwin);
176 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
177 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
178 flush_expose(win);
179 XCopyArea(display, wmgen.pixmap, win, NormalGC,
180 0,0, wmgen.attributes.width, wmgen.attributes.height, 0,0);
183 /*******************************************************************************\
184 |* RedrawWindowXY *|
185 \*******************************************************************************/
187 void RedrawWindowXY(int x, int y) {
189 flush_expose(iconwin);
190 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC,
191 x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
192 flush_expose(win);
193 XCopyArea(display, wmgen.pixmap, win, NormalGC,
194 x,y, wmgen.attributes.width, wmgen.attributes.height, 0,0);
197 /*******************************************************************************\
198 |* AddMouseRegion *|
199 \*******************************************************************************/
201 void AddMouseRegion(int index, int left, int top, int right, int bottom) {
203 if (index < MAX_MOUSE_REGION) {
204 mouse_region[index].enable = 1;
205 mouse_region[index].top = top;
206 mouse_region[index].left = left;
207 mouse_region[index].bottom = bottom;
208 mouse_region[index].right = right;
212 /*******************************************************************************\
213 |* CheckMouseRegion *|
214 \*******************************************************************************/
216 int CheckMouseRegion(int x, int y) {
218 int i;
219 int found;
221 found = 0;
223 for (i=0; i<MAX_MOUSE_REGION && !found; i++) {
224 if (mouse_region[i].enable &&
225 x <= mouse_region[i].right &&
226 x >= mouse_region[i].left &&
227 y <= mouse_region[i].bottom &&
228 y >= mouse_region[i].top)
229 found = 1;
231 if (!found) return -1;
232 return (i-1);
235 /*******************************************************************************\
236 |* copyXPMArea *|
237 \*******************************************************************************/
239 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
241 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
245 /*******************************************************************************\
246 |* copyXBMArea *|
247 \*******************************************************************************/
249 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
251 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
255 /*******************************************************************************\
256 |* setMaskXY *|
257 \*******************************************************************************/
259 void setMaskXY(int x, int y) {
261 XShapeCombineMask(display, win, ShapeBounding, x, y, pixmask, ShapeSet);
262 XShapeCombineMask(display, iconwin, ShapeBounding, x, y, pixmask, ShapeSet);
265 /*******************************************************************************\
266 |* openXwindow *|
267 \*******************************************************************************/
268 void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits, int pixmask_width, int pixmask_height) {
270 unsigned int borderwidth = 1;
271 XClassHint classHint;
272 char *display_name = NULL;
273 char *wname = argv[0];
274 XTextProperty name;
276 XGCValues gcv;
277 unsigned long gcm;
280 int dummy=0;
281 int i;
283 for (i=1; argv[i]; i++) {
284 if (!strcmp(argv[i], "-display"))
285 display_name = argv[i+1];
288 if (!(display = XOpenDisplay(display_name))) {
289 fprintf(stderr, "%s: can't open display %s\n",
290 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) {
331 fprintf(stderr, "%s: can't allocate window name\n", wname);
332 exit(1);
335 XSetWMName(display, win, &name);
337 /* Create GC for drawing */
339 gcm = GCForeground | GCBackground | GCGraphicsExposures;
340 gcv.foreground = fore_pix;
341 gcv.background = back_pix;
342 gcv.graphics_exposures = 0;
343 NormalGC = XCreateGC(display, Root, gcm, &gcv);
345 /* ONLYSHAPE ON */
347 pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
349 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
350 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
352 /* ONLYSHAPE OFF */
354 mywmhints.initial_state = WithdrawnState;
355 mywmhints.icon_window = iconwin;
356 mywmhints.icon_x = mysizehints.x;
357 mywmhints.icon_y = mysizehints.y;
358 mywmhints.window_group = win;
359 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
361 XSetWMHints(display, win, &mywmhints);
363 XSetCommand(display, win, argv, argc);
364 XMapWindow(display, win);