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.

1891 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(barwin == ev->window) {
  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((floating == layout->arrange) || 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(0 == ev->count) {
  616. if(barwin == ev->window)
  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 || 0 == size)
  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. {
  727. strncpy(text, *list, size - 1);
  728. XFreeStringList(list);
  729. }
  730. }
  731. text[size - 1] = '\0';
  732. XFree(name.value);
  733. return True;
  734. }
  735. void
  736. grabbuttons(Client *c, Bool focused) {
  737. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  738. if(focused) {
  739. XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  740. GrabModeAsync, GrabModeSync, None, None);
  741. XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
  742. GrabModeAsync, GrabModeSync, None, None);
  743. XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  744. GrabModeAsync, GrabModeSync, None, None);
  745. XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  746. GrabModeAsync, GrabModeSync, None, None);
  747. XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  748. GrabModeAsync, GrabModeSync, None, None);
  749. XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
  750. GrabModeAsync, GrabModeSync, None, None);
  751. XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  752. GrabModeAsync, GrabModeSync, None, None);
  753. XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  754. GrabModeAsync, GrabModeSync, None, None);
  755. XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  756. GrabModeAsync, GrabModeSync, None, None);
  757. XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
  758. GrabModeAsync, GrabModeSync, None, None);
  759. XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  760. GrabModeAsync, GrabModeSync, None, None);
  761. XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  762. GrabModeAsync, GrabModeSync, None, None);
  763. }
  764. else
  765. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
  766. GrabModeAsync, GrabModeSync, None, None);
  767. }
  768. unsigned int
  769. idxoftag(const char *tag) {
  770. unsigned int i;
  771. for(i = 0; (i < LENGTH(tags)) && (tags[i] != tag); i++);
  772. return (i < LENGTH(tags)) ? i : 0;
  773. }
  774. void
  775. initfont(const char *fontstr) {
  776. char *def, **missing;
  777. int i, n;
  778. missing = NULL;
  779. if(dc.font.set)
  780. XFreeFontSet(dpy, dc.font.set);
  781. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  782. if(missing) {
  783. while(n--)
  784. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  785. XFreeStringList(missing);
  786. }
  787. if(dc.font.set) {
  788. XFontSetExtents *font_extents;
  789. XFontStruct **xfonts;
  790. char **font_names;
  791. dc.font.ascent = dc.font.descent = 0;
  792. font_extents = XExtentsOfFontSet(dc.font.set);
  793. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  794. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  795. if(dc.font.ascent < (*xfonts)->ascent)
  796. dc.font.ascent = (*xfonts)->ascent;
  797. if(dc.font.descent < (*xfonts)->descent)
  798. dc.font.descent = (*xfonts)->descent;
  799. xfonts++;
  800. }
  801. }
  802. else {
  803. if(dc.font.xfont)
  804. XFreeFont(dpy, dc.font.xfont);
  805. dc.font.xfont = NULL;
  806. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  807. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  808. eprint("error, cannot load font: '%s'\n", fontstr);
  809. dc.font.ascent = dc.font.xfont->ascent;
  810. dc.font.descent = dc.font.xfont->descent;
  811. }
  812. dc.font.height = dc.font.ascent + dc.font.descent;
  813. }
  814. Bool
  815. isoccupied(unsigned int t) {
  816. Client *c;
  817. for(c = clients; c; c = c->next)
  818. if(c->tags[t])
  819. return True;
  820. return False;
  821. }
  822. Bool
  823. isprotodel(Client *c) {
  824. int i, n;
  825. Atom *protocols;
  826. Bool ret = False;
  827. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  828. for(i = 0; !ret && i < n; i++)
  829. if(protocols[i] == wmatom[WMDelete])
  830. ret = True;
  831. XFree(protocols);
  832. }
  833. return ret;
  834. }
  835. Bool
  836. isvisible(Client *c) {
  837. unsigned int i;
  838. for(i = 0; i < LENGTH(tags); i++)
  839. if(c->tags[i] && seltags[i])
  840. return True;
  841. return False;
  842. }
  843. void
  844. keypress(XEvent *e) {
  845. KEYS
  846. unsigned int i;
  847. KeyCode code;
  848. KeySym keysym;
  849. XKeyEvent *ev;
  850. if(!e) { /* grabkeys */
  851. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  852. for(i = 0; i < LENGTH(keys); i++) {
  853. code = XKeysymToKeycode(dpy, keys[i].keysym);
  854. XGrabKey(dpy, code, keys[i].mod, root, True,
  855. GrabModeAsync, GrabModeAsync);
  856. XGrabKey(dpy, code, keys[i].mod | LockMask, root, True,
  857. GrabModeAsync, GrabModeAsync);
  858. XGrabKey(dpy, code, keys[i].mod | numlockmask, root, True,
  859. GrabModeAsync, GrabModeAsync);
  860. XGrabKey(dpy, code, keys[i].mod | numlockmask | LockMask, root, True,
  861. GrabModeAsync, GrabModeAsync);
  862. }
  863. return;
  864. }
  865. ev = &e->xkey;
  866. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  867. for(i = 0; i < LENGTH(keys); i++)
  868. if(keysym == keys[i].keysym
  869. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state))
  870. {
  871. if(keys[i].func)
  872. keys[i].func(keys[i].arg);
  873. }
  874. }
  875. void
  876. killclient(const char *arg) {
  877. XEvent ev;
  878. if(!sel)
  879. return;
  880. if(isprotodel(sel)) {
  881. ev.type = ClientMessage;
  882. ev.xclient.window = sel->win;
  883. ev.xclient.message_type = wmatom[WMProtocols];
  884. ev.xclient.format = 32;
  885. ev.xclient.data.l[0] = wmatom[WMDelete];
  886. ev.xclient.data.l[1] = CurrentTime;
  887. XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
  888. }
  889. else
  890. XKillClient(dpy, sel->win);
  891. }
  892. void
  893. leavenotify(XEvent *e) {
  894. XCrossingEvent *ev = &e->xcrossing;
  895. if((ev->window == root) && !ev->same_screen) {
  896. selscreen = False;
  897. focus(NULL);
  898. }
  899. }
  900. void
  901. manage(Window w, XWindowAttributes *wa) {
  902. Client *c, *t = NULL;
  903. Window trans;
  904. Status rettrans;
  905. XWindowChanges wc;
  906. c = emallocz(sizeof(Client));
  907. c->tags = emallocz(sizeof seltags);
  908. c->win = w;
  909. c->x = wa->x;
  910. c->y = wa->y;
  911. c->w = wa->width;
  912. c->h = wa->height;
  913. c->oldborder = wa->border_width;
  914. if(c->w == sw && c->h == sh) {
  915. c->x = sx;
  916. c->y = sy;
  917. c->border = wa->border_width;
  918. }
  919. else {
  920. if(c->x + c->w + 2 * c->border > wax + waw)
  921. c->x = wax + waw - c->w - 2 * c->border;
  922. if(c->y + c->h + 2 * c->border > way + wah)
  923. c->y = way + wah - c->h - 2 * c->border;
  924. if(c->x < wax)
  925. c->x = wax;
  926. if(c->y < way)
  927. c->y = way;
  928. c->border = BORDERPX;
  929. }
  930. wc.border_width = c->border;
  931. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  932. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  933. configure(c); /* propagates border_width, if size doesn't change */
  934. updatesizehints(c);
  935. XSelectInput(dpy, w,
  936. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  937. grabbuttons(c, False);
  938. updatetitle(c);
  939. if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
  940. for(t = clients; t && t->win != trans; t = t->next);
  941. if(t)
  942. memcpy(c->tags, t->tags, sizeof seltags);
  943. applyrules(c);
  944. if(!c->isfloating)
  945. c->isfloating = (rettrans == Success) || c->isfixed;
  946. attach(c);
  947. attachstack(c);
  948. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
  949. ban(c);
  950. XMapWindow(dpy, c->win);
  951. setclientstate(c, NormalState);
  952. arrange();
  953. }
  954. void
  955. mappingnotify(XEvent *e) {
  956. XMappingEvent *ev = &e->xmapping;
  957. XRefreshKeyboardMapping(ev);
  958. if(ev->request == MappingKeyboard)
  959. keypress(NULL);
  960. }
  961. void
  962. maprequest(XEvent *e) {
  963. static XWindowAttributes wa;
  964. XMapRequestEvent *ev = &e->xmaprequest;
  965. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  966. return;
  967. if(wa.override_redirect)
  968. return;
  969. if(!getclient(ev->window))
  970. manage(ev->window, &wa);
  971. }
  972. void
  973. movemouse(Client *c) {
  974. int x1, y1, ocx, ocy, di, nx, ny;
  975. unsigned int dui;
  976. Window dummy;
  977. XEvent ev;
  978. ocx = nx = c->x;
  979. ocy = ny = c->y;
  980. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  981. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  982. return;
  983. c->ismax = False;
  984. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  985. for(;;) {
  986. XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
  987. switch (ev.type) {
  988. case ButtonRelease:
  989. XUngrabPointer(dpy, CurrentTime);
  990. return;
  991. case ConfigureRequest:
  992. case Expose:
  993. case MapRequest:
  994. handler[ev.type](&ev);
  995. break;
  996. case MotionNotify:
  997. XSync(dpy, False);
  998. nx = ocx + (ev.xmotion.x - x1);
  999. ny = ocy + (ev.xmotion.y - y1);
  1000. if(abs(wax + nx) < SNAP)
  1001. nx = wax;
  1002. else if(abs((wax + waw) - (nx + c->w + 2 * c->border)) < SNAP)
  1003. nx = wax + waw - c->w - 2 * c->border;
  1004. if(abs(way - ny) < SNAP)
  1005. ny = way;
  1006. else if(abs((way + wah) - (ny + c->h + 2 * c->border)) < SNAP)
  1007. ny = way + wah - c->h - 2 * c->border;
  1008. resize(c, nx, ny, c->w, c->h, False);
  1009. break;
  1010. }
  1011. }
  1012. }
  1013. Client *
  1014. nexttiled(Client *c) {
  1015. for(; c && (c->isfloating || !isvisible(c)); c = c->next);
  1016. return c;
  1017. }
  1018. void
  1019. propertynotify(XEvent *e) {
  1020. Client *c;
  1021. Window trans;
  1022. XPropertyEvent *ev = &e->xproperty;
  1023. if(ev->state == PropertyDelete)
  1024. return; /* ignore */
  1025. if((c = getclient(ev->window))) {
  1026. switch (ev->atom) {
  1027. default: break;
  1028. case XA_WM_TRANSIENT_FOR:
  1029. XGetTransientForHint(dpy, c->win, &trans);
  1030. if(!c->isfloating && (c->isfloating = (NULL != getclient(trans))))
  1031. arrange();
  1032. break;
  1033. case XA_WM_NORMAL_HINTS:
  1034. updatesizehints(c);
  1035. break;
  1036. }
  1037. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1038. updatetitle(c);
  1039. if(c == sel)
  1040. drawbar();
  1041. }
  1042. }
  1043. }
  1044. void
  1045. quit(const char *arg) {
  1046. readin = running = False;
  1047. }
  1048. void
  1049. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  1050. XWindowChanges wc;
  1051. if(sizehints) {
  1052. /* set minimum possible */
  1053. if (w < 1)
  1054. w = 1;
  1055. if (h < 1)
  1056. h = 1;
  1057. /* temporarily remove base dimensions */
  1058. w -= c->basew;
  1059. h -= c->baseh;
  1060. /* adjust for aspect limits */
  1061. if (c->minay > 0 && c->maxay > 0 && c->minax > 0 && c->maxax > 0) {
  1062. if (w * c->maxay > h * c->maxax)
  1063. w = h * c->maxax / c->maxay;
  1064. else if (w * c->minay < h * c->minax)
  1065. h = w * c->minay / c->minax;
  1066. }
  1067. /* adjust for increment value */
  1068. if(c->incw)
  1069. w -= w % c->incw;
  1070. if(c->inch)
  1071. h -= h % c->inch;
  1072. /* restore base dimensions */
  1073. w += c->basew;
  1074. h += c->baseh;
  1075. if(c->minw > 0 && w < c->minw)
  1076. w = c->minw;
  1077. if(c->minh > 0 && h < c->minh)
  1078. h = c->minh;
  1079. if(c->maxw > 0 && w > c->maxw)
  1080. w = c->maxw;
  1081. if(c->maxh > 0 && h > c->maxh)
  1082. h = c->maxh;
  1083. }
  1084. if(w <= 0 || h <= 0)
  1085. return;
  1086. /* offscreen appearance fixes */
  1087. if(x > sw)
  1088. x = sw - w - 2 * c->border;
  1089. if(y > sh)
  1090. y = sh - h - 2 * c->border;
  1091. if(x + w + 2 * c->border < sx)
  1092. x = sx;
  1093. if(y + h + 2 * c->border < sy)
  1094. y = sy;
  1095. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  1096. c->x = wc.x = x;
  1097. c->y = wc.y = y;
  1098. c->w = wc.width = w;
  1099. c->h = wc.height = h;
  1100. wc.border_width = c->border;
  1101. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  1102. configure(c);
  1103. XSync(dpy, False);
  1104. }
  1105. }
  1106. void
  1107. resizemouse(Client *c) {
  1108. int ocx, ocy;
  1109. int nw, nh;
  1110. XEvent ev;
  1111. ocx = c->x;
  1112. ocy = c->y;
  1113. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1114. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1115. return;
  1116. c->ismax = False;
  1117. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
  1118. for(;;) {
  1119. XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
  1120. switch(ev.type) {
  1121. case ButtonRelease:
  1122. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
  1123. c->w + c->border - 1, c->h + c->border - 1);
  1124. XUngrabPointer(dpy, CurrentTime);
  1125. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1126. return;
  1127. case ConfigureRequest:
  1128. case Expose:
  1129. case MapRequest:
  1130. handler[ev.type](&ev);
  1131. break;
  1132. case MotionNotify:
  1133. XSync(dpy, False);
  1134. if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
  1135. nw = 1;
  1136. if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
  1137. nh = 1;
  1138. resize(c, c->x, c->y, nw, nh, True);
  1139. break;
  1140. }
  1141. }
  1142. }
  1143. void
  1144. restack(void) {
  1145. Client *c;
  1146. XEvent ev;
  1147. XWindowChanges wc;
  1148. drawbar();
  1149. if(!sel)
  1150. return;
  1151. if(sel->isfloating || (floating == layout->arrange))
  1152. XRaiseWindow(dpy, sel->win);
  1153. if(floating != layout->arrange) {
  1154. wc.stack_mode = Below;
  1155. wc.sibling = barwin;
  1156. if(!sel->isfloating) {
  1157. XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc);
  1158. wc.sibling = sel->win;
  1159. }
  1160. for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
  1161. if(c == sel)
  1162. continue;
  1163. XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
  1164. wc.sibling = c->win;
  1165. }
  1166. }
  1167. XSync(dpy, False);
  1168. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1169. }
  1170. void
  1171. run(void) {
  1172. char *p;
  1173. fd_set rd;
  1174. int r, xfd;
  1175. unsigned int len, offset;
  1176. XEvent ev;
  1177. /* main event loop, also reads status text from stdin */
  1178. XSync(dpy, False);
  1179. xfd = ConnectionNumber(dpy);
  1180. readin = True;
  1181. offset = 0;
  1182. len = sizeof stext - 1;
  1183. stext[len] = '\0'; /* 0-terminator is never touched */
  1184. while(running) {
  1185. FD_ZERO(&rd);
  1186. if(readin)
  1187. FD_SET(STDIN_FILENO, &rd);
  1188. FD_SET(xfd, &rd);
  1189. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1190. if(errno == EINTR)
  1191. continue;
  1192. eprint("select failed\n");
  1193. }
  1194. if(FD_ISSET(STDIN_FILENO, &rd)) {
  1195. switch((r = read(STDIN_FILENO, stext + offset, len - offset))) {
  1196. case -1:
  1197. strncpy(stext, strerror(errno), len);
  1198. readin = False;
  1199. break;
  1200. case 0:
  1201. strncpy(stext, "EOF", 4);
  1202. readin = False;
  1203. break;
  1204. default:
  1205. stext[offset + r] = '\0';
  1206. for(p = stext; *p && *p != '\n'; p++);
  1207. if(*p == '\n') {
  1208. *p = '\0';
  1209. offset = 0;
  1210. }
  1211. else
  1212. offset = (offset + r < len - 1) ? offset + r : 0;
  1213. }
  1214. drawbar();
  1215. }
  1216. while(XPending(dpy)) {
  1217. XNextEvent(dpy, &ev);
  1218. if(handler[ev.type])
  1219. (handler[ev.type])(&ev); /* call handler */
  1220. }
  1221. }
  1222. }
  1223. void
  1224. scan(void) {
  1225. unsigned int i, num;
  1226. Window *wins, d1, d2;
  1227. XWindowAttributes wa;
  1228. wins = NULL;
  1229. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1230. for(i = 0; i < num; i++) {
  1231. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1232. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1233. continue;
  1234. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1235. manage(wins[i], &wa);
  1236. }
  1237. for(i = 0; i < num; i++) { /* now the transients */
  1238. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1239. continue;
  1240. if(XGetTransientForHint(dpy, wins[i], &d1)
  1241. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1242. manage(wins[i], &wa);
  1243. }
  1244. }
  1245. if(wins)
  1246. XFree(wins);
  1247. }
  1248. void
  1249. setclientstate(Client *c, long state) {
  1250. long data[] = {state, None};
  1251. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1252. PropModeReplace, (unsigned char *)data, 2);
  1253. }
  1254. void
  1255. setlayout(const char *arg) {
  1256. unsigned int i;
  1257. if(!arg) {
  1258. if(++layout == &layouts[LENGTH(layouts)])
  1259. layout = &layouts[0];
  1260. }
  1261. else {
  1262. for(i = 0; i < LENGTH(layouts); i++)
  1263. if(!strcmp(arg, layouts[i].symbol))
  1264. break;
  1265. if(i == LENGTH(layouts))
  1266. return;
  1267. layout = &layouts[i];
  1268. }
  1269. if(sel)
  1270. arrange();
  1271. else
  1272. drawbar();
  1273. }
  1274. void
  1275. setmwfact(const char *arg) {
  1276. double delta;
  1277. if(!domwfact)
  1278. return;
  1279. /* arg handling, manipulate mwfact */
  1280. if(NULL == arg)
  1281. mwfact = MWFACT;
  1282. else if(1 == sscanf(arg, "%lf", &delta)) {
  1283. if(arg[0] == '+' || arg[0] == '-')
  1284. mwfact += delta;
  1285. else
  1286. mwfact = delta;
  1287. if(mwfact < 0.1)
  1288. mwfact = 0.1;
  1289. else if(mwfact > 0.9)
  1290. mwfact = 0.9;
  1291. }
  1292. arrange();
  1293. }
  1294. void
  1295. setup(void) {
  1296. int d;
  1297. unsigned int i, j, mask;
  1298. Window w;
  1299. XModifierKeymap *modmap;
  1300. XSetWindowAttributes wa;
  1301. /* init atoms */
  1302. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1303. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1304. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1305. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1306. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1307. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1308. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1309. PropModeReplace, (unsigned char *) netatom, NetLast);
  1310. /* init cursors */
  1311. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1312. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1313. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1314. /* init geometry */
  1315. sx = sy = 0;
  1316. sw = DisplayWidth(dpy, screen);
  1317. sh = DisplayHeight(dpy, screen);
  1318. /* init modifier map */
  1319. modmap = XGetModifierMapping(dpy);
  1320. for(i = 0; i < 8; i++)
  1321. for(j = 0; j < modmap->max_keypermod; j++) {
  1322. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1323. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1324. numlockmask = (1 << i);
  1325. }
  1326. XFreeModifiermap(modmap);
  1327. /* select for events */
  1328. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  1329. | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
  1330. wa.cursor = cursor[CurNormal];
  1331. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  1332. XSelectInput(dpy, root, wa.event_mask);
  1333. /* grab keys */
  1334. keypress(NULL);
  1335. /* init tags */
  1336. compileregs();
  1337. /* init appearance */
  1338. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  1339. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  1340. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  1341. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  1342. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  1343. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  1344. initfont(FONT);
  1345. dc.h = bh = dc.font.height + 2;
  1346. /* init layouts */
  1347. mwfact = MWFACT;
  1348. layout = &layouts[0];
  1349. for(blw = i = 0; i < LENGTH(layouts); i++) {
  1350. j = textw(layouts[i].symbol);
  1351. if(j > blw)
  1352. blw = j;
  1353. }
  1354. /* init bar */
  1355. bpos = BARPOS;
  1356. wa.override_redirect = 1;
  1357. wa.background_pixmap = ParentRelative;
  1358. wa.event_mask = ButtonPressMask | ExposureMask;
  1359. barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
  1360. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  1361. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  1362. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  1363. updatebarpos();
  1364. XMapRaised(dpy, barwin);
  1365. strcpy(stext, "dwm-"VERSION);
  1366. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  1367. dc.gc = XCreateGC(dpy, root, 0, 0);
  1368. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1369. if(!dc.font.set)
  1370. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1371. /* multihead support */
  1372. selscreen = XQueryPointer(dpy, root, &w, &w, &d, &d, &d, &d, &mask);
  1373. }
  1374. void
  1375. spawn(const char *arg) {
  1376. static char *shell = NULL;
  1377. if(!shell && !(shell = getenv("SHELL")))
  1378. shell = "/bin/sh";
  1379. if(!arg)
  1380. return;
  1381. /* The double-fork construct avoids zombie processes and keeps the code
  1382. * clean from stupid signal handlers. */
  1383. if(0 == fork()) {
  1384. if(0 == fork()) {
  1385. if(dpy)
  1386. close(ConnectionNumber(dpy));
  1387. setsid();
  1388. execl(shell, shell, "-c", arg, (char *)NULL);
  1389. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
  1390. perror(" failed");
  1391. }
  1392. exit(0);
  1393. }
  1394. wait(0);
  1395. }
  1396. void
  1397. tag(const char *arg) {
  1398. unsigned int i;
  1399. if(!sel)
  1400. return;
  1401. for(i = 0; i < LENGTH(tags); i++)
  1402. sel->tags[i] = (NULL == arg);
  1403. sel->tags[idxoftag(arg)] = True;
  1404. arrange();
  1405. }
  1406. unsigned int
  1407. textnw(const char *text, unsigned int len) {
  1408. XRectangle r;
  1409. if(dc.font.set) {
  1410. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1411. return r.width;
  1412. }
  1413. return XTextWidth(dc.font.xfont, text, len);
  1414. }
  1415. unsigned int
  1416. textw(const char *text) {
  1417. return textnw(text, strlen(text)) + dc.font.height;
  1418. }
  1419. void
  1420. tile(void) {
  1421. unsigned int i, n, nx, ny, nw, nh, mw, th;
  1422. Client *c, *mc;
  1423. domwfact = dozoom = True;
  1424. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
  1425. n++;
  1426. /* window geoms */
  1427. mw = (n == 1) ? waw : mwfact * waw;
  1428. th = (n > 1) ? wah / (n - 1) : 0;
  1429. if(n > 1 && th < bh)
  1430. th = wah;
  1431. nx = wax;
  1432. ny = way;
  1433. nw = 0; /* gcc stupidity requires this */
  1434. for(i = 0, c = mc = nexttiled(clients); c; c = nexttiled(c->next), i++) {
  1435. c->ismax = False;
  1436. if(0 == i) { /* master */
  1437. nw = mw - 2 * c->border;
  1438. nh = wah - 2 * c->border;
  1439. }
  1440. else { /* tile window */
  1441. if(i == 1) {
  1442. ny = way;
  1443. nx += mc->w + 2 * mc->border;
  1444. nw = waw - nx - 2 * c->border;
  1445. }
  1446. if(i + 1 == n) /* remainder */
  1447. nh = (way + wah) - ny - 2 * c->border;
  1448. else
  1449. nh = th - 2 * c->border;
  1450. }
  1451. resize(c, nx, ny, nw, nh, RESIZEHINTS);
  1452. if((RESIZEHINTS) && ((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw)))
  1453. /* client doesn't accept size constraints */
  1454. resize(c, nx, ny, nw, nh, False);
  1455. if(n > 1 && th != wah)
  1456. ny = c->y + c->h + 2 * c->border;
  1457. }
  1458. }
  1459. void
  1460. togglebar(const char *arg) {
  1461. if(bpos == BarOff)
  1462. bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
  1463. else
  1464. bpos = BarOff;
  1465. updatebarpos();
  1466. arrange();
  1467. }
  1468. void
  1469. togglefloating(const char *arg) {
  1470. if(!sel)
  1471. return;
  1472. sel->isfloating = !sel->isfloating;
  1473. if(sel->isfloating)
  1474. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1475. arrange();
  1476. }
  1477. void
  1478. togglemax(const char *arg) {
  1479. XEvent ev;
  1480. if(!sel || sel->isfixed)
  1481. return;
  1482. if((sel->ismax = !sel->ismax)) {
  1483. if((floating == layout->arrange) || sel->isfloating)
  1484. sel->wasfloating = True;
  1485. else {
  1486. togglefloating(NULL);
  1487. sel->wasfloating = False;
  1488. }
  1489. sel->rx = sel->x;
  1490. sel->ry = sel->y;
  1491. sel->rw = sel->w;
  1492. sel->rh = sel->h;
  1493. resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True);
  1494. }
  1495. else {
  1496. resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
  1497. if(!sel->wasfloating)
  1498. togglefloating(NULL);
  1499. }
  1500. drawbar();
  1501. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1502. }
  1503. void
  1504. toggletag(const char *arg) {
  1505. unsigned int i, j;
  1506. if(!sel)
  1507. return;
  1508. i = idxoftag(arg);
  1509. sel->tags[i] = !sel->tags[i];
  1510. for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
  1511. if(j == LENGTH(tags))
  1512. sel->tags[i] = True; /* at least one tag must be enabled */
  1513. arrange();
  1514. }
  1515. void
  1516. toggleview(const char *arg) {
  1517. unsigned int i, j;
  1518. i = idxoftag(arg);
  1519. seltags[i] = !seltags[i];
  1520. for(j = 0; j < LENGTH(tags) && !seltags[j]; j++);
  1521. if(j == LENGTH(tags))
  1522. seltags[i] = True; /* at least one tag must be viewed */
  1523. arrange();
  1524. }
  1525. void
  1526. unban(Client *c) {
  1527. if(!c->isbanned)
  1528. return;
  1529. XMoveWindow(dpy, c->win, c->x, c->y);
  1530. c->isbanned = False;
  1531. }
  1532. void
  1533. unmanage(Client *c) {
  1534. XWindowChanges wc;
  1535. wc.border_width = c->oldborder;
  1536. /* The server grab construct avoids race conditions. */
  1537. XGrabServer(dpy);
  1538. XSetErrorHandler(xerrordummy);
  1539. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1540. detach(c);
  1541. detachstack(c);
  1542. if(sel == c)
  1543. focus(NULL);
  1544. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1545. setclientstate(c, WithdrawnState);
  1546. free(c->tags);
  1547. free(c);
  1548. XSync(dpy, False);
  1549. XSetErrorHandler(xerror);
  1550. XUngrabServer(dpy);
  1551. arrange();
  1552. }
  1553. void
  1554. unmapnotify(XEvent *e) {
  1555. Client *c;
  1556. XUnmapEvent *ev = &e->xunmap;
  1557. if((c = getclient(ev->window)))
  1558. unmanage(c);
  1559. }
  1560. void
  1561. updatebarpos(void) {
  1562. XEvent ev;
  1563. wax = sx;
  1564. way = sy;
  1565. wah = sh;
  1566. waw = sw;
  1567. switch(bpos) {
  1568. default:
  1569. wah -= bh;
  1570. way += bh;
  1571. XMoveWindow(dpy, barwin, sx, sy);
  1572. break;
  1573. case BarBot:
  1574. wah -= bh;
  1575. XMoveWindow(dpy, barwin, sx, sy + wah);
  1576. break;
  1577. case BarOff:
  1578. XMoveWindow(dpy, barwin, sx, sy - bh);
  1579. break;
  1580. }
  1581. XSync(dpy, False);
  1582. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1583. }
  1584. void
  1585. updatesizehints(Client *c) {
  1586. long msize;
  1587. XSizeHints size;
  1588. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  1589. size.flags = PSize;
  1590. c->flags = size.flags;
  1591. if(c->flags & PBaseSize) {
  1592. c->basew = size.base_width;
  1593. c->baseh = size.base_height;
  1594. }
  1595. else if(c->flags & PMinSize) {
  1596. c->basew = size.min_width;
  1597. c->baseh = size.min_height;
  1598. }
  1599. else
  1600. c->basew = c->baseh = 0;
  1601. if(c->flags & PResizeInc) {
  1602. c->incw = size.width_inc;
  1603. c->inch = size.height_inc;
  1604. }
  1605. else
  1606. c->incw = c->inch = 0;
  1607. if(c->flags & PMaxSize) {
  1608. c->maxw = size.max_width;
  1609. c->maxh = size.max_height;
  1610. }
  1611. else
  1612. c->maxw = c->maxh = 0;
  1613. if(c->flags & PMinSize) {
  1614. c->minw = size.min_width;
  1615. c->minh = size.min_height;
  1616. }
  1617. else if(c->flags & PBaseSize) {
  1618. c->minw = size.base_width;
  1619. c->minh = size.base_height;
  1620. }
  1621. else
  1622. c->minw = c->minh = 0;
  1623. if(c->flags & PAspect) {
  1624. c->minax = size.min_aspect.x;
  1625. c->maxax = size.max_aspect.x;
  1626. c->minay = size.min_aspect.y;
  1627. c->maxay = size.max_aspect.y;
  1628. }
  1629. else
  1630. c->minax = c->maxax = c->minay = c->maxay = 0;
  1631. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1632. && c->maxw == c->minw && c->maxh == c->minh);
  1633. }
  1634. void
  1635. updatetitle(Client *c) {
  1636. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1637. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1638. }
  1639. /* There's no way to check accesses to destroyed windows, thus those cases are
  1640. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1641. * default error handler, which may call exit. */
  1642. int
  1643. xerror(Display *dpy, XErrorEvent *ee) {
  1644. if(ee->error_code == BadWindow
  1645. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1646. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1647. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1648. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1649. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1650. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1651. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1652. return 0;
  1653. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1654. ee->request_code, ee->error_code);
  1655. return xerrorxlib(dpy, ee); /* may call exit */
  1656. }
  1657. int
  1658. xerrordummy(Display *dsply, XErrorEvent *ee) {
  1659. return 0;
  1660. }
  1661. /* Startup Error handler to check if another window manager
  1662. * is already running. */
  1663. int
  1664. xerrorstart(Display *dsply, XErrorEvent *ee) {
  1665. otherwm = True;
  1666. return -1;
  1667. }
  1668. void
  1669. view(const char *arg) {
  1670. unsigned int i;
  1671. memcpy(prevtags, seltags, sizeof seltags);
  1672. for(i = 0; i < LENGTH(tags); i++)
  1673. seltags[i] = (NULL == arg);
  1674. seltags[idxoftag(arg)] = True;
  1675. arrange();
  1676. }
  1677. void
  1678. viewprevtag(const char *arg) {
  1679. static Bool tmptags[sizeof tags / sizeof tags[0]];
  1680. memcpy(tmptags, seltags, sizeof seltags);
  1681. memcpy(seltags, prevtags, sizeof seltags);
  1682. memcpy(prevtags, tmptags, sizeof seltags);
  1683. arrange();
  1684. }
  1685. void
  1686. zoom(const char *arg) {
  1687. Client *c;
  1688. if(!sel || !dozoom || sel->isfloating)
  1689. return;
  1690. if((c = sel) == nexttiled(clients))
  1691. if(!(c = nexttiled(c->next)))
  1692. return;
  1693. detach(c);
  1694. attach(c);
  1695. focus(c);
  1696. arrange();
  1697. }
  1698. int
  1699. main(int argc, char *argv[]) {
  1700. if(argc == 2 && !strcmp("-v", argv[1]))
  1701. eprint("dwm-"VERSION", © 2006-2007 Anselm R. Garbe, Sander van Dijk, "
  1702. "Jukka Salmi, Premysl Hruby, Szabolcs Nagy\n");
  1703. else if(argc != 1)
  1704. eprint("usage: dwm [-v]\n");
  1705. setlocale(LC_CTYPE, "");
  1706. if(!(dpy = XOpenDisplay(0)))
  1707. eprint("dwm: cannot open display\n");
  1708. screen = DefaultScreen(dpy);
  1709. root = RootWindow(dpy, screen);
  1710. checkotherwm();
  1711. setup();
  1712. drawbar();
  1713. scan();
  1714. run();
  1715. cleanup();
  1716. XCloseDisplay(dpy);
  1717. return 0;
  1718. }