dwm.c 54 KB

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