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.

2174 lines
54 KiB

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