spacepaste

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