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

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