dwm.c 51 KB

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