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.

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