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 11/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
16 * Removed a bug from parse_rcfile. You could
17 not use "start" in a command if a label was
19 * Changed the needed geometry string.
20 We don't use window size, and don't support
22 03/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
24 02/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
25 * Added -geometry support (untested)
26 28/08/1998 (Martijn Pieterse, pieterse@xs4all.nl)
27 * Added createXBMfromXPM routine
28 * Saves a lot of work with changing xpm's.
29 02/05/1998 (Martijn Pieterse, pieterse@xs4all.nl)
30 * changed the read_rc_file to parse_rcfile, as suggested by Marcelo E. Magallon
31 * debugged the parse_rc file.
32 30/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
33 * Ripped similar code from all the wm* programs,
34 and put them in a single file.
47 #include <X11/extensions/shape.h>
49 #include "wmgeneral.h"
59 XSizeHints mysizehints
;
61 Pixel back_pix
, fore_pix
;
80 MOUSE_REGION mouse_region
[MAX_MOUSE_REGION
];
82 /***********************/
83 /* Function Prototypes */
84 /***********************/
86 static void GetXPM(XpmIcon
*, char **);
87 static Pixel
GetColor(char *);
88 void RedrawWindow(void);
89 void AddMouseRegion(int, int, int, int, int);
90 int CheckMouseRegion(int, int);
92 /*******************************************************************************\
94 \*******************************************************************************/
96 void parse_rcfile(const char *filename
, rckeys
*keys
) {
100 char *tokens
= " :\t\n";
104 fp
= fopen(filename
, "r");
106 while (fgets(temp
, 128, fp
)) {
109 q
= strtok(q
, tokens
);
110 while (key
>= 0 && keys
[key
].label
) {
111 if ((!strcmp(q
, keys
[key
].label
))) {
112 p
= strstr(temp
, keys
[key
].label
);
113 p
+= strlen(keys
[key
].label
);
114 p
+= strspn(p
, tokens
);
115 if ((i
= strcspn(p
, "#\n"))) p
[i
] = 0;
116 free(*keys
[key
].var
);
117 *keys
[key
].var
= strdup(p
);
127 /*******************************************************************************\
129 \*******************************************************************************/
131 void parse_rcfile2(const char *filename
, rckeys2
*keys
) {
135 char *tokens
= " :\t\n";
140 fp
= fopen(filename
, "r");
142 while (fgets(temp
, 128, fp
)) {
144 while (key
>= 0 && keys
[key
].label
) {
145 if ((p
= strstr(temp
, keys
[key
].label
))) {
146 p
+= strlen(keys
[key
].label
);
147 p
+= strspn(p
, tokens
);
148 if ((i
= strcspn(p
, "#\n"))) p
[i
] = 0;
149 free(*keys
[key
].var
);
150 *keys
[key
].var
= strdup(p
);
161 /*******************************************************************************\
163 \*******************************************************************************/
165 static void GetXPM(XpmIcon
*wmgen
, char *pixmap_bytes
[]) {
167 XWindowAttributes attributes
;
170 /* For the colormap */
171 XGetWindowAttributes(display
, Root
, &attributes
);
173 wmgen
->attributes
.valuemask
|= (XpmReturnPixels
| XpmReturnExtensions
);
175 err
= XpmCreatePixmapFromData(display
, Root
, pixmap_bytes
, &(wmgen
->pixmap
),
176 &(wmgen
->mask
), &(wmgen
->attributes
));
178 if (err
!= XpmSuccess
) {
179 fprintf(stderr
, "Not enough free colorcells.\n");
184 /*******************************************************************************\
186 \*******************************************************************************/
188 static Pixel
GetColor(char *name
) {
191 XWindowAttributes attributes
;
193 XGetWindowAttributes(display
, Root
, &attributes
);
196 if (!XParseColor(display
, attributes
.colormap
, name
, &color
)) {
197 fprintf(stderr
, "wm.app: can't parse %s.\n", name
);
198 } else if (!XAllocColor(display
, attributes
.colormap
, &color
)) {
199 fprintf(stderr
, "wm.app: can't allocate %s.\n", name
);
204 /*******************************************************************************\
206 \*******************************************************************************/
208 static int flush_expose(Window w
) {
213 while (XCheckTypedWindowEvent(display
, w
, Expose
, &dummy
))
219 /*******************************************************************************\
221 \*******************************************************************************/
223 void RedrawWindow(void) {
225 flush_expose(iconwin
);
226 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
,
227 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
229 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
,
230 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
233 /*******************************************************************************\
235 \*******************************************************************************/
237 void RedrawWindowXY(int x
, int y
) {
239 flush_expose(iconwin
);
240 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
,
241 x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
243 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
,
244 x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
247 /*******************************************************************************\
249 \*******************************************************************************/
251 void AddMouseRegion(int index
, int left
, int top
, int right
, int bottom
) {
253 if (index
< MAX_MOUSE_REGION
) {
254 mouse_region
[index
].enable
= 1;
255 mouse_region
[index
].top
= top
;
256 mouse_region
[index
].left
= left
;
257 mouse_region
[index
].bottom
= bottom
;
258 mouse_region
[index
].right
= right
;
262 /*******************************************************************************\
263 |* CheckMouseRegion *|
264 \*******************************************************************************/
266 int CheckMouseRegion(int x
, int y
) {
273 for (i
=0; i
<MAX_MOUSE_REGION
&& !found
; i
++) {
274 if (mouse_region
[i
].enable
&&
275 x
<= mouse_region
[i
].right
&&
276 x
>= mouse_region
[i
].left
&&
277 y
<= mouse_region
[i
].bottom
&&
278 y
>= mouse_region
[i
].top
)
281 if (!found
) return -1;
285 /*******************************************************************************\
286 |* createXBMfromXPM *|
287 \*******************************************************************************/
288 void createXBMfromXPM(char *xbm
, char **xpm
, int sx
, int sy
) {
291 int width
, height
, numcol
;
293 unsigned char bwrite
;
297 sscanf(*xpm
, "%d %d %d", &width
, &height
, &numcol
);
300 for (i
=numcol
+1; i
< numcol
+sy
+1; i
++) {
303 for (j
=0; j
<sx
; j
++) {
305 if (xpm
[i
][j
] != zero
) {
319 /*******************************************************************************\
321 \*******************************************************************************/
323 void copyXPMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
325 XCopyArea(display
, wmgen
.pixmap
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
329 /*******************************************************************************\
331 \*******************************************************************************/
333 void copyXBMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
335 XCopyArea(display
, wmgen
.mask
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
339 /*******************************************************************************\
341 \*******************************************************************************/
343 void setMaskXY(int x
, int y
) {
345 XShapeCombineMask(display
, win
, ShapeBounding
, x
, y
, pixmask
, ShapeSet
);
346 XShapeCombineMask(display
, iconwin
, ShapeBounding
, x
, y
, pixmask
, ShapeSet
);
349 /*******************************************************************************\
351 \*******************************************************************************/
352 void openXwindow(int argc
, char *argv
[], char *pixmap_bytes
[], char *pixmask_bits
, int pixmask_width
, int pixmask_height
) {
354 unsigned int borderwidth
= 1;
355 XClassHint classHint
;
356 char *display_name
= NULL
;
357 char *wname
= argv
[0];
363 char *geometry
= NULL
;
368 for (i
=1; argv
[i
]; i
++) {
369 if (!strcmp(argv
[i
], "-display")) {
370 display_name
= argv
[i
+1];
373 if (!strcmp(argv
[i
], "-geometry")) {
374 geometry
= argv
[i
+1];
379 if (!(display
= XOpenDisplay(display_name
))) {
380 fprintf(stderr
, "%s: can't open display %s\n",
381 wname
, XDisplayName(display_name
));
384 screen
= DefaultScreen(display
);
385 Root
= RootWindow(display
, screen
);
386 d_depth
= DefaultDepth(display
, screen
);
387 x_fd
= XConnectionNumber(display
);
389 /* Convert XPM to XImage */
390 GetXPM(&wmgen
, pixmap_bytes
);
392 /* Create a window to hold the stuff */
393 mysizehints
.flags
= USSize
| USPosition
;
397 back_pix
= GetColor("white");
398 fore_pix
= GetColor("black");
400 XWMGeometry(display
, screen
, Geometry
, NULL
, borderwidth
, &mysizehints
,
401 &mysizehints
.x
, &mysizehints
.y
,&mysizehints
.width
,&mysizehints
.height
, &dummy
);
403 mysizehints
.width
= 64;
404 mysizehints
.height
= 64;
406 win
= XCreateSimpleWindow(display
, Root
, mysizehints
.x
, mysizehints
.y
,
407 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
409 iconwin
= XCreateSimpleWindow(display
, win
, mysizehints
.x
, mysizehints
.y
,
410 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
413 XSetWMNormalHints(display
, win
, &mysizehints
);
414 classHint
.res_name
= wname
;
415 classHint
.res_class
= wname
;
416 XSetClassHint(display
, win
, &classHint
);
418 XSelectInput(display
, win
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
| PointerMotionMask
| StructureNotifyMask
);
419 XSelectInput(display
, iconwin
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
| PointerMotionMask
| StructureNotifyMask
);
421 if (XStringListToTextProperty(&wname
, 1, &name
) == 0) {
422 fprintf(stderr
, "%s: can't allocate window name\n", wname
);
426 XSetWMName(display
, win
, &name
);
428 /* Create GC for drawing */
430 gcm
= GCForeground
| GCBackground
| GCGraphicsExposures
;
431 gcv
.foreground
= fore_pix
;
432 gcv
.background
= back_pix
;
433 gcv
.graphics_exposures
= 0;
434 NormalGC
= XCreateGC(display
, Root
, gcm
, &gcv
);
438 pixmask
= XCreateBitmapFromData(display
, win
, pixmask_bits
, pixmask_width
, pixmask_height
);
440 XShapeCombineMask(display
, win
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
441 XShapeCombineMask(display
, iconwin
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
445 mywmhints
.initial_state
= WithdrawnState
;
446 mywmhints
.icon_window
= iconwin
;
447 mywmhints
.icon_x
= mysizehints
.x
;
448 mywmhints
.icon_y
= mysizehints
.y
;
449 mywmhints
.window_group
= win
;
450 mywmhints
.flags
= StateHint
| IconWindowHint
| IconPositionHint
| WindowGroupHint
;
452 XSetWMHints(display
, win
, &mywmhints
);
454 XSetCommand(display
, win
, argv
, argc
);
455 XMapWindow(display
, win
);
458 if (sscanf(geometry
, "+%d+%d", &wx
, &wy
) != 2) {
459 fprintf(stderr
, "Bad geometry string.\n");
462 XMoveWindow(display
, win
, wx
, wy
);