dwm.c 52 KB

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