dwm.c 47 KB

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