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.

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