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.

1900 lines
44 KiB

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