wmail: fixed comparisons between signed and unsigned.
[dockapps.git] / wmwlmon / xutils.c
bloba2d462543d20eb383e88d3bef3dde5bcebf08d56
1 /* $Id: xutils.c,v 1.6 2008/04/30 20:52:42 hacki Exp $ */
3 /*
4 * Copyright (c) 2005, 2006 Marcus Glocker <marcus@nazgul.ch>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * Those X functions have been taken from the xutils.c file used by almost
21 * every wmdockapp. It has been slightly modificated for our needs and
22 * unused functions have been removed. The original version was written
23 * by Martijn Pieterse <pieterse@xs4all.nl>.
26 #include <ctype.h>
27 #include <err.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <X11/Xlib.h>
34 #include <X11/xpm.h>
35 #include <X11/extensions/shape.h>
37 #include "xutils.h"
40 * global variables for this file
42 int x_fd;
43 XSizeHints mysizehints;
44 XWMHints mywmhints;
45 Pixel back_pix, fore_pix;
46 char *Geometry = "";
47 GC NormalGC;
48 XpmIcon wmgen;
49 Pixmap pixmask;
51 extern char TimeColor[30];
52 extern char BackgroundColor[30];
55 * flush_expose
57 int
58 flush_expose(Window w)
60 XEvent dummy;
61 int i = 0;
63 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
64 i++;
66 return (i);
70 * copxXPMArea
72 void
73 copyXPMArea(int x, int y, int sx, int sy, int dx, int dy)
75 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy,
76 dx, dy);
80 * copyXBMArea
82 void
83 copyXBMArea(int x, int y, int sx, int sy, int dx, int dy)
85 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy,
86 dx, dy);
90 * RedrawWindow
92 void
93 RedrawWindow(void) {
94 flush_expose(iconwin);
95 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, 0, 0,
96 wmgen.attributes.width, wmgen.attributes.height, 0, 0);
98 flush_expose(win);
99 XCopyArea(display, wmgen.pixmap, win, NormalGC, 0, 0,
100 wmgen.attributes.width, wmgen.attributes.height, 0, 0);
104 * initXwindow
106 void
107 initXwindow(char *opt_display)
109 char *display_name = NULL;
111 if (opt_display != NULL)
112 display_name = opt_display;
114 if (!(display = XOpenDisplay(display_name)))
115 errx(1, "Can't open display: %s", XDisplayName(display_name));
117 screen = DefaultScreen(display);
118 Root = RootWindow(display, screen);
119 DisplayDepth = DefaultDepth(display, screen);
120 x_fd = XConnectionNumber(display);
124 * openXwindow
126 void
127 openXwindow(int argc, char *argv[], char *pixmap_bytes[],
128 unsigned char *pixmask_bits, int pixmask_width, int pixmask_height)
130 int dummy = 0, red, grn, blu;
131 char *wname = argv[0];
132 unsigned int borderwidth = 1;
133 unsigned long gcm;
134 XGCValues gcv;
135 XClassHint classHint;
136 XTextProperty name;
137 XpmColorSymbol cols[10] = {
138 { "Back", NULL, 0 },
139 { "Color1", NULL, 0 },
140 { "Color2", NULL, 0 },
141 { "Color3", NULL, 0 },
142 { "Color4", NULL, 0 },
143 { "Color5", NULL, 0 },
144 { "Color6", NULL, 0 },
145 { "Color7", NULL, 0 },
146 { "Color8", NULL, 0 },
147 { "Color9", NULL, 0 }
151 * create pixmap
153 cols[0].pixel = getColor(BackgroundColor, 1.0000, &red, &grn, &blu);
154 cols[1].pixel = getBlendedColor(TimeColor, 0.1522, red, grn, blu);
155 cols[2].pixel = getBlendedColor(TimeColor, 0.2602, red, grn, blu);
156 cols[3].pixel = getBlendedColor(TimeColor, 0.3761, red, grn, blu);
157 cols[4].pixel = getBlendedColor(TimeColor, 0.4841, red, grn, blu);
158 cols[5].pixel = getBlendedColor(TimeColor, 0.5922, red, grn, blu);
159 cols[6].pixel = getBlendedColor(TimeColor, 0.6980, red, grn, blu);
160 cols[7].pixel = getBlendedColor(TimeColor, 0.7961, red, grn, blu);
161 cols[8].pixel = getBlendedColor(TimeColor, 0.8941, red, grn, blu);
162 cols[9].pixel = getBlendedColor(TimeColor, 1.0000, red, grn, blu);
164 wmgen.attributes.numsymbols = 10;
165 wmgen.attributes.colorsymbols = cols;
166 wmgen.attributes.exactColors = False;
167 wmgen.attributes.closeness = 40000;
168 wmgen.attributes.valuemask = XpmReturnPixels | XpmReturnExtensions |
169 XpmColorSymbols | XpmExactColors | XpmCloseness | XpmSize;
171 if (XpmCreatePixmapFromData(display, Root, pixmap_bytes,
172 &(wmgen.pixmap), &(wmgen.mask), &(wmgen.attributes)) != XpmSuccess)
173 errx(1, "Not enough free colocells\n");
176 * create a window
178 mysizehints.flags = USSize | USPosition;
179 mysizehints.x = 0;
180 mysizehints.y = 0;
182 back_pix = getColor("white", 1.0, &red, &grn, &blu);
183 fore_pix = getColor("black", 1.0, &red, &grn, &blu);
185 XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
186 &mysizehints.x, &mysizehints.y, &mysizehints.width,
187 &mysizehints.height, &dummy);
189 mysizehints.width = 64;
190 mysizehints.height = 64;
192 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
193 mysizehints.width, mysizehints.height, borderwidth, fore_pix,
194 back_pix);
196 iconwin = XCreateSimpleWindow(display, win, mysizehints.x,
197 mysizehints.y, mysizehints.width, mysizehints.height, borderwidth,
198 fore_pix, back_pix);
201 * activate hints
203 XSetWMNormalHints(display, win, &mysizehints);
204 classHint.res_name = wname;
205 classHint.res_class = wname;
206 XSetClassHint(display, win, &classHint);
209 * set up the xevents
211 XSelectInput(display, win, ButtonPressMask | ExposureMask |
212 ButtonReleaseMask | PointerMotionMask | StructureNotifyMask |
213 EnterWindowMask | LeaveWindowMask | KeyPressMask | KeyReleaseMask);
214 XSelectInput(display, win, ButtonPressMask | ExposureMask |
215 ButtonReleaseMask | PointerMotionMask | StructureNotifyMask |
216 EnterWindowMask | LeaveWindowMask | KeyPressMask | KeyReleaseMask);
218 if (XStringListToTextProperty(&wname, 1, &name) == 0)
219 errx(1, "Can't allocate window name: %s\n", wname);
221 XSetWMName(display, win, &name);
224 * create graphics context (gc) for drawing
226 gcm = GCForeground | GCBackground | GCGraphicsExposures;
227 gcv.foreground = fore_pix;
228 gcv.background = back_pix;
229 gcv.graphics_exposures = 0;
230 NormalGC = XCreateGC(display, Root, gcm, &gcv);
232 pixmask = XCreateBitmapFromData(display, win, (char *)pixmask_bits,
233 pixmask_width, pixmask_height);
234 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask,
235 ShapeSet);
236 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask,
237 ShapeSet);
239 mywmhints.initial_state = WithdrawnState;
240 mywmhints.icon_window = iconwin;
241 mywmhints.icon_x = mysizehints.x;
242 mywmhints.icon_y = mysizehints.y;
243 mywmhints.window_group = win;
244 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint |
245 WindowGroupHint;
247 XSetWMHints(display, win, &mywmhints);
249 XSetCommand(display, win, argv, argc);
250 XMapWindow(display, win);
254 * getColor
256 unsigned long
257 getColor(char *ColorName, float fac, int *red, int *grn, int *blu)
259 XColor Color;
260 XWindowAttributes Attributes;
262 XGetWindowAttributes(display, Root, &Attributes);
263 Color.pixel = 0;
265 XParseColor(display, Attributes.colormap, ColorName, &Color);
266 Color.red = (unsigned short)(fac*(Color.red-24) + 24);
267 Color.blue = (unsigned short)(fac*(Color.blue-24) + 24);
268 Color.green = (unsigned short)(fac*(Color.green-24) + 24);
269 Color.flags = DoRed | DoGreen | DoBlue;
270 XAllocColor(display, Attributes.colormap, &Color);
272 *red = Color.red;
273 *grn = Color.green;
274 *blu = Color.blue;
276 return (Color.pixel);
280 * getBlendedColor
282 unsigned long
283 getBlendedColor(char *ColorName, float fac, int red, int grn, int blu)
285 XColor Color;
286 XWindowAttributes Attributes;
288 XGetWindowAttributes(display, Root, &Attributes);
289 Color.pixel = 0;
291 XParseColor(display, Attributes.colormap, ColorName, &Color);
292 Color.red = (unsigned short)(fac*(Color.red-red) + red);
293 Color.blue = (unsigned short)(fac*(Color.blue-grn) + grn);
294 Color.green = (unsigned short)(fac*(Color.green-blu) + blu);
295 Color.flags = DoRed | DoGreen | DoBlue;
296 XAllocColor(display, Attributes.colormap, &Color);
298 return (Color.pixel);