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.

2020 lines
49 KiB

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