dwm.c 49 KB

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