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

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