dwm.c 47 KB

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