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

1880 lines
43 KiB

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