dwm-statuscolors-6.1.diff (7140B)
1 diff -up dwm/drw.c dwm-statuscolors/drw.c 2 --- dwm/drw.c 2015-11-08 15:39:37.000000000 -0700 3 +++ dwm-statuscolors/drw.c 2015-11-28 15:42:28.534399525 -0700 4 @@ -206,6 +206,68 @@ drw_setscheme(Drw *drw, ClrScheme *schem 5 drw->scheme = scheme; 6 } 7 8 +int 9 +drw_get_width(Drw *drw, int numcolors, const char *text) 10 +{ 11 + int i; 12 + Fnt *curfont = drw->fonts[0]; 13 + int w = drw_text(drw, 0, 0, 0, 0, text, 0) + curfont->h; 14 + 15 + for (i = 0; i < strlen(text); i++) { 16 + if (text[i] > 0 && text[i] <= numcolors) { 17 + /* we found a color code 18 + * drw_text counted it as a normal character and added one character's width 19 + * we aren't going to render this character, so we remove one character's width */ 20 + w -= curfont->xfont->max_advance_width; 21 + 22 + if (i == 0 || i + 1 == strlen(text)) { 23 + /* we're on the first or the last character of the string 24 + * drw_text already added one character's height (divided by 2) as padding to the beginning and end 25 + * we don't want to double this padding, so we skip this character */ 26 + continue; 27 + } 28 + 29 + if (text[i - 1] > 0 && text[i - 1] <= numcolors) { 30 + /* the previous character was also a color code 31 + * we already added padding in the previous iteration 32 + * we don't want to double this padding, so we skip this character */ 33 + continue; 34 + } 35 + 36 + /* we are somewhere in the middle of the string and the color has changed 37 + * we want to add one character's height (divided by 2) as padding to the end of the previous colored text 38 + * and to the beginning of the new colored text */ 39 + w += curfont->h; 40 + } 41 + } 42 + 43 + return w; 44 +} 45 + 46 +void 47 +drw_colored_text(Drw *drw, ClrScheme *scheme, int numcolors, int x, int y, unsigned int w, unsigned int h, char *text) 48 +{ 49 + if (!drw || !drw->fontcount || !drw->scheme) 50 + return; 51 + 52 + char *buf = text, *ptr = buf, c = 1; 53 + int i; 54 + 55 + while (*ptr) { 56 + for (i = 0; *ptr < 0 || *ptr > numcolors; i++, ptr++); 57 + if (!*ptr) 58 + break; 59 + c = *ptr; 60 + *ptr = 0; 61 + if (i) 62 + x = drw_text(drw, x, y, w, h, buf, 0) + drw->fonts[0]->h; 63 + *ptr = c; 64 + drw_setscheme(drw, &scheme[c-1]); 65 + buf = ++ptr; 66 + } 67 + drw_text(drw, x, y, w, h, buf, 0); 68 +} 69 + 70 void 71 drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int empty, int invert) 72 { 73 diff -up dwm/drw.h dwm-statuscolors/drw.h 74 --- dwm/drw.h 2015-11-08 15:39:37.000000000 -0700 75 +++ dwm-statuscolors/drw.h 2015-11-28 15:39:44.427726312 -0700 76 @@ -67,6 +67,8 @@ void drw_setfont(Drw *, Fnt *); 77 void drw_setscheme(Drw *, ClrScheme *); 78 79 /* Drawing functions */ 80 +int drw_get_width(Drw *, int, const char *); 81 +void drw_colored_text(Drw *, ClrScheme *, int, int, int, unsigned int, unsigned int, char *); 82 void drw_rect(Drw *, int, int, unsigned int, unsigned int, int, int, int); 83 int drw_text(Drw *, int, int, unsigned int, unsigned int, const char *, int); 84 85 diff -up dwm/dwm.c dwm-statuscolors/dwm.c 86 --- dwm/dwm.c 2015-11-08 15:39:37.000000000 -0700 87 +++ dwm-statuscolors/dwm.c 2015-11-28 15:45:32.134406853 -0700 88 @@ -51,6 +51,7 @@ 89 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy))) 90 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags])) 91 #define LENGTH(X) (sizeof X / sizeof X[0]) 92 +#define MAXCOLORS 9 93 #define MOUSEMASK (BUTTONMASK|PointerMotionMask) 94 #define WIDTH(X) ((X)->w + 2 * (X)->bw) 95 #define HEIGHT(X) ((X)->h + 2 * (X)->bw) 96 @@ -261,7 +262,7 @@ static void (*handler[LASTEvent]) (XEven 97 static Atom wmatom[WMLast], netatom[NetLast]; 98 static int running = 1; 99 static Cur *cursor[CurLast]; 100 -static ClrScheme scheme[SchemeLast]; 101 +static ClrScheme scheme[MAXCOLORS]; 102 static Display *dpy; 103 static Drw *drw; 104 static Monitor *mons, *selmon; 105 @@ -718,35 +719,35 @@ drawbar(Monitor *m) 106 x = 0; 107 for (i = 0; i < LENGTH(tags); i++) { 108 w = TEXTW(tags[i]); 109 - drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]); 110 - drw_text(drw, x, 0, w, bh, tags[i], urg & 1 << i); 111 + drw_setscheme(drw, &scheme[(m->tagset[m->seltags] & 1 << i) ? 1 : (urg & 1 << i ? 2 : 0)]); 112 + drw_text(drw, x, 0, w, bh, tags[i], 0); 113 drw_rect(drw, x + 1, 1, dx, dx, m == selmon && selmon->sel && selmon->sel->tags & 1 << i, 114 - occ & 1 << i, urg & 1 << i); 115 + occ & 1 << i, 0); 116 x += w; 117 } 118 w = blw = TEXTW(m->ltsymbol); 119 - drw_setscheme(drw, &scheme[SchemeNorm]); 120 + drw_setscheme(drw, &scheme[0]); 121 drw_text(drw, x, 0, w, bh, m->ltsymbol, 0); 122 x += w; 123 xx = x; 124 if (m == selmon) { /* status is only drawn on selected monitor */ 125 - w = TEXTW(stext); 126 + w = drw_get_width(drw, NUMCOLORS, stext); 127 x = m->ww - w; 128 if (x < xx) { 129 x = xx; 130 w = m->ww - xx; 131 } 132 - drw_text(drw, x, 0, w, bh, stext, 0); 133 + drw_colored_text(drw, scheme, NUMCOLORS, x, 0, w, bh, stext); 134 } else 135 x = m->ww; 136 if ((w = x - xx) > bh) { 137 x = xx; 138 if (m->sel) { 139 - drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]); 140 + drw_setscheme(drw, &scheme[m == selmon ? 1 : 0]); 141 drw_text(drw, x, 0, w, bh, m->sel->name, 0); 142 drw_rect(drw, x + 1, 1, dx, dx, m->sel->isfixed, m->sel->isfloating, 0); 143 } else { 144 - drw_setscheme(drw, &scheme[SchemeNorm]); 145 + drw_setscheme(drw, &scheme[0]); 146 drw_rect(drw, x, 0, w, bh, 1, 0, 1); 147 } 148 } 149 @@ -807,7 +808,7 @@ focus(Client *c) 150 detachstack(c); 151 attachstack(c); 152 grabbuttons(c, 1); 153 - XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->pix); 154 + XSetWindowBorder(dpy, c->win, scheme[1].border->pix); 155 setfocus(c); 156 } else { 157 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); 158 @@ -1065,7 +1066,7 @@ manage(Window w, XWindowAttributes *wa) 159 160 wc.border_width = c->bw; 161 XConfigureWindow(dpy, w, CWBorderWidth, &wc); 162 - XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->pix); 163 + XSetWindowBorder(dpy, w, scheme[0].border->pix); 164 configure(c); /* propagates border_width, if size doesn't change */ 165 updatewindowtype(c); 166 updatesizehints(c); 167 @@ -1580,12 +1581,12 @@ setup(void) 168 cursor[CurResize] = drw_cur_create(drw, XC_sizing); 169 cursor[CurMove] = drw_cur_create(drw, XC_fleur); 170 /* init appearance */ 171 - scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor); 172 - scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor); 173 - scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor); 174 - scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor); 175 - scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor); 176 - scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor); 177 + for(int i = 0; i < NUMCOLORS; i++){ 178 + scheme[i].border = drw_clr_create(drw, colors[i][0]); 179 + scheme[i].fg = drw_clr_create(drw, colors[i][1]); 180 + scheme[i].bg = drw_clr_create(drw, colors[i][2]); 181 + } 182 + 183 /* init bars */ 184 updatebars(); 185 updatestatus(); 186 @@ -1745,7 +1746,7 @@ unfocus(Client *c, int setfocus) 187 if (!c) 188 return; 189 grabbuttons(c, 0); 190 - XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->pix); 191 + XSetWindowBorder(dpy, c->win, scheme[0].border->pix); 192 if (setfocus) { 193 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); 194 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);