dwm.c 49 KB

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