dwm.c 48 KB

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