summaryrefslogtreecommitdiffstats
path: root/dwm.c
diff options
context:
space:
mode:
Diffstat (limited to 'dwm.c')
-rw-r--r--dwm.c102
1 files changed, 36 insertions, 66 deletions
diff --git a/dwm.c b/dwm.c
index 45191e9..4986b07 100644
--- a/dwm.c
+++ b/dwm.c
@@ -37,6 +37,8 @@
37#include <X11/Xproto.h> 37#include <X11/Xproto.h>
38#include <X11/Xutil.h> 38#include <X11/Xutil.h>
39#include <X11/XKBlib.h> 39#include <X11/XKBlib.h>
40#include <fontconfig/fontconfig.h>
41#include <X11/Xft/Xft.h>
40#ifdef XINERAMA 42#ifdef XINERAMA
41#include <X11/extensions/Xinerama.h> 43#include <X11/extensions/Xinerama.h>
42#endif /* XINERAMA */ 44#endif /* XINERAMA */
@@ -100,16 +102,15 @@ struct Client {
100 102
101typedef struct { 103typedef struct {
102 int x, y, w, h; 104 int x, y, w, h;
103 unsigned long norm[ColLast]; 105 XftColor norm[ColLast];
104 unsigned long sel[ColLast]; 106 XftColor sel[ColLast];
105 Drawable drawable; 107 Drawable drawable;
106 GC gc; 108 GC gc;
107 struct { 109 struct {
108 int ascent; 110 int ascent;
109 int descent; 111 int descent;
110 int height; 112 int height;
111 XFontSet set; 113 XftFont *xfont;
112 XFontStruct *xfont;
113 } font; 114 } font;
114} DC; /* draw context */ 115} DC; /* draw context */
115 116
@@ -179,15 +180,15 @@ static void die(const char *errstr, ...);
179static Monitor *dirtomon(int dir); 180static Monitor *dirtomon(int dir);
180static void drawbar(Monitor *m); 181static void drawbar(Monitor *m);
181static void drawbars(void); 182static void drawbars(void);
182static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]); 183static void drawsquare(Bool filled, Bool empty, Bool invert, XftColor col[ColLast]);
183static void drawtext(const char *text, unsigned long col[ColLast], Bool invert); 184static void drawtext(const char *text, XftColor col[ColLast], Bool invert);
184static void enternotify(XEvent *e); 185static void enternotify(XEvent *e);
185static void expose(XEvent *e); 186static void expose(XEvent *e);
186static void focus(Client *c); 187static void focus(Client *c);
187static void focusin(XEvent *e); 188static void focusin(XEvent *e);
188static void focusmon(const Arg *arg); 189static void focusmon(const Arg *arg);
189static void focusstack(const Arg *arg); 190static void focusstack(const Arg *arg);
190static unsigned long getcolor(const char *colstr); 191static XftColor getcolor(const char *colstr);
191static Bool getrootptr(int *x, int *y); 192static Bool getrootptr(int *x, int *y);
192static long getstate(Window w); 193static long getstate(Window w);
193static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size); 194static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
@@ -487,10 +488,6 @@ cleanup(void) {
487 for(m = mons; m; m = m->next) 488 for(m = mons; m; m = m->next)
488 while(m->stack) 489 while(m->stack)
489 unmanage(m->stack, False); 490 unmanage(m->stack, False);
490 if(dc.font.set)
491 XFreeFontSet(dpy, dc.font.set);
492 else
493 XFreeFont(dpy, dc.font.xfont);
494 XUngrabKey(dpy, AnyKey, AnyModifier, root); 491 XUngrabKey(dpy, AnyKey, AnyModifier, root);
495 XFreePixmap(dpy, dc.drawable); 492 XFreePixmap(dpy, dc.drawable);
496 XFreeGC(dpy, dc.gc); 493 XFreeGC(dpy, dc.gc);
@@ -723,7 +720,7 @@ void
723drawbar(Monitor *m) { 720drawbar(Monitor *m) {
724 int x; 721 int x;
725 unsigned int i, occ = 0, urg = 0; 722 unsigned int i, occ = 0, urg = 0;
726 unsigned long *col; 723 XftColor *col;
727 Client *c; 724 Client *c;
728 725
729 for(c = m->clients; c; c = c->next) { 726 for(c = m->clients; c; c = c->next) {
@@ -778,10 +775,10 @@ drawbars(void) {
778} 775}
779 776
780void 777void
781drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) { 778drawsquare(Bool filled, Bool empty, Bool invert, XftColor col[ColLast]) {
782 int x; 779 int x;
783 780
784 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]); 781 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG].pixel);
785 x = (dc.font.ascent + dc.font.descent + 2) / 4; 782 x = (dc.font.ascent + dc.font.descent + 2) / 4;
786 if(filled) 783 if(filled)
787 XFillRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x+1, x+1); 784 XFillRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x+1, x+1);
@@ -790,11 +787,12 @@ drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
790} 787}
791 788
792void 789void
793drawtext(const char *text, unsigned long col[ColLast], Bool invert) { 790drawtext(const char *text, XftColor col[ColLast], Bool invert) {
794 char buf[256]; 791 char buf[256];
795 int i, x, y, h, len, olen; 792 int i, x, y, h, len, olen;
793 XftDraw *d;
796 794
797 XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]); 795 XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG].pixel);
798 XFillRectangle(dpy, dc.drawable, dc.gc, dc.x, dc.y, dc.w, dc.h); 796 XFillRectangle(dpy, dc.drawable, dc.gc, dc.x, dc.y, dc.w, dc.h);
799 if(!text) 797 if(!text)
800 return; 798 return;
@@ -809,11 +807,11 @@ drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
809 memcpy(buf, text, len); 807 memcpy(buf, text, len);
810 if(len < olen) 808 if(len < olen)
811 for(i = len; i && i > len - 3; buf[--i] = '.'); 809 for(i = len; i && i > len - 3; buf[--i] = '.');
812 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]); 810
813 if(dc.font.set) 811 d = XftDrawCreate(dpy, dc.drawable, DefaultVisual(dpy, screen), DefaultColormap(dpy,screen));
814 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len); 812
815 else 813 XftDrawStringUtf8(d, &col[invert ? ColBG : ColFG], dc.font.xfont, x, y, (XftChar8 *) buf, len);
816 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len); 814 XftDrawDestroy(d);
817} 815}
818 816
819void 817void
@@ -859,7 +857,7 @@ focus(Client *c) {
859 detachstack(c); 857 detachstack(c);
860 attachstack(c); 858 attachstack(c);
861 grabbuttons(c, True); 859 grabbuttons(c, True);
862 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]); 860 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder].pixel);
863 setfocus(c); 861 setfocus(c);
864 } 862 }
865 else { 863 else {
@@ -933,14 +931,14 @@ getatomprop(Client *c, Atom prop) {
933 return atom; 931 return atom;
934} 932}
935 933
936unsigned long 934XftColor
937getcolor(const char *colstr) { 935getcolor(const char *colstr) {
938 Colormap cmap = DefaultColormap(dpy, screen); 936 XftColor color;
939 XColor color;
940 937
941 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color)) 938 if(!XftColorAllocName(dpy, DefaultVisual(dpy, screen), DefaultColormap(dpy, screen), colstr, &color))
942 die("error, cannot allocate color '%s'\n", colstr); 939 die("error, cannot allocate color '%s'\n", colstr);
943 return color.pixel; 940
941 return color;
944} 942}
945 943
946Bool 944Bool
@@ -1041,35 +1039,13 @@ incnmaster(const Arg *arg) {
1041 1039
1042void 1040void
1043initfont(const char *fontstr) { 1041initfont(const char *fontstr) {
1044 char *def, **missing;
1045 int n;
1046 1042
1047 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def); 1043 if(!(dc.font.xfont = XftFontOpenName(dpy,screen,fontstr))
1048 if(missing) { 1044 && !(dc.font.xfont = XftFontOpenName(dpy,screen,"fixed")))
1049 while(n--) 1045 die("error, cannot load font: '%s'\n", fontstr);
1050 fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]); 1046
1051 XFreeStringList(missing); 1047 dc.font.ascent = dc.font.xfont->ascent;
1052 } 1048 dc.font.descent = dc.font.xfont->descent;
1053 if(dc.font.set) {
1054 XFontStruct **xfonts;
1055 char **font_names;
1056
1057 dc.font.ascent = dc.font.descent = 0;
1058 XExtentsOfFontSet(dc.font.set);
1059 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
1060 while(n--) {
1061 dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
1062 dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
1063 xfonts++;
1064 }
1065 }
1066 else {
1067 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
1068 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
1069 die("error, cannot load font: '%s'\n", fontstr);
1070 dc.font.ascent = dc.font.xfont->ascent;
1071 dc.font.descent = dc.font.xfont->descent;
1072 }
1073 dc.font.height = dc.font.ascent + dc.font.descent; 1049 dc.font.height = dc.font.ascent + dc.font.descent;
1074} 1050}
1075 1051
@@ -1151,7 +1127,7 @@ manage(Window w, XWindowAttributes *wa) {
1151 1127
1152 wc.border_width = c->bw; 1128 wc.border_width = c->bw;
1153 XConfigureWindow(dpy, w, CWBorderWidth, &wc); 1129 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1154 XSetWindowBorder(dpy, w, dc.norm[ColBorder]); 1130 XSetWindowBorder(dpy, w, dc.norm[ColBorder].pixel);
1155 configure(c); /* propagates border_width, if size doesn't change */ 1131 configure(c); /* propagates border_width, if size doesn't change */
1156 updatewindowtype(c); 1132 updatewindowtype(c);
1157 updatesizehints(c); 1133 updatesizehints(c);
@@ -1640,8 +1616,6 @@ setup(void) {
1640 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen)); 1616 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
1641 dc.gc = XCreateGC(dpy, root, 0, NULL); 1617 dc.gc = XCreateGC(dpy, root, 0, NULL);
1642 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter); 1618 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1643 if(!dc.font.set)
1644 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
1645 /* init bars */ 1619 /* init bars */
1646 updatebars(); 1620 updatebars();
1647 updatestatus(); 1621 updatestatus();
@@ -1712,13 +1686,9 @@ tagmon(const Arg *arg) {
1712 1686
1713int 1687int
1714textnw(const char *text, unsigned int len) { 1688textnw(const char *text, unsigned int len) {
1715 XRectangle r; 1689 XGlyphInfo ext;
1716 1690 XftTextExtentsUtf8(dpy, dc.font.xfont, (XftChar8 *) text, len, &ext);
1717 if(dc.font.set) { 1691 return ext.xOff;
1718 XmbTextExtents(dc.font.set, text, len, NULL, &r);
1719 return r.width;
1720 }
1721 return XTextWidth(dc.font.xfont, text, len);
1722} 1692}
1723 1693
1724void 1694void
@@ -1798,7 +1768,7 @@ unfocus(Client *c, Bool setfocus) {
1798 if(!c) 1768 if(!c)
1799 return; 1769 return;
1800 grabbuttons(c, False); 1770 grabbuttons(c, False);
1801 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]); 1771 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder].pixel);
1802 if(setfocus) { 1772 if(setfocus) {
1803 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); 1773 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1804 XDeleteProperty(dpy, root, netatom[NetActiveWindow]); 1774 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);