You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1912 lines
44 KiB

17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
  1. /* See LICENSE file for copyright and license details.
  2. *
  3. * dynamic window manager is designed like any other X client as well. It is
  4. * driven through handling X events. In contrast to other X clients, a window
  5. * manager selects for SubstructureRedirectMask on the root window, to receive
  6. * events about window (dis-)appearance. Only one X connection at a time is
  7. * allowed to select for this event mask.
  8. *
  9. * Calls to fetch an X event from the event queue are blocking. Due reading
  10. * status text from standard input, a select()-driven main loop has been
  11. * implemented which selects for reads on the X connection and STDIN_FILENO to
  12. * handle all data smoothly. The event handlers of dwm are organized in an
  13. * array which is accessed whenever a new event has been fetched. This allows
  14. * event dispatching in O(1) time.
  15. *
  16. * Each child of the root window is called a client, except windows which have
  17. * set the override_redirect flag. Clients are organized in a global
  18. * doubly-linked client list, the focus history is remembered through a global
  19. * stack list. Each client contains an array of Bools of the same size as the
  20. * global tags array to indicate the tags of a client.
  21. *
  22. * Keys and tagging rules are organized as arrays and defined in config.h.
  23. *
  24. * To understand everything else, start reading main().
  25. */
  26. #include <errno.h>
  27. #include <locale.h>
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <sys/select.h>
  34. #include <sys/types.h>
  35. #include <sys/wait.h>
  36. #include <X11/cursorfont.h>
  37. #include <X11/keysym.h>
  38. #include <X11/Xatom.h>
  39. #include <X11/Xlib.h>
  40. #include <X11/Xproto.h>
  41. #include <X11/Xutil.h>
  42. /* macros */
  43. #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
  44. #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
  45. #define LENGTH(x) (sizeof x / sizeof x[0])
  46. #define MAXTAGLEN 16
  47. #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
  48. #define DEFGEOM(GEONAME,BX,BY,BW,WX,WY,WW,WH,MX,MY,MW,MH,TX,TY,TW,TH,MOX,MOY,MOW,MOH) \
  49. void GEONAME(void) { \
  50. bx = (BX); by = (BY); bw = (BW); \
  51. wx = (WX); wy = (WY); ww = (WW); wh = (WH); \
  52. mx = (MX); my = (MY); mw = (MW); mh = (MH); \
  53. tx = (TX); ty = (TY); tw = (TW); th = (TH); \
  54. mox = (MOX); moy = (MOY); mow = (MOW); moh = (MOH); \
  55. }
  56. /* enums */
  57. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  58. enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
  59. enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
  60. enum { WMProtocols, WMDelete, WMName, WMState, WMLast };/* default atoms */
  61. /* typedefs */
  62. typedef struct Client Client;
  63. struct Client {
  64. char name[256];
  65. int x, y, w, h;
  66. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  67. int minax, maxax, minay, maxay;
  68. long flags;
  69. unsigned int bw, oldbw;
  70. Bool isbanned, isfixed, isfloating, isurgent;
  71. Bool *tags;
  72. Client *next;
  73. Client *prev;
  74. Client *snext;
  75. Window win;
  76. };
  77. typedef struct {
  78. int x, y, w, h;
  79. unsigned long norm[ColLast];
  80. unsigned long sel[ColLast];
  81. Drawable drawable;
  82. GC gc;
  83. struct {
  84. int ascent;
  85. int descent;
  86. int height;
  87. XFontSet set;
  88. XFontStruct *xfont;
  89. } font;
  90. } DC; /* draw context */
  91. typedef struct {
  92. const char *symbol;
  93. void (*apply)(void);
  94. } Geom;
  95. typedef struct {
  96. unsigned long mod;
  97. KeySym keysym;
  98. void (*func)(const char *arg);
  99. const char *arg;
  100. } Key;
  101. typedef struct {
  102. const char *symbol;
  103. void (*arrange)(void);
  104. Bool isfloating;
  105. } Layout;
  106. typedef struct {
  107. const char *class;
  108. const char *instance;
  109. const char *title;
  110. const char *tag;
  111. Bool isfloating;
  112. } Rule;
  113. /* function declarations */
  114. void applyrules(Client *c);
  115. void arrange(void);
  116. void attach(Client *c);
  117. void attachstack(Client *c);
  118. void ban(Client *c);
  119. void buttonpress(XEvent *e);
  120. void checkotherwm(void);
  121. void cleanup(void);
  122. void configure(Client *c);
  123. void configurenotify(XEvent *e);
  124. void configurerequest(XEvent *e);
  125. unsigned int counttiled(void);
  126. void destroynotify(XEvent *e);
  127. void detach(Client *c);
  128. void detachstack(Client *c);
  129. void drawbar(void);
  130. void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
  131. void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
  132. void *emallocz(unsigned int size);
  133. void enternotify(XEvent *e);
  134. void eprint(const char *errstr, ...);
  135. void expose(XEvent *e);
  136. void floating(void); /* default floating layout */
  137. void focus(Client *c);
  138. void focusin(XEvent *e);
  139. void focusnext(const char *arg);
  140. void focusprev(const char *arg);
  141. Client *getclient(Window w);
  142. unsigned long getcolor(const char *colstr);
  143. long getstate(Window w);
  144. Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  145. void grabbuttons(Client *c, Bool focused);
  146. void grabkeys(void);
  147. unsigned int idxoftag(const char *t);
  148. void initfont(const char *fontstr);
  149. Bool isoccupied(unsigned int t);
  150. Bool isprotodel(Client *c);
  151. Bool isurgent(unsigned int t);
  152. Bool isvisible(Client *c);
  153. void keypress(XEvent *e);
  154. void killclient(const char *arg);
  155. void manage(Window w, XWindowAttributes *wa);
  156. void mappingnotify(XEvent *e);
  157. void maprequest(XEvent *e);
  158. void monocle(void);
  159. void movemouse(Client *c);
  160. Client *nexttiled(Client *c);
  161. void propertynotify(XEvent *e);
  162. void quit(const char *arg);
  163. void reapply(const char *arg);
  164. void resize(Client *c, int x, int y, int w, int h, Bool sizehints);
  165. void resizemouse(Client *c);
  166. void restack(void);
  167. void run(void);
  168. void scan(void);
  169. void setclientstate(Client *c, long state);
  170. void setgeom(const char *arg);
  171. void setlayout(const char *arg);
  172. void setup(void);
  173. void spawn(const char *arg);
  174. void tag(const char *arg);
  175. unsigned int textnw(const char *text, unsigned int len);
  176. unsigned int textw(const char *text);
  177. void tileh(void);
  178. void tilehstack(unsigned int n);
  179. Client *tilemaster(unsigned int n);
  180. void tileresize(Client *c, int x, int y, int w, int h);
  181. void tilev(void);
  182. void tilevstack(unsigned int n);
  183. void togglefloating(const char *arg);
  184. void toggletag(const char *arg);
  185. void toggleview(const char *arg);
  186. void unban(Client *c);
  187. void unmanage(Client *c);
  188. void unmapnotify(XEvent *e);
  189. void updatebarpos(void);
  190. void updatesizehints(Client *c);
  191. void updatetitle(Client *c);
  192. void updatewmhints(Client *c);
  193. void view(const char *arg);
  194. void viewprevtag(const char *arg); /* views previous selected tags */
  195. int xerror(Display *dpy, XErrorEvent *ee);
  196. int xerrordummy(Display *dpy, XErrorEvent *ee);
  197. int xerrorstart(Display *dpy, XErrorEvent *ee);
  198. void zoom(const char *arg);
  199. /* variables */
  200. char stext[256], buf[256];
  201. int screen, sx, sy, sw, sh;
  202. int (*xerrorxlib)(Display *, XErrorEvent *);
  203. int bx, by, bw, bh, blw, bgw, mx, my, mw, mh, mox, moy, mow, moh, tx, ty, tw, th, wx, wy, ww, wh;
  204. unsigned int numlockmask = 0;
  205. void (*handler[LASTEvent]) (XEvent *) = {
  206. [ButtonPress] = buttonpress,
  207. [ConfigureRequest] = configurerequest,
  208. [ConfigureNotify] = configurenotify,
  209. [DestroyNotify] = destroynotify,
  210. [EnterNotify] = enternotify,
  211. [Expose] = expose,
  212. [FocusIn] = focusin,
  213. [KeyPress] = keypress,
  214. [MappingNotify] = mappingnotify,
  215. [MapRequest] = maprequest,
  216. [PropertyNotify] = propertynotify,
  217. [UnmapNotify] = unmapnotify
  218. };
  219. Atom wmatom[WMLast], netatom[NetLast];
  220. Bool otherwm, readin;
  221. Bool running = True;
  222. Bool *prevtags;
  223. Bool *seltags;
  224. Client *clients = NULL;
  225. Client *sel = NULL;
  226. Client *stack = NULL;
  227. Cursor cursor[CurLast];
  228. Display *dpy;
  229. DC dc = {0};
  230. Geom *geom = NULL;
  231. Layout *lt = NULL;
  232. Window root, barwin;
  233. /* configuration, allows nested code to access above variables */
  234. #include "config.h"
  235. #define TAGSZ (LENGTH(tags) * sizeof(Bool))
  236. static Bool tmp[LENGTH(tags)];
  237. /* function implementations */
  238. void
  239. applyrules(Client *c) {
  240. unsigned int i;
  241. Bool matched = False;
  242. Rule *r;
  243. XClassHint ch = { 0 };
  244. /* rule matching */
  245. XGetClassHint(dpy, c->win, &ch);
  246. for(i = 0; i < LENGTH(rules); i++) {
  247. r = &rules[i];
  248. if(strstr(c->name, r->title)
  249. || (ch.res_class && r->class && strstr(ch.res_class, r->class))
  250. || (ch.res_name && r->instance && strstr(ch.res_name, r->instance)))
  251. {
  252. c->isfloating = r->isfloating;
  253. if(r->tag) {
  254. c->tags[idxoftag(r->tag)] = True;
  255. matched = True;
  256. }
  257. }
  258. }
  259. if(ch.res_class)
  260. XFree(ch.res_class);
  261. if(ch.res_name)
  262. XFree(ch.res_name);
  263. if(!matched)
  264. memcpy(c->tags, seltags, TAGSZ);
  265. }
  266. void
  267. arrange(void) {
  268. Client *c;
  269. for(c = clients; c; c = c->next)
  270. if(isvisible(c))
  271. unban(c);
  272. else
  273. ban(c);
  274. focus(NULL);
  275. lt->arrange();
  276. restack();
  277. }
  278. void
  279. attach(Client *c) {
  280. if(clients)
  281. clients->prev = c;
  282. c->next = clients;
  283. clients = c;
  284. }
  285. void
  286. attachstack(Client *c) {
  287. c->snext = stack;
  288. stack = c;
  289. }
  290. void
  291. ban(Client *c) {
  292. if(c->isbanned)
  293. return;
  294. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  295. c->isbanned = True;
  296. }
  297. void
  298. buttonpress(XEvent *e) {
  299. unsigned int i, x;
  300. Client *c;
  301. XButtonPressedEvent *ev = &e->xbutton;
  302. if(ev->window == barwin) {
  303. x = bgw;
  304. for(i = 0; i < LENGTH(tags); i++) {
  305. x += textw(tags[i]);
  306. if(ev->x > bgw && ev->x < x) {
  307. if(ev->button == Button1) {
  308. if(ev->state & MODKEY)
  309. tag(tags[i]);
  310. else
  311. view(tags[i]);
  312. }
  313. else if(ev->button == Button3) {
  314. if(ev->state & MODKEY)
  315. toggletag(tags[i]);
  316. else
  317. toggleview(tags[i]);
  318. }
  319. return;
  320. }
  321. }
  322. }
  323. else if((c = getclient(ev->window))) {
  324. focus(c);
  325. if(CLEANMASK(ev->state) != MODKEY)
  326. return;
  327. if(ev->button == Button1) {
  328. restack();
  329. movemouse(c);
  330. }
  331. else if(ev->button == Button2) {
  332. if((floating != lt->arrange) && c->isfloating)
  333. togglefloating(NULL);
  334. else
  335. zoom(NULL);
  336. }
  337. else if(ev->button == Button3 && !c->isfixed) {
  338. restack();
  339. resizemouse(c);
  340. }
  341. }
  342. }
  343. void
  344. checkotherwm(void) {
  345. otherwm = False;
  346. XSetErrorHandler(xerrorstart);
  347. /* this causes an error if some other window manager is running */
  348. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  349. XSync(dpy, False);
  350. if(otherwm)
  351. eprint("dwm: another window manager is already running\n");
  352. XSync(dpy, False);
  353. XSetErrorHandler(NULL);
  354. xerrorxlib = XSetErrorHandler(xerror);
  355. XSync(dpy, False);
  356. }
  357. void
  358. cleanup(void) {
  359. close(STDIN_FILENO);
  360. while(stack) {
  361. unban(stack);
  362. unmanage(stack);
  363. }
  364. if(dc.font.set)
  365. XFreeFontSet(dpy, dc.font.set);
  366. else
  367. XFreeFont(dpy, dc.font.xfont);
  368. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  369. XFreePixmap(dpy, dc.drawable);
  370. XFreeGC(dpy, dc.gc);
  371. XFreeCursor(dpy, cursor[CurNormal]);
  372. XFreeCursor(dpy, cursor[CurResize]);
  373. XFreeCursor(dpy, cursor[CurMove]);
  374. XDestroyWindow(dpy, barwin);
  375. XSync(dpy, False);
  376. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  377. }
  378. void
  379. configure(Client *c) {
  380. XConfigureEvent ce;
  381. ce.type = ConfigureNotify;
  382. ce.display = dpy;
  383. ce.event = c->win;
  384. ce.window = c->win;
  385. ce.x = c->x;
  386. ce.y = c->y;
  387. ce.width = c->w;
  388. ce.height = c->h;
  389. ce.border_width = c->bw;
  390. ce.above = None;
  391. ce.override_redirect = False;
  392. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  393. }
  394. void
  395. configurenotify(XEvent *e) {
  396. XConfigureEvent *ev = &e->xconfigure;
  397. if(ev->window == root && (ev->width != sw || ev->height != sh))
  398. setgeom(NULL);
  399. }
  400. void
  401. configurerequest(XEvent *e) {
  402. Client *c;
  403. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  404. XWindowChanges wc;
  405. if((c = getclient(ev->window))) {
  406. if(ev->value_mask & CWBorderWidth)
  407. c->bw = ev->border_width;
  408. if(c->isfixed || c->isfloating || lt->isfloating) {
  409. if(ev->value_mask & CWX)
  410. c->x = sx + ev->x;
  411. if(ev->value_mask & CWY)
  412. c->y = sy + ev->y;
  413. if(ev->value_mask & CWWidth)
  414. c->w = ev->width;
  415. if(ev->value_mask & CWHeight)
  416. c->h = ev->height;
  417. if((c->x - sx + c->w) > sw && c->isfloating)
  418. c->x = sx + (sw / 2 - c->w / 2); /* center in x direction */
  419. if((c->y - sy + c->h) > sh && c->isfloating)
  420. c->y = sy + (sh / 2 - c->h / 2); /* center in y direction */
  421. if((ev->value_mask & (CWX|CWY))
  422. && !(ev->value_mask & (CWWidth|CWHeight)))
  423. configure(c);
  424. if(isvisible(c))
  425. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  426. }
  427. else
  428. configure(c);
  429. }
  430. else {
  431. wc.x = ev->x;
  432. wc.y = ev->y;
  433. wc.width = ev->width;
  434. wc.height = ev->height;
  435. wc.border_width = ev->border_width;
  436. wc.sibling = ev->above;
  437. wc.stack_mode = ev->detail;
  438. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  439. }
  440. XSync(dpy, False);
  441. }
  442. unsigned int
  443. counttiled(void) {
  444. unsigned int n;
  445. Client *c;
  446. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next), n++);
  447. return n;
  448. }
  449. void
  450. destroynotify(XEvent *e) {
  451. Client *c;
  452. XDestroyWindowEvent *ev = &e->xdestroywindow;
  453. if((c = getclient(ev->window)))
  454. unmanage(c);
  455. }
  456. void
  457. detach(Client *c) {
  458. if(c->prev)
  459. c->prev->next = c->next;
  460. if(c->next)
  461. c->next->prev = c->prev;
  462. if(c == clients)
  463. clients = c->next;
  464. c->next = c->prev = NULL;
  465. }
  466. void
  467. detachstack(Client *c) {
  468. Client **tc;
  469. for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
  470. *tc = c->snext;
  471. }
  472. void
  473. drawbar(void) {
  474. int i, x;
  475. Client *c;
  476. dc.x = 0;
  477. dc.w = bgw;
  478. drawtext(geom->symbol, dc.norm, False);
  479. dc.x += bgw;
  480. for(c = stack; c && !isvisible(c); c = c->snext);
  481. for(i = 0; i < LENGTH(tags); i++) {
  482. dc.w = textw(tags[i]);
  483. if(seltags[i]) {
  484. drawtext(tags[i], dc.sel, isurgent(i));
  485. drawsquare(c && c->tags[i], isoccupied(i), isurgent(i), dc.sel);
  486. }
  487. else {
  488. drawtext(tags[i], dc.norm, isurgent(i));
  489. drawsquare(c && c->tags[i], isoccupied(i), isurgent(i), dc.norm);
  490. }
  491. dc.x += dc.w;
  492. }
  493. dc.w = blw;
  494. drawtext(lt->symbol, dc.norm, False);
  495. x = dc.x + dc.w;
  496. dc.w = textw(stext);
  497. dc.x = bw - dc.w;
  498. if(dc.x < x) {
  499. dc.x = x;
  500. dc.w = bw - x;
  501. }
  502. drawtext(stext, dc.norm, False);
  503. if((dc.w = dc.x - x) > bh) {
  504. dc.x = x;
  505. if(c) {
  506. drawtext(c->name, dc.sel, False);
  507. drawsquare(False, c->isfloating, False, dc.sel);
  508. }
  509. else
  510. drawtext(NULL, dc.norm, False);
  511. }
  512. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
  513. XSync(dpy, False);
  514. }
  515. void
  516. drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  517. int x;
  518. XGCValues gcv;
  519. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  520. gcv.foreground = col[invert ? ColBG : ColFG];
  521. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  522. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  523. r.x = dc.x + 1;
  524. r.y = dc.y + 1;
  525. if(filled) {
  526. r.width = r.height = x + 1;
  527. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  528. }
  529. else if(empty) {
  530. r.width = r.height = x;
  531. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  532. }
  533. }
  534. void
  535. drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
  536. int x, y, w, h;
  537. unsigned int len, olen;
  538. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  539. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  540. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  541. if(!text)
  542. return;
  543. w = 0;
  544. olen = len = strlen(text);
  545. if(len >= sizeof buf)
  546. len = sizeof buf - 1;
  547. memcpy(buf, text, len);
  548. buf[len] = 0;
  549. h = dc.font.ascent + dc.font.descent;
  550. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  551. x = dc.x + (h / 2);
  552. /* shorten text if necessary */
  553. while(len && (w = textnw(buf, len)) > dc.w - h)
  554. buf[--len] = 0;
  555. if(len < olen) {
  556. if(len > 1)
  557. buf[len - 1] = '.';
  558. if(len > 2)
  559. buf[len - 2] = '.';
  560. if(len > 3)
  561. buf[len - 3] = '.';
  562. }
  563. if(w > dc.w)
  564. return; /* too long */
  565. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  566. if(dc.font.set)
  567. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  568. else
  569. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  570. }
  571. void *
  572. emallocz(unsigned int size) {
  573. void *res = calloc(1, size);
  574. if(!res)
  575. eprint("fatal: could not malloc() %u bytes\n", size);
  576. return res;
  577. }
  578. void
  579. enternotify(XEvent *e) {
  580. Client *c;
  581. XCrossingEvent *ev = &e->xcrossing;
  582. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  583. return;
  584. if((c = getclient(ev->window)))
  585. focus(c);
  586. else
  587. focus(NULL);
  588. }
  589. void
  590. eprint(const char *errstr, ...) {
  591. va_list ap;
  592. va_start(ap, errstr);
  593. vfprintf(stderr, errstr, ap);
  594. va_end(ap);
  595. exit(EXIT_FAILURE);
  596. }
  597. void
  598. expose(XEvent *e) {
  599. XExposeEvent *ev = &e->xexpose;
  600. if(ev->count == 0 && (ev->window == barwin))
  601. drawbar();
  602. }
  603. void
  604. floating(void) { /* default floating layout */
  605. Client *c;
  606. for(c = clients; c; c = c->next)
  607. if(isvisible(c))
  608. resize(c, c->x, c->y, c->w, c->h, True);
  609. }
  610. void
  611. focus(Client *c) {
  612. if(!c || (c && !isvisible(c)))
  613. for(c = stack; c && !isvisible(c); c = c->snext);
  614. if(sel && sel != c) {
  615. grabbuttons(sel, False);
  616. XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
  617. }
  618. if(c) {
  619. detachstack(c);
  620. attachstack(c);
  621. grabbuttons(c, True);
  622. }
  623. sel = c;
  624. if(c) {
  625. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  626. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  627. }
  628. else
  629. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  630. drawbar();
  631. }
  632. void
  633. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  634. XFocusChangeEvent *ev = &e->xfocus;
  635. if(sel && ev->window != sel->win)
  636. XSetInputFocus(dpy, sel->win, RevertToPointerRoot, CurrentTime);
  637. }
  638. void
  639. focusnext(const char *arg) {
  640. Client *c;
  641. if(!sel)
  642. return;
  643. for(c = sel->next; c && !isvisible(c); c = c->next);
  644. if(!c)
  645. for(c = clients; c && !isvisible(c); c = c->next);
  646. if(c) {
  647. focus(c);
  648. restack();
  649. }
  650. }
  651. void
  652. focusprev(const char *arg) {
  653. Client *c;
  654. if(!sel)
  655. return;
  656. for(c = sel->prev; c && !isvisible(c); c = c->prev);
  657. if(!c) {
  658. for(c = clients; c && c->next; c = c->next);
  659. for(; c && !isvisible(c); c = c->prev);
  660. }
  661. if(c) {
  662. focus(c);
  663. restack();
  664. }
  665. }
  666. Client *
  667. getclient(Window w) {
  668. Client *c;
  669. for(c = clients; c && c->win != w; c = c->next);
  670. return c;
  671. }
  672. unsigned long
  673. getcolor(const char *colstr) {
  674. Colormap cmap = DefaultColormap(dpy, screen);
  675. XColor color;
  676. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  677. eprint("error, cannot allocate color '%s'\n", colstr);
  678. return color.pixel;
  679. }
  680. long
  681. getstate(Window w) {
  682. int format, status;
  683. long result = -1;
  684. unsigned char *p = NULL;
  685. unsigned long n, extra;
  686. Atom real;
  687. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  688. &real, &format, &n, &extra, (unsigned char **)&p);
  689. if(status != Success)
  690. return -1;
  691. if(n != 0)
  692. result = *p;
  693. XFree(p);
  694. return result;
  695. }
  696. Bool
  697. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  698. char **list = NULL;
  699. int n;
  700. XTextProperty name;
  701. if(!text || size == 0)
  702. return False;
  703. text[0] = '\0';
  704. XGetTextProperty(dpy, w, &name, atom);
  705. if(!name.nitems)
  706. return False;
  707. if(name.encoding == XA_STRING)
  708. strncpy(text, (char *)name.value, size - 1);
  709. else {
  710. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  711. && n > 0 && *list) {
  712. strncpy(text, *list, size - 1);
  713. XFreeStringList(list);
  714. }
  715. }
  716. text[size - 1] = '\0';
  717. XFree(name.value);
  718. return True;
  719. }
  720. void
  721. grabbuttons(Client *c, Bool focused) {
  722. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  723. if(focused) {
  724. XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  725. GrabModeAsync, GrabModeSync, None, None);
  726. XGrabButton(dpy, Button1, MODKEY|LockMask, c->win, False, BUTTONMASK,
  727. GrabModeAsync, GrabModeSync, None, None);
  728. XGrabButton(dpy, Button1, MODKEY|numlockmask, c->win, False, BUTTONMASK,
  729. GrabModeAsync, GrabModeSync, None, None);
  730. XGrabButton(dpy, Button1, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK,
  731. GrabModeAsync, GrabModeSync, None, None);
  732. XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  733. GrabModeAsync, GrabModeSync, None, None);
  734. XGrabButton(dpy, Button2, MODKEY|LockMask, c->win, False, BUTTONMASK,
  735. GrabModeAsync, GrabModeSync, None, None);
  736. XGrabButton(dpy, Button2, MODKEY|numlockmask, c->win, False, BUTTONMASK,
  737. GrabModeAsync, GrabModeSync, None, None);
  738. XGrabButton(dpy, Button2, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK,
  739. GrabModeAsync, GrabModeSync, None, None);
  740. XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  741. GrabModeAsync, GrabModeSync, None, None);
  742. XGrabButton(dpy, Button3, MODKEY|LockMask, c->win, False, BUTTONMASK,
  743. GrabModeAsync, GrabModeSync, None, None);
  744. XGrabButton(dpy, Button3, MODKEY|numlockmask, c->win, False, BUTTONMASK,
  745. GrabModeAsync, GrabModeSync, None, None);
  746. XGrabButton(dpy, Button3, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK,
  747. GrabModeAsync, GrabModeSync, None, None);
  748. }
  749. else
  750. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
  751. GrabModeAsync, GrabModeSync, None, None);
  752. }
  753. void
  754. grabkeys(void) {
  755. unsigned int i, j;
  756. KeyCode code;
  757. XModifierKeymap *modmap;
  758. /* init modifier map */
  759. modmap = XGetModifierMapping(dpy);
  760. for(i = 0; i < 8; i++)
  761. for(j = 0; j < modmap->max_keypermod; j++) {
  762. if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
  763. numlockmask = (1 << i);
  764. }
  765. XFreeModifiermap(modmap);
  766. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  767. for(i = 0; i < LENGTH(keys); i++) {
  768. code = XKeysymToKeycode(dpy, keys[i].keysym);
  769. XGrabKey(dpy, code, keys[i].mod, root, True,
  770. GrabModeAsync, GrabModeAsync);
  771. XGrabKey(dpy, code, keys[i].mod|LockMask, root, True,
  772. GrabModeAsync, GrabModeAsync);
  773. XGrabKey(dpy, code, keys[i].mod|numlockmask, root, True,
  774. GrabModeAsync, GrabModeAsync);
  775. XGrabKey(dpy, code, keys[i].mod|numlockmask|LockMask, root, True,
  776. GrabModeAsync, GrabModeAsync);
  777. }
  778. }
  779. unsigned int
  780. idxoftag(const char *t) {
  781. unsigned int i;
  782. for(i = 0; (i < LENGTH(tags)) && strcmp(tags[i], t); i++);
  783. return (i < LENGTH(tags)) ? i : 0;
  784. }
  785. void
  786. initfont(const char *fontstr) {
  787. char *def, **missing;
  788. int i, n;
  789. missing = NULL;
  790. if(dc.font.set)
  791. XFreeFontSet(dpy, dc.font.set);
  792. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  793. if(missing) {
  794. while(n--)
  795. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  796. XFreeStringList(missing);
  797. }
  798. if(dc.font.set) {
  799. XFontSetExtents *font_extents;
  800. XFontStruct **xfonts;
  801. char **font_names;
  802. dc.font.ascent = dc.font.descent = 0;
  803. font_extents = XExtentsOfFontSet(dc.font.set);
  804. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  805. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  806. if(dc.font.ascent < (*xfonts)->ascent)
  807. dc.font.ascent = (*xfonts)->ascent;
  808. if(dc.font.descent < (*xfonts)->descent)
  809. dc.font.descent = (*xfonts)->descent;
  810. xfonts++;
  811. }
  812. }
  813. else {
  814. if(dc.font.xfont)
  815. XFreeFont(dpy, dc.font.xfont);
  816. dc.font.xfont = NULL;
  817. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  818. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  819. eprint("error, cannot load font: '%s'\n", fontstr);
  820. dc.font.ascent = dc.font.xfont->ascent;
  821. dc.font.descent = dc.font.xfont->descent;
  822. }
  823. dc.font.height = dc.font.ascent + dc.font.descent;
  824. }
  825. Bool
  826. isoccupied(unsigned int t) {
  827. Client *c;
  828. for(c = clients; c; c = c->next)
  829. if(c->tags[t])
  830. return True;
  831. return False;
  832. }
  833. Bool
  834. isprotodel(Client *c) {
  835. int i, n;
  836. Atom *protocols;
  837. Bool ret = False;
  838. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  839. for(i = 0; !ret && i < n; i++)
  840. if(protocols[i] == wmatom[WMDelete])
  841. ret = True;
  842. XFree(protocols);
  843. }
  844. return ret;
  845. }
  846. Bool
  847. isurgent(unsigned int t) {
  848. Client *c;
  849. for(c = clients; c; c = c->next)
  850. if(c->isurgent && c->tags[t])
  851. return True;
  852. return False;
  853. }
  854. Bool
  855. isvisible(Client *c) {
  856. unsigned int i;
  857. for(i = 0; i < LENGTH(tags); i++)
  858. if(c->tags[i] && seltags[i])
  859. return True;
  860. return False;
  861. }
  862. void
  863. keypress(XEvent *e) {
  864. unsigned int i;
  865. KeySym keysym;
  866. XKeyEvent *ev;
  867. ev = &e->xkey;
  868. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  869. for(i = 0; i < LENGTH(keys); i++)
  870. if(keysym == keys[i].keysym
  871. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state))
  872. {
  873. if(keys[i].func)
  874. keys[i].func(keys[i].arg);
  875. }
  876. }
  877. void
  878. killclient(const char *arg) {
  879. XEvent ev;
  880. if(!sel)
  881. return;
  882. if(isprotodel(sel)) {
  883. ev.type = ClientMessage;
  884. ev.xclient.window = sel->win;
  885. ev.xclient.message_type = wmatom[WMProtocols];
  886. ev.xclient.format = 32;
  887. ev.xclient.data.l[0] = wmatom[WMDelete];
  888. ev.xclient.data.l[1] = CurrentTime;
  889. XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
  890. }
  891. else
  892. XKillClient(dpy, sel->win);
  893. }
  894. void
  895. manage(Window w, XWindowAttributes *wa) {
  896. Client *c, *t = NULL;
  897. Status rettrans;
  898. Window trans;
  899. XWindowChanges wc;
  900. c = emallocz(sizeof(Client));
  901. c->tags = emallocz(TAGSZ);
  902. c->win = w;
  903. /* geometry */
  904. c->x = wa->x;
  905. c->y = wa->y;
  906. c->w = wa->width;
  907. c->h = wa->height;
  908. c->oldbw = wa->border_width;
  909. if(c->w == sw && c->h == sh) {
  910. c->x = sx;
  911. c->y = sy;
  912. c->bw = wa->border_width;
  913. }
  914. else {
  915. if(c->x + c->w + 2 * c->bw > wx + ww)
  916. c->x = wx + ww - c->w - 2 * c->bw;
  917. if(c->y + c->h + 2 * c->bw > wy + wh)
  918. c->y = wy + wh - c->h - 2 * c->bw;
  919. if(c->x < wx)
  920. c->x = wx;
  921. if(c->y < wy)
  922. c->y = wy;
  923. c->bw = BORDERPX;
  924. }
  925. wc.border_width = c->bw;
  926. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  927. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  928. configure(c); /* propagates border_width, if size doesn't change */
  929. updatesizehints(c);
  930. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  931. grabbuttons(c, False);
  932. updatetitle(c);
  933. if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
  934. for(t = clients; t && t->win != trans; t = t->next);
  935. if(t)
  936. memcpy(c->tags, t->tags, TAGSZ);
  937. else
  938. applyrules(c);
  939. if(!c->isfloating)
  940. c->isfloating = (rettrans == Success) || c->isfixed;
  941. attach(c);
  942. attachstack(c);
  943. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
  944. ban(c);
  945. XMapWindow(dpy, c->win);
  946. setclientstate(c, NormalState);
  947. arrange();
  948. }
  949. void
  950. mappingnotify(XEvent *e) {
  951. XMappingEvent *ev = &e->xmapping;
  952. XRefreshKeyboardMapping(ev);
  953. if(ev->request == MappingKeyboard)
  954. grabkeys();
  955. }
  956. void
  957. maprequest(XEvent *e) {
  958. static XWindowAttributes wa;
  959. XMapRequestEvent *ev = &e->xmaprequest;
  960. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  961. return;
  962. if(wa.override_redirect)
  963. return;
  964. if(!getclient(ev->window))
  965. manage(ev->window, &wa);
  966. }
  967. void
  968. monocle(void) {
  969. Client *c;
  970. for(c = clients; c; c = c->next)
  971. if(isvisible(c))
  972. resize(c, mox, moy, mow - 2 * c->bw, moh - 2 * c->bw, RESIZEHINTS);
  973. }
  974. void
  975. movemouse(Client *c) {
  976. int x1, y1, ocx, ocy, di, nx, ny;
  977. unsigned int dui;
  978. Window dummy;
  979. XEvent ev;
  980. ocx = nx = c->x;
  981. ocy = ny = c->y;
  982. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  983. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  984. return;
  985. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  986. for(;;) {
  987. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  988. switch (ev.type) {
  989. case ButtonRelease:
  990. XUngrabPointer(dpy, CurrentTime);
  991. return;
  992. case ConfigureRequest:
  993. case Expose:
  994. case MapRequest:
  995. handler[ev.type](&ev);
  996. break;
  997. case MotionNotify:
  998. XSync(dpy, False);
  999. nx = ocx + (ev.xmotion.x - x1);
  1000. ny = ocy + (ev.xmotion.y - y1);
  1001. if(abs(wx - nx) < SNAP)
  1002. nx = wx;
  1003. else if(abs((wx + ww) - (nx + c->w + 2 * c->bw)) < SNAP)
  1004. nx = wx + ww - c->w - 2 * c->bw;
  1005. if(abs(wy - ny) < SNAP)
  1006. ny = wy;
  1007. else if(abs((wy + wh) - (ny + c->h + 2 * c->bw)) < SNAP)
  1008. ny = wy + wh - c->h - 2 * c->bw;
  1009. if(!c->isfloating && !lt->isfloating && (abs(nx - c->x) > SNAP || abs(ny - c->y) > SNAP))
  1010. togglefloating(NULL);
  1011. if((lt->isfloating) || c->isfloating)
  1012. resize(c, nx, ny, c->w, c->h, False);
  1013. break;
  1014. }
  1015. }
  1016. }
  1017. Client *
  1018. nexttiled(Client *c) {
  1019. for(; c && (c->isfloating || !isvisible(c)); c = c->next);
  1020. return c;
  1021. }
  1022. void
  1023. propertynotify(XEvent *e) {
  1024. Client *c;
  1025. Window trans;
  1026. XPropertyEvent *ev = &e->xproperty;
  1027. if(ev->state == PropertyDelete)
  1028. return; /* ignore */
  1029. if((c = getclient(ev->window))) {
  1030. switch (ev->atom) {
  1031. default: break;
  1032. case XA_WM_TRANSIENT_FOR:
  1033. XGetTransientForHint(dpy, c->win, &trans);
  1034. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  1035. arrange();
  1036. break;
  1037. case XA_WM_NORMAL_HINTS:
  1038. updatesizehints(c);
  1039. break;
  1040. case XA_WM_HINTS:
  1041. updatewmhints(c);
  1042. drawbar();
  1043. break;
  1044. }
  1045. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1046. updatetitle(c);
  1047. if(c == sel)
  1048. drawbar();
  1049. }
  1050. }
  1051. }
  1052. void
  1053. quit(const char *arg) {
  1054. readin = running = False;
  1055. }
  1056. void
  1057. reapply(const char *arg) {
  1058. static Bool zerotags[LENGTH(tags)] = { 0 };
  1059. Client *c;
  1060. for(c = clients; c; c = c->next) {
  1061. memcpy(c->tags, zerotags, sizeof zerotags);
  1062. applyrules(c);
  1063. }
  1064. arrange();
  1065. }
  1066. void
  1067. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  1068. XWindowChanges wc;
  1069. if(sizehints) {
  1070. /* set minimum possible */
  1071. if (w < 1)
  1072. w = 1;
  1073. if (h < 1)
  1074. h = 1;
  1075. /* temporarily remove base dimensions */
  1076. w -= c->basew;
  1077. h -= c->baseh;
  1078. /* adjust for aspect limits */
  1079. if (c->minay > 0 && c->maxay > 0 && c->minax > 0 && c->maxax > 0) {
  1080. if (w * c->maxay > h * c->maxax)
  1081. w = h * c->maxax / c->maxay;
  1082. else if (w * c->minay < h * c->minax)
  1083. h = w * c->minay / c->minax;
  1084. }
  1085. /* adjust for increment value */
  1086. if(c->incw)
  1087. w -= w % c->incw;
  1088. if(c->inch)
  1089. h -= h % c->inch;
  1090. /* restore base dimensions */
  1091. w += c->basew;
  1092. h += c->baseh;
  1093. if(c->minw > 0 && w < c->minw)
  1094. w = c->minw;
  1095. if(c->minh > 0 && h < c->minh)
  1096. h = c->minh;
  1097. if(c->maxw > 0 && w > c->maxw)
  1098. w = c->maxw;
  1099. if(c->maxh > 0 && h > c->maxh)
  1100. h = c->maxh;
  1101. }
  1102. if(w <= 0 || h <= 0)
  1103. return;
  1104. if(x > sx + sw)
  1105. x = sw - w - 2 * c->bw;
  1106. if(y > sy + sh)
  1107. y = sh - h - 2 * c->bw;
  1108. if(x + w + 2 * c->bw < sx)
  1109. x = sx;
  1110. if(y + h + 2 * c->bw < sy)
  1111. y = sy;
  1112. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  1113. c->x = wc.x = x;
  1114. c->y = wc.y = y;
  1115. c->w = wc.width = w;
  1116. c->h = wc.height = h;
  1117. wc.border_width = c->bw;
  1118. XConfigureWindow(dpy, c->win,
  1119. CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1120. configure(c);
  1121. XSync(dpy, False);
  1122. }
  1123. }
  1124. void
  1125. resizemouse(Client *c) {
  1126. int ocx, ocy;
  1127. int nw, nh;
  1128. XEvent ev;
  1129. ocx = c->x;
  1130. ocy = c->y;
  1131. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1132. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1133. return;
  1134. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1135. for(;;) {
  1136. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask , &ev);
  1137. switch(ev.type) {
  1138. case ButtonRelease:
  1139. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
  1140. c->w + c->bw - 1, c->h + c->bw - 1);
  1141. XUngrabPointer(dpy, CurrentTime);
  1142. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1143. return;
  1144. case ConfigureRequest:
  1145. case Expose:
  1146. case MapRequest:
  1147. handler[ev.type](&ev);
  1148. break;
  1149. case MotionNotify:
  1150. XSync(dpy, False);
  1151. if((nw = ev.xmotion.x - ocx - 2 * c->bw + 1) <= 0)
  1152. nw = 1;
  1153. if((nh = ev.xmotion.y - ocy - 2 * c->bw + 1) <= 0)
  1154. nh = 1;
  1155. if(!c->isfloating && !lt->isfloating && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP))
  1156. togglefloating(NULL);
  1157. if((lt->isfloating) || c->isfloating)
  1158. resize(c, c->x, c->y, nw, nh, True);
  1159. break;
  1160. }
  1161. }
  1162. }
  1163. void
  1164. restack(void) {
  1165. Client *c;
  1166. XEvent ev;
  1167. XWindowChanges wc;
  1168. drawbar();
  1169. if(!sel)
  1170. return;
  1171. if(sel->isfloating || lt->isfloating)
  1172. XRaiseWindow(dpy, sel->win);
  1173. if(!lt->isfloating) {
  1174. wc.stack_mode = Below;
  1175. wc.sibling = barwin;
  1176. if(!sel->isfloating) {
  1177. XConfigureWindow(dpy, sel->win, CWSibling|CWStackMode, &wc);
  1178. wc.sibling = sel->win;
  1179. }
  1180. for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
  1181. if(c == sel)
  1182. continue;
  1183. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1184. wc.sibling = c->win;
  1185. }
  1186. }
  1187. XSync(dpy, False);
  1188. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1189. }
  1190. void
  1191. run(void) {
  1192. char *p;
  1193. char sbuf[sizeof stext];
  1194. fd_set rd;
  1195. int r, xfd;
  1196. unsigned int len, offset;
  1197. XEvent ev;
  1198. /* main event loop, also reads status text from stdin */
  1199. XSync(dpy, False);
  1200. xfd = ConnectionNumber(dpy);
  1201. readin = True;
  1202. offset = 0;
  1203. len = sizeof stext - 1;
  1204. sbuf[len] = stext[len] = '\0'; /* 0-terminator is never touched */
  1205. while(running) {
  1206. FD_ZERO(&rd);
  1207. if(readin)
  1208. FD_SET(STDIN_FILENO, &rd);
  1209. FD_SET(xfd, &rd);
  1210. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1211. if(errno == EINTR)
  1212. continue;
  1213. eprint("select failed\n");
  1214. }
  1215. if(FD_ISSET(STDIN_FILENO, &rd)) {
  1216. switch((r = read(STDIN_FILENO, sbuf + offset, len - offset))) {
  1217. case -1:
  1218. strncpy(stext, strerror(errno), len);
  1219. readin = False;
  1220. break;
  1221. case 0:
  1222. strncpy(stext, "EOF", 4);
  1223. readin = False;
  1224. break;
  1225. default:
  1226. for(p = sbuf + offset; r > 0; p++, r--, offset++)
  1227. if(*p == '\n' || *p == '\0') {
  1228. *p = '\0';
  1229. strncpy(stext, sbuf, len);
  1230. p += r - 1; /* p is sbuf + offset + r - 1 */
  1231. for(r = 0; *(p - r) && *(p - r) != '\n'; r++);
  1232. offset = r;
  1233. if(r)
  1234. memmove(sbuf, p - r + 1, r);
  1235. break;
  1236. }
  1237. break;
  1238. }
  1239. drawbar();
  1240. }
  1241. while(XPending(dpy)) {
  1242. XNextEvent(dpy, &ev);
  1243. if(handler[ev.type])
  1244. (handler[ev.type])(&ev); /* call handler */
  1245. }
  1246. }
  1247. }
  1248. void
  1249. scan(void) {
  1250. unsigned int i, num;
  1251. Window *wins, d1, d2;
  1252. XWindowAttributes wa;
  1253. wins = NULL;
  1254. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1255. for(i = 0; i < num; i++) {
  1256. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1257. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1258. continue;
  1259. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1260. manage(wins[i], &wa);
  1261. }
  1262. for(i = 0; i < num; i++) { /* now the transients */
  1263. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1264. continue;
  1265. if(XGetTransientForHint(dpy, wins[i], &d1)
  1266. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1267. manage(wins[i], &wa);
  1268. }
  1269. }
  1270. if(wins)
  1271. XFree(wins);
  1272. }
  1273. void
  1274. setclientstate(Client *c, long state) {
  1275. long data[] = {state, None};
  1276. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1277. PropModeReplace, (unsigned char *)data, 2);
  1278. }
  1279. void
  1280. setgeom(const char *arg) {
  1281. unsigned int i;
  1282. for(i = 0; arg && i < LENGTH(geoms); i++)
  1283. if(!strcmp(geoms[i].symbol, arg))
  1284. break;
  1285. if(i == LENGTH(geoms))
  1286. return;
  1287. geom = &geoms[i];
  1288. geom->apply();
  1289. updatebarpos();
  1290. arrange();
  1291. }
  1292. void
  1293. setlayout(const char *arg) {
  1294. static Layout *revert = 0;
  1295. unsigned int i;
  1296. if(!arg)
  1297. return;
  1298. for(i = 0; i < LENGTH(layouts); i++)
  1299. if(!strcmp(arg, layouts[i].symbol))
  1300. break;
  1301. if(i == LENGTH(layouts))
  1302. return;
  1303. if(revert && &layouts[i] == lt)
  1304. lt = revert;
  1305. else {
  1306. revert = lt;
  1307. lt = &layouts[i];
  1308. }
  1309. if(sel)
  1310. arrange();
  1311. else
  1312. drawbar();
  1313. }
  1314. void
  1315. setup(void) {
  1316. unsigned int i;
  1317. XSetWindowAttributes wa;
  1318. /* init screen */
  1319. screen = DefaultScreen(dpy);
  1320. root = RootWindow(dpy, screen);
  1321. initfont(FONT);
  1322. /* apply default geometry */
  1323. sx = 0;
  1324. sy = 0;
  1325. sw = DisplayWidth(dpy, screen);
  1326. sh = DisplayHeight(dpy, screen);
  1327. bh = dc.font.height + 2;
  1328. geom = &geoms[0];
  1329. geom->apply();
  1330. /* init atoms */
  1331. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1332. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1333. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1334. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1335. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1336. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1337. /* init cursors */
  1338. wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1339. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1340. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1341. /* init appearance */
  1342. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  1343. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  1344. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  1345. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  1346. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  1347. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  1348. initfont(FONT);
  1349. dc.h = bh;
  1350. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1351. dc.gc = XCreateGC(dpy, root, 0, 0);
  1352. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1353. if(!dc.font.set)
  1354. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1355. /* init tags */
  1356. seltags = emallocz(TAGSZ);
  1357. prevtags = emallocz(TAGSZ);
  1358. seltags[0] = prevtags[0] = True;
  1359. /* init layouts */
  1360. lt = &layouts[0];
  1361. /* init bar */
  1362. for(blw = i = 0; i < LENGTH(layouts); i++) {
  1363. i = textw(layouts[i].symbol);
  1364. if(i > blw)
  1365. blw = i;
  1366. }
  1367. for(bgw = i = 0; i < LENGTH(geoms); i++) {
  1368. i = textw(geoms[i].symbol);
  1369. if(i > bgw)
  1370. bgw = i;
  1371. }
  1372. wa.override_redirect = 1;
  1373. wa.background_pixmap = ParentRelative;
  1374. wa.event_mask = ButtonPressMask|ExposureMask;
  1375. barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
  1376. CopyFromParent, DefaultVisual(dpy, screen),
  1377. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1378. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  1379. XMapRaised(dpy, barwin);
  1380. strcpy(stext, "dwm-"VERSION);
  1381. drawbar();
  1382. /* EWMH support per view */
  1383. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1384. PropModeReplace, (unsigned char *) netatom, NetLast);
  1385. /* select for events */
  1386. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
  1387. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask;
  1388. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1389. XSelectInput(dpy, root, wa.event_mask);
  1390. /* grab keys */
  1391. grabkeys();
  1392. }
  1393. void
  1394. spawn(const char *arg) {
  1395. static char *shell = NULL;
  1396. if(!shell && !(shell = getenv("SHELL")))
  1397. shell = "/bin/sh";
  1398. if(!arg)
  1399. return;
  1400. /* The double-fork construct avoids zombie processes and keeps the code
  1401. * clean from stupid signal handlers. */
  1402. if(fork() == 0) {
  1403. if(fork() == 0) {
  1404. if(dpy)
  1405. close(ConnectionNumber(dpy));
  1406. setsid();
  1407. execl(shell, shell, "-c", arg, (char *)NULL);
  1408. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
  1409. perror(" failed");
  1410. }
  1411. exit(0);
  1412. }
  1413. wait(0);
  1414. }
  1415. void
  1416. tag(const char *arg) {
  1417. unsigned int i;
  1418. if(!sel)
  1419. return;
  1420. for(i = 0; i < LENGTH(tags); i++)
  1421. sel->tags[i] = (NULL == arg);
  1422. sel->tags[idxoftag(arg)] = True;
  1423. arrange();
  1424. }
  1425. unsigned int
  1426. textnw(const char *text, unsigned int len) {
  1427. XRectangle r;
  1428. if(dc.font.set) {
  1429. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1430. return r.width;
  1431. }
  1432. return XTextWidth(dc.font.xfont, text, len);
  1433. }
  1434. unsigned int
  1435. textw(const char *text) {
  1436. return textnw(text, strlen(text)) + dc.font.height;
  1437. }
  1438. void
  1439. tileh(void) {
  1440. int x, w;
  1441. unsigned int i, n = counttiled();
  1442. Client *c;
  1443. if(n == 0)
  1444. return;
  1445. c = tilemaster(n);
  1446. if(--n == 0)
  1447. return;
  1448. x = tx;
  1449. w = tw / n;
  1450. if(w < bh)
  1451. w = tw;
  1452. for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1453. if(i + 1 == n) /* remainder */
  1454. tileresize(c, x, ty, (tx + tw) - x - 2 * c->bw, th - 2 * c->bw);
  1455. else
  1456. tileresize(c, x, ty, w - 2 * c->bw, th - 2 * c->bw);
  1457. if(w != tw)
  1458. x = c->x + c->w + 2 * c->bw;
  1459. }
  1460. }
  1461. Client *
  1462. tilemaster(unsigned int n) {
  1463. Client *c = nexttiled(clients);
  1464. if(n == 1)
  1465. tileresize(c, mox, moy, mow - 2 * c->bw, moh - 2 * c->bw);
  1466. else
  1467. tileresize(c, mx, my, mw - 2 * c->bw, mh - 2 * c->bw);
  1468. return c;
  1469. }
  1470. void
  1471. tileresize(Client *c, int x, int y, int w, int h) {
  1472. resize(c, x, y, w, h, RESIZEHINTS);
  1473. if((RESIZEHINTS) && ((c->h < bh) || (c->h > h) || (c->w < bh) || (c->w > w)))
  1474. /* client doesn't accept size constraints */
  1475. resize(c, x, y, w, h, False);
  1476. }
  1477. void
  1478. tilev(void) {
  1479. int y, h;
  1480. unsigned int i, n = counttiled();
  1481. Client *c;
  1482. if(n == 0)
  1483. return;
  1484. c = tilemaster(n);
  1485. if(--n == 0)
  1486. return;
  1487. y = ty;
  1488. h = th / n;
  1489. if(h < bh)
  1490. h = th;
  1491. for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1492. if(i + 1 == n) /* remainder */
  1493. tileresize(c, tx, y, tw - 2 * c->bw, (ty + th) - y - 2 * c->bw);
  1494. else
  1495. tileresize(c, tx, y, tw - 2 * c->bw, h - 2 * c->bw);
  1496. if(h != th)
  1497. y = c->y + c->h + 2 * c->bw;
  1498. }
  1499. }
  1500. void
  1501. togglefloating(const char *arg) {
  1502. if(!sel)
  1503. return;
  1504. sel->isfloating = !sel->isfloating;
  1505. if(sel->isfloating)
  1506. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1507. arrange();
  1508. }
  1509. void
  1510. toggletag(const char *arg) {
  1511. unsigned int i, j;
  1512. if(!sel)
  1513. return;
  1514. i = idxoftag(arg);
  1515. sel->tags[i] = !sel->tags[i];
  1516. for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
  1517. if(j == LENGTH(tags))
  1518. sel->tags[i] = True; /* at least one tag must be enabled */
  1519. arrange();
  1520. }
  1521. void
  1522. toggleview(const char *arg) {
  1523. unsigned int i, j;
  1524. i = idxoftag(arg);
  1525. seltags[i] = !seltags[i];
  1526. for(j = 0; j < LENGTH(tags) && !seltags[j]; j++);
  1527. if(j == LENGTH(tags))
  1528. seltags[i] = True; /* at least one tag must be viewed */
  1529. arrange();
  1530. }
  1531. void
  1532. unban(Client *c) {
  1533. if(!c->isbanned)
  1534. return;
  1535. XMoveWindow(dpy, c->win, c->x, c->y);
  1536. c->isbanned = False;
  1537. }
  1538. void
  1539. unmanage(Client *c) {
  1540. XWindowChanges wc;
  1541. wc.border_width = c->oldbw;
  1542. /* The server grab construct avoids race conditions. */
  1543. XGrabServer(dpy);
  1544. XSetErrorHandler(xerrordummy);
  1545. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1546. detach(c);
  1547. detachstack(c);
  1548. if(sel == c)
  1549. focus(NULL);
  1550. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1551. setclientstate(c, WithdrawnState);
  1552. free(c->tags);
  1553. free(c);
  1554. XSync(dpy, False);
  1555. XSetErrorHandler(xerror);
  1556. XUngrabServer(dpy);
  1557. arrange();
  1558. }
  1559. void
  1560. unmapnotify(XEvent *e) {
  1561. Client *c;
  1562. XUnmapEvent *ev = &e->xunmap;
  1563. if((c = getclient(ev->window)))
  1564. unmanage(c);
  1565. }
  1566. void
  1567. updatebarpos(void) {
  1568. if(dc.drawable != 0)
  1569. XFreePixmap(dpy, dc.drawable);
  1570. dc.drawable = XCreatePixmap(dpy, root, bw, bh, DefaultDepth(dpy, screen));
  1571. XMoveResizeWindow(dpy, barwin, bx, by, bw, bh);
  1572. }
  1573. void
  1574. updatesizehints(Client *c) {
  1575. long msize;
  1576. XSizeHints size;
  1577. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  1578. size.flags = PSize;
  1579. c->flags = size.flags;
  1580. if(c->flags & PBaseSize) {
  1581. c->basew = size.base_width;
  1582. c->baseh = size.base_height;
  1583. }
  1584. else if(c->flags & PMinSize) {
  1585. c->basew = size.min_width;
  1586. c->baseh = size.min_height;
  1587. }
  1588. else
  1589. c->basew = c->baseh = 0;
  1590. if(c->flags & PResizeInc) {
  1591. c->incw = size.width_inc;
  1592. c->inch = size.height_inc;
  1593. }
  1594. else
  1595. c->incw = c->inch = 0;
  1596. if(c->flags & PMaxSize) {
  1597. c->maxw = size.max_width;
  1598. c->maxh = size.max_height;
  1599. }
  1600. else
  1601. c->maxw = c->maxh = 0;
  1602. if(c->flags & PMinSize) {
  1603. c->minw = size.min_width;
  1604. c->minh = size.min_height;
  1605. }
  1606. else if(c->flags & PBaseSize) {
  1607. c->minw = size.base_width;
  1608. c->minh = size.base_height;
  1609. }
  1610. else
  1611. c->minw = c->minh = 0;
  1612. if(c->flags & PAspect) {
  1613. c->minax = size.min_aspect.x;
  1614. c->maxax = size.max_aspect.x;
  1615. c->minay = size.min_aspect.y;
  1616. c->maxay = size.max_aspect.y;
  1617. }
  1618. else
  1619. c->minax = c->maxax = c->minay = c->maxay = 0;
  1620. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1621. && c->maxw == c->minw && c->maxh == c->minh);
  1622. }
  1623. void
  1624. updatetitle(Client *c) {
  1625. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1626. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1627. }
  1628. void
  1629. updatewmhints(Client *c) {
  1630. XWMHints *wmh;
  1631. if((wmh = XGetWMHints(dpy, c->win))) {
  1632. if(c == sel)
  1633. sel->isurgent = False;
  1634. else
  1635. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1636. XFree(wmh);
  1637. }
  1638. }
  1639. void
  1640. view(const char *arg) {
  1641. unsigned int i;
  1642. for(i = 0; i < LENGTH(tags); i++)
  1643. tmp[i] = (NULL == arg);
  1644. tmp[idxoftag(arg)] = True;
  1645. if(memcmp(seltags, tmp, TAGSZ) != 0) {
  1646. memcpy(prevtags, seltags, TAGSZ);
  1647. memcpy(seltags, tmp, TAGSZ);
  1648. arrange();
  1649. }
  1650. }
  1651. void
  1652. viewprevtag(const char *arg) {
  1653. memcpy(tmp, seltags, TAGSZ);
  1654. memcpy(seltags, prevtags, TAGSZ);
  1655. memcpy(prevtags, tmp, TAGSZ);
  1656. arrange();
  1657. }
  1658. /* There's no way to check accesses to destroyed windows, thus those cases are
  1659. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1660. * default error handler, which may call exit. */
  1661. int
  1662. xerror(Display *dpy, XErrorEvent *ee) {
  1663. if(ee->error_code == BadWindow
  1664. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1665. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1666. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1667. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1668. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1669. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1670. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1671. return 0;
  1672. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1673. ee->request_code, ee->error_code);
  1674. return xerrorxlib(dpy, ee); /* may call exit */
  1675. }
  1676. int
  1677. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1678. return 0;
  1679. }
  1680. /* Startup Error handler to check if another window manager
  1681. * is already running. */
  1682. int
  1683. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1684. otherwm = True;
  1685. return -1;
  1686. }
  1687. void
  1688. zoom(const char *arg) {
  1689. Client *c = sel;
  1690. if(!sel || lt->isfloating || sel->isfloating)
  1691. return;
  1692. if(c == nexttiled(clients))
  1693. if(!(c = nexttiled(c->next)))
  1694. return;
  1695. detach(c);
  1696. attach(c);
  1697. focus(c);
  1698. arrange();
  1699. }
  1700. int
  1701. main(int argc, char *argv[]) {
  1702. if(argc == 2 && !strcmp("-v", argv[1]))
  1703. eprint("dwm-"VERSION", © 2006-2008 dwm engineers, see LICENSE for details\n");
  1704. else if(argc != 1)
  1705. eprint("usage: dwm [-v]\n");
  1706. setlocale(LC_CTYPE, "");
  1707. if(!(dpy = XOpenDisplay(0)))
  1708. eprint("dwm: cannot open display\n");
  1709. checkotherwm();
  1710. setup();
  1711. scan();
  1712. run();
  1713. cleanup();
  1714. XCloseDisplay(dpy);
  1715. return 0;
  1716. }