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.

1744 lines
42 KiB

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