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.
 
 
 
 
 

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