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

1891 lines
43 KiB

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