dwm.c 46 KB

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