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.

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