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.

2165 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
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
16 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. #ifdef XINERAMA
  40. #include <X11/extensions/Xinerama.h>
  41. #endif /* XINERAMA */
  42. /* macros */
  43. #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
  44. #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
  45. #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
  46. * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
  47. #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
  48. #define LENGTH(X) (sizeof X / sizeof X[0])
  49. #define MAX(A, B) ((A) > (B) ? (A) : (B))
  50. #define MIN(A, B) ((A) < (B) ? (A) : (B))
  51. #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
  52. #define WIDTH(X) ((X)->w + 2 * (X)->bw)
  53. #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
  54. #define TAGMASK ((1 << LENGTH(tags)) - 1)
  55. #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
  56. /* enums */
  57. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  58. enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
  59. enum { NetSupported, NetWMName, NetWMState,
  60. NetWMFullscreen, NetActiveWindow, NetWMWindowType,
  61. NetWMWindowTypeDialog, NetLast }; /* EWMH atoms */
  62. enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
  63. enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
  64. ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
  65. typedef union {
  66. int i;
  67. unsigned int ui;
  68. float f;
  69. const void *v;
  70. } Arg;
  71. typedef struct {
  72. unsigned int click;
  73. unsigned int mask;
  74. unsigned int button;
  75. void (*func)(const Arg *arg);
  76. const Arg arg;
  77. } Button;
  78. typedef struct Monitor Monitor;
  79. typedef struct Client Client;
  80. struct Client {
  81. char name[256];
  82. float mina, maxa;
  83. int x, y, w, h;
  84. int oldx, oldy, oldw, oldh;
  85. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  86. int bw, oldbw;
  87. unsigned int tags;
  88. Bool isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
  89. Client *next;
  90. Client *snext;
  91. Monitor *mon;
  92. Window win;
  93. };
  94. typedef struct {
  95. int x, y, w, h;
  96. unsigned long norm[ColLast];
  97. unsigned long sel[ColLast];
  98. Drawable drawable;
  99. GC gc;
  100. struct {
  101. int ascent;
  102. int descent;
  103. int height;
  104. XFontSet set;
  105. XFontStruct *xfont;
  106. } font;
  107. } DC; /* draw context */
  108. typedef struct {
  109. unsigned int mod;
  110. KeySym keysym;
  111. void (*func)(const Arg *);
  112. const Arg arg;
  113. } Key;
  114. typedef struct {
  115. const char *symbol;
  116. void (*arrange)(Monitor *);
  117. } Layout;
  118. struct Monitor {
  119. char ltsymbol[16];
  120. float mfact;
  121. int nmaster;
  122. int num;
  123. int by; /* bar geometry */
  124. int mx, my, mw, mh; /* screen size */
  125. int wx, wy, ww, wh; /* window area */
  126. unsigned int seltags;
  127. unsigned int sellt;
  128. unsigned int tagset[2];
  129. Bool showbar;
  130. Bool topbar;
  131. Client *clients;
  132. Client *sel;
  133. Client *stack;
  134. Monitor *next;
  135. Window barwin;
  136. const Layout *lt[2];
  137. };
  138. typedef struct {
  139. const char *class;
  140. const char *instance;
  141. const char *title;
  142. unsigned int tags;
  143. Bool isfloating;
  144. int monitor;
  145. } Rule;
  146. /* function declarations */
  147. static void applyrules(Client *c);
  148. static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
  149. static void arrange(Monitor *m);
  150. static void arrangemon(Monitor *m);
  151. static void attach(Client *c);
  152. static void attachstack(Client *c);
  153. static void buttonpress(XEvent *e);
  154. static void checkotherwm(void);
  155. static void cleanup(void);
  156. static void cleanupmon(Monitor *mon);
  157. static void clearurgent(Client *c);
  158. static void clientmessage(XEvent *e);
  159. static void configure(Client *c);
  160. static void configurenotify(XEvent *e);
  161. static void configurerequest(XEvent *e);
  162. static Monitor *createmon(void);
  163. static void destroynotify(XEvent *e);
  164. static void detach(Client *c);
  165. static void detachstack(Client *c);
  166. static void die(const char *errstr, ...);
  167. static Monitor *dirtomon(int dir);
  168. static void drawbar(Monitor *m);
  169. static void drawbars(void);
  170. static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
  171. static void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
  172. static void enternotify(XEvent *e);
  173. static void expose(XEvent *e);
  174. static void focus(Client *c);
  175. static void focusin(XEvent *e);
  176. static void focusmon(const Arg *arg);
  177. static void focusstack(const Arg *arg);
  178. static unsigned long getcolor(const char *colstr);
  179. static Bool getrootptr(int *x, int *y);
  180. static long getstate(Window w);
  181. static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  182. static void grabbuttons(Client *c, Bool focused);
  183. static void grabkeys(void);
  184. static void incnmaster(const Arg *arg);
  185. static void initfont(const char *fontstr);
  186. static void keypress(XEvent *e);
  187. static void killclient(const Arg *arg);
  188. static void manage(Window w, XWindowAttributes *wa);
  189. static void mappingnotify(XEvent *e);
  190. static void maprequest(XEvent *e);
  191. static void monocle(Monitor *m);
  192. static void motionnotify(XEvent *e);
  193. static void movemouse(const Arg *arg);
  194. static Client *nexttiled(Client *c);
  195. static void pop(Client *);
  196. static void propertynotify(XEvent *e);
  197. static void quit(const Arg *arg);
  198. static Monitor *recttomon(int x, int y, int w, int h);
  199. static void resize(Client *c, int x, int y, int w, int h, Bool interact);
  200. static void resizeclient(Client *c, int x, int y, int w, int h);
  201. static void resizemouse(const Arg *arg);
  202. static void restack(Monitor *m);
  203. static void run(void);
  204. static void scan(void);
  205. static Bool sendevent(Client *c, Atom proto);
  206. static void sendmon(Client *c, Monitor *m);
  207. static void setclientstate(Client *c, long state);
  208. static void setfocus(Client *c);
  209. static void setfullscreen(Client *c, Bool fullscreen);
  210. static void setlayout(const Arg *arg);
  211. static void setmfact(const Arg *arg);
  212. static void setup(void);
  213. static void showhide(Client *c);
  214. static void sigchld(int unused);
  215. static void spawn(const Arg *arg);
  216. static void tag(const Arg *arg);
  217. static void tagmon(const Arg *arg);
  218. static int textnw(const char *text, unsigned int len);
  219. static void tile(Monitor *);
  220. static void togglebar(const Arg *arg);
  221. static void togglefloating(const Arg *arg);
  222. static void toggletag(const Arg *arg);
  223. static void toggleview(const Arg *arg);
  224. static void unfocus(Client *c, Bool setfocus);
  225. static void unmanage(Client *c, Bool destroyed);
  226. static void unmapnotify(XEvent *e);
  227. static Bool updategeom(void);
  228. static void updatebarpos(Monitor *m);
  229. static void updatebars(void);
  230. static void updatenumlockmask(void);
  231. static void updatesizehints(Client *c);
  232. static void updatestatus(void);
  233. static void updatewindowtype(Client *c);
  234. static void updatetitle(Client *c);
  235. static void updatewmhints(Client *c);
  236. static void view(const Arg *arg);
  237. static Client *wintoclient(Window w);
  238. static Monitor *wintomon(Window w);
  239. static int xerror(Display *dpy, XErrorEvent *ee);
  240. static int xerrordummy(Display *dpy, XErrorEvent *ee);
  241. static int xerrorstart(Display *dpy, XErrorEvent *ee);
  242. static void zoom(const Arg *arg);
  243. /* variables */
  244. static const char broken[] = "broken";
  245. static char stext[256];
  246. static int screen;
  247. static int sw, sh; /* X display screen geometry width, height */
  248. static int bh, blw = 0; /* bar geometry */
  249. static int (*xerrorxlib)(Display *, XErrorEvent *);
  250. static unsigned int numlockmask = 0;
  251. static void (*handler[LASTEvent]) (XEvent *) = {
  252. [ButtonPress] = buttonpress,
  253. [ClientMessage] = clientmessage,
  254. [ConfigureRequest] = configurerequest,
  255. [ConfigureNotify] = configurenotify,
  256. [DestroyNotify] = destroynotify,
  257. [EnterNotify] = enternotify,
  258. [Expose] = expose,
  259. [FocusIn] = focusin,
  260. [KeyPress] = keypress,
  261. [MappingNotify] = mappingnotify,
  262. [MapRequest] = maprequest,
  263. [MotionNotify] = motionnotify,
  264. [PropertyNotify] = propertynotify,
  265. [UnmapNotify] = unmapnotify
  266. };
  267. static Atom wmatom[WMLast], netatom[NetLast];
  268. static Bool running = True;
  269. static Cursor cursor[CurLast];
  270. static Display *dpy;
  271. static DC dc;
  272. static Monitor *mons = NULL, *selmon = NULL;
  273. static Window root;
  274. /* configuration, allows nested code to access above variables */
  275. #include "config.h"
  276. /* compile-time check if all tags fit into an unsigned int bit array. */
  277. struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
  278. /* function implementations */
  279. void
  280. applyrules(Client *c) {
  281. const char *class, *instance;
  282. unsigned int i;
  283. const Rule *r;
  284. Monitor *m;
  285. XClassHint ch = { NULL, NULL };
  286. /* rule matching */
  287. c->isfloating = c->tags = 0;
  288. XGetClassHint(dpy, c->win, &ch);
  289. class = ch.res_class ? ch.res_class : broken;
  290. instance = ch.res_name ? ch.res_name : broken;
  291. for(i = 0; i < LENGTH(rules); i++) {
  292. r = &rules[i];
  293. if((!r->title || strstr(c->name, r->title))
  294. && (!r->class || strstr(class, r->class))
  295. && (!r->instance || strstr(instance, r->instance)))
  296. {
  297. c->isfloating = r->isfloating;
  298. c->tags |= r->tags;
  299. for(m = mons; m && m->num != r->monitor; m = m->next);
  300. if(m)
  301. c->mon = m;
  302. }
  303. }
  304. if(ch.res_class)
  305. XFree(ch.res_class);
  306. if(ch.res_name)
  307. XFree(ch.res_name);
  308. c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
  309. }
  310. Bool
  311. applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
  312. Bool baseismin;
  313. Monitor *m = c->mon;
  314. /* set minimum possible */
  315. *w = MAX(1, *w);
  316. *h = MAX(1, *h);
  317. if(interact) {
  318. if(*x > sw)
  319. *x = sw - WIDTH(c);
  320. if(*y > sh)
  321. *y = sh - HEIGHT(c);
  322. if(*x + *w + 2 * c->bw < 0)
  323. *x = 0;
  324. if(*y + *h + 2 * c->bw < 0)
  325. *y = 0;
  326. }
  327. else {
  328. if(*x >= m->wx + m->ww)
  329. *x = m->wx + m->ww - WIDTH(c);
  330. if(*y >= m->wy + m->wh)
  331. *y = m->wy + m->wh - HEIGHT(c);
  332. if(*x + *w + 2 * c->bw <= m->wx)
  333. *x = m->wx;
  334. if(*y + *h + 2 * c->bw <= m->wy)
  335. *y = m->wy;
  336. }
  337. if(*h < bh)
  338. *h = bh;
  339. if(*w < bh)
  340. *w = bh;
  341. if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
  342. /* see last two sentences in ICCCM 4.1.2.3 */
  343. baseismin = c->basew == c->minw && c->baseh == c->minh;
  344. if(!baseismin) { /* temporarily remove base dimensions */
  345. *w -= c->basew;
  346. *h -= c->baseh;
  347. }
  348. /* adjust for aspect limits */
  349. if(c->mina > 0 && c->maxa > 0) {
  350. if(c->maxa < (float)*w / *h)
  351. *w = *h * c->maxa + 0.5;
  352. else if(c->mina < (float)*h / *w)
  353. *h = *w * c->mina + 0.5;
  354. }
  355. if(baseismin) { /* increment calculation requires this */
  356. *w -= c->basew;
  357. *h -= c->baseh;
  358. }
  359. /* adjust for increment value */
  360. if(c->incw)
  361. *w -= *w % c->incw;
  362. if(c->inch)
  363. *h -= *h % c->inch;
  364. /* restore base dimensions */
  365. *w = MAX(*w + c->basew, c->minw);
  366. *h = MAX(*h + c->baseh, c->minh);
  367. if(c->maxw)
  368. *w = MIN(*w, c->maxw);
  369. if(c->maxh)
  370. *h = MIN(*h, c->maxh);
  371. }
  372. return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
  373. }
  374. void
  375. arrange(Monitor *m) {
  376. if(m)
  377. showhide(m->stack);
  378. else for(m = mons; m; m = m->next)
  379. showhide(m->stack);
  380. if(m) {
  381. arrangemon(m);
  382. restack(m);
  383. } else for(m = mons; m; m = m->next)
  384. arrangemon(m);
  385. }
  386. void
  387. arrangemon(Monitor *m) {
  388. strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
  389. if(m->lt[m->sellt]->arrange)
  390. m->lt[m->sellt]->arrange(m);
  391. }
  392. void
  393. attach(Client *c) {
  394. c->next = c->mon->clients;
  395. c->mon->clients = c;
  396. }
  397. void
  398. attachstack(Client *c) {
  399. c->snext = c->mon->stack;
  400. c->mon->stack = c;
  401. }
  402. void
  403. buttonpress(XEvent *e) {
  404. unsigned int i, x, click;
  405. Arg arg = {0};
  406. Client *c;
  407. Monitor *m;
  408. XButtonPressedEvent *ev = &e->xbutton;
  409. click = ClkRootWin;
  410. /* focus monitor if necessary */
  411. if((m = wintomon(ev->window)) && m != selmon) {
  412. unfocus(selmon->sel, True);
  413. selmon = m;
  414. focus(NULL);
  415. }
  416. if(ev->window == selmon->barwin) {
  417. i = x = 0;
  418. do
  419. x += TEXTW(tags[i]);
  420. while(ev->x >= x && ++i < LENGTH(tags));
  421. if(i < LENGTH(tags)) {
  422. click = ClkTagBar;
  423. arg.ui = 1 << i;
  424. }
  425. else if(ev->x < x + blw)
  426. click = ClkLtSymbol;
  427. else if(ev->x > selmon->ww - TEXTW(stext))
  428. click = ClkStatusText;
  429. else
  430. click = ClkWinTitle;
  431. }
  432. else if((c = wintoclient(ev->window))) {
  433. focus(c);
  434. click = ClkClientWin;
  435. }
  436. for(i = 0; i < LENGTH(buttons); i++)
  437. if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
  438. && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
  439. buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
  440. }
  441. void
  442. checkotherwm(void) {
  443. xerrorxlib = XSetErrorHandler(xerrorstart);
  444. /* this causes an error if some other window manager is running */
  445. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  446. XSync(dpy, False);
  447. XSetErrorHandler(xerror);
  448. XSync(dpy, False);
  449. }
  450. void
  451. cleanup(void) {
  452. Arg a = {.ui = ~0};
  453. Layout foo = { "", NULL };
  454. Monitor *m;
  455. view(&a);
  456. selmon->lt[selmon->sellt] = &foo;
  457. for(m = mons; m; m = m->next)
  458. while(m->stack)
  459. unmanage(m->stack, False);
  460. if(dc.font.set)
  461. XFreeFontSet(dpy, dc.font.set);
  462. else
  463. XFreeFont(dpy, dc.font.xfont);
  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. if(ev->window == root) {
  540. dirty = (sw != ev->width);
  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. unsigned long *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, unsigned long col[ColLast]) {
  721. int x;
  722. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  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, unsigned long col[ColLast], Bool invert) {
  731. char buf[256];
  732. int i, x, y, h, len, olen;
  733. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  734. XFillRectangle(dpy, dc.drawable, dc.gc, dc.x, dc.y, dc.w, dc.h);
  735. if(!text)
  736. return;
  737. olen = strlen(text);
  738. h = dc.font.ascent + dc.font.descent;
  739. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  740. x = dc.x + (h / 2);
  741. /* shorten text if necessary */
  742. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  743. if(!len)
  744. return;
  745. memcpy(buf, text, len);
  746. if(len < olen)
  747. for(i = len; i && i > len - 3; buf[--i] = '.');
  748. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  749. if(dc.font.set)
  750. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  751. else
  752. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  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]);
  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, True);
  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. unsigned long
  858. getcolor(const char *colstr) {
  859. Colormap cmap = DefaultColormap(dpy, screen);
  860. XColor color;
  861. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  862. die("error, cannot allocate color '%s'\n", colstr);
  863. return color.pixel;
  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. char *def, **missing;
  954. int n;
  955. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  956. if(missing) {
  957. while(n--)
  958. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  959. XFreeStringList(missing);
  960. }
  961. if(dc.font.set) {
  962. XFontStruct **xfonts;
  963. char **font_names;
  964. dc.font.ascent = dc.font.descent = 0;
  965. XExtentsOfFontSet(dc.font.set);
  966. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  967. while(n--) {
  968. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  969. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  970. xfonts++;
  971. }
  972. }
  973. else {
  974. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  975. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  976. die("error, cannot load font: '%s'\n", fontstr);
  977. dc.font.ascent = dc.font.xfont->ascent;
  978. dc.font.descent = dc.font.xfont->descent;
  979. }
  980. dc.font.height = dc.font.ascent + dc.font.descent;
  981. }
  982. #ifdef XINERAMA
  983. static Bool
  984. isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
  985. while(n--)
  986. if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
  987. && unique[n].width == info->width && unique[n].height == info->height)
  988. return False;
  989. return True;
  990. }
  991. #endif /* XINERAMA */
  992. void
  993. keypress(XEvent *e) {
  994. unsigned int i;
  995. KeySym keysym;
  996. XKeyEvent *ev;
  997. ev = &e->xkey;
  998. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  999. for(i = 0; i < LENGTH(keys); i++)
  1000. if(keysym == keys[i].keysym
  1001. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  1002. && keys[i].func)
  1003. keys[i].func(&(keys[i].arg));
  1004. }
  1005. void
  1006. killclient(const Arg *arg) {
  1007. if(!selmon->sel)
  1008. return;
  1009. if(!sendevent(selmon->sel, wmatom[WMDelete])) {
  1010. XGrabServer(dpy);
  1011. XSetErrorHandler(xerrordummy);
  1012. XSetCloseDownMode(dpy, DestroyAll);
  1013. XKillClient(dpy, selmon->sel->win);
  1014. XSync(dpy, False);
  1015. XSetErrorHandler(xerror);
  1016. XUngrabServer(dpy);
  1017. }
  1018. }
  1019. void
  1020. manage(Window w, XWindowAttributes *wa) {
  1021. Client *c, *t = NULL;
  1022. Window trans = None;
  1023. XWindowChanges wc;
  1024. if(!(c = calloc(1, sizeof(Client))))
  1025. die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  1026. c->win = w;
  1027. updatetitle(c);
  1028. if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
  1029. c->mon = t->mon;
  1030. c->tags = t->tags;
  1031. }
  1032. else {
  1033. c->mon = selmon;
  1034. applyrules(c);
  1035. }
  1036. /* geometry */
  1037. c->x = c->oldx = wa->x;
  1038. c->y = c->oldy = wa->y;
  1039. c->w = c->oldw = wa->width;
  1040. c->h = c->oldh = wa->height;
  1041. c->oldbw = wa->border_width;
  1042. if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  1043. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  1044. if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  1045. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  1046. c->x = MAX(c->x, c->mon->mx);
  1047. /* only fix client y-offset, if the client center might cover the bar */
  1048. c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
  1049. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  1050. c->bw = borderpx;
  1051. wc.border_width = c->bw;
  1052. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  1053. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  1054. configure(c); /* propagates border_width, if size doesn't change */
  1055. updatewindowtype(c);
  1056. updatesizehints(c);
  1057. updatewmhints(c);
  1058. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  1059. grabbuttons(c, False);
  1060. if(!c->isfloating)
  1061. c->isfloating = c->oldstate = trans != None || c->isfixed;
  1062. if(c->isfloating)
  1063. XRaiseWindow(dpy, c->win);
  1064. attach(c);
  1065. attachstack(c);
  1066. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  1067. setclientstate(c, NormalState);
  1068. if (c->mon == selmon)
  1069. unfocus(selmon->sel, False);
  1070. c->mon->sel = c;
  1071. arrange(c->mon);
  1072. XMapWindow(dpy, c->win);
  1073. focus(NULL);
  1074. }
  1075. void
  1076. mappingnotify(XEvent *e) {
  1077. XMappingEvent *ev = &e->xmapping;
  1078. XRefreshKeyboardMapping(ev);
  1079. if(ev->request == MappingKeyboard)
  1080. grabkeys();
  1081. }
  1082. void
  1083. maprequest(XEvent *e) {
  1084. static XWindowAttributes wa;
  1085. XMapRequestEvent *ev = &e->xmaprequest;
  1086. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1087. return;
  1088. if(wa.override_redirect)
  1089. return;
  1090. if(!wintoclient(ev->window))
  1091. manage(ev->window, &wa);
  1092. }
  1093. void
  1094. monocle(Monitor *m) {
  1095. unsigned int n = 0;
  1096. Client *c;
  1097. for(c = m->clients; c; c = c->next)
  1098. if(ISVISIBLE(c))
  1099. n++;
  1100. if(n > 0) /* override layout symbol */
  1101. snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
  1102. for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1103. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
  1104. }
  1105. void
  1106. motionnotify(XEvent *e) {
  1107. static Monitor *mon = NULL;
  1108. Monitor *m;
  1109. XMotionEvent *ev = &e->xmotion;
  1110. if(ev->window != root)
  1111. return;
  1112. if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
  1113. unfocus(selmon->sel, True);
  1114. selmon = m;
  1115. focus(NULL);
  1116. }
  1117. mon = m;
  1118. }
  1119. void
  1120. movemouse(const Arg *arg) {
  1121. int x, y, ocx, ocy, nx, ny;
  1122. Client *c;
  1123. Monitor *m;
  1124. XEvent ev;
  1125. if(!(c = selmon->sel))
  1126. return;
  1127. restack(selmon);
  1128. ocx = c->x;
  1129. ocy = c->y;
  1130. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1131. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1132. return;
  1133. if(!getrootptr(&x, &y))
  1134. return;
  1135. do {
  1136. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1137. switch(ev.type) {
  1138. case ConfigureRequest:
  1139. case Expose:
  1140. case MapRequest:
  1141. handler[ev.type](&ev);
  1142. break;
  1143. case MotionNotify:
  1144. nx = ocx + (ev.xmotion.x - x);
  1145. ny = ocy + (ev.xmotion.y - y);
  1146. if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1147. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1148. if(abs(selmon->wx - nx) < snap)
  1149. nx = selmon->wx;
  1150. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1151. nx = selmon->wx + selmon->ww - WIDTH(c);
  1152. if(abs(selmon->wy - ny) < snap)
  1153. ny = selmon->wy;
  1154. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1155. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1156. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1157. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1158. togglefloating(NULL);
  1159. }
  1160. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating) {
  1161. if(c->isfullscreen)
  1162. setfullscreen(c, False);
  1163. resize(c, nx, ny, c->w, c->h, True);
  1164. }
  1165. break;
  1166. }
  1167. } while(ev.type != ButtonRelease);
  1168. XUngrabPointer(dpy, CurrentTime);
  1169. if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1170. sendmon(c, m);
  1171. selmon = m;
  1172. focus(NULL);
  1173. }
  1174. }
  1175. Client *
  1176. nexttiled(Client *c) {
  1177. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1178. return c;
  1179. }
  1180. void
  1181. pop(Client *c) {
  1182. detach(c);
  1183. attach(c);
  1184. focus(c);
  1185. arrange(c->mon);
  1186. }
  1187. void
  1188. propertynotify(XEvent *e) {
  1189. Client *c;
  1190. Window trans;
  1191. XPropertyEvent *ev = &e->xproperty;
  1192. if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1193. updatestatus();
  1194. else if(ev->state == PropertyDelete)
  1195. return; /* ignore */
  1196. else if((c = wintoclient(ev->window))) {
  1197. switch(ev->atom) {
  1198. default: break;
  1199. case XA_WM_TRANSIENT_FOR:
  1200. if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
  1201. (c->isfloating = (wintoclient(trans)) != NULL))
  1202. arrange(c->mon);
  1203. break;
  1204. case XA_WM_NORMAL_HINTS:
  1205. updatesizehints(c);
  1206. break;
  1207. case XA_WM_HINTS:
  1208. updatewmhints(c);
  1209. drawbars();
  1210. break;
  1211. }
  1212. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1213. updatetitle(c);
  1214. if(c == c->mon->sel)
  1215. drawbar(c->mon);
  1216. }
  1217. if(ev->atom == netatom[NetWMWindowType])
  1218. updatewindowtype(c);
  1219. }
  1220. }
  1221. void
  1222. quit(const Arg *arg) {
  1223. running = False;
  1224. }
  1225. Monitor *
  1226. recttomon(int x, int y, int w, int h) {
  1227. Monitor *m, *r = selmon;
  1228. int a, area = 0;
  1229. for(m = mons; m; m = m->next)
  1230. if((a = INTERSECT(x, y, w, h, m)) > area) {
  1231. area = a;
  1232. r = m;
  1233. }
  1234. return r;
  1235. }
  1236. void
  1237. resize(Client *c, int x, int y, int w, int h, Bool interact) {
  1238. if(applysizehints(c, &x, &y, &w, &h, interact))
  1239. resizeclient(c, x, y, w, h);
  1240. }
  1241. void
  1242. resizeclient(Client *c, int x, int y, int w, int h) {
  1243. XWindowChanges wc;
  1244. c->oldx = c->x; c->x = wc.x = x;
  1245. c->oldy = c->y; c->y = wc.y = y;
  1246. c->oldw = c->w; c->w = wc.width = w;
  1247. c->oldh = c->h; c->h = wc.height = h;
  1248. wc.border_width = c->bw;
  1249. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1250. configure(c);
  1251. XSync(dpy, False);
  1252. }
  1253. void
  1254. resizemouse(const Arg *arg) {
  1255. int ocx, ocy;
  1256. int nw, nh;
  1257. Client *c;
  1258. Monitor *m;
  1259. XEvent ev;
  1260. if(!(c = selmon->sel))
  1261. return;
  1262. restack(selmon);
  1263. ocx = c->x;
  1264. ocy = c->y;
  1265. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1266. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1267. return;
  1268. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1269. do {
  1270. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1271. switch(ev.type) {
  1272. case ConfigureRequest:
  1273. case Expose:
  1274. case MapRequest:
  1275. handler[ev.type](&ev);
  1276. break;
  1277. case MotionNotify:
  1278. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1279. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1280. if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
  1281. && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
  1282. {
  1283. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1284. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1285. togglefloating(NULL);
  1286. }
  1287. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating) {
  1288. if(c->isfullscreen)
  1289. setfullscreen(c, False);
  1290. resize(c, c->x, c->y, nw, nh, True);
  1291. }
  1292. break;
  1293. }
  1294. } while(ev.type != ButtonRelease);
  1295. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1296. XUngrabPointer(dpy, CurrentTime);
  1297. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1298. if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1299. sendmon(c, m);
  1300. selmon = m;
  1301. focus(NULL);
  1302. }
  1303. }
  1304. void
  1305. restack(Monitor *m) {
  1306. Client *c;
  1307. XEvent ev;
  1308. XWindowChanges wc;
  1309. drawbar(m);
  1310. if(!m->sel)
  1311. return;
  1312. if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
  1313. XRaiseWindow(dpy, m->sel->win);
  1314. if(m->lt[m->sellt]->arrange) {
  1315. wc.stack_mode = Below;
  1316. wc.sibling = m->barwin;
  1317. for(c = m->stack; c; c = c->snext)
  1318. if(!c->isfloating && ISVISIBLE(c)) {
  1319. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1320. wc.sibling = c->win;
  1321. }
  1322. }
  1323. XSync(dpy, False);
  1324. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1325. }
  1326. void
  1327. run(void) {
  1328. XEvent ev;
  1329. /* main event loop */
  1330. XSync(dpy, False);
  1331. while(running && !XNextEvent(dpy, &ev))
  1332. if(handler[ev.type])
  1333. handler[ev.type](&ev); /* call handler */
  1334. }
  1335. void
  1336. scan(void) {
  1337. unsigned int i, num;
  1338. Window d1, d2, *wins = NULL;
  1339. XWindowAttributes wa;
  1340. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1341. for(i = 0; i < num; i++) {
  1342. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1343. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1344. continue;
  1345. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1346. manage(wins[i], &wa);
  1347. }
  1348. for(i = 0; i < num; i++) { /* now the transients */
  1349. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1350. continue;
  1351. if(XGetTransientForHint(dpy, wins[i], &d1)
  1352. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1353. manage(wins[i], &wa);
  1354. }
  1355. if(wins)
  1356. XFree(wins);
  1357. }
  1358. }
  1359. void
  1360. sendmon(Client *c, Monitor *m) {
  1361. if(c->mon == m)
  1362. return;
  1363. unfocus(c, True);
  1364. detach(c);
  1365. detachstack(c);
  1366. c->mon = m;
  1367. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1368. attach(c);
  1369. attachstack(c);
  1370. focus(NULL);
  1371. arrange(NULL);
  1372. }
  1373. void
  1374. setclientstate(Client *c, long state) {
  1375. long data[] = { state, None };
  1376. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1377. PropModeReplace, (unsigned char *)data, 2);
  1378. }
  1379. Bool
  1380. sendevent(Client *c, Atom proto) {
  1381. int n;
  1382. Atom *protocols;
  1383. Bool exists = False;
  1384. XEvent ev;
  1385. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  1386. while(!exists && n--)
  1387. exists = protocols[n] == proto;
  1388. XFree(protocols);
  1389. }
  1390. if(exists) {
  1391. ev.type = ClientMessage;
  1392. ev.xclient.window = c->win;
  1393. ev.xclient.message_type = wmatom[WMProtocols];
  1394. ev.xclient.format = 32;
  1395. ev.xclient.data.l[0] = proto;
  1396. ev.xclient.data.l[1] = CurrentTime;
  1397. XSendEvent(dpy, c->win, False, NoEventMask, &ev);
  1398. }
  1399. return exists;
  1400. }
  1401. void
  1402. setfocus(Client *c) {
  1403. if(!c->neverfocus) {
  1404. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  1405. XChangeProperty(dpy, root, netatom[NetActiveWindow],
  1406. XA_WINDOW, 32, PropModeReplace,
  1407. (unsigned char *) &(c->win), 1);
  1408. }
  1409. sendevent(c, wmatom[WMTakeFocus]);
  1410. }
  1411. void
  1412. setfullscreen(Client *c, Bool fullscreen) {
  1413. if(fullscreen) {
  1414. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1415. PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
  1416. c->isfullscreen = True;
  1417. c->oldstate = c->isfloating;
  1418. c->oldbw = c->bw;
  1419. c->bw = 0;
  1420. c->isfloating = True;
  1421. resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
  1422. XRaiseWindow(dpy, c->win);
  1423. }
  1424. else {
  1425. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1426. PropModeReplace, (unsigned char*)0, 0);
  1427. c->isfullscreen = False;
  1428. c->isfloating = c->oldstate;
  1429. c->bw = c->oldbw;
  1430. c->x = c->oldx;
  1431. c->y = c->oldy;
  1432. c->w = c->oldw;
  1433. c->h = c->oldh;
  1434. resizeclient(c, c->x, c->y, c->w, c->h);
  1435. arrange(c->mon);
  1436. }
  1437. }
  1438. void
  1439. setlayout(const Arg *arg) {
  1440. if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
  1441. selmon->sellt ^= 1;
  1442. if(arg && arg->v)
  1443. selmon->lt[selmon->sellt] = (Layout *)arg->v;
  1444. strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
  1445. if(selmon->sel)
  1446. arrange(selmon);
  1447. else
  1448. drawbar(selmon);
  1449. }
  1450. /* arg > 1.0 will set mfact absolutly */
  1451. void
  1452. setmfact(const Arg *arg) {
  1453. float f;
  1454. if(!arg || !selmon->lt[selmon->sellt]->arrange)
  1455. return;
  1456. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1457. if(f < 0.1 || f > 0.9)
  1458. return;
  1459. selmon->mfact = f;
  1460. arrange(selmon);
  1461. }
  1462. void
  1463. setup(void) {
  1464. XSetWindowAttributes wa;
  1465. /* clean up any zombies immediately */
  1466. sigchld(0);
  1467. /* init screen */
  1468. screen = DefaultScreen(dpy);
  1469. root = RootWindow(dpy, screen);
  1470. initfont(font);
  1471. sw = DisplayWidth(dpy, screen);
  1472. sh = DisplayHeight(dpy, screen);
  1473. bh = dc.h = dc.font.height + 2;
  1474. updategeom();
  1475. /* init atoms */
  1476. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1477. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1478. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1479. wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
  1480. netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
  1481. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1482. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1483. netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
  1484. netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
  1485. netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
  1486. netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
  1487. /* init cursors */
  1488. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1489. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1490. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1491. /* init appearance */
  1492. dc.norm[ColBorder] = getcolor(normbordercolor);
  1493. dc.norm[ColBG] = getcolor(normbgcolor);
  1494. dc.norm[ColFG] = getcolor(normfgcolor);
  1495. dc.sel[ColBorder] = getcolor(selbordercolor);
  1496. dc.sel[ColBG] = getcolor(selbgcolor);
  1497. dc.sel[ColFG] = getcolor(selfgcolor);
  1498. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1499. dc.gc = XCreateGC(dpy, root, 0, NULL);
  1500. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1501. if(!dc.font.set)
  1502. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1503. /* init bars */
  1504. updatebars();
  1505. updatestatus();
  1506. /* EWMH support per view */
  1507. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1508. PropModeReplace, (unsigned char *) netatom, NetLast);
  1509. /* select for events */
  1510. wa.cursor = cursor[CurNormal];
  1511. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
  1512. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
  1513. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1514. XSelectInput(dpy, root, wa.event_mask);
  1515. grabkeys();
  1516. }
  1517. void
  1518. showhide(Client *c) {
  1519. if(!c)
  1520. return;
  1521. if(ISVISIBLE(c)) { /* show clients top down */
  1522. XMoveWindow(dpy, c->win, c->x, c->y);
  1523. if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
  1524. resize(c, c->x, c->y, c->w, c->h, False);
  1525. showhide(c->snext);
  1526. }
  1527. else { /* hide clients bottom up */
  1528. showhide(c->snext);
  1529. XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
  1530. }
  1531. }
  1532. void
  1533. sigchld(int unused) {
  1534. if(signal(SIGCHLD, sigchld) == SIG_ERR)
  1535. die("Can't install SIGCHLD handler");
  1536. while(0 < waitpid(-1, NULL, WNOHANG));
  1537. }
  1538. void
  1539. spawn(const Arg *arg) {
  1540. if(fork() == 0) {
  1541. if(dpy)
  1542. close(ConnectionNumber(dpy));
  1543. setsid();
  1544. execvp(((char **)arg->v)[0], (char **)arg->v);
  1545. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1546. perror(" failed");
  1547. exit(EXIT_SUCCESS);
  1548. }
  1549. }
  1550. void
  1551. tag(const Arg *arg) {
  1552. if(selmon->sel && arg->ui & TAGMASK) {
  1553. selmon->sel->tags = arg->ui & TAGMASK;
  1554. focus(NULL);
  1555. arrange(selmon);
  1556. }
  1557. }
  1558. void
  1559. tagmon(const Arg *arg) {
  1560. if(!selmon->sel || !mons->next)
  1561. return;
  1562. sendmon(selmon->sel, dirtomon(arg->i));
  1563. }
  1564. int
  1565. textnw(const char *text, unsigned int len) {
  1566. XRectangle r;
  1567. if(dc.font.set) {
  1568. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1569. return r.width;
  1570. }
  1571. return XTextWidth(dc.font.xfont, text, len);
  1572. }
  1573. void
  1574. tile(Monitor *m) {
  1575. unsigned int i, n, h, mw, my, ty;
  1576. Client *c;
  1577. for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  1578. if(n == 0)
  1579. return;
  1580. if(n > m->nmaster)
  1581. mw = m->nmaster ? m->ww * m->mfact : 0;
  1582. else
  1583. mw = m->ww;
  1584. for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  1585. if(i < m->nmaster) {
  1586. h = (m->wh - my) / (MIN(n, m->nmaster) - i);
  1587. resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False);
  1588. my += HEIGHT(c);
  1589. }
  1590. else {
  1591. h = (m->wh - ty) / (n - i);
  1592. resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False);
  1593. ty += HEIGHT(c);
  1594. }
  1595. }
  1596. void
  1597. togglebar(const Arg *arg) {
  1598. selmon->showbar = !selmon->showbar;
  1599. updatebarpos(selmon);
  1600. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1601. arrange(selmon);
  1602. }
  1603. void
  1604. togglefloating(const Arg *arg) {
  1605. if(!selmon->sel)
  1606. return;
  1607. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1608. if(selmon->sel->isfloating)
  1609. resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  1610. selmon->sel->w, selmon->sel->h, False);
  1611. else if(selmon->sel->isfullscreen)
  1612. setfullscreen(selmon->sel, False);
  1613. arrange(selmon);
  1614. }
  1615. void
  1616. toggletag(const Arg *arg) {
  1617. unsigned int newtags;
  1618. if(!selmon->sel)
  1619. return;
  1620. newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1621. if(newtags) {
  1622. selmon->sel->tags = newtags;
  1623. focus(NULL);
  1624. arrange(selmon);
  1625. }
  1626. }
  1627. void
  1628. toggleview(const Arg *arg) {
  1629. unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1630. if(newtagset) {
  1631. selmon->tagset[selmon->seltags] = newtagset;
  1632. focus(NULL);
  1633. arrange(selmon);
  1634. }
  1635. }
  1636. void
  1637. unfocus(Client *c, Bool setfocus) {
  1638. if(!c)
  1639. return;
  1640. grabbuttons(c, False);
  1641. XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
  1642. if(setfocus) {
  1643. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1644. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  1645. }
  1646. }
  1647. void
  1648. unmanage(Client *c, Bool destroyed) {
  1649. Monitor *m = c->mon;
  1650. XWindowChanges wc;
  1651. /* The server grab construct avoids race conditions. */
  1652. detach(c);
  1653. detachstack(c);
  1654. if(!destroyed) {
  1655. wc.border_width = c->oldbw;
  1656. XGrabServer(dpy);
  1657. XSetErrorHandler(xerrordummy);
  1658. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1659. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1660. setclientstate(c, WithdrawnState);
  1661. XSync(dpy, False);
  1662. XSetErrorHandler(xerror);
  1663. XUngrabServer(dpy);
  1664. }
  1665. free(c);
  1666. focus(NULL);
  1667. arrange(m);
  1668. }
  1669. void
  1670. unmapnotify(XEvent *e) {
  1671. Client *c;
  1672. XUnmapEvent *ev = &e->xunmap;
  1673. if((c = wintoclient(ev->window))) {
  1674. if(ev->send_event)
  1675. setclientstate(c, WithdrawnState);
  1676. else
  1677. unmanage(c, False);
  1678. }
  1679. }
  1680. void
  1681. updatebars(void) {
  1682. Monitor *m;
  1683. XSetWindowAttributes wa = {
  1684. .override_redirect = True,
  1685. .background_pixmap = ParentRelative,
  1686. .event_mask = ButtonPressMask|ExposureMask
  1687. };
  1688. for(m = mons; m; m = m->next) {
  1689. if (m->barwin)
  1690. continue;
  1691. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  1692. CopyFromParent, DefaultVisual(dpy, screen),
  1693. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1694. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1695. XMapRaised(dpy, m->barwin);
  1696. }
  1697. }
  1698. void
  1699. updatebarpos(Monitor *m) {
  1700. m->wy = m->my;
  1701. m->wh = m->mh;
  1702. if(m->showbar) {
  1703. m->wh -= bh;
  1704. m->by = m->topbar ? m->wy : m->wy + m->wh;
  1705. m->wy = m->topbar ? m->wy + bh : m->wy;
  1706. }
  1707. else
  1708. m->by = -bh;
  1709. }
  1710. Bool
  1711. updategeom(void) {
  1712. Bool dirty = False;
  1713. #ifdef XINERAMA
  1714. if(XineramaIsActive(dpy)) {
  1715. int i, j, n, nn;
  1716. Client *c;
  1717. Monitor *m;
  1718. XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
  1719. XineramaScreenInfo *unique = NULL;
  1720. for(n = 0, m = mons; m; m = m->next, n++);
  1721. /* only consider unique geometries as separate screens */
  1722. if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
  1723. die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
  1724. for(i = 0, j = 0; i < nn; i++)
  1725. if(isuniquegeom(unique, j, &info[i]))
  1726. memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
  1727. XFree(info);
  1728. nn = j;
  1729. if(n <= nn) {
  1730. for(i = 0; i < (nn - n); i++) { /* new monitors available */
  1731. for(m = mons; m && m->next; m = m->next);
  1732. if(m)
  1733. m->next = createmon();
  1734. else
  1735. mons = createmon();
  1736. }
  1737. for(i = 0, m = mons; i < nn && m; m = m->next, i++)
  1738. if(i >= n
  1739. || (unique[i].x_org != m->mx || unique[i].y_org != m->my
  1740. || unique[i].width != m->mw || unique[i].height != m->mh))
  1741. {
  1742. dirty = True;
  1743. m->num = i;
  1744. m->mx = m->wx = unique[i].x_org;
  1745. m->my = m->wy = unique[i].y_org;
  1746. m->mw = m->ww = unique[i].width;
  1747. m->mh = m->wh = unique[i].height;
  1748. updatebarpos(m);
  1749. }
  1750. }
  1751. else { /* less monitors available nn < n */
  1752. for(i = nn; i < n; i++) {
  1753. for(m = mons; m && m->next; m = m->next);
  1754. while(m->clients) {
  1755. dirty = True;
  1756. c = m->clients;
  1757. m->clients = c->next;
  1758. detachstack(c);
  1759. c->mon = mons;
  1760. attach(c);
  1761. attachstack(c);
  1762. }
  1763. if(m == selmon)
  1764. selmon = mons;
  1765. cleanupmon(m);
  1766. }
  1767. }
  1768. free(unique);
  1769. }
  1770. else
  1771. #endif /* XINERAMA */
  1772. /* default monitor setup */
  1773. {
  1774. if(!mons)
  1775. mons = createmon();
  1776. if(mons->mw != sw || mons->mh != sh) {
  1777. dirty = True;
  1778. mons->mw = mons->ww = sw;
  1779. mons->mh = mons->wh = sh;
  1780. updatebarpos(mons);
  1781. }
  1782. }
  1783. if(dirty) {
  1784. selmon = mons;
  1785. selmon = wintomon(root);
  1786. }
  1787. return dirty;
  1788. }
  1789. void
  1790. updatenumlockmask(void) {
  1791. unsigned int i, j;
  1792. XModifierKeymap *modmap;
  1793. numlockmask = 0;
  1794. modmap = XGetModifierMapping(dpy);
  1795. for(i = 0; i < 8; i++)
  1796. for(j = 0; j < modmap->max_keypermod; j++)
  1797. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1798. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1799. numlockmask = (1 << i);
  1800. XFreeModifiermap(modmap);
  1801. }
  1802. void
  1803. updatesizehints(Client *c) {
  1804. long msize;
  1805. XSizeHints size;
  1806. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1807. /* size is uninitialized, ensure that size.flags aren't used */
  1808. size.flags = PSize;
  1809. if(size.flags & PBaseSize) {
  1810. c->basew = size.base_width;
  1811. c->baseh = size.base_height;
  1812. }
  1813. else if(size.flags & PMinSize) {
  1814. c->basew = size.min_width;
  1815. c->baseh = size.min_height;
  1816. }
  1817. else
  1818. c->basew = c->baseh = 0;
  1819. if(size.flags & PResizeInc) {
  1820. c->incw = size.width_inc;
  1821. c->inch = size.height_inc;
  1822. }
  1823. else
  1824. c->incw = c->inch = 0;
  1825. if(size.flags & PMaxSize) {
  1826. c->maxw = size.max_width;
  1827. c->maxh = size.max_height;
  1828. }
  1829. else
  1830. c->maxw = c->maxh = 0;
  1831. if(size.flags & PMinSize) {
  1832. c->minw = size.min_width;
  1833. c->minh = size.min_height;
  1834. }
  1835. else if(size.flags & PBaseSize) {
  1836. c->minw = size.base_width;
  1837. c->minh = size.base_height;
  1838. }
  1839. else
  1840. c->minw = c->minh = 0;
  1841. if(size.flags & PAspect) {
  1842. c->mina = (float)size.min_aspect.y / size.min_aspect.x;
  1843. c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
  1844. }
  1845. else
  1846. c->maxa = c->mina = 0.0;
  1847. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1848. && c->maxw == c->minw && c->maxh == c->minh);
  1849. }
  1850. void
  1851. updatetitle(Client *c) {
  1852. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1853. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1854. if(c->name[0] == '\0') /* hack to mark broken clients */
  1855. strcpy(c->name, broken);
  1856. }
  1857. void
  1858. updatestatus(void) {
  1859. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1860. strcpy(stext, "dwm-"VERSION);
  1861. drawbar(selmon);
  1862. }
  1863. void
  1864. updatewindowtype(Client *c) {
  1865. Atom state = getatomprop(c, netatom[NetWMState]);
  1866. Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
  1867. if(state == netatom[NetWMFullscreen])
  1868. setfullscreen(c, True);
  1869. if(wtype == netatom[NetWMWindowTypeDialog])
  1870. c->isfloating = True;
  1871. }
  1872. void
  1873. updatewmhints(Client *c) {
  1874. XWMHints *wmh;
  1875. if((wmh = XGetWMHints(dpy, c->win))) {
  1876. if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  1877. wmh->flags &= ~XUrgencyHint;
  1878. XSetWMHints(dpy, c->win, wmh);
  1879. }
  1880. else
  1881. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1882. if(wmh->flags & InputHint)
  1883. c->neverfocus = !wmh->input;
  1884. else
  1885. c->neverfocus = False;
  1886. XFree(wmh);
  1887. }
  1888. }
  1889. void
  1890. view(const Arg *arg) {
  1891. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1892. return;
  1893. selmon->seltags ^= 1; /* toggle sel tagset */
  1894. if(arg->ui & TAGMASK)
  1895. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1896. focus(NULL);
  1897. arrange(selmon);
  1898. }
  1899. Client *
  1900. wintoclient(Window w) {
  1901. Client *c;
  1902. Monitor *m;
  1903. for(m = mons; m; m = m->next)
  1904. for(c = m->clients; c; c = c->next)
  1905. if(c->win == w)
  1906. return c;
  1907. return NULL;
  1908. }
  1909. Monitor *
  1910. wintomon(Window w) {
  1911. int x, y;
  1912. Client *c;
  1913. Monitor *m;
  1914. if(w == root && getrootptr(&x, &y))
  1915. return recttomon(x, y, 1, 1);
  1916. for(m = mons; m; m = m->next)
  1917. if(w == m->barwin)
  1918. return m;
  1919. if((c = wintoclient(w)))
  1920. return c->mon;
  1921. return selmon;
  1922. }
  1923. /* There's no way to check accesses to destroyed windows, thus those cases are
  1924. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1925. * default error handler, which may call exit. */
  1926. int
  1927. xerror(Display *dpy, XErrorEvent *ee) {
  1928. if(ee->error_code == BadWindow
  1929. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1930. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1931. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1932. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1933. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1934. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1935. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1936. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1937. return 0;
  1938. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1939. ee->request_code, ee->error_code);
  1940. return xerrorxlib(dpy, ee); /* may call exit */
  1941. }
  1942. int
  1943. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1944. return 0;
  1945. }
  1946. /* Startup Error handler to check if another window manager
  1947. * is already running. */
  1948. int
  1949. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1950. die("dwm: another window manager is already running\n");
  1951. return -1;
  1952. }
  1953. void
  1954. zoom(const Arg *arg) {
  1955. Client *c = selmon->sel;
  1956. if(!selmon->lt[selmon->sellt]->arrange
  1957. || (selmon->sel && selmon->sel->isfloating))
  1958. return;
  1959. if(c == nexttiled(selmon->clients))
  1960. if(!c || !(c = nexttiled(c->next)))
  1961. return;
  1962. pop(c);
  1963. }
  1964. int
  1965. main(int argc, char *argv[]) {
  1966. if(argc == 2 && !strcmp("-v", argv[1]))
  1967. die("dwm-"VERSION", © 2006-2012 dwm engineers, see LICENSE for details\n");
  1968. else if(argc != 1)
  1969. die("usage: dwm [-v]\n");
  1970. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1971. fputs("warning: no locale support\n", stderr);
  1972. if(!(dpy = XOpenDisplay(NULL)))
  1973. die("dwm: cannot open display\n");
  1974. checkotherwm();
  1975. setup();
  1976. scan();
  1977. run();
  1978. cleanup();
  1979. XCloseDisplay(dpy);
  1980. return EXIT_SUCCESS;
  1981. }