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.

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