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.

1726 lines
41 KiB

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