Configuration of dwm for Mac Computers
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.

1933 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
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) { /* status is only drawn on selected monitor */
  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. }
  596. else {
  597. dc.x = m->ww;
  598. }
  599. if((dc.w = dc.x - x) > bh) {
  600. dc.x = x;
  601. if(m->sel) {
  602. col = m == selmon ? dc.sel : dc.norm;
  603. drawtext(m->sel->name, col, False);
  604. drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
  605. }
  606. else
  607. drawtext(NULL, dc.norm, False);
  608. }
  609. XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
  610. XSync(dpy, False);
  611. }
  612. void
  613. drawbars() {
  614. Monitor *m;
  615. for(m = mons; m; m = m->next)
  616. drawbar(m);
  617. }
  618. void
  619. drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  620. int x;
  621. XGCValues gcv;
  622. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  623. gcv.foreground = col[invert ? ColBG : ColFG];
  624. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  625. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  626. r.x = dc.x + 1;
  627. r.y = dc.y + 1;
  628. if(filled) {
  629. r.width = r.height = x + 1;
  630. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  631. }
  632. else if(empty) {
  633. r.width = r.height = x;
  634. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  635. }
  636. }
  637. void
  638. drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
  639. char buf[256];
  640. int i, x, y, h, len, olen;
  641. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  642. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  643. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  644. if(!text)
  645. return;
  646. olen = strlen(text);
  647. h = dc.font.ascent + dc.font.descent;
  648. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  649. x = dc.x + (h / 2);
  650. /* shorten text if necessary */
  651. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  652. if(!len)
  653. return;
  654. memcpy(buf, text, len);
  655. if(len < olen)
  656. for(i = len; i && i > len - 3; buf[--i] = '.');
  657. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  658. if(dc.font.set)
  659. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  660. else
  661. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  662. }
  663. void
  664. enternotify(XEvent *e) {
  665. Client *c;
  666. XCrossingEvent *ev = &e->xcrossing;
  667. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  668. return;
  669. if((c = getclient(ev->window)))
  670. focus(c);
  671. else
  672. focus(NULL);
  673. }
  674. void
  675. expose(XEvent *e) {
  676. Monitor *m;
  677. XExposeEvent *ev = &e->xexpose;
  678. if(ev->count == 0)
  679. for(m = mons; m; m = m->next)
  680. if(ev->window == m->barwin) {
  681. drawbar(m);
  682. break;
  683. }
  684. }
  685. void
  686. focus(Client *c) {
  687. if(!c || !ISVISIBLE(c))
  688. for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
  689. if(selmon->sel && selmon->sel != c) {
  690. grabbuttons(selmon->sel, False);
  691. XSetWindowBorder(dpy, selmon->sel->win, dc.norm[ColBorder]);
  692. }
  693. if(c) {
  694. if(c->mon != selmon)
  695. selmon = c->mon;
  696. if(c->isurgent)
  697. clearurgent(c);
  698. detachstack(c);
  699. attachstack(c);
  700. grabbuttons(c, True);
  701. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  702. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  703. }
  704. else
  705. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  706. selmon->sel = c;
  707. drawbars();
  708. }
  709. void
  710. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  711. XFocusChangeEvent *ev = &e->xfocus;
  712. if(selmon->sel && ev->window != selmon->sel->win)
  713. XSetInputFocus(dpy, selmon->sel->win, RevertToPointerRoot, CurrentTime);
  714. }
  715. #ifdef XINERAMA
  716. void
  717. focusmon(const Arg *arg) {
  718. unsigned int i;
  719. Monitor *m;
  720. for(i = 0, m = mons; m; m = m->next, i++)
  721. if(i == arg->ui) {
  722. if(m->stack)
  723. focus(m->stack);
  724. else {
  725. selmon = m;
  726. focus(NULL);
  727. }
  728. drawbars();
  729. break;
  730. }
  731. }
  732. #endif /* XINERAMA */
  733. void
  734. focusstack(const Arg *arg) {
  735. Client *c = NULL, *i;
  736. if(!selmon->sel)
  737. return;
  738. if(arg->i > 0) {
  739. for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
  740. if(!c)
  741. for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
  742. }
  743. else {
  744. for(i = selmon->clients; i != selmon->sel; i = i->next)
  745. if(ISVISIBLE(i))
  746. c = i;
  747. if(!c)
  748. for(; i; i = i->next)
  749. if(ISVISIBLE(i))
  750. c = i;
  751. }
  752. if(c) {
  753. focus(c);
  754. restack(selmon);
  755. }
  756. }
  757. Client *
  758. getclient(Window w) {
  759. Client *c;
  760. Monitor *m;
  761. for(m = mons; m; m = m->next)
  762. for(c = m->clients; c; c = c->next)
  763. if(c->win == w)
  764. return c;
  765. return NULL;
  766. }
  767. unsigned long
  768. getcolor(const char *colstr) {
  769. Colormap cmap = DefaultColormap(dpy, screen);
  770. XColor color;
  771. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  772. die("error, cannot allocate color '%s'\n", colstr);
  773. return color.pixel;
  774. }
  775. long
  776. getstate(Window w) {
  777. int format, status;
  778. long result = -1;
  779. unsigned char *p = NULL;
  780. unsigned long n, extra;
  781. Atom real;
  782. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  783. &real, &format, &n, &extra, (unsigned char **)&p);
  784. if(status != Success)
  785. return -1;
  786. if(n != 0)
  787. result = *p;
  788. XFree(p);
  789. return result;
  790. }
  791. Bool
  792. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  793. char **list = NULL;
  794. int n;
  795. XTextProperty name;
  796. if(!text || size == 0)
  797. return False;
  798. text[0] = '\0';
  799. XGetTextProperty(dpy, w, &name, atom);
  800. if(!name.nitems)
  801. return False;
  802. if(name.encoding == XA_STRING)
  803. strncpy(text, (char *)name.value, size - 1);
  804. else {
  805. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  806. && n > 0 && *list) {
  807. strncpy(text, *list, size - 1);
  808. XFreeStringList(list);
  809. }
  810. }
  811. text[size - 1] = '\0';
  812. XFree(name.value);
  813. return True;
  814. }
  815. void
  816. grabbuttons(Client *c, Bool focused) {
  817. updatenumlockmask();
  818. {
  819. unsigned int i, j;
  820. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  821. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  822. if(focused) {
  823. for(i = 0; i < LENGTH(buttons); i++)
  824. if(buttons[i].click == ClkClientWin)
  825. for(j = 0; j < LENGTH(modifiers); j++)
  826. XGrabButton(dpy, buttons[i].button,
  827. buttons[i].mask | modifiers[j],
  828. c->win, False, BUTTONMASK,
  829. GrabModeAsync, GrabModeSync, None, None);
  830. } else
  831. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  832. BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  833. }
  834. }
  835. void
  836. grabkeys(void) {
  837. updatenumlockmask();
  838. { /* grab keys */
  839. unsigned int i, j;
  840. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  841. KeyCode code;
  842. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  843. for(i = 0; i < LENGTH(keys); i++) {
  844. if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  845. for(j = 0; j < LENGTH(modifiers); j++)
  846. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  847. True, GrabModeAsync, GrabModeAsync);
  848. }
  849. }
  850. }
  851. void
  852. initfont(const char *fontstr) {
  853. char *def, **missing;
  854. int i, n;
  855. missing = NULL;
  856. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  857. if(missing) {
  858. while(n--)
  859. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  860. XFreeStringList(missing);
  861. }
  862. if(dc.font.set) {
  863. XFontSetExtents *font_extents;
  864. XFontStruct **xfonts;
  865. char **font_names;
  866. dc.font.ascent = dc.font.descent = 0;
  867. font_extents = XExtentsOfFontSet(dc.font.set);
  868. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  869. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  870. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  871. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  872. xfonts++;
  873. }
  874. }
  875. else {
  876. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  877. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  878. die("error, cannot load font: '%s'\n", fontstr);
  879. dc.font.ascent = dc.font.xfont->ascent;
  880. dc.font.descent = dc.font.xfont->descent;
  881. }
  882. dc.font.height = dc.font.ascent + dc.font.descent;
  883. }
  884. Bool
  885. isprotodel(Client *c) {
  886. int i, n;
  887. Atom *protocols;
  888. Bool ret = False;
  889. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  890. for(i = 0; !ret && i < n; i++)
  891. if(protocols[i] == wmatom[WMDelete])
  892. ret = True;
  893. XFree(protocols);
  894. }
  895. return ret;
  896. }
  897. void
  898. keypress(XEvent *e) {
  899. unsigned int i;
  900. KeySym keysym;
  901. XKeyEvent *ev;
  902. ev = &e->xkey;
  903. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  904. for(i = 0; i < LENGTH(keys); i++)
  905. if(keysym == keys[i].keysym
  906. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  907. && keys[i].func)
  908. keys[i].func(&(keys[i].arg));
  909. }
  910. void
  911. killclient(const Arg *arg) {
  912. XEvent ev;
  913. if(!selmon->sel)
  914. return;
  915. if(isprotodel(selmon->sel)) {
  916. ev.type = ClientMessage;
  917. ev.xclient.window = selmon->sel->win;
  918. ev.xclient.message_type = wmatom[WMProtocols];
  919. ev.xclient.format = 32;
  920. ev.xclient.data.l[0] = wmatom[WMDelete];
  921. ev.xclient.data.l[1] = CurrentTime;
  922. XSendEvent(dpy, selmon->sel->win, False, NoEventMask, &ev);
  923. }
  924. else
  925. XKillClient(dpy, selmon->sel->win);
  926. }
  927. void
  928. manage(Window w, XWindowAttributes *wa) {
  929. static Client cz;
  930. Client *c, *t = NULL;
  931. Window trans = None;
  932. XWindowChanges wc;
  933. if(!(c = malloc(sizeof(Client))))
  934. die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  935. *c = cz;
  936. c->win = w;
  937. c->mon = selmon;
  938. /* geometry */
  939. c->x = wa->x;
  940. c->y = wa->y;
  941. c->w = wa->width;
  942. c->h = wa->height;
  943. c->oldbw = wa->border_width;
  944. if(c->w == sw && c->h == sh) {
  945. c->x = sx;
  946. c->y = sy;
  947. c->bw = 0;
  948. }
  949. else {
  950. if(c->x + WIDTH(c) > sx + sw)
  951. c->x = sx + sw - WIDTH(c);
  952. if(c->y + HEIGHT(c) > sy + sh)
  953. c->y = sy + sh - HEIGHT(c);
  954. c->x = MAX(c->x, sx);
  955. /* only fix client y-offset, if the client center might cover the bar */
  956. c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
  957. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : sy);
  958. c->bw = borderpx;
  959. }
  960. wc.border_width = c->bw;
  961. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  962. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  963. configure(c); /* propagates border_width, if size doesn't change */
  964. updatesizehints(c);
  965. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  966. grabbuttons(c, False);
  967. updatetitle(c);
  968. if(XGetTransientForHint(dpy, w, &trans))
  969. t = getclient(trans);
  970. if(t)
  971. c->tags = t->tags;
  972. else
  973. applyrules(c);
  974. if(!c->isfloating)
  975. c->isfloating = trans != None || c->isfixed;
  976. if(c->isfloating)
  977. XRaiseWindow(dpy, c->win);
  978. attach(c);
  979. attachstack(c);
  980. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  981. XMapWindow(dpy, c->win);
  982. setclientstate(c, NormalState);
  983. arrange();
  984. }
  985. void
  986. mappingnotify(XEvent *e) {
  987. XMappingEvent *ev = &e->xmapping;
  988. XRefreshKeyboardMapping(ev);
  989. if(ev->request == MappingKeyboard)
  990. grabkeys();
  991. }
  992. void
  993. maprequest(XEvent *e) {
  994. static XWindowAttributes wa;
  995. XMapRequestEvent *ev = &e->xmaprequest;
  996. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  997. return;
  998. if(wa.override_redirect)
  999. return;
  1000. if(!getclient(ev->window))
  1001. manage(ev->window, &wa);
  1002. }
  1003. void
  1004. monocle(Monitor *m) {
  1005. Client *c;
  1006. for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1007. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw);
  1008. }
  1009. void
  1010. movemouse(const Arg *arg) {
  1011. int x, y, ocx, ocy, di, nx, ny;
  1012. unsigned int dui;
  1013. Client *c;
  1014. Window dummy;
  1015. XEvent ev;
  1016. if(!(c = selmon->sel))
  1017. return;
  1018. restack(selmon);
  1019. ocx = c->x;
  1020. ocy = c->y;
  1021. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1022. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1023. return;
  1024. XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui);
  1025. do {
  1026. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1027. switch (ev.type) {
  1028. case ConfigureRequest:
  1029. case Expose:
  1030. case MapRequest:
  1031. handler[ev.type](&ev);
  1032. break;
  1033. case MotionNotify:
  1034. nx = ocx + (ev.xmotion.x - x);
  1035. ny = ocy + (ev.xmotion.y - y);
  1036. if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1037. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1038. if(abs(selmon->wx - nx) < snap)
  1039. nx = selmon->wx;
  1040. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1041. nx = selmon->wx + selmon->ww - WIDTH(c);
  1042. if(abs(selmon->wy - ny) < snap)
  1043. ny = selmon->wy;
  1044. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1045. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1046. if(!c->isfloating && lt[selmon->sellt]->arrange
  1047. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1048. togglefloating(NULL);
  1049. }
  1050. if(!lt[selmon->sellt]->arrange || c->isfloating)
  1051. resize(c, nx, ny, c->w, c->h);
  1052. break;
  1053. }
  1054. }
  1055. while(ev.type != ButtonRelease);
  1056. XUngrabPointer(dpy, CurrentTime);
  1057. }
  1058. Client *
  1059. nexttiled(Client *c) {
  1060. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1061. return c;
  1062. }
  1063. void
  1064. propertynotify(XEvent *e) {
  1065. Client *c;
  1066. Window trans;
  1067. XPropertyEvent *ev = &e->xproperty;
  1068. if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1069. updatestatus();
  1070. else if(ev->state == PropertyDelete)
  1071. return; /* ignore */
  1072. else if((c = getclient(ev->window))) {
  1073. switch (ev->atom) {
  1074. default: break;
  1075. case XA_WM_TRANSIENT_FOR:
  1076. XGetTransientForHint(dpy, c->win, &trans);
  1077. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  1078. arrange();
  1079. break;
  1080. case XA_WM_NORMAL_HINTS:
  1081. updatesizehints(c);
  1082. break;
  1083. case XA_WM_HINTS:
  1084. updatewmhints(c);
  1085. drawbars();
  1086. break;
  1087. }
  1088. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1089. updatetitle(c);
  1090. if(c == selmon->sel)
  1091. drawbars();
  1092. }
  1093. }
  1094. }
  1095. void
  1096. quit(const Arg *arg) {
  1097. running = False;
  1098. }
  1099. void
  1100. resize(Client *c, int x, int y, int w, int h) {
  1101. XWindowChanges wc;
  1102. if(applysizehints(c, &x, &y, &w, &h)) {
  1103. c->x = wc.x = x;
  1104. c->y = wc.y = y;
  1105. c->w = wc.width = w;
  1106. c->h = wc.height = h;
  1107. wc.border_width = c->bw;
  1108. XConfigureWindow(dpy, c->win,
  1109. CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1110. configure(c);
  1111. XSync(dpy, False);
  1112. }
  1113. }
  1114. void
  1115. resizemouse(const Arg *arg) {
  1116. int ocx, ocy;
  1117. int nw, nh;
  1118. Client *c;
  1119. XEvent ev;
  1120. if(!(c = selmon->sel))
  1121. return;
  1122. restack(selmon);
  1123. ocx = c->x;
  1124. ocy = c->y;
  1125. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1126. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1127. return;
  1128. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1129. do {
  1130. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1131. switch(ev.type) {
  1132. case ConfigureRequest:
  1133. case Expose:
  1134. case MapRequest:
  1135. handler[ev.type](&ev);
  1136. break;
  1137. case MotionNotify:
  1138. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1139. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1140. if(snap && nw >= selmon->wx && nw <= selmon->wx + selmon->ww
  1141. && nh >= selmon->wy && nh <= selmon->wy + selmon->wh) {
  1142. if(!c->isfloating && lt[selmon->sellt]->arrange
  1143. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1144. togglefloating(NULL);
  1145. }
  1146. if(!lt[selmon->sellt]->arrange || c->isfloating)
  1147. resize(c, c->x, c->y, nw, nh);
  1148. break;
  1149. }
  1150. }
  1151. while(ev.type != ButtonRelease);
  1152. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1153. XUngrabPointer(dpy, CurrentTime);
  1154. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1155. }
  1156. void
  1157. restack(Monitor *m) {
  1158. Client *c;
  1159. XEvent ev;
  1160. XWindowChanges wc;
  1161. drawbars();
  1162. if(!m->sel)
  1163. return;
  1164. if(m->sel->isfloating || !lt[m->sellt]->arrange)
  1165. XRaiseWindow(dpy, m->sel->win);
  1166. if(lt[m->sellt]->arrange) {
  1167. wc.stack_mode = Below;
  1168. wc.sibling = m->barwin;
  1169. for(c = m->stack; c; c = c->snext)
  1170. if(!c->isfloating && ISVISIBLE(c)) {
  1171. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1172. wc.sibling = c->win;
  1173. }
  1174. }
  1175. XSync(dpy, False);
  1176. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1177. }
  1178. void
  1179. run(void) {
  1180. XEvent ev;
  1181. /* main event loop */
  1182. XSync(dpy, False);
  1183. while(running && !XNextEvent(dpy, &ev)) {
  1184. if(handler[ev.type])
  1185. (handler[ev.type])(&ev); /* call handler */
  1186. }
  1187. }
  1188. void
  1189. scan(void) {
  1190. unsigned int i, num;
  1191. Window d1, d2, *wins = NULL;
  1192. XWindowAttributes wa;
  1193. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1194. for(i = 0; i < num; i++) {
  1195. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1196. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1197. continue;
  1198. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1199. manage(wins[i], &wa);
  1200. }
  1201. for(i = 0; i < num; i++) { /* now the transients */
  1202. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1203. continue;
  1204. if(XGetTransientForHint(dpy, wins[i], &d1)
  1205. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1206. manage(wins[i], &wa);
  1207. }
  1208. if(wins)
  1209. XFree(wins);
  1210. }
  1211. }
  1212. void
  1213. setclientstate(Client *c, long state) {
  1214. long data[] = {state, None};
  1215. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1216. PropModeReplace, (unsigned char *)data, 2);
  1217. }
  1218. void
  1219. setlayout(const Arg *arg) {
  1220. if(!arg || !arg->v || arg->v != lt[selmon->sellt])
  1221. selmon->sellt ^= 1;
  1222. if(arg && arg->v)
  1223. lt[selmon->sellt] = (Layout *)arg->v;
  1224. if(selmon->sel)
  1225. arrange();
  1226. else
  1227. drawbars();
  1228. }
  1229. /* arg > 1.0 will set mfact absolutly */
  1230. void
  1231. setmfact(const Arg *arg) {
  1232. float f;
  1233. if(!arg || !lt[selmon->sellt]->arrange)
  1234. return;
  1235. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1236. if(f < 0.1 || f > 0.9)
  1237. return;
  1238. selmon->mfact = f;
  1239. arrange();
  1240. }
  1241. void
  1242. setup(void) {
  1243. unsigned int i;
  1244. int w;
  1245. XSetWindowAttributes wa;
  1246. /* init screen */
  1247. screen = DefaultScreen(dpy);
  1248. root = RootWindow(dpy, screen);
  1249. initfont(font);
  1250. sx = 0;
  1251. sy = 0;
  1252. sw = DisplayWidth(dpy, screen);
  1253. sh = DisplayHeight(dpy, screen);
  1254. bh = dc.h = dc.font.height + 2;
  1255. lt[0] = &layouts[0];
  1256. lt[1] = &layouts[1 % LENGTH(layouts)];
  1257. updategeom();
  1258. /* init atoms */
  1259. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1260. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1261. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1262. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1263. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1264. /* init cursors */
  1265. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1266. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1267. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1268. /* init appearance */
  1269. dc.norm[ColBorder] = getcolor(normbordercolor);
  1270. dc.norm[ColBG] = getcolor(normbgcolor);
  1271. dc.norm[ColFG] = getcolor(normfgcolor);
  1272. dc.sel[ColBorder] = getcolor(selbordercolor);
  1273. dc.sel[ColBG] = getcolor(selbgcolor);
  1274. dc.sel[ColFG] = getcolor(selfgcolor);
  1275. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1276. dc.gc = XCreateGC(dpy, root, 0, NULL);
  1277. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1278. if(!dc.font.set)
  1279. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1280. /* init bars */
  1281. for(blw = i = 0; LENGTH(layouts) > 1 && i < LENGTH(layouts); i++) {
  1282. w = TEXTW(layouts[i].symbol);
  1283. blw = MAX(blw, w);
  1284. }
  1285. updatebars();
  1286. updatestatus();
  1287. /* EWMH support per view */
  1288. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1289. PropModeReplace, (unsigned char *) netatom, NetLast);
  1290. /* select for events */
  1291. wa.cursor = cursor[CurNormal];
  1292. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
  1293. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
  1294. |PropertyChangeMask;
  1295. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1296. XSelectInput(dpy, root, wa.event_mask);
  1297. grabkeys();
  1298. }
  1299. void
  1300. showhide(Client *c) {
  1301. if(!c)
  1302. return;
  1303. if(ISVISIBLE(c)) { /* show clients top down */
  1304. XMoveWindow(dpy, c->win, c->x, c->y);
  1305. if(!lt[c->mon->sellt]->arrange || c->isfloating)
  1306. resize(c, c->x, c->y, c->w, c->h);
  1307. showhide(c->snext);
  1308. }
  1309. else { /* hide clients bottom up */
  1310. showhide(c->snext);
  1311. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  1312. }
  1313. }
  1314. void
  1315. sigchld(int signal) {
  1316. while(0 < waitpid(-1, NULL, WNOHANG));
  1317. }
  1318. void
  1319. spawn(const Arg *arg) {
  1320. signal(SIGCHLD, sigchld);
  1321. if(fork() == 0) {
  1322. if(dpy)
  1323. close(ConnectionNumber(dpy));
  1324. setsid();
  1325. execvp(((char **)arg->v)[0], (char **)arg->v);
  1326. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1327. perror(" failed");
  1328. exit(0);
  1329. }
  1330. }
  1331. void
  1332. tag(const Arg *arg) {
  1333. if(selmon->sel && arg->ui & TAGMASK) {
  1334. selmon->sel->tags = arg->ui & TAGMASK;
  1335. arrange();
  1336. }
  1337. }
  1338. #ifdef XINERAMA
  1339. void
  1340. tagmon(const Arg *arg) {
  1341. unsigned int i;
  1342. Client *c;
  1343. Monitor *m;
  1344. if(!(c = selmon->sel))
  1345. return;
  1346. for(i = 0, m = mons; m; m = m->next, i++)
  1347. if(i == arg->ui) {
  1348. detach(c);
  1349. detachstack(c);
  1350. c->mon = m;
  1351. attach(c);
  1352. attachstack(c);
  1353. selmon->sel = selmon->stack;
  1354. m->sel = c;
  1355. arrange();
  1356. break;
  1357. }
  1358. }
  1359. #endif /* XINERAMA */
  1360. int
  1361. textnw(const char *text, unsigned int len) {
  1362. XRectangle r;
  1363. if(dc.font.set) {
  1364. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1365. return r.width;
  1366. }
  1367. return XTextWidth(dc.font.xfont, text, len);
  1368. }
  1369. void
  1370. tile(Monitor *m) {
  1371. int x, y, h, w, mw;
  1372. unsigned int i, n;
  1373. Client *c;
  1374. for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  1375. if(n == 0)
  1376. return;
  1377. /* master */
  1378. c = nexttiled(m->clients);
  1379. mw = m->mfact * m->ww;
  1380. resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw);
  1381. if(--n == 0)
  1382. return;
  1383. /* tile stack */
  1384. x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
  1385. y = m->wy;
  1386. w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
  1387. h = m->wh / n;
  1388. if(h < bh)
  1389. h = m->wh;
  1390. for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1391. resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
  1392. ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw));
  1393. if(h != m->wh)
  1394. y = c->y + HEIGHT(c);
  1395. }
  1396. }
  1397. void
  1398. togglebar(const Arg *arg) {
  1399. selmon->showbar = !selmon->showbar;
  1400. updatebarpos(selmon);
  1401. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1402. arrange();
  1403. }
  1404. void
  1405. togglefloating(const Arg *arg) {
  1406. if(!selmon->sel)
  1407. return;
  1408. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1409. if(selmon->sel->isfloating)
  1410. resize(selmon->sel, selmon->sel->x, selmon->sel->y, selmon->sel->w, selmon->sel->h);
  1411. arrange();
  1412. }
  1413. void
  1414. toggletag(const Arg *arg) {
  1415. unsigned int mask;
  1416. if(!selmon->sel)
  1417. return;
  1418. mask = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1419. if(mask) {
  1420. selmon->sel->tags = mask;
  1421. arrange();
  1422. }
  1423. }
  1424. void
  1425. toggleview(const Arg *arg) {
  1426. unsigned int mask = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1427. if(mask) {
  1428. selmon->tagset[selmon->seltags] = mask;
  1429. arrange();
  1430. }
  1431. }
  1432. void
  1433. unmanage(Client *c) {
  1434. XWindowChanges wc;
  1435. wc.border_width = c->oldbw;
  1436. /* The server grab construct avoids race conditions. */
  1437. XGrabServer(dpy);
  1438. XSetErrorHandler(xerrordummy);
  1439. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1440. detach(c);
  1441. detachstack(c);
  1442. if(c->mon->sel == c) {
  1443. c->mon->sel = c->mon->stack;
  1444. focus(NULL);
  1445. }
  1446. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1447. setclientstate(c, WithdrawnState);
  1448. free(c);
  1449. XSync(dpy, False);
  1450. XSetErrorHandler(xerror);
  1451. XUngrabServer(dpy);
  1452. arrange();
  1453. }
  1454. void
  1455. unmapnotify(XEvent *e) {
  1456. Client *c;
  1457. XUnmapEvent *ev = &e->xunmap;
  1458. if((c = getclient(ev->window)))
  1459. unmanage(c);
  1460. }
  1461. void
  1462. updatebars(void) {
  1463. Monitor *m;
  1464. XSetWindowAttributes wa;
  1465. wa.override_redirect = True;
  1466. wa.background_pixmap = ParentRelative;
  1467. wa.event_mask = ButtonPressMask|ExposureMask;
  1468. for(m = mons; m; m = m->next) {
  1469. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  1470. CopyFromParent, DefaultVisual(dpy, screen),
  1471. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1472. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1473. XMapRaised(dpy, m->barwin);
  1474. }
  1475. }
  1476. void
  1477. updatebarpos(Monitor *m) {
  1478. m->wy = m->my;
  1479. m->wh = m->mh;
  1480. if(m->showbar) {
  1481. m->wh -= bh;
  1482. m->by = m->topbar ? m->wy : m->wy + m->wh;
  1483. m->wy = m->topbar ? m->wy + bh : m->wy;
  1484. }
  1485. else
  1486. m->by = -bh;
  1487. }
  1488. void
  1489. updategeom(void) {
  1490. int i, di, n = 1, x, y;
  1491. unsigned int dui;
  1492. Client *c;
  1493. Monitor *newmons = NULL, *m, *tm;
  1494. Window dummy;
  1495. #ifdef XINULATOR
  1496. n = 2;
  1497. #elif defined(XINERAMA)
  1498. XineramaScreenInfo *info = NULL;
  1499. if(XineramaIsActive(dpy))
  1500. info = XineramaQueryScreens(dpy, &n);
  1501. #endif
  1502. /* allocate monitor(s) for the new geometry setup */
  1503. for(i = 0; i < n; i++) {
  1504. m = (Monitor *)malloc(sizeof(Monitor));
  1505. m->next = newmons;
  1506. newmons = m;
  1507. }
  1508. /* initialise monitor(s) */
  1509. #ifdef XINULATOR
  1510. if(1) {
  1511. m = newmons;
  1512. m->screen_number = 0;
  1513. m->wx = sx;
  1514. m->my = m->wy = sy;
  1515. m->ww = sw;
  1516. m->mh = m->wh = sh / 2;
  1517. m = newmons->next;
  1518. m->screen_number = 1;
  1519. m->wx = sx;
  1520. m->my = m->wy = sy + sh / 2;
  1521. m->ww = sw;
  1522. m->mh = m->wh = sh / 2;
  1523. }
  1524. else
  1525. #elif defined(XINERAMA)
  1526. if(XineramaIsActive(dpy)) {
  1527. for(i = 0, m = newmons; m; m = m->next, i++) {
  1528. m->screen_number = info[i].screen_number;
  1529. m->wx = info[i].x_org;
  1530. m->my = m->wy = info[i].y_org;
  1531. m->ww = info[i].width;
  1532. m->mh = m->wh = info[i].height;
  1533. }
  1534. XFree(info);
  1535. }
  1536. else
  1537. #endif
  1538. /* default monitor setup */
  1539. {
  1540. m->screen_number = 0;
  1541. m->wx = sx;
  1542. m->my = m->wy = sy;
  1543. m->ww = sw;
  1544. m->mh = m->wh = sh;
  1545. }
  1546. /* bar geometry setup */
  1547. for(m = newmons; m; m = m->next) {
  1548. /* TODO: consider removing the following values from config.h */
  1549. m->clients = NULL;
  1550. m->sel = NULL;
  1551. m->stack = NULL;
  1552. m->seltags = 0;
  1553. m->sellt = 0;
  1554. m->tagset[0] = m->tagset[1] = 1;
  1555. m->mfact = mfact;
  1556. m->showbar = showbar;
  1557. m->topbar = topbar;
  1558. updatebarpos(m);
  1559. }
  1560. /* reassign left over clients of disappeared monitors */
  1561. for(tm = mons; tm; tm = tm->next) {
  1562. while(tm->clients) {
  1563. c = tm->clients;
  1564. tm->clients = c->next;
  1565. detachstack(c);
  1566. c->mon = newmons;
  1567. attach(c);
  1568. attachstack(c);
  1569. }
  1570. }
  1571. /* select focused monitor */
  1572. selmon = newmons;
  1573. if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui))
  1574. for(m = newmons; m; m = m->next)
  1575. if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh)) {
  1576. selmon = m;
  1577. break;
  1578. }
  1579. /* final assignment of new monitors */
  1580. cleanupmons();
  1581. mons = newmons;
  1582. }
  1583. void
  1584. updatenumlockmask(void) {
  1585. unsigned int i, j;
  1586. XModifierKeymap *modmap;
  1587. numlockmask = 0;
  1588. modmap = XGetModifierMapping(dpy);
  1589. for(i = 0; i < 8; i++)
  1590. for(j = 0; j < modmap->max_keypermod; j++)
  1591. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1592. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1593. numlockmask = (1 << i);
  1594. XFreeModifiermap(modmap);
  1595. }
  1596. void
  1597. updatesizehints(Client *c) {
  1598. long msize;
  1599. XSizeHints size;
  1600. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1601. /* size is uninitialized, ensure that size.flags aren't used */
  1602. size.flags = PSize;
  1603. if(size.flags & PBaseSize) {
  1604. c->basew = size.base_width;
  1605. c->baseh = size.base_height;
  1606. }
  1607. else if(size.flags & PMinSize) {
  1608. c->basew = size.min_width;
  1609. c->baseh = size.min_height;
  1610. }
  1611. else
  1612. c->basew = c->baseh = 0;
  1613. if(size.flags & PResizeInc) {
  1614. c->incw = size.width_inc;
  1615. c->inch = size.height_inc;
  1616. }
  1617. else
  1618. c->incw = c->inch = 0;
  1619. if(size.flags & PMaxSize) {
  1620. c->maxw = size.max_width;
  1621. c->maxh = size.max_height;
  1622. }
  1623. else
  1624. c->maxw = c->maxh = 0;
  1625. if(size.flags & PMinSize) {
  1626. c->minw = size.min_width;
  1627. c->minh = size.min_height;
  1628. }
  1629. else if(size.flags & PBaseSize) {
  1630. c->minw = size.base_width;
  1631. c->minh = size.base_height;
  1632. }
  1633. else
  1634. c->minw = c->minh = 0;
  1635. if(size.flags & PAspect) {
  1636. c->mina = (float)size.min_aspect.y / (float)size.min_aspect.x;
  1637. c->maxa = (float)size.max_aspect.x / (float)size.max_aspect.y;
  1638. }
  1639. else
  1640. c->maxa = c->mina = 0.0;
  1641. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1642. && c->maxw == c->minw && c->maxh == c->minh);
  1643. }
  1644. void
  1645. updatetitle(Client *c) {
  1646. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1647. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1648. }
  1649. void
  1650. updatestatus() {
  1651. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1652. strcpy(stext, "dwm-"VERSION);
  1653. drawbar(selmon);
  1654. }
  1655. void
  1656. updatewmhints(Client *c) {
  1657. XWMHints *wmh;
  1658. if((wmh = XGetWMHints(dpy, c->win))) {
  1659. if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  1660. wmh->flags &= ~XUrgencyHint;
  1661. XSetWMHints(dpy, c->win, wmh);
  1662. }
  1663. else
  1664. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1665. XFree(wmh);
  1666. }
  1667. }
  1668. void
  1669. view(const Arg *arg) {
  1670. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1671. return;
  1672. selmon->seltags ^= 1; /* toggle sel tagset */
  1673. if(arg->ui & TAGMASK)
  1674. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1675. arrange();
  1676. }
  1677. /* There's no way to check accesses to destroyed windows, thus those cases are
  1678. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1679. * default error handler, which may call exit. */
  1680. int
  1681. xerror(Display *dpy, XErrorEvent *ee) {
  1682. if(ee->error_code == BadWindow
  1683. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1684. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1685. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1686. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1687. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1688. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1689. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1690. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1691. return 0;
  1692. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1693. ee->request_code, ee->error_code);
  1694. return xerrorxlib(dpy, ee); /* may call exit */
  1695. }
  1696. int
  1697. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1698. return 0;
  1699. }
  1700. /* Startup Error handler to check if another window manager
  1701. * is already running. */
  1702. int
  1703. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1704. otherwm = True;
  1705. return -1;
  1706. }
  1707. void
  1708. zoom(const Arg *arg) {
  1709. Client *c = selmon->sel;
  1710. if(!lt[selmon->sellt]->arrange || lt[selmon->sellt]->arrange == monocle || (selmon->sel && selmon->sel->isfloating))
  1711. return;
  1712. if(c == nexttiled(selmon->clients))
  1713. if(!c || !(c = nexttiled(c->next)))
  1714. return;
  1715. detach(c);
  1716. attach(c);
  1717. focus(c);
  1718. arrange();
  1719. }
  1720. int
  1721. main(int argc, char *argv[]) {
  1722. if(argc == 2 && !strcmp("-v", argv[1]))
  1723. die("dwm-"VERSION", © 2006-2009 dwm engineers, see LICENSE for details\n");
  1724. else if(argc != 1)
  1725. die("usage: dwm [-v]\n");
  1726. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1727. fputs("warning: no locale support\n", stderr);
  1728. if(!(dpy = XOpenDisplay(NULL)))
  1729. die("dwm: cannot open display\n");
  1730. checkotherwm();
  1731. setup();
  1732. scan();
  1733. run();
  1734. cleanup();
  1735. XCloseDisplay(dpy);
  1736. return 0;
  1737. }