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.

1722 lines
41 KiB

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