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. if(ev->window == root) {
  541. dirty = (sw != ev->width);
  542. sw = ev->width;
  543. sh = ev->height;
  544. if(updategeom() || dirty) {
  545. if(dc.drawable != 0)
  546. XFreePixmap(dpy, dc.drawable);
  547. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  548. updatebars();
  549. for(m = mons; m; m = m->next)
  550. XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
  551. focus(NULL);
  552. arrange(NULL);
  553. }
  554. }
  555. }
  556. void
  557. configurerequest(XEvent *e) {
  558. Client *c;
  559. Monitor *m;
  560. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  561. XWindowChanges wc;
  562. if((c = wintoclient(ev->window))) {
  563. if(ev->value_mask & CWBorderWidth)
  564. c->bw = ev->border_width;
  565. else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
  566. m = c->mon;
  567. if(ev->value_mask & CWX) {
  568. c->oldx = c->x;
  569. c->x = m->mx + ev->x;
  570. }
  571. if(ev->value_mask & CWY) {
  572. c->oldy = c->y;
  573. c->y = m->my + ev->y;
  574. }
  575. if(ev->value_mask & CWWidth) {
  576. c->oldw = c->w;
  577. c->w = ev->width;
  578. }
  579. if(ev->value_mask & CWHeight) {
  580. c->oldh = c->h;
  581. c->h = ev->height;
  582. }
  583. if((c->x + c->w) > m->mx + m->mw && c->isfloating)
  584. c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
  585. if((c->y + c->h) > m->my + m->mh && c->isfloating)
  586. c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
  587. if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
  588. configure(c);
  589. if(ISVISIBLE(c))
  590. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  591. }
  592. else
  593. configure(c);
  594. }
  595. else {
  596. wc.x = ev->x;
  597. wc.y = ev->y;
  598. wc.width = ev->width;
  599. wc.height = ev->height;
  600. wc.border_width = ev->border_width;
  601. wc.sibling = ev->above;
  602. wc.stack_mode = ev->detail;
  603. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  604. }
  605. XSync(dpy, False);
  606. }
  607. Monitor *
  608. createmon(void) {
  609. Monitor *m;
  610. if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
  611. die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
  612. m->tagset[0] = m->tagset[1] = 1;
  613. m->mfact = mfact;
  614. m->nmaster = nmaster;
  615. m->showbar = showbar;
  616. m->topbar = topbar;
  617. m->lt[0] = &layouts[0];
  618. m->lt[1] = &layouts[1 % LENGTH(layouts)];
  619. strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
  620. return m;
  621. }
  622. void
  623. destroynotify(XEvent *e) {
  624. Client *c;
  625. XDestroyWindowEvent *ev = &e->xdestroywindow;
  626. if((c = wintoclient(ev->window)))
  627. unmanage(c, True);
  628. }
  629. void
  630. detach(Client *c) {
  631. Client **tc;
  632. for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
  633. *tc = c->next;
  634. }
  635. void
  636. detachstack(Client *c) {
  637. Client **tc, *t;
  638. for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
  639. *tc = c->snext;
  640. if(c == c->mon->sel) {
  641. for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
  642. c->mon->sel = t;
  643. }
  644. }
  645. void
  646. die(const char *errstr, ...) {
  647. va_list ap;
  648. va_start(ap, errstr);
  649. vfprintf(stderr, errstr, ap);
  650. va_end(ap);
  651. exit(EXIT_FAILURE);
  652. }
  653. Monitor *
  654. dirtomon(int dir) {
  655. Monitor *m = NULL;
  656. if(dir > 0) {
  657. if(!(m = selmon->next))
  658. m = mons;
  659. }
  660. else if(selmon == mons)
  661. for(m = mons; m->next; m = m->next);
  662. else
  663. for(m = mons; m->next != selmon; m = m->next);
  664. return m;
  665. }
  666. void
  667. drawbar(Monitor *m) {
  668. int x;
  669. unsigned int i, occ = 0, urg = 0;
  670. unsigned long *col;
  671. Client *c;
  672. for(c = m->clients; c; c = c->next) {
  673. occ |= c->tags;
  674. if(c->isurgent)
  675. urg |= c->tags;
  676. }
  677. dc.x = 0;
  678. for(i = 0; i < LENGTH(tags); i++) {
  679. dc.w = TEXTW(tags[i]);
  680. col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
  681. drawtext(tags[i], col, urg & 1 << i);
  682. drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
  683. occ & 1 << i, urg & 1 << i, col);
  684. dc.x += dc.w;
  685. }
  686. dc.w = blw = TEXTW(m->ltsymbol);
  687. drawtext(m->ltsymbol, dc.norm, False);
  688. dc.x += dc.w;
  689. x = dc.x;
  690. if(m == selmon) { /* status is only drawn on selected monitor */
  691. dc.w = TEXTW(stext);
  692. dc.x = m->ww - dc.w;
  693. if(dc.x < x) {
  694. dc.x = x;
  695. dc.w = m->ww - x;
  696. }
  697. drawtext(stext, dc.norm, False);
  698. }
  699. else
  700. dc.x = m->ww;
  701. if((dc.w = dc.x - x) > bh) {
  702. dc.x = x;
  703. if(m->sel) {
  704. col = m == selmon ? dc.sel : dc.norm;
  705. drawtext(m->sel->name, col, False);
  706. drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
  707. }
  708. else
  709. drawtext(NULL, dc.norm, False);
  710. }
  711. XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
  712. XSync(dpy, False);
  713. }
  714. void
  715. drawbars(void) {
  716. Monitor *m;
  717. for(m = mons; m; m = m->next)
  718. drawbar(m);
  719. }
  720. void
  721. drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  722. int x;
  723. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  724. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  725. if(filled)
  726. XFillRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x+1, x+1);
  727. else if(empty)
  728. XDrawRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x, x);
  729. }
  730. void
  731. drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
  732. char buf[256];
  733. int i, x, y, h, len, olen;
  734. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  735. XFillRectangle(dpy, dc.drawable, dc.gc, dc.x, dc.y, dc.w, dc.h);
  736. if(!text)
  737. return;
  738. olen = strlen(text);
  739. h = dc.font.ascent + dc.font.descent;
  740. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  741. x = dc.x + (h / 2);
  742. /* shorten text if necessary */
  743. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  744. if(!len)
  745. return;
  746. memcpy(buf, text, len);
  747. if(len < olen)
  748. for(i = len; i && i > len - 3; buf[--i] = '.');
  749. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  750. if(dc.font.set)
  751. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  752. else
  753. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  754. }
  755. void
  756. enternotify(XEvent *e) {
  757. Client *c;
  758. Monitor *m;
  759. XCrossingEvent *ev = &e->xcrossing;
  760. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  761. return;
  762. c = wintoclient(ev->window);
  763. m = c ? c->mon : wintomon(ev->window);
  764. if(m != selmon) {
  765. unfocus(selmon->sel, True);
  766. selmon = m;
  767. }
  768. else if(!c || c == selmon->sel)
  769. return;
  770. focus(c);
  771. }
  772. void
  773. expose(XEvent *e) {
  774. Monitor *m;
  775. XExposeEvent *ev = &e->xexpose;
  776. if(ev->count == 0 && (m = wintomon(ev->window)))
  777. drawbar(m);
  778. }
  779. void
  780. focus(Client *c) {
  781. if(!c || !ISVISIBLE(c))
  782. for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
  783. /* was if(selmon->sel) */
  784. if(selmon->sel && selmon->sel != c)
  785. unfocus(selmon->sel, False);
  786. if(c) {
  787. if(c->mon != selmon)
  788. selmon = c->mon;
  789. if(c->isurgent)
  790. clearurgent(c);
  791. detachstack(c);
  792. attachstack(c);
  793. grabbuttons(c, True);
  794. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  795. setfocus(c);
  796. }
  797. else {
  798. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  799. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  800. }
  801. selmon->sel = c;
  802. drawbars();
  803. }
  804. void
  805. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  806. XFocusChangeEvent *ev = &e->xfocus;
  807. if(selmon->sel && ev->window != selmon->sel->win)
  808. setfocus(selmon->sel);
  809. }
  810. void
  811. focusmon(const Arg *arg) {
  812. Monitor *m;
  813. if(!mons->next)
  814. return;
  815. if((m = dirtomon(arg->i)) == selmon)
  816. return;
  817. unfocus(selmon->sel, True);
  818. selmon = m;
  819. focus(NULL);
  820. }
  821. void
  822. focusstack(const Arg *arg) {
  823. Client *c = NULL, *i;
  824. if(!selmon->sel)
  825. return;
  826. if(arg->i > 0) {
  827. for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
  828. if(!c)
  829. for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
  830. }
  831. else {
  832. for(i = selmon->clients; i != selmon->sel; i = i->next)
  833. if(ISVISIBLE(i))
  834. c = i;
  835. if(!c)
  836. for(; i; i = i->next)
  837. if(ISVISIBLE(i))
  838. c = i;
  839. }
  840. if(c) {
  841. focus(c);
  842. restack(selmon);
  843. }
  844. }
  845. Atom
  846. getatomprop(Client *c, Atom prop) {
  847. int di;
  848. unsigned long dl;
  849. unsigned char *p = NULL;
  850. Atom da, atom = None;
  851. if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
  852. &da, &di, &dl, &dl, &p) == Success && p) {
  853. atom = *(Atom *)p;
  854. XFree(p);
  855. }
  856. return atom;
  857. }
  858. unsigned long
  859. getcolor(const char *colstr) {
  860. Colormap cmap = DefaultColormap(dpy, screen);
  861. XColor color;
  862. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  863. die("error, cannot allocate color '%s'\n", colstr);
  864. return color.pixel;
  865. }
  866. Bool
  867. getrootptr(int *x, int *y) {
  868. int di;
  869. unsigned int dui;
  870. Window dummy;
  871. return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
  872. }
  873. long
  874. getstate(Window w) {
  875. int format;
  876. long result = -1;
  877. unsigned char *p = NULL;
  878. unsigned long n, extra;
  879. Atom real;
  880. if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  881. &real, &format, &n, &extra, (unsigned char **)&p) != Success)
  882. return -1;
  883. if(n != 0)
  884. result = *p;
  885. XFree(p);
  886. return result;
  887. }
  888. Bool
  889. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  890. char **list = NULL;
  891. int n;
  892. XTextProperty name;
  893. if(!text || size == 0)
  894. return False;
  895. text[0] = '\0';
  896. XGetTextProperty(dpy, w, &name, atom);
  897. if(!name.nitems)
  898. return False;
  899. if(name.encoding == XA_STRING)
  900. strncpy(text, (char *)name.value, size - 1);
  901. else {
  902. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
  903. strncpy(text, *list, size - 1);
  904. XFreeStringList(list);
  905. }
  906. }
  907. text[size - 1] = '\0';
  908. XFree(name.value);
  909. return True;
  910. }
  911. void
  912. grabbuttons(Client *c, Bool focused) {
  913. updatenumlockmask();
  914. {
  915. unsigned int i, j;
  916. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  917. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  918. if(focused) {
  919. for(i = 0; i < LENGTH(buttons); i++)
  920. if(buttons[i].click == ClkClientWin)
  921. for(j = 0; j < LENGTH(modifiers); j++)
  922. XGrabButton(dpy, buttons[i].button,
  923. buttons[i].mask | modifiers[j],
  924. c->win, False, BUTTONMASK,
  925. GrabModeAsync, GrabModeSync, None, None);
  926. }
  927. else
  928. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  929. BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  930. }
  931. }
  932. void
  933. grabkeys(void) {
  934. updatenumlockmask();
  935. {
  936. unsigned int i, j;
  937. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  938. KeyCode code;
  939. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  940. for(i = 0; i < LENGTH(keys); i++)
  941. if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  942. for(j = 0; j < LENGTH(modifiers); j++)
  943. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  944. True, GrabModeAsync, GrabModeAsync);
  945. }
  946. }
  947. void
  948. incnmaster(const Arg *arg) {
  949. selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
  950. arrange(selmon);
  951. }
  952. void
  953. initfont(const char *fontstr) {
  954. char *def, **missing;
  955. int n;
  956. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  957. if(missing) {
  958. while(n--)
  959. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  960. XFreeStringList(missing);
  961. }
  962. if(dc.font.set) {
  963. XFontStruct **xfonts;
  964. char **font_names;
  965. dc.font.ascent = dc.font.descent = 0;
  966. XExtentsOfFontSet(dc.font.set);
  967. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  968. while(n--) {
  969. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  970. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  971. xfonts++;
  972. }
  973. }
  974. else {
  975. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  976. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  977. die("error, cannot load font: '%s'\n", fontstr);
  978. dc.font.ascent = dc.font.xfont->ascent;
  979. dc.font.descent = dc.font.xfont->descent;
  980. }
  981. dc.font.height = dc.font.ascent + dc.font.descent;
  982. }
  983. #ifdef XINERAMA
  984. static Bool
  985. isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
  986. while(n--)
  987. if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
  988. && unique[n].width == info->width && unique[n].height == info->height)
  989. return False;
  990. return True;
  991. }
  992. #endif /* XINERAMA */
  993. void
  994. keypress(XEvent *e) {
  995. unsigned int i;
  996. KeySym keysym;
  997. XKeyEvent *ev;
  998. ev = &e->xkey;
  999. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  1000. for(i = 0; i < LENGTH(keys); i++)
  1001. if(keysym == keys[i].keysym
  1002. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  1003. && keys[i].func)
  1004. keys[i].func(&(keys[i].arg));
  1005. }
  1006. void
  1007. killclient(const Arg *arg) {
  1008. if(!selmon->sel)
  1009. return;
  1010. if(!sendevent(selmon->sel, wmatom[WMDelete])) {
  1011. XGrabServer(dpy);
  1012. XSetErrorHandler(xerrordummy);
  1013. XSetCloseDownMode(dpy, DestroyAll);
  1014. XKillClient(dpy, selmon->sel->win);
  1015. XSync(dpy, False);
  1016. XSetErrorHandler(xerror);
  1017. XUngrabServer(dpy);
  1018. }
  1019. }
  1020. void
  1021. manage(Window w, XWindowAttributes *wa) {
  1022. Client *c, *t = NULL;
  1023. Window trans = None;
  1024. XWindowChanges wc;
  1025. if(!(c = calloc(1, sizeof(Client))))
  1026. die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  1027. c->win = w;
  1028. updatetitle(c);
  1029. if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
  1030. c->mon = t->mon;
  1031. c->tags = t->tags;
  1032. }
  1033. else {
  1034. c->mon = selmon;
  1035. applyrules(c);
  1036. }
  1037. /* geometry */
  1038. c->x = c->oldx = wa->x;
  1039. c->y = c->oldy = wa->y;
  1040. c->w = c->oldw = wa->width;
  1041. c->h = c->oldh = wa->height;
  1042. c->oldbw = wa->border_width;
  1043. if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  1044. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  1045. if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  1046. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  1047. c->x = MAX(c->x, c->mon->mx);
  1048. /* only fix client y-offset, if the client center might cover the bar */
  1049. c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
  1050. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  1051. c->bw = borderpx;
  1052. wc.border_width = c->bw;
  1053. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  1054. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  1055. configure(c); /* propagates border_width, if size doesn't change */
  1056. updatewindowtype(c);
  1057. updatesizehints(c);
  1058. updatewmhints(c);
  1059. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  1060. grabbuttons(c, False);
  1061. if(!c->isfloating)
  1062. c->isfloating = c->oldstate = trans != None || c->isfixed;
  1063. if(c->isfloating)
  1064. XRaiseWindow(dpy, c->win);
  1065. attach(c);
  1066. attachstack(c);
  1067. XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
  1068. (unsigned char *) &(c->win), 1);
  1069. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  1070. setclientstate(c, NormalState);
  1071. if (c->mon == selmon)
  1072. unfocus(selmon->sel, False);
  1073. c->mon->sel = c;
  1074. arrange(c->mon);
  1075. XMapWindow(dpy, c->win);
  1076. focus(NULL);
  1077. }
  1078. void
  1079. mappingnotify(XEvent *e) {
  1080. XMappingEvent *ev = &e->xmapping;
  1081. XRefreshKeyboardMapping(ev);
  1082. if(ev->request == MappingKeyboard)
  1083. grabkeys();
  1084. }
  1085. void
  1086. maprequest(XEvent *e) {
  1087. static XWindowAttributes wa;
  1088. XMapRequestEvent *ev = &e->xmaprequest;
  1089. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1090. return;
  1091. if(wa.override_redirect)
  1092. return;
  1093. if(!wintoclient(ev->window))
  1094. manage(ev->window, &wa);
  1095. }
  1096. void
  1097. monocle(Monitor *m) {
  1098. unsigned int n = 0;
  1099. Client *c;
  1100. for(c = m->clients; c; c = c->next)
  1101. if(ISVISIBLE(c))
  1102. n++;
  1103. if(n > 0) /* override layout symbol */
  1104. snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
  1105. for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1106. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
  1107. }
  1108. void
  1109. motionnotify(XEvent *e) {
  1110. static Monitor *mon = NULL;
  1111. Monitor *m;
  1112. XMotionEvent *ev = &e->xmotion;
  1113. if(ev->window != root)
  1114. return;
  1115. if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
  1116. unfocus(selmon->sel, True);
  1117. selmon = m;
  1118. focus(NULL);
  1119. }
  1120. mon = m;
  1121. }
  1122. void
  1123. movemouse(const Arg *arg) {
  1124. int x, y, ocx, ocy, nx, ny;
  1125. Client *c;
  1126. Monitor *m;
  1127. XEvent ev;
  1128. if(!(c = selmon->sel))
  1129. return;
  1130. restack(selmon);
  1131. ocx = c->x;
  1132. ocy = c->y;
  1133. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1134. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1135. return;
  1136. if(!getrootptr(&x, &y))
  1137. return;
  1138. do {
  1139. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1140. switch(ev.type) {
  1141. case ConfigureRequest:
  1142. case Expose:
  1143. case MapRequest:
  1144. handler[ev.type](&ev);
  1145. break;
  1146. case MotionNotify:
  1147. nx = ocx + (ev.xmotion.x - x);
  1148. ny = ocy + (ev.xmotion.y - y);
  1149. if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1150. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1151. if(abs(selmon->wx - nx) < snap)
  1152. nx = selmon->wx;
  1153. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1154. nx = selmon->wx + selmon->ww - WIDTH(c);
  1155. if(abs(selmon->wy - ny) < snap)
  1156. ny = selmon->wy;
  1157. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1158. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1159. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1160. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1161. togglefloating(NULL);
  1162. }
  1163. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating) {
  1164. if(c->isfullscreen)
  1165. setfullscreen(c, False);
  1166. resize(c, nx, ny, c->w, c->h, True);
  1167. }
  1168. break;
  1169. }
  1170. } while(ev.type != ButtonRelease);
  1171. XUngrabPointer(dpy, CurrentTime);
  1172. if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1173. sendmon(c, m);
  1174. selmon = m;
  1175. focus(NULL);
  1176. }
  1177. }
  1178. Client *
  1179. nexttiled(Client *c) {
  1180. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1181. return c;
  1182. }
  1183. void
  1184. pop(Client *c) {
  1185. detach(c);
  1186. attach(c);
  1187. focus(c);
  1188. arrange(c->mon);
  1189. }
  1190. void
  1191. propertynotify(XEvent *e) {
  1192. Client *c;
  1193. Window trans;
  1194. XPropertyEvent *ev = &e->xproperty;
  1195. if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1196. updatestatus();
  1197. else if(ev->state == PropertyDelete)
  1198. return; /* ignore */
  1199. else if((c = wintoclient(ev->window))) {
  1200. switch(ev->atom) {
  1201. default: break;
  1202. case XA_WM_TRANSIENT_FOR:
  1203. if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
  1204. (c->isfloating = (wintoclient(trans)) != NULL))
  1205. arrange(c->mon);
  1206. break;
  1207. case XA_WM_NORMAL_HINTS:
  1208. updatesizehints(c);
  1209. break;
  1210. case XA_WM_HINTS:
  1211. updatewmhints(c);
  1212. drawbars();
  1213. break;
  1214. }
  1215. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1216. updatetitle(c);
  1217. if(c == c->mon->sel)
  1218. drawbar(c->mon);
  1219. }
  1220. if(ev->atom == netatom[NetWMWindowType])
  1221. updatewindowtype(c);
  1222. }
  1223. }
  1224. void
  1225. quit(const Arg *arg) {
  1226. running = False;
  1227. }
  1228. Monitor *
  1229. recttomon(int x, int y, int w, int h) {
  1230. Monitor *m, *r = selmon;
  1231. int a, area = 0;
  1232. for(m = mons; m; m = m->next)
  1233. if((a = INTERSECT(x, y, w, h, m)) > area) {
  1234. area = a;
  1235. r = m;
  1236. }
  1237. return r;
  1238. }
  1239. void
  1240. resize(Client *c, int x, int y, int w, int h, Bool interact) {
  1241. if(applysizehints(c, &x, &y, &w, &h, interact))
  1242. resizeclient(c, x, y, w, h);
  1243. }
  1244. void
  1245. resizeclient(Client *c, int x, int y, int w, int h) {
  1246. XWindowChanges wc;
  1247. c->oldx = c->x; c->x = wc.x = x;
  1248. c->oldy = c->y; c->y = wc.y = y;
  1249. c->oldw = c->w; c->w = wc.width = w;
  1250. c->oldh = c->h; c->h = wc.height = h;
  1251. wc.border_width = c->bw;
  1252. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1253. configure(c);
  1254. XSync(dpy, False);
  1255. }
  1256. void
  1257. resizemouse(const Arg *arg) {
  1258. int ocx, ocy;
  1259. int nw, nh;
  1260. Client *c;
  1261. Monitor *m;
  1262. XEvent ev;
  1263. if(!(c = selmon->sel))
  1264. return;
  1265. restack(selmon);
  1266. ocx = c->x;
  1267. ocy = c->y;
  1268. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1269. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1270. return;
  1271. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1272. do {
  1273. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1274. switch(ev.type) {
  1275. case ConfigureRequest:
  1276. case Expose:
  1277. case MapRequest:
  1278. handler[ev.type](&ev);
  1279. break;
  1280. case MotionNotify:
  1281. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1282. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1283. if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
  1284. && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
  1285. {
  1286. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1287. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1288. togglefloating(NULL);
  1289. }
  1290. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating) {
  1291. if(c->isfullscreen)
  1292. setfullscreen(c, False);
  1293. resize(c, c->x, c->y, nw, nh, True);
  1294. }
  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. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1613. if(selmon->sel->isfloating)
  1614. resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  1615. selmon->sel->w, selmon->sel->h, False);
  1616. else if(selmon->sel->isfullscreen)
  1617. setfullscreen(selmon->sel, 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. }