You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2157 lines
54 KiB

17 years ago
17 years ago
16 years ago
16 years ago
14 years ago
17 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
17 years ago
14 years ago
17 years ago
14 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
13 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
13 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
17 years ago
  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 <stdarg.h>
  26. #include <signal.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. #include <X11/XKBlib.h>
  40. #include <fontconfig/fontconfig.h>
  41. #include <X11/Xft/Xft.h>
  42. #ifdef XINERAMA
  43. #include <X11/extensions/Xinerama.h>
  44. #endif /* XINERAMA */
  45. #include "draw.h"
  46. /* macros */
  47. #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
  48. #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
  49. #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
  50. * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
  51. #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
  52. #define LENGTH(X) (sizeof X / sizeof X[0])
  53. #define MAX(A, B) ((A) > (B) ? (A) : (B))
  54. #define MIN(A, B) ((A) < (B) ? (A) : (B))
  55. #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
  56. #define WIDTH(X) ((X)->w + 2 * (X)->bw)
  57. #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
  58. #define TAGMASK ((1 << LENGTH(tags)) - 1)
  59. #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
  60. /* enums */
  61. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  62. enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
  63. enum { NetSupported, NetWMName, NetWMState,
  64. NetWMFullscreen, NetActiveWindow, NetWMWindowType,
  65. NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
  66. enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
  67. enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
  68. ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
  69. typedef union {
  70. int i;
  71. unsigned int ui;
  72. float f;
  73. const void *v;
  74. } Arg;
  75. typedef struct {
  76. unsigned int click;
  77. unsigned int mask;
  78. unsigned int button;
  79. void (*func)(const Arg *arg);
  80. const Arg arg;
  81. } Button;
  82. typedef struct Monitor Monitor;
  83. typedef struct Client Client;
  84. struct Client {
  85. char name[256];
  86. float mina, maxa;
  87. int x, y, w, h;
  88. int oldx, oldy, oldw, oldh;
  89. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  90. int bw, oldbw;
  91. unsigned int tags;
  92. Bool isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
  93. Client *next;
  94. Client *snext;
  95. Monitor *mon;
  96. Window win;
  97. };
  98. typedef struct {
  99. int x, y, w, h;
  100. XftColor norm[ColLast];
  101. XftColor sel[ColLast];
  102. Drawable drawable;
  103. GC gc;
  104. struct {
  105. int ascent;
  106. int descent;
  107. int height;
  108. XftFont *xfont;
  109. } font;
  110. } DC; /* draw context */
  111. typedef struct {
  112. unsigned int mod;
  113. KeySym keysym;
  114. void (*func)(const Arg *);
  115. const Arg arg;
  116. } Key;
  117. typedef struct {
  118. const char *symbol;
  119. void (*arrange)(Monitor *);
  120. } Layout;
  121. struct Monitor {
  122. char ltsymbol[16];
  123. float mfact;
  124. int nmaster;
  125. int num;
  126. int by; /* bar geometry */
  127. int mx, my, mw, mh; /* screen size */
  128. int wx, wy, ww, wh; /* window area */
  129. unsigned int seltags;
  130. unsigned int sellt;
  131. unsigned int tagset[2];
  132. Bool showbar;
  133. Bool topbar;
  134. Client *clients;
  135. Client *sel;
  136. Client *stack;
  137. Monitor *next;
  138. Window barwin;
  139. const Layout *lt[2];
  140. };
  141. typedef struct {
  142. const char *class;
  143. const char *instance;
  144. const char *title;
  145. unsigned int tags;
  146. Bool isfloating;
  147. int monitor;
  148. } Rule;
  149. /* function declarations */
  150. static void applyrules(Client *c);
  151. static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
  152. static void arrange(Monitor *m);
  153. static void arrangemon(Monitor *m);
  154. static void attach(Client *c);
  155. static void attachstack(Client *c);
  156. static void buttonpress(XEvent *e);
  157. static void checkotherwm(void);
  158. static void cleanup(void);
  159. static void cleanupmon(Monitor *mon);
  160. static void clearurgent(Client *c);
  161. static void clientmessage(XEvent *e);
  162. static void configure(Client *c);
  163. static void configurenotify(XEvent *e);
  164. static void configurerequest(XEvent *e);
  165. static Monitor *createmon(void);
  166. static void destroynotify(XEvent *e);
  167. static void detach(Client *c);
  168. static void detachstack(Client *c);
  169. static void die(const char *errstr, ...);
  170. static Monitor *dirtomon(int dir);
  171. static void drawbar(Monitor *m);
  172. static void drawbars(void);
  173. static void drawsquare(Bool filled, Bool empty, Bool invert, XftColor col[ColLast]);
  174. static void drawtext(const char *text, XftColor col[ColLast], Bool invert);
  175. static void enternotify(XEvent *e);
  176. static void expose(XEvent *e);
  177. static void focus(Client *c);
  178. static void focusin(XEvent *e);
  179. static void focusmon(const Arg *arg);
  180. static void focusstack(const Arg *arg);
  181. static XftColor getcolor(const char *colstr);
  182. static Bool getrootptr(int *x, int *y);
  183. static long getstate(Window w);
  184. static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  185. static void grabbuttons(Client *c, Bool focused);
  186. static void grabkeys(void);
  187. static void incnmaster(const Arg *arg);
  188. static void initfont(const char *fontstr);
  189. static void keypress(XEvent *e);
  190. static void killclient(const Arg *arg);
  191. static void manage(Window w, XWindowAttributes *wa);
  192. static void mappingnotify(XEvent *e);
  193. static void maprequest(XEvent *e);
  194. static void monocle(Monitor *m);
  195. static void motionnotify(XEvent *e);
  196. static void movemouse(const Arg *arg);
  197. static Client *nexttiled(Client *c);
  198. static void pop(Client *);
  199. static void propertynotify(XEvent *e);
  200. static void quit(const Arg *arg);
  201. static Monitor *recttomon(int x, int y, int w, int h);
  202. static void resize(Client *c, int x, int y, int w, int h, Bool interact);
  203. static void resizeclient(Client *c, int x, int y, int w, int h);
  204. static void resizemouse(const Arg *arg);
  205. static void restack(Monitor *m);
  206. static void run(void);
  207. static void scan(void);
  208. static Bool sendevent(Client *c, Atom proto);
  209. static void sendmon(Client *c, Monitor *m);
  210. static void setclientstate(Client *c, long state);
  211. static void setfocus(Client *c);
  212. static void setfullscreen(Client *c, Bool fullscreen);
  213. static void setlayout(const Arg *arg);
  214. static void setmfact(const Arg *arg);
  215. static void setup(void);
  216. static void showhide(Client *c);
  217. static void sigchld(int unused);
  218. static void spawn(const Arg *arg);
  219. static void tag(const Arg *arg);
  220. static void tagmon(const Arg *arg);
  221. static int textnw(const char *text, unsigned int len);
  222. static void tile(Monitor *);
  223. static void togglebar(const Arg *arg);
  224. static void togglefloating(const Arg *arg);
  225. static void toggletag(const Arg *arg);
  226. static void toggleview(const Arg *arg);
  227. static void unfocus(Client *c, Bool setfocus);
  228. static void unmanage(Client *c, Bool destroyed);
  229. static void unmapnotify(XEvent *e);
  230. static Bool updategeom(void);
  231. static void updatebarpos(Monitor *m);
  232. static void updatebars(void);
  233. static void updateclientlist(void);
  234. static void updatenumlockmask(void);
  235. static void updatesizehints(Client *c);
  236. static void updatestatus(void);
  237. static void updatewindowtype(Client *c);
  238. static void updatetitle(Client *c);
  239. static void updatewmhints(Client *c);
  240. static void view(const Arg *arg);
  241. static Client *wintoclient(Window w);
  242. static Monitor *wintomon(Window w);
  243. static int xerror(Display *dpy, XErrorEvent *ee);
  244. static int xerrordummy(Display *dpy, XErrorEvent *ee);
  245. static int xerrorstart(Display *dpy, XErrorEvent *ee);
  246. static void zoom(const Arg *arg);
  247. /* variables */
  248. static const char broken[] = "broken";
  249. static char stext[256];
  250. static int screen;
  251. static int sw, sh; /* X display screen geometry width, height */
  252. static int bh, blw = 0; /* bar geometry */
  253. static int (*xerrorxlib)(Display *, XErrorEvent *);
  254. static unsigned int numlockmask = 0;
  255. static void (*handler[LASTEvent]) (XEvent *) = {
  256. [ButtonPress] = buttonpress,
  257. [ClientMessage] = clientmessage,
  258. [ConfigureRequest] = configurerequest,
  259. [ConfigureNotify] = configurenotify,
  260. [DestroyNotify] = destroynotify,
  261. [EnterNotify] = enternotify,
  262. [Expose] = expose,
  263. [FocusIn] = focusin,
  264. [KeyPress] = keypress,
  265. [MappingNotify] = mappingnotify,
  266. [MapRequest] = maprequest,
  267. [MotionNotify] = motionnotify,
  268. [PropertyNotify] = propertynotify,
  269. [UnmapNotify] = unmapnotify
  270. };
  271. static Atom wmatom[WMLast], netatom[NetLast];
  272. static Bool running = True;
  273. static Cursor cursor[CurLast];
  274. static Display *dpy;
  275. static DC dc;
  276. static Monitor *mons = NULL, *selmon = NULL;
  277. static Window root;
  278. /* configuration, allows nested code to access above variables */
  279. #include "config.h"
  280. /* compile-time check if all tags fit into an unsigned int bit array. */
  281. struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
  282. /* function implementations */
  283. void
  284. applyrules(Client *c) {
  285. const char *class, *instance;
  286. unsigned int i;
  287. const Rule *r;
  288. Monitor *m;
  289. XClassHint ch = { NULL, NULL };
  290. /* rule matching */
  291. c->isfloating = c->tags = 0;
  292. XGetClassHint(dpy, c->win, &ch);
  293. class = ch.res_class ? ch.res_class : broken;
  294. instance = ch.res_name ? ch.res_name : broken;
  295. for(i = 0; i < LENGTH(rules); i++) {
  296. r = &rules[i];
  297. if((!r->title || strstr(c->name, r->title))
  298. && (!r->class || strstr(class, r->class))
  299. && (!r->instance || strstr(instance, r->instance)))
  300. {
  301. c->isfloating = r->isfloating;
  302. c->tags |= r->tags;
  303. for(m = mons; m && m->num != r->monitor; m = m->next);
  304. if(m)
  305. c->mon = m;
  306. }
  307. }
  308. if(ch.res_class)
  309. XFree(ch.res_class);
  310. if(ch.res_name)
  311. XFree(ch.res_name);
  312. c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
  313. }
  314. Bool
  315. applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
  316. Bool baseismin;
  317. Monitor *m = c->mon;
  318. /* set minimum possible */
  319. *w = MAX(1, *w);
  320. *h = MAX(1, *h);
  321. if(interact) {
  322. if(*x > sw)
  323. *x = sw - WIDTH(c);
  324. if(*y > sh)
  325. *y = sh - HEIGHT(c);
  326. if(*x + *w + 2 * c->bw < 0)
  327. *x = 0;
  328. if(*y + *h + 2 * c->bw < 0)
  329. *y = 0;
  330. }
  331. else {
  332. if(*x >= m->wx + m->ww)
  333. *x = m->wx + m->ww - WIDTH(c);
  334. if(*y >= m->wy + m->wh)
  335. *y = m->wy + m->wh - HEIGHT(c);
  336. if(*x + *w + 2 * c->bw <= m->wx)
  337. *x = m->wx;
  338. if(*y + *h + 2 * c->bw <= m->wy)
  339. *y = m->wy;
  340. }
  341. if(*h < bh)
  342. *h = bh;
  343. if(*w < bh)
  344. *w = bh;
  345. if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
  346. /* see last two sentences in ICCCM 4.1.2.3 */
  347. baseismin = c->basew == c->minw && c->baseh == c->minh;
  348. if(!baseismin) { /* temporarily remove base dimensions */
  349. *w -= c->basew;
  350. *h -= c->baseh;
  351. }
  352. /* adjust for aspect limits */
  353. if(c->mina > 0 && c->maxa > 0) {
  354. if(c->maxa < (float)*w / *h)
  355. *w = *h * c->maxa + 0.5;
  356. else if(c->mina < (float)*h / *w)
  357. *h = *w * c->mina + 0.5;
  358. }
  359. if(baseismin) { /* increment calculation requires this */
  360. *w -= c->basew;
  361. *h -= c->baseh;
  362. }
  363. /* adjust for increment value */
  364. if(c->incw)
  365. *w -= *w % c->incw;
  366. if(c->inch)
  367. *h -= *h % c->inch;
  368. /* restore base dimensions */
  369. *w = MAX(*w + c->basew, c->minw);
  370. *h = MAX(*h + c->baseh, c->minh);
  371. if(c->maxw)
  372. *w = MIN(*w, c->maxw);
  373. if(c->maxh)
  374. *h = MIN(*h, c->maxh);
  375. }
  376. return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
  377. }
  378. void
  379. arrange(Monitor *m) {
  380. if(m)
  381. showhide(m->stack);
  382. else for(m = mons; m; m = m->next)
  383. showhide(m->stack);
  384. if(m) {
  385. arrangemon(m);
  386. restack(m);
  387. } else for(m = mons; m; m = m->next)
  388. arrangemon(m);
  389. }
  390. void
  391. arrangemon(Monitor *m) {
  392. strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
  393. if(m->lt[m->sellt]->arrange)
  394. m->lt[m->sellt]->arrange(m);
  395. }
  396. void
  397. attach(Client *c) {
  398. c->next = c->mon->clients;
  399. c->mon->clients = c;
  400. }
  401. void
  402. attachstack(Client *c) {
  403. c->snext = c->mon->stack;
  404. c->mon->stack = c;
  405. }
  406. void
  407. buttonpress(XEvent *e) {
  408. unsigned int i, x, click;
  409. Arg arg = {0};
  410. Client *c;
  411. Monitor *m;
  412. XButtonPressedEvent *ev = &e->xbutton;
  413. click = ClkRootWin;
  414. /* focus monitor if necessary */
  415. if((m = wintomon(ev->window)) && m != selmon) {
  416. unfocus(selmon->sel, True);
  417. selmon = m;
  418. focus(NULL);
  419. }
  420. if(ev->window == selmon->barwin) {
  421. i = x = 0;
  422. do
  423. x += TEXTW(tags[i]);
  424. while(ev->x >= x && ++i < LENGTH(tags));
  425. if(i < LENGTH(tags)) {
  426. click = ClkTagBar;
  427. arg.ui = 1 << i;
  428. }
  429. else if(ev->x < x + blw)
  430. click = ClkLtSymbol;
  431. else if(ev->x > selmon->ww - TEXTW(stext))
  432. click = ClkStatusText;
  433. else
  434. click = ClkWinTitle;
  435. }
  436. else if((c = wintoclient(ev->window))) {
  437. focus(c);
  438. click = ClkClientWin;
  439. }
  440. for(i = 0; i < LENGTH(buttons); i++)
  441. if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
  442. && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
  443. buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
  444. }
  445. void
  446. checkotherwm(void) {
  447. xerrorxlib = XSetErrorHandler(xerrorstart);
  448. /* this causes an error if some other window manager is running */
  449. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  450. XSync(dpy, False);
  451. XSetErrorHandler(xerror);
  452. XSync(dpy, False);
  453. }
  454. void
  455. cleanup(void) {
  456. Arg a = {.ui = ~0};
  457. Layout foo = { "", NULL };
  458. Monitor *m;
  459. view(&a);
  460. selmon->lt[selmon->sellt] = &foo;
  461. for(m = mons; m; m = m->next)
  462. while(m->stack)
  463. unmanage(m->stack, False);
  464. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  465. XFreePixmap(dpy, dc.drawable);
  466. XFreeGC(dpy, dc.gc);
  467. XFreeCursor(dpy, cursor[CurNormal]);
  468. XFreeCursor(dpy, cursor[CurResize]);
  469. XFreeCursor(dpy, cursor[CurMove]);
  470. while(mons)
  471. cleanupmon(mons);
  472. XSync(dpy, False);
  473. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  474. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  475. }
  476. void
  477. cleanupmon(Monitor *mon) {
  478. Monitor *m;
  479. if(mon == mons)
  480. mons = mons->next;
  481. else {
  482. for(m = mons; m && m->next != mon; m = m->next);
  483. m->next = mon->next;
  484. }
  485. XUnmapWindow(dpy, mon->barwin);
  486. XDestroyWindow(dpy, mon->barwin);
  487. free(mon);
  488. }
  489. void
  490. clearurgent(Client *c) {
  491. XWMHints *wmh;
  492. c->isurgent = False;
  493. if(!(wmh = XGetWMHints(dpy, c->win)))
  494. return;
  495. wmh->flags &= ~XUrgencyHint;
  496. XSetWMHints(dpy, c->win, wmh);
  497. XFree(wmh);
  498. }
  499. void
  500. clientmessage(XEvent *e) {
  501. XClientMessageEvent *cme = &e->xclient;
  502. Client *c = wintoclient(cme->window);
  503. if(!c)
  504. return;
  505. if(cme->message_type == netatom[NetWMState]) {
  506. if(cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
  507. setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
  508. || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
  509. }
  510. else if(cme->message_type == netatom[NetActiveWindow]) {
  511. if(!ISVISIBLE(c)) {
  512. c->mon->seltags ^= 1;
  513. c->mon->tagset[c->mon->seltags] = c->tags;
  514. }
  515. pop(c);
  516. }
  517. }
  518. void
  519. configure(Client *c) {
  520. XConfigureEvent ce;
  521. ce.type = ConfigureNotify;
  522. ce.display = dpy;
  523. ce.event = c->win;
  524. ce.window = c->win;
  525. ce.x = c->x;
  526. ce.y = c->y;
  527. ce.width = c->w;
  528. ce.height = c->h;
  529. ce.border_width = c->bw;
  530. ce.above = None;
  531. ce.override_redirect = False;
  532. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  533. }
  534. void
  535. configurenotify(XEvent *e) {
  536. Monitor *m;
  537. XConfigureEvent *ev = &e->xconfigure;
  538. Bool dirty;
  539. // TODO: updategeom handling sucks, needs to be simplified
  540. if(ev->window == root) {
  541. dirty = (sw != ev->width || sh != ev->height);
  542. sw = ev->width;
  543. sh = ev->height;
  544. if(updategeom() || dirty) {
  545. if(dc.drawable != 0)
  546. XFreePixmap(dpy, dc.drawable);
  547. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  548. updatebars();
  549. for(m = mons; m; m = m->next)
  550. XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
  551. focus(NULL);
  552. arrange(NULL);
  553. }
  554. }
  555. }
  556. void
  557. configurerequest(XEvent *e) {
  558. Client *c;
  559. Monitor *m;
  560. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  561. XWindowChanges wc;
  562. if((c = wintoclient(ev->window))) {
  563. if(ev->value_mask & CWBorderWidth)
  564. c->bw = ev->border_width;
  565. else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
  566. m = c->mon;
  567. if(ev->value_mask & CWX) {
  568. c->oldx = c->x;
  569. c->x = m->mx + ev->x;
  570. }
  571. if(ev->value_mask & CWY) {
  572. c->oldy = c->y;
  573. c->y = m->my + ev->y;
  574. }
  575. if(ev->value_mask & CWWidth) {
  576. c->oldw = c->w;
  577. c->w = ev->width;
  578. }
  579. if(ev->value_mask & CWHeight) {
  580. c->oldh = c->h;
  581. c->h = ev->height;
  582. }
  583. if((c->x + c->w) > m->mx + m->mw && c->isfloating)
  584. c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
  585. if((c->y + c->h) > m->my + m->mh && c->isfloating)
  586. c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
  587. if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
  588. configure(c);
  589. if(ISVISIBLE(c))
  590. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  591. }
  592. else
  593. configure(c);
  594. }
  595. else {
  596. wc.x = ev->x;
  597. wc.y = ev->y;
  598. wc.width = ev->width;
  599. wc.height = ev->height;
  600. wc.border_width = ev->border_width;
  601. wc.sibling = ev->above;
  602. wc.stack_mode = ev->detail;
  603. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  604. }
  605. XSync(dpy, False);
  606. }
  607. Monitor *
  608. createmon(void) {
  609. Monitor *m;
  610. if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
  611. die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
  612. m->tagset[0] = m->tagset[1] = 1;
  613. m->mfact = mfact;
  614. m->nmaster = nmaster;
  615. m->showbar = showbar;
  616. m->topbar = topbar;
  617. m->lt[0] = &layouts[0];
  618. m->lt[1] = &layouts[1 % LENGTH(layouts)];
  619. strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
  620. return m;
  621. }
  622. void
  623. destroynotify(XEvent *e) {
  624. Client *c;
  625. XDestroyWindowEvent *ev = &e->xdestroywindow;
  626. if((c = wintoclient(ev->window)))
  627. unmanage(c, True);
  628. }
  629. void
  630. detach(Client *c) {
  631. Client **tc;
  632. for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
  633. *tc = c->next;
  634. }
  635. void
  636. detachstack(Client *c) {
  637. Client **tc, *t;
  638. for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
  639. *tc = c->snext;
  640. if(c == c->mon->sel) {
  641. for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
  642. c->mon->sel = t;
  643. }
  644. }
  645. void
  646. die(const char *errstr, ...) {
  647. va_list ap;
  648. va_start(ap, errstr);
  649. vfprintf(stderr, errstr, ap);
  650. va_end(ap);
  651. exit(EXIT_FAILURE);
  652. }
  653. Monitor *
  654. dirtomon(int dir) {
  655. Monitor *m = NULL;
  656. if(dir > 0) {
  657. if(!(m = selmon->next))
  658. m = mons;
  659. }
  660. else if(selmon == mons)
  661. for(m = mons; m->next; m = m->next);
  662. else
  663. for(m = mons; m->next != selmon; m = m->next);
  664. return m;
  665. }
  666. void
  667. drawbar(Monitor *m) {
  668. int x;
  669. unsigned int i, occ = 0, urg = 0;
  670. XftColor *col;
  671. Client *c;
  672. for(c = m->clients; c; c = c->next) {
  673. occ |= c->tags;
  674. if(c->isurgent)
  675. urg |= c->tags;
  676. }
  677. dc.x = 0;
  678. for(i = 0; i < LENGTH(tags); i++) {
  679. dc.w = TEXTW(tags[i]);
  680. col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
  681. drawtext(tags[i], col, urg & 1 << i);
  682. drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
  683. occ & 1 << i, urg & 1 << i, col);
  684. dc.x += dc.w;
  685. }
  686. dc.w = blw = TEXTW(m->ltsymbol);
  687. drawtext(m->ltsymbol, dc.norm, False);
  688. dc.x += dc.w;
  689. x = dc.x;
  690. if(m == selmon) { /* status is only drawn on selected monitor */
  691. dc.w = TEXTW(stext);
  692. dc.x = m->ww - dc.w;
  693. if(dc.x < x) {
  694. dc.x = x;
  695. dc.w = m->ww - x;
  696. }
  697. drawtext(stext, dc.norm, False);
  698. }
  699. else
  700. dc.x = m->ww;
  701. if((dc.w = dc.x - x) > bh) {
  702. dc.x = x;
  703. if(m->sel) {
  704. col = m == selmon ? dc.sel : dc.norm;
  705. drawtext(m->sel->name, col, False);
  706. drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
  707. }
  708. else
  709. drawtext(NULL, dc.norm, False);
  710. }
  711. XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
  712. XSync(dpy, False);
  713. }
  714. void
  715. drawbars(void) {
  716. Monitor *m;
  717. for(m = mons; m; m = m->next)
  718. drawbar(m);
  719. }
  720. void
  721. drawsquare(Bool filled, Bool empty, Bool invert, XftColor col[ColLast]) {
  722. int x;
  723. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG].pixel);
  724. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  725. if(filled)
  726. XFillRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x+1, x+1);
  727. else if(empty)
  728. XDrawRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x, x);
  729. }
  730. void
  731. drawtext(const char *text, XftColor col[ColLast], Bool invert) {
  732. char buf[256];
  733. int i, x, y, h, len, olen;
  734. XftDraw *d;
  735. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG].pixel);
  736. XFillRectangle(dpy, dc.drawable, dc.gc, dc.x, dc.y, dc.w, dc.h);
  737. if(!text)
  738. return;
  739. olen = strlen(text);
  740. h = dc.font.ascent + dc.font.descent;
  741. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  742. x = dc.x + (h / 2);
  743. /* shorten text if necessary */
  744. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  745. if(!len)
  746. return;
  747. memcpy(buf, text, len);
  748. if(len < olen)
  749. for(i = len; i && i > len - 3; buf[--i] = '.');
  750. d = XftDrawCreate(dpy, dc.drawable, DefaultVisual(dpy, screen), DefaultColormap(dpy,screen));
  751. XftDrawStringUtf8(d, &col[invert ? ColBG : ColFG], dc.font.xfont, x, y, (XftChar8 *) buf, len);
  752. XftDrawDestroy(d);
  753. }
  754. void
  755. enternotify(XEvent *e) {
  756. Client *c;
  757. Monitor *m;
  758. XCrossingEvent *ev = &e->xcrossing;
  759. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  760. return;
  761. c = wintoclient(ev->window);
  762. m = c ? c->mon : wintomon(ev->window);
  763. if(m != selmon) {
  764. unfocus(selmon->sel, True);
  765. selmon = m;
  766. }
  767. else if(!c || c == selmon->sel)
  768. return;
  769. focus(c);
  770. }
  771. void
  772. expose(XEvent *e) {
  773. Monitor *m;
  774. XExposeEvent *ev = &e->xexpose;
  775. if(ev->count == 0 && (m = wintomon(ev->window)))
  776. drawbar(m);
  777. }
  778. void
  779. focus(Client *c) {
  780. if(!c || !ISVISIBLE(c))
  781. for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
  782. /* was if(selmon->sel) */
  783. if(selmon->sel && selmon->sel != c)
  784. unfocus(selmon->sel, False);
  785. if(c) {
  786. if(c->mon != selmon)
  787. selmon = c->mon;
  788. if(c->isurgent)
  789. clearurgent(c);
  790. detachstack(c);
  791. attachstack(c);
  792. grabbuttons(c, True);
  793. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder].pixel);
  794. setfocus(c);
  795. }
  796. else {
  797. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  798. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  799. }
  800. selmon->sel = c;
  801. drawbars();
  802. }
  803. void
  804. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  805. XFocusChangeEvent *ev = &e->xfocus;
  806. if(selmon->sel && ev->window != selmon->sel->win)
  807. setfocus(selmon->sel);
  808. }
  809. void
  810. focusmon(const Arg *arg) {
  811. Monitor *m;
  812. if(!mons->next)
  813. return;
  814. if((m = dirtomon(arg->i)) == selmon)
  815. return;
  816. unfocus(selmon->sel, False); /* s/True/False/ fixes input focus issues
  817. in gedit and anjuta */
  818. selmon = m;
  819. focus(NULL);
  820. }
  821. void
  822. focusstack(const Arg *arg) {
  823. Client *c = NULL, *i;
  824. if(!selmon->sel)
  825. return;
  826. if(arg->i > 0) {
  827. for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
  828. if(!c)
  829. for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
  830. }
  831. else {
  832. for(i = selmon->clients; i != selmon->sel; i = i->next)
  833. if(ISVISIBLE(i))
  834. c = i;
  835. if(!c)
  836. for(; i; i = i->next)
  837. if(ISVISIBLE(i))
  838. c = i;
  839. }
  840. if(c) {
  841. focus(c);
  842. restack(selmon);
  843. }
  844. }
  845. Atom
  846. getatomprop(Client *c, Atom prop) {
  847. int di;
  848. unsigned long dl;
  849. unsigned char *p = NULL;
  850. Atom da, atom = None;
  851. if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
  852. &da, &di, &dl, &dl, &p) == Success && p) {
  853. atom = *(Atom *)p;
  854. XFree(p);
  855. }
  856. return atom;
  857. }
  858. XftColor
  859. getcolor(const char *colstr) {
  860. XftColor color;
  861. if(!XftColorAllocName(dpy, DefaultVisual(dpy, screen), DefaultColormap(dpy, screen), colstr, &color))
  862. die("error, cannot allocate color '%s'\n", colstr);
  863. return color;
  864. }
  865. Bool
  866. getrootptr(int *x, int *y) {
  867. int di;
  868. unsigned int dui;
  869. Window dummy;
  870. return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
  871. }
  872. long
  873. getstate(Window w) {
  874. int format;
  875. long result = -1;
  876. unsigned char *p = NULL;
  877. unsigned long n, extra;
  878. Atom real;
  879. if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  880. &real, &format, &n, &extra, (unsigned char **)&p) != Success)
  881. return -1;
  882. if(n != 0)
  883. result = *p;
  884. XFree(p);
  885. return result;
  886. }
  887. Bool
  888. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  889. char **list = NULL;
  890. int n;
  891. XTextProperty name;
  892. if(!text || size == 0)
  893. return False;
  894. text[0] = '\0';
  895. XGetTextProperty(dpy, w, &name, atom);
  896. if(!name.nitems)
  897. return False;
  898. if(name.encoding == XA_STRING)
  899. strncpy(text, (char *)name.value, size - 1);
  900. else {
  901. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
  902. strncpy(text, *list, size - 1);
  903. XFreeStringList(list);
  904. }
  905. }
  906. text[size - 1] = '\0';
  907. XFree(name.value);
  908. return True;
  909. }
  910. void
  911. grabbuttons(Client *c, Bool focused) {
  912. updatenumlockmask();
  913. {
  914. unsigned int i, j;
  915. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  916. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  917. if(focused) {
  918. for(i = 0; i < LENGTH(buttons); i++)
  919. if(buttons[i].click == ClkClientWin)
  920. for(j = 0; j < LENGTH(modifiers); j++)
  921. XGrabButton(dpy, buttons[i].button,
  922. buttons[i].mask | modifiers[j],
  923. c->win, False, BUTTONMASK,
  924. GrabModeAsync, GrabModeSync, None, None);
  925. }
  926. else
  927. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  928. BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  929. }
  930. }
  931. void
  932. grabkeys(void) {
  933. updatenumlockmask();
  934. {
  935. unsigned int i, j;
  936. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  937. KeyCode code;
  938. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  939. for(i = 0; i < LENGTH(keys); i++)
  940. if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  941. for(j = 0; j < LENGTH(modifiers); j++)
  942. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  943. True, GrabModeAsync, GrabModeAsync);
  944. }
  945. }
  946. void
  947. incnmaster(const Arg *arg) {
  948. selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
  949. arrange(selmon);
  950. }
  951. void
  952. initfont(const char *fontstr) {
  953. if(!(dc.font.xfont = XftFontOpenName(dpy,screen,fontstr))
  954. && !(dc.font.xfont = XftFontOpenName(dpy,screen,"fixed")))
  955. die("error, cannot load font: '%s'\n", fontstr);
  956. dc.font.ascent = dc.font.xfont->ascent;
  957. dc.font.descent = dc.font.xfont->descent;
  958. dc.font.height = dc.font.ascent + dc.font.descent;
  959. }
  960. #ifdef XINERAMA
  961. static Bool
  962. isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
  963. while(n--)
  964. if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
  965. && unique[n].width == info->width && unique[n].height == info->height)
  966. return False;
  967. return True;
  968. }
  969. #endif /* XINERAMA */
  970. void
  971. keypress(XEvent *e) {
  972. unsigned int i;
  973. KeySym keysym;
  974. XKeyEvent *ev;
  975. ev = &e->xkey;
  976. keysym = XkbKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0, 0);
  977. for(i = 0; i < LENGTH(keys); i++)
  978. if(keysym == keys[i].keysym
  979. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  980. && keys[i].func)
  981. keys[i].func(&(keys[i].arg));
  982. }
  983. void
  984. killclient(const Arg *arg) {
  985. if(!selmon->sel)
  986. return;
  987. if(!sendevent(selmon->sel, wmatom[WMDelete])) {
  988. XGrabServer(dpy);
  989. XSetErrorHandler(xerrordummy);
  990. XSetCloseDownMode(dpy, DestroyAll);
  991. XKillClient(dpy, selmon->sel->win);
  992. XSync(dpy, False);
  993. XSetErrorHandler(xerror);
  994. XUngrabServer(dpy);
  995. }
  996. }
  997. void
  998. manage(Window w, XWindowAttributes *wa) {
  999. Client *c, *t = NULL;
  1000. Window trans = None;
  1001. XWindowChanges wc;
  1002. if(!(c = calloc(1, sizeof(Client))))
  1003. die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  1004. c->win = w;
  1005. updatetitle(c);
  1006. if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
  1007. c->mon = t->mon;
  1008. c->tags = t->tags;
  1009. }
  1010. else {
  1011. c->mon = selmon;
  1012. applyrules(c);
  1013. }
  1014. /* geometry */
  1015. c->x = c->oldx = wa->x;
  1016. c->y = c->oldy = wa->y;
  1017. c->w = c->oldw = wa->width;
  1018. c->h = c->oldh = wa->height;
  1019. c->oldbw = wa->border_width;
  1020. if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  1021. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  1022. if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  1023. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  1024. c->x = MAX(c->x, c->mon->mx);
  1025. /* only fix client y-offset, if the client center might cover the bar */
  1026. c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
  1027. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  1028. c->bw = borderpx;
  1029. wc.border_width = c->bw;
  1030. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  1031. XSetWindowBorder(dpy, w, dc.norm[ColBorder].pixel);
  1032. configure(c); /* propagates border_width, if size doesn't change */
  1033. updatewindowtype(c);
  1034. updatesizehints(c);
  1035. updatewmhints(c);
  1036. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  1037. grabbuttons(c, False);
  1038. if(!c->isfloating)
  1039. c->isfloating = c->oldstate = trans != None || c->isfixed;
  1040. if(c->isfloating)
  1041. XRaiseWindow(dpy, c->win);
  1042. attach(c);
  1043. attachstack(c);
  1044. XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
  1045. (unsigned char *) &(c->win), 1);
  1046. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  1047. setclientstate(c, NormalState);
  1048. if (c->mon == selmon)
  1049. unfocus(selmon->sel, False);
  1050. c->mon->sel = c;
  1051. arrange(c->mon);
  1052. XMapWindow(dpy, c->win);
  1053. focus(NULL);
  1054. }
  1055. void
  1056. mappingnotify(XEvent *e) {
  1057. XMappingEvent *ev = &e->xmapping;
  1058. XRefreshKeyboardMapping(ev);
  1059. if(ev->request == MappingKeyboard)
  1060. grabkeys();
  1061. }
  1062. void
  1063. maprequest(XEvent *e) {
  1064. static XWindowAttributes wa;
  1065. XMapRequestEvent *ev = &e->xmaprequest;
  1066. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1067. return;
  1068. if(wa.override_redirect)
  1069. return;
  1070. if(!wintoclient(ev->window))
  1071. manage(ev->window, &wa);
  1072. }
  1073. void
  1074. monocle(Monitor *m) {
  1075. unsigned int n = 0;
  1076. Client *c;
  1077. for(c = m->clients; c; c = c->next)
  1078. if(ISVISIBLE(c))
  1079. n++;
  1080. if(n > 0) /* override layout symbol */
  1081. snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
  1082. for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1083. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
  1084. }
  1085. void
  1086. motionnotify(XEvent *e) {
  1087. static Monitor *mon = NULL;
  1088. Monitor *m;
  1089. XMotionEvent *ev = &e->xmotion;
  1090. if(ev->window != root)
  1091. return;
  1092. if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
  1093. unfocus(selmon->sel, True);
  1094. selmon = m;
  1095. focus(NULL);
  1096. }
  1097. mon = m;
  1098. }
  1099. void
  1100. movemouse(const Arg *arg) {
  1101. int x, y, ocx, ocy, nx, ny;
  1102. Client *c;
  1103. Monitor *m;
  1104. XEvent ev;
  1105. if(!(c = selmon->sel))
  1106. return;
  1107. if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
  1108. return;
  1109. restack(selmon);
  1110. ocx = c->x;
  1111. ocy = c->y;
  1112. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1113. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1114. return;
  1115. if(!getrootptr(&x, &y))
  1116. return;
  1117. do {
  1118. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1119. switch(ev.type) {
  1120. case ConfigureRequest:
  1121. case Expose:
  1122. case MapRequest:
  1123. handler[ev.type](&ev);
  1124. break;
  1125. case MotionNotify:
  1126. nx = ocx + (ev.xmotion.x - x);
  1127. ny = ocy + (ev.xmotion.y - y);
  1128. if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1129. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1130. if(abs(selmon->wx - nx) < snap)
  1131. nx = selmon->wx;
  1132. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1133. nx = selmon->wx + selmon->ww - WIDTH(c);
  1134. if(abs(selmon->wy - ny) < snap)
  1135. ny = selmon->wy;
  1136. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1137. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1138. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1139. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1140. togglefloating(NULL);
  1141. }
  1142. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1143. resize(c, nx, ny, c->w, c->h, True);
  1144. break;
  1145. }
  1146. } while(ev.type != ButtonRelease);
  1147. XUngrabPointer(dpy, CurrentTime);
  1148. if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1149. sendmon(c, m);
  1150. selmon = m;
  1151. focus(NULL);
  1152. }
  1153. }
  1154. Client *
  1155. nexttiled(Client *c) {
  1156. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1157. return c;
  1158. }
  1159. void
  1160. pop(Client *c) {
  1161. detach(c);
  1162. attach(c);
  1163. focus(c);
  1164. arrange(c->mon);
  1165. }
  1166. void
  1167. propertynotify(XEvent *e) {
  1168. Client *c;
  1169. Window trans;
  1170. XPropertyEvent *ev = &e->xproperty;
  1171. if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1172. updatestatus();
  1173. else if(ev->state == PropertyDelete)
  1174. return; /* ignore */
  1175. else if((c = wintoclient(ev->window))) {
  1176. switch(ev->atom) {
  1177. default: break;
  1178. case XA_WM_TRANSIENT_FOR:
  1179. if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
  1180. (c->isfloating = (wintoclient(trans)) != NULL))
  1181. arrange(c->mon);
  1182. break;
  1183. case XA_WM_NORMAL_HINTS:
  1184. updatesizehints(c);
  1185. break;
  1186. case XA_WM_HINTS:
  1187. updatewmhints(c);
  1188. drawbars();
  1189. break;
  1190. }
  1191. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1192. updatetitle(c);
  1193. if(c == c->mon->sel)
  1194. drawbar(c->mon);
  1195. }
  1196. if(ev->atom == netatom[NetWMWindowType])
  1197. updatewindowtype(c);
  1198. }
  1199. }
  1200. void
  1201. quit(const Arg *arg) {
  1202. running = False;
  1203. }
  1204. Monitor *
  1205. recttomon(int x, int y, int w, int h) {
  1206. Monitor *m, *r = selmon;
  1207. int a, area = 0;
  1208. for(m = mons; m; m = m->next)
  1209. if((a = INTERSECT(x, y, w, h, m)) > area) {
  1210. area = a;
  1211. r = m;
  1212. }
  1213. return r;
  1214. }
  1215. void
  1216. resize(Client *c, int x, int y, int w, int h, Bool interact) {
  1217. if(applysizehints(c, &x, &y, &w, &h, interact))
  1218. resizeclient(c, x, y, w, h);
  1219. }
  1220. void
  1221. resizeclient(Client *c, int x, int y, int w, int h) {
  1222. XWindowChanges wc;
  1223. c->oldx = c->x; c->x = wc.x = x;
  1224. c->oldy = c->y; c->y = wc.y = y;
  1225. c->oldw = c->w; c->w = wc.width = w;
  1226. c->oldh = c->h; c->h = wc.height = h;
  1227. wc.border_width = c->bw;
  1228. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1229. configure(c);
  1230. XSync(dpy, False);
  1231. }
  1232. void
  1233. resizemouse(const Arg *arg) {
  1234. int ocx, ocy;
  1235. int nw, nh;
  1236. Client *c;
  1237. Monitor *m;
  1238. XEvent ev;
  1239. if(!(c = selmon->sel))
  1240. return;
  1241. if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
  1242. return;
  1243. restack(selmon);
  1244. ocx = c->x;
  1245. ocy = c->y;
  1246. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1247. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1248. return;
  1249. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1250. do {
  1251. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1252. switch(ev.type) {
  1253. case ConfigureRequest:
  1254. case Expose:
  1255. case MapRequest:
  1256. handler[ev.type](&ev);
  1257. break;
  1258. case MotionNotify:
  1259. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1260. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1261. if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
  1262. && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
  1263. {
  1264. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1265. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1266. togglefloating(NULL);
  1267. }
  1268. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1269. resize(c, c->x, c->y, nw, nh, True);
  1270. break;
  1271. }
  1272. } while(ev.type != ButtonRelease);
  1273. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1274. XUngrabPointer(dpy, CurrentTime);
  1275. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1276. if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1277. sendmon(c, m);
  1278. selmon = m;
  1279. focus(NULL);
  1280. }
  1281. }
  1282. void
  1283. restack(Monitor *m) {
  1284. Client *c;
  1285. XEvent ev;
  1286. XWindowChanges wc;
  1287. drawbar(m);
  1288. if(!m->sel)
  1289. return;
  1290. if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
  1291. XRaiseWindow(dpy, m->sel->win);
  1292. if(m->lt[m->sellt]->arrange) {
  1293. wc.stack_mode = Below;
  1294. wc.sibling = m->barwin;
  1295. for(c = m->stack; c; c = c->snext)
  1296. if(!c->isfloating && ISVISIBLE(c)) {
  1297. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1298. wc.sibling = c->win;
  1299. }
  1300. }
  1301. XSync(dpy, False);
  1302. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1303. }
  1304. void
  1305. run(void) {
  1306. XEvent ev;
  1307. /* main event loop */
  1308. XSync(dpy, False);
  1309. while(running && !XNextEvent(dpy, &ev))
  1310. if(handler[ev.type])
  1311. handler[ev.type](&ev); /* call handler */
  1312. }
  1313. void
  1314. scan(void) {
  1315. unsigned int i, num;
  1316. Window d1, d2, *wins = NULL;
  1317. XWindowAttributes wa;
  1318. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1319. for(i = 0; i < num; i++) {
  1320. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1321. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1322. continue;
  1323. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1324. manage(wins[i], &wa);
  1325. }
  1326. for(i = 0; i < num; i++) { /* now the transients */
  1327. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1328. continue;
  1329. if(XGetTransientForHint(dpy, wins[i], &d1)
  1330. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1331. manage(wins[i], &wa);
  1332. }
  1333. if(wins)
  1334. XFree(wins);
  1335. }
  1336. }
  1337. void
  1338. sendmon(Client *c, Monitor *m) {
  1339. if(c->mon == m)
  1340. return;
  1341. unfocus(c, True);
  1342. detach(c);
  1343. detachstack(c);
  1344. c->mon = m;
  1345. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1346. attach(c);
  1347. attachstack(c);
  1348. focus(NULL);
  1349. arrange(NULL);
  1350. }
  1351. void
  1352. setclientstate(Client *c, long state) {
  1353. long data[] = { state, None };
  1354. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1355. PropModeReplace, (unsigned char *)data, 2);
  1356. }
  1357. Bool
  1358. sendevent(Client *c, Atom proto) {
  1359. int n;
  1360. Atom *protocols;
  1361. Bool exists = False;
  1362. XEvent ev;
  1363. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  1364. while(!exists && n--)
  1365. exists = protocols[n] == proto;
  1366. XFree(protocols);
  1367. }
  1368. if(exists) {
  1369. ev.type = ClientMessage;
  1370. ev.xclient.window = c->win;
  1371. ev.xclient.message_type = wmatom[WMProtocols];
  1372. ev.xclient.format = 32;
  1373. ev.xclient.data.l[0] = proto;
  1374. ev.xclient.data.l[1] = CurrentTime;
  1375. XSendEvent(dpy, c->win, False, NoEventMask, &ev);
  1376. }
  1377. return exists;
  1378. }
  1379. void
  1380. setfocus(Client *c) {
  1381. if(!c->neverfocus) {
  1382. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  1383. XChangeProperty(dpy, root, netatom[NetActiveWindow],
  1384. XA_WINDOW, 32, PropModeReplace,
  1385. (unsigned char *) &(c->win), 1);
  1386. }
  1387. sendevent(c, wmatom[WMTakeFocus]);
  1388. }
  1389. void
  1390. setfullscreen(Client *c, Bool fullscreen) {
  1391. if(fullscreen) {
  1392. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1393. PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
  1394. c->isfullscreen = True;
  1395. c->oldstate = c->isfloating;
  1396. c->oldbw = c->bw;
  1397. c->bw = 0;
  1398. c->isfloating = True;
  1399. resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
  1400. XRaiseWindow(dpy, c->win);
  1401. }
  1402. else {
  1403. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1404. PropModeReplace, (unsigned char*)0, 0);
  1405. c->isfullscreen = False;
  1406. c->isfloating = c->oldstate;
  1407. c->bw = c->oldbw;
  1408. c->x = c->oldx;
  1409. c->y = c->oldy;
  1410. c->w = c->oldw;
  1411. c->h = c->oldh;
  1412. resizeclient(c, c->x, c->y, c->w, c->h);
  1413. arrange(c->mon);
  1414. }
  1415. }
  1416. void
  1417. setlayout(const Arg *arg) {
  1418. if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
  1419. selmon->sellt ^= 1;
  1420. if(arg && arg->v)
  1421. selmon->lt[selmon->sellt] = (Layout *)arg->v;
  1422. strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
  1423. if(selmon->sel)
  1424. arrange(selmon);
  1425. else
  1426. drawbar(selmon);
  1427. }
  1428. /* arg > 1.0 will set mfact absolutly */
  1429. void
  1430. setmfact(const Arg *arg) {
  1431. float f;
  1432. if(!arg || !selmon->lt[selmon->sellt]->arrange)
  1433. return;
  1434. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1435. if(f < 0.1 || f > 0.9)
  1436. return;
  1437. selmon->mfact = f;
  1438. arrange(selmon);
  1439. }
  1440. void
  1441. setup(void) {
  1442. XSetWindowAttributes wa;
  1443. /* clean up any zombies immediately */
  1444. sigchld(0);
  1445. /* init screen */
  1446. screen = DefaultScreen(dpy);
  1447. root = RootWindow(dpy, screen);
  1448. initfont(font);
  1449. sw = DisplayWidth(dpy, screen);
  1450. sh = DisplayHeight(dpy, screen);
  1451. bh = dc.h = dc.font.height + 2;
  1452. updategeom();
  1453. /* init atoms */
  1454. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1455. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1456. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1457. wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
  1458. netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
  1459. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1460. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1461. netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
  1462. netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
  1463. netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
  1464. netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
  1465. netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
  1466. /* init cursors */
  1467. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1468. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1469. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1470. /* init appearance */
  1471. dc.norm[ColBorder] = getcolor(normbordercolor);
  1472. dc.norm[ColBG] = getcolor(normbgcolor);
  1473. dc.norm[ColFG] = getcolor(normfgcolor);
  1474. dc.sel[ColBorder] = getcolor(selbordercolor);
  1475. dc.sel[ColBG] = getcolor(selbgcolor);
  1476. dc.sel[ColFG] = getcolor(selfgcolor);
  1477. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1478. dc.gc = XCreateGC(dpy, root, 0, NULL);
  1479. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1480. /* init bars */
  1481. updatebars();
  1482. updatestatus();
  1483. /* EWMH support per view */
  1484. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1485. PropModeReplace, (unsigned char *) netatom, NetLast);
  1486. XDeleteProperty(dpy, root, netatom[NetClientList]);
  1487. /* select for events */
  1488. wa.cursor = cursor[CurNormal];
  1489. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
  1490. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
  1491. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1492. XSelectInput(dpy, root, wa.event_mask);
  1493. grabkeys();
  1494. }
  1495. void
  1496. showhide(Client *c) {
  1497. if(!c)
  1498. return;
  1499. if(ISVISIBLE(c)) { /* show clients top down */
  1500. XMoveWindow(dpy, c->win, c->x, c->y);
  1501. if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
  1502. resize(c, c->x, c->y, c->w, c->h, False);
  1503. showhide(c->snext);
  1504. }
  1505. else { /* hide clients bottom up */
  1506. showhide(c->snext);
  1507. XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
  1508. }
  1509. }
  1510. void
  1511. sigchld(int unused) {
  1512. if(signal(SIGCHLD, sigchld) == SIG_ERR)
  1513. die("Can't install SIGCHLD handler");
  1514. while(0 < waitpid(-1, NULL, WNOHANG));
  1515. }
  1516. void
  1517. spawn(const Arg *arg) {
  1518. if(fork() == 0) {
  1519. if(dpy)
  1520. close(ConnectionNumber(dpy));
  1521. setsid();
  1522. execvp(((char **)arg->v)[0], (char **)arg->v);
  1523. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1524. perror(" failed");
  1525. exit(EXIT_SUCCESS);
  1526. }
  1527. }
  1528. void
  1529. tag(const Arg *arg) {
  1530. if(selmon->sel && arg->ui & TAGMASK) {
  1531. selmon->sel->tags = arg->ui & TAGMASK;
  1532. focus(NULL);
  1533. arrange(selmon);
  1534. }
  1535. }
  1536. void
  1537. tagmon(const Arg *arg) {
  1538. if(!selmon->sel || !mons->next)
  1539. return;
  1540. sendmon(selmon->sel, dirtomon(arg->i));
  1541. }
  1542. int
  1543. textnw(const char *text, unsigned int len) {
  1544. XGlyphInfo ext;
  1545. XftTextExtentsUtf8(dpy, dc.font.xfont, (XftChar8 *) text, len, &ext);
  1546. return ext.xOff;
  1547. }
  1548. void
  1549. tile(Monitor *m) {
  1550. unsigned int i, n, h, mw, my, ty;
  1551. Client *c;
  1552. for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  1553. if(n == 0)
  1554. return;
  1555. if(n > m->nmaster)
  1556. mw = m->nmaster ? m->ww * m->mfact : 0;
  1557. else
  1558. mw = m->ww;
  1559. for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  1560. if(i < m->nmaster) {
  1561. h = (m->wh - my) / (MIN(n, m->nmaster) - i);
  1562. resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False);
  1563. my += HEIGHT(c);
  1564. }
  1565. else {
  1566. h = (m->wh - ty) / (n - i);
  1567. resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False);
  1568. ty += HEIGHT(c);
  1569. }
  1570. }
  1571. void
  1572. togglebar(const Arg *arg) {
  1573. selmon->showbar = !selmon->showbar;
  1574. updatebarpos(selmon);
  1575. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1576. arrange(selmon);
  1577. }
  1578. void
  1579. togglefloating(const Arg *arg) {
  1580. if(!selmon->sel)
  1581. return;
  1582. if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
  1583. return;
  1584. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1585. if(selmon->sel->isfloating)
  1586. resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  1587. selmon->sel->w, selmon->sel->h, False);
  1588. arrange(selmon);
  1589. }
  1590. void
  1591. toggletag(const Arg *arg) {
  1592. unsigned int newtags;
  1593. if(!selmon->sel)
  1594. return;
  1595. newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1596. if(newtags) {
  1597. selmon->sel->tags = newtags;
  1598. focus(NULL);
  1599. arrange(selmon);
  1600. }
  1601. }
  1602. void
  1603. toggleview(const Arg *arg) {
  1604. unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1605. if(newtagset) {
  1606. selmon->tagset[selmon->seltags] = newtagset;
  1607. focus(NULL);
  1608. arrange(selmon);
  1609. }
  1610. }
  1611. void
  1612. unfocus(Client *c, Bool setfocus) {
  1613. if(!c)
  1614. return;
  1615. grabbuttons(c, False);
  1616. XSetWindowBorder(dpy, c->win, dc.norm[ColBorder].pixel);
  1617. if(setfocus) {
  1618. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1619. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  1620. }
  1621. }
  1622. void
  1623. unmanage(Client *c, Bool destroyed) {
  1624. Monitor *m = c->mon;
  1625. XWindowChanges wc;
  1626. /* The server grab construct avoids race conditions. */
  1627. detach(c);
  1628. detachstack(c);
  1629. if(!destroyed) {
  1630. wc.border_width = c->oldbw;
  1631. XGrabServer(dpy);
  1632. XSetErrorHandler(xerrordummy);
  1633. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1634. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1635. setclientstate(c, WithdrawnState);
  1636. XSync(dpy, False);
  1637. XSetErrorHandler(xerror);
  1638. XUngrabServer(dpy);
  1639. }
  1640. free(c);
  1641. focus(NULL);
  1642. updateclientlist();
  1643. arrange(m);
  1644. }
  1645. void
  1646. unmapnotify(XEvent *e) {
  1647. Client *c;
  1648. XUnmapEvent *ev = &e->xunmap;
  1649. if((c = wintoclient(ev->window))) {
  1650. if(ev->send_event)
  1651. setclientstate(c, WithdrawnState);
  1652. else
  1653. unmanage(c, False);
  1654. }
  1655. }
  1656. void
  1657. updatebars(void) {
  1658. Monitor *m;
  1659. XSetWindowAttributes wa = {
  1660. .override_redirect = True,
  1661. .background_pixmap = ParentRelative,
  1662. .event_mask = ButtonPressMask|ExposureMask
  1663. };
  1664. for(m = mons; m; m = m->next) {
  1665. if (m->barwin)
  1666. continue;
  1667. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  1668. CopyFromParent, DefaultVisual(dpy, screen),
  1669. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1670. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1671. XMapRaised(dpy, m->barwin);
  1672. }
  1673. }
  1674. void
  1675. updatebarpos(Monitor *m) {
  1676. m->wy = m->my;
  1677. m->wh = m->mh;
  1678. if(m->showbar) {
  1679. m->wh -= bh;
  1680. m->by = m->topbar ? m->wy : m->wy + m->wh;
  1681. m->wy = m->topbar ? m->wy + bh : m->wy;
  1682. }
  1683. else
  1684. m->by = -bh;
  1685. }
  1686. void
  1687. updateclientlist() {
  1688. Client *c;
  1689. Monitor *m;
  1690. XDeleteProperty(dpy, root, netatom[NetClientList]);
  1691. for(m = mons; m; m = m->next)
  1692. for(c = m->clients; c; c = c->next)
  1693. XChangeProperty(dpy, root, netatom[NetClientList],
  1694. XA_WINDOW, 32, PropModeAppend,
  1695. (unsigned char *) &(c->win), 1);
  1696. }
  1697. Bool
  1698. updategeom(void) {
  1699. Bool dirty = False;
  1700. #ifdef XINERAMA
  1701. if(XineramaIsActive(dpy)) {
  1702. int i, j, n, nn;
  1703. Client *c;
  1704. Monitor *m;
  1705. XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
  1706. XineramaScreenInfo *unique = NULL;
  1707. for(n = 0, m = mons; m; m = m->next, n++);
  1708. /* only consider unique geometries as separate screens */
  1709. if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
  1710. die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
  1711. for(i = 0, j = 0; i < nn; i++)
  1712. if(isuniquegeom(unique, j, &info[i]))
  1713. memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
  1714. XFree(info);
  1715. nn = j;
  1716. if(n <= nn) {
  1717. for(i = 0; i < (nn - n); i++) { /* new monitors available */
  1718. for(m = mons; m && m->next; m = m->next);
  1719. if(m)
  1720. m->next = createmon();
  1721. else
  1722. mons = createmon();
  1723. }
  1724. for(i = 0, m = mons; i < nn && m; m = m->next, i++)
  1725. if(i >= n
  1726. || (unique[i].x_org != m->mx || unique[i].y_org != m->my
  1727. || unique[i].width != m->mw || unique[i].height != m->mh))
  1728. {
  1729. dirty = True;
  1730. m->num = i;
  1731. m->mx = m->wx = unique[i].x_org;
  1732. m->my = m->wy = unique[i].y_org;
  1733. m->mw = m->ww = unique[i].width;
  1734. m->mh = m->wh = unique[i].height;
  1735. updatebarpos(m);
  1736. }
  1737. }
  1738. else { /* less monitors available nn < n */
  1739. for(i = nn; i < n; i++) {
  1740. for(m = mons; m && m->next; m = m->next);
  1741. while(m->clients) {
  1742. dirty = True;
  1743. c = m->clients;
  1744. m->clients = c->next;
  1745. detachstack(c);
  1746. c->mon = mons;
  1747. attach(c);
  1748. attachstack(c);
  1749. }
  1750. if(m == selmon)
  1751. selmon = mons;
  1752. cleanupmon(m);
  1753. }
  1754. }
  1755. free(unique);
  1756. }
  1757. else
  1758. #endif /* XINERAMA */
  1759. /* default monitor setup */
  1760. {
  1761. if(!mons)
  1762. mons = createmon();
  1763. if(mons->mw != sw || mons->mh != sh) {
  1764. dirty = True;
  1765. mons->mw = mons->ww = sw;
  1766. mons->mh = mons->wh = sh;
  1767. updatebarpos(mons);
  1768. }
  1769. }
  1770. if(dirty) {
  1771. selmon = mons;
  1772. selmon = wintomon(root);
  1773. }
  1774. return dirty;
  1775. }
  1776. void
  1777. updatenumlockmask(void) {
  1778. unsigned int i, j;
  1779. XModifierKeymap *modmap;
  1780. numlockmask = 0;
  1781. modmap = XGetModifierMapping(dpy);
  1782. for(i = 0; i < 8; i++)
  1783. for(j = 0; j < modmap->max_keypermod; j++)
  1784. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1785. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1786. numlockmask = (1 << i);
  1787. XFreeModifiermap(modmap);
  1788. }
  1789. void
  1790. updatesizehints(Client *c) {
  1791. long msize;
  1792. XSizeHints size;
  1793. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1794. /* size is uninitialized, ensure that size.flags aren't used */
  1795. size.flags = PSize;
  1796. if(size.flags & PBaseSize) {
  1797. c->basew = size.base_width;
  1798. c->baseh = size.base_height;
  1799. }
  1800. else if(size.flags & PMinSize) {
  1801. c->basew = size.min_width;
  1802. c->baseh = size.min_height;
  1803. }
  1804. else
  1805. c->basew = c->baseh = 0;
  1806. if(size.flags & PResizeInc) {
  1807. c->incw = size.width_inc;
  1808. c->inch = size.height_inc;
  1809. }
  1810. else
  1811. c->incw = c->inch = 0;
  1812. if(size.flags & PMaxSize) {
  1813. c->maxw = size.max_width;
  1814. c->maxh = size.max_height;
  1815. }
  1816. else
  1817. c->maxw = c->maxh = 0;
  1818. if(size.flags & PMinSize) {
  1819. c->minw = size.min_width;
  1820. c->minh = size.min_height;
  1821. }
  1822. else if(size.flags & PBaseSize) {
  1823. c->minw = size.base_width;
  1824. c->minh = size.base_height;
  1825. }
  1826. else
  1827. c->minw = c->minh = 0;
  1828. if(size.flags & PAspect) {
  1829. c->mina = (float)size.min_aspect.y / size.min_aspect.x;
  1830. c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
  1831. }
  1832. else
  1833. c->maxa = c->mina = 0.0;
  1834. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1835. && c->maxw == c->minw && c->maxh == c->minh);
  1836. }
  1837. void
  1838. updatetitle(Client *c) {
  1839. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1840. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1841. if(c->name[0] == '\0') /* hack to mark broken clients */
  1842. strcpy(c->name, broken);
  1843. }
  1844. void
  1845. updatestatus(void) {
  1846. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1847. strcpy(stext, "dwm-"VERSION);
  1848. drawbar(selmon);
  1849. }
  1850. void
  1851. updatewindowtype(Client *c) {
  1852. Atom state = getatomprop(c, netatom[NetWMState]);
  1853. Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
  1854. if(state == netatom[NetWMFullscreen])
  1855. setfullscreen(c, True);
  1856. if(wtype == netatom[NetWMWindowTypeDialog])
  1857. c->isfloating = True;
  1858. }
  1859. void
  1860. updatewmhints(Client *c) {
  1861. XWMHints *wmh;
  1862. if((wmh = XGetWMHints(dpy, c->win))) {
  1863. if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  1864. wmh->flags &= ~XUrgencyHint;
  1865. XSetWMHints(dpy, c->win, wmh);
  1866. }
  1867. else
  1868. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1869. if(wmh->flags & InputHint)
  1870. c->neverfocus = !wmh->input;
  1871. else
  1872. c->neverfocus = False;
  1873. XFree(wmh);
  1874. }
  1875. }
  1876. void
  1877. view(const Arg *arg) {
  1878. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1879. return;
  1880. selmon->seltags ^= 1; /* toggle sel tagset */
  1881. if(arg->ui & TAGMASK)
  1882. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1883. focus(NULL);
  1884. arrange(selmon);
  1885. }
  1886. Client *
  1887. wintoclient(Window w) {
  1888. Client *c;
  1889. Monitor *m;
  1890. for(m = mons; m; m = m->next)
  1891. for(c = m->clients; c; c = c->next)
  1892. if(c->win == w)
  1893. return c;
  1894. return NULL;
  1895. }
  1896. Monitor *
  1897. wintomon(Window w) {
  1898. int x, y;
  1899. Client *c;
  1900. Monitor *m;
  1901. if(w == root && getrootptr(&x, &y))
  1902. return recttomon(x, y, 1, 1);
  1903. for(m = mons; m; m = m->next)
  1904. if(w == m->barwin)
  1905. return m;
  1906. if((c = wintoclient(w)))
  1907. return c->mon;
  1908. return selmon;
  1909. }
  1910. /* There's no way to check accesses to destroyed windows, thus those cases are
  1911. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1912. * default error handler, which may call exit. */
  1913. int
  1914. xerror(Display *dpy, XErrorEvent *ee) {
  1915. if(ee->error_code == BadWindow
  1916. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1917. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1918. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1919. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1920. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1921. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1922. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1923. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1924. return 0;
  1925. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1926. ee->request_code, ee->error_code);
  1927. return xerrorxlib(dpy, ee); /* may call exit */
  1928. }
  1929. int
  1930. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1931. return 0;
  1932. }
  1933. /* Startup Error handler to check if another window manager
  1934. * is already running. */
  1935. int
  1936. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1937. die("dwm: another window manager is already running\n");
  1938. return -1;
  1939. }
  1940. void
  1941. zoom(const Arg *arg) {
  1942. Client *c = selmon->sel;
  1943. if(!selmon->lt[selmon->sellt]->arrange
  1944. || (selmon->sel && selmon->sel->isfloating))
  1945. return;
  1946. if(c == nexttiled(selmon->clients))
  1947. if(!c || !(c = nexttiled(c->next)))
  1948. return;
  1949. pop(c);
  1950. }
  1951. int
  1952. main(int argc, char *argv[]) {
  1953. if(argc == 2 && !strcmp("-v", argv[1]))
  1954. die("dwm-"VERSION", © 2006-2012 dwm engineers, see LICENSE for details\n");
  1955. else if(argc != 1)
  1956. die("usage: dwm [-v]\n");
  1957. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1958. fputs("warning: no locale support\n", stderr);
  1959. if(!(dpy = XOpenDisplay(NULL)))
  1960. die("dwm: cannot open display\n");
  1961. checkotherwm();
  1962. setup();
  1963. scan();
  1964. run();
  1965. cleanup();
  1966. XCloseDisplay(dpy);
  1967. return EXIT_SUCCESS;
  1968. }