2 Best viewed with vim5, using ts=4
4 wmgeneral was taken from wmppp.
6 It has a lot of routines which most of the wm* programs use.
8 ------------------------------------------------------------
10 Author: Martijn Pieterse (pieterse@xs4all.nl)
15 02/05/1998 (Martijn Pieterse, pieterse@xs4all.nl)
16 * changed the read_rc_file to parse_rcfile, as suggester by Marcelo E. Magallon
17 * debugged the parse_rc file.
18 30/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
19 * Ripped similar code from all the wm* programs,
20 and put them in a single file.
33 #include <X11/extensions/shape.h>
35 #include "wmgeneral.h"
44 XSizeHints mysizehints
;
46 Pixel back_pix
, fore_pix
;
63 #define MAX_MOUSE_REGION (8)
64 MOUSE_REGION mouse_region
[MAX_MOUSE_REGION
];
66 /***********************/
67 /* Function Prototypes */
68 /***********************/
70 static void GetXPM(XpmIcon
*, char **);
71 static Pixel
GetColor(char *);
72 void RedrawWindow(void);
73 void AddMouseRegion(unsigned, int, int, int, int);
74 int CheckMouseRegion(int, int);
76 /*******************************************************************************\
78 \*******************************************************************************/
80 void parse_rcfile(const char *filename
, rckeys
*keys
) {
84 char *tokens
= " :\t\n";
88 fp
= fopen(filename
, "r");
90 while (fgets(temp
, 128, fp
)) {
92 while (key
>= 0 && keys
[key
].label
) {
93 if ((p
= strstr(temp
, keys
[key
].label
))) {
94 p
+= strlen(keys
[key
].label
);
95 p
+= strspn(p
, tokens
);
96 if ((i
= strcspn(p
, "#\n"))) p
[i
] = 0;
98 *keys
[key
].var
= strdup(p
);
108 /*******************************************************************************\
110 \*******************************************************************************/
112 static void GetXPM(XpmIcon
*wmgen
, char *pixmap_bytes
[]) {
114 XWindowAttributes attributes
;
117 /* For the colormap */
118 XGetWindowAttributes(display
, Root
, &attributes
);
120 wmgen
->attributes
.valuemask
|= (XpmReturnPixels
| XpmReturnExtensions
);
122 err
= XpmCreatePixmapFromData(display
, Root
, pixmap_bytes
, &(wmgen
->pixmap
),
123 &(wmgen
->mask
), &(wmgen
->attributes
));
125 if (err
!= XpmSuccess
) {
126 fprintf(stderr
, "Not enough free colorcells.\n");
131 /*******************************************************************************\
133 \*******************************************************************************/
135 static Pixel
GetColor(char *name
) {
138 XWindowAttributes attributes
;
140 XGetWindowAttributes(display
, Root
, &attributes
);
143 if (!XParseColor(display
, attributes
.colormap
, name
, &color
)) {
144 fprintf(stderr
, "wm.app: can't parse %s.\n", name
);
145 } else if (!XAllocColor(display
, attributes
.colormap
, &color
)) {
146 fprintf(stderr
, "wm.app: can't allocate %s.\n", name
);
151 /*******************************************************************************\
153 \*******************************************************************************/
155 static int flush_expose(Window w
) {
160 while (XCheckTypedWindowEvent(display
, w
, Expose
, &dummy
))
166 /*******************************************************************************\
168 \*******************************************************************************/
170 void RedrawWindow(void) {
172 flush_expose(iconwin
);
173 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
,
174 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
176 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
,
177 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
180 /*******************************************************************************\
182 \*******************************************************************************/
184 void RedrawWindowXY(int x
, int y
) {
186 flush_expose(iconwin
);
187 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
,
188 x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
190 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
,
191 x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
194 /*******************************************************************************\
196 \*******************************************************************************/
198 void AddMouseRegion(unsigned index
, int left
, int top
, int right
, int bottom
) {
200 if (index
< MAX_MOUSE_REGION
) {
201 mouse_region
[index
].enable
= 1;
202 mouse_region
[index
].top
= top
;
203 mouse_region
[index
].left
= left
;
204 mouse_region
[index
].bottom
= bottom
;
205 mouse_region
[index
].right
= right
;
209 /*******************************************************************************\
210 |* CheckMouseRegion *|
211 \*******************************************************************************/
213 int CheckMouseRegion(int x
, int y
) {
220 for (i
=0; i
<MAX_MOUSE_REGION
&& !found
; i
++) {
221 if (mouse_region
[i
].enable
&&
222 x
<= mouse_region
[i
].right
&&
223 x
>= mouse_region
[i
].left
&&
224 y
<= mouse_region
[i
].bottom
&&
225 y
>= mouse_region
[i
].top
)
228 if (!found
) return -1;
232 /*******************************************************************************\
234 \*******************************************************************************/
236 void copyXPMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
238 XCopyArea(display
, wmgen
.pixmap
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
242 /*******************************************************************************\
244 \*******************************************************************************/
246 void copyXBMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
248 XCopyArea(display
, wmgen
.mask
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
252 /*******************************************************************************\
254 \*******************************************************************************/
256 void setMaskXY(int x
, int y
) {
258 XShapeCombineMask(display
, win
, ShapeBounding
, x
, y
, pixmask
, ShapeSet
);
259 XShapeCombineMask(display
, iconwin
, ShapeBounding
, x
, y
, pixmask
, ShapeSet
);
262 /*******************************************************************************\
264 \*******************************************************************************/
265 void openXwindow(int argc
, char *argv
[], char *pixmap_bytes
[], char *pixmask_bits
, int pixmask_width
, int pixmask_height
) {
267 unsigned int borderwidth
= 1;
268 XClassHint classHint
;
269 char *display_name
= NULL
;
270 char *wname
= argv
[0];
280 for (i
=1; argv
[i
]; i
++) {
281 if (!strcmp(argv
[i
], "-display"))
282 display_name
= argv
[i
+1];
285 if (!(display
= XOpenDisplay(display_name
))) {
286 fprintf(stderr
, "%s: can't open display %s\n",
287 wname
, XDisplayName(display_name
));
290 screen
= DefaultScreen(display
);
291 Root
= RootWindow(display
, screen
);
292 d_depth
= DefaultDepth(display
, screen
);
293 x_fd
= XConnectionNumber(display
);
295 /* Convert XPM to XImage */
296 GetXPM(&wmgen
, pixmap_bytes
);
298 /* Create a window to hold the stuff */
299 mysizehints
.flags
= USSize
| USPosition
;
303 back_pix
= GetColor("white");
304 fore_pix
= GetColor("black");
306 XWMGeometry(display
, screen
, Geometry
, NULL
, borderwidth
, &mysizehints
,
307 &mysizehints
.x
, &mysizehints
.y
,&mysizehints
.width
,&mysizehints
.height
, &dummy
);
309 mysizehints
.width
= 64;
310 mysizehints
.height
= 64;
312 win
= XCreateSimpleWindow(display
, Root
, mysizehints
.x
, mysizehints
.y
,
313 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
315 iconwin
= XCreateSimpleWindow(display
, win
, mysizehints
.x
, mysizehints
.y
,
316 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
319 XSetWMNormalHints(display
, win
, &mysizehints
);
320 classHint
.res_name
= wname
;
321 classHint
.res_class
= wname
;
322 XSetClassHint(display
, win
, &classHint
);
324 XSelectInput(display
, win
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
| PointerMotionMask
| StructureNotifyMask
);
325 XSelectInput(display
, iconwin
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
| PointerMotionMask
| StructureNotifyMask
);
327 if (XStringListToTextProperty(&wname
, 1, &name
) == 0) {
328 fprintf(stderr
, "%s: can't allocate window name\n", wname
);
332 XSetWMName(display
, win
, &name
);
334 /* Create GC for drawing */
336 gcm
= GCForeground
| GCBackground
| GCGraphicsExposures
;
337 gcv
.foreground
= fore_pix
;
338 gcv
.background
= back_pix
;
339 gcv
.graphics_exposures
= 0;
340 NormalGC
= XCreateGC(display
, Root
, gcm
, &gcv
);
344 pixmask
= XCreateBitmapFromData(display
, win
, pixmask_bits
, pixmask_width
, pixmask_height
);
346 XShapeCombineMask(display
, win
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
347 XShapeCombineMask(display
, iconwin
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
351 mywmhints
.initial_state
= WithdrawnState
;
352 mywmhints
.icon_window
= iconwin
;
353 mywmhints
.icon_x
= mysizehints
.x
;
354 mywmhints
.icon_y
= mysizehints
.y
;
355 mywmhints
.window_group
= win
;
356 mywmhints
.flags
= StateHint
| IconWindowHint
| IconPositionHint
| WindowGroupHint
;
358 XSetWMHints(display
, win
, &mywmhints
);
360 XSetCommand(display
, win
, argv
, argc
);
361 XMapWindow(display
, win
);