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.

2148 lines
53 KiB

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