Configuration file for DWM on MacBook Air
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1966 lines
48 KiB

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