dwm.c 47 KB

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