Configuration file for DWM on MacBook Air
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.

2077 lines
51 KiB

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