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.

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