Configuration file for DWM on MacBook Air
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2126 lines
52 KiB

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