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"
45 XSizeHints mysizehints
;
47 Pixel back_pix
, fore_pix
;
66 #define MAX_MOUSE_REGION (8)
67 MOUSE_REGION mouse_region
[MAX_MOUSE_REGION
];
69 /***********************/
70 /* Function Prototypes */
71 /***********************/
73 static void GetXPM(XpmIcon
*, char **);
74 static Pixel
GetColor(char *);
75 void RedrawWindow(void);
76 void AddMouseRegion(int, int, int, int, int);
77 int CheckMouseRegion(int, int);
79 /*******************************************************************************\
81 \*******************************************************************************/
83 void parse_rcfile(const char *filename
, rckeys
*keys
) {
87 char *tokens
= " :\t\n";
91 fp
= fopen(filename
, "r");
93 while (fgets(temp
, 128, fp
)) {
95 while (key
>= 0 && keys
[key
].label
) {
96 if ((p
= strstr(temp
, keys
[key
].label
))) {
97 p
+= strlen(keys
[key
].label
);
98 p
+= strspn(p
, tokens
);
99 if ((i
= strcspn(p
, "#\n"))) p
[i
] = 0;
100 free(*keys
[key
].var
);
101 *keys
[key
].var
= strdup(p
);
111 /*******************************************************************************\
113 \*******************************************************************************/
115 static void GetXPM(XpmIcon
*wmgen
, char *pixmap_bytes
[]) {
117 XWindowAttributes attributes
;
120 /* For the colormap */
121 XGetWindowAttributes(display
, Root
, &attributes
);
123 wmgen
->attributes
.valuemask
|= (XpmReturnPixels
| XpmReturnExtensions
);
125 err
= XpmCreatePixmapFromData(display
, Root
, pixmap_bytes
, &(wmgen
->pixmap
),
126 &(wmgen
->mask
), &(wmgen
->attributes
));
128 if (err
!= XpmSuccess
) {
129 fprintf(stderr
, "Not enough free colorcells.\n");
134 /*******************************************************************************\
136 \*******************************************************************************/
138 static Pixel
GetColor(char *name
) {
141 XWindowAttributes attributes
;
143 XGetWindowAttributes(display
, Root
, &attributes
);
146 if (!XParseColor(display
, attributes
.colormap
, name
, &color
)) {
147 fprintf(stderr
, "wm.app: can't parse %s.\n", name
);
148 } else if (!XAllocColor(display
, attributes
.colormap
, &color
)) {
149 fprintf(stderr
, "wm.app: can't allocate %s.\n", name
);
154 /*******************************************************************************\
156 \*******************************************************************************/
158 static int flush_expose(Window w
) {
163 while (XCheckTypedWindowEvent(display
, w
, Expose
, &dummy
))
169 /*******************************************************************************\
171 \*******************************************************************************/
173 void RedrawWindow(void) {
175 flush_expose(iconwin
);
176 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
,
177 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
179 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
,
180 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
183 /*******************************************************************************\
185 \*******************************************************************************/
187 void RedrawWindowXY(int x
, int y
) {
189 flush_expose(iconwin
);
190 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
,
191 x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
193 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
,
194 x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
197 /*******************************************************************************\
199 \*******************************************************************************/
201 void AddMouseRegion(int index
, int left
, int top
, int right
, int bottom
) {
203 if (index
< MAX_MOUSE_REGION
) {
204 mouse_region
[index
].enable
= 1;
205 mouse_region
[index
].top
= top
;
206 mouse_region
[index
].left
= left
;
207 mouse_region
[index
].bottom
= bottom
;
208 mouse_region
[index
].right
= right
;
212 /*******************************************************************************\
213 |* CheckMouseRegion *|
214 \*******************************************************************************/
216 int CheckMouseRegion(int x
, int y
) {
223 for (i
=0; i
<MAX_MOUSE_REGION
&& !found
; i
++) {
224 if (mouse_region
[i
].enable
&&
225 x
<= mouse_region
[i
].right
&&
226 x
>= mouse_region
[i
].left
&&
227 y
<= mouse_region
[i
].bottom
&&
228 y
>= mouse_region
[i
].top
)
231 if (!found
) return -1;
235 /*******************************************************************************\
237 \*******************************************************************************/
239 void copyXPMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
241 XCopyArea(display
, wmgen
.pixmap
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
245 /*******************************************************************************\
247 \*******************************************************************************/
249 void copyXBMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
251 XCopyArea(display
, wmgen
.mask
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
255 /*******************************************************************************\
257 \*******************************************************************************/
259 void setMaskXY(int x
, int y
) {
261 XShapeCombineMask(display
, win
, ShapeBounding
, x
, y
, pixmask
, ShapeSet
);
262 XShapeCombineMask(display
, iconwin
, ShapeBounding
, x
, y
, pixmask
, ShapeSet
);
265 /*******************************************************************************\
267 \*******************************************************************************/
268 void openXwindow(int argc
, char *argv
[], char *pixmap_bytes
[], char *pixmask_bits
, int pixmask_width
, int pixmask_height
) {
270 unsigned int borderwidth
= 1;
271 XClassHint classHint
;
272 char *display_name
= NULL
;
273 char *wname
= argv
[0];
283 for (i
=1; argv
[i
]; i
++) {
284 if (!strcmp(argv
[i
], "-display"))
285 display_name
= argv
[i
+1];
288 if (!(display
= XOpenDisplay(display_name
))) {
289 fprintf(stderr
, "%s: can't open display %s\n",
290 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) {
331 fprintf(stderr
, "%s: can't allocate window name\n", wname
);
335 XSetWMName(display
, win
, &name
);
337 /* Create GC for drawing */
339 gcm
= GCForeground
| GCBackground
| GCGraphicsExposures
;
340 gcv
.foreground
= fore_pix
;
341 gcv
.background
= back_pix
;
342 gcv
.graphics_exposures
= 0;
343 NormalGC
= XCreateGC(display
, Root
, gcm
, &gcv
);
347 pixmask
= XCreateBitmapFromData(display
, win
, pixmask_bits
, pixmask_width
, pixmask_height
);
349 XShapeCombineMask(display
, win
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
350 XShapeCombineMask(display
, iconwin
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
354 mywmhints
.initial_state
= WithdrawnState
;
355 mywmhints
.icon_window
= iconwin
;
356 mywmhints
.icon_x
= mysizehints
.x
;
357 mywmhints
.icon_y
= mysizehints
.y
;
358 mywmhints
.window_group
= win
;
359 mywmhints
.flags
= StateHint
| IconWindowHint
| IconPositionHint
| WindowGroupHint
;
361 XSetWMHints(display
, win
, &mywmhints
);
363 XSetCommand(display
, win
, argv
, argc
);
364 XMapWindow(display
, win
);