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

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