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.

1852 lines
42 KiB

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