Configuration file for DWM on MacBook Air
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1735 lines
41 KiB

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