Configuration of dwm for Mac Computers
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.

2079 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 Monitor Monitor;
  60. typedef struct Client Client;
  61. struct Client {
  62. char name[256];
  63. int x, y, w, h;
  64. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  65. int minax, maxax, minay, maxay;
  66. long flags;
  67. unsigned int border, oldborder;
  68. Bool isbanned, isfixed, isfloating, isurgent;
  69. Bool *tags;
  70. Client *next;
  71. Client *prev;
  72. Client *snext;
  73. Window win;
  74. Monitor *monitor;
  75. };
  76. typedef struct {
  77. int x, y, w, h;
  78. unsigned long norm[ColLast];
  79. unsigned long sel[ColLast];
  80. Drawable drawable;
  81. GC gc;
  82. struct {
  83. int ascent;
  84. int descent;
  85. int height;
  86. XFontSet set;
  87. XFontStruct *xfont;
  88. } font;
  89. } DC; /* draw context */
  90. typedef struct {
  91. unsigned long mod;
  92. KeySym keysym;
  93. void (*func)(const char *arg);
  94. const char *arg;
  95. } Key;
  96. typedef struct {
  97. const char *symbol;
  98. void (*arrange)(Monitor *);
  99. } Layout;
  100. typedef struct {
  101. const char *prop;
  102. const char *tags;
  103. Bool isfloating;
  104. int monitor;
  105. } Rule;
  106. typedef struct {
  107. regex_t *propregex;
  108. regex_t *tagregex;
  109. } Regs;
  110. struct Monitor {
  111. 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(Monitor *m);
  136. void drawsquare(Monitor *m, Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
  137. void drawtext(Monitor *m, 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. Monitor *getmonitor(Window barwin);
  150. long getstate(Window w);
  151. Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  152. void grabbuttons(Client *c, Bool focused);
  153. void grabkeys(void);
  154. unsigned int idxoftag(const char *tag);
  155. void initfont(const char *fontstr);
  156. Bool isoccupied(Monitor *monitor, unsigned int t);
  157. Bool isprotodel(Client *c);
  158. Bool isurgent(Monitor *monitor, unsigned int t);
  159. Bool isvisible(Client *c, Monitor *m);
  160. void keypress(XEvent *e);
  161. void killclient(const char *arg);
  162. void manage(Window w, XWindowAttributes *wa);
  163. void mappingnotify(XEvent *e);
  164. void maprequest(XEvent *e);
  165. Monitor *monitorat(void);
  166. void movemouse(Client *c);
  167. Client *nexttiled(Client *c, Monitor *monitor);
  168. void propertynotify(XEvent *e);
  169. void quit(const char *arg);
  170. void reapply(const char *arg);
  171. void resize(Client *c, int x, int y, int w, int h, Bool sizehints);
  172. void resizemouse(Client *c);
  173. void restack(Monitor *m);
  174. void run(void);
  175. void scan(void);
  176. void setclientstate(Client *c, long state);
  177. void setlayout(const char *arg);
  178. void setmwfact(const char *arg);
  179. void setup(void);
  180. void spawn(const char *arg);
  181. void tag(const char *arg);
  182. unsigned int textnw(const char *text, unsigned int len);
  183. unsigned int textw(const char *text);
  184. void tile(Monitor *m);
  185. void togglebar(const char *arg);
  186. void togglefloating(const char *arg);
  187. void toggletag(const char *arg);
  188. void toggleview(const char *arg);
  189. void unban(Client *c);
  190. void unmanage(Client *c);
  191. void unmapnotify(XEvent *e);
  192. void updatebarpos(Monitor *m);
  193. void updatesizehints(Client *c);
  194. void updatetitle(Client *c);
  195. void updatewmhints(Client *c);
  196. void view(const char *arg);
  197. void viewprevtag(const char *arg); /* views previous selected tags */
  198. int xerror(Display *dpy, XErrorEvent *ee);
  199. int xerrordummy(Display *dsply, XErrorEvent *ee);
  200. int xerrorstart(Display *dsply, XErrorEvent *ee);
  201. void zoom(const char *arg);
  202. void movetomonitor(const char *arg);
  203. void selectmonitor(const char *arg);
  204. /* variables */
  205. char stext[256];
  206. int mcount = 1;
  207. Monitor *selmonitor;
  208. int screen;
  209. int (*xerrorxlib)(Display *, XErrorEvent *);
  210. unsigned int bh, bpos;
  211. unsigned int blw = 0;
  212. unsigned int numlockmask = 0;
  213. void (*handler[LASTEvent]) (XEvent *) = {
  214. [ButtonPress] = buttonpress,
  215. [ConfigureRequest] = configurerequest,
  216. [ConfigureNotify] = configurenotify,
  217. [DestroyNotify] = destroynotify,
  218. [EnterNotify] = enternotify,
  219. [Expose] = expose,
  220. [FocusIn] = focusin,
  221. [KeyPress] = keypress,
  222. [MappingNotify] = mappingnotify,
  223. [MapRequest] = maprequest,
  224. [PropertyNotify] = propertynotify,
  225. [UnmapNotify] = unmapnotify
  226. };
  227. Atom wmatom[WMLast], netatom[NetLast];
  228. Bool isxinerama = False;
  229. Bool domwfact = True;
  230. Bool dozoom = True;
  231. Bool otherwm, readin;
  232. Bool running = True;
  233. Client *clients = NULL;
  234. Client *sel = NULL;
  235. Client *stack = NULL;
  236. Cursor cursor[CurLast];
  237. Display *dpy;
  238. DC dc = {0};
  239. Regs *regs = NULL;
  240. Monitor *monitors;
  241. Window root;
  242. /* configuration, allows nested code to access above variables */
  243. #include "config.h"
  244. //Bool prevtags[LENGTH(tags)];
  245. /* function implementations */
  246. void
  247. applyrules(Client *c) {
  248. static char buf[512];
  249. unsigned int i, j;
  250. regmatch_t tmp;
  251. Bool matched_tag = False;
  252. Bool matched_monitor = False;
  253. XClassHint ch = { 0 };
  254. /* rule matching */
  255. XGetClassHint(dpy, c->win, &ch);
  256. snprintf(buf, sizeof buf, "%s:%s:%s",
  257. ch.res_class ? ch.res_class : "",
  258. ch.res_name ? ch.res_name : "", c->name);
  259. for(i = 0; i < LENGTH(rules); i++)
  260. if(regs[i].propregex && !regexec(regs[i].propregex, buf, 1, &tmp, 0)) {
  261. if (rules[i].monitor >= 0 && rules[i].monitor < mcount) {
  262. matched_monitor = True;
  263. c->monitor = &monitors[rules[i].monitor];
  264. }
  265. c->isfloating = rules[i].isfloating;
  266. for(j = 0; regs[i].tagregex && j < LENGTH(tags); j++) {
  267. if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) {
  268. matched_tag = True;
  269. c->tags[j] = True;
  270. }
  271. }
  272. }
  273. if(ch.res_class)
  274. XFree(ch.res_class);
  275. if(ch.res_name)
  276. XFree(ch.res_name);
  277. if(!matched_tag)
  278. memcpy(c->tags, monitorat()->seltags, sizeof initags);
  279. if (!matched_monitor)
  280. c->monitor = monitorat();
  281. }
  282. void
  283. arrange(void) {
  284. Client *c;
  285. for(c = clients; c; c = c->next)
  286. if(isvisible(c, c->monitor))
  287. unban(c);
  288. else
  289. ban(c);
  290. selmonitor->layout->arrange(selmonitor);
  291. focus(NULL);
  292. restack(selmonitor);
  293. }
  294. void
  295. attach(Client *c) {
  296. if(clients)
  297. clients->prev = c;
  298. c->next = clients;
  299. clients = c;
  300. }
  301. void
  302. attachstack(Client *c) {
  303. c->snext = stack;
  304. stack = c;
  305. }
  306. void
  307. ban(Client *c) {
  308. if(c->isbanned)
  309. return;
  310. XMoveWindow(dpy, c->win, c->x + 3 * c->monitor->sw, c->y);
  311. c->isbanned = True;
  312. }
  313. void
  314. buttonpress(XEvent *e) {
  315. unsigned int i, x;
  316. Client *c;
  317. XButtonPressedEvent *ev = &e->xbutton;
  318. Monitor *m = monitorat();
  319. if(ev->window == m->barwin) {
  320. x = 0;
  321. for(i = 0; i < LENGTH(tags); i++) {
  322. x += textw(tags[i]);
  323. if(ev->x < x) {
  324. if(ev->button == Button1) {
  325. if(ev->state & MODKEY)
  326. tag(tags[i]);
  327. else
  328. view(tags[i]);
  329. }
  330. else if(ev->button == Button3) {
  331. if(ev->state & MODKEY)
  332. toggletag(tags[i]);
  333. else
  334. toggleview(tags[i]);
  335. }
  336. return;
  337. }
  338. }
  339. if((ev->x < x + blw) && ev->button == Button1)
  340. setlayout(NULL);
  341. }
  342. else if((c = getclient(ev->window))) {
  343. focus(c);
  344. if(CLEANMASK(ev->state) != MODKEY)
  345. return;
  346. if(ev->button == Button1) {
  347. restack(c->monitor);
  348. movemouse(c);
  349. }
  350. else if(ev->button == Button2) {
  351. if((floating != m->layout->arrange) && c->isfloating)
  352. togglefloating(NULL);
  353. else
  354. zoom(NULL);
  355. }
  356. else if(ev->button == Button3 && !c->isfixed) {
  357. restack(c->monitor);
  358. resizemouse(c);
  359. }
  360. }
  361. }
  362. void
  363. checkotherwm(void) {
  364. otherwm = False;
  365. XSetErrorHandler(xerrorstart);
  366. /* this causes an error if some other window manager is running */
  367. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  368. XSync(dpy, False);
  369. if(otherwm)
  370. eprint("dwm: another window manager is already running\n");
  371. XSync(dpy, False);
  372. XSetErrorHandler(NULL);
  373. xerrorxlib = XSetErrorHandler(xerror);
  374. XSync(dpy, False);
  375. }
  376. void
  377. cleanup(void) {
  378. unsigned int i;
  379. close(STDIN_FILENO);
  380. while(stack) {
  381. unban(stack);
  382. unmanage(stack);
  383. }
  384. if(dc.font.set)
  385. XFreeFontSet(dpy, dc.font.set);
  386. else
  387. XFreeFont(dpy, dc.font.xfont);
  388. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  389. XFreePixmap(dpy, dc.drawable);
  390. XFreeGC(dpy, dc.gc);
  391. XFreeCursor(dpy, cursor[CurNormal]);
  392. XFreeCursor(dpy, cursor[CurResize]);
  393. XFreeCursor(dpy, cursor[CurMove]);
  394. for(i = 0; i < mcount; i++)
  395. XDestroyWindow(dpy, monitors[i].barwin);
  396. XSync(dpy, False);
  397. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  398. }
  399. void
  400. compileregs(void) {
  401. unsigned int i;
  402. regex_t *reg;
  403. if(regs)
  404. return;
  405. regs = emallocz(LENGTH(rules) * sizeof(Regs));
  406. for(i = 0; i < LENGTH(rules); i++) {
  407. if(rules[i].prop) {
  408. reg = emallocz(sizeof(regex_t));
  409. if(regcomp(reg, rules[i].prop, REG_EXTENDED))
  410. free(reg);
  411. else
  412. regs[i].propregex = reg;
  413. }
  414. if(rules[i].tags) {
  415. reg = emallocz(sizeof(regex_t));
  416. if(regcomp(reg, rules[i].tags, REG_EXTENDED))
  417. free(reg);
  418. else
  419. regs[i].tagregex = reg;
  420. }
  421. }
  422. }
  423. void
  424. configure(Client *c) {
  425. XConfigureEvent ce;
  426. ce.type = ConfigureNotify;
  427. ce.display = dpy;
  428. ce.event = c->win;
  429. ce.window = c->win;
  430. ce.x = c->x;
  431. ce.y = c->y;
  432. ce.width = c->w;
  433. ce.height = c->h;
  434. ce.border_width = c->border;
  435. ce.above = None;
  436. ce.override_redirect = False;
  437. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  438. }
  439. void
  440. configurenotify(XEvent *e) {
  441. XConfigureEvent *ev = &e->xconfigure;
  442. Monitor *m = selmonitor;
  443. if(ev->window == root && (ev->width != m->sw || ev->height != m->sh)) {
  444. /* TODO -- update Xinerama dimensions here */
  445. m->sw = ev->width;
  446. m->sh = ev->height;
  447. XFreePixmap(dpy, dc.drawable);
  448. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(root, screen), bh, DefaultDepth(dpy, screen));
  449. XResizeWindow(dpy, m->barwin, m->sw, bh);
  450. updatebarpos(m);
  451. arrange();
  452. }
  453. }
  454. void
  455. configurerequest(XEvent *e) {
  456. Client *c;
  457. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  458. XWindowChanges wc;
  459. if((c = getclient(ev->window))) {
  460. Monitor *m = c->monitor;
  461. if(ev->value_mask & CWBorderWidth)
  462. c->border = ev->border_width;
  463. if(c->isfixed || c->isfloating || (floating == m->layout->arrange)) {
  464. if(ev->value_mask & CWX)
  465. c->x = m->sx+ev->x;
  466. if(ev->value_mask & CWY)
  467. c->y = m->sy+ev->y;
  468. if(ev->value_mask & CWWidth)
  469. c->w = ev->width;
  470. if(ev->value_mask & CWHeight)
  471. c->h = ev->height;
  472. if((c->x - m->sx + c->w) > m->sw && c->isfloating)
  473. c->x = m->sx + (m->sw / 2 - c->w / 2); /* center in x direction */
  474. if((c->y - m->sy + c->h) > m->sh && c->isfloating)
  475. c->y = m->sy + (m->sh / 2 - c->h / 2); /* center in y direction */
  476. if((ev->value_mask & (CWX | CWY))
  477. && !(ev->value_mask & (CWWidth | CWHeight)))
  478. configure(c);
  479. if(isvisible(c, monitorat()))
  480. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  481. }
  482. else
  483. configure(c);
  484. }
  485. else {
  486. wc.x = ev->x;
  487. wc.y = ev->y;
  488. wc.width = ev->width;
  489. wc.height = ev->height;
  490. wc.border_width = ev->border_width;
  491. wc.sibling = ev->above;
  492. wc.stack_mode = ev->detail;
  493. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  494. }
  495. XSync(dpy, False);
  496. }
  497. void
  498. destroynotify(XEvent *e) {
  499. Client *c;
  500. XDestroyWindowEvent *ev = &e->xdestroywindow;
  501. if((c = getclient(ev->window)))
  502. unmanage(c);
  503. }
  504. void
  505. detach(Client *c) {
  506. if(c->prev)
  507. c->prev->next = c->next;
  508. if(c->next)
  509. c->next->prev = c->prev;
  510. if(c == clients)
  511. clients = c->next;
  512. c->next = c->prev = NULL;
  513. }
  514. void
  515. detachstack(Client *c) {
  516. Client **tc;
  517. for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
  518. *tc = c->snext;
  519. }
  520. void
  521. drawbar(Monitor *m) {
  522. int j, x;
  523. Client *c;
  524. dc.x = 0;
  525. for(c = stack; c && !isvisible(c, m); c = c->snext);
  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(m, j));
  530. drawsquare(m, c && c->tags[j] && c->monitor == m,
  531. isoccupied(m, j), isurgent(m, j), dc.sel);
  532. }
  533. else {
  534. drawtext(m, tags[j], dc.norm, isurgent(m, j));
  535. drawsquare(m, c && c->tags[j] && c->monitor == m,
  536. isoccupied(m, j), isurgent(m, 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(m == 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. void
  567. drawsquare(Monitor *m, Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  568. int x;
  569. XGCValues gcv;
  570. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  571. gcv.foreground = col[invert ? ColBG : ColFG];
  572. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  573. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  574. r.x = dc.x + 1;
  575. r.y = dc.y + 1;
  576. if(filled) {
  577. r.width = r.height = x + 1;
  578. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  579. }
  580. else if(empty) {
  581. r.width = r.height = x;
  582. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  583. }
  584. }
  585. void
  586. drawtext(Monitor *m, const char *text, unsigned long col[ColLast], Bool invert) {
  587. int x, y, w, h;
  588. static char buf[256];
  589. unsigned int len, olen;
  590. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  591. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  592. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  593. if(!text)
  594. return;
  595. w = 0;
  596. olen = len = strlen(text);
  597. if(len >= sizeof buf)
  598. len = sizeof buf - 1;
  599. memcpy(buf, text, len);
  600. buf[len] = 0;
  601. h = dc.font.ascent + dc.font.descent;
  602. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  603. x = dc.x + (h / 2);
  604. /* shorten text if necessary */
  605. while(len && (w = textnw(buf, len)) > dc.w - h)
  606. buf[--len] = 0;
  607. if(len < olen) {
  608. if(len > 1)
  609. buf[len - 1] = '.';
  610. if(len > 2)
  611. buf[len - 2] = '.';
  612. if(len > 3)
  613. buf[len - 3] = '.';
  614. }
  615. if(w > dc.w)
  616. return; /* too long */
  617. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  618. if(dc.font.set)
  619. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  620. else
  621. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  622. }
  623. void *
  624. emallocz(unsigned int size) {
  625. void *res = calloc(1, size);
  626. if(!res)
  627. eprint("fatal: could not malloc() %u bytes\n", size);
  628. return res;
  629. }
  630. void
  631. enternotify(XEvent *e) {
  632. Client *c;
  633. XCrossingEvent *ev = &e->xcrossing;
  634. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior) {
  635. if(!isxinerama || ev->window != root)
  636. return;
  637. }
  638. if((c = getclient(ev->window)))
  639. focus(c);
  640. else {
  641. selmonitor = monitorat();
  642. fprintf(stderr, "updating selmonitor %d\n", selmonitor - monitors);
  643. focus(NULL);
  644. }
  645. }
  646. void
  647. eprint(const char *errstr, ...) {
  648. va_list ap;
  649. va_start(ap, errstr);
  650. vfprintf(stderr, errstr, ap);
  651. va_end(ap);
  652. exit(EXIT_FAILURE);
  653. }
  654. void
  655. expose(XEvent *e) {
  656. Monitor *m;
  657. XExposeEvent *ev = &e->xexpose;
  658. if(ev->count == 0 && (m = getmonitor(ev->window)))
  659. drawbar(m);
  660. }
  661. void
  662. floating(Monitor *m) { /* default floating layout */
  663. Client *c;
  664. domwfact = dozoom = False;
  665. for(c = clients; c; c = c->next)
  666. if(isvisible(c, m))
  667. resize(c, c->x, c->y, c->w, c->h, True);
  668. }
  669. void
  670. focus(Client *c) {
  671. if(c)
  672. selmonitor = c->monitor;
  673. if(!c || (c && !isvisible(c, selmonitor)))
  674. for(c = stack; c && !isvisible(c, c->monitor); c = c->snext);
  675. if(sel && sel != c) {
  676. grabbuttons(sel, False);
  677. XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
  678. }
  679. if(c) {
  680. detachstack(c);
  681. attachstack(c);
  682. grabbuttons(c, True);
  683. }
  684. sel = c;
  685. if(c) {
  686. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  687. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  688. selmonitor = c->monitor;
  689. }
  690. else
  691. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  692. drawbar(selmonitor);
  693. }
  694. void
  695. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  696. XFocusChangeEvent *ev = &e->xfocus;
  697. if(sel && ev->window != sel->win)
  698. XSetInputFocus(dpy, sel->win, RevertToPointerRoot, CurrentTime);
  699. }
  700. void
  701. focusnext(const char *arg) {
  702. Client *c;
  703. if(!sel)
  704. return;
  705. for(c = sel->next; c && !isvisible(c, selmonitor); c = c->next);
  706. if(!c)
  707. for(c = clients; c && !isvisible(c, selmonitor); c = c->next);
  708. if(c) {
  709. focus(c);
  710. restack(c->monitor);
  711. }
  712. }
  713. void
  714. focusprev(const char *arg) {
  715. Client *c;
  716. if(!sel)
  717. return;
  718. for(c = sel->prev; c && !isvisible(c, selmonitor); c = c->prev);
  719. if(!c) {
  720. for(c = clients; c && c->next; c = c->next);
  721. for(; c && !isvisible(c, selmonitor); c = c->prev);
  722. }
  723. if(c) {
  724. focus(c);
  725. restack(c->monitor);
  726. }
  727. }
  728. Client *
  729. getclient(Window w) {
  730. Client *c;
  731. for(c = clients; c && c->win != w; c = c->next);
  732. return c;
  733. }
  734. unsigned long
  735. getcolor(const char *colstr) {
  736. Colormap cmap = DefaultColormap(dpy, screen);
  737. XColor color;
  738. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  739. eprint("error, cannot allocate color '%s'\n", colstr);
  740. return color.pixel;
  741. }
  742. Monitor *
  743. getmonitor(Window barwin) {
  744. unsigned int i;
  745. for(i = 0; i < mcount; i++)
  746. if(monitors[i].barwin == barwin)
  747. return &monitors[i];
  748. return NULL;
  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(Monitor *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(Monitor *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, Monitor *m) {
  926. unsigned int i;
  927. if(c->monitor != m)
  928. return False;
  929. for(i = 0; i < LENGTH(tags); i++)
  930. if(c->tags[i] && 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 = selmonitor;
  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. Monitor *
  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 &monitors[i];
  1049. }
  1050. }
  1051. return NULL;
  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 = 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, Monitor *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(c->monitor);
  1124. break;
  1125. }
  1126. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1127. updatetitle(c);
  1128. if(c == sel)
  1129. drawbar(c->monitor);
  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 = 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 = 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(Monitor *m) {
  1249. Client *c;
  1250. XEvent ev;
  1251. XWindowChanges wc;
  1252. drawbar(m);
  1253. if(!sel)
  1254. return;
  1255. if(sel->isfloating || (m->layout->arrange == floating))
  1256. XRaiseWindow(dpy, sel->win);
  1257. if(m->layout->arrange != floating) {
  1258. wc.stack_mode = Below;
  1259. wc.sibling = m->barwin;
  1260. if(!sel->isfloating) {
  1261. XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc);
  1262. wc.sibling = sel->win;
  1263. }
  1264. for(c = nexttiled(clients, m); c; c = nexttiled(c->next, m)) {
  1265. if(c == sel)
  1266. continue;
  1267. XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
  1268. wc.sibling = c->win;
  1269. }
  1270. }
  1271. XSync(dpy, False);
  1272. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1273. }
  1274. void
  1275. run(void) {
  1276. char *p;
  1277. char buf[sizeof stext];
  1278. fd_set rd;
  1279. int r, xfd;
  1280. unsigned int len, offset;
  1281. XEvent ev;
  1282. /* main event loop, also reads status text from stdin */
  1283. XSync(dpy, False);
  1284. xfd = ConnectionNumber(dpy);
  1285. readin = True;
  1286. offset = 0;
  1287. len = sizeof stext - 1;
  1288. buf[len] = stext[len] = '\0'; /* 0-terminator is never touched */
  1289. while(running) {
  1290. FD_ZERO(&rd);
  1291. if(readin)
  1292. FD_SET(STDIN_FILENO, &rd);
  1293. FD_SET(xfd, &rd);
  1294. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1295. if(errno == EINTR)
  1296. continue;
  1297. eprint("select failed\n");
  1298. }
  1299. if(FD_ISSET(STDIN_FILENO, &rd)) {
  1300. switch((r = read(STDIN_FILENO, buf + offset, len - offset))) {
  1301. case -1:
  1302. strncpy(stext, strerror(errno), len);
  1303. readin = False;
  1304. break;
  1305. case 0:
  1306. strncpy(stext, "EOF", 4);
  1307. readin = False;
  1308. break;
  1309. default:
  1310. for(p = buf + offset; r > 0; p++, r--, offset++)
  1311. if(*p == '\n' || *p == '\0') {
  1312. *p = '\0';
  1313. strncpy(stext, buf, len);
  1314. p += r - 1; /* p is buf + offset + r - 1 */
  1315. for(r = 0; *(p - r) && *(p - r) != '\n'; r++);
  1316. offset = r;
  1317. if(r)
  1318. memmove(buf, p - r + 1, r);
  1319. break;
  1320. }
  1321. break;
  1322. }
  1323. drawbar(selmonitor);
  1324. }
  1325. while(XPending(dpy)) {
  1326. XNextEvent(dpy, &ev);
  1327. if(handler[ev.type])
  1328. (handler[ev.type])(&ev); /* call handler */
  1329. }
  1330. }
  1331. }
  1332. void
  1333. scan(void) {
  1334. unsigned int i, num;
  1335. Window *wins, d1, d2;
  1336. XWindowAttributes wa;
  1337. wins = NULL;
  1338. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1339. for(i = 0; i < num; i++) {
  1340. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1341. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1342. continue;
  1343. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1344. manage(wins[i], &wa);
  1345. }
  1346. for(i = 0; i < num; i++) { /* now the transients */
  1347. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1348. continue;
  1349. if(XGetTransientForHint(dpy, wins[i], &d1)
  1350. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1351. manage(wins[i], &wa);
  1352. }
  1353. }
  1354. if(wins)
  1355. XFree(wins);
  1356. }
  1357. void
  1358. setclientstate(Client *c, long state) {
  1359. long data[] = {state, None};
  1360. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1361. PropModeReplace, (unsigned char *)data, 2);
  1362. }
  1363. void
  1364. setlayout(const char *arg) {
  1365. unsigned int i;
  1366. Monitor *m = monitorat();
  1367. if(!arg) {
  1368. m->layout++;
  1369. if(m->layout == &layouts[LENGTH(layouts)])
  1370. m->layout = &layouts[0];
  1371. }
  1372. else {
  1373. for(i = 0; i < LENGTH(layouts); i++)
  1374. if(!strcmp(arg, layouts[i].symbol))
  1375. break;
  1376. if(i == LENGTH(layouts))
  1377. return;
  1378. m->layout = &layouts[i];
  1379. }
  1380. if(sel)
  1381. arrange();
  1382. else
  1383. drawbar(m);
  1384. }
  1385. void
  1386. setmwfact(const char *arg) {
  1387. double delta;
  1388. Monitor *m = monitorat();
  1389. if(!domwfact)
  1390. return;
  1391. /* arg handling, manipulate mwfact */
  1392. if(arg == NULL)
  1393. m->mwfact = MWFACT;
  1394. else if(sscanf(arg, "%lf", &delta) == 1) {
  1395. if(arg[0] == '+' || arg[0] == '-')
  1396. m->mwfact += delta;
  1397. else
  1398. m->mwfact = delta;
  1399. if(m->mwfact < 0.1)
  1400. m->mwfact = 0.1;
  1401. else if(m->mwfact > 0.9)
  1402. m->mwfact = 0.9;
  1403. }
  1404. arrange();
  1405. }
  1406. void
  1407. setup(void) {
  1408. unsigned int i;
  1409. Monitor *m;
  1410. XSetWindowAttributes wa;
  1411. XineramaScreenInfo *info = NULL;
  1412. /* init atoms */
  1413. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1414. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1415. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1416. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1417. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1418. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1419. /* init cursors */
  1420. wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1421. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1422. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1423. // init screens/monitors first
  1424. mcount = 1;
  1425. if((isxinerama = XineramaIsActive(dpy)))
  1426. info = XineramaQueryScreens(dpy, &mcount);
  1427. selmonitor = monitors = emallocz(mcount * sizeof(Monitor));
  1428. screen = DefaultScreen(dpy);
  1429. root = RootWindow(dpy, screen);
  1430. /* init appearance */
  1431. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  1432. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  1433. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  1434. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  1435. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  1436. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  1437. initfont(FONT);
  1438. dc.h = bh = dc.font.height + 2;
  1439. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1440. dc.gc = XCreateGC(dpy, root, 0, 0);
  1441. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1442. if(!dc.font.set)
  1443. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1444. for(blw = i = 0; i < LENGTH(layouts); i++) {
  1445. i = textw(layouts[i].symbol);
  1446. if(i > blw)
  1447. blw = i;
  1448. }
  1449. for(i = 0; i < mcount; i++) {
  1450. /* init geometry */
  1451. m = &monitors[i];
  1452. m->id = i;
  1453. if (mcount != 1 && isxinerama) {
  1454. m->sx = info[i].x_org;
  1455. m->sy = info[i].y_org;
  1456. m->sw = info[i].width;
  1457. m->sh = info[i].height;
  1458. fprintf(stderr, "monitor[%d]: %d,%d,%d,%d\n", i, m->sx, m->sy, m->sw, m->sh);
  1459. }
  1460. else {
  1461. m->sx = 0;
  1462. m->sy = 0;
  1463. m->sw = DisplayWidth(dpy, screen);
  1464. m->sh = DisplayHeight(dpy, screen);
  1465. }
  1466. m->seltags = emallocz(sizeof initags);
  1467. m->prevtags = emallocz(sizeof initags);
  1468. memcpy(m->seltags, initags, sizeof initags);
  1469. memcpy(m->prevtags, initags, sizeof initags);
  1470. /* init layouts */
  1471. m->mwfact = MWFACT;
  1472. m->layout = &layouts[0];
  1473. // TODO: bpos per screen?
  1474. bpos = BARPOS;
  1475. wa.override_redirect = 1;
  1476. wa.background_pixmap = ParentRelative;
  1477. wa.event_mask = ButtonPressMask | ExposureMask;
  1478. /* init bars */
  1479. m->barwin = XCreateWindow(dpy, root, m->sx, m->sy, m->sw, bh, 0,
  1480. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  1481. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  1482. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1483. updatebarpos(m);
  1484. XMapRaised(dpy, m->barwin);
  1485. strcpy(stext, "dwm-"VERSION);
  1486. /* EWMH support per monitor */
  1487. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1488. PropModeReplace, (unsigned char *) netatom, NetLast);
  1489. /* select for events */
  1490. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  1491. | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
  1492. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  1493. XSelectInput(dpy, root, wa.event_mask);
  1494. drawbar(m);
  1495. }
  1496. if(info)
  1497. XFree(info);
  1498. /* grab keys */
  1499. grabkeys();
  1500. /* init tags */
  1501. compileregs();
  1502. selmonitor = monitorat();
  1503. fprintf(stderr, "selmonitor == %d\n", selmonitor - monitors);
  1504. }
  1505. void
  1506. spawn(const char *arg) {
  1507. static char *shell = NULL;
  1508. if(!shell && !(shell = getenv("SHELL")))
  1509. shell = "/bin/sh";
  1510. if(!arg)
  1511. return;
  1512. /* The double-fork construct avoids zombie processes and keeps the code
  1513. * clean from stupid signal handlers. */
  1514. if(fork() == 0) {
  1515. if(fork() == 0) {
  1516. if(dpy)
  1517. close(ConnectionNumber(dpy));
  1518. setsid();
  1519. execl(shell, shell, "-c", arg, (char *)NULL);
  1520. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
  1521. perror(" failed");
  1522. }
  1523. exit(0);
  1524. }
  1525. wait(0);
  1526. }
  1527. void
  1528. tag(const char *arg) {
  1529. unsigned int i;
  1530. if(!sel)
  1531. return;
  1532. for(i = 0; i < LENGTH(tags); i++)
  1533. sel->tags[i] = (NULL == arg);
  1534. sel->tags[idxoftag(arg)] = True;
  1535. arrange();
  1536. }
  1537. unsigned int
  1538. textnw(const char *text, unsigned int len) {
  1539. XRectangle r;
  1540. if(dc.font.set) {
  1541. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1542. return r.width;
  1543. }
  1544. return XTextWidth(dc.font.xfont, text, len);
  1545. }
  1546. unsigned int
  1547. textw(const char *text) {
  1548. return textnw(text, strlen(text)) + dc.font.height;
  1549. }
  1550. void
  1551. tile(Monitor *m) {
  1552. unsigned int i, n, nx, ny, nw, nh, mw, th;
  1553. Client *c, *mc;
  1554. domwfact = dozoom = True;
  1555. nx = ny = nw = 0; /* gcc stupidity requires this */
  1556. for(n = 0, c = nexttiled(clients, m); c; c = nexttiled(c->next, m))
  1557. n++;
  1558. /* window geoms */
  1559. mw = (n == 1) ? m->waw : m->mwfact * m->waw;
  1560. th = (n > 1) ? m->wah / (n - 1) : 0;
  1561. if(n > 1 && th < bh)
  1562. th = m->wah;
  1563. for(i = 0, c = mc = nexttiled(clients, m); c; c = nexttiled(c->next, m)) {
  1564. if(i == 0) { /* master */
  1565. nx = m->wax;
  1566. ny = m->way;
  1567. nw = mw - 2 * c->border;
  1568. nh = m->wah - 2 * c->border;
  1569. }
  1570. else { /* tile window */
  1571. if(i == 1) {
  1572. ny = m->way;
  1573. nx += mc->w + 2 * mc->border;
  1574. nw = m->waw - mw - 2 * c->border;
  1575. }
  1576. if(i + 1 == n) /* remainder */
  1577. nh = (m->way + m->wah) - ny - 2 * c->border;
  1578. else
  1579. nh = th - 2 * c->border;
  1580. }
  1581. resize(c, nx, ny, nw, nh, RESIZEHINTS);
  1582. if((RESIZEHINTS) && ((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw)))
  1583. /* client doesn't accept size constraints */
  1584. resize(c, nx, ny, nw, nh, False);
  1585. if(n > 1 && th != m->wah)
  1586. ny = c->y + c->h + 2 * c->border;
  1587. i++;
  1588. }
  1589. }
  1590. void
  1591. togglebar(const char *arg) {
  1592. if(bpos == BarOff)
  1593. bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
  1594. else
  1595. bpos = BarOff;
  1596. updatebarpos(monitorat());
  1597. arrange();
  1598. }
  1599. void
  1600. togglefloating(const char *arg) {
  1601. if(!sel)
  1602. return;
  1603. sel->isfloating = !sel->isfloating;
  1604. if(sel->isfloating)
  1605. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1606. arrange();
  1607. }
  1608. void
  1609. toggletag(const char *arg) {
  1610. unsigned int i, j;
  1611. if(!sel)
  1612. return;
  1613. i = idxoftag(arg);
  1614. sel->tags[i] = !sel->tags[i];
  1615. for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
  1616. if(j == LENGTH(tags))
  1617. sel->tags[i] = True; /* at least one tag must be enabled */
  1618. arrange();
  1619. }
  1620. void
  1621. toggleview(const char *arg) {
  1622. unsigned int i, j;
  1623. Monitor *m = monitorat();
  1624. i = idxoftag(arg);
  1625. m->seltags[i] = !m->seltags[i];
  1626. for(j = 0; j < LENGTH(tags) && !m->seltags[j]; j++);
  1627. if(j == LENGTH(tags))
  1628. m->seltags[i] = True; /* at least one tag must be viewed */
  1629. arrange();
  1630. }
  1631. void
  1632. unban(Client *c) {
  1633. if(!c->isbanned)
  1634. return;
  1635. XMoveWindow(dpy, c->win, c->x, c->y);
  1636. c->isbanned = False;
  1637. }
  1638. void
  1639. unmanage(Client *c) {
  1640. XWindowChanges wc;
  1641. wc.border_width = c->oldborder;
  1642. /* The server grab construct avoids race conditions. */
  1643. XGrabServer(dpy);
  1644. XSetErrorHandler(xerrordummy);
  1645. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1646. detach(c);
  1647. detachstack(c);
  1648. if(sel == c)
  1649. focus(NULL);
  1650. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1651. setclientstate(c, WithdrawnState);
  1652. free(c->tags);
  1653. free(c);
  1654. XSync(dpy, False);
  1655. XSetErrorHandler(xerror);
  1656. XUngrabServer(dpy);
  1657. arrange();
  1658. }
  1659. void
  1660. unmapnotify(XEvent *e) {
  1661. Client *c;
  1662. XUnmapEvent *ev = &e->xunmap;
  1663. if((c = getclient(ev->window)))
  1664. unmanage(c);
  1665. }
  1666. void
  1667. updatebarpos(Monitor *m) {
  1668. XEvent ev;
  1669. m->wax = m->sx;
  1670. m->way = m->sy;
  1671. m->wah = m->sh;
  1672. m->waw = m->sw;
  1673. switch(bpos) {
  1674. default:
  1675. m->wah -= bh;
  1676. m->way += bh;
  1677. XMoveWindow(dpy, m->barwin, m->sx, m->sy);
  1678. break;
  1679. case BarBot:
  1680. m->wah -= bh;
  1681. XMoveWindow(dpy, m->barwin, m->sx, m->sy + m->wah);
  1682. break;
  1683. case BarOff:
  1684. XMoveWindow(dpy, m->barwin, m->sx, m->sy - bh);
  1685. break;
  1686. }
  1687. XSync(dpy, False);
  1688. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1689. }
  1690. void
  1691. updatesizehints(Client *c) {
  1692. long msize;
  1693. XSizeHints size;
  1694. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  1695. size.flags = PSize;
  1696. c->flags = size.flags;
  1697. if(c->flags & PBaseSize) {
  1698. c->basew = size.base_width;
  1699. c->baseh = size.base_height;
  1700. }
  1701. else if(c->flags & PMinSize) {
  1702. c->basew = size.min_width;
  1703. c->baseh = size.min_height;
  1704. }
  1705. else
  1706. c->basew = c->baseh = 0;
  1707. if(c->flags & PResizeInc) {
  1708. c->incw = size.width_inc;
  1709. c->inch = size.height_inc;
  1710. }
  1711. else
  1712. c->incw = c->inch = 0;
  1713. if(c->flags & PMaxSize) {
  1714. c->maxw = size.max_width;
  1715. c->maxh = size.max_height;
  1716. }
  1717. else
  1718. c->maxw = c->maxh = 0;
  1719. if(c->flags & PMinSize) {
  1720. c->minw = size.min_width;
  1721. c->minh = size.min_height;
  1722. }
  1723. else if(c->flags & PBaseSize) {
  1724. c->minw = size.base_width;
  1725. c->minh = size.base_height;
  1726. }
  1727. else
  1728. c->minw = c->minh = 0;
  1729. if(c->flags & PAspect) {
  1730. c->minax = size.min_aspect.x;
  1731. c->maxax = size.max_aspect.x;
  1732. c->minay = size.min_aspect.y;
  1733. c->maxay = size.max_aspect.y;
  1734. }
  1735. else
  1736. c->minax = c->maxax = c->minay = c->maxay = 0;
  1737. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1738. && c->maxw == c->minw && c->maxh == c->minh);
  1739. }
  1740. void
  1741. updatetitle(Client *c) {
  1742. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1743. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1744. }
  1745. void
  1746. updatewmhints(Client *c) {
  1747. XWMHints *wmh;
  1748. if((wmh = XGetWMHints(dpy, c->win))) {
  1749. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1750. XFree(wmh);
  1751. }
  1752. }
  1753. /* There's no way to check accesses to destroyed windows, thus those cases are
  1754. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1755. * default error handler, which may call exit. */
  1756. int
  1757. xerror(Display *dpy, XErrorEvent *ee) {
  1758. if(ee->error_code == BadWindow
  1759. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1760. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1761. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1762. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1763. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1764. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1765. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1766. return 0;
  1767. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1768. ee->request_code, ee->error_code);
  1769. return xerrorxlib(dpy, ee); /* may call exit */
  1770. }
  1771. int
  1772. xerrordummy(Display *dsply, XErrorEvent *ee) {
  1773. return 0;
  1774. }
  1775. /* Startup Error handler to check if another window manager
  1776. * is already running. */
  1777. int
  1778. xerrorstart(Display *dsply, XErrorEvent *ee) {
  1779. otherwm = True;
  1780. return -1;
  1781. }
  1782. void
  1783. view(const char *arg) {
  1784. unsigned int i;
  1785. Bool tmp[LENGTH(tags)];
  1786. Monitor *m = monitorat();
  1787. for(i = 0; i < LENGTH(tags); i++)
  1788. tmp[i] = (NULL == arg);
  1789. tmp[idxoftag(arg)] = True;
  1790. if(memcmp(m->seltags, tmp, sizeof initags) != 0) {
  1791. memcpy(m->prevtags, m->seltags, sizeof initags);
  1792. memcpy(m->seltags, tmp, sizeof initags);
  1793. arrange();
  1794. }
  1795. }
  1796. void
  1797. viewprevtag(const char *arg) {
  1798. static Bool tmp[LENGTH(tags)];
  1799. Monitor *m = monitorat();
  1800. memcpy(tmp, m->seltags, sizeof initags);
  1801. memcpy(m->seltags, m->prevtags, sizeof initags);
  1802. memcpy(m->prevtags, tmp, sizeof initags);
  1803. arrange();
  1804. }
  1805. void
  1806. zoom(const char *arg) {
  1807. Client *c = sel;
  1808. if(!sel || !dozoom || sel->isfloating)
  1809. return;
  1810. if(c == nexttiled(clients, c->monitor))
  1811. if(!(c = nexttiled(c->next, c->monitor)))
  1812. return;
  1813. detach(c);
  1814. attach(c);
  1815. focus(c);
  1816. arrange();
  1817. }
  1818. void
  1819. movetomonitor(const char *arg) {
  1820. int i;
  1821. if (sel)
  1822. return;
  1823. if(arg)
  1824. i = atoi(arg);
  1825. else {
  1826. for(i = 0; &monitors[i] != sel->monitor && i < mcount; i++);
  1827. i++;
  1828. }
  1829. sel->monitor = &monitors[i % mcount];
  1830. memcpy(sel->tags, sel->monitor->seltags, sizeof initags);
  1831. resize(sel, sel->monitor->wax, sel->monitor->way, sel->w, sel->h, True);
  1832. arrange();
  1833. }
  1834. void
  1835. selectmonitor(const char *arg) {
  1836. int i;
  1837. Monitor *m;
  1838. if(arg)
  1839. i = atoi(arg);
  1840. else {
  1841. for(i = 0; &monitors[i] != sel->monitor && i < mcount; i++);
  1842. i++;
  1843. }
  1844. m = &monitors[i % mcount];
  1845. XWarpPointer(dpy, None, root, 0, 0, 0, 0, m->wax+m->waw/2, m->way+m->wah/2);
  1846. focus(NULL);
  1847. }
  1848. int
  1849. main(int argc, char *argv[]) {
  1850. if(argc == 2 && !strcmp("-v", argv[1]))
  1851. eprint("dwm-"VERSION", © 2006-2008 Anselm R. Garbe, Sander van Dijk, "
  1852. "Jukka Salmi, Premysl Hruby, Szabolcs Nagy, Christof Musik\n");
  1853. else if(argc != 1)
  1854. eprint("usage: dwm [-v]\n");
  1855. setlocale(LC_CTYPE, "");
  1856. if(!(dpy = XOpenDisplay(0)))
  1857. eprint("dwm: cannot open display\n");
  1858. checkotherwm();
  1859. setup();
  1860. scan();
  1861. run();
  1862. cleanup();
  1863. XCloseDisplay(dpy);
  1864. return 0;
  1865. }