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.

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