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.

2156 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. unfocus(selmon->sel, True);
  1111. selmon = m;
  1112. focus(NULL);
  1113. }
  1114. mon = m;
  1115. }
  1116. void
  1117. movemouse(const Arg *arg) {
  1118. int x, y, ocx, ocy, nx, ny;
  1119. Client *c;
  1120. Monitor *m;
  1121. XEvent ev;
  1122. if(!(c = selmon->sel))
  1123. return;
  1124. restack(selmon);
  1125. ocx = c->x;
  1126. ocy = c->y;
  1127. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1128. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1129. return;
  1130. if(!getrootptr(&x, &y))
  1131. return;
  1132. do {
  1133. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1134. switch(ev.type) {
  1135. case ConfigureRequest:
  1136. case Expose:
  1137. case MapRequest:
  1138. handler[ev.type](&ev);
  1139. break;
  1140. case MotionNotify:
  1141. nx = ocx + (ev.xmotion.x - x);
  1142. ny = ocy + (ev.xmotion.y - y);
  1143. if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1144. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1145. if(abs(selmon->wx - nx) < snap)
  1146. nx = selmon->wx;
  1147. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1148. nx = selmon->wx + selmon->ww - WIDTH(c);
  1149. if(abs(selmon->wy - ny) < snap)
  1150. ny = selmon->wy;
  1151. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1152. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1153. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1154. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1155. togglefloating(NULL);
  1156. }
  1157. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating) {
  1158. if(c->isfullscreen)
  1159. setfullscreen(c, False);
  1160. resize(c, nx, ny, c->w, c->h, True);
  1161. }
  1162. break;
  1163. }
  1164. } while(ev.type != ButtonRelease);
  1165. XUngrabPointer(dpy, CurrentTime);
  1166. if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1167. sendmon(c, m);
  1168. selmon = m;
  1169. focus(NULL);
  1170. }
  1171. }
  1172. Client *
  1173. nexttiled(Client *c) {
  1174. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1175. return c;
  1176. }
  1177. void
  1178. pop(Client *c) {
  1179. detach(c);
  1180. attach(c);
  1181. focus(c);
  1182. arrange(c->mon);
  1183. }
  1184. void
  1185. propertynotify(XEvent *e) {
  1186. Client *c;
  1187. Window trans;
  1188. XPropertyEvent *ev = &e->xproperty;
  1189. if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1190. updatestatus();
  1191. else if(ev->state == PropertyDelete)
  1192. return; /* ignore */
  1193. else if((c = wintoclient(ev->window))) {
  1194. switch(ev->atom) {
  1195. default: break;
  1196. case XA_WM_TRANSIENT_FOR:
  1197. if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
  1198. (c->isfloating = (wintoclient(trans)) != NULL))
  1199. arrange(c->mon);
  1200. break;
  1201. case XA_WM_NORMAL_HINTS:
  1202. updatesizehints(c);
  1203. break;
  1204. case XA_WM_HINTS:
  1205. updatewmhints(c);
  1206. drawbars();
  1207. break;
  1208. }
  1209. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1210. updatetitle(c);
  1211. if(c == c->mon->sel)
  1212. drawbar(c->mon);
  1213. }
  1214. if(ev->atom == netatom[NetWMWindowType])
  1215. updatewindowtype(c);
  1216. }
  1217. }
  1218. void
  1219. quit(const Arg *arg) {
  1220. running = False;
  1221. }
  1222. Monitor *
  1223. recttomon(int x, int y, int w, int h) {
  1224. Monitor *m, *r = selmon;
  1225. int a, area = 0;
  1226. for(m = mons; m; m = m->next)
  1227. if((a = INTERSECT(x, y, w, h, m)) > area) {
  1228. area = a;
  1229. r = m;
  1230. }
  1231. return r;
  1232. }
  1233. void
  1234. resize(Client *c, int x, int y, int w, int h, Bool interact) {
  1235. if(applysizehints(c, &x, &y, &w, &h, interact))
  1236. resizeclient(c, x, y, w, h);
  1237. }
  1238. void
  1239. resizeclient(Client *c, int x, int y, int w, int h) {
  1240. XWindowChanges wc;
  1241. c->oldx = c->x; c->x = wc.x = x;
  1242. c->oldy = c->y; c->y = wc.y = y;
  1243. c->oldw = c->w; c->w = wc.width = w;
  1244. c->oldh = c->h; c->h = wc.height = h;
  1245. wc.border_width = c->bw;
  1246. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1247. configure(c);
  1248. XSync(dpy, False);
  1249. }
  1250. void
  1251. resizemouse(const Arg *arg) {
  1252. int ocx, ocy;
  1253. int nw, nh;
  1254. Client *c;
  1255. Monitor *m;
  1256. XEvent ev;
  1257. if(!(c = selmon->sel))
  1258. return;
  1259. restack(selmon);
  1260. ocx = c->x;
  1261. ocy = c->y;
  1262. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1263. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1264. return;
  1265. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1266. do {
  1267. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1268. switch(ev.type) {
  1269. case ConfigureRequest:
  1270. case Expose:
  1271. case MapRequest:
  1272. handler[ev.type](&ev);
  1273. break;
  1274. case MotionNotify:
  1275. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1276. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1277. if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
  1278. && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
  1279. {
  1280. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1281. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1282. togglefloating(NULL);
  1283. }
  1284. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating) {
  1285. if(c->isfullscreen)
  1286. setfullscreen(c, False);
  1287. resize(c, c->x, c->y, nw, nh, True);
  1288. }
  1289. break;
  1290. }
  1291. } while(ev.type != ButtonRelease);
  1292. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1293. XUngrabPointer(dpy, CurrentTime);
  1294. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1295. if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1296. sendmon(c, m);
  1297. selmon = m;
  1298. focus(NULL);
  1299. }
  1300. }
  1301. void
  1302. restack(Monitor *m) {
  1303. Client *c;
  1304. XEvent ev;
  1305. XWindowChanges wc;
  1306. drawbar(m);
  1307. if(!m->sel)
  1308. return;
  1309. if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
  1310. XRaiseWindow(dpy, m->sel->win);
  1311. if(m->lt[m->sellt]->arrange) {
  1312. wc.stack_mode = Below;
  1313. wc.sibling = m->barwin;
  1314. for(c = m->stack; c; c = c->snext)
  1315. if(!c->isfloating && ISVISIBLE(c)) {
  1316. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1317. wc.sibling = c->win;
  1318. }
  1319. }
  1320. XSync(dpy, False);
  1321. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1322. }
  1323. void
  1324. run(void) {
  1325. XEvent ev;
  1326. /* main event loop */
  1327. XSync(dpy, False);
  1328. while(running && !XNextEvent(dpy, &ev))
  1329. if(handler[ev.type])
  1330. handler[ev.type](&ev); /* call handler */
  1331. }
  1332. void
  1333. scan(void) {
  1334. unsigned int i, num;
  1335. Window d1, d2, *wins = NULL;
  1336. XWindowAttributes wa;
  1337. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1338. for(i = 0; i < num; i++) {
  1339. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1340. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1341. continue;
  1342. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1343. manage(wins[i], &wa);
  1344. }
  1345. for(i = 0; i < num; i++) { /* now the transients */
  1346. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1347. continue;
  1348. if(XGetTransientForHint(dpy, wins[i], &d1)
  1349. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1350. manage(wins[i], &wa);
  1351. }
  1352. if(wins)
  1353. XFree(wins);
  1354. }
  1355. }
  1356. void
  1357. sendmon(Client *c, Monitor *m) {
  1358. if(c->mon == m)
  1359. return;
  1360. unfocus(c, True);
  1361. detach(c);
  1362. detachstack(c);
  1363. c->mon = m;
  1364. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1365. attach(c);
  1366. attachstack(c);
  1367. focus(NULL);
  1368. arrange(NULL);
  1369. }
  1370. void
  1371. setclientstate(Client *c, long state) {
  1372. long data[] = { state, None };
  1373. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1374. PropModeReplace, (unsigned char *)data, 2);
  1375. }
  1376. Bool
  1377. sendevent(Client *c, Atom proto) {
  1378. int n;
  1379. Atom *protocols;
  1380. Bool exists = False;
  1381. XEvent ev;
  1382. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  1383. while(!exists && n--)
  1384. exists = protocols[n] == proto;
  1385. XFree(protocols);
  1386. }
  1387. if(exists) {
  1388. ev.type = ClientMessage;
  1389. ev.xclient.window = c->win;
  1390. ev.xclient.message_type = wmatom[WMProtocols];
  1391. ev.xclient.format = 32;
  1392. ev.xclient.data.l[0] = proto;
  1393. ev.xclient.data.l[1] = CurrentTime;
  1394. XSendEvent(dpy, c->win, False, NoEventMask, &ev);
  1395. }
  1396. return exists;
  1397. }
  1398. void
  1399. setfocus(Client *c) {
  1400. if(!c->neverfocus)
  1401. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  1402. sendevent(c, wmatom[WMTakeFocus]);
  1403. }
  1404. void
  1405. setfullscreen(Client *c, Bool fullscreen) {
  1406. if(fullscreen) {
  1407. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1408. PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
  1409. c->isfullscreen = True;
  1410. c->oldstate = c->isfloating;
  1411. c->oldbw = c->bw;
  1412. c->bw = 0;
  1413. c->isfloating = True;
  1414. resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
  1415. XRaiseWindow(dpy, c->win);
  1416. }
  1417. else {
  1418. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1419. PropModeReplace, (unsigned char*)0, 0);
  1420. c->isfullscreen = False;
  1421. c->isfloating = c->oldstate;
  1422. c->bw = c->oldbw;
  1423. c->x = c->oldx;
  1424. c->y = c->oldy;
  1425. c->w = c->oldw;
  1426. c->h = c->oldh;
  1427. resizeclient(c, c->x, c->y, c->w, c->h);
  1428. arrange(c->mon);
  1429. }
  1430. }
  1431. void
  1432. setlayout(const Arg *arg) {
  1433. if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
  1434. selmon->sellt ^= 1;
  1435. if(arg && arg->v)
  1436. selmon->lt[selmon->sellt] = (Layout *)arg->v;
  1437. strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
  1438. if(selmon->sel)
  1439. arrange(selmon);
  1440. else
  1441. drawbar(selmon);
  1442. }
  1443. /* arg > 1.0 will set mfact absolutly */
  1444. void
  1445. setmfact(const Arg *arg) {
  1446. float f;
  1447. if(!arg || !selmon->lt[selmon->sellt]->arrange)
  1448. return;
  1449. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1450. if(f < 0.1 || f > 0.9)
  1451. return;
  1452. selmon->mfact = f;
  1453. arrange(selmon);
  1454. }
  1455. void
  1456. setup(void) {
  1457. XSetWindowAttributes wa;
  1458. /* clean up any zombies immediately */
  1459. sigchld(0);
  1460. /* init screen */
  1461. screen = DefaultScreen(dpy);
  1462. root = RootWindow(dpy, screen);
  1463. initfont(font);
  1464. sw = DisplayWidth(dpy, screen);
  1465. sh = DisplayHeight(dpy, screen);
  1466. bh = dc.h = dc.font.height + 2;
  1467. updategeom();
  1468. /* init atoms */
  1469. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1470. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1471. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1472. wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
  1473. netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
  1474. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1475. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1476. netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
  1477. netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
  1478. netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
  1479. netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
  1480. /* init cursors */
  1481. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1482. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1483. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1484. /* init appearance */
  1485. dc.norm[ColBorder] = getcolor(normbordercolor);
  1486. dc.norm[ColBG] = getcolor(normbgcolor);
  1487. dc.norm[ColFG] = getcolor(normfgcolor);
  1488. dc.sel[ColBorder] = getcolor(selbordercolor);
  1489. dc.sel[ColBG] = getcolor(selbgcolor);
  1490. dc.sel[ColFG] = getcolor(selfgcolor);
  1491. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1492. dc.gc = XCreateGC(dpy, root, 0, NULL);
  1493. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1494. if(!dc.font.set)
  1495. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1496. /* init bars */
  1497. updatebars();
  1498. updatestatus();
  1499. /* EWMH support per view */
  1500. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1501. PropModeReplace, (unsigned char *) netatom, NetLast);
  1502. /* select for events */
  1503. wa.cursor = cursor[CurNormal];
  1504. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
  1505. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
  1506. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1507. XSelectInput(dpy, root, wa.event_mask);
  1508. grabkeys();
  1509. }
  1510. void
  1511. showhide(Client *c) {
  1512. if(!c)
  1513. return;
  1514. if(ISVISIBLE(c)) { /* show clients top down */
  1515. XMoveWindow(dpy, c->win, c->x, c->y);
  1516. if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
  1517. resize(c, c->x, c->y, c->w, c->h, False);
  1518. showhide(c->snext);
  1519. }
  1520. else { /* hide clients bottom up */
  1521. showhide(c->snext);
  1522. XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
  1523. }
  1524. }
  1525. void
  1526. sigchld(int unused) {
  1527. if(signal(SIGCHLD, sigchld) == SIG_ERR)
  1528. die("Can't install SIGCHLD handler");
  1529. while(0 < waitpid(-1, NULL, WNOHANG));
  1530. }
  1531. void
  1532. spawn(const Arg *arg) {
  1533. if(fork() == 0) {
  1534. if(dpy)
  1535. close(ConnectionNumber(dpy));
  1536. setsid();
  1537. execvp(((char **)arg->v)[0], (char **)arg->v);
  1538. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1539. perror(" failed");
  1540. exit(EXIT_SUCCESS);
  1541. }
  1542. }
  1543. void
  1544. tag(const Arg *arg) {
  1545. if(selmon->sel && arg->ui & TAGMASK) {
  1546. selmon->sel->tags = arg->ui & TAGMASK;
  1547. focus(NULL);
  1548. arrange(selmon);
  1549. }
  1550. }
  1551. void
  1552. tagmon(const Arg *arg) {
  1553. if(!selmon->sel || !mons->next)
  1554. return;
  1555. sendmon(selmon->sel, dirtomon(arg->i));
  1556. }
  1557. int
  1558. textnw(const char *text, unsigned int len) {
  1559. XRectangle r;
  1560. if(dc.font.set) {
  1561. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1562. return r.width;
  1563. }
  1564. return XTextWidth(dc.font.xfont, text, len);
  1565. }
  1566. void
  1567. tile(Monitor *m) {
  1568. unsigned int i, n, h, mw, my, ty;
  1569. Client *c;
  1570. for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  1571. if(n == 0)
  1572. return;
  1573. if(n > m->nmaster)
  1574. mw = m->nmaster ? m->ww * m->mfact : 0;
  1575. else
  1576. mw = m->ww;
  1577. for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  1578. if(i < m->nmaster) {
  1579. h = (m->wh - my) / (MIN(n, m->nmaster) - i);
  1580. resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False);
  1581. my += HEIGHT(c);
  1582. }
  1583. else {
  1584. h = (m->wh - ty) / (n - i);
  1585. resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False);
  1586. ty += HEIGHT(c);
  1587. }
  1588. }
  1589. void
  1590. togglebar(const Arg *arg) {
  1591. selmon->showbar = !selmon->showbar;
  1592. updatebarpos(selmon);
  1593. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1594. arrange(selmon);
  1595. }
  1596. void
  1597. togglefloating(const Arg *arg) {
  1598. if(!selmon->sel)
  1599. return;
  1600. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1601. if(selmon->sel->isfloating)
  1602. resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  1603. selmon->sel->w, selmon->sel->h, False);
  1604. else if(selmon->sel->isfullscreen)
  1605. setfullscreen(selmon->sel, False);
  1606. arrange(selmon);
  1607. }
  1608. void
  1609. toggletag(const Arg *arg) {
  1610. unsigned int newtags;
  1611. if(!selmon->sel)
  1612. return;
  1613. newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1614. if(newtags) {
  1615. selmon->sel->tags = newtags;
  1616. focus(NULL);
  1617. arrange(selmon);
  1618. }
  1619. }
  1620. void
  1621. toggleview(const Arg *arg) {
  1622. unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1623. if(newtagset) {
  1624. selmon->tagset[selmon->seltags] = newtagset;
  1625. focus(NULL);
  1626. arrange(selmon);
  1627. }
  1628. }
  1629. void
  1630. unfocus(Client *c, Bool setfocus) {
  1631. if(!c)
  1632. return;
  1633. grabbuttons(c, False);
  1634. XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
  1635. if(setfocus)
  1636. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1637. }
  1638. void
  1639. unmanage(Client *c, Bool destroyed) {
  1640. Monitor *m = c->mon;
  1641. XWindowChanges wc;
  1642. /* The server grab construct avoids race conditions. */
  1643. detach(c);
  1644. detachstack(c);
  1645. if(!destroyed) {
  1646. wc.border_width = c->oldbw;
  1647. XGrabServer(dpy);
  1648. XSetErrorHandler(xerrordummy);
  1649. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1650. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1651. setclientstate(c, WithdrawnState);
  1652. XSync(dpy, False);
  1653. XSetErrorHandler(xerror);
  1654. XUngrabServer(dpy);
  1655. }
  1656. free(c);
  1657. focus(NULL);
  1658. arrange(m);
  1659. }
  1660. void
  1661. unmapnotify(XEvent *e) {
  1662. Client *c;
  1663. XUnmapEvent *ev = &e->xunmap;
  1664. if((c = wintoclient(ev->window))) {
  1665. if(ev->send_event)
  1666. setclientstate(c, WithdrawnState);
  1667. else
  1668. unmanage(c, False);
  1669. }
  1670. }
  1671. void
  1672. updatebars(void) {
  1673. Monitor *m;
  1674. XSetWindowAttributes wa = {
  1675. .override_redirect = True,
  1676. .background_pixmap = ParentRelative,
  1677. .event_mask = ButtonPressMask|ExposureMask
  1678. };
  1679. for(m = mons; m; m = m->next) {
  1680. if (m->barwin)
  1681. continue;
  1682. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  1683. CopyFromParent, DefaultVisual(dpy, screen),
  1684. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1685. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1686. XMapRaised(dpy, m->barwin);
  1687. }
  1688. }
  1689. void
  1690. updatebarpos(Monitor *m) {
  1691. m->wy = m->my;
  1692. m->wh = m->mh;
  1693. if(m->showbar) {
  1694. m->wh -= bh;
  1695. m->by = m->topbar ? m->wy : m->wy + m->wh;
  1696. m->wy = m->topbar ? m->wy + bh : m->wy;
  1697. }
  1698. else
  1699. m->by = -bh;
  1700. }
  1701. Bool
  1702. updategeom(void) {
  1703. Bool dirty = False;
  1704. #ifdef XINERAMA
  1705. if(XineramaIsActive(dpy)) {
  1706. int i, j, n, nn;
  1707. Client *c;
  1708. Monitor *m;
  1709. XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
  1710. XineramaScreenInfo *unique = NULL;
  1711. for(n = 0, m = mons; m; m = m->next, n++);
  1712. /* only consider unique geometries as separate screens */
  1713. if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
  1714. die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
  1715. for(i = 0, j = 0; i < nn; i++)
  1716. if(isuniquegeom(unique, j, &info[i]))
  1717. memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
  1718. XFree(info);
  1719. nn = j;
  1720. if(n <= nn) {
  1721. for(i = 0; i < (nn - n); i++) { /* new monitors available */
  1722. for(m = mons; m && m->next; m = m->next);
  1723. if(m)
  1724. m->next = createmon();
  1725. else
  1726. mons = createmon();
  1727. }
  1728. for(i = 0, m = mons; i < nn && m; m = m->next, i++)
  1729. if(i >= n
  1730. || (unique[i].x_org != m->mx || unique[i].y_org != m->my
  1731. || unique[i].width != m->mw || unique[i].height != m->mh))
  1732. {
  1733. dirty = True;
  1734. m->num = i;
  1735. m->mx = m->wx = unique[i].x_org;
  1736. m->my = m->wy = unique[i].y_org;
  1737. m->mw = m->ww = unique[i].width;
  1738. m->mh = m->wh = unique[i].height;
  1739. updatebarpos(m);
  1740. }
  1741. }
  1742. else { /* less monitors available nn < n */
  1743. for(i = nn; i < n; i++) {
  1744. for(m = mons; m && m->next; m = m->next);
  1745. while(m->clients) {
  1746. dirty = True;
  1747. c = m->clients;
  1748. m->clients = c->next;
  1749. detachstack(c);
  1750. c->mon = mons;
  1751. attach(c);
  1752. attachstack(c);
  1753. }
  1754. if(m == selmon)
  1755. selmon = mons;
  1756. cleanupmon(m);
  1757. }
  1758. }
  1759. free(unique);
  1760. }
  1761. else
  1762. #endif /* XINERAMA */
  1763. /* default monitor setup */
  1764. {
  1765. if(!mons)
  1766. mons = createmon();
  1767. if(mons->mw != sw || mons->mh != sh) {
  1768. dirty = True;
  1769. mons->mw = mons->ww = sw;
  1770. mons->mh = mons->wh = sh;
  1771. updatebarpos(mons);
  1772. }
  1773. }
  1774. if(dirty) {
  1775. selmon = mons;
  1776. selmon = wintomon(root);
  1777. }
  1778. return dirty;
  1779. }
  1780. void
  1781. updatenumlockmask(void) {
  1782. unsigned int i, j;
  1783. XModifierKeymap *modmap;
  1784. numlockmask = 0;
  1785. modmap = XGetModifierMapping(dpy);
  1786. for(i = 0; i < 8; i++)
  1787. for(j = 0; j < modmap->max_keypermod; j++)
  1788. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1789. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1790. numlockmask = (1 << i);
  1791. XFreeModifiermap(modmap);
  1792. }
  1793. void
  1794. updatesizehints(Client *c) {
  1795. long msize;
  1796. XSizeHints size;
  1797. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1798. /* size is uninitialized, ensure that size.flags aren't used */
  1799. size.flags = PSize;
  1800. if(size.flags & PBaseSize) {
  1801. c->basew = size.base_width;
  1802. c->baseh = size.base_height;
  1803. }
  1804. else if(size.flags & PMinSize) {
  1805. c->basew = size.min_width;
  1806. c->baseh = size.min_height;
  1807. }
  1808. else
  1809. c->basew = c->baseh = 0;
  1810. if(size.flags & PResizeInc) {
  1811. c->incw = size.width_inc;
  1812. c->inch = size.height_inc;
  1813. }
  1814. else
  1815. c->incw = c->inch = 0;
  1816. if(size.flags & PMaxSize) {
  1817. c->maxw = size.max_width;
  1818. c->maxh = size.max_height;
  1819. }
  1820. else
  1821. c->maxw = c->maxh = 0;
  1822. if(size.flags & PMinSize) {
  1823. c->minw = size.min_width;
  1824. c->minh = size.min_height;
  1825. }
  1826. else if(size.flags & PBaseSize) {
  1827. c->minw = size.base_width;
  1828. c->minh = size.base_height;
  1829. }
  1830. else
  1831. c->minw = c->minh = 0;
  1832. if(size.flags & PAspect) {
  1833. c->mina = (float)size.min_aspect.y / size.min_aspect.x;
  1834. c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
  1835. }
  1836. else
  1837. c->maxa = c->mina = 0.0;
  1838. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1839. && c->maxw == c->minw && c->maxh == c->minh);
  1840. }
  1841. void
  1842. updatetitle(Client *c) {
  1843. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1844. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1845. if(c->name[0] == '\0') /* hack to mark broken clients */
  1846. strcpy(c->name, broken);
  1847. }
  1848. void
  1849. updatestatus(void) {
  1850. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1851. strcpy(stext, "dwm-"VERSION);
  1852. drawbar(selmon);
  1853. }
  1854. void
  1855. updatewindowtype(Client *c) {
  1856. Atom state = getatomprop(c, netatom[NetWMState]);
  1857. Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
  1858. if(state == netatom[NetWMFullscreen])
  1859. setfullscreen(c, True);
  1860. if(wtype == netatom[NetWMWindowTypeDialog])
  1861. c->isfloating = True;
  1862. }
  1863. void
  1864. updatewmhints(Client *c) {
  1865. XWMHints *wmh;
  1866. if((wmh = XGetWMHints(dpy, c->win))) {
  1867. if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  1868. wmh->flags &= ~XUrgencyHint;
  1869. XSetWMHints(dpy, c->win, wmh);
  1870. }
  1871. else
  1872. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1873. if(wmh->flags & InputHint)
  1874. c->neverfocus = !wmh->input;
  1875. else
  1876. c->neverfocus = False;
  1877. XFree(wmh);
  1878. }
  1879. }
  1880. void
  1881. view(const Arg *arg) {
  1882. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1883. return;
  1884. selmon->seltags ^= 1; /* toggle sel tagset */
  1885. if(arg->ui & TAGMASK)
  1886. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1887. focus(NULL);
  1888. arrange(selmon);
  1889. }
  1890. Client *
  1891. wintoclient(Window w) {
  1892. Client *c;
  1893. Monitor *m;
  1894. for(m = mons; m; m = m->next)
  1895. for(c = m->clients; c; c = c->next)
  1896. if(c->win == w)
  1897. return c;
  1898. return NULL;
  1899. }
  1900. Monitor *
  1901. wintomon(Window w) {
  1902. int x, y;
  1903. Client *c;
  1904. Monitor *m;
  1905. if(w == root && getrootptr(&x, &y))
  1906. return recttomon(x, y, 1, 1);
  1907. for(m = mons; m; m = m->next)
  1908. if(w == m->barwin)
  1909. return m;
  1910. if((c = wintoclient(w)))
  1911. return c->mon;
  1912. return selmon;
  1913. }
  1914. /* There's no way to check accesses to destroyed windows, thus those cases are
  1915. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1916. * default error handler, which may call exit. */
  1917. int
  1918. xerror(Display *dpy, XErrorEvent *ee) {
  1919. if(ee->error_code == BadWindow
  1920. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1921. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1922. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1923. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1924. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1925. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1926. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1927. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1928. return 0;
  1929. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1930. ee->request_code, ee->error_code);
  1931. return xerrorxlib(dpy, ee); /* may call exit */
  1932. }
  1933. int
  1934. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1935. return 0;
  1936. }
  1937. /* Startup Error handler to check if another window manager
  1938. * is already running. */
  1939. int
  1940. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1941. die("dwm: another window manager is already running\n");
  1942. return -1;
  1943. }
  1944. void
  1945. zoom(const Arg *arg) {
  1946. Client *c = selmon->sel;
  1947. if(!selmon->lt[selmon->sellt]->arrange
  1948. || (selmon->sel && selmon->sel->isfloating))
  1949. return;
  1950. if(c == nexttiled(selmon->clients))
  1951. if(!c || !(c = nexttiled(c->next)))
  1952. return;
  1953. pop(c);
  1954. }
  1955. int
  1956. main(int argc, char *argv[]) {
  1957. if(argc == 2 && !strcmp("-v", argv[1]))
  1958. die("dwm-"VERSION", © 2006-2012 dwm engineers, see LICENSE for details\n");
  1959. else if(argc != 1)
  1960. die("usage: dwm [-v]\n");
  1961. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1962. fputs("warning: no locale support\n", stderr);
  1963. if(!(dpy = XOpenDisplay(NULL)))
  1964. die("dwm: cannot open display\n");
  1965. checkotherwm();
  1966. setup();
  1967. scan();
  1968. run();
  1969. cleanup();
  1970. XCloseDisplay(dpy);
  1971. return EXIT_SUCCESS;
  1972. }