2 wmgeneral was taken from wmppp.
4 It has a lot of routines which most of the wm* programs use.
6 ------------------------------------------------------------
8 Copyright (C) 1998 Martijn Pieterse (pieterse@xs4all.nl)
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28 10/10/2003 (Simon Law, sfllaw@debian.org)
29 * changed the parse_rcfile function to use getline instead of
31 10/14/2000 (Chris Gray, cgray@tribsoft.com)
32 * Removed a bug from parse_rcfile. An extra
33 newline would cause a segfault.
34 14/09/1998 (Dave Clark, clarkd@skyia.com)
35 * Updated createXBMfromXPM routine
36 * Now supports >256 colors
37 11/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
38 * Removed a bug from parse_rcfile. You could
39 not use "start" in a command if a label was
41 * Changed the needed geometry string.
42 We don't use window size, and don't support
44 03/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
46 02/09/1998 (Martijn Pieterse, pieterse@xs4all.nl)
47 * Added -geometry support (untested)
48 28/08/1998 (Martijn Pieterse, pieterse@xs4all.nl)
49 * Added createXBMfromXPM routine
50 * Saves a lot of work with changing xpm's.
51 02/05/1998 (Martijn Pieterse, pieterse@xs4all.nl)
52 * changed the read_rc_file to parse_rcfile, as suggested by
54 * debugged the parse_rc file.
55 30/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
56 * Ripped similar code from all the wm* programs,
57 and put them in a single file.
61 #define _POSIX_C_SOURCE 200809L
62 #include "wmgeneral.h"
63 #include <X11/Xlib.h> /* for XCopyArea, etc */
64 #include <X11/Xutil.h> /* for XSizeHints, XWMHints, etc */
65 #include <X11/extensions/shape.h> /* for XShapeCombineMask */
66 #include <X11/extensions/shapeconst.h> /* for ShapeBounding, ShapeSet */
67 #include <X11/xpm.h> /* for XpmAttributes, Pixel, etc */
68 #include <stddef.h> /* for size_t */
69 #include <stdio.h> /* for fprintf, stderr, NULL, etc */
70 #include <stdlib.h> /* for exit, free */
71 #include <string.h> /* for strcmp, strdup, strcspn, etc */
81 XSizeHints mysizehints
;
83 Pixel back_pix
, fore_pix
;
101 MOUSE_REGION mouse_region
[MAX_MOUSE_REGION
];
103 /***********************/
104 /* Function Prototypes */
105 /***********************/
107 static void GetXPM(XpmIcon
*, char **);
108 static Pixel
GetColor(char *);
109 void RedrawWindow(void);
110 void AddMouseRegion(int, int, int, int, int);
111 int CheckMouseRegion(int, int);
113 /*******************************************************************************\
115 \*******************************************************************************/
117 void parse_rcfile(const char *filename
, rckeys
*keys
) {
122 fp
= fopen(filename
, "r");
126 while (fgets(temp
, 128, fp
)) {
128 char *tokens
= " :\t\n";
133 q
= strtok_r(q
, tokens
, &saveptr
);
136 while (key
>= 0 && keys
[key
].label
) {
137 if ((!strcmp(q
, keys
[key
].label
))) {
140 p
= strstr(temp
, keys
[key
].label
);
141 p
+= strlen(keys
[key
].label
);
142 p
+= strspn(p
, tokens
);
143 if ((i
= strcspn(p
, "#\n"))) p
[i
] = '\0';
144 *keys
[key
].var
= strdup(p
);
153 /*******************************************************************************\
155 \*******************************************************************************/
157 void parse_rcfile2(const char *filename
, rckeys2
*keys
) {
161 size_t line_size
= 0;
164 fp
= fopen(filename
, "r");
166 while (getline(&line
, &line_size
, fp
) >= 0) {
170 while (key
>= 0 && keys
[key
].label
) {
171 if ((p
= strstr(line
, keys
[key
].label
))) {
172 char *tokens
= " :\t\n";
175 p
+= strlen(keys
[key
].label
);
176 p
+= strspn(p
, tokens
);
177 if ((i
= strcspn(p
, "#\n"))) p
[i
] = 0;
178 *keys
[key
].var
= strdup(p
);
188 /*******************************************************************************\
190 \*******************************************************************************/
192 static void GetXPM(XpmIcon
*wmgen
, char *pixmap_bytes
[]) {
194 XWindowAttributes attributes
;
197 /* For the colormap */
198 XGetWindowAttributes(display
, Root
, &attributes
);
200 wmgen
->attributes
.valuemask
|= (XpmReturnPixels
| XpmReturnExtensions
);
202 err
= XpmCreatePixmapFromData(display
, Root
, pixmap_bytes
, &(wmgen
->pixmap
),
203 &(wmgen
->mask
), &(wmgen
->attributes
));
205 if (err
!= XpmSuccess
) {
206 fprintf(stderr
, "Not enough free colorcells.\n");
211 /*******************************************************************************\
213 \*******************************************************************************/
215 static Pixel
GetColor(char *name
) {
218 XWindowAttributes attributes
;
220 XGetWindowAttributes(display
, Root
, &attributes
);
223 if (!XParseColor(display
, attributes
.colormap
, name
, &color
)) {
224 fprintf(stderr
, "wm.app: can't parse %s.\n", name
);
225 } else if (!XAllocColor(display
, attributes
.colormap
, &color
)) {
226 fprintf(stderr
, "wm.app: can't allocate %s.\n", name
);
231 /*******************************************************************************\
233 \*******************************************************************************/
235 static int flush_expose(Window w
) {
240 while (XCheckTypedWindowEvent(display
, w
, Expose
, &dummy
))
246 /*******************************************************************************\
248 \*******************************************************************************/
250 void RedrawWindow(void) {
252 flush_expose(iconwin
);
253 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
,
254 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
256 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
,
257 0,0, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
260 /*******************************************************************************\
262 \*******************************************************************************/
264 void RedrawWindowXY(int x
, int y
) {
266 flush_expose(iconwin
);
267 XCopyArea(display
, wmgen
.pixmap
, iconwin
, NormalGC
,
268 x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
270 XCopyArea(display
, wmgen
.pixmap
, win
, NormalGC
,
271 x
,y
, wmgen
.attributes
.width
, wmgen
.attributes
.height
, 0,0);
274 /*******************************************************************************\
276 \*******************************************************************************/
278 void AddMouseRegion(int index
, int left
, int top
, int right
, int bottom
) {
280 if (index
< MAX_MOUSE_REGION
) {
281 mouse_region
[index
].enable
= 1;
282 mouse_region
[index
].top
= top
;
283 mouse_region
[index
].left
= left
;
284 mouse_region
[index
].bottom
= bottom
;
285 mouse_region
[index
].right
= right
;
289 /*******************************************************************************\
290 |* CheckMouseRegion *|
291 \*******************************************************************************/
293 int CheckMouseRegion(int x
, int y
) {
300 for (i
=0; i
<MAX_MOUSE_REGION
&& !found
; i
++) {
301 if (mouse_region
[i
].enable
&&
302 x
<= mouse_region
[i
].right
&&
303 x
>= mouse_region
[i
].left
&&
304 y
<= mouse_region
[i
].bottom
&&
305 y
>= mouse_region
[i
].top
)
308 if (!found
) return -1;
312 /*******************************************************************************\
313 |* createXBMfromXPM *|
314 \*******************************************************************************/
315 void createXBMfromXPM(char *xbm
, char **xpm
, int sx
, int sy
) {
318 int width
, height
, numcol
, depth
;
322 sscanf(*xpm
, "%10d %10d %10d %10d", &width
, &height
, &numcol
, &depth
);
325 for (k
=0; k
!=depth
; k
++)
331 for (i
=numcol
+1; i
< numcol
+sy
+1; i
++) {
332 unsigned char bwrite
;
337 for (j
=0; j
<sx
*depth
; j
+=depth
) {
341 for (k
=0; k
!=depth
; k
++)
344 curpixel
|= xpm
[i
][j
+k
];
347 if ( curpixel
!= zero
) {
361 /*******************************************************************************\
363 \*******************************************************************************/
365 void copyXPMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
367 XCopyArea(display
, wmgen
.pixmap
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
371 /*******************************************************************************\
373 \*******************************************************************************/
375 void copyXBMArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
) {
377 XCopyArea(display
, wmgen
.mask
, wmgen
.pixmap
, NormalGC
, x
, y
, sx
, sy
, dx
, dy
);
381 /*******************************************************************************\
383 \*******************************************************************************/
385 void setMaskXY(int x
, int y
) {
387 XShapeCombineMask(display
, win
, ShapeBounding
, x
, y
, pixmask
, ShapeSet
);
388 XShapeCombineMask(display
, iconwin
, ShapeBounding
, x
, y
, pixmask
, ShapeSet
);
391 /*******************************************************************************\
393 \*******************************************************************************/
394 void openXwindow(int argc
, char *argv
[], char *pixmap_bytes
[], char *pixmask_bits
, int pixmask_width
, int pixmask_height
) {
396 unsigned int borderwidth
= 1;
397 XClassHint classHint
;
398 char *display_name
= NULL
;
399 char *wname
= argv
[0];
405 char *geometry
= NULL
;
410 for (i
=1; argv
[i
]; i
++) {
411 if (!strcmp(argv
[i
], "-display"))
412 display_name
= argv
[++i
];
413 else if (!strcmp(argv
[i
], "-geometry"))
414 geometry
= argv
[++i
];
417 if (!(display
= XOpenDisplay(display_name
))) {
418 fprintf(stderr
, "%s: can't open display %s\n",
419 wname
, XDisplayName(display_name
));
422 screen
= DefaultScreen(display
);
423 Root
= RootWindow(display
, screen
);
424 d_depth
= DefaultDepth(display
, screen
);
425 x_fd
= XConnectionNumber(display
);
427 /* Convert XPM to XImage */
428 GetXPM(&wmgen
, pixmap_bytes
);
430 /* Create a window to hold the stuff */
431 mysizehints
.flags
= USSize
| USPosition
;
435 back_pix
= GetColor("white");
436 fore_pix
= GetColor("black");
438 XWMGeometry(display
, screen
, geometry
, NULL
, borderwidth
, &mysizehints
,
439 &mysizehints
.x
, &mysizehints
.y
,&mysizehints
.width
,&mysizehints
.height
, &dummy
);
441 mysizehints
.width
= 64;
442 mysizehints
.height
= 64;
444 win
= XCreateSimpleWindow(display
, Root
, mysizehints
.x
, mysizehints
.y
,
445 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
447 iconwin
= XCreateSimpleWindow(display
, win
, mysizehints
.x
, mysizehints
.y
,
448 mysizehints
.width
, mysizehints
.height
, borderwidth
, fore_pix
, back_pix
);
451 XSetWMNormalHints(display
, win
, &mysizehints
);
452 classHint
.res_name
= wname
;
453 classHint
.res_class
= wname
;
454 XSetClassHint(display
, win
, &classHint
);
456 XSelectInput(display
, win
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
| PointerMotionMask
| StructureNotifyMask
);
457 XSelectInput(display
, iconwin
, ButtonPressMask
| ExposureMask
| ButtonReleaseMask
| PointerMotionMask
| StructureNotifyMask
);
459 if (XStringListToTextProperty(&wname
, 1, &name
) == 0) {
460 fprintf(stderr
, "%s: can't allocate window name\n", wname
);
464 XSetWMName(display
, win
, &name
);
466 /* Create GC for drawing */
468 gcm
= GCForeground
| GCBackground
| GCGraphicsExposures
;
469 gcv
.foreground
= fore_pix
;
470 gcv
.background
= back_pix
;
471 gcv
.graphics_exposures
= 0;
472 NormalGC
= XCreateGC(display
, Root
, gcm
, &gcv
);
476 pixmask
= XCreateBitmapFromData(display
, win
, pixmask_bits
, pixmask_width
, pixmask_height
);
478 XShapeCombineMask(display
, win
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
479 XShapeCombineMask(display
, iconwin
, ShapeBounding
, 0, 0, pixmask
, ShapeSet
);
483 mywmhints
.initial_state
= WithdrawnState
;
484 mywmhints
.icon_window
= iconwin
;
485 mywmhints
.icon_x
= mysizehints
.x
;
486 mywmhints
.icon_y
= mysizehints
.y
;
487 mywmhints
.window_group
= win
;
488 mywmhints
.flags
= StateHint
| IconWindowHint
| IconPositionHint
| WindowGroupHint
;
490 XSetWMHints(display
, win
, &mywmhints
);
492 XSetCommand(display
, win
, argv
, argc
);
493 XMapWindow(display
, win
);