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.

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