added draw.h
Connor Lane Smith cls@lubutu.com
Wed, 23 Jun 2010 13:29:15 +0100
M
Makefile
→
Makefile
@@ -6,7 +6,7 @@
SRC = dinput.c dmenu.c draw.c OBJ = ${SRC:.c=.o} -all: options dinput dmenu +all: options draw.o dinput dmenu options: @echo dmenu build options:@@ -18,7 +18,7 @@ .c.o:
@echo CC $< @${CC} -c ${CFLAGS} $< -${OBJ}: config.h config.mk draw.c +${OBJ}: config.h config.mk draw.h config.h: @echo creating $@ from config.def.h@@ -26,7 +26,7 @@ @cp config.def.h $@
.o: @echo CC -o $@ - @${CC} -o $@ $< ${LDFLAGS} + @${CC} -o $@ $< draw.o ${LDFLAGS} clean: @echo cleaning
M
dinput.c
→
dinput.c
@@ -25,32 +25,27 @@ /* forward declarations */
static void cleanup(void); static void drawcursor(void); static void drawinput(void); -static void eprint(const char *errstr, ...); static Bool grabkeyboard(void); static void kpress(XKeyEvent * e); static void run(void); static void setup(Bool topbar); #include "config.h" +#include "draw.h" /* variables */ static char *prompt = NULL; static char text[4096]; static int promptw = 0; static int ret = 0; -static int screen; -static unsigned int mw, mh; static unsigned int cursor = 0; static unsigned int numlockmask = 0; static Bool running = True; -static Display *dpy; -static Window parent, win; - -#include "draw.c" +static Window win; void cleanup(void) { - dccleanup(); + drawcleanup(); XDestroyWindow(dpy, win); XUngrabKeyboard(dpy, CurrentTime); }@@ -84,16 +79,6 @@ drawtext(*text ? text : NULL, dc.norm);
drawcursor(); XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0); XFlush(dpy); -} - -void -eprint(const char *errstr, ...) { - va_list ap; - - va_start(ap, errstr); - vfprintf(stderr, errstr, ap); - va_end(ap); - exit(EXIT_FAILURE); } Bool@@ -318,7 +303,7 @@ DefaultDepth(dpy, screen), CopyFromParent,
DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); - dcsetup(); + drawsetup(); if(prompt) promptw = MIN(textw(prompt), mw / 5); cursor = strlen(text);
M
draw.c
→
draw.c
@@ -1,37 +1,20 @@
/* See LICENSE file for copyright and license details. */ +#include <ctype.h> +#include <locale.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <strings.h> +#include <X11/Xlib.h> +#include "draw.h" -/* enums */ -enum { ColFG, ColBG, ColLast }; - -/* typedefs */ -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 */ - -/* forward declarations */ -static void dccleanup(void); -static void dcsetup(void); -static void drawtext(const char *text, unsigned long col[ColLast]); -static unsigned long getcolor(const char *colstr); -static void initfont(const char *fontstr); -static int textnw(const char *text, unsigned int len); -static int textw(const char *text); - -static DC dc; +/* macros */ +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) void -dccleanup(void) { +drawcleanup(void) { if(dc.font.set) XFreeFontSet(dpy, dc.font.set); else@@ -41,7 +24,7 @@ XFreeGC(dpy, dc.gc);
} void -dcsetup(void) { +drawsetup(void) { /* style */ dc.norm[ColBG] = getcolor(normbgcolor); dc.norm[ColFG] = getcolor(normfgcolor);@@ -82,6 +65,16 @@ 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); +} + +void +eprint(const char *errstr, ...) { + va_list ap; + + va_start(ap, errstr); + vfprintf(stderr, errstr, ap); + va_end(ap); + exit(EXIT_FAILURE); } unsigned long
A
draw.h
@@ -0,0 +1,45 @@
+/* See LICENSE file for copyright and license details. */ + +/* enums */ +enum { ColFG, ColBG, ColLast }; + +/* typedefs */ +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 */ + +/* forward declarations */ +void drawcleanup(void); +void drawsetup(void); +void drawtext(const char *text, unsigned long col[ColLast]); +void eprint(const char *errstr, ...); +unsigned long getcolor(const char *colstr); +void initfont(const char *fontstr); +int textnw(const char *text, unsigned int len); +int textw(const char *text); + +/* variables */ +Display *dpy; +DC dc; +int screen; +unsigned int mw, mh; +unsigned int spaceitem; +Window parent; + +/* style */ +const char *font; +const char *normbgcolor; +const char *normfgcolor; +const char *selbgcolor; +const char *selfgcolor;