dwm.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  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 ((1 << 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[LENGTH(tags) > 31 ? -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 + 0.5;
  336. else if(c->mina < (float)*h / *w)
  337. *h = *w * c->mina + 0.5;
  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. for(m = mons; m; m = m->next)
  364. showhide(m->stack);
  365. focus(NULL);
  366. for(m = mons; m; m = m->next) {
  367. if(m->lt[m->sellt]->arrange)
  368. m->lt[m->sellt]->arrange(m);
  369. restack(m);
  370. }
  371. }
  372. void
  373. attach(Client *c) {
  374. c->next = c->mon->clients;
  375. c->mon->clients = c;
  376. }
  377. void
  378. attachstack(Client *c) {
  379. c->snext = c->mon->stack;
  380. c->mon->stack = c;
  381. }
  382. void
  383. buttonpress(XEvent *e) {
  384. unsigned int i, x, click;
  385. Arg arg = {0};
  386. Client *c;
  387. Monitor *m;
  388. XButtonPressedEvent *ev = &e->xbutton;
  389. click = ClkRootWin;
  390. /* focus monitor if necessary */
  391. if((m = wintomon(ev->window)) && m != selmon) {
  392. unfocus(selmon->sel);
  393. selmon = m;
  394. focus(NULL);
  395. }
  396. if(ev->window == selmon->barwin) {
  397. i = x = 0;
  398. do {
  399. x += TEXTW(tags[i]);
  400. } while(ev->x >= x && ++i < LENGTH(tags));
  401. if(i < LENGTH(tags)) {
  402. click = ClkTagBar;
  403. arg.ui = 1 << i;
  404. }
  405. else if(ev->x < x + blw)
  406. click = ClkLtSymbol;
  407. else if(ev->x > selmon->wx + selmon->ww - TEXTW(stext))
  408. click = ClkStatusText;
  409. else
  410. click = ClkWinTitle;
  411. }
  412. else if((c = wintoclient(ev->window))) {
  413. focus(c);
  414. click = ClkClientWin;
  415. }
  416. for(i = 0; i < LENGTH(buttons); i++)
  417. if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
  418. && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
  419. buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
  420. }
  421. void
  422. checkotherwm(void) {
  423. otherwm = False;
  424. xerrorxlib = XSetErrorHandler(xerrorstart);
  425. /* this causes an error if some other window manager is running */
  426. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  427. XSync(dpy, False);
  428. if(otherwm)
  429. die("dwm: another window manager is already running\n");
  430. XSetErrorHandler(xerror);
  431. XSync(dpy, False);
  432. }
  433. void
  434. cleanup(void) {
  435. Arg a = {.ui = ~0};
  436. Layout foo = { "", NULL };
  437. Monitor *m;
  438. view(&a);
  439. selmon->lt[selmon->sellt] = &foo;
  440. for(m = mons; m; m = m->next)
  441. while(m->stack)
  442. unmanage(m->stack);
  443. if(dc.font.set)
  444. XFreeFontSet(dpy, dc.font.set);
  445. else
  446. XFreeFont(dpy, dc.font.xfont);
  447. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  448. XFreePixmap(dpy, dc.drawable);
  449. XFreeGC(dpy, dc.gc);
  450. XFreeCursor(dpy, cursor[CurNormal]);
  451. XFreeCursor(dpy, cursor[CurResize]);
  452. XFreeCursor(dpy, cursor[CurMove]);
  453. cleanupmons();
  454. XSync(dpy, False);
  455. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  456. }
  457. void
  458. cleanupmons(void) {
  459. Monitor *m;
  460. while(mons) {
  461. m = mons->next;
  462. XUnmapWindow(dpy, mons->barwin);
  463. XDestroyWindow(dpy, mons->barwin);
  464. free(mons);
  465. mons = m;
  466. }
  467. }
  468. void
  469. clearurgent(Client *c) {
  470. XWMHints *wmh;
  471. c->isurgent = False;
  472. if(!(wmh = XGetWMHints(dpy, c->win)))
  473. return;
  474. wmh->flags &= ~XUrgencyHint;
  475. XSetWMHints(dpy, c->win, wmh);
  476. XFree(wmh);
  477. }
  478. void
  479. configure(Client *c) {
  480. XConfigureEvent ce;
  481. ce.type = ConfigureNotify;
  482. ce.display = dpy;
  483. ce.event = c->win;
  484. ce.window = c->win;
  485. ce.x = c->x;
  486. ce.y = c->y;
  487. ce.width = c->w;
  488. ce.height = c->h;
  489. ce.border_width = c->bw;
  490. ce.above = None;
  491. ce.override_redirect = False;
  492. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  493. }
  494. void
  495. configurenotify(XEvent *e) {
  496. Monitor *m;
  497. XConfigureEvent *ev = &e->xconfigure;
  498. if(ev->window == root && (ev->width != sw || ev->height != sh)) {
  499. sw = ev->width;
  500. sh = ev->height;
  501. updategeom();
  502. if(dc.drawable != 0)
  503. XFreePixmap(dpy, dc.drawable);
  504. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  505. updatebars();
  506. for(m = mons; m; m = m->next)
  507. XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
  508. arrange();
  509. }
  510. }
  511. void
  512. configurerequest(XEvent *e) {
  513. Client *c;
  514. Monitor *m;
  515. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  516. XWindowChanges wc;
  517. if((c = wintoclient(ev->window))) {
  518. if(ev->value_mask & CWBorderWidth)
  519. c->bw = ev->border_width;
  520. else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
  521. m = c->mon;
  522. if(ev->value_mask & CWX)
  523. c->x = m->mx + ev->x;
  524. if(ev->value_mask & CWY)
  525. c->y = m->my + ev->y;
  526. if(ev->value_mask & CWWidth)
  527. c->w = ev->width;
  528. if(ev->value_mask & CWHeight)
  529. c->h = ev->height;
  530. if((c->x - m->mx + c->w) > m->mw && c->isfloating)
  531. c->x = m->mx + (m->mw / 2 - c->w / 2); /* center in x direction */
  532. if((c->y - m->my + c->h) > m->mh && c->isfloating)
  533. c->y = m->my + (m->mh / 2 - c->h / 2); /* center in y direction */
  534. if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
  535. configure(c);
  536. if(ISVISIBLE(c))
  537. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  538. }
  539. else
  540. configure(c);
  541. }
  542. else {
  543. wc.x = ev->x;
  544. wc.y = ev->y;
  545. wc.width = ev->width;
  546. wc.height = ev->height;
  547. wc.border_width = ev->border_width;
  548. wc.sibling = ev->above;
  549. wc.stack_mode = ev->detail;
  550. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  551. }
  552. XSync(dpy, False);
  553. }
  554. void
  555. destroynotify(XEvent *e) {
  556. Client *c;
  557. XDestroyWindowEvent *ev = &e->xdestroywindow;
  558. if((c = wintoclient(ev->window)))
  559. unmanage(c);
  560. }
  561. void
  562. detach(Client *c) {
  563. Client **tc;
  564. for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
  565. *tc = c->next;
  566. }
  567. void
  568. detachstack(Client *c) {
  569. Client **tc, *t;
  570. for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
  571. *tc = c->snext;
  572. if(c == c->mon->sel) {
  573. for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
  574. c->mon->sel = t;
  575. }
  576. }
  577. void
  578. die(const char *errstr, ...) {
  579. va_list ap;
  580. va_start(ap, errstr);
  581. vfprintf(stderr, errstr, ap);
  582. va_end(ap);
  583. exit(EXIT_FAILURE);
  584. }
  585. Monitor *
  586. dirtomon(int dir) {
  587. Monitor *m = NULL;
  588. if(dir > 0) {
  589. if(!(m = selmon->next))
  590. m = mons;
  591. }
  592. else {
  593. if(selmon == mons)
  594. for(m = mons; m->next; m = m->next);
  595. else
  596. for(m = mons; m->next != selmon; m = m->next);
  597. }
  598. return m;
  599. }
  600. void
  601. drawbar(Monitor *m) {
  602. int x;
  603. unsigned int i, n = 0, occ = 0, urg = 0;
  604. unsigned long *col;
  605. Client *c;
  606. for(c = m->clients; c; c = c->next) {
  607. if(ISVISIBLE(c))
  608. n++;
  609. occ |= c->tags;
  610. if(c->isurgent)
  611. urg |= c->tags;
  612. }
  613. dc.x = 0;
  614. for(i = 0; i < LENGTH(tags); i++) {
  615. dc.w = TEXTW(tags[i]);
  616. col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
  617. drawtext(tags[i], col, urg & 1 << i);
  618. drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
  619. occ & 1 << i, urg & 1 << i, col);
  620. dc.x += dc.w;
  621. }
  622. if(blw > 0) {
  623. dc.w = blw;
  624. drawtext(m->lt[m->sellt]->symbol, dc.norm, False);
  625. dc.x += dc.w;
  626. }
  627. snprintf(ntext, sizeof ntext, "%u", n);
  628. dc.w = TEXTW(ntext);
  629. drawtext(ntext, dc.norm, False);
  630. x = (dc.x += dc.w);
  631. if(m == selmon) { /* status is only drawn on selected monitor */
  632. dc.w = TEXTW(stext);
  633. dc.x = m->ww - dc.w;
  634. if(dc.x < x) {
  635. dc.x = x;
  636. dc.w = m->ww - x;
  637. }
  638. drawtext(stext, dc.norm, False);
  639. }
  640. else
  641. dc.x = m->ww;
  642. if((dc.w = dc.x - x) > bh) {
  643. dc.x = x;
  644. if(m->sel) {
  645. col = m == selmon ? dc.sel : dc.norm;
  646. drawtext(m->sel->name, col, False);
  647. drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
  648. }
  649. else
  650. drawtext(NULL, dc.norm, False);
  651. }
  652. XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
  653. XSync(dpy, False);
  654. }
  655. void
  656. drawbars(void) {
  657. Monitor *m;
  658. for(m = mons; m; m = m->next)
  659. drawbar(m);
  660. }
  661. void
  662. drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  663. int x;
  664. XGCValues gcv;
  665. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  666. gcv.foreground = col[invert ? ColBG : ColFG];
  667. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  668. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  669. r.x = dc.x + 1;
  670. r.y = dc.y + 1;
  671. if(filled) {
  672. r.width = r.height = x + 1;
  673. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  674. }
  675. else if(empty) {
  676. r.width = r.height = x;
  677. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  678. }
  679. }
  680. void
  681. drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
  682. char buf[256];
  683. int i, x, y, h, len, olen;
  684. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  685. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  686. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  687. if(!text)
  688. return;
  689. olen = strlen(text);
  690. h = dc.font.ascent + dc.font.descent;
  691. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  692. x = dc.x + (h / 2);
  693. /* shorten text if necessary */
  694. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  695. if(!len)
  696. return;
  697. memcpy(buf, text, len);
  698. if(len < olen)
  699. for(i = len; i && i > len - 3; buf[--i] = '.');
  700. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  701. if(dc.font.set)
  702. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  703. else
  704. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  705. }
  706. void
  707. enternotify(XEvent *e) {
  708. Client *c;
  709. Monitor *m;
  710. XCrossingEvent *ev = &e->xcrossing;
  711. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  712. return;
  713. if((m = wintomon(ev->window)) && m != selmon) {
  714. unfocus(selmon->sel);
  715. selmon = m;
  716. }
  717. if((c = wintoclient(ev->window)))
  718. focus(c);
  719. else
  720. focus(NULL);
  721. }
  722. void
  723. expose(XEvent *e) {
  724. Monitor *m;
  725. XExposeEvent *ev = &e->xexpose;
  726. if(ev->count == 0 && (m = wintomon(ev->window)))
  727. drawbar(m);
  728. }
  729. void
  730. focus(Client *c) {
  731. if(!c || !ISVISIBLE(c))
  732. for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
  733. if(selmon->sel)
  734. unfocus(selmon->sel);
  735. if(c) {
  736. if(c->mon != selmon)
  737. selmon = c->mon;
  738. if(c->isurgent)
  739. clearurgent(c);
  740. detachstack(c);
  741. attachstack(c);
  742. grabbuttons(c, True);
  743. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  744. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  745. }
  746. else
  747. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  748. selmon->sel = c;
  749. drawbars();
  750. }
  751. void
  752. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  753. XFocusChangeEvent *ev = &e->xfocus;
  754. if(selmon->sel && ev->window != selmon->sel->win)
  755. XSetInputFocus(dpy, selmon->sel->win, RevertToPointerRoot, CurrentTime);
  756. }
  757. void
  758. focusmon(const Arg *arg) {
  759. Monitor *m = NULL;
  760. if(!mons->next)
  761. return;
  762. m = dirtomon(arg->i);
  763. unfocus(selmon->sel);
  764. selmon = m;
  765. focus(NULL);
  766. }
  767. void
  768. focusstack(const Arg *arg) {
  769. Client *c = NULL, *i;
  770. if(!selmon->sel)
  771. return;
  772. if(arg->i > 0) {
  773. for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
  774. if(!c)
  775. for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
  776. }
  777. else {
  778. for(i = selmon->clients; i != selmon->sel; i = i->next)
  779. if(ISVISIBLE(i))
  780. c = i;
  781. if(!c)
  782. for(; i; i = i->next)
  783. if(ISVISIBLE(i))
  784. c = i;
  785. }
  786. if(c) {
  787. focus(c);
  788. restack(selmon);
  789. }
  790. }
  791. unsigned long
  792. getcolor(const char *colstr) {
  793. Colormap cmap = DefaultColormap(dpy, screen);
  794. XColor color;
  795. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  796. die("error, cannot allocate color '%s'\n", colstr);
  797. return color.pixel;
  798. }
  799. Bool
  800. getrootptr(int *x, int *y) {
  801. int di;
  802. unsigned int dui;
  803. Window dummy;
  804. return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
  805. }
  806. long
  807. getstate(Window w) {
  808. int format, status;
  809. long result = -1;
  810. unsigned char *p = NULL;
  811. unsigned long n, extra;
  812. Atom real;
  813. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  814. &real, &format, &n, &extra, (unsigned char **)&p);
  815. if(status != Success)
  816. return -1;
  817. if(n != 0)
  818. result = *p;
  819. XFree(p);
  820. return result;
  821. }
  822. Bool
  823. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  824. char **list = NULL;
  825. int n;
  826. XTextProperty name;
  827. if(!text || size == 0)
  828. return False;
  829. text[0] = '\0';
  830. XGetTextProperty(dpy, w, &name, atom);
  831. if(!name.nitems)
  832. return False;
  833. if(name.encoding == XA_STRING)
  834. strncpy(text, (char *)name.value, size - 1);
  835. else {
  836. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
  837. strncpy(text, *list, size - 1);
  838. XFreeStringList(list);
  839. }
  840. }
  841. text[size - 1] = '\0';
  842. XFree(name.value);
  843. return True;
  844. }
  845. void
  846. grabbuttons(Client *c, Bool focused) {
  847. updatenumlockmask();
  848. {
  849. unsigned int i, j;
  850. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  851. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  852. if(focused) {
  853. for(i = 0; i < LENGTH(buttons); i++)
  854. if(buttons[i].click == ClkClientWin)
  855. for(j = 0; j < LENGTH(modifiers); j++)
  856. XGrabButton(dpy, buttons[i].button,
  857. buttons[i].mask | modifiers[j],
  858. c->win, False, BUTTONMASK,
  859. GrabModeAsync, GrabModeSync, None, None);
  860. }
  861. else
  862. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  863. BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  864. }
  865. }
  866. void
  867. grabkeys(void) {
  868. updatenumlockmask();
  869. {
  870. unsigned int i, j;
  871. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  872. KeyCode code;
  873. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  874. for(i = 0; i < LENGTH(keys); i++) {
  875. if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  876. for(j = 0; j < LENGTH(modifiers); j++)
  877. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  878. True, GrabModeAsync, GrabModeAsync);
  879. }
  880. }
  881. }
  882. void
  883. initfont(const char *fontstr) {
  884. char *def, **missing;
  885. int i, n;
  886. missing = NULL;
  887. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  888. if(missing) {
  889. while(n--)
  890. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  891. XFreeStringList(missing);
  892. }
  893. if(dc.font.set) {
  894. XFontSetExtents *font_extents;
  895. XFontStruct **xfonts;
  896. char **font_names;
  897. dc.font.ascent = dc.font.descent = 0;
  898. font_extents = XExtentsOfFontSet(dc.font.set);
  899. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  900. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  901. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  902. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  903. xfonts++;
  904. }
  905. }
  906. else {
  907. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  908. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  909. die("error, cannot load font: '%s'\n", fontstr);
  910. dc.font.ascent = dc.font.xfont->ascent;
  911. dc.font.descent = dc.font.xfont->descent;
  912. }
  913. dc.font.height = dc.font.ascent + dc.font.descent;
  914. }
  915. Bool
  916. isprotodel(Client *c) {
  917. int i, n;
  918. Atom *protocols;
  919. Bool ret = False;
  920. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  921. for(i = 0; !ret && i < n; i++)
  922. if(protocols[i] == wmatom[WMDelete])
  923. ret = True;
  924. XFree(protocols);
  925. }
  926. return ret;
  927. }
  928. void
  929. keypress(XEvent *e) {
  930. unsigned int i;
  931. KeySym keysym;
  932. XKeyEvent *ev;
  933. ev = &e->xkey;
  934. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  935. for(i = 0; i < LENGTH(keys); i++)
  936. if(keysym == keys[i].keysym
  937. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  938. && keys[i].func)
  939. keys[i].func(&(keys[i].arg));
  940. }
  941. void
  942. killclient(const Arg *arg) {
  943. XEvent ev;
  944. if(!selmon->sel)
  945. return;
  946. if(isprotodel(selmon->sel)) {
  947. ev.type = ClientMessage;
  948. ev.xclient.window = selmon->sel->win;
  949. ev.xclient.message_type = wmatom[WMProtocols];
  950. ev.xclient.format = 32;
  951. ev.xclient.data.l[0] = wmatom[WMDelete];
  952. ev.xclient.data.l[1] = CurrentTime;
  953. XSendEvent(dpy, selmon->sel->win, False, NoEventMask, &ev);
  954. }
  955. else
  956. XKillClient(dpy, selmon->sel->win);
  957. }
  958. void
  959. manage(Window w, XWindowAttributes *wa) {
  960. static Client cz;
  961. Client *c, *t = NULL;
  962. Window trans = None;
  963. XWindowChanges wc;
  964. if(!(c = malloc(sizeof(Client))))
  965. die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  966. *c = cz;
  967. c->win = w;
  968. updatetitle(c);
  969. if(XGetTransientForHint(dpy, w, &trans))
  970. t = wintoclient(trans);
  971. if(t) {
  972. c->mon = t->mon;
  973. c->tags = t->tags;
  974. }
  975. else {
  976. c->mon = selmon;
  977. applyrules(c);
  978. }
  979. /* geometry */
  980. c->x = wa->x + c->mon->wx;
  981. c->y = wa->y + c->mon->wy;
  982. c->w = wa->width;
  983. c->h = wa->height;
  984. c->oldbw = wa->border_width;
  985. if(c->w == c->mon->mw && c->h == c->mon->mh) {
  986. c->x = c->mon->mx;
  987. c->y = c->mon->my;
  988. c->bw = 0;
  989. }
  990. else {
  991. if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  992. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  993. if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  994. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  995. c->x = MAX(c->x, c->mon->mx);
  996. /* only fix client y-offset, if the client center might cover the bar */
  997. c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
  998. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  999. c->bw = borderpx;
  1000. }
  1001. wc.border_width = c->bw;
  1002. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  1003. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  1004. configure(c); /* propagates border_width, if size doesn't change */
  1005. updatesizehints(c);
  1006. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  1007. grabbuttons(c, False);
  1008. if(!c->isfloating)
  1009. c->isfloating = trans != None || c->isfixed;
  1010. if(c->isfloating)
  1011. XRaiseWindow(dpy, c->win);
  1012. attach(c);
  1013. attachstack(c);
  1014. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  1015. XMapWindow(dpy, c->win);
  1016. setclientstate(c, NormalState);
  1017. arrange();
  1018. }
  1019. void
  1020. mappingnotify(XEvent *e) {
  1021. XMappingEvent *ev = &e->xmapping;
  1022. XRefreshKeyboardMapping(ev);
  1023. if(ev->request == MappingKeyboard)
  1024. grabkeys();
  1025. }
  1026. void
  1027. maprequest(XEvent *e) {
  1028. static XWindowAttributes wa;
  1029. XMapRequestEvent *ev = &e->xmaprequest;
  1030. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1031. return;
  1032. if(wa.override_redirect)
  1033. return;
  1034. if(!wintoclient(ev->window))
  1035. manage(ev->window, &wa);
  1036. }
  1037. void
  1038. monocle(Monitor *m) {
  1039. Client *c;
  1040. for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1041. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
  1042. }
  1043. void
  1044. movemouse(const Arg *arg) {
  1045. int x, y, ocx, ocy, nx, ny;
  1046. Client *c;
  1047. Monitor *m;
  1048. XEvent ev;
  1049. if(!(c = selmon->sel))
  1050. return;
  1051. restack(selmon);
  1052. ocx = c->x;
  1053. ocy = c->y;
  1054. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1055. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1056. return;
  1057. if(!getrootptr(&x, &y))
  1058. return;
  1059. do {
  1060. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1061. switch (ev.type) {
  1062. case ConfigureRequest:
  1063. case Expose:
  1064. case MapRequest:
  1065. handler[ev.type](&ev);
  1066. break;
  1067. case MotionNotify:
  1068. nx = ocx + (ev.xmotion.x - x);
  1069. ny = ocy + (ev.xmotion.y - y);
  1070. if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1071. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1072. if(abs(selmon->wx - nx) < snap)
  1073. nx = selmon->wx;
  1074. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1075. nx = selmon->wx + selmon->ww - WIDTH(c);
  1076. if(abs(selmon->wy - ny) < snap)
  1077. ny = selmon->wy;
  1078. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1079. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1080. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1081. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1082. togglefloating(NULL);
  1083. }
  1084. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1085. resize(c, nx, ny, c->w, c->h, True);
  1086. break;
  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. {
  1189. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1190. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1191. togglefloating(NULL);
  1192. }
  1193. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1194. resize(c, c->x, c->y, nw, nh, True);
  1195. break;
  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 / size.min_aspect.x;
  1659. c->maxa = (float)size.max_aspect.x / 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. if(c->name[0] == '\0') /* hack to mark broken clients */
  1671. strcpy(c->name, broken);
  1672. }
  1673. void
  1674. updatestatus(void) {
  1675. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1676. strcpy(stext, "dwm-"VERSION);
  1677. drawbar(selmon);
  1678. }
  1679. void
  1680. updatewmhints(Client *c) {
  1681. XWMHints *wmh;
  1682. if((wmh = XGetWMHints(dpy, c->win))) {
  1683. if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  1684. wmh->flags &= ~XUrgencyHint;
  1685. XSetWMHints(dpy, c->win, wmh);
  1686. }
  1687. else
  1688. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1689. XFree(wmh);
  1690. }
  1691. }
  1692. void
  1693. view(const Arg *arg) {
  1694. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1695. return;
  1696. selmon->seltags ^= 1; /* toggle sel tagset */
  1697. if(arg->ui & TAGMASK)
  1698. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1699. arrange();
  1700. }
  1701. Client *
  1702. wintoclient(Window w) {
  1703. Client *c;
  1704. Monitor *m;
  1705. for(m = mons; m; m = m->next)
  1706. for(c = m->clients; c; c = c->next)
  1707. if(c->win == w)
  1708. return c;
  1709. return NULL;
  1710. }
  1711. Monitor *
  1712. wintomon(Window w) {
  1713. int x, y;
  1714. Client *c;
  1715. Monitor *m;
  1716. if(w == root && getrootptr(&x, &y))
  1717. return ptrtomon(x, y);
  1718. for(m = mons; m; m = m->next)
  1719. if(w == m->barwin)
  1720. return m;
  1721. if((c = wintoclient(w)))
  1722. return c->mon;
  1723. return selmon;
  1724. }
  1725. /* There's no way to check accesses to destroyed windows, thus those cases are
  1726. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1727. * default error handler, which may call exit. */
  1728. int
  1729. xerror(Display *dpy, XErrorEvent *ee) {
  1730. if(ee->error_code == BadWindow
  1731. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1732. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1733. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1734. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1735. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1736. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1737. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1738. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1739. return 0;
  1740. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1741. ee->request_code, ee->error_code);
  1742. return xerrorxlib(dpy, ee); /* may call exit */
  1743. }
  1744. int
  1745. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1746. return 0;
  1747. }
  1748. /* Startup Error handler to check if another window manager
  1749. * is already running. */
  1750. int
  1751. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1752. otherwm = True;
  1753. return -1;
  1754. }
  1755. void
  1756. zoom(const Arg *arg) {
  1757. Client *c = selmon->sel;
  1758. if(!selmon->lt[selmon->sellt]->arrange
  1759. || selmon->lt[selmon->sellt]->arrange == monocle
  1760. || (selmon->sel && selmon->sel->isfloating))
  1761. return;
  1762. if(c == nexttiled(selmon->clients))
  1763. if(!c || !(c = nexttiled(c->next)))
  1764. return;
  1765. detach(c);
  1766. attach(c);
  1767. focus(c);
  1768. arrange();
  1769. }
  1770. int
  1771. main(int argc, char *argv[]) {
  1772. if(argc == 2 && !strcmp("-v", argv[1]))
  1773. die("dwm-"VERSION", © 2006-2009 dwm engineers, see LICENSE for details\n");
  1774. else if(argc != 1)
  1775. die("usage: dwm [-v]\n");
  1776. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1777. fputs("warning: no locale support\n", stderr);
  1778. if(!(dpy = XOpenDisplay(NULL)))
  1779. die("dwm: cannot open display\n");
  1780. checkotherwm();
  1781. setup();
  1782. scan();
  1783. run();
  1784. cleanup();
  1785. XCloseDisplay(dpy);
  1786. return 0;
  1787. }