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.

1742 lines
42 KiB

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