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.
 
 
 
 
 

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