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.

2064 lines
51 KiB

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