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.

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