fixed offsets, updated eprint, cleaned up
[dmenu.git] / draw.c
diff --git a/draw.c b/draw.c
index 41022f0..6791b97 100644 (file)
--- a/draw.c
+++ b/draw.c
@@ -1,37 +1,23 @@
 /* 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 */
-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;
+#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"
+
+/* macros */
+#define MIN(a, b)               ((a) < (b) ? (a) : (b))
+#define MAX(a, b)               ((a) > (b) ? (a) : (b))
+
+/* variables */
+char *progname;
 
 void
-dccleanup(void) {
+drawcleanup(void) {
        if(dc.font.set)
                XFreeFontSet(dpy, dc.font.set);
        else
@@ -41,13 +27,12 @@ dccleanup(void) {
 }
 
 void
-dcsetup(void) {
+drawsetup(void) {
        /* style */
        dc.norm[ColBG] = getcolor(normbgcolor);
        dc.norm[ColFG] = getcolor(normfgcolor);
        dc.sel[ColBG] = getcolor(selbgcolor);
        dc.sel[ColFG] = getcolor(selfgcolor);
-       initfont(font);
 
        /* pixmap */
        dc.drawable = XCreatePixmap(dpy, parent, mw, mh, DefaultDepth(dpy, screen));
@@ -85,13 +70,24 @@ drawtext(const char *text, unsigned long col[ColLast]) {
                XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
 }
 
+void
+eprint(const char *errstr, ...) {
+       va_list ap;
+
+       fprintf(stderr, "%s: ", progname);
+       va_start(ap, errstr);
+       vfprintf(stderr, errstr, ap);
+       va_end(ap);
+       exit(EXIT_FAILURE);
+}
+
 unsigned long
 getcolor(const char *colstr) {
        Colormap cmap = DefaultColormap(dpy, screen);
        XColor color;
 
        if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
-               eprint("drawtext: cannot allocate color '%s'\n", colstr);
+               eprint("cannot allocate color '%s'\n", colstr);
        return color.pixel;
 }
 
@@ -100,8 +96,8 @@ initfont(const char *fontstr) {
        char *def, **missing = NULL;
        int i, n;
 
-       if(!fontstr || fontstr[0] == '\0')
-               eprint("drawtext: cannot load font: '%s'\n", fontstr);
+       if(!fontstr || !*fontstr)
+               eprint("cannot load null font\n");
        dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
        if(missing)
                XFreeStringList(missing);
@@ -119,7 +115,7 @@ initfont(const char *fontstr) {
        else {
                if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
                && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
-                       eprint("drawtext: cannot load font: '%s'\n", fontstr);
+                       eprint("cannot load font '%s'\n", fontstr);
                dc.font.ascent = dc.font.xfont->ascent;
                dc.font.descent = dc.font.xfont->descent;
        }