wmclockmon: rename `DFLAGS` `debug_CFLAGS`
[dockapps.git] / wmCalClock / Src / xutils.c
blob008afa577ebac1319bcce056f22cba04ec429910
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
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;
56 Display *display;
57 Window Root;
58 Window iconwin, win;
59 int screen;
60 int DisplayDepth;
63 * Colors for wmCalClock
65 extern char TimeColor[30];
66 extern char BackgroundColor[30];
74 * flush_expose
76 static int flush_expose(Window w) {
78 XEvent dummy;
79 int i=0;
81 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
82 i++;
84 return i;
97 * RedrawWindow
98 * RedrawWindowXY
100 void RedrawWindow(void) {
102 flush_expose(iconwin);
103 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, 0,0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
105 flush_expose(win);
106 XCopyArea(display, wmgen.pixmap, win, NormalGC, 0,0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
110 void RedrawWindowXY(int x, int y) {
112 flush_expose(iconwin);
113 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, x,y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
115 flush_expose(win);
116 XCopyArea(display, wmgen.pixmap, win, NormalGC, x,y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
127 * copyXPMArea
128 * copyXBMArea
130 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
131 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
134 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
136 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
142 * initXwindow
144 void initXwindow(int argc, char *argv[]){
146 int i;
147 char *display_name = NULL;
149 for (i=1; argv[i]; ++i) {
150 if (!strcmp(argv[i], "-display")) display_name = argv[i+1];
154 if (!(display = XOpenDisplay(display_name))) {
155 fprintf(stderr, "%s: can't open display %s\n",
156 argv[0], XDisplayName(display_name));
157 exit(1);
161 screen = DefaultScreen(display);
162 Root = RootWindow(display, screen);
163 DisplayDepth = DefaultDepth(display, screen);
164 x_fd = XConnectionNumber(display);
173 * openXwindow
175 void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits,
176 int pixmask_width, int pixmask_height) {
178 unsigned int borderwidth = 1;
179 XClassHint classHint;
180 char *wname = argv[0];
181 XTextProperty name;
182 XGCValues gcv;
183 unsigned long gcm;
184 int dummy=0, red, grn, blu;
185 XpmColorSymbol cols[10]={ {"Back", NULL, 0},
186 {"Color1", NULL, 0},
187 {"Color2", NULL, 0},
188 {"Color3", NULL, 0},
189 {"Color4", NULL, 0},
190 {"Color5", NULL, 0},
191 {"Color6", NULL, 0},
192 {"Color7", NULL, 0},
193 {"Color8", NULL, 0},
194 {"Color9", NULL, 0}};
200 * Create Pixmap
202 cols[0].pixel = getColor(BackgroundColor, 1.0000, &red, &grn, &blu);
203 cols[1].pixel = getBlendedColor(TimeColor, 0.1522, red, grn, blu);
204 cols[2].pixel = getBlendedColor(TimeColor, 0.2602, red, grn, blu);
205 cols[3].pixel = getBlendedColor(TimeColor, 0.3761, red, grn, blu);
206 cols[4].pixel = getBlendedColor(TimeColor, 0.4841, red, grn, blu);
207 cols[5].pixel = getBlendedColor(TimeColor, 0.5922, red, grn, blu);
208 cols[6].pixel = getBlendedColor(TimeColor, 0.6980, red, grn, blu);
209 cols[7].pixel = getBlendedColor(TimeColor, 0.7961, red, grn, blu);
210 cols[8].pixel = getBlendedColor(TimeColor, 0.8941, red, grn, blu);
211 cols[9].pixel = getBlendedColor(TimeColor, 1.0000, red, grn, blu);
213 wmgen.attributes.numsymbols = 10;
214 wmgen.attributes.colorsymbols = cols;
215 wmgen.attributes.exactColors = False;
216 wmgen.attributes.closeness = 40000;
217 wmgen.attributes.valuemask = XpmReturnPixels | XpmReturnExtensions | XpmColorSymbols
218 | XpmExactColors | XpmCloseness | XpmSize;
219 if (XpmCreatePixmapFromData(display, Root, pixmap_bytes,
220 &(wmgen.pixmap), &(wmgen.mask), &(wmgen.attributes)) != XpmSuccess){
221 fprintf(stderr, "Not enough free colorcells.\n");
222 exit(1);
229 * Create a window
231 mysizehints.flags = USSize | USPosition;
232 mysizehints.x = 0;
233 mysizehints.y = 0;
235 back_pix = getColor("white", 1.0, &red, &grn, &blu);
236 fore_pix = getColor("black", 1.0, &red, &grn, &blu);
238 XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
239 &mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
241 mysizehints.width = 64;
242 mysizehints.height = 64;
246 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
247 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
249 iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
250 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
255 * Activate hints
257 XSetWMNormalHints(display, win, &mysizehints);
258 classHint.res_name = wname;
259 classHint.res_class = wname;
260 XSetClassHint(display, win, &classHint);
265 * Set up the xevents that you want the relevent windows to inherit
266 * Currently, its seems that setting KeyPress events here has no
267 * effect. I.e. for some you will need to Grab the focus and then return
268 * it after you are done...
270 XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask
271 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
272 | KeyPressMask | KeyReleaseMask);
273 XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask
274 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
275 | KeyPressMask | KeyReleaseMask);
278 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
279 fprintf(stderr, "%s: can't allocate window name\n", wname);
280 exit(1);
284 XSetWMName(display, win, &name);
287 * Create Graphics Context (GC) for drawing
289 gcm = GCForeground | GCBackground | GCGraphicsExposures;
290 gcv.foreground = fore_pix;
291 gcv.background = back_pix;
292 gcv.graphics_exposures = 0;
293 NormalGC = XCreateGC(display, Root, gcm, &gcv);
297 pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
298 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
299 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
302 mywmhints.initial_state = WithdrawnState;
303 mywmhints.icon_window = iconwin;
304 mywmhints.icon_x = mysizehints.x;
305 mywmhints.icon_y = mysizehints.y;
306 mywmhints.window_group = win;
307 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
310 XSetWMHints(display, win, &mywmhints);
313 XSetCommand(display, win, argv, argc);
314 XMapWindow(display, win);
318 unsigned long getColor(char *ColorName, float fac, int *red, int *grn, int *blu) {
320 XColor Color;
321 XWindowAttributes Attributes;
323 XGetWindowAttributes(display, Root, &Attributes);
324 Color.pixel = 0;
326 XParseColor(display, Attributes.colormap, ColorName, &Color);
327 Color.red = (unsigned short)(fac*(Color.red-24) + 24);
328 Color.blue = (unsigned short)(fac*(Color.blue-24) + 24);
329 Color.green = (unsigned short)(fac*(Color.green-24) + 24);
330 Color.flags = DoRed | DoGreen | DoBlue;
331 XAllocColor(display, Attributes.colormap, &Color);
334 *red = Color.red;
335 *grn = Color.green;
336 *blu = Color.blue;
337 return Color.pixel;
341 unsigned long getBlendedColor(char *ColorName, float fac, int red, int grn, int blu) {
343 XColor Color;
344 XWindowAttributes Attributes;
346 XGetWindowAttributes(display, Root, &Attributes);
347 Color.pixel = 0;
349 XParseColor(display, Attributes.colormap, ColorName, &Color);
350 Color.red = (unsigned short)(fac*(Color.red-red) + red);
351 Color.blue = (unsigned short)(fac*(Color.blue-grn) + grn);
352 Color.green = (unsigned short)(fac*(Color.green-blu) + blu);
353 Color.flags = DoRed | DoGreen | DoBlue;
354 XAllocColor(display, Attributes.colormap, &Color);
356 return Color.pixel;