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.

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