dwm.c 47 KB

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