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

1719 lines
41 KiB

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