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.

2062 lines
51 KiB

17 years ago
16 years ago
16 years ago
14 years ago
17 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
13 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
17 years ago
17 years ago
17 years ago
16 years ago
13 years ago
17 years ago
17 years ago
17 years ago
17 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. #include "drw.h"
  43. #include "util.h"
  44. /* macros */
  45. #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
  46. #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
  47. #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
  48. * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
  49. #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
  50. #define LENGTH(X) (sizeof X / sizeof X[0])
  51. #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
  52. #define WIDTH(X) ((X)->w + 2 * (X)->bw)
  53. #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
  54. #define TAGMASK ((1 << LENGTH(tags)) - 1)
  55. #define TEXTW(X) (drw_font_getexts_width(drw->font, X, strlen(X)) + drw->font->h)
  56. /* enums */
  57. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  58. enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */
  59. enum { NetSupported, NetWMName, NetWMState,
  60. NetWMFullscreen, NetActiveWindow, NetWMWindowType,
  61. NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
  62. enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
  63. enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
  64. ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
  65. typedef union {
  66. int i;
  67. unsigned int ui;
  68. float f;
  69. const void *v;
  70. } Arg;
  71. typedef struct {
  72. unsigned int click;
  73. unsigned int mask;
  74. unsigned int button;
  75. void (*func)(const Arg *arg);
  76. const Arg arg;
  77. } Button;
  78. typedef struct Monitor Monitor;
  79. typedef struct Client Client;
  80. struct Client {
  81. char name[256];
  82. float mina, maxa;
  83. int x, y, w, h;
  84. int oldx, oldy, oldw, oldh;
  85. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  86. int bw, oldbw;
  87. unsigned int tags;
  88. Bool isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
  89. Client *next;
  90. Client *snext;
  91. Monitor *mon;
  92. Window win;
  93. };
  94. typedef struct {
  95. unsigned int mod;
  96. KeySym keysym;
  97. void (*func)(const Arg *);
  98. const Arg arg;
  99. } Key;
  100. typedef struct {
  101. const char *symbol;
  102. void (*arrange)(Monitor *);
  103. } Layout;
  104. struct Monitor {
  105. char ltsymbol[16];
  106. float mfact;
  107. int nmaster;
  108. int num;
  109. int by; /* bar geometry */
  110. int mx, my, mw, mh; /* screen size */
  111. int wx, wy, ww, wh; /* window area */
  112. unsigned int seltags;
  113. unsigned int sellt;
  114. unsigned int tagset[2];
  115. Bool showbar;
  116. Bool topbar;
  117. Client *clients;
  118. Client *sel;
  119. Client *stack;
  120. Monitor *next;
  121. Window barwin;
  122. const Layout *lt[2];
  123. };
  124. typedef struct {
  125. const char *class;
  126. const char *instance;
  127. const char *title;
  128. unsigned int tags;
  129. Bool isfloating;
  130. int monitor;
  131. } Rule;
  132. /* function declarations */
  133. static void applyrules(Client *c);
  134. static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
  135. static void arrange(Monitor *m);
  136. static void arrangemon(Monitor *m);
  137. static void attach(Client *c);
  138. static void attachstack(Client *c);
  139. static void buttonpress(XEvent *e);
  140. static void checkotherwm(void);
  141. static void cleanup(void);
  142. static void cleanupmon(Monitor *mon);
  143. static void clearurgent(Client *c);
  144. static void clientmessage(XEvent *e);
  145. static void configure(Client *c);
  146. static void configurenotify(XEvent *e);
  147. static void configurerequest(XEvent *e);
  148. static Monitor *createmon(void);
  149. static void destroynotify(XEvent *e);
  150. static void detach(Client *c);
  151. static void detachstack(Client *c);
  152. static Monitor *dirtomon(int dir);
  153. static void drawbar(Monitor *m);
  154. static void drawbars(void);
  155. static void enternotify(XEvent *e);
  156. static void expose(XEvent *e);
  157. static void focus(Client *c);
  158. static void focusin(XEvent *e);
  159. static void focusmon(const Arg *arg);
  160. static void focusstack(const Arg *arg);
  161. static Bool getrootptr(int *x, int *y);
  162. static long getstate(Window w);
  163. static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  164. static void grabbuttons(Client *c, Bool focused);
  165. static void grabkeys(void);
  166. static void incnmaster(const Arg *arg);
  167. static void keypress(XEvent *e);
  168. static void killclient(const Arg *arg);
  169. static void manage(Window w, XWindowAttributes *wa);
  170. static void mappingnotify(XEvent *e);
  171. static void maprequest(XEvent *e);
  172. static void monocle(Monitor *m);
  173. static void motionnotify(XEvent *e);
  174. static void movemouse(const Arg *arg);
  175. static Client *nexttiled(Client *c);
  176. static void pop(Client *);
  177. static void propertynotify(XEvent *e);
  178. static void quit(const Arg *arg);
  179. static Monitor *recttomon(int x, int y, int w, int h);
  180. static void resize(Client *c, int x, int y, int w, int h, Bool interact);
  181. static void resizeclient(Client *c, int x, int y, int w, int h);
  182. static void resizemouse(const Arg *arg);
  183. static void restack(Monitor *m);
  184. static void run(void);
  185. static void scan(void);
  186. static Bool sendevent(Client *c, Atom proto);
  187. static void sendmon(Client *c, Monitor *m);
  188. static void setclientstate(Client *c, long state);
  189. static void setfocus(Client *c);
  190. static void setfullscreen(Client *c, Bool fullscreen);
  191. static void setlayout(const Arg *arg);
  192. static void setmfact(const Arg *arg);
  193. static void setup(void);
  194. static void showhide(Client *c);
  195. static void sigchld(int unused);
  196. static void spawn(const Arg *arg);
  197. static void tag(const Arg *arg);
  198. static void tagmon(const Arg *arg);
  199. static void tile(Monitor *);
  200. static void togglebar(const Arg *arg);
  201. static void togglefloating(const Arg *arg);
  202. static void toggletag(const Arg *arg);
  203. static void toggleview(const Arg *arg);
  204. static void unfocus(Client *c, Bool setfocus);
  205. static void unmanage(Client *c, Bool destroyed);
  206. static void unmapnotify(XEvent *e);
  207. static Bool updategeom(void);
  208. static void updatebarpos(Monitor *m);
  209. static void updatebars(void);
  210. static void updateclientlist(void);
  211. static void updatenumlockmask(void);
  212. static void updatesizehints(Client *c);
  213. static void updatestatus(void);
  214. static void updatewindowtype(Client *c);
  215. static void updatetitle(Client *c);
  216. static void updatewmhints(Client *c);
  217. static void view(const Arg *arg);
  218. static Client *wintoclient(Window w);
  219. static Monitor *wintomon(Window w);
  220. static int xerror(Display *dpy, XErrorEvent *ee);
  221. static int xerrordummy(Display *dpy, XErrorEvent *ee);
  222. static int xerrorstart(Display *dpy, XErrorEvent *ee);
  223. static void zoom(const Arg *arg);
  224. /* variables */
  225. static const char broken[] = "broken";
  226. static char stext[256];
  227. static int screen;
  228. static int sw, sh; /* X display screen geometry width, height */
  229. static int bh, blw = 0; /* bar geometry */
  230. static int (*xerrorxlib)(Display *, XErrorEvent *);
  231. static unsigned int numlockmask = 0;
  232. static void (*handler[LASTEvent]) (XEvent *) = {
  233. [ButtonPress] = buttonpress,
  234. [ClientMessage] = clientmessage,
  235. [ConfigureRequest] = configurerequest,
  236. [ConfigureNotify] = configurenotify,
  237. [DestroyNotify] = destroynotify,
  238. [EnterNotify] = enternotify,
  239. [Expose] = expose,
  240. [FocusIn] = focusin,
  241. [KeyPress] = keypress,
  242. [MappingNotify] = mappingnotify,
  243. [MapRequest] = maprequest,
  244. [MotionNotify] = motionnotify,
  245. [PropertyNotify] = propertynotify,
  246. [UnmapNotify] = unmapnotify
  247. };
  248. static Atom wmatom[WMLast], netatom[NetLast];
  249. static Bool running = True;
  250. static Cur *cursor[CurLast];
  251. static ClrScheme scheme[SchemeLast];
  252. static Display *dpy;
  253. static Drw *drw;
  254. static Fnt *fnt;
  255. static Monitor *mons, *selmon;
  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 = { NULL, NULL };
  269. /* rule matching */
  270. c->isfloating = c->tags = 0;
  271. 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. c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
  292. }
  293. Bool
  294. applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
  295. Bool baseismin;
  296. Monitor *m = c->mon;
  297. /* set minimum possible */
  298. *w = MAX(1, *w);
  299. *h = MAX(1, *h);
  300. if(interact) {
  301. if(*x > sw)
  302. *x = sw - WIDTH(c);
  303. if(*y > sh)
  304. *y = sh - HEIGHT(c);
  305. if(*x + *w + 2 * c->bw < 0)
  306. *x = 0;
  307. if(*y + *h + 2 * c->bw < 0)
  308. *y = 0;
  309. }
  310. else {
  311. if(*x >= m->wx + m->ww)
  312. *x = m->wx + m->ww - WIDTH(c);
  313. if(*y >= m->wy + m->wh)
  314. *y = m->wy + m->wh - HEIGHT(c);
  315. if(*x + *w + 2 * c->bw <= m->wx)
  316. *x = m->wx;
  317. if(*y + *h + 2 * c->bw <= m->wy)
  318. *y = m->wy;
  319. }
  320. if(*h < bh)
  321. *h = bh;
  322. if(*w < bh)
  323. *w = bh;
  324. if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
  325. /* see last two sentences in ICCCM 4.1.2.3 */
  326. baseismin = c->basew == c->minw && c->baseh == c->minh;
  327. if(!baseismin) { /* temporarily remove base dimensions */
  328. *w -= c->basew;
  329. *h -= c->baseh;
  330. }
  331. /* adjust for aspect limits */
  332. if(c->mina > 0 && c->maxa > 0) {
  333. if(c->maxa < (float)*w / *h)
  334. *w = *h * c->maxa + 0.5;
  335. else if(c->mina < (float)*h / *w)
  336. *h = *w * c->mina + 0.5;
  337. }
  338. if(baseismin) { /* increment calculation requires this */
  339. *w -= c->basew;
  340. *h -= c->baseh;
  341. }
  342. /* adjust for increment value */
  343. if(c->incw)
  344. *w -= *w % c->incw;
  345. if(c->inch)
  346. *h -= *h % c->inch;
  347. /* restore base dimensions */
  348. *w = MAX(*w + c->basew, c->minw);
  349. *h = MAX(*h + c->baseh, c->minh);
  350. if(c->maxw)
  351. *w = MIN(*w, c->maxw);
  352. if(c->maxh)
  353. *h = MIN(*h, c->maxh);
  354. }
  355. return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
  356. }
  357. void
  358. arrange(Monitor *m) {
  359. if(m)
  360. showhide(m->stack);
  361. else for(m = mons; m; m = m->next)
  362. showhide(m->stack);
  363. if(m) {
  364. arrangemon(m);
  365. restack(m);
  366. } else for(m = mons; m; m = m->next)
  367. arrangemon(m);
  368. }
  369. void
  370. arrangemon(Monitor *m) {
  371. strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
  372. if(m->lt[m->sellt]->arrange)
  373. m->lt[m->sellt]->arrange(m);
  374. }
  375. void
  376. attach(Client *c) {
  377. c->next = c->mon->clients;
  378. c->mon->clients = c;
  379. }
  380. void
  381. attachstack(Client *c) {
  382. c->snext = c->mon->stack;
  383. c->mon->stack = c;
  384. }
  385. void
  386. buttonpress(XEvent *e) {
  387. unsigned int i, x, click;
  388. Arg arg = {0};
  389. Client *c;
  390. Monitor *m;
  391. XButtonPressedEvent *ev = &e->xbutton;
  392. click = ClkRootWin;
  393. /* focus monitor if necessary */
  394. if((m = wintomon(ev->window)) && m != selmon) {
  395. unfocus(selmon->sel, True);
  396. selmon = m;
  397. focus(NULL);
  398. }
  399. if(ev->window == selmon->barwin) {
  400. i = x = 0;
  401. do
  402. x += TEXTW(tags[i]);
  403. while(ev->x >= x && ++i < LENGTH(tags));
  404. if(i < LENGTH(tags)) {
  405. click = ClkTagBar;
  406. arg.ui = 1 << i;
  407. }
  408. else if(ev->x < x + blw)
  409. click = ClkLtSymbol;
  410. else if(ev->x > selmon->ww - TEXTW(stext))
  411. click = ClkStatusText;
  412. else
  413. click = ClkWinTitle;
  414. }
  415. else if((c = wintoclient(ev->window))) {
  416. focus(c);
  417. click = ClkClientWin;
  418. }
  419. for(i = 0; i < LENGTH(buttons); i++)
  420. if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
  421. && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
  422. buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
  423. }
  424. void
  425. checkotherwm(void) {
  426. xerrorxlib = XSetErrorHandler(xerrorstart);
  427. /* this causes an error if some other window manager is running */
  428. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  429. XSync(dpy, False);
  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. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  444. while(mons)
  445. cleanupmon(mons);
  446. drw_cur_free(drw, cursor[CurNormal]);
  447. drw_cur_free(drw, cursor[CurResize]);
  448. drw_cur_free(drw, cursor[CurMove]);
  449. drw_font_free(dpy, fnt);
  450. drw_clr_free(scheme[SchemeNorm].border);
  451. drw_clr_free(scheme[SchemeNorm].bg);
  452. drw_clr_free(scheme[SchemeNorm].fg);
  453. drw_clr_free(scheme[SchemeSel].border);
  454. drw_clr_free(scheme[SchemeSel].bg);
  455. drw_clr_free(scheme[SchemeSel].fg);
  456. drw_free(drw);
  457. XSync(dpy, False);
  458. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  459. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  460. }
  461. void
  462. cleanupmon(Monitor *mon) {
  463. Monitor *m;
  464. if(mon == mons)
  465. mons = mons->next;
  466. else {
  467. for(m = mons; m && m->next != mon; m = m->next);
  468. m->next = mon->next;
  469. }
  470. XUnmapWindow(dpy, mon->barwin);
  471. XDestroyWindow(dpy, mon->barwin);
  472. free(mon);
  473. }
  474. void
  475. clearurgent(Client *c) {
  476. XWMHints *wmh;
  477. c->isurgent = False;
  478. if(!(wmh = XGetWMHints(dpy, c->win)))
  479. return;
  480. wmh->flags &= ~XUrgencyHint;
  481. XSetWMHints(dpy, c->win, wmh);
  482. XFree(wmh);
  483. }
  484. void
  485. clientmessage(XEvent *e) {
  486. XClientMessageEvent *cme = &e->xclient;
  487. Client *c = wintoclient(cme->window);
  488. if(!c)
  489. return;
  490. if(cme->message_type == netatom[NetWMState]) {
  491. if(cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
  492. setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
  493. || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
  494. }
  495. else if(cme->message_type == netatom[NetActiveWindow]) {
  496. if(!ISVISIBLE(c)) {
  497. c->mon->seltags ^= 1;
  498. c->mon->tagset[c->mon->seltags] = c->tags;
  499. }
  500. pop(c);
  501. }
  502. }
  503. void
  504. configure(Client *c) {
  505. XConfigureEvent ce;
  506. ce.type = ConfigureNotify;
  507. ce.display = dpy;
  508. ce.event = c->win;
  509. ce.window = c->win;
  510. ce.x = c->x;
  511. ce.y = c->y;
  512. ce.width = c->w;
  513. ce.height = c->h;
  514. ce.border_width = c->bw;
  515. ce.above = None;
  516. ce.override_redirect = False;
  517. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  518. }
  519. void
  520. configurenotify(XEvent *e) {
  521. Monitor *m;
  522. XConfigureEvent *ev = &e->xconfigure;
  523. Bool dirty;
  524. // TODO: updategeom handling sucks, needs to be simplified
  525. if(ev->window == root) {
  526. dirty = (sw != ev->width || sh != ev->height);
  527. sw = ev->width;
  528. sh = ev->height;
  529. if(updategeom() || dirty) {
  530. drw_resize(drw, sw, bh);
  531. updatebars();
  532. for(m = mons; m; m = m->next)
  533. XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
  534. focus(NULL);
  535. arrange(NULL);
  536. }
  537. }
  538. }
  539. void
  540. configurerequest(XEvent *e) {
  541. Client *c;
  542. Monitor *m;
  543. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  544. XWindowChanges wc;
  545. if((c = wintoclient(ev->window))) {
  546. if(ev->value_mask & CWBorderWidth)
  547. c->bw = ev->border_width;
  548. else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
  549. m = c->mon;
  550. if(ev->value_mask & CWX) {
  551. c->oldx = c->x;
  552. c->x = m->mx + ev->x;
  553. }
  554. if(ev->value_mask & CWY) {
  555. c->oldy = c->y;
  556. c->y = m->my + ev->y;
  557. }
  558. if(ev->value_mask & CWWidth) {
  559. c->oldw = c->w;
  560. c->w = ev->width;
  561. }
  562. if(ev->value_mask & CWHeight) {
  563. c->oldh = c->h;
  564. c->h = ev->height;
  565. }
  566. if((c->x + c->w) > m->mx + m->mw && c->isfloating)
  567. c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
  568. if((c->y + c->h) > m->my + m->mh && c->isfloating)
  569. c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
  570. if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
  571. configure(c);
  572. if(ISVISIBLE(c))
  573. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  574. }
  575. else
  576. configure(c);
  577. }
  578. else {
  579. wc.x = ev->x;
  580. wc.y = ev->y;
  581. wc.width = ev->width;
  582. wc.height = ev->height;
  583. wc.border_width = ev->border_width;
  584. wc.sibling = ev->above;
  585. wc.stack_mode = ev->detail;
  586. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  587. }
  588. XSync(dpy, False);
  589. }
  590. Monitor *
  591. createmon(void) {
  592. Monitor *m;
  593. if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
  594. die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
  595. m->tagset[0] = m->tagset[1] = 1;
  596. m->mfact = mfact;
  597. m->nmaster = nmaster;
  598. m->showbar = showbar;
  599. m->topbar = topbar;
  600. m->lt[0] = &layouts[0];
  601. m->lt[1] = &layouts[1 % LENGTH(layouts)];
  602. strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
  603. return m;
  604. }
  605. void
  606. destroynotify(XEvent *e) {
  607. Client *c;
  608. XDestroyWindowEvent *ev = &e->xdestroywindow;
  609. if((c = wintoclient(ev->window)))
  610. unmanage(c, True);
  611. }
  612. void
  613. detach(Client *c) {
  614. Client **tc;
  615. for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
  616. *tc = c->next;
  617. }
  618. void
  619. detachstack(Client *c) {
  620. Client **tc, *t;
  621. for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
  622. *tc = c->snext;
  623. if(c == c->mon->sel) {
  624. for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
  625. c->mon->sel = t;
  626. }
  627. }
  628. Monitor *
  629. dirtomon(int dir) {
  630. Monitor *m = NULL;
  631. if(dir > 0) {
  632. if(!(m = selmon->next))
  633. m = mons;
  634. }
  635. else if(selmon == mons)
  636. for(m = mons; m->next; m = m->next);
  637. else
  638. for(m = mons; m->next != selmon; m = m->next);
  639. return m;
  640. }
  641. void
  642. drawbar(Monitor *m) {
  643. int x, xx, w;
  644. unsigned int i, occ = 0, urg = 0;
  645. Client *c;
  646. for(c = m->clients; c; c = c->next) {
  647. occ |= c->tags;
  648. if(c->isurgent)
  649. urg |= c->tags;
  650. }
  651. x = 0;
  652. for(i = 0; i < LENGTH(tags); i++) {
  653. w = TEXTW(tags[i]);
  654. drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
  655. drw_text(drw, x, 0, w, bh, tags[i], urg & 1 << i);
  656. drw_rect(drw, x, 0, w, bh, m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
  657. occ & 1 << i, urg & 1 << i);
  658. x += w;
  659. }
  660. w = blw = TEXTW(m->ltsymbol);
  661. drw_setscheme(drw, &scheme[SchemeNorm]);
  662. drw_text(drw, x, 0, w, bh, m->ltsymbol, 0);
  663. x += w;
  664. xx = x;
  665. if(m == selmon) { /* status is only drawn on selected monitor */
  666. w = TEXTW(stext);
  667. x = m->ww - w;
  668. if(x < xx) {
  669. x = xx;
  670. w = m->ww - xx;
  671. }
  672. drw_text(drw, x, 0, w, bh, stext, 0);
  673. }
  674. else
  675. x = m->ww;
  676. if((w = x - xx) > bh) {
  677. x = xx;
  678. if(m->sel) {
  679. drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
  680. drw_text(drw, x, 0, w, bh, m->sel->name, 0);
  681. drw_rect(drw, x, 0, w, bh, m->sel->isfixed, m->sel->isfloating, 0);
  682. }
  683. else {
  684. drw_setscheme(drw, &scheme[SchemeNorm]);
  685. drw_text(drw, x, 0, w, bh, NULL, 0);
  686. }
  687. }
  688. drw_map(drw, m->barwin, 0, 0, m->ww, bh);
  689. }
  690. void
  691. drawbars(void) {
  692. Monitor *m;
  693. for(m = mons; m; m = m->next)
  694. drawbar(m);
  695. }
  696. void
  697. enternotify(XEvent *e) {
  698. Client *c;
  699. Monitor *m;
  700. XCrossingEvent *ev = &e->xcrossing;
  701. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  702. return;
  703. c = wintoclient(ev->window);
  704. m = c ? c->mon : wintomon(ev->window);
  705. if(m != selmon) {
  706. unfocus(selmon->sel, True);
  707. selmon = m;
  708. }
  709. else if(!c || c == selmon->sel)
  710. return;
  711. focus(c);
  712. }
  713. void
  714. expose(XEvent *e) {
  715. Monitor *m;
  716. XExposeEvent *ev = &e->xexpose;
  717. if(ev->count == 0 && (m = wintomon(ev->window)))
  718. drawbar(m);
  719. }
  720. void
  721. focus(Client *c) {
  722. if(!c || !ISVISIBLE(c))
  723. for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
  724. /* was if(selmon->sel) */
  725. if(selmon->sel && selmon->sel != c)
  726. unfocus(selmon->sel, False);
  727. if(c) {
  728. if(c->mon != selmon)
  729. selmon = c->mon;
  730. if(c->isurgent)
  731. clearurgent(c);
  732. detachstack(c);
  733. attachstack(c);
  734. grabbuttons(c, True);
  735. XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->rgb);
  736. setfocus(c);
  737. }
  738. else {
  739. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  740. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  741. }
  742. selmon->sel = c;
  743. drawbars();
  744. }
  745. void
  746. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  747. XFocusChangeEvent *ev = &e->xfocus;
  748. if(selmon->sel && ev->window != selmon->sel->win)
  749. setfocus(selmon->sel);
  750. }
  751. void
  752. focusmon(const Arg *arg) {
  753. Monitor *m;
  754. if(!mons->next)
  755. return;
  756. if((m = dirtomon(arg->i)) == selmon)
  757. return;
  758. unfocus(selmon->sel, False); /* s/True/False/ fixes input focus issues
  759. in gedit and anjuta */
  760. selmon = m;
  761. focus(NULL);
  762. }
  763. void
  764. focusstack(const Arg *arg) {
  765. Client *c = NULL, *i;
  766. if(!selmon->sel)
  767. return;
  768. if(arg->i > 0) {
  769. for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
  770. if(!c)
  771. for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
  772. }
  773. else {
  774. for(i = selmon->clients; i != selmon->sel; i = i->next)
  775. if(ISVISIBLE(i))
  776. c = i;
  777. if(!c)
  778. for(; i; i = i->next)
  779. if(ISVISIBLE(i))
  780. c = i;
  781. }
  782. if(c) {
  783. focus(c);
  784. restack(selmon);
  785. }
  786. }
  787. Atom
  788. getatomprop(Client *c, Atom prop) {
  789. int di;
  790. unsigned long dl;
  791. unsigned char *p = NULL;
  792. Atom da, atom = None;
  793. if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
  794. &da, &di, &dl, &dl, &p) == Success && p) {
  795. atom = *(Atom *)p;
  796. XFree(p);
  797. }
  798. return atom;
  799. }
  800. Bool
  801. getrootptr(int *x, int *y) {
  802. int di;
  803. unsigned int dui;
  804. Window dummy;
  805. return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
  806. }
  807. long
  808. getstate(Window w) {
  809. int format;
  810. long result = -1;
  811. unsigned char *p = NULL;
  812. unsigned long n, extra;
  813. Atom real;
  814. if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  815. &real, &format, &n, &extra, (unsigned char **)&p) != 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. void
  882. incnmaster(const Arg *arg) {
  883. selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
  884. arrange(selmon);
  885. }
  886. #ifdef XINERAMA
  887. static Bool
  888. isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
  889. while(n--)
  890. if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
  891. && unique[n].width == info->width && unique[n].height == info->height)
  892. return False;
  893. return True;
  894. }
  895. #endif /* XINERAMA */
  896. void
  897. keypress(XEvent *e) {
  898. unsigned int i;
  899. KeySym keysym;
  900. XKeyEvent *ev;
  901. ev = &e->xkey;
  902. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  903. for(i = 0; i < LENGTH(keys); i++)
  904. if(keysym == keys[i].keysym
  905. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  906. && keys[i].func)
  907. keys[i].func(&(keys[i].arg));
  908. }
  909. void
  910. killclient(const Arg *arg) {
  911. if(!selmon->sel)
  912. return;
  913. if(!sendevent(selmon->sel, wmatom[WMDelete])) {
  914. XGrabServer(dpy);
  915. XSetErrorHandler(xerrordummy);
  916. XSetCloseDownMode(dpy, DestroyAll);
  917. XKillClient(dpy, selmon->sel->win);
  918. XSync(dpy, False);
  919. XSetErrorHandler(xerror);
  920. XUngrabServer(dpy);
  921. }
  922. }
  923. void
  924. manage(Window w, XWindowAttributes *wa) {
  925. Client *c, *t = NULL;
  926. Window trans = None;
  927. XWindowChanges wc;
  928. if(!(c = calloc(1, sizeof(Client))))
  929. die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  930. c->win = w;
  931. updatetitle(c);
  932. if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
  933. c->mon = t->mon;
  934. c->tags = t->tags;
  935. }
  936. else {
  937. c->mon = selmon;
  938. applyrules(c);
  939. }
  940. /* geometry */
  941. c->x = c->oldx = wa->x;
  942. c->y = c->oldy = wa->y;
  943. c->w = c->oldw = wa->width;
  944. c->h = c->oldh = wa->height;
  945. c->oldbw = wa->border_width;
  946. if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  947. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  948. if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  949. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  950. c->x = MAX(c->x, c->mon->mx);
  951. /* only fix client y-offset, if the client center might cover the bar */
  952. c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
  953. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  954. c->bw = borderpx;
  955. wc.border_width = c->bw;
  956. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  957. XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->rgb);
  958. configure(c); /* propagates border_width, if size doesn't change */
  959. updatewindowtype(c);
  960. updatesizehints(c);
  961. updatewmhints(c);
  962. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  963. grabbuttons(c, False);
  964. if(!c->isfloating)
  965. c->isfloating = c->oldstate = trans != None || c->isfixed;
  966. if(c->isfloating)
  967. XRaiseWindow(dpy, c->win);
  968. attach(c);
  969. attachstack(c);
  970. XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
  971. (unsigned char *) &(c->win), 1);
  972. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  973. setclientstate(c, NormalState);
  974. if (c->mon == selmon)
  975. unfocus(selmon->sel, False);
  976. c->mon->sel = c;
  977. arrange(c->mon);
  978. XMapWindow(dpy, c->win);
  979. focus(NULL);
  980. }
  981. void
  982. mappingnotify(XEvent *e) {
  983. XMappingEvent *ev = &e->xmapping;
  984. XRefreshKeyboardMapping(ev);
  985. if(ev->request == MappingKeyboard)
  986. grabkeys();
  987. }
  988. void
  989. maprequest(XEvent *e) {
  990. static XWindowAttributes wa;
  991. XMapRequestEvent *ev = &e->xmaprequest;
  992. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  993. return;
  994. if(wa.override_redirect)
  995. return;
  996. if(!wintoclient(ev->window))
  997. manage(ev->window, &wa);
  998. }
  999. void
  1000. monocle(Monitor *m) {
  1001. unsigned int n = 0;
  1002. Client *c;
  1003. for(c = m->clients; c; c = c->next)
  1004. if(ISVISIBLE(c))
  1005. n++;
  1006. if(n > 0) /* override layout symbol */
  1007. snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
  1008. for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1009. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
  1010. }
  1011. void
  1012. motionnotify(XEvent *e) {
  1013. static Monitor *mon = NULL;
  1014. Monitor *m;
  1015. XMotionEvent *ev = &e->xmotion;
  1016. if(ev->window != root)
  1017. return;
  1018. if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
  1019. unfocus(selmon->sel, True);
  1020. selmon = m;
  1021. focus(NULL);
  1022. }
  1023. mon = m;
  1024. }
  1025. void
  1026. movemouse(const Arg *arg) {
  1027. int x, y, ocx, ocy, nx, ny;
  1028. Client *c;
  1029. Monitor *m;
  1030. XEvent ev;
  1031. if(!(c = selmon->sel))
  1032. return;
  1033. if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
  1034. return;
  1035. restack(selmon);
  1036. ocx = c->x;
  1037. ocy = c->y;
  1038. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1039. None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
  1040. return;
  1041. if(!getrootptr(&x, &y))
  1042. return;
  1043. do {
  1044. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1045. switch(ev.type) {
  1046. case ConfigureRequest:
  1047. case Expose:
  1048. case MapRequest:
  1049. handler[ev.type](&ev);
  1050. break;
  1051. case MotionNotify:
  1052. nx = ocx + (ev.xmotion.x - x);
  1053. ny = ocy + (ev.xmotion.y - y);
  1054. if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1055. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1056. if(abs(selmon->wx - nx) < snap)
  1057. nx = selmon->wx;
  1058. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1059. nx = selmon->wx + selmon->ww - WIDTH(c);
  1060. if(abs(selmon->wy - ny) < snap)
  1061. ny = selmon->wy;
  1062. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1063. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1064. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1065. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1066. togglefloating(NULL);
  1067. }
  1068. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1069. resize(c, nx, ny, c->w, c->h, True);
  1070. break;
  1071. }
  1072. } while(ev.type != ButtonRelease);
  1073. XUngrabPointer(dpy, CurrentTime);
  1074. if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1075. sendmon(c, m);
  1076. selmon = m;
  1077. focus(NULL);
  1078. }
  1079. }
  1080. Client *
  1081. nexttiled(Client *c) {
  1082. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1083. return c;
  1084. }
  1085. void
  1086. pop(Client *c) {
  1087. detach(c);
  1088. attach(c);
  1089. focus(c);
  1090. arrange(c->mon);
  1091. }
  1092. void
  1093. propertynotify(XEvent *e) {
  1094. Client *c;
  1095. Window trans;
  1096. XPropertyEvent *ev = &e->xproperty;
  1097. if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1098. updatestatus();
  1099. else if(ev->state == PropertyDelete)
  1100. return; /* ignore */
  1101. else if((c = wintoclient(ev->window))) {
  1102. switch(ev->atom) {
  1103. default: break;
  1104. case XA_WM_TRANSIENT_FOR:
  1105. if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
  1106. (c->isfloating = (wintoclient(trans)) != NULL))
  1107. arrange(c->mon);
  1108. break;
  1109. case XA_WM_NORMAL_HINTS:
  1110. updatesizehints(c);
  1111. break;
  1112. case XA_WM_HINTS:
  1113. updatewmhints(c);
  1114. drawbars();
  1115. break;
  1116. }
  1117. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1118. updatetitle(c);
  1119. if(c == c->mon->sel)
  1120. drawbar(c->mon);
  1121. }
  1122. if(ev->atom == netatom[NetWMWindowType])
  1123. updatewindowtype(c);
  1124. }
  1125. }
  1126. void
  1127. quit(const Arg *arg) {
  1128. running = False;
  1129. }
  1130. Monitor *
  1131. recttomon(int x, int y, int w, int h) {
  1132. Monitor *m, *r = selmon;
  1133. int a, area = 0;
  1134. for(m = mons; m; m = m->next)
  1135. if((a = INTERSECT(x, y, w, h, m)) > area) {
  1136. area = a;
  1137. r = m;
  1138. }
  1139. return r;
  1140. }
  1141. void
  1142. resize(Client *c, int x, int y, int w, int h, Bool interact) {
  1143. if(applysizehints(c, &x, &y, &w, &h, interact))
  1144. resizeclient(c, x, y, w, h);
  1145. }
  1146. void
  1147. resizeclient(Client *c, int x, int y, int w, int h) {
  1148. XWindowChanges wc;
  1149. c->oldx = c->x; c->x = wc.x = x;
  1150. c->oldy = c->y; c->y = wc.y = y;
  1151. c->oldw = c->w; c->w = wc.width = w;
  1152. c->oldh = c->h; 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. void
  1159. resizemouse(const Arg *arg) {
  1160. int ocx, ocy;
  1161. int nw, nh;
  1162. Client *c;
  1163. Monitor *m;
  1164. XEvent ev;
  1165. if(!(c = selmon->sel))
  1166. return;
  1167. if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
  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]->cursor, 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(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
  1188. && c->mon->wy + nh >= selmon->wy && c->mon->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 = recttomon(c->x, c->y, c->w, c->h)) != 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. drawbar(m);
  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, True);
  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(NULL);
  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. Bool
  1284. sendevent(Client *c, Atom proto) {
  1285. int n;
  1286. Atom *protocols;
  1287. Bool exists = False;
  1288. XEvent ev;
  1289. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  1290. while(!exists && n--)
  1291. exists = protocols[n] == proto;
  1292. XFree(protocols);
  1293. }
  1294. if(exists) {
  1295. ev.type = ClientMessage;
  1296. ev.xclient.window = c->win;
  1297. ev.xclient.message_type = wmatom[WMProtocols];
  1298. ev.xclient.format = 32;
  1299. ev.xclient.data.l[0] = proto;
  1300. ev.xclient.data.l[1] = CurrentTime;
  1301. XSendEvent(dpy, c->win, False, NoEventMask, &ev);
  1302. }
  1303. return exists;
  1304. }
  1305. void
  1306. setfocus(Client *c) {
  1307. if(!c->neverfocus) {
  1308. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  1309. XChangeProperty(dpy, root, netatom[NetActiveWindow],
  1310. XA_WINDOW, 32, PropModeReplace,
  1311. (unsigned char *) &(c->win), 1);
  1312. }
  1313. sendevent(c, wmatom[WMTakeFocus]);
  1314. }
  1315. void
  1316. setfullscreen(Client *c, Bool fullscreen) {
  1317. if(fullscreen) {
  1318. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1319. PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
  1320. c->isfullscreen = True;
  1321. c->oldstate = c->isfloating;
  1322. c->oldbw = c->bw;
  1323. c->bw = 0;
  1324. c->isfloating = True;
  1325. resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
  1326. XRaiseWindow(dpy, c->win);
  1327. }
  1328. else {
  1329. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1330. PropModeReplace, (unsigned char*)0, 0);
  1331. c->isfullscreen = False;
  1332. c->isfloating = c->oldstate;
  1333. c->bw = c->oldbw;
  1334. c->x = c->oldx;
  1335. c->y = c->oldy;
  1336. c->w = c->oldw;
  1337. c->h = c->oldh;
  1338. resizeclient(c, c->x, c->y, c->w, c->h);
  1339. arrange(c->mon);
  1340. }
  1341. }
  1342. void
  1343. setlayout(const Arg *arg) {
  1344. if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
  1345. selmon->sellt ^= 1;
  1346. if(arg && arg->v)
  1347. selmon->lt[selmon->sellt] = (Layout *)arg->v;
  1348. strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
  1349. if(selmon->sel)
  1350. arrange(selmon);
  1351. else
  1352. drawbar(selmon);
  1353. }
  1354. /* arg > 1.0 will set mfact absolutly */
  1355. void
  1356. setmfact(const Arg *arg) {
  1357. float f;
  1358. if(!arg || !selmon->lt[selmon->sellt]->arrange)
  1359. return;
  1360. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1361. if(f < 0.1 || f > 0.9)
  1362. return;
  1363. selmon->mfact = f;
  1364. arrange(selmon);
  1365. }
  1366. void
  1367. setup(void) {
  1368. XSetWindowAttributes wa;
  1369. /* clean up any zombies immediately */
  1370. sigchld(0);
  1371. /* init screen */
  1372. screen = DefaultScreen(dpy);
  1373. root = RootWindow(dpy, screen);
  1374. fnt = drw_font_create(dpy, font);
  1375. sw = DisplayWidth(dpy, screen);
  1376. sh = DisplayHeight(dpy, screen);
  1377. bh = fnt->h + 2;
  1378. drw = drw_create(dpy, screen, root, sw, sh);
  1379. drw_setfont(drw, fnt);
  1380. updategeom();
  1381. /* init atoms */
  1382. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1383. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1384. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1385. wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
  1386. netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
  1387. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1388. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1389. netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
  1390. netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
  1391. netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
  1392. netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
  1393. netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
  1394. /* init cursors */
  1395. cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
  1396. cursor[CurResize] = drw_cur_create(drw, XC_sizing);
  1397. cursor[CurMove] = drw_cur_create(drw, XC_fleur);
  1398. /* init appearance */
  1399. scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor);
  1400. scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
  1401. scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
  1402. scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor);
  1403. scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
  1404. scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
  1405. /* init bars */
  1406. updatebars();
  1407. updatestatus();
  1408. /* EWMH support per view */
  1409. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1410. PropModeReplace, (unsigned char *) netatom, NetLast);
  1411. XDeleteProperty(dpy, root, netatom[NetClientList]);
  1412. /* select for events */
  1413. wa.cursor = cursor[CurNormal]->cursor;
  1414. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
  1415. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
  1416. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1417. XSelectInput(dpy, root, wa.event_mask);
  1418. grabkeys();
  1419. }
  1420. void
  1421. showhide(Client *c) {
  1422. if(!c)
  1423. return;
  1424. if(ISVISIBLE(c)) { /* show clients top down */
  1425. XMoveWindow(dpy, c->win, c->x, c->y);
  1426. if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
  1427. resize(c, c->x, c->y, c->w, c->h, False);
  1428. showhide(c->snext);
  1429. }
  1430. else { /* hide clients bottom up */
  1431. showhide(c->snext);
  1432. XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
  1433. }
  1434. }
  1435. void
  1436. sigchld(int unused) {
  1437. if(signal(SIGCHLD, sigchld) == SIG_ERR)
  1438. die("Can't install SIGCHLD handler");
  1439. while(0 < waitpid(-1, NULL, WNOHANG));
  1440. }
  1441. void
  1442. spawn(const Arg *arg) {
  1443. if(arg->v == dmenucmd)
  1444. dmenumon[0] = '0' + selmon->num;
  1445. if(fork() == 0) {
  1446. if(dpy)
  1447. close(ConnectionNumber(dpy));
  1448. setsid();
  1449. execvp(((char **)arg->v)[0], (char **)arg->v);
  1450. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1451. perror(" failed");
  1452. exit(EXIT_SUCCESS);
  1453. }
  1454. }
  1455. void
  1456. tag(const Arg *arg) {
  1457. if(selmon->sel && arg->ui & TAGMASK) {
  1458. selmon->sel->tags = arg->ui & TAGMASK;
  1459. focus(NULL);
  1460. arrange(selmon);
  1461. }
  1462. }
  1463. void
  1464. tagmon(const Arg *arg) {
  1465. if(!selmon->sel || !mons->next)
  1466. return;
  1467. sendmon(selmon->sel, dirtomon(arg->i));
  1468. }
  1469. void
  1470. tile(Monitor *m) {
  1471. unsigned int i, n, h, mw, my, ty;
  1472. Client *c;
  1473. for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  1474. if(n == 0)
  1475. return;
  1476. if(n > m->nmaster)
  1477. mw = m->nmaster ? m->ww * m->mfact : 0;
  1478. else
  1479. mw = m->ww;
  1480. for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  1481. if(i < m->nmaster) {
  1482. h = (m->wh - my) / (MIN(n, m->nmaster) - i);
  1483. resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False);
  1484. my += HEIGHT(c);
  1485. }
  1486. else {
  1487. h = (m->wh - ty) / (n - i);
  1488. resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False);
  1489. ty += HEIGHT(c);
  1490. }
  1491. }
  1492. void
  1493. togglebar(const Arg *arg) {
  1494. selmon->showbar = !selmon->showbar;
  1495. updatebarpos(selmon);
  1496. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1497. arrange(selmon);
  1498. }
  1499. void
  1500. togglefloating(const Arg *arg) {
  1501. if(!selmon->sel)
  1502. return;
  1503. if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
  1504. return;
  1505. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1506. if(selmon->sel->isfloating)
  1507. resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  1508. selmon->sel->w, selmon->sel->h, False);
  1509. arrange(selmon);
  1510. }
  1511. void
  1512. toggletag(const Arg *arg) {
  1513. unsigned int newtags;
  1514. if(!selmon->sel)
  1515. return;
  1516. newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1517. if(newtags) {
  1518. selmon->sel->tags = newtags;
  1519. focus(NULL);
  1520. arrange(selmon);
  1521. }
  1522. }
  1523. void
  1524. toggleview(const Arg *arg) {
  1525. unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1526. if(newtagset) {
  1527. selmon->tagset[selmon->seltags] = newtagset;
  1528. focus(NULL);
  1529. arrange(selmon);
  1530. }
  1531. }
  1532. void
  1533. unfocus(Client *c, Bool setfocus) {
  1534. if(!c)
  1535. return;
  1536. grabbuttons(c, False);
  1537. XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->rgb);
  1538. if(setfocus) {
  1539. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1540. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  1541. }
  1542. }
  1543. void
  1544. unmanage(Client *c, Bool destroyed) {
  1545. Monitor *m = c->mon;
  1546. XWindowChanges wc;
  1547. /* The server grab construct avoids race conditions. */
  1548. detach(c);
  1549. detachstack(c);
  1550. if(!destroyed) {
  1551. wc.border_width = c->oldbw;
  1552. XGrabServer(dpy);
  1553. XSetErrorHandler(xerrordummy);
  1554. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1555. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1556. setclientstate(c, WithdrawnState);
  1557. XSync(dpy, False);
  1558. XSetErrorHandler(xerror);
  1559. XUngrabServer(dpy);
  1560. }
  1561. free(c);
  1562. focus(NULL);
  1563. updateclientlist();
  1564. arrange(m);
  1565. }
  1566. void
  1567. unmapnotify(XEvent *e) {
  1568. Client *c;
  1569. XUnmapEvent *ev = &e->xunmap;
  1570. if((c = wintoclient(ev->window))) {
  1571. if(ev->send_event)
  1572. setclientstate(c, WithdrawnState);
  1573. else
  1574. unmanage(c, False);
  1575. }
  1576. }
  1577. void
  1578. updatebars(void) {
  1579. Monitor *m;
  1580. XSetWindowAttributes wa = {
  1581. .override_redirect = True,
  1582. .background_pixmap = ParentRelative,
  1583. .event_mask = ButtonPressMask|ExposureMask
  1584. };
  1585. for(m = mons; m; m = m->next) {
  1586. if (m->barwin)
  1587. continue;
  1588. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  1589. CopyFromParent, DefaultVisual(dpy, screen),
  1590. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1591. XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
  1592. XMapRaised(dpy, m->barwin);
  1593. }
  1594. }
  1595. void
  1596. updatebarpos(Monitor *m) {
  1597. m->wy = m->my;
  1598. m->wh = m->mh;
  1599. if(m->showbar) {
  1600. m->wh -= bh;
  1601. m->by = m->topbar ? m->wy : m->wy + m->wh;
  1602. m->wy = m->topbar ? m->wy + bh : m->wy;
  1603. }
  1604. else
  1605. m->by = -bh;
  1606. }
  1607. void
  1608. updateclientlist() {
  1609. Client *c;
  1610. Monitor *m;
  1611. XDeleteProperty(dpy, root, netatom[NetClientList]);
  1612. for(m = mons; m; m = m->next)
  1613. for(c = m->clients; c; c = c->next)
  1614. XChangeProperty(dpy, root, netatom[NetClientList],
  1615. XA_WINDOW, 32, PropModeAppend,
  1616. (unsigned char *) &(c->win), 1);
  1617. }
  1618. Bool
  1619. updategeom(void) {
  1620. Bool dirty = False;
  1621. #ifdef XINERAMA
  1622. if(XineramaIsActive(dpy)) {
  1623. int i, j, n, nn;
  1624. Client *c;
  1625. Monitor *m;
  1626. XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
  1627. XineramaScreenInfo *unique = NULL;
  1628. for(n = 0, m = mons; m; m = m->next, n++);
  1629. /* only consider unique geometries as separate screens */
  1630. if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
  1631. die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
  1632. for(i = 0, j = 0; i < nn; i++)
  1633. if(isuniquegeom(unique, j, &info[i]))
  1634. memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
  1635. XFree(info);
  1636. nn = j;
  1637. if(n <= nn) {
  1638. for(i = 0; i < (nn - n); i++) { /* new monitors available */
  1639. for(m = mons; m && m->next; m = m->next);
  1640. if(m)
  1641. m->next = createmon();
  1642. else
  1643. mons = createmon();
  1644. }
  1645. for(i = 0, m = mons; i < nn && m; m = m->next, i++)
  1646. if(i >= n
  1647. || (unique[i].x_org != m->mx || unique[i].y_org != m->my
  1648. || unique[i].width != m->mw || unique[i].height != m->mh))
  1649. {
  1650. dirty = True;
  1651. m->num = i;
  1652. m->mx = m->wx = unique[i].x_org;
  1653. m->my = m->wy = unique[i].y_org;
  1654. m->mw = m->ww = unique[i].width;
  1655. m->mh = m->wh = unique[i].height;
  1656. updatebarpos(m);
  1657. }
  1658. }
  1659. else { /* less monitors available nn < n */
  1660. for(i = nn; i < n; i++) {
  1661. for(m = mons; m && m->next; m = m->next);
  1662. while(m->clients) {
  1663. dirty = True;
  1664. c = m->clients;
  1665. m->clients = c->next;
  1666. detachstack(c);
  1667. c->mon = mons;
  1668. attach(c);
  1669. attachstack(c);
  1670. }
  1671. if(m == selmon)
  1672. selmon = mons;
  1673. cleanupmon(m);
  1674. }
  1675. }
  1676. free(unique);
  1677. }
  1678. else
  1679. #endif /* XINERAMA */
  1680. /* default monitor setup */
  1681. {
  1682. if(!mons)
  1683. mons = createmon();
  1684. if(mons->mw != sw || mons->mh != sh) {
  1685. dirty = True;
  1686. mons->mw = mons->ww = sw;
  1687. mons->mh = mons->wh = sh;
  1688. updatebarpos(mons);
  1689. }
  1690. }
  1691. if(dirty) {
  1692. selmon = mons;
  1693. selmon = wintomon(root);
  1694. }
  1695. return dirty;
  1696. }
  1697. void
  1698. updatenumlockmask(void) {
  1699. unsigned int i, j;
  1700. XModifierKeymap *modmap;
  1701. numlockmask = 0;
  1702. modmap = XGetModifierMapping(dpy);
  1703. for(i = 0; i < 8; i++)
  1704. for(j = 0; j < modmap->max_keypermod; j++)
  1705. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1706. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1707. numlockmask = (1 << i);
  1708. XFreeModifiermap(modmap);
  1709. }
  1710. void
  1711. updatesizehints(Client *c) {
  1712. long msize;
  1713. XSizeHints size;
  1714. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1715. /* size is uninitialized, ensure that size.flags aren't used */
  1716. size.flags = PSize;
  1717. if(size.flags & PBaseSize) {
  1718. c->basew = size.base_width;
  1719. c->baseh = size.base_height;
  1720. }
  1721. else if(size.flags & PMinSize) {
  1722. c->basew = size.min_width;
  1723. c->baseh = size.min_height;
  1724. }
  1725. else
  1726. c->basew = c->baseh = 0;
  1727. if(size.flags & PResizeInc) {
  1728. c->incw = size.width_inc;
  1729. c->inch = size.height_inc;
  1730. }
  1731. else
  1732. c->incw = c->inch = 0;
  1733. if(size.flags & PMaxSize) {
  1734. c->maxw = size.max_width;
  1735. c->maxh = size.max_height;
  1736. }
  1737. else
  1738. c->maxw = c->maxh = 0;
  1739. if(size.flags & PMinSize) {
  1740. c->minw = size.min_width;
  1741. c->minh = size.min_height;
  1742. }
  1743. else if(size.flags & PBaseSize) {
  1744. c->minw = size.base_width;
  1745. c->minh = size.base_height;
  1746. }
  1747. else
  1748. c->minw = c->minh = 0;
  1749. if(size.flags & PAspect) {
  1750. c->mina = (float)size.min_aspect.y / size.min_aspect.x;
  1751. c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
  1752. }
  1753. else
  1754. c->maxa = c->mina = 0.0;
  1755. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1756. && c->maxw == c->minw && c->maxh == c->minh);
  1757. }
  1758. void
  1759. updatetitle(Client *c) {
  1760. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1761. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1762. if(c->name[0] == '\0') /* hack to mark broken clients */
  1763. strcpy(c->name, broken);
  1764. }
  1765. void
  1766. updatestatus(void) {
  1767. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1768. strcpy(stext, "dwm-"VERSION);
  1769. drawbar(selmon);
  1770. }
  1771. void
  1772. updatewindowtype(Client *c) {
  1773. Atom state = getatomprop(c, netatom[NetWMState]);
  1774. Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
  1775. if(state == netatom[NetWMFullscreen])
  1776. setfullscreen(c, True);
  1777. if(wtype == netatom[NetWMWindowTypeDialog])
  1778. c->isfloating = True;
  1779. }
  1780. void
  1781. updatewmhints(Client *c) {
  1782. XWMHints *wmh;
  1783. if((wmh = XGetWMHints(dpy, c->win))) {
  1784. if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  1785. wmh->flags &= ~XUrgencyHint;
  1786. XSetWMHints(dpy, c->win, wmh);
  1787. }
  1788. else
  1789. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1790. if(wmh->flags & InputHint)
  1791. c->neverfocus = !wmh->input;
  1792. else
  1793. c->neverfocus = False;
  1794. XFree(wmh);
  1795. }
  1796. }
  1797. void
  1798. view(const Arg *arg) {
  1799. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1800. return;
  1801. selmon->seltags ^= 1; /* toggle sel tagset */
  1802. if(arg->ui & TAGMASK)
  1803. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1804. focus(NULL);
  1805. arrange(selmon);
  1806. }
  1807. Client *
  1808. wintoclient(Window w) {
  1809. Client *c;
  1810. Monitor *m;
  1811. for(m = mons; m; m = m->next)
  1812. for(c = m->clients; c; c = c->next)
  1813. if(c->win == w)
  1814. return c;
  1815. return NULL;
  1816. }
  1817. Monitor *
  1818. wintomon(Window w) {
  1819. int x, y;
  1820. Client *c;
  1821. Monitor *m;
  1822. if(w == root && getrootptr(&x, &y))
  1823. return recttomon(x, y, 1, 1);
  1824. for(m = mons; m; m = m->next)
  1825. if(w == m->barwin)
  1826. return m;
  1827. if((c = wintoclient(w)))
  1828. return c->mon;
  1829. return selmon;
  1830. }
  1831. /* There's no way to check accesses to destroyed windows, thus those cases are
  1832. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1833. * default error handler, which may call exit. */
  1834. int
  1835. xerror(Display *dpy, XErrorEvent *ee) {
  1836. if(ee->error_code == BadWindow
  1837. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1838. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1839. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1840. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1841. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1842. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1843. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1844. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1845. return 0;
  1846. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1847. ee->request_code, ee->error_code);
  1848. return xerrorxlib(dpy, ee); /* may call exit */
  1849. }
  1850. int
  1851. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1852. return 0;
  1853. }
  1854. /* Startup Error handler to check if another window manager
  1855. * is already running. */
  1856. int
  1857. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1858. die("dwm: another window manager is already running\n");
  1859. return -1;
  1860. }
  1861. void
  1862. zoom(const Arg *arg) {
  1863. Client *c = selmon->sel;
  1864. if(!selmon->lt[selmon->sellt]->arrange
  1865. || (selmon->sel && selmon->sel->isfloating))
  1866. return;
  1867. if(c == nexttiled(selmon->clients))
  1868. if(!c || !(c = nexttiled(c->next)))
  1869. return;
  1870. pop(c);
  1871. }
  1872. int
  1873. main(int argc, char *argv[]) {
  1874. if(argc == 2 && !strcmp("-v", argv[1]))
  1875. die("dwm-"VERSION", © 2006-2012 dwm engineers, see LICENSE for details\n");
  1876. else if(argc != 1)
  1877. die("usage: dwm [-v]\n");
  1878. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1879. fputs("warning: no locale support\n", stderr);
  1880. if(!(dpy = XOpenDisplay(NULL)))
  1881. die("dwm: cannot open display\n");
  1882. checkotherwm();
  1883. setup();
  1884. scan();
  1885. run();
  1886. cleanup();
  1887. XCloseDisplay(dpy);
  1888. return EXIT_SUCCESS;
  1889. }