dwm.c.orig 53 KB

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