Configuration of dwm for Mac Computers
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.

2163 lines
54 KiB

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