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.

2071 lines
51 KiB

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