dwm.c 50 KB

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