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.

2193 lines
55 KiB

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