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.

2167 lines
54 KiB

17 years ago
16 years ago
15 years ago
15 years ago
14 years ago
16 years ago
15 years ago
15 years ago
17 years ago
17 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
17 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
16 years ago
15 years ago
15 years ago
13 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
15 years ago
15 years ago
17 years ago
16 years ago
16 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, NetClientList, 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 void updategeom(void);
  228. static void updatebarpos(Monitor *m);
  229. static void updatebars(void);
  230. static void updateclientlist(void);
  231. static void updatenumlockmask(void);
  232. static void updatesizehints(Client *c);
  233. static void updatestatus(void);
  234. static void updatewindowtype(Client *c);
  235. static void updatetitle(Client *c);
  236. static void updatewmhints(Client *c);
  237. static void view(const Arg *arg);
  238. static Client *wintoclient(Window w);
  239. static Monitor *wintomon(Window w);
  240. static int xerror(Display *dpy, XErrorEvent *ee);
  241. static int xerrordummy(Display *dpy, XErrorEvent *ee);
  242. static int xerrorstart(Display *dpy, XErrorEvent *ee);
  243. static void zoom(const Arg *arg);
  244. /* variables */
  245. static const char broken[] = "broken";
  246. static char stext[256];
  247. static int screen;
  248. static int sw, sh; /* X display screen geometry width, height */
  249. static int bh, blw = 0; /* bar geometry */
  250. static int (*xerrorxlib)(Display *, XErrorEvent *);
  251. static unsigned int numlockmask = 0;
  252. static void (*handler[LASTEvent]) (XEvent *) = {
  253. [ButtonPress] = buttonpress,
  254. [ClientMessage] = clientmessage,
  255. [ConfigureRequest] = configurerequest,
  256. [ConfigureNotify] = configurenotify,
  257. [DestroyNotify] = destroynotify,
  258. [EnterNotify] = enternotify,
  259. [Expose] = expose,
  260. [FocusIn] = focusin,
  261. [KeyPress] = keypress,
  262. [MappingNotify] = mappingnotify,
  263. [MapRequest] = maprequest,
  264. [MotionNotify] = motionnotify,
  265. [PropertyNotify] = propertynotify,
  266. [UnmapNotify] = unmapnotify
  267. };
  268. static Atom wmatom[WMLast], netatom[NetLast];
  269. static Bool running = True;
  270. static Cursor cursor[CurLast];
  271. static Display *dpy;
  272. static DC dc;
  273. static Monitor *mons = NULL, *selmon = NULL;
  274. static Window root;
  275. /* configuration, allows nested code to access above variables */
  276. #include "config.h"
  277. /* compile-time check if all tags fit into an unsigned int bit array. */
  278. struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
  279. /* function implementations */
  280. void
  281. applyrules(Client *c) {
  282. const char *class, *instance;
  283. unsigned int i;
  284. const Rule *r;
  285. Monitor *m;
  286. XClassHint ch = { NULL, NULL };
  287. /* rule matching */
  288. c->isfloating = c->tags = 0;
  289. XGetClassHint(dpy, c->win, &ch);
  290. class = ch.res_class ? ch.res_class : broken;
  291. instance = ch.res_name ? ch.res_name : broken;
  292. for(i = 0; i < LENGTH(rules); i++) {
  293. r = &rules[i];
  294. if((!r->title || strstr(c->name, r->title))
  295. && (!r->class || strstr(class, r->class))
  296. && (!r->instance || strstr(instance, r->instance)))
  297. {
  298. c->isfloating = r->isfloating;
  299. c->tags |= r->tags;
  300. for(m = mons; m && m->num != r->monitor; m = m->next);
  301. if(m)
  302. c->mon = m;
  303. }
  304. }
  305. if(ch.res_class)
  306. XFree(ch.res_class);
  307. if(ch.res_name)
  308. XFree(ch.res_name);
  309. c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
  310. }
  311. Bool
  312. applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
  313. Bool baseismin;
  314. Monitor *m = c->mon;
  315. /* set minimum possible */
  316. *w = MAX(1, *w);
  317. *h = MAX(1, *h);
  318. if(interact) {
  319. if(*x > sw)
  320. *x = sw - WIDTH(c);
  321. if(*y > sh)
  322. *y = sh - HEIGHT(c);
  323. if(*x + *w + 2 * c->bw < 0)
  324. *x = 0;
  325. if(*y + *h + 2 * c->bw < 0)
  326. *y = 0;
  327. }
  328. else {
  329. if(*x >= m->wx + m->ww)
  330. *x = m->wx + m->ww - WIDTH(c);
  331. if(*y >= m->wy + m->wh)
  332. *y = m->wy + m->wh - HEIGHT(c);
  333. if(*x + *w + 2 * c->bw <= m->wx)
  334. *x = m->wx;
  335. if(*y + *h + 2 * c->bw <= m->wy)
  336. *y = m->wy;
  337. }
  338. if(*h < bh)
  339. *h = bh;
  340. if(*w < bh)
  341. *w = bh;
  342. if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
  343. /* see last two sentences in ICCCM 4.1.2.3 */
  344. baseismin = c->basew == c->minw && c->baseh == c->minh;
  345. if(!baseismin) { /* temporarily remove base dimensions */
  346. *w -= c->basew;
  347. *h -= c->baseh;
  348. }
  349. /* adjust for aspect limits */
  350. if(c->mina > 0 && c->maxa > 0) {
  351. if(c->maxa < (float)*w / *h)
  352. *w = *h * c->maxa + 0.5;
  353. else if(c->mina < (float)*h / *w)
  354. *h = *w * c->mina + 0.5;
  355. }
  356. if(baseismin) { /* increment calculation requires this */
  357. *w -= c->basew;
  358. *h -= c->baseh;
  359. }
  360. /* adjust for increment value */
  361. if(c->incw)
  362. *w -= *w % c->incw;
  363. if(c->inch)
  364. *h -= *h % c->inch;
  365. /* restore base dimensions */
  366. *w = MAX(*w + c->basew, c->minw);
  367. *h = MAX(*h + c->baseh, c->minh);
  368. if(c->maxw)
  369. *w = MIN(*w, c->maxw);
  370. if(c->maxh)
  371. *h = MIN(*h, c->maxh);
  372. }
  373. return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
  374. }
  375. void
  376. arrange(Monitor *m) {
  377. if(m)
  378. showhide(m->stack);
  379. else for(m = mons; m; m = m->next)
  380. showhide(m->stack);
  381. if(m) {
  382. arrangemon(m);
  383. restack(m);
  384. } else for(m = mons; m; m = m->next)
  385. arrangemon(m);
  386. }
  387. void
  388. arrangemon(Monitor *m) {
  389. strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
  390. if(m->lt[m->sellt]->arrange)
  391. m->lt[m->sellt]->arrange(m);
  392. }
  393. void
  394. attach(Client *c) {
  395. c->next = c->mon->clients;
  396. c->mon->clients = c;
  397. }
  398. void
  399. attachstack(Client *c) {
  400. c->snext = c->mon->stack;
  401. c->mon->stack = c;
  402. }
  403. void
  404. buttonpress(XEvent *e) {
  405. unsigned int i, x, click;
  406. Arg arg = {0};
  407. Client *c;
  408. Monitor *m;
  409. XButtonPressedEvent *ev = &e->xbutton;
  410. click = ClkRootWin;
  411. /* focus monitor if necessary */
  412. if((m = wintomon(ev->window)) && m != selmon) {
  413. unfocus(selmon->sel, True);
  414. selmon = m;
  415. focus(NULL);
  416. }
  417. if(ev->window == selmon->barwin) {
  418. i = x = 0;
  419. do
  420. x += TEXTW(tags[i]);
  421. while(ev->x >= x && ++i < LENGTH(tags));
  422. if(i < LENGTH(tags)) {
  423. click = ClkTagBar;
  424. arg.ui = 1 << i;
  425. }
  426. else if(ev->x < x + blw)
  427. click = ClkLtSymbol;
  428. else if(ev->x > selmon->ww - TEXTW(stext))
  429. click = ClkStatusText;
  430. else
  431. click = ClkWinTitle;
  432. }
  433. else if((c = wintoclient(ev->window))) {
  434. focus(c);
  435. click = ClkClientWin;
  436. }
  437. for(i = 0; i < LENGTH(buttons); i++)
  438. if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
  439. && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
  440. buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
  441. }
  442. void
  443. checkotherwm(void) {
  444. xerrorxlib = XSetErrorHandler(xerrorstart);
  445. /* this causes an error if some other window manager is running */
  446. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  447. XSync(dpy, False);
  448. XSetErrorHandler(xerror);
  449. XSync(dpy, False);
  450. }
  451. void
  452. cleanup(void) {
  453. Arg a = {.ui = ~0};
  454. Layout foo = { "", NULL };
  455. Monitor *m;
  456. view(&a);
  457. selmon->lt[selmon->sellt] = &foo;
  458. for(m = mons; m; m = m->next)
  459. while(m->stack)
  460. unmanage(m->stack, False);
  461. if(dc.font.set)
  462. XFreeFontSet(dpy, dc.font.set);
  463. else
  464. XFreeFont(dpy, dc.font.xfont);
  465. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  466. XFreePixmap(dpy, dc.drawable);
  467. XFreeGC(dpy, dc.gc);
  468. XFreeCursor(dpy, cursor[CurNormal]);
  469. XFreeCursor(dpy, cursor[CurResize]);
  470. XFreeCursor(dpy, cursor[CurMove]);
  471. while(mons)
  472. cleanupmon(mons);
  473. XSync(dpy, False);
  474. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  475. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  476. }
  477. void
  478. cleanupmon(Monitor *mon) {
  479. Monitor *m;
  480. if(mon == mons)
  481. mons = mons->next;
  482. else {
  483. for(m = mons; m && m->next != mon; m = m->next);
  484. m->next = mon->next;
  485. }
  486. XUnmapWindow(dpy, mon->barwin);
  487. XDestroyWindow(dpy, mon->barwin);
  488. free(mon);
  489. }
  490. void
  491. clearurgent(Client *c) {
  492. XWMHints *wmh;
  493. c->isurgent = False;
  494. if(!(wmh = XGetWMHints(dpy, c->win)))
  495. return;
  496. wmh->flags &= ~XUrgencyHint;
  497. XSetWMHints(dpy, c->win, wmh);
  498. XFree(wmh);
  499. }
  500. void
  501. clientmessage(XEvent *e) {
  502. XClientMessageEvent *cme = &e->xclient;
  503. Client *c = wintoclient(cme->window);
  504. if(!c)
  505. return;
  506. if(cme->message_type == netatom[NetWMState]) {
  507. if(cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
  508. setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
  509. || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
  510. }
  511. else if(cme->message_type == netatom[NetActiveWindow]) {
  512. if(!ISVISIBLE(c)) {
  513. c->mon->seltags ^= 1;
  514. c->mon->tagset[c->mon->seltags] = c->tags;
  515. }
  516. pop(c);
  517. }
  518. }
  519. void
  520. configure(Client *c) {
  521. XConfigureEvent ce;
  522. ce.type = ConfigureNotify;
  523. ce.display = dpy;
  524. ce.event = c->win;
  525. ce.window = c->win;
  526. ce.x = c->x;
  527. ce.y = c->y;
  528. ce.width = c->w;
  529. ce.height = c->h;
  530. ce.border_width = c->bw;
  531. ce.above = None;
  532. ce.override_redirect = False;
  533. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  534. }
  535. void
  536. configurenotify(XEvent *e) {
  537. Monitor *m;
  538. XConfigureEvent *ev = &e->xconfigure;
  539. if(ev->window == root) {
  540. sw = ev->width;
  541. sh = ev->height;
  542. if(dc.drawable != 0)
  543. XFreePixmap(dpy, dc.drawable);
  544. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  545. updatebars();
  546. for(m = mons; m; m = m->next)
  547. XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
  548. focus(NULL);
  549. arrange(NULL);
  550. }
  551. }
  552. void
  553. configurerequest(XEvent *e) {
  554. Client *c;
  555. Monitor *m;
  556. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  557. XWindowChanges wc;
  558. if((c = wintoclient(ev->window))) {
  559. if(ev->value_mask & CWBorderWidth)
  560. c->bw = ev->border_width;
  561. else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
  562. m = c->mon;
  563. if(ev->value_mask & CWX) {
  564. c->oldx = c->x;
  565. c->x = m->mx + ev->x;
  566. }
  567. if(ev->value_mask & CWY) {
  568. c->oldy = c->y;
  569. c->y = m->my + ev->y;
  570. }
  571. if(ev->value_mask & CWWidth) {
  572. c->oldw = c->w;
  573. c->w = ev->width;
  574. }
  575. if(ev->value_mask & CWHeight) {
  576. c->oldh = c->h;
  577. c->h = ev->height;
  578. }
  579. if((c->x + c->w) > m->mx + m->mw && c->isfloating)
  580. c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
  581. if((c->y + c->h) > m->my + m->mh && c->isfloating)
  582. c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
  583. if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
  584. configure(c);
  585. if(ISVISIBLE(c))
  586. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  587. }
  588. else
  589. configure(c);
  590. }
  591. else {
  592. wc.x = ev->x;
  593. wc.y = ev->y;
  594. wc.width = ev->width;
  595. wc.height = ev->height;
  596. wc.border_width = ev->border_width;
  597. wc.sibling = ev->above;
  598. wc.stack_mode = ev->detail;
  599. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  600. }
  601. XSync(dpy, False);
  602. }
  603. Monitor *
  604. createmon(void) {
  605. Monitor *m;
  606. if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
  607. die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
  608. m->tagset[0] = m->tagset[1] = 1;
  609. m->mfact = mfact;
  610. m->nmaster = nmaster;
  611. m->showbar = showbar;
  612. m->topbar = topbar;
  613. m->lt[0] = &layouts[0];
  614. m->lt[1] = &layouts[1 % LENGTH(layouts)];
  615. strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
  616. return m;
  617. }
  618. void
  619. destroynotify(XEvent *e) {
  620. Client *c;
  621. XDestroyWindowEvent *ev = &e->xdestroywindow;
  622. if((c = wintoclient(ev->window)))
  623. unmanage(c, True);
  624. }
  625. void
  626. detach(Client *c) {
  627. Client **tc;
  628. for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
  629. *tc = c->next;
  630. }
  631. void
  632. detachstack(Client *c) {
  633. Client **tc, *t;
  634. for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
  635. *tc = c->snext;
  636. if(c == c->mon->sel) {
  637. for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
  638. c->mon->sel = t;
  639. }
  640. }
  641. void
  642. die(const char *errstr, ...) {
  643. va_list ap;
  644. va_start(ap, errstr);
  645. vfprintf(stderr, errstr, ap);
  646. va_end(ap);
  647. exit(EXIT_FAILURE);
  648. }
  649. Monitor *
  650. dirtomon(int dir) {
  651. Monitor *m = NULL;
  652. if(dir > 0) {
  653. if(!(m = selmon->next))
  654. m = mons;
  655. }
  656. else if(selmon == mons)
  657. for(m = mons; m->next; m = m->next);
  658. else
  659. for(m = mons; m->next != selmon; m = m->next);
  660. return m;
  661. }
  662. void
  663. drawbar(Monitor *m) {
  664. int x;
  665. unsigned int i, occ = 0, urg = 0;
  666. unsigned long *col;
  667. Client *c;
  668. for(c = m->clients; c; c = c->next) {
  669. occ |= c->tags;
  670. if(c->isurgent)
  671. urg |= c->tags;
  672. }
  673. dc.x = 0;
  674. for(i = 0; i < LENGTH(tags); i++) {
  675. dc.w = TEXTW(tags[i]);
  676. col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
  677. drawtext(tags[i], col, urg & 1 << i);
  678. drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
  679. occ & 1 << i, urg & 1 << i, col);
  680. dc.x += dc.w;
  681. }
  682. dc.w = blw = TEXTW(m->ltsymbol);
  683. drawtext(m->ltsymbol, dc.norm, False);
  684. dc.x += dc.w;
  685. x = dc.x;
  686. if(m == selmon) { /* status is only drawn on selected monitor */
  687. dc.w = TEXTW(stext);
  688. dc.x = m->ww - dc.w;
  689. if(dc.x < x) {
  690. dc.x = x;
  691. dc.w = m->ww - x;
  692. }
  693. drawtext(stext, dc.norm, False);
  694. }
  695. else
  696. dc.x = m->ww;
  697. if((dc.w = dc.x - x) > bh) {
  698. dc.x = x;
  699. if(m->sel) {
  700. col = m == selmon ? dc.sel : dc.norm;
  701. drawtext(m->sel->name, col, False);
  702. drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
  703. }
  704. else
  705. drawtext(NULL, dc.norm, False);
  706. }
  707. XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
  708. XSync(dpy, False);
  709. }
  710. void
  711. drawbars(void) {
  712. Monitor *m;
  713. for(m = mons; m; m = m->next)
  714. drawbar(m);
  715. }
  716. void
  717. drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  718. int x;
  719. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  720. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  721. if(filled)
  722. XFillRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x+1, x+1);
  723. else if(empty)
  724. XDrawRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x, x);
  725. }
  726. void
  727. drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
  728. char buf[256];
  729. int i, x, y, h, len, olen;
  730. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  731. XFillRectangle(dpy, dc.drawable, dc.gc, dc.x, dc.y, dc.w, dc.h);
  732. if(!text)
  733. return;
  734. olen = strlen(text);
  735. h = dc.font.ascent + dc.font.descent;
  736. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  737. x = dc.x + (h / 2);
  738. /* shorten text if necessary */
  739. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  740. if(!len)
  741. return;
  742. memcpy(buf, text, len);
  743. if(len < olen)
  744. for(i = len; i && i > len - 3; buf[--i] = '.');
  745. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  746. if(dc.font.set)
  747. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  748. else
  749. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  750. }
  751. void
  752. enternotify(XEvent *e) {
  753. Client *c;
  754. Monitor *m;
  755. XCrossingEvent *ev = &e->xcrossing;
  756. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  757. return;
  758. c = wintoclient(ev->window);
  759. m = c ? c->mon : wintomon(ev->window);
  760. if(m != selmon) {
  761. unfocus(selmon->sel, True);
  762. selmon = m;
  763. }
  764. else if(!c || c == selmon->sel)
  765. return;
  766. focus(c);
  767. }
  768. void
  769. expose(XEvent *e) {
  770. Monitor *m;
  771. XExposeEvent *ev = &e->xexpose;
  772. if(ev->count == 0 && (m = wintomon(ev->window)))
  773. drawbar(m);
  774. }
  775. void
  776. focus(Client *c) {
  777. if(!c || !ISVISIBLE(c))
  778. for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
  779. /* was if(selmon->sel) */
  780. if(selmon->sel && selmon->sel != c)
  781. unfocus(selmon->sel, False);
  782. if(c) {
  783. if(c->mon != selmon)
  784. selmon = c->mon;
  785. if(c->isurgent)
  786. clearurgent(c);
  787. detachstack(c);
  788. attachstack(c);
  789. grabbuttons(c, True);
  790. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  791. setfocus(c);
  792. }
  793. else {
  794. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  795. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  796. }
  797. selmon->sel = c;
  798. drawbars();
  799. }
  800. void
  801. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  802. XFocusChangeEvent *ev = &e->xfocus;
  803. if(selmon->sel && ev->window != selmon->sel->win)
  804. setfocus(selmon->sel);
  805. }
  806. void
  807. focusmon(const Arg *arg) {
  808. Monitor *m;
  809. if(!mons->next)
  810. return;
  811. if((m = dirtomon(arg->i)) == selmon)
  812. return;
  813. unfocus(selmon->sel, False); /* s/True/False/ fixes input focus issues
  814. in gedit and anjuta */
  815. selmon = m;
  816. focus(NULL);
  817. }
  818. void
  819. focusstack(const Arg *arg) {
  820. Client *c = NULL, *i;
  821. if(!selmon->sel)
  822. return;
  823. if(arg->i > 0) {
  824. for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
  825. if(!c)
  826. for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
  827. }
  828. else {
  829. for(i = selmon->clients; i != selmon->sel; i = i->next)
  830. if(ISVISIBLE(i))
  831. c = i;
  832. if(!c)
  833. for(; i; i = i->next)
  834. if(ISVISIBLE(i))
  835. c = i;
  836. }
  837. if(c) {
  838. focus(c);
  839. restack(selmon);
  840. }
  841. }
  842. Atom
  843. getatomprop(Client *c, Atom prop) {
  844. int di;
  845. unsigned long dl;
  846. unsigned char *p = NULL;
  847. Atom da, atom = None;
  848. if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
  849. &da, &di, &dl, &dl, &p) == Success && p) {
  850. atom = *(Atom *)p;
  851. XFree(p);
  852. }
  853. return atom;
  854. }
  855. unsigned long
  856. getcolor(const char *colstr) {
  857. Colormap cmap = DefaultColormap(dpy, screen);
  858. XColor color;
  859. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  860. die("error, cannot allocate color '%s'\n", colstr);
  861. return color.pixel;
  862. }
  863. Bool
  864. getrootptr(int *x, int *y) {
  865. int di;
  866. unsigned int dui;
  867. Window dummy;
  868. return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
  869. }
  870. long
  871. getstate(Window w) {
  872. int format;
  873. long result = -1;
  874. unsigned char *p = NULL;
  875. unsigned long n, extra;
  876. Atom real;
  877. if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  878. &real, &format, &n, &extra, (unsigned char **)&p) != Success)
  879. return -1;
  880. if(n != 0)
  881. result = *p;
  882. XFree(p);
  883. return result;
  884. }
  885. Bool
  886. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  887. char **list = NULL;
  888. int n;
  889. XTextProperty name;
  890. if(!text || size == 0)
  891. return False;
  892. text[0] = '\0';
  893. XGetTextProperty(dpy, w, &name, atom);
  894. if(!name.nitems)
  895. return False;
  896. if(name.encoding == XA_STRING)
  897. strncpy(text, (char *)name.value, size - 1);
  898. else {
  899. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
  900. strncpy(text, *list, size - 1);
  901. XFreeStringList(list);
  902. }
  903. }
  904. text[size - 1] = '\0';
  905. XFree(name.value);
  906. return True;
  907. }
  908. void
  909. grabbuttons(Client *c, Bool focused) {
  910. updatenumlockmask();
  911. {
  912. unsigned int i, j;
  913. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  914. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  915. if(focused) {
  916. for(i = 0; i < LENGTH(buttons); i++)
  917. if(buttons[i].click == ClkClientWin)
  918. for(j = 0; j < LENGTH(modifiers); j++)
  919. XGrabButton(dpy, buttons[i].button,
  920. buttons[i].mask | modifiers[j],
  921. c->win, False, BUTTONMASK,
  922. GrabModeAsync, GrabModeSync, None, None);
  923. }
  924. else
  925. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  926. BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  927. }
  928. }
  929. void
  930. grabkeys(void) {
  931. updatenumlockmask();
  932. {
  933. unsigned int i, j;
  934. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  935. KeyCode code;
  936. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  937. for(i = 0; i < LENGTH(keys); i++)
  938. if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  939. for(j = 0; j < LENGTH(modifiers); j++)
  940. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  941. True, GrabModeAsync, GrabModeAsync);
  942. }
  943. }
  944. void
  945. incnmaster(const Arg *arg) {
  946. selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
  947. arrange(selmon);
  948. }
  949. void
  950. initfont(const char *fontstr) {
  951. char *def, **missing;
  952. int n;
  953. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  954. if(missing) {
  955. while(n--)
  956. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  957. XFreeStringList(missing);
  958. }
  959. if(dc.font.set) {
  960. XFontStruct **xfonts;
  961. char **font_names;
  962. dc.font.ascent = dc.font.descent = 0;
  963. XExtentsOfFontSet(dc.font.set);
  964. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  965. while(n--) {
  966. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  967. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  968. xfonts++;
  969. }
  970. }
  971. else {
  972. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  973. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  974. die("error, cannot load font: '%s'\n", fontstr);
  975. dc.font.ascent = dc.font.xfont->ascent;
  976. dc.font.descent = dc.font.xfont->descent;
  977. }
  978. dc.font.height = dc.font.ascent + dc.font.descent;
  979. }
  980. #ifdef XINERAMA
  981. static Bool
  982. isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
  983. while(n--)
  984. /* treat origin (x, y) as fixpoint for uniqueness only, first screen wins */
  985. if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org)
  986. return False;
  987. return True;
  988. }
  989. #endif /* XINERAMA */
  990. void
  991. keypress(XEvent *e) {
  992. unsigned int i;
  993. KeySym keysym;
  994. XKeyEvent *ev;
  995. ev = &e->xkey;
  996. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  997. for(i = 0; i < LENGTH(keys); i++)
  998. if(keysym == keys[i].keysym
  999. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  1000. && keys[i].func)
  1001. keys[i].func(&(keys[i].arg));
  1002. }
  1003. void
  1004. killclient(const Arg *arg) {
  1005. if(!selmon->sel)
  1006. return;
  1007. if(!sendevent(selmon->sel, wmatom[WMDelete])) {
  1008. XGrabServer(dpy);
  1009. XSetErrorHandler(xerrordummy);
  1010. XSetCloseDownMode(dpy, DestroyAll);
  1011. XKillClient(dpy, selmon->sel->win);
  1012. XSync(dpy, False);
  1013. XSetErrorHandler(xerror);
  1014. XUngrabServer(dpy);
  1015. }
  1016. }
  1017. void
  1018. manage(Window w, XWindowAttributes *wa) {
  1019. Client *c, *t = NULL;
  1020. Window trans = None;
  1021. XWindowChanges wc;
  1022. if(!(c = calloc(1, sizeof(Client))))
  1023. die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  1024. c->win = w;
  1025. updatetitle(c);
  1026. if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
  1027. c->mon = t->mon;
  1028. c->tags = t->tags;
  1029. }
  1030. else {
  1031. c->mon = selmon;
  1032. applyrules(c);
  1033. }
  1034. /* geometry */
  1035. c->x = c->oldx = wa->x;
  1036. c->y = c->oldy = wa->y;
  1037. c->w = c->oldw = wa->width;
  1038. c->h = c->oldh = wa->height;
  1039. c->oldbw = wa->border_width;
  1040. if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  1041. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  1042. if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  1043. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  1044. c->x = MAX(c->x, c->mon->mx);
  1045. /* only fix client y-offset, if the client center might cover the bar */
  1046. c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
  1047. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  1048. c->bw = borderpx;
  1049. wc.border_width = c->bw;
  1050. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  1051. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  1052. configure(c); /* propagates border_width, if size doesn't change */
  1053. updatewindowtype(c);
  1054. updatesizehints(c);
  1055. updatewmhints(c);
  1056. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  1057. grabbuttons(c, False);
  1058. if(!c->isfloating)
  1059. c->isfloating = c->oldstate = trans != None || c->isfixed;
  1060. if(c->isfloating)
  1061. XRaiseWindow(dpy, c->win);
  1062. attach(c);
  1063. attachstack(c);
  1064. XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
  1065. (unsigned char *) &(c->win), 1);
  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. if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
  1128. return;
  1129. restack(selmon);
  1130. ocx = c->x;
  1131. ocy = c->y;
  1132. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1133. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1134. return;
  1135. if(!getrootptr(&x, &y))
  1136. return;
  1137. do {
  1138. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1139. switch(ev.type) {
  1140. case ConfigureRequest:
  1141. case Expose:
  1142. case MapRequest:
  1143. handler[ev.type](&ev);
  1144. break;
  1145. case MotionNotify:
  1146. nx = ocx + (ev.xmotion.x - x);
  1147. ny = ocy + (ev.xmotion.y - y);
  1148. if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1149. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1150. if(abs(selmon->wx - nx) < snap)
  1151. nx = selmon->wx;
  1152. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1153. nx = selmon->wx + selmon->ww - WIDTH(c);
  1154. if(abs(selmon->wy - ny) < snap)
  1155. ny = selmon->wy;
  1156. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1157. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1158. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1159. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1160. togglefloating(NULL);
  1161. }
  1162. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1163. resize(c, nx, ny, c->w, c->h, True);
  1164. break;
  1165. }
  1166. } while(ev.type != ButtonRelease);
  1167. XUngrabPointer(dpy, CurrentTime);
  1168. if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1169. sendmon(c, m);
  1170. selmon = m;
  1171. focus(NULL);
  1172. }
  1173. }
  1174. Client *
  1175. nexttiled(Client *c) {
  1176. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1177. return c;
  1178. }
  1179. void
  1180. pop(Client *c) {
  1181. detach(c);
  1182. attach(c);
  1183. focus(c);
  1184. arrange(c->mon);
  1185. }
  1186. void
  1187. propertynotify(XEvent *e) {
  1188. Client *c;
  1189. Window trans;
  1190. XPropertyEvent *ev = &e->xproperty;
  1191. if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1192. updatestatus();
  1193. else if(ev->state == PropertyDelete)
  1194. return; /* ignore */
  1195. else if((c = wintoclient(ev->window))) {
  1196. switch(ev->atom) {
  1197. default: break;
  1198. case XA_WM_TRANSIENT_FOR:
  1199. if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
  1200. (c->isfloating = (wintoclient(trans)) != NULL))
  1201. arrange(c->mon);
  1202. break;
  1203. case XA_WM_NORMAL_HINTS:
  1204. updatesizehints(c);
  1205. break;
  1206. case XA_WM_HINTS:
  1207. updatewmhints(c);
  1208. drawbars();
  1209. break;
  1210. }
  1211. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1212. updatetitle(c);
  1213. if(c == c->mon->sel)
  1214. drawbar(c->mon);
  1215. }
  1216. if(ev->atom == netatom[NetWMWindowType])
  1217. updatewindowtype(c);
  1218. }
  1219. }
  1220. void
  1221. quit(const Arg *arg) {
  1222. running = False;
  1223. }
  1224. Monitor *
  1225. recttomon(int x, int y, int w, int h) {
  1226. Monitor *m, *r = selmon;
  1227. int a, area = 0;
  1228. for(m = mons; m; m = m->next)
  1229. if((a = INTERSECT(x, y, w, h, m)) > area) {
  1230. area = a;
  1231. r = m;
  1232. }
  1233. return r;
  1234. }
  1235. void
  1236. resize(Client *c, int x, int y, int w, int h, Bool interact) {
  1237. if(applysizehints(c, &x, &y, &w, &h, interact))
  1238. resizeclient(c, x, y, w, h);
  1239. }
  1240. void
  1241. resizeclient(Client *c, int x, int y, int w, int h) {
  1242. XWindowChanges wc;
  1243. c->oldx = c->x; c->x = wc.x = x;
  1244. c->oldy = c->y; c->y = wc.y = y;
  1245. c->oldw = c->w; c->w = wc.width = w;
  1246. c->oldh = c->h; c->h = wc.height = h;
  1247. wc.border_width = c->bw;
  1248. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1249. configure(c);
  1250. XSync(dpy, False);
  1251. }
  1252. void
  1253. resizemouse(const Arg *arg) {
  1254. int ocx, ocy;
  1255. int nw, nh;
  1256. Client *c;
  1257. Monitor *m;
  1258. XEvent ev;
  1259. if(!(c = selmon->sel))
  1260. return;
  1261. if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
  1262. return;
  1263. restack(selmon);
  1264. ocx = c->x;
  1265. ocy = c->y;
  1266. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1267. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1268. return;
  1269. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1270. do {
  1271. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1272. switch(ev.type) {
  1273. case ConfigureRequest:
  1274. case Expose:
  1275. case MapRequest:
  1276. handler[ev.type](&ev);
  1277. break;
  1278. case MotionNotify:
  1279. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1280. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1281. if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
  1282. && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
  1283. {
  1284. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1285. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1286. togglefloating(NULL);
  1287. }
  1288. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1289. resize(c, c->x, c->y, nw, nh, True);
  1290. break;
  1291. }
  1292. } while(ev.type != ButtonRelease);
  1293. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1294. XUngrabPointer(dpy, CurrentTime);
  1295. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1296. if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1297. sendmon(c, m);
  1298. selmon = m;
  1299. focus(NULL);
  1300. }
  1301. }
  1302. void
  1303. restack(Monitor *m) {
  1304. Client *c;
  1305. XEvent ev;
  1306. XWindowChanges wc;
  1307. drawbar(m);
  1308. if(!m->sel)
  1309. return;
  1310. if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
  1311. XRaiseWindow(dpy, m->sel->win);
  1312. if(m->lt[m->sellt]->arrange) {
  1313. wc.stack_mode = Below;
  1314. wc.sibling = m->barwin;
  1315. for(c = m->stack; c; c = c->snext)
  1316. if(!c->isfloating && ISVISIBLE(c)) {
  1317. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1318. wc.sibling = c->win;
  1319. }
  1320. }
  1321. XSync(dpy, False);
  1322. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1323. }
  1324. void
  1325. run(void) {
  1326. XEvent ev;
  1327. /* main event loop */
  1328. XSync(dpy, False);
  1329. while(running && !XNextEvent(dpy, &ev))
  1330. if(handler[ev.type])
  1331. handler[ev.type](&ev); /* call handler */
  1332. }
  1333. void
  1334. scan(void) {
  1335. unsigned int i, num;
  1336. Window d1, d2, *wins = NULL;
  1337. XWindowAttributes wa;
  1338. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1339. for(i = 0; i < num; i++) {
  1340. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1341. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1342. continue;
  1343. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1344. manage(wins[i], &wa);
  1345. }
  1346. for(i = 0; i < num; i++) { /* now the transients */
  1347. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1348. continue;
  1349. if(XGetTransientForHint(dpy, wins[i], &d1)
  1350. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1351. manage(wins[i], &wa);
  1352. }
  1353. if(wins)
  1354. XFree(wins);
  1355. }
  1356. }
  1357. void
  1358. sendmon(Client *c, Monitor *m) {
  1359. if(c->mon == m)
  1360. return;
  1361. unfocus(c, True);
  1362. detach(c);
  1363. detachstack(c);
  1364. c->mon = m;
  1365. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1366. attach(c);
  1367. attachstack(c);
  1368. focus(NULL);
  1369. arrange(NULL);
  1370. }
  1371. void
  1372. setclientstate(Client *c, long state) {
  1373. long data[] = { state, None };
  1374. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1375. PropModeReplace, (unsigned char *)data, 2);
  1376. }
  1377. Bool
  1378. sendevent(Client *c, Atom proto) {
  1379. int n;
  1380. Atom *protocols;
  1381. Bool exists = False;
  1382. XEvent ev;
  1383. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  1384. while(!exists && n--)
  1385. exists = protocols[n] == proto;
  1386. XFree(protocols);
  1387. }
  1388. if(exists) {
  1389. ev.type = ClientMessage;
  1390. ev.xclient.window = c->win;
  1391. ev.xclient.message_type = wmatom[WMProtocols];
  1392. ev.xclient.format = 32;
  1393. ev.xclient.data.l[0] = proto;
  1394. ev.xclient.data.l[1] = CurrentTime;
  1395. XSendEvent(dpy, c->win, False, NoEventMask, &ev);
  1396. }
  1397. return exists;
  1398. }
  1399. void
  1400. setfocus(Client *c) {
  1401. if(!c->neverfocus) {
  1402. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  1403. XChangeProperty(dpy, root, netatom[NetActiveWindow],
  1404. XA_WINDOW, 32, PropModeReplace,
  1405. (unsigned char *) &(c->win), 1);
  1406. }
  1407. sendevent(c, wmatom[WMTakeFocus]);
  1408. }
  1409. void
  1410. setfullscreen(Client *c, Bool fullscreen) {
  1411. if(fullscreen) {
  1412. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1413. PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
  1414. c->isfullscreen = True;
  1415. c->oldstate = c->isfloating;
  1416. c->oldbw = c->bw;
  1417. c->bw = 0;
  1418. c->isfloating = True;
  1419. resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
  1420. XRaiseWindow(dpy, c->win);
  1421. }
  1422. else {
  1423. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1424. PropModeReplace, (unsigned char*)0, 0);
  1425. c->isfullscreen = False;
  1426. c->isfloating = c->oldstate;
  1427. c->bw = c->oldbw;
  1428. c->x = c->oldx;
  1429. c->y = c->oldy;
  1430. c->w = c->oldw;
  1431. c->h = c->oldh;
  1432. resizeclient(c, c->x, c->y, c->w, c->h);
  1433. arrange(c->mon);
  1434. }
  1435. }
  1436. void
  1437. setlayout(const Arg *arg) {
  1438. if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
  1439. selmon->sellt ^= 1;
  1440. if(arg && arg->v)
  1441. selmon->lt[selmon->sellt] = (Layout *)arg->v;
  1442. strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
  1443. if(selmon->sel)
  1444. arrange(selmon);
  1445. else
  1446. drawbar(selmon);
  1447. }
  1448. /* arg > 1.0 will set mfact absolutly */
  1449. void
  1450. setmfact(const Arg *arg) {
  1451. float f;
  1452. if(!arg || !selmon->lt[selmon->sellt]->arrange)
  1453. return;
  1454. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1455. if(f < 0.1 || f > 0.9)
  1456. return;
  1457. selmon->mfact = f;
  1458. arrange(selmon);
  1459. }
  1460. void
  1461. setup(void) {
  1462. XSetWindowAttributes wa;
  1463. /* clean up any zombies immediately */
  1464. sigchld(0);
  1465. /* init screen */
  1466. screen = DefaultScreen(dpy);
  1467. root = RootWindow(dpy, screen);
  1468. initfont(font);
  1469. sw = DisplayWidth(dpy, screen);
  1470. sh = DisplayHeight(dpy, screen);
  1471. bh = dc.h = dc.font.height + 2;
  1472. updategeom();
  1473. /* init atoms */
  1474. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1475. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1476. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1477. wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
  1478. netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
  1479. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1480. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1481. netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
  1482. netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
  1483. netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
  1484. netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
  1485. netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
  1486. /* init cursors */
  1487. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1488. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1489. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1490. /* init appearance */
  1491. dc.norm[ColBorder] = getcolor(normbordercolor);
  1492. dc.norm[ColBG] = getcolor(normbgcolor);
  1493. dc.norm[ColFG] = getcolor(normfgcolor);
  1494. dc.sel[ColBorder] = getcolor(selbordercolor);
  1495. dc.sel[ColBG] = getcolor(selbgcolor);
  1496. dc.sel[ColFG] = getcolor(selfgcolor);
  1497. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1498. dc.gc = XCreateGC(dpy, root, 0, NULL);
  1499. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1500. if(!dc.font.set)
  1501. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1502. /* init bars */
  1503. updatebars();
  1504. updatestatus();
  1505. /* EWMH support per view */
  1506. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1507. PropModeReplace, (unsigned char *) netatom, NetLast);
  1508. XDeleteProperty(dpy, root, netatom[NetClientList]);
  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. if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
  1608. return;
  1609. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1610. if(selmon->sel->isfloating)
  1611. resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  1612. selmon->sel->w, selmon->sel->h, 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. updateclientlist();
  1668. arrange(m);
  1669. }
  1670. void
  1671. unmapnotify(XEvent *e) {
  1672. Client *c;
  1673. XUnmapEvent *ev = &e->xunmap;
  1674. if((c = wintoclient(ev->window))) {
  1675. if(ev->send_event)
  1676. setclientstate(c, WithdrawnState);
  1677. else
  1678. unmanage(c, False);
  1679. }
  1680. }
  1681. void
  1682. updatebars(void) {
  1683. Monitor *m;
  1684. XSetWindowAttributes wa = {
  1685. .override_redirect = True,
  1686. .background_pixmap = ParentRelative,
  1687. .event_mask = ButtonPressMask|ExposureMask
  1688. };
  1689. for(m = mons; m; m = m->next) {
  1690. if (m->barwin)
  1691. continue;
  1692. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  1693. CopyFromParent, DefaultVisual(dpy, screen),
  1694. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1695. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1696. XMapRaised(dpy, m->barwin);
  1697. }
  1698. }
  1699. void
  1700. updatebarpos(Monitor *m) {
  1701. m->wy = m->my;
  1702. m->wh = m->mh;
  1703. if(m->showbar) {
  1704. m->wh -= bh;
  1705. m->by = m->topbar ? m->wy : m->wy + m->wh;
  1706. m->wy = m->topbar ? m->wy + bh : m->wy;
  1707. }
  1708. else
  1709. m->by = -bh;
  1710. }
  1711. void
  1712. updateclientlist() {
  1713. Client *c;
  1714. Monitor *m;
  1715. XDeleteProperty(dpy, root, netatom[NetClientList]);
  1716. for(m = mons; m; m = m->next)
  1717. for(c = m->clients; c; c = c->next)
  1718. XChangeProperty(dpy, root, netatom[NetClientList],
  1719. XA_WINDOW, 32, PropModeAppend,
  1720. (unsigned char *) &(c->win), 1);
  1721. }
  1722. void
  1723. updategeom(void) {
  1724. /* Starting with dwm 6.1 this function uses a new (simpler) strategy:
  1725. * whenever screen changes are reported, we destroy all monitors
  1726. * and recreate all unique origin monitors and add all clients to
  1727. * the first monitor, only. In several circumstances this may suck,
  1728. * but dealing with all corner-cases sucks even more.*/
  1729. #ifdef XINERAMA
  1730. if(XineramaIsActive(dpy)) {
  1731. int i, j, n;
  1732. Client *c;
  1733. Monitor *m, *oldmons = mons;
  1734. XineramaScreenInfo *info = XineramaQueryScreens(dpy, &n);
  1735. XineramaScreenInfo *unique = NULL;
  1736. /* only consider unique geometries as separate screens */
  1737. if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * n)))
  1738. die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * n);
  1739. for(i = 0, j = 0; i < n; i++)
  1740. if(isuniquegeom(unique, j, &info[i]))
  1741. memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
  1742. XFree(info);
  1743. /* create new monitor structure */
  1744. n = j;
  1745. mons = m = createmon(); /* new first monitor */
  1746. for(i = 1; i < n; i++) {
  1747. m->next = createmon();
  1748. m = m->next;
  1749. }
  1750. for(i = 0, m = mons; i < n && m; m = m->next, i++) {
  1751. m->num = i;
  1752. m->mx = m->wx = unique[i].x_org;
  1753. m->my = m->wy = unique[i].y_org;
  1754. m->mw = m->ww = unique[i].width;
  1755. m->mh = m->wh = unique[i].height;
  1756. updatebarpos(m);
  1757. }
  1758. free(unique);
  1759. /* re-attach old clients and cleanup old monitor structure */
  1760. while(oldmons) {
  1761. m = oldmons;
  1762. while(m->clients) {
  1763. c = m->clients;
  1764. m->clients = c->next;
  1765. detachstack(c);
  1766. c->mon = mons;
  1767. attach(c);
  1768. attachstack(c);
  1769. }
  1770. oldmons = m->next;
  1771. cleanupmon(m);
  1772. }
  1773. }
  1774. else
  1775. #endif /* XINERAMA */
  1776. /* default monitor setup */
  1777. {
  1778. if(!mons) /* only true if !XINERAMA compile flag */
  1779. mons = createmon();
  1780. if(mons->mw != sw || mons->mh != sh) {
  1781. mons->mw = mons->ww = sw;
  1782. mons->mh = mons->wh = sh;
  1783. updatebarpos(mons);
  1784. }
  1785. }
  1786. selmon = mons;
  1787. selmon = wintomon(root);
  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. }