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.

1951 lines
47 KiB

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