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.

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