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.

2081 lines
49 KiB

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