]> git.armaanb.net Git - dmenu.git/blobdiff - dmenu.c
applied Matthias Christian Ott's sanity fixes (thanks a lot!)
[dmenu.git] / dmenu.c
diff --git a/dmenu.c b/dmenu.c
index 863a262a5679f728bd7e9789fd3693212fb123fe..0f95af68249e9eb4c8d4f882df6a1f416515e145 100644 (file)
--- a/dmenu.c
+++ b/dmenu.c
@@ -1,5 +1,4 @@
 /* See LICENSE file for copyright and license details. */
-#define _BSD_SOURCE
 #include <ctype.h>
 #include <locale.h>
 #include <stdarg.h>
@@ -18,6 +17,7 @@
 /* macros */
 #define CLEANMASK(mask)         (mask & ~(numlockmask | LockMask))
 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
+#define MIN(a, b)               ((a) < (b) ? (a) : (b))
 
 /* enums */
 enum { ColFG, ColBG, ColLast };
@@ -78,7 +78,7 @@ static unsigned int mw, mh;
 static unsigned int numlockmask = 0;
 static Bool running = True;
 static Display *dpy;
-static DC dc = {0};
+static DC dc;
 static Item *allitems = NULL;  /* first of all items */
 static Item *item = NULL;      /* first of pattern matching items */
 static Item *sel = NULL;
@@ -140,7 +140,7 @@ cistrstr(const char *s, const char *sub) {
                do {
                        do {
                                if((csub = *s++) == 0)
-                                       return (NULL);
+                                       return NULL;
                        }
                        while(tolower(csub) != c);
                }
@@ -213,37 +213,25 @@ drawmenu(void) {
 
 void
 drawtext(const char *text, unsigned long col[ColLast]) {
-       int x, y, w, h;
-       static char buf[256];
-       unsigned int len, olen;
+       char buf[256];
+       int i, x, y, h, 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;
+       olen = strlen(text);
        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 */
+       for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
+       if(!len)
+               return;
+       memcpy(buf, text, len);
+       if(len < olen)
+               for(i = len; i && i > len - 3; buf[--i] = '.');
        XSetForeground(dpy, dc.gc, col[ColFG]);
        if(dc.font.set)
                XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
@@ -292,8 +280,6 @@ initfont(const char *fontstr) {
        if(!fontstr || fontstr[0] == '\0')
                eprint("error, cannot load font: '%s'\n", fontstr);
        missing = NULL;
-       if(dc.font.set)
-               XFreeFontSet(dpy, dc.font.set);
        dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
        if(missing)
                XFreeStringList(missing);
@@ -313,9 +299,6 @@ initfont(const char *fontstr) {
                }
        }
        else {
-               if(dc.font.xfont)
-                       XFreeFont(dpy, dc.font.xfont);
-               dc.font.xfont = NULL;
                if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
                && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
                        eprint("error, cannot load font: '%s'\n", fontstr);
@@ -334,7 +317,7 @@ kpress(XKeyEvent * e) {
 
        len = strlen(text);
        buf[0] = 0;
-       num = XLookupString(e, buf, sizeof buf, &ksym, 0);
+       num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
        if(IsKeypadKey(ksym)) {
                if(ksym == XK_KP_Enter)
                        ksym = XK_Return;
@@ -412,10 +395,7 @@ kpress(XKeyEvent * e) {
        default:
                if(num && !iscntrl((int) buf[0])) {
                        buf[num] = 0;
-                       if(len > 0)
-                               strncat(text, buf, sizeof text);
-                       else
-                               strncpy(text, buf, sizeof text);
+                       strncpy(text + len, buf, sizeof text - len);
                        match(text);
                }
                break;
@@ -553,7 +533,7 @@ readstdin(void) {
                        maxname = p;
                        max = len;
                }
-               if((new = (Item *)malloc(sizeof(Item))) == NULL)
+               if(!(new = (Item *)malloc(sizeof(Item))))
                        eprint("fatal: could not malloc() %u bytes\n", sizeof(Item));
                new->next = new->left = new->right = NULL;
                new->text = p;
@@ -587,11 +567,12 @@ run(void) {
 void
 setup(Bool topbar) {
        int i, j, x, y;
-       XModifierKeymap *modmap;
-       XSetWindowAttributes wa;
 #if XINERAMA
+       int n;
        XineramaScreenInfo *info = NULL;
 #endif
+       XModifierKeymap *modmap;
+       XSetWindowAttributes wa;
 
        /* init modifier map */
        modmap = XGetModifierMapping(dpy);
@@ -611,17 +592,15 @@ setup(Bool topbar) {
        initfont(font);
 
        /* menu window */
-       wa.override_redirect = 1;
+       wa.override_redirect = True;
        wa.background_pixmap = ParentRelative;
        wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
 
        /* menu window geometry */
        mh = dc.font.height + 2;
 #if XINERAMA
-       if(XineramaIsActive(dpy)) {
-               int n;
+       if(XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
                i = 0;
-               info = XineramaQueryScreens(dpy, &n);
                if(n > 1) {
                        int di;
                        unsigned int dui;
@@ -651,7 +630,7 @@ setup(Bool topbar) {
 
        /* pixmap */
        dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
-       dc.gc = XCreateGC(dpy, root, 0, 0);
+       dc.gc = XCreateGC(dpy, root, 0, NULL);
        XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
        if(!dc.font.set)
                XSetFont(dpy, dc.gc, dc.font.xfont->fid);
@@ -722,7 +701,7 @@ main(int argc, char *argv[]) {
                               "             [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
        if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
                fprintf(stderr, "warning: no locale support\n");
-       if(!(dpy = XOpenDisplay(0)))
+       if(!(dpy = XOpenDisplay(NULL)))
                eprint("dmenu: cannot open display\n");
        screen = DefaultScreen(dpy);
        root = RootWindow(dpy, screen);