aewl

changeset 2:a79188fe4a40

added new stuff
author Anselm R. Garbe <garbeam@wmii.de>
date Mon, 10 Jul 2006 18:35:39 +0200
parents f10194d4b76d
children e969f3575b7a
files Makefile config.h config.mk draw.c draw.h font.c gridmenu.1 util.c util.h wm.c wm.h
diffstat 11 files changed, 328 insertions(+), 36 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Mon Jul 10 16:49:43 2006 +0200
     1.2 +++ b/Makefile	Mon Jul 10 18:35:39 2006 +0200
     1.3 @@ -3,8 +3,8 @@
     1.4  
     1.5  include config.mk
     1.6  
     1.7 -SRC = wm.c
     1.8 -OBJ = ${SRC:.c=.o}
     1.9 +WMSRC = wm.c draw.c util.c
    1.10 +WMOBJ = ${WMSRC:.c=.o}
    1.11  MAN = gridwm.1
    1.12  BIN = gridwm gridmenu     
    1.13  
    1.14 @@ -22,18 +22,18 @@
    1.15  	@echo CC $<
    1.16  	@${CC} -c ${CFLAGS} $<
    1.17  
    1.18 -${OBJ}: wm.h
    1.19 +${WMOBJ}: wm.h draw.h config.h
    1.20  
    1.21 -gridwm: ${OBJ}
    1.22 +gridwm: ${WMOBJ}
    1.23  	@echo LD $@
    1.24 -	@${CC} -o $@ ${OBJ} ${X11LDFLAGS}
    1.25 +	@${CC} -o $@ ${WMOBJ} ${LDFLAGS}
    1.26  
    1.27  clean:
    1.28  	rm -f gridwm *.o
    1.29  
    1.30  dist: clean
    1.31  	mkdir -p gridwm-${VERSION}
    1.32 -	cp -R Makefile README LICENSE config.mk ${SRC} ${MAN} gridwm-${VERSION}
    1.33 +	cp -R Makefile README LICENSE config.mk ${WMSRC} ${MAN} gridwm-${VERSION}
    1.34  	tar -cf gridwm-${VERSION}.tar gridwm-${VERSION}
    1.35  	gzip gridwm-${VERSION}.tar
    1.36  	rm -rf gridwm-${VERSION}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/config.h	Mon Jul 10 18:35:39 2006 +0200
     2.3 @@ -0,0 +1,9 @@
     2.4 +/*
     2.5 + * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
     2.6 + * See LICENSE file for license details.
     2.7 + */
     2.8 +
     2.9 +#define FONT	"fixed"
    2.10 +#define FGCOLOR	"#000000"
    2.11 +#define BGCOLOR	"#ffaa00"
    2.12 +#define BOCOLOR	"#000000"
     3.1 --- a/config.mk	Mon Jul 10 16:49:43 2006 +0200
     3.2 +++ b/config.mk	Mon Jul 10 18:35:39 2006 +0200
     3.3 @@ -11,14 +11,12 @@
     3.4  VERSION = 0.0
     3.5  
     3.6  # includes and libs
     3.7 -LIBS = -L${PREFIX}/lib -L/usr/lib -lc
     3.8 -X11LIBS = -L${X11LIB} -lX11
     3.9 +LIBS = -L${PREFIX}/lib -L/usr/lib -lc -L${X11LIB} -lX11
    3.10  
    3.11  # Linux/BSD
    3.12  CFLAGS = -g -Wall -I. -I${PREFIX}/include -I/usr/include -I${X11INC} \
    3.13  	-DVERSION=\"${VERSION}\"
    3.14  LDFLAGS = -g ${LIBS}
    3.15 -X11LDFLAGS = ${LDFLAGS} ${X11LIBS}
    3.16  
    3.17  # Solaris
    3.18  #CFLAGS = -fast -xtarget=ultra ${INCLUDES} -DVERSION=\"${VERSION}\"
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/draw.c	Mon Jul 10 18:35:39 2006 +0200
     4.3 @@ -0,0 +1,163 @@
     4.4 +/*
     4.5 + * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
     4.6 + * See LICENSE file for license details.
     4.7 + */
     4.8 +
     4.9 +#include <stdio.h>
    4.10 +#include <string.h>
    4.11 +
    4.12 +#include "draw.h"
    4.13 +#include "util.h"
    4.14 +
    4.15 +static void
    4.16 +drawborder(Display *dpy, Brush *b)
    4.17 +{
    4.18 +	XPoint points[5];
    4.19 +	XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter);
    4.20 +	XSetForeground(dpy, b->gc, b->color.border);
    4.21 +	points[0].x = b->rect.x;
    4.22 +	points[0].y = b->rect.y;
    4.23 +	points[1].x = b->rect.width - 1;
    4.24 +	points[1].y = 0;
    4.25 +	points[2].x = 0;
    4.26 +	points[2].y = b->rect.height - 1;
    4.27 +	points[3].x = -(b->rect.width - 1);
    4.28 +	points[3].y = 0;
    4.29 +	points[4].x = 0;
    4.30 +	points[4].y = -(b->rect.height - 1);
    4.31 +	XDrawLines(dpy, b->drawable, b->gc, points, 5, CoordModePrevious);
    4.32 +}
    4.33 +
    4.34 +void
    4.35 +draw(Display *dpy, Brush *b)
    4.36 +{
    4.37 +	unsigned int x, y, w, h, len;
    4.38 +	static char buf[256];
    4.39 +	XGCValues gcv;
    4.40 +
    4.41 +	XSetForeground(dpy, b->gc, b->color.bg);
    4.42 +	XFillRectangles(dpy, b->drawable, b->gc, &b->rect, 1);
    4.43 +
    4.44 +	if(b->border)
    4.45 +		drawborder(dpy, b);
    4.46 +
    4.47 +	if(!b->text)
    4.48 +		return;
    4.49 +
    4.50 +	len = strlen(b->text);
    4.51 +	if(len >= sizeof(buf))
    4.52 +		len = sizeof(buf) - 1;
    4.53 +	memcpy(buf, b->text, len);
    4.54 +	buf[len] = 0;
    4.55 +
    4.56 +	h = b->font->ascent + b->font->descent;
    4.57 +	y = b->rect.y + (b->rect.height / 2) - (h / 2) + b->font->ascent;
    4.58 +	x = b->rect.x + (h / 2);
    4.59 +
    4.60 +	/* shorten text if necessary */
    4.61 +	while(len && (w = textwidth_l(b->font, buf, len)) > b->rect.width - h)
    4.62 +		buf[--len] = 0;
    4.63 +
    4.64 +	if(w > b->rect.width)
    4.65 +		return; /* too long */
    4.66 +
    4.67 +	gcv.foreground = b->color.fg;
    4.68 +	gcv.background = b->color.bg;
    4.69 +	if(b->font->set) {
    4.70 +		XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv);
    4.71 +		XmbDrawImageString(dpy, b->drawable, b->font->set, b->gc,
    4.72 +				x, y, buf, len);
    4.73 +	}
    4.74 +	else {
    4.75 +		gcv.font = b->font->xfont->fid;
    4.76 +		XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv);
    4.77 +		XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len);
    4.78 +	}
    4.79 +}
    4.80 +
    4.81 +static unsigned long
    4.82 +xloadcolor(Display *dpy, Colormap cmap, const char *colstr)
    4.83 +{
    4.84 +	XColor color;
    4.85 +	XAllocNamedColor(dpy, cmap, colstr, &color, &color);
    4.86 +	return color.pixel;
    4.87 +}
    4.88 +
    4.89 +void
    4.90 +loadcolor(Display *dpy, int screen, Color *c,
    4.91 +		const char *bg, const char *fg, const char *border)
    4.92 +{
    4.93 +	Colormap cmap = DefaultColormap(dpy, screen);
    4.94 +	c->bg = xloadcolor(dpy, cmap, bg);
    4.95 +	c->fg = xloadcolor(dpy, cmap, fg);
    4.96 +	c->border = xloadcolor(dpy, cmap, border);
    4.97 +}
    4.98 +
    4.99 +unsigned int
   4.100 +textwidth_l(Fnt *font, char *text, unsigned int len)
   4.101 +{
   4.102 +	if(font->set) {
   4.103 +		XRectangle r;
   4.104 +		XmbTextExtents(font->set, text, len, 0, &r);
   4.105 +		return r.width;
   4.106 +	}
   4.107 +	return XTextWidth(font->xfont, text, len);
   4.108 +}
   4.109 +
   4.110 +unsigned int
   4.111 +textwidth(Fnt *font, char *text)
   4.112 +{
   4.113 +	return textwidth_l(font, text, strlen(text));
   4.114 +}
   4.115 +
   4.116 +void
   4.117 +loadfont(Display *dpy, Fnt *font, const char *fontstr)
   4.118 +{
   4.119 +	char **missing, *def;
   4.120 +	int n;
   4.121 +
   4.122 +	missing = 0;
   4.123 +	def = "?";
   4.124 +	setlocale(LC_ALL, "");
   4.125 +	if(font->set)
   4.126 +		XFreeFontSet(dpy, font->set);
   4.127 +	font->set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
   4.128 +	if(missing) {
   4.129 +		while(n--)
   4.130 +			fprintf(stderr, "missing fontset: %s\n", missing[n]);
   4.131 +		XFreeStringList(missing);
   4.132 +		if(font->set) {
   4.133 +			XFreeFontSet(dpy, font->set);
   4.134 +			font->set = 0;
   4.135 +		}
   4.136 +	}
   4.137 +	if(font->set) {
   4.138 +		XFontSetExtents *font_extents;
   4.139 +		XFontStruct **xfonts;
   4.140 +		char **font_names;
   4.141 +		unsigned int i;
   4.142 +
   4.143 +		font->ascent = font->descent = 0;
   4.144 +		font_extents = XExtentsOfFontSet(font->set);
   4.145 +		n = XFontsOfFontSet(font->set, &xfonts, &font_names);
   4.146 +		for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) {
   4.147 +			if(font->ascent < (*xfonts)->ascent)
   4.148 +				font->ascent = (*xfonts)->ascent;
   4.149 +			if(font->descent < (*xfonts)->descent)
   4.150 +				font->descent = (*xfonts)->descent;
   4.151 +			xfonts++;
   4.152 +		}
   4.153 +	}
   4.154 +	else {
   4.155 +		if(font->xfont)
   4.156 +			XFreeFont(dpy, font->xfont);
   4.157 +		font->xfont = 0;
   4.158 +		font->xfont = XLoadQueryFont(dpy, fontstr);
   4.159 +		if (!font->xfont)
   4.160 +			font->xfont = XLoadQueryFont(dpy, "fixed");
   4.161 +		if (!font->xfont)
   4.162 +			error("error, cannot load 'fixed' font\n");
   4.163 +		font->ascent = font->xfont->ascent;
   4.164 +		font->descent = font->xfont->descent;
   4.165 +	}
   4.166 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/draw.h	Mon Jul 10 18:35:39 2006 +0200
     5.3 @@ -0,0 +1,6 @@
     5.4 +/*
     5.5 + * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
     5.6 + * See LICENSE file for license details.
     5.7 + */
     5.8 +
     5.9 +extern void error(char *errstr, ...);
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/font.c	Mon Jul 10 18:35:39 2006 +0200
     6.3 @@ -0,0 +1,81 @@
     6.4 +/*
     6.5 + * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
     6.6 + * See LICENSE file for license details.
     6.7 + */
     6.8 +
     6.9 +#include <stdio.h>
    6.10 +#include <stdlib.h>
    6.11 +#include <string.h>
    6.12 +#include <locale.h>
    6.13 +
    6.14 +unsigned int
    6.15 +textwidth_l(BlitzFont *font, char *text, unsigned int len)
    6.16 +{
    6.17 +	if(font->set) {
    6.18 +		XRectangle r;
    6.19 +		XmbTextExtents(font->set, text, len, nil, &r);
    6.20 +		return r.width;
    6.21 +	}
    6.22 +	return XTextWidth(font->xfont, text, len);
    6.23 +}
    6.24 +
    6.25 +unsigned int
    6.26 +textwidth(BlitzFont *font, char *text)
    6.27 +{
    6.28 +	return blitz_textwidth_l(font, text, strlen(text));
    6.29 +}
    6.30 +
    6.31 +void
    6.32 +loadfont(Blitz *blitz, BlitzFont *font)
    6.33 +{
    6.34 +	char *fontname = font->fontstr;
    6.35 +	char **missing = nil, *def = "?";
    6.36 +	int n;
    6.37 +
    6.38 +	setlocale(LC_ALL, "");
    6.39 +	if(font->set)
    6.40 +		XFreeFontSet(blitz->dpy, font->set);
    6.41 +	font->set = XCreateFontSet(blitz->dpy, fontname, &missing, &n, &def);
    6.42 +	if(missing) {
    6.43 +		while(n--)
    6.44 +			fprintf(stderr, "liblitz: missing fontset: %s\n", missing[n]);
    6.45 +		XFreeStringList(missing);
    6.46 +		if(font->set) {
    6.47 +			XFreeFontSet(blitz->dpy, font->set);
    6.48 +			font->set = nil;
    6.49 +		}
    6.50 +	}
    6.51 +	if(font->set) {
    6.52 +		XFontSetExtents *font_extents;
    6.53 +		XFontStruct **xfonts;
    6.54 +		char **font_names;
    6.55 +		unsigned int i;
    6.56 +
    6.57 +		font->ascent = font->descent = 0;
    6.58 +		font_extents = XExtentsOfFontSet(font->set);
    6.59 +		n = XFontsOfFontSet(font->set, &xfonts, &font_names);
    6.60 +		for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) {
    6.61 +			if(font->ascent < (*xfonts)->ascent)
    6.62 +				font->ascent = (*xfonts)->ascent;
    6.63 +			if(font->descent < (*xfonts)->descent)
    6.64 +				font->descent = (*xfonts)->descent;
    6.65 +			xfonts++;
    6.66 +		}
    6.67 +	}
    6.68 +	else {
    6.69 +		if(font->xfont)
    6.70 +			XFreeFont(blitz->dpy, font->xfont);
    6.71 +		font->xfont = nil;
    6.72 +		font->xfont = XLoadQueryFont(blitz->dpy, fontname);
    6.73 +		if (!font->xfont) {
    6.74 +			fontname = "fixed";
    6.75 +			font->xfont = XLoadQueryFont(blitz->dpy, fontname);
    6.76 +		}
    6.77 +		if (!font->xfont) {
    6.78 +			fprintf(stderr, "%s", "liblitz: error, cannot load 'fixed' font\n");
    6.79 +			exit(1);
    6.80 +		}
    6.81 +		font->ascent = font->xfont->ascent;
    6.82 +		font->descent = font->xfont->descent;
    6.83 +	}
    6.84 +}
     7.1 --- a/gridmenu.1	Mon Jul 10 16:49:43 2006 +0200
     7.2 +++ b/gridmenu.1	Mon Jul 10 18:35:39 2006 +0200
     7.3 @@ -64,21 +64,5 @@
     7.4  if Enter is pressed on termination,
     7.5  .B 1
     7.6  if Escape is pressed.
     7.7 -.SH ENVIRONMENT
     7.8 -.TP
     7.9 -GRID_FONT
    7.10 -The X11 font used to display each item in the menu.
    7.11 -.br
    7.12 -Default: fixed
    7.13 -.TP
    7.14 -GRID_NORMCOLORS
    7.15 -The foreground, background, and border colors of a label. Syntactically, three blank-separated color values of the form #RRGGBB are expected.
    7.16 -.br
    7.17 -Default: #222222 #eeeeee #666666
    7.18 -.TP
    7.19 -GRID_SELCOLORS
    7.20 -Like GRID_NORMCOLORS, but for the selected label.
    7.21 -.br
    7.22 -Default: #ffffff #335577 #447799
    7.23  .SH SEE ALSO
    7.24  .BR gridwm (1)
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/util.c	Mon Jul 10 18:35:39 2006 +0200
     8.3 @@ -0,0 +1,18 @@
     8.4 +/*
     8.5 + * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
     8.6 + * See LICENSE file for license details.
     8.7 + */
     8.8 +
     8.9 +#include <stdarg.h>
    8.10 +#include <stdio.h>
    8.11 +#include <stdlib.h>
    8.12 +
    8.13 +void
    8.14 +error(char *errstr, ...) {
    8.15 +	va_list ap;
    8.16 +	va_start(ap, errstr);
    8.17 +	vfprintf(stderr, errstr, ap);
    8.18 +	va_end(ap);
    8.19 +	exit(1);
    8.20 +}
    8.21 +
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/util.h	Mon Jul 10 18:35:39 2006 +0200
     9.3 @@ -0,0 +1,41 @@
     9.4 +/*
     9.5 + * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
     9.6 + * See LICENSE file for license details.
     9.7 + */
     9.8 +
     9.9 +#include <X11/Xlib.h>
    9.10 +#include <X11/Xlocale.h>
    9.11 +
    9.12 +typedef struct Brush Brush;
    9.13 +typedef struct Color Color;
    9.14 +typedef struct Fnt Fnt;
    9.15 +
    9.16 +struct Color {
    9.17 +	unsigned long bg;
    9.18 +	unsigned long fg;
    9.19 +	unsigned long border;
    9.20 +};
    9.21 +
    9.22 +struct Fnt {
    9.23 +	XFontStruct *xfont;
    9.24 +	XFontSet set;
    9.25 +	int ascent;
    9.26 +	int descent;
    9.27 +};
    9.28 +
    9.29 +struct Brush {
    9.30 +	GC gc;
    9.31 +	Drawable drawable;
    9.32 +	XRectangle rect;
    9.33 +	Bool border;
    9.34 +	Fnt *font;
    9.35 +	Color color;
    9.36 +	const char *text;
    9.37 +};
    9.38 +
    9.39 +extern void draw(Display *dpy, Brush *b);
    9.40 +extern void loadcolor(Display *dpy, int screen, Color *c,
    9.41 +		const char *bg, const char *fg, const char *bo);
    9.42 +extern unsigned int textwidth_l(Fnt *font, char *text, unsigned int len);
    9.43 +extern unsigned int textwidth(Fnt *font, char *text);
    9.44 +extern void loadfont(Display *dpy, Fnt *font, const char *fontstr);
    10.1 --- a/wm.c	Mon Jul 10 16:49:43 2006 +0200
    10.2 +++ b/wm.c	Mon Jul 10 18:35:39 2006 +0200
    10.3 @@ -36,15 +36,6 @@
    10.4  	exit(1);
    10.5  }
    10.6  
    10.7 -void
    10.8 -error(char *errstr, ...) {
    10.9 -	va_list ap;
   10.10 -	va_start(ap, errstr);
   10.11 -	vfprintf(stderr, errstr, ap);
   10.12 -	va_end(ap);
   10.13 -	exit(1);
   10.14 -}
   10.15 -
   10.16  static void
   10.17  scan_wins()
   10.18  {
    11.1 --- a/wm.h	Mon Jul 10 16:49:43 2006 +0200
    11.2 +++ b/wm.h	Mon Jul 10 18:35:39 2006 +0200
    11.3 @@ -3,7 +3,9 @@
    11.4   * See LICENSE file for license details.
    11.5   */
    11.6  
    11.7 -#include <X11/Xlib.h>
    11.8 +#include "draw.h"
    11.9 +#include "util.h"
   11.10 +
   11.11  #include <X11/Xutil.h>
   11.12  
   11.13  /* WM atoms */
   11.14 @@ -54,4 +56,3 @@
   11.15  extern Pixmap pmap;
   11.16  
   11.17  /* wm.c */
   11.18 -extern void error(char *errstr, ...);