Configuration file for DWM on MacBook Air
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.

1907 lines
43 KiB

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