dwm.c 66 KB

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