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.

1872 lines
43 KiB

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