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.

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