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 10/10/2003 (Simon Law, sfllaw@debian.org)
16 * changed the parse_rcfile function to use getline instead of fgets.
17 14/09/1998 (Dave Clark, clarkd@skyia.com)
18 * Updated createXBMfromXPM routine
19 * Now supports >256 colors
20 11/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
21 * Removed a bug from parse_rcfile. You could
22 not use "start" in a command if a label was
24 * Changed the needed geometry string.
25 We don't use window size, and don't support
27 03/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
29 02/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
30 * Added -geometry support (untested)
31 28/08/1998 (Martijn Pieterse, pieterse@xs4all.nl)
32 * Added createXBMfromXPM routine
33 * Saves a lot of work with changing xpm's.
34 02/05/1998 (Martijn Pieterse, pieterse@xs4all.nl)
35 * changed the read_rc_file to parse_rcfile, as suggested by Marcelo E. Magallon
36 * debugged the parse_rc file.
37 30/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
38 * Ripped similar code from all the wm* programs,
39 and put them in a single file.
52 #include <X11/extensions/shape.h>
54 #include "wmgeneral.h"
64 XSizeHints mysizehints
;
66 Pixel back_pix
, fore_pix
;
85 MOUSE_REGION mouse_region
[MAX_MOUSE_REGION
];
87 /***********************/
88 /* Function Prototypes */
89 /***********************/
91 static void GetXPM(XpmIcon
*, char **);
92 static Pixel
GetColor(char *);
93 void RedrawWindow(void);
94 void AddMouseRegion(int, int, int, int, int);
95 int CheckMouseRegion(int, int);
97 /*******************************************************************************\
99 \*******************************************************************************/
101 void parse_rcfile(const char *filename
, rckeys
*keys
) {
105 char *tokens
= " :\t\n";
109 fp
= fopen(filename
, "r");
111 while (fgets(temp
, 128, fp
)) {
114 q
= strtok(q
, tokens
);
115 while (key
>= 0 && keys
[key
].label
) {
116 if ((!strcmp(q
, keys
[key
].label
))) {
117 p
= strstr(temp
, keys
[key
].label
);
118 p
+= strlen(keys
[key
].label
);
119 p
+= strspn(p
, tokens
);
120 if ((i
= strcspn(p
, "#\n"))) p
[i
] = 0;
121 free(*keys
[key
].var
);
122 *keys
[key
].var
= strdup(p
);
132 /*******************************************************************************\
134 \*******************************************************************************/
136 void parse_rcfile2(const char *filename
, rckeys2
*keys
) {
140 size_t line_size
= 0;
141 char *tokens
= " :\t\n";
146 fp
= fopen(filename
, "r");
148 while (getline(&line
, &line_size
, fp
) >= 0) {
150 while (key
>= 0 && keys
[key
].label
) {
151 if ((p
= strstr(line
, keys
[key
].label
))) {
152 p
+= strlen(keys
[key
].label
);
153 p
+= strspn(p
, tokens
);
154 if ((i
= strcspn(p
, "#\n"))) p
[i
] = 0;
155 free(*keys
[key
].var
);
156 *keys
[key
].var
= strdup(p
);
167 /*******************************************************************************\
169 \*******************************************************************************/
171 static void GetXPM(XpmIcon
*wmgen
, char *pixmap_bytes
[]) {
173 XWindowAttributes attributes
;
176 /* For the colormap */
177 XGetWindowAttributes(display
, Root
, &attributes
);
179 wmgen
->attributes
.valuemask
|= (XpmReturnPixels
| XpmReturnExtensions
);
181 err
= XpmCreatePixmapFromData(display
, Root
, pixmap_bytes
, &(wmgen
->pixmap
),
182 &(wmgen
->mask
), &(wmgen
->attributes
));
184 if (err
!= XpmSuccess
) {
185 fprintf(stderr
, "Not enough free colorcells.\n");
190 /*******************************************************************************\
192 \*******************************************************************************/
194 static Pixel
GetColor(char *name
) {
197 XWindowAttributes attributes
;
199 XGetWindowAttributes(display
, Root
, &attributes
);
202 if (!XParseColor(display
, attributes
.colormap
, name
, &color
)) {
203 fprintf(stderr
, "wm.app: can't parse %s.\n", name
);
204 } else if (!XAllocColor(display
, attributes
.colormap
, &color
)) {
205 fprintf(stderr
, "wm.app: can't allocate %s.\n", name
);
210 /*******************************************************************************\
212 \*******************************************************************************/
214 static int flush_expose(Window w
) {
219 while (XCheckTypedWindowEvent(display
, w
, Expose
, &dummy
))
225 /*******************************************************************************\
227 \*******************************************************************************/
229 void RedrawWindow(void) {
231 flush_expose(iconwin
);
232 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
,
233 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
235 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
,
236 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
239 /*******************************************************************************\
241 \*******************************************************************************/
243 void RedrawWindowXY(int x
, int y
) {
245 flush_expose(iconwin
);
246 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
,
247 x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
249 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
,
250 x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
253 /*******************************************************************************\
255 \*******************************************************************************/
257 void AddMouseRegion(int index
, int left
, int top
, int right
, int bottom
) {
259 if (index
< MAX_MOUSE_REGION
) {
260 mouse_region
[index
].enable
= 1;
261 mouse_region
[index
].top
= top
;
262 mouse_region
[index
].left
= left
;
263 mouse_region
[index
].bottom
= bottom
;
264 mouse_region
[index
].right
= right
;
268 /*******************************************************************************\
269 |* CheckMouseRegion *|
270 \*******************************************************************************/
272 int CheckMouseRegion(int x
, int y
) {
279 for (i
=0; i
<MAX_MOUSE_REGION
&& !found
; i
++) {
280 if (mouse_region
[i
].enable
&&
281 x
<= mouse_region
[i
].right
&&
282 x
>= mouse_region
[i
].left
&&
283 y
<= mouse_region
[i
].bottom
&&
284 y
>= mouse_region
[i
].top
)
287 if (!found
) return -1;
291 /*******************************************************************************\
292 |* createXBMfromXPM *|
293 \*******************************************************************************/
294 void createXBMfromXPM(char *xbm
, char **xpm
, int sx
, int sy
) {
297 int width
, height
, numcol
, depth
;
299 unsigned char bwrite
;
303 sscanf(*xpm
, "%d %d %d %d", &width
, &height
, &numcol
, &depth
);
306 for (k
=0; k
!=depth
; k
++)
312 for (i
=numcol
+1; i
< numcol
+sy
+1; i
++) {
315 for (j
=0; j
<sx
*depth
; j
+=depth
) {
319 for (k
=0; k
!=depth
; k
++)
322 curpixel
|= xpm
[i
][j
+k
];
325 if ( curpixel
!= zero
) {
339 /*******************************************************************************\
341 \*******************************************************************************/
343 void copyXPMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
345 XCopyArea(display
, wmgen
.pixmap
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
349 /*******************************************************************************\
351 \*******************************************************************************/
353 void copyXBMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
355 XCopyArea(display
, wmgen
.mask
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
359 /*******************************************************************************\
361 \*******************************************************************************/
363 void setMaskXY(int x
, int y
) {
365 XShapeCombineMask(display
, win
, ShapeBounding
, x
, y
, pixmask
, ShapeSet
);
366 XShapeCombineMask(display
, iconwin
, ShapeBounding
, x
, y
, pixmask
, ShapeSet
);
369 /*******************************************************************************\
371 \*******************************************************************************/
372 void openXwindow(int argc
, char *argv
[], char *pixmap_bytes
[], char *pixmask_bits
, int pixmask_width
, int pixmask_height
) {
374 unsigned int borderwidth
= 1;
375 XClassHint classHint
;
376 char *display_name
= NULL
;
377 char *wname
= argv
[0];
383 char *geometry
= NULL
;
388 for (i
=1; argv
[i
]; i
++) {
389 if (!strcmp(argv
[i
], "-display"))
390 display_name
= argv
[++i
];
391 else if (!strcmp(argv
[i
], "-geometry"))
392 geometry
= argv
[++i
];
395 if (!(display
= XOpenDisplay(display_name
))) {
396 fprintf(stderr
, "%s: can't open display %s\n",
397 wname
, XDisplayName(display_name
));
400 screen
= DefaultScreen(display
);
401 Root
= RootWindow(display
, screen
);
402 d_depth
= DefaultDepth(display
, screen
);
403 x_fd
= XConnectionNumber(display
);
405 /* Convert XPM to XImage */
406 GetXPM(&wmgen
, pixmap_bytes
);
408 /* Create a window to hold the stuff */
409 mysizehints
.flags
= USSize
| USPosition
;
413 back_pix
= GetColor("white");
414 fore_pix
= GetColor("black");
416 XWMGeometry(display
, screen
, Geometry
, NULL
, borderwidth
, &mysizehints
,
417 &mysizehints
.x
, &mysizehints
.y
,&mysizehints
.width
,&mysizehints
.height
, &dummy
);
419 XParseGeometry(geometry
, &mysizehints
.x
, &mysizehints
.y
,
420 (unsigned int *) &mysizehints
.width
, (unsigned int *) &mysizehints
.height
);
422 mysizehints
.width
= 64;
423 mysizehints
.height
= 64;
425 win
= XCreateSimpleWindow(display
, Root
, mysizehints
.x
, mysizehints
.y
,
426 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
428 iconwin
= XCreateSimpleWindow(display
, win
, mysizehints
.x
, mysizehints
.y
,
429 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
432 XSetWMNormalHints(display
, win
, &mysizehints
);
433 classHint
.res_name
= wname
;
434 classHint
.res_class
= wname
;
435 XSetClassHint(display
, win
, &classHint
);
437 XSelectInput(display
, win
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
| PointerMotionMask
| StructureNotifyMask
);
438 XSelectInput(display
, iconwin
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
| PointerMotionMask
| StructureNotifyMask
);
440 if (XStringListToTextProperty(&wname
, 1, &name
) == 0) {
441 fprintf(stderr
, "%s: can't allocate window name\n", wname
);
445 XSetWMName(display
, win
, &name
);
447 /* Create GC for drawing */
449 gcm
= GCForeground
| GCBackground
| GCGraphicsExposures
;
450 gcv
.foreground
= fore_pix
;
451 gcv
.background
= back_pix
;
452 gcv
.graphics_exposures
= 0;
453 NormalGC
= XCreateGC(display
, Root
, gcm
, &gcv
);
457 pixmask
= XCreateBitmapFromData(display
, win
, pixmask_bits
, pixmask_width
, pixmask_height
);
459 XShapeCombineMask(display
, win
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
460 XShapeCombineMask(display
, iconwin
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
464 mywmhints
.initial_state
= WithdrawnState
;
465 mywmhints
.icon_window
= iconwin
;
466 mywmhints
.icon_x
= mysizehints
.x
;
467 mywmhints
.icon_y
= mysizehints
.y
;
468 mywmhints
.window_group
= win
;
469 mywmhints
.flags
= StateHint
| IconWindowHint
| IconPositionHint
| WindowGroupHint
;
471 XSetWMHints(display
, win
, &mywmhints
);
473 XSetCommand(display
, win
, argv
, argc
);
474 XMapWindow(display
, win
);
477 if (sscanf(geometry
, "+%d+%d", &wx
, &wy
) != 2) {
478 fprintf(stderr
, "Bad geometry string.\n");
481 XMoveWindow(display
, win
, wx
, wy
);