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.

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