Configuration of dwm for Mac Computers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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