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.

1898 lines
44 KiB

17 years ago
17 years ago
17 years ago
  1. /* See LICENSE file for copyright and license details.
  2. *
  3. * dynamic window manager is designed like any other X client as well. It is
  4. * driven through handling X events. In contrast to other X clients, a window
  5. * manager selects for SubstructureRedirectMask on the root window, to receive
  6. * events about window (dis-)appearance. Only one X connection at a time is
  7. * allowed to select for this event mask.
  8. *
  9. * Calls to fetch an X event from the event queue are blocking. Due reading
  10. * status text from standard input, a select()-driven main loop has been
  11. * implemented which selects for reads on the X connection and STDIN_FILENO to
  12. * handle all data smoothly. The event handlers of dwm are organized in an
  13. * array which is accessed whenever a new event has been fetched. This allows
  14. * event dispatching in O(1) time.
  15. *
  16. * Each child of the root window is called a client, except windows which have
  17. * set the override_redirect flag. Clients are organized in a global
  18. * doubly-linked client list, the focus history is remembered through a global
  19. * stack list. Each client contains an array of Bools of the same size as the
  20. * global tags array to indicate the tags of a client. For each client dwm
  21. * creates a small title window, which is resized whenever the (_NET_)WM_NAME
  22. * properties are updated or the client is moved/resized.
  23. *
  24. * Keys and tagging rules are organized as arrays and defined in config.h.
  25. *
  26. * To understand everything else, start reading main().
  27. */
  28. #include <errno.h>
  29. #include <locale.h>
  30. #include <stdarg.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <unistd.h>
  35. #include <sys/select.h>
  36. #include <sys/types.h>
  37. #include <sys/wait.h>
  38. #include <regex.h>
  39. #include <X11/cursorfont.h>
  40. #include <X11/keysym.h>
  41. #include <X11/Xatom.h>
  42. #include <X11/Xlib.h>
  43. #include <X11/Xproto.h>
  44. #include <X11/Xutil.h>
  45. /* macros */
  46. #define BUTTONMASK (ButtonPressMask | ButtonReleaseMask)
  47. #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
  48. #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
  49. /* enums */
  50. enum { BarTop, BarBot, BarOff }; /* bar position */
  51. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  52. enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
  53. enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
  54. enum { WMProtocols, WMDelete, WMName, WMState, WMLast };/* default atoms */
  55. /* typedefs */
  56. typedef struct Client Client;
  57. struct Client {
  58. char name[256];
  59. int x, y, w, h;
  60. int rx, ry, rw, rh; /* revert geometry */
  61. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  62. int minax, maxax, minay, maxay;
  63. long flags;
  64. unsigned int border, oldborder;
  65. Bool isbanned, isfixed, ismax, isfloating, wasfloating;
  66. Bool *tags;
  67. Client *next;
  68. Client *prev;
  69. Client *snext;
  70. Window win;
  71. };
  72. typedef struct {
  73. int x, y, w, h;
  74. unsigned long norm[ColLast];
  75. unsigned long sel[ColLast];
  76. Drawable drawable;
  77. GC gc;
  78. struct {
  79. int ascent;
  80. int descent;
  81. int height;
  82. XFontSet set;
  83. XFontStruct *xfont;
  84. } font;
  85. } DC; /* draw context */
  86. typedef struct {
  87. unsigned long mod;
  88. KeySym keysym;
  89. void (*func)(const char *arg);
  90. const char *arg;
  91. } Key;
  92. typedef struct {
  93. const char *symbol;
  94. void (*arrange)(void);
  95. } Layout;
  96. typedef struct {
  97. const char *prop;
  98. const char *tags;
  99. Bool isfloating;
  100. } Rule;
  101. typedef struct {
  102. regex_t *propregex;
  103. regex_t *tagregex;
  104. } Regs;
  105. /* forward declarations */
  106. void applyrules(Client *c);
  107. void arrange(void);
  108. void attach(Client *c);
  109. void attachstack(Client *c);
  110. void ban(Client *c);
  111. void buttonpress(XEvent *e);
  112. void checkotherwm(void);
  113. void cleanup(void);
  114. void compileregs(void);
  115. void configure(Client *c);
  116. void configurenotify(XEvent *e);
  117. void configurerequest(XEvent *e);
  118. void destroynotify(XEvent *e);
  119. void detach(Client *c);
  120. void detachstack(Client *c);
  121. void drawbar(void);
  122. void drawsquare(Bool filled, Bool empty, unsigned long col[ColLast]);
  123. void drawtext(const char *text, unsigned long col[ColLast]);
  124. void *emallocz(unsigned int size);
  125. void enternotify(XEvent *e);
  126. void eprint(const char *errstr, ...);
  127. void expose(XEvent *e);
  128. void floating(void); /* default floating layout */
  129. void focus(Client *c);
  130. void focusnext(const char *arg);
  131. void focusprev(const char *arg);
  132. Client *getclient(Window w);
  133. unsigned long getcolor(const char *colstr);
  134. long getstate(Window w);
  135. Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  136. void grabbuttons(Client *c, Bool focused);
  137. unsigned int idxoftag(const char *tag);
  138. void initfont(const char *fontstr);
  139. Bool isarrange(void (*func)());
  140. Bool isoccupied(unsigned int t);
  141. Bool isprotodel(Client *c);
  142. Bool isvisible(Client *c);
  143. void keypress(XEvent *e);
  144. void killclient(const char *arg);
  145. void leavenotify(XEvent *e);
  146. void manage(Window w, XWindowAttributes *wa);
  147. void mappingnotify(XEvent *e);
  148. void maprequest(XEvent *e);
  149. void movemouse(Client *c);
  150. Client *nexttiled(Client *c);
  151. void propertynotify(XEvent *e);
  152. void quit(const char *arg);
  153. void resize(Client *c, int x, int y, int w, int h, Bool sizehints);
  154. void resizemouse(Client *c);
  155. void restack(void);
  156. void run(void);
  157. void scan(void);
  158. void setclientstate(Client *c, long state);
  159. void setlayout(const char *arg);
  160. void setmwfact(const char *arg);
  161. void setup(void);
  162. void spawn(const char *arg);
  163. void tag(const char *arg);
  164. unsigned int textnw(const char *text, unsigned int len);
  165. unsigned int textw(const char *text);
  166. void tile(void);
  167. void togglebar(const char *arg);
  168. void togglefloating(const char *arg);
  169. void togglemax(const char *arg);
  170. void toggletag(const char *arg);
  171. void toggleview(const char *arg);
  172. void unban(Client *c);
  173. void unmanage(Client *c);
  174. void unmapnotify(XEvent *e);
  175. void updatebarpos(void);
  176. void updatesizehints(Client *c);
  177. void updatetitle(Client *c);
  178. void view(const char *arg);
  179. void viewprevtag(const char *arg); /* views previous selected tags */
  180. int xerror(Display *dpy, XErrorEvent *ee);
  181. int xerrordummy(Display *dsply, XErrorEvent *ee);
  182. int xerrorstart(Display *dsply, XErrorEvent *ee);
  183. void zoom(const char *arg);
  184. /* variables */
  185. char stext[256];
  186. double mwfact;
  187. int screen, sx, sy, sw, sh, wax, way, waw, wah;
  188. int (*xerrorxlib)(Display *, XErrorEvent *);
  189. unsigned int bh, bpos;
  190. unsigned int blw = 0;
  191. unsigned int ltidx = 0; /* default */
  192. unsigned int nlayouts = 0;
  193. unsigned int nrules = 0;
  194. unsigned int numlockmask = 0;
  195. 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. Atom wmatom[WMLast], netatom[NetLast];
  210. Bool otherwm, readin;
  211. Bool running = True;
  212. Bool selscreen = True;
  213. Client *clients = NULL;
  214. Client *sel = NULL;
  215. Client *stack = NULL;
  216. Cursor cursor[CurLast];
  217. Display *dpy;
  218. DC dc = {0};
  219. Window barwin, root;
  220. Regs *regs = NULL;
  221. /* configuration, allows nested code to access above variables */
  222. #include "config.h"
  223. /* Statically define the number of tags. */
  224. unsigned int ntags = sizeof tags / sizeof tags[0];
  225. Bool seltags[sizeof tags / sizeof tags[0]] = {[0] = True};
  226. Bool prevtags[sizeof tags / sizeof tags[0]] = {[0] = True};
  227. /* functions*/
  228. void
  229. applyrules(Client *c) {
  230. static char buf[512];
  231. unsigned int i, j;
  232. regmatch_t tmp;
  233. Bool matched = False;
  234. XClassHint ch = { 0 };
  235. /* rule matching */
  236. XGetClassHint(dpy, c->win, &ch);
  237. snprintf(buf, sizeof buf, "%s:%s:%s",
  238. ch.res_class ? ch.res_class : "",
  239. ch.res_name ? ch.res_name : "", c->name);
  240. for(i = 0; i < nrules; i++)
  241. if(regs[i].propregex && !regexec(regs[i].propregex, buf, 1, &tmp, 0)) {
  242. c->isfloating = rules[i].isfloating;
  243. for(j = 0; regs[i].tagregex && j < ntags; j++) {
  244. if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) {
  245. matched = True;
  246. c->tags[j] = True;
  247. }
  248. }
  249. }
  250. if(ch.res_class)
  251. XFree(ch.res_class);
  252. if(ch.res_name)
  253. XFree(ch.res_name);
  254. if(!matched)
  255. memcpy(c->tags, seltags, sizeof seltags);
  256. }
  257. void
  258. arrange(void) {
  259. Client *c;
  260. for(c = clients; c; c = c->next)
  261. if(isvisible(c))
  262. unban(c);
  263. else
  264. ban(c);
  265. layouts[ltidx].arrange();
  266. focus(NULL);
  267. restack();
  268. }
  269. void
  270. attach(Client *c) {
  271. if(clients)
  272. clients->prev = c;
  273. c->next = clients;
  274. clients = c;
  275. }
  276. void
  277. attachstack(Client *c) {
  278. c->snext = stack;
  279. stack = c;
  280. }
  281. void
  282. ban(Client *c) {
  283. if(c->isbanned)
  284. return;
  285. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  286. c->isbanned = True;
  287. }
  288. void
  289. buttonpress(XEvent *e) {
  290. unsigned int i, x;
  291. Client *c;
  292. XButtonPressedEvent *ev = &e->xbutton;
  293. if(barwin == ev->window) {
  294. x = 0;
  295. for(i = 0; i < ntags; i++) {
  296. x += textw(tags[i]);
  297. if(ev->x < x) {
  298. if(ev->button == Button1) {
  299. if(ev->state & MODKEY)
  300. tag(tags[i]);
  301. else
  302. view(tags[i]);
  303. }
  304. else if(ev->button == Button3) {
  305. if(ev->state & MODKEY)
  306. toggletag(tags[i]);
  307. else
  308. toggleview(tags[i]);
  309. }
  310. return;
  311. }
  312. }
  313. if((ev->x < x + blw) && ev->button == Button1)
  314. setlayout(NULL);
  315. }
  316. else if((c = getclient(ev->window))) {
  317. focus(c);
  318. if(CLEANMASK(ev->state) != MODKEY)
  319. return;
  320. if(ev->button == Button1) {
  321. if(isarrange(floating) || c->isfloating)
  322. restack();
  323. else
  324. togglefloating(NULL);
  325. movemouse(c);
  326. }
  327. else if(ev->button == Button2) {
  328. if(ISTILE && !c->isfixed && c->isfloating)
  329. togglefloating(NULL);
  330. else
  331. zoom(NULL);
  332. }
  333. else if(ev->button == Button3 && !c->isfixed) {
  334. if(isarrange(floating) || c->isfloating)
  335. restack();
  336. else
  337. togglefloating(NULL);
  338. resizemouse(c);
  339. }
  340. }
  341. }
  342. void
  343. checkotherwm(void) {
  344. otherwm = False;
  345. XSetErrorHandler(xerrorstart);
  346. /* this causes an error if some other window manager is running */
  347. XSelectInput(dpy, root, SubstructureRedirectMask);
  348. XSync(dpy, False);
  349. if(otherwm)
  350. eprint("dwm: another window manager is already running\n");
  351. XSync(dpy, False);
  352. XSetErrorHandler(NULL);
  353. xerrorxlib = XSetErrorHandler(xerror);
  354. XSync(dpy, False);
  355. }
  356. void
  357. cleanup(void) {
  358. close(STDIN_FILENO);
  359. while(stack) {
  360. unban(stack);
  361. unmanage(stack);
  362. }
  363. if(dc.font.set)
  364. XFreeFontSet(dpy, dc.font.set);
  365. else
  366. XFreeFont(dpy, dc.font.xfont);
  367. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  368. XFreePixmap(dpy, dc.drawable);
  369. XFreeGC(dpy, dc.gc);
  370. XDestroyWindow(dpy, barwin);
  371. XFreeCursor(dpy, cursor[CurNormal]);
  372. XFreeCursor(dpy, cursor[CurResize]);
  373. XFreeCursor(dpy, cursor[CurMove]);
  374. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  375. XSync(dpy, False);
  376. }
  377. 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. 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. 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. 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. void
  475. destroynotify(XEvent *e) {
  476. Client *c;
  477. XDestroyWindowEvent *ev = &e->xdestroywindow;
  478. if((c = getclient(ev->window)))
  479. unmanage(c);
  480. }
  481. 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. void
  492. detachstack(Client *c) {
  493. Client **tc;
  494. for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
  495. *tc = c->snext;
  496. }
  497. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Client *
  688. getclient(Window w) {
  689. Client *c;
  690. for(c = clients; c && c->win != w; c = c->next);
  691. return c;
  692. }
  693. 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. 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. 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. 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. 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. 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. Bool
  824. isarrange(void (*func)())
  825. {
  826. return func == layouts[ltidx].arrange;
  827. }
  828. 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. 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. 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. 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. 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. 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. void
  916. manage(Window w, XWindowAttributes *wa) {
  917. Client *c, *t = NULL;
  918. Window trans;
  919. Status rettrans;
  920. XWindowChanges wc;
  921. c = emallocz(sizeof(Client));
  922. c->tags = emallocz(sizeof seltags);
  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. memcpy(c->tags, t->tags, sizeof seltags);
  958. applyrules(c);
  959. if(!c->isfloating)
  960. c->isfloating = (rettrans == Success) || c->isfixed;
  961. attach(c);
  962. attachstack(c);
  963. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
  964. ban(c);
  965. XMapWindow(dpy, c->win);
  966. setclientstate(c, NormalState);
  967. arrange();
  968. }
  969. void
  970. mappingnotify(XEvent *e) {
  971. XMappingEvent *ev = &e->xmapping;
  972. XRefreshKeyboardMapping(ev);
  973. if(ev->request == MappingKeyboard)
  974. keypress(NULL);
  975. }
  976. void
  977. maprequest(XEvent *e) {
  978. static XWindowAttributes wa;
  979. XMapRequestEvent *ev = &e->xmaprequest;
  980. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  981. return;
  982. if(wa.override_redirect)
  983. return;
  984. if(!getclient(ev->window))
  985. manage(ev->window, &wa);
  986. }
  987. void
  988. movemouse(Client *c) {
  989. int x1, y1, ocx, ocy, di, nx, ny;
  990. unsigned int dui;
  991. Window dummy;
  992. XEvent ev;
  993. ocx = nx = c->x;
  994. ocy = ny = c->y;
  995. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  996. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  997. return;
  998. c->ismax = False;
  999. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  1000. for(;;) {
  1001. XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
  1002. switch (ev.type) {
  1003. case ButtonRelease:
  1004. XUngrabPointer(dpy, CurrentTime);
  1005. return;
  1006. case ConfigureRequest:
  1007. case Expose:
  1008. case MapRequest:
  1009. handler[ev.type](&ev);
  1010. break;
  1011. case MotionNotify:
  1012. XSync(dpy, False);
  1013. nx = ocx + (ev.xmotion.x - x1);
  1014. ny = ocy + (ev.xmotion.y - y1);
  1015. if(abs(wax + nx) < SNAP)
  1016. nx = wax;
  1017. else if(abs((wax + waw) - (nx + c->w + 2 * c->border)) < SNAP)
  1018. nx = wax + waw - c->w - 2 * c->border;
  1019. if(abs(way - ny) < SNAP)
  1020. ny = way;
  1021. else if(abs((way + wah) - (ny + c->h + 2 * c->border)) < SNAP)
  1022. ny = way + wah - c->h - 2 * c->border;
  1023. resize(c, nx, ny, c->w, c->h, False);
  1024. break;
  1025. }
  1026. }
  1027. }
  1028. Client *
  1029. nexttiled(Client *c) {
  1030. for(; c && (c->isfloating || !isvisible(c)); c = c->next);
  1031. return c;
  1032. }
  1033. void
  1034. propertynotify(XEvent *e) {
  1035. Client *c;
  1036. Window trans;
  1037. XPropertyEvent *ev = &e->xproperty;
  1038. if(ev->state == PropertyDelete)
  1039. return; /* ignore */
  1040. if((c = getclient(ev->window))) {
  1041. switch (ev->atom) {
  1042. default: break;
  1043. case XA_WM_TRANSIENT_FOR:
  1044. XGetTransientForHint(dpy, c->win, &trans);
  1045. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  1046. arrange();
  1047. break;
  1048. case XA_WM_NORMAL_HINTS:
  1049. updatesizehints(c);
  1050. break;
  1051. }
  1052. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1053. updatetitle(c);
  1054. if(c == sel)
  1055. drawbar();
  1056. }
  1057. }
  1058. }
  1059. void
  1060. quit(const char *arg) {
  1061. readin = running = False;
  1062. }
  1063. void
  1064. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  1065. double dx, dy, max, min, ratio;
  1066. XWindowChanges wc;
  1067. if(sizehints) {
  1068. if(c->minay > 0 && c->maxay > 0 && (h - c->baseh) > 0 && (w - c->basew) > 0) {
  1069. dx = (double)(w - c->basew);
  1070. dy = (double)(h - c->baseh);
  1071. min = (double)(c->minax) / (double)(c->minay);
  1072. max = (double)(c->maxax) / (double)(c->maxay);
  1073. ratio = dx / dy;
  1074. if(max > 0 && min > 0 && ratio > 0) {
  1075. if(ratio < min) {
  1076. dy = (dx * min + dy) / (min * min + 1);
  1077. dx = dy * min;
  1078. w = (int)dx + c->basew;
  1079. h = (int)dy + c->baseh;
  1080. }
  1081. else if(ratio > max) {
  1082. dy = (dx * min + dy) / (max * max + 1);
  1083. dx = dy * min;
  1084. w = (int)dx + c->basew;
  1085. h = (int)dy + c->baseh;
  1086. }
  1087. }
  1088. }
  1089. if(c->minw && w < c->minw)
  1090. w = c->minw;
  1091. if(c->minh && h < c->minh)
  1092. h = c->minh;
  1093. if(c->maxw && w > c->maxw)
  1094. w = c->maxw;
  1095. if(c->maxh && h > c->maxh)
  1096. h = c->maxh;
  1097. if(c->incw)
  1098. w -= (w - c->basew) % c->incw;
  1099. if(c->inch)
  1100. h -= (h - c->baseh) % c->inch;
  1101. }
  1102. if(w <= 0 || h <= 0)
  1103. return;
  1104. /* offscreen appearance fixes */
  1105. if(x > sw)
  1106. x = sw - w - 2 * c->border;
  1107. if(y > sh)
  1108. y = sh - h - 2 * c->border;
  1109. if(x + w + 2 * c->border < sx)
  1110. x = sx;
  1111. if(y + h + 2 * c->border < sy)
  1112. y = sy;
  1113. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  1114. c->x = wc.x = x;
  1115. c->y = wc.y = y;
  1116. c->w = wc.width = w;
  1117. c->h = wc.height = h;
  1118. wc.border_width = c->border;
  1119. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  1120. configure(c);
  1121. XSync(dpy, False);
  1122. }
  1123. }
  1124. void
  1125. resizemouse(Client *c) {
  1126. int ocx, ocy;
  1127. int nw, nh;
  1128. XEvent ev;
  1129. ocx = c->x;
  1130. ocy = c->y;
  1131. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1132. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1133. return;
  1134. c->ismax = False;
  1135. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
  1136. for(;;) {
  1137. XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
  1138. switch(ev.type) {
  1139. case ButtonRelease:
  1140. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
  1141. c->w + c->border - 1, c->h + c->border - 1);
  1142. XUngrabPointer(dpy, CurrentTime);
  1143. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1144. return;
  1145. case ConfigureRequest:
  1146. case Expose:
  1147. case MapRequest:
  1148. handler[ev.type](&ev);
  1149. break;
  1150. case MotionNotify:
  1151. XSync(dpy, False);
  1152. if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
  1153. nw = 1;
  1154. if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
  1155. nh = 1;
  1156. resize(c, c->x, c->y, nw, nh, True);
  1157. break;
  1158. }
  1159. }
  1160. }
  1161. void
  1162. restack(void) {
  1163. Client *c;
  1164. XEvent ev;
  1165. XWindowChanges wc;
  1166. drawbar();
  1167. if(!sel)
  1168. return;
  1169. if(sel->isfloating || isarrange(floating))
  1170. XRaiseWindow(dpy, sel->win);
  1171. if(!isarrange(floating)) {
  1172. wc.stack_mode = Below;
  1173. wc.sibling = barwin;
  1174. if(!sel->isfloating) {
  1175. XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc);
  1176. wc.sibling = sel->win;
  1177. }
  1178. for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
  1179. if(c == sel)
  1180. continue;
  1181. XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
  1182. wc.sibling = c->win;
  1183. }
  1184. }
  1185. XSync(dpy, False);
  1186. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1187. }
  1188. void
  1189. run(void) {
  1190. char *p;
  1191. int r, xfd;
  1192. fd_set rd;
  1193. XEvent ev;
  1194. /* main event loop, also reads status text from stdin */
  1195. XSync(dpy, False);
  1196. xfd = ConnectionNumber(dpy);
  1197. readin = True;
  1198. while(running) {
  1199. FD_ZERO(&rd);
  1200. if(readin)
  1201. FD_SET(STDIN_FILENO, &rd);
  1202. FD_SET(xfd, &rd);
  1203. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1204. if(errno == EINTR)
  1205. continue;
  1206. eprint("select failed\n");
  1207. }
  1208. if(FD_ISSET(STDIN_FILENO, &rd)) {
  1209. switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
  1210. case -1:
  1211. strncpy(stext, strerror(errno), sizeof stext - 1);
  1212. stext[sizeof stext - 1] = '\0';
  1213. readin = False;
  1214. break;
  1215. case 0:
  1216. strncpy(stext, "EOF", 4);
  1217. readin = False;
  1218. break;
  1219. default:
  1220. for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
  1221. for(; p >= stext && *p != '\n'; --p);
  1222. if(p > stext)
  1223. strncpy(stext, p + 1, sizeof stext);
  1224. }
  1225. drawbar();
  1226. }
  1227. while(XPending(dpy)) {
  1228. XNextEvent(dpy, &ev);
  1229. if(handler[ev.type])
  1230. (handler[ev.type])(&ev); /* call handler */
  1231. }
  1232. }
  1233. }
  1234. void
  1235. scan(void) {
  1236. unsigned int i, num;
  1237. Window *wins, d1, d2;
  1238. XWindowAttributes wa;
  1239. wins = NULL;
  1240. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1241. for(i = 0; i < num; i++) {
  1242. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1243. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1244. continue;
  1245. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1246. manage(wins[i], &wa);
  1247. }
  1248. for(i = 0; i < num; i++) { /* now the transients */
  1249. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1250. continue;
  1251. if(XGetTransientForHint(dpy, wins[i], &d1)
  1252. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1253. manage(wins[i], &wa);
  1254. }
  1255. }
  1256. if(wins)
  1257. XFree(wins);
  1258. }
  1259. void
  1260. setclientstate(Client *c, long state) {
  1261. long data[] = {state, None};
  1262. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1263. PropModeReplace, (unsigned char *)data, 2);
  1264. }
  1265. void
  1266. setlayout(const char *arg) {
  1267. unsigned int i;
  1268. if(!arg) {
  1269. if(++ltidx == nlayouts)
  1270. ltidx = 0;;
  1271. }
  1272. else {
  1273. for(i = 0; i < nlayouts; i++)
  1274. if(!strcmp(arg, layouts[i].symbol))
  1275. break;
  1276. if(i == nlayouts)
  1277. return;
  1278. ltidx = i;
  1279. }
  1280. if(sel)
  1281. arrange();
  1282. else
  1283. drawbar();
  1284. }
  1285. void
  1286. setmwfact(const char *arg) {
  1287. double delta;
  1288. if(!ISTILE)
  1289. return;
  1290. /* arg handling, manipulate mwfact */
  1291. if(arg == NULL)
  1292. mwfact = MWFACT;
  1293. else if(1 == sscanf(arg, "%lf", &delta)) {
  1294. if(arg[0] == '+' || arg[0] == '-')
  1295. mwfact += delta;
  1296. else
  1297. mwfact = delta;
  1298. if(mwfact < 0.1)
  1299. mwfact = 0.1;
  1300. else if(mwfact > 0.9)
  1301. mwfact = 0.9;
  1302. }
  1303. arrange();
  1304. }
  1305. void
  1306. setup(void) {
  1307. int d;
  1308. unsigned int i, j, mask;
  1309. Window w;
  1310. XModifierKeymap *modmap;
  1311. XSetWindowAttributes wa;
  1312. /* init atoms */
  1313. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1314. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1315. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1316. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1317. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1318. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1319. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1320. PropModeReplace, (unsigned char *) netatom, NetLast);
  1321. /* init cursors */
  1322. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1323. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1324. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1325. /* init geometry */
  1326. sx = sy = 0;
  1327. sw = DisplayWidth(dpy, screen);
  1328. sh = DisplayHeight(dpy, screen);
  1329. /* init modifier map */
  1330. modmap = XGetModifierMapping(dpy);
  1331. for(i = 0; i < 8; i++)
  1332. for(j = 0; j < modmap->max_keypermod; j++) {
  1333. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1334. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1335. numlockmask = (1 << i);
  1336. }
  1337. XFreeModifiermap(modmap);
  1338. /* select for events */
  1339. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  1340. | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
  1341. wa.cursor = cursor[CurNormal];
  1342. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  1343. XSelectInput(dpy, root, wa.event_mask);
  1344. /* grab keys */
  1345. keypress(NULL);
  1346. /* init tags */
  1347. compileregs();
  1348. /* init appearance */
  1349. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  1350. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  1351. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  1352. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  1353. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  1354. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  1355. initfont(FONT);
  1356. dc.h = bh = dc.font.height + 2;
  1357. /* init layouts */
  1358. mwfact = MWFACT;
  1359. nlayouts = sizeof layouts / sizeof layouts[0];
  1360. for(blw = i = 0; i < nlayouts; i++) {
  1361. j = textw(layouts[i].symbol);
  1362. if(j > blw)
  1363. blw = j;
  1364. }
  1365. /* init bar */
  1366. bpos = BARPOS;
  1367. wa.override_redirect = 1;
  1368. wa.background_pixmap = ParentRelative;
  1369. wa.event_mask = ButtonPressMask | ExposureMask;
  1370. barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
  1371. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  1372. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  1373. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  1374. updatebarpos();
  1375. XMapRaised(dpy, barwin);
  1376. strcpy(stext, "dwm-"VERSION);
  1377. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  1378. dc.gc = XCreateGC(dpy, root, 0, 0);
  1379. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1380. if(!dc.font.set)
  1381. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1382. /* multihead support */
  1383. selscreen = XQueryPointer(dpy, root, &w, &w, &d, &d, &d, &d, &mask);
  1384. }
  1385. void
  1386. spawn(const char *arg) {
  1387. static char *shell = NULL;
  1388. if(!shell && !(shell = getenv("SHELL")))
  1389. shell = "/bin/sh";
  1390. if(!arg)
  1391. return;
  1392. /* The double-fork construct avoids zombie processes and keeps the code
  1393. * clean from stupid signal handlers. */
  1394. if(fork() == 0) {
  1395. if(fork() == 0) {
  1396. if(dpy)
  1397. close(ConnectionNumber(dpy));
  1398. setsid();
  1399. execl(shell, shell, "-c", arg, (char *)NULL);
  1400. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
  1401. perror(" failed");
  1402. }
  1403. exit(0);
  1404. }
  1405. wait(0);
  1406. }
  1407. void
  1408. tag(const char *arg) {
  1409. unsigned int i;
  1410. if(!sel)
  1411. return;
  1412. for(i = 0; i < ntags; i++)
  1413. sel->tags[i] = arg == NULL;
  1414. i = idxoftag(arg);
  1415. if(i >= 0 && i < ntags)
  1416. sel->tags[i] = True;
  1417. arrange();
  1418. }
  1419. unsigned int
  1420. textnw(const char *text, unsigned int len) {
  1421. XRectangle r;
  1422. if(dc.font.set) {
  1423. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1424. return r.width;
  1425. }
  1426. return XTextWidth(dc.font.xfont, text, len);
  1427. }
  1428. unsigned int
  1429. textw(const char *text) {
  1430. return textnw(text, strlen(text)) + dc.font.height;
  1431. }
  1432. void
  1433. tile(void) {
  1434. unsigned int i, n, nx, ny, nw, nh, mw, th;
  1435. Client *c, *mc;
  1436. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
  1437. n++;
  1438. /* window geoms */
  1439. mw = (n == 1) ? waw : mwfact * waw;
  1440. th = (n > 1) ? wah / (n - 1) : 0;
  1441. if(n > 1 && th < bh)
  1442. th = wah;
  1443. nx = wax;
  1444. ny = way;
  1445. nw = 0; /* gcc stupidity requires this */
  1446. for(i = 0, c = mc = nexttiled(clients); c; c = nexttiled(c->next), i++) {
  1447. c->ismax = False;
  1448. if(i == 0) { /* master */
  1449. nw = mw - 2 * c->border;
  1450. nh = wah - 2 * c->border;
  1451. }
  1452. else { /* tile window */
  1453. if(i == 1) {
  1454. ny = way;
  1455. nx += mc->w + 2 * mc->border;
  1456. nw = waw - nx - 2 * c->border;
  1457. }
  1458. if(i + 1 == n) /* remainder */
  1459. nh = (way + wah) - ny - 2 * c->border;
  1460. else
  1461. nh = th - 2 * c->border;
  1462. }
  1463. resize(c, nx, ny, nw, nh, RESIZEHINTS);
  1464. if(n > 1 && th != wah)
  1465. ny = c->y + c->h + 2 * c->border;
  1466. }
  1467. }
  1468. void
  1469. togglebar(const char *arg) {
  1470. if(bpos == BarOff)
  1471. bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
  1472. else
  1473. bpos = BarOff;
  1474. updatebarpos();
  1475. arrange();
  1476. }
  1477. void
  1478. togglefloating(const char *arg) {
  1479. if(!sel)
  1480. return;
  1481. sel->isfloating = !sel->isfloating;
  1482. if(sel->isfloating)
  1483. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1484. arrange();
  1485. }
  1486. void
  1487. togglemax(const char *arg) {
  1488. XEvent ev;
  1489. if(!sel || sel->isfixed)
  1490. return;
  1491. if((sel->ismax = !sel->ismax)) {
  1492. if(isarrange(floating) || sel->isfloating)
  1493. sel->wasfloating = True;
  1494. else {
  1495. togglefloating(NULL);
  1496. sel->wasfloating = False;
  1497. }
  1498. sel->rx = sel->x;
  1499. sel->ry = sel->y;
  1500. sel->rw = sel->w;
  1501. sel->rh = sel->h;
  1502. resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True);
  1503. }
  1504. else {
  1505. resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
  1506. if(!sel->wasfloating)
  1507. togglefloating(NULL);
  1508. }
  1509. drawbar();
  1510. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1511. }
  1512. void
  1513. toggletag(const char *arg) {
  1514. unsigned int i, j;
  1515. if(!sel)
  1516. return;
  1517. i = idxoftag(arg);
  1518. sel->tags[i] = !sel->tags[i];
  1519. for(j = 0; j < ntags && !sel->tags[j]; j++);
  1520. if(j == ntags)
  1521. sel->tags[i] = True;
  1522. arrange();
  1523. }
  1524. void
  1525. toggleview(const char *arg) {
  1526. unsigned int i, j;
  1527. i = idxoftag(arg);
  1528. seltags[i] = !seltags[i];
  1529. for(j = 0; j < ntags && !seltags[j]; j++);
  1530. if(j == ntags)
  1531. seltags[i] = True; /* at least one tag must be viewed */
  1532. arrange();
  1533. }
  1534. void
  1535. unban(Client *c) {
  1536. if(!c->isbanned)
  1537. return;
  1538. XMoveWindow(dpy, c->win, c->x, c->y);
  1539. c->isbanned = False;
  1540. }
  1541. void
  1542. unmanage(Client *c) {
  1543. XWindowChanges wc;
  1544. wc.border_width = c->oldborder;
  1545. /* The server grab construct avoids race conditions. */
  1546. XGrabServer(dpy);
  1547. XSetErrorHandler(xerrordummy);
  1548. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1549. detach(c);
  1550. detachstack(c);
  1551. if(sel == c)
  1552. focus(NULL);
  1553. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1554. setclientstate(c, WithdrawnState);
  1555. free(c->tags);
  1556. free(c);
  1557. XSync(dpy, False);
  1558. XSetErrorHandler(xerror);
  1559. XUngrabServer(dpy);
  1560. arrange();
  1561. }
  1562. void
  1563. unmapnotify(XEvent *e) {
  1564. Client *c;
  1565. XUnmapEvent *ev = &e->xunmap;
  1566. if((c = getclient(ev->window)))
  1567. unmanage(c);
  1568. }
  1569. void
  1570. updatebarpos(void) {
  1571. XEvent ev;
  1572. wax = sx;
  1573. way = sy;
  1574. wah = sh;
  1575. waw = sw;
  1576. switch(bpos) {
  1577. default:
  1578. wah -= bh;
  1579. way += bh;
  1580. XMoveWindow(dpy, barwin, sx, sy);
  1581. break;
  1582. case BarBot:
  1583. wah -= bh;
  1584. XMoveWindow(dpy, barwin, sx, sy + wah);
  1585. break;
  1586. case BarOff:
  1587. XMoveWindow(dpy, barwin, sx, sy - bh);
  1588. break;
  1589. }
  1590. XSync(dpy, False);
  1591. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1592. }
  1593. void
  1594. updatesizehints(Client *c) {
  1595. long msize;
  1596. XSizeHints size;
  1597. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  1598. size.flags = PSize;
  1599. c->flags = size.flags;
  1600. if(c->flags & PBaseSize) {
  1601. c->basew = size.base_width;
  1602. c->baseh = size.base_height;
  1603. }
  1604. else if(c->flags & PMinSize) {
  1605. c->basew = size.min_width;
  1606. c->baseh = size.min_height;
  1607. }
  1608. else
  1609. c->basew = c->baseh = 0;
  1610. if(c->flags & PResizeInc) {
  1611. c->incw = size.width_inc;
  1612. c->inch = size.height_inc;
  1613. }
  1614. else
  1615. c->incw = c->inch = 0;
  1616. if(c->flags & PMaxSize) {
  1617. c->maxw = size.max_width;
  1618. c->maxh = size.max_height;
  1619. }
  1620. else
  1621. c->maxw = c->maxh = 0;
  1622. if(c->flags & PMinSize) {
  1623. c->minw = size.min_width;
  1624. c->minh = size.min_height;
  1625. }
  1626. else if(c->flags & PBaseSize) {
  1627. c->minw = size.base_width;
  1628. c->minh = size.base_height;
  1629. }
  1630. else
  1631. c->minw = c->minh = 0;
  1632. if(c->flags & PAspect) {
  1633. c->minax = size.min_aspect.x;
  1634. c->maxax = size.max_aspect.x;
  1635. c->minay = size.min_aspect.y;
  1636. c->maxay = size.max_aspect.y;
  1637. }
  1638. else
  1639. c->minax = c->maxax = c->minay = c->maxay = 0;
  1640. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1641. && c->maxw == c->minw && c->maxh == c->minh);
  1642. }
  1643. void
  1644. updatetitle(Client *c) {
  1645. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1646. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1647. }
  1648. /* There's no way to check accesses to destroyed windows, thus those cases are
  1649. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1650. * default error handler, which may call exit. */
  1651. int
  1652. xerror(Display *dpy, XErrorEvent *ee) {
  1653. if(ee->error_code == BadWindow
  1654. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1655. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1656. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1657. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1658. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1659. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1660. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1661. return 0;
  1662. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1663. ee->request_code, ee->error_code);
  1664. return xerrorxlib(dpy, ee); /* may call exit */
  1665. }
  1666. int
  1667. xerrordummy(Display *dsply, XErrorEvent *ee) {
  1668. return 0;
  1669. }
  1670. /* Startup Error handler to check if another window manager
  1671. * is already running. */
  1672. int
  1673. xerrorstart(Display *dsply, XErrorEvent *ee) {
  1674. otherwm = True;
  1675. return -1;
  1676. }
  1677. void
  1678. view(const char *arg) {
  1679. unsigned int i;
  1680. memcpy(prevtags, seltags, sizeof seltags);
  1681. for(i = 0; i < ntags; i++)
  1682. seltags[i] = arg == NULL;
  1683. i = idxoftag(arg);
  1684. if(i >= 0 && i < ntags)
  1685. seltags[i] = True;
  1686. arrange();
  1687. }
  1688. void
  1689. viewprevtag(const char *arg) {
  1690. static Bool tmptags[sizeof tags / sizeof tags[0]];
  1691. memcpy(tmptags, seltags, sizeof seltags);
  1692. memcpy(seltags, prevtags, sizeof seltags);
  1693. memcpy(prevtags, tmptags, sizeof seltags);
  1694. arrange();
  1695. }
  1696. void
  1697. zoom(const char *arg) {
  1698. Client *c;
  1699. if(!sel || !ISTILE || sel->isfloating)
  1700. return;
  1701. if((c = sel) == nexttiled(clients))
  1702. if(!(c = nexttiled(c->next)))
  1703. return;
  1704. detach(c);
  1705. attach(c);
  1706. focus(c);
  1707. arrange();
  1708. }
  1709. int
  1710. main(int argc, char *argv[]) {
  1711. if(argc == 2 && !strcmp("-v", argv[1]))
  1712. eprint("dwm-"VERSION", © 2006-2007 A. R. Garbe, S. van Dijk, J. Salmi, P. Hruby, S. Nagy\n");
  1713. else if(argc != 1)
  1714. eprint("usage: dwm [-v]\n");
  1715. setlocale(LC_CTYPE, "");
  1716. if(!(dpy = XOpenDisplay(0)))
  1717. eprint("dwm: cannot open display\n");
  1718. screen = DefaultScreen(dpy);
  1719. root = RootWindow(dpy, screen);
  1720. checkotherwm();
  1721. setup();
  1722. drawbar();
  1723. scan();
  1724. run();
  1725. cleanup();
  1726. XCloseDisplay(dpy);
  1727. return 0;
  1728. }