dwm.c 65 KB

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