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

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