dwm.c 66 KB

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