dwm.c 61 KB

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