My dwm build
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

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