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.

2016 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
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 domwfact = True;
  220. Bool dozoom = True;
  221. Bool otherwm, readin;
  222. Bool running = True;
  223. Bool *prevtags;
  224. Bool *seltags;
  225. Client *clients = NULL;
  226. Client *sel = NULL;
  227. Client *stack = NULL;
  228. Cursor cursor[CurLast];
  229. Display *dpy;
  230. DC dc = {0};
  231. View *views;
  232. View *selview;
  233. Window root;
  234. /* configuration, allows nested code to access above variables */
  235. #include "config.h"
  236. #define TAGSZ (LENGTH(tags) * sizeof(Bool))
  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, TAGSZ);
  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. if(&views[vtags[i]] != selview)
  320. continue;
  321. x += textw(tags[i]);
  322. if(ev->x < x) {
  323. if(ev->button == Button1) {
  324. if(ev->state & MODKEY)
  325. tag(tags[i]);
  326. else
  327. view(tags[i]);
  328. }
  329. else if(ev->button == Button3) {
  330. if(ev->state & MODKEY)
  331. toggletag(tags[i]);
  332. else
  333. toggleview(tags[i]);
  334. }
  335. return;
  336. }
  337. }
  338. if((ev->x < x + blw) && ev->button == Button1)
  339. setlayout(NULL);
  340. }
  341. else if((c = getclient(ev->window))) {
  342. focus(c);
  343. if(CLEANMASK(ev->state) != MODKEY)
  344. return;
  345. if(ev->button == Button1) {
  346. restack(c->view);
  347. movemouse(c);
  348. }
  349. else if(ev->button == Button2) {
  350. if((floating != c->view->layout->arrange) && c->isfloating)
  351. togglefloating(NULL);
  352. else
  353. zoom(NULL);
  354. }
  355. else if(ev->button == Button3 && !c->isfixed) {
  356. restack(c->view);
  357. resizemouse(c);
  358. }
  359. }
  360. }
  361. void
  362. checkotherwm(void) {
  363. otherwm = False;
  364. XSetErrorHandler(xerrorstart);
  365. /* this causes an error if some other window manager is running */
  366. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  367. XSync(dpy, False);
  368. if(otherwm)
  369. eprint("dwm: another window manager is already running\n");
  370. XSync(dpy, False);
  371. XSetErrorHandler(NULL);
  372. xerrorxlib = XSetErrorHandler(xerror);
  373. XSync(dpy, False);
  374. }
  375. void
  376. cleanup(void) {
  377. unsigned int i;
  378. close(STDIN_FILENO);
  379. while(stack) {
  380. unban(stack);
  381. unmanage(stack);
  382. }
  383. if(dc.font.set)
  384. XFreeFontSet(dpy, dc.font.set);
  385. else
  386. XFreeFont(dpy, dc.font.xfont);
  387. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  388. XFreePixmap(dpy, dc.drawable);
  389. XFreeGC(dpy, dc.gc);
  390. XFreeCursor(dpy, cursor[CurNormal]);
  391. XFreeCursor(dpy, cursor[CurResize]);
  392. XFreeCursor(dpy, cursor[CurMove]);
  393. for(i = 0; i < nviews; i++)
  394. XDestroyWindow(dpy, views[i].barwin);
  395. XSync(dpy, False);
  396. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  397. }
  398. void
  399. configure(Client *c) {
  400. XConfigureEvent ce;
  401. ce.type = ConfigureNotify;
  402. ce.display = dpy;
  403. ce.event = c->win;
  404. ce.window = c->win;
  405. ce.x = c->x;
  406. ce.y = c->y;
  407. ce.width = c->w;
  408. ce.height = c->h;
  409. ce.border_width = c->border;
  410. ce.above = None;
  411. ce.override_redirect = False;
  412. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  413. }
  414. void
  415. configurenotify(XEvent *e) {
  416. XConfigureEvent *ev = &e->xconfigure;
  417. View *v = selview;
  418. if(ev->window == root && (ev->width != v->w || ev->height != v->h)) {
  419. /* TODO -- update Xinerama dimensions here */
  420. v->w = ev->width;
  421. v->h = ev->height;
  422. XFreePixmap(dpy, dc.drawable);
  423. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(root, screen), bh, DefaultDepth(dpy, screen));
  424. XResizeWindow(dpy, v->barwin, v->w, bh);
  425. updatebarpos(selview);
  426. arrange();
  427. }
  428. }
  429. void
  430. configurerequest(XEvent *e) {
  431. Client *c;
  432. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  433. XWindowChanges wc;
  434. if((c = getclient(ev->window))) {
  435. View *v = c->view;
  436. if(ev->value_mask & CWBorderWidth)
  437. c->border = ev->border_width;
  438. if(c->isfixed || c->isfloating || (floating == v->layout->arrange)) {
  439. if(ev->value_mask & CWX)
  440. c->x = v->x + ev->x;
  441. if(ev->value_mask & CWY)
  442. c->y = v->y + ev->y;
  443. if(ev->value_mask & CWWidth)
  444. c->w = ev->width;
  445. if(ev->value_mask & CWHeight)
  446. c->h = ev->height;
  447. if((c->x - v->x + c->w) > v->w && c->isfloating)
  448. c->x = v->x + (v->w / 2 - c->w / 2); /* center in x direction */
  449. if((c->y - v->y + c->h) > v->h && c->isfloating)
  450. c->y = v->y + (v->h / 2 - c->h / 2); /* center in y direction */
  451. if((ev->value_mask & (CWX|CWY))
  452. && !(ev->value_mask & (CWWidth|CWHeight)))
  453. configure(c);
  454. if(isvisible(c))
  455. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  456. }
  457. else
  458. configure(c);
  459. }
  460. else {
  461. wc.x = ev->x;
  462. wc.y = ev->y;
  463. wc.width = ev->width;
  464. wc.height = ev->height;
  465. wc.border_width = ev->border_width;
  466. wc.sibling = ev->above;
  467. wc.stack_mode = ev->detail;
  468. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  469. }
  470. XSync(dpy, False);
  471. }
  472. void
  473. destroynotify(XEvent *e) {
  474. Client *c;
  475. XDestroyWindowEvent *ev = &e->xdestroywindow;
  476. if((c = getclient(ev->window)))
  477. unmanage(c);
  478. }
  479. void
  480. detach(Client *c) {
  481. if(c->prev)
  482. c->prev->next = c->next;
  483. if(c->next)
  484. c->next->prev = c->prev;
  485. if(c == clients)
  486. clients = c->next;
  487. c->next = c->prev = NULL;
  488. }
  489. void
  490. detachstack(Client *c) {
  491. Client **tc;
  492. for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
  493. *tc = c->snext;
  494. }
  495. void
  496. drawbar(View *v) {
  497. int i, x;
  498. Client *c;
  499. dc.x = 0;
  500. for(c = stack; c && (!isvisible(c) || c->view != v); c = c->snext);
  501. for(i = 0; i < LENGTH(tags); i++) {
  502. if(&views[vtags[i]] != v)
  503. continue;
  504. dc.w = textw(tags[i]);
  505. if(seltags[i]) {
  506. drawtext(tags[i], dc.sel, isurgent(i));
  507. drawsquare(c && c->tags[i], isoccupied(i), isurgent(i), dc.sel);
  508. }
  509. else {
  510. drawtext(tags[i], dc.norm, isurgent(i));
  511. drawsquare(c && c->tags[i], isoccupied(i), isurgent(i), dc.norm);
  512. }
  513. dc.x += dc.w;
  514. }
  515. dc.w = blw;
  516. drawtext(v->layout->symbol, dc.norm, False);
  517. x = dc.x + dc.w;
  518. if(v == selview) {
  519. dc.w = textw(stext);
  520. dc.x = v->w - dc.w;
  521. if(dc.x < x) {
  522. dc.x = x;
  523. dc.w = v->w - x;
  524. }
  525. drawtext(stext, dc.norm, False);
  526. }
  527. else
  528. dc.x = v->w;
  529. if((dc.w = dc.x - x) > bh) {
  530. dc.x = x;
  531. if(c) {
  532. drawtext(c->name, dc.sel, False);
  533. drawsquare(False, c->isfloating, False, dc.sel);
  534. }
  535. else
  536. drawtext(NULL, dc.norm, False);
  537. }
  538. XCopyArea(dpy, dc.drawable, v->barwin, dc.gc, 0, 0, v->w, bh, 0, 0);
  539. XSync(dpy, False);
  540. }
  541. void
  542. drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  543. int x;
  544. XGCValues gcv;
  545. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  546. gcv.foreground = col[invert ? ColBG : ColFG];
  547. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  548. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  549. r.x = dc.x + 1;
  550. r.y = dc.y + 1;
  551. if(filled) {
  552. r.width = r.height = x + 1;
  553. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  554. }
  555. else if(empty) {
  556. r.width = r.height = x;
  557. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  558. }
  559. }
  560. void
  561. drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
  562. int x, y, w, h;
  563. unsigned int len, olen;
  564. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  565. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  566. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  567. if(!text)
  568. return;
  569. w = 0;
  570. olen = len = strlen(text);
  571. if(len >= sizeof buf)
  572. len = sizeof buf - 1;
  573. memcpy(buf, text, len);
  574. buf[len] = 0;
  575. h = dc.font.ascent + dc.font.descent;
  576. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  577. x = dc.x + (h / 2);
  578. /* shorten text if necessary */
  579. while(len && (w = textnw(buf, len)) > dc.w - h)
  580. buf[--len] = 0;
  581. if(len < olen) {
  582. if(len > 1)
  583. buf[len - 1] = '.';
  584. if(len > 2)
  585. buf[len - 2] = '.';
  586. if(len > 3)
  587. buf[len - 3] = '.';
  588. }
  589. if(w > dc.w)
  590. return; /* too long */
  591. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  592. if(dc.font.set)
  593. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  594. else
  595. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  596. }
  597. void *
  598. emallocz(unsigned int size) {
  599. void *res = calloc(1, size);
  600. if(!res)
  601. eprint("fatal: could not malloc() %u bytes\n", size);
  602. return res;
  603. }
  604. void
  605. enternotify(XEvent *e) {
  606. Client *c;
  607. XCrossingEvent *ev = &e->xcrossing;
  608. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  609. return;
  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(TAGSZ);
  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, TAGSZ);
  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, j;
  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(XineramaIsActive(dpy))
  1383. info = XineramaQueryScreens(dpy, &nviews);
  1384. #if defined(AIM_XINERAMA)
  1385. nviews = 2; /* aim Xinerama */
  1386. #endif
  1387. views = emallocz(nviews * sizeof(View));
  1388. screen = DefaultScreen(dpy);
  1389. root = RootWindow(dpy, screen);
  1390. /* init appearance */
  1391. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  1392. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  1393. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  1394. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  1395. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  1396. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  1397. initfont(FONT);
  1398. dc.h = bh = dc.font.height + 2;
  1399. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1400. dc.gc = XCreateGC(dpy, root, 0, 0);
  1401. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1402. if(!dc.font.set)
  1403. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1404. for(blw = i = 0; i < LENGTH(layouts); i++) {
  1405. i = textw(layouts[i].symbol);
  1406. if(i > blw)
  1407. blw = i;
  1408. }
  1409. seltags = emallocz(TAGSZ);
  1410. prevtags = emallocz(TAGSZ);
  1411. /* check, if vtags need assistance, because there is only 1 view */
  1412. if(nviews == 1)
  1413. for(i = 0; i < LENGTH(tags); i++)
  1414. vtags[i] = 0;
  1415. for(i = 0; i < nviews; i++) {
  1416. /* init geometry */
  1417. v = &views[i];
  1418. /* select first tag in each view */
  1419. for(j = 0; j < LENGTH(tags); j++)
  1420. if(vtags[j] == i) {
  1421. seltags[j] = prevtags[j] = True;
  1422. break;
  1423. }
  1424. if(info) {
  1425. #if defined(AIM_XINERAMA)
  1426. v->w = DisplayWidth(dpy, screen) / 2;
  1427. v->x = (i == 0) ? 0 : v->w;
  1428. v->y = 0;
  1429. v->h = DisplayHeight(dpy, screen);
  1430. #else
  1431. v->x = info[i].x_org;
  1432. v->y = info[i].y_org;
  1433. v->w = info[i].width;
  1434. v->h = info[i].height;
  1435. #endif
  1436. }
  1437. else {
  1438. v->x = 0;
  1439. v->y = 0;
  1440. v->w = DisplayWidth(dpy, screen);
  1441. v->h = DisplayHeight(dpy, screen);
  1442. }
  1443. /* init layouts */
  1444. v->mwfact = MWFACT;
  1445. v->layout = &layouts[0];
  1446. // TODO: bpos per screen?
  1447. bpos = BARPOS;
  1448. wa.override_redirect = 1;
  1449. wa.background_pixmap = ParentRelative;
  1450. wa.event_mask = ButtonPressMask|ExposureMask;
  1451. /* init bars */
  1452. v->barwin = XCreateWindow(dpy, root, v->x, v->y, v->w, bh, 0,
  1453. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  1454. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1455. XDefineCursor(dpy, v->barwin, cursor[CurNormal]);
  1456. updatebarpos(v);
  1457. XMapRaised(dpy, v->barwin);
  1458. strcpy(stext, "dwm-"VERSION);
  1459. /* EWMH support per view */
  1460. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1461. PropModeReplace, (unsigned char *) netatom, NetLast);
  1462. /* select for events */
  1463. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
  1464. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask;
  1465. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1466. XSelectInput(dpy, root, wa.event_mask);
  1467. drawbar(v);
  1468. }
  1469. if(info)
  1470. XFree(info);
  1471. selview = viewat();
  1472. /* grab keys */
  1473. grabkeys();
  1474. }
  1475. void
  1476. spawn(const char *arg) {
  1477. static char *shell = NULL;
  1478. if(!shell && !(shell = getenv("SHELL")))
  1479. shell = "/bin/sh";
  1480. if(!arg)
  1481. return;
  1482. /* The double-fork construct avoids zombie processes and keeps the code
  1483. * clean from stupid signal handlers. */
  1484. if(fork() == 0) {
  1485. if(fork() == 0) {
  1486. if(dpy)
  1487. close(ConnectionNumber(dpy));
  1488. setsid();
  1489. execl(shell, shell, "-c", arg, (char *)NULL);
  1490. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
  1491. perror(" failed");
  1492. }
  1493. exit(0);
  1494. }
  1495. wait(0);
  1496. }
  1497. void
  1498. tag(const char *arg) {
  1499. unsigned int i;
  1500. if(!sel)
  1501. return;
  1502. for(i = 0; i < LENGTH(tags); i++)
  1503. sel->tags[i] = (NULL == arg);
  1504. sel->tags[idxoftag(arg)] = True;
  1505. arrange();
  1506. }
  1507. unsigned int
  1508. textnw(const char *text, unsigned int len) {
  1509. XRectangle r;
  1510. if(dc.font.set) {
  1511. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1512. return r.width;
  1513. }
  1514. return XTextWidth(dc.font.xfont, text, len);
  1515. }
  1516. unsigned int
  1517. textw(const char *text) {
  1518. return textnw(text, strlen(text)) + dc.font.height;
  1519. }
  1520. void
  1521. tile(View *v) {
  1522. unsigned int i, n, nx, ny, nw, nh, mw, th;
  1523. Client *c, *mc;
  1524. domwfact = dozoom = True;
  1525. nx = v->wax;
  1526. ny = v->way;
  1527. nw = 0;
  1528. for(n = 0, c = nexttiled(clients, v); c; c = nexttiled(c->next, v))
  1529. n++;
  1530. /* window geoms */
  1531. mw = (n == 1) ? v->waw : v->mwfact * v->waw;
  1532. th = (n > 1) ? v->wah / (n - 1) : 0;
  1533. if(n > 1 && th < bh)
  1534. th = v->wah;
  1535. for(i = 0, c = mc = nexttiled(clients, v); c; c = nexttiled(c->next, v)) {
  1536. if(i == 0) { /* master */
  1537. nw = mw - 2 * c->border;
  1538. nh = v->wah - 2 * c->border;
  1539. }
  1540. else { /* tile window */
  1541. if(i == 1) {
  1542. ny = v->way;
  1543. nx += mc->w + 2 * mc->border;
  1544. nw = v->waw - mw - 2 * c->border;
  1545. }
  1546. if(i + 1 == n) /* remainder */
  1547. nh = (v->way + v->wah) - ny - 2 * c->border;
  1548. else
  1549. nh = th - 2 * c->border;
  1550. }
  1551. resize(c, nx, ny, nw, nh, RESIZEHINTS);
  1552. if((RESIZEHINTS) && ((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw)))
  1553. /* client doesn't accept size constraints */
  1554. resize(c, nx, ny, nw, nh, False);
  1555. if(n > 1 && th != v->wah)
  1556. ny = c->y + c->h + 2 * c->border;
  1557. i++;
  1558. }
  1559. }
  1560. void
  1561. togglebar(const char *arg) {
  1562. if(bpos == BarOff)
  1563. bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
  1564. else
  1565. bpos = BarOff;
  1566. updatebarpos(selview);
  1567. arrange();
  1568. }
  1569. void
  1570. togglefloating(const char *arg) {
  1571. if(!sel)
  1572. return;
  1573. sel->isfloating = !sel->isfloating;
  1574. if(sel->isfloating)
  1575. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1576. arrange();
  1577. }
  1578. void
  1579. toggletag(const char *arg) {
  1580. unsigned int i, j;
  1581. if(!sel)
  1582. return;
  1583. i = idxoftag(arg);
  1584. sel->tags[i] = !sel->tags[i];
  1585. for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
  1586. if(j == LENGTH(tags))
  1587. sel->tags[i] = True; /* at least one tag must be enabled */
  1588. arrange();
  1589. }
  1590. void
  1591. toggleview(const char *arg) {
  1592. unsigned int i, j;
  1593. i = idxoftag(arg);
  1594. seltags[i] = !seltags[i];
  1595. for(j = 0; j < LENGTH(tags) && !seltags[j]; j++);
  1596. if(j == LENGTH(tags))
  1597. seltags[i] = True; /* at least one tag must be viewed */
  1598. arrange();
  1599. }
  1600. void
  1601. unban(Client *c) {
  1602. if(!c->isbanned)
  1603. return;
  1604. XMoveWindow(dpy, c->win, c->x, c->y);
  1605. c->isbanned = False;
  1606. }
  1607. void
  1608. unmanage(Client *c) {
  1609. XWindowChanges wc;
  1610. wc.border_width = c->oldborder;
  1611. /* The server grab construct avoids race conditions. */
  1612. XGrabServer(dpy);
  1613. XSetErrorHandler(xerrordummy);
  1614. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1615. detach(c);
  1616. detachstack(c);
  1617. if(sel == c)
  1618. focus(NULL);
  1619. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1620. setclientstate(c, WithdrawnState);
  1621. free(c->tags);
  1622. free(c);
  1623. XSync(dpy, False);
  1624. XSetErrorHandler(xerror);
  1625. XUngrabServer(dpy);
  1626. arrange();
  1627. }
  1628. void
  1629. unmapnotify(XEvent *e) {
  1630. Client *c;
  1631. XUnmapEvent *ev = &e->xunmap;
  1632. if((c = getclient(ev->window)))
  1633. unmanage(c);
  1634. }
  1635. void
  1636. updatebarpos(View *v) {
  1637. XEvent ev;
  1638. v->wax = v->x;
  1639. v->way = v->y;
  1640. v->wah = v->h;
  1641. v->waw = v->w;
  1642. switch(bpos) {
  1643. default:
  1644. v->wah -= bh;
  1645. v->way += bh;
  1646. XMoveWindow(dpy, v->barwin, v->x, v->y);
  1647. break;
  1648. case BarBot:
  1649. v->wah -= bh;
  1650. XMoveWindow(dpy, v->barwin, v->x, v->y + v->wah);
  1651. break;
  1652. case BarOff:
  1653. XMoveWindow(dpy, v->barwin, v->x, v->y - bh);
  1654. break;
  1655. }
  1656. XSync(dpy, False);
  1657. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1658. }
  1659. void
  1660. updatesizehints(Client *c) {
  1661. long msize;
  1662. XSizeHints size;
  1663. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  1664. size.flags = PSize;
  1665. c->flags = size.flags;
  1666. if(c->flags & PBaseSize) {
  1667. c->basew = size.base_width;
  1668. c->baseh = size.base_height;
  1669. }
  1670. else if(c->flags & PMinSize) {
  1671. c->basew = size.min_width;
  1672. c->baseh = size.min_height;
  1673. }
  1674. else
  1675. c->basew = c->baseh = 0;
  1676. if(c->flags & PResizeInc) {
  1677. c->incw = size.width_inc;
  1678. c->inch = size.height_inc;
  1679. }
  1680. else
  1681. c->incw = c->inch = 0;
  1682. if(c->flags & PMaxSize) {
  1683. c->maxw = size.max_width;
  1684. c->maxh = size.max_height;
  1685. }
  1686. else
  1687. c->maxw = c->maxh = 0;
  1688. if(c->flags & PMinSize) {
  1689. c->minw = size.min_width;
  1690. c->minh = size.min_height;
  1691. }
  1692. else if(c->flags & PBaseSize) {
  1693. c->minw = size.base_width;
  1694. c->minh = size.base_height;
  1695. }
  1696. else
  1697. c->minw = c->minh = 0;
  1698. if(c->flags & PAspect) {
  1699. c->minax = size.min_aspect.x;
  1700. c->maxax = size.max_aspect.x;
  1701. c->minay = size.min_aspect.y;
  1702. c->maxay = size.max_aspect.y;
  1703. }
  1704. else
  1705. c->minax = c->maxax = c->minay = c->maxay = 0;
  1706. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1707. && c->maxw == c->minw && c->maxh == c->minh);
  1708. }
  1709. void
  1710. updatetitle(Client *c) {
  1711. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1712. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1713. }
  1714. void
  1715. updatewmhints(Client *c) {
  1716. XWMHints *wmh;
  1717. if((wmh = XGetWMHints(dpy, c->win))) {
  1718. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1719. XFree(wmh);
  1720. }
  1721. }
  1722. void
  1723. view(const char *arg) {
  1724. unsigned int i;
  1725. Bool tmp[LENGTH(tags)];
  1726. for(i = 0; i < LENGTH(tags); i++)
  1727. tmp[i] = (NULL == arg);
  1728. tmp[idxoftag(arg)] = True;
  1729. if(memcmp(seltags, tmp, TAGSZ) != 0) {
  1730. memcpy(prevtags, seltags, TAGSZ);
  1731. memcpy(seltags, tmp, TAGSZ);
  1732. arrange();
  1733. }
  1734. }
  1735. View *
  1736. viewat() {
  1737. int i, x, y;
  1738. Window win;
  1739. unsigned int mask;
  1740. XQueryPointer(dpy, root, &win, &win, &x, &y, &i, &i, &mask);
  1741. for(i = 0; i < nviews; i++) {
  1742. if((x >= views[i].x && x < views[i].x + views[i].w)
  1743. && (y >= views[i].y && y < views[i].y + views[i].h))
  1744. return &views[i];
  1745. }
  1746. return NULL;
  1747. }
  1748. void
  1749. viewprevtag(const char *arg) {
  1750. static Bool tmp[LENGTH(tags)];
  1751. memcpy(tmp, seltags, TAGSZ);
  1752. memcpy(seltags, prevtags, TAGSZ);
  1753. memcpy(prevtags, tmp, TAGSZ);
  1754. arrange();
  1755. }
  1756. /* There's no way to check accesses to destroyed windows, thus those cases are
  1757. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1758. * default error handler, which may call exit. */
  1759. int
  1760. xerror(Display *dpy, XErrorEvent *ee) {
  1761. if(ee->error_code == BadWindow
  1762. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1763. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1764. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1765. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1766. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1767. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1768. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1769. return 0;
  1770. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1771. ee->request_code, ee->error_code);
  1772. return xerrorxlib(dpy, ee); /* may call exit */
  1773. }
  1774. int
  1775. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1776. return 0;
  1777. }
  1778. /* Startup Error handler to check if another window manager
  1779. * is already running. */
  1780. int
  1781. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1782. otherwm = True;
  1783. return -1;
  1784. }
  1785. void
  1786. zoom(const char *arg) {
  1787. Client *c = sel;
  1788. if(!sel || !dozoom || sel->isfloating)
  1789. return;
  1790. if(c == nexttiled(clients, c->view))
  1791. if(!(c = nexttiled(c->next, c->view)))
  1792. return;
  1793. detach(c);
  1794. attach(c);
  1795. focus(c);
  1796. arrange();
  1797. }
  1798. int
  1799. main(int argc, char *argv[]) {
  1800. if(argc == 2 && !strcmp("-v", argv[1]))
  1801. eprint("dwm-"VERSION", © 2006-2008 dwm engineers, see LICENSE for details\n");
  1802. else if(argc != 1)
  1803. eprint("usage: dwm [-v]\n");
  1804. setlocale(LC_CTYPE, "");
  1805. if(!(dpy = XOpenDisplay(0)))
  1806. eprint("dwm: cannot open display\n");
  1807. checkotherwm();
  1808. setup();
  1809. scan();
  1810. run();
  1811. cleanup();
  1812. XCloseDisplay(dpy);
  1813. return 0;
  1814. }