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
6 * with wmppp (by Martijn Pieterse (pieterse@xs4all.nl)), but its been
7 * hacked up quite a bit 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>
48 XSizeHints mysizehints
;
50 Pixel back_pix
, fore_pix
;
63 static int flush_expose(Window w
) {
68 while (XCheckTypedWindowEvent(display
, w
, Expose
, &dummy
))
87 void RedrawWindow(void) {
89 flush_expose(iconwin
);
90 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
, 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
93 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
, 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
97 void RedrawWindowXY(int x
, int y
) {
99 flush_expose(iconwin
);
100 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
, x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
103 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
, x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
119 /*void copyXPMArea(source_x, source_y, width, height, destx, desty) */
123 void copyXPMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
124 XCopyArea(display
, wmgen
.pixmap
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
127 void copyXBMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
129 XCopyArea(display
, wmgen
.mask
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
137 void initXwindow(int argc
, char *argv
[]){
140 char *display_name
= NULL
;
142 for (i
=1; argv
[i
]; ++i
) {
143 if (!strcmp(argv
[i
], "-display")) display_name
= argv
[i
+1];
147 if (!(display
= XOpenDisplay(display_name
))) {
148 fprintf(stderr
, "%s: can't open display %s\n",
149 argv
[0], XDisplayName(display_name
));
154 screen
= DefaultScreen(display
);
155 Root
= RootWindow(display
, screen
);
156 DisplayDepth
= DefaultDepth(display
, screen
);
157 x_fd
= XConnectionNumber(display
);
168 void openXwindow(int argc
, char *argv
[], char *pixmap_bytes
[], char *pixmask_bits
,
169 int pixmask_width
, int pixmask_height
, char *BackColor
, char *LabelColor
,
170 char *WindGustColor
, char *DataColor
, char *StationTimeColor
) {
172 unsigned int borderwidth
= 1;
173 XClassHint classHint
;
174 char *wname
= argv
[0];
179 XpmColorSymbol cols
[5]={ {"BackColor", NULL
, 0},
180 {"LabelColor", NULL
, 0},
181 {"DataColor", NULL
, 0},
182 {"WindGustColor", NULL
, 0},
183 {"StationTimeColor", NULL
, 0} };
191 cols
[0].pixel
= getColor(BackColor
, 1.0);
192 cols
[1].pixel
= getColor(LabelColor
, 1.0);
193 cols
[2].pixel
= getColor(DataColor
, 1.0);
194 cols
[3].pixel
= getColor(WindGustColor
, 1.0);
195 cols
[4].pixel
= getColor(StationTimeColor
, 1.0);
196 wmgen
.attributes
.numsymbols
= 5;
197 wmgen
.attributes
.colorsymbols
= cols
;
198 wmgen
.attributes
.exactColors
= False
;
199 wmgen
.attributes
.closeness
= 40000;
200 wmgen
.attributes
.valuemask
= XpmReturnPixels
| XpmReturnExtensions
| XpmColorSymbols
201 | XpmExactColors
| XpmCloseness
| XpmSize
;
202 if (XpmCreatePixmapFromData(display
, Root
, pixmap_bytes
,
203 &(wmgen
.pixmap
), &(wmgen
.mask
), &(wmgen
.attributes
)) != XpmSuccess
){
204 fprintf(stderr
, "Not enough free colorcells.\n");
214 mysizehints
.flags
= USSize
| USPosition
;
218 back_pix
= getColor("white", 1.0);
219 fore_pix
= getColor("black", 1.0);
221 XWMGeometry(display
, screen
, Geometry
, NULL
, borderwidth
, &mysizehints
,
222 &mysizehints
.x
, &mysizehints
.y
,&mysizehints
.width
,&mysizehints
.height
, &dummy
);
224 mysizehints
.width
= 64;
225 mysizehints
.height
= 64;
229 win
= XCreateSimpleWindow(display
, Root
, mysizehints
.x
, mysizehints
.y
,
230 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
232 iconwin
= XCreateSimpleWindow(display
, win
, mysizehints
.x
, mysizehints
.y
,
233 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
240 XSetWMNormalHints(display
, win
, &mysizehints
);
241 classHint
.res_name
= wname
;
242 classHint
.res_class
= wname
;
243 XSetClassHint(display
, win
, &classHint
);
248 * Set up the xevents that you want the relevent windows to inherit
249 * Currently, its seems that setting KeyPress events here has no
250 * effect. I.e. for some you will need to Grab the focus and then return
251 * it after you are done...
253 XSelectInput(display
, win
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
254 | PointerMotionMask
| StructureNotifyMask
| EnterWindowMask
| LeaveWindowMask
255 | KeyPressMask
| KeyReleaseMask
);
256 XSelectInput(display
, iconwin
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
257 | PointerMotionMask
| StructureNotifyMask
| EnterWindowMask
| LeaveWindowMask
258 | KeyPressMask
| KeyReleaseMask
);
261 if (XStringListToTextProperty(&wname
, 1, &name
) == 0) {
262 fprintf(stderr
, "%s: can't allocate window name\n", wname
);
267 XSetWMName(display
, win
, &name
);
270 * Create Graphics Context (GC) for drawing
272 gcm
= GCForeground
| GCBackground
| GCGraphicsExposures
;
273 gcv
.foreground
= fore_pix
;
274 gcv
.background
= back_pix
;
275 gcv
.graphics_exposures
= 0;
276 NormalGC
= XCreateGC(display
, Root
, gcm
, &gcv
);
280 pixmask
= XCreateBitmapFromData(display
, win
, pixmask_bits
, pixmask_width
, pixmask_height
);
281 XShapeCombineMask(display
, win
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
282 XShapeCombineMask(display
, iconwin
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
285 mywmhints
.initial_state
= WithdrawnState
;
286 mywmhints
.icon_window
= iconwin
;
287 mywmhints
.icon_x
= mysizehints
.x
;
288 mywmhints
.icon_y
= mysizehints
.y
;
289 mywmhints
.window_group
= win
;
290 mywmhints
.flags
= StateHint
| IconWindowHint
| IconPositionHint
| WindowGroupHint
;
293 XSetWMHints(display
, win
, &mywmhints
);
296 XSetCommand(display
, win
, argv
, argc
);
297 XMapWindow(display
, win
);
301 unsigned long getColor(char *ColorName
, float fac
) {
304 XWindowAttributes Attributes
;
306 XGetWindowAttributes(display
, Root
, &Attributes
);
309 XParseColor(display
, Attributes
.colormap
, ColorName
, &Color
);
310 Color
.red
= (unsigned short)(Color
.red
/fac
);
311 Color
.blue
= (unsigned short)(Color
.blue
/fac
);
312 Color
.green
= (unsigned short)(Color
.green
/fac
);
313 Color
.flags
= DoRed
| DoGreen
| DoBlue
;
314 XAllocColor(display
, Attributes
.colormap
, &Color
);