dwm.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507
  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. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1247. configure(c);
  1248. XSync(dpy, False);
  1249. }
  1250. void
  1251. resizemouse(const Arg *arg)
  1252. {
  1253. int ocx, ocy, nw, nh;
  1254. Client *c;
  1255. Monitor *m;
  1256. XEvent ev;
  1257. Time lasttime = 0;
  1258. if (!(c = selmon->sel))
  1259. return;
  1260. if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
  1261. return;
  1262. restack(selmon);
  1263. ocx = c->x;
  1264. ocy = c->y;
  1265. if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1266. None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
  1267. return;
  1268. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1269. do {
  1270. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1271. switch(ev.type) {
  1272. case ConfigureRequest:
  1273. case Expose:
  1274. case MapRequest:
  1275. handler[ev.type](&ev);
  1276. break;
  1277. case MotionNotify:
  1278. if ((ev.xmotion.time - lasttime) <= (1000 / 60))
  1279. continue;
  1280. lasttime = ev.xmotion.time;
  1281. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1282. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1283. if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
  1284. && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
  1285. {
  1286. if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1287. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1288. togglefloating(NULL);
  1289. }
  1290. if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1291. resize(c, c->x, c->y, nw, nh, 1);
  1292. break;
  1293. }
  1294. } while (ev.type != ButtonRelease);
  1295. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1296. XUngrabPointer(dpy, CurrentTime);
  1297. while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1298. if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1299. sendmon(c, m);
  1300. selmon = m;
  1301. focus(NULL);
  1302. }
  1303. }
  1304. void
  1305. restack(Monitor *m)
  1306. {
  1307. Client *c;
  1308. XEvent ev;
  1309. XWindowChanges wc;
  1310. drawbar(m);
  1311. if (!m->sel)
  1312. return;
  1313. if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
  1314. XRaiseWindow(dpy, m->sel->win);
  1315. if (m->lt[m->sellt]->arrange) {
  1316. wc.stack_mode = Below;
  1317. wc.sibling = m->barwin;
  1318. for (c = m->stack; c; c = c->snext)
  1319. if (!c->isfloating && ISVISIBLE(c)) {
  1320. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1321. wc.sibling = c->win;
  1322. }
  1323. }
  1324. XSync(dpy, False);
  1325. while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1326. }
  1327. void
  1328. run(void)
  1329. {
  1330. XEvent ev;
  1331. /* main event loop */
  1332. XSync(dpy, False);
  1333. while (running && !XNextEvent(dpy, &ev))
  1334. if (handler[ev.type])
  1335. handler[ev.type](&ev); /* call handler */
  1336. }
  1337. void
  1338. runAutostart(void)
  1339. {
  1340. system("cd ~/.dwm; ./autostart_blocking.sh");
  1341. system("cd ~/.dwm; ./autostart.sh &");
  1342. }
  1343. void
  1344. scan(void)
  1345. {
  1346. unsigned int i, num;
  1347. Window d1, d2, *wins = NULL;
  1348. XWindowAttributes wa;
  1349. if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1350. for (i = 0; i < num; i++) {
  1351. if (!XGetWindowAttributes(dpy, wins[i], &wa)
  1352. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1353. continue;
  1354. if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1355. manage(wins[i], &wa);
  1356. }
  1357. for (i = 0; i < num; i++) { /* now the transients */
  1358. if (!XGetWindowAttributes(dpy, wins[i], &wa))
  1359. continue;
  1360. if (XGetTransientForHint(dpy, wins[i], &d1)
  1361. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1362. manage(wins[i], &wa);
  1363. }
  1364. if (wins)
  1365. XFree(wins);
  1366. }
  1367. }
  1368. void
  1369. sendmon(Client *c, Monitor *m)
  1370. {
  1371. if (c->mon == m)
  1372. return;
  1373. unfocus(c, 1);
  1374. detach(c);
  1375. detachstack(c);
  1376. c->mon = m;
  1377. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1378. attach(c);
  1379. attachstack(c);
  1380. focus(NULL);
  1381. arrange(NULL);
  1382. }
  1383. void
  1384. setclientstate(Client *c, long state)
  1385. {
  1386. long data[] = { state, None };
  1387. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1388. PropModeReplace, (unsigned char *)data, 2);
  1389. }
  1390. int
  1391. sendevent(Client *c, Atom proto)
  1392. {
  1393. int n;
  1394. Atom *protocols;
  1395. int exists = 0;
  1396. XEvent ev;
  1397. if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  1398. while (!exists && n--)
  1399. exists = protocols[n] == proto;
  1400. XFree(protocols);
  1401. }
  1402. if (exists) {
  1403. ev.type = ClientMessage;
  1404. ev.xclient.window = c->win;
  1405. ev.xclient.message_type = wmatom[WMProtocols];
  1406. ev.xclient.format = 32;
  1407. ev.xclient.data.l[0] = proto;
  1408. ev.xclient.data.l[1] = CurrentTime;
  1409. XSendEvent(dpy, c->win, False, NoEventMask, &ev);
  1410. }
  1411. return exists;
  1412. }
  1413. void
  1414. setfocus(Client *c)
  1415. {
  1416. if (!c->neverfocus) {
  1417. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  1418. XChangeProperty(dpy, root, netatom[NetActiveWindow],
  1419. XA_WINDOW, 32, PropModeReplace,
  1420. (unsigned char *) &(c->win), 1);
  1421. }
  1422. sendevent(c, wmatom[WMTakeFocus]);
  1423. }
  1424. void
  1425. setfullscreen(Client *c, int fullscreen)
  1426. {
  1427. if (fullscreen && !c->isfullscreen) {
  1428. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1429. PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
  1430. c->isfullscreen = 1;
  1431. c->oldstate = c->isfloating;
  1432. c->oldbw = c->bw;
  1433. c->bw = 0;
  1434. c->isfloating = 1;
  1435. resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
  1436. XRaiseWindow(dpy, c->win);
  1437. } else if (!fullscreen && c->isfullscreen){
  1438. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1439. PropModeReplace, (unsigned char*)0, 0);
  1440. c->isfullscreen = 0;
  1441. c->isfloating = c->oldstate;
  1442. c->bw = c->oldbw;
  1443. c->x = c->oldx;
  1444. c->y = c->oldy;
  1445. c->w = c->oldw;
  1446. c->h = c->oldh;
  1447. resizeclient(c, c->x, c->y, c->w, c->h);
  1448. arrange(c->mon);
  1449. }
  1450. }
  1451. void
  1452. setgaps(int oh, int ov, int ih, int iv)
  1453. {
  1454. if (oh < 0) oh = 0;
  1455. if (ov < 0) ov = 0;
  1456. if (ih < 0) ih = 0;
  1457. if (iv < 0) iv = 0;
  1458. selmon->gappoh = oh;
  1459. selmon->gappov = ov;
  1460. selmon->gappih = ih;
  1461. selmon->gappiv = iv;
  1462. arrange(selmon);
  1463. }
  1464. void
  1465. togglegaps(const Arg *arg)
  1466. {
  1467. enablegaps = !enablegaps;
  1468. arrange(selmon);
  1469. }
  1470. void
  1471. defaultgaps(const Arg *arg)
  1472. {
  1473. setgaps(gappoh, gappov, gappih, gappiv);
  1474. }
  1475. void
  1476. incrgaps(const Arg *arg)
  1477. {
  1478. setgaps(
  1479. selmon->gappoh + arg->i,
  1480. selmon->gappov + arg->i,
  1481. selmon->gappih + arg->i,
  1482. selmon->gappiv + arg->i
  1483. );
  1484. }
  1485. void
  1486. incrigaps(const Arg *arg)
  1487. {
  1488. setgaps(
  1489. selmon->gappoh,
  1490. selmon->gappov,
  1491. selmon->gappih + arg->i,
  1492. selmon->gappiv + arg->i
  1493. );
  1494. }
  1495. void
  1496. incrogaps(const Arg *arg)
  1497. {
  1498. setgaps(
  1499. selmon->gappoh + arg->i,
  1500. selmon->gappov + arg->i,
  1501. selmon->gappih,
  1502. selmon->gappiv
  1503. );
  1504. }
  1505. void
  1506. incrohgaps(const Arg *arg)
  1507. {
  1508. setgaps(
  1509. selmon->gappoh + arg->i,
  1510. selmon->gappov,
  1511. selmon->gappih,
  1512. selmon->gappiv
  1513. );
  1514. }
  1515. void
  1516. incrovgaps(const Arg *arg)
  1517. {
  1518. setgaps(
  1519. selmon->gappoh,
  1520. selmon->gappov + arg->i,
  1521. selmon->gappih,
  1522. selmon->gappiv
  1523. );
  1524. }
  1525. void
  1526. incrihgaps(const Arg *arg)
  1527. {
  1528. setgaps(
  1529. selmon->gappoh,
  1530. selmon->gappov,
  1531. selmon->gappih + arg->i,
  1532. selmon->gappiv
  1533. );
  1534. }
  1535. void
  1536. incrivgaps(const Arg *arg)
  1537. {
  1538. setgaps(
  1539. selmon->gappoh,
  1540. selmon->gappov,
  1541. selmon->gappih,
  1542. selmon->gappiv + arg->i
  1543. );
  1544. }
  1545. void
  1546. setlayout(const Arg *arg)
  1547. {
  1548. if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
  1549. selmon->sellt ^= 1;
  1550. if (arg && arg->v)
  1551. selmon->lt[selmon->sellt] = (Layout *)arg->v;
  1552. strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
  1553. if (selmon->sel)
  1554. arrange(selmon);
  1555. else
  1556. drawbar(selmon);
  1557. }
  1558. /* arg > 1.0 will set mfact absolutely */
  1559. void
  1560. setmfact(const Arg *arg)
  1561. {
  1562. float f;
  1563. if (!arg || !selmon->lt[selmon->sellt]->arrange)
  1564. return;
  1565. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1566. if (f < 0.1 || f > 0.9)
  1567. return;
  1568. selmon->mfact = f;
  1569. arrange(selmon);
  1570. }
  1571. void
  1572. setup(void)
  1573. {
  1574. int i;
  1575. XSetWindowAttributes wa;
  1576. Atom utf8string;
  1577. /* clean up any zombies immediately */
  1578. sigchld(0);
  1579. signal(SIGHUP, sighup);
  1580. signal(SIGTERM, sigterm);
  1581. /* init screen */
  1582. screen = DefaultScreen(dpy);
  1583. sw = DisplayWidth(dpy, screen);
  1584. sh = DisplayHeight(dpy, screen);
  1585. root = RootWindow(dpy, screen);
  1586. xinitvisual();
  1587. drw = drw_create(dpy, screen, root, sw, sh, visual, depth, cmap);
  1588. if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
  1589. die("no fonts could be loaded.");
  1590. lrpad = drw->fonts->h;
  1591. bh = drw->fonts->h + 2;
  1592. updategeom();
  1593. /* init atoms */
  1594. utf8string = XInternAtom(dpy, "UTF8_STRING", False);
  1595. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1596. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1597. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1598. wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
  1599. netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
  1600. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1601. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1602. netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
  1603. netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
  1604. netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
  1605. netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
  1606. netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
  1607. netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
  1608. /* init cursors */
  1609. cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
  1610. cursor[CurResize] = drw_cur_create(drw, XC_sizing);
  1611. cursor[CurMove] = drw_cur_create(drw, XC_fleur);
  1612. /* init appearance */
  1613. scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
  1614. for (i = 0; i < LENGTH(colors); i++)
  1615. scheme[i] = drw_scm_create(drw, colors[i], alphas[i], 3);
  1616. /* init bars */
  1617. updatebars();
  1618. updatestatus();
  1619. /* supporting window for NetWMCheck */
  1620. wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
  1621. XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
  1622. PropModeReplace, (unsigned char *) &wmcheckwin, 1);
  1623. XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
  1624. PropModeReplace, (unsigned char *) "dwm", 3);
  1625. XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
  1626. PropModeReplace, (unsigned char *) &wmcheckwin, 1);
  1627. /* EWMH support per view */
  1628. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1629. PropModeReplace, (unsigned char *) netatom, NetLast);
  1630. XDeleteProperty(dpy, root, netatom[NetClientList]);
  1631. /* select events */
  1632. wa.cursor = cursor[CurNormal]->cursor;
  1633. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
  1634. |ButtonPressMask|PointerMotionMask|EnterWindowMask
  1635. |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
  1636. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1637. XSelectInput(dpy, root, wa.event_mask);
  1638. grabkeys();
  1639. focus(NULL);
  1640. }
  1641. void
  1642. seturgent(Client *c, int urg)
  1643. {
  1644. XWMHints *wmh;
  1645. c->isurgent = urg;
  1646. if (!(wmh = XGetWMHints(dpy, c->win)))
  1647. return;
  1648. wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
  1649. XSetWMHints(dpy, c->win, wmh);
  1650. XFree(wmh);
  1651. }
  1652. void
  1653. showhide(Client *c)
  1654. {
  1655. if (!c)
  1656. return;
  1657. if (ISVISIBLE(c)) {
  1658. /* show clients top down */
  1659. XMoveWindow(dpy, c->win, c->x, c->y);
  1660. if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
  1661. resize(c, c->x, c->y, c->w, c->h, 0);
  1662. showhide(c->snext);
  1663. } else {
  1664. /* hide clients bottom up */
  1665. showhide(c->snext);
  1666. XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
  1667. }
  1668. }
  1669. void
  1670. sigchld(int unused)
  1671. {
  1672. if (signal(SIGCHLD, sigchld) == SIG_ERR)
  1673. die("can't install SIGCHLD handler:");
  1674. while (0 < waitpid(-1, NULL, WNOHANG));
  1675. }
  1676. void
  1677. sighup(int unused)
  1678. {
  1679. Arg a = {.i = 1};
  1680. quit(&a);
  1681. }
  1682. void
  1683. sigterm(int unused)
  1684. {
  1685. Arg a = {.i = 0};
  1686. quit(&a);
  1687. }
  1688. void
  1689. spawn(const Arg *arg)
  1690. {
  1691. if (arg->v == dmenucmd)
  1692. dmenumon[0] = '0' + selmon->num;
  1693. if (fork() == 0) {
  1694. if (dpy)
  1695. close(ConnectionNumber(dpy));
  1696. setsid();
  1697. execvp(((char **)arg->v)[0], (char **)arg->v);
  1698. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1699. perror(" failed");
  1700. exit(EXIT_SUCCESS);
  1701. }
  1702. }
  1703. void
  1704. tag(const Arg *arg)
  1705. {
  1706. if (selmon->sel && arg->ui & TAGMASK) {
  1707. selmon->sel->tags = arg->ui & TAGMASK;
  1708. focus(NULL);
  1709. arrange(selmon);
  1710. }
  1711. }
  1712. void
  1713. tagmon(const Arg *arg)
  1714. {
  1715. if (!selmon->sel || !mons->next)
  1716. return;
  1717. sendmon(selmon->sel, dirtomon(arg->i));
  1718. }
  1719. void
  1720. tile(Monitor *m)
  1721. {
  1722. unsigned int i, n, h, r, oe = enablegaps, ie = enablegaps, mw, my, ty;
  1723. Client *c;
  1724. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  1725. if (n == 0)
  1726. return;
  1727. if (smartgaps == n) {
  1728. oe = 0; // outer gaps disabled
  1729. }
  1730. if (n > m->nmaster)
  1731. mw = m->nmaster ? (m->ww + m->gappiv*ie) * m->mfact : 0;
  1732. else
  1733. mw = m->ww - 2*m->gappov*oe + m->gappiv*ie;
  1734. for (i = 0, my = ty = m->gappoh*oe, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  1735. if (i < m->nmaster) {
  1736. r = MIN(n, m->nmaster) - i;
  1737. h = (m->wh - my - m->gappoh*oe - m->gappih*ie * (r - 1)) / r;
  1738. resize(c, m->wx + m->gappov*oe, m->wy + my, mw - (2*c->bw) - m->gappiv*ie, h - (2*c->bw), 0);
  1739. my += HEIGHT(c) + m->gappih*ie;
  1740. } else {
  1741. r = n - i;
  1742. h = (m->wh - ty - m->gappoh*oe - m->gappih*ie * (r - 1)) / r;
  1743. 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);
  1744. ty += HEIGHT(c) + m->gappih*ie;
  1745. }
  1746. }
  1747. void
  1748. togglebar(const Arg *arg)
  1749. {
  1750. selmon->showbar = !selmon->showbar;
  1751. updatebarpos(selmon);
  1752. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1753. arrange(selmon);
  1754. }
  1755. void
  1756. togglefloating(const Arg *arg)
  1757. {
  1758. if (!selmon->sel)
  1759. return;
  1760. if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
  1761. return;
  1762. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1763. if (selmon->sel->isfloating)
  1764. resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  1765. selmon->sel->w, selmon->sel->h, 0);
  1766. arrange(selmon);
  1767. }
  1768. void
  1769. toggletag(const Arg *arg)
  1770. {
  1771. unsigned int newtags;
  1772. if (!selmon->sel)
  1773. return;
  1774. newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1775. if (newtags) {
  1776. selmon->sel->tags = newtags;
  1777. focus(NULL);
  1778. arrange(selmon);
  1779. }
  1780. }
  1781. void
  1782. toggleview(const Arg *arg)
  1783. {
  1784. unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1785. if (newtagset) {
  1786. selmon->tagset[selmon->seltags] = newtagset;
  1787. focus(NULL);
  1788. arrange(selmon);
  1789. }
  1790. }
  1791. void
  1792. unfocus(Client *c, int setfocus)
  1793. {
  1794. if (!c)
  1795. return;
  1796. grabbuttons(c, 0);
  1797. XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
  1798. if (setfocus) {
  1799. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1800. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  1801. }
  1802. }
  1803. void
  1804. unmanage(Client *c, int destroyed)
  1805. {
  1806. Monitor *m = c->mon;
  1807. XWindowChanges wc;
  1808. detach(c);
  1809. detachstack(c);
  1810. if (!destroyed) {
  1811. wc.border_width = c->oldbw;
  1812. XGrabServer(dpy); /* avoid race conditions */
  1813. XSetErrorHandler(xerrordummy);
  1814. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1815. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1816. setclientstate(c, WithdrawnState);
  1817. XSync(dpy, False);
  1818. XSetErrorHandler(xerror);
  1819. XUngrabServer(dpy);
  1820. }
  1821. free(c);
  1822. focus(NULL);
  1823. updateclientlist();
  1824. arrange(m);
  1825. }
  1826. void
  1827. unmapnotify(XEvent *e)
  1828. {
  1829. Client *c;
  1830. XUnmapEvent *ev = &e->xunmap;
  1831. if ((c = wintoclient(ev->window))) {
  1832. if (ev->send_event)
  1833. setclientstate(c, WithdrawnState);
  1834. else
  1835. unmanage(c, 0);
  1836. }
  1837. }
  1838. void
  1839. updatebars(void)
  1840. {
  1841. Monitor *m;
  1842. XSetWindowAttributes wa = {
  1843. .override_redirect = True,
  1844. .background_pixel = 0,
  1845. .border_pixel = 0,
  1846. .colormap = cmap,
  1847. .event_mask = ButtonPressMask|ExposureMask
  1848. };
  1849. XClassHint ch = {"dwm", "dwm"};
  1850. for (m = mons; m; m = m->next) {
  1851. if (m->barwin)
  1852. continue;
  1853. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, depth,
  1854. InputOutput, visual,
  1855. CWOverrideRedirect|CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &wa);
  1856. XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
  1857. XMapRaised(dpy, m->barwin);
  1858. XSetClassHint(dpy, m->barwin, &ch);
  1859. }
  1860. }
  1861. void
  1862. updatebarpos(Monitor *m)
  1863. {
  1864. m->wy = m->my;
  1865. m->wh = m->mh;
  1866. if (m->showbar) {
  1867. m->wh -= bh;
  1868. m->by = m->topbar ? m->wy : m->wy + m->wh;
  1869. m->wy = m->topbar ? m->wy + bh : m->wy;
  1870. } else
  1871. m->by = -bh;
  1872. }
  1873. void
  1874. updateclientlist()
  1875. {
  1876. Client *c;
  1877. Monitor *m;
  1878. XDeleteProperty(dpy, root, netatom[NetClientList]);
  1879. for (m = mons; m; m = m->next)
  1880. for (c = m->clients; c; c = c->next)
  1881. XChangeProperty(dpy, root, netatom[NetClientList],
  1882. XA_WINDOW, 32, PropModeAppend,
  1883. (unsigned char *) &(c->win), 1);
  1884. }
  1885. int
  1886. updategeom(void)
  1887. {
  1888. int dirty = 0;
  1889. #ifdef XINERAMA
  1890. if (XineramaIsActive(dpy)) {
  1891. int i, j, n, nn;
  1892. Client *c;
  1893. Monitor *m;
  1894. XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
  1895. XineramaScreenInfo *unique = NULL;
  1896. for (n = 0, m = mons; m; m = m->next, n++);
  1897. /* only consider unique geometries as separate screens */
  1898. unique = ecalloc(nn, sizeof(XineramaScreenInfo));
  1899. for (i = 0, j = 0; i < nn; i++)
  1900. if (isuniquegeom(unique, j, &info[i]))
  1901. memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
  1902. XFree(info);
  1903. nn = j;
  1904. if (n <= nn) { /* new monitors available */
  1905. for (i = 0; i < (nn - n); i++) {
  1906. for (m = mons; m && m->next; m = m->next);
  1907. if (m)
  1908. m->next = createmon();
  1909. else
  1910. mons = createmon();
  1911. }
  1912. for (i = 0, m = mons; i < nn && m; m = m->next, i++)
  1913. if (i >= n
  1914. || unique[i].x_org != m->mx || unique[i].y_org != m->my
  1915. || unique[i].width != m->mw || unique[i].height != m->mh)
  1916. {
  1917. dirty = 1;
  1918. m->num = i;
  1919. m->mx = m->wx = unique[i].x_org;
  1920. m->my = m->wy = unique[i].y_org;
  1921. m->mw = m->ww = unique[i].width;
  1922. m->mh = m->wh = unique[i].height;
  1923. updatebarpos(m);
  1924. }
  1925. } else { /* less monitors available nn < n */
  1926. for (i = nn; i < n; i++) {
  1927. for (m = mons; m && m->next; m = m->next);
  1928. while ((c = m->clients)) {
  1929. dirty = 1;
  1930. m->clients = c->next;
  1931. detachstack(c);
  1932. c->mon = mons;
  1933. attach(c);
  1934. attachstack(c);
  1935. }
  1936. if (m == selmon)
  1937. selmon = mons;
  1938. cleanupmon(m);
  1939. }
  1940. }
  1941. free(unique);
  1942. } else
  1943. #endif /* XINERAMA */
  1944. { /* default monitor setup */
  1945. if (!mons)
  1946. mons = createmon();
  1947. if (mons->mw != sw || mons->mh != sh) {
  1948. dirty = 1;
  1949. mons->mw = mons->ww = sw;
  1950. mons->mh = mons->wh = sh;
  1951. updatebarpos(mons);
  1952. }
  1953. }
  1954. if (dirty) {
  1955. selmon = mons;
  1956. selmon = wintomon(root);
  1957. }
  1958. return dirty;
  1959. }
  1960. void
  1961. updatenumlockmask(void)
  1962. {
  1963. unsigned int i, j;
  1964. XModifierKeymap *modmap;
  1965. numlockmask = 0;
  1966. modmap = XGetModifierMapping(dpy);
  1967. for (i = 0; i < 8; i++)
  1968. for (j = 0; j < modmap->max_keypermod; j++)
  1969. if (modmap->modifiermap[i * modmap->max_keypermod + j]
  1970. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1971. numlockmask = (1 << i);
  1972. XFreeModifiermap(modmap);
  1973. }
  1974. void
  1975. updatesizehints(Client *c)
  1976. {
  1977. long msize;
  1978. XSizeHints size;
  1979. if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1980. /* size is uninitialized, ensure that size.flags aren't used */
  1981. size.flags = PSize;
  1982. if (size.flags & PBaseSize) {
  1983. c->basew = size.base_width;
  1984. c->baseh = size.base_height;
  1985. } else if (size.flags & PMinSize) {
  1986. c->basew = size.min_width;
  1987. c->baseh = size.min_height;
  1988. } else
  1989. c->basew = c->baseh = 0;
  1990. if (size.flags & PResizeInc) {
  1991. c->incw = size.width_inc;
  1992. c->inch = size.height_inc;
  1993. } else
  1994. c->incw = c->inch = 0;
  1995. if (size.flags & PMaxSize) {
  1996. c->maxw = size.max_width;
  1997. c->maxh = size.max_height;
  1998. } else
  1999. c->maxw = c->maxh = 0;
  2000. if (size.flags & PMinSize) {
  2001. c->minw = size.min_width;
  2002. c->minh = size.min_height;
  2003. } else if (size.flags & PBaseSize) {
  2004. c->minw = size.base_width;
  2005. c->minh = size.base_height;
  2006. } else
  2007. c->minw = c->minh = 0;
  2008. if (size.flags & PAspect) {
  2009. c->mina = (float)size.min_aspect.y / size.min_aspect.x;
  2010. c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
  2011. } else
  2012. c->maxa = c->mina = 0.0;
  2013. c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
  2014. }
  2015. void
  2016. updatestatus(void)
  2017. {
  2018. if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  2019. strcpy(stext, "dwm-"VERSION);
  2020. drawbar(selmon);
  2021. }
  2022. void
  2023. updatetitle(Client *c)
  2024. {
  2025. if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  2026. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  2027. if (c->name[0] == '\0') /* hack to mark broken clients */
  2028. strcpy(c->name, broken);
  2029. }
  2030. void
  2031. updatewindowtype(Client *c)
  2032. {
  2033. Atom state = getatomprop(c, netatom[NetWMState]);
  2034. Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
  2035. if (state == netatom[NetWMFullscreen])
  2036. setfullscreen(c, 1);
  2037. if (wtype == netatom[NetWMWindowTypeDialog])
  2038. c->isfloating = 1;
  2039. }
  2040. void
  2041. updatewmhints(Client *c)
  2042. {
  2043. XWMHints *wmh;
  2044. if ((wmh = XGetWMHints(dpy, c->win))) {
  2045. if (c == selmon->sel && wmh->flags & XUrgencyHint) {
  2046. wmh->flags &= ~XUrgencyHint;
  2047. XSetWMHints(dpy, c->win, wmh);
  2048. } else
  2049. c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
  2050. if (wmh->flags & InputHint)
  2051. c->neverfocus = !wmh->input;
  2052. else
  2053. c->neverfocus = 0;
  2054. XFree(wmh);
  2055. }
  2056. }
  2057. void
  2058. view(const Arg *arg)
  2059. {
  2060. if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  2061. return;
  2062. selmon->seltags ^= 1; /* toggle sel tagset */
  2063. if (arg->ui & TAGMASK)
  2064. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  2065. focus(NULL);
  2066. arrange(selmon);
  2067. }
  2068. Client *
  2069. wintoclient(Window w)
  2070. {
  2071. Client *c;
  2072. Monitor *m;
  2073. for (m = mons; m; m = m->next)
  2074. for (c = m->clients; c; c = c->next)
  2075. if (c->win == w)
  2076. return c;
  2077. return NULL;
  2078. }
  2079. Monitor *
  2080. wintomon(Window w)
  2081. {
  2082. int x, y;
  2083. Client *c;
  2084. Monitor *m;
  2085. if (w == root && getrootptr(&x, &y))
  2086. return recttomon(x, y, 1, 1);
  2087. for (m = mons; m; m = m->next)
  2088. if (w == m->barwin)
  2089. return m;
  2090. if ((c = wintoclient(w)))
  2091. return c->mon;
  2092. return selmon;
  2093. }
  2094. /* There's no way to check accesses to destroyed windows, thus those cases are
  2095. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  2096. * default error handler, which may call exit. */
  2097. int
  2098. xerror(Display *dpy, XErrorEvent *ee)
  2099. {
  2100. if (ee->error_code == BadWindow
  2101. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  2102. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  2103. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  2104. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  2105. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  2106. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  2107. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  2108. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  2109. return 0;
  2110. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  2111. ee->request_code, ee->error_code);
  2112. return xerrorxlib(dpy, ee); /* may call exit */
  2113. }
  2114. int
  2115. xerrordummy(Display *dpy, XErrorEvent *ee)
  2116. {
  2117. return 0;
  2118. }
  2119. /* Startup Error handler to check if another window manager
  2120. * is already running. */
  2121. int
  2122. xerrorstart(Display *dpy, XErrorEvent *ee)
  2123. {
  2124. die("dwm: another window manager is already running");
  2125. return -1;
  2126. }
  2127. void
  2128. xinitvisual()
  2129. {
  2130. XVisualInfo *infos;
  2131. XRenderPictFormat *fmt;
  2132. int nitems;
  2133. int i;
  2134. XVisualInfo tpl = {
  2135. .screen = screen,
  2136. .depth = 32,
  2137. .class = TrueColor
  2138. };
  2139. long masks = VisualScreenMask | VisualDepthMask | VisualClassMask;
  2140. infos = XGetVisualInfo(dpy, masks, &tpl, &nitems);
  2141. visual = NULL;
  2142. for(i = 0; i < nitems; i ++) {
  2143. fmt = XRenderFindVisualFormat(dpy, infos[i].visual);
  2144. if (fmt->type == PictTypeDirect && fmt->direct.alphaMask) {
  2145. visual = infos[i].visual;
  2146. depth = infos[i].depth;
  2147. cmap = XCreateColormap(dpy, root, visual, AllocNone);
  2148. useargb = 1;
  2149. break;
  2150. }
  2151. }
  2152. XFree(infos);
  2153. if (! visual) {
  2154. visual = DefaultVisual(dpy, screen);
  2155. depth = DefaultDepth(dpy, screen);
  2156. cmap = DefaultColormap(dpy, screen);
  2157. }
  2158. }
  2159. void
  2160. zoom(const Arg *arg)
  2161. {
  2162. Client *c = selmon->sel;
  2163. if (!selmon->lt[selmon->sellt]->arrange
  2164. || (selmon->sel && selmon->sel->isfloating))
  2165. return;
  2166. if (c == nexttiled(selmon->clients))
  2167. if (!c || !(c = nexttiled(c->next)))
  2168. return;
  2169. pop(c);
  2170. }
  2171. int
  2172. main(int argc, char *argv[])
  2173. {
  2174. if (argc == 2 && !strcmp("-v", argv[1]))
  2175. die("dwm-"VERSION);
  2176. else if (argc != 1)
  2177. die("usage: dwm [-v]");
  2178. if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  2179. fputs("warning: no locale support\n", stderr);
  2180. if (!(dpy = XOpenDisplay(NULL)))
  2181. die("dwm: cannot open display");
  2182. checkotherwm();
  2183. setup();
  2184. #ifdef __OpenBSD__
  2185. if (pledge("stdio rpath proc exec", NULL) == -1)
  2186. die("pledge");
  2187. #endif /* __OpenBSD__ */
  2188. scan();
  2189. runAutostart();
  2190. run();
  2191. if (restart) execvp(argv[0], argv);
  2192. cleanup();
  2193. XCloseDisplay(dpy);
  2194. return EXIT_SUCCESS;
  2195. }
  2196. void
  2197. centeredmaster(Monitor *m)
  2198. {
  2199. unsigned int i, n, h, mw, mx, my, oty, ety, tw;
  2200. Client *c;
  2201. /* count number of clients in the selected monitor */
  2202. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  2203. if (n == 0)
  2204. return;
  2205. /* initialize areas */
  2206. mw = m->ww;
  2207. mx = 0;
  2208. my = 0;
  2209. tw = mw;
  2210. if (n > m->nmaster) {
  2211. /* go mfact box in the center if more than nmaster clients */
  2212. mw = m->nmaster ? m->ww * m->mfact : 0;
  2213. tw = m->ww - mw;
  2214. if (n - m->nmaster > 1) {
  2215. /* only one client */
  2216. mx = (m->ww - mw) / 2;
  2217. tw = (m->ww - mw) / 2;
  2218. }
  2219. }
  2220. oty = 0;
  2221. ety = 0;
  2222. for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  2223. if (i < m->nmaster) {
  2224. /* nmaster clients are stacked vertically, in the center
  2225. * of the screen */
  2226. h = (m->wh - my) / (MIN(n, m->nmaster) - i);
  2227. resize(c, m->wx + mx, m->wy + my, mw - (2*c->bw),
  2228. h - (2*c->bw), 0);
  2229. my += HEIGHT(c);
  2230. } else {
  2231. /* stack clients are stacked vertically */
  2232. if ((i - m->nmaster) % 2 ) {
  2233. h = (m->wh - ety) / ( (1 + n - i) / 2);
  2234. resize(c, m->wx, m->wy + ety, tw - (2*c->bw),
  2235. h - (2*c->bw), 0);
  2236. ety += HEIGHT(c);
  2237. } else {
  2238. h = (m->wh - oty) / ((1 + n - i) / 2);
  2239. resize(c, m->wx + mx + mw, m->wy + oty,
  2240. tw - (2*c->bw), h - (2*c->bw), 0);
  2241. oty += HEIGHT(c);
  2242. }
  2243. }
  2244. }
  2245. void
  2246. centeredfloatingmaster(Monitor *m)
  2247. {
  2248. unsigned int i, n, w, mh, mw, mx, mxo, my, myo, tx;
  2249. Client *c;
  2250. /* count number of clients in the selected monitor */
  2251. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  2252. if (n == 0)
  2253. return;
  2254. /* initialize nmaster area */
  2255. if (n > m->nmaster) {
  2256. /* go mfact box in the center if more than nmaster clients */
  2257. if (m->ww > m->wh) {
  2258. mw = m->nmaster ? m->ww * m->mfact : 0;
  2259. mh = m->nmaster ? m->wh * 0.9 : 0;
  2260. } else {
  2261. mh = m->nmaster ? m->wh * m->mfact : 0;
  2262. mw = m->nmaster ? m->ww * 0.9 : 0;
  2263. }
  2264. mx = mxo = (m->ww - mw) / 2;
  2265. my = myo = (m->wh - mh) / 2;
  2266. } else {
  2267. /* go fullscreen if all clients are in the master area */
  2268. mh = m->wh;
  2269. mw = m->ww;
  2270. mx = mxo = 0;
  2271. my = myo = 0;
  2272. }
  2273. for(i = tx = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  2274. if (i < m->nmaster) {
  2275. /* nmaster clients are stacked horizontally, in the center
  2276. * of the screen */
  2277. w = (mw + mxo - mx) / (MIN(n, m->nmaster) - i);
  2278. resize(c, m->wx + mx, m->wy + my, w - (2*c->bw),
  2279. mh - (2*c->bw), 0);
  2280. mx += WIDTH(c);
  2281. } else {
  2282. /* stack clients are stacked horizontally */
  2283. w = (m->ww - tx) / (n - i);
  2284. resize(c, m->wx + tx, m->wy, w - (2*c->bw),
  2285. m->wh - (2*c->bw), 0);
  2286. tx += WIDTH(c);
  2287. }
  2288. }