dwm.c 52 KB

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