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.

2066 lines
49 KiB

17 years ago
17 years ago
17 years ago
  1. /* See LICENSE file for copyright and license details.
  2. *
  3. * dynamic window manager is designed like any other X client as well. It is
  4. * driven through handling X events. In contrast to other X clients, a window
  5. * manager selects for SubstructureRedirectMask on the root window, to receive
  6. * events about window (dis-)appearance. Only one X connection at a time is
  7. * allowed to select for this event mask.
  8. *
  9. * Calls to fetch an X event from the event queue are blocking. Due reading
  10. * status text from standard input, a select()-driven main loop has been
  11. * implemented which selects for reads on the X connection and STDIN_FILENO to
  12. * handle all data smoothly. The event handlers of dwm are organized in an
  13. * array which is accessed whenever a new event has been fetched. This allows
  14. * event dispatching in O(1) time.
  15. *
  16. * Each child of the root window is called a client, except windows which have
  17. * set the override_redirect flag. Clients are organized in a global
  18. * doubly-linked client list, the focus history is remembered through a global
  19. * stack list. Each client contains an array of Bools of the same size as the
  20. * global tags array to indicate the tags of a client.
  21. *
  22. * Keys and tagging rules are organized as arrays and defined in config.h.
  23. *
  24. * To understand everything else, start reading main().
  25. */
  26. #include <errno.h>
  27. #include <locale.h>
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <sys/select.h>
  34. #include <sys/types.h>
  35. #include <sys/wait.h>
  36. #include <regex.h>
  37. #include <X11/cursorfont.h>
  38. #include <X11/keysym.h>
  39. #include <X11/Xatom.h>
  40. #include <X11/Xlib.h>
  41. #include <X11/Xproto.h>
  42. #include <X11/Xutil.h>
  43. //#ifdef XINERAMA
  44. #include <X11/extensions/Xinerama.h>
  45. //#endif
  46. /* macros */
  47. #define BUTTONMASK (ButtonPressMask | ButtonReleaseMask)
  48. #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
  49. #define LENGTH(x) (sizeof x / sizeof x[0])
  50. #define MAXTAGLEN 16
  51. #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
  52. /* enums */
  53. enum { BarTop, BarBot, BarOff }; /* bar position */
  54. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  55. enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
  56. enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
  57. enum { WMProtocols, WMDelete, WMName, WMState, WMLast };/* default atoms */
  58. /* typedefs */
  59. typedef struct Client Client;
  60. struct Client {
  61. char name[256];
  62. int x, y, w, h;
  63. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  64. int minax, maxax, minay, maxay;
  65. long flags;
  66. unsigned int border, oldborder;
  67. Bool isbanned, isfixed, isfloating, isurgent;
  68. Bool *tags;
  69. Client *next;
  70. Client *prev;
  71. Client *snext;
  72. Window win;
  73. int monitor;
  74. };
  75. typedef struct {
  76. int x, y, w, h;
  77. unsigned long norm[ColLast];
  78. unsigned long sel[ColLast];
  79. Drawable drawable;
  80. GC gc;
  81. struct {
  82. int ascent;
  83. int descent;
  84. int height;
  85. XFontSet set;
  86. XFontStruct *xfont;
  87. } font;
  88. } DC; /* draw context */
  89. typedef struct {
  90. unsigned long mod;
  91. KeySym keysym;
  92. void (*func)(const char *arg);
  93. const char *arg;
  94. } Key;
  95. typedef struct Monitor Monitor;
  96. typedef struct {
  97. const char *symbol;
  98. void (*arrange)(Monitor *);
  99. } Layout;
  100. typedef struct {
  101. const char *prop;
  102. const char *tags;
  103. Bool isfloating;
  104. int monitor;
  105. } Rule;
  106. typedef struct {
  107. regex_t *propregex;
  108. regex_t *tagregex;
  109. } Regs;
  110. struct Monitor {
  111. unsigned int id;
  112. int sx, sy, sw, sh, wax, way, wah, waw;
  113. double mwfact;
  114. Bool *seltags;
  115. Bool *prevtags;
  116. Layout *layout;
  117. Window barwin;
  118. };
  119. /* function declarations */
  120. void applyrules(Client *c);
  121. void arrange(void);
  122. void attach(Client *c);
  123. void attachstack(Client *c);
  124. void ban(Client *c);
  125. void buttonpress(XEvent *e);
  126. void checkotherwm(void);
  127. void cleanup(void);
  128. void compileregs(void);
  129. void configure(Client *c);
  130. void configurenotify(XEvent *e);
  131. void configurerequest(XEvent *e);
  132. void destroynotify(XEvent *e);
  133. void detach(Client *c);
  134. void detachstack(Client *c);
  135. void drawbar(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(unsigned int monitor, unsigned int t);
  157. Bool isprotodel(Client *c);
  158. Bool isurgent(unsigned int monitor, unsigned int t);
  159. Bool isvisible(Client *c, int monitor);
  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. void movemouse(Client *c);
  166. Client *nexttiled(Client *c, int monitor);
  167. void propertynotify(XEvent *e);
  168. void quit(const char *arg);
  169. void reapply(const char *arg);
  170. void resize(Client *c, int x, int y, int w, int h, Bool sizehints);
  171. void resizemouse(Client *c);
  172. void restack(Monitor *m);
  173. void run(void);
  174. void scan(void);
  175. void setclientstate(Client *c, long state);
  176. void setlayout(const char *arg);
  177. void setmwfact(const char *arg);
  178. void setup(void);
  179. void spawn(const char *arg);
  180. void tag(const char *arg);
  181. unsigned int textnw(const char *text, unsigned int len);
  182. unsigned int textw(const char *text);
  183. void tile(Monitor *m);
  184. void togglebar(const char *arg);
  185. void togglefloating(const char *arg);
  186. void toggletag(const char *arg);
  187. void toggleview(const char *arg);
  188. void unban(Client *c);
  189. void unmanage(Client *c);
  190. void unmapnotify(XEvent *e);
  191. void updatebarpos(Monitor *m);
  192. void updatesizehints(Client *c);
  193. void updatetitle(Client *c);
  194. void updatewmhints(Client *c);
  195. void view(const char *arg);
  196. void viewprevtag(const char *arg); /* views previous selected tags */
  197. int xerror(Display *dpy, XErrorEvent *ee);
  198. int xerrordummy(Display *dsply, XErrorEvent *ee);
  199. int xerrorstart(Display *dsply, XErrorEvent *ee);
  200. void zoom(const char *arg);
  201. int monitorat(void);
  202. void movetomonitor(const char *arg);
  203. void selectmonitor(const char *arg);
  204. /* variables */
  205. char stext[256];
  206. int mcount = 1;
  207. int selmonitor = 0;
  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 = 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, monitors[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. monitors[selmonitor].layout->arrange(&monitors[selmonitor]);
  291. focus(NULL);
  292. restack(&monitors[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 * monitors[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 = &monitors[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(&monitors[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(&monitors[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 = &monitors[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 = &monitors[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->id); 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->id, j));
  530. drawsquare(m, c && c->tags[j] && c->monitor == m->id,
  531. isoccupied(m->id, j), isurgent(m->id, j), dc.sel);
  532. }
  533. else {
  534. drawtext(m, tags[j], dc.norm, isurgent(m->id, j));
  535. drawsquare(m, c && c->tags[j] && c->monitor == m->id,
  536. isoccupied(m->id, j), isurgent(m->id, 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->id == 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);
  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->id))
  667. resize(c, c->x, c->y, c->w, c->h, True);
  668. }
  669. void
  670. focus(Client *c) {
  671. Monitor *m;
  672. if(c)
  673. selmonitor = c->monitor;
  674. m = &monitors[selmonitor];
  675. if(!c || (c && !isvisible(c, selmonitor)))
  676. for(c = stack; c && !isvisible(c, c->monitor); c = c->snext);
  677. if(sel && sel != c) {
  678. grabbuttons(sel, False);
  679. XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
  680. }
  681. if(c) {
  682. detachstack(c);
  683. attachstack(c);
  684. grabbuttons(c, True);
  685. }
  686. sel = c;
  687. if(c) {
  688. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  689. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  690. selmonitor = c->monitor;
  691. }
  692. else
  693. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  694. drawbar(&monitors[selmonitor]);
  695. }
  696. void
  697. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  698. XFocusChangeEvent *ev = &e->xfocus;
  699. if(sel && ev->window != sel->win)
  700. XSetInputFocus(dpy, sel->win, RevertToPointerRoot, CurrentTime);
  701. }
  702. void
  703. focusnext(const char *arg) {
  704. Client *c;
  705. if(!sel)
  706. return;
  707. for(c = sel->next; c && !isvisible(c, selmonitor); c = c->next);
  708. if(!c)
  709. for(c = clients; c && !isvisible(c, selmonitor); c = c->next);
  710. if(c) {
  711. focus(c);
  712. restack(&monitors[c->monitor]);
  713. }
  714. }
  715. void
  716. focusprev(const char *arg) {
  717. Client *c;
  718. if(!sel)
  719. return;
  720. for(c = sel->prev; c && !isvisible(c, selmonitor); c = c->prev);
  721. if(!c) {
  722. for(c = clients; c && c->next; c = c->next);
  723. for(; c && !isvisible(c, selmonitor); c = c->prev);
  724. }
  725. if(c) {
  726. focus(c);
  727. restack(&monitors[c->monitor]);
  728. }
  729. }
  730. Client *
  731. getclient(Window w) {
  732. Client *c;
  733. for(c = clients; c && c->win != w; c = c->next);
  734. return c;
  735. }
  736. unsigned long
  737. getcolor(const char *colstr) {
  738. Colormap cmap = DefaultColormap(dpy, screen);
  739. XColor color;
  740. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  741. eprint("error, cannot allocate color '%s'\n", colstr);
  742. return color.pixel;
  743. }
  744. Monitor *
  745. getmonitor(Window barwin) {
  746. unsigned int i;
  747. for(i = 0; i < mcount; i++)
  748. if(monitors[i].barwin == barwin)
  749. return &monitors[i];
  750. return NULL;
  751. }
  752. long
  753. getstate(Window w) {
  754. int format, status;
  755. long result = -1;
  756. unsigned char *p = NULL;
  757. unsigned long n, extra;
  758. Atom real;
  759. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  760. &real, &format, &n, &extra, (unsigned char **)&p);
  761. if(status != Success)
  762. return -1;
  763. if(n != 0)
  764. result = *p;
  765. XFree(p);
  766. return result;
  767. }
  768. Bool
  769. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  770. char **list = NULL;
  771. int n;
  772. XTextProperty name;
  773. if(!text || size == 0)
  774. return False;
  775. text[0] = '\0';
  776. XGetTextProperty(dpy, w, &name, atom);
  777. if(!name.nitems)
  778. return False;
  779. if(name.encoding == XA_STRING)
  780. strncpy(text, (char *)name.value, size - 1);
  781. else {
  782. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  783. && n > 0 && *list) {
  784. strncpy(text, *list, size - 1);
  785. XFreeStringList(list);
  786. }
  787. }
  788. text[size - 1] = '\0';
  789. XFree(name.value);
  790. return True;
  791. }
  792. void
  793. grabbuttons(Client *c, Bool focused) {
  794. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  795. if(focused) {
  796. XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  797. GrabModeAsync, GrabModeSync, None, None);
  798. XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
  799. GrabModeAsync, GrabModeSync, None, None);
  800. XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  801. GrabModeAsync, GrabModeSync, None, None);
  802. XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  803. GrabModeAsync, GrabModeSync, None, None);
  804. XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  805. GrabModeAsync, GrabModeSync, None, None);
  806. XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
  807. GrabModeAsync, GrabModeSync, None, None);
  808. XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  809. GrabModeAsync, GrabModeSync, None, None);
  810. XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  811. GrabModeAsync, GrabModeSync, None, None);
  812. XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  813. GrabModeAsync, GrabModeSync, None, None);
  814. XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
  815. GrabModeAsync, GrabModeSync, None, None);
  816. XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  817. GrabModeAsync, GrabModeSync, None, None);
  818. XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  819. GrabModeAsync, GrabModeSync, None, None);
  820. }
  821. else
  822. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
  823. GrabModeAsync, GrabModeSync, None, None);
  824. }
  825. void
  826. grabkeys(void) {
  827. unsigned int i, j;
  828. KeyCode code;
  829. XModifierKeymap *modmap;
  830. /* init modifier map */
  831. modmap = XGetModifierMapping(dpy);
  832. for(i = 0; i < 8; i++)
  833. for(j = 0; j < modmap->max_keypermod; j++) {
  834. if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
  835. numlockmask = (1 << i);
  836. }
  837. XFreeModifiermap(modmap);
  838. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  839. for(i = 0; i < LENGTH(keys); i++) {
  840. code = XKeysymToKeycode(dpy, keys[i].keysym);
  841. XGrabKey(dpy, code, keys[i].mod, root, True,
  842. GrabModeAsync, GrabModeAsync);
  843. XGrabKey(dpy, code, keys[i].mod | LockMask, root, True,
  844. GrabModeAsync, GrabModeAsync);
  845. XGrabKey(dpy, code, keys[i].mod | numlockmask, root, True,
  846. GrabModeAsync, GrabModeAsync);
  847. XGrabKey(dpy, code, keys[i].mod | numlockmask | LockMask, root, True,
  848. GrabModeAsync, GrabModeAsync);
  849. }
  850. }
  851. unsigned int
  852. idxoftag(const char *tag) {
  853. unsigned int i;
  854. for(i = 0; (i < LENGTH(tags)) && (tags[i] != tag); i++);
  855. return (i < LENGTH(tags)) ? i : 0;
  856. }
  857. void
  858. initfont(const char *fontstr) {
  859. char *def, **missing;
  860. int i, n;
  861. missing = NULL;
  862. if(dc.font.set)
  863. XFreeFontSet(dpy, dc.font.set);
  864. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  865. if(missing) {
  866. while(n--)
  867. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  868. XFreeStringList(missing);
  869. }
  870. if(dc.font.set) {
  871. XFontSetExtents *font_extents;
  872. XFontStruct **xfonts;
  873. char **font_names;
  874. dc.font.ascent = dc.font.descent = 0;
  875. font_extents = XExtentsOfFontSet(dc.font.set);
  876. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  877. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  878. if(dc.font.ascent < (*xfonts)->ascent)
  879. dc.font.ascent = (*xfonts)->ascent;
  880. if(dc.font.descent < (*xfonts)->descent)
  881. dc.font.descent = (*xfonts)->descent;
  882. xfonts++;
  883. }
  884. }
  885. else {
  886. if(dc.font.xfont)
  887. XFreeFont(dpy, dc.font.xfont);
  888. dc.font.xfont = NULL;
  889. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  890. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  891. eprint("error, cannot load font: '%s'\n", fontstr);
  892. dc.font.ascent = dc.font.xfont->ascent;
  893. dc.font.descent = dc.font.xfont->descent;
  894. }
  895. dc.font.height = dc.font.ascent + dc.font.descent;
  896. }
  897. Bool
  898. isoccupied(unsigned int monitor, unsigned int t) {
  899. Client *c;
  900. for(c = clients; c; c = c->next)
  901. if(c->tags[t] && c->monitor == monitor)
  902. return True;
  903. return False;
  904. }
  905. Bool
  906. isprotodel(Client *c) {
  907. int i, n;
  908. Atom *protocols;
  909. Bool ret = False;
  910. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  911. for(i = 0; !ret && i < n; i++)
  912. if(protocols[i] == wmatom[WMDelete])
  913. ret = True;
  914. XFree(protocols);
  915. }
  916. return ret;
  917. }
  918. Bool
  919. isurgent(unsigned int monitor, unsigned int t) {
  920. Client *c;
  921. for(c = clients; c; c = c->next)
  922. if(c->monitor == monitor && c->isurgent && c->tags[t])
  923. return True;
  924. return False;
  925. }
  926. Bool
  927. isvisible(Client *c, int monitor) {
  928. unsigned int i;
  929. if(c->monitor != monitor)
  930. return False;
  931. for(i = 0; i < LENGTH(tags); i++)
  932. if(c->tags[i] && monitors[c->monitor].seltags[i])
  933. return True;
  934. return False;
  935. }
  936. void
  937. keypress(XEvent *e) {
  938. unsigned int i;
  939. KeySym keysym;
  940. XKeyEvent *ev;
  941. ev = &e->xkey;
  942. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  943. for(i = 0; i < LENGTH(keys); i++)
  944. if(keysym == keys[i].keysym
  945. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state))
  946. {
  947. if(keys[i].func)
  948. keys[i].func(keys[i].arg);
  949. }
  950. }
  951. void
  952. killclient(const char *arg) {
  953. XEvent ev;
  954. if(!sel)
  955. return;
  956. if(isprotodel(sel)) {
  957. ev.type = ClientMessage;
  958. ev.xclient.window = sel->win;
  959. ev.xclient.message_type = wmatom[WMProtocols];
  960. ev.xclient.format = 32;
  961. ev.xclient.data.l[0] = wmatom[WMDelete];
  962. ev.xclient.data.l[1] = CurrentTime;
  963. XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
  964. }
  965. else
  966. XKillClient(dpy, sel->win);
  967. }
  968. void
  969. manage(Window w, XWindowAttributes *wa) {
  970. Client *c, *t = NULL;
  971. Monitor *m;
  972. Status rettrans;
  973. Window trans;
  974. XWindowChanges wc;
  975. c = emallocz(sizeof(Client));
  976. c->tags = emallocz(sizeof initags);
  977. c->win = w;
  978. applyrules(c);
  979. m = &monitors[c->monitor];
  980. c->x = wa->x + m->sx;
  981. c->y = wa->y + m->sy;
  982. c->w = wa->width;
  983. c->h = wa->height;
  984. c->oldborder = wa->border_width;
  985. if(c->w == m->sw && c->h == m->sh) {
  986. c->x = m->sx;
  987. c->y = m->sy;
  988. c->border = wa->border_width;
  989. }
  990. else {
  991. if(c->x + c->w + 2 * c->border > m->wax + m->waw)
  992. c->x = m->wax + m->waw - c->w - 2 * c->border;
  993. if(c->y + c->h + 2 * c->border > m->way + m->wah)
  994. c->y = m->way + m->wah - c->h - 2 * c->border;
  995. if(c->x < m->wax)
  996. c->x = m->wax;
  997. if(c->y < m->way)
  998. c->y = m->way;
  999. c->border = BORDERPX;
  1000. }
  1001. wc.border_width = c->border;
  1002. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  1003. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  1004. configure(c); /* propagates border_width, if size doesn't change */
  1005. updatesizehints(c);
  1006. XSelectInput(dpy, w, EnterWindowMask | FocusChangeMask | PropertyChangeMask | StructureNotifyMask);
  1007. grabbuttons(c, False);
  1008. updatetitle(c);
  1009. if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
  1010. for(t = clients; t && t->win != trans; t = t->next);
  1011. if(t)
  1012. memcpy(c->tags, t->tags, sizeof initags);
  1013. if(!c->isfloating)
  1014. c->isfloating = (rettrans == Success) || c->isfixed;
  1015. attach(c);
  1016. attachstack(c);
  1017. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
  1018. ban(c);
  1019. XMapWindow(dpy, c->win);
  1020. setclientstate(c, NormalState);
  1021. arrange();
  1022. }
  1023. void
  1024. mappingnotify(XEvent *e) {
  1025. XMappingEvent *ev = &e->xmapping;
  1026. XRefreshKeyboardMapping(ev);
  1027. if(ev->request == MappingKeyboard)
  1028. grabkeys();
  1029. }
  1030. void
  1031. maprequest(XEvent *e) {
  1032. static XWindowAttributes wa;
  1033. XMapRequestEvent *ev = &e->xmaprequest;
  1034. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1035. return;
  1036. if(wa.override_redirect)
  1037. return;
  1038. if(!getclient(ev->window))
  1039. manage(ev->window, &wa);
  1040. }
  1041. int
  1042. monitorat() {
  1043. int i, x, y;
  1044. Window win;
  1045. unsigned int mask;
  1046. XQueryPointer(dpy, root, &win, &win, &x, &y, &i, &i, &mask);
  1047. for(i = 0; i < mcount; i++) {
  1048. if((x >= monitors[i].sx && x < monitors[i].sx + monitors[i].sw)
  1049. && (y >= monitors[i].sy && y < monitors[i].sy + monitors[i].sh)) {
  1050. return i;
  1051. }
  1052. }
  1053. return 0;
  1054. }
  1055. void
  1056. movemouse(Client *c) {
  1057. int x1, y1, ocx, ocy, di, nx, ny;
  1058. unsigned int dui;
  1059. Monitor *m;
  1060. Window dummy;
  1061. XEvent ev;
  1062. ocx = nx = c->x;
  1063. ocy = ny = c->y;
  1064. m = &monitors[c->monitor];
  1065. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1066. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1067. return;
  1068. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  1069. for(;;) {
  1070. XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
  1071. switch (ev.type) {
  1072. case ButtonRelease:
  1073. XUngrabPointer(dpy, CurrentTime);
  1074. return;
  1075. case ConfigureRequest:
  1076. case Expose:
  1077. case MapRequest:
  1078. handler[ev.type](&ev);
  1079. break;
  1080. case MotionNotify:
  1081. XSync(dpy, False);
  1082. nx = ocx + (ev.xmotion.x - x1);
  1083. ny = ocy + (ev.xmotion.y - y1);
  1084. if(abs(m->wax - nx) < SNAP)
  1085. nx = m->wax;
  1086. else if(abs((m->wax + m->waw) - (nx + c->w + 2 * c->border)) < SNAP)
  1087. nx = m->wax + m->waw - c->w - 2 * c->border;
  1088. if(abs(m->way - ny) < SNAP)
  1089. ny = m->way;
  1090. else if(abs((m->way + m->wah) - (ny + c->h + 2 * c->border)) < SNAP)
  1091. ny = m->way + m->wah - c->h - 2 * c->border;
  1092. if((m->layout->arrange != floating) && (abs(nx - c->x) > SNAP || abs(ny - c->y) > SNAP))
  1093. togglefloating(NULL);
  1094. if((m->layout->arrange == floating) || c->isfloating)
  1095. resize(c, nx, ny, c->w, c->h, False);
  1096. break;
  1097. }
  1098. }
  1099. }
  1100. Client *
  1101. nexttiled(Client *c, int monitor) {
  1102. for(; c && (c->isfloating || !isvisible(c, monitor)); c = c->next);
  1103. return c;
  1104. }
  1105. void
  1106. propertynotify(XEvent *e) {
  1107. Client *c;
  1108. Window trans;
  1109. XPropertyEvent *ev = &e->xproperty;
  1110. if(ev->state == PropertyDelete)
  1111. return; /* ignore */
  1112. if((c = getclient(ev->window))) {
  1113. switch (ev->atom) {
  1114. default: break;
  1115. case XA_WM_TRANSIENT_FOR:
  1116. XGetTransientForHint(dpy, c->win, &trans);
  1117. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  1118. arrange();
  1119. break;
  1120. case XA_WM_NORMAL_HINTS:
  1121. updatesizehints(c);
  1122. break;
  1123. case XA_WM_HINTS:
  1124. updatewmhints(c);
  1125. drawbar(&monitors[c->monitor]);
  1126. break;
  1127. }
  1128. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1129. updatetitle(c);
  1130. if(c == sel)
  1131. drawbar(&monitors[c->monitor]);
  1132. }
  1133. }
  1134. }
  1135. void
  1136. quit(const char *arg) {
  1137. readin = running = False;
  1138. }
  1139. void
  1140. reapply(const char *arg) {
  1141. static Bool zerotags[LENGTH(tags)] = { 0 };
  1142. Client *c;
  1143. for(c = clients; c; c = c->next) {
  1144. memcpy(c->tags, zerotags, sizeof zerotags);
  1145. applyrules(c);
  1146. }
  1147. arrange();
  1148. }
  1149. void
  1150. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  1151. Monitor *m;
  1152. XWindowChanges wc;
  1153. m = &monitors[c->monitor];
  1154. if(sizehints) {
  1155. /* set minimum possible */
  1156. if (w < 1)
  1157. w = 1;
  1158. if (h < 1)
  1159. h = 1;
  1160. /* temporarily remove base dimensions */
  1161. w -= c->basew;
  1162. h -= c->baseh;
  1163. /* adjust for aspect limits */
  1164. if (c->minay > 0 && c->maxay > 0 && c->minax > 0 && c->maxax > 0) {
  1165. if (w * c->maxay > h * c->maxax)
  1166. w = h * c->maxax / c->maxay;
  1167. else if (w * c->minay < h * c->minax)
  1168. h = w * c->minay / c->minax;
  1169. }
  1170. /* adjust for increment value */
  1171. if(c->incw)
  1172. w -= w % c->incw;
  1173. if(c->inch)
  1174. h -= h % c->inch;
  1175. /* restore base dimensions */
  1176. w += c->basew;
  1177. h += c->baseh;
  1178. if(c->minw > 0 && w < c->minw)
  1179. w = c->minw;
  1180. if(c->minh > 0 && h < c->minh)
  1181. h = c->minh;
  1182. if(c->maxw > 0 && w > c->maxw)
  1183. w = c->maxw;
  1184. if(c->maxh > 0 && h > c->maxh)
  1185. h = c->maxh;
  1186. }
  1187. if(w <= 0 || h <= 0)
  1188. return;
  1189. if(x > m->sw)
  1190. x = m->sw - w - 2 * c->border;
  1191. if(y > m->sh)
  1192. y = m->sh - h - 2 * c->border;
  1193. if(x + w + 2 * c->border < m->sx)
  1194. x = m->sx;
  1195. if(y + h + 2 * c->border < m->sy)
  1196. y = m->sy;
  1197. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  1198. c->x = wc.x = x;
  1199. c->y = wc.y = y;
  1200. c->w = wc.width = w;
  1201. c->h = wc.height = h;
  1202. wc.border_width = c->border;
  1203. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  1204. configure(c);
  1205. XSync(dpy, False);
  1206. }
  1207. }
  1208. void
  1209. resizemouse(Client *c) {
  1210. int ocx, ocy;
  1211. int nw, nh;
  1212. Monitor *m;
  1213. XEvent ev;
  1214. ocx = c->x;
  1215. ocy = c->y;
  1216. m = &monitors[c->monitor];
  1217. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1218. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1219. return;
  1220. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
  1221. for(;;) {
  1222. XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
  1223. switch(ev.type) {
  1224. case ButtonRelease:
  1225. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
  1226. c->w + c->border - 1, c->h + c->border - 1);
  1227. XUngrabPointer(dpy, CurrentTime);
  1228. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1229. return;
  1230. case ConfigureRequest:
  1231. case Expose:
  1232. case MapRequest:
  1233. handler[ev.type](&ev);
  1234. break;
  1235. case MotionNotify:
  1236. XSync(dpy, False);
  1237. if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
  1238. nw = 1;
  1239. if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
  1240. nh = 1;
  1241. if((m->layout->arrange != floating) && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP))
  1242. togglefloating(NULL);
  1243. if((m->layout->arrange == floating) || c->isfloating)
  1244. resize(c, c->x, c->y, nw, nh, True);
  1245. break;
  1246. }
  1247. }
  1248. }
  1249. void
  1250. restack(Monitor *m) {
  1251. Client *c;
  1252. XEvent ev;
  1253. XWindowChanges wc;
  1254. drawbar(m);
  1255. if(!sel)
  1256. return;
  1257. if(sel->isfloating || (m->layout->arrange == floating))
  1258. XRaiseWindow(dpy, sel->win);
  1259. if(m->layout->arrange != floating) {
  1260. wc.stack_mode = Below;
  1261. wc.sibling = m->barwin;
  1262. if(!sel->isfloating) {
  1263. XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc);
  1264. wc.sibling = sel->win;
  1265. }
  1266. for(c = nexttiled(clients, m->id); c; c = nexttiled(c->next, m->id)) {
  1267. if(c == sel)
  1268. continue;
  1269. XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
  1270. wc.sibling = c->win;
  1271. }
  1272. }
  1273. XSync(dpy, False);
  1274. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1275. }
  1276. void
  1277. run(void) {
  1278. char *p;
  1279. char buf[sizeof stext];
  1280. fd_set rd;
  1281. int r, xfd;
  1282. unsigned int len, offset;
  1283. XEvent ev;
  1284. /* main event loop, also reads status text from stdin */
  1285. XSync(dpy, False);
  1286. xfd = ConnectionNumber(dpy);
  1287. readin = True;
  1288. offset = 0;
  1289. len = sizeof stext - 1;
  1290. buf[len] = stext[len] = '\0'; /* 0-terminator is never touched */
  1291. while(running) {
  1292. FD_ZERO(&rd);
  1293. if(readin)
  1294. FD_SET(STDIN_FILENO, &rd);
  1295. FD_SET(xfd, &rd);
  1296. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1297. if(errno == EINTR)
  1298. continue;
  1299. eprint("select failed\n");
  1300. }
  1301. if(FD_ISSET(STDIN_FILENO, &rd)) {
  1302. switch((r = read(STDIN_FILENO, buf + offset, len - offset))) {
  1303. case -1:
  1304. strncpy(stext, strerror(errno), len);
  1305. readin = False;
  1306. break;
  1307. case 0:
  1308. strncpy(stext, "EOF", 4);
  1309. readin = False;
  1310. break;
  1311. default:
  1312. for(p = buf + offset; r > 0; p++, r--, offset++)
  1313. if(*p == '\n' || *p == '\0') {
  1314. *p = '\0';
  1315. strncpy(stext, buf, len);
  1316. p += r - 1; /* p is buf + offset + r - 1 */
  1317. for(r = 0; *(p - r) && *(p - r) != '\n'; r++);
  1318. offset = r;
  1319. if(r)
  1320. memmove(buf, p - r + 1, r);
  1321. break;
  1322. }
  1323. break;
  1324. }
  1325. drawbar(&monitors[selmonitor]);
  1326. }
  1327. while(XPending(dpy)) {
  1328. XNextEvent(dpy, &ev);
  1329. if(handler[ev.type])
  1330. (handler[ev.type])(&ev); /* call handler */
  1331. }
  1332. }
  1333. }
  1334. void
  1335. scan(void) {
  1336. unsigned int i, num;
  1337. Window *wins, d1, d2;
  1338. XWindowAttributes wa;
  1339. wins = NULL;
  1340. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1341. for(i = 0; i < num; i++) {
  1342. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1343. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1344. continue;
  1345. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1346. manage(wins[i], &wa);
  1347. }
  1348. for(i = 0; i < num; i++) { /* now the transients */
  1349. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1350. continue;
  1351. if(XGetTransientForHint(dpy, wins[i], &d1)
  1352. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1353. manage(wins[i], &wa);
  1354. }
  1355. }
  1356. if(wins)
  1357. XFree(wins);
  1358. }
  1359. void
  1360. setclientstate(Client *c, long state) {
  1361. long data[] = {state, None};
  1362. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1363. PropModeReplace, (unsigned char *)data, 2);
  1364. }
  1365. void
  1366. setlayout(const char *arg) {
  1367. unsigned int i;
  1368. Monitor *m = &monitors[monitorat()];
  1369. if(!arg) {
  1370. m->layout++;
  1371. if(m->layout == &layouts[LENGTH(layouts)])
  1372. m->layout = &layouts[0];
  1373. }
  1374. else {
  1375. for(i = 0; i < LENGTH(layouts); i++)
  1376. if(!strcmp(arg, layouts[i].symbol))
  1377. break;
  1378. if(i == LENGTH(layouts))
  1379. return;
  1380. m->layout = &layouts[i];
  1381. }
  1382. if(sel)
  1383. arrange();
  1384. else
  1385. drawbar(m);
  1386. }
  1387. void
  1388. setmwfact(const char *arg) {
  1389. double delta;
  1390. Monitor *m = &monitors[monitorat()];
  1391. if(!domwfact)
  1392. return;
  1393. /* arg handling, manipulate mwfact */
  1394. if(arg == NULL)
  1395. m->mwfact = MWFACT;
  1396. else if(sscanf(arg, "%lf", &delta) == 1) {
  1397. if(arg[0] == '+' || arg[0] == '-')
  1398. m->mwfact += delta;
  1399. else
  1400. m->mwfact = delta;
  1401. if(m->mwfact < 0.1)
  1402. m->mwfact = 0.1;
  1403. else if(m->mwfact > 0.9)
  1404. m->mwfact = 0.9;
  1405. }
  1406. arrange();
  1407. }
  1408. void
  1409. setup(void) {
  1410. unsigned int i;
  1411. Monitor *m;
  1412. XSetWindowAttributes wa;
  1413. XineramaScreenInfo *info = NULL;
  1414. /* init atoms */
  1415. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1416. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1417. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1418. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1419. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1420. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1421. /* init cursors */
  1422. wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1423. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1424. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1425. // init screens/monitors first
  1426. mcount = 1;
  1427. if((isxinerama = XineramaIsActive(dpy)))
  1428. info = XineramaQueryScreens(dpy, &mcount);
  1429. monitors = emallocz(mcount * sizeof(Monitor));
  1430. screen = DefaultScreen(dpy);
  1431. root = RootWindow(dpy, screen);
  1432. /* init appearance */
  1433. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  1434. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  1435. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  1436. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  1437. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  1438. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  1439. initfont(FONT);
  1440. dc.h = bh = dc.font.height + 2;
  1441. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1442. dc.gc = XCreateGC(dpy, root, 0, 0);
  1443. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1444. if(!dc.font.set)
  1445. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1446. for(blw = i = 0; i < LENGTH(layouts); i++) {
  1447. i = textw(layouts[i].symbol);
  1448. if(i > blw)
  1449. blw = i;
  1450. }
  1451. for(i = 0; i < mcount; i++) {
  1452. /* init geometry */
  1453. m = &monitors[i];
  1454. m->id = i;
  1455. if (mcount != 1 && isxinerama) {
  1456. m->sx = info[i].x_org;
  1457. m->sy = info[i].y_org;
  1458. m->sw = info[i].width;
  1459. m->sh = info[i].height;
  1460. fprintf(stderr, "monitor[%d]: %d,%d,%d,%d\n", i, m->sx, m->sy, m->sw, m->sh);
  1461. }
  1462. else {
  1463. m->sx = 0;
  1464. m->sy = 0;
  1465. m->sw = DisplayWidth(dpy, screen);
  1466. m->sh = DisplayHeight(dpy, screen);
  1467. }
  1468. m->seltags = emallocz(sizeof initags);
  1469. m->prevtags = emallocz(sizeof initags);
  1470. memcpy(m->seltags, initags, sizeof initags);
  1471. memcpy(m->prevtags, initags, sizeof initags);
  1472. /* init layouts */
  1473. m->mwfact = MWFACT;
  1474. m->layout = &layouts[0];
  1475. // TODO: bpos per screen?
  1476. bpos = BARPOS;
  1477. wa.override_redirect = 1;
  1478. wa.background_pixmap = ParentRelative;
  1479. wa.event_mask = ButtonPressMask | ExposureMask;
  1480. /* init bars */
  1481. m->barwin = XCreateWindow(dpy, root, m->sx, m->sy, m->sw, bh, 0,
  1482. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  1483. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  1484. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1485. updatebarpos(m);
  1486. XMapRaised(dpy, m->barwin);
  1487. strcpy(stext, "dwm-"VERSION);
  1488. /* EWMH support per monitor */
  1489. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1490. PropModeReplace, (unsigned char *) netatom, NetLast);
  1491. /* select for events */
  1492. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  1493. | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
  1494. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  1495. XSelectInput(dpy, root, wa.event_mask);
  1496. drawbar(m);
  1497. }
  1498. if(info)
  1499. XFree(info);
  1500. /* grab keys */
  1501. grabkeys();
  1502. /* init tags */
  1503. compileregs();
  1504. selmonitor = monitorat();
  1505. fprintf(stderr, "selmonitor == %d\n", selmonitor);
  1506. }
  1507. void
  1508. spawn(const char *arg) {
  1509. static char *shell = NULL;
  1510. if(!shell && !(shell = getenv("SHELL")))
  1511. shell = "/bin/sh";
  1512. if(!arg)
  1513. return;
  1514. /* The double-fork construct avoids zombie processes and keeps the code
  1515. * clean from stupid signal handlers. */
  1516. if(fork() == 0) {
  1517. if(fork() == 0) {
  1518. if(dpy)
  1519. close(ConnectionNumber(dpy));
  1520. setsid();
  1521. execl(shell, shell, "-c", arg, (char *)NULL);
  1522. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
  1523. perror(" failed");
  1524. }
  1525. exit(0);
  1526. }
  1527. wait(0);
  1528. }
  1529. void
  1530. tag(const char *arg) {
  1531. unsigned int i;
  1532. if(!sel)
  1533. return;
  1534. for(i = 0; i < LENGTH(tags); i++)
  1535. sel->tags[i] = (NULL == arg);
  1536. sel->tags[idxoftag(arg)] = True;
  1537. arrange();
  1538. }
  1539. unsigned int
  1540. textnw(const char *text, unsigned int len) {
  1541. XRectangle r;
  1542. if(dc.font.set) {
  1543. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1544. return r.width;
  1545. }
  1546. return XTextWidth(dc.font.xfont, text, len);
  1547. }
  1548. unsigned int
  1549. textw(const char *text) {
  1550. return textnw(text, strlen(text)) + dc.font.height;
  1551. }
  1552. void
  1553. tile(Monitor *m) {
  1554. unsigned int i, n, nx, ny, nw, nh, mw, th;
  1555. Client *c, *mc;
  1556. domwfact = dozoom = True;
  1557. nx = ny = nw = 0; /* gcc stupidity requires this */
  1558. for(n = 0, c = nexttiled(clients, m->id); c; c = nexttiled(c->next, m->id))
  1559. n++;
  1560. /* window geoms */
  1561. mw = (n == 1) ? m->waw : m->mwfact * m->waw;
  1562. th = (n > 1) ? m->wah / (n - 1) : 0;
  1563. if(n > 1 && th < bh)
  1564. th = m->wah;
  1565. for(i = 0, c = mc = nexttiled(clients, m->id); c; c = nexttiled(c->next, m->id)) {
  1566. if(i == 0) { /* master */
  1567. nx = m->wax;
  1568. ny = m->way;
  1569. nw = mw - 2 * c->border;
  1570. nh = m->wah - 2 * c->border;
  1571. }
  1572. else { /* tile window */
  1573. if(i == 1) {
  1574. ny = m->way;
  1575. nx += mc->w + 2 * mc->border;
  1576. nw = m->waw - mw - 2 * c->border;
  1577. }
  1578. if(i + 1 == n) /* remainder */
  1579. nh = (m->way + m->wah) - ny - 2 * c->border;
  1580. else
  1581. nh = th - 2 * c->border;
  1582. }
  1583. resize(c, nx, ny, nw, nh, RESIZEHINTS);
  1584. if((RESIZEHINTS) && ((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw)))
  1585. /* client doesn't accept size constraints */
  1586. resize(c, nx, ny, nw, nh, False);
  1587. if(n > 1 && th != m->wah)
  1588. ny = c->y + c->h + 2 * c->border;
  1589. i++;
  1590. }
  1591. }
  1592. void
  1593. togglebar(const char *arg) {
  1594. if(bpos == BarOff)
  1595. bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
  1596. else
  1597. bpos = BarOff;
  1598. updatebarpos(&monitors[monitorat()]);
  1599. arrange();
  1600. }
  1601. void
  1602. togglefloating(const char *arg) {
  1603. if(!sel)
  1604. return;
  1605. sel->isfloating = !sel->isfloating;
  1606. if(sel->isfloating)
  1607. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1608. arrange();
  1609. }
  1610. void
  1611. toggletag(const char *arg) {
  1612. unsigned int i, j;
  1613. if(!sel)
  1614. return;
  1615. i = idxoftag(arg);
  1616. sel->tags[i] = !sel->tags[i];
  1617. for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
  1618. if(j == LENGTH(tags))
  1619. sel->tags[i] = True; /* at least one tag must be enabled */
  1620. arrange();
  1621. }
  1622. void
  1623. toggleview(const char *arg) {
  1624. unsigned int i, j;
  1625. Monitor *m = &monitors[monitorat()];
  1626. i = idxoftag(arg);
  1627. m->seltags[i] = !m->seltags[i];
  1628. for(j = 0; j < LENGTH(tags) && !m->seltags[j]; j++);
  1629. if(j == LENGTH(tags))
  1630. m->seltags[i] = True; /* at least one tag must be viewed */
  1631. arrange();
  1632. }
  1633. void
  1634. unban(Client *c) {
  1635. if(!c->isbanned)
  1636. return;
  1637. XMoveWindow(dpy, c->win, c->x, c->y);
  1638. c->isbanned = False;
  1639. }
  1640. void
  1641. unmanage(Client *c) {
  1642. XWindowChanges wc;
  1643. wc.border_width = c->oldborder;
  1644. /* The server grab construct avoids race conditions. */
  1645. XGrabServer(dpy);
  1646. XSetErrorHandler(xerrordummy);
  1647. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1648. detach(c);
  1649. detachstack(c);
  1650. if(sel == c)
  1651. focus(NULL);
  1652. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1653. setclientstate(c, WithdrawnState);
  1654. free(c->tags);
  1655. free(c);
  1656. XSync(dpy, False);
  1657. XSetErrorHandler(xerror);
  1658. XUngrabServer(dpy);
  1659. arrange();
  1660. }
  1661. void
  1662. unmapnotify(XEvent *e) {
  1663. Client *c;
  1664. XUnmapEvent *ev = &e->xunmap;
  1665. if((c = getclient(ev->window)))
  1666. unmanage(c);
  1667. }
  1668. void
  1669. updatebarpos(Monitor *m) {
  1670. XEvent ev;
  1671. m->wax = m->sx;
  1672. m->way = m->sy;
  1673. m->wah = m->sh;
  1674. m->waw = m->sw;
  1675. switch(bpos) {
  1676. default:
  1677. m->wah -= bh;
  1678. m->way += bh;
  1679. XMoveWindow(dpy, m->barwin, m->sx, m->sy);
  1680. break;
  1681. case BarBot:
  1682. m->wah -= bh;
  1683. XMoveWindow(dpy, m->barwin, m->sx, m->sy + m->wah);
  1684. break;
  1685. case BarOff:
  1686. XMoveWindow(dpy, m->barwin, m->sx, m->sy - bh);
  1687. break;
  1688. }
  1689. XSync(dpy, False);
  1690. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1691. }
  1692. void
  1693. updatesizehints(Client *c) {
  1694. long msize;
  1695. XSizeHints size;
  1696. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  1697. size.flags = PSize;
  1698. c->flags = size.flags;
  1699. if(c->flags & PBaseSize) {
  1700. c->basew = size.base_width;
  1701. c->baseh = size.base_height;
  1702. }
  1703. else if(c->flags & PMinSize) {
  1704. c->basew = size.min_width;
  1705. c->baseh = size.min_height;
  1706. }
  1707. else
  1708. c->basew = c->baseh = 0;
  1709. if(c->flags & PResizeInc) {
  1710. c->incw = size.width_inc;
  1711. c->inch = size.height_inc;
  1712. }
  1713. else
  1714. c->incw = c->inch = 0;
  1715. if(c->flags & PMaxSize) {
  1716. c->maxw = size.max_width;
  1717. c->maxh = size.max_height;
  1718. }
  1719. else
  1720. c->maxw = c->maxh = 0;
  1721. if(c->flags & PMinSize) {
  1722. c->minw = size.min_width;
  1723. c->minh = size.min_height;
  1724. }
  1725. else if(c->flags & PBaseSize) {
  1726. c->minw = size.base_width;
  1727. c->minh = size.base_height;
  1728. }
  1729. else
  1730. c->minw = c->minh = 0;
  1731. if(c->flags & PAspect) {
  1732. c->minax = size.min_aspect.x;
  1733. c->maxax = size.max_aspect.x;
  1734. c->minay = size.min_aspect.y;
  1735. c->maxay = size.max_aspect.y;
  1736. }
  1737. else
  1738. c->minax = c->maxax = c->minay = c->maxay = 0;
  1739. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1740. && c->maxw == c->minw && c->maxh == c->minh);
  1741. }
  1742. void
  1743. updatetitle(Client *c) {
  1744. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1745. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1746. }
  1747. void
  1748. updatewmhints(Client *c) {
  1749. XWMHints *wmh;
  1750. if((wmh = XGetWMHints(dpy, c->win))) {
  1751. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1752. XFree(wmh);
  1753. }
  1754. }
  1755. /* There's no way to check accesses to destroyed windows, thus those cases are
  1756. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1757. * default error handler, which may call exit. */
  1758. int
  1759. xerror(Display *dpy, XErrorEvent *ee) {
  1760. if(ee->error_code == BadWindow
  1761. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1762. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1763. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1764. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1765. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1766. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1767. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1768. return 0;
  1769. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1770. ee->request_code, ee->error_code);
  1771. return xerrorxlib(dpy, ee); /* may call exit */
  1772. }
  1773. int
  1774. xerrordummy(Display *dsply, XErrorEvent *ee) {
  1775. return 0;
  1776. }
  1777. /* Startup Error handler to check if another window manager
  1778. * is already running. */
  1779. int
  1780. xerrorstart(Display *dsply, XErrorEvent *ee) {
  1781. otherwm = True;
  1782. return -1;
  1783. }
  1784. void
  1785. view(const char *arg) {
  1786. unsigned int i;
  1787. Bool tmp[LENGTH(tags)];
  1788. Monitor *m = &monitors[monitorat()];
  1789. for(i = 0; i < LENGTH(tags); i++)
  1790. tmp[i] = (NULL == arg);
  1791. tmp[idxoftag(arg)] = True;
  1792. if(memcmp(m->seltags, tmp, sizeof initags) != 0) {
  1793. memcpy(m->prevtags, m->seltags, sizeof initags);
  1794. memcpy(m->seltags, tmp, sizeof initags);
  1795. arrange();
  1796. }
  1797. }
  1798. void
  1799. viewprevtag(const char *arg) {
  1800. static Bool tmp[LENGTH(tags)];
  1801. Monitor *m = &monitors[monitorat()];
  1802. memcpy(tmp, m->seltags, sizeof initags);
  1803. memcpy(m->seltags, m->prevtags, sizeof initags);
  1804. memcpy(m->prevtags, tmp, sizeof initags);
  1805. arrange();
  1806. }
  1807. void
  1808. zoom(const char *arg) {
  1809. Client *c = sel;
  1810. if(!sel || !dozoom || sel->isfloating)
  1811. return;
  1812. if(c == nexttiled(clients, c->monitor))
  1813. if(!(c = nexttiled(c->next, c->monitor)))
  1814. return;
  1815. detach(c);
  1816. attach(c);
  1817. focus(c);
  1818. arrange();
  1819. }
  1820. void
  1821. movetomonitor(const char *arg) {
  1822. if (sel) {
  1823. sel->monitor = arg ? atoi(arg) : (sel->monitor+1) % mcount;
  1824. memcpy(sel->tags, monitors[sel->monitor].seltags, sizeof initags);
  1825. resize(sel, monitors[sel->monitor].wax, monitors[sel->monitor].way, sel->w, sel->h, True);
  1826. arrange();
  1827. }
  1828. }
  1829. void
  1830. selectmonitor(const char *arg) {
  1831. Monitor *m = &monitors[arg ? atoi(arg) : (monitorat()+1) % mcount];
  1832. XWarpPointer(dpy, None, root, 0, 0, 0, 0, m->wax+m->waw/2, m->way+m->wah/2);
  1833. focus(NULL);
  1834. }
  1835. int
  1836. main(int argc, char *argv[]) {
  1837. if(argc == 2 && !strcmp("-v", argv[1]))
  1838. eprint("dwm-"VERSION", © 2006-2008 Anselm R. Garbe, Sander van Dijk, "
  1839. "Jukka Salmi, Premysl Hruby, Szabolcs Nagy, Christof Musik\n");
  1840. else if(argc != 1)
  1841. eprint("usage: dwm [-v]\n");
  1842. setlocale(LC_CTYPE, "");
  1843. if(!(dpy = XOpenDisplay(0)))
  1844. eprint("dwm: cannot open display\n");
  1845. checkotherwm();
  1846. setup();
  1847. scan();
  1848. run();
  1849. cleanup();
  1850. XCloseDisplay(dpy);
  1851. return 0;
  1852. }