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
;
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
;
78 #define KB_Y_OFFSET 24
79 #define KB_DES_WIDTH 54
80 #define KB_DES_HEIGHT 35
81 #define KB_MAX_GROUPS 4
88 static int flush_expose(Window w
) {
93 while (XCheckTypedWindowEvent(display
, w
, Expose
, &dummy
))
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);
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);
128 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
, x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
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
);
156 void initXwindow(int argc
, char *argv
[]){
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
));
173 screen
= DefaultScreen(display
);
174 Root
= RootWindow(display
, screen
);
175 DisplayDepth
= DefaultDepth(display
, screen
);
176 x_fd
= XConnectionNumber(display
);
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];
196 int dummy
=0, red
, grn
, blu
;
197 XpmColorSymbol cols
[10]={ {"Back", NULL
, 0},
206 {"Color9", NULL
, 0}};
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");
243 mysizehints
.flags
= USSize
| USPosition
;
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
);
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
);
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
) {
333 XWindowAttributes Attributes
;
335 XGetWindowAttributes(display
, Root
, &Attributes
);
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
);
353 unsigned long getBlendedColor(char *ColorName
, float fac
, int red
, int grn
, int blu
) {
356 XWindowAttributes Attributes
;
358 XGetWindowAttributes(display
, Root
, &Attributes
);
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
);
373 int LoadKbImg(char *names
)
375 char tmp
[1024], *point
= NULL
, aux
[1024];
380 strncpy(tmp
, names
, 1024);
381 if (tmp
[1023] != '\0') {
382 fprintf(stderr
, "wmCalClock: Error parsing image names for keyboard layouts.\n");
387 /* load images from file */
388 point
= strtok(tmp
, ",");
389 for (i
= 0; i
< KB_MAX_GROUPS
; i
++) {
391 if ((point
[0] == '.') || (point
[0] == '/')) {
394 strcpy(aux
, KB_PIXMAP_PREFIX
);
395 strncat(aux
, point
, 1024 - strlen(aux
) - 1);
398 if (XpmReadFileToImage(display
, aux
, &kb_orig_img
[i
], NULL
, &attr
) != XpmSuccess
) {
399 fprintf(stderr
, "wmCalClock: Could not read flag image %s.\n",
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
);
410 kb_trans_img
[i
] = XSubImage(kb_orig_img
[i
], 0, 0, KB_DES_WIDTH
, KB_DES_HEIGHT
);
414 point
= strtok(NULL
, ",");
418 return ((i
> KB_MAX_GROUPS
) ? KB_MAX_GROUPS
: i
);
422 void CreateKbTranImgs(void)
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
);