Configuration of dwm for Mac Computers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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