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>
45 XSizeHints mysizehints
;
47 Pixel back_pix
, fore_pix
;
67 #define MAX_MOUSE_REGION (8)
68 MOUSE_REGION mouse_region
[MAX_MOUSE_REGION
];
70 /***********************/
71 /* Function Prototypes */
72 /***********************/
74 static void GetXPM(XpmIcon
*, char **);
75 static Pixel
GetColor(char *);
76 void RedrawWindow(void);
77 void AddMouseRegion(int, int, int, int, int);
78 int CheckMouseRegion(int, int);
80 /*******************************************************************************\
82 \*******************************************************************************/
84 void parse_rcfile(const char *filename
, rckeys
*keys
)
88 char *tokens
= " :\t\n";
92 fp
= fopen(filename
, "r");
94 while (fgets(temp
, 128, fp
)) {
96 while (key
>= 0 && keys
[key
].label
) {
97 if ((p
= strstr(temp
, keys
[key
].label
))) {
98 p
+= strlen(keys
[key
].label
);
99 p
+= strspn(p
, tokens
);
100 if ((i
= strcspn(p
, "#\n"))) p
[i
] = 0;
101 free(*keys
[key
].var
);
102 *keys
[key
].var
= strdup(p
);
112 /*******************************************************************************\
114 \*******************************************************************************/
116 static void GetXPM(XpmIcon
*wmgen
, char *pixmap_bytes
[])
118 XWindowAttributes attributes
;
121 /* For the colormap */
122 XGetWindowAttributes(display
, Root
, &attributes
);
124 wmgen
->attributes
.valuemask
|= (XpmReturnPixels
| XpmReturnExtensions
);
126 err
= XpmCreatePixmapFromData(display
, Root
, pixmap_bytes
, &(wmgen
->pixmap
),
127 &(wmgen
->mask
), &(wmgen
->attributes
));
129 if (err
!= XpmSuccess
) {
130 fprintf(stderr
, "Not enough free colorcells.\n");
135 /*******************************************************************************\
137 \*******************************************************************************/
139 static Pixel
GetColor(char *name
)
142 XWindowAttributes attributes
;
144 XGetWindowAttributes(display
, Root
, &attributes
);
147 if (!XParseColor(display
, attributes
.colormap
, name
, &color
)) {
148 fprintf(stderr
, "wm.app: can't parse %s.\n", name
);
149 } else if (!XAllocColor(display
, attributes
.colormap
, &color
)) {
150 fprintf(stderr
, "wm.app: can't allocate %s.\n", name
);
155 /*******************************************************************************\
157 \*******************************************************************************/
159 static int flush_expose(Window w
)
164 while (XCheckTypedWindowEvent(display
, w
, Expose
, &dummy
))
170 /*******************************************************************************\
172 \*******************************************************************************/
174 void RedrawWindow(void)
176 flush_expose(iconwin
);
177 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
,
178 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
180 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
,
181 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
184 /*******************************************************************************\
186 \*******************************************************************************/
188 void RedrawWindowXY(int x
, int y
)
190 flush_expose(iconwin
);
191 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
,
192 x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
194 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
,
195 x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
198 /*******************************************************************************\
200 \*******************************************************************************/
202 void AddMouseRegion(int index
, int left
, int top
, int right
, int bottom
)
204 if (index
< MAX_MOUSE_REGION
)
206 mouse_region
[index
].enable
= 1;
207 mouse_region
[index
].top
= top
;
208 mouse_region
[index
].left
= left
;
209 mouse_region
[index
].bottom
= bottom
;
210 mouse_region
[index
].right
= right
;
214 /*******************************************************************************\
215 |* CheckMouseRegion *|
216 \*******************************************************************************/
218 int CheckMouseRegion(int x
, int y
)
225 for (i
=0; i
<MAX_MOUSE_REGION
&& !found
; i
++)
227 if (mouse_region
[i
].enable
&&
228 x
<= mouse_region
[i
].right
&&
229 x
>= mouse_region
[i
].left
&&
230 y
<= mouse_region
[i
].bottom
&&
231 y
>= mouse_region
[i
].top
)
234 if (!found
) return -1;
238 /*******************************************************************************\
240 \*******************************************************************************/
242 void copyXPMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
)
244 XCopyArea(display
, wmgen
.pixmap
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
247 /*******************************************************************************\
249 \*******************************************************************************/
251 void copyXBMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
)
253 XCopyArea(display
, wmgen
.mask
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
257 /*******************************************************************************\
259 \*******************************************************************************/
261 void setMaskXY(int x
, int y
)
263 XShapeCombineMask(display
, win
, ShapeBounding
, x
, y
, pixmask
, ShapeSet
);
264 XShapeCombineMask(display
, iconwin
, ShapeBounding
, x
, y
, pixmask
, ShapeSet
);
267 /*******************************************************************************\
269 \*******************************************************************************/
270 void openXwindow(int argc
, char *argv
[], char *pixmap_bytes
[], char *pixmask_bits
, int pixmask_width
, int pixmask_height
)
272 unsigned int borderwidth
= 1;
273 XClassHint classHint
;
274 char *display_name
= NULL
;
275 char *wname
= argv
[0];
283 for (i
=1; argv
[i
]; i
++)
285 if (!strcmp(argv
[i
], "-display")) display_name
= argv
[i
+1];
288 if (!(display
= XOpenDisplay(display_name
)))
290 fprintf(stderr
, "%s: can't open display %s\n", wname
, XDisplayName(display_name
));
293 screen
= DefaultScreen(display
);
294 Root
= RootWindow(display
, screen
);
295 d_depth
= DefaultDepth(display
, screen
);
296 x_fd
= XConnectionNumber(display
);
298 /* Convert XPM to XImage */
299 GetXPM(&wmgen
, pixmap_bytes
);
301 /* Create a window to hold the stuff */
302 mysizehints
.flags
= USSize
| USPosition
;
306 back_pix
= GetColor("white");
307 fore_pix
= GetColor("black");
309 XWMGeometry(display
, screen
, Geometry
, NULL
, borderwidth
, &mysizehints
,
310 &mysizehints
.x
, &mysizehints
.y
,&mysizehints
.width
,&mysizehints
.height
, &dummy
);
312 mysizehints
.width
= 64;
313 mysizehints
.height
= 64;
315 win
= XCreateSimpleWindow(display
, Root
, mysizehints
.x
, mysizehints
.y
,
316 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
318 iconwin
= XCreateSimpleWindow(display
, win
, mysizehints
.x
, mysizehints
.y
,
319 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
322 XSetWMNormalHints(display
, win
, &mysizehints
);
323 classHint
.res_name
= wname
;
324 classHint
.res_class
= wname
;
325 XSetClassHint(display
, win
, &classHint
);
327 XSelectInput(display
, win
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
| PointerMotionMask
| StructureNotifyMask
);
328 XSelectInput(display
, iconwin
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
| PointerMotionMask
| StructureNotifyMask
);
330 if (XStringListToTextProperty(&wname
, 1, &name
) == 0)
332 fprintf(stderr
, "%s: can't allocate window name\n", wname
);
336 XSetWMName(display
, win
, &name
);
338 /* Create GC for drawing */
340 gcm
= GCForeground
| GCBackground
| GCGraphicsExposures
;
341 gcv
.foreground
= fore_pix
;
342 gcv
.background
= back_pix
;
343 gcv
.graphics_exposures
= 0;
344 NormalGC
= XCreateGC(display
, Root
, gcm
, &gcv
);
348 pixmask
= XCreateBitmapFromData(display
, win
, pixmask_bits
, pixmask_width
, pixmask_height
);
350 XShapeCombineMask(display
, win
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
351 XShapeCombineMask(display
, iconwin
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
355 mywmhints
.initial_state
= WithdrawnState
;
356 mywmhints
.icon_window
= iconwin
;
357 mywmhints
.icon_x
= mysizehints
.x
;
358 mywmhints
.icon_y
= mysizehints
.y
;
359 mywmhints
.window_group
= win
;
360 mywmhints
.flags
= StateHint
| IconWindowHint
| IconPositionHint
| WindowGroupHint
;
362 XSetWMHints(display
, win
, &mywmhints
);
364 XSetCommand(display
, win
, argv
, argc
);
365 XMapWindow(display
, win
);