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.

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