dwm.c 47 KB

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