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.

2009 lines
46 KiB

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