My dwm build
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.
 
 
 
 
 

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