dwm.c 52 KB

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