dwm.c 41 KB

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