summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--dwm.c290
1 files changed, 143 insertions, 147 deletions
diff --git a/dwm.c b/dwm.c
index b6659ae..9932974 100644
--- a/dwm.c
+++ b/dwm.c
@@ -112,14 +112,14 @@ typedef struct {
112 regex_t *tagregex; 112 regex_t *tagregex;
113} Regs; 113} Regs;
114 114
115/* functions */ 115/* forward declarations */
116
117static void applyrules(Client *c); 116static void applyrules(Client *c);
118static void arrange(void); 117static void arrange(void);
119static void attach(Client *c); 118static void attach(Client *c);
120static void attachstack(Client *c); 119static void attachstack(Client *c);
121static void ban(Client *c); 120static void ban(Client *c);
122static void buttonpress(XEvent *e); 121static void buttonpress(XEvent *e);
122static void checkotherwm(void);
123static void cleanup(void); 123static void cleanup(void);
124static void compileregs(void); 124static void compileregs(void);
125static void configure(Client *c); 125static void configure(Client *c);
@@ -140,17 +140,13 @@ static void focus(Client *c);
140static void focusnext(const char *arg); 140static void focusnext(const char *arg);
141static void focusprev(const char *arg); 141static void focusprev(const char *arg);
142static Client *getclient(Window w); 142static Client *getclient(Window w);
143static unsigned long getcolor(const char *colstr);
143static long getstate(Window w); 144static long getstate(Window w);
144static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size); 145static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
145static void grabbuttons(Client *c, Bool focused); 146static void grabbuttons(Client *c, Bool focused);
146static unsigned int idxoftag(const char *tag); 147static unsigned int idxoftag(const char *tag);
147static void initbar(void);
148static unsigned long initcolor(const char *colstr);
149static void initfont(const char *fontstr); 148static void initfont(const char *fontstr);
150static void initlayouts(void);
151static void initstyle(void);
152static Bool isarrange(void (*func)()); 149static Bool isarrange(void (*func)());
153static Bool isfloating(void);
154static Bool isoccupied(unsigned int t); 150static Bool isoccupied(unsigned int t);
155static Bool isprotodel(Client *c); 151static Bool isprotodel(Client *c);
156static Bool isvisible(Client *c); 152static Bool isvisible(Client *c);
@@ -167,6 +163,7 @@ static void quit(const char *arg);
167static void resize(Client *c, int x, int y, int w, int h, Bool sizehints); 163static void resize(Client *c, int x, int y, int w, int h, Bool sizehints);
168static void resizemouse(Client *c); 164static void resizemouse(Client *c);
169static void restack(void); 165static void restack(void);
166static void run(void);
170static void scan(void); 167static void scan(void);
171static void setclientstate(Client *c, long state); 168static void setclientstate(Client *c, long state);
172static void setlayout(const char *arg); 169static void setlayout(const char *arg);
@@ -236,7 +233,7 @@ static Regs *regs = NULL;
236/* configuration, allows nested code to access above variables */ 233/* configuration, allows nested code to access above variables */
237#include "config.h" 234#include "config.h"
238 235
239/* implementation */ 236/* functions*/
240static void 237static void
241applyrules(Client *c) { 238applyrules(Client *c) {
242 static char buf[512]; 239 static char buf[512];
@@ -338,14 +335,14 @@ buttonpress(XEvent *e) {
338 focus(c); 335 focus(c);
339 if(CLEANMASK(ev->state) != MODKEY) 336 if(CLEANMASK(ev->state) != MODKEY)
340 return; 337 return;
341 if(ev->button == Button1 && (isfloating() || c->isfloating)) { 338 if(ev->button == Button1 && (isarrange(floating) || c->isfloating)) {
342 restack(); 339 restack();
343 movemouse(c); 340 movemouse(c);
344 } 341 }
345 else if(ev->button == Button2) 342 else if(ev->button == Button2)
346 zoom(NULL); 343 zoom(NULL);
347 else if(ev->button == Button3 344 else if(ev->button == Button3
348 && (isfloating() || c->isfloating) && !c->isfixed) 345 && (isarrange(floating) || c->isfloating) && !c->isfixed)
349 { 346 {
350 restack(); 347 restack();
351 resizemouse(c); 348 resizemouse(c);
@@ -354,6 +351,22 @@ buttonpress(XEvent *e) {
354} 351}
355 352
356static void 353static void
354checkotherwm(void) {
355 otherwm = False;
356 XSetErrorHandler(xerrorstart);
357
358 /* this causes an error if some other window manager is running */
359 XSelectInput(dpy, root, SubstructureRedirectMask);
360 XSync(dpy, False);
361 if(otherwm)
362 eprint("dwm: another window manager is already running\n");
363 XSync(dpy, False);
364 XSetErrorHandler(NULL);
365 xerrorxlib = XSetErrorHandler(xerror);
366 XSync(dpy, False);
367}
368
369static void
357cleanup(void) { 370cleanup(void) {
358 close(STDIN_FILENO); 371 close(STDIN_FILENO);
359 while(stack) { 372 while(stack) {
@@ -446,7 +459,7 @@ configurerequest(XEvent *e) {
446 c->ismax = False; 459 c->ismax = False;
447 if(ev->value_mask & CWBorderWidth) 460 if(ev->value_mask & CWBorderWidth)
448 c->border = ev->border_width; 461 c->border = ev->border_width;
449 if(c->isfixed || c->isfloating || isfloating()) { 462 if(c->isfixed || c->isfloating || isarrange(floating)) {
450 if(ev->value_mask & CWX) 463 if(ev->value_mask & CWX)
451 c->x = ev->x; 464 c->x = ev->x;
452 if(ev->value_mask & CWY) 465 if(ev->value_mask & CWY)
@@ -728,6 +741,16 @@ getclient(Window w) {
728 return c; 741 return c;
729} 742}
730 743
744static unsigned long
745getcolor(const char *colstr) {
746 Colormap cmap = DefaultColormap(dpy, screen);
747 XColor color;
748
749 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
750 eprint("error, cannot allocate color '%s'\n", colstr);
751 return color.pixel;
752}
753
731static long 754static long
732getstate(Window w) { 755getstate(Window w) {
733 int format, status; 756 int format, status;
@@ -821,37 +844,6 @@ idxoftag(const char *tag) {
821} 844}
822 845
823static void 846static void
824initbar(void) {
825 XSetWindowAttributes wa;
826
827 wa.override_redirect = 1;
828 wa.background_pixmap = ParentRelative;
829 wa.event_mask = ButtonPressMask | ExposureMask;
830 barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
831 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
832 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
833 XDefineCursor(dpy, barwin, cursor[CurNormal]);
834 updatebarpos();
835 XMapRaised(dpy, barwin);
836 strcpy(stext, "dwm-"VERSION);
837 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
838 dc.gc = XCreateGC(dpy, root, 0, 0);
839 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
840 if(!dc.font.set)
841 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
842}
843
844static unsigned long
845initcolor(const char *colstr) {
846 Colormap cmap = DefaultColormap(dpy, screen);
847 XColor color;
848
849 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
850 eprint("error, cannot allocate color '%s'\n", colstr);
851 return color.pixel;
852}
853
854static void
855initfont(const char *fontstr) { 847initfont(const char *fontstr) {
856 char *def, **missing; 848 char *def, **missing;
857 int i, n; 849 int i, n;
@@ -893,30 +885,6 @@ initfont(const char *fontstr) {
893 dc.font.height = dc.font.ascent + dc.font.descent; 885 dc.font.height = dc.font.ascent + dc.font.descent;
894} 886}
895 887
896static void
897initlayouts(void) {
898 unsigned int i, w;
899
900 nlayouts = sizeof layouts / sizeof layouts[0];
901 for(blw = i = 0; i < nlayouts; i++) {
902 w = textw(layouts[i].symbol);
903 if(w > blw)
904 blw = w;
905 }
906}
907
908static void
909initstyle(void) {
910 dc.norm[ColBorder] = initcolor(NORMBORDERCOLOR);
911 dc.norm[ColBG] = initcolor(NORMBGCOLOR);
912 dc.norm[ColFG] = initcolor(NORMFGCOLOR);
913 dc.sel[ColBorder] = initcolor(SELBORDERCOLOR);
914 dc.sel[ColBG] = initcolor(SELBGCOLOR);
915 dc.sel[ColFG] = initcolor(SELFGCOLOR);
916 initfont(FONT);
917 dc.h = bh = dc.font.height + 2;
918}
919
920static Bool 888static Bool
921isarrange(void (*func)()) 889isarrange(void (*func)())
922{ 890{
@@ -924,11 +892,6 @@ isarrange(void (*func)())
924} 892}
925 893
926static Bool 894static Bool
927isfloating(void) {
928 return layouts[ltidx].arrange == floating;
929}
930
931static Bool
932isoccupied(unsigned int t) { 895isoccupied(unsigned int t) {
933 Client *c; 896 Client *c;
934 897
@@ -1300,9 +1263,9 @@ restack(void) {
1300 drawbar(); 1263 drawbar();
1301 if(!sel) 1264 if(!sel)
1302 return; 1265 return;
1303 if(sel->isfloating || isfloating()) 1266 if(sel->isfloating || isarrange(floating))
1304 XRaiseWindow(dpy, sel->win); 1267 XRaiseWindow(dpy, sel->win);
1305 if(!isfloating()) { 1268 if(!isarrange(floating)) {
1306 wc.stack_mode = Below; 1269 wc.stack_mode = Below;
1307 wc.sibling = barwin; 1270 wc.sibling = barwin;
1308 if(!sel->isfloating) { 1271 if(!sel->isfloating) {
@@ -1321,6 +1284,54 @@ restack(void) {
1321} 1284}
1322 1285
1323static void 1286static void
1287run(void) {
1288 char *p;
1289 int r, xfd;
1290 fd_set rd;
1291 XEvent ev;
1292
1293 /* main event loop, also reads status text from stdin */
1294 XSync(dpy, False);
1295 xfd = ConnectionNumber(dpy);
1296 readin = True;
1297 while(running) {
1298 FD_ZERO(&rd);
1299 if(readin)
1300 FD_SET(STDIN_FILENO, &rd);
1301 FD_SET(xfd, &rd);
1302 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
1303 if(errno == EINTR)
1304 continue;
1305 eprint("select failed\n");
1306 }
1307 if(FD_ISSET(STDIN_FILENO, &rd)) {
1308 switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
1309 case -1:
1310 strncpy(stext, strerror(errno), sizeof stext - 1);
1311 stext[sizeof stext - 1] = '\0';
1312 readin = False;
1313 break;
1314 case 0:
1315 strncpy(stext, "EOF", 4);
1316 readin = False;
1317 break;
1318 default:
1319 for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
1320 for(; p >= stext && *p != '\n'; --p);
1321 if(p > stext)
1322 strncpy(stext, p + 1, sizeof stext);
1323 }
1324 drawbar();
1325 }
1326 while(XPending(dpy)) {
1327 XNextEvent(dpy, &ev);
1328 if(handler[ev.type])
1329 (handler[ev.type])(&ev); /* call handler */
1330 }
1331 }
1332}
1333
1334static void
1324scan(void) { 1335scan(void) {
1325 unsigned int i, num; 1336 unsigned int i, num;
1326 Window *wins, d1, d2; 1337 Window *wins, d1, d2;
@@ -1401,8 +1412,7 @@ setmwfact(const char *arg) {
1401 1412
1402static void 1413static void
1403setup(void) { 1414setup(void) {
1404 int i, j; 1415 unsigned int i, j, mask;
1405 unsigned int mask;
1406 Window w; 1416 Window w;
1407 XModifierKeymap *modmap; 1417 XModifierKeymap *modmap;
1408 XSetWindowAttributes wa; 1418 XSetWindowAttributes wa;
@@ -1416,37 +1426,80 @@ setup(void) {
1416 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); 1426 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1417 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32, 1427 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1418 PropModeReplace, (unsigned char *) netatom, NetLast); 1428 PropModeReplace, (unsigned char *) netatom, NetLast);
1429
1419 /* init cursors */ 1430 /* init cursors */
1420 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr); 1431 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1421 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing); 1432 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1422 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur); 1433 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1434
1435 /* init geometry */
1436 sx = sy = 0;
1437 sw = DisplayWidth(dpy, screen);
1438 sh = DisplayHeight(dpy, screen);
1439
1423 /* init modifier map */ 1440 /* init modifier map */
1424 modmap = XGetModifierMapping(dpy); 1441 modmap = XGetModifierMapping(dpy);
1425 for (i = 0; i < 8; i++) 1442 for(i = 0; i < 8; i++)
1426 for (j = 0; j < modmap->max_keypermod; j++) { 1443 for(j = 0; j < modmap->max_keypermod; j++) {
1427 if(modmap->modifiermap[i * modmap->max_keypermod + j] 1444 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1428 == XKeysymToKeycode(dpy, XK_Num_Lock)) 1445 == XKeysymToKeycode(dpy, XK_Num_Lock))
1429 numlockmask = (1 << i); 1446 numlockmask = (1 << i);
1430 } 1447 }
1431 XFreeModifiermap(modmap); 1448 XFreeModifiermap(modmap);
1449
1432 /* select for events */ 1450 /* select for events */
1433 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask 1451 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
1434 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask; 1452 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
1435 wa.cursor = cursor[CurNormal]; 1453 wa.cursor = cursor[CurNormal];
1436 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); 1454 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
1437 XSelectInput(dpy, root, wa.event_mask); 1455 XSelectInput(dpy, root, wa.event_mask);
1438 keypress(NULL); /* grabkeys */ 1456
1457 /* grab keys */
1458 keypress(NULL);
1459
1460 /* init tags */
1439 compileregs(); 1461 compileregs();
1440 for(ntags = 0; tags[ntags]; ntags++); 1462 for(ntags = 0; tags[ntags]; ntags++);
1441 seltags = emallocz(sizeof(Bool) * ntags); 1463 seltags = emallocz(sizeof(Bool) * ntags);
1442 seltags[0] = True; 1464 seltags[0] = True;
1443 /* geometry */ 1465
1444 sx = sy = 0; 1466 /* init appearance */
1445 sw = DisplayWidth(dpy, screen); 1467 dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
1446 sh = DisplayHeight(dpy, screen); 1468 dc.norm[ColBG] = getcolor(NORMBGCOLOR);
1447 initstyle(); 1469 dc.norm[ColFG] = getcolor(NORMFGCOLOR);
1448 initlayouts(); 1470 dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
1449 initbar(); 1471 dc.sel[ColBG] = getcolor(SELBGCOLOR);
1472 dc.sel[ColFG] = getcolor(SELFGCOLOR);
1473 initfont(FONT);
1474 dc.h = bh = dc.font.height + 2;
1475
1476 /* init layouts */
1477 mwfact = MWFACT;
1478 nlayouts = sizeof layouts / sizeof layouts[0];
1479 for(blw = i = 0; i < nlayouts; i++) {
1480 j = textw(layouts[i].symbol);
1481 if(j > blw)
1482 blw = j;
1483 }
1484
1485 /* init bar */
1486 bpos = BARPOS;
1487 wa.override_redirect = 1;
1488 wa.background_pixmap = ParentRelative;
1489 wa.event_mask = ButtonPressMask | ExposureMask;
1490 barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
1491 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
1492 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
1493 XDefineCursor(dpy, barwin, cursor[CurNormal]);
1494 updatebarpos();
1495 XMapRaised(dpy, barwin);
1496 strcpy(stext, "dwm-"VERSION);
1497 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
1498 dc.gc = XCreateGC(dpy, root, 0, 0);
1499 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1500 if(!dc.font.set)
1501 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
1502
1450 /* multihead support */ 1503 /* multihead support */
1451 selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); 1504 selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
1452} 1505}
@@ -1568,7 +1621,7 @@ static void
1568togglemax(const char *arg) { 1621togglemax(const char *arg) {
1569 XEvent ev; 1622 XEvent ev;
1570 1623
1571 if(!sel || (!isfloating() && !sel->isfloating) || sel->isfixed) 1624 if(!sel || (!isarrange(floating) && !sel->isfloating) || sel->isfixed)
1572 return; 1625 return;
1573 if((sel->ismax = !sel->ismax)) { 1626 if((sel->ismax = !sel->ismax)) {
1574 sel->rx = sel->x; 1627 sel->rx = sel->x;
@@ -1794,81 +1847,24 @@ zoom(const char *arg) {
1794 1847
1795int 1848int
1796main(int argc, char *argv[]) { 1849main(int argc, char *argv[]) {
1797 char *p;
1798 int r, xfd;
1799 fd_set rd;
1800 XEvent ev;
1801
1802 if(argc == 2 && !strcmp("-v", argv[1])) 1850 if(argc == 2 && !strcmp("-v", argv[1]))
1803 eprint("dwm-"VERSION", © 2006-2007 A. R. Garbe, S. van Dijk, J. Salmi, P. Hruby, S. Nagy\n"); 1851 eprint("dwm-"VERSION", © 2006-2007 A. R. Garbe, S. van Dijk, J. Salmi, P. Hruby, S. Nagy\n");
1804 else if(argc != 1) 1852 else if(argc != 1)
1805 eprint("usage: dwm [-v]\n"); 1853 eprint("usage: dwm [-v]\n");
1806 1854
1807 /* macros from config.h can be used at function level only */
1808 mwfact = MWFACT;
1809 bpos = BARPOS;
1810
1811 setlocale(LC_CTYPE, ""); 1855 setlocale(LC_CTYPE, "");
1812 if(!(dpy = XOpenDisplay(0))) 1856 if(!(dpy = XOpenDisplay(0)))
1813 eprint("dwm: cannot open display\n"); 1857 eprint("dwm: cannot open display\n");
1814 xfd = ConnectionNumber(dpy);
1815 screen = DefaultScreen(dpy); 1858 screen = DefaultScreen(dpy);
1816 root = RootWindow(dpy, screen); 1859 root = RootWindow(dpy, screen);
1817 otherwm = False;
1818 XSetErrorHandler(xerrorstart);
1819 /* this causes an error if some other window manager is running */
1820 XSelectInput(dpy, root, SubstructureRedirectMask);
1821 XSync(dpy, False);
1822 if(otherwm)
1823 eprint("dwm: another window manager is already running\n");
1824 1860
1825 XSync(dpy, False); 1861 checkotherwm();
1826 XSetErrorHandler(NULL);
1827 xerrorxlib = XSetErrorHandler(xerror);
1828 XSync(dpy, False);
1829 setup(); 1862 setup();
1830 drawbar(); 1863 drawbar();
1831 scan(); 1864 scan();
1832 1865 run();
1833 /* main event loop, also reads status text from stdin */
1834 XSync(dpy, False);
1835 readin = True;
1836 while(running) {
1837 FD_ZERO(&rd);
1838 if(readin)
1839 FD_SET(STDIN_FILENO, &rd);
1840 FD_SET(xfd, &rd);
1841 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
1842 if(errno == EINTR)
1843 continue;
1844 eprint("select failed\n");
1845 }
1846 if(FD_ISSET(STDIN_FILENO, &rd)) {
1847 switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
1848 case -1:
1849 strncpy(stext, strerror(errno), sizeof stext - 1);
1850 stext[sizeof stext - 1] = '\0';
1851 readin = False;
1852 break;
1853 case 0:
1854 strncpy(stext, "EOF", 4);
1855 readin = False;
1856 break;
1857 default:
1858 for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
1859 for(; p >= stext && *p != '\n'; --p);
1860 if(p > stext)
1861 strncpy(stext, p + 1, sizeof stext);
1862 }
1863 drawbar();
1864 }
1865 while(XPending(dpy)) {
1866 XNextEvent(dpy, &ev);
1867 if(handler[ev.type])
1868 (handler[ev.type])(&ev); /* call handler */
1869 }
1870 }
1871 cleanup(); 1866 cleanup();
1867
1872 XCloseDisplay(dpy); 1868 XCloseDisplay(dpy);
1873 return 0; 1869 return 0;
1874} 1870}