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.
 
 
 
 
 

2867 lines
69 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 <X11/Xlib-xcb.h>
  44. #include <xcb/res.h>
  45. #ifdef __OpenBSD__
  46. #include <sys/sysctl.h>
  47. #include <kvm.h>
  48. #endif /* __OpenBSD */
  49. #include "drw.h"
  50. #include "util.h"
  51. /* macros */
  52. #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
  53. #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
  54. #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
  55. * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
  56. #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
  57. #define LENGTH(X) (sizeof X / sizeof X[0])
  58. #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
  59. #define WIDTH(X) ((X)->w + 2 * (X)->bw)
  60. #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
  61. #define TAGMASK ((1 << LENGTH(tags)) - 1)
  62. #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
  63. /* enums */
  64. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  65. enum { SchemeNorm, SchemeSel }; /* color schemes */
  66. enum { NetSupported, NetWMName, NetWMState, NetWMCheck,
  67. NetWMFullscreen, NetActiveWindow, NetWMWindowType,
  68. NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
  69. enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
  70. enum { ClkTagBar, ClkTabBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
  71. ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
  72. typedef union {
  73. int i;
  74. unsigned int ui;
  75. float f;
  76. const void *v;
  77. } Arg;
  78. typedef struct {
  79. unsigned int click;
  80. unsigned int mask;
  81. unsigned int button;
  82. void (*func)(const Arg *arg);
  83. const Arg arg;
  84. } Button;
  85. typedef struct Monitor Monitor;
  86. typedef struct Client Client;
  87. struct Client {
  88. char name[256];
  89. float mina, maxa;
  90. int x, y, w, h;
  91. int oldx, oldy, oldw, oldh;
  92. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  93. int bw, oldbw;
  94. unsigned int tags;
  95. int isfixed, iscentered, isfloating, isalwaysontop, isurgent, neverfocus, oldstate, isfullscreen, isterminal, noswallow;
  96. pid_t pid;
  97. Client *next;
  98. Client *snext;
  99. Client *swallowing;
  100. Monitor *mon;
  101. Window win;
  102. };
  103. typedef struct {
  104. unsigned int mod;
  105. KeySym keysym;
  106. void (*func)(const Arg *);
  107. const Arg arg;
  108. } Key;
  109. typedef struct {
  110. const char *symbol;
  111. void (*arrange)(Monitor *);
  112. } Layout;
  113. #define MAXTABS 50
  114. typedef struct Pertag Pertag;
  115. struct Monitor {
  116. char ltsymbol[16];
  117. float mfact;
  118. int nmaster;
  119. int num;
  120. int by; /* bar geometry */
  121. int ty; /* tab bar geometry */
  122. int mx, my, mw, mh; /* screen size */
  123. int wx, wy, ww, wh; /* window area */
  124. int gappx; /* gaps between windows */
  125. unsigned int seltags;
  126. unsigned int sellt;
  127. unsigned int tagset[2];
  128. int showbar;
  129. int showtab;
  130. int topbar;
  131. int toptab;
  132. Client *clients;
  133. Client *sel;
  134. Client *stack;
  135. Monitor *next;
  136. Window barwin;
  137. Window tabwin;
  138. int ntabs;
  139. int tab_widths[MAXTABS] ;
  140. const Layout *lt[2];
  141. Pertag *pertag;
  142. };
  143. typedef struct {
  144. const char *class;
  145. const char *instance;
  146. const char *title;
  147. unsigned int tags;
  148. int iscentered;
  149. int isfloating;
  150. int isterminal;
  151. int noswallow;
  152. int monitor;
  153. } Rule;
  154. /* function declarations */
  155. static void applyrules(Client *c);
  156. static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
  157. static void arrange(Monitor *m);
  158. static void arrangemon(Monitor *m);
  159. static void attach(Client *c);
  160. static void attachstack(Client *c);
  161. static void buttonpress(XEvent *e);
  162. static void checkotherwm(void);
  163. static void cleanup(void);
  164. static void cleanupmon(Monitor *mon);
  165. static void clientmessage(XEvent *e);
  166. static void configure(Client *c);
  167. static void configurenotify(XEvent *e);
  168. static void configurerequest(XEvent *e);
  169. static Monitor *createmon(void);
  170. static void destroynotify(XEvent *e);
  171. static void detach(Client *c);
  172. static void detachstack(Client *c);
  173. static Monitor *dirtomon(int dir);
  174. static void drawbar(Monitor *m);
  175. static void drawbars(void);
  176. static void drawtab(Monitor *m);
  177. static void drawtabs(void);
  178. static void enternotify(XEvent *e);
  179. static void expose(XEvent *e);
  180. static void focus(Client *c);
  181. static void focusin(XEvent *e);
  182. static void focusmon(const Arg *arg);
  183. static void focusstack(const Arg *arg);
  184. static void focuswin(const Arg* arg);
  185. static int getrootptr(int *x, int *y);
  186. static long getstate(Window w);
  187. static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
  188. static void grabbuttons(Client *c, int focused);
  189. static void grabkeys(void);
  190. static void incnmaster(const Arg *arg);
  191. static void keypress(XEvent *e);
  192. static void killclient(const Arg *arg);
  193. static void manage(Window w, XWindowAttributes *wa);
  194. static void mappingnotify(XEvent *e);
  195. static void maprequest(XEvent *e);
  196. static void monocle(Monitor *m);
  197. static void motionnotify(XEvent *e);
  198. static void movemouse(const Arg *arg);
  199. static Client *nexttiled(Client *c);
  200. static void pop(Client *);
  201. static void propertynotify(XEvent *e);
  202. static void quit(const Arg *arg);
  203. static Monitor *recttomon(int x, int y, int w, int h);
  204. static void resize(Client *c, int x, int y, int w, int h, int interact);
  205. static void resizeclient(Client *c, int x, int y, int w, int h);
  206. static void resizemouse(const Arg *arg);
  207. static void restack(Monitor *m);
  208. static void run(void);
  209. static void scan(void);
  210. static int sendevent(Client *c, Atom proto);
  211. static void sendmon(Client *c, Monitor *m);
  212. static void setclientstate(Client *c, long state);
  213. static void setfocus(Client *c);
  214. static void setfullscreen(Client *c, int fullscreen);
  215. static void setgaps(const Arg *arg);
  216. static void setlayout(const Arg *arg);
  217. static void setmfact(const Arg *arg);
  218. static void setup(void);
  219. static void seturgent(Client *c, int urg);
  220. static void showhide(Client *c);
  221. static void sigchld(int unused);
  222. static void spawn(const Arg *arg);
  223. static void tabmode(const Arg *arg);
  224. static void tag(const Arg *arg);
  225. static void tagmon(const Arg *arg);
  226. static void tile(Monitor *);
  227. static void togglebar(const Arg *arg);
  228. static void togglefloating(const Arg *arg);
  229. static void togglealwaysontop(const Arg *arg);
  230. static void toggletag(const Arg *arg);
  231. static void toggleview(const Arg *arg);
  232. static void unfocus(Client *c, int setfocus);
  233. static void unmanage(Client *c, int destroyed);
  234. static void unmapnotify(XEvent *e);
  235. static void updatebarpos(Monitor *m);
  236. static void updatebars(void);
  237. static void updateclientlist(void);
  238. static int updategeom(void);
  239. static void updatenumlockmask(void);
  240. static void updatesizehints(Client *c);
  241. static void updatestatus(void);
  242. static void updatetitle(Client *c);
  243. static void updatewindowtype(Client *c);
  244. static void updatewmhints(Client *c);
  245. static void view(const Arg *arg);
  246. static Client *wintoclient(Window w);
  247. static Monitor *wintomon(Window w);
  248. static int xerror(Display *dpy, XErrorEvent *ee);
  249. static int xerrordummy(Display *dpy, XErrorEvent *ee);
  250. static int xerrorstart(Display *dpy, XErrorEvent *ee);
  251. static void zoom(const Arg *arg);
  252. static void centeredmaster(Monitor *m);
  253. static void centeredfloatingmaster(Monitor *m);
  254. static void autostart_exec(void);
  255. static pid_t getparentprocess(pid_t p);
  256. static int isdescprocess(pid_t p, pid_t c);
  257. static Client *swallowingclient(Window w);
  258. static Client *termforwin(const Client *c);
  259. static pid_t winpid(Window w);
  260. /* variables */
  261. static const char broken[] = "broken";
  262. static char stext[256];
  263. static int screen;
  264. static int sw, sh; /* X display screen geometry width, height */
  265. static int bh, blw = 0; /* bar geometry */
  266. static int th = 0; /* tab bar geometry */
  267. static int lrpad; /* sum of left and right padding for text */
  268. static int (*xerrorxlib)(Display *, XErrorEvent *);
  269. static unsigned int numlockmask = 0;
  270. static void (*handler[LASTEvent]) (XEvent *) = {
  271. [ButtonPress] = buttonpress,
  272. [ClientMessage] = clientmessage,
  273. [ConfigureRequest] = configurerequest,
  274. [ConfigureNotify] = configurenotify,
  275. [DestroyNotify] = destroynotify,
  276. [EnterNotify] = enternotify,
  277. [Expose] = expose,
  278. [FocusIn] = focusin,
  279. [KeyPress] = keypress,
  280. [MappingNotify] = mappingnotify,
  281. [MapRequest] = maprequest,
  282. [MotionNotify] = motionnotify,
  283. [PropertyNotify] = propertynotify,
  284. [UnmapNotify] = unmapnotify
  285. };
  286. static Atom wmatom[WMLast], netatom[NetLast];
  287. static int running = 1;
  288. static Cur *cursor[CurLast];
  289. static Clr **scheme;
  290. static Display *dpy;
  291. static Drw *drw;
  292. static Monitor *mons, *selmon;
  293. static Window root, wmcheckwin;
  294. static xcb_connection_t *xcon;
  295. /* configuration, allows nested code to access above variables */
  296. #include "config.h"
  297. struct Pertag {
  298. unsigned int curtag, prevtag; /* current and previous tag */
  299. int nmasters[LENGTH(tags) + 1]; /* number of windows in master area */
  300. float mfacts[LENGTH(tags) + 1]; /* mfacts per tag */
  301. unsigned int sellts[LENGTH(tags) + 1]; /* selected layouts */
  302. const Layout *ltidxs[LENGTH(tags) + 1][2]; /* matrix of tags and layouts indexes */
  303. Bool showbars[LENGTH(tags) + 1]; /* display bar for the current tag */
  304. Client *prevzooms[LENGTH(tags) + 1]; /* store zoom information */
  305. };
  306. /* compile-time check if all tags fit into an unsigned int bit array. */
  307. struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
  308. /* dwm will keep pid's of processes from autostart array and kill them at quit */
  309. static pid_t *autostart_pids;
  310. static size_t autostart_len;
  311. /* execute command from autostart array */
  312. static void
  313. autostart_exec() {
  314. const char *const *p;
  315. size_t i = 0;
  316. /* count entries */
  317. for (p = autostart; *p; autostart_len++, p++)
  318. while (*++p);
  319. autostart_pids = malloc(autostart_len * sizeof(pid_t));
  320. for (p = autostart; *p; i++, p++) {
  321. if ((autostart_pids[i] = fork()) == 0) {
  322. setsid();
  323. execvp(*p, (char *const *)p);
  324. fprintf(stderr, "dwm: execvp %s\n", *p);
  325. perror(" failed");
  326. _exit(EXIT_FAILURE);
  327. }
  328. /* skip arguments */
  329. while (*++p);
  330. }
  331. }
  332. /* function implementations */
  333. void
  334. applyrules(Client *c)
  335. {
  336. const char *class, *instance;
  337. unsigned int i;
  338. const Rule *r;
  339. Monitor *m;
  340. XClassHint ch = { NULL, NULL };
  341. /* rule matching */
  342. c->iscentered = 0;
  343. c->isfloating = 0;
  344. c->tags = 0;
  345. XGetClassHint(dpy, c->win, &ch);
  346. class = ch.res_class ? ch.res_class : broken;
  347. instance = ch.res_name ? ch.res_name : broken;
  348. for (i = 0; i < LENGTH(rules); i++) {
  349. r = &rules[i];
  350. if ((!r->title || strstr(c->name, r->title))
  351. && (!r->class || strstr(class, r->class))
  352. && (!r->instance || strstr(instance, r->instance)))
  353. {
  354. c->isterminal = r->isterminal;
  355. c->noswallow = r->noswallow;
  356. c->iscentered = r->iscentered;
  357. c->isfloating = r->isfloating;
  358. c->tags |= r->tags;
  359. for (m = mons; m && m->num != r->monitor; m = m->next);
  360. if (m)
  361. c->mon = m;
  362. }
  363. }
  364. if (ch.res_class)
  365. XFree(ch.res_class);
  366. if (ch.res_name)
  367. XFree(ch.res_name);
  368. c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
  369. }
  370. int
  371. applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact)
  372. {
  373. int baseismin;
  374. Monitor *m = c->mon;
  375. /* set minimum possible */
  376. *w = MAX(1, *w);
  377. *h = MAX(1, *h);
  378. if (interact) {
  379. if (*x > sw)
  380. *x = sw - WIDTH(c);
  381. if (*y > sh)
  382. *y = sh - HEIGHT(c);
  383. if (*x + *w + 2 * c->bw < 0)
  384. *x = 0;
  385. if (*y + *h + 2 * c->bw < 0)
  386. *y = 0;
  387. } else {
  388. if (*x >= m->wx + m->ww)
  389. *x = m->wx + m->ww - WIDTH(c);
  390. if (*y >= m->wy + m->wh)
  391. *y = m->wy + m->wh - HEIGHT(c);
  392. if (*x + *w + 2 * c->bw <= m->wx)
  393. *x = m->wx;
  394. if (*y + *h + 2 * c->bw <= m->wy)
  395. *y = m->wy;
  396. }
  397. if (*h < bh)
  398. *h = bh;
  399. if (*w < bh)
  400. *w = bh;
  401. if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
  402. /* see last two sentences in ICCCM 4.1.2.3 */
  403. baseismin = c->basew == c->minw && c->baseh == c->minh;
  404. if (!baseismin) { /* temporarily remove base dimensions */
  405. *w -= c->basew;
  406. *h -= c->baseh;
  407. }
  408. /* adjust for aspect limits */
  409. if (c->mina > 0 && c->maxa > 0) {
  410. if (c->maxa < (float)*w / *h)
  411. *w = *h * c->maxa + 0.5;
  412. else if (c->mina < (float)*h / *w)
  413. *h = *w * c->mina + 0.5;
  414. }
  415. if (baseismin) { /* increment calculation requires this */
  416. *w -= c->basew;
  417. *h -= c->baseh;
  418. }
  419. /* adjust for increment value */
  420. if (c->incw)
  421. *w -= *w % c->incw;
  422. if (c->inch)
  423. *h -= *h % c->inch;
  424. /* restore base dimensions */
  425. *w = MAX(*w + c->basew, c->minw);
  426. *h = MAX(*h + c->baseh, c->minh);
  427. if (c->maxw)
  428. *w = MIN(*w, c->maxw);
  429. if (c->maxh)
  430. *h = MIN(*h, c->maxh);
  431. }
  432. return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
  433. }
  434. void
  435. arrange(Monitor *m)
  436. {
  437. if (m)
  438. showhide(m->stack);
  439. else for (m = mons; m; m = m->next)
  440. showhide(m->stack);
  441. if (m) {
  442. arrangemon(m);
  443. restack(m);
  444. } else for (m = mons; m; m = m->next)
  445. arrangemon(m);
  446. }
  447. void
  448. arrangemon(Monitor *m)
  449. {
  450. updatebarpos(m);
  451. XMoveResizeWindow(dpy, m->tabwin, m->wx, m->ty, m->ww, th);
  452. strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
  453. if (m->lt[m->sellt]->arrange)
  454. m->lt[m->sellt]->arrange(m);
  455. }
  456. void
  457. attach(Client *c)
  458. {
  459. c->next = c->mon->clients;
  460. c->mon->clients = c;
  461. }
  462. void
  463. attachstack(Client *c)
  464. {
  465. c->snext = c->mon->stack;
  466. c->mon->stack = c;
  467. }
  468. void
  469. swallow(Client *p, Client *c)
  470. {
  471. if (c->noswallow || c->isterminal)
  472. return;
  473. if (c->noswallow && !swallowfloating && c->isfloating)
  474. return;
  475. detach(c);
  476. detachstack(c);
  477. setclientstate(c, WithdrawnState);
  478. XUnmapWindow(dpy, p->win);
  479. p->swallowing = c;
  480. c->mon = p->mon;
  481. Window w = p->win;
  482. p->win = c->win;
  483. c->win = w;
  484. updatetitle(p);
  485. XMoveResizeWindow(dpy, p->win, p->x, p->y, p->w, p->h);
  486. arrange(p->mon);
  487. configure(p);
  488. updateclientlist();
  489. }
  490. void
  491. unswallow(Client *c)
  492. {
  493. c->win = c->swallowing->win;
  494. free(c->swallowing);
  495. c->swallowing = NULL;
  496. /* unfullscreen the client */
  497. setfullscreen(c, 0);
  498. updatetitle(c);
  499. arrange(c->mon);
  500. XMapWindow(dpy, c->win);
  501. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  502. setclientstate(c, NormalState);
  503. focus(NULL);
  504. arrange(c->mon);
  505. }
  506. void
  507. buttonpress(XEvent *e)
  508. {
  509. unsigned int i, x, click;
  510. Arg arg = {0};
  511. Client *c;
  512. Monitor *m;
  513. XButtonPressedEvent *ev = &e->xbutton;
  514. click = ClkRootWin;
  515. /* focus monitor if necessary */
  516. if ((m = wintomon(ev->window)) && m != selmon) {
  517. unfocus(selmon->sel, 1);
  518. selmon = m;
  519. focus(NULL);
  520. }
  521. if (ev->window == selmon->barwin) {
  522. i = x = 0;
  523. do
  524. x += TEXTW(tags[i]);
  525. while (ev->x >= x && ++i < LENGTH(tags));
  526. if (i < LENGTH(tags)) {
  527. click = ClkTagBar;
  528. arg.ui = 1 << i;
  529. } else if (ev->x < x + blw)
  530. click = ClkLtSymbol;
  531. else if (ev->x > selmon->ww - TEXTW(stext))
  532. click = ClkStatusText;
  533. else
  534. click = ClkWinTitle;
  535. }
  536. if(ev->window == selmon->tabwin) {
  537. i = 0; x = 0;
  538. for(c = selmon->clients; c; c = c->next){
  539. if(!ISVISIBLE(c)) continue;
  540. x += selmon->tab_widths[i];
  541. if (ev->x > x)
  542. ++i;
  543. else
  544. break;
  545. if(i >= m->ntabs) break;
  546. }
  547. if(c) {
  548. click = ClkTabBar;
  549. arg.ui = i;
  550. }
  551. }
  552. else if((c = wintoclient(ev->window))) {
  553. focus(c);
  554. restack(selmon);
  555. XAllowEvents(dpy, ReplayPointer, CurrentTime);
  556. click = ClkClientWin;
  557. }
  558. for(i = 0; i < LENGTH(buttons); i++)
  559. if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
  560. && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)){
  561. buttons[i].func(((click == ClkTagBar || click == ClkTabBar)
  562. && buttons[i].arg.i == 0) ? &arg : &buttons[i].arg);
  563. }
  564. }
  565. void
  566. checkotherwm(void)
  567. {
  568. xerrorxlib = XSetErrorHandler(xerrorstart);
  569. /* this causes an error if some other window manager is running */
  570. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  571. XSync(dpy, False);
  572. XSetErrorHandler(xerror);
  573. XSync(dpy, False);
  574. }
  575. void
  576. cleanup(void)
  577. {
  578. Arg a = {.ui = ~0};
  579. Layout foo = { "", NULL };
  580. Monitor *m;
  581. size_t i;
  582. view(&a);
  583. selmon->lt[selmon->sellt] = &foo;
  584. for (m = mons; m; m = m->next)
  585. while(m->stack)
  586. unmanage(m->stack, False);
  587. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  588. while (mons)
  589. cleanupmon(mons);
  590. for (i = 0; i < CurLast; i++)
  591. drw_cur_free(drw, cursor[i]);
  592. for (i = 0; i < LENGTH(colors); i++)
  593. free(scheme[i]);
  594. XDestroyWindow(dpy, wmcheckwin);
  595. drw_free(drw);
  596. XSync(dpy, False);
  597. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  598. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  599. }
  600. void
  601. cleanupmon(Monitor *mon)
  602. {
  603. Monitor *m;
  604. if (mon == mons)
  605. mons = mons->next;
  606. else {
  607. for (m = mons; m && m->next != mon; m = m->next);
  608. m->next = mon->next;
  609. }
  610. XUnmapWindow(dpy, mon->barwin);
  611. XDestroyWindow(dpy, mon->barwin);
  612. XUnmapWindow(dpy, mon->tabwin);
  613. XDestroyWindow(dpy, mon->tabwin);
  614. free(mon);
  615. }
  616. void
  617. clientmessage(XEvent *e)
  618. {
  619. XClientMessageEvent *cme = &e->xclient;
  620. Client *c = wintoclient(cme->window);
  621. int i;
  622. if (!c)
  623. return;
  624. if (cme->message_type == netatom[NetWMState]) {
  625. if (cme->data.l[1] == netatom[NetWMFullscreen]
  626. || cme->data.l[2] == netatom[NetWMFullscreen])
  627. setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
  628. || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
  629. } else if (cme->message_type == netatom[NetActiveWindow]) {
  630. if (c != selmon->sel && !c->isurgent)
  631. seturgent(c, 1);
  632. }
  633. }
  634. void
  635. configure(Client *c)
  636. {
  637. XConfigureEvent ce;
  638. ce.type = ConfigureNotify;
  639. ce.display = dpy;
  640. ce.event = c->win;
  641. ce.window = c->win;
  642. ce.x = c->x;
  643. ce.y = c->y;
  644. ce.width = c->w;
  645. ce.height = c->h;
  646. ce.border_width = c->bw;
  647. ce.above = None;
  648. ce.override_redirect = False;
  649. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  650. }
  651. void
  652. configurenotify(XEvent *e)
  653. {
  654. Monitor *m;
  655. XConfigureEvent *ev = &e->xconfigure;
  656. int dirty;
  657. // TODO: updategeom handling sucks, needs to be simplified
  658. if (ev->window == root) {
  659. dirty = (sw != ev->width || sh != ev->height);
  660. sw = ev->width;
  661. sh = ev->height;
  662. if (updategeom() || dirty) {
  663. drw_resize(drw, sw, bh);
  664. updatebars();
  665. //refreshing display of status bar. The tab bar is handled by the arrange()
  666. //method, which is called below
  667. for (m = mons; m; m = m->next){
  668. XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
  669. }
  670. focus(NULL);
  671. arrange(NULL);
  672. }
  673. }
  674. }
  675. void
  676. configurerequest(XEvent *e)
  677. {
  678. Client *c;
  679. Monitor *m;
  680. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  681. XWindowChanges wc;
  682. if ((c = wintoclient(ev->window))) {
  683. if (ev->value_mask & CWBorderWidth)
  684. c->bw = ev->border_width;
  685. else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
  686. m = c->mon;
  687. if (ev->value_mask & CWX) {
  688. c->oldx = c->x;
  689. c->x = m->mx + ev->x;
  690. }
  691. if (ev->value_mask & CWY) {
  692. c->oldy = c->y;
  693. c->y = m->my + ev->y;
  694. }
  695. if (ev->value_mask & CWWidth) {
  696. c->oldw = c->w;
  697. c->w = ev->width;
  698. }
  699. if (ev->value_mask & CWHeight) {
  700. c->oldh = c->h;
  701. c->h = ev->height;
  702. }
  703. if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
  704. c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
  705. if ((c->y + c->h) > m->my + m->mh && c->isfloating)
  706. c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
  707. if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
  708. configure(c);
  709. if (ISVISIBLE(c))
  710. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  711. } else
  712. configure(c);
  713. } else {
  714. wc.x = ev->x;
  715. wc.y = ev->y;
  716. wc.width = ev->width;
  717. wc.height = ev->height;
  718. wc.border_width = ev->border_width;
  719. wc.sibling = ev->above;
  720. wc.stack_mode = ev->detail;
  721. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  722. }
  723. XSync(dpy, False);
  724. }
  725. Monitor *
  726. createmon(void)
  727. {
  728. Monitor *m;
  729. int i ;
  730. m = ecalloc(1, sizeof(Monitor));
  731. m->tagset[0] = m->tagset[1] = 1;
  732. m->mfact = mfact;
  733. m->nmaster = nmaster;
  734. m->showbar = showbar;
  735. m->showtab = showtab;
  736. m->topbar = topbar;
  737. m->gappx = gappx;
  738. m->toptab = toptab;
  739. m->ntabs = 0;
  740. m->lt[0] = &layouts[def_layouts[1] % LENGTH(layouts)];
  741. m->lt[1] = &layouts[1 % LENGTH(layouts)];
  742. strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
  743. if(!(m->pertag = (Pertag *)calloc(1, sizeof(Pertag))))
  744. die("fatal: could not malloc() %u bytes\n", sizeof(Pertag));
  745. m->pertag->curtag = m->pertag->prevtag = 1;
  746. for(i=0; i <= LENGTH(tags); i++) {
  747. /* init nmaster */
  748. m->pertag->nmasters[i] = m->nmaster;
  749. /* init mfacts */
  750. m->pertag->mfacts[i] = m->mfact;
  751. /* init layouts */
  752. m->pertag->ltidxs[i][0] = &layouts[def_layouts[i % LENGTH(def_layouts)] % LENGTH(layouts)];
  753. m->pertag->ltidxs[i][1] = m->lt[1];
  754. m->pertag->sellts[i] = m->sellt;
  755. /* init showbar */
  756. m->pertag->showbars[i] = m->showbar;
  757. /* swap focus and zoomswap*/
  758. m->pertag->prevzooms[i] = NULL;
  759. }
  760. return m;
  761. }
  762. void
  763. destroynotify(XEvent *e)
  764. {
  765. Client *c;
  766. XDestroyWindowEvent *ev = &e->xdestroywindow;
  767. if ((c = wintoclient(ev->window)))
  768. unmanage(c, 1);
  769. else if ((c = swallowingclient(ev->window)))
  770. unmanage(c->swallowing, 1);
  771. }
  772. void
  773. detach(Client *c)
  774. {
  775. Client **tc;
  776. for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
  777. *tc = c->next;
  778. }
  779. void
  780. detachstack(Client *c)
  781. {
  782. Client **tc, *t;
  783. for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
  784. *tc = c->snext;
  785. if (c == c->mon->sel) {
  786. for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
  787. c->mon->sel = t;
  788. }
  789. }
  790. Monitor *
  791. dirtomon(int dir)
  792. {
  793. Monitor *m = NULL;
  794. if (dir > 0) {
  795. if (!(m = selmon->next))
  796. m = mons;
  797. } else if (selmon == mons)
  798. for (m = mons; m->next; m = m->next);
  799. else
  800. for (m = mons; m->next != selmon; m = m->next);
  801. return m;
  802. }
  803. void
  804. drawbar(Monitor *m)
  805. {
  806. int x, w, sw = 0;
  807. int boxs = drw->fonts->h / 9;
  808. int boxw = drw->fonts->h / 6 + 2;
  809. unsigned int i, occ = 0, urg = 0;
  810. Client *c;
  811. /* draw status first so it can be overdrawn by tags later */
  812. if (m == selmon) { /* status is only drawn on selected monitor */
  813. drw_setscheme(drw, scheme[SchemeNorm]);
  814. sw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
  815. drw_text(drw, m->ww - sw, 0, sw, bh, 0, stext, 0);
  816. }
  817. for (c = m->clients; c; c = c->next) {
  818. occ |= c->tags;
  819. if (c->isurgent)
  820. urg |= c->tags;
  821. }
  822. x = 0;
  823. for (i = 0; i < LENGTH(tags); i++) {
  824. w = TEXTW(tags[i]);
  825. drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
  826. drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
  827. if (occ & 1 << i)
  828. drw_rect(drw, x + boxs, boxs, boxw, boxw,
  829. m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
  830. urg & 1 << i);
  831. x += w;
  832. }
  833. w = blw = TEXTW(m->ltsymbol);
  834. drw_setscheme(drw, scheme[SchemeNorm]);
  835. x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
  836. if ((w = m->ww - sw - x) > bh) {
  837. if (m->sel) {
  838. drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
  839. drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0);
  840. if (m->sel->isfloating) {
  841. drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
  842. if (m->sel->isalwaysontop)
  843. drw_rect(drw, x + boxs, bh - boxw, boxw, boxw, 0, 0);
  844. }
  845. } else {
  846. drw_setscheme(drw, scheme[SchemeNorm]);
  847. drw_rect(drw, x, 0, w, bh, 1, 1);
  848. }
  849. }
  850. drw_map(drw, m->barwin, 0, 0, m->ww, bh);
  851. }
  852. void
  853. drawbars(void)
  854. {
  855. Monitor *m;
  856. for (m = mons; m; m = m->next)
  857. drawbar(m);
  858. }
  859. void
  860. drawtabs(void) {
  861. Monitor *m;
  862. for(m = mons; m; m = m->next)
  863. drawtab(m);
  864. }
  865. static int
  866. cmpint(const void *p1, const void *p2) {
  867. /* The actual arguments to this function are "pointers to
  868. pointers to char", but strcmp(3) arguments are "pointers
  869. to char", hence the following cast plus dereference */
  870. return *((int*) p1) > * (int*) p2;
  871. }
  872. void
  873. drawtab(Monitor *m) {
  874. Client *c;
  875. int i;
  876. int itag = -1;
  877. char view_info[50];
  878. int view_info_w = 0;
  879. int sorted_label_widths[MAXTABS];
  880. int tot_width;
  881. int maxsize = bh;
  882. int x = 0;
  883. int w = 0;
  884. //view_info: indicate the tag which is displayed in the view
  885. for(i = 0; i < LENGTH(tags); ++i){
  886. if((selmon->tagset[selmon->seltags] >> i) & 1) {
  887. if(itag >=0){ //more than one tag selected
  888. itag = -1;
  889. break;
  890. }
  891. itag = i;
  892. }
  893. }
  894. if(0 <= itag && itag < LENGTH(tags)){
  895. snprintf(view_info, sizeof view_info, "[%s]", tags[itag]);
  896. } else {
  897. strncpy(view_info, "[...]", sizeof view_info);
  898. }
  899. view_info[sizeof(view_info) - 1 ] = 0;
  900. view_info_w = TEXTW(view_info);
  901. tot_width = view_info_w;
  902. /* Calculates number of labels and their width */
  903. m->ntabs = 0;
  904. for(c = m->clients; c; c = c->next){
  905. if(!ISVISIBLE(c)) continue;
  906. m->tab_widths[m->ntabs] = TEXTW(c->name);
  907. tot_width += m->tab_widths[m->ntabs];
  908. ++m->ntabs;
  909. if(m->ntabs >= MAXTABS) break;
  910. }
  911. if(tot_width > m->ww){ //not enough space to display the labels, they need to be truncated
  912. memcpy(sorted_label_widths, m->tab_widths, sizeof(int) * m->ntabs);
  913. qsort(sorted_label_widths, m->ntabs, sizeof(int), cmpint);
  914. tot_width = view_info_w;
  915. for(i = 0; i < m->ntabs; ++i){
  916. if(tot_width + (m->ntabs - i) * sorted_label_widths[i] > m->ww)
  917. break;
  918. tot_width += sorted_label_widths[i];
  919. }
  920. maxsize = (m->ww - tot_width) / (m->ntabs - i);
  921. } else{
  922. maxsize = m->ww;
  923. }
  924. i = 0;
  925. for(c = m->clients; c; c = c->next){
  926. if(!ISVISIBLE(c)) continue;
  927. if(i >= m->ntabs) break;
  928. if(m->tab_widths[i] > maxsize) m->tab_widths[i] = maxsize;
  929. w = m->tab_widths[i];
  930. drw_setscheme(drw, (c == m->sel) ? scheme[SchemeSel] : scheme[SchemeNorm]);
  931. drw_text(drw, x, 0, w, th, lrpad / 2, c->name, 0);
  932. x += w;
  933. ++i;
  934. }
  935. drw_setscheme(drw, scheme[SchemeNorm]);
  936. /* cleans interspace between window names and current viewed tag label */
  937. w = m->ww - view_info_w - x;
  938. drw_text(drw, x, 0, w, th, lrpad / 2, "", 0);
  939. /* view info */
  940. x += w;
  941. w = view_info_w;
  942. drw_text(drw, x, 0, w, th, lrpad / 2, view_info, 0);
  943. drw_map(drw, m->tabwin, 0, 0, m->ww, th);
  944. }
  945. void
  946. enternotify(XEvent *e)
  947. {
  948. Client *c;
  949. Monitor *m;
  950. XCrossingEvent *ev = &e->xcrossing;
  951. if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  952. return;
  953. c = wintoclient(ev->window);
  954. m = c ? c->mon : wintomon(ev->window);
  955. if (m != selmon) {
  956. unfocus(selmon->sel, 1);
  957. selmon = m;
  958. } else if (!c || c == selmon->sel)
  959. return;
  960. focus(c);
  961. }
  962. void
  963. expose(XEvent *e)
  964. {
  965. Monitor *m;
  966. XExposeEvent *ev = &e->xexpose;
  967. if (ev->count == 0 && (m = wintomon(ev->window))){
  968. drawbar(m);
  969. drawtab(m);
  970. }
  971. }
  972. void
  973. focus(Client *c)
  974. {
  975. if (!c || !ISVISIBLE(c))
  976. for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
  977. if (selmon->sel && selmon->sel != c)
  978. unfocus(selmon->sel, 0);
  979. if (c) {
  980. if (c->mon != selmon)
  981. selmon = c->mon;
  982. if (c->isurgent)
  983. seturgent(c, 0);
  984. detachstack(c);
  985. attachstack(c);
  986. grabbuttons(c, 1);
  987. XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
  988. setfocus(c);
  989. } else {
  990. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  991. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  992. }
  993. selmon->sel = c;
  994. drawbars();
  995. drawtabs();
  996. }
  997. /* there are some broken focus acquiring clients needing extra handling */
  998. void
  999. focusin(XEvent *e)
  1000. {
  1001. XFocusChangeEvent *ev = &e->xfocus;
  1002. if (selmon->sel && ev->window != selmon->sel->win)
  1003. setfocus(selmon->sel);
  1004. }
  1005. void
  1006. focusmon(const Arg *arg)
  1007. {
  1008. Monitor *m;
  1009. if (!mons->next)
  1010. return;
  1011. if ((m = dirtomon(arg->i)) == selmon)
  1012. return;
  1013. unfocus(selmon->sel, 0);
  1014. selmon = m;
  1015. focus(NULL);
  1016. }
  1017. void
  1018. focusstack(const Arg *arg)
  1019. {
  1020. Client *c = NULL, *i;
  1021. if (!selmon->sel)
  1022. return;
  1023. if (arg->i > 0) {
  1024. for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
  1025. if (!c)
  1026. for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
  1027. } else {
  1028. for (i = selmon->clients; i != selmon->sel; i = i->next)
  1029. if (ISVISIBLE(i))
  1030. c = i;
  1031. if (!c)
  1032. for (; i; i = i->next)
  1033. if (ISVISIBLE(i))
  1034. c = i;
  1035. }
  1036. if (c) {
  1037. focus(c);
  1038. restack(selmon);
  1039. }
  1040. }
  1041. void
  1042. focuswin(const Arg* arg){
  1043. int iwin = arg->i;
  1044. Client* c = NULL;
  1045. for(c = selmon->clients; c && (iwin || !ISVISIBLE(c)) ; c = c->next){
  1046. if(ISVISIBLE(c)) --iwin;
  1047. };
  1048. if(c) {
  1049. focus(c);
  1050. restack(selmon);
  1051. }
  1052. }
  1053. Atom
  1054. getatomprop(Client *c, Atom prop)
  1055. {
  1056. int di;
  1057. unsigned long dl;
  1058. unsigned char *p = NULL;
  1059. Atom da, atom = None;
  1060. if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
  1061. &da, &di, &dl, &dl, &p) == Success && p) {
  1062. atom = *(Atom *)p;
  1063. XFree(p);
  1064. }
  1065. return atom;
  1066. }
  1067. int
  1068. getrootptr(int *x, int *y)
  1069. {
  1070. int di;
  1071. unsigned int dui;
  1072. Window dummy;
  1073. return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
  1074. }
  1075. long
  1076. getstate(Window w)
  1077. {
  1078. int format;
  1079. long result = -1;
  1080. unsigned char *p = NULL;
  1081. unsigned long n, extra;
  1082. Atom real;
  1083. if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  1084. &real, &format, &n, &extra, (unsigned char **)&p) != Success)
  1085. return -1;
  1086. if (n != 0)
  1087. result = *p;
  1088. XFree(p);
  1089. return result;
  1090. }
  1091. int
  1092. gettextprop(Window w, Atom atom, char *text, unsigned int size)
  1093. {
  1094. char **list = NULL;
  1095. int n;
  1096. XTextProperty name;
  1097. if (!text || size == 0)
  1098. return 0;
  1099. text[0] = '\0';
  1100. if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
  1101. return 0;
  1102. if (name.encoding == XA_STRING)
  1103. strncpy(text, (char *)name.value, size - 1);
  1104. else {
  1105. if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
  1106. strncpy(text, *list, size - 1);
  1107. XFreeStringList(list);
  1108. }
  1109. }
  1110. text[size - 1] = '\0';
  1111. XFree(name.value);
  1112. return 1;
  1113. }
  1114. void
  1115. grabbuttons(Client *c, int focused)
  1116. {
  1117. updatenumlockmask();
  1118. {
  1119. unsigned int i, j;
  1120. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  1121. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1122. if (!focused)
  1123. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  1124. BUTTONMASK, GrabModeSync, GrabModeSync, None, None);
  1125. for (i = 0; i < LENGTH(buttons); i++)
  1126. if (buttons[i].click == ClkClientWin)
  1127. for (j = 0; j < LENGTH(modifiers); j++)
  1128. XGrabButton(dpy, buttons[i].button,
  1129. buttons[i].mask | modifiers[j],
  1130. c->win, False, BUTTONMASK,
  1131. GrabModeAsync, GrabModeSync, None, None);
  1132. }
  1133. }
  1134. void
  1135. grabkeys(void)
  1136. {
  1137. updatenumlockmask();
  1138. {
  1139. unsigned int i, j;
  1140. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  1141. KeyCode code;
  1142. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  1143. for (i = 0; i < LENGTH(keys); i++)
  1144. if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  1145. for (j = 0; j < LENGTH(modifiers); j++)
  1146. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  1147. True, GrabModeAsync, GrabModeAsync);
  1148. }
  1149. }
  1150. void
  1151. incnmaster(const Arg *arg)
  1152. {
  1153. selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag] = MAX(selmon->nmaster + arg->i, 0);
  1154. arrange(selmon);
  1155. }
  1156. #ifdef XINERAMA
  1157. static int
  1158. isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
  1159. {
  1160. while (n--)
  1161. if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
  1162. && unique[n].width == info->width && unique[n].height == info->height)
  1163. return 0;
  1164. return 1;
  1165. }
  1166. #endif /* XINERAMA */
  1167. void
  1168. keypress(XEvent *e)
  1169. {
  1170. unsigned int i;
  1171. KeySym keysym;
  1172. XKeyEvent *ev;
  1173. ev = &e->xkey;
  1174. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  1175. for (i = 0; i < LENGTH(keys); i++)
  1176. if (keysym == keys[i].keysym
  1177. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  1178. && keys[i].func)
  1179. keys[i].func(&(keys[i].arg));
  1180. }
  1181. void
  1182. killclient(const Arg *arg)
  1183. {
  1184. if (!selmon->sel)
  1185. return;
  1186. if (!sendevent(selmon->sel, wmatom[WMDelete])) {
  1187. XGrabServer(dpy);
  1188. XSetErrorHandler(xerrordummy);
  1189. XSetCloseDownMode(dpy, DestroyAll);
  1190. XKillClient(dpy, selmon->sel->win);
  1191. XSync(dpy, False);
  1192. XSetErrorHandler(xerror);
  1193. XUngrabServer(dpy);
  1194. }
  1195. }
  1196. void
  1197. manage(Window w, XWindowAttributes *wa)
  1198. {
  1199. Client *c, *t = NULL, *term = NULL;
  1200. Window trans = None;
  1201. XWindowChanges wc;
  1202. c = ecalloc(1, sizeof(Client));
  1203. c->win = w;
  1204. c->pid = winpid(w);
  1205. /* geometry */
  1206. c->x = c->oldx = wa->x;
  1207. c->y = c->oldy = wa->y;
  1208. c->w = c->oldw = wa->width;
  1209. c->h = c->oldh = wa->height;
  1210. c->oldbw = wa->border_width;
  1211. updatetitle(c);
  1212. if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
  1213. c->mon = t->mon;
  1214. c->tags = t->tags;
  1215. } else {
  1216. c->mon = selmon;
  1217. applyrules(c);
  1218. term = termforwin(c);
  1219. }
  1220. if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  1221. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  1222. if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  1223. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  1224. c->x = MAX(c->x, c->mon->mx);
  1225. /* only fix client y-offset, if the client center might cover the bar */
  1226. c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
  1227. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  1228. c->bw = borderpx;
  1229. wc.border_width = c->bw;
  1230. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  1231. XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
  1232. configure(c); /* propagates border_width, if size doesn't change */
  1233. updatewindowtype(c);
  1234. updatesizehints(c);
  1235. updatewmhints(c);
  1236. if (c->iscentered) {
  1237. c->x = c->mon->mx + (c->mon->mw - WIDTH(c)) / 2;
  1238. c->y = c->mon->my + (c->mon->mh - HEIGHT(c)) / 2;
  1239. }
  1240. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  1241. grabbuttons(c, 0);
  1242. if (!c->isfloating)
  1243. c->isfloating = c->oldstate = trans != None || c->isfixed;
  1244. if (c->isfloating)
  1245. XRaiseWindow(dpy, c->win);
  1246. attach(c);
  1247. attachstack(c);
  1248. XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
  1249. (unsigned char *) &(c->win), 1);
  1250. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  1251. setclientstate(c, NormalState);
  1252. if (c->mon == selmon)
  1253. unfocus(selmon->sel, 0);
  1254. c->mon->sel = c;
  1255. arrange(c->mon);
  1256. XMapWindow(dpy, c->win);
  1257. if (term)
  1258. swallow(term, c);
  1259. focus(NULL);
  1260. }
  1261. void
  1262. mappingnotify(XEvent *e)
  1263. {
  1264. XMappingEvent *ev = &e->xmapping;
  1265. XRefreshKeyboardMapping(ev);
  1266. if (ev->request == MappingKeyboard)
  1267. grabkeys();
  1268. }
  1269. void
  1270. maprequest(XEvent *e)
  1271. {
  1272. static XWindowAttributes wa;
  1273. XMapRequestEvent *ev = &e->xmaprequest;
  1274. if (!XGetWindowAttributes(dpy, ev->window, &wa))
  1275. return;
  1276. if (wa.override_redirect)
  1277. return;
  1278. if (!wintoclient(ev->window))
  1279. manage(ev->window, &wa);
  1280. }
  1281. void
  1282. monocle(Monitor *m)
  1283. {
  1284. unsigned int n = 0;
  1285. Client *c;
  1286. for (c = m->clients; c; c = c->next)
  1287. if (ISVISIBLE(c))
  1288. n++;
  1289. if (n > 0) /* override layout symbol */
  1290. snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
  1291. for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1292. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
  1293. }
  1294. void
  1295. motionnotify(XEvent *e)
  1296. {
  1297. static Monitor *mon = NULL;
  1298. Monitor *m;
  1299. XMotionEvent *ev = &e->xmotion;
  1300. if (ev->window != root)
  1301. return;
  1302. if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
  1303. unfocus(selmon->sel, True);
  1304. selmon = m;
  1305. focus(NULL);
  1306. }
  1307. mon = m;
  1308. }
  1309. void
  1310. movemouse(const Arg *arg)
  1311. {
  1312. int x, y, ocx, ocy, nx, ny;
  1313. Client *c;
  1314. Monitor *m;
  1315. XEvent ev;
  1316. Time lasttime = 0;
  1317. if (!(c = selmon->sel))
  1318. return;
  1319. if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
  1320. return;
  1321. restack(selmon);
  1322. ocx = c->x;
  1323. ocy = c->y;
  1324. if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1325. None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
  1326. return;
  1327. if (!getrootptr(&x, &y))
  1328. return;
  1329. do {
  1330. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1331. switch(ev.type) {
  1332. case ConfigureRequest:
  1333. case Expose:
  1334. case MapRequest:
  1335. handler[ev.type](&ev);
  1336. break;
  1337. case MotionNotify:
  1338. if ((ev.xmotion.time - lasttime) <= (1000 / 60))
  1339. continue;
  1340. lasttime = ev.xmotion.time;
  1341. nx = ocx + (ev.xmotion.x - x);
  1342. ny = ocy + (ev.xmotion.y - y);
  1343. if (abs(selmon->wx - nx) < snap)
  1344. nx = selmon->wx;
  1345. else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1346. nx = selmon->wx + selmon->ww - WIDTH(c);
  1347. if (abs(selmon->wy - ny) < snap)
  1348. ny = selmon->wy;
  1349. else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1350. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1351. if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1352. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1353. togglefloating(NULL);
  1354. if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1355. resize(c, nx, ny, c->w, c->h, 1);
  1356. break;
  1357. }
  1358. } while (ev.type != ButtonRelease);
  1359. XUngrabPointer(dpy, CurrentTime);
  1360. if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1361. sendmon(c, m);
  1362. selmon = m;
  1363. focus(NULL);
  1364. }
  1365. }
  1366. Client *
  1367. nexttiled(Client *c)
  1368. {
  1369. for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1370. return c;
  1371. }
  1372. void
  1373. pop(Client *c)
  1374. {
  1375. detach(c);
  1376. attach(c);
  1377. focus(c);
  1378. arrange(c->mon);
  1379. }
  1380. void
  1381. propertynotify(XEvent *e)
  1382. {
  1383. Client *c;
  1384. Window trans;
  1385. XPropertyEvent *ev = &e->xproperty;
  1386. if ((ev->window == root) && (ev->atom == XA_WM_NAME))
  1387. updatestatus();
  1388. else if (ev->state == PropertyDelete)
  1389. return; /* ignore */
  1390. else if ((c = wintoclient(ev->window))) {
  1391. switch(ev->atom) {
  1392. default: break;
  1393. case XA_WM_TRANSIENT_FOR:
  1394. if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
  1395. (c->isfloating = (wintoclient(trans)) != NULL))
  1396. arrange(c->mon);
  1397. break;
  1398. case XA_WM_NORMAL_HINTS:
  1399. updatesizehints(c);
  1400. break;
  1401. case XA_WM_HINTS:
  1402. updatewmhints(c);
  1403. drawbars();
  1404. drawtabs();
  1405. break;
  1406. }
  1407. if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1408. updatetitle(c);
  1409. if (c == c->mon->sel)
  1410. drawbar(c->mon);
  1411. drawtab(c->mon);
  1412. }
  1413. if (ev->atom == netatom[NetWMWindowType])
  1414. updatewindowtype(c);
  1415. }
  1416. }
  1417. void
  1418. quit(const Arg *arg)
  1419. {
  1420. size_t i;
  1421. /* kill child processes */
  1422. for (i = 0; i < autostart_len; i++) {
  1423. if (0 < autostart_pids[i]) {
  1424. kill(autostart_pids[i], SIGTERM);
  1425. waitpid(autostart_pids[i], NULL, 0);
  1426. }
  1427. }
  1428. running = 0;
  1429. }
  1430. Monitor *
  1431. recttomon(int x, int y, int w, int h)
  1432. {
  1433. Monitor *m, *r = selmon;
  1434. int a, area = 0;
  1435. for (m = mons; m; m = m->next)
  1436. if ((a = INTERSECT(x, y, w, h, m)) > area) {
  1437. area = a;
  1438. r = m;
  1439. }
  1440. return r;
  1441. }
  1442. void
  1443. resize(Client *c, int x, int y, int w, int h, int interact)
  1444. {
  1445. if (applysizehints(c, &x, &y, &w, &h, interact))
  1446. resizeclient(c, x, y, w, h);
  1447. }
  1448. void
  1449. resizeclient(Client *c, int x, int y, int w, int h)
  1450. {
  1451. XWindowChanges wc;
  1452. c->oldx = c->x; c->x = wc.x = x;
  1453. c->oldy = c->y; c->y = wc.y = y;
  1454. c->oldw = c->w; c->w = wc.width = w;
  1455. c->oldh = c->h; c->h = wc.height = h;
  1456. wc.border_width = c->bw;
  1457. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1458. configure(c);
  1459. XSync(dpy, False);
  1460. }
  1461. void
  1462. resizemouse(const Arg *arg)
  1463. {
  1464. int ocx, ocy, nw, nh;
  1465. Client *c;
  1466. Monitor *m;
  1467. XEvent ev;
  1468. Time lasttime = 0;
  1469. if (!(c = selmon->sel))
  1470. return;
  1471. if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
  1472. return;
  1473. restack(selmon);
  1474. ocx = c->x;
  1475. ocy = c->y;
  1476. if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1477. None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
  1478. return;
  1479. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1480. do {
  1481. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1482. switch(ev.type) {
  1483. case ConfigureRequest:
  1484. case Expose:
  1485. case MapRequest:
  1486. handler[ev.type](&ev);
  1487. break;
  1488. case MotionNotify:
  1489. if ((ev.xmotion.time - lasttime) <= (1000 / 60))
  1490. continue;
  1491. lasttime = ev.xmotion.time;
  1492. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1493. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1494. if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
  1495. && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
  1496. {
  1497. if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1498. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1499. togglefloating(NULL);
  1500. }
  1501. if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1502. resize(c, c->x, c->y, nw, nh, 1);
  1503. break;
  1504. }
  1505. } while (ev.type != ButtonRelease);
  1506. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1507. XUngrabPointer(dpy, CurrentTime);
  1508. while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1509. if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1510. sendmon(c, m);
  1511. selmon = m;
  1512. focus(NULL);
  1513. }
  1514. }
  1515. void
  1516. restack(Monitor *m)
  1517. {
  1518. Client *c;
  1519. XEvent ev;
  1520. XWindowChanges wc;
  1521. drawbar(m);
  1522. drawtab(m);
  1523. if (!m->sel)
  1524. return;
  1525. if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
  1526. XRaiseWindow(dpy, m->sel->win);
  1527. /* raise the aot window */
  1528. for(Monitor *m_search = mons; m_search; m_search = m_search->next){
  1529. for(c = m_search->clients; c; c = c->next){
  1530. if(c->isalwaysontop){
  1531. XRaiseWindow(dpy, c->win);
  1532. break;
  1533. }
  1534. }
  1535. }
  1536. if (m->lt[m->sellt]->arrange) {
  1537. wc.stack_mode = Below;
  1538. wc.sibling = m->barwin;
  1539. for (c = m->stack; c; c = c->snext)
  1540. if (!c->isfloating && ISVISIBLE(c)) {
  1541. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1542. wc.sibling = c->win;
  1543. }
  1544. }
  1545. XSync(dpy, False);
  1546. while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1547. }
  1548. void
  1549. run(void)
  1550. {
  1551. XEvent ev;
  1552. /* main event loop */
  1553. XSync(dpy, False);
  1554. while (running && !XNextEvent(dpy, &ev))
  1555. if (handler[ev.type])
  1556. handler[ev.type](&ev); /* call handler */
  1557. }
  1558. void
  1559. scan(void)
  1560. {
  1561. unsigned int i, num;
  1562. Window d1, d2, *wins = NULL;
  1563. XWindowAttributes wa;
  1564. if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1565. for (i = 0; i < num; i++) {
  1566. if (!XGetWindowAttributes(dpy, wins[i], &wa)
  1567. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1568. continue;
  1569. if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1570. manage(wins[i], &wa);
  1571. }
  1572. for (i = 0; i < num; i++) { /* now the transients */
  1573. if (!XGetWindowAttributes(dpy, wins[i], &wa))
  1574. continue;
  1575. if (XGetTransientForHint(dpy, wins[i], &d1)
  1576. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1577. manage(wins[i], &wa);
  1578. }
  1579. if (wins)
  1580. XFree(wins);
  1581. }
  1582. }
  1583. void
  1584. sendmon(Client *c, Monitor *m)
  1585. {
  1586. if (c->mon == m)
  1587. return;
  1588. unfocus(c, 1);
  1589. detach(c);
  1590. detachstack(c);
  1591. c->mon = m;
  1592. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1593. attach(c);
  1594. attachstack(c);
  1595. focus(NULL);
  1596. arrange(NULL);
  1597. }
  1598. void
  1599. setclientstate(Client *c, long state)
  1600. {
  1601. long data[] = { state, None };
  1602. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1603. PropModeReplace, (unsigned char *)data, 2);
  1604. }
  1605. int
  1606. sendevent(Client *c, Atom proto)
  1607. {
  1608. int n;
  1609. Atom *protocols;
  1610. int exists = 0;
  1611. XEvent ev;
  1612. if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  1613. while (!exists && n--)
  1614. exists = protocols[n] == proto;
  1615. XFree(protocols);
  1616. }
  1617. if (exists) {
  1618. ev.type = ClientMessage;
  1619. ev.xclient.window = c->win;
  1620. ev.xclient.message_type = wmatom[WMProtocols];
  1621. ev.xclient.format = 32;
  1622. ev.xclient.data.l[0] = proto;
  1623. ev.xclient.data.l[1] = CurrentTime;
  1624. XSendEvent(dpy, c->win, False, NoEventMask, &ev);
  1625. }
  1626. return exists;
  1627. }
  1628. void
  1629. setfocus(Client *c)
  1630. {
  1631. if (!c->neverfocus) {
  1632. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  1633. XChangeProperty(dpy, root, netatom[NetActiveWindow],
  1634. XA_WINDOW, 32, PropModeReplace,
  1635. (unsigned char *) &(c->win), 1);
  1636. }
  1637. sendevent(c, wmatom[WMTakeFocus]);
  1638. }
  1639. void
  1640. setfullscreen(Client *c, int fullscreen)
  1641. {
  1642. if (fullscreen && !c->isfullscreen) {
  1643. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1644. PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
  1645. c->isfullscreen = 1;
  1646. c->oldstate = c->isfloating;
  1647. c->oldbw = c->bw;
  1648. c->bw = 0;
  1649. c->isfloating = 1;
  1650. resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
  1651. XRaiseWindow(dpy, c->win);
  1652. } else if (!fullscreen && c->isfullscreen){
  1653. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1654. PropModeReplace, (unsigned char*)0, 0);
  1655. c->isfullscreen = 0;
  1656. c->isfloating = c->oldstate;
  1657. c->bw = c->oldbw;
  1658. c->x = c->oldx;
  1659. c->y = c->oldy;
  1660. c->w = c->oldw;
  1661. c->h = c->oldh;
  1662. resizeclient(c, c->x, c->y, c->w, c->h);
  1663. arrange(c->mon);
  1664. }
  1665. }
  1666. void
  1667. setgaps(const Arg *arg)
  1668. {
  1669. if ((arg->i == 0) || (selmon->gappx + arg->i < 0))
  1670. selmon->gappx = 0;
  1671. else
  1672. selmon->gappx += arg->i;
  1673. arrange(selmon);
  1674. }
  1675. void
  1676. setlayout(const Arg *arg)
  1677. {
  1678. if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt]) {
  1679. selmon->pertag->sellts[selmon->pertag->curtag] ^= 1;
  1680. selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
  1681. }
  1682. if(arg && arg->v)
  1683. selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt] = (Layout *)arg->v;
  1684. selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
  1685. strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
  1686. if (selmon->sel)
  1687. arrange(selmon);
  1688. else
  1689. drawbar(selmon);
  1690. }
  1691. /* arg > 1.0 will set mfact absolutely */
  1692. void
  1693. setmfact(const Arg *arg)
  1694. {
  1695. float f;
  1696. if (!arg || !selmon->lt[selmon->sellt]->arrange)
  1697. return;
  1698. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1699. if (f < 0.1 || f > 0.9)
  1700. return;
  1701. selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag] = f;
  1702. arrange(selmon);
  1703. }
  1704. void
  1705. setup(void)
  1706. {
  1707. int i;
  1708. XSetWindowAttributes wa;
  1709. Atom utf8string;
  1710. /* clean up any zombies immediately */
  1711. sigchld(0);
  1712. /* init screen */
  1713. screen = DefaultScreen(dpy);
  1714. sw = DisplayWidth(dpy, screen);
  1715. sh = DisplayHeight(dpy, screen);
  1716. root = RootWindow(dpy, screen);
  1717. drw = drw_create(dpy, screen, root, sw, sh);
  1718. if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
  1719. die("no fonts could be loaded.");
  1720. lrpad = drw->fonts->h;
  1721. bh = drw->fonts->h + 2;
  1722. th = bh;
  1723. updategeom();
  1724. /* init atoms */
  1725. utf8string = XInternAtom(dpy, "UTF8_STRING", False);
  1726. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1727. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1728. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1729. wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
  1730. netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
  1731. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1732. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1733. netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
  1734. netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
  1735. netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
  1736. netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
  1737. netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
  1738. netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
  1739. /* init cursors */
  1740. cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
  1741. cursor[CurResize] = drw_cur_create(drw, XC_sizing);
  1742. cursor[CurMove] = drw_cur_create(drw, XC_fleur);
  1743. /* init appearance */
  1744. scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
  1745. for (i = 0; i < LENGTH(colors); i++)
  1746. scheme[i] = drw_scm_create(drw, colors[i], 3);
  1747. /* init bars */
  1748. updatebars();
  1749. updatestatus();
  1750. /* supporting window for NetWMCheck */
  1751. wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
  1752. XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
  1753. PropModeReplace, (unsigned char *) &wmcheckwin, 1);
  1754. XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
  1755. PropModeReplace, (unsigned char *) "dwm", 3);
  1756. XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
  1757. PropModeReplace, (unsigned char *) &wmcheckwin, 1);
  1758. /* EWMH support per view */
  1759. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1760. PropModeReplace, (unsigned char *) netatom, NetLast);
  1761. XDeleteProperty(dpy, root, netatom[NetClientList]);
  1762. /* select events */
  1763. wa.cursor = cursor[CurNormal]->cursor;
  1764. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
  1765. |ButtonPressMask|PointerMotionMask|EnterWindowMask
  1766. |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
  1767. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1768. XSelectInput(dpy, root, wa.event_mask);
  1769. grabkeys();
  1770. focus(NULL);
  1771. }
  1772. void
  1773. seturgent(Client *c, int urg)
  1774. {
  1775. XWMHints *wmh;
  1776. c->isurgent = urg;
  1777. if (!(wmh = XGetWMHints(dpy, c->win)))
  1778. return;
  1779. wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
  1780. XSetWMHints(dpy, c->win, wmh);
  1781. XFree(wmh);
  1782. }
  1783. void
  1784. showhide(Client *c)
  1785. {
  1786. if (!c)
  1787. return;
  1788. if (ISVISIBLE(c)) {
  1789. /* show clients top down */
  1790. XMoveWindow(dpy, c->win, c->x, c->y);
  1791. if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
  1792. resize(c, c->x, c->y, c->w, c->h, 0);
  1793. showhide(c->snext);
  1794. } else {
  1795. /* hide clients bottom up */
  1796. showhide(c->snext);
  1797. XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
  1798. }
  1799. }
  1800. void
  1801. sigchld(int unused)
  1802. {
  1803. pid_t pid;
  1804. if (signal(SIGCHLD, sigchld) == SIG_ERR)
  1805. die("can't install SIGCHLD handler:");
  1806. while (0 < (pid = waitpid(-1, NULL, WNOHANG))) {
  1807. pid_t *p, *lim;
  1808. if (!(p = autostart_pids))
  1809. continue;
  1810. lim = &p[autostart_len];
  1811. for (; p < lim; p++) {
  1812. if (*p == pid) {
  1813. *p = -1;
  1814. break;
  1815. }
  1816. }
  1817. }
  1818. }
  1819. void
  1820. spawn(const Arg *arg)
  1821. {
  1822. if(arg->v == dmenucmd)
  1823. dmenumon[0] = '0' + selmon->num;
  1824. if(fork() == 0) {
  1825. if(dpy)
  1826. close(ConnectionNumber(dpy));
  1827. setsid();
  1828. execvp(((char **)arg->v)[0], (char **)arg->v);
  1829. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1830. perror(" failed");
  1831. exit(EXIT_SUCCESS);
  1832. }
  1833. }
  1834. void
  1835. tag(const Arg *arg)
  1836. {
  1837. if (selmon->sel && arg->ui & TAGMASK) {
  1838. selmon->sel->tags = arg->ui & TAGMASK;
  1839. focus(NULL);
  1840. arrange(selmon);
  1841. }
  1842. }
  1843. void
  1844. tagmon(const Arg *arg)
  1845. {
  1846. if (!selmon->sel || !mons->next)
  1847. return;
  1848. sendmon(selmon->sel, dirtomon(arg->i));
  1849. }
  1850. void
  1851. tile(Monitor *m)
  1852. {
  1853. unsigned int i, n, h, mw, my, ty;
  1854. Client *c;
  1855. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  1856. if (n == 0)
  1857. return;
  1858. if (n > m->nmaster)
  1859. mw = m->nmaster ? m->ww * m->mfact : 0;
  1860. else
  1861. mw = m->ww - m->gappx;
  1862. for (i = 0, my = ty = m->gappx, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  1863. if (i < m->nmaster) {
  1864. h = (m->wh - my) / (MIN(n, m->nmaster) - i) - m->gappx;
  1865. resize(c, m->wx + m->gappx, m->wy + my, mw - (2*c->bw) - m->gappx, h - (2*c->bw), 0);
  1866. my += HEIGHT(c) + m->gappx;
  1867. } else {
  1868. h = (m->wh - ty) / (n - i) - m->gappx;
  1869. resize(c, m->wx + mw + m->gappx, m->wy + ty, m->ww - mw - (2*c->bw) - 2*m->gappx, h - (2*c->bw), 0);
  1870. ty += HEIGHT(c) + m->gappx;
  1871. }
  1872. }
  1873. void
  1874. togglebar(const Arg *arg)
  1875. {
  1876. selmon->showbar = selmon->pertag->showbars[selmon->pertag->curtag] = !selmon->showbar;
  1877. updatebarpos(selmon);
  1878. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1879. arrange(selmon);
  1880. }
  1881. void
  1882. tabmode(const Arg *arg)
  1883. {
  1884. if(arg && arg->i >= 0)
  1885. selmon->showtab = arg->ui % showtab_nmodes;
  1886. else
  1887. selmon->showtab = (selmon->showtab + 1 ) % showtab_nmodes;
  1888. arrange(selmon);
  1889. }
  1890. void
  1891. togglefloating(const Arg *arg)
  1892. {
  1893. if(!selmon->sel)
  1894. return;
  1895. if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
  1896. return;
  1897. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1898. if (selmon->sel->isfloating)
  1899. resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  1900. selmon->sel->w, selmon->sel->h, 0);
  1901. else
  1902. selmon->sel->isalwaysontop = 0; /* disabled, turn this off too */
  1903. arrange(selmon);
  1904. }
  1905. void
  1906. togglealwaysontop(const Arg *arg)
  1907. {
  1908. if (!selmon->sel)
  1909. return;
  1910. if (selmon->sel->isfullscreen)
  1911. return;
  1912. if(selmon->sel->isalwaysontop){
  1913. selmon->sel->isalwaysontop = 0;
  1914. }else{
  1915. /* disable others */
  1916. for(Monitor *m = mons; m; m = m->next)
  1917. for(Client *c = m->clients; c; c = c->next)
  1918. c->isalwaysontop = 0;
  1919. /* turn on, make it float too */
  1920. selmon->sel->isfloating = 1;
  1921. selmon->sel->isalwaysontop = 1;
  1922. }
  1923. arrange(selmon);
  1924. }
  1925. void
  1926. toggletag(const Arg *arg)
  1927. {
  1928. unsigned int newtags;
  1929. if (!selmon->sel)
  1930. return;
  1931. newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1932. if (newtags) {
  1933. selmon->sel->tags = newtags;
  1934. focus(NULL);
  1935. arrange(selmon);
  1936. }
  1937. }
  1938. void
  1939. toggleview(const Arg *arg)
  1940. {
  1941. unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1942. int i;
  1943. if (newtagset) {
  1944. if(newtagset == ~0) {
  1945. selmon->pertag->prevtag = selmon->pertag->curtag;
  1946. selmon->pertag->curtag = 0;
  1947. }
  1948. /* test if the user did not select the same tag */
  1949. if(!(newtagset & 1 << (selmon->pertag->curtag - 1))) {
  1950. selmon->pertag->prevtag = selmon->pertag->curtag;
  1951. for (i=0; !(newtagset & 1 << i); i++) ;
  1952. selmon->pertag->curtag = i + 1;
  1953. }
  1954. selmon->tagset[selmon->seltags] = newtagset;
  1955. /* apply settings for this view */
  1956. selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
  1957. selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
  1958. selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
  1959. selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
  1960. selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
  1961. if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
  1962. togglebar(NULL);
  1963. focus(NULL);
  1964. arrange(selmon);
  1965. }
  1966. }
  1967. void
  1968. unfocus(Client *c, int setfocus)
  1969. {
  1970. if (!c)
  1971. return;
  1972. grabbuttons(c, 0);
  1973. XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
  1974. if (setfocus) {
  1975. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1976. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  1977. }
  1978. }
  1979. void
  1980. unmanage(Client *c, int destroyed)
  1981. {
  1982. Monitor *m = c->mon;
  1983. XWindowChanges wc;
  1984. if (c->swallowing) {
  1985. unswallow(c);
  1986. return;
  1987. }
  1988. Client *s = swallowingclient(c->win);
  1989. if (s) {
  1990. free(s->swallowing);
  1991. s->swallowing = NULL;
  1992. arrange(m);
  1993. focus(NULL);
  1994. return;
  1995. }
  1996. detach(c);
  1997. detachstack(c);
  1998. if (!destroyed) {
  1999. wc.border_width = c->oldbw;
  2000. XGrabServer(dpy); /* avoid race conditions */
  2001. XSetErrorHandler(xerrordummy);
  2002. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  2003. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  2004. setclientstate(c, WithdrawnState);
  2005. XSync(dpy, False);
  2006. XSetErrorHandler(xerror);
  2007. XUngrabServer(dpy);
  2008. }
  2009. free(c);
  2010. if (!s) {
  2011. arrange(m);
  2012. focus(NULL);
  2013. updateclientlist();
  2014. }
  2015. }
  2016. void
  2017. unmapnotify(XEvent *e)
  2018. {
  2019. Client *c;
  2020. XUnmapEvent *ev = &e->xunmap;
  2021. if ((c = wintoclient(ev->window))) {
  2022. if (ev->send_event)
  2023. setclientstate(c, WithdrawnState);
  2024. else
  2025. unmanage(c, 0);
  2026. }
  2027. }
  2028. void
  2029. updatebars(void)
  2030. {
  2031. Monitor *m;
  2032. XSetWindowAttributes wa = {
  2033. .override_redirect = True,
  2034. .background_pixmap = ParentRelative,
  2035. .event_mask = ButtonPressMask|ExposureMask
  2036. };
  2037. XClassHint ch = {"dwm", "dwm"};
  2038. for (m = mons; m; m = m->next) {
  2039. if (m->barwin)
  2040. continue;
  2041. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  2042. CopyFromParent, DefaultVisual(dpy, screen),
  2043. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  2044. XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
  2045. XMapRaised(dpy, m->barwin);
  2046. m->tabwin = XCreateWindow(dpy, root, m->wx, m->ty, m->ww, th, 0, DefaultDepth(dpy, screen),
  2047. CopyFromParent, DefaultVisual(dpy, screen),
  2048. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  2049. XDefineCursor(dpy, m->tabwin, cursor[CurNormal]->cursor);
  2050. XMapRaised(dpy, m->tabwin);
  2051. XSetClassHint(dpy, m->barwin, &ch);
  2052. }
  2053. }
  2054. void
  2055. updatebarpos(Monitor *m)
  2056. {
  2057. Client *c;
  2058. int nvis = 0;
  2059. m->wy = m->my;
  2060. m->wh = m->mh;
  2061. if (m->showbar) {
  2062. m->wh -= bh;
  2063. m->by = m->topbar ? m->wy : m->wy + m->wh;
  2064. if ( m->topbar )
  2065. m->wy += bh;
  2066. } else {
  2067. m->by = -bh;
  2068. }
  2069. for(c = m->clients; c; c = c->next){
  2070. if(ISVISIBLE(c)) ++nvis;
  2071. }
  2072. if(m->showtab == showtab_always
  2073. || ((m->showtab == showtab_auto) && (nvis > 1) && (m->lt[m->sellt]->arrange == monocle))){
  2074. m->wh -= th;
  2075. m->ty = m->toptab ? m->wy : m->wy + m->wh;
  2076. if ( m->toptab )
  2077. m->wy += th;
  2078. } else {
  2079. m->ty = -th;
  2080. }
  2081. }
  2082. void
  2083. updateclientlist()
  2084. {
  2085. Client *c;
  2086. Monitor *m;
  2087. XDeleteProperty(dpy, root, netatom[NetClientList]);
  2088. for (m = mons; m; m = m->next)
  2089. for (c = m->clients; c; c = c->next)
  2090. XChangeProperty(dpy, root, netatom[NetClientList],
  2091. XA_WINDOW, 32, PropModeAppend,
  2092. (unsigned char *) &(c->win), 1);
  2093. }
  2094. int
  2095. updategeom(void)
  2096. {
  2097. int dirty = 0;
  2098. #ifdef XINERAMA
  2099. if (XineramaIsActive(dpy)) {
  2100. int i, j, n, nn;
  2101. Client *c;
  2102. Monitor *m;
  2103. XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
  2104. XineramaScreenInfo *unique = NULL;
  2105. for (n = 0, m = mons; m; m = m->next, n++);
  2106. /* only consider unique geometries as separate screens */
  2107. unique = ecalloc(nn, sizeof(XineramaScreenInfo));
  2108. for (i = 0, j = 0; i < nn; i++)
  2109. if (isuniquegeom(unique, j, &info[i]))
  2110. memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
  2111. XFree(info);
  2112. nn = j;
  2113. if (n <= nn) { /* new monitors available */
  2114. for (i = 0; i < (nn - n); i++) {
  2115. for (m = mons; m && m->next; m = m->next);
  2116. if (m)
  2117. m->next = createmon();
  2118. else
  2119. mons = createmon();
  2120. }
  2121. for (i = 0, m = mons; i < nn && m; m = m->next, i++)
  2122. if (i >= n
  2123. || unique[i].x_org != m->mx || unique[i].y_org != m->my
  2124. || unique[i].width != m->mw || unique[i].height != m->mh)
  2125. {
  2126. dirty = 1;
  2127. m->num = i;
  2128. m->mx = m->wx = unique[i].x_org;
  2129. m->my = m->wy = unique[i].y_org;
  2130. m->mw = m->ww = unique[i].width;
  2131. m->mh = m->wh = unique[i].height;
  2132. updatebarpos(m);
  2133. }
  2134. } else { /* less monitors available nn < n */
  2135. for (i = nn; i < n; i++) {
  2136. for (m = mons; m && m->next; m = m->next);
  2137. while ((c = m->clients)) {
  2138. dirty = 1;
  2139. m->clients = c->next;
  2140. detachstack(c);
  2141. c->mon = mons;
  2142. attach(c);
  2143. attachstack(c);
  2144. }
  2145. if (m == selmon)
  2146. selmon = mons;
  2147. cleanupmon(m);
  2148. }
  2149. }
  2150. free(unique);
  2151. } else
  2152. #endif /* XINERAMA */
  2153. { /* default monitor setup */
  2154. if (!mons)
  2155. mons = createmon();
  2156. if (mons->mw != sw || mons->mh != sh) {
  2157. dirty = 1;
  2158. mons->mw = mons->ww = sw;
  2159. mons->mh = mons->wh = sh;
  2160. updatebarpos(mons);
  2161. }
  2162. }
  2163. if (dirty) {
  2164. selmon = mons;
  2165. selmon = wintomon(root);
  2166. }
  2167. return dirty;
  2168. }
  2169. void
  2170. updatenumlockmask(void)
  2171. {
  2172. unsigned int i, j;
  2173. XModifierKeymap *modmap;
  2174. numlockmask = 0;
  2175. modmap = XGetModifierMapping(dpy);
  2176. for (i = 0; i < 8; i++)
  2177. for (j = 0; j < modmap->max_keypermod; j++)
  2178. if (modmap->modifiermap[i * modmap->max_keypermod + j]
  2179. == XKeysymToKeycode(dpy, XK_Num_Lock))
  2180. numlockmask = (1 << i);
  2181. XFreeModifiermap(modmap);
  2182. }
  2183. void
  2184. updatesizehints(Client *c)
  2185. {
  2186. long msize;
  2187. XSizeHints size;
  2188. if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
  2189. /* size is uninitialized, ensure that size.flags aren't used */
  2190. size.flags = PSize;
  2191. if (size.flags & PBaseSize) {
  2192. c->basew = size.base_width;
  2193. c->baseh = size.base_height;
  2194. } else if (size.flags & PMinSize) {
  2195. c->basew = size.min_width;
  2196. c->baseh = size.min_height;
  2197. } else
  2198. c->basew = c->baseh = 0;
  2199. if (size.flags & PResizeInc) {
  2200. c->incw = size.width_inc;
  2201. c->inch = size.height_inc;
  2202. } else
  2203. c->incw = c->inch = 0;
  2204. if (size.flags & PMaxSize) {
  2205. c->maxw = size.max_width;
  2206. c->maxh = size.max_height;
  2207. } else
  2208. c->maxw = c->maxh = 0;
  2209. if (size.flags & PMinSize) {
  2210. c->minw = size.min_width;
  2211. c->minh = size.min_height;
  2212. } else if (size.flags & PBaseSize) {
  2213. c->minw = size.base_width;
  2214. c->minh = size.base_height;
  2215. } else
  2216. c->minw = c->minh = 0;
  2217. if (size.flags & PAspect) {
  2218. c->mina = (float)size.min_aspect.y / size.min_aspect.x;
  2219. c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
  2220. } else
  2221. c->maxa = c->mina = 0.0;
  2222. c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
  2223. }
  2224. void
  2225. updatestatus(void)
  2226. {
  2227. if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  2228. strcpy(stext, "dwm-"VERSION);
  2229. drawbar(selmon);
  2230. }
  2231. void
  2232. updatetitle(Client *c)
  2233. {
  2234. if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  2235. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  2236. if (c->name[0] == '\0') /* hack to mark broken clients */
  2237. strcpy(c->name, broken);
  2238. }
  2239. void
  2240. updatewindowtype(Client *c)
  2241. {
  2242. Atom state = getatomprop(c, netatom[NetWMState]);
  2243. Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
  2244. if (state == netatom[NetWMFullscreen])
  2245. setfullscreen(c, True);
  2246. if (wtype == netatom[NetWMWindowTypeDialog]) {
  2247. c->iscentered = 1;
  2248. c->isfloating = True;
  2249. }
  2250. }
  2251. void
  2252. updatewmhints(Client *c)
  2253. {
  2254. XWMHints *wmh;
  2255. if ((wmh = XGetWMHints(dpy, c->win))) {
  2256. if (c == selmon->sel && wmh->flags & XUrgencyHint) {
  2257. wmh->flags &= ~XUrgencyHint;
  2258. XSetWMHints(dpy, c->win, wmh);
  2259. } else
  2260. c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
  2261. if (wmh->flags & InputHint)
  2262. c->neverfocus = !wmh->input;
  2263. else
  2264. c->neverfocus = 0;
  2265. XFree(wmh);
  2266. }
  2267. }
  2268. void
  2269. view(const Arg *arg)
  2270. {
  2271. int i;
  2272. unsigned int tmptag;
  2273. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  2274. return;
  2275. selmon->seltags ^= 1; /* toggle sel tagset */
  2276. if(arg->ui & TAGMASK) {
  2277. selmon->pertag->prevtag = selmon->pertag->curtag;
  2278. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  2279. if(arg->ui == ~0)
  2280. selmon->pertag->curtag = 0;
  2281. else {
  2282. for (i=0; !(arg->ui & 1 << i); i++) ;
  2283. selmon->pertag->curtag = i + 1;
  2284. }
  2285. } else {
  2286. tmptag = selmon->pertag->prevtag;
  2287. selmon->pertag->prevtag = selmon->pertag->curtag;
  2288. selmon->pertag->curtag = tmptag;
  2289. }
  2290. selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
  2291. selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
  2292. selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
  2293. selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
  2294. selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
  2295. if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
  2296. togglebar(NULL);
  2297. focus(NULL);
  2298. arrange(selmon);
  2299. }
  2300. pid_t
  2301. winpid(Window w)
  2302. {
  2303. pid_t result = 0;
  2304. #ifdef __linux__
  2305. xcb_res_client_id_spec_t spec = {0};
  2306. spec.client = w;
  2307. spec.mask = XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID;
  2308. xcb_generic_error_t *e = NULL;
  2309. xcb_res_query_client_ids_cookie_t c = xcb_res_query_client_ids(xcon, 1, &spec);
  2310. xcb_res_query_client_ids_reply_t *r = xcb_res_query_client_ids_reply(xcon, c, &e);
  2311. if (!r)
  2312. return (pid_t)0;
  2313. xcb_res_client_id_value_iterator_t i = xcb_res_query_client_ids_ids_iterator(r);
  2314. for (; i.rem; xcb_res_client_id_value_next(&i)) {
  2315. spec = i.data->spec;
  2316. if (spec.mask & XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID) {
  2317. uint32_t *t = xcb_res_client_id_value_value(i.data);
  2318. result = *t;
  2319. break;
  2320. }
  2321. }
  2322. free(r);
  2323. if (result == (pid_t)-1)
  2324. result = 0;
  2325. #endif /* __linux__ */
  2326. #ifdef __OpenBSD__
  2327. Atom type;
  2328. int format;
  2329. unsigned long len, bytes;
  2330. unsigned char *prop;
  2331. pid_t ret;
  2332. if (XGetWindowProperty(dpy, w, XInternAtom(dpy, "_NET_WM_PID", 1), 0, 1, False, AnyPropertyType, &type, &format, &len, &bytes, &prop) != Success || !prop)
  2333. return 0;
  2334. ret = *(pid_t*)prop;
  2335. XFree(prop);
  2336. result = ret;
  2337. #endif /* __OpenBSD__ */
  2338. return result;
  2339. }
  2340. pid_t
  2341. getparentprocess(pid_t p)
  2342. {
  2343. unsigned int v = 0;
  2344. #ifdef __linux__
  2345. FILE *f;
  2346. char buf[256];
  2347. snprintf(buf, sizeof(buf) - 1, "/proc/%u/stat", (unsigned)p);
  2348. if (!(f = fopen(buf, "r")))
  2349. return 0;
  2350. fscanf(f, "%*u %*s %*c %u", &v);
  2351. fclose(f);
  2352. #endif /* __linux__*/
  2353. #ifdef __OpenBSD__
  2354. int n;
  2355. kvm_t *kd;
  2356. struct kinfo_proc *kp;
  2357. kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, NULL);
  2358. if (!kd)
  2359. return 0;
  2360. kp = kvm_getprocs(kd, KERN_PROC_PID, p, sizeof(*kp), &n);
  2361. v = kp->p_ppid;
  2362. #endif /* __OpenBSD__ */
  2363. return (pid_t)v;
  2364. }
  2365. int
  2366. isdescprocess(pid_t p, pid_t c)
  2367. {
  2368. while (p != c && c != 0)
  2369. c = getparentprocess(c);
  2370. return (int)c;
  2371. }
  2372. Client *
  2373. termforwin(const Client *w)
  2374. {
  2375. Client *c;
  2376. Monitor *m;
  2377. if (!w->pid || w->isterminal)
  2378. return NULL;
  2379. for (m = mons; m; m = m->next) {
  2380. for (c = m->clients; c; c = c->next) {
  2381. if (c->isterminal && !c->swallowing && c->pid && isdescprocess(c->pid, w->pid))
  2382. return c;
  2383. }
  2384. }
  2385. return NULL;
  2386. }
  2387. Client *
  2388. swallowingclient(Window w)
  2389. {
  2390. Client *c;
  2391. Monitor *m;
  2392. for (m = mons; m; m = m->next) {
  2393. for (c = m->clients; c; c = c->next) {
  2394. if (c->swallowing && c->swallowing->win == w)
  2395. return c;
  2396. }
  2397. }
  2398. return NULL;
  2399. }
  2400. Client *
  2401. wintoclient(Window w)
  2402. {
  2403. Client *c;
  2404. Monitor *m;
  2405. for (m = mons; m; m = m->next)
  2406. for (c = m->clients; c; c = c->next)
  2407. if (c->win == w)
  2408. return c;
  2409. return NULL;
  2410. }
  2411. Monitor *
  2412. wintomon(Window w)
  2413. {
  2414. int x, y;
  2415. Client *c;
  2416. Monitor *m;
  2417. if (w == root && getrootptr(&x, &y))
  2418. return recttomon(x, y, 1, 1);
  2419. for (m = mons; m; m = m->next)
  2420. if(w == m->barwin || w == m->tabwin)
  2421. return m;
  2422. if ((c = wintoclient(w)))
  2423. return c->mon;
  2424. return selmon;
  2425. }
  2426. /* There's no way to check accesses to destroyed windows, thus those cases are
  2427. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  2428. * default error handler, which may call exit. */
  2429. int
  2430. xerror(Display *dpy, XErrorEvent *ee)
  2431. {
  2432. if (ee->error_code == BadWindow
  2433. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  2434. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  2435. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  2436. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  2437. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  2438. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  2439. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  2440. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  2441. return 0;
  2442. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  2443. ee->request_code, ee->error_code);
  2444. return xerrorxlib(dpy, ee); /* may call exit */
  2445. }
  2446. int
  2447. xerrordummy(Display *dpy, XErrorEvent *ee)
  2448. {
  2449. return 0;
  2450. }
  2451. /* Startup Error handler to check if another window manager
  2452. * is already running. */
  2453. int
  2454. xerrorstart(Display *dpy, XErrorEvent *ee)
  2455. {
  2456. die("dwm: another window manager is already running");
  2457. return -1;
  2458. }
  2459. void
  2460. zoom(const Arg *arg)
  2461. {
  2462. Client *c = selmon->sel;
  2463. if (!selmon->lt[selmon->sellt]->arrange
  2464. || (selmon->sel && selmon->sel->isfloating))
  2465. return;
  2466. if (c == nexttiled(selmon->clients))
  2467. if (!c || !(c = nexttiled(c->next)))
  2468. return;
  2469. pop(c);
  2470. }
  2471. int
  2472. main(int argc, char *argv[])
  2473. {
  2474. if (argc == 2 && !strcmp("-v", argv[1]))
  2475. die("dwm-"VERSION);
  2476. else if (argc != 1)
  2477. die("usage: dwm [-v]");
  2478. if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  2479. fputs("warning: no locale support\n", stderr);
  2480. if (!(dpy = XOpenDisplay(NULL)))
  2481. die("dwm: cannot open display");
  2482. if (!(xcon = XGetXCBConnection(dpy)))
  2483. die("dwm: cannot get xcb connection\n");
  2484. checkotherwm();
  2485. autostart_exec();
  2486. setup();
  2487. #ifdef __OpenBSD__
  2488. if (pledge("stdio rpath proc exec ps", NULL) == -1)
  2489. die("pledge");
  2490. #endif /* __OpenBSD__ */
  2491. scan();
  2492. run();
  2493. cleanup();
  2494. XCloseDisplay(dpy);
  2495. return EXIT_SUCCESS;
  2496. }
  2497. void
  2498. centeredmaster(Monitor *m)
  2499. {
  2500. unsigned int i, n, h, mw, mx, my, oty, ety, tw;
  2501. Client *c;
  2502. /* count number of clients in the selected monitor */
  2503. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  2504. if (n == 0)
  2505. return;
  2506. /* initialize areas */
  2507. mw = m->ww;
  2508. mx = 0;
  2509. my = 0;
  2510. tw = mw;
  2511. if (n > m->nmaster) {
  2512. /* go mfact box in the center if more than nmaster clients */
  2513. mw = m->nmaster ? m->ww * m->mfact : 0;
  2514. tw = m->ww - mw;
  2515. if (n - m->nmaster > 1) {
  2516. /* only one client */
  2517. mx = (m->ww - mw) / 2;
  2518. tw = (m->ww - mw) / 2;
  2519. }
  2520. }
  2521. oty = 0;
  2522. ety = 0;
  2523. for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  2524. if (i < m->nmaster) {
  2525. /* nmaster clients are stacked vertically, in the center
  2526. * of the screen */
  2527. h = (m->wh - my) / (MIN(n, m->nmaster) - i);
  2528. resize(c, m->wx + mx, m->wy + my, mw - (2*c->bw),
  2529. h - (2*c->bw), 0);
  2530. my += HEIGHT(c);
  2531. } else {
  2532. /* stack clients are stacked vertically */
  2533. if ((i - m->nmaster) % 2 ) {
  2534. h = (m->wh - ety) / ( (1 + n - i) / 2);
  2535. resize(c, m->wx, m->wy + ety, tw - (2*c->bw),
  2536. h - (2*c->bw), 0);
  2537. ety += HEIGHT(c);
  2538. } else {
  2539. h = (m->wh - oty) / ((1 + n - i) / 2);
  2540. resize(c, m->wx + mx + mw, m->wy + oty,
  2541. tw - (2*c->bw), h - (2*c->bw), 0);
  2542. oty += HEIGHT(c);
  2543. }
  2544. }
  2545. }
  2546. void
  2547. centeredfloatingmaster(Monitor *m)
  2548. {
  2549. unsigned int i, n, w, mh, mw, mx, mxo, my, myo, tx;
  2550. Client *c;
  2551. /* count number of clients in the selected monitor */
  2552. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  2553. if (n == 0)
  2554. return;
  2555. /* initialize nmaster area */
  2556. if (n > m->nmaster) {
  2557. /* go mfact box in the center if more than nmaster clients */
  2558. if (m->ww > m->wh) {
  2559. mw = m->nmaster ? m->ww * m->mfact : 0;
  2560. mh = m->nmaster ? m->wh * 0.9 : 0;
  2561. } else {
  2562. mh = m->nmaster ? m->wh * m->mfact : 0;
  2563. mw = m->nmaster ? m->ww * 0.9 : 0;
  2564. }
  2565. mx = mxo = (m->ww - mw) / 2;
  2566. my = myo = (m->wh - mh) / 2;
  2567. } else {
  2568. /* go fullscreen if all clients are in the master area */
  2569. mh = m->wh;
  2570. mw = m->ww;
  2571. mx = mxo = 0;
  2572. my = myo = 0;
  2573. }
  2574. for(i = tx = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  2575. if (i < m->nmaster) {
  2576. /* nmaster clients are stacked horizontally, in the center
  2577. * of the screen */
  2578. w = (mw + mxo - mx) / (MIN(n, m->nmaster) - i);
  2579. resize(c, m->wx + mx, m->wy + my, w - (2*c->bw),
  2580. mh - (2*c->bw), 0);
  2581. mx += WIDTH(c);
  2582. } else {
  2583. /* stack clients are stacked horizontally */
  2584. w = (m->ww - tx) / (n - i);
  2585. resize(c, m->wx + tx, m->wy, w - (2*c->bw),
  2586. m->wh - (2*c->bw), 0);
  2587. tx += WIDTH(c);
  2588. }
  2589. }