Configuration file for DWM on MacBook Air
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.

1711 lines
41 KiB

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