Make AddMouseRegion's index unsigned
[dockapps.git] / wmcalclockkbd / src / xutils.c
blobc2e5456db78b506c6227c3442b042d8eb0c3d7ab
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., 59 Temple Place - Suite 330,
25 * Boston, MA 02111-1307, 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 <X11/XKBlib.h>
41 #include "xutils.h"
46 * X11 Variables
48 int x_fd;
49 XSizeHints mysizehints;
50 XWMHints mywmhints;
51 Pixel back_pix, fore_pix;
52 char *Geometry = "";
53 GC NormalGC;
54 XpmIcon wmgen;
55 Pixmap pixmask;
57 Display *display;
58 Window Root;
59 Window iconwin, win;
60 int screen;
61 int DisplayDepth;
64 * Colors for wmCalClock
66 extern char TimeColor[30];
67 extern char BackgroundColor[30];
71 /* Images for keyboard layouts */
72 XImage *kb_orig_img[4];
73 XImage *kb_trans_img[4];
74 extern int KbLoadedImgs;
75 extern float KbTransparency;
77 #define KB_X_OFFSET 5
78 #define KB_Y_OFFSET 24
79 #define KB_DES_WIDTH 54
80 #define KB_DES_HEIGHT 35
81 #define KB_MAX_GROUPS 4
86 * flush_expose
88 static int flush_expose(Window w) {
90 XEvent dummy;
91 int i=0;
93 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
94 i++;
96 return i;
109 * RedrawWindow
110 * RedrawWindowXY
112 void RedrawWindow(void) {
114 flush_expose(iconwin);
115 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, 0,0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
117 flush_expose(win);
118 XCopyArea(display, wmgen.pixmap, win, NormalGC, 0,0, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
122 void RedrawWindowXY(int x, int y) {
124 flush_expose(iconwin);
125 XCopyArea(display, wmgen.pixmap, iconwin, NormalGC, x,y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
127 flush_expose(win);
128 XCopyArea(display, wmgen.pixmap, win, NormalGC, x,y, wmgen.attributes.width, wmgen.attributes.height, 0, 0);
139 * copyXPMArea
140 * copyXBMArea
142 void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
143 XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
146 void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
148 XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
154 * initXwindow
156 void initXwindow(int argc, char *argv[]){
158 int i;
159 char *display_name = NULL;
161 for (i=1; argv[i]; ++i) {
162 if (!strcmp(argv[i], "-display")) display_name = argv[i+1];
166 if (!(display = XOpenDisplay(display_name))) {
167 fprintf(stderr, "%s: can't open display %s\n",
168 argv[0], XDisplayName(display_name));
169 exit(1);
173 screen = DefaultScreen(display);
174 Root = RootWindow(display, screen);
175 DisplayDepth = DefaultDepth(display, screen);
176 x_fd = XConnectionNumber(display);
185 * openXwindow
187 void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits,
188 int pixmask_width, int pixmask_height) {
190 unsigned int borderwidth = 1;
191 XClassHint classHint;
192 char *wname = argv[0];
193 XTextProperty name;
194 XGCValues gcv;
195 unsigned long gcm;
196 int dummy=0, red, grn, blu;
197 XpmColorSymbol cols[10]={ {"Back", NULL, 0},
198 {"Color1", NULL, 0},
199 {"Color2", NULL, 0},
200 {"Color3", NULL, 0},
201 {"Color4", NULL, 0},
202 {"Color5", NULL, 0},
203 {"Color6", NULL, 0},
204 {"Color7", NULL, 0},
205 {"Color8", NULL, 0},
206 {"Color9", NULL, 0}};
212 * Create Pixmap
214 cols[0].pixel = getColor(BackgroundColor, 1.0000, &red, &grn, &blu);
215 cols[1].pixel = getBlendedColor(TimeColor, 0.1522, red, grn, blu);
216 cols[2].pixel = getBlendedColor(TimeColor, 0.2602, red, grn, blu);
217 cols[3].pixel = getBlendedColor(TimeColor, 0.3761, red, grn, blu);
218 cols[4].pixel = getBlendedColor(TimeColor, 0.4841, red, grn, blu);
219 cols[5].pixel = getBlendedColor(TimeColor, 0.5922, red, grn, blu);
220 cols[6].pixel = getBlendedColor(TimeColor, 0.6980, red, grn, blu);
221 cols[7].pixel = getBlendedColor(TimeColor, 0.7961, red, grn, blu);
222 cols[8].pixel = getBlendedColor(TimeColor, 0.8941, red, grn, blu);
223 cols[9].pixel = getBlendedColor(TimeColor, 1.0000, red, grn, blu);
225 wmgen.attributes.numsymbols = 10;
226 wmgen.attributes.colorsymbols = cols;
227 wmgen.attributes.exactColors = False;
228 wmgen.attributes.closeness = 40000;
229 wmgen.attributes.valuemask = XpmReturnPixels | XpmReturnExtensions | XpmColorSymbols
230 | XpmExactColors | XpmCloseness | XpmSize;
231 if (XpmCreatePixmapFromData(display, Root, pixmap_bytes,
232 &(wmgen.pixmap), &(wmgen.mask), &(wmgen.attributes)) != XpmSuccess){
233 fprintf(stderr, "Not enough free colorcells.\n");
234 exit(1);
241 * Create a window
243 mysizehints.flags = USSize | USPosition;
244 mysizehints.x = 0;
245 mysizehints.y = 0;
247 back_pix = getColor("white", 1.0, &red, &grn, &blu);
248 fore_pix = getColor("black", 1.0, &red, &grn, &blu);
250 XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
251 &mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
253 mysizehints.width = 64;
254 mysizehints.height = 64;
258 win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
259 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
261 iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
262 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
267 * Activate hints
269 XSetWMNormalHints(display, win, &mysizehints);
270 classHint.res_name = wname;
271 classHint.res_class = wname;
272 XSetClassHint(display, win, &classHint);
277 * Set up the xevents that you want the relevent windows to inherit
278 * Currently, its seems that setting KeyPress events here has no
279 * effect. I.e. for some you will need to Grab the focus and then return
280 * it after you are done...
282 XSelectInput(display, win, ButtonPressMask | ExposureMask | ButtonReleaseMask
283 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
284 | KeyPressMask | KeyReleaseMask);
285 XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask
286 | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask
287 | KeyPressMask | KeyReleaseMask);
290 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
291 fprintf(stderr, "%s: can't allocate window name\n", wname);
292 exit(1);
296 XSetWMName(display, win, &name);
299 * Create Graphics Context (GC) for drawing
301 gcm = GCForeground | GCBackground | GCGraphicsExposures;
302 gcv.foreground = fore_pix;
303 gcv.background = back_pix;
304 gcv.graphics_exposures = 0;
305 NormalGC = XCreateGC(display, Root, gcm, &gcv);
309 pixmask = XCreateBitmapFromData(display, win, pixmask_bits, pixmask_width, pixmask_height);
310 XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
311 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
314 mywmhints.initial_state = WithdrawnState;
315 mywmhints.icon_window = iconwin;
316 mywmhints.icon_x = mysizehints.x;
317 mywmhints.icon_y = mysizehints.y;
318 mywmhints.window_group = win;
319 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
322 XSetWMHints(display, win, &mywmhints);
325 XSetCommand(display, win, argv, argc);
326 XMapWindow(display, win);
330 unsigned long getColor(char *ColorName, float fac, int *red, int *grn, int *blu) {
332 XColor Color;
333 XWindowAttributes Attributes;
335 XGetWindowAttributes(display, Root, &Attributes);
336 Color.pixel = 0;
338 XParseColor(display, Attributes.colormap, ColorName, &Color);
339 Color.red = (unsigned short)(fac*(Color.red-24) + 24);
340 Color.blue = (unsigned short)(fac*(Color.blue-24) + 24);
341 Color.green = (unsigned short)(fac*(Color.green-24) + 24);
342 Color.flags = DoRed | DoGreen | DoBlue;
343 XAllocColor(display, Attributes.colormap, &Color);
346 *red = Color.red;
347 *grn = Color.green;
348 *blu = Color.blue;
349 return Color.pixel;
353 unsigned long getBlendedColor(char *ColorName, float fac, int red, int grn, int blu) {
355 XColor Color;
356 XWindowAttributes Attributes;
358 XGetWindowAttributes(display, Root, &Attributes);
359 Color.pixel = 0;
361 XParseColor(display, Attributes.colormap, ColorName, &Color);
362 Color.red = (unsigned short)(fac*(Color.red-red) + red);
363 Color.blue = (unsigned short)(fac*(Color.blue-grn) + grn);
364 Color.green = (unsigned short)(fac*(Color.green-blu) + blu);
365 Color.flags = DoRed | DoGreen | DoBlue;
366 XAllocColor(display, Attributes.colormap, &Color);
368 return Color.pixel;
373 int LoadKbImg(char *names)
375 char tmp[1024], *point = NULL, aux[1024];
376 int i;
377 XpmAttributes attr;
379 tmp[1023] = '\0';
380 strncpy(tmp, names, 1024);
381 if (tmp[1023] != '\0') {
382 fprintf(stderr, "wmCalClock: Error parsing image names for keyboard layouts.\n");
384 return (-1);
387 /* load images from file */
388 point = strtok(tmp, ",");
389 for (i = 0; i < KB_MAX_GROUPS; i++) {
390 if (point) {
391 if ((point[0] == '.') || (point[0] == '/')) {
392 strcpy(aux, point);
393 } else {
394 strcpy(aux, KB_PIXMAP_PREFIX);
395 strncat(aux, point, 1024 - strlen(aux) - 1);
397 attr.valuemask = 0;
398 if (XpmReadFileToImage(display, aux, &kb_orig_img[i], NULL, &attr) != XpmSuccess) {
399 fprintf(stderr, "wmCalClock: Could not read flag image %s.\n",
400 point);
402 return (-1);
404 if ((attr.width != KB_DES_WIDTH) || (attr.height != KB_DES_HEIGHT)) {
405 fprintf(stderr, "wmCalClock: %s hasn't %dx%d dimension.",
406 point, KB_DES_WIDTH, KB_DES_HEIGHT);
408 return (-1);
410 kb_trans_img[i] = XSubImage(kb_orig_img[i], 0, 0, KB_DES_WIDTH, KB_DES_HEIGHT);
411 } else {
412 break;
414 point = strtok(NULL, ",");
418 return ((i > KB_MAX_GROUPS) ? KB_MAX_GROUPS : i);
422 void CreateKbTranImgs(void)
424 int i, j, k;
425 XImage *image;
426 XColor c_o, c_n; /* original and new color */
427 XWindowAttributes attributes;
428 float KT = KbTransparency;
430 XGetWindowAttributes(display, Root, &attributes);
431 image = XGetImage(display, wmgen.pixmap, KB_X_OFFSET, KB_Y_OFFSET,
432 KB_DES_WIDTH, KB_DES_HEIGHT, -1, ZPixmap);
434 for (i = 0; i < KB_DES_WIDTH; i++) {
435 for (j = 0; j < KB_DES_HEIGHT; j++) {
436 c_o.pixel = XGetPixel(image, i, j);
437 XQueryColor(display, attributes.colormap, &c_o);
439 for (k = 0; k < KbLoadedImgs; k++) {
440 c_n.pixel = XGetPixel(kb_orig_img[k], i, j);
441 XQueryColor(display, attributes.colormap, &c_n);
442 c_n.red = c_o.red * (1 - KT) + c_n.red * KT;
443 c_n.green = c_o.green * (1 - KT) + c_n.green * KT;
444 c_n.blue = c_o.blue * (1 - KT) + c_n.blue * KT;
445 XAllocColor(display, attributes.colormap, &c_n);
446 XPutPixel(kb_trans_img[k], i, j, c_n.pixel);
451 XDestroyImage(image);
455 void ShowGroupImage(int group)
457 XPutImage(display, wmgen.pixmap, NormalGC, kb_trans_img[group],
458 0, 0, KB_X_OFFSET, KB_Y_OFFSET, KB_DES_WIDTH, KB_DES_HEIGHT);