dwm.c 47 KB

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