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
51 KiB

17 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 <stdbool.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <sys/types.h>
  33. #include <sys/wait.h>
  34. #include <X11/cursorfont.h>
  35. #include <X11/keysym.h>
  36. #include <X11/Xatom.h>
  37. #include <X11/Xlib.h>
  38. #include <X11/Xproto.h>
  39. #include <X11/Xutil.h>
  40. #ifdef XINERAMA
  41. #include <X11/extensions/Xinerama.h>
  42. #endif /* XINERAMA */
  43. #include <X11/Xft/Xft.h>
  44. #include "drw.h"
  45. #include "util.h"
  46. /* macros */
  47. #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
  48. #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
  49. #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
  50. * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
  51. #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
  52. #define LENGTH(X) (sizeof X / sizeof X[0])
  53. #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
  54. #define WIDTH(X) ((X)->w + 2 * (X)->bw)
  55. #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
  56. #define TAGMASK ((1 << LENGTH(tags)) - 1)
  57. #define TEXTW(X) (drw_text(drw, 0, 0, 0, 0, (X), 0) + drw->fonts[0]->h)
  58. /* enums */
  59. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  60. enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */
  61. enum { NetSupported, NetWMName, NetWMState,
  62. NetWMFullscreen, NetActiveWindow, NetWMWindowType,
  63. NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
  64. enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
  65. enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
  66. ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
  67. typedef union {
  68. int i;
  69. unsigned int ui;
  70. float f;
  71. const void *v;
  72. } Arg;
  73. typedef struct {
  74. unsigned int click;
  75. unsigned int mask;
  76. unsigned int button;
  77. void (*func)(const Arg *arg);
  78. const Arg arg;
  79. } Button;
  80. typedef struct Monitor Monitor;
  81. typedef struct Client Client;
  82. struct Client {
  83. char name[256];
  84. float mina, maxa;
  85. int x, y, w, h;
  86. int oldx, oldy, oldw, oldh;
  87. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  88. int bw, oldbw;
  89. unsigned int tags;
  90. bool isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
  91. Client *next;
  92. Client *snext;
  93. Monitor *mon;
  94. Window win;
  95. };
  96. typedef struct {
  97. unsigned int mod;
  98. KeySym keysym;
  99. void (*func)(const Arg *);
  100. const Arg arg;
  101. } Key;
  102. typedef struct {
  103. const char *symbol;
  104. void (*arrange)(Monitor *);
  105. } Layout;
  106. struct Monitor {
  107. char ltsymbol[16];
  108. float mfact;
  109. int nmaster;
  110. int num;
  111. int by; /* bar geometry */
  112. int mx, my, mw, mh; /* screen size */
  113. int wx, wy, ww, wh; /* window area */
  114. unsigned int seltags;
  115. unsigned int sellt;
  116. unsigned int tagset[2];
  117. bool showbar;
  118. bool topbar;
  119. Client *clients;
  120. Client *sel;
  121. Client *stack;
  122. Monitor *next;
  123. Window barwin;
  124. const Layout *lt[2];
  125. };
  126. typedef struct {
  127. const char *class;
  128. const char *instance;
  129. const char *title;
  130. unsigned int tags;
  131. bool isfloating;
  132. int monitor;
  133. } Rule;
  134. /* function declarations */
  135. static void applyrules(Client *c);
  136. static bool applysizehints(Client *c, int *x, int *y, int *w, int *h, bool interact);
  137. static void arrange(Monitor *m);
  138. static void arrangemon(Monitor *m);
  139. static void attach(Client *c);
  140. static void attachstack(Client *c);
  141. static void buttonpress(XEvent *e);
  142. static void checkotherwm(void);
  143. static void cleanup(void);
  144. static void cleanupmon(Monitor *mon);
  145. static void clearurgent(Client *c);
  146. static void clientmessage(XEvent *e);
  147. static void configure(Client *c);
  148. static void configurenotify(XEvent *e);
  149. static void configurerequest(XEvent *e);
  150. static Monitor *createmon(void);
  151. static void destroynotify(XEvent *e);
  152. static void detach(Client *c);
  153. static void detachstack(Client *c);
  154. static Monitor *dirtomon(int dir);
  155. static void drawbar(Monitor *m);
  156. static void drawbars(void);
  157. static void enternotify(XEvent *e);
  158. static void expose(XEvent *e);
  159. static void focus(Client *c);
  160. static void focusin(XEvent *e);
  161. static void focusmon(const Arg *arg);
  162. static void focusstack(const Arg *arg);
  163. static bool getrootptr(int *x, int *y);
  164. static long getstate(Window w);
  165. static bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  166. static void grabbuttons(Client *c, bool focused);
  167. static void grabkeys(void);
  168. static void incnmaster(const Arg *arg);
  169. static void keypress(XEvent *e);
  170. static void killclient(const Arg *arg);
  171. static void manage(Window w, XWindowAttributes *wa);
  172. static void mappingnotify(XEvent *e);
  173. static void maprequest(XEvent *e);
  174. static void monocle(Monitor *m);
  175. static void motionnotify(XEvent *e);
  176. static void movemouse(const Arg *arg);
  177. static Client *nexttiled(Client *c);
  178. static void pop(Client *);
  179. static void propertynotify(XEvent *e);
  180. static void quit(const Arg *arg);
  181. static Monitor *recttomon(int x, int y, int w, int h);
  182. static void resize(Client *c, int x, int y, int w, int h, bool interact);
  183. static void resizeclient(Client *c, int x, int y, int w, int h);
  184. static void resizemouse(const Arg *arg);
  185. static void restack(Monitor *m);
  186. static void run(void);
  187. static void scan(void);
  188. static bool sendevent(Client *c, Atom proto);
  189. static void sendmon(Client *c, Monitor *m);
  190. static void setclientstate(Client *c, long state);
  191. static void setfocus(Client *c);
  192. static void setfullscreen(Client *c, bool fullscreen);
  193. static void setlayout(const Arg *arg);
  194. static void setmfact(const Arg *arg);
  195. static void setup(void);
  196. static void showhide(Client *c);
  197. static void sigchld(int unused);
  198. static void spawn(const Arg *arg);
  199. static void tag(const Arg *arg);
  200. static void tagmon(const Arg *arg);
  201. static void tile(Monitor *);
  202. static void togglebar(const Arg *arg);
  203. static void togglefloating(const Arg *arg);
  204. static void toggletag(const Arg *arg);
  205. static void toggleview(const Arg *arg);
  206. static void unfocus(Client *c, bool setfocus);
  207. static void unmanage(Client *c, bool destroyed);
  208. static void unmapnotify(XEvent *e);
  209. static bool updategeom(void);
  210. static void updatebarpos(Monitor *m);
  211. static void updatebars(void);
  212. static void updateclientlist(void);
  213. static void updatenumlockmask(void);
  214. static void updatesizehints(Client *c);
  215. static void updatestatus(void);
  216. static void updatewindowtype(Client *c);
  217. static void updatetitle(Client *c);
  218. static void updatewmhints(Client *c);
  219. static void view(const Arg *arg);
  220. static Client *wintoclient(Window w);
  221. static Monitor *wintomon(Window w);
  222. static int xerror(Display *dpy, XErrorEvent *ee);
  223. static int xerrordummy(Display *dpy, XErrorEvent *ee);
  224. static int xerrorstart(Display *dpy, XErrorEvent *ee);
  225. static void zoom(const Arg *arg);
  226. /* variables */
  227. static const char broken[] = "broken";
  228. static char stext[256];
  229. static int screen;
  230. static int sw, sh; /* X display screen geometry width, height */
  231. static int bh, blw = 0; /* bar geometry */
  232. static int (*xerrorxlib)(Display *, XErrorEvent *);
  233. static unsigned int numlockmask = 0;
  234. static void (*handler[LASTEvent]) (XEvent *) = {
  235. [ButtonPress] = buttonpress,
  236. [ClientMessage] = clientmessage,
  237. [ConfigureRequest] = configurerequest,
  238. [ConfigureNotify] = configurenotify,
  239. [DestroyNotify] = destroynotify,
  240. [EnterNotify] = enternotify,
  241. [Expose] = expose,
  242. [FocusIn] = focusin,
  243. [KeyPress] = keypress,
  244. [MappingNotify] = mappingnotify,
  245. [MapRequest] = maprequest,
  246. [MotionNotify] = motionnotify,
  247. [PropertyNotify] = propertynotify,
  248. [UnmapNotify] = unmapnotify
  249. };
  250. static Atom wmatom[WMLast], netatom[NetLast];
  251. static bool running = true;
  252. static Cur *cursor[CurLast];
  253. static ClrScheme scheme[SchemeLast];
  254. static Display *dpy;
  255. static Drw *drw;
  256. static Monitor *mons, *selmon;
  257. static Window root;
  258. /* configuration, allows nested code to access above variables */
  259. #include "config.h"
  260. /* compile-time check if all tags fit into an unsigned int bit array. */
  261. struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
  262. /* function implementations */
  263. void
  264. applyrules(Client *c) {
  265. const char *class, *instance;
  266. unsigned int i;
  267. const Rule *r;
  268. Monitor *m;
  269. XClassHint ch = { NULL, NULL };
  270. /* rule matching */
  271. c->isfloating = false;
  272. c->tags = 0;
  273. XGetClassHint(dpy, c->win, &ch);
  274. class = ch.res_class ? ch.res_class : broken;
  275. instance = ch.res_name ? ch.res_name : broken;
  276. for(i = 0; i < LENGTH(rules); i++) {
  277. r = &rules[i];
  278. if((!r->title || strstr(c->name, r->title))
  279. && (!r->class || strstr(class, r->class))
  280. && (!r->instance || strstr(instance, r->instance)))
  281. {
  282. c->isfloating = r->isfloating;
  283. c->tags |= r->tags;
  284. for(m = mons; m && m->num != r->monitor; m = m->next);
  285. if(m)
  286. c->mon = m;
  287. }
  288. }
  289. if(ch.res_class)
  290. XFree(ch.res_class);
  291. if(ch.res_name)
  292. XFree(ch.res_name);
  293. c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
  294. }
  295. bool
  296. applysizehints(Client *c, int *x, int *y, int *w, int *h, bool interact) {
  297. bool baseismin;
  298. Monitor *m = c->mon;
  299. /* set minimum possible */
  300. *w = MAX(1, *w);
  301. *h = MAX(1, *h);
  302. if(interact) {
  303. if(*x > sw)
  304. *x = sw - WIDTH(c);
  305. if(*y > sh)
  306. *y = sh - HEIGHT(c);
  307. if(*x + *w + 2 * c->bw < 0)
  308. *x = 0;
  309. if(*y + *h + 2 * c->bw < 0)
  310. *y = 0;
  311. }
  312. else {
  313. if(*x >= m->wx + m->ww)
  314. *x = m->wx + m->ww - WIDTH(c);
  315. if(*y >= m->wy + m->wh)
  316. *y = m->wy + m->wh - HEIGHT(c);
  317. if(*x + *w + 2 * c->bw <= m->wx)
  318. *x = m->wx;
  319. if(*y + *h + 2 * c->bw <= m->wy)
  320. *y = m->wy;
  321. }
  322. if(*h < bh)
  323. *h = bh;
  324. if(*w < bh)
  325. *w = bh;
  326. if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
  327. /* see last two sentences in ICCCM 4.1.2.3 */
  328. baseismin = c->basew == c->minw && c->baseh == c->minh;
  329. if(!baseismin) { /* temporarily remove base dimensions */
  330. *w -= c->basew;
  331. *h -= c->baseh;
  332. }
  333. /* adjust for aspect limits */
  334. if(c->mina > 0 && c->maxa > 0) {
  335. if(c->maxa < (float)*w / *h)
  336. *w = *h * c->maxa + 0.5;
  337. else if(c->mina < (float)*h / *w)
  338. *h = *w * c->mina + 0.5;
  339. }
  340. if(baseismin) { /* increment calculation requires this */
  341. *w -= c->basew;
  342. *h -= c->baseh;
  343. }
  344. /* adjust for increment value */
  345. if(c->incw)
  346. *w -= *w % c->incw;
  347. if(c->inch)
  348. *h -= *h % c->inch;
  349. /* restore base dimensions */
  350. *w = MAX(*w + c->basew, c->minw);
  351. *h = MAX(*h + c->baseh, c->minh);
  352. if(c->maxw)
  353. *w = MIN(*w, c->maxw);
  354. if(c->maxh)
  355. *h = MIN(*h, c->maxh);
  356. }
  357. return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
  358. }
  359. void
  360. arrange(Monitor *m) {
  361. if(m)
  362. showhide(m->stack);
  363. else for(m = mons; m; m = m->next)
  364. showhide(m->stack);
  365. if(m) {
  366. arrangemon(m);
  367. restack(m);
  368. } else for(m = mons; m; m = m->next)
  369. arrangemon(m);
  370. }
  371. void
  372. arrangemon(Monitor *m) {
  373. strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
  374. if(m->lt[m->sellt]->arrange)
  375. m->lt[m->sellt]->arrange(m);
  376. }
  377. void
  378. attach(Client *c) {
  379. c->next = c->mon->clients;
  380. c->mon->clients = c;
  381. }
  382. void
  383. attachstack(Client *c) {
  384. c->snext = c->mon->stack;
  385. c->mon->stack = c;
  386. }
  387. void
  388. buttonpress(XEvent *e) {
  389. unsigned int i, x, click;
  390. Arg arg = {0};
  391. Client *c;
  392. Monitor *m;
  393. XButtonPressedEvent *ev = &e->xbutton;
  394. click = ClkRootWin;
  395. /* focus monitor if necessary */
  396. if((m = wintomon(ev->window)) && m != selmon) {
  397. unfocus(selmon->sel, true);
  398. selmon = m;
  399. focus(NULL);
  400. }
  401. if(ev->window == selmon->barwin) {
  402. i = x = 0;
  403. do
  404. x += TEXTW(tags[i]);
  405. while(ev->x >= x && ++i < LENGTH(tags));
  406. if(i < LENGTH(tags)) {
  407. click = ClkTagBar;
  408. arg.ui = 1 << i;
  409. }
  410. else if(ev->x < x + blw)
  411. click = ClkLtSymbol;
  412. else if(ev->x > selmon->ww - TEXTW(stext))
  413. click = ClkStatusText;
  414. else
  415. click = ClkWinTitle;
  416. }
  417. else if((c = wintoclient(ev->window))) {
  418. focus(c);
  419. click = ClkClientWin;
  420. }
  421. for(i = 0; i < LENGTH(buttons); i++)
  422. if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
  423. && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
  424. buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
  425. }
  426. void
  427. checkotherwm(void) {
  428. xerrorxlib = XSetErrorHandler(xerrorstart);
  429. /* this causes an error if some other window manager is running */
  430. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  431. XSync(dpy, False);
  432. XSetErrorHandler(xerror);
  433. XSync(dpy, False);
  434. }
  435. void
  436. cleanup(void) {
  437. Arg a = {.ui = ~0};
  438. Layout foo = { "", NULL };
  439. Monitor *m;
  440. size_t i;
  441. view(&a);
  442. selmon->lt[selmon->sellt] = &foo;
  443. for(m = mons; m; m = m->next)
  444. while(m->stack)
  445. unmanage(m->stack, false);
  446. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  447. while(mons)
  448. cleanupmon(mons);
  449. for(i = 0; i < CurLast; i++)
  450. drw_cur_free(drw, cursor[i]);
  451. for(i = 0; i < SchemeLast; i++) {
  452. drw_clr_free(scheme[i].border);
  453. drw_clr_free(scheme[i].bg);
  454. drw_clr_free(scheme[i].fg);
  455. }
  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. m = ecalloc(1, 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, dx;
  643. unsigned int i, occ = 0, urg = 0;
  644. Client *c;
  645. dx = (drw->fonts[0]->ascent + drw->fonts[0]->descent + 2) / 4;
  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 + 1, 1, dx, dx, 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 + 1, 1, dx, dx, m->sel->isfixed, m->sel->isfloating, 0);
  682. }
  683. else {
  684. drw_setscheme(drw, &scheme[SchemeNorm]);
  685. drw_rect(drw, x, 0, w, bh, 1, 0, 1);
  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->pix);
  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) == True;
  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. c = ecalloc(1, 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 && !c->isfullscreen) {
  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 if(!fullscreen && c->isfullscreen){
  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. unique = ecalloc(nn, sizeof(XineramaScreenInfo));
  1639. for(i = 0, j = 0; i < nn; i++)
  1640. if(isuniquegeom(unique, j, &info[i]))
  1641. memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
  1642. XFree(info);
  1643. nn = j;
  1644. if(n <= nn) {
  1645. for(i = 0; i < (nn - n); i++) { /* new monitors available */
  1646. for(m = mons; m && m->next; m = m->next);
  1647. if(m)
  1648. m->next = createmon();
  1649. else
  1650. mons = createmon();
  1651. }
  1652. for(i = 0, m = mons; i < nn && m; m = m->next, i++)
  1653. if(i >= n
  1654. || (unique[i].x_org != m->mx || unique[i].y_org != m->my
  1655. || unique[i].width != m->mw || unique[i].height != m->mh))
  1656. {
  1657. dirty = true;
  1658. m->num = i;
  1659. m->mx = m->wx = unique[i].x_org;
  1660. m->my = m->wy = unique[i].y_org;
  1661. m->mw = m->ww = unique[i].width;
  1662. m->mh = m->wh = unique[i].height;
  1663. updatebarpos(m);
  1664. }
  1665. }
  1666. else { /* less monitors available nn < n */
  1667. for(i = nn; i < n; i++) {
  1668. for(m = mons; m && m->next; m = m->next);
  1669. while(m->clients) {
  1670. dirty = true;
  1671. c = m->clients;
  1672. m->clients = c->next;
  1673. detachstack(c);
  1674. c->mon = mons;
  1675. attach(c);
  1676. attachstack(c);
  1677. }
  1678. if(m == selmon)
  1679. selmon = mons;
  1680. cleanupmon(m);
  1681. }
  1682. }
  1683. free(unique);
  1684. }
  1685. else
  1686. #endif /* XINERAMA */
  1687. /* default monitor setup */
  1688. {
  1689. if(!mons)
  1690. mons = createmon();
  1691. if(mons->mw != sw || mons->mh != sh) {
  1692. dirty = true;
  1693. mons->mw = mons->ww = sw;
  1694. mons->mh = mons->wh = sh;
  1695. updatebarpos(mons);
  1696. }
  1697. }
  1698. if(dirty) {
  1699. selmon = mons;
  1700. selmon = wintomon(root);
  1701. }
  1702. return dirty;
  1703. }
  1704. void
  1705. updatenumlockmask(void) {
  1706. unsigned int i, j;
  1707. XModifierKeymap *modmap;
  1708. numlockmask = 0;
  1709. modmap = XGetModifierMapping(dpy);
  1710. for(i = 0; i < 8; i++)
  1711. for(j = 0; j < modmap->max_keypermod; j++)
  1712. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1713. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1714. numlockmask = (1 << i);
  1715. XFreeModifiermap(modmap);
  1716. }
  1717. void
  1718. updatesizehints(Client *c) {
  1719. long msize;
  1720. XSizeHints size;
  1721. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1722. /* size is uninitialized, ensure that size.flags aren't used */
  1723. size.flags = PSize;
  1724. if(size.flags & PBaseSize) {
  1725. c->basew = size.base_width;
  1726. c->baseh = size.base_height;
  1727. }
  1728. else if(size.flags & PMinSize) {
  1729. c->basew = size.min_width;
  1730. c->baseh = size.min_height;
  1731. }
  1732. else
  1733. c->basew = c->baseh = 0;
  1734. if(size.flags & PResizeInc) {
  1735. c->incw = size.width_inc;
  1736. c->inch = size.height_inc;
  1737. }
  1738. else
  1739. c->incw = c->inch = 0;
  1740. if(size.flags & PMaxSize) {
  1741. c->maxw = size.max_width;
  1742. c->maxh = size.max_height;
  1743. }
  1744. else
  1745. c->maxw = c->maxh = 0;
  1746. if(size.flags & PMinSize) {
  1747. c->minw = size.min_width;
  1748. c->minh = size.min_height;
  1749. }
  1750. else if(size.flags & PBaseSize) {
  1751. c->minw = size.base_width;
  1752. c->minh = size.base_height;
  1753. }
  1754. else
  1755. c->minw = c->minh = 0;
  1756. if(size.flags & PAspect) {
  1757. c->mina = (float)size.min_aspect.y / size.min_aspect.x;
  1758. c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
  1759. }
  1760. else
  1761. c->maxa = c->mina = 0.0;
  1762. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1763. && c->maxw == c->minw && c->maxh == c->minh);
  1764. }
  1765. void
  1766. updatetitle(Client *c) {
  1767. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1768. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1769. if(c->name[0] == '\0') /* hack to mark broken clients */
  1770. strcpy(c->name, broken);
  1771. }
  1772. void
  1773. updatestatus(void) {
  1774. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1775. strcpy(stext, "dwm-"VERSION);
  1776. drawbar(selmon);
  1777. }
  1778. void
  1779. updatewindowtype(Client *c) {
  1780. Atom state = getatomprop(c, netatom[NetWMState]);
  1781. Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
  1782. if(state == netatom[NetWMFullscreen])
  1783. setfullscreen(c, true);
  1784. if(wtype == netatom[NetWMWindowTypeDialog])
  1785. c->isfloating = true;
  1786. }
  1787. void
  1788. updatewmhints(Client *c) {
  1789. XWMHints *wmh;
  1790. if((wmh = XGetWMHints(dpy, c->win))) {
  1791. if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  1792. wmh->flags &= ~XUrgencyHint;
  1793. XSetWMHints(dpy, c->win, wmh);
  1794. }
  1795. else
  1796. c->isurgent = (wmh->flags & XUrgencyHint) ? true : false;
  1797. if(wmh->flags & InputHint)
  1798. c->neverfocus = !wmh->input;
  1799. else
  1800. c->neverfocus = false;
  1801. XFree(wmh);
  1802. }
  1803. }
  1804. void
  1805. view(const Arg *arg) {
  1806. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1807. return;
  1808. selmon->seltags ^= 1; /* toggle sel tagset */
  1809. if(arg->ui & TAGMASK)
  1810. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1811. focus(NULL);
  1812. arrange(selmon);
  1813. }
  1814. Client *
  1815. wintoclient(Window w) {
  1816. Client *c;
  1817. Monitor *m;
  1818. for(m = mons; m; m = m->next)
  1819. for(c = m->clients; c; c = c->next)
  1820. if(c->win == w)
  1821. return c;
  1822. return NULL;
  1823. }
  1824. Monitor *
  1825. wintomon(Window w) {
  1826. int x, y;
  1827. Client *c;
  1828. Monitor *m;
  1829. if(w == root && getrootptr(&x, &y))
  1830. return recttomon(x, y, 1, 1);
  1831. for(m = mons; m; m = m->next)
  1832. if(w == m->barwin)
  1833. return m;
  1834. if((c = wintoclient(w)))
  1835. return c->mon;
  1836. return selmon;
  1837. }
  1838. /* There's no way to check accesses to destroyed windows, thus those cases are
  1839. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1840. * default error handler, which may call exit. */
  1841. int
  1842. xerror(Display *dpy, XErrorEvent *ee) {
  1843. if(ee->error_code == BadWindow
  1844. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1845. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1846. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1847. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1848. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1849. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1850. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1851. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1852. return 0;
  1853. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1854. ee->request_code, ee->error_code);
  1855. return xerrorxlib(dpy, ee); /* may call exit */
  1856. }
  1857. int
  1858. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1859. return 0;
  1860. }
  1861. /* Startup Error handler to check if another window manager
  1862. * is already running. */
  1863. int
  1864. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1865. die("dwm: another window manager is already running\n");
  1866. return -1;
  1867. }
  1868. void
  1869. zoom(const Arg *arg) {
  1870. Client *c = selmon->sel;
  1871. if(!selmon->lt[selmon->sellt]->arrange
  1872. || (selmon->sel && selmon->sel->isfloating))
  1873. return;
  1874. if(c == nexttiled(selmon->clients))
  1875. if(!c || !(c = nexttiled(c->next)))
  1876. return;
  1877. pop(c);
  1878. }
  1879. int
  1880. main(int argc, char *argv[]) {
  1881. if(argc == 2 && !strcmp("-v", argv[1]))
  1882. die("dwm-"VERSION", © 2006-2015 dwm engineers, see LICENSE for details\n");
  1883. else if(argc != 1)
  1884. die("usage: dwm [-v]\n");
  1885. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1886. fputs("warning: no locale support\n", stderr);
  1887. if(!(dpy = XOpenDisplay(NULL)))
  1888. die("dwm: cannot open display\n");
  1889. checkotherwm();
  1890. setup();
  1891. scan();
  1892. run();
  1893. cleanup();
  1894. XCloseDisplay(dpy);
  1895. return EXIT_SUCCESS;
  1896. }