4 Best viewed with vim5, using ts=4
6 wmgeneral was taken from wmppp.
8 It has a lot of routines which most of the wm* programs use.
10 ------------------------------------------------------------
12 Author: Martijn Pieterse (pieterse@xs4all.nl)
17 02/05/1998 (Martijn Pieterse, pieterse@xs4all.nl)
18 * changed the read_rc_file to parse_rcfile, as suggester by Marcelo E. Magallon
19 * debugged the parse_rc file.
20 30/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
21 * Ripped similar code from all the wm* programs,
22 and put them in a single file.
35 #include <X11/extensions/shape.h>
37 #include "wmgeneral.h"
46 XSizeHints mysizehints
;
48 Pixel back_pix
, fore_pix
;
65 #define MAX_MOUSE_REGION (8)
66 MOUSE_REGION mouse_region
[MAX_MOUSE_REGION
];
68 /***********************/
69 /* Function Prototypes */
70 /***********************/
72 static void GetXPM(XpmIcon
*, char **);
73 static Pixel
GetColor(char *);
74 void RedrawWindow(void);
75 void AddMouseRegion(unsigned, int, int, int, int);
76 int CheckMouseRegion(int, int);
78 /*******************************************************************************\
80 \*******************************************************************************/
82 void parse_rcfile(const char *filename
, rckeys
*keys
) {
86 char *tokens
= " :\t\n";
90 fp
= fopen(filename
, "r");
92 while (fgets(temp
, 128, fp
)) {
94 while (key
>= 0 && keys
[key
].label
) {
95 if ((p
= strstr(temp
, keys
[key
].label
))) {
96 p
+= strlen(keys
[key
].label
);
97 p
+= strspn(p
, tokens
);
98 if ((i
= strcspn(p
, "#\n"))) p
[i
] = 0;
100 *keys
[key
].var
= strdup(p
);
110 /*******************************************************************************\
112 \*******************************************************************************/
114 static void GetXPM(XpmIcon
*wmgen
, char *pixmap_bytes
[]) {
116 XWindowAttributes attributes
;
119 /* For the colormap */
120 XGetWindowAttributes(display
, Root
, &attributes
);
122 wmgen
->attributes
.valuemask
|= (XpmReturnPixels
| XpmReturnExtensions
);
124 err
= XpmCreatePixmapFromData(display
, Root
, pixmap_bytes
, &(wmgen
->pixmap
),
125 &(wmgen
->mask
), &(wmgen
->attributes
));
127 if (err
!= XpmSuccess
) {
128 fprintf(stderr
, "Not enough free colorcells.\n");
133 /*******************************************************************************\
135 \*******************************************************************************/
137 static Pixel
GetColor(char *name
) {
140 XWindowAttributes attributes
;
142 XGetWindowAttributes(display
, Root
, &attributes
);
145 if (!XParseColor(display
, attributes
.colormap
, name
, &color
)) {
146 fprintf(stderr
, "wm.app: can't parse %s.\n", name
);
147 } else if (!XAllocColor(display
, attributes
.colormap
, &color
)) {
148 fprintf(stderr
, "wm.app: can't allocate %s.\n", name
);
153 /*******************************************************************************\
155 \*******************************************************************************/
157 static int flush_expose(Window w
) {
162 while (XCheckTypedWindowEvent(display
, w
, Expose
, &dummy
))
168 /*******************************************************************************\
170 \*******************************************************************************/
172 void RedrawWindow(void) {
174 flush_expose(iconwin
);
175 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
,
176 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
178 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
,
179 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
182 /*******************************************************************************\
184 \*******************************************************************************/
186 void RedrawWindowXY(int x
, int y
) {
188 flush_expose(iconwin
);
189 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
,
190 x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
192 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
,
193 x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
196 /*******************************************************************************\
198 \*******************************************************************************/
200 void AddMouseRegion(unsigned index
, int left
, int top
, int right
, int bottom
) {
202 if (index
< MAX_MOUSE_REGION
) {
203 mouse_region
[index
].enable
= 1;
204 mouse_region
[index
].top
= top
;
205 mouse_region
[index
].left
= left
;
206 mouse_region
[index
].bottom
= bottom
;
207 mouse_region
[index
].right
= right
;
211 /*******************************************************************************\
212 |* CheckMouseRegion *|
213 \*******************************************************************************/
215 int CheckMouseRegion(int x
, int y
) {
222 for (i
=0; i
<MAX_MOUSE_REGION
&& !found
; i
++) {
223 if (mouse_region
[i
].enable
&&
224 x
<= mouse_region
[i
].right
&&
225 x
>= mouse_region
[i
].left
&&
226 y
<= mouse_region
[i
].bottom
&&
227 y
>= mouse_region
[i
].top
)
230 if (!found
) return -1;
234 /*******************************************************************************\
236 \*******************************************************************************/
238 void copyXPMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
240 XCopyArea(display
, wmgen
.pixmap
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
244 /*******************************************************************************\
246 \*******************************************************************************/
248 void copyXBMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
250 XCopyArea(display
, wmgen
.mask
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
254 /*******************************************************************************\
256 \*******************************************************************************/
258 void setMaskXY(int x
, int y
) {
260 XShapeCombineMask(display
, win
, ShapeBounding
, x
, y
, pixmask
, ShapeSet
);
261 XShapeCombineMask(display
, iconwin
, ShapeBounding
, x
, y
, pixmask
, ShapeSet
);
264 /*******************************************************************************\
266 \*******************************************************************************/
267 void openXwindow(int argc
, char *argv
[], char *pixmap_bytes
[], char *pixmask_bits
, int pixmask_width
, int pixmask_height
) {
269 unsigned int borderwidth
= 1;
270 XClassHint classHint
;
271 char *display_name
= NULL
;
272 char *wname
= argv
[0];
282 for (i
=1; argv
[i
]; i
++) {
283 if (!strcmp(argv
[i
], "-display"))
284 display_name
= argv
[i
+1];
287 if (!(display
= XOpenDisplay(display_name
))) {
288 fprintf(stderr
, "%s: can't open display %s\n",
289 wname
, XDisplayName(display_name
));
292 screen
= DefaultScreen(display
);
293 Root
= RootWindow(display
, screen
);
294 d_depth
= DefaultDepth(display
, screen
);
295 x_fd
= XConnectionNumber(display
);
297 /* Convert XPM to XImage */
298 GetXPM(&wmgen
, pixmap_bytes
);
300 /* Create a window to hold the stuff */
301 mysizehints
.flags
= USSize
| USPosition
;
305 back_pix
= GetColor("white");
306 fore_pix
= GetColor("black");
308 XWMGeometry(display
, screen
, Geometry
, NULL
, borderwidth
, &mysizehints
,
309 &mysizehints
.x
, &mysizehints
.y
,&mysizehints
.width
,&mysizehints
.height
, &dummy
);
311 mysizehints
.width
= 64;
312 mysizehints
.height
= 64;
314 win
= XCreateSimpleWindow(display
, Root
, mysizehints
.x
, mysizehints
.y
,
315 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
317 iconwin
= XCreateSimpleWindow(display
, win
, mysizehints
.x
, mysizehints
.y
,
318 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
321 XSetWMNormalHints(display
, win
, &mysizehints
);
322 classHint
.res_name
= wname
;
323 classHint
.res_class
= wname
;
324 XSetClassHint(display
, win
, &classHint
);
326 XSelectInput(display
, win
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
| PointerMotionMask
| StructureNotifyMask
);
327 XSelectInput(display
, iconwin
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
| PointerMotionMask
| StructureNotifyMask
);
329 if (XStringListToTextProperty(&wname
, 1, &name
) == 0) {
330 fprintf(stderr
, "%s: can't allocate window name\n", wname
);
334 XSetWMName(display
, win
, &name
);
336 /* Create GC for drawing */
338 gcm
= GCForeground
| GCBackground
| GCGraphicsExposures
;
339 gcv
.foreground
= fore_pix
;
340 gcv
.background
= back_pix
;
341 gcv
.graphics_exposures
= 0;
342 NormalGC
= XCreateGC(display
, Root
, gcm
, &gcv
);
346 pixmask
= XCreateBitmapFromData(display
, win
, pixmask_bits
, pixmask_width
, pixmask_height
);
348 XShapeCombineMask(display
, win
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
349 XShapeCombineMask(display
, iconwin
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
353 mywmhints
.initial_state
= WithdrawnState
;
354 mywmhints
.icon_window
= iconwin
;
355 mywmhints
.icon_x
= mysizehints
.x
;
356 mywmhints
.icon_y
= mysizehints
.y
;
357 mywmhints
.window_group
= win
;
358 mywmhints
.flags
= StateHint
| IconWindowHint
| IconPositionHint
| WindowGroupHint
;
360 XSetWMHints(display
, win
, &mywmhints
);
362 XSetCommand(display
, win
, argv
, argc
);
363 XMapWindow(display
, win
);