wmmoonclock: add autogen.
[dockapps.git] / wmifinfo / xutils.c
blobed40202fe9fcc22a17af890ce0f7c2ad1a9cfb60
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
28 * $Id: xutils.c,v 1.2 2002/09/15 14:31:41 ico Exp $
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <ctype.h>
37 #include <stdarg.h>
38 #include <X11/Xlib.h>
39 #include <X11/xpm.h>
40 #include <X11/extensions/shape.h>
41 #include "xutils.h"
44 Display *display;
45 Window Root;
46 Window iconwin, win;
47 int screen;
48 int DisplayDepth;
52 * X11 Variables
54 int x_fd;
55 XSizeHints mysizehints;
56 XWMHints mywmhints;
57 Pixel back_pix, fore_pix;
58 char *Geometry = "";
59 GC NormalGC;
60 XpmIcon wmgen;
61 Pixmap pixmask;
68 * flush_expose
70 static int flush_expose(Window w) {
72 XEvent dummy;
73 int i=0;
75 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
76 i++;
78 return i;
91 * RedrawWindow
92 * RedrawWindowXY
94 void RedrawWindow(void) {
96 flush_expose(iconwin);
97 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, 0,0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
99 flush_expose(win);
100 XCopyArea(display, wmgen.pixmap, win, NormalGC, 0,0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
104 void RedrawWindowXY(int x, int y) {
106 flush_expose(iconwin);
107 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, x,y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
109 flush_expose(win);
110 XCopyArea(display, wmgen.pixmap, win, NormalGC, x,y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
121 * copyXPMArea
122 * copyXBMArea
124 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
125 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
128 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
130 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
136 * initXwindow
138 void initXwindow(int argc, char *argv[]){
140 int i;
141 char *display_name = NULL;
143 for (i=1; argv[i]; ++i) {
144 if (!strcmp(argv[i], "-display")) display_name = argv[i+1];
148 if (!(display = XOpenDisplay(display_name))) {
149 fprintf(stderr, "%s: can't open display %s\n",
150 argv[0], XDisplayName(display_name));
151 exit(1);
155 screen = DefaultScreen(display);
156 Root = RootWindow(display, screen);
157 DisplayDepth = DefaultDepth(display, screen);
158 x_fd = XConnectionNumber(display);
167 * openXwindow
169 void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits,
170 int pixmask_width, int pixmask_height, char *BackColor, char *LabelColor,
171 char *WindGustColor, char *DataColor, char *StationTimeColor) {
173 unsigned int borderwidth = 1;
174 XClassHint classHint;
175 char *wname = argv[0];
176 XTextProperty name;
177 XGCValues gcv;
178 unsigned long gcm;
179 int dummy=0;
180 XpmColorSymbol cols[5]={ {"BackColor", NULL, 0},
181 {"LabelColor", NULL, 0},
182 {"DataColor", NULL, 0},
183 {"WindGustColor", NULL, 0},
184 {"StationTimeColor", NULL, 0} };
190 * Create Pixmap
192 cols[0].pixel = getColor(BackColor, 1.0);
193 cols[1].pixel = getColor(LabelColor, 1.0);
194 cols[2].pixel = getColor(DataColor, 1.0);
195 cols[3].pixel = getColor(WindGustColor, 1.0);
196 cols[4].pixel = getColor(StationTimeColor, 1.0);
197 wmgen.attributes.numsymbols = 5;
198 wmgen.attributes.colorsymbols = cols;
199 wmgen.attributes.exactColors = False;
200 wmgen.attributes.closeness = 40000;
201 wmgen.attributes.valuemask = XpmReturnPixels | XpmReturnExtensions | XpmColorSymbols
202 | XpmExactColors | XpmCloseness | XpmSize;
203 if (XpmCreatePixmapFromData(display, Root, pixmap_bytes,
204 &(wmgen.pixmap), &(wmgen.mask), &(wmgen.attributes)) != XpmSuccess){
205 fprintf(stderr, "Not enough free colorcells.\n");
206 exit(1);
213 * Create a window
215 mysizehints.flags = USSize | USPosition;
216 mysizehints.x = 0;
217 mysizehints.y = 0;
219 back_pix = getColor("white", 1.0);
220 fore_pix = getColor("black", 1.0);
222 XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
223 &mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
225 mysizehints.width = 64;
226 mysizehints.height = 64;
230 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
231 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
233 iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
234 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
239 * Activate hints
241 XSetWMNormalHints(display, win, &mysizehints);
242 classHint.res_name = wname;
243 classHint.res_class = wname;
244 XSetClassHint(display, win, &classHint);
249 * Set up the xevents that you want the relevent windows to inherit
250 * Currently, its seems that setting KeyPress events here has no
251 * effect. I.e. for some you will need to Grab the focus and then return
252 * it after you are done...
254 XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask
255 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
256 | KeyPressMask | KeyReleaseMask);
257 XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask
258 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
259 | KeyPressMask | KeyReleaseMask);
262 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
263 fprintf(stderr, "%s: can't allocate window name\n", wname);
264 exit(1);
268 XSetWMName(display, win, &name);
271 * Create Graphics Context (GC) for drawing
273 gcm = GCForeground | GCBackground | GCGraphicsExposures;
274 gcv.foreground = fore_pix;
275 gcv.background = back_pix;
276 gcv.graphics_exposures = 0;
277 NormalGC = XCreateGC(display, Root, gcm, &gcv);
281 pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
282 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
283 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
286 mywmhints.initial_state = WithdrawnState;
287 mywmhints.icon_window = iconwin;
288 mywmhints.icon_x = mysizehints.x;
289 mywmhints.icon_y = mysizehints.y;
290 mywmhints.window_group = win;
291 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
294 XSetWMHints(display, win, &mywmhints);
297 XSetCommand(display, win, argv, argc);
298 XMapWindow(display, win);
302 unsigned long getColor(char *ColorName, float fac) {
304 XColor Color;
305 XWindowAttributes Attributes;
307 XGetWindowAttributes(display, Root, &Attributes);
308 Color.pixel = 0;
310 XParseColor(display, Attributes.colormap, ColorName, &Color);
311 Color.red = (unsigned short)(Color.red/fac);
312 Color.blue = (unsigned short)(Color.blue/fac);
313 Color.green = (unsigned short)(Color.green/fac);
314 Color.flags = DoRed | DoGreen | DoBlue;
315 XAllocColor(display, Attributes.colormap, &Color);
317 return Color.pixel;