micromizing dmenu step 1
Anselm R. Garbe garbeam@gmail.com
Sun, 16 Sep 2007 20:14:09 +0200
M
Makefile
→
Makefile
@@ -3,7 +3,7 @@ # © 2006-2007 Anselm R. Garbe, Sander van Dijk
include config.mk -SRC = draw.c main.c util.c +SRC = dmenu.c OBJ = ${SRC:.c=.o} all: options dmenu@@ -18,7 +18,7 @@ .c.o:
@echo CC $< @${CC} -c ${CFLAGS} $< -${OBJ}: dmenu.h config.mk +${OBJ}: config.h config.mk dmenu: ${OBJ} @echo CC -o $@@@ -31,7 +31,7 @@
dist: clean @echo creating dist tarball @mkdir -p dmenu-${VERSION} - @cp -R LICENSE Makefile README config.mk dmenu.1 dmenu.h dmenu_path ${SRC} dmenu-${VERSION} + @cp -R LICENSE Makefile README config.mk dmenu.1 config.h dmenu_path ${SRC} dmenu-${VERSION} @tar -cf dmenu-${VERSION}.tar dmenu-${VERSION} @gzip dmenu-${VERSION}.tar @rm -rf dmenu-${VERSION}
A
config.h
@@ -0,0 +1,10 @@
+/* See LICENSE file for copyright and license details. */ + +/* appearance */ +#define FONT "-*-terminus-medium-r-*-*-12-*-*-*-*-*-iso10646-*" +#define NORMBGCOLOR "#000" +#define NORMFGCOLOR "#ccc" +#define SELBGCOLOR "#00f" +#define SELFGCOLOR "#fff" +/* next macro defines the space between menu items */ +#define SPACE 30 /* px */
D
dmenu.h
@@ -1,41 +0,0 @@
-/* See LICENSE file for copyright and license details. */ -#include <X11/Xlib.h> - -#define FONT "-*-fixed-medium-r-normal-*-13-*-*-*-*-*-*-*" -#define NORMBGCOLOR "#eeeeee" -#define NORMFGCOLOR "#222222" -#define SELBGCOLOR "#006699" -#define SELFGCOLOR "#ffffff" -#define SPACE 30 /* px */ - -/* color */ -enum { ColFG, ColBG, ColLast }; - -typedef struct { - int x, y, w, h; - unsigned long norm[ColLast]; - unsigned long sel[ColLast]; - Drawable drawable; - GC gc; - struct { - XFontStruct *xfont; - XFontSet set; - int ascent; - int descent; - int height; - } font; -} DC; /* draw context */ - -extern int screen; -extern Display *dpy; -extern DC dc; /* global drawing context */ - -/* draw.c */ -void drawtext(const char *text, unsigned long col[ColLast]); -unsigned int textw(const char *text); -unsigned int textnw(const char *text, unsigned int len); - -/* util.c */ -void *emalloc(unsigned int size); /* allocates memory, exits on error */ -void eprint(const char *errstr, ...); /* prints errstr and exits with 1 */ -char *estrdup(const char *str); /* duplicates str, exits on allocation error */
D
draw.c
@@ -1,61 +0,0 @@
-/* See LICENSE file for copyright and license details. */ -#include "dmenu.h" -#include <string.h> - -/* extern */ - -void -drawtext(const char *text, unsigned long col[ColLast]) { - int x, y, w, h; - static char buf[256]; - unsigned int len, olen; - XRectangle r = { dc.x, dc.y, dc.w, dc.h }; - - XSetForeground(dpy, dc.gc, col[ColBG]); - XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); - if(!text) - return; - w = 0; - olen = len = strlen(text); - if(len >= sizeof buf) - len = sizeof buf - 1; - memcpy(buf, text, len); - buf[len] = 0; - h = dc.font.ascent + dc.font.descent; - y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; - x = dc.x + (h / 2); - /* shorten text if necessary */ - while(len && (w = textnw(buf, len)) > dc.w - h) - buf[--len] = 0; - if(len < olen) { - if(len > 1) - buf[len - 1] = '.'; - if(len > 2) - buf[len - 2] = '.'; - if(len > 3) - buf[len - 3] = '.'; - } - if(w > dc.w) - return; /* too long */ - XSetForeground(dpy, dc.gc, col[ColFG]); - if(dc.font.set) - XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len); - else - XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len); -} - -unsigned int -textw(const char *text) { - return textnw(text, strlen(text)) + dc.font.height; -} - -unsigned int -textnw(const char *text, unsigned int len) { - XRectangle r; - - if(dc.font.set) { - XmbTextExtents(dc.font.set, text, len, NULL, &r); - return r.width; - } - return XTextWidth(dc.font.xfont, text, len); -}
D
util.c
@@ -1,34 +0,0 @@
-/* See LICENSE file for copyright and license details. */ -#include "dmenu.h" -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -void * -emalloc(unsigned int size) { - void *res = malloc(size); - - if(!res) - eprint("fatal: could not malloc() %u bytes\n", size); - return res; -} - -void -eprint(const char *errstr, ...) { - va_list ap; - - va_start(ap, errstr); - vfprintf(stderr, errstr, ap); - va_end(ap); - exit(EXIT_FAILURE); -} - -char * -estrdup(const char *str) { - void *res = strdup(str); - - if(!res) - eprint("fatal: could not malloc() %u bytes\n", strlen(str)); - return res; -}