dwm.c 47 KB

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