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.

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