]> git.armaanb.net Git - dwm.git/blobdiff - dwm.c
dwm: use ecalloc, prevent theoretical overflow
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index 6f716e9925912a11e9e436c624e5b6001d7aff7c..6295831b976059a604bedb3db5025c6551be12af 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -39,6 +39,7 @@
 #ifdef XINERAMA
 #include <X11/extensions/Xinerama.h>
 #endif /* XINERAMA */
+#include <X11/Xft/Xft.h>
 
 #include "drw.h"
 #include "util.h"
@@ -54,7 +55,7 @@
 #define WIDTH(X)                ((X)->w + 2 * (X)->bw)
 #define HEIGHT(X)               ((X)->h + 2 * (X)->bw)
 #define TAGMASK                 ((1 << LENGTH(tags)) - 1)
-#define TEXTW(X)                (drw_font_getexts_width(drw->font, X, strlen(X)) + drw->font->h)
+#define TEXTW(X)                (drw_text(drw, 0, 0, 0, 0, (X), 0) + drw->fonts[0]->h)
 
 /* enums */
 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
@@ -263,7 +264,6 @@ static Cur *cursor[CurLast];
 static ClrScheme scheme[SchemeLast];
 static Display *dpy;
 static Drw *drw;
-static Fnt *fnt;
 static Monitor *mons, *selmon;
 static Window root;
 
