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.

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