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.

1827 lines
42 KiB

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