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.

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