Configuration file for DWM on MacBook Air
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1890 lines
44 KiB

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