dwm.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938
  1. #define XINULATOR /* debug, simulates dual head */
  2. /* See LICENSE file for copyright and license details.
  3. *
  4. * dynamic window manager is designed like any other X client as well. It is
  5. * driven through handling X events. In contrast to other X clients, a window
  6. * manager selects for SubstructureRedirectMask on the root window, to receive
  7. * events about window (dis-)appearance. Only one X connection at a time is
  8. * allowed to select for this event mask.
  9. *
  10. * The event handlers of dwm are organized in an array which is accessed
  11. * whenever a new event has been fetched. This allows event dispatching
  12. * in O(1) time.
  13. *
  14. * Each child of the root window is called a client, except windows which have
  15. * set the override_redirect flag. Clients are organized in a global
  16. * linked client list, the focus history is remembered through a global
  17. * stack list. Each client contains a bit array to indicate the tags of a
  18. * client.
  19. *
  20. * Keys and tagging rules are organized as arrays and defined in config.h.
  21. *
  22. * To understand everything else, start reading main().
  23. */
  24. #include <errno.h>
  25. #include <locale.h>
  26. #include <stdarg.h>
  27. #include <signal.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <sys/types.h>
  33. #include <sys/wait.h>
  34. #include <X11/cursorfont.h>
  35. #include <X11/keysym.h>
  36. #include <X11/Xatom.h>
  37. #include <X11/Xlib.h>
  38. #include <X11/Xproto.h>
  39. #include <X11/Xutil.h>
  40. #ifdef XINERAMA
  41. #include <X11/extensions/Xinerama.h>
  42. #endif /* XINERAMA */
  43. /* macros */
  44. #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
  45. #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
  46. #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
  47. #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
  48. #define LENGTH(X) (sizeof X / sizeof X[0])
  49. #define MAX(A, B) ((A) > (B) ? (A) : (B))
  50. #define MIN(A, B) ((A) < (B) ? (A) : (B))
  51. #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
  52. #define WIDTH(X) ((X)->w + 2 * (X)->bw)
  53. #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
  54. #define TAGMASK ((int)((1LL << LENGTH(tags)) - 1))
  55. #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
  56. /* enums */
  57. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  58. enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
  59. enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
  60. enum { WMProtocols, WMDelete, WMState, WMLast }; /* default atoms */
  61. enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
  62. ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
  63. typedef union {
  64. int i;
  65. unsigned int ui;
  66. float f;
  67. void *v;
  68. } Arg;
  69. typedef struct {
  70. unsigned int click;
  71. unsigned int mask;
  72. unsigned int button;
  73. void (*func)(const Arg *arg);
  74. const Arg arg;
  75. } Button;
  76. typedef struct Monitor Monitor;
  77. typedef struct Client Client;
  78. struct Client {
  79. char name[256];
  80. float mina, maxa;
  81. int x, y, w, h;
  82. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  83. int bw, oldbw;
  84. unsigned int tags;
  85. Bool isfixed, isfloating, isurgent;
  86. Client *next;
  87. Client *snext;
  88. Monitor *mon;
  89. Window win;
  90. };
  91. typedef struct {
  92. int x, y, w, h;
  93. unsigned long norm[ColLast];
  94. unsigned long sel[ColLast];
  95. Drawable drawable;
  96. GC gc;
  97. struct {
  98. int ascent;
  99. int descent;
  100. int height;
  101. XFontSet set;
  102. XFontStruct *xfont;
  103. } font;
  104. } DC; /* draw context */
  105. typedef struct {
  106. unsigned int mod;
  107. KeySym keysym;
  108. void (*func)(const Arg *);
  109. const Arg arg;
  110. } Key;
  111. typedef struct {
  112. const char *symbol;
  113. void (*arrange)(Monitor *);
  114. } Layout;
  115. struct Monitor {
  116. int screen_number;
  117. float mfact;
  118. int by, btx; /* bar geometry */
  119. int my, mh; /* vertical screen size*/
  120. int wx, wy, ww, wh; /* window area */
  121. unsigned int seltags;
  122. unsigned int sellt;
  123. unsigned int tagset[2];
  124. Bool showbar;
  125. Bool topbar;
  126. Client *clients;
  127. Client *sel;
  128. Client *stack;
  129. Monitor *next;
  130. Window barwin;
  131. };
  132. typedef struct {
  133. const char *class;
  134. const char *instance;
  135. const char *title;
  136. unsigned int tags;
  137. Bool isfloating;
  138. } Rule;
  139. /* function declarations */
  140. static void applyrules(Client *c);
  141. static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h);
  142. static void arrange(void);
  143. static void attach(Client *c);
  144. static void attachstack(Client *c);
  145. static void buttonpress(XEvent *e);
  146. static void checkotherwm(void);
  147. static void cleanup(void);
  148. static void cleanupmons(void);
  149. static void clearurgent(Client *c);
  150. static void configure(Client *c);
  151. static void configurenotify(XEvent *e);
  152. static void configurerequest(XEvent *e);
  153. static void destroynotify(XEvent *e);
  154. static void detach(Client *c);
  155. static void detachstack(Client *c);
  156. static void die(const char *errstr, ...);
  157. static void drawbar(Monitor *m);
  158. static void drawbars();
  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 focusstack(const Arg *arg);
  166. static Client *getclient(Window w);
  167. static unsigned long getcolor(const char *colstr);
  168. static long getstate(Window w);
  169. static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  170. static void grabbuttons(Client *c, Bool focused);
  171. static void grabkeys(void);
  172. static void initfont(const char *fontstr);
  173. static Bool isprotodel(Client *c);
  174. static void keypress(XEvent *e);
  175. static void killclient(const Arg *arg);
  176. static void manage(Window w, XWindowAttributes *wa);
  177. static void mappingnotify(XEvent *e);
  178. static void maprequest(XEvent *e);
  179. static void monocle(Monitor *m);
  180. static void movemouse(const Arg *arg);
  181. static Client *nexttiled(Client *c);
  182. static void propertynotify(XEvent *e);
  183. static void quit(const Arg *arg);
  184. static void resize(Client *c, int x, int y, int w, int h);
  185. static void resizemouse(const Arg *arg);
  186. static void restack(Monitor *m);
  187. static void run(void);
  188. static void scan(void);
  189. static void setclientstate(Client *c, long state);
  190. static void setlayout(const Arg *arg);
  191. static void setmfact(const Arg *arg);
  192. static void setup(void);
  193. static void showhide(Client *c);
  194. static void sigchld(int signal);
  195. static void spawn(const Arg *arg);
  196. static void tag(const Arg *arg);
  197. static int textnw(const char *text, unsigned int len);
  198. static void tile(Monitor *);
  199. static void togglebar(const Arg *arg);
  200. static void togglefloating(const Arg *arg);
  201. static void toggletag(const Arg *arg);
  202. static void toggleview(const Arg *arg);
  203. static void unmanage(Client *c);
  204. static void unmapnotify(XEvent *e);
  205. static void updategeom(void);
  206. static void updatebarpos(Monitor *m);
  207. static void updatebars(void);
  208. static void updatenumlockmask(void);
  209. static void updatesizehints(Client *c);
  210. static void updatestatus(void);
  211. static void updatetitle(Client *c);
  212. static void updatewmhints(Client *c);
  213. static void view(const Arg *arg);
  214. static int xerror(Display *dpy, XErrorEvent *ee);
  215. static int xerrordummy(Display *dpy, XErrorEvent *ee);
  216. static int xerrorstart(Display *dpy, XErrorEvent *ee);
  217. static void zoom(const Arg *arg);
  218. #ifdef XINERAMA
  219. static void focusmon(const Arg *arg);
  220. static void tagmon(const Arg *arg);
  221. #endif /* XINERAMA */
  222. /* variables */
  223. static char stext[256];
  224. static int screen;
  225. static int sx, sy, sw, sh; /* X display screen geometry x, y, width, height */
  226. static int bh, blw = 0; /* bar geometry */
  227. static int (*xerrorxlib)(Display *, XErrorEvent *);
  228. static unsigned int numlockmask = 0;
  229. static void (*handler[LASTEvent]) (XEvent *) = {
  230. [ButtonPress] = buttonpress,
  231. [ConfigureRequest] = configurerequest,
  232. [ConfigureNotify] = configurenotify,
  233. [DestroyNotify] = destroynotify,
  234. [EnterNotify] = enternotify,
  235. [Expose] = expose,
  236. [FocusIn] = focusin,
  237. [KeyPress] = keypress,
  238. [MappingNotify] = mappingnotify,
  239. [MapRequest] = maprequest,
  240. [PropertyNotify] = propertynotify,
  241. [UnmapNotify] = unmapnotify
  242. };
  243. static Atom wmatom[WMLast], netatom[NetLast];
  244. static Bool otherwm;
  245. static Bool running = True;
  246. static Cursor cursor[CurLast];
  247. static Display *dpy;
  248. static DC dc;
  249. static Layout *lt[] = { NULL, NULL };
  250. static Monitor *mons = NULL, *selmon = NULL;
  251. static Window root;
  252. /* configuration, allows nested code to access above variables */
  253. #include "config.h"
  254. /* compile-time check if all tags fit into an unsigned int bit array. */
  255. struct NumTags { char limitexceeded[sizeof(unsigned int) * 8 < LENGTH(tags) ? -1 : 1]; };
  256. /* function implementations */
  257. void
  258. applyrules(Client *c) {
  259. unsigned int i;
  260. Rule *r;
  261. XClassHint ch = { 0 };
  262. /* rule matching */
  263. c->isfloating = c->tags = 0;
  264. if(XGetClassHint(dpy, c->win, &ch)) {
  265. for(i = 0; i < LENGTH(rules); i++) {
  266. r = &rules[i];
  267. if((!r->title || strstr(c->name, r->title))
  268. && (!r->class || (ch.res_class && strstr(ch.res_class, r->class)))
  269. && (!r->instance || (ch.res_name && strstr(ch.res_name, r->instance)))) {
  270. c->isfloating = r->isfloating;
  271. c->tags |= r->tags;
  272. }
  273. }
  274. if(ch.res_class)
  275. XFree(ch.res_class);
  276. if(ch.res_name)
  277. XFree(ch.res_name);
  278. }
  279. c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
  280. }
  281. Bool
  282. applysizehints(Client *c, int *x, int *y, int *w, int *h) {
  283. Bool baseismin;
  284. /* set minimum possible */
  285. *w = MAX(1, *w);
  286. *h = MAX(1, *h);
  287. if(*x > sx + sw)
  288. *x = sw - WIDTH(c);
  289. if(*y > sy + sh)
  290. *y = sh - HEIGHT(c);
  291. if(*x + *w + 2 * c->bw < sx)
  292. *x = sx;
  293. if(*y + *h + 2 * c->bw < sy)
  294. *y = sy;
  295. if(*h < bh)
  296. *h = bh;
  297. if(*w < bh)
  298. *w = bh;
  299. if(resizehints || c->isfloating) {
  300. /* see last two sentences in ICCCM 4.1.2.3 */
  301. baseismin = c->basew == c->minw && c->baseh == c->minh;
  302. if(!baseismin) { /* temporarily remove base dimensions */
  303. *w -= c->basew;
  304. *h -= c->baseh;
  305. }
  306. /* adjust for aspect limits */
  307. if(c->mina > 0 && c->maxa > 0) {
  308. if(c->maxa < (float)*w / *h)
  309. *w = *h * c->maxa;
  310. else if(c->mina < (float)*h / *w)
  311. *h = *w * c->mina;
  312. }
  313. if(baseismin) { /* increment calculation requires this */
  314. *w -= c->basew;
  315. *h -= c->baseh;
  316. }
  317. /* adjust for increment value */
  318. if(c->incw)
  319. *w -= *w % c->incw;
  320. if(c->inch)
  321. *h -= *h % c->inch;
  322. /* restore base dimensions */
  323. *w += c->basew;
  324. *h += c->baseh;
  325. *w = MAX(*w, c->minw);
  326. *h = MAX(*h, c->minh);
  327. if(c->maxw)
  328. *w = MIN(*w, c->maxw);
  329. if(c->maxh)
  330. *h = MIN(*h, c->maxh);
  331. }
  332. return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
  333. }
  334. void
  335. arrange(void) {
  336. Monitor *m;
  337. /* optimise two loops into one, check focus(NULL) */
  338. for(m = mons; m; m = m->next)
  339. showhide(m->stack);
  340. focus(NULL);
  341. for(m = mons; m; m = m->next) {
  342. if(lt[m->sellt]->arrange)
  343. lt[m->sellt]->arrange(m);
  344. restack(m);
  345. }
  346. }
  347. void
  348. attach(Client *c) {
  349. c->next = c->mon->clients;
  350. c->mon->clients = c;
  351. }
  352. void
  353. attachstack(Client *c) {
  354. c->snext = c->mon->stack;
  355. c->mon->stack = c;
  356. }
  357. void
  358. buttonpress(XEvent *e) {
  359. unsigned int i, x, click;
  360. Arg arg = {0};
  361. Client *c;
  362. XButtonPressedEvent *ev = &e->xbutton;
  363. click = ClkRootWin;
  364. if(ev->window == selmon->barwin && ev->x >= selmon->btx) {
  365. i = 0;
  366. x = selmon->btx;
  367. do
  368. x += TEXTW(tags[i]);
  369. while(ev->x >= x && ++i < LENGTH(tags));
  370. if(i < LENGTH(tags)) {
  371. click = ClkTagBar;
  372. arg.ui = 1 << i;
  373. }
  374. else if(ev->x < x + blw)
  375. click = ClkLtSymbol;
  376. else if(ev->x > selmon->wx + selmon->ww - TEXTW(stext))
  377. click = ClkStatusText;
  378. else
  379. click = ClkWinTitle;
  380. }
  381. else if((c = getclient(ev->window))) {
  382. focus(c);
  383. click = ClkClientWin;
  384. }
  385. for(i = 0; i < LENGTH(buttons); i++)
  386. if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
  387. && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
  388. buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
  389. }
  390. void
  391. checkotherwm(void) {
  392. otherwm = False;
  393. xerrorxlib = XSetErrorHandler(xerrorstart);
  394. /* this causes an error if some other window manager is running */
  395. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  396. XSync(dpy, False);
  397. if(otherwm)
  398. die("dwm: another window manager is already running\n");
  399. XSetErrorHandler(xerror);
  400. XSync(dpy, False);
  401. }
  402. void
  403. cleanup(void) {
  404. Arg a = {.ui = ~0};
  405. Layout foo = { "", NULL };
  406. Monitor *m;
  407. view(&a);
  408. lt[selmon->sellt] = &foo;
  409. /* TODO: consider simplifying cleanup code of the stack, perhaps do that in cleanmons() ? */
  410. for(m = mons; m; m = m->next)
  411. while(m->stack)
  412. unmanage(m->stack);
  413. if(dc.font.set)
  414. XFreeFontSet(dpy, dc.font.set);
  415. else
  416. XFreeFont(dpy, dc.font.xfont);
  417. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  418. XFreePixmap(dpy, dc.drawable);
  419. XFreeGC(dpy, dc.gc);
  420. XFreeCursor(dpy, cursor[CurNormal]);
  421. XFreeCursor(dpy, cursor[CurResize]);
  422. XFreeCursor(dpy, cursor[CurMove]);
  423. cleanupmons();
  424. XSync(dpy, False);
  425. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  426. }
  427. void
  428. cleanupmons(void) {
  429. Monitor *m;
  430. while(mons) {
  431. m = mons->next;
  432. XUnmapWindow(dpy, mons->barwin);
  433. XDestroyWindow(dpy, mons->barwin);
  434. free(mons);
  435. mons = m;
  436. }
  437. }
  438. void
  439. clearurgent(Client *c) {
  440. XWMHints *wmh;
  441. c->isurgent = False;
  442. if(!(wmh = XGetWMHints(dpy, c->win)))
  443. return;
  444. wmh->flags &= ~XUrgencyHint;
  445. XSetWMHints(dpy, c->win, wmh);
  446. XFree(wmh);
  447. }
  448. void
  449. configure(Client *c) {
  450. XConfigureEvent ce;
  451. ce.type = ConfigureNotify;
  452. ce.display = dpy;
  453. ce.event = c->win;
  454. ce.window = c->win;
  455. ce.x = c->x;
  456. ce.y = c->y;
  457. ce.width = c->w;
  458. ce.height = c->h;
  459. ce.border_width = c->bw;
  460. ce.above = None;
  461. ce.override_redirect = False;
  462. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  463. }
  464. void
  465. configurenotify(XEvent *e) {
  466. Monitor *m;
  467. XConfigureEvent *ev = &e->xconfigure;
  468. if(ev->window == root && (ev->width != sw || ev->height != sh)) {
  469. sw = ev->width;
  470. sh = ev->height;
  471. updategeom();
  472. if(dc.drawable != 0)
  473. XFreePixmap(dpy, dc.drawable);
  474. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  475. updatebars();
  476. for(m = mons; m; m = m->next)
  477. XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
  478. arrange();
  479. }
  480. }
  481. void
  482. configurerequest(XEvent *e) {
  483. Client *c;
  484. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  485. XWindowChanges wc;
  486. if((c = getclient(ev->window))) {
  487. if(ev->value_mask & CWBorderWidth)
  488. c->bw = ev->border_width;
  489. else if(c->isfloating || !lt[selmon->sellt]->arrange) {
  490. if(ev->value_mask & CWX)
  491. c->x = sx + ev->x;
  492. if(ev->value_mask & CWY)
  493. c->y = sy + ev->y;
  494. if(ev->value_mask & CWWidth)
  495. c->w = ev->width;
  496. if(ev->value_mask & CWHeight)
  497. c->h = ev->height;
  498. if((c->x - sx + c->w) > sw && c->isfloating)
  499. c->x = sx + (sw / 2 - c->w / 2); /* center in x direction */
  500. if((c->y - sy + c->h) > sh && c->isfloating)
  501. c->y = sy + (sh / 2 - c->h / 2); /* center in y direction */
  502. if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
  503. configure(c);
  504. if(ISVISIBLE(c))
  505. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  506. }
  507. else
  508. configure(c);
  509. }
  510. else {
  511. wc.x = ev->x;
  512. wc.y = ev->y;
  513. wc.width = ev->width;
  514. wc.height = ev->height;
  515. wc.border_width = ev->border_width;
  516. wc.sibling = ev->above;
  517. wc.stack_mode = ev->detail;
  518. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  519. }
  520. XSync(dpy, False);
  521. }
  522. void
  523. destroynotify(XEvent *e) {
  524. Client *c;
  525. XDestroyWindowEvent *ev = &e->xdestroywindow;
  526. if((c = getclient(ev->window)))
  527. unmanage(c);
  528. }
  529. void
  530. detach(Client *c) {
  531. Client **tc;
  532. for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
  533. *tc = c->next;
  534. }
  535. void
  536. detachstack(Client *c) {
  537. Client **tc;
  538. for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
  539. *tc = c->snext;
  540. }
  541. void
  542. die(const char *errstr, ...) {
  543. va_list ap;
  544. va_start(ap, errstr);
  545. vfprintf(stderr, errstr, ap);
  546. va_end(ap);
  547. exit(EXIT_FAILURE);
  548. }
  549. void
  550. drawbar(Monitor *m) {
  551. int x;
  552. unsigned int i, occ = 0, urg = 0;
  553. unsigned long *col;
  554. Client *c;
  555. for(c = m->clients; c; c = c->next) {
  556. occ |= c->tags;
  557. if(c->isurgent)
  558. urg |= c->tags;
  559. }
  560. dc.x = 0;
  561. #ifdef XINERAMA
  562. {
  563. char buf[2];
  564. buf[0] = m->screen_number + '0';
  565. buf[1] = '\0';
  566. dc.w = TEXTW(buf);
  567. drawtext(buf, selmon == m ? dc.sel : dc.norm, True);
  568. dc.x += dc.w;
  569. }
  570. #endif /* XINERAMA */
  571. m->btx = dc.x;
  572. for(i = 0; i < LENGTH(tags); i++) {
  573. dc.w = TEXTW(tags[i]);
  574. col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
  575. drawtext(tags[i], col, urg & 1 << i);
  576. drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
  577. occ & 1 << i, urg & 1 << i, col);
  578. dc.x += dc.w;
  579. }
  580. if(blw > 0) {
  581. dc.w = blw;
  582. drawtext(lt[m->sellt]->symbol, dc.norm, False);
  583. x = dc.x + dc.w;
  584. }
  585. else
  586. x = dc.x;
  587. if(m == selmon) {
  588. dc.w = TEXTW(stext);
  589. dc.x = m->ww - dc.w;
  590. if(dc.x < x) {
  591. dc.x = x;
  592. dc.w = m->ww - x;
  593. }
  594. drawtext(stext, dc.norm, False);
  595. if((dc.w = dc.x - x) > bh) {
  596. dc.x = x;
  597. if(selmon->sel) {
  598. drawtext(selmon->sel->name, dc.sel, False);
  599. drawsquare(selmon->sel->isfixed, selmon->sel->isfloating, False, dc.sel);
  600. }
  601. else
  602. drawtext(NULL, dc.norm, False);
  603. }
  604. }
  605. else {
  606. dc.x = x;
  607. dc.w = m->ww - x;
  608. drawtext(NULL, dc.norm, False);
  609. }
  610. XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
  611. XSync(dpy, False);
  612. }
  613. void
  614. drawbars() {
  615. Monitor *m;
  616. for(m = mons; m; m = m->next)
  617. drawbar(m);
  618. }
  619. void
  620. drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  621. int x;
  622. XGCValues gcv;
  623. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  624. gcv.foreground = col[invert ? ColBG : ColFG];
  625. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  626. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  627. r.x = dc.x + 1;
  628. r.y = dc.y + 1;
  629. if(filled) {
  630. r.width = r.height = x + 1;
  631. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  632. }
  633. else if(empty) {
  634. r.width = r.height = x;
  635. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  636. }
  637. }
  638. void
  639. drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
  640. char buf[256];
  641. int i, x, y, h, len, olen;
  642. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  643. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  644. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  645. if(!text)
  646. return;
  647. olen = strlen(text);
  648. h = dc.font.ascent + dc.font.descent;
  649. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  650. x = dc.x + (h / 2);
  651. /* shorten text if necessary */
  652. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  653. if(!len)
  654. return;
  655. memcpy(buf, text, len);
  656. if(len < olen)
  657. for(i = len; i && i > len - 3; buf[--i] = '.');
  658. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  659. if(dc.font.set)
  660. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  661. else
  662. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  663. }
  664. void
  665. enternotify(XEvent *e) {
  666. Client *c;
  667. XCrossingEvent *ev = &e->xcrossing;
  668. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  669. return;
  670. if((c = getclient(ev->window)))
  671. focus(c);
  672. else
  673. focus(NULL);
  674. }
  675. void
  676. expose(XEvent *e) {
  677. Monitor *m;
  678. XExposeEvent *ev = &e->xexpose;
  679. if(ev->count == 0)
  680. for(m = mons; m; m = m->next)
  681. if(ev->window == m->barwin) {
  682. drawbar(m);
  683. break;
  684. }
  685. }
  686. void
  687. focus(Client *c) {
  688. if(!c || !ISVISIBLE(c))
  689. for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
  690. if(selmon->sel && selmon->sel != c) {
  691. grabbuttons(selmon->sel, False);
  692. XSetWindowBorder(dpy, selmon->sel->win, dc.norm[ColBorder]);
  693. }
  694. if(c) {
  695. if(c->isurgent)
  696. clearurgent(c);
  697. detachstack(c);
  698. attachstack(c);
  699. grabbuttons(c, True);
  700. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  701. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  702. }
  703. else
  704. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  705. selmon->sel = c;
  706. drawbars();
  707. }
  708. void
  709. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  710. XFocusChangeEvent *ev = &e->xfocus;
  711. if(selmon->sel && ev->window != selmon->sel->win)
  712. XSetInputFocus(dpy, selmon->sel->win, RevertToPointerRoot, CurrentTime);
  713. }
  714. #ifdef XINERAMA
  715. void
  716. focusmon(const Arg *arg) {
  717. unsigned int i;
  718. Monitor *m;
  719. for(i = 0, m = mons; m; m = m->next, i++)
  720. if(i == arg->ui) {
  721. selmon = m;
  722. focus(NULL);
  723. drawbars();
  724. break;
  725. }
  726. }
  727. #endif /* XINERAMA */
  728. void
  729. focusstack(const Arg *arg) {
  730. Client *c = NULL, *i;
  731. if(!selmon->sel)
  732. return;
  733. if(arg->i > 0) {
  734. for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
  735. if(!c)
  736. for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
  737. }
  738. else {
  739. for(i = selmon->clients; i != selmon->sel; i = i->next)
  740. if(ISVISIBLE(i))
  741. c = i;
  742. if(!c)
  743. for(; i; i = i->next)
  744. if(ISVISIBLE(i))
  745. c = i;
  746. }
  747. if(c) {
  748. focus(c);
  749. restack(selmon);
  750. }
  751. }
  752. Client *
  753. getclient(Window w) {
  754. Client *c = NULL;
  755. Monitor *m;
  756. for(m = mons; m; m = m->next)
  757. for(c = m->clients; c && c->win != w; c = c->next);
  758. return c;
  759. }
  760. unsigned long
  761. getcolor(const char *colstr) {
  762. Colormap cmap = DefaultColormap(dpy, screen);
  763. XColor color;
  764. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  765. die("error, cannot allocate color '%s'\n", colstr);
  766. return color.pixel;
  767. }
  768. long
  769. getstate(Window w) {
  770. int format, status;
  771. long result = -1;
  772. unsigned char *p = NULL;
  773. unsigned long n, extra;
  774. Atom real;
  775. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  776. &real, &format, &n, &extra, (unsigned char **)&p);
  777. if(status != Success)
  778. return -1;
  779. if(n != 0)
  780. result = *p;
  781. XFree(p);
  782. return result;
  783. }
  784. Bool
  785. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  786. char **list = NULL;
  787. int n;
  788. XTextProperty name;
  789. if(!text || size == 0)
  790. return False;
  791. text[0] = '\0';
  792. XGetTextProperty(dpy, w, &name, atom);
  793. if(!name.nitems)
  794. return False;
  795. if(name.encoding == XA_STRING)
  796. strncpy(text, (char *)name.value, size - 1);
  797. else {
  798. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  799. && n > 0 && *list) {
  800. strncpy(text, *list, size - 1);
  801. XFreeStringList(list);
  802. }
  803. }
  804. text[size - 1] = '\0';
  805. XFree(name.value);
  806. return True;
  807. }
  808. void
  809. grabbuttons(Client *c, Bool focused) {
  810. updatenumlockmask();
  811. {
  812. unsigned int i, j;
  813. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  814. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  815. if(focused) {
  816. for(i = 0; i < LENGTH(buttons); i++)
  817. if(buttons[i].click == ClkClientWin)
  818. for(j = 0; j < LENGTH(modifiers); j++)
  819. XGrabButton(dpy, buttons[i].button,
  820. buttons[i].mask | modifiers[j],
  821. c->win, False, BUTTONMASK,
  822. GrabModeAsync, GrabModeSync, None, None);
  823. } else
  824. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  825. BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  826. }
  827. }
  828. void
  829. grabkeys(void) {
  830. updatenumlockmask();
  831. { /* grab keys */
  832. unsigned int i, j;
  833. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  834. KeyCode code;
  835. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  836. for(i = 0; i < LENGTH(keys); i++) {
  837. if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  838. for(j = 0; j < LENGTH(modifiers); j++)
  839. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  840. True, GrabModeAsync, GrabModeAsync);
  841. }
  842. }
  843. }
  844. void
  845. initfont(const char *fontstr) {
  846. char *def, **missing;
  847. int i, n;
  848. missing = NULL;
  849. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  850. if(missing) {
  851. while(n--)
  852. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  853. XFreeStringList(missing);
  854. }
  855. if(dc.font.set) {
  856. XFontSetExtents *font_extents;
  857. XFontStruct **xfonts;
  858. char **font_names;
  859. dc.font.ascent = dc.font.descent = 0;
  860. font_extents = XExtentsOfFontSet(dc.font.set);
  861. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  862. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  863. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  864. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  865. xfonts++;
  866. }
  867. }
  868. else {
  869. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  870. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  871. die("error, cannot load font: '%s'\n", fontstr);
  872. dc.font.ascent = dc.font.xfont->ascent;
  873. dc.font.descent = dc.font.xfont->descent;
  874. }
  875. dc.font.height = dc.font.ascent + dc.font.descent;
  876. }
  877. Bool
  878. isprotodel(Client *c) {
  879. int i, n;
  880. Atom *protocols;
  881. Bool ret = False;
  882. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  883. for(i = 0; !ret && i < n; i++)
  884. if(protocols[i] == wmatom[WMDelete])
  885. ret = True;
  886. XFree(protocols);
  887. }
  888. return ret;
  889. }
  890. void
  891. keypress(XEvent *e) {
  892. unsigned int i;
  893. KeySym keysym;
  894. XKeyEvent *ev;
  895. ev = &e->xkey;
  896. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  897. for(i = 0; i < LENGTH(keys); i++)
  898. if(keysym == keys[i].keysym
  899. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  900. && keys[i].func)
  901. keys[i].func(&(keys[i].arg));
  902. }
  903. void
  904. killclient(const Arg *arg) {
  905. XEvent ev;
  906. if(!selmon->sel)
  907. return;
  908. if(isprotodel(selmon->sel)) {
  909. ev.type = ClientMessage;
  910. ev.xclient.window = selmon->sel->win;
  911. ev.xclient.message_type = wmatom[WMProtocols];
  912. ev.xclient.format = 32;
  913. ev.xclient.data.l[0] = wmatom[WMDelete];
  914. ev.xclient.data.l[1] = CurrentTime;
  915. XSendEvent(dpy, selmon->sel->win, False, NoEventMask, &ev);
  916. }
  917. else
  918. XKillClient(dpy, selmon->sel->win);
  919. }
  920. void
  921. manage(Window w, XWindowAttributes *wa) {
  922. static Client cz;
  923. Client *c, *t = NULL;
  924. Window trans = None;
  925. XWindowChanges wc;
  926. if(!(c = malloc(sizeof(Client))))
  927. die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  928. *c = cz;
  929. c->win = w;
  930. c->mon = selmon;
  931. /* geometry */
  932. c->x = wa->x;
  933. c->y = wa->y;
  934. c->w = wa->width;
  935. c->h = wa->height;
  936. c->oldbw = wa->border_width;
  937. if(c->w == sw && c->h == sh) {
  938. c->x = sx;
  939. c->y = sy;
  940. c->bw = 0;
  941. }
  942. else {
  943. if(c->x + WIDTH(c) > sx + sw)
  944. c->x = sx + sw - WIDTH(c);
  945. if(c->y + HEIGHT(c) > sy + sh)
  946. c->y = sy + sh - HEIGHT(c);
  947. c->x = MAX(c->x, sx);
  948. /* only fix client y-offset, if the client center might cover the bar */
  949. c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
  950. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : sy);
  951. c->bw = borderpx;
  952. }
  953. wc.border_width = c->bw;
  954. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  955. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  956. configure(c); /* propagates border_width, if size doesn't change */
  957. updatesizehints(c);
  958. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  959. grabbuttons(c, False);
  960. updatetitle(c);
  961. if(XGetTransientForHint(dpy, w, &trans))
  962. t = getclient(trans);
  963. if(t)
  964. c->tags = t->tags;
  965. else
  966. applyrules(c);
  967. if(!c->isfloating)
  968. c->isfloating = trans != None || c->isfixed;
  969. if(c->isfloating)
  970. XRaiseWindow(dpy, c->win);
  971. attach(c);
  972. attachstack(c);
  973. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  974. XMapWindow(dpy, c->win);
  975. setclientstate(c, NormalState);
  976. arrange();
  977. }
  978. void
  979. mappingnotify(XEvent *e) {
  980. XMappingEvent *ev = &e->xmapping;
  981. XRefreshKeyboardMapping(ev);
  982. if(ev->request == MappingKeyboard)
  983. grabkeys();
  984. }
  985. void
  986. maprequest(XEvent *e) {
  987. static XWindowAttributes wa;
  988. XMapRequestEvent *ev = &e->xmaprequest;
  989. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  990. return;
  991. if(wa.override_redirect)
  992. return;
  993. if(!getclient(ev->window))
  994. manage(ev->window, &wa);
  995. }
  996. void
  997. monocle(Monitor *m) {
  998. Client *c;
  999. for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1000. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw);
  1001. }
  1002. void
  1003. movemouse(const Arg *arg) {
  1004. int x, y, ocx, ocy, di, nx, ny;
  1005. unsigned int dui;
  1006. Client *c;
  1007. Window dummy;
  1008. XEvent ev;
  1009. if(!(c = selmon->sel))
  1010. return;
  1011. restack(selmon);
  1012. ocx = c->x;
  1013. ocy = c->y;
  1014. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1015. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1016. return;
  1017. XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui);
  1018. do {
  1019. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1020. switch (ev.type) {
  1021. case ConfigureRequest:
  1022. case Expose:
  1023. case MapRequest:
  1024. handler[ev.type](&ev);
  1025. break;
  1026. case MotionNotify:
  1027. nx = ocx + (ev.xmotion.x - x);
  1028. ny = ocy + (ev.xmotion.y - y);
  1029. if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1030. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1031. if(abs(selmon->wx - nx) < snap)
  1032. nx = selmon->wx;
  1033. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1034. nx = selmon->wx + selmon->ww - WIDTH(c);
  1035. if(abs(selmon->wy - ny) < snap)
  1036. ny = selmon->wy;
  1037. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1038. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1039. if(!c->isfloating && lt[selmon->sellt]->arrange
  1040. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1041. togglefloating(NULL);
  1042. }
  1043. if(!lt[selmon->sellt]->arrange || c->isfloating)
  1044. resize(c, nx, ny, c->w, c->h);
  1045. break;
  1046. }
  1047. }
  1048. while(ev.type != ButtonRelease);
  1049. XUngrabPointer(dpy, CurrentTime);
  1050. }
  1051. Client *
  1052. nexttiled(Client *c) {
  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(!m->sel)
  1156. return;
  1157. if(m->sel->isfloating || !lt[m->sellt]->arrange)
  1158. XRaiseWindow(dpy, m->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(c->mon->sel == c) {
  1436. c->mon->sel = c->mon->stack;
  1437. focus(NULL);
  1438. }
  1439. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1440. setclientstate(c, WithdrawnState);
  1441. free(c);
  1442. XSync(dpy, False);
  1443. XSetErrorHandler(xerror);
  1444. XUngrabServer(dpy);
  1445. arrange();
  1446. }
  1447. void
  1448. unmapnotify(XEvent *e) {
  1449. Client *c;
  1450. XUnmapEvent *ev = &e->xunmap;
  1451. if((c = getclient(ev->window)))
  1452. unmanage(c);
  1453. }
  1454. void
  1455. updatebars(void) {
  1456. Monitor *m;
  1457. XSetWindowAttributes wa;
  1458. wa.override_redirect = True;
  1459. wa.background_pixmap = ParentRelative;
  1460. wa.event_mask = ButtonPressMask|ExposureMask;
  1461. for(m = mons; m; m = m->next) {
  1462. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  1463. CopyFromParent, DefaultVisual(dpy, screen),
  1464. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1465. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1466. XMapRaised(dpy, m->barwin);
  1467. }
  1468. }
  1469. void
  1470. updatebarpos(Monitor *m) {
  1471. m->wy = m->my;
  1472. m->wh = m->mh;
  1473. if(m->showbar) {
  1474. m->wh -= bh;
  1475. m->by = m->topbar ? m->wy : m->wy + m->wh;
  1476. m->wy = m->topbar ? m->wy + bh : m->wy;
  1477. }
  1478. else
  1479. m->by = -bh;
  1480. }
  1481. void
  1482. updategeom(void) {
  1483. int i, n = 1;
  1484. Client *c;
  1485. Monitor *newmons = NULL, *m, *tm;
  1486. #ifdef XINULATOR
  1487. n = 2;
  1488. #elif defined(XINERAMA)
  1489. XineramaScreenInfo *info = NULL;
  1490. if(XineramaIsActive(dpy))
  1491. info = XineramaQueryScreens(dpy, &n);
  1492. #endif
  1493. /* allocate monitor(s) for the new geometry setup */
  1494. for(i = 0; i < n; i++) {
  1495. m = (Monitor *)malloc(sizeof(Monitor));
  1496. m->next = newmons;
  1497. newmons = m;
  1498. }
  1499. /* initialise monitor(s) */
  1500. #ifdef XINULATOR
  1501. if(1) {
  1502. m = newmons;
  1503. m->screen_number = 0;
  1504. m->wx = sx;
  1505. m->my = m->wy = sy;
  1506. m->ww = sw;
  1507. m->mh = m->wh = sh / 2;
  1508. m = newmons->next;
  1509. m->screen_number = 1;
  1510. m->wx = sx;
  1511. m->my = m->wy = sy + sh / 2;
  1512. m->ww = sw;
  1513. m->mh = m->wh = sh / 2;
  1514. }
  1515. else
  1516. #elif defined(XINERAMA)
  1517. if(XineramaIsActive(dpy)) {
  1518. for(i = 0, m = newmons; m; m = m->next, i++) {
  1519. m->screen_number = info[i].screen_number;
  1520. m->wx = info[i].x_org;
  1521. m->my = m->wy = info[i].y_org;
  1522. m->ww = info[i].width;
  1523. m->mh = m->wh = info[i].height;
  1524. }
  1525. XFree(info);
  1526. }
  1527. else
  1528. #endif
  1529. /* default monitor setup */
  1530. {
  1531. m->screen_number = 0;
  1532. m->wx = sx;
  1533. m->my = m->wy = sy;
  1534. m->ww = sw;
  1535. m->mh = m->wh = sh;
  1536. }
  1537. /* bar geometry setup */
  1538. for(m = newmons; m; m = m->next) {
  1539. /* TODO: consider removing the following values from config.h */
  1540. m->clients = NULL;
  1541. m->sel = NULL;
  1542. m->stack = NULL;
  1543. m->seltags = 0;
  1544. m->sellt = 0;
  1545. m->tagset[0] = m->tagset[1] = 1;
  1546. m->mfact = mfact;
  1547. m->showbar = showbar;
  1548. m->topbar = topbar;
  1549. updatebarpos(m);
  1550. /* reassign all clients with same screen number */
  1551. for(tm = mons; tm; tm = tm->next)
  1552. if(tm->screen_number == m->screen_number) {
  1553. m->clients = tm->clients;
  1554. m->sel = m->stack = tm->stack;
  1555. tm->clients = NULL;
  1556. tm->stack = NULL;
  1557. for(c = m->clients; c; c = c->next)
  1558. c->mon = m;
  1559. }
  1560. }
  1561. /* reassign left over clients of disappeared monitors */
  1562. for(tm = mons; tm; tm = tm->next) {
  1563. while((c = tm->clients)) {
  1564. detach(c);
  1565. detachstack(c);
  1566. c->mon = newmons;
  1567. attach(c);
  1568. attachstack(c);
  1569. }
  1570. }
  1571. /* select focused monitor */
  1572. if(!selmon) {
  1573. selmon = newmons;
  1574. int di, x, y;
  1575. unsigned int dui;
  1576. Window dummy;
  1577. if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui))
  1578. for(m = newmons; m; m = m->next)
  1579. if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh)) {
  1580. selmon = m;
  1581. break;
  1582. }
  1583. }
  1584. /* final assignment of new monitors */
  1585. cleanupmons();
  1586. mons = newmons;
  1587. }
  1588. void
  1589. updatenumlockmask(void) {
  1590. unsigned int i, j;
  1591. XModifierKeymap *modmap;
  1592. numlockmask = 0;
  1593. modmap = XGetModifierMapping(dpy);
  1594. for(i = 0; i < 8; i++)
  1595. for(j = 0; j < modmap->max_keypermod; j++)
  1596. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1597. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1598. numlockmask = (1 << i);
  1599. XFreeModifiermap(modmap);
  1600. }
  1601. void
  1602. updatesizehints(Client *c) {
  1603. long msize;
  1604. XSizeHints size;
  1605. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1606. /* size is uninitialized, ensure that size.flags aren't used */
  1607. size.flags = PSize;
  1608. if(size.flags & PBaseSize) {
  1609. c->basew = size.base_width;
  1610. c->baseh = size.base_height;
  1611. }
  1612. else if(size.flags & PMinSize) {
  1613. c->basew = size.min_width;
  1614. c->baseh = size.min_height;
  1615. }
  1616. else
  1617. c->basew = c->baseh = 0;
  1618. if(size.flags & PResizeInc) {
  1619. c->incw = size.width_inc;
  1620. c->inch = size.height_inc;
  1621. }
  1622. else
  1623. c->incw = c->inch = 0;
  1624. if(size.flags & PMaxSize) {
  1625. c->maxw = size.max_width;
  1626. c->maxh = size.max_height;
  1627. }
  1628. else
  1629. c->maxw = c->maxh = 0;
  1630. if(size.flags & PMinSize) {
  1631. c->minw = size.min_width;
  1632. c->minh = size.min_height;
  1633. }
  1634. else if(size.flags & PBaseSize) {
  1635. c->minw = size.base_width;
  1636. c->minh = size.base_height;
  1637. }
  1638. else
  1639. c->minw = c->minh = 0;
  1640. if(size.flags & PAspect) {
  1641. c->mina = (float)size.min_aspect.y / (float)size.min_aspect.x;
  1642. c->maxa = (float)size.max_aspect.x / (float)size.max_aspect.y;
  1643. }
  1644. else
  1645. c->maxa = c->mina = 0.0;
  1646. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1647. && c->maxw == c->minw && c->maxh == c->minh);
  1648. }
  1649. void
  1650. updatetitle(Client *c) {
  1651. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1652. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1653. }
  1654. void
  1655. updatestatus() {
  1656. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1657. strcpy(stext, "dwm-"VERSION);
  1658. drawbar(selmon);
  1659. }
  1660. void
  1661. updatewmhints(Client *c) {
  1662. XWMHints *wmh;
  1663. if((wmh = XGetWMHints(dpy, c->win))) {
  1664. if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  1665. wmh->flags &= ~XUrgencyHint;
  1666. XSetWMHints(dpy, c->win, wmh);
  1667. }
  1668. else
  1669. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1670. XFree(wmh);
  1671. }
  1672. }
  1673. void
  1674. view(const Arg *arg) {
  1675. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1676. return;
  1677. selmon->seltags ^= 1; /* toggle sel tagset */
  1678. if(arg->ui & TAGMASK)
  1679. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1680. arrange();
  1681. }
  1682. /* There's no way to check accesses to destroyed windows, thus those cases are
  1683. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1684. * default error handler, which may call exit. */
  1685. int
  1686. xerror(Display *dpy, XErrorEvent *ee) {
  1687. if(ee->error_code == BadWindow
  1688. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1689. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1690. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1691. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1692. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1693. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1694. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1695. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1696. return 0;
  1697. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1698. ee->request_code, ee->error_code);
  1699. return xerrorxlib(dpy, ee); /* may call exit */
  1700. }
  1701. int
  1702. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1703. return 0;
  1704. }
  1705. /* Startup Error handler to check if another window manager
  1706. * is already running. */
  1707. int
  1708. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1709. otherwm = True;
  1710. return -1;
  1711. }
  1712. void
  1713. zoom(const Arg *arg) {
  1714. Client *c = selmon->sel;
  1715. if(!lt[selmon->sellt]->arrange || lt[selmon->sellt]->arrange == monocle || (selmon->sel && selmon->sel->isfloating))
  1716. return;
  1717. if(c == nexttiled(selmon->clients))
  1718. if(!c || !(c = nexttiled(c->next)))
  1719. return;
  1720. detach(c);
  1721. attach(c);
  1722. focus(c);
  1723. arrange();
  1724. }
  1725. int
  1726. main(int argc, char *argv[]) {
  1727. if(argc == 2 && !strcmp("-v", argv[1]))
  1728. die("dwm-"VERSION", © 2006-2009 dwm engineers, see LICENSE for details\n");
  1729. else if(argc != 1)
  1730. die("usage: dwm [-v]\n");
  1731. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1732. fputs("warning: no locale support\n", stderr);
  1733. if(!(dpy = XOpenDisplay(NULL)))
  1734. die("dwm: cannot open display\n");
  1735. checkotherwm();
  1736. setup();
  1737. scan();
  1738. run();
  1739. cleanup();
  1740. XCloseDisplay(dpy);
  1741. return 0;
  1742. }