dwm.c 44 KB

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