Configuration file for DWM on MacBook Air
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.

2084 lines
49 KiB

17 years ago
17 years ago
16 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. monitors[i].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. fprintf(stderr, "resize %d %d %d %d (%d %d %d %d)\n", x, y , w, h, m->sx, m->sy, m->sw, m->sh);
  1201. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  1202. c->x = wc.x = x;
  1203. c->y = wc.y = y;
  1204. c->w = wc.width = w;
  1205. c->h = wc.height = h;
  1206. wc.border_width = c->border;
  1207. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  1208. configure(c);
  1209. XSync(dpy, False);
  1210. }
  1211. }
  1212. void
  1213. resizemouse(Client *c) {
  1214. int ocx, ocy;
  1215. int nw, nh;
  1216. Monitor *m;
  1217. XEvent ev;
  1218. ocx = c->x;
  1219. ocy = c->y;
  1220. m = c->monitor;
  1221. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1222. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1223. return;
  1224. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
  1225. for(;;) {
  1226. XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
  1227. switch(ev.type) {
  1228. case ButtonRelease:
  1229. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
  1230. c->w + c->border - 1, c->h + c->border - 1);
  1231. XUngrabPointer(dpy, CurrentTime);
  1232. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1233. return;
  1234. case ConfigureRequest:
  1235. case Expose:
  1236. case MapRequest:
  1237. handler[ev.type](&ev);
  1238. break;
  1239. case MotionNotify:
  1240. XSync(dpy, False);
  1241. if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
  1242. nw = 1;
  1243. if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
  1244. nh = 1;
  1245. if(!c->isfloating && (m->layout->arrange != floating) && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP))
  1246. togglefloating(NULL);
  1247. if((m->layout->arrange == floating) || c->isfloating)
  1248. resize(c, c->x, c->y, nw, nh, True);
  1249. break;
  1250. }
  1251. }
  1252. }
  1253. void
  1254. restack(Monitor *m) {
  1255. Client *c;
  1256. XEvent ev;
  1257. XWindowChanges wc;
  1258. drawbar(m);
  1259. if(!sel)
  1260. return;
  1261. if(sel->isfloating || (m->layout->arrange == floating))
  1262. XRaiseWindow(dpy, sel->win);
  1263. if(m->layout->arrange != floating) {
  1264. wc.stack_mode = Below;
  1265. wc.sibling = m->barwin;
  1266. if(!sel->isfloating) {
  1267. XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc);
  1268. wc.sibling = sel->win;
  1269. }
  1270. for(c = nexttiled(clients, m); c; c = nexttiled(c->next, m)) {
  1271. if(c == sel)
  1272. continue;
  1273. XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
  1274. wc.sibling = c->win;
  1275. }
  1276. }
  1277. XSync(dpy, False);
  1278. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1279. }
  1280. void
  1281. run(void) {
  1282. char *p;
  1283. char buf[sizeof stext];
  1284. fd_set rd;
  1285. int r, xfd;
  1286. unsigned int len, offset;
  1287. XEvent ev;
  1288. /* main event loop, also reads status text from stdin */
  1289. XSync(dpy, False);
  1290. xfd = ConnectionNumber(dpy);
  1291. readin = True;
  1292. offset = 0;
  1293. len = sizeof stext - 1;
  1294. buf[len] = stext[len] = '\0'; /* 0-terminator is never touched */
  1295. while(running) {
  1296. FD_ZERO(&rd);
  1297. if(readin)
  1298. FD_SET(STDIN_FILENO, &rd);
  1299. FD_SET(xfd, &rd);
  1300. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1301. if(errno == EINTR)
  1302. continue;
  1303. eprint("select failed\n");
  1304. }
  1305. if(FD_ISSET(STDIN_FILENO, &rd)) {
  1306. switch((r = read(STDIN_FILENO, buf + offset, len - offset))) {
  1307. case -1:
  1308. strncpy(stext, strerror(errno), len);
  1309. readin = False;
  1310. break;
  1311. case 0:
  1312. strncpy(stext, "EOF", 4);
  1313. readin = False;
  1314. break;
  1315. default:
  1316. for(p = buf + offset; r > 0; p++, r--, offset++)
  1317. if(*p == '\n' || *p == '\0') {
  1318. *p = '\0';
  1319. strncpy(stext, buf, len);
  1320. p += r - 1; /* p is buf + offset + r - 1 */
  1321. for(r = 0; *(p - r) && *(p - r) != '\n'; r++);
  1322. offset = r;
  1323. if(r)
  1324. memmove(buf, p - r + 1, r);
  1325. break;
  1326. }
  1327. break;
  1328. }
  1329. drawbar(selmonitor);
  1330. }
  1331. while(XPending(dpy)) {
  1332. XNextEvent(dpy, &ev);
  1333. if(handler[ev.type])
  1334. (handler[ev.type])(&ev); /* call handler */
  1335. }
  1336. }
  1337. }
  1338. void
  1339. scan(void) {
  1340. unsigned int i, num;
  1341. Window *wins, d1, d2;
  1342. XWindowAttributes wa;
  1343. wins = NULL;
  1344. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1345. for(i = 0; i < num; i++) {
  1346. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1347. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1348. continue;
  1349. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1350. manage(wins[i], &wa);
  1351. }
  1352. for(i = 0; i < num; i++) { /* now the transients */
  1353. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1354. continue;
  1355. if(XGetTransientForHint(dpy, wins[i], &d1)
  1356. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1357. manage(wins[i], &wa);
  1358. }
  1359. }
  1360. if(wins)
  1361. XFree(wins);
  1362. }
  1363. void
  1364. setclientstate(Client *c, long state) {
  1365. long data[] = {state, None};
  1366. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1367. PropModeReplace, (unsigned char *)data, 2);
  1368. }
  1369. void
  1370. setlayout(const char *arg) {
  1371. unsigned int i;
  1372. Monitor *m = monitorat();
  1373. if(!arg) {
  1374. m->layout++;
  1375. if(m->layout == &layouts[LENGTH(layouts)])
  1376. m->layout = &layouts[0];
  1377. }
  1378. else {
  1379. for(i = 0; i < LENGTH(layouts); i++)
  1380. if(!strcmp(arg, layouts[i].symbol))
  1381. break;
  1382. if(i == LENGTH(layouts))
  1383. return;
  1384. m->layout = &layouts[i];
  1385. }
  1386. if(sel)
  1387. arrange(m);
  1388. else
  1389. drawbar(m);
  1390. }
  1391. void
  1392. setmwfact(const char *arg) {
  1393. double delta;
  1394. Monitor *m = monitorat();
  1395. if(!domwfact)
  1396. return;
  1397. /* arg handling, manipulate mwfact */
  1398. if(arg == NULL)
  1399. m->mwfact = MWFACT;
  1400. else if(sscanf(arg, "%lf", &delta) == 1) {
  1401. if(arg[0] == '+' || arg[0] == '-')
  1402. m->mwfact += delta;
  1403. else
  1404. m->mwfact = delta;
  1405. if(m->mwfact < 0.1)
  1406. m->mwfact = 0.1;
  1407. else if(m->mwfact > 0.9)
  1408. m->mwfact = 0.9;
  1409. }
  1410. arrange(m);
  1411. }
  1412. void
  1413. setup(void) {
  1414. unsigned int i;
  1415. Monitor *m;
  1416. XSetWindowAttributes wa;
  1417. XineramaScreenInfo *info = NULL;
  1418. /* init atoms */
  1419. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1420. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1421. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1422. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1423. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1424. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1425. /* init cursors */
  1426. wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1427. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1428. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1429. // init screens/monitors first
  1430. if((isxinerama = XineramaIsActive(dpy)))
  1431. info = XineramaQueryScreens(dpy, &mcount);
  1432. selmonitor = monitors = emallocz(mcount * sizeof(Monitor));
  1433. screen = DefaultScreen(dpy);
  1434. root = RootWindow(dpy, screen);
  1435. /* init appearance */
  1436. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  1437. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  1438. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  1439. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  1440. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  1441. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  1442. initfont(FONT);
  1443. dc.h = bh = dc.font.height + 2;
  1444. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1445. dc.gc = XCreateGC(dpy, root, 0, 0);
  1446. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1447. if(!dc.font.set)
  1448. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1449. for(blw = i = 0; i < LENGTH(layouts); i++) {
  1450. i = textw(layouts[i].symbol);
  1451. if(i > blw)
  1452. blw = i;
  1453. }
  1454. for(i = 0; i < mcount; i++) {
  1455. /* init geometry */
  1456. m = &monitors[i];
  1457. m->id = i;
  1458. if(mcount != 1 && isxinerama) {
  1459. m->sx = info[i].x_org;
  1460. m->sy = info[i].y_org;
  1461. m->sw = info[i].width;
  1462. m->sh = info[i].height;
  1463. fprintf(stderr, "monitor[%d]: %d,%d,%d,%d\n", i, m->sx, m->sy, m->sw, m->sh);
  1464. }
  1465. else {
  1466. m->sx = 0;
  1467. m->sy = 0;
  1468. m->sw = DisplayWidth(dpy, screen);
  1469. m->sh = DisplayHeight(dpy, screen);
  1470. }
  1471. m->seltags = emallocz(sizeof initags);
  1472. m->prevtags = emallocz(sizeof initags);
  1473. memcpy(m->seltags, initags, sizeof initags);
  1474. memcpy(m->prevtags, initags, sizeof initags);
  1475. /* init layouts */
  1476. m->mwfact = MWFACT;
  1477. m->layout = &layouts[0];
  1478. // TODO: bpos per screen?
  1479. bpos = BARPOS;
  1480. wa.override_redirect = 1;
  1481. wa.background_pixmap = ParentRelative;
  1482. wa.event_mask = ButtonPressMask | ExposureMask;
  1483. /* init bars */
  1484. m->barwin = XCreateWindow(dpy, root, m->sx, m->sy, m->sw, bh, 0,
  1485. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  1486. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  1487. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1488. updatebarpos(m);
  1489. XMapRaised(dpy, m->barwin);
  1490. strcpy(stext, "dwm-"VERSION);
  1491. /* EWMH support per monitor */
  1492. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1493. PropModeReplace, (unsigned char *) netatom, NetLast);
  1494. /* select for events */
  1495. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  1496. | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
  1497. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  1498. XSelectInput(dpy, root, wa.event_mask);
  1499. drawbar(m);
  1500. }
  1501. if(info)
  1502. XFree(info);
  1503. /* grab keys */
  1504. grabkeys();
  1505. /* init tags */
  1506. compileregs();
  1507. selmonitor = monitorat();
  1508. fprintf(stderr, "selmonitor == %d\n", selmonitor - monitors);
  1509. }
  1510. void
  1511. spawn(const char *arg) {
  1512. static char *shell = NULL;
  1513. if(!shell && !(shell = getenv("SHELL")))
  1514. shell = "/bin/sh";
  1515. if(!arg)
  1516. return;
  1517. /* The double-fork construct avoids zombie processes and keeps the code
  1518. * clean from stupid signal handlers. */
  1519. if(fork() == 0) {
  1520. if(fork() == 0) {
  1521. if(dpy)
  1522. close(ConnectionNumber(dpy));
  1523. setsid();
  1524. execl(shell, shell, "-c", arg, (char *)NULL);
  1525. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
  1526. perror(" failed");
  1527. }
  1528. exit(0);
  1529. }
  1530. wait(0);
  1531. }
  1532. void
  1533. tag(const char *arg) {
  1534. unsigned int i;
  1535. if(!sel)
  1536. return;
  1537. for(i = 0; i < LENGTH(tags); i++)
  1538. sel->tags[i] = (NULL == arg);
  1539. sel->tags[idxoftag(arg)] = True;
  1540. arrange(sel->monitor);
  1541. }
  1542. unsigned int
  1543. textnw(const char *text, unsigned int len) {
  1544. XRectangle r;
  1545. if(dc.font.set) {
  1546. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1547. return r.width;
  1548. }
  1549. return XTextWidth(dc.font.xfont, text, len);
  1550. }
  1551. unsigned int
  1552. textw(const char *text) {
  1553. return textnw(text, strlen(text)) + dc.font.height;
  1554. }
  1555. void
  1556. tile(Monitor *m) {
  1557. unsigned int i, n, nx, ny, nw, nh, mw, th;
  1558. Client *c, *mc;
  1559. domwfact = dozoom = True;
  1560. nx = m->wax;
  1561. ny = m->way;
  1562. nw = 0;
  1563. for(n = 0, c = nexttiled(clients, m); c; c = nexttiled(c->next, m))
  1564. n++;
  1565. /* window geoms */
  1566. mw = (n == 1) ? m->waw : m->mwfact * m->waw;
  1567. th = (n > 1) ? m->wah / (n - 1) : 0;
  1568. if(n > 1 && th < bh)
  1569. th = m->wah;
  1570. for(i = 0, c = mc = nexttiled(clients, m); c; c = nexttiled(c->next, m)) {
  1571. if(i == 0) { /* master */
  1572. nx = m->wax;
  1573. ny = m->way;
  1574. nw = mw - 2 * c->border;
  1575. nh = m->wah - 2 * c->border;
  1576. }
  1577. else { /* tile window */
  1578. if(i == 1) {
  1579. ny = m->way;
  1580. nx += mc->w + 2 * mc->border;
  1581. nw = m->waw - mw - 2 * c->border;
  1582. }
  1583. if(i + 1 == n) /* remainder */
  1584. nh = (m->way + m->wah) - ny - 2 * c->border;
  1585. else
  1586. nh = th - 2 * c->border;
  1587. }
  1588. fprintf(stderr, "tile %d %d %d %d\n", nx, ny, nw, nh);
  1589. resize(c, nx, ny, nw, nh, RESIZEHINTS);
  1590. if((RESIZEHINTS) && ((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw)))
  1591. /* client doesn't accept size constraints */
  1592. resize(c, nx, ny, nw, nh, False);
  1593. if(n > 1 && th != m->wah)
  1594. ny = c->y + c->h + 2 * c->border;
  1595. i++;
  1596. }
  1597. }
  1598. void
  1599. togglebar(const char *arg) {
  1600. if(bpos == BarOff)
  1601. bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
  1602. else
  1603. bpos = BarOff;
  1604. updatebarpos(monitorat());
  1605. arrange(monitorat());
  1606. }
  1607. void
  1608. togglefloating(const char *arg) {
  1609. if(!sel)
  1610. return;
  1611. sel->isfloating = !sel->isfloating;
  1612. if(sel->isfloating)
  1613. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1614. arrange(sel->monitor);
  1615. }
  1616. void
  1617. toggletag(const char *arg) {
  1618. unsigned int i, j;
  1619. if(!sel)
  1620. return;
  1621. i = idxoftag(arg);
  1622. sel->tags[i] = !sel->tags[i];
  1623. for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
  1624. if(j == LENGTH(tags))
  1625. sel->tags[i] = True; /* at least one tag must be enabled */
  1626. arrange(sel->monitor);
  1627. }
  1628. void
  1629. toggleview(const char *arg) {
  1630. unsigned int i, j;
  1631. Monitor *m = monitorat();
  1632. i = idxoftag(arg);
  1633. m->seltags[i] = !m->seltags[i];
  1634. for(j = 0; j < LENGTH(tags) && !m->seltags[j]; j++);
  1635. if(j == LENGTH(tags))
  1636. m->seltags[i] = True; /* at least one tag must be viewed */
  1637. arrange(m);
  1638. }
  1639. void
  1640. unban(Client *c) {
  1641. if(!c->isbanned)
  1642. return;
  1643. XMoveWindow(dpy, c->win, c->x, c->y);
  1644. c->isbanned = False;
  1645. }
  1646. void
  1647. unmanage(Client *c) {
  1648. Monitor *m = c->monitor;
  1649. XWindowChanges wc;
  1650. wc.border_width = c->oldborder;
  1651. /* The server grab construct avoids race conditions. */
  1652. XGrabServer(dpy);
  1653. XSetErrorHandler(xerrordummy);
  1654. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1655. detach(c);
  1656. detachstack(c);
  1657. if(sel == c)
  1658. focus(NULL);
  1659. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1660. setclientstate(c, WithdrawnState);
  1661. free(c->tags);
  1662. free(c);
  1663. XSync(dpy, False);
  1664. XSetErrorHandler(xerror);
  1665. XUngrabServer(dpy);
  1666. arrange(m);
  1667. }
  1668. void
  1669. unmapnotify(XEvent *e) {
  1670. Client *c;
  1671. XUnmapEvent *ev = &e->xunmap;
  1672. if((c = getclient(ev->window)))
  1673. unmanage(c);
  1674. }
  1675. void
  1676. updatebarpos(Monitor *m) {
  1677. XEvent ev;
  1678. m->wax = m->sx;
  1679. m->way = m->sy;
  1680. m->wah = m->sh;
  1681. m->waw = m->sw;
  1682. switch(bpos) {
  1683. default:
  1684. m->wah -= bh;
  1685. m->way += bh;
  1686. XMoveWindow(dpy, m->barwin, m->sx, m->sy);
  1687. break;
  1688. case BarBot:
  1689. m->wah -= bh;
  1690. XMoveWindow(dpy, m->barwin, m->sx, m->sy + m->wah);
  1691. break;
  1692. case BarOff:
  1693. XMoveWindow(dpy, m->barwin, m->sx, m->sy - bh);
  1694. break;
  1695. }
  1696. XSync(dpy, False);
  1697. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1698. }
  1699. void
  1700. updatesizehints(Client *c) {
  1701. long msize;
  1702. XSizeHints size;
  1703. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  1704. size.flags = PSize;
  1705. c->flags = size.flags;
  1706. if(c->flags & PBaseSize) {
  1707. c->basew = size.base_width;
  1708. c->baseh = size.base_height;
  1709. }
  1710. else if(c->flags & PMinSize) {
  1711. c->basew = size.min_width;
  1712. c->baseh = size.min_height;
  1713. }
  1714. else
  1715. c->basew = c->baseh = 0;
  1716. if(c->flags & PResizeInc) {
  1717. c->incw = size.width_inc;
  1718. c->inch = size.height_inc;
  1719. }
  1720. else
  1721. c->incw = c->inch = 0;
  1722. if(c->flags & PMaxSize) {
  1723. c->maxw = size.max_width;
  1724. c->maxh = size.max_height;
  1725. }
  1726. else
  1727. c->maxw = c->maxh = 0;
  1728. if(c->flags & PMinSize) {
  1729. c->minw = size.min_width;
  1730. c->minh = size.min_height;
  1731. }
  1732. else if(c->flags & PBaseSize) {
  1733. c->minw = size.base_width;
  1734. c->minh = size.base_height;
  1735. }
  1736. else
  1737. c->minw = c->minh = 0;
  1738. if(c->flags & PAspect) {
  1739. c->minax = size.min_aspect.x;
  1740. c->maxax = size.max_aspect.x;
  1741. c->minay = size.min_aspect.y;
  1742. c->maxay = size.max_aspect.y;
  1743. }
  1744. else
  1745. c->minax = c->maxax = c->minay = c->maxay = 0;
  1746. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1747. && c->maxw == c->minw && c->maxh == c->minh);
  1748. }
  1749. void
  1750. updatetitle(Client *c) {
  1751. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1752. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1753. }
  1754. void
  1755. updatewmhints(Client *c) {
  1756. XWMHints *wmh;
  1757. if((wmh = XGetWMHints(dpy, c->win))) {
  1758. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1759. XFree(wmh);
  1760. }
  1761. }
  1762. /* There's no way to check accesses to destroyed windows, thus those cases are
  1763. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1764. * default error handler, which may call exit. */
  1765. int
  1766. xerror(Display *dpy, XErrorEvent *ee) {
  1767. if(ee->error_code == BadWindow
  1768. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1769. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1770. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1771. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1772. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1773. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1774. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1775. return 0;
  1776. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1777. ee->request_code, ee->error_code);
  1778. return xerrorxlib(dpy, ee); /* may call exit */
  1779. }
  1780. int
  1781. xerrordummy(Display *dsply, XErrorEvent *ee) {
  1782. return 0;
  1783. }
  1784. /* Startup Error handler to check if another window manager
  1785. * is already running. */
  1786. int
  1787. xerrorstart(Display *dsply, XErrorEvent *ee) {
  1788. otherwm = True;
  1789. return -1;
  1790. }
  1791. void
  1792. view(const char *arg) {
  1793. unsigned int i;
  1794. Bool tmp[LENGTH(tags)];
  1795. Monitor *m = monitorat();
  1796. for(i = 0; i < LENGTH(tags); i++)
  1797. tmp[i] = (NULL == arg);
  1798. tmp[idxoftag(arg)] = True;
  1799. if(memcmp(m->seltags, tmp, sizeof initags) != 0) {
  1800. memcpy(m->prevtags, m->seltags, sizeof initags);
  1801. memcpy(m->seltags, tmp, sizeof initags);
  1802. arrange(m);
  1803. }
  1804. }
  1805. void
  1806. viewprevtag(const char *arg) {
  1807. static Bool tmp[LENGTH(tags)];
  1808. Monitor *m = monitorat();
  1809. memcpy(tmp, m->seltags, sizeof initags);
  1810. memcpy(m->seltags, m->prevtags, sizeof initags);
  1811. memcpy(m->prevtags, tmp, sizeof initags);
  1812. arrange(m);
  1813. }
  1814. void
  1815. zoom(const char *arg) {
  1816. Client *c = sel;
  1817. if(!sel || !dozoom || sel->isfloating)
  1818. return;
  1819. if(c == nexttiled(clients, c->monitor))
  1820. if(!(c = nexttiled(c->next, c->monitor)))
  1821. return;
  1822. detach(c);
  1823. attach(c);
  1824. focus(c);
  1825. arrange(c->monitor);
  1826. }
  1827. void
  1828. movetomonitor(const char *arg) {
  1829. int i;
  1830. if (sel)
  1831. return;
  1832. if(arg)
  1833. i = atoi(arg);
  1834. else {
  1835. for(i = 0; &monitors[i] != sel->monitor && i < mcount; i++);
  1836. i++;
  1837. }
  1838. sel->monitor = &monitors[i % mcount];
  1839. memcpy(sel->tags, sel->monitor->seltags, sizeof initags);
  1840. resize(sel, sel->monitor->wax, sel->monitor->way, sel->w, sel->h, True);
  1841. arrange(sel->monitor);
  1842. }
  1843. void
  1844. selectmonitor(const char *arg) {
  1845. int i;
  1846. Monitor *m;
  1847. if(arg)
  1848. i = atoi(arg);
  1849. else {
  1850. for(i = 0; &monitors[i] != sel->monitor && i < mcount; i++);
  1851. i++;
  1852. }
  1853. m = &monitors[i % mcount];
  1854. XWarpPointer(dpy, None, root, 0, 0, 0, 0, m->wax+m->waw/2, m->way+m->wah/2);
  1855. focus(NULL);
  1856. }
  1857. int
  1858. main(int argc, char *argv[]) {
  1859. if(argc == 2 && !strcmp("-v", argv[1]))
  1860. eprint("dwm-"VERSION", © 2006-2008 Anselm R. Garbe, Sander van Dijk, "
  1861. "Jukka Salmi, Premysl Hruby, Szabolcs Nagy, Christof Musik\n");
  1862. else if(argc != 1)
  1863. eprint("usage: dwm [-v]\n");
  1864. setlocale(LC_CTYPE, "");
  1865. if(!(dpy = XOpenDisplay(0)))
  1866. eprint("dwm: cannot open display\n");
  1867. checkotherwm();
  1868. setup();
  1869. scan();
  1870. run();
  1871. cleanup();
  1872. XCloseDisplay(dpy);
  1873. return 0;
  1874. }