Configuration file for DWM on MacBook Air
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1928 lines
46 KiB

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