dwm.c 47 KB

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