dwm.c 53 KB

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