2 * xutils.c - A collection of X-windows utilties for creating WindowMAker
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)
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
39 #include <X11/extensions/shape.h>
40 #include <X11/XKBlib.h>
49 XSizeHints mysizehints
;
51 Pixel back_pix
, fore_pix
;
59 * Colors for wmCalClock
61 extern char TimeColor
[30];
62 extern char BackgroundColor
[30];
66 /* Images for keyboard layouts */
67 XImage
*kb_orig_img
[4];
68 XImage
*kb_trans_img
[4];
69 extern int KbLoadedImgs
;
70 extern float KbTransparency
;
73 #define KB_Y_OFFSET 24
74 #define KB_DES_WIDTH 54
75 #define KB_DES_HEIGHT 35
76 #define KB_MAX_GROUPS 4
83 static int flush_expose(Window w
) {
88 while (XCheckTypedWindowEvent(display
, w
, Expose
, &dummy
))
107 void RedrawWindow(void) {
109 flush_expose(iconwin
);
110 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
, 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
113 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
, 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
117 void RedrawWindowXY(int x
, int y
) {
119 flush_expose(iconwin
);
120 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
, x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
123 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
, x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
137 void copyXPMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
138 XCopyArea(display
, wmgen
.pixmap
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
141 void copyXBMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
143 XCopyArea(display
, wmgen
.mask
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
151 void initXwindow(int argc
, char *argv
[]){
154 char *display_name
= NULL
;
156 for (i
=1; argv
[i
]; ++i
) {
157 if (!strcmp(argv
[i
], "-display")) display_name
= argv
[i
+1];
161 if (!(display
= XOpenDisplay(display_name
))) {
162 fprintf(stderr
, "%s: can't open display %s\n",
163 argv
[0], XDisplayName(display_name
));
168 screen
= DefaultScreen(display
);
169 Root
= RootWindow(display
, screen
);
170 DisplayDepth
= DefaultDepth(display
, screen
);
171 x_fd
= XConnectionNumber(display
);
182 void openXwindow(int argc
, char *argv
[], char *pixmap_bytes
[], char *pixmask_bits
,
183 int pixmask_width
, int pixmask_height
) {
185 unsigned int borderwidth
= 1;
186 XClassHint classHint
;
187 char *wname
= argv
[0];
191 int dummy
=0, red
, grn
, blu
;
192 XpmColorSymbol cols
[10]={ {"Back", NULL
, 0},
201 {"Color9", NULL
, 0}};
209 cols
[0].pixel
= getColor(BackgroundColor
, 1.0000, &red
, &grn
, &blu
);
210 cols
[1].pixel
= getBlendedColor(TimeColor
, 0.1522, red
, grn
, blu
);
211 cols
[2].pixel
= getBlendedColor(TimeColor
, 0.2602, red
, grn
, blu
);
212 cols
[3].pixel
= getBlendedColor(TimeColor
, 0.3761, red
, grn
, blu
);
213 cols
[4].pixel
= getBlendedColor(TimeColor
, 0.4841, red
, grn
, blu
);
214 cols
[5].pixel
= getBlendedColor(TimeColor
, 0.5922, red
, grn
, blu
);
215 cols
[6].pixel
= getBlendedColor(TimeColor
, 0.6980, red
, grn
, blu
);
216 cols
[7].pixel
= getBlendedColor(TimeColor
, 0.7961, red
, grn
, blu
);
217 cols
[8].pixel
= getBlendedColor(TimeColor
, 0.8941, red
, grn
, blu
);
218 cols
[9].pixel
= getBlendedColor(TimeColor
, 1.0000, red
, grn
, blu
);
220 wmgen
.attributes
.numsymbols
= 10;
221 wmgen
.attributes
.colorsymbols
= cols
;
222 wmgen
.attributes
.exactColors
= False
;
223 wmgen
.attributes
.closeness
= 40000;
224 wmgen
.attributes
.valuemask
= XpmReturnPixels
| XpmReturnExtensions
| XpmColorSymbols
225 | XpmExactColors
| XpmCloseness
| XpmSize
;
226 if (XpmCreatePixmapFromData(display
, Root
, pixmap_bytes
,
227 &(wmgen
.pixmap
), &(wmgen
.mask
), &(wmgen
.attributes
)) != XpmSuccess
){
228 fprintf(stderr
, "Not enough free colorcells.\n");
238 mysizehints
.flags
= USSize
| USPosition
;
242 back_pix
= getColor("white", 1.0, &red
, &grn
, &blu
);
243 fore_pix
= getColor("black", 1.0, &red
, &grn
, &blu
);
245 XWMGeometry(display
, screen
, Geometry
, NULL
, borderwidth
, &mysizehints
,
246 &mysizehints
.x
, &mysizehints
.y
,&mysizehints
.width
,&mysizehints
.height
, &dummy
);
248 mysizehints
.width
= 64;
249 mysizehints
.height
= 64;
253 win
= XCreateSimpleWindow(display
, Root
, mysizehints
.x
, mysizehints
.y
,
254 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
256 iconwin
= XCreateSimpleWindow(display
, win
, mysizehints
.x
, mysizehints
.y
,
257 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
264 XSetWMNormalHints(display
, win
, &mysizehints
);
265 classHint
.res_name
= wname
;
266 classHint
.res_class
= wname
;
267 XSetClassHint(display
, win
, &classHint
);
272 * Set up the xevents that you want the relevent windows to inherit
273 * Currently, its seems that setting KeyPress events here has no
274 * effect. I.e. for some you will need to Grab the focus and then return
275 * it after you are done...
277 XSelectInput(display
, win
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
278 | PointerMotionMask
| StructureNotifyMask
| EnterWindowMask
| LeaveWindowMask
279 | KeyPressMask
| KeyReleaseMask
);
280 XSelectInput(display
, iconwin
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
281 | PointerMotionMask
| StructureNotifyMask
| EnterWindowMask
| LeaveWindowMask
282 | KeyPressMask
| KeyReleaseMask
);
285 if (XStringListToTextProperty(&wname
, 1, &name
) == 0) {
286 fprintf(stderr
, "%s: can't allocate window name\n", wname
);
291 XSetWMName(display
, win
, &name
);
294 * Create Graphics Context (GC) for drawing
296 gcm
= GCForeground
| GCBackground
| GCGraphicsExposures
;
297 gcv
.foreground
= fore_pix
;
298 gcv
.background
= back_pix
;
299 gcv
.graphics_exposures
= 0;
300 NormalGC
= 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
) {
328 XWindowAttributes Attributes
;
330 XGetWindowAttributes(display
, Root
, &Attributes
);
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
);
348 unsigned long getBlendedColor(char *ColorName
, float fac
, int red
, int grn
, int blu
) {
351 XWindowAttributes Attributes
;
353 XGetWindowAttributes(display
, Root
, &Attributes
);
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
);
368 int LoadKbImg(char *names
)
370 char tmp
[1024], *point
= NULL
, aux
[1024];
375 strncpy(tmp
, names
, 1024);
376 if (tmp
[1023] != '\0') {
377 fprintf(stderr
, "wmCalClock: Error parsing image names for keyboard layouts.\n");
382 /* load images from file */
383 point
= strtok(tmp
, ",");
384 for (i
= 0; i
< KB_MAX_GROUPS
; i
++) {
386 if ((point
[0] == '.') || (point
[0] == '/')) {
389 strcpy(aux
, KB_PIXMAP_PREFIX
);
390 strncat(aux
, point
, 1024 - strlen(aux
) - 1);
393 if (XpmReadFileToImage(display
, aux
, &kb_orig_img
[i
], NULL
, &attr
) != XpmSuccess
) {
394 fprintf(stderr
, "wmCalClock: Could not read flag image %s.\n",
399 if ((attr
.width
!= KB_DES_WIDTH
) || (attr
.height
!= KB_DES_HEIGHT
)) {
400 fprintf(stderr
, "wmCalClock: %s hasn't %dx%d dimension.",
401 point
, KB_DES_WIDTH
, KB_DES_HEIGHT
);
405 kb_trans_img
[i
] = XSubImage(kb_orig_img
[i
], 0, 0, KB_DES_WIDTH
, KB_DES_HEIGHT
);
409 point
= strtok(NULL
, ",");
413 return ((i
> KB_MAX_GROUPS
) ? KB_MAX_GROUPS
: i
);
417 void CreateKbTranImgs(void)
421 XColor c_o
, c_n
; /* original and new color */
422 XWindowAttributes attributes
;
423 float KT
= KbTransparency
;
425 XGetWindowAttributes(display
, Root
, &attributes
);
426 image
= XGetImage(display
, wmgen
.pixmap
, KB_X_OFFSET
, KB_Y_OFFSET
,
427 KB_DES_WIDTH
, KB_DES_HEIGHT
, -1, ZPixmap
);
429 for (i
= 0; i
< KB_DES_WIDTH
; i
++) {
430 for (j
= 0; j
< KB_DES_HEIGHT
; j
++) {
431 c_o
.pixel
= XGetPixel(image
, i
, j
);
432 XQueryColor(display
, attributes
.colormap
, &c_o
);
434 for (k
= 0; k
< KbLoadedImgs
; k
++) {
435 c_n
.pixel
= XGetPixel(kb_orig_img
[k
], i
, j
);
436 XQueryColor(display
, attributes
.colormap
, &c_n
);
437 c_n
.red
= c_o
.red
* (1 - KT
) + c_n
.red
* KT
;
438 c_n
.green
= c_o
.green
* (1 - KT
) + c_n
.green
* KT
;
439 c_n
.blue
= c_o
.blue
* (1 - KT
) + c_n
.blue
* KT
;
440 XAllocColor(display
, attributes
.colormap
, &c_n
);
441 XPutPixel(kb_trans_img
[k
], i
, j
, c_n
.pixel
);
446 XDestroyImage(image
);
450 void ShowGroupImage(int group
)
452 XPutImage(display
, wmgen
.pixmap
, NormalGC
, kb_trans_img
[group
],
453 0, 0, KB_X_OFFSET
, KB_Y_OFFSET
, KB_DES_WIDTH
, KB_DES_HEIGHT
);