wmpager: allow WMPAGER_DEFAULT_INSTALL_DIR to be defined in $FLAGS
[dockapps.git] / wmifinfo / xutils.c
blob1f0df153a19ab1f7217a15e57be03d119c6909e0
1 /*
2 * xutils.c - A collection of X-windows utilties for creating WindowMAker
3 * DockApps.
5 * This file contains alot of the lower-level X windows routines. Origins with wmppp
6 * (by Martijn Pieterse (pieterse@xs4all.nl)), but its been hacked up quite a bit
7 * and passed on from one new DockApp to the next.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program (see the file COPYING); if not, write to the
24 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 * Boston, MA 02110-1301 USA
30 */
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <ctype.h>
36 #include <stdarg.h>
37 #include <X11/Xlib.h>
38 #include <X11/xpm.h>
39 #include <X11/extensions/shape.h>
40 #include "xutils.h"
45 * X11 Variables
47 int x_fd;
48 XSizeHints mysizehints;
49 XWMHints mywmhints;
50 Pixel back_pix, fore_pix;
51 char *Geometry = "";
52 GC NormalGC;
53 XpmIcon wmgen;
54 Pixmap pixmask;
61 * flush_expose
63 static int flush_expose(Window w) {
65 XEvent dummy;
66 int i=0;
68 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
69 i++;
71 return i;
84 * RedrawWindow
85 * RedrawWindowXY
87 void RedrawWindow(void) {
89 flush_expose(iconwin);
90 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, 0,0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
92 flush_expose(win);
93 XCopyArea(display, wmgen.pixmap, win, NormalGC, 0,0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
97 void RedrawWindowXY(int x, int y) {
99 flush_expose(iconwin);
100 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, x,y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
102 flush_expose(win);
103 XCopyArea(display, wmgen.pixmap, win, NormalGC, x,y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
114 * copyXPMArea
115 * copyXBMArea
117 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
118 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
121 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
123 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
129 * initXwindow
131 void initXwindow(int argc, char *argv[]){
133 int i;
134 char *display_name = NULL;
136 for (i=1; argv[i]; ++i) {
137 if (!strcmp(argv[i], "-display")) display_name = argv[i+1];
141 if (!(display = XOpenDisplay(display_name))) {
142 fprintf(stderr, "%s: can't open display %s\n",
143 argv[0], XDisplayName(display_name));
144 exit(1);
148 screen = DefaultScreen(display);
149 Root = RootWindow(display, screen);
150 DisplayDepth = DefaultDepth(display, screen);
151 x_fd = XConnectionNumber(display);
160 * openXwindow
162 void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits,
163 int pixmask_width, int pixmask_height, char *BackColor, char *LabelColor,
164 char *WindGustColor, char *DataColor, char *StationTimeColor) {
166 unsigned int borderwidth = 1;
167 XClassHint classHint;
168 char *wname = argv[0];
169 XTextProperty name;
170 XGCValues gcv;
171 unsigned long gcm;
172 int dummy=0;
173 XpmColorSymbol cols[5]={ {"BackColor", NULL, 0},
174 {"LabelColor", NULL, 0},
175 {"DataColor", NULL, 0},
176 {"WindGustColor", NULL, 0},
177 {"StationTimeColor", NULL, 0} };
183 * Create Pixmap
185 cols[0].pixel = getColor(BackColor, 1.0);
186 cols[1].pixel = getColor(LabelColor, 1.0);
187 cols[2].pixel = getColor(DataColor, 1.0);
188 cols[3].pixel = getColor(WindGustColor, 1.0);
189 cols[4].pixel = getColor(StationTimeColor, 1.0);
190 wmgen.attributes.numsymbols = 5;
191 wmgen.attributes.colorsymbols = cols;
192 wmgen.attributes.exactColors = False;
193 wmgen.attributes.closeness = 40000;
194 wmgen.attributes.valuemask = XpmReturnPixels | XpmReturnExtensions | XpmColorSymbols
195 | XpmExactColors | XpmCloseness | XpmSize;
196 if (XpmCreatePixmapFromData(display, Root, pixmap_bytes,
197 &(wmgen.pixmap), &(wmgen.mask), &(wmgen.attributes)) != XpmSuccess){
198 fprintf(stderr, "Not enough free colorcells.\n");
199 exit(1);
206 * Create a window
208 mysizehints.flags = USSize | USPosition;
209 mysizehints.x = 0;
210 mysizehints.y = 0;
212 back_pix = getColor("white", 1.0);
213 fore_pix = getColor("black", 1.0);
215 XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
216 &mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
218 mysizehints.width = 64;
219 mysizehints.height = 64;
223 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
224 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
226 iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
227 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
232 * Activate hints
234 XSetWMNormalHints(display, win, &mysizehints);
235 classHint.res_name = wname;
236 classHint.res_class = wname;
237 XSetClassHint(display, win, &classHint);
242 * Set up the xevents that you want the relevent windows to inherit
243 * Currently, its seems that setting KeyPress events here has no
244 * effect. I.e. for some you will need to Grab the focus and then return
245 * it after you are done...
247 XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask
248 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
249 | KeyPressMask | KeyReleaseMask);
250 XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask
251 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
252 | KeyPressMask | KeyReleaseMask);
255 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
256 fprintf(stderr, "%s: can't allocate window name\n", wname);
257 exit(1);
261 XSetWMName(display, win, &name);
264 * Create Graphics Context (GC) for drawing
266 gcm = GCForeground | GCBackground | GCGraphicsExposures;
267 gcv.foreground = fore_pix;
268 gcv.background = back_pix;
269 gcv.graphics_exposures = 0;
270 NormalGC = XCreateGC(display, Root, gcm, &gcv);
274 pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
275 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
276 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
279 mywmhints.initial_state = WithdrawnState;
280 mywmhints.icon_window = iconwin;
281 mywmhints.icon_x = mysizehints.x;
282 mywmhints.icon_y = mysizehints.y;
283 mywmhints.window_group = win;
284 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
287 XSetWMHints(display, win, &mywmhints);
290 XSetCommand(display, win, argv, argc);
291 XMapWindow(display, win);
295 unsigned long getColor(char *ColorName, float fac) {
297 XColor Color;
298 XWindowAttributes Attributes;
300 XGetWindowAttributes(display, Root, &Attributes);
301 Color.pixel = 0;
303 XParseColor(display, Attributes.colormap, ColorName, &Color);
304 Color.red = (unsigned short)(Color.red/fac);
305 Color.blue = (unsigned short)(Color.blue/fac);
306 Color.green = (unsigned short)(Color.green/fac);
307 Color.flags = DoRed | DoGreen | DoBlue;
308 XAllocColor(display, Attributes.colormap, &Color);
310 return Color.pixel;