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.

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