Configuration file for DWM on MacBook Air
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1874 lines
43 KiB

17 years ago
17 years ago
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((readin = (stext == fgets(stext, sizeof stext - 1, stdin))))
  1194. stext[strlen(stext) - 1] = '\0'; /* remove tailing '\n' */
  1195. else if(feof(stdin))
  1196. strncpy(stext, "EOF", 4);
  1197. else /* error occured */
  1198. strncpy(stext, strerror(errno), sizeof stext - 1);
  1199. drawbar();
  1200. }
  1201. while(XPending(dpy)) {
  1202. XNextEvent(dpy, &ev);
  1203. if(handler[ev.type])
  1204. (handler[ev.type])(&ev); /* call handler */
  1205. }
  1206. }
  1207. }
  1208. void
  1209. scan(void) {
  1210. unsigned int i, num;
  1211. Window *wins, d1, d2;
  1212. XWindowAttributes wa;
  1213. wins = NULL;
  1214. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1215. for(i = 0; i < num; i++) {
  1216. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1217. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1218. continue;
  1219. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1220. manage(wins[i], &wa);
  1221. }
  1222. for(i = 0; i < num; i++) { /* now the transients */
  1223. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1224. continue;
  1225. if(XGetTransientForHint(dpy, wins[i], &d1)
  1226. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1227. manage(wins[i], &wa);
  1228. }
  1229. }
  1230. if(wins)
  1231. XFree(wins);
  1232. }
  1233. void
  1234. setclientstate(Client *c, long state) {
  1235. long data[] = {state, None};
  1236. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1237. PropModeReplace, (unsigned char *)data, 2);
  1238. }
  1239. void
  1240. setlayout(const char *arg) {
  1241. unsigned int i;
  1242. if(!arg) {
  1243. if(++layout == &layouts[LENGTH(layouts)])
  1244. layout = &layouts[0];
  1245. }
  1246. else {
  1247. for(i = 0; i < LENGTH(layouts); i++)
  1248. if(!strcmp(arg, layouts[i].symbol))
  1249. break;
  1250. if(i == LENGTH(layouts))
  1251. return;
  1252. layout = &layouts[i];
  1253. }
  1254. if(sel)
  1255. arrange();
  1256. else
  1257. drawbar();
  1258. }
  1259. void
  1260. setmwfact(const char *arg) {
  1261. double delta;
  1262. if(!domwfact)
  1263. return;
  1264. /* arg handling, manipulate mwfact */
  1265. if(NULL == arg)
  1266. mwfact = MWFACT;
  1267. else if(1 == sscanf(arg, "%lf", &delta)) {
  1268. if(arg[0] == '+' || arg[0] == '-')
  1269. mwfact += delta;
  1270. else
  1271. mwfact = delta;
  1272. if(mwfact < 0.1)
  1273. mwfact = 0.1;
  1274. else if(mwfact > 0.9)
  1275. mwfact = 0.9;
  1276. }
  1277. arrange();
  1278. }
  1279. void
  1280. setup(void) {
  1281. int d;
  1282. unsigned int i, j, mask;
  1283. Window w;
  1284. XModifierKeymap *modmap;
  1285. XSetWindowAttributes wa;
  1286. /* init atoms */
  1287. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1288. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1289. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1290. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1291. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1292. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1293. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1294. PropModeReplace, (unsigned char *) netatom, NetLast);
  1295. /* init cursors */
  1296. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1297. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1298. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1299. /* init geometry */
  1300. sx = sy = 0;
  1301. sw = DisplayWidth(dpy, screen);
  1302. sh = DisplayHeight(dpy, screen);
  1303. /* init modifier map */
  1304. modmap = XGetModifierMapping(dpy);
  1305. for(i = 0; i < 8; i++)
  1306. for(j = 0; j < modmap->max_keypermod; j++) {
  1307. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1308. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1309. numlockmask = (1 << i);
  1310. }
  1311. XFreeModifiermap(modmap);
  1312. /* select for events */
  1313. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  1314. | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
  1315. wa.cursor = cursor[CurNormal];
  1316. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  1317. XSelectInput(dpy, root, wa.event_mask);
  1318. /* grab keys */
  1319. keypress(NULL);
  1320. /* init tags */
  1321. compileregs();
  1322. /* init appearance */
  1323. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  1324. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  1325. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  1326. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  1327. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  1328. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  1329. initfont(FONT);
  1330. dc.h = bh = dc.font.height + 2;
  1331. /* init layouts */
  1332. mwfact = MWFACT;
  1333. layout = &layouts[0];
  1334. for(blw = i = 0; i < LENGTH(layouts); i++) {
  1335. j = textw(layouts[i].symbol);
  1336. if(j > blw)
  1337. blw = j;
  1338. }
  1339. /* init bar */
  1340. bpos = BARPOS;
  1341. wa.override_redirect = 1;
  1342. wa.background_pixmap = ParentRelative;
  1343. wa.event_mask = ButtonPressMask | ExposureMask;
  1344. barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
  1345. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  1346. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  1347. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  1348. updatebarpos();
  1349. XMapRaised(dpy, barwin);
  1350. strcpy(stext, "dwm-"VERSION);
  1351. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  1352. dc.gc = XCreateGC(dpy, root, 0, 0);
  1353. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1354. if(!dc.font.set)
  1355. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1356. /* multihead support */
  1357. selscreen = XQueryPointer(dpy, root, &w, &w, &d, &d, &d, &d, &mask);
  1358. }
  1359. void
  1360. spawn(const char *arg) {
  1361. static char *shell = NULL;
  1362. if(!shell && !(shell = getenv("SHELL")))
  1363. shell = "/bin/sh";
  1364. if(!arg)
  1365. return;
  1366. /* The double-fork construct avoids zombie processes and keeps the code
  1367. * clean from stupid signal handlers. */
  1368. if(0 == fork()) {
  1369. if(0 == fork()) {
  1370. if(dpy)
  1371. close(ConnectionNumber(dpy));
  1372. setsid();
  1373. execl(shell, shell, "-c", arg, (char *)NULL);
  1374. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
  1375. perror(" failed");
  1376. }
  1377. exit(0);
  1378. }
  1379. wait(0);
  1380. }
  1381. void
  1382. tag(const char *arg) {
  1383. unsigned int i;
  1384. if(!sel)
  1385. return;
  1386. for(i = 0; i < LENGTH(tags); i++)
  1387. sel->tags[i] = (NULL == arg);
  1388. sel->tags[idxoftag(arg)] = True;
  1389. arrange();
  1390. }
  1391. unsigned int
  1392. textnw(const char *text, unsigned int len) {
  1393. XRectangle r;
  1394. if(dc.font.set) {
  1395. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1396. return r.width;
  1397. }
  1398. return XTextWidth(dc.font.xfont, text, len);
  1399. }
  1400. unsigned int
  1401. textw(const char *text) {
  1402. return textnw(text, strlen(text)) + dc.font.height;
  1403. }
  1404. void
  1405. tile(void) {
  1406. unsigned int i, n, nx, ny, nw, nh, mw, th;
  1407. Client *c, *mc;
  1408. domwfact = dozoom = True;
  1409. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
  1410. n++;
  1411. /* window geoms */
  1412. mw = (n == 1) ? waw : mwfact * waw;
  1413. th = (n > 1) ? wah / (n - 1) : 0;
  1414. if(n > 1 && th < bh)
  1415. th = wah;
  1416. nx = wax;
  1417. ny = way;
  1418. nw = 0; /* gcc stupidity requires this */
  1419. for(i = 0, c = mc = nexttiled(clients); c; c = nexttiled(c->next), i++) {
  1420. c->ismax = False;
  1421. if(0 == i) { /* master */
  1422. nw = mw - 2 * c->border;
  1423. nh = wah - 2 * c->border;
  1424. }
  1425. else { /* tile window */
  1426. if(i == 1) {
  1427. ny = way;
  1428. nx += mc->w + 2 * mc->border;
  1429. nw = waw - nx - 2 * c->border;
  1430. }
  1431. if(i + 1 == n) /* remainder */
  1432. nh = (way + wah) - ny - 2 * c->border;
  1433. else
  1434. nh = th - 2 * c->border;
  1435. }
  1436. resize(c, nx, ny, nw, nh, RESIZEHINTS);
  1437. if((RESIZEHINTS) && ((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw)))
  1438. /* client doesn't accept size constraints */
  1439. resize(c, nx, ny, nw, nh, False);
  1440. if(n > 1 && th != wah)
  1441. ny = c->y + c->h + 2 * c->border;
  1442. }
  1443. }
  1444. void
  1445. togglebar(const char *arg) {
  1446. if(bpos == BarOff)
  1447. bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
  1448. else
  1449. bpos = BarOff;
  1450. updatebarpos();
  1451. arrange();
  1452. }
  1453. void
  1454. togglefloating(const char *arg) {
  1455. if(!sel)
  1456. return;
  1457. sel->isfloating = !sel->isfloating;
  1458. if(sel->isfloating)
  1459. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1460. arrange();
  1461. }
  1462. void
  1463. togglemax(const char *arg) {
  1464. XEvent ev;
  1465. if(!sel || sel->isfixed)
  1466. return;
  1467. if((sel->ismax = !sel->ismax)) {
  1468. if((floating == layout->arrange) || sel->isfloating)
  1469. sel->wasfloating = True;
  1470. else {
  1471. togglefloating(NULL);
  1472. sel->wasfloating = False;
  1473. }
  1474. sel->rx = sel->x;
  1475. sel->ry = sel->y;
  1476. sel->rw = sel->w;
  1477. sel->rh = sel->h;
  1478. resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True);
  1479. }
  1480. else {
  1481. resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
  1482. if(!sel->wasfloating)
  1483. togglefloating(NULL);
  1484. }
  1485. drawbar();
  1486. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1487. }
  1488. void
  1489. toggletag(const char *arg) {
  1490. unsigned int i, j;
  1491. if(!sel)
  1492. return;
  1493. i = idxoftag(arg);
  1494. sel->tags[i] = !sel->tags[i];
  1495. for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
  1496. if(j == LENGTH(tags))
  1497. sel->tags[i] = True; /* at least one tag must be enabled */
  1498. arrange();
  1499. }
  1500. void
  1501. toggleview(const char *arg) {
  1502. unsigned int i, j;
  1503. i = idxoftag(arg);
  1504. seltags[i] = !seltags[i];
  1505. for(j = 0; j < LENGTH(tags) && !seltags[j]; j++);
  1506. if(j == LENGTH(tags))
  1507. seltags[i] = True; /* at least one tag must be viewed */
  1508. arrange();
  1509. }
  1510. void
  1511. unban(Client *c) {
  1512. if(!c->isbanned)
  1513. return;
  1514. XMoveWindow(dpy, c->win, c->x, c->y);
  1515. c->isbanned = False;
  1516. }
  1517. void
  1518. unmanage(Client *c) {
  1519. XWindowChanges wc;
  1520. wc.border_width = c->oldborder;
  1521. /* The server grab construct avoids race conditions. */
  1522. XGrabServer(dpy);
  1523. XSetErrorHandler(xerrordummy);
  1524. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1525. detach(c);
  1526. detachstack(c);
  1527. if(sel == c)
  1528. focus(NULL);
  1529. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1530. setclientstate(c, WithdrawnState);
  1531. free(c->tags);
  1532. free(c);
  1533. XSync(dpy, False);
  1534. XSetErrorHandler(xerror);
  1535. XUngrabServer(dpy);
  1536. arrange();
  1537. }
  1538. void
  1539. unmapnotify(XEvent *e) {
  1540. Client *c;
  1541. XUnmapEvent *ev = &e->xunmap;
  1542. if((c = getclient(ev->window)))
  1543. unmanage(c);
  1544. }
  1545. void
  1546. updatebarpos(void) {
  1547. XEvent ev;
  1548. wax = sx;
  1549. way = sy;
  1550. wah = sh;
  1551. waw = sw;
  1552. switch(bpos) {
  1553. default:
  1554. wah -= bh;
  1555. way += bh;
  1556. XMoveWindow(dpy, barwin, sx, sy);
  1557. break;
  1558. case BarBot:
  1559. wah -= bh;
  1560. XMoveWindow(dpy, barwin, sx, sy + wah);
  1561. break;
  1562. case BarOff:
  1563. XMoveWindow(dpy, barwin, sx, sy - bh);
  1564. break;
  1565. }
  1566. XSync(dpy, False);
  1567. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1568. }
  1569. void
  1570. updatesizehints(Client *c) {
  1571. long msize;
  1572. XSizeHints size;
  1573. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  1574. size.flags = PSize;
  1575. c->flags = size.flags;
  1576. if(c->flags & PBaseSize) {
  1577. c->basew = size.base_width;
  1578. c->baseh = size.base_height;
  1579. }
  1580. else if(c->flags & PMinSize) {
  1581. c->basew = size.min_width;
  1582. c->baseh = size.min_height;
  1583. }
  1584. else
  1585. c->basew = c->baseh = 0;
  1586. if(c->flags & PResizeInc) {
  1587. c->incw = size.width_inc;
  1588. c->inch = size.height_inc;
  1589. }
  1590. else
  1591. c->incw = c->inch = 0;
  1592. if(c->flags & PMaxSize) {
  1593. c->maxw = size.max_width;
  1594. c->maxh = size.max_height;
  1595. }
  1596. else
  1597. c->maxw = c->maxh = 0;
  1598. if(c->flags & PMinSize) {
  1599. c->minw = size.min_width;
  1600. c->minh = size.min_height;
  1601. }
  1602. else if(c->flags & PBaseSize) {
  1603. c->minw = size.base_width;
  1604. c->minh = size.base_height;
  1605. }
  1606. else
  1607. c->minw = c->minh = 0;
  1608. if(c->flags & PAspect) {
  1609. c->minax = size.min_aspect.x;
  1610. c->maxax = size.max_aspect.x;
  1611. c->minay = size.min_aspect.y;
  1612. c->maxay = size.max_aspect.y;
  1613. }
  1614. else
  1615. c->minax = c->maxax = c->minay = c->maxay = 0;
  1616. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1617. && c->maxw == c->minw && c->maxh == c->minh);
  1618. }
  1619. void
  1620. updatetitle(Client *c) {
  1621. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1622. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1623. }
  1624. /* There's no way to check accesses to destroyed windows, thus those cases are
  1625. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1626. * default error handler, which may call exit. */
  1627. int
  1628. xerror(Display *dpy, XErrorEvent *ee) {
  1629. if(ee->error_code == BadWindow
  1630. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1631. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1632. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1633. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1634. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1635. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1636. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1637. return 0;
  1638. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1639. ee->request_code, ee->error_code);
  1640. return xerrorxlib(dpy, ee); /* may call exit */
  1641. }
  1642. int
  1643. xerrordummy(Display *dsply, XErrorEvent *ee) {
  1644. return 0;
  1645. }
  1646. /* Startup Error handler to check if another window manager
  1647. * is already running. */
  1648. int
  1649. xerrorstart(Display *dsply, XErrorEvent *ee) {
  1650. otherwm = True;
  1651. return -1;
  1652. }
  1653. void
  1654. view(const char *arg) {
  1655. unsigned int i;
  1656. memcpy(prevtags, seltags, sizeof seltags);
  1657. for(i = 0; i < LENGTH(tags); i++)
  1658. seltags[i] = (NULL == arg);
  1659. seltags[idxoftag(arg)] = True;
  1660. arrange();
  1661. }
  1662. void
  1663. viewprevtag(const char *arg) {
  1664. static Bool tmptags[sizeof tags / sizeof tags[0]];
  1665. memcpy(tmptags, seltags, sizeof seltags);
  1666. memcpy(seltags, prevtags, sizeof seltags);
  1667. memcpy(prevtags, tmptags, sizeof seltags);
  1668. arrange();
  1669. }
  1670. void
  1671. zoom(const char *arg) {
  1672. Client *c;
  1673. if(!sel || !dozoom || sel->isfloating)
  1674. return;
  1675. if((c = sel) == nexttiled(clients))
  1676. if(!(c = nexttiled(c->next)))
  1677. return;
  1678. detach(c);
  1679. attach(c);
  1680. focus(c);
  1681. arrange();
  1682. }
  1683. int
  1684. main(int argc, char *argv[]) {
  1685. if(argc == 2 && !strcmp("-v", argv[1]))
  1686. eprint("dwm-"VERSION", © 2006-2007 A. R. Garbe, S. van Dijk, J. Salmi, P. Hruby, S. Nagy\n");
  1687. else if(argc != 1)
  1688. eprint("usage: dwm [-v]\n");
  1689. setlocale(LC_CTYPE, "");
  1690. if(!(dpy = XOpenDisplay(0)))
  1691. eprint("dwm: cannot open display\n");
  1692. screen = DefaultScreen(dpy);
  1693. root = RootWindow(dpy, screen);
  1694. checkotherwm();
  1695. setup();
  1696. drawbar();
  1697. scan();
  1698. run();
  1699. cleanup();
  1700. XCloseDisplay(dpy);
  1701. return 0;
  1702. }