dwm.c 56 KB

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