wmail: strcasecmp and strncasecmp are properly declared in strings.h.
[dockapps.git] / wmCalClock / Src / xutils.c
blob5426d4ff713a031d0eb076f3a9ac087b95bb5ebd
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;
58 * Colors for wmCalClock
60 extern char TimeColor[30];
61 extern char BackgroundColor[30];
69 * flush_expose
71 static int flush_expose(Window w) {
73 XEvent dummy;
74 int i=0;
76 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
77 i++;
79 return i;
92 * RedrawWindow
93 * RedrawWindowXY
95 void RedrawWindow(void) {
97 flush_expose(iconwin);
98 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, 0,0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
100 flush_expose(win);
101 XCopyArea(display, wmgen.pixmap, win, NormalGC, 0,0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
105 void RedrawWindowXY(int x, int y) {
107 flush_expose(iconwin);
108 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, x,y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
110 flush_expose(win);
111 XCopyArea(display, wmgen.pixmap, win, NormalGC, x,y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
122 * copyXPMArea
123 * copyXBMArea
125 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
126 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
129 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
131 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
137 * initXwindow
139 void initXwindow(int argc, char *argv[]){
141 int i;
142 char *display_name = NULL;
144 for (i=1; argv[i]; ++i) {
145 if (!strcmp(argv[i], "-display")) display_name = argv[i+1];
149 if (!(display = XOpenDisplay(display_name))) {
150 fprintf(stderr, "%s: can't open display %s\n",
151 argv[0], XDisplayName(display_name));
152 exit(1);
156 screen = DefaultScreen(display);
157 Root = RootWindow(display, screen);
158 DisplayDepth = DefaultDepth(display, screen);
159 x_fd = XConnectionNumber(display);
168 * openXwindow
170 void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits,
171 int pixmask_width, int pixmask_height) {
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, red, grn, blu;
180 XpmColorSymbol cols[10]={ {"Back", NULL, 0},
181 {"Color1", NULL, 0},
182 {"Color2", NULL, 0},
183 {"Color3", NULL, 0},
184 {"Color4", NULL, 0},
185 {"Color5", NULL, 0},
186 {"Color6", NULL, 0},
187 {"Color7", NULL, 0},
188 {"Color8", NULL, 0},
189 {"Color9", NULL, 0}};
195 * Create Pixmap
197 cols[0].pixel = getColor(BackgroundColor, 1.0000, &red, &grn, &blu);
198 cols[1].pixel = getBlendedColor(TimeColor, 0.1522, red, grn, blu);
199 cols[2].pixel = getBlendedColor(TimeColor, 0.2602, red, grn, blu);
200 cols[3].pixel = getBlendedColor(TimeColor, 0.3761, red, grn, blu);
201 cols[4].pixel = getBlendedColor(TimeColor, 0.4841, red, grn, blu);
202 cols[5].pixel = getBlendedColor(TimeColor, 0.5922, red, grn, blu);
203 cols[6].pixel = getBlendedColor(TimeColor, 0.6980, red, grn, blu);
204 cols[7].pixel = getBlendedColor(TimeColor, 0.7961, red, grn, blu);
205 cols[8].pixel = getBlendedColor(TimeColor, 0.8941, red, grn, blu);
206 cols[9].pixel = getBlendedColor(TimeColor, 1.0000, red, grn, blu);
208 wmgen.attributes.numsymbols = 10;
209 wmgen.attributes.colorsymbols = cols;
210 wmgen.attributes.exactColors = False;
211 wmgen.attributes.closeness = 40000;
212 wmgen.attributes.valuemask = XpmReturnPixels | XpmReturnExtensions | XpmColorSymbols
213 | XpmExactColors | XpmCloseness | XpmSize;
214 if (XpmCreatePixmapFromData(display, Root, pixmap_bytes,
215 &(wmgen.pixmap), &(wmgen.mask), &(wmgen.attributes)) != XpmSuccess){
216 fprintf(stderr, "Not enough free colorcells.\n");
217 exit(1);
224 * Create a window
226 mysizehints.flags = USSize | USPosition;
227 mysizehints.x = 0;
228 mysizehints.y = 0;
230 back_pix = getColor("white", 1.0, &red, &grn, &blu);
231 fore_pix = getColor("black", 1.0, &red, &grn, &blu);
233 XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
234 &mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
236 mysizehints.width = 64;
237 mysizehints.height = 64;
241 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
242 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
244 iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
245 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
250 * Activate hints
252 XSetWMNormalHints(display, win, &mysizehints);
253 classHint.res_name = wname;
254 classHint.res_class = wname;
255 XSetClassHint(display, win, &classHint);
260 * Set up the xevents that you want the relevent windows to inherit
261 * Currently, its seems that setting KeyPress events here has no
262 * effect. I.e. for some you will need to Grab the focus and then return
263 * it after you are done...
265 XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask
266 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
267 | KeyPressMask | KeyReleaseMask);
268 XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask
269 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
270 | KeyPressMask | KeyReleaseMask);
273 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
274 fprintf(stderr, "%s: can't allocate window name\n", wname);
275 exit(1);
279 XSetWMName(display, win, &name);
282 * Create Graphics Context (GC) for drawing
284 gcm = GCForeground | GCBackground | GCGraphicsExposures;
285 gcv.foreground = fore_pix;
286 gcv.background = back_pix;
287 gcv.graphics_exposures = 0;
288 NormalGC = XCreateGC(display, Root, gcm, &gcv);
292 pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
293 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
294 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
297 mywmhints.initial_state = WithdrawnState;
298 mywmhints.icon_window = iconwin;
299 mywmhints.icon_x = mysizehints.x;
300 mywmhints.icon_y = mysizehints.y;
301 mywmhints.window_group = win;
302 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
305 XSetWMHints(display, win, &mywmhints);
308 XSetCommand(display, win, argv, argc);
309 XMapWindow(display, win);
313 unsigned long getColor(char *ColorName, float fac, int *red, int *grn, int *blu) {
315 XColor Color;
316 XWindowAttributes Attributes;
318 XGetWindowAttributes(display, Root, &Attributes);
319 Color.pixel = 0;
321 XParseColor(display, Attributes.colormap, ColorName, &Color);
322 Color.red = (unsigned short)(fac*(Color.red-24) + 24);
323 Color.blue = (unsigned short)(fac*(Color.blue-24) + 24);
324 Color.green = (unsigned short)(fac*(Color.green-24) + 24);
325 Color.flags = DoRed | DoGreen | DoBlue;
326 XAllocColor(display, Attributes.colormap, &Color);
329 *red = Color.red;
330 *grn = Color.green;
331 *blu = Color.blue;
332 return Color.pixel;
336 unsigned long getBlendedColor(char *ColorName, float fac, int red, int grn, int blu) {
338 XColor Color;
339 XWindowAttributes Attributes;
341 XGetWindowAttributes(display, Root, &Attributes);
342 Color.pixel = 0;
344 XParseColor(display, Attributes.colormap, ColorName, &Color);
345 Color.red = (unsigned short)(fac*(Color.red-red) + red);
346 Color.blue = (unsigned short)(fac*(Color.blue-grn) + grn);
347 Color.green = (unsigned short)(fac*(Color.green-blu) + blu);
348 Color.flags = DoRed | DoGreen | DoBlue;
349 XAllocColor(display, Attributes.colormap, &Color);
351 return Color.pixel;