dwm.c 47 KB

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