dwm.c 51 KB

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