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
35 #include <X11/Xutil.h>
37 #include <X11/extensions/shape.h>
38 #include <X11/extensions/shapeconst.h>
47 XSizeHints mysizehints
;
49 Pixel back_pix
, fore_pix
;
62 static int flush_expose(Window w
) {
67 while (XCheckTypedWindowEvent(display
, w
, Expose
, &dummy
))
86 void RedrawWindow(void) {
88 flush_expose(iconwin
);
89 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
, 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
92 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
, 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
96 void RedrawWindowXY(int x
, int y
) {
98 flush_expose(iconwin
);
99 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
, x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
102 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
, x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0, 0);
116 void copyXPMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
117 XCopyArea(display
, wmgen
.pixmap
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
120 void copyXBMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
122 XCopyArea(display
, wmgen
.mask
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
130 void initXwindow(int argc
, char *argv
[]){
133 char *display_name
= NULL
;
135 for (i
=1; argv
[i
]; ++i
) {
136 if (!strcmp(argv
[i
], "-display")) display_name
= argv
[i
+1];
140 if (!(display
= XOpenDisplay(display_name
))) {
141 fprintf(stderr
, "%s: can't open display %s\n",
142 argv
[0], XDisplayName(display_name
));
147 screen
= DefaultScreen(display
);
148 Root
= RootWindow(display
, screen
);
149 DisplayDepth
= DefaultDepth(display
, screen
);
150 x_fd
= XConnectionNumber(display
);
161 void openXwindow(int argc
, char *argv
[], char *pixmap_bytes
[], char *pixmask_bits
,
162 int pixmask_width
, int pixmask_height
, char *BackColor
, char *LabelColor
,
165 unsigned int borderwidth
= 1;
166 XClassHint classHint
;
167 char *wname
= argv
[0];
172 XpmColorSymbol cols
[3]={ {"BackColor", NULL
, 0},
173 {"LabelColor", NULL
, 0},
174 {"DataColor", NULL
, 0}};
182 cols
[0].pixel
= getColor(BackColor
, 1.0);
183 cols
[1].pixel
= getColor(LabelColor
, 1.0);
184 cols
[2].pixel
= getColor(DataColor
, 1.0);
185 wmgen
.attributes
.numsymbols
= 3;
186 wmgen
.attributes
.colorsymbols
= cols
;
187 wmgen
.attributes
.exactColors
= False
;
188 wmgen
.attributes
.closeness
= 40000;
189 wmgen
.attributes
.valuemask
= XpmReturnPixels
| XpmReturnExtensions
| XpmColorSymbols
190 | XpmExactColors
| XpmCloseness
| XpmSize
;
191 if (XpmCreatePixmapFromData(display
, Root
, pixmap_bytes
,
192 &(wmgen
.pixmap
), &(wmgen
.mask
), &(wmgen
.attributes
)) != XpmSuccess
){
193 fprintf(stderr
, "Not enough free colorcells.\n");
203 mysizehints
.flags
= USSize
| USPosition
;
207 back_pix
= getColor("white", 1.0);
208 fore_pix
= getColor("black", 1.0);
210 XWMGeometry(display
, screen
, Geometry
, NULL
, borderwidth
, &mysizehints
,
211 &mysizehints
.x
, &mysizehints
.y
,&mysizehints
.width
,&mysizehints
.height
, &dummy
);
213 mysizehints
.width
= 64;
214 mysizehints
.height
= 64;
218 win
= XCreateSimpleWindow(display
, Root
, mysizehints
.x
, mysizehints
.y
,
219 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
221 iconwin
= XCreateSimpleWindow(display
, win
, mysizehints
.x
, mysizehints
.y
,
222 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
229 XSetWMNormalHints(display
, win
, &mysizehints
);
230 classHint
.res_name
= wname
;
231 classHint
.res_class
= wname
;
232 XSetClassHint(display
, win
, &classHint
);
237 * Set up the xevents that you want the relevent windows to inherit
238 * Currently, its seems that setting KeyPress events here has no
239 * effect. I.e. for some you will need to Grab the focus and then return
240 * it after you are done...
242 XSelectInput(display
, win
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
243 | PointerMotionMask
| StructureNotifyMask
| EnterWindowMask
| LeaveWindowMask
244 | KeyPressMask
| KeyReleaseMask
);
245 XSelectInput(display
, iconwin
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
246 | PointerMotionMask
| StructureNotifyMask
| EnterWindowMask
| LeaveWindowMask
247 | KeyPressMask
| KeyReleaseMask
);
250 if (XStringListToTextProperty(&wname
, 1, &name
) == 0) {
251 fprintf(stderr
, "%s: can't allocate window name\n", wname
);
256 XSetWMName(display
, win
, &name
);
259 * Create Graphics Context (GC) for drawing
261 gcm
= GCForeground
| GCBackground
| GCGraphicsExposures
;
262 gcv
.foreground
= fore_pix
;
263 gcv
.background
= back_pix
;
264 gcv
.graphics_exposures
= 0;
265 NormalGC
= XCreateGC(display
, Root
, gcm
, &gcv
);
269 pixmask
= XCreateBitmapFromData(display
, win
, pixmask_bits
, pixmask_width
, pixmask_height
);
270 XShapeCombineMask(display
, win
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
271 XShapeCombineMask(display
, iconwin
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
274 mywmhints
.initial_state
= WithdrawnState
;
275 mywmhints
.icon_window
= iconwin
;
276 mywmhints
.icon_x
= mysizehints
.x
;
277 mywmhints
.icon_y
= mysizehints
.y
;
278 mywmhints
.window_group
= win
;
279 mywmhints
.flags
= StateHint
| IconWindowHint
| IconPositionHint
| WindowGroupHint
;
282 XSetWMHints(display
, win
, &mywmhints
);
285 XSetCommand(display
, win
, argv
, argc
);
286 XMapWindow(display
, win
);
290 unsigned long getColor(char *ColorName
, float fac
) {
293 XWindowAttributes Attributes
;
295 XGetWindowAttributes(display
, Root
, &Attributes
);
298 XParseColor(display
, Attributes
.colormap
, ColorName
, &Color
);
299 Color
.red
= (unsigned short)(Color
.red
/fac
);
300 Color
.blue
= (unsigned short)(Color
.blue
/fac
);
301 Color
.green
= (unsigned short)(Color
.green
/fac
);
302 Color
.flags
= DoRed
| DoGreen
| DoBlue
;
303 XAllocColor(display
, Attributes
.colormap
, &Color
);