dwm.c 61 KB

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