dwm.c 51 KB

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