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.

2048 lines
50 KiB

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