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.

2071 lines
51 KiB

9 years ago
9 years ago
9 years ago
17 years ago
9 years ago
16 years ago
9 years ago
9 years ago
14 years ago
9 years ago
9 years ago
9 years ago
17 years ago
16 years ago
9 years ago
9 years ago
9 years ago
16 years ago
17 years ago
9 years ago
17 years ago
9 years ago
9 years ago
16 years ago
9 years ago
9 years ago
9 years ago
9 years ago
16 years ago
16 years ago
16 years ago
16 years ago
9 years ago
9 years ago
16 years ago
9 years ago
9 years ago
9 years ago
16 years ago
9 years ago
9 years ago
9 years ago
9 years ago
16 years ago
9 years ago
9 years ago
16 years ago
13 years ago
9 years ago
9 years ago
16 years ago
16 years ago
9 years ago
9 years ago
17 years ago
17 years ago
9 years ago
17 years ago
9 years ago
16 years ago
16 years ago
16 years ago
9 years ago
9 years ago
9 years ago
16 years ago
16 years ago
9 years ago
9 years ago
16 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
17 years ago
17 years ago
17 years ago
9 years ago
16 years ago
13 years ago
9 years ago
9 years ago
9 years ago
17 years ago
17 years ago
17 years ago
17 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
17 years ago
17 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
17 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 <signal.h>
  26. #include <stdarg.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. int 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. int showbar;
  117. int 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. int isfloating;
  131. int monitor;
  132. } Rule;
  133. /* function declarations */
  134. static void applyrules(Client *c);
  135. static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int 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 int getrootptr(int *x, int *y);
  163. static long getstate(Window w);
  164. static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
  165. static void grabbuttons(Client *c, int 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, int 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 int 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, int 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, int setfocus);
  206. static void unmanage(Client *c, int destroyed);
  207. static void unmapnotify(XEvent *e);
  208. static int 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 int running = 1;
  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 = 0;
  271. c->tags = 0;
  272. XGetClassHint(dpy, c->win, &ch);
  273. class = ch.res_class ? ch.res_class : broken;
  274. instance = ch.res_name ? ch.res_name : broken;
  275. for(i = 0; i < LENGTH(rules); i++) {
  276. r = &rules[i];
  277. if((!r->title || strstr(c->name, r->title))
  278. && (!r->class || strstr(class, r->class))
  279. && (!r->instance || strstr(instance, r->instance)))
  280. {
  281. c->isfloating = r->isfloating;
  282. c->tags |= r->tags;
  283. for(m = mons; m && m->num != r->monitor; m = m->next);
  284. if(m)
  285. c->mon = m;
  286. }
  287. }
  288. if(ch.res_class)
  289. XFree(ch.res_class);
  290. if(ch.res_name)
  291. XFree(ch.res_name);
  292. c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
  293. }
  294. int
  295. applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact) {
  296. int baseismin;
  297. Monitor *m = c->mon;
  298. /* set minimum possible */
  299. *w = MAX(1, *w);
  300. *h = MAX(1, *h);
  301. if(interact) {
  302. if(*x > sw)
  303. *x = sw - WIDTH(c);
  304. if(*y > sh)
  305. *y = sh - HEIGHT(c);
  306. if(*x + *w + 2 * c->bw < 0)
  307. *x = 0;
  308. if(*y + *h + 2 * c->bw < 0)
  309. *y = 0;
  310. }
  311. else {
  312. if(*x >= m->wx + m->ww)
  313. *x = m->wx + m->ww - WIDTH(c);
  314. if(*y >= m->wy + m->wh)
  315. *y = m->wy + m->wh - HEIGHT(c);
  316. if(*x + *w + 2 * c->bw <= m->wx)
  317. *x = m->wx;
  318. if(*y + *h + 2 * c->bw <= m->wy)
  319. *y = m->wy;
  320. }
  321. if(*h < bh)
  322. *h = bh;
  323. if(*w < bh)
  324. *w = bh;
  325. if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
  326. /* see last two sentences in ICCCM 4.1.2.3 */
  327. baseismin = c->basew == c->minw && c->baseh == c->minh;
  328. if(!baseismin) { /* temporarily remove base dimensions */
  329. *w -= c->basew;
  330. *h -= c->baseh;
  331. }
  332. /* adjust for aspect limits */
  333. if(c->mina > 0 && c->maxa > 0) {
  334. if(c->maxa < (float)*w / *h)
  335. *w = *h * c->maxa + 0.5;
  336. else if(c->mina < (float)*h / *w)
  337. *h = *w * c->mina + 0.5;
  338. }
  339. if(baseismin) { /* increment calculation requires this */
  340. *w -= c->basew;
  341. *h -= c->baseh;
  342. }
  343. /* adjust for increment value */
  344. if(c->incw)
  345. *w -= *w % c->incw;
  346. if(c->inch)
  347. *h -= *h % c->inch;
  348. /* restore base dimensions */
  349. *w = MAX(*w + c->basew, c->minw);
  350. *h = MAX(*h + c->baseh, c->minh);
  351. if(c->maxw)
  352. *w = MIN(*w, c->maxw);
  353. if(c->maxh)
  354. *h = MIN(*h, c->maxh);
  355. }
  356. return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
  357. }
  358. void
  359. arrange(Monitor *m) {
  360. if(m)
  361. showhide(m->stack);
  362. else for(m = mons; m; m = m->next)
  363. showhide(m->stack);
  364. if(m) {
  365. arrangemon(m);
  366. restack(m);
  367. } else for(m = mons; m; m = m->next)
  368. arrangemon(m);
  369. }
  370. void
  371. arrangemon(Monitor *m) {
  372. strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
  373. if(m->lt[m->sellt]->arrange)
  374. m->lt[m->sellt]->arrange(m);
  375. }
  376. void
  377. attach(Client *c) {
  378. c->next = c->mon->clients;
  379. c->mon->clients = c;
  380. }
  381. void
  382. attachstack(Client *c) {
  383. c->snext = c->mon->stack;
  384. c->mon->stack = c;
  385. }
  386. void
  387. buttonpress(XEvent *e) {
  388. unsigned int i, x, click;
  389. Arg arg = {0};
  390. Client *c;
  391. Monitor *m;
  392. XButtonPressedEvent *ev = &e->xbutton;
  393. click = ClkRootWin;
  394. /* focus monitor if necessary */
  395. if((m = wintomon(ev->window)) && m != selmon) {
  396. unfocus(selmon->sel, 1);
  397. selmon = m;
  398. focus(NULL);
  399. }
  400. if(ev->window == selmon->barwin) {
  401. i = x = 0;
  402. do
  403. x += TEXTW(tags[i]);
  404. while(ev->x >= x && ++i < LENGTH(tags));
  405. if(i < LENGTH(tags)) {
  406. click = ClkTagBar;
  407. arg.ui = 1 << i;
  408. }
  409. else if(ev->x < x + blw)
  410. click = ClkLtSymbol;
  411. else if(ev->x > selmon->ww - TEXTW(stext))
  412. click = ClkStatusText;
  413. else
  414. click = ClkWinTitle;
  415. }
  416. else if((c = wintoclient(ev->window))) {
  417. focus(c);
  418. click = ClkClientWin;
  419. }
  420. for(i = 0; i < LENGTH(buttons); i++)
  421. if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
  422. && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
  423. buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
  424. }
  425. void
  426. checkotherwm(void) {
  427. xerrorxlib = XSetErrorHandler(xerrorstart);
  428. /* this causes an error if some other window manager is running */
  429. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  430. XSync(dpy, False);
  431. XSetErrorHandler(xerror);
  432. XSync(dpy, False);
  433. }
  434. void
  435. cleanup(void) {
  436. Arg a = {.ui = ~0};
  437. Layout foo = { "", NULL };
  438. Monitor *m;
  439. size_t i;
  440. view(&a);
  441. selmon->lt[selmon->sellt] = &foo;
  442. for(m = mons; m; m = m->next)
  443. while(m->stack)
  444. unmanage(m->stack, 0);
  445. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  446. while(mons)
  447. cleanupmon(mons);
  448. for(i = 0; i < CurLast; i++)
  449. drw_cur_free(drw, cursor[i]);
  450. for(i = 0; i < SchemeLast; i++) {
  451. drw_clr_free(scheme[i].border);
  452. drw_clr_free(scheme[i].bg);
  453. drw_clr_free(scheme[i].fg);
  454. }
  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 = 0;
  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. int 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. m = ecalloc(1, sizeof(Monitor));
  593. m->tagset[0] = m->tagset[1] = 1;
  594. m->mfact = mfact;
  595. m->nmaster = nmaster;
  596. m->showbar = showbar;
  597. m->topbar = topbar;
  598. m->lt[0] = &layouts[0];
  599. m->lt[1] = &layouts[1 % LENGTH(layouts)];
  600. strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
  601. return m;
  602. }
  603. void
  604. destroynotify(XEvent *e) {
  605. Client *c;
  606. XDestroyWindowEvent *ev = &e->xdestroywindow;
  607. if((c = wintoclient(ev->window)))
  608. unmanage(c, 1);
  609. }
  610. void
  611. detach(Client *c) {
  612. Client **tc;
  613. for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
  614. *tc = c->next;
  615. }
  616. void
  617. detachstack(Client *c) {
  618. Client **tc, *t;
  619. for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
  620. *tc = c->snext;
  621. if(c == c->mon->sel) {
  622. for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
  623. c->mon->sel = t;
  624. }
  625. }
  626. Monitor *
  627. dirtomon(int dir) {
  628. Monitor *m = NULL;
  629. if(dir > 0) {
  630. if(!(m = selmon->next))
  631. m = mons;
  632. }
  633. else if(selmon == mons)
  634. for(m = mons; m->next; m = m->next);
  635. else
  636. for(m = mons; m->next != selmon; m = m->next);
  637. return m;
  638. }
  639. void
  640. drawbar(Monitor *m) {
  641. int x, xx, w, dx;
  642. unsigned int i, occ = 0, urg = 0;
  643. Client *c;
  644. dx = (drw->fonts[0]->ascent + drw->fonts[0]->descent + 2) / 4;
  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 + 1, 1, dx, dx, 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 + 1, 1, dx, dx, m->sel->isfixed, m->sel->isfloating, 0);
  681. }
  682. else {
  683. drw_setscheme(drw, &scheme[SchemeNorm]);
  684. drw_rect(drw, x, 0, w, bh, 1, 0, 1);
  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, 1);
  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, 0);
  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, 1);
  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, 0); /* s/1/0/ 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. int
  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. int
  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 0;
  828. text[0] = '\0';
  829. XGetTextProperty(dpy, w, &name, atom);
  830. if(!name.nitems)
  831. return 0;
  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 1;
  843. }
  844. void
  845. grabbuttons(Client *c, int 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 int
  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 0;
  892. return 1;
  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. c = ecalloc(1, sizeof(Client));
  928. c->win = w;
  929. updatetitle(c);
  930. if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
  931. c->mon = t->mon;
  932. c->tags = t->tags;
  933. }
  934. else {
  935. c->mon = selmon;
  936. applyrules(c);
  937. }
  938. /* geometry */
  939. c->x = c->oldx = wa->x;
  940. c->y = c->oldy = wa->y;
  941. c->w = c->oldw = wa->width;
  942. c->h = c->oldh = wa->height;
  943. c->oldbw = wa->border_width;
  944. if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  945. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  946. if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  947. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  948. c->x = MAX(c->x, c->mon->mx);
  949. /* only fix client y-offset, if the client center might cover the bar */
  950. c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
  951. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  952. c->bw = borderpx;
  953. wc.border_width = c->bw;
  954. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  955. XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->pix);
  956. configure(c); /* propagates border_width, if size doesn't change */
  957. updatewindowtype(c);
  958. updatesizehints(c);
  959. updatewmhints(c);
  960. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  961. grabbuttons(c, 0);
  962. if(!c->isfloating)
  963. c->isfloating = c->oldstate = trans != None || c->isfixed;
  964. if(c->isfloating)
  965. XRaiseWindow(dpy, c->win);
  966. attach(c);
  967. attachstack(c);
  968. XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
  969. (unsigned char *) &(c->win), 1);
  970. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  971. setclientstate(c, NormalState);
  972. if (c->mon == selmon)
  973. unfocus(selmon->sel, 0);
  974. c->mon->sel = c;
  975. arrange(c->mon);
  976. XMapWindow(dpy, c->win);
  977. focus(NULL);
  978. }
  979. void
  980. mappingnotify(XEvent *e) {
  981. XMappingEvent *ev = &e->xmapping;
  982. XRefreshKeyboardMapping(ev);
  983. if(ev->request == MappingKeyboard)
  984. grabkeys();
  985. }
  986. void
  987. maprequest(XEvent *e) {
  988. static XWindowAttributes wa;
  989. XMapRequestEvent *ev = &e->xmaprequest;
  990. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  991. return;
  992. if(wa.override_redirect)
  993. return;
  994. if(!wintoclient(ev->window))
  995. manage(ev->window, &wa);
  996. }
  997. void
  998. monocle(Monitor *m) {
  999. unsigned int n = 0;
  1000. Client *c;
  1001. for(c = m->clients; c; c = c->next)
  1002. if(ISVISIBLE(c))
  1003. n++;
  1004. if(n > 0) /* override layout symbol */
  1005. snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
  1006. for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1007. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
  1008. }
  1009. void
  1010. motionnotify(XEvent *e) {
  1011. static Monitor *mon = NULL;
  1012. Monitor *m;
  1013. XMotionEvent *ev = &e->xmotion;
  1014. if(ev->window != root)
  1015. return;
  1016. if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
  1017. unfocus(selmon->sel, 1);
  1018. selmon = m;
  1019. focus(NULL);
  1020. }
  1021. mon = m;
  1022. }
  1023. void
  1024. movemouse(const Arg *arg) {
  1025. int x, y, ocx, ocy, nx, ny;
  1026. Client *c;
  1027. Monitor *m;
  1028. XEvent ev;
  1029. Time lasttime = 0;
  1030. if(!(c = selmon->sel))
  1031. return;
  1032. if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
  1033. return;
  1034. restack(selmon);
  1035. ocx = c->x;
  1036. ocy = c->y;
  1037. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1038. None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
  1039. return;
  1040. if(!getrootptr(&x, &y))
  1041. return;
  1042. do {
  1043. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1044. switch(ev.type) {
  1045. case ConfigureRequest:
  1046. case Expose:
  1047. case MapRequest:
  1048. handler[ev.type](&ev);
  1049. break;
  1050. case MotionNotify:
  1051. if ((ev.xmotion.time - lasttime) <= (1000 / 60))
  1052. continue;
  1053. lasttime = ev.xmotion.time;
  1054. nx = ocx + (ev.xmotion.x - x);
  1055. ny = ocy + (ev.xmotion.y - y);
  1056. if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1057. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1058. if(abs(selmon->wx - nx) < snap)
  1059. nx = selmon->wx;
  1060. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1061. nx = selmon->wx + selmon->ww - WIDTH(c);
  1062. if(abs(selmon->wy - ny) < snap)
  1063. ny = selmon->wy;
  1064. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1065. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1066. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1067. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1068. togglefloating(NULL);
  1069. }
  1070. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1071. resize(c, nx, ny, c->w, c->h, 1);
  1072. break;
  1073. }
  1074. } while(ev.type != ButtonRelease);
  1075. XUngrabPointer(dpy, CurrentTime);
  1076. if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1077. sendmon(c, m);
  1078. selmon = m;
  1079. focus(NULL);
  1080. }
  1081. }
  1082. Client *
  1083. nexttiled(Client *c) {
  1084. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1085. return c;
  1086. }
  1087. void
  1088. pop(Client *c) {
  1089. detach(c);
  1090. attach(c);
  1091. focus(c);
  1092. arrange(c->mon);
  1093. }
  1094. void
  1095. propertynotify(XEvent *e) {
  1096. Client *c;
  1097. Window trans;
  1098. XPropertyEvent *ev = &e->xproperty;
  1099. if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1100. updatestatus();
  1101. else if(ev->state == PropertyDelete)
  1102. return; /* ignore */
  1103. else if((c = wintoclient(ev->window))) {
  1104. switch(ev->atom) {
  1105. default: break;
  1106. case XA_WM_TRANSIENT_FOR:
  1107. if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
  1108. (c->isfloating = (wintoclient(trans)) != NULL))
  1109. arrange(c->mon);
  1110. break;
  1111. case XA_WM_NORMAL_HINTS:
  1112. updatesizehints(c);
  1113. break;
  1114. case XA_WM_HINTS:
  1115. updatewmhints(c);
  1116. drawbars();
  1117. break;
  1118. }
  1119. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1120. updatetitle(c);
  1121. if(c == c->mon->sel)
  1122. drawbar(c->mon);
  1123. }
  1124. if(ev->atom == netatom[NetWMWindowType])
  1125. updatewindowtype(c);
  1126. }
  1127. }
  1128. void
  1129. quit(const Arg *arg) {
  1130. running = 0;
  1131. }
  1132. Monitor *
  1133. recttomon(int x, int y, int w, int h) {
  1134. Monitor *m, *r = selmon;
  1135. int a, area = 0;
  1136. for(m = mons; m; m = m->next)
  1137. if((a = INTERSECT(x, y, w, h, m)) > area) {
  1138. area = a;
  1139. r = m;
  1140. }
  1141. return r;
  1142. }
  1143. void
  1144. resize(Client *c, int x, int y, int w, int h, int interact) {
  1145. if(applysizehints(c, &x, &y, &w, &h, interact))
  1146. resizeclient(c, x, y, w, h);
  1147. }
  1148. void
  1149. resizeclient(Client *c, int x, int y, int w, int h) {
  1150. XWindowChanges wc;
  1151. c->oldx = c->x; c->x = wc.x = x;
  1152. c->oldy = c->y; c->y = wc.y = y;
  1153. c->oldw = c->w; c->w = wc.width = w;
  1154. c->oldh = c->h; c->h = wc.height = h;
  1155. wc.border_width = c->bw;
  1156. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1157. configure(c);
  1158. XSync(dpy, False);
  1159. }
  1160. void
  1161. resizemouse(const Arg *arg) {
  1162. int ocx, ocy, nw, nh;
  1163. Client *c;
  1164. Monitor *m;
  1165. XEvent ev;
  1166. Time lasttime = 0;
  1167. if(!(c = selmon->sel))
  1168. return;
  1169. if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
  1170. return;
  1171. restack(selmon);
  1172. ocx = c->x;
  1173. ocy = c->y;
  1174. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1175. None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
  1176. return;
  1177. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1178. do {
  1179. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1180. switch(ev.type) {
  1181. case ConfigureRequest:
  1182. case Expose:
  1183. case MapRequest:
  1184. handler[ev.type](&ev);
  1185. break;
  1186. case MotionNotify:
  1187. if ((ev.xmotion.time - lasttime) <= (1000 / 60))
  1188. continue;
  1189. lasttime = ev.xmotion.time;
  1190. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1191. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1192. if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
  1193. && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
  1194. {
  1195. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1196. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1197. togglefloating(NULL);
  1198. }
  1199. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1200. resize(c, c->x, c->y, nw, nh, 1);
  1201. break;
  1202. }
  1203. } while(ev.type != ButtonRelease);
  1204. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1205. XUngrabPointer(dpy, CurrentTime);
  1206. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1207. if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1208. sendmon(c, m);
  1209. selmon = m;
  1210. focus(NULL);
  1211. }
  1212. }
  1213. void
  1214. restack(Monitor *m) {
  1215. Client *c;
  1216. XEvent ev;
  1217. XWindowChanges wc;
  1218. drawbar(m);
  1219. if(!m->sel)
  1220. return;
  1221. if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
  1222. XRaiseWindow(dpy, m->sel->win);
  1223. if(m->lt[m->sellt]->arrange) {
  1224. wc.stack_mode = Below;
  1225. wc.sibling = m->barwin;
  1226. for(c = m->stack; c; c = c->snext)
  1227. if(!c->isfloating && ISVISIBLE(c)) {
  1228. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1229. wc.sibling = c->win;
  1230. }
  1231. }
  1232. XSync(dpy, False);
  1233. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1234. }
  1235. void
  1236. run(void) {
  1237. XEvent ev;
  1238. /* main event loop */
  1239. XSync(dpy, False);
  1240. while(running && !XNextEvent(dpy, &ev))
  1241. if(handler[ev.type])
  1242. handler[ev.type](&ev); /* call handler */
  1243. }
  1244. void
  1245. scan(void) {
  1246. unsigned int i, num;
  1247. Window d1, d2, *wins = NULL;
  1248. XWindowAttributes wa;
  1249. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1250. for(i = 0; i < num; i++) {
  1251. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1252. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1253. continue;
  1254. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1255. manage(wins[i], &wa);
  1256. }
  1257. for(i = 0; i < num; i++) { /* now the transients */
  1258. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1259. continue;
  1260. if(XGetTransientForHint(dpy, wins[i], &d1)
  1261. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1262. manage(wins[i], &wa);
  1263. }
  1264. if(wins)
  1265. XFree(wins);
  1266. }
  1267. }
  1268. void
  1269. sendmon(Client *c, Monitor *m) {
  1270. if(c->mon == m)
  1271. return;
  1272. unfocus(c, 1);
  1273. detach(c);
  1274. detachstack(c);
  1275. c->mon = m;
  1276. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1277. attach(c);
  1278. attachstack(c);
  1279. focus(NULL);
  1280. arrange(NULL);
  1281. }
  1282. void
  1283. setclientstate(Client *c, long state) {
  1284. long data[] = { state, None };
  1285. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1286. PropModeReplace, (unsigned char *)data, 2);
  1287. }
  1288. int
  1289. sendevent(Client *c, Atom proto) {
  1290. int n;
  1291. Atom *protocols;
  1292. int exists = 0;
  1293. XEvent ev;
  1294. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  1295. while(!exists && n--)
  1296. exists = protocols[n] == proto;
  1297. XFree(protocols);
  1298. }
  1299. if(exists) {
  1300. ev.type = ClientMessage;
  1301. ev.xclient.window = c->win;
  1302. ev.xclient.message_type = wmatom[WMProtocols];
  1303. ev.xclient.format = 32;
  1304. ev.xclient.data.l[0] = proto;
  1305. ev.xclient.data.l[1] = CurrentTime;
  1306. XSendEvent(dpy, c->win, False, NoEventMask, &ev);
  1307. }
  1308. return exists;
  1309. }
  1310. void
  1311. setfocus(Client *c) {
  1312. if(!c->neverfocus) {
  1313. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  1314. XChangeProperty(dpy, root, netatom[NetActiveWindow],
  1315. XA_WINDOW, 32, PropModeReplace,
  1316. (unsigned char *) &(c->win), 1);
  1317. }
  1318. sendevent(c, wmatom[WMTakeFocus]);
  1319. }
  1320. void
  1321. setfullscreen(Client *c, int fullscreen) {
  1322. if(fullscreen && !c->isfullscreen) {
  1323. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1324. PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
  1325. c->isfullscreen = 1;
  1326. c->oldstate = c->isfloating;
  1327. c->oldbw = c->bw;
  1328. c->bw = 0;
  1329. c->isfloating = 1;
  1330. resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
  1331. XRaiseWindow(dpy, c->win);
  1332. }
  1333. else if(!fullscreen && c->isfullscreen){
  1334. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1335. PropModeReplace, (unsigned char*)0, 0);
  1336. c->isfullscreen = 0;
  1337. c->isfloating = c->oldstate;
  1338. c->bw = c->oldbw;
  1339. c->x = c->oldx;
  1340. c->y = c->oldy;
  1341. c->w = c->oldw;
  1342. c->h = c->oldh;
  1343. resizeclient(c, c->x, c->y, c->w, c->h);
  1344. arrange(c->mon);
  1345. }
  1346. }
  1347. void
  1348. setlayout(const Arg *arg) {
  1349. if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
  1350. selmon->sellt ^= 1;
  1351. if(arg && arg->v)
  1352. selmon->lt[selmon->sellt] = (Layout *)arg->v;
  1353. strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
  1354. if(selmon->sel)
  1355. arrange(selmon);
  1356. else
  1357. drawbar(selmon);
  1358. }
  1359. /* arg > 1.0 will set mfact absolutly */
  1360. void
  1361. setmfact(const Arg *arg) {
  1362. float f;
  1363. if(!arg || !selmon->lt[selmon->sellt]->arrange)
  1364. return;
  1365. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1366. if(f < 0.1 || f > 0.9)
  1367. return;
  1368. selmon->mfact = f;
  1369. arrange(selmon);
  1370. }
  1371. void
  1372. setup(void) {
  1373. XSetWindowAttributes wa;
  1374. /* clean up any zombies immediately */
  1375. sigchld(0);
  1376. /* init screen */
  1377. screen = DefaultScreen(dpy);
  1378. sw = DisplayWidth(dpy, screen);
  1379. sh = DisplayHeight(dpy, screen);
  1380. root = RootWindow(dpy, screen);
  1381. drw = drw_create(dpy, screen, root, sw, sh);
  1382. drw_load_fonts(drw, fonts, LENGTH(fonts));
  1383. if (!drw->fontcount)
  1384. die("no fonts could be loaded.\n");
  1385. bh = drw->fonts[0]->h + 2;
  1386. updategeom();
  1387. /* init atoms */
  1388. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1389. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1390. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1391. wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
  1392. netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
  1393. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1394. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1395. netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
  1396. netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
  1397. netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
  1398. netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
  1399. netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
  1400. /* init cursors */
  1401. cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
  1402. cursor[CurResize] = drw_cur_create(drw, XC_sizing);
  1403. cursor[CurMove] = drw_cur_create(drw, XC_fleur);
  1404. /* init appearance */
  1405. scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor);
  1406. scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
  1407. scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
  1408. scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor);
  1409. scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
  1410. scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
  1411. /* init bars */
  1412. updatebars();
  1413. updatestatus();
  1414. /* EWMH support per view */
  1415. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1416. PropModeReplace, (unsigned char *) netatom, NetLast);
  1417. XDeleteProperty(dpy, root, netatom[NetClientList]);
  1418. /* select for events */
  1419. wa.cursor = cursor[CurNormal]->cursor;
  1420. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
  1421. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
  1422. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1423. XSelectInput(dpy, root, wa.event_mask);
  1424. grabkeys();
  1425. focus(NULL);
  1426. }
  1427. void
  1428. showhide(Client *c) {
  1429. if(!c)
  1430. return;
  1431. if(ISVISIBLE(c)) { /* show clients top down */
  1432. XMoveWindow(dpy, c->win, c->x, c->y);
  1433. if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
  1434. resize(c, c->x, c->y, c->w, c->h, 0);
  1435. showhide(c->snext);
  1436. }
  1437. else { /* hide clients bottom up */
  1438. showhide(c->snext);
  1439. XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
  1440. }
  1441. }
  1442. void
  1443. sigchld(int unused) {
  1444. if(signal(SIGCHLD, sigchld) == SIG_ERR)
  1445. die("can't install SIGCHLD handler:");
  1446. while(0 < waitpid(-1, NULL, WNOHANG));
  1447. }
  1448. void
  1449. spawn(const Arg *arg) {
  1450. if(arg->v == dmenucmd)
  1451. dmenumon[0] = '0' + selmon->num;
  1452. if(fork() == 0) {
  1453. if(dpy)
  1454. close(ConnectionNumber(dpy));
  1455. setsid();
  1456. execvp(((char **)arg->v)[0], (char **)arg->v);
  1457. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1458. perror(" failed");
  1459. exit(EXIT_SUCCESS);
  1460. }
  1461. }
  1462. void
  1463. tag(const Arg *arg) {
  1464. if(selmon->sel && arg->ui & TAGMASK) {
  1465. selmon->sel->tags = arg->ui & TAGMASK;
  1466. focus(NULL);
  1467. arrange(selmon);
  1468. }
  1469. }
  1470. void
  1471. tagmon(const Arg *arg) {
  1472. if(!selmon->sel || !mons->next)
  1473. return;
  1474. sendmon(selmon->sel, dirtomon(arg->i));
  1475. }
  1476. void
  1477. tile(Monitor *m) {
  1478. unsigned int i, n, h, mw, my, ty;
  1479. Client *c;
  1480. for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  1481. if(n == 0)
  1482. return;
  1483. if(n > m->nmaster)
  1484. mw = m->nmaster ? m->ww * m->mfact : 0;
  1485. else
  1486. mw = m->ww;
  1487. for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  1488. if(i < m->nmaster) {
  1489. h = (m->wh - my) / (MIN(n, m->nmaster) - i);
  1490. resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), 0);
  1491. my += HEIGHT(c);
  1492. }
  1493. else {
  1494. h = (m->wh - ty) / (n - i);
  1495. resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), 0);
  1496. ty += HEIGHT(c);
  1497. }
  1498. }
  1499. void
  1500. togglebar(const Arg *arg) {
  1501. selmon->showbar = !selmon->showbar;
  1502. updatebarpos(selmon);
  1503. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1504. arrange(selmon);
  1505. }
  1506. void
  1507. togglefloating(const Arg *arg) {
  1508. if(!selmon->sel)
  1509. return;
  1510. if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
  1511. return;
  1512. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1513. if(selmon->sel->isfloating)
  1514. resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  1515. selmon->sel->w, selmon->sel->h, 0);
  1516. arrange(selmon);
  1517. }
  1518. void
  1519. toggletag(const Arg *arg) {
  1520. unsigned int newtags;
  1521. if(!selmon->sel)
  1522. return;
  1523. newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1524. if(newtags) {
  1525. selmon->sel->tags = newtags;
  1526. focus(NULL);
  1527. arrange(selmon);
  1528. }
  1529. }
  1530. void
  1531. toggleview(const Arg *arg) {
  1532. unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1533. if(newtagset) {
  1534. selmon->tagset[selmon->seltags] = newtagset;
  1535. focus(NULL);
  1536. arrange(selmon);
  1537. }
  1538. }
  1539. void
  1540. unfocus(Client *c, int setfocus) {
  1541. if(!c)
  1542. return;
  1543. grabbuttons(c, 0);
  1544. XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->pix);
  1545. if(setfocus) {
  1546. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1547. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  1548. }
  1549. }
  1550. void
  1551. unmanage(Client *c, int destroyed) {
  1552. Monitor *m = c->mon;
  1553. XWindowChanges wc;
  1554. /* The server grab construct avoids race conditions. */
  1555. detach(c);
  1556. detachstack(c);
  1557. if(!destroyed) {
  1558. wc.border_width = c->oldbw;
  1559. XGrabServer(dpy);
  1560. XSetErrorHandler(xerrordummy);
  1561. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1562. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1563. setclientstate(c, WithdrawnState);
  1564. XSync(dpy, False);
  1565. XSetErrorHandler(xerror);
  1566. XUngrabServer(dpy);
  1567. }
  1568. free(c);
  1569. focus(NULL);
  1570. updateclientlist();
  1571. arrange(m);
  1572. }
  1573. void
  1574. unmapnotify(XEvent *e) {
  1575. Client *c;
  1576. XUnmapEvent *ev = &e->xunmap;
  1577. if((c = wintoclient(ev->window))) {
  1578. if(ev->send_event)
  1579. setclientstate(c, WithdrawnState);
  1580. else
  1581. unmanage(c, 0);
  1582. }
  1583. }
  1584. void
  1585. updatebars(void) {
  1586. Monitor *m;
  1587. XSetWindowAttributes wa = {
  1588. .override_redirect = True,
  1589. .background_pixmap = ParentRelative,
  1590. .event_mask = ButtonPressMask|ExposureMask
  1591. };
  1592. for(m = mons; m; m = m->next) {
  1593. if (m->barwin)
  1594. continue;
  1595. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  1596. CopyFromParent, DefaultVisual(dpy, screen),
  1597. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1598. XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
  1599. XMapRaised(dpy, m->barwin);
  1600. }
  1601. }
  1602. void
  1603. updatebarpos(Monitor *m) {
  1604. m->wy = m->my;
  1605. m->wh = m->mh;
  1606. if(m->showbar) {
  1607. m->wh -= bh;
  1608. m->by = m->topbar ? m->wy : m->wy + m->wh;
  1609. m->wy = m->topbar ? m->wy + bh : m->wy;
  1610. }
  1611. else
  1612. m->by = -bh;
  1613. }
  1614. void
  1615. updateclientlist() {
  1616. Client *c;
  1617. Monitor *m;
  1618. XDeleteProperty(dpy, root, netatom[NetClientList]);
  1619. for(m = mons; m; m = m->next)
  1620. for(c = m->clients; c; c = c->next)
  1621. XChangeProperty(dpy, root, netatom[NetClientList],
  1622. XA_WINDOW, 32, PropModeAppend,
  1623. (unsigned char *) &(c->win), 1);
  1624. }
  1625. int
  1626. updategeom(void) {
  1627. int dirty = 0;
  1628. #ifdef XINERAMA
  1629. if(XineramaIsActive(dpy)) {
  1630. int i, j, n, nn;
  1631. Client *c;
  1632. Monitor *m;
  1633. XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
  1634. XineramaScreenInfo *unique = NULL;
  1635. for(n = 0, m = mons; m; m = m->next, n++);
  1636. /* only consider unique geometries as separate screens */
  1637. unique = ecalloc(nn, sizeof(XineramaScreenInfo));
  1638. for(i = 0, j = 0; i < nn; i++)
  1639. if(isuniquegeom(unique, j, &info[i]))
  1640. memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
  1641. XFree(info);
  1642. nn = j;
  1643. if(n <= nn) {
  1644. for(i = 0; i < (nn - n); i++) { /* new monitors available */
  1645. for(m = mons; m && m->next; m = m->next);
  1646. if(m)
  1647. m->next = createmon();
  1648. else
  1649. mons = createmon();
  1650. }
  1651. for(i = 0, m = mons; i < nn && m; m = m->next, i++)
  1652. if(i >= n
  1653. || (unique[i].x_org != m->mx || unique[i].y_org != m->my
  1654. || unique[i].width != m->mw || unique[i].height != m->mh))
  1655. {
  1656. dirty = 1;
  1657. m->num = i;
  1658. m->mx = m->wx = unique[i].x_org;
  1659. m->my = m->wy = unique[i].y_org;
  1660. m->mw = m->ww = unique[i].width;
  1661. m->mh = m->wh = unique[i].height;
  1662. updatebarpos(m);
  1663. }
  1664. }
  1665. else { /* less monitors available nn < n */
  1666. for(i = nn; i < n; i++) {
  1667. for(m = mons; m && m->next; m = m->next);
  1668. while(m->clients) {
  1669. dirty = 1;
  1670. c = m->clients;
  1671. m->clients = c->next;
  1672. detachstack(c);
  1673. c->mon = mons;
  1674. attach(c);
  1675. attachstack(c);
  1676. }
  1677. if(m == selmon)
  1678. selmon = mons;
  1679. cleanupmon(m);
  1680. }
  1681. }
  1682. free(unique);
  1683. }
  1684. else
  1685. #endif /* XINERAMA */
  1686. /* default monitor setup */
  1687. {
  1688. if(!mons)
  1689. mons = createmon();
  1690. if(mons->mw != sw || mons->mh != sh) {
  1691. dirty = 1;
  1692. mons->mw = mons->ww = sw;
  1693. mons->mh = mons->wh = sh;
  1694. updatebarpos(mons);
  1695. }
  1696. }
  1697. if(dirty) {
  1698. selmon = mons;
  1699. selmon = wintomon(root);
  1700. }
  1701. return dirty;
  1702. }
  1703. void
  1704. updatenumlockmask(void) {
  1705. unsigned int i, j;
  1706. XModifierKeymap *modmap;
  1707. numlockmask = 0;
  1708. modmap = XGetModifierMapping(dpy);
  1709. for(i = 0; i < 8; i++)
  1710. for(j = 0; j < modmap->max_keypermod; j++)
  1711. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1712. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1713. numlockmask = (1 << i);
  1714. XFreeModifiermap(modmap);
  1715. }
  1716. void
  1717. updatesizehints(Client *c) {
  1718. long msize;
  1719. XSizeHints size;
  1720. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1721. /* size is uninitialized, ensure that size.flags aren't used */
  1722. size.flags = PSize;
  1723. if(size.flags & PBaseSize) {
  1724. c->basew = size.base_width;
  1725. c->baseh = size.base_height;
  1726. }
  1727. else if(size.flags & PMinSize) {
  1728. c->basew = size.min_width;
  1729. c->baseh = size.min_height;
  1730. }
  1731. else
  1732. c->basew = c->baseh = 0;
  1733. if(size.flags & PResizeInc) {
  1734. c->incw = size.width_inc;
  1735. c->inch = size.height_inc;
  1736. }
  1737. else
  1738. c->incw = c->inch = 0;
  1739. if(size.flags & PMaxSize) {
  1740. c->maxw = size.max_width;
  1741. c->maxh = size.max_height;
  1742. }
  1743. else
  1744. c->maxw = c->maxh = 0;
  1745. if(size.flags & PMinSize) {
  1746. c->minw = size.min_width;
  1747. c->minh = size.min_height;
  1748. }
  1749. else if(size.flags & PBaseSize) {
  1750. c->minw = size.base_width;
  1751. c->minh = size.base_height;
  1752. }
  1753. else
  1754. c->minw = c->minh = 0;
  1755. if(size.flags & PAspect) {
  1756. c->mina = (float)size.min_aspect.y / size.min_aspect.x;
  1757. c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
  1758. }
  1759. else
  1760. c->maxa = c->mina = 0.0;
  1761. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1762. && c->maxw == c->minw && c->maxh == c->minh);
  1763. }
  1764. void
  1765. updatetitle(Client *c) {
  1766. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1767. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1768. if(c->name[0] == '\0') /* hack to mark broken clients */
  1769. strcpy(c->name, broken);
  1770. }
  1771. void
  1772. updatestatus(void) {
  1773. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1774. strcpy(stext, "dwm-"VERSION);
  1775. drawbar(selmon);
  1776. }
  1777. void
  1778. updatewindowtype(Client *c) {
  1779. Atom state = getatomprop(c, netatom[NetWMState]);
  1780. Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
  1781. if(state == netatom[NetWMFullscreen])
  1782. setfullscreen(c, 1);
  1783. if(wtype == netatom[NetWMWindowTypeDialog])
  1784. c->isfloating = 1;
  1785. }
  1786. void
  1787. updatewmhints(Client *c) {
  1788. XWMHints *wmh;
  1789. if((wmh = XGetWMHints(dpy, c->win))) {
  1790. if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  1791. wmh->flags &= ~XUrgencyHint;
  1792. XSetWMHints(dpy, c->win, wmh);
  1793. }
  1794. else
  1795. c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
  1796. if(wmh->flags & InputHint)
  1797. c->neverfocus = !wmh->input;
  1798. else
  1799. c->neverfocus = 0;
  1800. XFree(wmh);
  1801. }
  1802. }
  1803. void
  1804. view(const Arg *arg) {
  1805. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1806. return;
  1807. selmon->seltags ^= 1; /* toggle sel tagset */
  1808. if(arg->ui & TAGMASK)
  1809. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1810. focus(NULL);
  1811. arrange(selmon);
  1812. }
  1813. Client *
  1814. wintoclient(Window w) {
  1815. Client *c;
  1816. Monitor *m;
  1817. for(m = mons; m; m = m->next)
  1818. for(c = m->clients; c; c = c->next)
  1819. if(c->win == w)
  1820. return c;
  1821. return NULL;
  1822. }
  1823. Monitor *
  1824. wintomon(Window w) {
  1825. int x, y;
  1826. Client *c;
  1827. Monitor *m;
  1828. if(w == root && getrootptr(&x, &y))
  1829. return recttomon(x, y, 1, 1);
  1830. for(m = mons; m; m = m->next)
  1831. if(w == m->barwin)
  1832. return m;
  1833. if((c = wintoclient(w)))
  1834. return c->mon;
  1835. return selmon;
  1836. }
  1837. /* There's no way to check accesses to destroyed windows, thus those cases are
  1838. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1839. * default error handler, which may call exit. */
  1840. int
  1841. xerror(Display *dpy, XErrorEvent *ee) {
  1842. if(ee->error_code == BadWindow
  1843. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1844. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1845. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1846. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1847. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1848. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1849. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1850. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1851. return 0;
  1852. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1853. ee->request_code, ee->error_code);
  1854. return xerrorxlib(dpy, ee); /* may call exit */
  1855. }
  1856. int
  1857. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1858. return 0;
  1859. }
  1860. /* Startup Error handler to check if another window manager
  1861. * is already running. */
  1862. int
  1863. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1864. die("dwm: another window manager is already running\n");
  1865. return -1;
  1866. }
  1867. void
  1868. zoom(const Arg *arg) {
  1869. Client *c = selmon->sel;
  1870. if(!selmon->lt[selmon->sellt]->arrange
  1871. || (selmon->sel && selmon->sel->isfloating))
  1872. return;
  1873. if(c == nexttiled(selmon->clients))
  1874. if(!c || !(c = nexttiled(c->next)))
  1875. return;
  1876. pop(c);
  1877. }
  1878. int
  1879. main(int argc, char *argv[]) {
  1880. if(argc == 2 && !strcmp("-v", argv[1]))
  1881. die("dwm-"VERSION", © 2006-2015 dwm engineers, see LICENSE for details\n");
  1882. else if(argc != 1)
  1883. die("usage: dwm [-v]\n");
  1884. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1885. fputs("warning: no locale support\n", stderr);
  1886. if(!(dpy = XOpenDisplay(NULL)))
  1887. die("dwm: cannot open display\n");
  1888. checkotherwm();
  1889. setup();
  1890. scan();
  1891. run();
  1892. cleanup();
  1893. XCloseDisplay(dpy);
  1894. return EXIT_SUCCESS;
  1895. }