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.
 
 
 
 
 

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