wmix: added autogen and .gitignore.
[dockapps.git] / wmmoonclock / src / xutils.c
blob347b46b7e4afecc5c1233dcd06297a61700261a1
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 <X11/Xlib.h>
35 #include <X11/Xutil.h>
36 #include <X11/xpm.h>
37 #include <X11/extensions/shape.h>
38 #include <X11/extensions/shapeconst.h>
39 #include "xutils.h"
44 * X11 Variables
46 int x_fd;
47 XSizeHints mysizehints;
48 XWMHints mywmhints;
49 Pixel back_pix, fore_pix;
50 char *Geometry = "";
51 GC NormalGC;
52 XpmIcon wmgen;
53 Pixmap pixmask;
60 * flush_expose
62 static int flush_expose(Window w) {
64 XEvent dummy;
65 int i=0;
67 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
68 i++;
70 return i;
83 * RedrawWindow
84 * RedrawWindowXY
86 void RedrawWindow(void) {
88 flush_expose(iconwin);
89 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, 0,0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
91 flush_expose(win);
92 XCopyArea(display, wmgen.pixmap, win, NormalGC, 0,0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
96 void RedrawWindowXY(int x, int y) {
98 flush_expose(iconwin);
99 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, x,y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
101 flush_expose(win);
102 XCopyArea(display, wmgen.pixmap, win, NormalGC, x,y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
113 * copyXPMArea
114 * copyXBMArea
116 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
117 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
120 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
122 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
128 * initXwindow
130 void initXwindow(int argc, char *argv[]){
132 int i;
133 char *display_name = NULL;
135 for (i=1; argv[i]; ++i) {
136 if (!strcmp(argv[i], "-display")) display_name = argv[i+1];
140 if (!(display = XOpenDisplay(display_name))) {
141 fprintf(stderr, "%s: can't open display %s\n",
142 argv[0], XDisplayName(display_name));
143 exit(1);
147 screen = DefaultScreen(display);
148 Root = RootWindow(display, screen);
149 DisplayDepth = DefaultDepth(display, screen);
150 x_fd = XConnectionNumber(display);
159 * openXwindow
161 void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits,
162 int pixmask_width, int pixmask_height, char *BackColor, char *LabelColor,
163 char *DataColor) {
165 unsigned int borderwidth = 1;
166 XClassHint classHint;
167 char *wname = argv[0];
168 XTextProperty name;
169 XGCValues gcv;
170 unsigned long gcm;
171 int dummy=0;
172 XpmColorSymbol cols[3]={ {"BackColor", NULL, 0},
173 {"LabelColor", NULL, 0},
174 {"DataColor", NULL, 0}};
180 * Create Pixmap
182 cols[0].pixel = getColor(BackColor, 1.0);
183 cols[1].pixel = getColor(LabelColor, 1.0);
184 cols[2].pixel = getColor(DataColor, 1.0);
185 wmgen.attributes.numsymbols = 3;
186 wmgen.attributes.colorsymbols = cols;
187 wmgen.attributes.exactColors = False;
188 wmgen.attributes.closeness = 40000;
189 wmgen.attributes.valuemask = XpmReturnPixels | XpmReturnExtensions | XpmColorSymbols
190 | XpmExactColors | XpmCloseness | XpmSize;
191 if (XpmCreatePixmapFromData(display, Root, pixmap_bytes,
192 &(wmgen.pixmap), &(wmgen.mask), &(wmgen.attributes)) != XpmSuccess){
193 fprintf(stderr, "Not enough free colorcells.\n");
194 exit(1);
201 * Create a window
203 mysizehints.flags = USSize | USPosition;
204 mysizehints.x = 0;
205 mysizehints.y = 0;
207 back_pix = getColor("white", 1.0);
208 fore_pix = getColor("black", 1.0);
210 XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
211 &mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
213 mysizehints.width = 64;
214 mysizehints.height = 64;
218 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
219 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
221 iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
222 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
227 * Activate hints
229 XSetWMNormalHints(display, win, &mysizehints);
230 classHint.res_name = wname;
231 classHint.res_class = wname;
232 XSetClassHint(display, win, &classHint);
237 * Set up the xevents that you want the relevent windows to inherit
238 * Currently, its seems that setting KeyPress events here has no
239 * effect. I.e. for some you will need to Grab the focus and then return
240 * it after you are done...
242 XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask
243 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
244 | KeyPressMask | KeyReleaseMask);
245 XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask
246 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
247 | KeyPressMask | KeyReleaseMask);
250 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
251 fprintf(stderr, "%s: can't allocate window name\n", wname);
252 exit(1);
256 XSetWMName(display, win, &name);
259 * Create Graphics Context (GC) for drawing
261 gcm = GCForeground | GCBackground | GCGraphicsExposures;
262 gcv.foreground = fore_pix;
263 gcv.background = back_pix;
264 gcv.graphics_exposures = 0;
265 NormalGC = XCreateGC(display, Root, gcm, &gcv);
269 pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
270 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
271 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
274 mywmhints.initial_state = WithdrawnState;
275 mywmhints.icon_window = iconwin;
276 mywmhints.icon_x = mysizehints.x;
277 mywmhints.icon_y = mysizehints.y;
278 mywmhints.window_group = win;
279 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
282 XSetWMHints(display, win, &mywmhints);
285 XSetCommand(display, win, argv, argc);
286 XMapWindow(display, win);
290 unsigned long getColor(char *ColorName, float fac) {
292 XColor Color;
293 XWindowAttributes Attributes;
295 XGetWindowAttributes(display, Root, &Attributes);
296 Color.pixel = 0;
298 XParseColor(display, Attributes.colormap, ColorName, &Color);
299 Color.red = (unsigned short)(Color.red/fac);
300 Color.blue = (unsigned short)(Color.blue/fac);
301 Color.green = (unsigned short)(Color.green/fac);
302 Color.flags = DoRed | DoGreen | DoBlue;
303 XAllocColor(display, Attributes.colormap, &Color);
305 return Color.pixel;