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

2060 lines
51 KiB

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