dwm.c 56 KB

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