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.

2185 lines
54 KiB

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