Better select(2) usage
[dockapps.git] / wmMatrix / xutils.c
blob6adb87ba96ba435664752bfa264320b184013d79
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
30 */
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 <sys/time.h>
41 #include <sys/select.h>
42 #include "xutils.h"
47 * X11 Variables
49 int x_fd;
50 XSizeHints mysizehints;
51 XWMHints mywmhints;
52 Pixel back_pix, fore_pix;
53 char *Geometry = "";
54 GC NormalGC;
55 GC EraseGC;
56 XpmIcon wmgen;
57 Pixmap pixmask;
61 * Colors for wmCalClock
63 extern char TimeColor[30];
64 extern char BackgroundColor[30];
72 * flush_expose
74 static int flush_expose(Window w) {
76 XEvent dummy;
77 int i=0;
79 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
80 i++;
82 return i;
95 * RedrawWindow
96 * RedrawWindowXY
98 void RedrawWindow(void) {
100 flush_expose(iconwin);
101 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, 0,0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
103 flush_expose(win);
104 XCopyArea(display, wmgen.pixmap, win, NormalGC, 0,0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
108 void RedrawWindowXY(int x, int y) {
110 flush_expose(iconwin);
111 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, x,y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
113 flush_expose(win);
114 XCopyArea(display, wmgen.pixmap, win, NormalGC, x,y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
125 * copyXPMArea
126 * copyXBMArea
128 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
129 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
132 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
134 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
140 * initXwindow
142 void initXwindow(int argc, char *argv[]){
144 int i;
145 char *display_name = NULL;
147 for (i=1; argv[i]; ++i) {
148 if (!strcmp(argv[i], "-display")) display_name = argv[i+1];
152 if (!(display = XOpenDisplay(display_name))) {
153 fprintf(stderr, "%s: can't open display %s\n",
154 argv[0], XDisplayName(display_name));
155 exit(1);
159 screen = DefaultScreen(display);
160 Root = RootWindow(display, screen);
161 DisplayDepth = DefaultDepth(display, screen);
162 x_fd = XConnectionNumber(display);
171 * openXwindow
173 void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits,
174 int pixmask_width, int pixmask_height) {
176 unsigned int borderwidth = 1;
177 XClassHint classHint;
178 char *wname = argv[0];
179 XTextProperty name;
180 XGCValues gcv;
181 unsigned long gcm;
182 int dummy=0, red, grn, blu;
183 XpmColorSymbol cols[10]={ {"Back", NULL, 0},
184 {"Color1", NULL, 0},
185 {"Color2", NULL, 0},
186 {"Color3", NULL, 0},
187 {"Color4", NULL, 0},
188 {"Color5", NULL, 0},
189 {"Color6", NULL, 0},
190 {"Color7", NULL, 0},
191 {"Color8", NULL, 0},
192 {"Color9", NULL, 0}};
198 * Create Pixmap
200 cols[0].pixel = getColor(BackgroundColor, 1.0000, &red, &grn, &blu);
201 cols[1].pixel = getBlendedColor(TimeColor, 0.1522, red, grn, blu);
202 cols[2].pixel = getBlendedColor(TimeColor, 0.2602, red, grn, blu);
203 cols[3].pixel = getBlendedColor(TimeColor, 0.3761, red, grn, blu);
204 cols[4].pixel = getBlendedColor(TimeColor, 0.4841, red, grn, blu);
205 cols[5].pixel = getBlendedColor(TimeColor, 0.5922, red, grn, blu);
206 cols[6].pixel = getBlendedColor(TimeColor, 0.6980, red, grn, blu);
207 cols[7].pixel = getBlendedColor(TimeColor, 0.7961, red, grn, blu);
208 cols[8].pixel = getBlendedColor(TimeColor, 0.8941, red, grn, blu);
209 cols[9].pixel = getBlendedColor(TimeColor, 1.0000, red, grn, blu);
211 wmgen.attributes.numsymbols = 10;
212 wmgen.attributes.colorsymbols = cols;
213 wmgen.attributes.exactColors = False;
214 wmgen.attributes.closeness = 40000;
215 wmgen.attributes.valuemask = XpmReturnPixels | XpmReturnExtensions | XpmColorSymbols
216 | XpmExactColors | XpmCloseness | XpmSize;
217 if (XpmCreatePixmapFromData(display, Root, pixmap_bytes,
218 &(wmgen.pixmap), &(wmgen.mask), &(wmgen.attributes)) != XpmSuccess){
219 fprintf(stderr, "Not enough free colorcells.\n");
220 exit(1);
227 * Create a window
229 mysizehints.flags = USSize | USPosition;
230 mysizehints.x = 0;
231 mysizehints.y = 0;
233 back_pix = getColor("black", 1.0, &red, &grn, &blu);
234 fore_pix = getColor("white", 1.0, &red, &grn, &blu);
236 XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
237 &mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
239 mysizehints.width = 64;
240 mysizehints.height = 64;
244 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
245 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
247 iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
248 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
253 * Activate hints
255 XSetWMNormalHints(display, win, &mysizehints);
256 classHint.res_name = wname;
257 classHint.res_class = wname;
258 XSetClassHint(display, win, &classHint);
263 * Set up the xevents that you want the relevent windows to inherit
264 * Currently, its seems that setting KeyPress events here has no
265 * effect. I.e. for some you will need to Grab the focus and then return
266 * it after you are done...
268 XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask
269 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
270 | KeyPressMask | KeyReleaseMask);
271 XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask
272 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
273 | KeyPressMask | KeyReleaseMask);
276 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
277 fprintf(stderr, "%s: can't allocate window name\n", wname);
278 exit(1);
282 XSetWMName(display, win, &name);
285 * Create Graphics Context (GC) for drawing
287 gcm = GCForeground | GCBackground | GCGraphicsExposures;
288 gcv.foreground = fore_pix;
289 gcv.background = back_pix;
290 gcv.graphics_exposures = 0;
291 NormalGC = XCreateGC(display, Root, gcm, &gcv);
294 * Create Graphics Context (GC) for erasing
296 gcm = GCForeground | GCBackground | GCGraphicsExposures;
297 gcv.foreground = back_pix;
298 gcv.background = back_pix;
299 gcv.graphics_exposures = 0;
300 EraseGC = XCreateGC(display, Root, gcm, &gcv);
304 pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
305 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
306 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
309 mywmhints.initial_state = WithdrawnState;
310 mywmhints.icon_window = iconwin;
311 mywmhints.icon_x = mysizehints.x;
312 mywmhints.icon_y = mysizehints.y;
313 mywmhints.window_group = win;
314 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
317 XSetWMHints(display, win, &mywmhints);
320 XSetCommand(display, win, argv, argc);
321 XMapWindow(display, win);
325 unsigned long getColor(char *ColorName, float fac, int *red, int *grn, int *blu) {
327 XColor Color;
328 XWindowAttributes Attributes;
330 XGetWindowAttributes(display, Root, &Attributes);
331 Color.pixel = 0;
333 XParseColor(display, Attributes.colormap, ColorName, &Color);
334 Color.red = (unsigned short)(fac*(Color.red-24) + 24);
335 Color.blue = (unsigned short)(fac*(Color.blue-24) + 24);
336 Color.green = (unsigned short)(fac*(Color.green-24) + 24);
337 Color.flags = DoRed | DoGreen | DoBlue;
338 XAllocColor(display, Attributes.colormap, &Color);
341 *red = Color.red;
342 *grn = Color.green;
343 *blu = Color.blue;
344 return Color.pixel;
348 unsigned long getBlendedColor(char *ColorName, float fac, int red, int grn, int blu) {
350 XColor Color;
351 XWindowAttributes Attributes;
353 XGetWindowAttributes(display, Root, &Attributes);
354 Color.pixel = 0;
356 XParseColor(display, Attributes.colormap, ColorName, &Color);
357 Color.red = (unsigned short)(fac*(Color.red-red) + red);
358 Color.blue = (unsigned short)(fac*(Color.blue-grn) + grn);
359 Color.green = (unsigned short)(fac*(Color.green-blu) + blu);
360 Color.flags = DoRed | DoGreen | DoBlue;
361 XAllocColor(display, Attributes.colormap, &Color);
363 return Color.pixel;
369 * Here is a faster version of usleep using select()
371 void uusleep(unsigned long usecs) {
373 struct timeval tv;
374 fd_set fds;
375 tv.tv_sec = usecs / 1000000UL;
376 tv.tv_usec = usecs % 1000000UL;
377 FD_ZERO(&fds);
378 FD_SET(x_fd, &fds);
379 select(x_fd + 1, &fds, NULL, NULL, &tv);
384 * This version assumes the tv_sec value will be zero. I.e. the delay
385 * will be less than 1 second. This allows us to save on a div operation.
388 void short_uusleep(unsigned long usecs) {
390 struct timeval tv;
391 fd_set fds;
392 tv.tv_sec = 0;
393 tv.tv_usec = usecs;
394 FD_ZERO(&fds);
395 FD_SET(x_fd, &fds);
396 select(x_fd + 1, &fds, NULL, NULL, &tv);