Configuration file for DWM on MacBook Air
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.

2155 lines
54 KiB

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