My dwm build
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

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