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.

1874 lines
44 KiB

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