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.

1755 lines
42 KiB

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