dwm.c 51 KB

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