dwm.c 46 KB

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