dwm.c 52 KB

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