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.

2088 lines
51 KiB

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