Commit 39677ec
config.h
@@ -3,7 +3,7 @@
* See LICENSE file for license details.
*/
-#define FONT "fixed"
-#define FGCOLOR "#000000"
-#define BGCOLOR "#ffaa00"
-#define BOCOLOR "#000000"
+#define FONT "-*-terminus-medium-*-*-*-14-*-*-*-*-*-iso10646-*"
+#define FGCOLOR "#000000"
+#define BGCOLOR "#ffaa00"
+#define BORDERCOLOR "#000000"
draw.c
@@ -14,7 +14,7 @@ drawborder(Display *dpy, Brush *b)
{
XPoint points[5];
XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter);
- XSetForeground(dpy, b->gc, b->color.border);
+ XSetForeground(dpy, b->gc, b->border);
points[0].x = b->rect.x;
points[0].y = b->rect.y;
points[1].x = b->rect.width - 1;
@@ -29,54 +29,54 @@ drawborder(Display *dpy, Brush *b)
}
void
-draw(Display *dpy, Brush *b)
+draw(Display *dpy, Brush *b, Bool border, const char *text)
{
unsigned int x, y, w, h, len;
static char buf[256];
XGCValues gcv;
- XSetForeground(dpy, b->gc, b->color.bg);
+ XSetForeground(dpy, b->gc, b->bg);
XFillRectangles(dpy, b->drawable, b->gc, &b->rect, 1);
- if(b->border)
+ if(border)
drawborder(dpy, b);
- if(!b->text)
+ if(!text)
return;
- len = strlen(b->text);
+ len = strlen(text);
if(len >= sizeof(buf))
len = sizeof(buf) - 1;
- memcpy(buf, b->text, len);
+ memcpy(buf, text, len);
buf[len] = 0;
- h = b->font->ascent + b->font->descent;
- y = b->rect.y + (b->rect.height / 2) - (h / 2) + b->font->ascent;
+ h = b->font.ascent + b->font.descent;
+ y = b->rect.y + (b->rect.height / 2) - (h / 2) + b->font.ascent;
x = b->rect.x + (h / 2);
/* shorten text if necessary */
- while(len && (w = textwidth_l(b->font, buf, len)) > b->rect.width - h)
+ while(len && (w = textwidth_l(&b->font, buf, len)) > b->rect.width - h)
buf[--len] = 0;
if(w > b->rect.width)
return; /* too long */
- gcv.foreground = b->color.fg;
- gcv.background = b->color.bg;
- if(b->font->set) {
+ gcv.foreground = b->fg;
+ gcv.background = b->bg;
+ if(b->font.set) {
XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv);
- XmbDrawImageString(dpy, b->drawable, b->font->set, b->gc,
+ XmbDrawImageString(dpy, b->drawable, b->font.set, b->gc,
x, y, buf, len);
}
else {
- gcv.font = b->font->xfont->fid;
+ gcv.font = b->font.xfont->fid;
XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv);
XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len);
}
}
static unsigned long
-xloadcolor(Display *dpy, Colormap cmap, const char *colstr)
+xloadcolors(Display *dpy, Colormap cmap, const char *colstr)
{
XColor color;
XAllocNamedColor(dpy, cmap, colstr, &color, &color);
@@ -84,13 +84,13 @@ xloadcolor(Display *dpy, Colormap cmap, const char *colstr)
}
void
-loadcolor(Display *dpy, int screen, Color *c,
+loadcolors(Display *dpy, int screen, Brush *b,
const char *bg, const char *fg, const char *border)
{
Colormap cmap = DefaultColormap(dpy, screen);
- c->bg = xloadcolor(dpy, cmap, bg);
- c->fg = xloadcolor(dpy, cmap, fg);
- c->border = xloadcolor(dpy, cmap, border);
+ b->bg = xloadcolors(dpy, cmap, bg);
+ b->fg = xloadcolors(dpy, cmap, fg);
+ b->border = xloadcolors(dpy, cmap, border);
}
unsigned int
@@ -160,4 +160,5 @@ loadfont(Display *dpy, Fnt *font, const char *fontstr)
font->ascent = font->xfont->ascent;
font->descent = font->xfont->descent;
}
+ font->height = font->ascent + font->descent;
}
draw.h
@@ -3,4 +3,33 @@
* See LICENSE file for license details.
*/
-extern void error(char *errstr, ...);
+#include <X11/Xlib.h>
+#include <X11/Xlocale.h>
+
+typedef struct Brush Brush;
+typedef struct Fnt Fnt;
+
+struct Fnt {
+ XFontStruct *xfont;
+ XFontSet set;
+ int ascent;
+ int descent;
+ int height;
+};
+
+struct Brush {
+ GC gc;
+ Drawable drawable;
+ XRectangle rect;
+ Fnt font;
+ unsigned long bg;
+ unsigned long fg;
+ unsigned long border;
+};
+
+extern void draw(Display *dpy, Brush *b, Bool border, const char *text);
+extern void loadcolors(Display *dpy, int screen, Brush *b,
+ const char *bg, const char *fg, const char *bo);
+extern void loadfont(Display *dpy, Fnt *font, const char *fontstr);
+extern unsigned int textwidth_l(Fnt *font, char *text, unsigned int len);
+extern unsigned int textwidth(Fnt *font, char *text);
Makefile
@@ -5,10 +5,12 @@ include config.mk
WMSRC = wm.c draw.c util.c
WMOBJ = ${WMSRC:.c=.o}
+MENSRC = gridmenu.c draw.c util.c
+MENOBJ = ${MENSRC:.c=.o}
MAN = gridwm.1
BIN = gridwm gridmenu
-all: config gridwm
+all: config gridwm gridmenu
@echo finished
config:
@@ -22,18 +24,22 @@ config:
@echo CC $<
@${CC} -c ${CFLAGS} $<
-${WMOBJ}: wm.h draw.h config.h
+${WMOBJ}: wm.h draw.h config.h util.h
+
+gridmenu: ${MENOBJ}
+ @echo LD $@
+ @${CC} -o $@ ${MENOBJ} ${LDFLAGS}
gridwm: ${WMOBJ}
@echo LD $@
@${CC} -o $@ ${WMOBJ} ${LDFLAGS}
clean:
- rm -f gridwm *.o
+ rm -f gridwm gridmenu *.o core
dist: clean
mkdir -p gridwm-${VERSION}
- cp -R Makefile README LICENSE config.mk ${WMSRC} ${MAN} gridwm-${VERSION}
+ cp -R Makefile README LICENSE config.mk *.h *.c ${MAN} gridwm-${VERSION}
tar -cf gridwm-${VERSION}.tar gridwm-${VERSION}
gzip gridwm-${VERSION}.tar
rm -rf gridwm-${VERSION}
util.c
@@ -6,6 +6,7 @@
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
void
error(char *errstr, ...) {
@@ -16,3 +17,61 @@ error(char *errstr, ...) {
exit(1);
}
+static void
+bad_malloc(unsigned int size)
+{
+ fprintf(stderr, "fatal: could not malloc() %d bytes\n",
+ (int) size);
+ exit(1);
+}
+
+void *
+emallocz(unsigned int size)
+{
+ void *res = calloc(1, size);
+ if(!res)
+ bad_malloc(size);
+ return res;
+}
+
+void *
+emalloc(unsigned int size)
+{
+ void *res = malloc(size);
+ if(!res)
+ bad_malloc(size);
+ return res;
+}
+
+void *
+erealloc(void *ptr, unsigned int size)
+{
+ void *res = realloc(ptr, size);
+ if(!res)
+ bad_malloc(size);
+ return res;
+}
+
+char *
+estrdup(const char *str)
+{
+ void *res = strdup(str);
+ if(!res)
+ bad_malloc(strlen(str));
+ return res;
+}
+
+void
+failed_assert(char *a, char *file, int line)
+{
+ fprintf(stderr, "Assertion \"%s\" failed at %s:%d\n", a, file, line);
+ abort();
+}
+
+void
+swap(void **p1, void **p2)
+{
+ void *tmp = *p1;
+ *p1 = *p2;
+ *p2 = tmp;
+}
util.h
@@ -3,39 +3,14 @@
* See LICENSE file for license details.
*/
-#include <X11/Xlib.h>
-#include <X11/Xlocale.h>
-
-typedef struct Brush Brush;
-typedef struct Color Color;
-typedef struct Fnt Fnt;
-
-struct Color {
- unsigned long bg;
- unsigned long fg;
- unsigned long border;
-};
-
-struct Fnt {
- XFontStruct *xfont;
- XFontSet set;
- int ascent;
- int descent;
-};
-
-struct Brush {
- GC gc;
- Drawable drawable;
- XRectangle rect;
- Bool border;
- Fnt *font;
- Color color;
- const char *text;
-};
-
-extern void draw(Display *dpy, Brush *b);
-extern void loadcolor(Display *dpy, int screen, Color *c,
- const char *bg, const char *fg, const char *bo);
-extern unsigned int textwidth_l(Fnt *font, char *text, unsigned int len);
-extern unsigned int textwidth(Fnt *font, char *text);
-extern void loadfont(Display *dpy, Fnt *font, const char *fontstr);
+extern void error(char *errstr, ...);
+extern void *emallocz(unsigned int size);
+extern void *emalloc(unsigned int size);
+extern void *erealloc(void *ptr, unsigned int size);
+extern char *estrdup(const char *str);
+#define eassert(a) do { \
+ if(!(a)) \
+ failed_assert(#a, __FILE__, __LINE__); \
+ } while (0)
+void failed_assert(char *a, char *file, int line);
+void swap(void **p1, void **p2);
wm.c
@@ -13,15 +13,20 @@
#include "wm.h"
+/* X structs */
Display *dpy;
Window root;
XRectangle rect;
-int screen, sel_screen;
+Pixmap pmap;
Atom wm_atom[WMLast];
Atom net_atom[NetLast];
Cursor cursor[CurLast];
+
+int screen, sel_screen;
unsigned int kmask, numlock_mask;
-Pixmap pmap;
+
+/* draw structs */
+Brush brush = {0};
enum { WM_PROTOCOL_DELWIN = 1 };
@@ -208,7 +213,7 @@ main(int argc, char *argv[])
XSetErrorHandler(startup_error_handler);
/* this causes an error if some other WM is running */
XSelectInput(dpy, root, SubstructureRedirectMask);
- XSync(dpy, False);
+ XFlush(dpy);
if(other_wm_running)
error("gridwm: another window manager is already running\n");
@@ -246,6 +251,10 @@ main(int argc, char *argv[])
wa.cursor = cursor[CurNormal];
XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
+ /* style */
+ loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
+ loadfont(dpy, &brush.font, FONT);
+
scan_wins();
cleanup();
wm.h
@@ -3,6 +3,7 @@
* See LICENSE file for license details.
*/
+#include "config.h"
#include "draw.h"
#include "util.h"
@@ -48,11 +49,14 @@ struct Tag {
extern Display *dpy;
extern Window root;
extern XRectangle rect;
-extern int screen, sel_screen;
-extern unsigned int kmask, numlock_mask;
extern Atom wm_atom[WMLast];
extern Atom net_atom[NetLast];
extern Cursor cursor[CurLast];
extern Pixmap pmap;
+extern int screen, sel_screen;
+extern unsigned int kmask, numlock_mask;
+
+extern Brush brush;
+
/* wm.c */