dwm.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828
  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) { /* status is only drawn on selected monitor */
  812. drw_setscheme(drw, scheme[SchemeNorm]);
  813. sw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
  814. while (1) {
  815. if ((unsigned int)*ts > LENGTH(colors)) { ts++; continue ; }
  816. ctmp = *ts;
  817. *ts = '\0';
  818. drw_text(drw, m->ww - sw + tx, 0, sw - tx, bh, 0, tp, 0);
  819. tx += TEXTW(tp) -lrpad;
  820. if (ctmp == '\0') { break; }
  821. drw_setscheme(drw, scheme[(unsigned int)(ctmp-1)]);
  822. *ts = ctmp;
  823. tp = ++ts;
  824. }
  825. }
  826. for (c = m->clients; c; c = c->next) {
  827. occ |= c->tags == 255 ? 0 : c->tags;
  828. if (c->isurgent)
  829. urg |= c->tags;
  830. }
  831. x = 0;
  832. for (i = 0; i < LENGTH(tags); i++) {
  833. /* do not draw vacant tags */
  834. if (!(occ & 1 << i || m->tagset[m->seltags] & 1 << i))
  835. continue;
  836. w = TEXTW(tags[i]);
  837. drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
  838. drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
  839. x += w;
  840. }
  841. w = blw = TEXTW(m->ltsymbol);
  842. drw_setscheme(drw, scheme[SchemeNorm]);
  843. x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
  844. if ((w = m->ww - sw - x) > bh) {
  845. if (m->sel) {
  846. drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
  847. drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0);
  848. if (m->sel->isfloating)
  849. drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
  850. } else {
  851. drw_setscheme(drw, scheme[SchemeNorm]);
  852. drw_rect(drw, x, 0, w, bh, 1, 1);
  853. }
  854. }
  855. drw_map(drw, m->barwin, 0, 0, m->ww, bh);
  856. }
  857. void
  858. drawbars(void)
  859. {
  860. Monitor *m;
  861. for (m = mons; m; m = m->next)
  862. drawbar(m);
  863. }
  864. void
  865. enternotify(XEvent *e)
  866. {
  867. Client *c;
  868. Monitor *m;
  869. XCrossingEvent *ev = &e->xcrossing;
  870. if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  871. return;
  872. c = wintoclient(ev->window);
  873. m = c ? c->mon : wintomon(ev->window);
  874. if (m != selmon) {
  875. unfocus(selmon->sel, 1);
  876. selmon = m;
  877. } else if (!c || c == selmon->sel)
  878. return;
  879. focus(c);
  880. }
  881. void
  882. expose(XEvent *e)
  883. {
  884. Monitor *m;
  885. XExposeEvent *ev = &e->xexpose;
  886. if (ev->count == 0 && (m = wintomon(ev->window)))
  887. drawbar(m);
  888. }
  889. void
  890. focus(Client *c)
  891. {
  892. if (!c || !ISVISIBLE(c))
  893. for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
  894. if (selmon->sel && selmon->sel != c)
  895. unfocus(selmon->sel, 0);
  896. if (c) {
  897. if (c->mon != selmon)
  898. selmon = c->mon;
  899. if (c->isurgent)
  900. seturgent(c, 0);
  901. detachstack(c);
  902. attachstack(c);
  903. grabbuttons(c, 1);
  904. XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
  905. setfocus(c);
  906. } else {
  907. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  908. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  909. }
  910. selmon->sel = c;
  911. drawbars();
  912. }
  913. /* there are some broken focus acquiring clients needing extra handling */
  914. void
  915. focusin(XEvent *e)
  916. {
  917. XFocusChangeEvent *ev = &e->xfocus;
  918. if (selmon->sel && ev->window != selmon->sel->win)
  919. setfocus(selmon->sel);
  920. }
  921. void
  922. focusmon(const Arg *arg)
  923. {
  924. Monitor *m;
  925. if (!mons->next)
  926. return;
  927. if ((m = dirtomon(arg->i)) == selmon)
  928. return;
  929. unfocus(selmon->sel, 0);
  930. selmon = m;
  931. focus(NULL);
  932. }
  933. void
  934. focusstack(const Arg *arg)
  935. {
  936. Client *c = NULL, *i;
  937. if (!selmon->sel)
  938. return;
  939. if (arg->i > 0) {
  940. for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
  941. if (!c)
  942. for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
  943. } else {
  944. for (i = selmon->clients; i != selmon->sel; i = i->next)
  945. if (ISVISIBLE(i))
  946. c = i;
  947. if (!c)
  948. for (; i; i = i->next)
  949. if (ISVISIBLE(i))
  950. c = i;
  951. }
  952. if (c) {
  953. focus(c);
  954. restack(selmon);
  955. }
  956. }
  957. Atom
  958. getatomprop(Client *c, Atom prop)
  959. {
  960. int di;
  961. unsigned long dl;
  962. unsigned char *p = NULL;
  963. Atom da, atom = None;
  964. if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
  965. &da, &di, &dl, &dl, &p) == Success && p) {
  966. atom = *(Atom *)p;
  967. XFree(p);
  968. }
  969. return atom;
  970. }
  971. int
  972. getrootptr(int *x, int *y)
  973. {
  974. int di;
  975. unsigned int dui;
  976. Window dummy;
  977. return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
  978. }
  979. long
  980. getstate(Window w)
  981. {
  982. int format;
  983. long result = -1;
  984. unsigned char *p = NULL;
  985. unsigned long n, extra;
  986. Atom real;
  987. if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  988. &real, &format, &n, &extra, (unsigned char **)&p) != Success)
  989. return -1;
  990. if (n != 0)
  991. result = *p;
  992. XFree(p);
  993. return result;
  994. }
  995. int
  996. gettextprop(Window w, Atom atom, char *text, unsigned int size)
  997. {
  998. char **list = NULL;
  999. int n;
  1000. XTextProperty name;
  1001. if (!text || size == 0)
  1002. return 0;
  1003. text[0] = '\0';
  1004. if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
  1005. return 0;
  1006. if (name.encoding == XA_STRING)
  1007. strncpy(text, (char *)name.value, size - 1);
  1008. else {
  1009. if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
  1010. strncpy(text, *list, size - 1);
  1011. XFreeStringList(list);
  1012. }
  1013. }
  1014. text[size - 1] = '\0';
  1015. XFree(name.value);
  1016. return 1;
  1017. }
  1018. void
  1019. grabbuttons(Client *c, int focused)
  1020. {
  1021. updatenumlockmask();
  1022. {
  1023. unsigned int i, j;
  1024. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  1025. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1026. if (!focused)
  1027. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  1028. BUTTONMASK, GrabModeSync, GrabModeSync, None, None);
  1029. for (i = 0; i < LENGTH(buttons); i++)
  1030. if (buttons[i].click == ClkClientWin)
  1031. for (j = 0; j < LENGTH(modifiers); j++)
  1032. XGrabButton(dpy, buttons[i].button,
  1033. buttons[i].mask | modifiers[j],
  1034. c->win, False, BUTTONMASK,
  1035. GrabModeAsync, GrabModeSync, None, None);
  1036. }
  1037. }
  1038. void
  1039. grabkeys(void)
  1040. {
  1041. updatenumlockmask();
  1042. {
  1043. unsigned int i, j;
  1044. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  1045. KeyCode code;
  1046. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  1047. for (i = 0; i < LENGTH(keys); i++)
  1048. if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  1049. for (j = 0; j < LENGTH(modifiers); j++)
  1050. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  1051. True, GrabModeAsync, GrabModeAsync);
  1052. }
  1053. }
  1054. void
  1055. incnmaster(const Arg *arg)
  1056. {
  1057. selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
  1058. arrange(selmon);
  1059. }
  1060. #ifdef XINERAMA
  1061. static int
  1062. isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
  1063. {
  1064. while (n--)
  1065. if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
  1066. && unique[n].width == info->width && unique[n].height == info->height)
  1067. return 0;
  1068. return 1;
  1069. }
  1070. #endif /* XINERAMA */
  1071. void
  1072. keypress(XEvent *e)
  1073. {
  1074. unsigned int i;
  1075. KeySym keysym;
  1076. XKeyEvent *ev;
  1077. ev = &e->xkey;
  1078. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  1079. for (i = 0; i < LENGTH(keys); i++)
  1080. if (keysym == keys[i].keysym
  1081. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  1082. && keys[i].func)
  1083. keys[i].func(&(keys[i].arg));
  1084. }
  1085. void
  1086. killclient(const Arg *arg)
  1087. {
  1088. if (!selmon->sel)
  1089. return;
  1090. if (!sendevent(selmon->sel, wmatom[WMDelete])) {
  1091. XGrabServer(dpy);
  1092. XSetErrorHandler(xerrordummy);
  1093. XSetCloseDownMode(dpy, DestroyAll);
  1094. XKillClient(dpy, selmon->sel->win);
  1095. XSync(dpy, False);
  1096. XSetErrorHandler(xerror);
  1097. XUngrabServer(dpy);
  1098. }
  1099. }
  1100. void
  1101. loadxrdb()
  1102. {
  1103. Display *display;
  1104. char * resm;
  1105. XrmDatabase xrdb;
  1106. char *type;
  1107. XrmValue value;
  1108. display = XOpenDisplay(NULL);
  1109. if (display != NULL) {
  1110. resm = XResourceManagerString(display);
  1111. if (resm != NULL) {
  1112. xrdb = XrmGetStringDatabase(resm);
  1113. if (xrdb != NULL) {
  1114. XRDB_LOAD_COLOR("dwm.background", normbordercolor);
  1115. XRDB_LOAD_COLOR("dwm.background", normbgcolor);
  1116. XRDB_LOAD_COLOR("dwm.foreground", normfgcolor);
  1117. XRDB_LOAD_COLOR("dwm.color1", selbordercolor);
  1118. XRDB_LOAD_COLOR("dwm.color1", selbgcolor);
  1119. XRDB_LOAD_COLOR("dwm.foreground", selfgcolor);
  1120. }
  1121. }
  1122. }
  1123. XCloseDisplay(display);
  1124. }
  1125. void
  1126. manage(Window w, XWindowAttributes *wa)
  1127. {
  1128. Client *c, *t = NULL, *term = NULL;
  1129. Window trans = None;
  1130. XWindowChanges wc;
  1131. c = ecalloc(1, sizeof(Client));
  1132. c->win = w;
  1133. c->pid = winpid(w);
  1134. /* geometry */
  1135. c->x = c->oldx = wa->x;
  1136. c->y = c->oldy = wa->y;
  1137. c->w = c->oldw = wa->width;
  1138. c->h = c->oldh = wa->height;
  1139. c->oldbw = wa->border_width;
  1140. updatetitle(c);
  1141. if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
  1142. c->mon = t->mon;
  1143. c->tags = t->tags;
  1144. } else {
  1145. c->mon = selmon;
  1146. applyrules(c);
  1147. term = termforwin(c);
  1148. }
  1149. if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  1150. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  1151. if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  1152. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  1153. c->x = MAX(c->x, c->mon->mx);
  1154. /* only fix client y-offset, if the client center might cover the bar */
  1155. c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
  1156. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  1157. c->bw = borderpx;
  1158. selmon->tagset[selmon->seltags] &= ~scratchtag;
  1159. if (!strcmp(c->name, scratchpadname)) {
  1160. c->mon->tagset[c->mon->seltags] |= c->tags = scratchtag;
  1161. c->isfloating = True;
  1162. c->x = c->mon->wx + (c->mon->ww / 2 - WIDTH(c) / 2);
  1163. c->y = c->mon->wy + (c->mon->wh / 2 - HEIGHT(c) / 2);
  1164. }
  1165. wc.border_width = c->bw;
  1166. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  1167. XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
  1168. configure(c); /* propagates border_width, if size doesn't change */
  1169. updatewindowtype(c);
  1170. updatesizehints(c);
  1171. updatewmhints(c);
  1172. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  1173. grabbuttons(c, 0);
  1174. if (!c->isfloating)
  1175. c->isfloating = c->oldstate = trans != None || c->isfixed;
  1176. if (c->isfloating)
  1177. XRaiseWindow(dpy, c->win);
  1178. attach(c);
  1179. attachstack(c);
  1180. XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
  1181. (unsigned char *) &(c->win), 1);
  1182. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  1183. setclientstate(c, NormalState);
  1184. if (c->mon == selmon)
  1185. unfocus(selmon->sel, 0);
  1186. c->mon->sel = c;
  1187. arrange(c->mon);
  1188. XMapWindow(dpy, c->win);
  1189. if (term)
  1190. swallow(term, c);
  1191. focus(NULL);
  1192. }
  1193. void
  1194. mappingnotify(XEvent *e)
  1195. {
  1196. XMappingEvent *ev = &e->xmapping;
  1197. XRefreshKeyboardMapping(ev);
  1198. if (ev->request == MappingKeyboard)
  1199. grabkeys();
  1200. }
  1201. void
  1202. maprequest(XEvent *e)
  1203. {
  1204. static XWindowAttributes wa;
  1205. XMapRequestEvent *ev = &e->xmaprequest;
  1206. if (!XGetWindowAttributes(dpy, ev->window, &wa))
  1207. return;
  1208. if (wa.override_redirect)
  1209. return;
  1210. if (!wintoclient(ev->window))
  1211. manage(ev->window, &wa);
  1212. }
  1213. void
  1214. monocle(Monitor *m)
  1215. {
  1216. unsigned int n = 0;
  1217. Client *c;
  1218. for (c = m->clients; c; c = c->next)
  1219. if (ISVISIBLE(c))
  1220. n++;
  1221. if (n > 0) /* override layout symbol */
  1222. snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
  1223. for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1224. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
  1225. }
  1226. void
  1227. motionnotify(XEvent *e)
  1228. {
  1229. static Monitor *mon = NULL;
  1230. Monitor *m;
  1231. XMotionEvent *ev = &e->xmotion;
  1232. if (ev->window != root)
  1233. return;
  1234. if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
  1235. unfocus(selmon->sel, 1);
  1236. selmon = m;
  1237. focus(NULL);
  1238. }
  1239. mon = m;
  1240. }
  1241. void
  1242. movemouse(const Arg *arg)
  1243. {
  1244. int x, y, ocx, ocy, nx, ny;
  1245. Client *c;
  1246. Monitor *m;
  1247. XEvent ev;
  1248. Time lasttime = 0;
  1249. if (!(c = selmon->sel))
  1250. return;
  1251. if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
  1252. return;
  1253. restack(selmon);
  1254. ocx = c->x;
  1255. ocy = c->y;
  1256. if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1257. None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
  1258. return;
  1259. if (!getrootptr(&x, &y))
  1260. return;
  1261. do {
  1262. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1263. switch(ev.type) {
  1264. case ConfigureRequest:
  1265. case Expose:
  1266. case MapRequest:
  1267. handler[ev.type](&ev);
  1268. break;
  1269. case MotionNotify:
  1270. if ((ev.xmotion.time - lasttime) <= (1000 / 60))
  1271. continue;
  1272. lasttime = ev.xmotion.time;
  1273. nx = ocx + (ev.xmotion.x - x);
  1274. ny = ocy + (ev.xmotion.y - y);
  1275. if (abs(selmon->wx - nx) < snap)
  1276. nx = selmon->wx;
  1277. else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1278. nx = selmon->wx + selmon->ww - WIDTH(c);
  1279. if (abs(selmon->wy - ny) < snap)
  1280. ny = selmon->wy;
  1281. else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1282. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1283. if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1284. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1285. togglefloating(NULL);
  1286. if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1287. resize(c, nx, ny, c->w, c->h, 1);
  1288. break;
  1289. }
  1290. } while (ev.type != ButtonRelease);
  1291. XUngrabPointer(dpy, CurrentTime);
  1292. if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1293. sendmon(c, m);
  1294. selmon = m;
  1295. focus(NULL);
  1296. }
  1297. }
  1298. Client *
  1299. nexttiled(Client *c)
  1300. {
  1301. for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1302. return c;
  1303. }
  1304. void
  1305. pop(Client *c)
  1306. {
  1307. detach(c);
  1308. attach(c);
  1309. focus(c);
  1310. arrange(c->mon);
  1311. }
  1312. void
  1313. propertynotify(XEvent *e)
  1314. {
  1315. Client *c;
  1316. Window trans;
  1317. XPropertyEvent *ev = &e->xproperty;
  1318. if ((ev->window == root) && (ev->atom == XA_WM_NAME))
  1319. updatestatus();
  1320. else if (ev->state == PropertyDelete)
  1321. return; /* ignore */
  1322. else if ((c = wintoclient(ev->window))) {
  1323. switch(ev->atom) {
  1324. default: break;
  1325. case XA_WM_TRANSIENT_FOR:
  1326. if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
  1327. (c->isfloating = (wintoclient(trans)) != NULL))
  1328. arrange(c->mon);
  1329. break;
  1330. case XA_WM_NORMAL_HINTS:
  1331. updatesizehints(c);
  1332. break;
  1333. case XA_WM_HINTS:
  1334. updatewmhints(c);
  1335. drawbars();
  1336. break;
  1337. }
  1338. if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1339. updatetitle(c);
  1340. if (c == c->mon->sel)
  1341. drawbar(c->mon);
  1342. }
  1343. if (ev->atom == netatom[NetWMWindowType])
  1344. updatewindowtype(c);
  1345. }
  1346. }
  1347. void
  1348. quit(const Arg *arg)
  1349. {
  1350. if(arg->i) restart = 1;
  1351. running = 0;
  1352. }
  1353. Monitor *
  1354. recttomon(int x, int y, int w, int h)
  1355. {
  1356. Monitor *m, *r = selmon;
  1357. int a, area = 0;
  1358. for (m = mons; m; m = m->next)
  1359. if ((a = INTERSECT(x, y, w, h, m)) > area) {
  1360. area = a;
  1361. r = m;
  1362. }
  1363. return r;
  1364. }
  1365. void
  1366. resize(Client *c, int x, int y, int w, int h, int interact)
  1367. {
  1368. if (applysizehints(c, &x, &y, &w, &h, interact))
  1369. resizeclient(c, x, y, w, h);
  1370. }
  1371. void
  1372. resizeclient(Client *c, int x, int y, int w, int h)
  1373. {
  1374. XWindowChanges wc;
  1375. c->oldx = c->x; c->x = wc.x = x;
  1376. c->oldy = c->y; c->y = wc.y = y;
  1377. c->oldw = c->w; c->w = wc.width = w;
  1378. c->oldh = c->h; c->h = wc.height = h;
  1379. wc.border_width = c->bw;
  1380. if (((nexttiled(c->mon->clients) == c && !nexttiled(c->next))
  1381. || &monocle == c->mon->lt[c->mon->sellt]->arrange)
  1382. && !c->isfullscreen && !c->isfloating) {
  1383. c->w = wc.width += c->bw * 2;
  1384. c->h = wc.height += c->bw * 2;
  1385. wc.border_width = 0;
  1386. }
  1387. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1388. configure(c);
  1389. XSync(dpy, False);
  1390. }
  1391. void
  1392. resizemouse(const Arg *arg)
  1393. {
  1394. int ocx, ocy, nw, nh;
  1395. Client *c;
  1396. Monitor *m;
  1397. XEvent ev;
  1398. Time lasttime = 0;
  1399. if (!(c = selmon->sel))
  1400. return;
  1401. if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
  1402. return;
  1403. restack(selmon);
  1404. ocx = c->x;
  1405. ocy = c->y;
  1406. if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1407. None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
  1408. return;
  1409. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1410. do {
  1411. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1412. switch(ev.type) {
  1413. case ConfigureRequest:
  1414. case Expose:
  1415. case MapRequest:
  1416. handler[ev.type](&ev);
  1417. break;
  1418. case MotionNotify:
  1419. if ((ev.xmotion.time - lasttime) <= (1000 / 60))
  1420. continue;
  1421. lasttime = ev.xmotion.time;
  1422. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1423. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1424. if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
  1425. && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
  1426. {
  1427. if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1428. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1429. togglefloating(NULL);
  1430. }
  1431. if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1432. resize(c, c->x, c->y, nw, nh, 1);
  1433. break;
  1434. }
  1435. } while (ev.type != ButtonRelease);
  1436. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1437. XUngrabPointer(dpy, CurrentTime);
  1438. while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1439. if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1440. sendmon(c, m);
  1441. selmon = m;
  1442. focus(NULL);
  1443. }
  1444. }
  1445. void
  1446. restack(Monitor *m)
  1447. {
  1448. Client *c;
  1449. XEvent ev;
  1450. XWindowChanges wc;
  1451. drawbar(m);
  1452. if (!m->sel)
  1453. return;
  1454. if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
  1455. XRaiseWindow(dpy, m->sel->win);
  1456. if (m->lt[m->sellt]->arrange) {
  1457. wc.stack_mode = Below;
  1458. wc.sibling = m->barwin;
  1459. for (c = m->stack; c; c = c->snext)
  1460. if (!c->isfloating && ISVISIBLE(c)) {
  1461. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1462. wc.sibling = c->win;
  1463. }
  1464. }
  1465. XSync(dpy, False);
  1466. while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1467. }
  1468. void
  1469. run(void)
  1470. {
  1471. XEvent ev;
  1472. /* main event loop */
  1473. XSync(dpy, False);
  1474. while (running && !XNextEvent(dpy, &ev))
  1475. if (handler[ev.type])
  1476. handler[ev.type](&ev); /* call handler */
  1477. }
  1478. void
  1479. scan(void)
  1480. {
  1481. scanner = 1;
  1482. unsigned int i, num;
  1483. char swin[256];
  1484. Window d1, d2, *wins = NULL;
  1485. XWindowAttributes wa;
  1486. if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1487. for (i = 0; i < num; i++) {
  1488. if (!XGetWindowAttributes(dpy, wins[i], &wa)
  1489. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1490. continue;
  1491. if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1492. manage(wins[i], &wa);
  1493. else if (gettextprop(wins[i], netatom[NetClientList], swin, sizeof swin))
  1494. manage(wins[i], &wa);
  1495. }
  1496. for (i = 0; i < num; i++) { /* now the transients */
  1497. if (!XGetWindowAttributes(dpy, wins[i], &wa))
  1498. continue;
  1499. if (XGetTransientForHint(dpy, wins[i], &d1)
  1500. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1501. manage(wins[i], &wa);
  1502. }
  1503. if (wins)
  1504. XFree(wins);
  1505. }
  1506. scanner = 0;
  1507. }
  1508. void
  1509. sendmon(Client *c, Monitor *m)
  1510. {
  1511. if (c->mon == m)
  1512. return;
  1513. unfocus(c, 1);
  1514. detach(c);
  1515. detachstack(c);
  1516. c->mon = m;
  1517. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1518. attach(c);
  1519. attachstack(c);
  1520. focus(NULL);
  1521. arrange(NULL);
  1522. }
  1523. void
  1524. setclientstate(Client *c, long state)
  1525. {
  1526. long data[] = { state, None };
  1527. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1528. PropModeReplace, (unsigned char *)data, 2);
  1529. }
  1530. int
  1531. sendevent(Client *c, Atom proto)
  1532. {
  1533. int n;
  1534. Atom *protocols;
  1535. int exists = 0;
  1536. XEvent ev;
  1537. if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  1538. while (!exists && n--)
  1539. exists = protocols[n] == proto;
  1540. XFree(protocols);
  1541. }
  1542. if (exists) {
  1543. ev.type = ClientMessage;
  1544. ev.xclient.window = c->win;
  1545. ev.xclient.message_type = wmatom[WMProtocols];
  1546. ev.xclient.format = 32;
  1547. ev.xclient.data.l[0] = proto;
  1548. ev.xclient.data.l[1] = CurrentTime;
  1549. XSendEvent(dpy, c->win, False, NoEventMask, &ev);
  1550. }
  1551. return exists;
  1552. }
  1553. void
  1554. setfocus(Client *c)
  1555. {
  1556. if (!c->neverfocus) {
  1557. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  1558. XChangeProperty(dpy, root, netatom[NetActiveWindow],
  1559. XA_WINDOW, 32, PropModeReplace,
  1560. (unsigned char *) &(c->win), 1);
  1561. }
  1562. sendevent(c, wmatom[WMTakeFocus]);
  1563. }
  1564. void
  1565. setfullscreen(Client *c, int fullscreen)
  1566. {
  1567. if (fullscreen && !c->isfullscreen) {
  1568. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1569. PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
  1570. c->isfullscreen = 1;
  1571. c->oldstate = c->isfloating;
  1572. c->oldbw = c->bw;
  1573. c->bw = 0;
  1574. c->isfloating = 1;
  1575. resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
  1576. XRaiseWindow(dpy, c->win);
  1577. } else if (!fullscreen && c->isfullscreen){
  1578. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1579. PropModeReplace, (unsigned char*)0, 0);
  1580. c->isfullscreen = 0;
  1581. c->isfloating = c->oldstate;
  1582. c->bw = c->oldbw;
  1583. c->x = c->oldx;
  1584. c->y = c->oldy;
  1585. c->w = c->oldw;
  1586. c->h = c->oldh;
  1587. resizeclient(c, c->x, c->y, c->w, c->h);
  1588. arrange(c->mon);
  1589. }
  1590. }
  1591. void
  1592. setgaps(int oh, int ov, int ih, int iv)
  1593. {
  1594. if (oh < 0) oh = 0;
  1595. if (ov < 0) ov = 0;
  1596. if (ih < 0) ih = 0;
  1597. if (iv < 0) iv = 0;
  1598. selmon->gappoh = oh;
  1599. selmon->gappov = ov;
  1600. selmon->gappih = ih;
  1601. selmon->gappiv = iv;
  1602. arrange(selmon);
  1603. }
  1604. void
  1605. togglegaps(const Arg *arg)
  1606. {
  1607. enablegaps = !enablegaps;
  1608. arrange(selmon);
  1609. }
  1610. void
  1611. defaultgaps(const Arg *arg)
  1612. {
  1613. setgaps(gappoh, gappov, gappih, gappiv);
  1614. }
  1615. void
  1616. incrgaps(const Arg *arg)
  1617. {
  1618. setgaps(
  1619. selmon->gappoh + arg->i,
  1620. selmon->gappov + arg->i,
  1621. selmon->gappih + arg->i,
  1622. selmon->gappiv + arg->i
  1623. );
  1624. }
  1625. void
  1626. incrigaps(const Arg *arg)
  1627. {
  1628. setgaps(
  1629. selmon->gappoh,
  1630. selmon->gappov,
  1631. selmon->gappih + arg->i,
  1632. selmon->gappiv + arg->i
  1633. );
  1634. }
  1635. void
  1636. incrogaps(const Arg *arg)
  1637. {
  1638. setgaps(
  1639. selmon->gappoh + arg->i,
  1640. selmon->gappov + arg->i,
  1641. selmon->gappih,
  1642. selmon->gappiv
  1643. );
  1644. }
  1645. void
  1646. incrohgaps(const Arg *arg)
  1647. {
  1648. setgaps(
  1649. selmon->gappoh + arg->i,
  1650. selmon->gappov,
  1651. selmon->gappih,
  1652. selmon->gappiv
  1653. );
  1654. }
  1655. void
  1656. incrovgaps(const Arg *arg)
  1657. {
  1658. setgaps(
  1659. selmon->gappoh,
  1660. selmon->gappov + arg->i,
  1661. selmon->gappih,
  1662. selmon->gappiv
  1663. );
  1664. }
  1665. void
  1666. incrihgaps(const Arg *arg)
  1667. {
  1668. setgaps(
  1669. selmon->gappoh,
  1670. selmon->gappov,
  1671. selmon->gappih + arg->i,
  1672. selmon->gappiv
  1673. );
  1674. }
  1675. void
  1676. incrivgaps(const Arg *arg)
  1677. {
  1678. setgaps(
  1679. selmon->gappoh,
  1680. selmon->gappov,
  1681. selmon->gappih,
  1682. selmon->gappiv + arg->i
  1683. );
  1684. }
  1685. void
  1686. setlayout(const Arg *arg)
  1687. {
  1688. if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
  1689. selmon->sellt ^= 1;
  1690. if (arg && arg->v)
  1691. selmon->lt[selmon->sellt] = (Layout *)arg->v;
  1692. strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
  1693. if (selmon->sel)
  1694. arrange(selmon);
  1695. else
  1696. drawbar(selmon);
  1697. }
  1698. /* arg > 1.0 will set mfact absolutely */
  1699. void
  1700. setmfact(const Arg *arg)
  1701. {
  1702. float f;
  1703. if (!arg || !selmon->lt[selmon->sellt]->arrange)
  1704. return;
  1705. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1706. if (f < 0.1 || f > 0.9)
  1707. return;
  1708. selmon->mfact = f;
  1709. arrange(selmon);
  1710. }
  1711. void
  1712. setup(void)
  1713. {
  1714. int i;
  1715. XSetWindowAttributes wa;
  1716. Atom utf8string;
  1717. /* clean up any zombies immediately */
  1718. sigchld(0);
  1719. signal(SIGHUP, sighup);
  1720. signal(SIGTERM, sigterm);
  1721. /* init screen */
  1722. screen = DefaultScreen(dpy);
  1723. sw = DisplayWidth(dpy, screen);
  1724. sh = DisplayHeight(dpy, screen);
  1725. root = RootWindow(dpy, screen);
  1726. xinitvisual();
  1727. drw = drw_create(dpy, screen, root, sw, sh, visual, depth, cmap);
  1728. if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
  1729. die("no fonts could be loaded.");
  1730. lrpad = drw->fonts->h;
  1731. bh = drw->fonts->h + 2;
  1732. updategeom();
  1733. /* init atoms */
  1734. utf8string = XInternAtom(dpy, "UTF8_STRING", False);
  1735. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1736. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1737. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1738. wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
  1739. netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
  1740. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1741. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1742. netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
  1743. netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
  1744. netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
  1745. netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
  1746. netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
  1747. netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
  1748. /* init cursors */
  1749. cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
  1750. cursor[CurResize] = drw_cur_create(drw, XC_sizing);
  1751. cursor[CurMove] = drw_cur_create(drw, XC_fleur);
  1752. /* init appearance */
  1753. scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
  1754. for (i = 0; i < LENGTH(colors); i++)
  1755. scheme[i] = drw_scm_create(drw, colors[i], alphas[i], 3);
  1756. xrdb(NULL);
  1757. /* init bars */
  1758. updatebars();
  1759. updatestatus();
  1760. /* supporting window for NetWMCheck */
  1761. wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
  1762. XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
  1763. PropModeReplace, (unsigned char *) &wmcheckwin, 1);
  1764. XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
  1765. PropModeReplace, (unsigned char *) "dwm", 3);
  1766. XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
  1767. PropModeReplace, (unsigned char *) &wmcheckwin, 1);
  1768. /* EWMH support per view */
  1769. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1770. PropModeReplace, (unsigned char *) netatom, NetLast);
  1771. XDeleteProperty(dpy, root, netatom[NetClientList]);
  1772. /* select events */
  1773. wa.cursor = cursor[CurNormal]->cursor;
  1774. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
  1775. |ButtonPressMask|PointerMotionMask|EnterWindowMask
  1776. |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
  1777. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1778. XSelectInput(dpy, root, wa.event_mask);
  1779. grabkeys();
  1780. focus(NULL);
  1781. }
  1782. void
  1783. seturgent(Client *c, int urg)
  1784. {
  1785. XWMHints *wmh;
  1786. c->isurgent = urg;
  1787. if (!(wmh = XGetWMHints(dpy, c->win)))
  1788. return;
  1789. wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
  1790. XSetWMHints(dpy, c->win, wmh);
  1791. XFree(wmh);
  1792. }
  1793. void
  1794. showhide(Client *c)
  1795. {
  1796. if (!c)
  1797. return;
  1798. if (ISVISIBLE(c)) {
  1799. /* show clients top down */
  1800. XMoveWindow(dpy, c->win, c->x, c->y);
  1801. if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
  1802. resize(c, c->x, c->y, c->w, c->h, 0);
  1803. showhide(c->snext);
  1804. } else {
  1805. /* hide clients bottom up */
  1806. showhide(c->snext);
  1807. XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
  1808. }
  1809. }
  1810. void
  1811. sigchld(int unused)
  1812. {
  1813. if (signal(SIGCHLD, sigchld) == SIG_ERR)
  1814. die("can't install SIGCHLD handler:");
  1815. while (0 < waitpid(-1, NULL, WNOHANG));
  1816. }
  1817. void
  1818. sighup(int unused)
  1819. {
  1820. Arg a = {.i = 1};
  1821. quit(&a);
  1822. }
  1823. void
  1824. sigterm(int unused)
  1825. {
  1826. Arg a = {.i = 0};
  1827. quit(&a);
  1828. }
  1829. void
  1830. spawn(const Arg *arg)
  1831. {
  1832. if (arg->v == dmenucmd)
  1833. dmenumon[0] = '0' + selmon->num;
  1834. selmon->tagset[selmon->seltags] &= ~scratchtag;
  1835. if (fork() == 0) {
  1836. if (dpy)
  1837. close(ConnectionNumber(dpy));
  1838. setsid();
  1839. execvp(((char **)arg->v)[0], (char **)arg->v);
  1840. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1841. perror(" failed");
  1842. exit(EXIT_SUCCESS);
  1843. }
  1844. }
  1845. void
  1846. tag(const Arg *arg)
  1847. {
  1848. if (selmon->sel && arg->ui & TAGMASK) {
  1849. selmon->sel->tags = arg->ui & TAGMASK;
  1850. focus(NULL);
  1851. arrange(selmon);
  1852. }
  1853. }
  1854. void
  1855. tagmon(const Arg *arg)
  1856. {
  1857. if (!selmon->sel || !mons->next)
  1858. return;
  1859. sendmon(selmon->sel, dirtomon(arg->i));
  1860. }
  1861. void
  1862. tile(Monitor *m)
  1863. {
  1864. unsigned int i, n, h, r, oe = enablegaps, ie = enablegaps, mw, my, ty;
  1865. Client *c;
  1866. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  1867. if (n == 0)
  1868. return;
  1869. if (smartgaps == n) {
  1870. oe = 0; // outer gaps disabled
  1871. }
  1872. if (n > m->nmaster)
  1873. mw = m->nmaster ? (m->ww + m->gappiv*ie) * m->mfact : 0;
  1874. else
  1875. mw = m->ww - 2*m->gappov*oe + m->gappiv*ie;
  1876. for (i = 0, my = ty = m->gappoh*oe, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  1877. if (i < m->nmaster) {
  1878. r = MIN(n, m->nmaster) - i;
  1879. h = (m->wh - my - m->gappoh*oe - m->gappih*ie * (r - 1)) / r;
  1880. resize(c, m->wx + m->gappov*oe, m->wy + my, mw - (2*c->bw) - m->gappiv*ie, h - (2*c->bw), 0);
  1881. my += HEIGHT(c) + m->gappih*ie;
  1882. } else {
  1883. r = n - i;
  1884. h = (m->wh - ty - m->gappoh*oe - m->gappih*ie * (r - 1)) / r;
  1885. 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);
  1886. ty += HEIGHT(c) + m->gappih*ie;
  1887. }
  1888. }
  1889. void
  1890. togglebar(const Arg *arg)
  1891. {
  1892. selmon->showbar = !selmon->showbar;
  1893. updatebarpos(selmon);
  1894. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1895. arrange(selmon);
  1896. }
  1897. void
  1898. togglefloating(const Arg *arg)
  1899. {
  1900. if (!selmon->sel)
  1901. return;
  1902. if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
  1903. return;
  1904. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1905. if (selmon->sel->isfloating)
  1906. resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  1907. selmon->sel->w, selmon->sel->h, 0);
  1908. arrange(selmon);
  1909. }
  1910. void
  1911. togglescratch(const Arg *arg)
  1912. {
  1913. Client *c;
  1914. unsigned int found = 0;
  1915. for (c = selmon->clients; c && !(found = c->tags & scratchtag); c = c->next);
  1916. if (found) {
  1917. unsigned int newtagset = selmon->tagset[selmon->seltags] ^ scratchtag;
  1918. if (newtagset) {
  1919. selmon->tagset[selmon->seltags] = newtagset;
  1920. focus(NULL);
  1921. arrange(selmon);
  1922. }
  1923. if (ISVISIBLE(c)) {
  1924. focus(c);
  1925. restack(selmon);
  1926. }
  1927. } else
  1928. spawn(arg);
  1929. }
  1930. void
  1931. toggletag(const Arg *arg)
  1932. {
  1933. unsigned int newtags;
  1934. if (!selmon->sel)
  1935. return;
  1936. newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1937. if (newtags) {
  1938. selmon->sel->tags = newtags;
  1939. focus(NULL);
  1940. arrange(selmon);
  1941. }
  1942. }
  1943. void
  1944. toggleview(const Arg *arg)
  1945. {
  1946. unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1947. if (newtagset) {
  1948. selmon->tagset[selmon->seltags] = newtagset;
  1949. focus(NULL);
  1950. arrange(selmon);
  1951. }
  1952. }
  1953. void
  1954. unfocus(Client *c, int setfocus)
  1955. {
  1956. if (!c)
  1957. return;
  1958. grabbuttons(c, 0);
  1959. XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
  1960. if (setfocus) {
  1961. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1962. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  1963. }
  1964. }
  1965. void
  1966. unmanage(Client *c, int destroyed)
  1967. {
  1968. Monitor *m = c->mon;
  1969. XWindowChanges wc;
  1970. if (c->swallowing) {
  1971. unswallow(c);
  1972. return;
  1973. }
  1974. Client *s = swallowingclient(c->win);
  1975. if (s) {
  1976. free(s->swallowing);
  1977. s->swallowing = NULL;
  1978. arrange(m);
  1979. focus(NULL);
  1980. return;
  1981. }
  1982. detach(c);
  1983. detachstack(c);
  1984. if (!destroyed) {
  1985. wc.border_width = c->oldbw;
  1986. XGrabServer(dpy); /* avoid race conditions */
  1987. XSetErrorHandler(xerrordummy);
  1988. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1989. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1990. setclientstate(c, WithdrawnState);
  1991. XSync(dpy, False);
  1992. XSetErrorHandler(xerror);
  1993. XUngrabServer(dpy);
  1994. }
  1995. free(c);
  1996. if (!s) {
  1997. arrange(m);
  1998. focus(NULL);
  1999. updateclientlist();
  2000. }
  2001. }
  2002. void
  2003. unmapnotify(XEvent *e)
  2004. {
  2005. Client *c;
  2006. XUnmapEvent *ev = &e->xunmap;
  2007. if ((c = wintoclient(ev->window))) {
  2008. if (ev->send_event)
  2009. setclientstate(c, WithdrawnState);
  2010. else
  2011. unmanage(c, 0);
  2012. }
  2013. }
  2014. void
  2015. updatebars(void)
  2016. {
  2017. Monitor *m;
  2018. XSetWindowAttributes wa = {
  2019. .override_redirect = True,
  2020. .background_pixel = 0,
  2021. .border_pixel = 0,
  2022. .colormap = cmap,
  2023. .event_mask = ButtonPressMask|ExposureMask
  2024. };
  2025. XClassHint ch = {"dwm", "dwm"};
  2026. for (m = mons; m; m = m->next) {
  2027. if (m->barwin)
  2028. continue;
  2029. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, depth,
  2030. InputOutput, visual,
  2031. CWOverrideRedirect|CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &wa);
  2032. XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
  2033. XMapRaised(dpy, m->barwin);
  2034. XSetClassHint(dpy, m->barwin, &ch);
  2035. }
  2036. }
  2037. void
  2038. updatebarpos(Monitor *m)
  2039. {
  2040. m->wy = m->my;
  2041. m->wh = m->mh;
  2042. if (m->showbar) {
  2043. m->wh -= bh;
  2044. m->by = m->topbar ? m->wy : m->wy + m->wh;
  2045. m->wy = m->topbar ? m->wy + bh : m->wy;
  2046. } else
  2047. m->by = -bh;
  2048. }
  2049. void
  2050. updateclientlist()
  2051. {
  2052. Client *c;
  2053. Monitor *m;
  2054. XDeleteProperty(dpy, root, netatom[NetClientList]);
  2055. for (m = mons; m; m = m->next)
  2056. for (c = m->clients; c; c = c->next)
  2057. XChangeProperty(dpy, root, netatom[NetClientList],
  2058. XA_WINDOW, 32, PropModeAppend,
  2059. (unsigned char *) &(c->win), 1);
  2060. }
  2061. int
  2062. updategeom(void)
  2063. {
  2064. int dirty = 0;
  2065. #ifdef XINERAMA
  2066. if (XineramaIsActive(dpy)) {
  2067. int i, j, n, nn;
  2068. Client *c;
  2069. Monitor *m;
  2070. XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
  2071. XineramaScreenInfo *unique = NULL;
  2072. for (n = 0, m = mons; m; m = m->next, n++);
  2073. /* only consider unique geometries as separate screens */
  2074. unique = ecalloc(nn, sizeof(XineramaScreenInfo));
  2075. for (i = 0, j = 0; i < nn; i++)
  2076. if (isuniquegeom(unique, j, &info[i]))
  2077. memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
  2078. XFree(info);
  2079. nn = j;
  2080. if (n <= nn) { /* new monitors available */
  2081. for (i = 0; i < (nn - n); i++) {
  2082. for (m = mons; m && m->next; m = m->next);
  2083. if (m)
  2084. m->next = createmon();
  2085. else
  2086. mons = createmon();
  2087. }
  2088. for (i = 0, m = mons; i < nn && m; m = m->next, i++)
  2089. if (i >= n
  2090. || unique[i].x_org != m->mx || unique[i].y_org != m->my
  2091. || unique[i].width != m->mw || unique[i].height != m->mh)
  2092. {
  2093. dirty = 1;
  2094. m->num = i;
  2095. m->mx = m->wx = unique[i].x_org;
  2096. m->my = m->wy = unique[i].y_org;
  2097. m->mw = m->ww = unique[i].width;
  2098. m->mh = m->wh = unique[i].height;
  2099. updatebarpos(m);
  2100. }
  2101. } else { /* less monitors available nn < n */
  2102. for (i = nn; i < n; i++) {
  2103. for (m = mons; m && m->next; m = m->next);
  2104. while ((c = m->clients)) {
  2105. dirty = 1;
  2106. m->clients = c->next;
  2107. detachstack(c);
  2108. c->mon = mons;
  2109. attach(c);
  2110. attachstack(c);
  2111. }
  2112. if (m == selmon)
  2113. selmon = mons;
  2114. cleanupmon(m);
  2115. }
  2116. }
  2117. free(unique);
  2118. } else
  2119. #endif /* XINERAMA */
  2120. { /* default monitor setup */
  2121. if (!mons)
  2122. mons = createmon();
  2123. if (mons->mw != sw || mons->mh != sh) {
  2124. dirty = 1;
  2125. mons->mw = mons->ww = sw;
  2126. mons->mh = mons->wh = sh;
  2127. updatebarpos(mons);
  2128. }
  2129. }
  2130. if (dirty) {
  2131. selmon = mons;
  2132. selmon = wintomon(root);
  2133. }
  2134. return dirty;
  2135. }
  2136. void
  2137. updatenumlockmask(void)
  2138. {
  2139. unsigned int i, j;
  2140. XModifierKeymap *modmap;
  2141. numlockmask = 0;
  2142. modmap = XGetModifierMapping(dpy);
  2143. for (i = 0; i < 8; i++)
  2144. for (j = 0; j < modmap->max_keypermod; j++)
  2145. if (modmap->modifiermap[i * modmap->max_keypermod + j]
  2146. == XKeysymToKeycode(dpy, XK_Num_Lock))
  2147. numlockmask = (1 << i);
  2148. XFreeModifiermap(modmap);
  2149. }
  2150. void
  2151. updatesizehints(Client *c)
  2152. {
  2153. long msize;
  2154. XSizeHints size;
  2155. if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
  2156. /* size is uninitialized, ensure that size.flags aren't used */
  2157. size.flags = PSize;
  2158. if (size.flags & PBaseSize) {
  2159. c->basew = size.base_width;
  2160. c->baseh = size.base_height;
  2161. } else if (size.flags & PMinSize) {
  2162. c->basew = size.min_width;
  2163. c->baseh = size.min_height;
  2164. } else
  2165. c->basew = c->baseh = 0;
  2166. if (size.flags & PResizeInc) {
  2167. c->incw = size.width_inc;
  2168. c->inch = size.height_inc;
  2169. } else
  2170. c->incw = c->inch = 0;
  2171. if (size.flags & PMaxSize) {
  2172. c->maxw = size.max_width;
  2173. c->maxh = size.max_height;
  2174. } else
  2175. c->maxw = c->maxh = 0;
  2176. if (size.flags & PMinSize) {
  2177. c->minw = size.min_width;
  2178. c->minh = size.min_height;
  2179. } else if (size.flags & PBaseSize) {
  2180. c->minw = size.base_width;
  2181. c->minh = size.base_height;
  2182. } else
  2183. c->minw = c->minh = 0;
  2184. if (size.flags & PAspect) {
  2185. c->mina = (float)size.min_aspect.y / size.min_aspect.x;
  2186. c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
  2187. } else
  2188. c->maxa = c->mina = 0.0;
  2189. c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
  2190. }
  2191. void
  2192. updatestatus(void)
  2193. {
  2194. if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  2195. strcpy(stext, "dwm-"VERSION);
  2196. drawbar(selmon);
  2197. }
  2198. void
  2199. updatetitle(Client *c)
  2200. {
  2201. if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  2202. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  2203. if (c->name[0] == '\0') /* hack to mark broken clients */
  2204. strcpy(c->name, broken);
  2205. }
  2206. void
  2207. updatewindowtype(Client *c)
  2208. {
  2209. Atom state = getatomprop(c, netatom[NetWMState]);
  2210. Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
  2211. if (state == netatom[NetWMFullscreen])
  2212. setfullscreen(c, 1);
  2213. if (wtype == netatom[NetWMWindowTypeDialog])
  2214. c->isfloating = 1;
  2215. }
  2216. void
  2217. updatewmhints(Client *c)
  2218. {
  2219. XWMHints *wmh;
  2220. if ((wmh = XGetWMHints(dpy, c->win))) {
  2221. if (c == selmon->sel && wmh->flags & XUrgencyHint) {
  2222. wmh->flags &= ~XUrgencyHint;
  2223. XSetWMHints(dpy, c->win, wmh);
  2224. } else
  2225. c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
  2226. if (wmh->flags & InputHint)
  2227. c->neverfocus = !wmh->input;
  2228. else
  2229. c->neverfocus = 0;
  2230. XFree(wmh);
  2231. }
  2232. }
  2233. void
  2234. view(const Arg *arg)
  2235. {
  2236. if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  2237. return;
  2238. selmon->seltags ^= 1; /* toggle sel tagset */
  2239. if (arg->ui & TAGMASK)
  2240. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  2241. focus(NULL);
  2242. arrange(selmon);
  2243. }
  2244. pid_t
  2245. winpid(Window w)
  2246. {
  2247. pid_t result = 0;
  2248. xcb_res_client_id_spec_t spec = {0};
  2249. spec.client = w;
  2250. spec.mask = XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID;
  2251. xcb_generic_error_t *e = NULL;
  2252. xcb_res_query_client_ids_cookie_t c = xcb_res_query_client_ids(xcon, 1, &spec);
  2253. xcb_res_query_client_ids_reply_t *r = xcb_res_query_client_ids_reply(xcon, c, &e);
  2254. if (!r)
  2255. return (pid_t)0;
  2256. xcb_res_client_id_value_iterator_t i = xcb_res_query_client_ids_ids_iterator(r);
  2257. for (; i.rem; xcb_res_client_id_value_next(&i)) {
  2258. spec = i.data->spec;
  2259. if (spec.mask & XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID) {
  2260. uint32_t *t = xcb_res_client_id_value_value(i.data);
  2261. result = *t;
  2262. break;
  2263. }
  2264. }
  2265. free(r);
  2266. if (result == (pid_t)-1)
  2267. result = 0;
  2268. return result;
  2269. }
  2270. pid_t
  2271. getparentprocess(pid_t p)
  2272. {
  2273. unsigned int v = 0;
  2274. #if defined(__linux__)
  2275. FILE *f;
  2276. char buf[256];
  2277. snprintf(buf, sizeof(buf) - 1, "/proc/%u/stat", (unsigned)p);
  2278. if (!(f = fopen(buf, "r")))
  2279. return (pid_t)0;
  2280. if (fscanf(f, "%*u %*s %*c %u", (unsigned *)&v) != 1)
  2281. v = (pid_t)0;
  2282. fclose(f);
  2283. #elif defined(__FreeBSD__)
  2284. struct kinfo_proc *proc = kinfo_getproc(p);
  2285. if (!proc)
  2286. return (pid_t)0;
  2287. v = proc->ki_ppid;
  2288. free(proc);
  2289. #endif
  2290. return (pid_t)v;
  2291. }
  2292. int
  2293. isdescprocess(pid_t p, pid_t c)
  2294. {
  2295. while (p != c && c != 0)
  2296. c = getparentprocess(c);
  2297. return (int)c;
  2298. }
  2299. Client *
  2300. termforwin(const Client *w)
  2301. {
  2302. Client *c;
  2303. Monitor *m;
  2304. if (!w->pid || w->isterminal)
  2305. return NULL;
  2306. for (m = mons; m; m = m->next) {
  2307. for (c = m->clients; c; c = c->next) {
  2308. if (c->isterminal && !c->swallowing && c->pid && isdescprocess(c->pid, w->pid))
  2309. return c;
  2310. }
  2311. }
  2312. return NULL;
  2313. }
  2314. Client *
  2315. swallowingclient(Window w)
  2316. {
  2317. Client *c;
  2318. Monitor *m;
  2319. for (m = mons; m; m = m->next) {
  2320. for (c = m->clients; c; c = c->next) {
  2321. if (c->swallowing && c->swallowing->win == w)
  2322. return c;
  2323. }
  2324. }
  2325. return NULL;
  2326. }
  2327. Client *
  2328. wintoclient(Window w)
  2329. {
  2330. Client *c;
  2331. Monitor *m;
  2332. for (m = mons; m; m = m->next)
  2333. for (c = m->clients; c; c = c->next)
  2334. if (c->win == w)
  2335. return c;
  2336. return NULL;
  2337. }
  2338. Monitor *
  2339. wintomon(Window w)
  2340. {
  2341. int x, y;
  2342. Client *c;
  2343. Monitor *m;
  2344. if (w == root && getrootptr(&x, &y))
  2345. return recttomon(x, y, 1, 1);
  2346. for (m = mons; m; m = m->next)
  2347. if (w == m->barwin)
  2348. return m;
  2349. if ((c = wintoclient(w)))
  2350. return c->mon;
  2351. return selmon;
  2352. }
  2353. /* There's no way to check accesses to destroyed windows, thus those cases are
  2354. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  2355. * default error handler, which may call exit. */
  2356. int
  2357. xerror(Display *dpy, XErrorEvent *ee)
  2358. {
  2359. if (ee->error_code == BadWindow
  2360. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  2361. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  2362. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  2363. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  2364. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  2365. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  2366. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  2367. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  2368. return 0;
  2369. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  2370. ee->request_code, ee->error_code);
  2371. return xerrorxlib(dpy, ee); /* may call exit */
  2372. }
  2373. int
  2374. xerrordummy(Display *dpy, XErrorEvent *ee)
  2375. {
  2376. return 0;
  2377. }
  2378. /* Startup Error handler to check if another window manager
  2379. * is already running. */
  2380. int
  2381. xerrorstart(Display *dpy, XErrorEvent *ee)
  2382. {
  2383. die("dwm: another window manager is already running");
  2384. return -1;
  2385. }
  2386. void
  2387. xinitvisual()
  2388. {
  2389. XVisualInfo *infos;
  2390. XRenderPictFormat *fmt;
  2391. int nitems;
  2392. int i;
  2393. XVisualInfo tpl = {
  2394. .screen = screen,
  2395. .depth = 32,
  2396. .class = TrueColor
  2397. };
  2398. long masks = VisualScreenMask | VisualDepthMask | VisualClassMask;
  2399. infos = XGetVisualInfo(dpy, masks, &tpl, &nitems);
  2400. visual = NULL;
  2401. for(i = 0; i < nitems; i ++) {
  2402. fmt = XRenderFindVisualFormat(dpy, infos[i].visual);
  2403. if (fmt->type == PictTypeDirect && fmt->direct.alphaMask) {
  2404. visual = infos[i].visual;
  2405. depth = infos[i].depth;
  2406. cmap = XCreateColormap(dpy, root, visual, AllocNone);
  2407. useargb = 1;
  2408. break;
  2409. }
  2410. }
  2411. XFree(infos);
  2412. if (! visual) {
  2413. visual = DefaultVisual(dpy, screen);
  2414. depth = DefaultDepth(dpy, screen);
  2415. cmap = DefaultColormap(dpy, screen);
  2416. }
  2417. }
  2418. void
  2419. xrdb(const Arg *arg)
  2420. {
  2421. loadxrdb();
  2422. int i;
  2423. for (i = 0; i < LENGTH(colors); i++)
  2424. scheme[i] = drw_scm_create(drw, colors[i], alphas[i], 3);
  2425. focus(NULL);
  2426. arrange(NULL);
  2427. }
  2428. void
  2429. zoom(const Arg *arg)
  2430. {
  2431. Client *c = selmon->sel;
  2432. if (!selmon->lt[selmon->sellt]->arrange
  2433. || (selmon->sel && selmon->sel->isfloating))
  2434. return;
  2435. if (c == nexttiled(selmon->clients))
  2436. if (!c || !(c = nexttiled(c->next)))
  2437. return;
  2438. pop(c);
  2439. }
  2440. int
  2441. main(int argc, char *argv[])
  2442. {
  2443. if (argc == 2 && !strcmp("-v", argv[1]))
  2444. die("dwm-"VERSION);
  2445. else if (argc != 1)
  2446. die("usage: dwm [-v]");
  2447. if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  2448. fputs("warning: no locale support\n", stderr);
  2449. if (!(dpy = XOpenDisplay(NULL)))
  2450. die("dwm: cannot open display");
  2451. if (!(xcon = XGetXCBConnection(dpy)))
  2452. die("dwm: cannot get xcb connection\n");
  2453. checkotherwm();
  2454. XrmInitialize();
  2455. setup();
  2456. #ifdef __OpenBSD__
  2457. if (pledge("stdio rpath proc exec", NULL) == -1)
  2458. die("pledge");
  2459. #endif /* __OpenBSD__ */
  2460. scan();
  2461. run();
  2462. if (restart) execvp(argv[0], argv);
  2463. cleanup();
  2464. XCloseDisplay(dpy);
  2465. return EXIT_SUCCESS;
  2466. }
  2467. void
  2468. centeredmaster(Monitor *m)
  2469. {
  2470. unsigned int i, n, h, mw, mx, my, oty, ety, tw;
  2471. Client *c;
  2472. /* count number of clients in the selected monitor */
  2473. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  2474. if (n == 0)
  2475. return;
  2476. /* initialize areas */
  2477. mw = m->ww;
  2478. mx = 0;
  2479. my = 0;
  2480. tw = mw;
  2481. if (n > m->nmaster) {
  2482. /* go mfact box in the center if more than nmaster clients */
  2483. mw = m->nmaster ? m->ww * m->mfact : 0;
  2484. tw = m->ww - mw;
  2485. if (n - m->nmaster > 1) {
  2486. /* only one client */
  2487. mx = (m->ww - mw) / 2;
  2488. tw = (m->ww - mw) / 2;
  2489. }
  2490. }
  2491. oty = 0;
  2492. ety = 0;
  2493. for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  2494. if (i < m->nmaster) {
  2495. /* nmaster clients are stacked vertically, in the center
  2496. * of the screen */
  2497. h = (m->wh - my) / (MIN(n, m->nmaster) - i);
  2498. resize(c, m->wx + mx, m->wy + my, mw - (2*c->bw),
  2499. h - (2*c->bw), 0);
  2500. my += HEIGHT(c);
  2501. } else {
  2502. /* stack clients are stacked vertically */
  2503. if ((i - m->nmaster) % 2 ) {
  2504. h = (m->wh - ety) / ( (1 + n - i) / 2);
  2505. resize(c, m->wx, m->wy + ety, tw - (2*c->bw),
  2506. h - (2*c->bw), 0);
  2507. ety += HEIGHT(c);
  2508. } else {
  2509. h = (m->wh - oty) / ((1 + n - i) / 2);
  2510. resize(c, m->wx + mx + mw, m->wy + oty,
  2511. tw - (2*c->bw), h - (2*c->bw), 0);
  2512. oty += HEIGHT(c);
  2513. }
  2514. }
  2515. }
  2516. void
  2517. centeredfloatingmaster(Monitor *m)
  2518. {
  2519. unsigned int i, n, w, mh, mw, mx, mxo, my, myo, tx;
  2520. Client *c;
  2521. /* count number of clients in the selected monitor */
  2522. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  2523. if (n == 0)
  2524. return;
  2525. /* initialize nmaster area */
  2526. if (n > m->nmaster) {
  2527. /* go mfact box in the center if more than nmaster clients */
  2528. if (m->ww > m->wh) {
  2529. mw = m->nmaster ? m->ww * m->mfact : 0;
  2530. mh = m->nmaster ? m->wh * 0.9 : 0;
  2531. } else {
  2532. mh = m->nmaster ? m->wh * m->mfact : 0;
  2533. mw = m->nmaster ? m->ww * 0.9 : 0;
  2534. }
  2535. mx = mxo = (m->ww - mw) / 2;
  2536. my = myo = (m->wh - mh) / 2;
  2537. } else {
  2538. /* go fullscreen if all clients are in the master area */
  2539. mh = m->wh;
  2540. mw = m->ww;
  2541. mx = mxo = 0;
  2542. my = myo = 0;
  2543. }
  2544. for(i = tx = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  2545. if (i < m->nmaster) {
  2546. /* nmaster clients are stacked horizontally, in the center
  2547. * of the screen */
  2548. w = (mw + mxo - mx) / (MIN(n, m->nmaster) - i);
  2549. resize(c, m->wx + mx, m->wy + my, w - (2*c->bw),
  2550. mh - (2*c->bw), 0);
  2551. mx += WIDTH(c);
  2552. } else {
  2553. /* stack clients are stacked horizontally */
  2554. w = (m->ww - tx) / (n - i);
  2555. resize(c, m->wx + tx, m->wy, w - (2*c->bw),
  2556. m->wh - (2*c->bw), 0);
  2557. tx += WIDTH(c);
  2558. }
  2559. }