Configuration of dwm for Mac Computers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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