dwm.c 52 KB

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