dwm.c 54 KB

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