dwm.c 54 KB

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