dwm.c 51 KB

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