removed the string-based setgeom approach, introduced a new Geom type instead and a helper macro
aa2395b6
2 file(s) · +40 −87
| 9 | 9 | #define SELBORDERCOLOR "#0066ff" |
|
| 10 | 10 | #define SELBGCOLOR "#0066ff" |
|
| 11 | 11 | #define SELFGCOLOR "#ffffff" |
|
| 12 | - | #define GEOMETRY "0 0 W B " \ |
|
| 13 | - | "0 B W H-B " \ |
|
| 14 | - | "0 B W*0.55 H-B " \ |
|
| 15 | - | "W*0.55 B W*0.45 H-B " \ |
|
| 16 | - | "0 B W H-B" |
|
| 17 | - | ||
| 18 | - | /* Anselm's dual head geometry in the office */ |
|
| 19 | - | #define DUALGEOMETRY "0 0 1280 B " \ |
|
| 20 | - | "0 B W H-B " \ |
|
| 21 | - | "0 B 1280 800-B " \ |
|
| 22 | - | "1280 0 W-1280 H " \ |
|
| 23 | - | "0 B 1280 800-B" |
|
| 24 | - | ||
| 25 | 12 | ||
| 26 | 13 | /* tagging */ |
|
| 27 | 14 | const char tags[][MAXTAGLEN] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" }; |
|
| 34 | 21 | { NULL, NULL, "Acroread", NULL, True }, |
|
| 35 | 22 | }; |
|
| 36 | 23 | ||
| 24 | + | /* geometries, s{x,y,w,h} and bh are already initualized here */ |
|
| 25 | + | /* func name bx by bw wx wy ww wh mx my mw mh tx ty tw th mox moy mow moh */ |
|
| 26 | + | DEFGEOM(single, 0, 0, sw, 0, bh, sw, sh-bh, wx, wy, 0.55*sw, wh, mx+mw, wy, ww-mw, wh, wx, wy, ww, wh) |
|
| 27 | + | DEFGEOM(dual, 0, 0,1280, 0, bh, ww, wh-bh, wx, wy, 1280,800-bh, 1280, 0, ww-mw, sh, mx, my, mw, mh) |
|
| 28 | + | ||
| 29 | + | Geom geoms[] = { |
|
| 30 | + | /* symbol function */ |
|
| 31 | + | { "[]", single }, /* first entry is default */ |
|
| 32 | + | { "[][]", dual }, |
|
| 33 | + | }; |
|
| 34 | + | ||
| 37 | 35 | /* layout(s) */ |
|
| 38 | 36 | #define RESIZEHINTS True /* False - respect size hints in tiled resizals */ |
|
| 39 | 37 | #define SNAP 32 /* snap pixel */ |
|
| 50 | 48 | #define MODKEY Mod1Mask |
|
| 51 | 49 | Key keys[] = { |
|
| 52 | 50 | /* modifier key function argument */ |
|
| 53 | - | { MODKEY, XK_a, setgeom, DUALGEOMETRY }, |
|
| 54 | - | { MODKEY, XK_d, setgeom, GEOMETRY }, |
|
| 51 | + | { MODKEY, XK_a, setgeom, "[][]" }, |
|
| 52 | + | { MODKEY, XK_d, setgeom, "[]" }, |
|
| 55 | 53 | { MODKEY, XK_p, spawn, |
|
| 56 | 54 | "exec dmenu_run -fn '"FONT"' -nb '"NORMBGCOLOR"' -nf '"NORMFGCOLOR"' -sb '"SELBGCOLOR"' -sf '"SELFGCOLOR"'" }, |
|
| 57 | 55 | { MODKEY|ShiftMask, XK_Return, spawn, "exec uxterm" }, |
|
| 46 | 46 | #define LENGTH(x) (sizeof x / sizeof x[0]) |
|
| 47 | 47 | #define MAXTAGLEN 16 |
|
| 48 | 48 | #define MOUSEMASK (BUTTONMASK|PointerMotionMask) |
|
| 49 | + | #define DEFGEOM(GEONAME,BX,BY,BW,WX,WY,WW,WH,MX,MY,MW,MH,TX,TY,TW,TH,MOX,MOY,MOW,MOH) \ |
|
| 50 | + | void GEONAME(void) { \ |
|
| 51 | + | bx = (BX); by = (BY); bw = (BW); \ |
|
| 52 | + | wx = (WX); wy = (WY); ww = (WW); wh = (WH); \ |
|
| 53 | + | mx = (MX); my = (MY); mw = (MW); mh = (MH); \ |
|
| 54 | + | tx = (TX); ty = (TY); tw = (TW); th = (TH); \ |
|
| 55 | + | mox = (MOX); moy = (MOY); mow = (MOW); moh = (MOH); \ |
|
| 56 | + | } |
|
| 49 | 57 | ||
| 50 | 58 | /* enums */ |
|
| 51 | 59 | enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */ |
|
| 86 | 94 | } DC; /* draw context */ |
|
| 87 | 95 | ||
| 88 | 96 | typedef struct { |
|
| 97 | + | const char *symbol; |
|
| 98 | + | void (*apply)(void); |
|
| 99 | + | } Geom; |
|
| 100 | + | ||
| 101 | + | typedef struct { |
|
| 89 | 102 | unsigned long mod; |
|
| 90 | 103 | KeySym keysym; |
|
| 91 | 104 | void (*func)(const char *arg); |
|
| 107 | 120 | } Rule; |
|
| 108 | 121 | ||
| 109 | 122 | /* function declarations */ |
|
| 110 | - | void applygeom(const char *arg); |
|
| 111 | 123 | void applyrules(Client *c); |
|
| 112 | 124 | void arrange(void); |
|
| 113 | 125 | void attach(Client *c); |
|
| 137 | 149 | void focusprev(const char *arg); |
|
| 138 | 150 | Client *getclient(Window w); |
|
| 139 | 151 | unsigned long getcolor(const char *colstr); |
|
| 140 | - | double getdouble(const char *s); |
|
| 141 | 152 | long getstate(Window w); |
|
| 142 | 153 | Bool gettextprop(Window w, Atom atom, char *text, unsigned int size); |
|
| 143 | 154 | void grabbuttons(Client *c, Bool focused); |
|
| 226 | 237 | Cursor cursor[CurLast]; |
|
| 227 | 238 | Display *dpy; |
|
| 228 | 239 | DC dc = {0}; |
|
| 240 | + | Geom *geom = NULL; |
|
| 229 | 241 | Layout *lt = NULL; |
|
| 230 | 242 | Window root, barwin; |
|
| 231 | 243 | ||
| 235 | 247 | static Bool tmp[LENGTH(tags)]; |
|
| 236 | 248 | ||
| 237 | 249 | /* function implementations */ |
|
| 238 | - | ||
| 239 | - | void |
|
| 240 | - | applygeometry(const char *arg) { |
|
| 241 | - | static const char *lastArg = NULL; |
|
| 242 | - | char delim, op, *s, *e, *p; |
|
| 243 | - | double val; |
|
| 244 | - | int i, *map[] = { &bx, &by, &bw, &bh, |
|
| 245 | - | &wx, &wy, &ww, &wh, |
|
| 246 | - | &mx, &my, &mw, &mh, |
|
| 247 | - | &tx, &ty, &tw, &th, |
|
| 248 | - | &mox, &moy, &mow, &moh }; |
|
| 249 | - | ||
| 250 | - | if(!arg) |
|
| 251 | - | arg = lastArg; |
|
| 252 | - | else |
|
| 253 | - | lastArg = arg; |
|
| 254 | - | if(!lastArg) |
|
| 255 | - | return; |
|
| 256 | - | strncpy(buf, arg, sizeof buf); |
|
| 257 | - | for(i = 0, e = s = buf; i < LENGTH(map) && e; e++) |
|
| 258 | - | if(*e == ' ' || *e == 0) { |
|
| 259 | - | delim = *e; |
|
| 260 | - | *e = 0; |
|
| 261 | - | op = 0; |
|
| 262 | - | /* check if there is an operator */ |
|
| 263 | - | for(p = s; p < e && *p != '-' && *p != '+' && *p != '*'; p++); |
|
| 264 | - | if(*p) { |
|
| 265 | - | op = *p; |
|
| 266 | - | *p = 0; |
|
| 267 | - | } |
|
| 268 | - | val = getdouble(s); |
|
| 269 | - | if(op && p > s) { /* intermediate operand, e.g. H-B */ |
|
| 270 | - | *(map[i]) = (int)val; |
|
| 271 | - | s = ++p; |
|
| 272 | - | val = getdouble(s); |
|
| 273 | - | } |
|
| 274 | - | switch(op) { |
|
| 275 | - | default: *(map[i]) = (int)val; break; |
|
| 276 | - | case '-': *(map[i]) -= (int)val; break; |
|
| 277 | - | case '+': *(map[i]) += (int)val; break; |
|
| 278 | - | case '*': *(map[i]) = (int)(((double)*(map[i])) * val); break; |
|
| 279 | - | } |
|
| 280 | - | if(delim == 0) |
|
| 281 | - | e = NULL; |
|
| 282 | - | else |
|
| 283 | - | s = ++e; |
|
| 284 | - | i++; |
|
| 285 | - | } |
|
| 286 | - | } |
|
| 287 | 250 | ||
| 288 | 251 | void |
|
| 289 | 252 | applyrules(Client *c) { |
|
| 1438 | 1401 | PropModeReplace, (unsigned char *)data, 2); |
|
| 1439 | 1402 | } |
|
| 1440 | 1403 | ||
| 1441 | - | double |
|
| 1442 | - | getdouble(const char *s) { |
|
| 1443 | - | char *endp; |
|
| 1444 | - | double result = 0; |
|
| 1445 | - | ||
| 1446 | - | switch(*s) { |
|
| 1447 | - | default: |
|
| 1448 | - | result = strtod(s, &endp); |
|
| 1449 | - | if(s == endp || *endp != 0) |
|
| 1450 | - | result = strtol(s, &endp, 0); |
|
| 1451 | - | break; |
|
| 1452 | - | case 'B': result = dc.font.height + 2; break; |
|
| 1453 | - | case 'W': result = sw; break; |
|
| 1454 | - | case 'H': result = sh; break; |
|
| 1455 | - | } |
|
| 1456 | - | return result; |
|
| 1457 | - | } |
|
| 1458 | - | ||
| 1459 | 1404 | void |
|
| 1460 | 1405 | setgeom(const char *arg) { |
|
| 1461 | - | applygeometry(arg); |
|
| 1406 | + | unsigned int i; |
|
| 1407 | + | ||
| 1408 | + | for(i = 0; arg && i < LENGTH(geoms); i++) |
|
| 1409 | + | if(!strcmp(geoms[i].symbol, arg)) |
|
| 1410 | + | break; |
|
| 1411 | + | if(i == LENGTH(geoms)) |
|
| 1412 | + | return; |
|
| 1413 | + | geom = &geoms[i]; |
|
| 1414 | + | geom->apply(); |
|
| 1462 | 1415 | updatebarpos(); |
|
| 1463 | 1416 | arrange(); |
|
| 1464 | 1417 | } |
|
| 1497 | 1450 | root = RootWindow(dpy, screen); |
|
| 1498 | 1451 | initfont(FONT); |
|
| 1499 | 1452 | ||
| 1500 | - | /* apply default dimensions */ |
|
| 1453 | + | /* apply default geometry */ |
|
| 1501 | 1454 | sx = 0; |
|
| 1502 | 1455 | sy = 0; |
|
| 1503 | 1456 | sw = DisplayWidth(dpy, screen); |
|
| 1504 | 1457 | sh = DisplayHeight(dpy, screen); |
|
| 1505 | - | applygeometry(GEOMETRY); |
|
| 1458 | + | bh = dc.font.height + 2; |
|
| 1459 | + | geom = &geoms[0]; |
|
| 1460 | + | geom->apply(); |
|
| 1506 | 1461 | ||
| 1507 | 1462 | /* init atoms */ |
|
| 1508 | 1463 | wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); |
|