dwm.c 59 KB

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