dwm.c 52 KB

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