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.

1926 lines
44 KiB

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