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.

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