My build of dwm
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 

2443 rindas
58 KiB

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