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.
 
 
 
 
 

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