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.

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