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.

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