dwm.c 52 KB

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