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., 51 Franklin Street, Fifth Floor,
25 * Boston, MA 02110-1301 USA
39 #include <X11/extensions/shape.h>
48 XSizeHints mysizehints
;
50 Pixel back_pix
, fore_pix
;
63 * Colors for wmCalClock
65 extern char TimeColor
[30];
66 extern char BackgroundColor
[30];
76 static int flush_expose(Window w
) {
81 while (XCheckTypedWindowEvent(display
, w
, Expose
, &dummy
))
100 void RedrawWindow(void) {
102 flush_expose(iconwin
);
103 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
, 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
106 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
, 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
110 void RedrawWindowXY(int x
, int y
) {
112 flush_expose(iconwin
);
113 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
, x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
116 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
, x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
130 void copyXPMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
131 XCopyArea(display
, wmgen
.pixmap
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
134 void copyXBMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
136 XCopyArea(display
, wmgen
.mask
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
144 void initXwindow(int argc
, char *argv
[]){
147 char *display_name
= NULL
;
149 for (i
=1; argv
[i
]; ++i
) {
150 if (!strcmp(argv
[i
], "-display")) display_name
= argv
[i
+1];
154 if (!(display
= XOpenDisplay(display_name
))) {
155 fprintf(stderr
, "%s: can't open display %s\n",
156 argv
[0], XDisplayName(display_name
));
161 screen
= DefaultScreen(display
);
162 Root
= RootWindow(display
, screen
);
163 DisplayDepth
= DefaultDepth(display
, screen
);
164 x_fd
= XConnectionNumber(display
);
175 void openXwindow(int argc
, char *argv
[], char *pixmap_bytes
[], char *pixmask_bits
,
176 int pixmask_width
, int pixmask_height
) {
178 unsigned int borderwidth
= 1;
179 XClassHint classHint
;
180 char *wname
= argv
[0];
184 int dummy
=0, red
, grn
, blu
;
185 XpmColorSymbol cols
[10]={ {"Back", NULL
, 0},
194 {"Color9", NULL
, 0}};
202 cols
[0].pixel
= getColor(BackgroundColor
, 1.0000, &red
, &grn
, &blu
);
203 cols
[1].pixel
= getBlendedColor(TimeColor
, 0.1522, red
, grn
, blu
);
204 cols
[2].pixel
= getBlendedColor(TimeColor
, 0.2602, red
, grn
, blu
);
205 cols
[3].pixel
= getBlendedColor(TimeColor
, 0.3761, red
, grn
, blu
);
206 cols
[4].pixel
= getBlendedColor(TimeColor
, 0.4841, red
, grn
, blu
);
207 cols
[5].pixel
= getBlendedColor(TimeColor
, 0.5922, red
, grn
, blu
);
208 cols
[6].pixel
= getBlendedColor(TimeColor
, 0.6980, red
, grn
, blu
);
209 cols
[7].pixel
= getBlendedColor(TimeColor
, 0.7961, red
, grn
, blu
);
210 cols
[8].pixel
= getBlendedColor(TimeColor
, 0.8941, red
, grn
, blu
);
211 cols
[9].pixel
= getBlendedColor(TimeColor
, 1.0000, red
, grn
, blu
);
213 wmgen
.attributes
.numsymbols
= 10;
214 wmgen
.attributes
.colorsymbols
= cols
;
215 wmgen
.attributes
.exactColors
= False
;
216 wmgen
.attributes
.closeness
= 40000;
217 wmgen
.attributes
.valuemask
= XpmReturnPixels
| XpmReturnExtensions
| XpmColorSymbols
218 | XpmExactColors
| XpmCloseness
| XpmSize
;
219 if (XpmCreatePixmapFromData(display
, Root
, pixmap_bytes
,
220 &(wmgen
.pixmap
), &(wmgen
.mask
), &(wmgen
.attributes
)) != XpmSuccess
){
221 fprintf(stderr
, "Not enough free colorcells.\n");
231 mysizehints
.flags
= USSize
| USPosition
;
235 back_pix
= getColor("white", 1.0, &red
, &grn
, &blu
);
236 fore_pix
= getColor("black", 1.0, &red
, &grn
, &blu
);
238 XWMGeometry(display
, screen
, Geometry
, NULL
, borderwidth
, &mysizehints
,
239 &mysizehints
.x
, &mysizehints
.y
,&mysizehints
.width
,&mysizehints
.height
, &dummy
);
241 mysizehints
.width
= 64;
242 mysizehints
.height
= 64;
246 win
= XCreateSimpleWindow(display
, Root
, mysizehints
.x
, mysizehints
.y
,
247 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
249 iconwin
= XCreateSimpleWindow(display
, win
, mysizehints
.x
, mysizehints
.y
,
250 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
257 XSetWMNormalHints(display
, win
, &mysizehints
);
258 classHint
.res_name
= wname
;
259 classHint
.res_class
= wname
;
260 XSetClassHint(display
, win
, &classHint
);
265 * Set up the xevents that you want the relevent windows to inherit
266 * Currently, its seems that setting KeyPress events here has no
267 * effect. I.e. for some you will need to Grab the focus and then return
268 * it after you are done...
270 XSelectInput(display
, win
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
271 | PointerMotionMask
| StructureNotifyMask
| EnterWindowMask
| LeaveWindowMask
272 | KeyPressMask
| KeyReleaseMask
);
273 XSelectInput(display
, iconwin
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
274 | PointerMotionMask
| StructureNotifyMask
| EnterWindowMask
| LeaveWindowMask
275 | KeyPressMask
| KeyReleaseMask
);
278 if (XStringListToTextProperty(&wname
, 1, &name
) == 0) {
279 fprintf(stderr
, "%s: can't allocate window name\n", wname
);
284 XSetWMName(display
, win
, &name
);
287 * Create Graphics Context (GC) for drawing
289 gcm
= GCForeground
| GCBackground
| GCGraphicsExposures
;
290 gcv
.foreground
= fore_pix
;
291 gcv
.background
= back_pix
;
292 gcv
.graphics_exposures
= 0;
293 NormalGC
= XCreateGC(display
, Root
, gcm
, &gcv
);
297 pixmask
= XCreateBitmapFromData(display
, win
, pixmask_bits
, pixmask_width
, pixmask_height
);
298 XShapeCombineMask(display
, win
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
299 XShapeCombineMask(display
, iconwin
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
302 mywmhints
.initial_state
= WithdrawnState
;
303 mywmhints
.icon_window
= iconwin
;
304 mywmhints
.icon_x
= mysizehints
.x
;
305 mywmhints
.icon_y
= mysizehints
.y
;
306 mywmhints
.window_group
= win
;
307 mywmhints
.flags
= StateHint
| IconWindowHint
| IconPositionHint
| WindowGroupHint
;
310 XSetWMHints(display
, win
, &mywmhints
);
313 XSetCommand(display
, win
, argv
, argc
);
314 XMapWindow(display
, win
);
318 unsigned long getColor(char *ColorName
, float fac
, int *red
, int *grn
, int *blu
) {
321 XWindowAttributes Attributes
;
323 XGetWindowAttributes(display
, Root
, &Attributes
);
326 XParseColor(display
, Attributes
.colormap
, ColorName
, &Color
);
327 Color
.red
= (unsigned short)(fac
*(Color
.red
-24) + 24);
328 Color
.blue
= (unsigned short)(fac
*(Color
.blue
-24) + 24);
329 Color
.green
= (unsigned short)(fac
*(Color
.green
-24) + 24);
330 Color
.flags
= DoRed
| DoGreen
| DoBlue
;
331 XAllocColor(display
, Attributes
.colormap
, &Color
);
341 unsigned long getBlendedColor(char *ColorName
, float fac
, int red
, int grn
, int blu
) {
344 XWindowAttributes Attributes
;
346 XGetWindowAttributes(display
, Root
, &Attributes
);
349 XParseColor(display
, Attributes
.colormap
, ColorName
, &Color
);
350 Color
.red
= (unsigned short)(fac
*(Color
.red
-red
) + red
);
351 Color
.blue
= (unsigned short)(fac
*(Color
.blue
-grn
) + grn
);
352 Color
.green
= (unsigned short)(fac
*(Color
.green
-blu
) + blu
);
353 Color
.flags
= DoRed
| DoGreen
| DoBlue
;
354 XAllocColor(display
, Attributes
.colormap
, &Color
);