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
;
58 * Colors for wmCalClock
60 extern char TimeColor
[30];
61 extern char BackgroundColor
[30];
71 static int flush_expose(Window w
) {
76 while (XCheckTypedWindowEvent(display
, w
, Expose
, &dummy
))
95 void RedrawWindow(void) {
97 flush_expose(iconwin
);
98 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
, 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
101 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
, 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
105 void RedrawWindowXY(int x
, int y
) {
107 flush_expose(iconwin
);
108 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
, x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
111 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
, x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
125 void copyXPMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
126 XCopyArea(display
, wmgen
.pixmap
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
129 void copyXBMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
131 XCopyArea(display
, wmgen
.mask
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
139 void initXwindow(int argc
, char *argv
[]){
142 char *display_name
= NULL
;
144 for (i
=1; argv
[i
]; ++i
) {
145 if (!strcmp(argv
[i
], "-display")) display_name
= argv
[i
+1];
149 if (!(display
= XOpenDisplay(display_name
))) {
150 fprintf(stderr
, "%s: can't open display %s\n",
151 argv
[0], XDisplayName(display_name
));
156 screen
= DefaultScreen(display
);
157 Root
= RootWindow(display
, screen
);
158 DisplayDepth
= DefaultDepth(display
, screen
);
159 x_fd
= XConnectionNumber(display
);
170 void openXwindow(int argc
, char *argv
[], char *pixmap_bytes
[], char *pixmask_bits
,
171 int pixmask_width
, int pixmask_height
) {
173 unsigned int borderwidth
= 1;
174 XClassHint classHint
;
175 char *wname
= argv
[0];
179 int dummy
=0, red
, grn
, blu
;
180 XpmColorSymbol cols
[10]={ {"Back", NULL
, 0},
189 {"Color9", NULL
, 0}};
197 cols
[0].pixel
= getColor(BackgroundColor
, 1.0000, &red
, &grn
, &blu
);
198 cols
[1].pixel
= getBlendedColor(TimeColor
, 0.1522, red
, grn
, blu
);
199 cols
[2].pixel
= getBlendedColor(TimeColor
, 0.2602, red
, grn
, blu
);
200 cols
[3].pixel
= getBlendedColor(TimeColor
, 0.3761, red
, grn
, blu
);
201 cols
[4].pixel
= getBlendedColor(TimeColor
, 0.4841, red
, grn
, blu
);
202 cols
[5].pixel
= getBlendedColor(TimeColor
, 0.5922, red
, grn
, blu
);
203 cols
[6].pixel
= getBlendedColor(TimeColor
, 0.6980, red
, grn
, blu
);
204 cols
[7].pixel
= getBlendedColor(TimeColor
, 0.7961, red
, grn
, blu
);
205 cols
[8].pixel
= getBlendedColor(TimeColor
, 0.8941, red
, grn
, blu
);
206 cols
[9].pixel
= getBlendedColor(TimeColor
, 1.0000, red
, grn
, blu
);
208 wmgen
.attributes
.numsymbols
= 10;
209 wmgen
.attributes
.colorsymbols
= cols
;
210 wmgen
.attributes
.exactColors
= False
;
211 wmgen
.attributes
.closeness
= 40000;
212 wmgen
.attributes
.valuemask
= XpmReturnPixels
| XpmReturnExtensions
| XpmColorSymbols
213 | XpmExactColors
| XpmCloseness
| XpmSize
;
214 if (XpmCreatePixmapFromData(display
, Root
, pixmap_bytes
,
215 &(wmgen
.pixmap
), &(wmgen
.mask
), &(wmgen
.attributes
)) != XpmSuccess
){
216 fprintf(stderr
, "Not enough free colorcells.\n");
226 mysizehints
.flags
= USSize
| USPosition
;
230 back_pix
= getColor("white", 1.0, &red
, &grn
, &blu
);
231 fore_pix
= getColor("black", 1.0, &red
, &grn
, &blu
);
233 XWMGeometry(display
, screen
, Geometry
, NULL
, borderwidth
, &mysizehints
,
234 &mysizehints
.x
, &mysizehints
.y
,&mysizehints
.width
,&mysizehints
.height
, &dummy
);
236 mysizehints
.width
= 64;
237 mysizehints
.height
= 64;
241 win
= XCreateSimpleWindow(display
, Root
, mysizehints
.x
, mysizehints
.y
,
242 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
244 iconwin
= XCreateSimpleWindow(display
, win
, mysizehints
.x
, mysizehints
.y
,
245 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
252 XSetWMNormalHints(display
, win
, &mysizehints
);
253 classHint
.res_name
= wname
;
254 classHint
.res_class
= wname
;
255 XSetClassHint(display
, win
, &classHint
);
260 * Set up the xevents that you want the relevent windows to inherit
261 * Currently, its seems that setting KeyPress events here has no
262 * effect. I.e. for some you will need to Grab the focus and then return
263 * it after you are done...
265 XSelectInput(display
, win
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
266 | PointerMotionMask
| StructureNotifyMask
| EnterWindowMask
| LeaveWindowMask
267 | KeyPressMask
| KeyReleaseMask
);
268 XSelectInput(display
, iconwin
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
269 | PointerMotionMask
| StructureNotifyMask
| EnterWindowMask
| LeaveWindowMask
270 | KeyPressMask
| KeyReleaseMask
);
273 if (XStringListToTextProperty(&wname
, 1, &name
) == 0) {
274 fprintf(stderr
, "%s: can't allocate window name\n", wname
);
279 XSetWMName(display
, win
, &name
);
282 * Create Graphics Context (GC) for drawing
284 gcm
= GCForeground
| GCBackground
| GCGraphicsExposures
;
285 gcv
.foreground
= fore_pix
;
286 gcv
.background
= back_pix
;
287 gcv
.graphics_exposures
= 0;
288 NormalGC
= XCreateGC(display
, Root
, gcm
, &gcv
);
292 pixmask
= XCreateBitmapFromData(display
, win
, pixmask_bits
, pixmask_width
, pixmask_height
);
293 XShapeCombineMask(display
, win
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
294 XShapeCombineMask(display
, iconwin
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
297 mywmhints
.initial_state
= WithdrawnState
;
298 mywmhints
.icon_window
= iconwin
;
299 mywmhints
.icon_x
= mysizehints
.x
;
300 mywmhints
.icon_y
= mysizehints
.y
;
301 mywmhints
.window_group
= win
;
302 mywmhints
.flags
= StateHint
| IconWindowHint
| IconPositionHint
| WindowGroupHint
;
305 XSetWMHints(display
, win
, &mywmhints
);
308 XSetCommand(display
, win
, argv
, argc
);
309 XMapWindow(display
, win
);
313 unsigned long getColor(char *ColorName
, float fac
, int *red
, int *grn
, int *blu
) {
316 XWindowAttributes Attributes
;
318 XGetWindowAttributes(display
, Root
, &Attributes
);
321 XParseColor(display
, Attributes
.colormap
, ColorName
, &Color
);
322 Color
.red
= (unsigned short)(fac
*(Color
.red
-24) + 24);
323 Color
.blue
= (unsigned short)(fac
*(Color
.blue
-24) + 24);
324 Color
.green
= (unsigned short)(fac
*(Color
.green
-24) + 24);
325 Color
.flags
= DoRed
| DoGreen
| DoBlue
;
326 XAllocColor(display
, Attributes
.colormap
, &Color
);
336 unsigned long getBlendedColor(char *ColorName
, float fac
, int red
, int grn
, int blu
) {
339 XWindowAttributes Attributes
;
341 XGetWindowAttributes(display
, Root
, &Attributes
);
344 XParseColor(display
, Attributes
.colormap
, ColorName
, &Color
);
345 Color
.red
= (unsigned short)(fac
*(Color
.red
-red
) + red
);
346 Color
.blue
= (unsigned short)(fac
*(Color
.blue
-grn
) + grn
);
347 Color
.green
= (unsigned short)(fac
*(Color
.green
-blu
) + blu
);
348 Color
.flags
= DoRed
| DoGreen
| DoBlue
;
349 XAllocColor(display
, Attributes
.colormap
, &Color
);