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.

2021 lines
46 KiB

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