dwm.c 47 KB

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