dwm.c 54 KB

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