Configuration of dwm for Mac Computers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1921 lines
44 KiB

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