dwm.c 51 KB

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