dwm.c 51 KB

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