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.

2068 lines
49 KiB

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