@@ -462,6 +462,7 @@ cleanup(void) {
        Arg a = {.ui = ~0};
        Layout foo = { "", NULL };
        Monitor *m;
+       size_t i;
 
        view(&a);
        selmon->lt[selmon->sellt] = &foo;
@@ -471,16 +472,13 @@ cleanup(void) {
        XUngrabKey(dpy, AnyKey, AnyModifier, root);
        while(mons)
                cleanupmon(mons);
-       drw_cur_free(drw, cursor[CurNormal]);
-       drw_cur_free(drw, cursor[CurResize]);
-       drw_cur_free(drw, cursor[CurMove]);
-       drw_font_free(dpy, fnt);
-       drw_clr_free(scheme[SchemeNorm].border);
-       drw_clr_free(scheme[SchemeNorm].bg);
-       drw_clr_free(scheme[SchemeNorm].fg);
-       drw_clr_free(scheme[SchemeSel].border);
-       drw_clr_free(scheme[SchemeSel].bg);
-       drw_clr_free(scheme[SchemeSel].fg);
+       for(i = 0; i < CurLast; i++)
+               drw_cur_free(drw, cursor[i]);
+       for(i = 0; i < SchemeLast; i++) {
+               drw_clr_free(scheme[i].border);
+               drw_clr_free(scheme[i].bg);
+               drw_clr_free(scheme[i].fg);
+       }
        drw_free(drw);
        XSync(dpy, False);
        XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
@@ -559,7 +557,7 @@ configurenotify(XEvent *e) {
        XConfigureEvent *ev = &e->xconfigure;
        Bool dirty;
 
-       // TODO: updategeom handling sucks, needs to be simplified
+       /* TODO: updategeom handling sucks, needs to be simplified */
        if(ev->window == root) {
                dirty = (sw != ev->width || sh != ev->height);
                sw = ev->width;
@@ -632,8 +630,7 @@ Monitor *
 createmon(void) {
        Monitor *m;
 
-       if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
-               die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
+       m = ecalloc(1, sizeof(Monitor));
        m->tagset[0] = m->tagset[1] = 1;
        m->mfact = mfact;
        m->nmaster = nmaster;
@@ -792,7 +789,7 @@ focus(Client *c) {
                detachstack(c);
                attachstack(c);
                grabbuttons(c, True);
-               XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->rgb);
+               XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->pix);
                setfocus(c);
        }
        else {
@@ -1009,8 +1006,7 @@ manage(Window w, XWindowAttributes *wa) {
        Window trans = None;
        XWindowChanges wc;
 
-       if(!(c = calloc(1, sizeof(Client))))
-               die("fatal: could not malloc() %u bytes\n", sizeof(Client));
+       c = ecalloc(1, sizeof(Client));
        c->win = w;
        updatetitle(c);
        if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
@@ -1040,7 +1036,7 @@ manage(Window w, XWindowAttributes *wa) {
 
        wc.border_width = c->bw;
        XConfigureWindow(dpy, w, CWBorderWidth, &wc);
-       XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->rgb);
+       XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->pix);
        configure(c); /* propagates border_width, if size doesn't change */
        updatewindowtype(c);
        updatesizehints(c);
@@ -1123,6 +1119,7 @@ movemouse(const Arg *arg) {
        Client *c;
        Monitor *m;
        XEvent ev;
+       Time lasttime = 0;
 
        if(!(c = selmon->sel))
                return;
@@ -1145,6 +1142,10 @@ movemouse(const Arg *arg) {
                        handler[ev.type](&ev);
                        break;
                case MotionNotify:
+                       if ((ev.xmotion.time - lasttime) <= (1000 / 60))
+                               continue;
+                       lasttime = ev.xmotion.time;
+
                        nx = ocx + (ev.xmotion.x - x);
                        ny = ocy + (ev.xmotion.y - y);
                        if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
@@ -1264,11 +1265,11 @@ resizeclient(Client *c, int x, int y, int w, int h) {
 
 void
 resizemouse(const Arg *arg) {
-       int ocx, ocy;
-       int nw, nh;
+       int ocx, ocy, nw, nh;
        Client *c;
        Monitor *m;
        XEvent ev;
+       Time lasttime = 0;
 
        if(!(c = selmon->sel))
                return;
@@ -1290,6 +1291,10 @@ resizemouse(const Arg *arg) {
                        handler[ev.type](&ev);
                        break;
                case MotionNotify:
+                       if ((ev.xmotion.time - lasttime) <= (1000 / 60))
+                               continue;
+                       lasttime = ev.xmotion.time;
+
                        nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
                        nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
                        if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
@@ -1496,13 +1501,14 @@ setup(void) {
 
        /* init screen */
        screen = DefaultScreen(dpy);
-       root = RootWindow(dpy, screen);
-       fnt = drw_font_create(dpy, font);
        sw = DisplayWidth(dpy, screen);
        sh = DisplayHeight(dpy, screen);
-       bh = fnt->h + 2;
+       root = RootWindow(dpy, screen);
        drw = drw_create(dpy, screen, root, sw, sh);
-       drw_setfont(drw, fnt);
+       drw_load_fonts(drw, fonts, LENGTH(fonts));
+       if (!drw->fontcount)
+               die("no fonts could be loaded.\n");
+       bh = drw->fonts[0]->h + 2;
        updategeom();
        /* init atoms */
        wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
@@ -1542,6 +1548,7 @@ setup(void) {
        XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
        XSelectInput(dpy, root, wa.event_mask);
        grabkeys();
+       focus(NULL);
 }
 
 void
@@ -1563,7 +1570,7 @@ showhide(Client *c) {
 void
 sigchld(int unused) {
        if(signal(SIGCHLD, sigchld) == SIG_ERR)
-               die("Can't install SIGCHLD handler");
+               die("can't install SIGCHLD handler:");
        while(0 < waitpid(-1, NULL, WNOHANG));
 }
 
@@ -1675,7 +1682,7 @@ unfocus(Client *c, Bool setfocus) {
        if(!c)
                return;
        grabbuttons(c, False);
-       XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->rgb);
+       XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->pix);
        if(setfocus) {
                XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
                XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
@@ -1779,8 +1786,7 @@ updategeom(void) {
 
                for(n = 0, m = mons; m; m = m->next, n++);
                /* only consider unique geometries as separate screens */
-               if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
-                       die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
+               unique = ecalloc(nn, sizeof(XineramaScreenInfo));
                for(i = 0, j = 0; i < nn; i++)
                        if(isuniquegeom(unique, j, &info[i]))
                                memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
@@ -2045,7 +2051,7 @@ zoom(const Arg *arg) {
 int
 main(int argc, char *argv[]) {
        if(argc == 2 && !strcmp("-v", argv[1]))
-               die("dwm-"VERSION", © 2006-2012 dwm engineers, see LICENSE for details\n");
+               die("dwm-"VERSION", © 2006-2015 dwm engineers, see LICENSE for details\n");
        else if(argc != 1)
                die("usage: dwm [-v]\n");
        if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())