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

2009 lines
46 KiB

16 years ago
17 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
  1. /* See LICENSE file for copyright and license details.
  2. *
  3. * dynamic window manager is designed like any other X client as well. It is
  4. * driven through handling X events. In contrast to other X clients, a window
  5. * manager selects for SubstructureRedirectMask on the root window, to receive
  6. * events about window (dis-)appearance. Only one X connection at a time is
  7. * allowed to select for this event mask.
  8. *
  9. * Calls to fetch an X event from the event queue are blocking. Due reading
  10. * status text from standard input, a select()-driven main loop has been
  11. * implemented which selects for reads on the X connection and STDIN_FILENO to
  12. * handle all data smoothly. The event handlers of dwm are organized in an
  13. * array which is accessed whenever a new event has been fetched. This allows
  14. * event dispatching in O(1) time.
  15. *
  16. * Each child of the root window is called a client, except windows which have
  17. * set the override_redirect flag. Clients are organized in a global
  18. * doubly-linked client list, the focus history is remembered through a global
  19. * stack list. Each client contains an array of Bools of the same size as the
  20. * global tags array to indicate the tags of a client.
  21. *
  22. * Keys and tagging rules are organized as arrays and defined in config.h.
  23. *
  24. * To understand everything else, start reading main().
  25. */
  26. #include <errno.h>
  27. #include <locale.h>
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <sys/select.h>
  34. #include <sys/types.h>
  35. #include <sys/wait.h>
  36. #include <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. 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) {
  609. if(!isxinerama || ev->window != root)
  610. return;
  611. }
  612. if((c = getclient(ev->window)))
  613. focus(c);
  614. else
  615. focus(NULL);
  616. }
  617. void
  618. eprint(const char *errstr, ...) {
  619. va_list ap;
  620. va_start(ap, errstr);
  621. vfprintf(stderr, errstr, ap);
  622. va_end(ap);
  623. exit(EXIT_FAILURE);
  624. }
  625. void
  626. expose(XEvent *e) {
  627. View *v;
  628. XExposeEvent *ev = &e->xexpose;
  629. if(ev->count == 0 && ((v = getviewbar(ev->window))))
  630. drawbar(v);
  631. }
  632. void
  633. floating(View *v) { /* default floating layout */
  634. Client *c;
  635. domwfact = dozoom = False;
  636. for(c = clients; c; c = c->next)
  637. if(isvisible(c))
  638. resize(c, c->x, c->y, c->w, c->h, True);
  639. }
  640. void
  641. focus(Client *c) {
  642. View *v = selview;
  643. if(c)
  644. selview = c->view;
  645. else
  646. selview = viewat();
  647. if(selview != v)
  648. drawbar(v);
  649. if(!c || (c && !isvisible(c)))
  650. for(c = stack; c && (!isvisible(c) || c->view != selview); c = c->snext);
  651. if(sel && sel != c) {
  652. grabbuttons(sel, False);
  653. XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
  654. }
  655. if(c) {
  656. detachstack(c);
  657. attachstack(c);
  658. grabbuttons(c, True);
  659. }
  660. sel = c;
  661. if(c) {
  662. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  663. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  664. selview = c->view;
  665. }
  666. else
  667. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  668. drawbar(selview);
  669. }
  670. void
  671. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  672. XFocusChangeEvent *ev = &e->xfocus;
  673. if(sel && ev->window != sel->win)
  674. XSetInputFocus(dpy, sel->win, RevertToPointerRoot, CurrentTime);
  675. }
  676. void
  677. focusnext(const char *arg) {
  678. Client *c;
  679. if(!sel)
  680. return;
  681. for(c = sel->next; c && !isvisible(c); c = c->next);
  682. if(!c)
  683. for(c = clients; c && !isvisible(c); c = c->next);
  684. if(c) {
  685. focus(c);
  686. restack(c->view);
  687. }
  688. }
  689. void
  690. focusprev(const char *arg) {
  691. Client *c;
  692. if(!sel)
  693. return;
  694. for(c = sel->prev; c && !isvisible(c); c = c->prev);
  695. if(!c) {
  696. for(c = clients; c && c->next; c = c->next);
  697. for(; c && !isvisible(c); c = c->prev);
  698. }
  699. if(c) {
  700. focus(c);
  701. restack(c->view);
  702. }
  703. }
  704. Client *
  705. getclient(Window w) {
  706. Client *c;
  707. for(c = clients; c && c->win != w; c = c->next);
  708. return c;
  709. }
  710. unsigned long
  711. getcolor(const char *colstr) {
  712. Colormap cmap = DefaultColormap(dpy, screen);
  713. XColor color;
  714. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  715. eprint("error, cannot allocate color '%s'\n", colstr);
  716. return color.pixel;
  717. }
  718. View *
  719. getviewbar(Window barwin) {
  720. unsigned int i;
  721. for(i = 0; i < nviews; i++)
  722. if(views[i].barwin == barwin)
  723. return &views[i];
  724. return NULL;
  725. }
  726. long
  727. getstate(Window w) {
  728. int format, status;
  729. long result = -1;
  730. unsigned char *p = NULL;
  731. unsigned long n, extra;
  732. Atom real;
  733. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  734. &real, &format, &n, &extra, (unsigned char **)&p);
  735. if(status != Success)
  736. return -1;
  737. if(n != 0)
  738. result = *p;
  739. XFree(p);
  740. return result;
  741. }
  742. Bool
  743. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  744. char **list = NULL;
  745. int n;
  746. XTextProperty name;
  747. if(!text || size == 0)
  748. return False;
  749. text[0] = '\0';
  750. XGetTextProperty(dpy, w, &name, atom);
  751. if(!name.nitems)
  752. return False;
  753. if(name.encoding == XA_STRING)
  754. strncpy(text, (char *)name.value, size - 1);
  755. else {
  756. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  757. && n > 0 && *list) {
  758. strncpy(text, *list, size - 1);
  759. XFreeStringList(list);
  760. }
  761. }
  762. text[size - 1] = '\0';
  763. XFree(name.value);
  764. return True;
  765. }
  766. void
  767. grabbuttons(Client *c, Bool focused) {
  768. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  769. if(focused) {
  770. XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  771. GrabModeAsync, GrabModeSync, None, None);
  772. XGrabButton(dpy, Button1, MODKEY|LockMask, c->win, False, BUTTONMASK,
  773. GrabModeAsync, GrabModeSync, None, None);
  774. XGrabButton(dpy, Button1, MODKEY|numlockmask, c->win, False, BUTTONMASK,
  775. GrabModeAsync, GrabModeSync, None, None);
  776. XGrabButton(dpy, Button1, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK,
  777. GrabModeAsync, GrabModeSync, None, None);
  778. XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  779. GrabModeAsync, GrabModeSync, None, None);
  780. XGrabButton(dpy, Button2, MODKEY|LockMask, c->win, False, BUTTONMASK,
  781. GrabModeAsync, GrabModeSync, None, None);
  782. XGrabButton(dpy, Button2, MODKEY|numlockmask, c->win, False, BUTTONMASK,
  783. GrabModeAsync, GrabModeSync, None, None);
  784. XGrabButton(dpy, Button2, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK,
  785. GrabModeAsync, GrabModeSync, None, None);
  786. XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  787. GrabModeAsync, GrabModeSync, None, None);
  788. XGrabButton(dpy, Button3, MODKEY|LockMask, c->win, False, BUTTONMASK,
  789. GrabModeAsync, GrabModeSync, None, None);
  790. XGrabButton(dpy, Button3, MODKEY|numlockmask, c->win, False, BUTTONMASK,
  791. GrabModeAsync, GrabModeSync, None, None);
  792. XGrabButton(dpy, Button3, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK,
  793. GrabModeAsync, GrabModeSync, None, None);
  794. }
  795. else
  796. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
  797. GrabModeAsync, GrabModeSync, None, None);
  798. }
  799. void
  800. grabkeys(void) {
  801. unsigned int i, j;
  802. KeyCode code;
  803. XModifierKeymap *modmap;
  804. /* init modifier map */
  805. modmap = XGetModifierMapping(dpy);
  806. for(i = 0; i < 8; i++)
  807. for(j = 0; j < modmap->max_keypermod; j++) {
  808. if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
  809. numlockmask = (1 << i);
  810. }
  811. XFreeModifiermap(modmap);
  812. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  813. for(i = 0; i < LENGTH(keys); i++) {
  814. code = XKeysymToKeycode(dpy, keys[i].keysym);
  815. XGrabKey(dpy, code, keys[i].mod, root, True,
  816. GrabModeAsync, GrabModeAsync);
  817. XGrabKey(dpy, code, keys[i].mod|LockMask, root, True,
  818. GrabModeAsync, GrabModeAsync);
  819. XGrabKey(dpy, code, keys[i].mod|numlockmask, root, True,
  820. GrabModeAsync, GrabModeAsync);
  821. XGrabKey(dpy, code, keys[i].mod|numlockmask|LockMask, root, True,
  822. GrabModeAsync, GrabModeAsync);
  823. }
  824. }
  825. unsigned int
  826. idxoftag(const char *t) {
  827. unsigned int i;
  828. for(i = 0; (i < LENGTH(tags)) && (tags[i] != t); i++);
  829. return (i < LENGTH(tags)) ? i : 0;
  830. }
  831. void
  832. initfont(const char *fontstr) {
  833. char *def, **missing;
  834. int i, n;
  835. missing = NULL;
  836. if(dc.font.set)
  837. XFreeFontSet(dpy, dc.font.set);
  838. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  839. if(missing) {
  840. while(n--)
  841. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  842. XFreeStringList(missing);
  843. }
  844. if(dc.font.set) {
  845. XFontSetExtents *font_extents;
  846. XFontStruct **xfonts;
  847. char **font_names;
  848. dc.font.ascent = dc.font.descent = 0;
  849. font_extents = XExtentsOfFontSet(dc.font.set);
  850. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  851. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  852. if(dc.font.ascent < (*xfonts)->ascent)
  853. dc.font.ascent = (*xfonts)->ascent;
  854. if(dc.font.descent < (*xfonts)->descent)
  855. dc.font.descent = (*xfonts)->descent;
  856. xfonts++;
  857. }
  858. }
  859. else {
  860. if(dc.font.xfont)
  861. XFreeFont(dpy, dc.font.xfont);
  862. dc.font.xfont = NULL;
  863. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  864. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  865. eprint("error, cannot load font: '%s'\n", fontstr);
  866. dc.font.ascent = dc.font.xfont->ascent;
  867. dc.font.descent = dc.font.xfont->descent;
  868. }
  869. dc.font.height = dc.font.ascent + dc.font.descent;
  870. }
  871. Bool
  872. isoccupied(unsigned int t) {
  873. Client *c;
  874. for(c = clients; c; c = c->next)
  875. if(c->tags[t])
  876. return True;
  877. return False;
  878. }
  879. Bool
  880. isprotodel(Client *c) {
  881. int i, n;
  882. Atom *protocols;
  883. Bool ret = False;
  884. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  885. for(i = 0; !ret && i < n; i++)
  886. if(protocols[i] == wmatom[WMDelete])
  887. ret = True;
  888. XFree(protocols);
  889. }
  890. return ret;
  891. }
  892. Bool
  893. isurgent(unsigned int t) {
  894. Client *c;
  895. for(c = clients; c; c = c->next)
  896. if(c->isurgent && c->tags[t])
  897. return True;
  898. return False;
  899. }
  900. Bool
  901. isvisible(Client *c) {
  902. unsigned int i;
  903. for(i = 0; i < LENGTH(tags); i++)
  904. if(c->tags[i] && seltags[i])
  905. return True;
  906. return False;
  907. }
  908. void
  909. keypress(XEvent *e) {
  910. unsigned int i;
  911. KeySym keysym;
  912. XKeyEvent *ev;
  913. ev = &e->xkey;
  914. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  915. for(i = 0; i < LENGTH(keys); i++)
  916. if(keysym == keys[i].keysym
  917. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state))
  918. {
  919. if(keys[i].func)
  920. keys[i].func(keys[i].arg);
  921. }
  922. }
  923. void
  924. killclient(const char *arg) {
  925. XEvent ev;
  926. if(!sel)
  927. return;
  928. if(isprotodel(sel)) {
  929. ev.type = ClientMessage;
  930. ev.xclient.window = sel->win;
  931. ev.xclient.message_type = wmatom[WMProtocols];
  932. ev.xclient.format = 32;
  933. ev.xclient.data.l[0] = wmatom[WMDelete];
  934. ev.xclient.data.l[1] = CurrentTime;
  935. XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
  936. }
  937. else
  938. XKillClient(dpy, sel->win);
  939. }
  940. void
  941. manage(Window w, XWindowAttributes *wa) {
  942. Client *c, *t = NULL;
  943. View *v;
  944. Status rettrans;
  945. Window trans;
  946. XWindowChanges wc;
  947. c = emallocz(sizeof(Client));
  948. c->tags = emallocz(sizeof initags);
  949. c->win = w;
  950. applyrules(c);
  951. v = c->view;
  952. c->x = wa->x + v->x;
  953. c->y = wa->y + v->y;
  954. c->w = wa->width;
  955. c->h = wa->height;
  956. c->oldborder = wa->border_width;
  957. if(c->w == v->w && c->h == v->h) {
  958. c->x = v->x;
  959. c->y = v->y;
  960. c->border = wa->border_width;
  961. }
  962. else {
  963. if(c->x + c->w + 2 * c->border > v->wax + v->waw)
  964. c->x = v->wax + v->waw - c->w - 2 * c->border;
  965. if(c->y + c->h + 2 * c->border > v->way + v->wah)
  966. c->y = v->way + v->wah - c->h - 2 * c->border;
  967. if(c->x < v->wax)
  968. c->x = v->wax;
  969. if(c->y < v->way)
  970. c->y = v->way;
  971. c->border = BORDERPX;
  972. }
  973. wc.border_width = c->border;
  974. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  975. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  976. configure(c); /* propagates border_width, if size doesn't change */
  977. updatesizehints(c);
  978. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  979. grabbuttons(c, False);
  980. updatetitle(c);
  981. if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
  982. for(t = clients; t && t->win != trans; t = t->next);
  983. if(t)
  984. memcpy(c->tags, t->tags, sizeof initags);
  985. if(!c->isfloating)
  986. c->isfloating = (rettrans == Success) || c->isfixed;
  987. attach(c);
  988. attachstack(c);
  989. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
  990. ban(c);
  991. XMapWindow(dpy, c->win);
  992. setclientstate(c, NormalState);
  993. arrange();
  994. }
  995. void
  996. mappingnotify(XEvent *e) {
  997. XMappingEvent *ev = &e->xmapping;
  998. XRefreshKeyboardMapping(ev);
  999. if(ev->request == MappingKeyboard)
  1000. grabkeys();
  1001. }
  1002. void
  1003. maprequest(XEvent *e) {
  1004. static XWindowAttributes wa;
  1005. XMapRequestEvent *ev = &e->xmaprequest;
  1006. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1007. return;
  1008. if(wa.override_redirect)
  1009. return;
  1010. if(!getclient(ev->window))
  1011. manage(ev->window, &wa);
  1012. }
  1013. void
  1014. movemouse(Client *c) {
  1015. int x1, y1, ocx, ocy, di, nx, ny;
  1016. unsigned int dui;
  1017. View *v;
  1018. Window dummy;
  1019. XEvent ev;
  1020. ocx = nx = c->x;
  1021. ocy = ny = c->y;
  1022. v = c->view;
  1023. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1024. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1025. return;
  1026. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  1027. for(;;) {
  1028. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1029. switch (ev.type) {
  1030. case ButtonRelease:
  1031. XUngrabPointer(dpy, CurrentTime);
  1032. return;
  1033. case ConfigureRequest:
  1034. case Expose:
  1035. case MapRequest:
  1036. handler[ev.type](&ev);
  1037. break;
  1038. case MotionNotify:
  1039. XSync(dpy, False);
  1040. nx = ocx + (ev.xmotion.x - x1);
  1041. ny = ocy + (ev.xmotion.y - y1);
  1042. if(abs(v->wax - nx) < SNAP)
  1043. nx = v->wax;
  1044. else if(abs((v->wax + v->waw) - (nx + c->w + 2 * c->border)) < SNAP)
  1045. nx = v->wax + v->waw - c->w - 2 * c->border;
  1046. if(abs(v->way - ny) < SNAP)
  1047. ny = v->way;
  1048. else if(abs((v->way + v->wah) - (ny + c->h + 2 * c->border)) < SNAP)
  1049. ny = v->way + v->wah - c->h - 2 * c->border;
  1050. if(!c->isfloating && (v->layout->arrange != floating) && (abs(nx - c->x) > SNAP || abs(ny - c->y) > SNAP))
  1051. togglefloating(NULL);
  1052. if((v->layout->arrange == floating) || c->isfloating)
  1053. resize(c, nx, ny, c->w, c->h, False);
  1054. break;
  1055. }
  1056. }
  1057. }
  1058. Client *
  1059. nexttiled(Client *c, View *v) {
  1060. for(; c && (c->isfloating || c->view != v || !isvisible(c)); c = c->next);
  1061. return c;
  1062. }
  1063. void
  1064. propertynotify(XEvent *e) {
  1065. Client *c;
  1066. Window trans;
  1067. XPropertyEvent *ev = &e->xproperty;
  1068. if(ev->state == PropertyDelete)
  1069. return; /* ignore */
  1070. if((c = getclient(ev->window))) {
  1071. switch (ev->atom) {
  1072. default: break;
  1073. case XA_WM_TRANSIENT_FOR:
  1074. XGetTransientForHint(dpy, c->win, &trans);
  1075. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  1076. arrange();
  1077. break;
  1078. case XA_WM_NORMAL_HINTS:
  1079. updatesizehints(c);
  1080. break;
  1081. case XA_WM_HINTS:
  1082. updatewmhints(c);
  1083. drawbar(c->view);
  1084. break;
  1085. }
  1086. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1087. updatetitle(c);
  1088. if(c == sel)
  1089. drawbar(selview);
  1090. }
  1091. }
  1092. }
  1093. void
  1094. quit(const char *arg) {
  1095. readin = running = False;
  1096. }
  1097. void
  1098. reapply(const char *arg) {
  1099. static Bool zerotags[LENGTH(tags)] = { 0 };
  1100. Client *c;
  1101. for(c = clients; c; c = c->next) {
  1102. memcpy(c->tags, zerotags, sizeof zerotags);
  1103. applyrules(c);
  1104. }
  1105. arrange();
  1106. }
  1107. void
  1108. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  1109. View *v;
  1110. XWindowChanges wc;
  1111. v = c->view;
  1112. if(sizehints) {
  1113. /* set minimum possible */
  1114. if (w < 1)
  1115. w = 1;
  1116. if (h < 1)
  1117. h = 1;
  1118. /* temporarily remove base dimensions */
  1119. w -= c->basew;
  1120. h -= c->baseh;
  1121. /* adjust for aspect limits */
  1122. if (c->minay > 0 && c->maxay > 0 && c->minax > 0 && c->maxax > 0) {
  1123. if (w * c->maxay > h * c->maxax)
  1124. w = h * c->maxax / c->maxay;
  1125. else if (w * c->minay < h * c->minax)
  1126. h = w * c->minay / c->minax;
  1127. }
  1128. /* adjust for increment value */
  1129. if(c->incw)
  1130. w -= w % c->incw;
  1131. if(c->inch)
  1132. h -= h % c->inch;
  1133. /* restore base dimensions */
  1134. w += c->basew;
  1135. h += c->baseh;
  1136. if(c->minw > 0 && w < c->minw)
  1137. w = c->minw;
  1138. if(c->minh > 0 && h < c->minh)
  1139. h = c->minh;
  1140. if(c->maxw > 0 && w > c->maxw)
  1141. w = c->maxw;
  1142. if(c->maxh > 0 && h > c->maxh)
  1143. h = c->maxh;
  1144. }
  1145. if(w <= 0 || h <= 0)
  1146. return;
  1147. if(x > v->x + v->w)
  1148. x = v->w - w - 2 * c->border;
  1149. if(y > v->y + v->h)
  1150. y = v->h - h - 2 * c->border;
  1151. if(x + w + 2 * c->border < v->x)
  1152. x = v->x;
  1153. if(y + h + 2 * c->border < v->y)
  1154. y = v->y;
  1155. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  1156. c->x = wc.x = x;
  1157. c->y = wc.y = y;
  1158. c->w = wc.width = w;
  1159. c->h = wc.height = h;
  1160. wc.border_width = c->border;
  1161. XConfigureWindow(dpy, c->win,
  1162. CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1163. configure(c);
  1164. XSync(dpy, False);
  1165. }
  1166. }
  1167. void
  1168. resizemouse(Client *c) {
  1169. int ocx, ocy;
  1170. int nw, nh;
  1171. View *v;
  1172. XEvent ev;
  1173. ocx = c->x;
  1174. ocy = c->y;
  1175. v = c->view;
  1176. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1177. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1178. return;
  1179. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
  1180. for(;;) {
  1181. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask , &ev);
  1182. switch(ev.type) {
  1183. case ButtonRelease:
  1184. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
  1185. c->w + c->border - 1, c->h + c->border - 1);
  1186. XUngrabPointer(dpy, CurrentTime);
  1187. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1188. return;
  1189. case ConfigureRequest:
  1190. case Expose:
  1191. case MapRequest:
  1192. handler[ev.type](&ev);
  1193. break;
  1194. case MotionNotify:
  1195. XSync(dpy, False);
  1196. if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
  1197. nw = 1;
  1198. if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
  1199. nh = 1;
  1200. if(!c->isfloating && (v->layout->arrange != floating) && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP))
  1201. togglefloating(NULL);
  1202. if((v->layout->arrange == floating) || c->isfloating)
  1203. resize(c, c->x, c->y, nw, nh, True);
  1204. break;
  1205. }
  1206. }
  1207. }
  1208. void
  1209. restack(View *v) {
  1210. Client *c;
  1211. XEvent ev;
  1212. XWindowChanges wc;
  1213. drawbar(v);
  1214. if(!sel)
  1215. return;
  1216. if(sel->isfloating || (v->layout->arrange == floating))
  1217. XRaiseWindow(dpy, sel->win);
  1218. if(v->layout->arrange != floating) {
  1219. wc.stack_mode = Below;
  1220. wc.sibling = v->barwin;
  1221. if(!sel->isfloating) {
  1222. XConfigureWindow(dpy, sel->win, CWSibling|CWStackMode, &wc);
  1223. wc.sibling = sel->win;
  1224. }
  1225. for(c = nexttiled(clients, v); c; c = nexttiled(c->next, v)) {
  1226. if(c == sel)
  1227. continue;
  1228. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1229. wc.sibling = c->win;
  1230. }
  1231. }
  1232. XSync(dpy, False);
  1233. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1234. }
  1235. void
  1236. run(void) {
  1237. char *p;
  1238. char sbuf[sizeof stext];
  1239. fd_set rd;
  1240. int r, xfd;
  1241. unsigned int len, offset;
  1242. XEvent ev;
  1243. /* main event loop, also reads status text from stdin */
  1244. XSync(dpy, False);
  1245. xfd = ConnectionNumber(dpy);
  1246. readin = True;
  1247. offset = 0;
  1248. len = sizeof stext - 1;
  1249. sbuf[len] = stext[len] = '\0'; /* 0-terminator is never touched */
  1250. while(running) {
  1251. FD_ZERO(&rd);
  1252. if(readin)
  1253. FD_SET(STDIN_FILENO, &rd);
  1254. FD_SET(xfd, &rd);
  1255. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1256. if(errno == EINTR)
  1257. continue;
  1258. eprint("select failed\n");
  1259. }
  1260. if(FD_ISSET(STDIN_FILENO, &rd)) {
  1261. switch((r = read(STDIN_FILENO, sbuf + offset, len - offset))) {
  1262. case -1:
  1263. strncpy(stext, strerror(errno), len);
  1264. readin = False;
  1265. break;
  1266. case 0:
  1267. strncpy(stext, "EOF", 4);
  1268. readin = False;
  1269. break;
  1270. default:
  1271. for(p = sbuf + offset; r > 0; p++, r--, offset++)
  1272. if(*p == '\n' || *p == '\0') {
  1273. *p = '\0';
  1274. strncpy(stext, sbuf, len);
  1275. p += r - 1; /* p is sbuf + offset + r - 1 */
  1276. for(r = 0; *(p - r) && *(p - r) != '\n'; r++);
  1277. offset = r;
  1278. if(r)
  1279. memmove(sbuf, p - r + 1, r);
  1280. break;
  1281. }
  1282. break;
  1283. }
  1284. drawbar(selview);
  1285. }
  1286. while(XPending(dpy)) {
  1287. XNextEvent(dpy, &ev);
  1288. if(handler[ev.type])
  1289. (handler[ev.type])(&ev); /* call handler */
  1290. }
  1291. }
  1292. }
  1293. void
  1294. scan(void) {
  1295. unsigned int i, num;
  1296. Window *wins, d1, d2;
  1297. XWindowAttributes wa;
  1298. wins = NULL;
  1299. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1300. for(i = 0; i < num; i++) {
  1301. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1302. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1303. continue;
  1304. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1305. manage(wins[i], &wa);
  1306. }
  1307. for(i = 0; i < num; i++) { /* now the transients */
  1308. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1309. continue;
  1310. if(XGetTransientForHint(dpy, wins[i], &d1)
  1311. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1312. manage(wins[i], &wa);
  1313. }
  1314. }
  1315. if(wins)
  1316. XFree(wins);
  1317. }
  1318. void
  1319. setclientstate(Client *c, long state) {
  1320. long data[] = {state, None};
  1321. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1322. PropModeReplace, (unsigned char *)data, 2);
  1323. }
  1324. void
  1325. setlayout(const char *arg) {
  1326. unsigned int i;
  1327. View *v = selview;
  1328. if(!arg) {
  1329. v->layout++;
  1330. if(v->layout == &layouts[LENGTH(layouts)])
  1331. v->layout = &layouts[0];
  1332. }
  1333. else {
  1334. for(i = 0; i < LENGTH(layouts); i++)
  1335. if(!strcmp(arg, layouts[i].symbol))
  1336. break;
  1337. if(i == LENGTH(layouts))
  1338. return;
  1339. v->layout = &layouts[i];
  1340. }
  1341. if(sel)
  1342. arrange();
  1343. else
  1344. drawbar(selview);
  1345. }
  1346. void
  1347. setmwfact(const char *arg) {
  1348. double delta;
  1349. View *v = selview;
  1350. if(!domwfact)
  1351. return;
  1352. /* arg handling, manipulate mwfact */
  1353. if(arg == NULL)
  1354. v->mwfact = MWFACT;
  1355. else if(sscanf(arg, "%lf", &delta) == 1) {
  1356. if(arg[0] == '+' || arg[0] == '-')
  1357. v->mwfact += delta;
  1358. else
  1359. v->mwfact = delta;
  1360. if(v->mwfact < 0.1)
  1361. v->mwfact = 0.1;
  1362. else if(v->mwfact > 0.9)
  1363. v->mwfact = 0.9;
  1364. }
  1365. arrange();
  1366. }
  1367. void
  1368. setup(void) {
  1369. unsigned int i;
  1370. View *v;
  1371. XSetWindowAttributes wa;
  1372. XineramaScreenInfo *info = NULL;
  1373. /* init atoms */
  1374. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1375. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1376. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1377. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1378. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1379. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1380. /* init cursors */
  1381. wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1382. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1383. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1384. if((isxinerama = XineramaIsActive(dpy)))
  1385. info = XineramaQueryScreens(dpy, &nviews);
  1386. #if defined(AIM_XINERAMA)
  1387. isxinerama = True;
  1388. nviews = 2; /* aim Xinerama */
  1389. #endif
  1390. views = emallocz(nviews * sizeof(View));
  1391. screen = DefaultScreen(dpy);
  1392. root = RootWindow(dpy, screen);
  1393. /* init appearance */
  1394. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  1395. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  1396. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  1397. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  1398. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  1399. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  1400. initfont(FONT);
  1401. dc.h = bh = dc.font.height + 2;
  1402. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1403. dc.gc = XCreateGC(dpy, root, 0, 0);
  1404. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1405. if(!dc.font.set)
  1406. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1407. for(blw = i = 0; i < LENGTH(layouts); i++) {
  1408. i = textw(layouts[i].symbol);
  1409. if(i > blw)
  1410. blw = i;
  1411. }
  1412. seltags = emallocz(sizeof initags);
  1413. prevtags = emallocz(sizeof initags);
  1414. memcpy(seltags, initags, sizeof initags);
  1415. memcpy(prevtags, initags, sizeof initags);
  1416. for(i = 0; i < nviews; i++) {
  1417. /* init geometry */
  1418. v = &views[i];
  1419. if(nviews != 1 && isxinerama) {
  1420. #if defined(AIM_XINERAMA)
  1421. v->w = DisplayWidth(dpy, screen) / 2;
  1422. v->x = (i == 0) ? 0 : v->w;
  1423. v->y = 0;
  1424. v->h = DisplayHeight(dpy, screen);
  1425. #else
  1426. v->x = info[i].x_org;
  1427. v->y = info[i].y_org;
  1428. v->w = info[i].width;
  1429. v->h = info[i].height;
  1430. #endif
  1431. }
  1432. else {
  1433. v->x = 0;
  1434. v->y = 0;
  1435. v->w = DisplayWidth(dpy, screen);
  1436. v->h = DisplayHeight(dpy, screen);
  1437. }
  1438. /* init layouts */
  1439. v->mwfact = MWFACT;
  1440. v->layout = &layouts[0];
  1441. // TODO: bpos per screen?
  1442. bpos = BARPOS;
  1443. wa.override_redirect = 1;
  1444. wa.background_pixmap = ParentRelative;
  1445. wa.event_mask = ButtonPressMask|ExposureMask;
  1446. /* init bars */
  1447. v->barwin = XCreateWindow(dpy, root, v->x, v->y, v->w, bh, 0,
  1448. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  1449. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1450. XDefineCursor(dpy, v->barwin, cursor[CurNormal]);
  1451. updatebarpos(v);
  1452. XMapRaised(dpy, v->barwin);
  1453. strcpy(stext, "dwm-"VERSION);
  1454. /* EWMH support per view */
  1455. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1456. PropModeReplace, (unsigned char *) netatom, NetLast);
  1457. /* select for events */
  1458. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
  1459. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask;
  1460. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1461. XSelectInput(dpy, root, wa.event_mask);
  1462. drawbar(v);
  1463. }
  1464. if(info)
  1465. XFree(info);
  1466. selview = viewat();
  1467. /* grab keys */
  1468. grabkeys();
  1469. }
  1470. void
  1471. spawn(const char *arg) {
  1472. static char *shell = NULL;
  1473. if(!shell && !(shell = getenv("SHELL")))
  1474. shell = "/bin/sh";
  1475. if(!arg)
  1476. return;
  1477. /* The double-fork construct avoids zombie processes and keeps the code
  1478. * clean from stupid signal handlers. */
  1479. if(fork() == 0) {
  1480. if(fork() == 0) {
  1481. if(dpy)
  1482. close(ConnectionNumber(dpy));
  1483. setsid();
  1484. execl(shell, shell, "-c", arg, (char *)NULL);
  1485. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
  1486. perror(" failed");
  1487. }
  1488. exit(0);
  1489. }
  1490. wait(0);
  1491. }
  1492. void
  1493. tag(const char *arg) {
  1494. unsigned int i;
  1495. if(!sel)
  1496. return;
  1497. for(i = 0; i < LENGTH(tags); i++)
  1498. sel->tags[i] = (NULL == arg);
  1499. sel->tags[idxoftag(arg)] = True;
  1500. arrange();
  1501. }
  1502. unsigned int
  1503. textnw(const char *text, unsigned int len) {
  1504. XRectangle r;
  1505. if(dc.font.set) {
  1506. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1507. return r.width;
  1508. }
  1509. return XTextWidth(dc.font.xfont, text, len);
  1510. }
  1511. unsigned int
  1512. textw(const char *text) {
  1513. return textnw(text, strlen(text)) + dc.font.height;
  1514. }
  1515. void
  1516. tile(View *v) {
  1517. unsigned int i, n, nx, ny, nw, nh, mw, th;
  1518. Client *c, *mc;
  1519. domwfact = dozoom = True;
  1520. nx = v->wax;
  1521. ny = v->way;
  1522. nw = 0;
  1523. for(n = 0, c = nexttiled(clients, v); c; c = nexttiled(c->next, v))
  1524. n++;
  1525. /* window geoms */
  1526. mw = (n == 1) ? v->waw : v->mwfact * v->waw;
  1527. th = (n > 1) ? v->wah / (n - 1) : 0;
  1528. if(n > 1 && th < bh)
  1529. th = v->wah;
  1530. for(i = 0, c = mc = nexttiled(clients, v); c; c = nexttiled(c->next, v)) {
  1531. if(i == 0) { /* master */
  1532. nx = v->wax;
  1533. ny = v->way;
  1534. nw = mw - 2 * c->border;
  1535. nh = v->wah - 2 * c->border;
  1536. }
  1537. else { /* tile window */
  1538. if(i == 1) {
  1539. ny = v->way;
  1540. nx += mc->w + 2 * mc->border;
  1541. nw = v->waw - mw - 2 * c->border;
  1542. }
  1543. if(i + 1 == n) /* remainder */
  1544. nh = (v->way + v->wah) - ny - 2 * c->border;
  1545. else
  1546. nh = th - 2 * c->border;
  1547. }
  1548. resize(c, nx, ny, nw, nh, RESIZEHINTS);
  1549. if((RESIZEHINTS) && ((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw)))
  1550. /* client doesn't accept size constraints */
  1551. resize(c, nx, ny, nw, nh, False);
  1552. if(n > 1 && th != v->wah)
  1553. ny = c->y + c->h + 2 * c->border;
  1554. i++;
  1555. }
  1556. }
  1557. void
  1558. togglebar(const char *arg) {
  1559. if(bpos == BarOff)
  1560. bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
  1561. else
  1562. bpos = BarOff;
  1563. updatebarpos(selview);
  1564. arrange();
  1565. }
  1566. void
  1567. togglefloating(const char *arg) {
  1568. if(!sel)
  1569. return;
  1570. sel->isfloating = !sel->isfloating;
  1571. if(sel->isfloating)
  1572. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1573. arrange();
  1574. }
  1575. void
  1576. toggletag(const char *arg) {
  1577. unsigned int i, j;
  1578. if(!sel)
  1579. return;
  1580. i = idxoftag(arg);
  1581. sel->tags[i] = !sel->tags[i];
  1582. for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
  1583. if(j == LENGTH(tags))
  1584. sel->tags[i] = True; /* at least one tag must be enabled */
  1585. arrange();
  1586. }
  1587. void
  1588. toggleview(const char *arg) {
  1589. unsigned int i, j;
  1590. i = idxoftag(arg);
  1591. seltags[i] = !seltags[i];
  1592. for(j = 0; j < LENGTH(tags) && !seltags[j]; j++);
  1593. if(j == LENGTH(tags))
  1594. seltags[i] = True; /* at least one tag must be viewed */
  1595. arrange();
  1596. }
  1597. void
  1598. unban(Client *c) {
  1599. if(!c->isbanned)
  1600. return;
  1601. XMoveWindow(dpy, c->win, c->x, c->y);
  1602. c->isbanned = False;
  1603. }
  1604. void
  1605. unmanage(Client *c) {
  1606. XWindowChanges wc;
  1607. wc.border_width = c->oldborder;
  1608. /* The server grab construct avoids race conditions. */
  1609. XGrabServer(dpy);
  1610. XSetErrorHandler(xerrordummy);
  1611. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1612. detach(c);
  1613. detachstack(c);
  1614. if(sel == c)
  1615. focus(NULL);
  1616. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1617. setclientstate(c, WithdrawnState);
  1618. free(c->tags);
  1619. free(c);
  1620. XSync(dpy, False);
  1621. XSetErrorHandler(xerror);
  1622. XUngrabServer(dpy);
  1623. arrange();
  1624. }
  1625. void
  1626. unmapnotify(XEvent *e) {
  1627. Client *c;
  1628. XUnmapEvent *ev = &e->xunmap;
  1629. if((c = getclient(ev->window)))
  1630. unmanage(c);
  1631. }
  1632. void
  1633. updatebarpos(View *v) {
  1634. XEvent ev;
  1635. v->wax = v->x;
  1636. v->way = v->y;
  1637. v->wah = v->h;
  1638. v->waw = v->w;
  1639. switch(bpos) {
  1640. default:
  1641. v->wah -= bh;
  1642. v->way += bh;
  1643. XMoveWindow(dpy, v->barwin, v->x, v->y);
  1644. break;
  1645. case BarBot:
  1646. v->wah -= bh;
  1647. XMoveWindow(dpy, v->barwin, v->x, v->y + v->wah);
  1648. break;
  1649. case BarOff:
  1650. XMoveWindow(dpy, v->barwin, v->x, v->y - bh);
  1651. break;
  1652. }
  1653. XSync(dpy, False);
  1654. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1655. }
  1656. void
  1657. updatesizehints(Client *c) {
  1658. long msize;
  1659. XSizeHints size;
  1660. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  1661. size.flags = PSize;
  1662. c->flags = size.flags;
  1663. if(c->flags & PBaseSize) {
  1664. c->basew = size.base_width;
  1665. c->baseh = size.base_height;
  1666. }
  1667. else if(c->flags & PMinSize) {
  1668. c->basew = size.min_width;
  1669. c->baseh = size.min_height;
  1670. }
  1671. else
  1672. c->basew = c->baseh = 0;
  1673. if(c->flags & PResizeInc) {
  1674. c->incw = size.width_inc;
  1675. c->inch = size.height_inc;
  1676. }
  1677. else
  1678. c->incw = c->inch = 0;
  1679. if(c->flags & PMaxSize) {
  1680. c->maxw = size.max_width;
  1681. c->maxh = size.max_height;
  1682. }
  1683. else
  1684. c->maxw = c->maxh = 0;
  1685. if(c->flags & PMinSize) {
  1686. c->minw = size.min_width;
  1687. c->minh = size.min_height;
  1688. }
  1689. else if(c->flags & PBaseSize) {
  1690. c->minw = size.base_width;
  1691. c->minh = size.base_height;
  1692. }
  1693. else
  1694. c->minw = c->minh = 0;
  1695. if(c->flags & PAspect) {
  1696. c->minax = size.min_aspect.x;
  1697. c->maxax = size.max_aspect.x;
  1698. c->minay = size.min_aspect.y;
  1699. c->maxay = size.max_aspect.y;
  1700. }
  1701. else
  1702. c->minax = c->maxax = c->minay = c->maxay = 0;
  1703. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1704. && c->maxw == c->minw && c->maxh == c->minh);
  1705. }
  1706. void
  1707. updatetitle(Client *c) {
  1708. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1709. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1710. }
  1711. void
  1712. updatewmhints(Client *c) {
  1713. XWMHints *wmh;
  1714. if((wmh = XGetWMHints(dpy, c->win))) {
  1715. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1716. XFree(wmh);
  1717. }
  1718. }
  1719. void
  1720. view(const char *arg) {
  1721. unsigned int i;
  1722. Bool tmp[LENGTH(tags)];
  1723. for(i = 0; i < LENGTH(tags); i++)
  1724. tmp[i] = (NULL == arg);
  1725. tmp[idxoftag(arg)] = True;
  1726. if(memcmp(seltags, tmp, sizeof initags) != 0) {
  1727. memcpy(prevtags, seltags, sizeof initags);
  1728. memcpy(seltags, tmp, sizeof initags);
  1729. arrange();
  1730. }
  1731. }
  1732. View *
  1733. viewat() {
  1734. int i, x, y;
  1735. Window win;
  1736. unsigned int mask;
  1737. XQueryPointer(dpy, root, &win, &win, &x, &y, &i, &i, &mask);
  1738. for(i = 0; i < nviews; i++) {
  1739. if((x >= views[i].x && x < views[i].x + views[i].w)
  1740. && (y >= views[i].y && y < views[i].y + views[i].h))
  1741. return &views[i];
  1742. }
  1743. return NULL;
  1744. }
  1745. void
  1746. viewprevtag(const char *arg) {
  1747. static Bool tmp[LENGTH(tags)];
  1748. memcpy(tmp, seltags, sizeof initags);
  1749. memcpy(seltags, prevtags, sizeof initags);
  1750. memcpy(prevtags, tmp, sizeof initags);
  1751. arrange();
  1752. }
  1753. /* There's no way to check accesses to destroyed windows, thus those cases are
  1754. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1755. * default error handler, which may call exit. */
  1756. int
  1757. xerror(Display *dpy, XErrorEvent *ee) {
  1758. if(ee->error_code == BadWindow
  1759. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1760. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1761. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1762. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1763. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1764. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1765. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1766. return 0;
  1767. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1768. ee->request_code, ee->error_code);
  1769. return xerrorxlib(dpy, ee); /* may call exit */
  1770. }
  1771. int
  1772. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1773. return 0;
  1774. }
  1775. /* Startup Error handler to check if another window manager
  1776. * is already running. */
  1777. int
  1778. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1779. otherwm = True;
  1780. return -1;
  1781. }
  1782. void
  1783. zoom(const char *arg) {
  1784. Client *c = sel;
  1785. if(!sel || !dozoom || sel->isfloating)
  1786. return;
  1787. if(c == nexttiled(clients, c->view))
  1788. if(!(c = nexttiled(c->next, c->view)))
  1789. return;
  1790. detach(c);
  1791. attach(c);
  1792. focus(c);
  1793. arrange();
  1794. }
  1795. int
  1796. main(int argc, char *argv[]) {
  1797. if(argc == 2 && !strcmp("-v", argv[1]))
  1798. eprint("dwm-"VERSION", © 2006-2008 dwm engineers, see LICENSE for details\n");
  1799. else if(argc != 1)
  1800. eprint("usage: dwm [-v]\n");
  1801. setlocale(LC_CTYPE, "");
  1802. if(!(dpy = XOpenDisplay(0)))
  1803. eprint("dwm: cannot open display\n");
  1804. checkotherwm();
  1805. setup();
  1806. scan();
  1807. run();
  1808. cleanup();
  1809. XCloseDisplay(dpy);
  1810. return 0;
  1811. }