My patched version of suckless' dwm.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2192 lines
53 KiB

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