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 14/09/1998 (Dave Clark, clarkd@skyia.com)
16 * Updated createXBMfromXPM routine
17 * Now supports >256 colors
18 11/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
19 * Removed a bug from parse_rcfile. You could
20 not use "start" in a command if a label was
22 * Changed the needed geometry string.
23 We don't use window size, and don't support
25 03/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
27 02/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
28 * Added -geometry support (untested)
29 28/08/1998 (Martijn Pieterse, pieterse@xs4all.nl)
30 * Added createXBMfromXPM routine
31 * Saves a lot of work with changing xpm's.
32 02/05/1998 (Martijn Pieterse, pieterse@xs4all.nl)
33 * changed the read_rc_file to parse_rcfile, as suggested by Marcelo E. Magallon
34 * debugged the parse_rc file.
35 30/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
36 * Ripped similar code from all the wm* programs,
37 and put them in a single file.
50 #include <X11/extensions/shape.h>
52 #include "wmgeneral.h"
62 XSizeHints mysizehints
;
64 Pixel back_pix
, fore_pix
;
83 MOUSE_REGION mouse_region
[MAX_MOUSE_REGION
];
85 /***********************/
86 /* Function Prototypes */
87 /***********************/
89 static void GetXPM(XpmIcon
*, char **);
90 static Pixel
GetColor(char *);
91 void RedrawWindow(void);
92 void AddMouseRegion(int, int, int, int, int);
93 int CheckMouseRegion(int, int);
95 /*******************************************************************************\
97 \*******************************************************************************/
99 void parse_rcfile(const char *filename
, rckeys
*keys
) {
103 char *tokens
= " :\t\n";
107 fp
= fopen(filename
, "r");
109 while (fgets(temp
, 128, fp
)) {
112 t
= strtok(q
, tokens
);
114 while (key
>= 0 && keys
[key
].label
) {
115 if ((!strcmp(t
, keys
[key
].label
))) {
116 p
= strstr(temp
, keys
[key
].label
);
117 p
+= strlen(keys
[key
].label
);
118 p
+= strspn(p
, tokens
);
119 if ((i
= strcspn(p
, "#\n"))) p
[i
] = 0;
120 free(*keys
[key
].var
);
121 *keys
[key
].var
= strdup(p
);
132 /*******************************************************************************\
134 \*******************************************************************************/
136 void parse_rcfile2(const char *filename
, rckeys2
*keys
) {
140 char *tokens
= " :\t\n";
145 fp
= fopen(filename
, "r");
147 while (fgets(temp
, 128, fp
)) {
149 while (key
>= 0 && keys
[key
].label
) {
150 if ((p
= strstr(temp
, keys
[key
].label
))) {
151 p
+= strlen(keys
[key
].label
);
152 p
+= strspn(p
, tokens
);
153 if ((i
= strcspn(p
, "#\n"))) p
[i
] = 0;
154 free(*keys
[key
].var
);
155 *keys
[key
].var
= strdup(p
);
166 /*******************************************************************************\
168 \*******************************************************************************/
170 static void GetXPM(XpmIcon
*wmgen
, char *pixmap_bytes
[]) {
172 XWindowAttributes attributes
;
175 /* For the colormap */
176 XGetWindowAttributes(display
, Root
, &attributes
);
178 wmgen
->attributes
.valuemask
|= (XpmReturnPixels
| XpmReturnExtensions
);
180 err
= XpmCreatePixmapFromData(display
, Root
, pixmap_bytes
, &(wmgen
->pixmap
),
181 &(wmgen
->mask
), &(wmgen
->attributes
));
183 if (err
!= XpmSuccess
) {
184 fprintf(stderr
, "Not enough free colorcells.\n");
189 /*******************************************************************************\
191 \*******************************************************************************/
193 static Pixel
GetColor(char *name
) {
196 XWindowAttributes attributes
;
198 XGetWindowAttributes(display
, Root
, &attributes
);
201 if (!XParseColor(display
, attributes
.colormap
, name
, &color
)) {
202 fprintf(stderr
, "wm.app: can't parse %s.\n", name
);
203 } else if (!XAllocColor(display
, attributes
.colormap
, &color
)) {
204 fprintf(stderr
, "wm.app: can't allocate %s.\n", name
);
209 /*******************************************************************************\
211 \*******************************************************************************/
213 static int flush_expose(Window w
) {
218 while (XCheckTypedWindowEvent(display
, w
, Expose
, &dummy
))
224 /*******************************************************************************\
226 \*******************************************************************************/
228 void RedrawWindow(void) {
230 flush_expose(iconwin
);
231 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
,
232 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
234 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
,
235 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
238 /*******************************************************************************\
240 \*******************************************************************************/
242 void RedrawWindowXY(int x
, int y
) {
244 flush_expose(iconwin
);
245 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
,
246 x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
248 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
,
249 x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
252 /*******************************************************************************\
254 \*******************************************************************************/
256 void AddMouseRegion(int index
, int left
, int top
, int right
, int bottom
) {
258 if (index
< MAX_MOUSE_REGION
) {
259 mouse_region
[index
].enable
= 1;
260 mouse_region
[index
].top
= top
;
261 mouse_region
[index
].left
= left
;
262 mouse_region
[index
].bottom
= bottom
;
263 mouse_region
[index
].right
= right
;
267 /*******************************************************************************\
268 |* CheckMouseRegion *|
269 \*******************************************************************************/
271 int CheckMouseRegion(int x
, int y
) {
278 for (i
=0; i
<MAX_MOUSE_REGION
&& !found
; i
++) {
279 if (mouse_region
[i
].enable
&&
280 x
<= mouse_region
[i
].right
&&
281 x
>= mouse_region
[i
].left
&&
282 y
<= mouse_region
[i
].bottom
&&
283 y
>= mouse_region
[i
].top
)
286 if (!found
) return -1;
290 /*******************************************************************************\
291 |* createXBMfromXPM *|
292 \*******************************************************************************/
293 void createXBMfromXPM(char *xbm
, char **xpm
, int sx
, int sy
) {
296 int width
, height
, numcol
, depth
;
298 unsigned char bwrite
;
302 sscanf(*xpm
, "%d %d %d %d", &width
, &height
, &numcol
, &depth
);
305 for (k
=0; k
!=depth
; k
++)
311 for (i
=numcol
+1; i
< numcol
+sy
+1; i
++) {
314 for (j
=0; j
<sx
*depth
; j
+=depth
) {
318 for (k
=0; k
!=depth
; k
++)
321 curpixel
|= xpm
[i
][j
+k
];
324 if ( curpixel
!= zero
) {
338 /*******************************************************************************\
340 \*******************************************************************************/
342 void copyXPMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
344 XCopyArea(display
, wmgen
.pixmap
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
348 /*******************************************************************************\
350 \*******************************************************************************/
352 void copyXBMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
354 XCopyArea(display
, wmgen
.mask
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
358 /*******************************************************************************\
360 \*******************************************************************************/
362 void setMaskXY(int x
, int y
) {
364 XShapeCombineMask(display
, win
, ShapeBounding
, x
, y
, pixmask
, ShapeSet
);
365 XShapeCombineMask(display
, iconwin
, ShapeBounding
, x
, y
, pixmask
, ShapeSet
);
368 /*******************************************************************************\
370 \*******************************************************************************/
371 void openXwindow(int argc
, char *argv
[], char *pixmap_bytes
[], char *pixmask_bits
, int pixmask_width
, int pixmask_height
) {
373 unsigned int borderwidth
= 1;
374 XClassHint classHint
;
375 char *display_name
= NULL
;
376 char *wname
= argv
[0];
382 char *geometry
= NULL
;
387 for (i
=1; argv
[i
]; i
++) {
388 if (!strcmp(argv
[i
], "-display")) {
389 display_name
= argv
[i
+1];
392 if (!strcmp(argv
[i
], "-geometry")) {
393 geometry
= argv
[i
+1];
398 if (!(display
= XOpenDisplay(display_name
))) {
399 fprintf(stderr
, "%s: can't open display %s\n",
400 wname
, XDisplayName(display_name
));
403 screen
= DefaultScreen(display
);
404 Root
= RootWindow(display
, screen
);
405 d_depth
= DefaultDepth(display
, screen
);
406 x_fd
= XConnectionNumber(display
);
408 /* Convert XPM to XImage */
409 GetXPM(&wmgen
, pixmap_bytes
);
411 /* Create a window to hold the stuff */
412 mysizehints
.flags
= USSize
| USPosition
;
416 back_pix
= GetColor("white");
417 fore_pix
= GetColor("black");
419 XWMGeometry(display
, screen
, Geometry
, NULL
, borderwidth
, &mysizehints
,
420 &mysizehints
.x
, &mysizehints
.y
,&mysizehints
.width
,&mysizehints
.height
, &dummy
);
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
);