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.

1968 lines
45 KiB

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