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.

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