dwm.c 49 KB

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