Add missing libraries to examples' Makefiles
[dockapps.git] / yawmppp / src / dockapp / wmgeneral.c
blob92ee8f77600842cf8de2f285c2792a14c19a152f
1 /*
3 WMPPP - PPP dock app/helper for WindowMaker
4 Copyright (C) 1997-2000:
6 Authors: Martijn Pieterse (pieterse@xs4all.nl)
7 Antoine Nulle (warp@xs4all.nl)
8 Felipe Bergo (bergo@seul.org)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <ctype.h>
31 #include <stdarg.h>
33 #include <X11/Xlib.h>
34 #include <X11/xpm.h>
35 #include <X11/extensions/shape.h>
37 #include "wmgeneral.h"
39 Window Root;
40 int screen;
41 int x_fd;
42 int d_depth;
43 XSizeHints mysizehints;
44 XWMHints mywmhints;
45 Pixel back_pix, fore_pix;
46 char *Geometry = "";
47 Window iconwin, win;
48 GC NormalGC;
49 XpmIcon wmgen;
50 Pixmap pixmask;
52 typedef struct
54 int enable;
55 int top;
56 int bottom;
57 int left;
58 int right;
60 MOUSE_REGION;
62 MOUSE_REGION mouse_region[MAX_MOUSE_REGION];
64 static void GetXPM (XpmIcon *, char **);
65 static Pixel GetColor (char *);
66 void RedrawWindow (void);
67 void AddMouseRegion (unsigned, int, int, int, int);
68 int CheckMouseRegion (int, int);
70 static void
71 GetXPM (XpmIcon * wmgen, char *pixmap_bytes[])
74 XWindowAttributes attributes;
75 int err;
77 /* For the colormap */
78 XGetWindowAttributes (display, Root, &attributes);
80 wmgen->attributes.valuemask |= (XpmReturnPixels | XpmReturnExtensions);
82 err = XpmCreatePixmapFromData (display, Root, pixmap_bytes, &(wmgen->pixmap),
83 &(wmgen->mask), &(wmgen->attributes));
85 if (err != XpmSuccess)
87 fprintf (stderr, "Not enough free colorcells.\n");
88 exit (1);
92 static Pixel
93 GetColor (char *name)
96 XColor color;
97 XWindowAttributes attributes;
99 XGetWindowAttributes (display, Root, &attributes);
101 color.pixel = 0;
102 if (!XParseColor (display, attributes.colormap, name, &color))
104 fprintf (stderr, "wm.app: can't parse %s.\n", name);
106 else if (!XAllocColor (display, attributes.colormap, &color))
108 fprintf (stderr, "wm.app: can't allocate %s.\n", name);
110 return color.pixel;
113 static int
114 flush_expose (Window w)
117 XEvent dummy;
118 int i = 0;
120 while (XCheckTypedWindowEvent (display, w, Expose, &dummy))
121 i++;
123 return i;
126 void
127 RedrawWindow (void)
130 flush_expose (iconwin);
131 XCopyArea (display, wmgen.pixmap, iconwin, NormalGC,
132 0, 0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
133 flush_expose (win);
134 XCopyArea (display, wmgen.pixmap, win, NormalGC,
135 0, 0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
138 void
139 RedrawWindowXY (int x, int y)
142 flush_expose (iconwin);
143 XCopyArea (display, wmgen.pixmap, iconwin, NormalGC,
144 x, y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
145 flush_expose (win);
146 XCopyArea (display, wmgen.pixmap, win, NormalGC,
147 x, y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
150 void
151 AddMouseRegion (unsigned index, int left, int top, int right, int bottom)
154 if (index < MAX_MOUSE_REGION)
156 mouse_region[index].enable = 1;
157 mouse_region[index].top = top;
158 mouse_region[index].left = left;
159 mouse_region[index].bottom = bottom;
160 mouse_region[index].right = right;
165 CheckMouseRegion (int x, int y)
168 int i;
169 int found;
171 found = 0;
173 for (i = 0; i < MAX_MOUSE_REGION && !found; i++)
175 if (mouse_region[i].enable &&
176 x <= mouse_region[i].right &&
177 x >= mouse_region[i].left &&
178 y <= mouse_region[i].bottom &&
179 y >= mouse_region[i].top)
180 found = 1;
182 if (!found)
183 return -1;
184 return (i - 1);
187 void
188 createXBMfromXPM (char *xbm, char **xpm, int sx, int sy)
191 int i, j;
192 int width, height, numcol;
193 char zero;
194 unsigned char bwrite;
195 int bcount;
198 sscanf (*xpm, "%d %d %d", &width, &height, &numcol);
200 zero = xpm[1][0];
201 for (i = numcol + 1; i < numcol + sy + 1; i++)
203 bcount = 0;
204 bwrite = 0;
205 for (j = 0; j < sx; j++)
207 bwrite >>= 1;
208 if (xpm[i][j] != zero)
210 bwrite += 128;
212 bcount++;
213 if (bcount == 8)
215 *xbm = bwrite;
216 xbm++;
217 bcount = 0;
218 bwrite = 0;
224 void
225 copyXPMArea (int x, int y, int sx, int sy, int dx, int dy)
228 XCopyArea (display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
232 void
233 copyXBMArea (int x, int y, int sx, int sy, int dx, int dy)
236 XCopyArea (display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
239 void
240 setMaskXY (int x, int y)
243 XShapeCombineMask (display, win, ShapeBounding, x, y, pixmask, ShapeSet);
244 XShapeCombineMask (display, iconwin, ShapeBounding, x, y, pixmask, ShapeSet);
247 void
248 openXwindow (int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits, int pixmask_width, int pixmask_height)
251 unsigned int borderwidth = 1;
252 XClassHint classHint;
253 char *display_name = NULL;
254 char *wname = argv[0];
255 XTextProperty name;
257 XGCValues gcv;
258 unsigned long gcm;
260 char *geometry = NULL;
262 int dummy = 0;
263 int i, wx, wy;
265 for (i = 1; argv[i]; i++)
267 if (!strcmp (argv[i], "-display"))
269 display_name = argv[i + 1];
270 i++;
272 if (!strcmp (argv[i], "-geometry"))
274 geometry = argv[i + 1];
275 i++;
279 if (!(display = XOpenDisplay (display_name)))
281 fprintf (stderr, "%s: can't open display %s\n",
282 wname, XDisplayName (display_name));
283 exit (1);
285 screen = DefaultScreen (display);
286 Root = RootWindow (display, screen);
287 d_depth = DefaultDepth (display, screen);
288 x_fd = XConnectionNumber (display);
290 /* Convert XPM to XImage */
291 GetXPM (&wmgen, pixmap_bytes);
293 /* Create a window to hold the stuff */
294 mysizehints.flags = USSize | USPosition;
295 mysizehints.x = 0;
296 mysizehints.y = 0;
298 back_pix = GetColor ("white");
299 fore_pix = GetColor ("black");
301 XWMGeometry (display, screen, Geometry, NULL, borderwidth, &mysizehints,
302 &mysizehints.x, &mysizehints.y, &mysizehints.width, &mysizehints.height, &dummy);
304 mysizehints.width = 64;
305 mysizehints.height = 64;
307 win = XCreateSimpleWindow (display, Root, mysizehints.x, mysizehints.y,
308 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
310 iconwin = XCreateSimpleWindow (display, win, mysizehints.x, mysizehints.y,
311 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
313 /* Activate hints */
314 XSetWMNormalHints (display, win, &mysizehints);
315 classHint.res_name = wname;
316 classHint.res_class = wname;
317 XSetClassHint (display, win, &classHint);
319 XSelectInput (display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
320 XSelectInput (display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
322 if (XStringListToTextProperty (&wname, 1, &name) == 0)
324 fprintf (stderr, "%s: can't allocate window name\n", wname);
325 exit (1);
328 XSetWMName (display, win, &name);
330 /* Create GC for drawing */
332 gcm = GCForeground | GCBackground | GCGraphicsExposures;
333 gcv.foreground = fore_pix;
334 gcv.background = back_pix;
335 gcv.graphics_exposures = 0;
336 NormalGC = XCreateGC (display, Root, gcm, &gcv);
338 /* ONLYSHAPE ON */
340 pixmask = XCreateBitmapFromData (display, win, pixmask_bits, pixmask_width, pixmask_height);
342 XShapeCombineMask (display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
343 XShapeCombineMask (display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
345 /* ONLYSHAPE OFF */
347 mywmhints.initial_state = WithdrawnState;
348 mywmhints.icon_window = iconwin;
349 mywmhints.icon_x = mysizehints.x;
350 mywmhints.icon_y = mysizehints.y;
351 mywmhints.window_group = win;
352 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
354 XSetWMHints (display, win, &mywmhints);
356 XSetCommand (display, win, argv, argc);
357 XMapWindow (display, win);
359 if (geometry)
361 if (sscanf (geometry, "+%d+%d", &wx, &wy) != 2)
363 fprintf (stderr, "Bad geometry string.\n");
364 exit (1);
366 XMoveWindow (display, win, wx, wy);