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.

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