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.

1950 lines
47 KiB

17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
16 years ago
17 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
17 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. 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. 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. 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. XLowerWindow(dpy, m->barwin);
  1225. XSync(dpy, False);
  1226. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1227. }
  1228. void
  1229. run(void) {
  1230. XEvent ev;
  1231. /* main event loop */
  1232. XSync(dpy, False);
  1233. while(running && !XNextEvent(dpy, &ev))
  1234. if(handler[ev.type])
  1235. (handler[ev.type])(&ev); /* call handler */
  1236. }
  1237. void
  1238. scan(void) {
  1239. unsigned int i, num;
  1240. Window d1, d2, *wins = NULL;
  1241. XWindowAttributes wa;
  1242. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1243. for(i = 0; i < num; i++) {
  1244. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1245. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1246. continue;
  1247. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1248. manage(wins[i], &wa);
  1249. }
  1250. for(i = 0; i < num; i++) { /* now the transients */
  1251. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1252. continue;
  1253. if(XGetTransientForHint(dpy, wins[i], &d1)
  1254. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1255. manage(wins[i], &wa);
  1256. }
  1257. if(wins)
  1258. XFree(wins);
  1259. }
  1260. }
  1261. void
  1262. sendmon(Client *c, Monitor *m) {
  1263. if(c->mon == m)
  1264. return;
  1265. unfocus(c);
  1266. detach(c);
  1267. detachstack(c);
  1268. c->mon = m;
  1269. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1270. attach(c);
  1271. attachstack(c);
  1272. focus(NULL);
  1273. arrange();
  1274. }
  1275. void
  1276. setclientstate(Client *c, long state) {
  1277. long data[] = { state, None };
  1278. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1279. PropModeReplace, (unsigned char *)data, 2);
  1280. }
  1281. void
  1282. setlayout(const Arg *arg) {
  1283. if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
  1284. selmon->sellt ^= 1;
  1285. if(arg && arg->v)
  1286. selmon->lt[selmon->sellt] = (Layout *)arg->v;
  1287. if(selmon->sel)
  1288. arrange();
  1289. else
  1290. drawbars();
  1291. }
  1292. /* arg > 1.0 will set mfact absolutly */
  1293. void
  1294. setmfact(const Arg *arg) {
  1295. float f;
  1296. if(!arg || !selmon->lt[selmon->sellt]->arrange)
  1297. return;
  1298. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1299. if(f < 0.1 || f > 0.9)
  1300. return;
  1301. selmon->mfact = f;
  1302. arrange();
  1303. }
  1304. void
  1305. setup(void) {
  1306. unsigned int i;
  1307. int w;
  1308. XSetWindowAttributes wa;
  1309. /* init screen */
  1310. screen = DefaultScreen(dpy);
  1311. root = RootWindow(dpy, screen);
  1312. initfont(font);
  1313. sw = DisplayWidth(dpy, screen);
  1314. sh = DisplayHeight(dpy, screen);
  1315. bh = dc.h = dc.font.height + 2;
  1316. updategeom();
  1317. /* init atoms */
  1318. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1319. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1320. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1321. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1322. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1323. /* init cursors */
  1324. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1325. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1326. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1327. /* init appearance */
  1328. dc.norm[ColBorder] = getcolor(normbordercolor);
  1329. dc.norm[ColBG] = getcolor(normbgcolor);
  1330. dc.norm[ColFG] = getcolor(normfgcolor);
  1331. dc.sel[ColBorder] = getcolor(selbordercolor);
  1332. dc.sel[ColBG] = getcolor(selbgcolor);
  1333. dc.sel[ColFG] = getcolor(selfgcolor);
  1334. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1335. dc.gc = XCreateGC(dpy, root, 0, NULL);
  1336. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1337. if(!dc.font.set)
  1338. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1339. /* init bars */
  1340. for(blw = i = 0; LENGTH(layouts) > 1 && i < LENGTH(layouts); i++) {
  1341. w = TEXTW(layouts[i].symbol);
  1342. blw = MAX(blw, w);
  1343. }
  1344. updatebars();
  1345. updatestatus();
  1346. /* EWMH support per view */
  1347. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1348. PropModeReplace, (unsigned char *) netatom, NetLast);
  1349. /* select for events */
  1350. wa.cursor = cursor[CurNormal];
  1351. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
  1352. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
  1353. |PropertyChangeMask;
  1354. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1355. XSelectInput(dpy, root, wa.event_mask);
  1356. grabkeys();
  1357. }
  1358. void
  1359. showhide(Client *c) {
  1360. if(!c)
  1361. return;
  1362. if(ISVISIBLE(c)) { /* show clients top down */
  1363. XMoveWindow(dpy, c->win, c->x, c->y);
  1364. if(!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
  1365. resize(c, c->x, c->y, c->w, c->h, False);
  1366. showhide(c->snext);
  1367. }
  1368. else { /* hide clients bottom up */
  1369. showhide(c->snext);
  1370. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  1371. }
  1372. }
  1373. void
  1374. sigchld(int signal) {
  1375. while(0 < waitpid(-1, NULL, WNOHANG));
  1376. }
  1377. void
  1378. spawn(const Arg *arg) {
  1379. signal(SIGCHLD, sigchld);
  1380. if(fork() == 0) {
  1381. if(dpy)
  1382. close(ConnectionNumber(dpy));
  1383. setsid();
  1384. execvp(((char **)arg->v)[0], (char **)arg->v);
  1385. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1386. perror(" failed");
  1387. exit(0);
  1388. }
  1389. }
  1390. void
  1391. tag(const Arg *arg) {
  1392. if(selmon->sel && arg->ui & TAGMASK) {
  1393. selmon->sel->tags = arg->ui & TAGMASK;
  1394. arrange();
  1395. }
  1396. }
  1397. void
  1398. tagmon(const Arg *arg) {
  1399. if(!selmon->sel || !mons->next)
  1400. return;
  1401. sendmon(selmon->sel, dirtomon(arg->i));
  1402. }
  1403. int
  1404. textnw(const char *text, unsigned int len) {
  1405. XRectangle r;
  1406. if(dc.font.set) {
  1407. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1408. return r.width;
  1409. }
  1410. return XTextWidth(dc.font.xfont, text, len);
  1411. }
  1412. void
  1413. tile(Monitor *m) {
  1414. int x, y, h, w, mw;
  1415. unsigned int i, n;
  1416. Client *c;
  1417. for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  1418. if(n == 0)
  1419. return;
  1420. /* master */
  1421. c = nexttiled(m->clients);
  1422. mw = m->mfact * m->ww;
  1423. resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw, False);
  1424. if(--n == 0)
  1425. return;
  1426. /* tile stack */
  1427. x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
  1428. y = m->wy;
  1429. w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
  1430. h = m->wh / n;
  1431. if(h < bh)
  1432. h = m->wh;
  1433. for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1434. resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
  1435. ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw), False);
  1436. if(h != m->wh)
  1437. y = c->y + HEIGHT(c);
  1438. }
  1439. }
  1440. void
  1441. togglebar(const Arg *arg) {
  1442. selmon->showbar = !selmon->showbar;
  1443. updatebarpos(selmon);
  1444. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1445. arrange();
  1446. }
  1447. void
  1448. togglefloating(const Arg *arg) {
  1449. if(!selmon->sel)
  1450. return;
  1451. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1452. if(selmon->sel->isfloating)
  1453. resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  1454. selmon->sel->w, selmon->sel->h, False);
  1455. arrange();
  1456. }
  1457. void
  1458. toggletag(const Arg *arg) {
  1459. unsigned int mask;
  1460. if(!selmon->sel)
  1461. return;
  1462. mask = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1463. if(mask) {
  1464. selmon->sel->tags = mask;
  1465. arrange();
  1466. }
  1467. }
  1468. void
  1469. toggleview(const Arg *arg) {
  1470. unsigned int mask = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1471. if(mask) {
  1472. selmon->tagset[selmon->seltags] = mask;
  1473. arrange();
  1474. }
  1475. }
  1476. void
  1477. unfocus(Client *c) {
  1478. if(!c)
  1479. return;
  1480. grabbuttons(c, False);
  1481. XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
  1482. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1483. }
  1484. void
  1485. unmanage(Client *c) {
  1486. XWindowChanges wc;
  1487. wc.border_width = c->oldbw;
  1488. /* The server grab construct avoids race conditions. */
  1489. XGrabServer(dpy);
  1490. XSetErrorHandler(xerrordummy);
  1491. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1492. detach(c);
  1493. detachstack(c);
  1494. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1495. setclientstate(c, WithdrawnState);
  1496. free(c);
  1497. XSync(dpy, False);
  1498. XSetErrorHandler(xerror);
  1499. XUngrabServer(dpy);
  1500. focus(NULL);
  1501. arrange();
  1502. }
  1503. void
  1504. unmapnotify(XEvent *e) {
  1505. Client *c;
  1506. XUnmapEvent *ev = &e->xunmap;
  1507. if((c = wintoclient(ev->window)))
  1508. unmanage(c);
  1509. }
  1510. void
  1511. updatebars(void) {
  1512. Monitor *m;
  1513. XSetWindowAttributes wa;
  1514. wa.override_redirect = True;
  1515. wa.background_pixmap = ParentRelative;
  1516. wa.event_mask = ButtonPressMask|ExposureMask;
  1517. for(m = mons; m; m = m->next) {
  1518. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  1519. CopyFromParent, DefaultVisual(dpy, screen),
  1520. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1521. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1522. XMapRaised(dpy, m->barwin);
  1523. }
  1524. }
  1525. void
  1526. updatebarpos(Monitor *m) {
  1527. m->wy = m->my;
  1528. m->wh = m->mh;
  1529. if(m->showbar) {
  1530. m->wh -= bh;
  1531. m->by = m->topbar ? m->wy : m->wy + m->wh;
  1532. m->wy = m->topbar ? m->wy + bh : m->wy;
  1533. }
  1534. else
  1535. m->by = -bh;
  1536. }
  1537. void
  1538. updategeom(void) {
  1539. int i, n = 1;
  1540. Client *c;
  1541. Monitor *newmons = NULL, *m, *tm;
  1542. #ifdef XINERAMA
  1543. XineramaScreenInfo *info = NULL;
  1544. if(XineramaIsActive(dpy))
  1545. info = XineramaQueryScreens(dpy, &n);
  1546. #endif /* XINERAMA */
  1547. /* allocate monitor(s) for the new geometry setup */
  1548. for(i = 0; i < n; i++) {
  1549. if(!(m = (Monitor *)malloc(sizeof(Monitor))))
  1550. die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
  1551. m->next = newmons;
  1552. newmons = m;
  1553. }
  1554. /* initialise monitor(s) */
  1555. #ifdef XINERAMA
  1556. if(XineramaIsActive(dpy)) {
  1557. for(i = 0, m = newmons; m; m = m->next, i++) {
  1558. m->screen_number = info[i].screen_number;
  1559. m->mx = m->wx = info[i].x_org;
  1560. m->my = m->wy = info[i].y_org;
  1561. m->mw = m->ww = info[i].width;
  1562. m->mh = m->wh = info[i].height;
  1563. }
  1564. XFree(info);
  1565. }
  1566. else
  1567. #endif /* XINERAMA */
  1568. /* default monitor setup */
  1569. {
  1570. m->screen_number = 0;
  1571. m->mx = m->wx = 0;
  1572. m->my = m->wy = 0;
  1573. m->mw = m->ww = sw;
  1574. m->mh = m->wh = sh;
  1575. }
  1576. /* bar geometry setup */
  1577. for(m = newmons; m; m = m->next) {
  1578. m->sel = m->stack = m->clients = NULL;
  1579. m->seltags = 0;
  1580. m->sellt = 0;
  1581. m->tagset[0] = m->tagset[1] = 1;
  1582. m->mfact = mfact;
  1583. m->showbar = SHOWBAR;
  1584. m->topbar = TOPBAR;
  1585. m->lt[0] = &layouts[0];
  1586. m->lt[1] = &layouts[1 % LENGTH(layouts)];
  1587. updatebarpos(m);
  1588. }
  1589. /* reassign left over clients of disappeared monitors */
  1590. for(tm = mons; tm; tm = tm->next)
  1591. while(tm->clients) {
  1592. c = tm->clients;
  1593. tm->clients = c->next;
  1594. detachstack(c);
  1595. c->mon = newmons;
  1596. attach(c);
  1597. attachstack(c);
  1598. }
  1599. /* select focused monitor */
  1600. cleanupmons();
  1601. selmon = mons = newmons;
  1602. selmon = wintomon(root);
  1603. }
  1604. void
  1605. updatenumlockmask(void) {
  1606. unsigned int i, j;
  1607. XModifierKeymap *modmap;
  1608. numlockmask = 0;
  1609. modmap = XGetModifierMapping(dpy);
  1610. for(i = 0; i < 8; i++)
  1611. for(j = 0; j < modmap->max_keypermod; j++)
  1612. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1613. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1614. numlockmask = (1 << i);
  1615. XFreeModifiermap(modmap);
  1616. }
  1617. void
  1618. updatesizehints(Client *c) {
  1619. long msize;
  1620. XSizeHints size;
  1621. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1622. /* size is uninitialized, ensure that size.flags aren't used */
  1623. size.flags = PSize;
  1624. if(size.flags & PBaseSize) {
  1625. c->basew = size.base_width;
  1626. c->baseh = size.base_height;
  1627. }
  1628. else if(size.flags & PMinSize) {
  1629. c->basew = size.min_width;
  1630. c->baseh = size.min_height;
  1631. }
  1632. else
  1633. c->basew = c->baseh = 0;
  1634. if(size.flags & PResizeInc) {
  1635. c->incw = size.width_inc;
  1636. c->inch = size.height_inc;
  1637. }
  1638. else
  1639. c->incw = c->inch = 0;
  1640. if(size.flags & PMaxSize) {
  1641. c->maxw = size.max_width;
  1642. c->maxh = size.max_height;
  1643. }
  1644. else
  1645. c->maxw = c->maxh = 0;
  1646. if(size.flags & PMinSize) {
  1647. c->minw = size.min_width;
  1648. c->minh = size.min_height;
  1649. }
  1650. else if(size.flags & PBaseSize) {
  1651. c->minw = size.base_width;
  1652. c->minh = size.base_height;
  1653. }
  1654. else
  1655. c->minw = c->minh = 0;
  1656. if(size.flags & PAspect) {
  1657. c->mina = (float)size.min_aspect.y / (float)size.min_aspect.x;
  1658. c->maxa = (float)size.max_aspect.x / (float)size.max_aspect.y;
  1659. }
  1660. else
  1661. c->maxa = c->mina = 0.0;
  1662. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1663. && c->maxw == c->minw && c->maxh == c->minh);
  1664. }
  1665. void
  1666. updatetitle(Client *c) {
  1667. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1668. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1669. }
  1670. void
  1671. updatestatus(void) {
  1672. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1673. strcpy(stext, "dwm-"VERSION);
  1674. drawbar(selmon);
  1675. }
  1676. void
  1677. updatewmhints(Client *c) {
  1678. XWMHints *wmh;
  1679. if((wmh = XGetWMHints(dpy, c->win))) {
  1680. if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  1681. wmh->flags &= ~XUrgencyHint;
  1682. XSetWMHints(dpy, c->win, wmh);
  1683. }
  1684. else
  1685. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1686. XFree(wmh);
  1687. }
  1688. }
  1689. void
  1690. view(const Arg *arg) {
  1691. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1692. return;
  1693. selmon->seltags ^= 1; /* toggle sel tagset */
  1694. if(arg->ui & TAGMASK)
  1695. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1696. arrange();
  1697. }
  1698. Client *
  1699. wintoclient(Window w) {
  1700. Client *c;
  1701. Monitor *m;
  1702. for(m = mons; m; m = m->next)
  1703. for(c = m->clients; c; c = c->next)
  1704. if(c->win == w)
  1705. return c;
  1706. return NULL;
  1707. }
  1708. Monitor *
  1709. wintomon(Window w) {
  1710. int x, y;
  1711. Client *c;
  1712. Monitor *m;
  1713. if(w == root && getrootptr(&x, &y))
  1714. return ptrtomon(x, y);
  1715. for(m = mons; m; m = m->next)
  1716. if(w == m->barwin)
  1717. return m;
  1718. if((c = wintoclient(w)))
  1719. return c->mon;
  1720. return selmon;
  1721. }
  1722. /* There's no way to check accesses to destroyed windows, thus those cases are
  1723. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1724. * default error handler, which may call exit. */
  1725. int
  1726. xerror(Display *dpy, XErrorEvent *ee) {
  1727. if(ee->error_code == BadWindow
  1728. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1729. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1730. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1731. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1732. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1733. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1734. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1735. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1736. return 0;
  1737. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1738. ee->request_code, ee->error_code);
  1739. return xerrorxlib(dpy, ee); /* may call exit */
  1740. }
  1741. int
  1742. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1743. return 0;
  1744. }
  1745. /* Startup Error handler to check if another window manager
  1746. * is already running. */
  1747. int
  1748. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1749. otherwm = True;
  1750. return -1;
  1751. }
  1752. void
  1753. zoom(const Arg *arg) {
  1754. Client *c = selmon->sel;
  1755. if(!selmon->lt[selmon->sellt]->arrange
  1756. || selmon->lt[selmon->sellt]->arrange == monocle
  1757. || (selmon->sel && selmon->sel->isfloating))
  1758. return;
  1759. if(c == nexttiled(selmon->clients))
  1760. if(!c || !(c = nexttiled(c->next)))
  1761. return;
  1762. detach(c);
  1763. attach(c);
  1764. focus(c);
  1765. arrange();
  1766. }
  1767. int
  1768. main(int argc, char *argv[]) {
  1769. if(argc == 2 && !strcmp("-v", argv[1]))
  1770. die("dwm-"VERSION", © 2006-2009 dwm engineers, see LICENSE for details\n");
  1771. else if(argc != 1)
  1772. die("usage: dwm [-v]\n");
  1773. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1774. fputs("warning: no locale support\n", stderr);
  1775. if(!(dpy = XOpenDisplay(NULL)))
  1776. die("dwm: cannot open display\n");
  1777. checkotherwm();
  1778. setup();
  1779. scan();
  1780. run();
  1781. cleanup();
  1782. XCloseDisplay(dpy);
  1783. return 0;
  1784. }