1 /* $Id: xutils.c,v 1.6 2008/04/30 20:52:42 hacki Exp $ */
4 * Copyright (c) 2005, 2006 Marcus Glocker <marcus@nazgul.ch>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * Those X functions have been taken from the xutils.c file used by almost
21 * every wmdockapp. It has been slightly modificated for our needs and
22 * unused functions have been removed. The original version was written
23 * by Martijn Pieterse <pieterse@xs4all.nl>.
35 #include <X11/extensions/shape.h>
40 * global variables for this file
43 XSizeHints mysizehints
;
45 Pixel back_pix
, fore_pix
;
51 extern char TimeColor
[30];
52 extern char BackgroundColor
[30];
58 flush_expose(Window w
)
63 while (XCheckTypedWindowEvent(display
, w
, Expose
, &dummy
))
73 copyXPMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
)
75 XCopyArea(display
, wmgen
.pixmap
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
,
83 copyXBMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
)
85 XCopyArea(display
, wmgen
.mask
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
,
94 flush_expose(iconwin
);
95 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
, 0, 0,
96 wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
99 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
, 0, 0,
100 wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
107 initXwindow(char *opt_display
)
109 char *display_name
= NULL
;
111 if (opt_display
!= NULL
)
112 display_name
= opt_display
;
114 if (!(display
= XOpenDisplay(display_name
)))
115 errx(1, "Can't open display: %s", XDisplayName(display_name
));
117 screen
= DefaultScreen(display
);
118 Root
= RootWindow(display
, screen
);
119 DisplayDepth
= DefaultDepth(display
, screen
);
120 x_fd
= XConnectionNumber(display
);
127 openXwindow(int argc
, char *argv
[], char *pixmap_bytes
[],
128 unsigned char *pixmask_bits
, int pixmask_width
, int pixmask_height
)
130 int dummy
= 0, red
, grn
, blu
;
131 char *wname
= argv
[0];
132 unsigned int borderwidth
= 1;
135 XClassHint classHint
;
137 XpmColorSymbol cols
[10] = {
139 { "Color1", NULL
, 0 },
140 { "Color2", NULL
, 0 },
141 { "Color3", NULL
, 0 },
142 { "Color4", NULL
, 0 },
143 { "Color5", NULL
, 0 },
144 { "Color6", NULL
, 0 },
145 { "Color7", NULL
, 0 },
146 { "Color8", NULL
, 0 },
147 { "Color9", NULL
, 0 }
153 cols
[0].pixel
= getColor(BackgroundColor
, 1.0000, &red
, &grn
, &blu
);
154 cols
[1].pixel
= getBlendedColor(TimeColor
, 0.1522, red
, grn
, blu
);
155 cols
[2].pixel
= getBlendedColor(TimeColor
, 0.2602, red
, grn
, blu
);
156 cols
[3].pixel
= getBlendedColor(TimeColor
, 0.3761, red
, grn
, blu
);
157 cols
[4].pixel
= getBlendedColor(TimeColor
, 0.4841, red
, grn
, blu
);
158 cols
[5].pixel
= getBlendedColor(TimeColor
, 0.5922, red
, grn
, blu
);
159 cols
[6].pixel
= getBlendedColor(TimeColor
, 0.6980, red
, grn
, blu
);
160 cols
[7].pixel
= getBlendedColor(TimeColor
, 0.7961, red
, grn
, blu
);
161 cols
[8].pixel
= getBlendedColor(TimeColor
, 0.8941, red
, grn
, blu
);
162 cols
[9].pixel
= getBlendedColor(TimeColor
, 1.0000, red
, grn
, blu
);
164 wmgen
.attributes
.numsymbols
= 10;
165 wmgen
.attributes
.colorsymbols
= cols
;
166 wmgen
.attributes
.exactColors
= False
;
167 wmgen
.attributes
.closeness
= 40000;
168 wmgen
.attributes
.valuemask
= XpmReturnPixels
| XpmReturnExtensions
|
169 XpmColorSymbols
| XpmExactColors
| XpmCloseness
| XpmSize
;
171 if (XpmCreatePixmapFromData(display
, Root
, pixmap_bytes
,
172 &(wmgen
.pixmap
), &(wmgen
.mask
), &(wmgen
.attributes
)) != XpmSuccess
)
173 errx(1, "Not enough free colocells\n");
178 mysizehints
.flags
= USSize
| USPosition
;
182 back_pix
= getColor("white", 1.0, &red
, &grn
, &blu
);
183 fore_pix
= getColor("black", 1.0, &red
, &grn
, &blu
);
185 XWMGeometry(display
, screen
, Geometry
, NULL
, borderwidth
, &mysizehints
,
186 &mysizehints
.x
, &mysizehints
.y
, &mysizehints
.width
,
187 &mysizehints
.height
, &dummy
);
189 mysizehints
.width
= 64;
190 mysizehints
.height
= 64;
192 win
= XCreateSimpleWindow(display
, Root
, mysizehints
.x
, mysizehints
.y
,
193 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
,
196 iconwin
= XCreateSimpleWindow(display
, win
, mysizehints
.x
,
197 mysizehints
.y
, mysizehints
.width
, mysizehints
.height
, borderwidth
,
203 XSetWMNormalHints(display
, win
, &mysizehints
);
204 classHint
.res_name
= wname
;
205 classHint
.res_class
= wname
;
206 XSetClassHint(display
, win
, &classHint
);
211 XSelectInput(display
, win
, ButtonPressMask
| ExposureMask
|
212 ButtonReleaseMask
| PointerMotionMask
| StructureNotifyMask
|
213 EnterWindowMask
| LeaveWindowMask
| KeyPressMask
| KeyReleaseMask
);
214 XSelectInput(display
, win
, ButtonPressMask
| ExposureMask
|
215 ButtonReleaseMask
| PointerMotionMask
| StructureNotifyMask
|
216 EnterWindowMask
| LeaveWindowMask
| KeyPressMask
| KeyReleaseMask
);
218 if (XStringListToTextProperty(&wname
, 1, &name
) == 0)
219 errx(1, "Can't allocate window name: %s\n", wname
);
221 XSetWMName(display
, win
, &name
);
224 * create graphics context (gc) for drawing
226 gcm
= GCForeground
| GCBackground
| GCGraphicsExposures
;
227 gcv
.foreground
= fore_pix
;
228 gcv
.background
= back_pix
;
229 gcv
.graphics_exposures
= 0;
230 NormalGC
= XCreateGC(display
, Root
, gcm
, &gcv
);
232 pixmask
= XCreateBitmapFromData(display
, win
, (char *)pixmask_bits
,
233 pixmask_width
, pixmask_height
);
234 XShapeCombineMask(display
, win
, ShapeBounding
, 0, 0, pixmask
,
236 XShapeCombineMask(display
, iconwin
, ShapeBounding
, 0, 0, pixmask
,
239 mywmhints
.initial_state
= WithdrawnState
;
240 mywmhints
.icon_window
= iconwin
;
241 mywmhints
.icon_x
= mysizehints
.x
;
242 mywmhints
.icon_y
= mysizehints
.y
;
243 mywmhints
.window_group
= win
;
244 mywmhints
.flags
= StateHint
| IconWindowHint
| IconPositionHint
|
247 XSetWMHints(display
, win
, &mywmhints
);
249 XSetCommand(display
, win
, argv
, argc
);
250 XMapWindow(display
, win
);
257 getColor(char *ColorName
, float fac
, int *red
, int *grn
, int *blu
)
260 XWindowAttributes Attributes
;
262 XGetWindowAttributes(display
, Root
, &Attributes
);
265 XParseColor(display
, Attributes
.colormap
, ColorName
, &Color
);
266 Color
.red
= (unsigned short)(fac
*(Color
.red
-24) + 24);
267 Color
.blue
= (unsigned short)(fac
*(Color
.blue
-24) + 24);
268 Color
.green
= (unsigned short)(fac
*(Color
.green
-24) + 24);
269 Color
.flags
= DoRed
| DoGreen
| DoBlue
;
270 XAllocColor(display
, Attributes
.colormap
, &Color
);
276 return (Color
.pixel
);
283 getBlendedColor(char *ColorName
, float fac
, int red
, int grn
, int blu
)
286 XWindowAttributes Attributes
;
288 XGetWindowAttributes(display
, Root
, &Attributes
);
291 XParseColor(display
, Attributes
.colormap
, ColorName
, &Color
);
292 Color
.red
= (unsigned short)(fac
*(Color
.red
-red
) + red
);
293 Color
.blue
= (unsigned short)(fac
*(Color
.blue
-grn
) + grn
);
294 Color
.green
= (unsigned short)(fac
*(Color
.green
-blu
) + blu
);
295 Color
.flags
= DoRed
| DoGreen
| DoBlue
;
296 XAllocColor(display
, Attributes
.colormap
, &Color
);
298 return (Color
.pixel
);