Configuration of dwm for Mac Computers
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.

1735 lines
42 KiB

17 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
  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 NOBORDER(x) ((x) - 2 * c->bw)
  56. #define TAGMASK ((int)((1LL << LENGTH(tags)) - 1))
  57. #define TEXTW(x) (textnw(x, strlen(x)) + dc.font.height)
  58. /* enums */
  59. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  60. enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
  61. enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
  62. enum { WMProtocols, WMDelete, WMState, WMLast }; /* default atoms */
  63. enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
  64. ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
  65. typedef union {
  66. int i;
  67. unsigned int ui;
  68. float f;
  69. void *v;
  70. } Arg;
  71. typedef struct {
  72. unsigned int click;
  73. unsigned int mask;
  74. unsigned int button;
  75. void (*func)(const Arg *arg);
  76. const Arg arg;
  77. } Button;
  78. typedef struct Client Client;
  79. struct Client {
  80. char name[256];
  81. float mina, maxa;
  82. int x, y, w, h;
  83. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  84. int bw, oldbw;
  85. unsigned int tags;
  86. Bool isfixed, isfloating, isurgent;
  87. Client *next;
  88. Client *snext;
  89. Window win;
  90. };
  91. typedef struct {
  92. int x, y, w, h;
  93. unsigned long norm[ColLast];
  94. unsigned long sel[ColLast];
  95. Drawable drawable;
  96. GC gc;
  97. struct {
  98. int ascent;
  99. int descent;
  100. int height;
  101. XFontSet set;
  102. XFontStruct *xfont;
  103. } font;
  104. } DC; /* draw context */
  105. typedef struct {
  106. unsigned int mod;
  107. KeySym keysym;
  108. void (*func)(const Arg *);
  109. const Arg arg;
  110. } Key;
  111. typedef struct {
  112. const char *symbol;
  113. void (*arrange)(void);
  114. } Layout;
  115. typedef struct {
  116. const char *class;
  117. const char *instance;
  118. const char *title;
  119. unsigned int tags;
  120. Bool isfloating;
  121. } Rule;
  122. /* function declarations */
  123. static void applyrules(Client *c);
  124. static void arrange(void);
  125. static void attach(Client *c);
  126. static void attachstack(Client *c);
  127. static void buttonpress(XEvent *e);
  128. static void checkotherwm(void);
  129. static void cleanup(void);
  130. static void clearurgent(void);
  131. static void configure(Client *c);
  132. static void configurenotify(XEvent *e);
  133. static void configurerequest(XEvent *e);
  134. static void destroynotify(XEvent *e);
  135. static void detach(Client *c);
  136. static void detachstack(Client *c);
  137. static void die(const char *errstr, ...);
  138. static void drawbar(void);
  139. static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
  140. static void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
  141. static void enternotify(XEvent *e);
  142. static void expose(XEvent *e);
  143. static void focus(Client *c);
  144. static void focusin(XEvent *e);
  145. static void focusstack(const Arg *arg);
  146. static Client *getclient(Window w);
  147. static unsigned long getcolor(const char *colstr);
  148. static long getstate(Window w);
  149. static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  150. static void grabbuttons(Client *c, Bool focused);
  151. static void grabkeys(void);
  152. static void initfont(const char *fontstr);
  153. static Bool isprotodel(Client *c);
  154. static void keypress(XEvent *e);
  155. static void killclient(const Arg *arg);
  156. static void manage(Window w, XWindowAttributes *wa);
  157. static void mappingnotify(XEvent *e);
  158. static void maprequest(XEvent *e);
  159. static void monocle(void);
  160. static void movemouse(const Arg *arg);
  161. static Client *nexttiled(Client *c);
  162. static void propertynotify(XEvent *e);
  163. static void quit(const Arg *arg);
  164. static void resize(Client *c, int x, int y, int w, int h, Bool sizehints);
  165. static void resizemouse(const Arg *arg);
  166. static void restack(void);
  167. static void run(void);
  168. static void scan(void);
  169. static void setclientstate(Client *c, long state);
  170. static void setlayout(const Arg *arg);
  171. static void setmfact(const Arg *arg);
  172. static void setup(void);
  173. static void spawn(const Arg *arg);
  174. static void tag(const Arg *arg);
  175. static int textnw(const char *text, unsigned int len);
  176. static void tile(void);
  177. static void togglebar(const Arg *arg);
  178. static void togglefloating(const Arg *arg);
  179. static void toggletag(const Arg *arg);
  180. static void toggleview(const Arg *arg);
  181. static void unmanage(Client *c);
  182. static void unmapnotify(XEvent *e);
  183. static void updatebar(void);
  184. static void updategeom(void);
  185. static void updatenumlockmask(void);
  186. static void updatesizehints(Client *c);
  187. static void updatetitle(Client *c);
  188. static void updatewmhints(Client *c);
  189. static void view(const Arg *arg);
  190. static int xerror(Display *dpy, XErrorEvent *ee);
  191. static int xerrordummy(Display *dpy, XErrorEvent *ee);
  192. static int xerrorstart(Display *dpy, XErrorEvent *ee);
  193. static void zoom(const Arg *arg);
  194. /* variables */
  195. static char stext[256];
  196. static int screen;
  197. static int sx, sy, sw, sh; /* X display screen geometry x, y, width, height */
  198. static int by, bh, blw; /* bar geometry y, height and layout symbol width */
  199. static int wx, wy, ww, wh; /* window area geometry x, y, width, height, bar excluded */
  200. static unsigned int seltags = 0, sellt = 0;
  201. static int (*xerrorxlib)(Display *, XErrorEvent *);
  202. static unsigned int numlockmask = 0;
  203. static void (*handler[LASTEvent]) (XEvent *) = {
  204. [ButtonPress] = buttonpress,
  205. [ConfigureRequest] = configurerequest,
  206. [ConfigureNotify] = configurenotify,
  207. [DestroyNotify] = destroynotify,
  208. [EnterNotify] = enternotify,
  209. [Expose] = expose,
  210. [FocusIn] = focusin,
  211. [KeyPress] = keypress,
  212. [MappingNotify] = mappingnotify,
  213. [MapRequest] = maprequest,
  214. [PropertyNotify] = propertynotify,
  215. [UnmapNotify] = unmapnotify
  216. };
  217. static Atom wmatom[WMLast], netatom[NetLast];
  218. static Bool otherwm;
  219. static Bool running = True;
  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 - NOBORDER(c->w);
  803. if(c->y + c->h + 2 * c->bw > sy + sh)
  804. c->y = sy + sh - NOBORDER(c->h);
  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, NOBORDER(ww), NOBORDER(wh), 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 - NOBORDER(c->w);
  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 - NOBORDER(c->h);
  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 - NOBORDER(w);
  990. if(y > sy + sh)
  991. y = sh - NOBORDER(h);
  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 - NOBORDER(ocx) + 1, 1);
  1038. nh = MAX(ev.xmotion.y - NOBORDER(ocy) + 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[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1208. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1209. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1210. /* init cursors */
  1211. wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1212. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1213. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1214. /* init appearance */
  1215. dc.norm[ColBorder] = getcolor(normbordercolor);
  1216. dc.norm[ColBG] = getcolor(normbgcolor);
  1217. dc.norm[ColFG] = getcolor(normfgcolor);
  1218. dc.sel[ColBorder] = getcolor(selbordercolor);
  1219. dc.sel[ColBG] = getcolor(selbgcolor);
  1220. dc.sel[ColFG] = getcolor(selfgcolor);
  1221. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1222. dc.gc = XCreateGC(dpy, root, 0, 0);
  1223. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1224. if(!dc.font.set)
  1225. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1226. /* init bar */
  1227. for(blw = i = 0; LENGTH(layouts) > 1 && i < LENGTH(layouts); i++) {
  1228. w = TEXTW(layouts[i].symbol);
  1229. blw = MAX(blw, w);
  1230. }
  1231. wa.override_redirect = 1;
  1232. wa.background_pixmap = ParentRelative;
  1233. wa.event_mask = ButtonPressMask|ExposureMask;
  1234. barwin = XCreateWindow(dpy, root, wx, by, ww, bh, 0, DefaultDepth(dpy, screen),
  1235. CopyFromParent, DefaultVisual(dpy, screen),
  1236. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1237. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  1238. XMapRaised(dpy, barwin);
  1239. strcpy(stext, "dwm-"VERSION);
  1240. drawbar();
  1241. /* EWMH support per view */
  1242. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1243. PropModeReplace, (unsigned char *) netatom, NetLast);
  1244. /* select for events */
  1245. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
  1246. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask;
  1247. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1248. XSelectInput(dpy, root, wa.event_mask);
  1249. grabkeys();
  1250. }
  1251. void
  1252. spawn(const Arg *arg) {
  1253. /* The double-fork construct avoids zombie processes and keeps the code
  1254. * clean from stupid signal handlers. */
  1255. if(fork() == 0) {
  1256. if(fork() == 0) {
  1257. if(dpy)
  1258. close(ConnectionNumber(dpy));
  1259. setsid();
  1260. execvp(((char **)arg->v)[0], (char **)arg->v);
  1261. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1262. perror(" failed");
  1263. }
  1264. exit(0);
  1265. }
  1266. wait(0);
  1267. }
  1268. void
  1269. tag(const Arg *arg) {
  1270. if(sel && arg->ui & TAGMASK) {
  1271. sel->tags = arg->ui & TAGMASK;
  1272. arrange();
  1273. }
  1274. }
  1275. int
  1276. textnw(const char *text, unsigned int len) {
  1277. XRectangle r;
  1278. if(dc.font.set) {
  1279. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1280. return r.width;
  1281. }
  1282. return XTextWidth(dc.font.xfont, text, len);
  1283. }
  1284. void
  1285. tile(void) {
  1286. int x, y, h, w, mw;
  1287. unsigned int i, n;
  1288. Client *c;
  1289. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next), n++);
  1290. if(n == 0)
  1291. return;
  1292. /* master */
  1293. c = nexttiled(clients);
  1294. mw = mfact * ww;
  1295. resize(c, wx, wy, NOBORDER(n == 1 ? ww : mw), NOBORDER(wh), resizehints);
  1296. if(--n == 0)
  1297. return;
  1298. /* tile stack */
  1299. x = (wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : wx + mw;
  1300. y = wy;
  1301. w = (wx + mw > c->x + c->w) ? wx + ww - x : ww - mw;
  1302. h = wh / n;
  1303. if(h < bh)
  1304. h = wh;
  1305. for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1306. resize(c, x, y, NOBORDER(w), /* remainder */ ((i + 1 == n)
  1307. ? NOBORDER(wy + wh) - y : h), resizehints);
  1308. if(h != wh)
  1309. y = c->y + c->h + 2 * c->bw;
  1310. }
  1311. }
  1312. void
  1313. togglebar(const Arg *arg) {
  1314. showbar = !showbar;
  1315. updategeom();
  1316. updatebar();
  1317. arrange();
  1318. }
  1319. void
  1320. togglefloating(const Arg *arg) {
  1321. if(!sel)
  1322. return;
  1323. sel->isfloating = !sel->isfloating || sel->isfixed;
  1324. if(sel->isfloating)
  1325. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1326. arrange();
  1327. }
  1328. void
  1329. toggletag(const Arg *arg) {
  1330. unsigned int mask;
  1331. if (!sel)
  1332. return;
  1333. mask = sel->tags ^ (arg->ui & TAGMASK);
  1334. if(sel && mask) {
  1335. sel->tags = mask;
  1336. arrange();
  1337. }
  1338. }
  1339. void
  1340. toggleview(const Arg *arg) {
  1341. unsigned int mask = tagset[seltags] ^ (arg->ui & TAGMASK);
  1342. if(mask) {
  1343. tagset[seltags] = mask;
  1344. clearurgent();
  1345. arrange();
  1346. }
  1347. }
  1348. void
  1349. unmanage(Client *c) {
  1350. XWindowChanges wc;
  1351. wc.border_width = c->oldbw;
  1352. /* The server grab construct avoids race conditions. */
  1353. XGrabServer(dpy);
  1354. XSetErrorHandler(xerrordummy);
  1355. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1356. detach(c);
  1357. detachstack(c);
  1358. if(sel == c)
  1359. focus(NULL);
  1360. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1361. setclientstate(c, WithdrawnState);
  1362. free(c);
  1363. XSync(dpy, False);
  1364. XSetErrorHandler(xerror);
  1365. XUngrabServer(dpy);
  1366. arrange();
  1367. }
  1368. void
  1369. unmapnotify(XEvent *e) {
  1370. Client *c;
  1371. XUnmapEvent *ev = &e->xunmap;
  1372. if((c = getclient(ev->window)))
  1373. unmanage(c);
  1374. }
  1375. void
  1376. updatebar(void) {
  1377. if(dc.drawable != 0)
  1378. XFreePixmap(dpy, dc.drawable);
  1379. dc.drawable = XCreatePixmap(dpy, root, ww, bh, DefaultDepth(dpy, screen));
  1380. XMoveResizeWindow(dpy, barwin, wx, by, ww, bh);
  1381. }
  1382. void
  1383. updategeom(void) {
  1384. #ifdef XINERAMA
  1385. int n, i = 0;
  1386. XineramaScreenInfo *info = NULL;
  1387. /* window area geometry */
  1388. if(XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
  1389. if(n > 1) {
  1390. int di, x, y;
  1391. unsigned int dui;
  1392. Window dummy;
  1393. if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui))
  1394. for(i = 0; i < n; i++)
  1395. if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
  1396. break;
  1397. }
  1398. wx = info[i].x_org;
  1399. wy = showbar && topbar ? info[i].y_org + bh : info[i].y_org;
  1400. ww = info[i].width;
  1401. wh = showbar ? info[i].height - bh : info[i].height;
  1402. XFree(info);
  1403. }
  1404. else
  1405. #endif
  1406. {
  1407. wx = sx;
  1408. wy = showbar && topbar ? sy + bh : sy;
  1409. ww = sw;
  1410. wh = showbar ? sh - bh : sh;
  1411. }
  1412. /* bar position */
  1413. by = showbar ? (topbar ? wy - bh : wy + wh) : -bh;
  1414. }
  1415. void
  1416. updatenumlockmask(void) {
  1417. unsigned int i, j;
  1418. XModifierKeymap *modmap;
  1419. numlockmask = 0;
  1420. modmap = XGetModifierMapping(dpy);
  1421. for(i = 0; i < 8; i++)
  1422. for(j = 0; j < modmap->max_keypermod; j++)
  1423. if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
  1424. numlockmask = (1 << i);
  1425. XFreeModifiermap(modmap);
  1426. }
  1427. void
  1428. updatesizehints(Client *c) {
  1429. long msize;
  1430. XSizeHints size;
  1431. XGetWMNormalHints(dpy, c->win, &size, &msize);
  1432. if(size.flags & PBaseSize) {
  1433. c->basew = size.base_width;
  1434. c->baseh = size.base_height;
  1435. }
  1436. else if(size.flags & PMinSize) {
  1437. c->basew = size.min_width;
  1438. c->baseh = size.min_height;
  1439. }
  1440. else
  1441. c->basew = c->baseh = 0;
  1442. if(size.flags & PResizeInc) {
  1443. c->incw = size.width_inc;
  1444. c->inch = size.height_inc;
  1445. }
  1446. else
  1447. c->incw = c->inch = 0;
  1448. if(size.flags & PMaxSize) {
  1449. c->maxw = size.max_width;
  1450. c->maxh = size.max_height;
  1451. }
  1452. else
  1453. c->maxw = c->maxh = 0;
  1454. if(size.flags & PMinSize) {
  1455. c->minw = size.min_width;
  1456. c->minh = size.min_height;
  1457. }
  1458. else if(size.flags & PBaseSize) {
  1459. c->minw = size.base_width;
  1460. c->minh = size.base_height;
  1461. }
  1462. else
  1463. c->minw = c->minh = 0;
  1464. if(size.flags & PAspect) {
  1465. c->mina = (float)size.min_aspect.y / (float)size.min_aspect.x;
  1466. c->maxa = (float)size.max_aspect.x / (float)size.max_aspect.y;
  1467. }
  1468. else
  1469. c->maxa = c->mina = 0.0;
  1470. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1471. && c->maxw == c->minw && c->maxh == c->minh);
  1472. }
  1473. void
  1474. updatetitle(Client *c) {
  1475. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1476. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1477. }
  1478. void
  1479. updatewmhints(Client *c) {
  1480. XWMHints *wmh;
  1481. if((wmh = XGetWMHints(dpy, c->win))) {
  1482. if(ISVISIBLE(c) && wmh->flags & XUrgencyHint) {
  1483. wmh->flags &= ~XUrgencyHint;
  1484. XSetWMHints(dpy, c->win, wmh);
  1485. }
  1486. else
  1487. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1488. XFree(wmh);
  1489. }
  1490. }
  1491. void
  1492. view(const Arg *arg) {
  1493. if((arg->ui & TAGMASK) == tagset[seltags])
  1494. return;
  1495. seltags ^= 1; /* toggle sel tagset */
  1496. if(arg->ui & TAGMASK)
  1497. tagset[seltags] = arg->ui & TAGMASK;
  1498. clearurgent();
  1499. arrange();
  1500. }
  1501. /* There's no way to check accesses to destroyed windows, thus those cases are
  1502. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1503. * default error handler, which may call exit. */
  1504. int
  1505. xerror(Display *dpy, XErrorEvent *ee) {
  1506. if(ee->error_code == BadWindow
  1507. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1508. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1509. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1510. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1511. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1512. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1513. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1514. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1515. return 0;
  1516. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1517. ee->request_code, ee->error_code);
  1518. return xerrorxlib(dpy, ee); /* may call exit */
  1519. }
  1520. int
  1521. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1522. return 0;
  1523. }
  1524. /* Startup Error handler to check if another window manager
  1525. * is already running. */
  1526. int
  1527. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1528. otherwm = True;
  1529. return -1;
  1530. }
  1531. void
  1532. zoom(const Arg *arg) {
  1533. Client *c = sel;
  1534. if(!lt[sellt]->arrange || lt[sellt]->arrange == monocle || (sel && sel->isfloating))
  1535. return;
  1536. if(c == nexttiled(clients))
  1537. if(!c || !(c = nexttiled(c->next)))
  1538. return;
  1539. detach(c);
  1540. attach(c);
  1541. focus(c);
  1542. arrange();
  1543. }
  1544. int
  1545. main(int argc, char *argv[]) {
  1546. if(argc == 2 && !strcmp("-v", argv[1]))
  1547. die("dwm-"VERSION", © 2006-2008 dwm engineers, see LICENSE for details\n");
  1548. else if(argc != 1)
  1549. die("usage: dwm [-v]\n");
  1550. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1551. fprintf(stderr, "warning: no locale support\n");
  1552. if(!(dpy = XOpenDisplay(0)))
  1553. die("dwm: cannot open display\n");
  1554. checkotherwm();
  1555. setup();
  1556. scan();
  1557. run();
  1558. cleanup();
  1559. XCloseDisplay(dpy);
  1560. return 0;
  1561. }