dwm.c 53 KB

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