wmclockmon: update change-log
[dockapps.git] / wmjmail / src / xutils / xutils.c
blob10e71eb153649ddcfa57331d3eec2815aedc140f
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
6 * with wmppp (by Martijn Pieterse (pieterse@xs4all.nl)), but its been
7 * hacked up quite a bit 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., 59 Temple Place - Suite 330,
25 * Boston, MA 02111-1307, USA
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
119 /*void copyXPMArea(source_x, source_y, width, height, destx, desty) */
123 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
124 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
127 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
129 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
135 * initXwindow
137 void initXwindow(int argc, char *argv[]){
139 int i;
140 char *display_name = NULL;
142 for (i=1; argv[i]; ++i) {
143 if (!strcmp(argv[i], "-display")) display_name = argv[i+1];
147 if (!(display = XOpenDisplay(display_name))) {
148 fprintf(stderr, "%s: can't open display %s\n",
149 argv[0], XDisplayName(display_name));
150 exit(1);
154 screen = DefaultScreen(display);
155 Root = RootWindow(display, screen);
156 DisplayDepth = DefaultDepth(display, screen);
157 x_fd = XConnectionNumber(display);
166 * openXwindow
168 void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits,
169 int pixmask_width, int pixmask_height, char *BackColor, char *LabelColor,
170 char *WindGustColor, char *DataColor, char *StationTimeColor) {
172 unsigned int borderwidth = 1;
173 XClassHint classHint;
174 char *wname = argv[0];
175 XTextProperty name;
176 XGCValues gcv;
177 unsigned long gcm;
178 int dummy=0;
179 XpmColorSymbol cols[5]={ {"BackColor", NULL, 0},
180 {"LabelColor", NULL, 0},
181 {"DataColor", NULL, 0},
182 {"WindGustColor", NULL, 0},
183 {"StationTimeColor", NULL, 0} };
189 * Create Pixmap
191 cols[0].pixel = getColor(BackColor, 1.0);
192 cols[1].pixel = getColor(LabelColor, 1.0);
193 cols[2].pixel = getColor(DataColor, 1.0);
194 cols[3].pixel = getColor(WindGustColor, 1.0);
195 cols[4].pixel = getColor(StationTimeColor, 1.0);
196 wmgen.attributes.numsymbols = 5;
197 wmgen.attributes.colorsymbols = cols;
198 wmgen.attributes.exactColors = False;
199 wmgen.attributes.closeness = 40000;
200 wmgen.attributes.valuemask = XpmReturnPixels | XpmReturnExtensions | XpmColorSymbols
201 | XpmExactColors | XpmCloseness | XpmSize;
202 if (XpmCreatePixmapFromData(display, Root, pixmap_bytes,
203 &(wmgen.pixmap), &(wmgen.mask), &(wmgen.attributes)) != XpmSuccess){
204 fprintf(stderr, "Not enough free colorcells.\n");
205 exit(1);
212 * Create a window
214 mysizehints.flags = USSize | USPosition;
215 mysizehints.x = 0;
216 mysizehints.y = 0;
218 back_pix = getColor("white", 1.0);
219 fore_pix = getColor("black", 1.0);
221 XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
222 &mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
224 mysizehints.width = 64;
225 mysizehints.height = 64;
229 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
230 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
232 iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
233 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
238 * Activate hints
240 XSetWMNormalHints(display, win, &mysizehints);
241 classHint.res_name = wname;
242 classHint.res_class = wname;
243 XSetClassHint(display, win, &classHint);
248 * Set up the xevents that you want the relevent windows to inherit
249 * Currently, its seems that setting KeyPress events here has no
250 * effect. I.e. for some you will need to Grab the focus and then return
251 * it after you are done...
253 XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask
254 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
255 | KeyPressMask | KeyReleaseMask);
256 XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask
257 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
258 | KeyPressMask | KeyReleaseMask);
261 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
262 fprintf(stderr, "%s: can't allocate window name\n", wname);
263 exit(1);
267 XSetWMName(display, win, &name);
270 * Create Graphics Context (GC) for drawing
272 gcm = GCForeground | GCBackground | GCGraphicsExposures;
273 gcv.foreground = fore_pix;
274 gcv.background = back_pix;
275 gcv.graphics_exposures = 0;
276 NormalGC = XCreateGC(display, Root, gcm, &gcv);
280 pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
281 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
282 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
285 mywmhints.initial_state = WithdrawnState;
286 mywmhints.icon_window = iconwin;
287 mywmhints.icon_x = mysizehints.x;
288 mywmhints.icon_y = mysizehints.y;
289 mywmhints.window_group = win;
290 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
293 XSetWMHints(display, win, &mywmhints);
296 XSetCommand(display, win, argv, argc);
297 XMapWindow(display, win);
301 unsigned long getColor(char *ColorName, float fac) {
303 XColor Color;
304 XWindowAttributes Attributes;
306 XGetWindowAttributes(display, Root, &Attributes);
307 Color.pixel = 0;
309 XParseColor(display, Attributes.colormap, ColorName, &Color);
310 Color.red = (unsigned short)(Color.red/fac);
311 Color.blue = (unsigned short)(Color.blue/fac);
312 Color.green = (unsigned short)(Color.green/fac);
313 Color.flags = DoRed | DoGreen | DoBlue;
314 XAllocColor(display, Attributes.colormap, &Color);
316 return Color.pixel;