Remove trailing whitespace.
[dockapps.git] / wmmoonclock / src / xutils.c
blob25d2e1f0eabff7ec4914881002ff52b431c6ae63
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;
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
117 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
118 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
121 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
123 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
129 * initXwindow
131 void initXwindow(int argc, char *argv[]){
133 int i;
134 char *display_name = NULL;
136 for (i=1; argv[i]; ++i) {
137 if (!strcmp(argv[i], "-display")) display_name = argv[i+1];
141 if (!(display = XOpenDisplay(display_name))) {
142 fprintf(stderr, "%s: can't open display %s\n",
143 argv[0], XDisplayName(display_name));
144 exit(1);
148 screen = DefaultScreen(display);
149 Root = RootWindow(display, screen);
150 DisplayDepth = DefaultDepth(display, screen);
151 x_fd = XConnectionNumber(display);
160 * openXwindow
162 void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits,
163 int pixmask_width, int pixmask_height, char *BackColor, char *LabelColor,
164 char *DataColor) {
166 unsigned int borderwidth = 1;
167 XClassHint classHint;
168 char *wname = argv[0];
169 XTextProperty name;
170 XGCValues gcv;
171 unsigned long gcm;
172 int dummy=0;
173 XpmColorSymbol cols[3]={ {"BackColor", NULL, 0},
174 {"LabelColor", NULL, 0},
175 {"DataColor", NULL, 0}};
181 * Create Pixmap
183 cols[0].pixel = getColor(BackColor, 1.0);
184 cols[1].pixel = getColor(LabelColor, 1.0);
185 cols[2].pixel = getColor(DataColor, 1.0);
186 wmgen.attributes.numsymbols = 3;
187 wmgen.attributes.colorsymbols = cols;
188 wmgen.attributes.exactColors = False;
189 wmgen.attributes.closeness = 40000;
190 wmgen.attributes.valuemask = XpmReturnPixels | XpmReturnExtensions | XpmColorSymbols
191 | XpmExactColors | XpmCloseness | XpmSize;
192 if (XpmCreatePixmapFromData(display, Root, pixmap_bytes,
193 &(wmgen.pixmap), &(wmgen.mask), &(wmgen.attributes)) != XpmSuccess){
194 fprintf(stderr, "Not enough free colorcells.\n");
195 exit(1);
202 * Create a window
204 mysizehints.flags = USSize | USPosition;
205 mysizehints.x = 0;
206 mysizehints.y = 0;
208 back_pix = getColor("white", 1.0);
209 fore_pix = getColor("black", 1.0);
211 XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
212 &mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
214 mysizehints.width = 64;
215 mysizehints.height = 64;
219 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
220 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
222 iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
223 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
228 * Activate hints
230 XSetWMNormalHints(display, win, &mysizehints);
231 classHint.res_name = wname;
232 classHint.res_class = wname;
233 XSetClassHint(display, win, &classHint);
238 * Set up the xevents that you want the relevent windows to inherit
239 * Currently, its seems that setting KeyPress events here has no
240 * effect. I.e. for some you will need to Grab the focus and then return
241 * it after you are done...
243 XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask
244 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
245 | KeyPressMask | KeyReleaseMask);
246 XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask
247 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
248 | KeyPressMask | KeyReleaseMask);
251 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
252 fprintf(stderr, "%s: can't allocate window name\n", wname);
253 exit(1);
257 XSetWMName(display, win, &name);
260 * Create Graphics Context (GC) for drawing
262 gcm = GCForeground | GCBackground | GCGraphicsExposures;
263 gcv.foreground = fore_pix;
264 gcv.background = back_pix;
265 gcv.graphics_exposures = 0;
266 NormalGC = XCreateGC(display, Root, gcm, &gcv);
270 pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
271 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
272 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
275 mywmhints.initial_state = WithdrawnState;
276 mywmhints.icon_window = iconwin;
277 mywmhints.icon_x = mysizehints.x;
278 mywmhints.icon_y = mysizehints.y;
279 mywmhints.window_group = win;
280 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
283 XSetWMHints(display, win, &mywmhints);
286 XSetCommand(display, win, argv, argc);
287 XMapWindow(display, win);
291 unsigned long getColor(char *ColorName, float fac) {
293 XColor Color;
294 XWindowAttributes Attributes;
296 XGetWindowAttributes(display, Root, &Attributes);
297 Color.pixel = 0;
299 XParseColor(display, Attributes.colormap, ColorName, &Color);
300 Color.red = (unsigned short)(Color.red/fac);
301 Color.blue = (unsigned short)(Color.blue/fac);
302 Color.green = (unsigned short)(Color.green/fac);
303 Color.flags = DoRed | DoGreen | DoBlue;
304 XAllocColor(display, Attributes.colormap, &Color);
306 return Color.pixel;