wmclockmon: update change-log
[dockapps.git] / wmmoonclock / src / xutils.c
blobdc12f939f8f1e4ffeafa97c8b49f2e3686c7fcfd
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 * Global variable
46 Display *display;
47 Window Root;
48 Window iconwin, win;
49 int screen;
50 int DisplayDepth;
53 * X11 Variables
55 static int x_fd;
56 static XSizeHints mysizehints;
57 static XWMHints mywmhints;
58 static Pixel back_pix, fore_pix;
59 static char *Geometry = "";
60 static GC NormalGC;
61 static XpmIcon wmgen;
62 static Pixmap pixmask;
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, char *BackColor, char *LabelColor,
172 char *DataColor) {
174 unsigned int borderwidth = 1;
175 XClassHint classHint;
176 char *wname = argv[0];
177 XTextProperty name;
178 XGCValues gcv;
179 unsigned long gcm;
180 int dummy=0;
181 XpmColorSymbol cols[3]={ {"BackColor", NULL, 0},
182 {"LabelColor", NULL, 0},
183 {"DataColor", NULL, 0}};
189 * Create Pixmap
191 cols[0].pixel = getColor(BackColor, 1.0);
192 cols[1].pixel = getColor(LabelColor, 1.0);
193 cols[2].pixel = getColor(DataColor, 1.0);
194 wmgen.attributes.numsymbols = 3;
195 wmgen.attributes.colorsymbols = cols;
196 wmgen.attributes.exactColors = False;
197 wmgen.attributes.closeness = 40000;
198 wmgen.attributes.valuemask = XpmReturnPixels | XpmReturnExtensions | XpmColorSymbols
199 | XpmExactColors | XpmCloseness | XpmSize;
200 if (XpmCreatePixmapFromData(display, Root, pixmap_bytes,
201 &(wmgen.pixmap), &(wmgen.mask), &(wmgen.attributes)) != XpmSuccess){
202 fprintf(stderr, "Not enough free colorcells.\n");
203 exit(1);
210 * Create a window
212 mysizehints.flags = USSize | USPosition;
213 mysizehints.x = 0;
214 mysizehints.y = 0;
216 back_pix = getColor("white", 1.0);
217 fore_pix = getColor("black", 1.0);
219 XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
220 &mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
222 mysizehints.width = 64;
223 mysizehints.height = 64;
227 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
228 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
230 iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
231 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
236 * Activate hints
238 XSetWMNormalHints(display, win, &mysizehints);
239 classHint.res_name = wname;
240 classHint.res_class = wname;
241 XSetClassHint(display, win, &classHint);
246 * Set up the xevents that you want the relevent windows to inherit
247 * Currently, its seems that setting KeyPress events here has no
248 * effect. I.e. for some you will need to Grab the focus and then return
249 * it after you are done...
251 XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask
252 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
253 | KeyPressMask | KeyReleaseMask);
254 XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask
255 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
256 | KeyPressMask | KeyReleaseMask);
259 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
260 fprintf(stderr, "%s: can't allocate window name\n", wname);
261 exit(1);
265 XSetWMName(display, win, &name);
268 * Create Graphics Context (GC) for drawing
270 gcm = GCForeground | GCBackground | GCGraphicsExposures;
271 gcv.foreground = fore_pix;
272 gcv.background = back_pix;
273 gcv.graphics_exposures = 0;
274 NormalGC = XCreateGC(display, Root, gcm, &gcv);
278 pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
279 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
280 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
283 mywmhints.initial_state = WithdrawnState;
284 mywmhints.icon_window = iconwin;
285 mywmhints.icon_x = mysizehints.x;
286 mywmhints.icon_y = mysizehints.y;
287 mywmhints.window_group = win;
288 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
291 XSetWMHints(display, win, &mywmhints);
294 XSetCommand(display, win, argv, argc);
295 XMapWindow(display, win);
299 unsigned long getColor(char *ColorName, float fac) {
301 XColor Color;
302 XWindowAttributes Attributes;
304 XGetWindowAttributes(display, Root, &Attributes);
305 Color.pixel = 0;
307 XParseColor(display, Attributes.colormap, ColorName, &Color);
308 Color.red = (unsigned short)(Color.red/fac);
309 Color.blue = (unsigned short)(Color.blue/fac);
310 Color.green = (unsigned short)(Color.green/fac);
311 Color.flags = DoRed | DoGreen | DoBlue;
312 XAllocColor(display, Attributes.colormap, &Color);
314 return Color.pixel;