dwm.c 51 KB

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