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.

2073 lines
51 KiB

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