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.

1918 lines
46 KiB

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