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