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.

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