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.

1835 lines
42 KiB

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