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.

1964 lines
48 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
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
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
16 years ago
17 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
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
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
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 ((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);
  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);
  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 && (ev->width != sw || ev->height != sh)) {
  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);
  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. }
  958. void
  959. manage(Window w, XWindowAttributes *wa) {
  960. static Client cz;
  961. Client *c, *t = NULL;
  962. Window trans = None;
  963. XWindowChanges wc;
  964. if(!(c = malloc(sizeof(Client))))
  965. die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  966. *c = cz;
  967. c->win = w;
  968. updatetitle(c);
  969. if(XGetTransientForHint(dpy, w, &trans))
  970. t = wintoclient(trans);
  971. if(t) {
  972. c->mon = t->mon;
  973. c->tags = t->tags;
  974. }
  975. else {
  976. c->mon = selmon;
  977. applyrules(c);
  978. }
  979. /* geometry */
  980. c->x = wa->x + c->mon->wx;
  981. c->y = wa->y + c->mon->wy;
  982. c->w = wa->width;
  983. c->h = wa->height;
  984. c->oldbw = wa->border_width;
  985. if(c->w == c->mon->mw && c->h == c->mon->mh) {
  986. c->x = c->mon->mx;
  987. c->y = c->mon->my;
  988. c->bw = 0;
  989. }
  990. else {
  991. if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  992. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  993. if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  994. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  995. c->x = MAX(c->x, c->mon->mx);
  996. /* only fix client y-offset, if the client center might cover the bar */
  997. c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
  998. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  999. c->bw = borderpx;
  1000. }
  1001. wc.border_width = c->bw;
  1002. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  1003. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  1004. configure(c); /* propagates border_width, if size doesn't change */
  1005. updatesizehints(c);
  1006. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  1007. grabbuttons(c, False);
  1008. if(!c->isfloating)
  1009. c->isfloating = trans != None || c->isfixed;
  1010. if(c->isfloating)
  1011. XRaiseWindow(dpy, c->win);
  1012. attach(c);
  1013. attachstack(c);
  1014. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  1015. XMapWindow(dpy, c->win);
  1016. setclientstate(c, NormalState);
  1017. arrange();
  1018. }
  1019. void
  1020. mappingnotify(XEvent *e) {
  1021. XMappingEvent *ev = &e->xmapping;
  1022. XRefreshKeyboardMapping(ev);
  1023. if(ev->request == MappingKeyboard)
  1024. grabkeys();
  1025. }
  1026. void
  1027. maprequest(XEvent *e) {
  1028. static XWindowAttributes wa;
  1029. XMapRequestEvent *ev = &e->xmaprequest;
  1030. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1031. return;
  1032. if(wa.override_redirect)
  1033. return;
  1034. if(!wintoclient(ev->window))
  1035. manage(ev->window, &wa);
  1036. }
  1037. void
  1038. monocle(Monitor *m) {
  1039. Client *c;
  1040. for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1041. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
  1042. }
  1043. void
  1044. movemouse(const Arg *arg) {
  1045. int x, y, ocx, ocy, nx, ny;
  1046. Client *c;
  1047. Monitor *m;
  1048. XEvent ev;
  1049. if(!(c = selmon->sel))
  1050. return;
  1051. restack(selmon);
  1052. ocx = c->x;
  1053. ocy = c->y;
  1054. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1055. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1056. return;
  1057. if(!getrootptr(&x, &y))
  1058. return;
  1059. do {
  1060. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1061. switch (ev.type) {
  1062. case ConfigureRequest:
  1063. case Expose:
  1064. case MapRequest:
  1065. handler[ev.type](&ev);
  1066. break;
  1067. case MotionNotify:
  1068. nx = ocx + (ev.xmotion.x - x);
  1069. ny = ocy + (ev.xmotion.y - y);
  1070. if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1071. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1072. if(abs(selmon->wx - nx) < snap)
  1073. nx = selmon->wx;
  1074. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1075. nx = selmon->wx + selmon->ww - WIDTH(c);
  1076. if(abs(selmon->wy - ny) < snap)
  1077. ny = selmon->wy;
  1078. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1079. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1080. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1081. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1082. togglefloating(NULL);
  1083. }
  1084. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1085. resize(c, nx, ny, c->w, c->h, True);
  1086. break;
  1087. }
  1088. } while(ev.type != ButtonRelease);
  1089. XUngrabPointer(dpy, CurrentTime);
  1090. if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
  1091. sendmon(c, m);
  1092. selmon = m;
  1093. focus(NULL);
  1094. }
  1095. }
  1096. Client *
  1097. nexttiled(Client *c) {
  1098. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1099. return c;
  1100. }
  1101. Monitor *
  1102. ptrtomon(int x, int y) {
  1103. Monitor *m;
  1104. for(m = mons; m; m = m->next)
  1105. if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
  1106. return m;
  1107. return selmon;
  1108. }
  1109. void
  1110. propertynotify(XEvent *e) {
  1111. Client *c;
  1112. Window trans;
  1113. XPropertyEvent *ev = &e->xproperty;
  1114. if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1115. updatestatus();
  1116. else if(ev->state == PropertyDelete)
  1117. return; /* ignore */
  1118. else if((c = wintoclient(ev->window))) {
  1119. switch (ev->atom) {
  1120. default: break;
  1121. case XA_WM_TRANSIENT_FOR:
  1122. XGetTransientForHint(dpy, c->win, &trans);
  1123. if(!c->isfloating && (c->isfloating = (wintoclient(trans) != NULL)))
  1124. arrange();
  1125. break;
  1126. case XA_WM_NORMAL_HINTS:
  1127. updatesizehints(c);
  1128. break;
  1129. case XA_WM_HINTS:
  1130. updatewmhints(c);
  1131. drawbars();
  1132. break;
  1133. }
  1134. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1135. updatetitle(c);
  1136. if(c == c->mon->sel)
  1137. drawbars();
  1138. }
  1139. }
  1140. }
  1141. void
  1142. quit(const Arg *arg) {
  1143. running = False;
  1144. }
  1145. void
  1146. resize(Client *c, int x, int y, int w, int h, Bool interact) {
  1147. XWindowChanges wc;
  1148. if(applysizehints(c, &x, &y, &w, &h, interact)) {
  1149. c->x = wc.x = x;
  1150. c->y = wc.y = y;
  1151. c->w = wc.width = w;
  1152. c->h = wc.height = h;
  1153. wc.border_width = c->bw;
  1154. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1155. configure(c);
  1156. XSync(dpy, False);
  1157. }
  1158. }
  1159. void
  1160. resizemouse(const Arg *arg) {
  1161. int ocx, ocy;
  1162. int nw, nh;
  1163. Client *c;
  1164. Monitor *m;
  1165. XEvent ev;
  1166. if(!(c = selmon->sel))
  1167. return;
  1168. restack(selmon);
  1169. ocx = c->x;
  1170. ocy = c->y;
  1171. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1172. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1173. return;
  1174. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1175. do {
  1176. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1177. switch(ev.type) {
  1178. case ConfigureRequest:
  1179. case Expose:
  1180. case MapRequest:
  1181. handler[ev.type](&ev);
  1182. break;
  1183. case MotionNotify:
  1184. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1185. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1186. if(snap && nw >= selmon->wx && nw <= selmon->wx + selmon->ww
  1187. && nh >= selmon->wy && nh <= selmon->wy + selmon->wh)
  1188. {
  1189. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1190. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1191. togglefloating(NULL);
  1192. }
  1193. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1194. resize(c, c->x, c->y, nw, nh, True);
  1195. break;
  1196. }
  1197. } while(ev.type != ButtonRelease);
  1198. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1199. XUngrabPointer(dpy, CurrentTime);
  1200. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1201. if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
  1202. sendmon(c, m);
  1203. selmon = m;
  1204. focus(NULL);
  1205. }
  1206. }
  1207. void
  1208. restack(Monitor *m) {
  1209. Client *c;
  1210. XEvent ev;
  1211. XWindowChanges wc;
  1212. drawbars();
  1213. if(!m->sel)
  1214. return;
  1215. if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
  1216. XRaiseWindow(dpy, m->sel->win);
  1217. if(m->lt[m->sellt]->arrange) {
  1218. wc.stack_mode = Below;
  1219. wc.sibling = m->barwin;
  1220. for(c = m->stack; c; c = c->snext)
  1221. if(!c->isfloating && ISVISIBLE(c)) {
  1222. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1223. wc.sibling = c->win;
  1224. }
  1225. }
  1226. XSync(dpy, False);
  1227. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1228. }
  1229. void
  1230. run(void) {
  1231. XEvent ev;
  1232. /* main event loop */
  1233. XSync(dpy, False);
  1234. while(running && !XNextEvent(dpy, &ev))
  1235. if(handler[ev.type])
  1236. handler[ev.type](&ev); /* call handler */
  1237. }
  1238. void
  1239. scan(void) {
  1240. unsigned int i, num;
  1241. Window d1, d2, *wins = NULL;
  1242. XWindowAttributes wa;
  1243. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1244. for(i = 0; i < num; i++) {
  1245. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1246. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1247. continue;
  1248. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1249. manage(wins[i], &wa);
  1250. }
  1251. for(i = 0; i < num; i++) { /* now the transients */
  1252. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1253. continue;
  1254. if(XGetTransientForHint(dpy, wins[i], &d1)
  1255. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1256. manage(wins[i], &wa);
  1257. }
  1258. if(wins)
  1259. XFree(wins);
  1260. }
  1261. }
  1262. void
  1263. sendmon(Client *c, Monitor *m) {
  1264. if(c->mon == m)
  1265. return;
  1266. unfocus(c);
  1267. detach(c);
  1268. detachstack(c);
  1269. c->mon = m;
  1270. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1271. attach(c);
  1272. attachstack(c);
  1273. focus(NULL);
  1274. arrange();
  1275. }
  1276. void
  1277. setclientstate(Client *c, long state) {
  1278. long data[] = { state, None };
  1279. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1280. PropModeReplace, (unsigned char *)data, 2);
  1281. }
  1282. void
  1283. setlayout(const Arg *arg) {
  1284. if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
  1285. selmon->sellt ^= 1;
  1286. if(arg && arg->v)
  1287. selmon->lt[selmon->sellt] = (Layout *)arg->v;
  1288. if(selmon->sel)
  1289. arrange();
  1290. else
  1291. drawbars();
  1292. }
  1293. /* arg > 1.0 will set mfact absolutly */
  1294. void
  1295. setmfact(const Arg *arg) {
  1296. float f;
  1297. if(!arg || !selmon->lt[selmon->sellt]->arrange)
  1298. return;
  1299. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1300. if(f < 0.1 || f > 0.9)
  1301. return;
  1302. selmon->mfact = f;
  1303. arrange();
  1304. }
  1305. void
  1306. setup(void) {
  1307. unsigned int i;
  1308. int w;
  1309. XSetWindowAttributes wa;
  1310. /* clean up any zombies immediately */
  1311. sigchld(0);
  1312. /* init screen */
  1313. screen = DefaultScreen(dpy);
  1314. root = RootWindow(dpy, screen);
  1315. initfont(font);
  1316. sw = DisplayWidth(dpy, screen);
  1317. sh = DisplayHeight(dpy, screen);
  1318. bh = dc.h = dc.font.height + 2;
  1319. updategeom();
  1320. /* init atoms */
  1321. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1322. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1323. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1324. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1325. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1326. /* init cursors */
  1327. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1328. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1329. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1330. /* init appearance */
  1331. dc.norm[ColBorder] = getcolor(normbordercolor);
  1332. dc.norm[ColBG] = getcolor(normbgcolor);
  1333. dc.norm[ColFG] = getcolor(normfgcolor);
  1334. dc.sel[ColBorder] = getcolor(selbordercolor);
  1335. dc.sel[ColBG] = getcolor(selbgcolor);
  1336. dc.sel[ColFG] = getcolor(selfgcolor);
  1337. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1338. dc.gc = XCreateGC(dpy, root, 0, NULL);
  1339. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1340. if(!dc.font.set)
  1341. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1342. /* init bars */
  1343. for(blw = i = 0; LENGTH(layouts) > 1 && i < LENGTH(layouts); i++) {
  1344. w = TEXTW(layouts[i].symbol);
  1345. blw = MAX(blw, w);
  1346. }
  1347. updatebars();
  1348. updatestatus();
  1349. /* EWMH support per view */
  1350. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1351. PropModeReplace, (unsigned char *) netatom, NetLast);
  1352. /* select for events */
  1353. wa.cursor = cursor[CurNormal];
  1354. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
  1355. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
  1356. |PropertyChangeMask;
  1357. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1358. XSelectInput(dpy, root, wa.event_mask);
  1359. grabkeys();
  1360. }
  1361. void
  1362. showhide(Client *c) {
  1363. if(!c)
  1364. return;
  1365. if(ISVISIBLE(c)) { /* show clients top down */
  1366. XMoveWindow(dpy, c->win, c->x, c->y);
  1367. if(!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
  1368. resize(c, c->x, c->y, c->w, c->h, False);
  1369. showhide(c->snext);
  1370. }
  1371. else { /* hide clients bottom up */
  1372. showhide(c->snext);
  1373. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  1374. }
  1375. }
  1376. void
  1377. sigchld(int unused) {
  1378. if(signal(SIGCHLD, sigchld) == SIG_ERR)
  1379. die("Can't install SIGCHLD handler");
  1380. while(0 < waitpid(-1, NULL, WNOHANG));
  1381. }
  1382. void
  1383. spawn(const Arg *arg) {
  1384. if(fork() == 0) {
  1385. if(dpy)
  1386. close(ConnectionNumber(dpy));
  1387. setsid();
  1388. execvp(((char **)arg->v)[0], (char **)arg->v);
  1389. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1390. perror(" failed");
  1391. exit(0);
  1392. }
  1393. }
  1394. void
  1395. tag(const Arg *arg) {
  1396. if(selmon->sel && arg->ui & TAGMASK) {
  1397. selmon->sel->tags = arg->ui & TAGMASK;
  1398. arrange();
  1399. }
  1400. }
  1401. void
  1402. tagmon(const Arg *arg) {
  1403. if(!selmon->sel || !mons->next)
  1404. return;
  1405. sendmon(selmon->sel, dirtomon(arg->i));
  1406. }
  1407. int
  1408. textnw(const char *text, unsigned int len) {
  1409. XRectangle r;
  1410. if(dc.font.set) {
  1411. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1412. return r.width;
  1413. }
  1414. return XTextWidth(dc.font.xfont, text, len);
  1415. }
  1416. void
  1417. tile(Monitor *m) {
  1418. int x, y, h, w, mw;
  1419. unsigned int i, n;
  1420. Client *c;
  1421. for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  1422. if(n == 0)
  1423. return;
  1424. /* master */
  1425. c = nexttiled(m->clients);
  1426. mw = m->mfact * m->ww;
  1427. resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw, False);
  1428. if(--n == 0)
  1429. return;
  1430. /* tile stack */
  1431. x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
  1432. y = m->wy;
  1433. w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
  1434. h = m->wh / n;
  1435. if(h < bh)
  1436. h = m->wh;
  1437. for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1438. resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
  1439. ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw), False);
  1440. if(h != m->wh)
  1441. y = c->y + HEIGHT(c);
  1442. }
  1443. }
  1444. void
  1445. togglebar(const Arg *arg) {
  1446. selmon->showbar = !selmon->showbar;
  1447. updatebarpos(selmon);
  1448. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1449. arrange();
  1450. }
  1451. void
  1452. togglefloating(const Arg *arg) {
  1453. if(!selmon->sel)
  1454. return;
  1455. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1456. if(selmon->sel->isfloating)
  1457. resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  1458. selmon->sel->w, selmon->sel->h, False);
  1459. arrange();
  1460. }
  1461. void
  1462. toggletag(const Arg *arg) {
  1463. unsigned int newtags;
  1464. if(!selmon->sel)
  1465. return;
  1466. newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1467. if(newtags) {
  1468. selmon->sel->tags = newtags;
  1469. arrange();
  1470. }
  1471. }
  1472. void
  1473. toggleview(const Arg *arg) {
  1474. unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1475. if(newtagset) {
  1476. selmon->tagset[selmon->seltags] = newtagset;
  1477. arrange();
  1478. }
  1479. }
  1480. void
  1481. unfocus(Client *c) {
  1482. if(!c)
  1483. return;
  1484. grabbuttons(c, False);
  1485. XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
  1486. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1487. }
  1488. void
  1489. unmanage(Client *c) {
  1490. XWindowChanges wc;
  1491. wc.border_width = c->oldbw;
  1492. /* The server grab construct avoids race conditions. */
  1493. XGrabServer(dpy);
  1494. XSetErrorHandler(xerrordummy);
  1495. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1496. detach(c);
  1497. detachstack(c);
  1498. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1499. setclientstate(c, WithdrawnState);
  1500. free(c);
  1501. XSync(dpy, False);
  1502. XSetErrorHandler(xerror);
  1503. XUngrabServer(dpy);
  1504. focus(NULL);
  1505. arrange();
  1506. }
  1507. void
  1508. unmapnotify(XEvent *e) {
  1509. Client *c;
  1510. XUnmapEvent *ev = &e->xunmap;
  1511. if((c = wintoclient(ev->window)))
  1512. unmanage(c);
  1513. }
  1514. void
  1515. updatebars(void) {
  1516. Monitor *m;
  1517. XSetWindowAttributes wa;
  1518. wa.override_redirect = True;
  1519. wa.background_pixmap = ParentRelative;
  1520. wa.event_mask = ButtonPressMask|ExposureMask;
  1521. for(m = mons; m; m = m->next) {
  1522. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  1523. CopyFromParent, DefaultVisual(dpy, screen),
  1524. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1525. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1526. XMapRaised(dpy, m->barwin);
  1527. }
  1528. }
  1529. void
  1530. updatebarpos(Monitor *m) {
  1531. m->wy = m->my;
  1532. m->wh = m->mh;
  1533. if(m->showbar) {
  1534. m->wh -= bh;
  1535. m->by = m->topbar ? m->wy : m->wy + m->wh;
  1536. m->wy = m->topbar ? m->wy + bh : m->wy;
  1537. }
  1538. else
  1539. m->by = -bh;
  1540. }
  1541. void
  1542. updategeom(void) {
  1543. int i, n = 1;
  1544. Client *c;
  1545. Monitor *newmons = NULL, *m = NULL, *tm;
  1546. #ifdef XINERAMA
  1547. int nn;
  1548. XineramaScreenInfo *info = NULL;
  1549. if(XineramaIsActive(dpy))
  1550. info = XineramaQueryScreens(dpy, &n);
  1551. for(i = 1, nn = n; i < n; i++)
  1552. if(info[i - 1].x_org == info[i].x_org && info[i - 1].y_org == info[i].y_org
  1553. && info[i - 1].width == info[i].width && info[i - 1].height == info[i].height)
  1554. --nn;
  1555. n = nn; /* we only consider unique geometries as separate screens */
  1556. #endif /* XINERAMA */
  1557. /* allocate monitor(s) for the new geometry setup */
  1558. for(i = 0; i < n; i++) {
  1559. if(!(m = (Monitor *)malloc(sizeof(Monitor))))
  1560. die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
  1561. m->next = newmons;
  1562. newmons = m;
  1563. }
  1564. /* initialise monitor(s) */
  1565. #ifdef XINERAMA
  1566. if(XineramaIsActive(dpy)) {
  1567. for(i = 0, m = newmons; m; m = m->next, i++) {
  1568. m->num = info[i].screen_number;
  1569. m->mx = m->wx = info[i].x_org;
  1570. m->my = m->wy = info[i].y_org;
  1571. m->mw = m->ww = info[i].width;
  1572. m->mh = m->wh = info[i].height;
  1573. }
  1574. XFree(info);
  1575. }
  1576. else
  1577. #endif /* XINERAMA */
  1578. /* default monitor setup */
  1579. {
  1580. m->num = 0;
  1581. m->mx = m->wx = 0;
  1582. m->my = m->wy = 0;
  1583. m->mw = m->ww = sw;
  1584. m->mh = m->wh = sh;
  1585. }
  1586. /* bar geometry setup */
  1587. for(m = newmons; m; m = m->next) {
  1588. m->sel = m->stack = m->clients = NULL;
  1589. m->seltags = 0;
  1590. m->sellt = 0;
  1591. m->tagset[0] = m->tagset[1] = 1;
  1592. m->mfact = mfact;
  1593. m->showbar = showbar;
  1594. m->topbar = topbar;
  1595. m->lt[0] = &layouts[0];
  1596. m->lt[1] = &layouts[1 % LENGTH(layouts)];
  1597. updatebarpos(m);
  1598. }
  1599. /* reassign left over clients of disappeared monitors */
  1600. for(tm = mons; tm; tm = tm->next)
  1601. while(tm->clients) {
  1602. c = tm->clients;
  1603. tm->clients = c->next;
  1604. detachstack(c);
  1605. c->mon = newmons;
  1606. attach(c);
  1607. attachstack(c);
  1608. }
  1609. /* select focused monitor */
  1610. cleanupmons();
  1611. selmon = mons = newmons;
  1612. selmon = wintomon(root);
  1613. }
  1614. void
  1615. updatenumlockmask(void) {
  1616. unsigned int i, j;
  1617. XModifierKeymap *modmap;
  1618. numlockmask = 0;
  1619. modmap = XGetModifierMapping(dpy);
  1620. for(i = 0; i < 8; i++)
  1621. for(j = 0; j < modmap->max_keypermod; j++)
  1622. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1623. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1624. numlockmask = (1 << i);
  1625. XFreeModifiermap(modmap);
  1626. }
  1627. void
  1628. updatesizehints(Client *c) {
  1629. long msize;
  1630. XSizeHints size;
  1631. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1632. /* size is uninitialized, ensure that size.flags aren't used */
  1633. size.flags = PSize;
  1634. if(size.flags & PBaseSize) {
  1635. c->basew = size.base_width;
  1636. c->baseh = size.base_height;
  1637. }
  1638. else if(size.flags & PMinSize) {
  1639. c->basew = size.min_width;
  1640. c->baseh = size.min_height;
  1641. }
  1642. else
  1643. c->basew = c->baseh = 0;
  1644. if(size.flags & PResizeInc) {
  1645. c->incw = size.width_inc;
  1646. c->inch = size.height_inc;
  1647. }
  1648. else
  1649. c->incw = c->inch = 0;
  1650. if(size.flags & PMaxSize) {
  1651. c->maxw = size.max_width;
  1652. c->maxh = size.max_height;
  1653. }
  1654. else
  1655. c->maxw = c->maxh = 0;
  1656. if(size.flags & PMinSize) {
  1657. c->minw = size.min_width;
  1658. c->minh = size.min_height;
  1659. }
  1660. else if(size.flags & PBaseSize) {
  1661. c->minw = size.base_width;
  1662. c->minh = size.base_height;
  1663. }
  1664. else
  1665. c->minw = c->minh = 0;
  1666. if(size.flags & PAspect) {
  1667. c->mina = (float)size.min_aspect.y / size.min_aspect.x;
  1668. c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
  1669. }
  1670. else
  1671. c->maxa = c->mina = 0.0;
  1672. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1673. && c->maxw == c->minw && c->maxh == c->minh);
  1674. }
  1675. void
  1676. updatetitle(Client *c) {
  1677. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1678. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1679. if(c->name[0] == '\0') /* hack to mark broken clients */
  1680. strcpy(c->name, broken);
  1681. }
  1682. void
  1683. updatestatus(void) {
  1684. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1685. strcpy(stext, "dwm-"VERSION);
  1686. drawbar(selmon);
  1687. }
  1688. void
  1689. updatewmhints(Client *c) {
  1690. XWMHints *wmh;
  1691. if((wmh = XGetWMHints(dpy, c->win))) {
  1692. if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  1693. wmh->flags &= ~XUrgencyHint;
  1694. XSetWMHints(dpy, c->win, wmh);
  1695. }
  1696. else
  1697. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1698. XFree(wmh);
  1699. }
  1700. }
  1701. void
  1702. view(const Arg *arg) {
  1703. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1704. return;
  1705. selmon->seltags ^= 1; /* toggle sel tagset */
  1706. if(arg->ui & TAGMASK)
  1707. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1708. arrange();
  1709. }
  1710. Client *
  1711. wintoclient(Window w) {
  1712. Client *c;
  1713. Monitor *m;
  1714. for(m = mons; m; m = m->next)
  1715. for(c = m->clients; c; c = c->next)
  1716. if(c->win == w)
  1717. return c;
  1718. return NULL;
  1719. }
  1720. Monitor *
  1721. wintomon(Window w) {
  1722. int x, y;
  1723. Client *c;
  1724. Monitor *m;
  1725. if(w == root && getrootptr(&x, &y))
  1726. return ptrtomon(x, y);
  1727. for(m = mons; m; m = m->next)
  1728. if(w == m->barwin)
  1729. return m;
  1730. if((c = wintoclient(w)))
  1731. return c->mon;
  1732. return selmon;
  1733. }
  1734. /* There's no way to check accesses to destroyed windows, thus those cases are
  1735. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1736. * default error handler, which may call exit. */
  1737. int
  1738. xerror(Display *dpy, XErrorEvent *ee) {
  1739. if(ee->error_code == BadWindow
  1740. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1741. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1742. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1743. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1744. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1745. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1746. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1747. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1748. return 0;
  1749. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1750. ee->request_code, ee->error_code);
  1751. return xerrorxlib(dpy, ee); /* may call exit */
  1752. }
  1753. int
  1754. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1755. return 0;
  1756. }
  1757. /* Startup Error handler to check if another window manager
  1758. * is already running. */
  1759. int
  1760. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1761. otherwm = True;
  1762. return -1;
  1763. }
  1764. void
  1765. zoom(const Arg *arg) {
  1766. Client *c = selmon->sel;
  1767. if(!selmon->lt[selmon->sellt]->arrange
  1768. || selmon->lt[selmon->sellt]->arrange == monocle
  1769. || (selmon->sel && selmon->sel->isfloating))
  1770. return;
  1771. if(c == nexttiled(selmon->clients))
  1772. if(!c || !(c = nexttiled(c->next)))
  1773. return;
  1774. detach(c);
  1775. attach(c);
  1776. focus(c);
  1777. arrange();
  1778. }
  1779. int
  1780. main(int argc, char *argv[]) {
  1781. if(argc == 2 && !strcmp("-v", argv[1]))
  1782. die("dwm-"VERSION", © 2006-2009 dwm engineers, see LICENSE for details\n");
  1783. else if(argc != 1)
  1784. die("usage: dwm [-v]\n");
  1785. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1786. fputs("warning: no locale support\n", stderr);
  1787. if(!(dpy = XOpenDisplay(NULL)))
  1788. die("dwm: cannot open display\n");
  1789. checkotherwm();
  1790. setup();
  1791. scan();
  1792. run();
  1793. cleanup();
  1794. XCloseDisplay(dpy);
  1795. return 0;
  1796. }