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.

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