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.

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