dwm.c 54 KB

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