dwm.c 47 KB

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