Configuration file for DWM on MacBook Air
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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
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, WMTakeFocus, WMLast }; /* default atoms */
  61. enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
  62. ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
  63. typedef union {
  64. int i;
  65. unsigned int ui;
  66. float f;
  67. const void *v;
  68. } Arg;
  69. typedef struct {
  70. unsigned int click;
  71. unsigned int mask;
  72. unsigned int button;
  73. void (*func)(const Arg *arg);
  74. const Arg arg;
  75. } Button;
  76. typedef struct Monitor Monitor;
  77. typedef struct Client Client;
  78. struct Client {
  79. char name[256];
  80. float mina, maxa;
  81. int x, y, w, h;
  82. int oldx, oldy, oldw, oldh;
  83. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  84. int bw, oldbw;
  85. unsigned int tags;
  86. Bool isfixed, isfloating, isurgent, neverfocus, oldstate;
  87. Client *next;
  88. Client *snext;
  89. Monitor *mon;
  90. Window win;
  91. };
  92. typedef struct {
  93. int x, y, w, h;
  94. unsigned long norm[ColLast];
  95. unsigned long sel[ColLast];
  96. Drawable drawable;
  97. GC gc;
  98. struct {
  99. int ascent;
  100. int descent;
  101. int height;
  102. XFontSet set;
  103. XFontStruct *xfont;
  104. } font;
  105. } DC; /* draw context */
  106. typedef struct {
  107. unsigned int mod;
  108. KeySym keysym;
  109. void (*func)(const Arg *);
  110. const Arg arg;
  111. } Key;
  112. typedef struct {
  113. const char *symbol;
  114. void (*arrange)(Monitor *);
  115. } Layout;
  116. struct Monitor {
  117. char ltsymbol[16];
  118. float mfact;
  119. int num;
  120. int by; /* bar geometry */
  121. int mx, my, mw, mh; /* screen size */
  122. int wx, wy, ww, wh; /* window area */
  123. unsigned int seltags;
  124. unsigned int sellt;
  125. unsigned int tagset[2];
  126. Bool showbar;
  127. Bool topbar;
  128. Client *clients;
  129. Client *sel;
  130. Client *stack;
  131. Monitor *next;
  132. Window barwin;
  133. const Layout *lt[2];
  134. };
  135. typedef struct {
  136. const char *class;
  137. const char *instance;
  138. const char *title;
  139. unsigned int tags;
  140. Bool isfloating;
  141. int monitor;
  142. } Rule;
  143. /* function declarations */
  144. static void applyrules(Client *c);
  145. static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
  146. static void arrange(Monitor *m);
  147. static void arrangemon(Monitor *m);
  148. static void attach(Client *c);
  149. static void attachstack(Client *c);
  150. static void buttonpress(XEvent *e);
  151. static void checkotherwm(void);
  152. static void cleanup(void);
  153. static void cleanupmon(Monitor *mon);
  154. static void clearurgent(Client *c);
  155. static void clientmessage(XEvent *e);
  156. static void configure(Client *c);
  157. static void configurenotify(XEvent *e);
  158. static void configurerequest(XEvent *e);
  159. static Monitor *createmon(void);
  160. static void destroynotify(XEvent *e);
  161. static void detach(Client *c);
  162. static void detachstack(Client *c);
  163. static void die(const char *errstr, ...);
  164. static Monitor *dirtomon(int dir);
  165. static void drawbar(Monitor *m);
  166. static void drawbars(void);
  167. static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
  168. static void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
  169. static void enternotify(XEvent *e);
  170. static void expose(XEvent *e);
  171. static void focus(Client *c);
  172. static void focusin(XEvent *e);
  173. static void focusmon(const Arg *arg);
  174. static void focusstack(const Arg *arg);
  175. static unsigned long getcolor(const char *colstr);
  176. static Bool getrootptr(int *x, int *y);
  177. static long getstate(Window w);
  178. static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  179. static void grabbuttons(Client *c, Bool focused);
  180. static void grabkeys(void);
  181. static void initfont(const char *fontstr);
  182. static void keypress(XEvent *e);
  183. static void killclient(const Arg *arg);
  184. static void manage(Window w, XWindowAttributes *wa);
  185. static void mappingnotify(XEvent *e);
  186. static void maprequest(XEvent *e);
  187. static void monocle(Monitor *m);
  188. static void movemouse(const Arg *arg);
  189. static Client *nexttiled(Client *c);
  190. static Monitor *ptrtomon(int x, int y);
  191. static void propertynotify(XEvent *e);
  192. static void quit(const Arg *arg);
  193. static void resize(Client *c, int x, int y, int w, int h, Bool interact);
  194. static void resizeclient(Client *c, int x, int y, int w, int h);
  195. static void resizemouse(const Arg *arg);
  196. static void restack(Monitor *m);
  197. static void run(void);
  198. static void scan(void);
  199. static Bool sendevent(Client *c, Atom proto);
  200. static void sendmon(Client *c, Monitor *m);
  201. static void setclientstate(Client *c, long state);
  202. static void setfocus(Client *c);
  203. static void setlayout(const Arg *arg);
  204. static void setmfact(const Arg *arg);
  205. static void setup(void);
  206. static void showhide(Client *c);
  207. static void sigchld(int unused);
  208. static void spawn(const Arg *arg);
  209. static void tag(const Arg *arg);
  210. static void tagmon(const Arg *arg);
  211. static int textnw(const char *text, unsigned int len);
  212. static void tile(Monitor *);
  213. static void togglebar(const Arg *arg);
  214. static void togglefloating(const Arg *arg);
  215. static void toggletag(const Arg *arg);
  216. static void toggleview(const Arg *arg);
  217. static void unfocus(Client *c, Bool setfocus);
  218. static void unmanage(Client *c, Bool destroyed);
  219. static void unmapnotify(XEvent *e);
  220. static Bool updategeom(void);
  221. static void updatebarpos(Monitor *m);
  222. static void updatebars(void);
  223. static void updatenumlockmask(void);
  224. static void updatesizehints(Client *c);
  225. static void updatestatus(void);
  226. static void updatetitle(Client *c);
  227. static void updatewmhints(Client *c);
  228. static void view(const Arg *arg);
  229. static Client *wintoclient(Window w);
  230. static Monitor *wintomon(Window w);
  231. static int xerror(Display *dpy, XErrorEvent *ee);
  232. static int xerrordummy(Display *dpy, XErrorEvent *ee);
  233. static int xerrorstart(Display *dpy, XErrorEvent *ee);
  234. static void zoom(const Arg *arg);
  235. /* variables */
  236. static const char broken[] = "broken";
  237. static char stext[256];
  238. static int screen;
  239. static int sw, sh; /* X display screen geometry width, height */
  240. static int bh, blw = 0; /* bar geometry */
  241. static int (*xerrorxlib)(Display *, XErrorEvent *);
  242. static unsigned int numlockmask = 0;
  243. static void (*handler[LASTEvent]) (XEvent *) = {
  244. [ButtonPress] = buttonpress,
  245. [ClientMessage] = clientmessage,
  246. [ConfigureRequest] = configurerequest,
  247. [ConfigureNotify] = configurenotify,
  248. [DestroyNotify] = destroynotify,
  249. [EnterNotify] = enternotify,
  250. [Expose] = expose,
  251. [FocusIn] = focusin,
  252. [KeyPress] = keypress,
  253. [MappingNotify] = mappingnotify,
  254. [MapRequest] = maprequest,
  255. [PropertyNotify] = propertynotify,
  256. [UnmapNotify] = unmapnotify
  257. };
  258. static Atom wmatom[WMLast], netatom[NetLast];
  259. static Bool running = True;
  260. static Cursor cursor[CurLast];
  261. static Display *dpy;
  262. static DC dc;
  263. static Monitor *mons = NULL, *selmon = NULL;
  264. static Window root;
  265. /* configuration, allows nested code to access above variables */
  266. #include "config.h"
  267. /* compile-time check if all tags fit into an unsigned int bit array. */
  268. struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
  269. /* function implementations */
  270. void
  271. applyrules(Client *c) {
  272. const char *class, *instance;
  273. unsigned int i;
  274. const Rule *r;
  275. Monitor *m;
  276. XClassHint ch = { 0 };
  277. /* rule matching */
  278. c->isfloating = c->tags = 0;
  279. if(XGetClassHint(dpy, c->win, &ch)) {
  280. class = ch.res_class ? ch.res_class : broken;
  281. instance = ch.res_name ? ch.res_name : broken;
  282. for(i = 0; i < LENGTH(rules); i++) {
  283. r = &rules[i];
  284. if((!r->title || strstr(c->name, r->title))
  285. && (!r->class || strstr(class, r->class))
  286. && (!r->instance || strstr(instance, r->instance)))
  287. {
  288. c->isfloating = r->isfloating;
  289. c->tags |= r->tags;
  290. for(m = mons; m && m->num != r->monitor; m = m->next);
  291. if(m)
  292. c->mon = m;
  293. }
  294. }
  295. if(ch.res_class)
  296. XFree(ch.res_class);
  297. if(ch.res_name)
  298. XFree(ch.res_name);
  299. }
  300. c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
  301. }
  302. Bool
  303. applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
  304. Bool baseismin;
  305. Monitor *m = c->mon;
  306. /* set minimum possible */
  307. *w = MAX(1, *w);
  308. *h = MAX(1, *h);
  309. if(interact) {
  310. if(*x > sw)
  311. *x = sw - WIDTH(c);
  312. if(*y > sh)
  313. *y = sh - HEIGHT(c);
  314. if(*x + *w + 2 * c->bw < 0)
  315. *x = 0;
  316. if(*y + *h + 2 * c->bw < 0)
  317. *y = 0;
  318. }
  319. else {
  320. if(*x > m->mx + m->mw)
  321. *x = m->mx + m->mw - WIDTH(c);
  322. if(*y > m->my + m->mh)
  323. *y = m->my + m->mh - HEIGHT(c);
  324. if(*x + *w + 2 * c->bw < m->mx)
  325. *x = m->mx;
  326. if(*y + *h + 2 * c->bw < m->my)
  327. *y = m->my;
  328. }
  329. if(*h < bh)
  330. *h = bh;
  331. if(*w < bh)
  332. *w = bh;
  333. if(resizehints || c->isfloating) {
  334. /* see last two sentences in ICCCM 4.1.2.3 */
  335. baseismin = c->basew == c->minw && c->baseh == c->minh;
  336. if(!baseismin) { /* temporarily remove base dimensions */
  337. *w -= c->basew;
  338. *h -= c->baseh;
  339. }
  340. /* adjust for aspect limits */
  341. if(c->mina > 0 && c->maxa > 0) {
  342. if(c->maxa < (float)*w / *h)
  343. *w = *h * c->maxa + 0.5;
  344. else if(c->mina < (float)*h / *w)
  345. *h = *w * c->mina + 0.5;
  346. }
  347. if(baseismin) { /* increment calculation requires this */
  348. *w -= c->basew;
  349. *h -= c->baseh;
  350. }
  351. /* adjust for increment value */
  352. if(c->incw)
  353. *w -= *w % c->incw;
  354. if(c->inch)
  355. *h -= *h % c->inch;
  356. /* restore base dimensions */
  357. *w = MAX(*w + c->basew, c->minw);
  358. *h = MAX(*h + c->baseh, c->minh);
  359. if(c->maxw)
  360. *w = MIN(*w, c->maxw);
  361. if(c->maxh)
  362. *h = MIN(*h, c->maxh);
  363. }
  364. return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
  365. }
  366. void
  367. arrange(Monitor *m) {
  368. if(m)
  369. showhide(m->stack);
  370. else for(m = mons; m; m = m->next)
  371. showhide(m->stack);
  372. focus(NULL);
  373. if(m)
  374. arrangemon(m);
  375. else for(m = mons; m; m = m->next)
  376. arrangemon(m);
  377. }
  378. void
  379. arrangemon(Monitor *m) {
  380. strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
  381. if(m->lt[m->sellt]->arrange)
  382. m->lt[m->sellt]->arrange(m);
  383. restack(m);
  384. }
  385. void
  386. attach(Client *c) {
  387. c->next = c->mon->clients;
  388. c->mon->clients = c;
  389. }
  390. void
  391. attachstack(Client *c) {
  392. c->snext = c->mon->stack;
  393. c->mon->stack = c;
  394. }
  395. void
  396. buttonpress(XEvent *e) {
  397. unsigned int i, x, click;
  398. Arg arg = {0};
  399. Client *c;
  400. Monitor *m;
  401. XButtonPressedEvent *ev = &e->xbutton;
  402. click = ClkRootWin;
  403. /* focus monitor if necessary */
  404. if((m = wintomon(ev->window)) && m != selmon) {
  405. unfocus(selmon->sel, True);
  406. selmon = m;
  407. focus(NULL);
  408. }
  409. if(ev->window == selmon->barwin) {
  410. i = x = 0;
  411. do {
  412. x += TEXTW(tags[i]);
  413. } while(ev->x >= x && ++i < LENGTH(tags));
  414. if(i < LENGTH(tags)) {
  415. click = ClkTagBar;
  416. arg.ui = 1 << i;
  417. }
  418. else if(ev->x < x + blw)
  419. click = ClkLtSymbol;
  420. else if(ev->x > selmon->wx + selmon->ww - TEXTW(stext))
  421. click = ClkStatusText;
  422. else
  423. click = ClkWinTitle;
  424. }
  425. else if((c = wintoclient(ev->window))) {
  426. focus(c);
  427. click = ClkClientWin;
  428. }
  429. for(i = 0; i < LENGTH(buttons); i++)
  430. if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
  431. && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
  432. buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
  433. }
  434. void
  435. checkotherwm(void) {
  436. xerrorxlib = XSetErrorHandler(xerrorstart);
  437. /* this causes an error if some other window manager is running */
  438. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  439. XSync(dpy, False);
  440. XSetErrorHandler(xerror);
  441. XSync(dpy, False);
  442. }
  443. void
  444. cleanup(void) {
  445. Arg a = {.ui = ~0};
  446. Layout foo = { "", NULL };
  447. Monitor *m;
  448. view(&a);
  449. selmon->lt[selmon->sellt] = &foo;
  450. for(m = mons; m; m = m->next)
  451. while(m->stack)
  452. unmanage(m->stack, False);
  453. if(dc.font.set)
  454. XFreeFontSet(dpy, dc.font.set);
  455. else
  456. XFreeFont(dpy, dc.font.xfont);
  457. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  458. XFreePixmap(dpy, dc.drawable);
  459. XFreeGC(dpy, dc.gc);
  460. XFreeCursor(dpy, cursor[CurNormal]);
  461. XFreeCursor(dpy, cursor[CurResize]);
  462. XFreeCursor(dpy, cursor[CurMove]);
  463. while(mons)
  464. cleanupmon(mons);
  465. XSync(dpy, False);
  466. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  467. }
  468. void
  469. cleanupmon(Monitor *mon) {
  470. Monitor *m;
  471. if(mon == mons)
  472. mons = mons->next;
  473. else {
  474. for(m = mons; m && m->next != mon; m = m->next);
  475. m->next = mon->next;
  476. }
  477. XUnmapWindow(dpy, mon->barwin);
  478. XDestroyWindow(dpy, mon->barwin);
  479. free(mon);
  480. }
  481. void
  482. clearurgent(Client *c) {
  483. XWMHints *wmh;
  484. c->isurgent = False;
  485. if(!(wmh = XGetWMHints(dpy, c->win)))
  486. return;
  487. wmh->flags &= ~XUrgencyHint;
  488. XSetWMHints(dpy, c->win, wmh);
  489. XFree(wmh);
  490. }
  491. void
  492. configure(Client *c) {
  493. XConfigureEvent ce;
  494. ce.type = ConfigureNotify;
  495. ce.display = dpy;
  496. ce.event = c->win;
  497. ce.window = c->win;
  498. ce.x = c->x;
  499. ce.y = c->y;
  500. ce.width = c->w;
  501. ce.height = c->h;
  502. ce.border_width = c->bw;
  503. ce.above = None;
  504. ce.override_redirect = False;
  505. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  506. }
  507. void
  508. configurenotify(XEvent *e) {
  509. Monitor *m;
  510. XConfigureEvent *ev = &e->xconfigure;
  511. if(ev->window == root) {
  512. sw = ev->width;
  513. sh = ev->height;
  514. if(updategeom()) {
  515. if(dc.drawable != 0)
  516. XFreePixmap(dpy, dc.drawable);
  517. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  518. updatebars();
  519. for(m = mons; m; m = m->next)
  520. XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
  521. arrange(NULL);
  522. }
  523. }
  524. }
  525. void
  526. configurerequest(XEvent *e) {
  527. Client *c;
  528. Monitor *m;
  529. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  530. XWindowChanges wc;
  531. if((c = wintoclient(ev->window))) {
  532. if(ev->value_mask & CWBorderWidth)
  533. c->bw = ev->border_width;
  534. else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
  535. m = c->mon;
  536. if(ev->value_mask & CWX)
  537. c->x = m->mx + ev->x;
  538. if(ev->value_mask & CWY)
  539. c->y = m->my + ev->y;
  540. if(ev->value_mask & CWWidth)
  541. c->w = ev->width;
  542. if(ev->value_mask & CWHeight)
  543. c->h = ev->height;
  544. if((c->x + c->w) > m->mx + m->mw && c->isfloating)
  545. c->x = m->mx + (m->mw / 2 - c->w / 2); /* center in x direction */
  546. if((c->y + c->h) > m->my + m->mh && c->isfloating)
  547. c->y = m->my + (m->mh / 2 - c->h / 2); /* center in y direction */
  548. if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
  549. configure(c);
  550. if(ISVISIBLE(c))
  551. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  552. }
  553. else
  554. configure(c);
  555. }
  556. else {
  557. wc.x = ev->x;
  558. wc.y = ev->y;
  559. wc.width = ev->width;
  560. wc.height = ev->height;
  561. wc.border_width = ev->border_width;
  562. wc.sibling = ev->above;
  563. wc.stack_mode = ev->detail;
  564. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  565. }
  566. XSync(dpy, False);
  567. }
  568. Monitor *
  569. createmon(void) {
  570. Monitor *m;
  571. if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
  572. die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
  573. m->tagset[0] = m->tagset[1] = 1;
  574. m->mfact = mfact;
  575. m->showbar = showbar;
  576. m->topbar = topbar;
  577. m->lt[0] = &layouts[0];
  578. m->lt[1] = &layouts[1 % LENGTH(layouts)];
  579. strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
  580. return m;
  581. }
  582. void
  583. destroynotify(XEvent *e) {
  584. Client *c;
  585. XDestroyWindowEvent *ev = &e->xdestroywindow;
  586. if((c = wintoclient(ev->window)))
  587. unmanage(c, True);
  588. }
  589. void
  590. detach(Client *c) {
  591. Client **tc;
  592. for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
  593. *tc = c->next;
  594. }
  595. void
  596. detachstack(Client *c) {
  597. Client **tc, *t;
  598. for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
  599. *tc = c->snext;
  600. if(c == c->mon->sel) {
  601. for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
  602. c->mon->sel = t;
  603. }
  604. }
  605. void
  606. die(const char *errstr, ...) {
  607. va_list ap;
  608. va_start(ap, errstr);
  609. vfprintf(stderr, errstr, ap);
  610. va_end(ap);
  611. exit(EXIT_FAILURE);
  612. }
  613. Monitor *
  614. dirtomon(int dir) {
  615. Monitor *m = NULL;
  616. if(dir > 0) {
  617. if(!(m = selmon->next))
  618. m = mons;
  619. }
  620. else {
  621. if(selmon == mons)
  622. for(m = mons; m->next; m = m->next);
  623. else
  624. for(m = mons; m->next != selmon; m = m->next);
  625. }
  626. return m;
  627. }
  628. void
  629. drawbar(Monitor *m) {
  630. int x;
  631. unsigned int i, occ = 0, urg = 0;
  632. unsigned long *col;
  633. Client *c;
  634. for(c = m->clients; c; c = c->next) {
  635. occ |= c->tags;
  636. if(c->isurgent)
  637. urg |= c->tags;
  638. }
  639. dc.x = 0;
  640. for(i = 0; i < LENGTH(tags); i++) {
  641. dc.w = TEXTW(tags[i]);
  642. col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
  643. drawtext(tags[i], col, urg & 1 << i);
  644. drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
  645. occ & 1 << i, urg & 1 << i, col);
  646. dc.x += dc.w;
  647. }
  648. dc.w = blw = TEXTW(m->ltsymbol);
  649. drawtext(m->ltsymbol, dc.norm, False);
  650. dc.x += dc.w;
  651. x = dc.x;
  652. if(m == selmon) { /* status is only drawn on selected monitor */
  653. dc.w = TEXTW(stext);
  654. dc.x = m->ww - dc.w;
  655. if(dc.x < x) {
  656. dc.x = x;
  657. dc.w = m->ww - x;
  658. }
  659. drawtext(stext, dc.norm, False);
  660. }
  661. else
  662. dc.x = m->ww;
  663. if((dc.w = dc.x - x) > bh) {
  664. dc.x = x;
  665. if(m->sel) {
  666. col = m == selmon ? dc.sel : dc.norm;
  667. drawtext(m->sel->name, col, False);
  668. drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
  669. }
  670. else
  671. drawtext(NULL, dc.norm, False);
  672. }
  673. XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
  674. XSync(dpy, False);
  675. }
  676. void
  677. drawbars(void) {
  678. Monitor *m;
  679. for(m = mons; m; m = m->next)
  680. drawbar(m);
  681. }
  682. void
  683. drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  684. int x;
  685. XGCValues gcv;
  686. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  687. gcv.foreground = col[invert ? ColBG : ColFG];
  688. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  689. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  690. r.x = dc.x + 1;
  691. r.y = dc.y + 1;
  692. if(filled) {
  693. r.width = r.height = x + 1;
  694. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  695. }
  696. else if(empty) {
  697. r.width = r.height = x;
  698. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  699. }
  700. }
  701. void
  702. drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
  703. char buf[256];
  704. int i, x, y, h, len, olen;
  705. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  706. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  707. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  708. if(!text)
  709. return;
  710. olen = strlen(text);
  711. h = dc.font.ascent + dc.font.descent;
  712. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  713. x = dc.x + (h / 2);
  714. /* shorten text if necessary */
  715. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  716. if(!len)
  717. return;
  718. memcpy(buf, text, len);
  719. if(len < olen)
  720. for(i = len; i && i > len - 3; buf[--i] = '.');
  721. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  722. if(dc.font.set)
  723. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  724. else
  725. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  726. }
  727. void
  728. enternotify(XEvent *e) {
  729. Monitor *m;
  730. XCrossingEvent *ev = &e->xcrossing;
  731. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  732. return;
  733. if((m = wintomon(ev->window)) && m != selmon) {
  734. unfocus(selmon->sel, True);
  735. selmon = m;
  736. }
  737. focus((wintoclient(ev->window)));
  738. }
  739. void
  740. expose(XEvent *e) {
  741. Monitor *m;
  742. XExposeEvent *ev = &e->xexpose;
  743. if(ev->count == 0 && (m = wintomon(ev->window)))
  744. drawbar(m);
  745. }
  746. void
  747. focus(Client *c) {
  748. if(!c || !ISVISIBLE(c))
  749. for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
  750. /* was if(selmon->sel) */
  751. if(selmon->sel && selmon->sel != c)
  752. unfocus(selmon->sel, False);
  753. if(c) {
  754. if(c->mon != selmon)
  755. selmon = c->mon;
  756. if(c->isurgent)
  757. clearurgent(c);
  758. detachstack(c);
  759. attachstack(c);
  760. grabbuttons(c, True);
  761. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  762. setfocus(c);
  763. }
  764. else
  765. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  766. selmon->sel = c;
  767. drawbars();
  768. }
  769. void
  770. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  771. XFocusChangeEvent *ev = &e->xfocus;
  772. if(selmon->sel && ev->window != selmon->sel->win)
  773. setfocus(selmon->sel);
  774. }
  775. void
  776. focusmon(const Arg *arg) {
  777. Monitor *m;
  778. if(!mons->next)
  779. return;
  780. if((m = dirtomon(arg->i)) == selmon)
  781. return;
  782. unfocus(selmon->sel, True);
  783. selmon = m;
  784. focus(NULL);
  785. }
  786. void
  787. focusstack(const Arg *arg) {
  788. Client *c = NULL, *i;
  789. if(!selmon->sel)
  790. return;
  791. if(arg->i > 0) {
  792. for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
  793. if(!c)
  794. for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
  795. }
  796. else {
  797. for(i = selmon->clients; i != selmon->sel; i = i->next)
  798. if(ISVISIBLE(i))
  799. c = i;
  800. if(!c)
  801. for(; i; i = i->next)
  802. if(ISVISIBLE(i))
  803. c = i;
  804. }
  805. if(c) {
  806. focus(c);
  807. restack(selmon);
  808. }
  809. }
  810. unsigned long
  811. getcolor(const char *colstr) {
  812. Colormap cmap = DefaultColormap(dpy, screen);
  813. XColor color;
  814. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  815. die("error, cannot allocate color '%s'\n", colstr);
  816. return color.pixel;
  817. }
  818. Bool
  819. getrootptr(int *x, int *y) {
  820. int di;
  821. unsigned int dui;
  822. Window dummy;
  823. return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
  824. }
  825. long
  826. getstate(Window w) {
  827. int format;
  828. long result = -1;
  829. unsigned char *p = NULL;
  830. unsigned long n, extra;
  831. Atom real;
  832. if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  833. &real, &format, &n, &extra, (unsigned char **)&p) != Success)
  834. return -1;
  835. if(n != 0)
  836. result = *p;
  837. XFree(p);
  838. return result;
  839. }
  840. Bool
  841. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  842. char **list = NULL;
  843. int n;
  844. XTextProperty name;
  845. if(!text || size == 0)
  846. return False;
  847. text[0] = '\0';
  848. XGetTextProperty(dpy, w, &name, atom);
  849. if(!name.nitems)
  850. return False;
  851. if(name.encoding == XA_STRING)
  852. strncpy(text, (char *)name.value, size - 1);
  853. else {
  854. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
  855. strncpy(text, *list, size - 1);
  856. XFreeStringList(list);
  857. }
  858. }
  859. text[size - 1] = '\0';
  860. XFree(name.value);
  861. return True;
  862. }
  863. void
  864. grabbuttons(Client *c, Bool focused) {
  865. updatenumlockmask();
  866. {
  867. unsigned int i, j;
  868. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  869. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  870. if(focused) {
  871. for(i = 0; i < LENGTH(buttons); i++)
  872. if(buttons[i].click == ClkClientWin)
  873. for(j = 0; j < LENGTH(modifiers); j++)
  874. XGrabButton(dpy, buttons[i].button,
  875. buttons[i].mask | modifiers[j],
  876. c->win, False, BUTTONMASK,
  877. GrabModeAsync, GrabModeSync, None, None);
  878. }
  879. else
  880. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  881. BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  882. }
  883. }
  884. void
  885. grabkeys(void) {
  886. updatenumlockmask();
  887. {
  888. unsigned int i, j;
  889. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  890. KeyCode code;
  891. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  892. for(i = 0; i < LENGTH(keys); i++) {
  893. if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  894. for(j = 0; j < LENGTH(modifiers); j++)
  895. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  896. True, GrabModeAsync, GrabModeAsync);
  897. }
  898. }
  899. }
  900. void
  901. initfont(const char *fontstr) {
  902. char *def, **missing;
  903. int n;
  904. missing = NULL;
  905. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  906. if(missing) {
  907. while(n--)
  908. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  909. XFreeStringList(missing);
  910. }
  911. if(dc.font.set) {
  912. XFontStruct **xfonts;
  913. char **font_names;
  914. dc.font.ascent = dc.font.descent = 0;
  915. XExtentsOfFontSet(dc.font.set);
  916. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  917. while(n--) {
  918. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  919. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  920. xfonts++;
  921. }
  922. }
  923. else {
  924. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  925. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  926. die("error, cannot load font: '%s'\n", fontstr);
  927. dc.font.ascent = dc.font.xfont->ascent;
  928. dc.font.descent = dc.font.xfont->descent;
  929. }
  930. dc.font.height = dc.font.ascent + dc.font.descent;
  931. }
  932. #ifdef XINERAMA
  933. static Bool
  934. isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
  935. while(n--)
  936. if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
  937. && unique[n].width == info->width && unique[n].height == info->height)
  938. return False;
  939. return True;
  940. }
  941. #endif /* XINERAMA */
  942. void
  943. keypress(XEvent *e) {
  944. unsigned int i;
  945. KeySym keysym;
  946. XKeyEvent *ev;
  947. ev = &e->xkey;
  948. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  949. for(i = 0; i < LENGTH(keys); i++)
  950. if(keysym == keys[i].keysym
  951. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  952. && keys[i].func)
  953. keys[i].func(&(keys[i].arg));
  954. }
  955. void
  956. killclient(const Arg *arg) {
  957. if(!selmon->sel)
  958. return;
  959. if(!sendevent(selmon->sel, wmatom[WMDelete])) {
  960. XGrabServer(dpy);
  961. XSetErrorHandler(xerrordummy);
  962. XSetCloseDownMode(dpy, DestroyAll);
  963. XKillClient(dpy, selmon->sel->win);
  964. XSync(dpy, False);
  965. XSetErrorHandler(xerror);
  966. XUngrabServer(dpy);
  967. }
  968. }
  969. void
  970. manage(Window w, XWindowAttributes *wa) {
  971. Client *c, *t = NULL;
  972. Window trans = None;
  973. XWindowChanges wc;
  974. if(!(c = calloc(1, sizeof(Client))))
  975. die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  976. c->win = w;
  977. updatetitle(c);
  978. if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
  979. c->mon = t->mon;
  980. c->tags = t->tags;
  981. }
  982. else {
  983. c->mon = selmon;
  984. applyrules(c);
  985. }
  986. /* geometry */
  987. c->x = c->oldx = wa->x + c->mon->wx;
  988. c->y = c->oldy = wa->y + c->mon->wy;
  989. c->w = c->oldw = wa->width;
  990. c->h = c->oldh = wa->height;
  991. c->oldbw = wa->border_width;
  992. if(c->w == c->mon->mw && c->h == c->mon->mh) {
  993. c->isfloating = True;
  994. c->x = c->mon->mx;
  995. c->y = c->mon->my;
  996. c->bw = 0;
  997. }
  998. else {
  999. if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  1000. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  1001. if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  1002. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  1003. c->x = MAX(c->x, c->mon->mx);
  1004. /* only fix client y-offset, if the client center might cover the bar */
  1005. c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
  1006. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  1007. c->bw = borderpx;
  1008. }
  1009. wc.border_width = c->bw;
  1010. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  1011. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  1012. configure(c); /* propagates border_width, if size doesn't change */
  1013. updatesizehints(c);
  1014. updatewmhints(c);
  1015. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  1016. grabbuttons(c, False);
  1017. if(!c->isfloating)
  1018. c->isfloating = c->oldstate = trans != None || c->isfixed;
  1019. if(c->isfloating)
  1020. XRaiseWindow(dpy, c->win);
  1021. attach(c);
  1022. attachstack(c);
  1023. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  1024. XMapWindow(dpy, c->win);
  1025. setclientstate(c, NormalState);
  1026. arrange(c->mon);
  1027. }
  1028. void
  1029. mappingnotify(XEvent *e) {
  1030. XMappingEvent *ev = &e->xmapping;
  1031. XRefreshKeyboardMapping(ev);
  1032. if(ev->request == MappingKeyboard)
  1033. grabkeys();
  1034. }
  1035. void
  1036. maprequest(XEvent *e) {
  1037. static XWindowAttributes wa;
  1038. XMapRequestEvent *ev = &e->xmaprequest;
  1039. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1040. return;
  1041. if(wa.override_redirect)
  1042. return;
  1043. if(!wintoclient(ev->window))
  1044. manage(ev->window, &wa);
  1045. }
  1046. void
  1047. monocle(Monitor *m) {
  1048. unsigned int n = 0;
  1049. Client *c;
  1050. for(c = m->clients; c; c = c->next)
  1051. if(ISVISIBLE(c))
  1052. n++;
  1053. if(n > 0) /* override layout symbol */
  1054. snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
  1055. for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1056. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
  1057. }
  1058. void
  1059. movemouse(const Arg *arg) {
  1060. int x, y, ocx, ocy, nx, ny;
  1061. Client *c;
  1062. Monitor *m;
  1063. XEvent ev;
  1064. if(!(c = selmon->sel))
  1065. return;
  1066. restack(selmon);
  1067. ocx = c->x;
  1068. ocy = c->y;
  1069. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1070. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1071. return;
  1072. if(!getrootptr(&x, &y))
  1073. return;
  1074. do {
  1075. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1076. switch (ev.type) {
  1077. case ConfigureRequest:
  1078. case Expose:
  1079. case MapRequest:
  1080. handler[ev.type](&ev);
  1081. break;
  1082. case MotionNotify:
  1083. nx = ocx + (ev.xmotion.x - x);
  1084. ny = ocy + (ev.xmotion.y - y);
  1085. if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1086. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1087. if(abs(selmon->wx - nx) < snap)
  1088. nx = selmon->wx;
  1089. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1090. nx = selmon->wx + selmon->ww - WIDTH(c);
  1091. if(abs(selmon->wy - ny) < snap)
  1092. ny = selmon->wy;
  1093. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1094. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1095. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1096. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1097. togglefloating(NULL);
  1098. }
  1099. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1100. resize(c, nx, ny, c->w, c->h, True);
  1101. break;
  1102. }
  1103. } while(ev.type != ButtonRelease);
  1104. XUngrabPointer(dpy, CurrentTime);
  1105. if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
  1106. sendmon(c, m);
  1107. selmon = m;
  1108. focus(NULL);
  1109. }
  1110. }
  1111. Client *
  1112. nexttiled(Client *c) {
  1113. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1114. return c;
  1115. }
  1116. Monitor *
  1117. ptrtomon(int x, int y) {
  1118. Monitor *m;
  1119. for(m = mons; m; m = m->next)
  1120. if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
  1121. return m;
  1122. return selmon;
  1123. }
  1124. void
  1125. propertynotify(XEvent *e) {
  1126. Client *c;
  1127. Window trans;
  1128. XPropertyEvent *ev = &e->xproperty;
  1129. if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1130. updatestatus();
  1131. else if(ev->state == PropertyDelete)
  1132. return; /* ignore */
  1133. else if((c = wintoclient(ev->window))) {
  1134. switch (ev->atom) {
  1135. default: break;
  1136. case XA_WM_TRANSIENT_FOR:
  1137. if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
  1138. (c->isfloating = (wintoclient(trans)) != NULL))
  1139. arrange(c->mon);
  1140. break;
  1141. case XA_WM_NORMAL_HINTS:
  1142. updatesizehints(c);
  1143. break;
  1144. case XA_WM_HINTS:
  1145. updatewmhints(c);
  1146. drawbars();
  1147. break;
  1148. }
  1149. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1150. updatetitle(c);
  1151. if(c == c->mon->sel)
  1152. drawbar(c->mon);
  1153. }
  1154. }
  1155. }
  1156. void
  1157. clientmessage(XEvent *e) {
  1158. XClientMessageEvent *cme = &e->xclient;
  1159. Client *c;
  1160. if((c = wintoclient(cme->window))
  1161. && (cme->message_type == netatom[NetWMState] && cme->data.l[1] == netatom[NetWMFullscreen]))
  1162. {
  1163. if(cme->data.l[0]) {
  1164. XChangeProperty(dpy, cme->window, netatom[NetWMState], XA_ATOM, 32,
  1165. PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
  1166. c->oldstate = c->isfloating;
  1167. c->oldbw = c->bw;
  1168. c->bw = 0;
  1169. c->isfloating = True;
  1170. resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
  1171. XRaiseWindow(dpy, c->win);
  1172. }
  1173. else {
  1174. XChangeProperty(dpy, cme->window, netatom[NetWMState], XA_ATOM, 32,
  1175. PropModeReplace, (unsigned char*)0, 0);
  1176. c->isfloating = c->oldstate;
  1177. c->bw = c->oldbw;
  1178. c->x = c->oldx;
  1179. c->y = c->oldy;
  1180. c->w = c->oldw;
  1181. c->h = c->oldh;
  1182. resizeclient(c, c->x, c->y, c->w, c->h);
  1183. arrange(c->mon);
  1184. }
  1185. }
  1186. }
  1187. void
  1188. quit(const Arg *arg) {
  1189. running = False;
  1190. }
  1191. void
  1192. resize(Client *c, int x, int y, int w, int h, Bool interact) {
  1193. if(applysizehints(c, &x, &y, &w, &h, interact))
  1194. resizeclient(c, x, y, w, h);
  1195. }
  1196. void
  1197. resizeclient(Client *c, int x, int y, int w, int h) {
  1198. XWindowChanges wc;
  1199. c->oldx = c->x; c->x = wc.x = x;
  1200. c->oldy = c->y; c->y = wc.y = y;
  1201. c->oldw = c->w; c->w = wc.width = w;
  1202. c->oldh = c->h; c->h = wc.height = h;
  1203. wc.border_width = c->bw;
  1204. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1205. configure(c);
  1206. XSync(dpy, False);
  1207. }
  1208. void
  1209. resizemouse(const Arg *arg) {
  1210. int ocx, ocy;
  1211. int nw, nh;
  1212. Client *c;
  1213. Monitor *m;
  1214. XEvent ev;
  1215. if(!(c = selmon->sel))
  1216. return;
  1217. restack(selmon);
  1218. ocx = c->x;
  1219. ocy = c->y;
  1220. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1221. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1222. return;
  1223. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1224. do {
  1225. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1226. switch(ev.type) {
  1227. case ConfigureRequest:
  1228. case Expose:
  1229. case MapRequest:
  1230. handler[ev.type](&ev);
  1231. break;
  1232. case MotionNotify:
  1233. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1234. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1235. if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
  1236. && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
  1237. {
  1238. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1239. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1240. togglefloating(NULL);
  1241. }
  1242. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1243. resize(c, c->x, c->y, nw, nh, True);
  1244. break;
  1245. }
  1246. } while(ev.type != ButtonRelease);
  1247. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1248. XUngrabPointer(dpy, CurrentTime);
  1249. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1250. if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
  1251. sendmon(c, m);
  1252. selmon = m;
  1253. focus(NULL);
  1254. }
  1255. }
  1256. void
  1257. restack(Monitor *m) {
  1258. Client *c;
  1259. XEvent ev;
  1260. XWindowChanges wc;
  1261. drawbar(m);
  1262. if(!m->sel)
  1263. return;
  1264. if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
  1265. XRaiseWindow(dpy, m->sel->win);
  1266. if(m->lt[m->sellt]->arrange) {
  1267. wc.stack_mode = Below;
  1268. wc.sibling = m->barwin;
  1269. for(c = m->stack; c; c = c->snext)
  1270. if(!c->isfloating && ISVISIBLE(c)) {
  1271. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1272. wc.sibling = c->win;
  1273. }
  1274. }
  1275. XSync(dpy, False);
  1276. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1277. }
  1278. void
  1279. run(void) {
  1280. XEvent ev;
  1281. /* main event loop */
  1282. XSync(dpy, False);
  1283. while(running && !XNextEvent(dpy, &ev)) {
  1284. if(handler[ev.type])
  1285. handler[ev.type](&ev); /* call handler */
  1286. }
  1287. }
  1288. void
  1289. scan(void) {
  1290. unsigned int i, num;
  1291. Window d1, d2, *wins = NULL;
  1292. XWindowAttributes wa;
  1293. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1294. for(i = 0; i < num; i++) {
  1295. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1296. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1297. continue;
  1298. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1299. manage(wins[i], &wa);
  1300. }
  1301. for(i = 0; i < num; i++) { /* now the transients */
  1302. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1303. continue;
  1304. if(XGetTransientForHint(dpy, wins[i], &d1)
  1305. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1306. manage(wins[i], &wa);
  1307. }
  1308. if(wins)
  1309. XFree(wins);
  1310. }
  1311. }
  1312. void
  1313. sendmon(Client *c, Monitor *m) {
  1314. if(c->mon == m)
  1315. return;
  1316. unfocus(c, True);
  1317. detach(c);
  1318. detachstack(c);
  1319. c->mon = m;
  1320. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1321. attach(c);
  1322. attachstack(c);
  1323. focus(NULL);
  1324. arrange(NULL);
  1325. }
  1326. void
  1327. setclientstate(Client *c, long state) {
  1328. long data[] = { state, None };
  1329. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1330. PropModeReplace, (unsigned char *)data, 2);
  1331. }
  1332. Bool
  1333. sendevent(Client *c, Atom proto) {
  1334. int n;
  1335. Atom *protocols;
  1336. Bool exists = False;
  1337. XEvent ev;
  1338. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  1339. while(!exists && n--)
  1340. exists = protocols[n] == proto;
  1341. XFree(protocols);
  1342. }
  1343. if (exists) {
  1344. ev.type = ClientMessage;
  1345. ev.xclient.window = c->win;
  1346. ev.xclient.message_type = wmatom[WMProtocols];
  1347. ev.xclient.format = 32;
  1348. ev.xclient.data.l[0] = proto;
  1349. ev.xclient.data.l[1] = CurrentTime;
  1350. XSendEvent(dpy, c->win, False, NoEventMask, &ev);
  1351. }
  1352. return exists;
  1353. }
  1354. void
  1355. setfocus(Client *c) {
  1356. if (!c->neverfocus)
  1357. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  1358. sendevent(c, wmatom[WMTakeFocus]);
  1359. }
  1360. void
  1361. setlayout(const Arg *arg) {
  1362. if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
  1363. selmon->sellt ^= 1;
  1364. if(arg && arg->v)
  1365. selmon->lt[selmon->sellt] = (Layout *)arg->v;
  1366. strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
  1367. if(selmon->sel)
  1368. arrange(selmon);
  1369. else
  1370. drawbar(selmon);
  1371. }
  1372. /* arg > 1.0 will set mfact absolutly */
  1373. void
  1374. setmfact(const Arg *arg) {
  1375. float f;
  1376. if(!arg || !selmon->lt[selmon->sellt]->arrange)
  1377. return;
  1378. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1379. if(f < 0.1 || f > 0.9)
  1380. return;
  1381. selmon->mfact = f;
  1382. arrange(selmon);
  1383. }
  1384. void
  1385. setup(void) {
  1386. XSetWindowAttributes wa;
  1387. /* clean up any zombies immediately */
  1388. sigchld(0);
  1389. /* init screen */
  1390. screen = DefaultScreen(dpy);
  1391. root = RootWindow(dpy, screen);
  1392. initfont(font);
  1393. sw = DisplayWidth(dpy, screen);
  1394. sh = DisplayHeight(dpy, screen);
  1395. bh = dc.h = dc.font.height + 2;
  1396. updategeom();
  1397. /* init atoms */
  1398. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1399. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1400. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1401. wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", 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. if (wmh->flags & InputHint) c->neverfocus = !wmh->input;
  1785. else c->neverfocus = False;
  1786. XFree(wmh);
  1787. }
  1788. }
  1789. void
  1790. view(const Arg *arg) {
  1791. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1792. return;
  1793. selmon->seltags ^= 1; /* toggle sel tagset */
  1794. if(arg->ui & TAGMASK)
  1795. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1796. arrange(selmon);
  1797. }
  1798. Client *
  1799. wintoclient(Window w) {
  1800. Client *c;
  1801. Monitor *m;
  1802. for(m = mons; m; m = m->next)
  1803. for(c = m->clients; c; c = c->next)
  1804. if(c->win == w)
  1805. return c;
  1806. return NULL;
  1807. }
  1808. Monitor *
  1809. wintomon(Window w) {
  1810. int x, y;
  1811. Client *c;
  1812. Monitor *m;
  1813. if(w == root && getrootptr(&x, &y))
  1814. return ptrtomon(x, y);
  1815. for(m = mons; m; m = m->next)
  1816. if(w == m->barwin)
  1817. return m;
  1818. if((c = wintoclient(w)))
  1819. return c->mon;
  1820. return selmon;
  1821. }
  1822. /* There's no way to check accesses to destroyed windows, thus those cases are
  1823. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1824. * default error handler, which may call exit. */
  1825. int
  1826. xerror(Display *dpy, XErrorEvent *ee) {
  1827. if(ee->error_code == BadWindow
  1828. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1829. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1830. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1831. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1832. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1833. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1834. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1835. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1836. return 0;
  1837. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1838. ee->request_code, ee->error_code);
  1839. return xerrorxlib(dpy, ee); /* may call exit */
  1840. }
  1841. int
  1842. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1843. return 0;
  1844. }
  1845. /* Startup Error handler to check if another window manager
  1846. * is already running. */
  1847. int
  1848. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1849. die("dwm: another window manager is already running\n");
  1850. return -1;
  1851. }
  1852. void
  1853. zoom(const Arg *arg) {
  1854. Client *c = selmon->sel;
  1855. if(!selmon->lt[selmon->sellt]->arrange
  1856. || selmon->lt[selmon->sellt]->arrange == monocle
  1857. || (selmon->sel && selmon->sel->isfloating))
  1858. return;
  1859. if(c == nexttiled(selmon->clients))
  1860. if(!c || !(c = nexttiled(c->next)))
  1861. return;
  1862. detach(c);
  1863. attach(c);
  1864. focus(c);
  1865. arrange(c->mon);
  1866. }
  1867. int
  1868. main(int argc, char *argv[]) {
  1869. if(argc == 2 && !strcmp("-v", argv[1]))
  1870. die("dwm-"VERSION", © 2006-2010 dwm engineers, see LICENSE for details\n");
  1871. else if(argc != 1)
  1872. die("usage: dwm [-v]\n");
  1873. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1874. fputs("warning: no locale support\n", stderr);
  1875. if(!(dpy = XOpenDisplay(NULL)))
  1876. die("dwm: cannot open display\n");
  1877. checkotherwm();
  1878. setup();
  1879. scan();
  1880. run();
  1881. cleanup();
  1882. XCloseDisplay(dpy);
  1883. return 0;
  1884. }