Configuration of dwm for Mac Computers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2061 lines
47 KiB

17 years ago
17 years ago
17 years ago
  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. * Calls to fetch an X event from the event queue are blocking. Due reading
  10. * status text from standard input, a select()-driven main loop has been
  11. * implemented which selects for reads on the X connection and STDIN_FILENO to
  12. * handle all data smoothly. The event handlers of dwm are organized in an
  13. * array which is accessed whenever a new event has been fetched. This allows
  14. * event dispatching in O(1) time.
  15. *
  16. * Each child of the root window is called a client, except windows which have
  17. * set the override_redirect flag. Clients are organized in a global
  18. * doubly-linked client list, the focus history is remembered through a global
  19. * stack list. Each client contains an array of Bools of the same size as the
  20. * global tags array to indicate the tags of a client.
  21. *
  22. * Keys and tagging rules are organized as arrays and defined in config.h.
  23. *
  24. * To understand everything else, start reading main().
  25. */
  26. #include <errno.h>
  27. #include <locale.h>
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <sys/select.h>
  34. #include <sys/types.h>
  35. #include <sys/wait.h>
  36. #include <regex.h>
  37. #include <X11/cursorfont.h>
  38. #include <X11/keysym.h>
  39. #include <X11/Xatom.h>
  40. #include <X11/Xlib.h>
  41. #include <X11/Xproto.h>
  42. #include <X11/Xutil.h>
  43. //#ifdef XINERAMA
  44. #include <X11/extensions/Xinerama.h>
  45. //#endif
  46. /* macros */
  47. #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
  48. #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
  49. #define LENGTH(x) (sizeof x / sizeof x[0])
  50. #define MAXTAGLEN 16
  51. #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
  52. /* enums */
  53. enum { BarTop, BarBot, BarOff }; /* bar position */
  54. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  55. enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
  56. enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
  57. enum { WMProtocols, WMDelete, WMName, WMState, WMLast };/* default atoms */
  58. /* typedefs */
  59. typedef struct View View;
  60. typedef struct Client Client;
  61. struct Client {
  62. char name[256];
  63. int x, y, w, h;
  64. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  65. int minax, maxax, minay, maxay;
  66. int *tags;
  67. long flags;
  68. unsigned int border, oldborder;
  69. Bool isbanned, isfixed, isfloating, isurgent;
  70. Client *next;
  71. Client *prev;
  72. Client *snext;
  73. Window win;
  74. };
  75. typedef struct {
  76. int x, y, w, h;
  77. unsigned long norm[ColLast];
  78. unsigned long sel[ColLast];
  79. Drawable drawable;
  80. GC gc;
  81. struct {
  82. int ascent;
  83. int descent;
  84. int height;
  85. XFontSet set;
  86. XFontStruct *xfont;
  87. } font;
  88. } DC; /* draw context */
  89. typedef struct {
  90. unsigned long mod;
  91. KeySym keysym;
  92. void (*func)(const char *arg);
  93. const char *arg;
  94. } Key;
  95. typedef struct {
  96. const char *symbol;
  97. void (*arrange)(View *);
  98. } Layout;
  99. typedef struct {
  100. const char *prop;
  101. const char *tags;
  102. Bool isfloating;
  103. } Rule;
  104. typedef struct {
  105. regex_t *propregex;
  106. regex_t *tagregex;
  107. } Regs;
  108. struct View {
  109. int id;
  110. int x, y, w, h, wax, way, wah, waw;
  111. double mwfact;
  112. Layout *layout;
  113. Window barwin;
  114. };
  115. /* function declarations */
  116. void applyrules(Client *c);
  117. void arrange(void);
  118. void attach(Client *c);
  119. void attachstack(Client *c);
  120. void ban(Client *c);
  121. void buttonpress(XEvent *e);
  122. void checkotherwm(void);
  123. void cleanup(void);
  124. void compileregs(void);
  125. void configure(Client *c);
  126. void configurenotify(XEvent *e);
  127. void configurerequest(XEvent *e);
  128. void destroynotify(XEvent *e);
  129. void detach(Client *c);
  130. void detachstack(Client *c);
  131. void drawbar(View *v);
  132. void drawsquare(View *v, Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
  133. void drawtext(View *v, const char *text, unsigned long col[ColLast], Bool invert);
  134. void *emallocz(unsigned int size);
  135. void enternotify(XEvent *e);
  136. void eprint(const char *errstr, ...);
  137. void expose(XEvent *e);
  138. void floating(View *v); /* default floating layout */
  139. void focus(Client *c);
  140. void focusin(XEvent *e);
  141. void focusnext(const char *arg);
  142. void focusprev(const char *arg);
  143. Client *getclient(Window w);
  144. unsigned long getcolor(const char *colstr);
  145. View *getviewbar(Window barwin);
  146. View *getview(Client *c);
  147. long getstate(Window w);
  148. Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  149. void grabbuttons(Client *c, Bool focused);
  150. void grabkeys(void);
  151. unsigned int idxoftag(const char *tag);
  152. void initfont(const char *fontstr);
  153. Bool isoccupied(unsigned int t);
  154. Bool isprotodel(Client *c);
  155. Bool isurgent(unsigned int t);
  156. Bool isvisible(Client *c);
  157. void keypress(XEvent *e);
  158. void killclient(const char *arg);
  159. void manage(Window w, XWindowAttributes *wa);
  160. void mappingnotify(XEvent *e);
  161. void maprequest(XEvent *e);
  162. View *viewat(void);
  163. void movemouse(Client *c);
  164. Client *nexttiled(Client *c, View *v);
  165. void propertynotify(XEvent *e);
  166. void quit(const char *arg);
  167. void reapply(const char *arg);
  168. void resize(Client *c, int x, int y, int w, int h, Bool sizehints);
  169. void resizemouse(Client *c);
  170. void restack(View *v);
  171. void run(void);
  172. void scan(void);
  173. void setclientstate(Client *c, long state);
  174. void setlayout(const char *arg);
  175. void setmwfact(const char *arg);
  176. void setup(void);
  177. void spawn(const char *arg);
  178. void tag(const char *arg);
  179. unsigned int textnw(const char *text, unsigned int len);
  180. unsigned int textw(const char *text);
  181. void tile(View *v);
  182. void togglebar(const char *arg);
  183. void togglefloating(const char *arg);
  184. void toggletag(const char *arg);
  185. void toggleview(const char *arg);
  186. void unban(Client *c);
  187. void unmanage(Client *c);
  188. void unmapnotify(XEvent *e);
  189. void updatebarpos(View *v);
  190. void updatesizehints(Client *c);
  191. void updatetitle(Client *c);
  192. void updatewmhints(Client *c);
  193. void view(const char *arg);
  194. void viewprevtag(const char *arg); /* views previous selected tags */
  195. int xerror(Display *dpy, XErrorEvent *ee);
  196. int xerrordummy(Display *dpy, XErrorEvent *ee);
  197. int xerrorstart(Display *dpy, XErrorEvent *ee);
  198. void zoom(const char *arg);
  199. void selectview(const char *arg);
  200. /* variables */
  201. char stext[256];
  202. int nviews = 1;
  203. View *selview;
  204. int screen;
  205. int *seltags;
  206. int *prevtags;
  207. int (*xerrorxlib)(Display *, XErrorEvent *);
  208. unsigned int bh, bpos;
  209. unsigned int blw = 0;
  210. unsigned int numlockmask = 0;
  211. void (*handler[LASTEvent]) (XEvent *) = {
  212. [ButtonPress] = buttonpress,
  213. [ConfigureRequest] = configurerequest,
  214. [ConfigureNotify] = configurenotify,
  215. [DestroyNotify] = destroynotify,
  216. [EnterNotify] = enternotify,
  217. [Expose] = expose,
  218. [FocusIn] = focusin,
  219. [KeyPress] = keypress,
  220. [MappingNotify] = mappingnotify,
  221. [MapRequest] = maprequest,
  222. [PropertyNotify] = propertynotify,
  223. [UnmapNotify] = unmapnotify
  224. };
  225. Atom wmatom[WMLast], netatom[NetLast];
  226. Bool isxinerama = False;
  227. Bool domwfact = True;
  228. Bool dozoom = True;
  229. Bool otherwm, readin;
  230. Bool running = True;
  231. Client *clients = NULL;
  232. Client *sel = NULL;
  233. Client *stack = NULL;
  234. Cursor cursor[CurLast];
  235. Display *dpy;
  236. DC dc = {0};
  237. Regs *regs = NULL;
  238. View *views;
  239. Window root;
  240. /* configuration, allows nested code to access above variables */
  241. #include "config.h"
  242. /* function implementations */
  243. void
  244. applyrules(Client *c) {
  245. static char buf[512];
  246. unsigned int i, j;
  247. regmatch_t tmp;
  248. Bool matched_tag = False;
  249. XClassHint ch = { 0 };
  250. /* rule matching */
  251. XGetClassHint(dpy, c->win, &ch);
  252. snprintf(buf, sizeof buf, "%s:%s:%s",
  253. ch.res_class ? ch.res_class : "",
  254. ch.res_name ? ch.res_name : "", c->name);
  255. for(i = 0; i < LENGTH(rules); i++)
  256. if(regs[i].propregex && !regexec(regs[i].propregex, buf, 1, &tmp, 0)) {
  257. c->isfloating = rules[i].isfloating;
  258. for(j = 0; regs[i].tagregex && j < LENGTH(tags); j++) {
  259. if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) {
  260. matched_tag = True;
  261. c->tags[j] = selview->id;
  262. }
  263. }
  264. }
  265. if(ch.res_class)
  266. XFree(ch.res_class);
  267. if(ch.res_name)
  268. XFree(ch.res_name);
  269. if(!matched_tag)
  270. memcpy(c->tags, seltags, sizeof initags);
  271. }
  272. void
  273. arrange(void) {
  274. unsigned int i;
  275. Client *c;
  276. for(c = clients; c; c = c->next)
  277. if(isvisible(c))
  278. unban(c);
  279. else
  280. ban(c);
  281. for(i = 0; i < nviews; i++) {
  282. views[i].layout->arrange(&views[i]);
  283. restack(&views[i]);
  284. }
  285. focus(NULL);
  286. }
  287. void
  288. attach(Client *c) {
  289. if(clients)
  290. clients->prev = c;
  291. c->next = clients;
  292. clients = c;
  293. }
  294. void
  295. attachstack(Client *c) {
  296. c->snext = stack;
  297. stack = c;
  298. }
  299. void
  300. ban(Client *c) {
  301. if(c->isbanned)
  302. return;
  303. XMoveWindow(dpy, c->win, c->x + 3 * getview(c)->w, c->y);
  304. c->isbanned = True;
  305. }
  306. void
  307. buttonpress(XEvent *e) {
  308. unsigned int i, x;
  309. Client *c;
  310. XButtonPressedEvent *ev = &e->xbutton;
  311. View *v = selview;
  312. if(ev->window == v->barwin) {
  313. x = 0;
  314. for(i = 0; i < LENGTH(tags); i++) {
  315. x += textw(tags[i]);
  316. if(ev->x < x) {
  317. if(ev->button == Button1) {
  318. if(ev->state & MODKEY)
  319. tag(tags[i]);
  320. else
  321. view(tags[i]);
  322. }
  323. else if(ev->button == Button3) {
  324. if(ev->state & MODKEY)
  325. toggletag(tags[i]);
  326. else
  327. toggleview(tags[i]);
  328. }
  329. return;
  330. }
  331. }
  332. if((ev->x < x + blw) && ev->button == Button1)
  333. setlayout(NULL);
  334. }
  335. else if((c = getclient(ev->window))) {
  336. focus(c);
  337. if(CLEANMASK(ev->state) != MODKEY)
  338. return;
  339. if(ev->button == Button1) {
  340. restack(getview(c));
  341. movemouse(c);
  342. }
  343. else if(ev->button == Button2) {
  344. if((floating != v->layout->arrange) && c->isfloating)
  345. togglefloating(NULL);
  346. else
  347. zoom(NULL);
  348. }
  349. else if(ev->button == Button3 && !c->isfixed) {
  350. restack(getview(c));
  351. resizemouse(c);
  352. }
  353. }
  354. }
  355. void
  356. checkotherwm(void) {
  357. otherwm = False;
  358. XSetErrorHandler(xerrorstart);
  359. /* this causes an error if some other window manager is running */
  360. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  361. XSync(dpy, False);
  362. if(otherwm)
  363. eprint("dwm: another window manager is already running\n");
  364. XSync(dpy, False);
  365. XSetErrorHandler(NULL);
  366. xerrorxlib = XSetErrorHandler(xerror);
  367. XSync(dpy, False);
  368. }
  369. void
  370. cleanup(void) {
  371. unsigned int i;
  372. close(STDIN_FILENO);
  373. while(stack) {
  374. unban(stack);
  375. unmanage(stack);
  376. }
  377. if(dc.font.set)
  378. XFreeFontSet(dpy, dc.font.set);
  379. else
  380. XFreeFont(dpy, dc.font.xfont);
  381. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  382. XFreePixmap(dpy, dc.drawable);
  383. XFreeGC(dpy, dc.gc);
  384. XFreeCursor(dpy, cursor[CurNormal]);
  385. XFreeCursor(dpy, cursor[CurResize]);
  386. XFreeCursor(dpy, cursor[CurMove]);
  387. for(i = 0; i < nviews; i++)
  388. XDestroyWindow(dpy, views[i].barwin);
  389. XSync(dpy, False);
  390. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  391. }
  392. void
  393. compileregs(void) {
  394. unsigned int i;
  395. regex_t *reg;
  396. if(regs)
  397. return;
  398. regs = emallocz(LENGTH(rules) * sizeof(Regs));
  399. for(i = 0; i < LENGTH(rules); i++) {
  400. if(rules[i].prop) {
  401. reg = emallocz(sizeof(regex_t));
  402. if(regcomp(reg, rules[i].prop, REG_EXTENDED))
  403. free(reg);
  404. else
  405. regs[i].propregex = reg;
  406. }
  407. if(rules[i].tags) {
  408. reg = emallocz(sizeof(regex_t));
  409. if(regcomp(reg, rules[i].tags, REG_EXTENDED))
  410. free(reg);
  411. else
  412. regs[i].tagregex = reg;
  413. }
  414. }
  415. }
  416. void
  417. configure(Client *c) {
  418. XConfigureEvent ce;
  419. ce.type = ConfigureNotify;
  420. ce.display = dpy;
  421. ce.event = c->win;
  422. ce.window = c->win;
  423. ce.x = c->x;
  424. ce.y = c->y;
  425. ce.width = c->w;
  426. ce.height = c->h;
  427. ce.border_width = c->border;
  428. ce.above = None;
  429. ce.override_redirect = False;
  430. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  431. }
  432. void
  433. configurenotify(XEvent *e) {
  434. XConfigureEvent *ev = &e->xconfigure;
  435. View *v = selview;
  436. if(ev->window == root && (ev->width != v->w || ev->height != v->h)) {
  437. /* TODO -- update Xinerama dimensions here */
  438. v->w = ev->width;
  439. v->h = ev->height;
  440. XFreePixmap(dpy, dc.drawable);
  441. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(root, screen), bh, DefaultDepth(dpy, screen));
  442. XResizeWindow(dpy, v->barwin, v->w, bh);
  443. updatebarpos(v);
  444. arrange();
  445. }
  446. }
  447. void
  448. configurerequest(XEvent *e) {
  449. Client *c;
  450. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  451. XWindowChanges wc;
  452. if((c = getclient(ev->window))) {
  453. View *v = getview(c);
  454. if(ev->value_mask & CWBorderWidth)
  455. c->border = ev->border_width;
  456. if(c->isfixed || c->isfloating || (floating == v->layout->arrange)) {
  457. if(ev->value_mask & CWX)
  458. c->x = v->x + ev->x;
  459. if(ev->value_mask & CWY)
  460. c->y = v->y + ev->y;
  461. if(ev->value_mask & CWWidth)
  462. c->w = ev->width;
  463. if(ev->value_mask & CWHeight)
  464. c->h = ev->height;
  465. if((c->x - v->x + c->w) > v->w && c->isfloating)
  466. c->x = v->x + (v->w / 2 - c->w / 2); /* center in x direction */
  467. if((c->y - v->y + c->h) > v->h && c->isfloating)
  468. c->y = v->y + (v->h / 2 - c->h / 2); /* center in y direction */
  469. if((ev->value_mask & (CWX|CWY))
  470. && !(ev->value_mask & (CWWidth|CWHeight)))
  471. configure(c);
  472. if(isvisible(c))
  473. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  474. }
  475. else
  476. configure(c);
  477. }
  478. else {
  479. wc.x = ev->x;
  480. wc.y = ev->y;
  481. wc.width = ev->width;
  482. wc.height = ev->height;
  483. wc.border_width = ev->border_width;
  484. wc.sibling = ev->above;
  485. wc.stack_mode = ev->detail;
  486. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  487. }
  488. XSync(dpy, False);
  489. }
  490. void
  491. destroynotify(XEvent *e) {
  492. Client *c;
  493. XDestroyWindowEvent *ev = &e->xdestroywindow;
  494. if((c = getclient(ev->window)))
  495. unmanage(c);
  496. }
  497. void
  498. detach(Client *c) {
  499. if(c->prev)
  500. c->prev->next = c->next;
  501. if(c->next)
  502. c->next->prev = c->prev;
  503. if(c == clients)
  504. clients = c->next;
  505. c->next = c->prev = NULL;
  506. }
  507. void
  508. detachstack(Client *c) {
  509. Client **tc;
  510. for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
  511. *tc = c->snext;
  512. }
  513. void
  514. drawbar(View *v) {
  515. int i, x;
  516. Client *c;
  517. dc.x = 0;
  518. for(c = stack; c && !isvisible(c); c = c->snext);
  519. for(i = 0; i < LENGTH(tags); i++) {
  520. dc.w = textw(tags[i]);
  521. if(seltags[i]) {
  522. drawtext(v, tags[i], dc.sel, isurgent(i));
  523. drawsquare(v, c && c->tags[i], isoccupied(i), isurgent(i), dc.sel);
  524. }
  525. else {
  526. drawtext(v, tags[i], dc.norm, isurgent(i));
  527. drawsquare(v, c && c->tags[i], isoccupied(i), isurgent(i), dc.norm);
  528. }
  529. dc.x += dc.w;
  530. }
  531. dc.w = blw;
  532. drawtext(v, v->layout->symbol, dc.norm, False);
  533. x = dc.x + dc.w;
  534. if(v == selview) {
  535. dc.w = textw(stext);
  536. dc.x = v->w - dc.w;
  537. if(dc.x < x) {
  538. dc.x = x;
  539. dc.w = v->w - x;
  540. }
  541. drawtext(v, stext, dc.norm, False);
  542. }
  543. else
  544. dc.x = v->w;
  545. if((dc.w = dc.x - x) > bh) {
  546. dc.x = x;
  547. if(c) {
  548. drawtext(v, c->name, dc.sel, False);
  549. drawsquare(v, False, c->isfloating, False, dc.sel);
  550. }
  551. else
  552. drawtext(v, NULL, dc.norm, False);
  553. }
  554. XCopyArea(dpy, dc.drawable, v->barwin, dc.gc, 0, 0, v->w, bh, 0, 0);
  555. XSync(dpy, False);
  556. }
  557. void
  558. drawsquare(View *v, Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  559. int x;
  560. XGCValues gcv;
  561. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  562. gcv.foreground = col[invert ? ColBG : ColFG];
  563. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  564. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  565. r.x = dc.x + 1;
  566. r.y = dc.y + 1;
  567. if(filled) {
  568. r.width = r.height = x + 1;
  569. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  570. }
  571. else if(empty) {
  572. r.width = r.height = x;
  573. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  574. }
  575. }
  576. void
  577. drawtext(View *v, const char *text, unsigned long col[ColLast], Bool invert) {
  578. int x, y, w, h;
  579. static char buf[256];
  580. unsigned int len, olen;
  581. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  582. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  583. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  584. if(!text)
  585. return;
  586. w = 0;
  587. olen = len = strlen(text);
  588. if(len >= sizeof buf)
  589. len = sizeof buf - 1;
  590. memcpy(buf, text, len);
  591. buf[len] = 0;
  592. h = dc.font.ascent + dc.font.descent;
  593. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  594. x = dc.x + (h / 2);
  595. /* shorten text if necessary */
  596. while(len && (w = textnw(buf, len)) > dc.w - h)
  597. buf[--len] = 0;
  598. if(len < olen) {
  599. if(len > 1)
  600. buf[len - 1] = '.';
  601. if(len > 2)
  602. buf[len - 2] = '.';
  603. if(len > 3)
  604. buf[len - 3] = '.';
  605. }
  606. if(w > dc.w)
  607. return; /* too long */
  608. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  609. if(dc.font.set)
  610. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  611. else
  612. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  613. }
  614. void *
  615. emallocz(unsigned int size) {
  616. void *res = calloc(1, size);
  617. if(!res)
  618. eprint("fatal: could not malloc() %u bytes\n", size);
  619. return res;
  620. }
  621. void
  622. enternotify(XEvent *e) {
  623. Client *c;
  624. XCrossingEvent *ev = &e->xcrossing;
  625. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior) {
  626. if(!isxinerama || ev->window != root)
  627. return;
  628. }
  629. if((c = getclient(ev->window)))
  630. focus(c);
  631. else
  632. focus(NULL);
  633. }
  634. void
  635. eprint(const char *errstr, ...) {
  636. va_list ap;
  637. va_start(ap, errstr);
  638. vfprintf(stderr, errstr, ap);
  639. va_end(ap);
  640. exit(EXIT_FAILURE);
  641. }
  642. void
  643. expose(XEvent *e) {
  644. View *v;
  645. XExposeEvent *ev = &e->xexpose;
  646. if(ev->count == 0 && (v = getviewbar(ev->window)))
  647. drawbar(v);
  648. }
  649. void
  650. floating(View *v) { /* default floating layout */
  651. Client *c;
  652. domwfact = dozoom = False;
  653. for(c = clients; c; c = c->next)
  654. if(isvisible(c))
  655. resize(c, c->x, c->y, c->w, c->h, True);
  656. }
  657. void
  658. focus(Client *c) {
  659. View *v = selview;
  660. if(c)
  661. selview = getview(c);
  662. else
  663. selview = viewat();
  664. if(selview != v)
  665. drawbar(v);
  666. if(!c || (c && !isvisible(c)))
  667. for(c = stack; c && !isvisible(c); c = c->snext);
  668. if(sel && sel != c) {
  669. grabbuttons(sel, False);
  670. XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
  671. }
  672. if(c) {
  673. detachstack(c);
  674. attachstack(c);
  675. grabbuttons(c, True);
  676. }
  677. sel = c;
  678. if(c) {
  679. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  680. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  681. selview = getview(c);
  682. }
  683. else
  684. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  685. drawbar(selview);
  686. }
  687. void
  688. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  689. XFocusChangeEvent *ev = &e->xfocus;
  690. if(sel && ev->window != sel->win)
  691. XSetInputFocus(dpy, sel->win, RevertToPointerRoot, CurrentTime);
  692. }
  693. void
  694. focusnext(const char *arg) {
  695. Client *c;
  696. if(!sel)
  697. return;
  698. for(c = sel->next; c && !isvisible(c); c = c->next);
  699. if(!c)
  700. for(c = clients; c && !isvisible(c); c = c->next);
  701. if(c) {
  702. focus(c);
  703. restack(getview(c));
  704. }
  705. }
  706. void
  707. focusprev(const char *arg) {
  708. Client *c;
  709. if(!sel)
  710. return;
  711. for(c = sel->prev; c && !isvisible(c); c = c->prev);
  712. if(!c) {
  713. for(c = clients; c && c->next; c = c->next);
  714. for(; c && !isvisible(c); c = c->prev);
  715. }
  716. if(c) {
  717. focus(c);
  718. restack(getview(c));
  719. }
  720. }
  721. Client *
  722. getclient(Window w) {
  723. Client *c;
  724. for(c = clients; c && c->win != w; c = c->next);
  725. return c;
  726. }
  727. unsigned long
  728. getcolor(const char *colstr) {
  729. Colormap cmap = DefaultColormap(dpy, screen);
  730. XColor color;
  731. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  732. eprint("error, cannot allocate color '%s'\n", colstr);
  733. return color.pixel;
  734. }
  735. View *
  736. getviewbar(Window barwin) {
  737. unsigned int i;
  738. for(i = 0; i < nviews; i++)
  739. if(views[i].barwin == barwin)
  740. return &views[i];
  741. return NULL;
  742. }
  743. View *
  744. getview(Client *c) {
  745. unsigned int i;
  746. for(i = 0; i < LENGTH(tags); i++)
  747. if(c->tags[i])
  748. return &views[c->tags[i] - 1];
  749. return &views[0]; /* fallback */
  750. }
  751. long
  752. getstate(Window w) {
  753. int format, status;
  754. long result = -1;
  755. unsigned char *p = NULL;
  756. unsigned long n, extra;
  757. Atom real;
  758. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  759. &real, &format, &n, &extra, (unsigned char **)&p);
  760. if(status != Success)
  761. return -1;
  762. if(n != 0)
  763. result = *p;
  764. XFree(p);
  765. return result;
  766. }
  767. Bool
  768. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  769. char **list = NULL;
  770. int n;
  771. XTextProperty name;
  772. if(!text || size == 0)
  773. return False;
  774. text[0] = '\0';
  775. XGetTextProperty(dpy, w, &name, atom);
  776. if(!name.nitems)
  777. return False;
  778. if(name.encoding == XA_STRING)
  779. strncpy(text, (char *)name.value, size - 1);
  780. else {
  781. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  782. && n > 0 && *list) {
  783. strncpy(text, *list, size - 1);
  784. XFreeStringList(list);
  785. }
  786. }
  787. text[size - 1] = '\0';
  788. XFree(name.value);
  789. return True;
  790. }
  791. void
  792. grabbuttons(Client *c, Bool focused) {
  793. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  794. if(focused) {
  795. XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  796. GrabModeAsync, GrabModeSync, None, None);
  797. XGrabButton(dpy, Button1, MODKEY|LockMask, c->win, False, BUTTONMASK,
  798. GrabModeAsync, GrabModeSync, None, None);
  799. XGrabButton(dpy, Button1, MODKEY|numlockmask, c->win, False, BUTTONMASK,
  800. GrabModeAsync, GrabModeSync, None, None);
  801. XGrabButton(dpy, Button1, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK,
  802. GrabModeAsync, GrabModeSync, None, None);
  803. XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  804. GrabModeAsync, GrabModeSync, None, None);
  805. XGrabButton(dpy, Button2, MODKEY|LockMask, c->win, False, BUTTONMASK,
  806. GrabModeAsync, GrabModeSync, None, None);
  807. XGrabButton(dpy, Button2, MODKEY|numlockmask, c->win, False, BUTTONMASK,
  808. GrabModeAsync, GrabModeSync, None, None);
  809. XGrabButton(dpy, Button2, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK,
  810. GrabModeAsync, GrabModeSync, None, None);
  811. XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  812. GrabModeAsync, GrabModeSync, None, None);
  813. XGrabButton(dpy, Button3, MODKEY|LockMask, c->win, False, BUTTONMASK,
  814. GrabModeAsync, GrabModeSync, None, None);
  815. XGrabButton(dpy, Button3, MODKEY|numlockmask, c->win, False, BUTTONMASK,
  816. GrabModeAsync, GrabModeSync, None, None);
  817. XGrabButton(dpy, Button3, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK,
  818. GrabModeAsync, GrabModeSync, None, None);
  819. }
  820. else
  821. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
  822. GrabModeAsync, GrabModeSync, None, None);
  823. }
  824. void
  825. grabkeys(void) {
  826. unsigned int i, j;
  827. KeyCode code;
  828. XModifierKeymap *modmap;
  829. /* init modifier map */
  830. modmap = XGetModifierMapping(dpy);
  831. for(i = 0; i < 8; i++)
  832. for(j = 0; j < modmap->max_keypermod; j++) {
  833. if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
  834. numlockmask = (1 << i);
  835. }
  836. XFreeModifiermap(modmap);
  837. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  838. for(i = 0; i < LENGTH(keys); i++) {
  839. code = XKeysymToKeycode(dpy, keys[i].keysym);
  840. XGrabKey(dpy, code, keys[i].mod, root, True,
  841. GrabModeAsync, GrabModeAsync);
  842. XGrabKey(dpy, code, keys[i].mod|LockMask, root, True,
  843. GrabModeAsync, GrabModeAsync);
  844. XGrabKey(dpy, code, keys[i].mod|numlockmask, root, True,
  845. GrabModeAsync, GrabModeAsync);
  846. XGrabKey(dpy, code, keys[i].mod|numlockmask|LockMask, root, True,
  847. GrabModeAsync, GrabModeAsync);
  848. }
  849. }
  850. unsigned int
  851. idxoftag(const char *tag) {
  852. unsigned int i;
  853. for(i = 0; (i < LENGTH(tags)) && (tags[i] != tag); i++);
  854. return (i < LENGTH(tags)) ? i : 0;
  855. }
  856. void
  857. initfont(const char *fontstr) {
  858. char *def, **missing;
  859. int i, n;
  860. missing = NULL;
  861. if(dc.font.set)
  862. XFreeFontSet(dpy, dc.font.set);
  863. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  864. if(missing) {
  865. while(n--)
  866. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  867. XFreeStringList(missing);
  868. }
  869. if(dc.font.set) {
  870. XFontSetExtents *font_extents;
  871. XFontStruct **xfonts;
  872. char **font_names;
  873. dc.font.ascent = dc.font.descent = 0;
  874. font_extents = XExtentsOfFontSet(dc.font.set);
  875. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  876. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  877. if(dc.font.ascent < (*xfonts)->ascent)
  878. dc.font.ascent = (*xfonts)->ascent;
  879. if(dc.font.descent < (*xfonts)->descent)
  880. dc.font.descent = (*xfonts)->descent;
  881. xfonts++;
  882. }
  883. }
  884. else {
  885. if(dc.font.xfont)
  886. XFreeFont(dpy, dc.font.xfont);
  887. dc.font.xfont = NULL;
  888. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  889. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  890. eprint("error, cannot load font: '%s'\n", fontstr);
  891. dc.font.ascent = dc.font.xfont->ascent;
  892. dc.font.descent = dc.font.xfont->descent;
  893. }
  894. dc.font.height = dc.font.ascent + dc.font.descent;
  895. }
  896. Bool
  897. isoccupied(unsigned int t) {
  898. Client *c;
  899. for(c = clients; c; c = c->next)
  900. if(c->tags[t])
  901. return True;
  902. return False;
  903. }
  904. Bool
  905. isprotodel(Client *c) {
  906. int i, n;
  907. Atom *protocols;
  908. Bool ret = False;
  909. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  910. for(i = 0; !ret && i < n; i++)
  911. if(protocols[i] == wmatom[WMDelete])
  912. ret = True;
  913. XFree(protocols);
  914. }
  915. return ret;
  916. }
  917. Bool
  918. isurgent(unsigned int t) {
  919. Client *c;
  920. for(c = clients; c; c = c->next)
  921. if(c->isurgent && c->tags[t])
  922. return True;
  923. return False;
  924. }
  925. Bool
  926. isvisible(Client *c) {
  927. unsigned int i;
  928. for(i = 0; i < LENGTH(tags); i++)
  929. if(c->tags[i] && seltags[i])
  930. return True;
  931. return False;
  932. }
  933. void
  934. keypress(XEvent *e) {
  935. unsigned int i;
  936. KeySym keysym;
  937. XKeyEvent *ev;
  938. ev = &e->xkey;
  939. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  940. for(i = 0; i < LENGTH(keys); i++)
  941. if(keysym == keys[i].keysym
  942. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state))
  943. {
  944. if(keys[i].func)
  945. keys[i].func(keys[i].arg);
  946. }
  947. }
  948. void
  949. killclient(const char *arg) {
  950. XEvent ev;
  951. if(!sel)
  952. return;
  953. if(isprotodel(sel)) {
  954. ev.type = ClientMessage;
  955. ev.xclient.window = sel->win;
  956. ev.xclient.message_type = wmatom[WMProtocols];
  957. ev.xclient.format = 32;
  958. ev.xclient.data.l[0] = wmatom[WMDelete];
  959. ev.xclient.data.l[1] = CurrentTime;
  960. XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
  961. }
  962. else
  963. XKillClient(dpy, sel->win);
  964. }
  965. void
  966. manage(Window w, XWindowAttributes *wa) {
  967. Client *c, *t = NULL;
  968. View *v;
  969. Status rettrans;
  970. Window trans;
  971. XWindowChanges wc;
  972. c = emallocz(sizeof(Client));
  973. c->tags = emallocz(sizeof initags);
  974. c->win = w;
  975. applyrules(c);
  976. v = getview(c);
  977. c->x = wa->x + v->x;
  978. c->y = wa->y + v->y;
  979. c->w = wa->width;
  980. c->h = wa->height;
  981. c->oldborder = wa->border_width;
  982. if(c->w == v->w && c->h == v->h) {
  983. c->x = v->x;
  984. c->y = v->y;
  985. c->border = wa->border_width;
  986. }
  987. else {
  988. if(c->x + c->w + 2 * c->border > v->wax + v->waw)
  989. c->x = v->wax + v->waw - c->w - 2 * c->border;
  990. if(c->y + c->h + 2 * c->border > v->way + v->wah)
  991. c->y = v->way + v->wah - c->h - 2 * c->border;
  992. if(c->x < v->wax)
  993. c->x = v->wax;
  994. if(c->y < v->way)
  995. c->y = v->way;
  996. c->border = BORDERPX;
  997. }
  998. wc.border_width = c->border;
  999. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  1000. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  1001. configure(c); /* propagates border_width, if size doesn't change */
  1002. updatesizehints(c);
  1003. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  1004. grabbuttons(c, False);
  1005. updatetitle(c);
  1006. if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
  1007. for(t = clients; t && t->win != trans; t = t->next);
  1008. if(t)
  1009. memcpy(c->tags, t->tags, sizeof initags);
  1010. if(!c->isfloating)
  1011. c->isfloating = (rettrans == Success) || c->isfixed;
  1012. attach(c);
  1013. attachstack(c);
  1014. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
  1015. ban(c);
  1016. XMapWindow(dpy, c->win);
  1017. setclientstate(c, NormalState);
  1018. arrange();
  1019. }
  1020. void
  1021. mappingnotify(XEvent *e) {
  1022. XMappingEvent *ev = &e->xmapping;
  1023. XRefreshKeyboardMapping(ev);
  1024. if(ev->request == MappingKeyboard)
  1025. grabkeys();
  1026. }
  1027. void
  1028. maprequest(XEvent *e) {
  1029. static XWindowAttributes wa;
  1030. XMapRequestEvent *ev = &e->xmaprequest;
  1031. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1032. return;
  1033. if(wa.override_redirect)
  1034. return;
  1035. if(!getclient(ev->window))
  1036. manage(ev->window, &wa);
  1037. }
  1038. View *
  1039. viewat() {
  1040. int i, x, y;
  1041. Window win;
  1042. unsigned int mask;
  1043. XQueryPointer(dpy, root, &win, &win, &x, &y, &i, &i, &mask);
  1044. for(i = 0; i < nviews; i++) {
  1045. if((x >= views[i].x && x < views[i].x + views[i].w)
  1046. && (y >= views[i].y && y < views[i].y + views[i].h)) {
  1047. return &views[i];
  1048. }
  1049. }
  1050. return NULL;
  1051. }
  1052. void
  1053. movemouse(Client *c) {
  1054. int x1, y1, ocx, ocy, di, nx, ny;
  1055. unsigned int dui;
  1056. View *v;
  1057. Window dummy;
  1058. XEvent ev;
  1059. ocx = nx = c->x;
  1060. ocy = ny = c->y;
  1061. v = getview(c);
  1062. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1063. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1064. return;
  1065. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  1066. for(;;) {
  1067. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1068. switch (ev.type) {
  1069. case ButtonRelease:
  1070. XUngrabPointer(dpy, CurrentTime);
  1071. return;
  1072. case ConfigureRequest:
  1073. case Expose:
  1074. case MapRequest:
  1075. handler[ev.type](&ev);
  1076. break;
  1077. case MotionNotify:
  1078. XSync(dpy, False);
  1079. nx = ocx + (ev.xmotion.x - x1);
  1080. ny = ocy + (ev.xmotion.y - y1);
  1081. if(abs(v->wax - nx) < SNAP)
  1082. nx = v->wax;
  1083. else if(abs((v->wax + v->waw) - (nx + c->w + 2 * c->border)) < SNAP)
  1084. nx = v->wax + v->waw - c->w - 2 * c->border;
  1085. if(abs(v->way - ny) < SNAP)
  1086. ny = v->way;
  1087. else if(abs((v->way + v->wah) - (ny + c->h + 2 * c->border)) < SNAP)
  1088. ny = v->way + v->wah - c->h - 2 * c->border;
  1089. if(!c->isfloating && (v->layout->arrange != floating) && (abs(nx - c->x) > SNAP || abs(ny - c->y) > SNAP))
  1090. togglefloating(NULL);
  1091. if((v->layout->arrange == floating) || c->isfloating)
  1092. resize(c, nx, ny, c->w, c->h, False);
  1093. break;
  1094. }
  1095. }
  1096. }
  1097. Client *
  1098. nexttiled(Client *c, View *v) {
  1099. for(; c && (c->isfloating || getview(c) != v || !isvisible(c)); c = c->next);
  1100. return c;
  1101. }
  1102. void
  1103. propertynotify(XEvent *e) {
  1104. Client *c;
  1105. Window trans;
  1106. XPropertyEvent *ev = &e->xproperty;
  1107. if(ev->state == PropertyDelete)
  1108. return; /* ignore */
  1109. if((c = getclient(ev->window))) {
  1110. switch (ev->atom) {
  1111. default: break;
  1112. case XA_WM_TRANSIENT_FOR:
  1113. XGetTransientForHint(dpy, c->win, &trans);
  1114. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  1115. arrange();
  1116. break;
  1117. case XA_WM_NORMAL_HINTS:
  1118. updatesizehints(c);
  1119. break;
  1120. case XA_WM_HINTS:
  1121. updatewmhints(c);
  1122. drawbar(getview(c));
  1123. break;
  1124. }
  1125. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1126. updatetitle(c);
  1127. if(c == sel)
  1128. drawbar(selview);
  1129. }
  1130. }
  1131. }
  1132. void
  1133. quit(const char *arg) {
  1134. readin = running = False;
  1135. }
  1136. void
  1137. reapply(const char *arg) {
  1138. static Bool zerotags[LENGTH(tags)] = { 0 };
  1139. Client *c;
  1140. for(c = clients; c; c = c->next) {
  1141. memcpy(c->tags, zerotags, sizeof zerotags);
  1142. applyrules(c);
  1143. }
  1144. arrange();
  1145. }
  1146. void
  1147. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  1148. View *v;
  1149. XWindowChanges wc;
  1150. v = getview(c);
  1151. if(sizehints) {
  1152. /* set minimum possible */
  1153. if (w < 1)
  1154. w = 1;
  1155. if (h < 1)
  1156. h = 1;
  1157. /* temporarily remove base dimensions */
  1158. w -= c->basew;
  1159. h -= c->baseh;
  1160. /* adjust for aspect limits */
  1161. if (c->minay > 0 && c->maxay > 0 && c->minax > 0 && c->maxax > 0) {
  1162. if (w * c->maxay > h * c->maxax)
  1163. w = h * c->maxax / c->maxay;
  1164. else if (w * c->minay < h * c->minax)
  1165. h = w * c->minay / c->minax;
  1166. }
  1167. /* adjust for increment value */
  1168. if(c->incw)
  1169. w -= w % c->incw;
  1170. if(c->inch)
  1171. h -= h % c->inch;
  1172. /* restore base dimensions */
  1173. w += c->basew;
  1174. h += c->baseh;
  1175. if(c->minw > 0 && w < c->minw)
  1176. w = c->minw;
  1177. if(c->minh > 0 && h < c->minh)
  1178. h = c->minh;
  1179. if(c->maxw > 0 && w > c->maxw)
  1180. w = c->maxw;
  1181. if(c->maxh > 0 && h > c->maxh)
  1182. h = c->maxh;
  1183. }
  1184. if(w <= 0 || h <= 0)
  1185. return;
  1186. if(x > v->x + v->w)
  1187. x = v->w - w - 2 * c->border;
  1188. if(y > v->y + v->h)
  1189. y = v->h - h - 2 * c->border;
  1190. if(x + w + 2 * c->border < v->x)
  1191. x = v->x;
  1192. if(y + h + 2 * c->border < v->y)
  1193. y = v->y;
  1194. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  1195. c->x = wc.x = x;
  1196. c->y = wc.y = y;
  1197. c->w = wc.width = w;
  1198. c->h = wc.height = h;
  1199. wc.border_width = c->border;
  1200. XConfigureWindow(dpy, c->win,
  1201. CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1202. configure(c);
  1203. XSync(dpy, False);
  1204. }
  1205. }
  1206. void
  1207. resizemouse(Client *c) {
  1208. int ocx, ocy;
  1209. int nw, nh;
  1210. View *v;
  1211. XEvent ev;
  1212. ocx = c->x;
  1213. ocy = c->y;
  1214. v = getview(c);
  1215. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1216. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1217. return;
  1218. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
  1219. for(;;) {
  1220. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask , &ev);
  1221. switch(ev.type) {
  1222. case ButtonRelease:
  1223. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
  1224. c->w + c->border - 1, c->h + c->border - 1);
  1225. XUngrabPointer(dpy, CurrentTime);
  1226. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1227. return;
  1228. case ConfigureRequest:
  1229. case Expose:
  1230. case MapRequest:
  1231. handler[ev.type](&ev);
  1232. break;
  1233. case MotionNotify:
  1234. XSync(dpy, False);
  1235. if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
  1236. nw = 1;
  1237. if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
  1238. nh = 1;
  1239. if(!c->isfloating && (v->layout->arrange != floating) && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP))
  1240. togglefloating(NULL);
  1241. if((v->layout->arrange == floating) || c->isfloating)
  1242. resize(c, c->x, c->y, nw, nh, True);
  1243. break;
  1244. }
  1245. }
  1246. }
  1247. void
  1248. restack(View *v) {
  1249. Client *c;
  1250. XEvent ev;
  1251. XWindowChanges wc;
  1252. drawbar(v);
  1253. if(!sel)
  1254. return;
  1255. if(sel->isfloating || (v->layout->arrange == floating))
  1256. XRaiseWindow(dpy, sel->win);
  1257. if(v->layout->arrange != floating) {
  1258. wc.stack_mode = Below;
  1259. wc.sibling = v->barwin;
  1260. if(!sel->isfloating) {
  1261. XConfigureWindow(dpy, sel->win, CWSibling|CWStackMode, &wc);
  1262. wc.sibling = sel->win;
  1263. }
  1264. for(c = nexttiled(clients, v); c; c = nexttiled(c->next, v)) {
  1265. if(c == sel)
  1266. continue;
  1267. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1268. wc.sibling = c->win;
  1269. }
  1270. }
  1271. XSync(dpy, False);
  1272. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1273. }
  1274. void
  1275. run(void) {
  1276. char *p;
  1277. char buf[sizeof stext];
  1278. fd_set rd;
  1279. int r, xfd;
  1280. unsigned int len, offset;
  1281. XEvent ev;
  1282. /* main event loop, also reads status text from stdin */
  1283. XSync(dpy, False);
  1284. xfd = ConnectionNumber(dpy);
  1285. readin = True;
  1286. offset = 0;
  1287. len = sizeof stext - 1;
  1288. buf[len] = stext[len] = '\0'; /* 0-terminator is never touched */
  1289. while(running) {
  1290. FD_ZERO(&rd);
  1291. if(readin)
  1292. FD_SET(STDIN_FILENO, &rd);
  1293. FD_SET(xfd, &rd);
  1294. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1295. if(errno == EINTR)
  1296. continue;
  1297. eprint("select failed\n");
  1298. }
  1299. if(FD_ISSET(STDIN_FILENO, &rd)) {
  1300. switch((r = read(STDIN_FILENO, buf + offset, len - offset))) {
  1301. case -1:
  1302. strncpy(stext, strerror(errno), len);
  1303. readin = False;
  1304. break;
  1305. case 0:
  1306. strncpy(stext, "EOF", 4);
  1307. readin = False;
  1308. break;
  1309. default:
  1310. for(p = buf + offset; r > 0; p++, r--, offset++)
  1311. if(*p == '\n' || *p == '\0') {
  1312. *p = '\0';
  1313. strncpy(stext, buf, len);
  1314. p += r - 1; /* p is buf + offset + r - 1 */
  1315. for(r = 0; *(p - r) && *(p - r) != '\n'; r++);
  1316. offset = r;
  1317. if(r)
  1318. memmove(buf, p - r + 1, r);
  1319. break;
  1320. }
  1321. break;
  1322. }
  1323. drawbar(selview);
  1324. }
  1325. while(XPending(dpy)) {
  1326. XNextEvent(dpy, &ev);
  1327. if(handler[ev.type])
  1328. (handler[ev.type])(&ev); /* call handler */
  1329. }
  1330. }
  1331. }
  1332. void
  1333. scan(void) {
  1334. unsigned int i, num;
  1335. Window *wins, d1, d2;
  1336. XWindowAttributes wa;
  1337. wins = NULL;
  1338. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1339. for(i = 0; i < num; i++) {
  1340. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1341. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1342. continue;
  1343. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1344. manage(wins[i], &wa);
  1345. }
  1346. for(i = 0; i < num; i++) { /* now the transients */
  1347. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1348. continue;
  1349. if(XGetTransientForHint(dpy, wins[i], &d1)
  1350. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1351. manage(wins[i], &wa);
  1352. }
  1353. }
  1354. if(wins)
  1355. XFree(wins);
  1356. }
  1357. void
  1358. setclientstate(Client *c, long state) {
  1359. long data[] = {state, None};
  1360. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1361. PropModeReplace, (unsigned char *)data, 2);
  1362. }
  1363. void
  1364. setlayout(const char *arg) {
  1365. unsigned int i;
  1366. View *v = selview;
  1367. if(!arg) {
  1368. v->layout++;
  1369. if(v->layout == &layouts[LENGTH(layouts)])
  1370. v->layout = &layouts[0];
  1371. }
  1372. else {
  1373. for(i = 0; i < LENGTH(layouts); i++)
  1374. if(!strcmp(arg, layouts[i].symbol))
  1375. break;
  1376. if(i == LENGTH(layouts))
  1377. return;
  1378. v->layout = &layouts[i];
  1379. }
  1380. if(sel)
  1381. arrange();
  1382. else
  1383. drawbar(v);
  1384. }
  1385. void
  1386. setmwfact(const char *arg) {
  1387. double delta;
  1388. View *v = selview;
  1389. if(!domwfact)
  1390. return;
  1391. /* arg handling, manipulate mwfact */
  1392. if(arg == NULL)
  1393. v->mwfact = MWFACT;
  1394. else if(sscanf(arg, "%lf", &delta) == 1) {
  1395. if(arg[0] == '+' || arg[0] == '-')
  1396. v->mwfact += delta;
  1397. else
  1398. v->mwfact = delta;
  1399. if(v->mwfact < 0.1)
  1400. v->mwfact = 0.1;
  1401. else if(v->mwfact > 0.9)
  1402. v->mwfact = 0.9;
  1403. }
  1404. arrange();
  1405. }
  1406. void
  1407. setup(void) {
  1408. unsigned int i;
  1409. View *v;
  1410. XSetWindowAttributes wa;
  1411. XineramaScreenInfo *info = NULL;
  1412. /* init atoms */
  1413. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1414. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1415. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1416. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1417. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1418. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1419. /* init cursors */
  1420. wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1421. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1422. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1423. if((isxinerama = XineramaIsActive(dpy)))
  1424. info = XineramaQueryScreens(dpy, &nviews);
  1425. #if defined(AIM_XINERAMA)
  1426. isxinerama = True;
  1427. nviews = 2; /* aim Xinerama */
  1428. #endif
  1429. selview = views = emallocz(nviews * sizeof(View));
  1430. screen = DefaultScreen(dpy);
  1431. root = RootWindow(dpy, screen);
  1432. /* init appearance */
  1433. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  1434. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  1435. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  1436. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  1437. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  1438. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  1439. initfont(FONT);
  1440. dc.h = bh = dc.font.height + 2;
  1441. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1442. dc.gc = XCreateGC(dpy, root, 0, 0);
  1443. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1444. if(!dc.font.set)
  1445. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1446. for(blw = i = 0; i < LENGTH(layouts); i++) {
  1447. i = textw(layouts[i].symbol);
  1448. if(i > blw)
  1449. blw = i;
  1450. }
  1451. seltags = emallocz(sizeof initags);
  1452. prevtags = emallocz(sizeof initags);
  1453. memcpy(seltags, initags, sizeof initags);
  1454. memcpy(prevtags, initags, sizeof initags);
  1455. for(i = 0; i < nviews; i++) {
  1456. /* init geometry */
  1457. v = &views[i];
  1458. v->id = i + 1;
  1459. if(nviews != 1 && isxinerama) {
  1460. #if defined(AIM_XINERAMA)
  1461. v->w = DisplayWidth(dpy, screen) / 2;
  1462. v->x = (i == 0) ? 0 : v->w;
  1463. v->y = 0;
  1464. v->h = DisplayHeight(dpy, screen);
  1465. #else
  1466. v->x = info[i].x_org;
  1467. v->y = info[i].y_org;
  1468. v->w = info[i].width;
  1469. v->h = info[i].height;
  1470. #endif
  1471. }
  1472. else {
  1473. v->x = 0;
  1474. v->y = 0;
  1475. v->w = DisplayWidth(dpy, screen);
  1476. v->h = DisplayHeight(dpy, screen);
  1477. }
  1478. /* init layouts */
  1479. v->mwfact = MWFACT;
  1480. v->layout = &layouts[0];
  1481. // TODO: bpos per screen?
  1482. bpos = BARPOS;
  1483. wa.override_redirect = 1;
  1484. wa.background_pixmap = ParentRelative;
  1485. wa.event_mask = ButtonPressMask|ExposureMask;
  1486. /* init bars */
  1487. v->barwin = XCreateWindow(dpy, root, v->x, v->y, v->w, bh, 0,
  1488. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  1489. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1490. XDefineCursor(dpy, v->barwin, cursor[CurNormal]);
  1491. updatebarpos(v);
  1492. XMapRaised(dpy, v->barwin);
  1493. strcpy(stext, "dwm-"VERSION);
  1494. /* EWMH support per view */
  1495. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1496. PropModeReplace, (unsigned char *) netatom, NetLast);
  1497. /* select for events */
  1498. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
  1499. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask;
  1500. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1501. XSelectInput(dpy, root, wa.event_mask);
  1502. drawbar(v);
  1503. }
  1504. if(info)
  1505. XFree(info);
  1506. /* grab keys */
  1507. grabkeys();
  1508. /* init tags */
  1509. compileregs();
  1510. selview = &views[0];
  1511. }
  1512. void
  1513. spawn(const char *arg) {
  1514. static char *shell = NULL;
  1515. if(!shell && !(shell = getenv("SHELL")))
  1516. shell = "/bin/sh";
  1517. if(!arg)
  1518. return;
  1519. /* The double-fork construct avoids zombie processes and keeps the code
  1520. * clean from stupid signal handlers. */
  1521. if(fork() == 0) {
  1522. if(fork() == 0) {
  1523. if(dpy)
  1524. close(ConnectionNumber(dpy));
  1525. setsid();
  1526. execl(shell, shell, "-c", arg, (char *)NULL);
  1527. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
  1528. perror(" failed");
  1529. }
  1530. exit(0);
  1531. }
  1532. wait(0);
  1533. }
  1534. void
  1535. tag(const char *arg) {
  1536. unsigned int i;
  1537. if(!sel)
  1538. return;
  1539. for(i = 0; i < LENGTH(tags); i++)
  1540. sel->tags[i] = (NULL == arg) ? selview->id : 0;
  1541. sel->tags[idxoftag(arg)] = selview->id;
  1542. arrange();
  1543. }
  1544. unsigned int
  1545. textnw(const char *text, unsigned int len) {
  1546. XRectangle r;
  1547. if(dc.font.set) {
  1548. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1549. return r.width;
  1550. }
  1551. return XTextWidth(dc.font.xfont, text, len);
  1552. }
  1553. unsigned int
  1554. textw(const char *text) {
  1555. return textnw(text, strlen(text)) + dc.font.height;
  1556. }
  1557. void
  1558. tile(View *v) {
  1559. unsigned int i, n, nx, ny, nw, nh, mw, th;
  1560. Client *c, *mc;
  1561. domwfact = dozoom = True;
  1562. nx = v->wax;
  1563. ny = v->way;
  1564. nw = 0;
  1565. for(n = 0, c = nexttiled(clients, v); c; c = nexttiled(c->next, v))
  1566. n++;
  1567. /* window geoms */
  1568. mw = (n == 1) ? v->waw : v->mwfact * v->waw;
  1569. th = (n > 1) ? v->wah / (n - 1) : 0;
  1570. if(n > 1 && th < bh)
  1571. th = v->wah;
  1572. for(i = 0, c = mc = nexttiled(clients, v); c; c = nexttiled(c->next, v)) {
  1573. if(i == 0) { /* master */
  1574. nx = v->wax;
  1575. ny = v->way;
  1576. nw = mw - 2 * c->border;
  1577. nh = v->wah - 2 * c->border;
  1578. }
  1579. else { /* tile window */
  1580. if(i == 1) {
  1581. ny = v->way;
  1582. nx += mc->w + 2 * mc->border;
  1583. nw = v->waw - mw - 2 * c->border;
  1584. }
  1585. if(i + 1 == n) /* remainder */
  1586. nh = (v->way + v->wah) - ny - 2 * c->border;
  1587. else
  1588. nh = th - 2 * c->border;
  1589. }
  1590. fprintf(stderr, "tile %d %d %d %d\n", nx, ny, nw, nh);
  1591. resize(c, nx, ny, nw, nh, RESIZEHINTS);
  1592. if((RESIZEHINTS) && ((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw)))
  1593. /* client doesn't accept size constraints */
  1594. resize(c, nx, ny, nw, nh, False);
  1595. if(n > 1 && th != v->wah)
  1596. ny = c->y + c->h + 2 * c->border;
  1597. i++;
  1598. }
  1599. }
  1600. void
  1601. togglebar(const char *arg) {
  1602. if(bpos == BarOff)
  1603. bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
  1604. else
  1605. bpos = BarOff;
  1606. updatebarpos(selview);
  1607. arrange();
  1608. }
  1609. void
  1610. togglefloating(const char *arg) {
  1611. if(!sel)
  1612. return;
  1613. sel->isfloating = !sel->isfloating;
  1614. if(sel->isfloating)
  1615. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1616. arrange();
  1617. }
  1618. void
  1619. toggletag(const char *arg) {
  1620. unsigned int i, j;
  1621. if(!sel)
  1622. return;
  1623. i = idxoftag(arg);
  1624. sel->tags[i] = !sel->tags[i];
  1625. for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
  1626. if(j == LENGTH(tags))
  1627. sel->tags[i] = selview->id; /* at least one tag must be enabled */
  1628. arrange();
  1629. }
  1630. void
  1631. toggleview(const char *arg) {
  1632. unsigned int i, j;
  1633. i = idxoftag(arg);
  1634. seltags[i] = !seltags[i];
  1635. for(j = 0; j < LENGTH(tags) && !seltags[j]; j++);
  1636. if(j == LENGTH(tags))
  1637. seltags[i] = selview->id; /* at least one tag must be viewed */
  1638. arrange();
  1639. }
  1640. void
  1641. unban(Client *c) {
  1642. if(!c->isbanned)
  1643. return;
  1644. XMoveWindow(dpy, c->win, c->x, c->y);
  1645. c->isbanned = False;
  1646. }
  1647. void
  1648. unmanage(Client *c) {
  1649. XWindowChanges wc;
  1650. wc.border_width = c->oldborder;
  1651. /* The server grab construct avoids race conditions. */
  1652. XGrabServer(dpy);
  1653. XSetErrorHandler(xerrordummy);
  1654. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1655. detach(c);
  1656. detachstack(c);
  1657. if(sel == c)
  1658. focus(NULL);
  1659. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1660. setclientstate(c, WithdrawnState);
  1661. free(c->tags);
  1662. free(c);
  1663. XSync(dpy, False);
  1664. XSetErrorHandler(xerror);
  1665. XUngrabServer(dpy);
  1666. arrange();
  1667. }
  1668. void
  1669. unmapnotify(XEvent *e) {
  1670. Client *c;
  1671. XUnmapEvent *ev = &e->xunmap;
  1672. if((c = getclient(ev->window)))
  1673. unmanage(c);
  1674. }
  1675. void
  1676. updatebarpos(View *v) {
  1677. XEvent ev;
  1678. v->wax = v->x;
  1679. v->way = v->y;
  1680. v->wah = v->h;
  1681. v->waw = v->w;
  1682. switch(bpos) {
  1683. default:
  1684. v->wah -= bh;
  1685. v->way += bh;
  1686. XMoveWindow(dpy, v->barwin, v->x, v->y);
  1687. break;
  1688. case BarBot:
  1689. v->wah -= bh;
  1690. XMoveWindow(dpy, v->barwin, v->x, v->y + v->wah);
  1691. break;
  1692. case BarOff:
  1693. XMoveWindow(dpy, v->barwin, v->x, v->y - bh);
  1694. break;
  1695. }
  1696. XSync(dpy, False);
  1697. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1698. }
  1699. void
  1700. updatesizehints(Client *c) {
  1701. long msize;
  1702. XSizeHints size;
  1703. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  1704. size.flags = PSize;
  1705. c->flags = size.flags;
  1706. if(c->flags & PBaseSize) {
  1707. c->basew = size.base_width;
  1708. c->baseh = size.base_height;
  1709. }
  1710. else if(c->flags & PMinSize) {
  1711. c->basew = size.min_width;
  1712. c->baseh = size.min_height;
  1713. }
  1714. else
  1715. c->basew = c->baseh = 0;
  1716. if(c->flags & PResizeInc) {
  1717. c->incw = size.width_inc;
  1718. c->inch = size.height_inc;
  1719. }
  1720. else
  1721. c->incw = c->inch = 0;
  1722. if(c->flags & PMaxSize) {
  1723. c->maxw = size.max_width;
  1724. c->maxh = size.max_height;
  1725. }
  1726. else
  1727. c->maxw = c->maxh = 0;
  1728. if(c->flags & PMinSize) {
  1729. c->minw = size.min_width;
  1730. c->minh = size.min_height;
  1731. }
  1732. else if(c->flags & PBaseSize) {
  1733. c->minw = size.base_width;
  1734. c->minh = size.base_height;
  1735. }
  1736. else
  1737. c->minw = c->minh = 0;
  1738. if(c->flags & PAspect) {
  1739. c->minax = size.min_aspect.x;
  1740. c->maxax = size.max_aspect.x;
  1741. c->minay = size.min_aspect.y;
  1742. c->maxay = size.max_aspect.y;
  1743. }
  1744. else
  1745. c->minax = c->maxax = c->minay = c->maxay = 0;
  1746. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1747. && c->maxw == c->minw && c->maxh == c->minh);
  1748. }
  1749. void
  1750. updatetitle(Client *c) {
  1751. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1752. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1753. }
  1754. void
  1755. updatewmhints(Client *c) {
  1756. XWMHints *wmh;
  1757. if((wmh = XGetWMHints(dpy, c->win))) {
  1758. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1759. XFree(wmh);
  1760. }
  1761. }
  1762. /* There's no way to check accesses to destroyed windows, thus those cases are
  1763. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1764. * default error handler, which may call exit. */
  1765. int
  1766. xerror(Display *dpy, XErrorEvent *ee) {
  1767. if(ee->error_code == BadWindow
  1768. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1769. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1770. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1771. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1772. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1773. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1774. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1775. return 0;
  1776. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1777. ee->request_code, ee->error_code);
  1778. return xerrorxlib(dpy, ee); /* may call exit */
  1779. }
  1780. int
  1781. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1782. return 0;
  1783. }
  1784. /* Startup Error handler to check if another window manager
  1785. * is already running. */
  1786. int
  1787. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1788. otherwm = True;
  1789. return -1;
  1790. }
  1791. void
  1792. view(const char *arg) {
  1793. unsigned int i;
  1794. int tmp[LENGTH(tags)];
  1795. for(i = 0; i < LENGTH(tags); i++)
  1796. tmp[i] = (NULL == arg) ? selview->id : 0;
  1797. tmp[idxoftag(arg)] = selview->id;
  1798. if(memcmp(seltags, tmp, sizeof initags) != 0) {
  1799. memcpy(prevtags, seltags, sizeof initags);
  1800. memcpy(seltags, tmp, sizeof initags);
  1801. arrange();
  1802. }
  1803. }
  1804. void
  1805. viewprevtag(const char *arg) {
  1806. static Bool tmp[LENGTH(tags)];
  1807. memcpy(tmp, seltags, sizeof initags);
  1808. memcpy(seltags, prevtags, sizeof initags);
  1809. memcpy(prevtags, tmp, sizeof initags);
  1810. arrange();
  1811. }
  1812. void
  1813. zoom(const char *arg) {
  1814. Client *c = sel;
  1815. if(!sel || !dozoom || sel->isfloating)
  1816. return;
  1817. if(c == nexttiled(clients, getview(c)))
  1818. if(!(c = nexttiled(c->next, getview(c))))
  1819. return;
  1820. detach(c);
  1821. attach(c);
  1822. focus(c);
  1823. arrange();
  1824. }
  1825. void
  1826. selectview(const char *arg) {
  1827. int i;
  1828. View *v;
  1829. if(!arg)
  1830. return;
  1831. if(arg)
  1832. i = atoi(arg);
  1833. v = &views[i % nviews];
  1834. XWarpPointer(dpy, None, root, 0, 0, 0, 0, v->wax+v->waw/2, v->way+v->wah/2);
  1835. focus(NULL);
  1836. }
  1837. int
  1838. main(int argc, char *argv[]) {
  1839. if(argc == 2 && !strcmp("-v", argv[1]))
  1840. eprint("dwm-"VERSION", © 2006-2008 dwm engineers, see LICENSE for details\n");
  1841. else if(argc != 1)
  1842. eprint("usage: dwm [-v]\n");
  1843. setlocale(LC_CTYPE, "");
  1844. if(!(dpy = XOpenDisplay(0)))
  1845. eprint("dwm: cannot open display\n");
  1846. checkotherwm();
  1847. setup();
  1848. scan();
  1849. run();
  1850. cleanup();
  1851. XCloseDisplay(dpy);
  1852. return 0;
  1853. }