dwm.c 53 KB

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