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

2069 lines
51 KiB

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