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.

1888 lines
43 KiB

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