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
17 years ago
16 years ago
16 years ago
14 years ago
14 years ago
17 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
17 years ago
  1. /* See LICENSE file for copyright and license details.
  2. *
  3. * dynamic window manager is designed like any other X client as well. It is
  4. * driven through handling X events. In contrast to other X clients, a window
  5. * manager selects for SubstructureRedirectMask on the root window, to receive
  6. * events about window (dis-)appearance. Only one X connection at a time is
  7. * allowed to select for this event mask.
  8. *
  9. * The event handlers of dwm are organized in an array which is accessed
  10. * whenever a new event has been fetched. This allows event dispatching
  11. * in O(1) time.
  12. *
  13. * Each child of the root window is called a client, except windows which have
  14. * set the override_redirect flag. Clients are organized in a linked client
  15. * list on each monitor, the focus history is remembered through a stack list
  16. * on each monitor. Each client contains a bit array to indicate the tags of a
  17. * client.
  18. *
  19. * Keys and tagging rules are organized as arrays and defined in config.h.
  20. *
  21. * To understand everything else, start reading main().
  22. */
  23. #include <errno.h>
  24. #include <locale.h>
  25. #include <stdarg.h>
  26. #include <signal.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include <sys/types.h>
  32. #include <sys/wait.h>
  33. #include <X11/cursorfont.h>
  34. #include <X11/keysym.h>
  35. #include <X11/Xatom.h>
  36. #include <X11/Xlib.h>
  37. #include <X11/Xproto.h>
  38. #include <X11/Xutil.h>
  39. #ifdef XINERAMA
  40. #include <X11/extensions/Xinerama.h>
  41. #endif /* XINERAMA */
  42. /* macros */
  43. #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
  44. #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
  45. #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
  46. #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
  47. #define LENGTH(X) (sizeof X / sizeof X[0])
  48. #define MAX(A, B) ((A) > (B) ? (A) : (B))
  49. #define MIN(A, B) ((A) < (B) ? (A) : (B))
  50. #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
  51. #define WIDTH(X) ((X)->w + 2 * (X)->bw)
  52. #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
  53. #define TAGMASK ((1 << LENGTH(tags)) - 1)
  54. #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
  55. /* enums */
  56. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  57. enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
  58. enum { NetSupported, NetWMName, NetWMState,
  59. NetWMFullscreen, 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. if(ev->window == root) {
  553. sw = ev->width;
  554. sh = ev->height;
  555. if(updategeom()) {
  556. if(dc.drawable != 0)
  557. XFreePixmap(dpy, dc.drawable);
  558. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  559. updatebars();
  560. for(m = mons; m; m = m->next)
  561. XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
  562. arrange(NULL);
  563. }
  564. }
  565. }
  566. void
  567. configurerequest(XEvent *e) {
  568. Client *c;
  569. Monitor *m;
  570. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  571. XWindowChanges wc;
  572. if((c = wintoclient(ev->window))) {
  573. if(ev->value_mask & CWBorderWidth)
  574. c->bw = ev->border_width;
  575. else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
  576. m = c->mon;
  577. if(ev->value_mask & CWX)
  578. c->x = m->mx + ev->x;
  579. if(ev->value_mask & CWY)
  580. c->y = m->my + ev->y;
  581. if(ev->value_mask & CWWidth)
  582. c->w = ev->width;
  583. if(ev->value_mask & CWHeight)
  584. c->h = ev->height;
  585. if((c->x + c->w) > m->mx + m->mw && c->isfloating)
  586. c->x = m->mx + (m->mw / 2 - c->w / 2); /* center in x direction */
  587. if((c->y + c->h) > m->my + m->mh && c->isfloating)
  588. c->y = m->my + (m->mh / 2 - c->h / 2); /* center in y direction */
  589. if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
  590. configure(c);
  591. if(ISVISIBLE(c))
  592. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  593. }
  594. else
  595. configure(c);
  596. }
  597. else {
  598. wc.x = ev->x;
  599. wc.y = ev->y;
  600. wc.width = ev->width;
  601. wc.height = ev->height;
  602. wc.border_width = ev->border_width;
  603. wc.sibling = ev->above;
  604. wc.stack_mode = ev->detail;
  605. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  606. }
  607. XSync(dpy, False);
  608. }
  609. Monitor *
  610. createmon(void) {
  611. Monitor *m;
  612. if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
  613. die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
  614. m->tagset[0] = m->tagset[1] = 1;
  615. m->mfact = mfact;
  616. m->showbar = showbar;
  617. m->topbar = topbar;
  618. m->lt[0] = &layouts[0];
  619. m->lt[1] = &layouts[1 % LENGTH(layouts)];
  620. strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
  621. return m;
  622. }
  623. void
  624. destroynotify(XEvent *e) {
  625. Client *c;
  626. XDestroyWindowEvent *ev = &e->xdestroywindow;
  627. if((c = wintoclient(ev->window)))
  628. unmanage(c, True);
  629. }
  630. void
  631. detach(Client *c) {
  632. Client **tc;
  633. for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
  634. *tc = c->next;
  635. }
  636. void
  637. detachstack(Client *c) {
  638. Client **tc, *t;
  639. for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
  640. *tc = c->snext;
  641. if(c == c->mon->sel) {
  642. for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
  643. c->mon->sel = t;
  644. }
  645. }
  646. void
  647. die(const char *errstr, ...) {
  648. va_list ap;
  649. va_start(ap, errstr);
  650. vfprintf(stderr, errstr, ap);
  651. va_end(ap);
  652. exit(EXIT_FAILURE);
  653. }
  654. Monitor *
  655. dirtomon(int dir) {
  656. Monitor *m = NULL;
  657. if(dir > 0) {
  658. if(!(m = selmon->next))
  659. m = mons;
  660. }
  661. else {
  662. if(selmon == mons)
  663. for(m = mons; m->next; m = m->next);
  664. else
  665. for(m = mons; m->next != selmon; m = m->next);
  666. }
  667. return m;
  668. }
  669. void
  670. drawbar(Monitor *m) {
  671. int x;
  672. unsigned int i, occ = 0, urg = 0;
  673. unsigned long *col;
  674. Client *c;
  675. for(c = m->clients; c; c = c->next) {
  676. occ |= c->tags;
  677. if(c->isurgent)
  678. urg |= c->tags;
  679. }
  680. dc.x = 0;
  681. for(i = 0; i < LENGTH(tags); i++) {
  682. dc.w = TEXTW(tags[i]);
  683. col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
  684. drawtext(tags[i], col, urg & 1 << i);
  685. drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
  686. occ & 1 << i, urg & 1 << i, col);
  687. dc.x += dc.w;
  688. }
  689. dc.w = blw = TEXTW(m->ltsymbol);
  690. drawtext(m->ltsymbol, dc.norm, False);
  691. dc.x += dc.w;
  692. x = dc.x;
  693. if(m == selmon) { /* status is only drawn on selected monitor */
  694. dc.w = TEXTW(stext);
  695. dc.x = m->ww - dc.w;
  696. if(dc.x < x) {
  697. dc.x = x;
  698. dc.w = m->ww - x;
  699. }
  700. drawtext(stext, dc.norm, False);
  701. }
  702. else
  703. dc.x = m->ww;
  704. if((dc.w = dc.x - x) > bh) {
  705. dc.x = x;
  706. if(m->sel) {
  707. col = m == selmon ? dc.sel : dc.norm;
  708. drawtext(m->sel->name, col, False);
  709. drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
  710. }
  711. else
  712. drawtext(NULL, dc.norm, False);
  713. }
  714. XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
  715. XSync(dpy, False);
  716. }
  717. void
  718. drawbars(void) {
  719. Monitor *m;
  720. for(m = mons; m; m = m->next)
  721. drawbar(m);
  722. }
  723. void
  724. drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  725. int x;
  726. XGCValues gcv;
  727. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  728. gcv.foreground = col[invert ? ColBG : ColFG];
  729. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  730. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  731. r.x = dc.x + 1;
  732. r.y = dc.y + 1;
  733. if(filled) {
  734. r.width = r.height = x + 1;
  735. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  736. }
  737. else if(empty) {
  738. r.width = r.height = x;
  739. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  740. }
  741. }
  742. void
  743. drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
  744. char buf[256];
  745. int i, x, y, h, len, olen;
  746. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  747. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  748. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  749. if(!text)
  750. return;
  751. olen = strlen(text);
  752. h = dc.font.ascent + dc.font.descent;
  753. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  754. x = dc.x + (h / 2);
  755. /* shorten text if necessary */
  756. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  757. if(!len)
  758. return;
  759. memcpy(buf, text, len);
  760. if(len < olen)
  761. for(i = len; i && i > len - 3; buf[--i] = '.');
  762. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  763. if(dc.font.set)
  764. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  765. else
  766. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  767. }
  768. void
  769. enternotify(XEvent *e) {
  770. Monitor *m;
  771. XCrossingEvent *ev = &e->xcrossing;
  772. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  773. return;
  774. if((m = wintomon(ev->window)) && m != selmon) {
  775. unfocus(selmon->sel, True);
  776. selmon = m;
  777. }
  778. focus((wintoclient(ev->window)));
  779. }
  780. void
  781. expose(XEvent *e) {
  782. Monitor *m;
  783. XExposeEvent *ev = &e->xexpose;
  784. if(ev->count == 0 && (m = wintomon(ev->window)))
  785. drawbar(m);
  786. }
  787. void
  788. focus(Client *c) {
  789. if(!c || !ISVISIBLE(c))
  790. for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
  791. /* was if(selmon->sel) */
  792. if(selmon->sel && selmon->sel != c)
  793. unfocus(selmon->sel, False);
  794. if(c) {
  795. if(c->mon != selmon)
  796. selmon = c->mon;
  797. if(c->isurgent)
  798. clearurgent(c);
  799. detachstack(c);
  800. attachstack(c);
  801. grabbuttons(c, True);
  802. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  803. setfocus(c);
  804. }
  805. else
  806. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  807. selmon->sel = c;
  808. drawbars();
  809. }
  810. void
  811. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  812. XFocusChangeEvent *ev = &e->xfocus;
  813. if(selmon->sel && ev->window != selmon->sel->win)
  814. setfocus(selmon->sel);
  815. }
  816. void
  817. focusmon(const Arg *arg) {
  818. Monitor *m;
  819. if(!mons->next)
  820. return;
  821. if((m = dirtomon(arg->i)) == selmon)
  822. return;
  823. unfocus(selmon->sel, True);
  824. selmon = m;
  825. focus(NULL);
  826. }
  827. void
  828. focusstack(const Arg *arg) {
  829. Client *c = NULL, *i;
  830. if(!selmon->sel)
  831. return;
  832. if(arg->i > 0) {
  833. for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
  834. if(!c)
  835. for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
  836. }
  837. else {
  838. for(i = selmon->clients; i != selmon->sel; i = i->next)
  839. if(ISVISIBLE(i))
  840. c = i;
  841. if(!c)
  842. for(; i; i = i->next)
  843. if(ISVISIBLE(i))
  844. c = i;
  845. }
  846. if(c) {
  847. focus(c);
  848. restack(selmon);
  849. }
  850. }
  851. unsigned long
  852. getcolor(const char *colstr) {
  853. Colormap cmap = DefaultColormap(dpy, screen);
  854. XColor color;
  855. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  856. die("error, cannot allocate color '%s'\n", colstr);
  857. return color.pixel;
  858. }
  859. Bool
  860. getrootptr(int *x, int *y) {
  861. int di;
  862. unsigned int dui;
  863. Window dummy;
  864. return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
  865. }
  866. long
  867. getstate(Window w) {
  868. int format;
  869. long result = -1;
  870. unsigned char *p = NULL;
  871. unsigned long n, extra;
  872. Atom real;
  873. if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  874. &real, &format, &n, &extra, (unsigned char **)&p) != Success)
  875. return -1;
  876. if(n != 0)
  877. result = *p;
  878. XFree(p);
  879. return result;
  880. }
  881. Bool
  882. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  883. char **list = NULL;
  884. int n;
  885. XTextProperty name;
  886. if(!text || size == 0)
  887. return False;
  888. text[0] = '\0';
  889. XGetTextProperty(dpy, w, &name, atom);
  890. if(!name.nitems)
  891. return False;
  892. if(name.encoding == XA_STRING)
  893. strncpy(text, (char *)name.value, size - 1);
  894. else {
  895. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
  896. strncpy(text, *list, size - 1);
  897. XFreeStringList(list);
  898. }
  899. }
  900. text[size - 1] = '\0';
  901. XFree(name.value);
  902. return True;
  903. }
  904. void
  905. grabbuttons(Client *c, Bool focused) {
  906. updatenumlockmask();
  907. {
  908. unsigned int i, j;
  909. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  910. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  911. if(focused) {
  912. for(i = 0; i < LENGTH(buttons); i++)
  913. if(buttons[i].click == ClkClientWin)
  914. for(j = 0; j < LENGTH(modifiers); j++)
  915. XGrabButton(dpy, buttons[i].button,
  916. buttons[i].mask | modifiers[j],
  917. c->win, False, BUTTONMASK,
  918. GrabModeAsync, GrabModeSync, None, None);
  919. }
  920. else
  921. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  922. BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  923. }
  924. }
  925. void
  926. grabkeys(void) {
  927. updatenumlockmask();
  928. {
  929. unsigned int i, j;
  930. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  931. KeyCode code;
  932. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  933. for(i = 0; i < LENGTH(keys); i++) {
  934. if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  935. for(j = 0; j < LENGTH(modifiers); j++)
  936. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  937. True, GrabModeAsync, GrabModeAsync);
  938. }
  939. }
  940. }
  941. void
  942. initfont(const char *fontstr) {
  943. char *def, **missing;
  944. int n;
  945. missing = NULL;
  946. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  947. if(missing) {
  948. while(n--)
  949. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  950. XFreeStringList(missing);
  951. }
  952. if(dc.font.set) {
  953. XFontStruct **xfonts;
  954. char **font_names;
  955. dc.font.ascent = dc.font.descent = 0;
  956. XExtentsOfFontSet(dc.font.set);
  957. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  958. while(n--) {
  959. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  960. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  961. xfonts++;
  962. }
  963. }
  964. else {
  965. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  966. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  967. die("error, cannot load font: '%s'\n", fontstr);
  968. dc.font.ascent = dc.font.xfont->ascent;
  969. dc.font.descent = dc.font.xfont->descent;
  970. }
  971. dc.font.height = dc.font.ascent + dc.font.descent;
  972. }
  973. #ifdef XINERAMA
  974. static Bool
  975. isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
  976. while(n--)
  977. if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
  978. && unique[n].width == info->width && unique[n].height == info->height)
  979. return False;
  980. return True;
  981. }
  982. #endif /* XINERAMA */
  983. void
  984. keypress(XEvent *e) {
  985. unsigned int i;
  986. KeySym keysym;
  987. XKeyEvent *ev;
  988. ev = &e->xkey;
  989. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  990. for(i = 0; i < LENGTH(keys); i++)
  991. if(keysym == keys[i].keysym
  992. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  993. && keys[i].func)
  994. keys[i].func(&(keys[i].arg));
  995. }
  996. void
  997. killclient(const Arg *arg) {
  998. if(!selmon->sel)
  999. return;
  1000. if(!sendevent(selmon->sel, wmatom[WMDelete])) {
  1001. XGrabServer(dpy);
  1002. XSetErrorHandler(xerrordummy);
  1003. XSetCloseDownMode(dpy, DestroyAll);
  1004. XKillClient(dpy, selmon->sel->win);
  1005. XSync(dpy, False);
  1006. XSetErrorHandler(xerror);
  1007. XUngrabServer(dpy);
  1008. }
  1009. }
  1010. void
  1011. manage(Window w, XWindowAttributes *wa) {
  1012. Client *c, *t = NULL;
  1013. Window trans = None;
  1014. XWindowChanges wc;
  1015. if(!(c = calloc(1, sizeof(Client))))
  1016. die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  1017. c->win = w;
  1018. updatetitle(c);
  1019. if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
  1020. c->mon = t->mon;
  1021. c->tags = t->tags;
  1022. }
  1023. else {
  1024. c->mon = selmon;
  1025. applyrules(c);
  1026. }
  1027. /* geometry */
  1028. c->x = c->oldx = wa->x + c->mon->wx;
  1029. c->y = c->oldy = wa->y + c->mon->wy;
  1030. c->w = c->oldw = wa->width;
  1031. c->h = c->oldh = wa->height;
  1032. c->oldbw = wa->border_width;
  1033. if(c->w == c->mon->mw && c->h == c->mon->mh) {
  1034. c->isfloating = True;
  1035. c->x = c->mon->mx;
  1036. c->y = c->mon->my;
  1037. c->bw = 0;
  1038. }
  1039. else {
  1040. if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  1041. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  1042. if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  1043. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  1044. c->x = MAX(c->x, c->mon->mx);
  1045. /* only fix client y-offset, if the client center might cover the bar */
  1046. c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
  1047. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  1048. c->bw = borderpx;
  1049. }
  1050. wc.border_width = c->bw;
  1051. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  1052. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  1053. configure(c); /* propagates border_width, if size doesn't change */
  1054. updatesizehints(c);
  1055. updatewmhints(c);
  1056. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  1057. grabbuttons(c, False);
  1058. if(!c->isfloating)
  1059. c->isfloating = c->oldstate = trans != None || c->isfixed;
  1060. if(c->isfloating)
  1061. XRaiseWindow(dpy, c->win);
  1062. attach(c);
  1063. attachstack(c);
  1064. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  1065. XMapWindow(dpy, c->win);
  1066. setclientstate(c, NormalState);
  1067. arrange(c->mon);
  1068. }
  1069. void
  1070. mappingnotify(XEvent *e) {
  1071. XMappingEvent *ev = &e->xmapping;
  1072. XRefreshKeyboardMapping(ev);
  1073. if(ev->request == MappingKeyboard)
  1074. grabkeys();
  1075. }
  1076. void
  1077. maprequest(XEvent *e) {
  1078. static XWindowAttributes wa;
  1079. XMapRequestEvent *ev = &e->xmaprequest;
  1080. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1081. return;
  1082. if(wa.override_redirect)
  1083. return;
  1084. if(!wintoclient(ev->window))
  1085. manage(ev->window, &wa);
  1086. }
  1087. void
  1088. monocle(Monitor *m) {
  1089. unsigned int n = 0;
  1090. Client *c;
  1091. for(c = m->clients; c; c = c->next)
  1092. if(ISVISIBLE(c))
  1093. n++;
  1094. if(n > 0) /* override layout symbol */
  1095. snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
  1096. for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1097. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
  1098. }
  1099. void
  1100. movemouse(const Arg *arg) {
  1101. int x, y, ocx, ocy, nx, ny;
  1102. Client *c;
  1103. Monitor *m;
  1104. XEvent ev;
  1105. if(!(c = selmon->sel))
  1106. return;
  1107. restack(selmon);
  1108. ocx = c->x;
  1109. ocy = c->y;
  1110. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1111. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1112. return;
  1113. if(!getrootptr(&x, &y))
  1114. return;
  1115. do {
  1116. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1117. switch(ev.type) {
  1118. case ConfigureRequest:
  1119. case Expose:
  1120. case MapRequest:
  1121. handler[ev.type](&ev);
  1122. break;
  1123. case MotionNotify:
  1124. nx = ocx + (ev.xmotion.x - x);
  1125. ny = ocy + (ev.xmotion.y - y);
  1126. if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1127. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1128. if(abs(selmon->wx - nx) < snap)
  1129. nx = selmon->wx;
  1130. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1131. nx = selmon->wx + selmon->ww - WIDTH(c);
  1132. if(abs(selmon->wy - ny) < snap)
  1133. ny = selmon->wy;
  1134. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1135. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1136. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1137. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1138. togglefloating(NULL);
  1139. }
  1140. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1141. resize(c, nx, ny, c->w, c->h, True);
  1142. break;
  1143. }
  1144. } while(ev.type != ButtonRelease);
  1145. XUngrabPointer(dpy, CurrentTime);
  1146. if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
  1147. sendmon(c, m);
  1148. selmon = m;
  1149. focus(NULL);
  1150. }
  1151. }
  1152. Client *
  1153. nexttiled(Client *c) {
  1154. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1155. return c;
  1156. }
  1157. void
  1158. pop(Client *c) {
  1159. detach(c);
  1160. attach(c);
  1161. focus(c);
  1162. arrange(c->mon);
  1163. }
  1164. void
  1165. propertynotify(XEvent *e) {
  1166. Client *c;
  1167. Window trans;
  1168. XPropertyEvent *ev = &e->xproperty;
  1169. if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1170. updatestatus();
  1171. else if(ev->state == PropertyDelete)
  1172. return; /* ignore */
  1173. else if((c = wintoclient(ev->window))) {
  1174. switch(ev->atom) {
  1175. default: break;
  1176. case XA_WM_TRANSIENT_FOR:
  1177. if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
  1178. (c->isfloating = (wintoclient(trans)) != NULL))
  1179. arrange(c->mon);
  1180. break;
  1181. case XA_WM_NORMAL_HINTS:
  1182. updatesizehints(c);
  1183. break;
  1184. case XA_WM_HINTS:
  1185. updatewmhints(c);
  1186. drawbars();
  1187. break;
  1188. }
  1189. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1190. updatetitle(c);
  1191. if(c == c->mon->sel)
  1192. drawbar(c->mon);
  1193. }
  1194. }
  1195. }
  1196. Monitor *
  1197. ptrtomon(int x, int y) {
  1198. Monitor *m;
  1199. for(m = mons; m; m = m->next)
  1200. if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
  1201. return m;
  1202. return selmon;
  1203. }
  1204. void
  1205. quit(const Arg *arg) {
  1206. running = False;
  1207. }
  1208. void
  1209. resize(Client *c, int x, int y, int w, int h, Bool interact) {
  1210. if(applysizehints(c, &x, &y, &w, &h, interact))
  1211. resizeclient(c, x, y, w, h);
  1212. }
  1213. void
  1214. resizeclient(Client *c, int x, int y, int w, int h) {
  1215. XWindowChanges wc;
  1216. c->oldx = c->x; c->x = wc.x = x;
  1217. c->oldy = c->y; c->y = wc.y = y;
  1218. c->oldw = c->w; c->w = wc.width = w;
  1219. c->oldh = c->h; c->h = wc.height = h;
  1220. wc.border_width = c->bw;
  1221. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1222. configure(c);
  1223. XSync(dpy, False);
  1224. }
  1225. void
  1226. resizemouse(const Arg *arg) {
  1227. int ocx, ocy;
  1228. int nw, nh;
  1229. Client *c;
  1230. Monitor *m;
  1231. XEvent ev;
  1232. if(!(c = selmon->sel))
  1233. return;
  1234. restack(selmon);
  1235. ocx = c->x;
  1236. ocy = c->y;
  1237. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1238. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1239. return;
  1240. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1241. do {
  1242. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1243. switch(ev.type) {
  1244. case ConfigureRequest:
  1245. case Expose:
  1246. case MapRequest:
  1247. handler[ev.type](&ev);
  1248. break;
  1249. case MotionNotify:
  1250. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1251. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1252. if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
  1253. && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
  1254. {
  1255. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1256. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1257. togglefloating(NULL);
  1258. }
  1259. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1260. resize(c, c->x, c->y, nw, nh, True);
  1261. break;
  1262. }
  1263. } while(ev.type != ButtonRelease);
  1264. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1265. XUngrabPointer(dpy, CurrentTime);
  1266. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1267. if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
  1268. sendmon(c, m);
  1269. selmon = m;
  1270. focus(NULL);
  1271. }
  1272. }
  1273. void
  1274. restack(Monitor *m) {
  1275. Client *c;
  1276. XEvent ev;
  1277. XWindowChanges wc;
  1278. drawbar(m);
  1279. if(!m->sel)
  1280. return;
  1281. if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
  1282. XRaiseWindow(dpy, m->sel->win);
  1283. if(m->lt[m->sellt]->arrange) {
  1284. wc.stack_mode = Below;
  1285. wc.sibling = m->barwin;
  1286. for(c = m->stack; c; c = c->snext)
  1287. if(!c->isfloating && ISVISIBLE(c)) {
  1288. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1289. wc.sibling = c->win;
  1290. }
  1291. }
  1292. XSync(dpy, False);
  1293. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1294. }
  1295. void
  1296. run(void) {
  1297. XEvent ev;
  1298. /* main event loop */
  1299. XSync(dpy, False);
  1300. while(running && !XNextEvent(dpy, &ev)) {
  1301. if(handler[ev.type])
  1302. handler[ev.type](&ev); /* call handler */
  1303. }
  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 + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
  1526. y = m->wy;
  1527. w = (m->wx + mw > c->x + c->w) ? 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->lt[selmon->sellt]->arrange == monocle
  1878. || (selmon->sel && selmon->sel->isfloating))
  1879. return;
  1880. if(c == nexttiled(selmon->clients))
  1881. if(!c || !(c = nexttiled(c->next)))
  1882. return;
  1883. pop(c);
  1884. }
  1885. int
  1886. main(int argc, char *argv[]) {
  1887. if(argc == 2 && !strcmp("-v", argv[1]))
  1888. die("dwm-"VERSION", © 2006-2010 dwm engineers, see LICENSE for details\n");
  1889. else if(argc != 1)
  1890. die("usage: dwm [-v]\n");
  1891. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1892. fputs("warning: no locale support\n", stderr);
  1893. if(!(dpy = XOpenDisplay(NULL)))
  1894. die("dwm: cannot open display\n");
  1895. checkotherwm();
  1896. setup();
  1897. scan();
  1898. run();
  1899. cleanup();
  1900. XCloseDisplay(dpy);
  1901. return EXIT_SUCCESS;
  1902. }