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.

2059 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) || getview(c) != v); c = c->snext);
  519. for(i = 0; i < LENGTH(tags); i++) {
  520. dc.w = textw(tags[i]);
  521. if(seltags[i] && seltags[i] == v->id) {
  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) || getview(c) != selview); 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. return NULL;
  1050. }
  1051. void
  1052. movemouse(Client *c) {
  1053. int x1, y1, ocx, ocy, di, nx, ny;
  1054. unsigned int dui;
  1055. View *v;
  1056. Window dummy;
  1057. XEvent ev;
  1058. ocx = nx = c->x;
  1059. ocy = ny = c->y;
  1060. v = getview(c);
  1061. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1062. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1063. return;
  1064. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  1065. for(;;) {
  1066. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1067. switch (ev.type) {
  1068. case ButtonRelease:
  1069. XUngrabPointer(dpy, CurrentTime);
  1070. return;
  1071. case ConfigureRequest:
  1072. case Expose:
  1073. case MapRequest:
  1074. handler[ev.type](&ev);
  1075. break;
  1076. case MotionNotify:
  1077. XSync(dpy, False);
  1078. nx = ocx + (ev.xmotion.x - x1);
  1079. ny = ocy + (ev.xmotion.y - y1);
  1080. if(abs(v->wax - nx) < SNAP)
  1081. nx = v->wax;
  1082. else if(abs((v->wax + v->waw) - (nx + c->w + 2 * c->border)) < SNAP)
  1083. nx = v->wax + v->waw - c->w - 2 * c->border;
  1084. if(abs(v->way - ny) < SNAP)
  1085. ny = v->way;
  1086. else if(abs((v->way + v->wah) - (ny + c->h + 2 * c->border)) < SNAP)
  1087. ny = v->way + v->wah - c->h - 2 * c->border;
  1088. if(!c->isfloating && (v->layout->arrange != floating) && (abs(nx - c->x) > SNAP || abs(ny - c->y) > SNAP))
  1089. togglefloating(NULL);
  1090. if((v->layout->arrange == floating) || c->isfloating)
  1091. resize(c, nx, ny, c->w, c->h, False);
  1092. break;
  1093. }
  1094. }
  1095. }
  1096. Client *
  1097. nexttiled(Client *c, View *v) {
  1098. for(; c && (c->isfloating || getview(c) != v || !isvisible(c)); c = c->next);
  1099. return c;
  1100. }
  1101. void
  1102. propertynotify(XEvent *e) {
  1103. Client *c;
  1104. Window trans;
  1105. XPropertyEvent *ev = &e->xproperty;
  1106. if(ev->state == PropertyDelete)
  1107. return; /* ignore */
  1108. if((c = getclient(ev->window))) {
  1109. switch (ev->atom) {
  1110. default: break;
  1111. case XA_WM_TRANSIENT_FOR:
  1112. XGetTransientForHint(dpy, c->win, &trans);
  1113. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  1114. arrange();
  1115. break;
  1116. case XA_WM_NORMAL_HINTS:
  1117. updatesizehints(c);
  1118. break;
  1119. case XA_WM_HINTS:
  1120. updatewmhints(c);
  1121. drawbar(getview(c));
  1122. break;
  1123. }
  1124. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1125. updatetitle(c);
  1126. if(c == sel)
  1127. drawbar(selview);
  1128. }
  1129. }
  1130. }
  1131. void
  1132. quit(const char *arg) {
  1133. readin = running = False;
  1134. }
  1135. void
  1136. reapply(const char *arg) {
  1137. static Bool zerotags[LENGTH(tags)] = { 0 };
  1138. Client *c;
  1139. for(c = clients; c; c = c->next) {
  1140. memcpy(c->tags, zerotags, sizeof zerotags);
  1141. applyrules(c);
  1142. }
  1143. arrange();
  1144. }
  1145. void
  1146. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  1147. View *v;
  1148. XWindowChanges wc;
  1149. v = getview(c);
  1150. if(sizehints) {
  1151. /* set minimum possible */
  1152. if (w < 1)
  1153. w = 1;
  1154. if (h < 1)
  1155. h = 1;
  1156. /* temporarily remove base dimensions */
  1157. w -= c->basew;
  1158. h -= c->baseh;
  1159. /* adjust for aspect limits */
  1160. if (c->minay > 0 && c->maxay > 0 && c->minax > 0 && c->maxax > 0) {
  1161. if (w * c->maxay > h * c->maxax)
  1162. w = h * c->maxax / c->maxay;
  1163. else if (w * c->minay < h * c->minax)
  1164. h = w * c->minay / c->minax;
  1165. }
  1166. /* adjust for increment value */
  1167. if(c->incw)
  1168. w -= w % c->incw;
  1169. if(c->inch)
  1170. h -= h % c->inch;
  1171. /* restore base dimensions */
  1172. w += c->basew;
  1173. h += c->baseh;
  1174. if(c->minw > 0 && w < c->minw)
  1175. w = c->minw;
  1176. if(c->minh > 0 && h < c->minh)
  1177. h = c->minh;
  1178. if(c->maxw > 0 && w > c->maxw)
  1179. w = c->maxw;
  1180. if(c->maxh > 0 && h > c->maxh)
  1181. h = c->maxh;
  1182. }
  1183. if(w <= 0 || h <= 0)
  1184. return;
  1185. if(x > v->x + v->w)
  1186. x = v->w - w - 2 * c->border;
  1187. if(y > v->y + v->h)
  1188. y = v->h - h - 2 * c->border;
  1189. if(x + w + 2 * c->border < v->x)
  1190. x = v->x;
  1191. if(y + h + 2 * c->border < v->y)
  1192. y = v->y;
  1193. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  1194. c->x = wc.x = x;
  1195. c->y = wc.y = y;
  1196. c->w = wc.width = w;
  1197. c->h = wc.height = h;
  1198. wc.border_width = c->border;
  1199. XConfigureWindow(dpy, c->win,
  1200. CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1201. configure(c);
  1202. XSync(dpy, False);
  1203. }
  1204. }
  1205. void
  1206. resizemouse(Client *c) {
  1207. int ocx, ocy;
  1208. int nw, nh;
  1209. View *v;
  1210. XEvent ev;
  1211. ocx = c->x;
  1212. ocy = c->y;
  1213. v = getview(c);
  1214. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1215. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1216. return;
  1217. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
  1218. for(;;) {
  1219. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask , &ev);
  1220. switch(ev.type) {
  1221. case ButtonRelease:
  1222. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
  1223. c->w + c->border - 1, c->h + c->border - 1);
  1224. XUngrabPointer(dpy, CurrentTime);
  1225. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1226. return;
  1227. case ConfigureRequest:
  1228. case Expose:
  1229. case MapRequest:
  1230. handler[ev.type](&ev);
  1231. break;
  1232. case MotionNotify:
  1233. XSync(dpy, False);
  1234. if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
  1235. nw = 1;
  1236. if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
  1237. nh = 1;
  1238. if(!c->isfloating && (v->layout->arrange != floating) && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP))
  1239. togglefloating(NULL);
  1240. if((v->layout->arrange == floating) || c->isfloating)
  1241. resize(c, c->x, c->y, nw, nh, True);
  1242. break;
  1243. }
  1244. }
  1245. }
  1246. void
  1247. restack(View *v) {
  1248. Client *c;
  1249. XEvent ev;
  1250. XWindowChanges wc;
  1251. drawbar(v);
  1252. if(!sel)
  1253. return;
  1254. if(sel->isfloating || (v->layout->arrange == floating))
  1255. XRaiseWindow(dpy, sel->win);
  1256. if(v->layout->arrange != floating) {
  1257. wc.stack_mode = Below;
  1258. wc.sibling = v->barwin;
  1259. if(!sel->isfloating) {
  1260. XConfigureWindow(dpy, sel->win, CWSibling|CWStackMode, &wc);
  1261. wc.sibling = sel->win;
  1262. }
  1263. for(c = nexttiled(clients, v); c; c = nexttiled(c->next, v)) {
  1264. if(c == sel)
  1265. continue;
  1266. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1267. wc.sibling = c->win;
  1268. }
  1269. }
  1270. XSync(dpy, False);
  1271. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1272. }
  1273. void
  1274. run(void) {
  1275. char *p;
  1276. char buf[sizeof stext];
  1277. fd_set rd;
  1278. int r, xfd;
  1279. unsigned int len, offset;
  1280. XEvent ev;
  1281. /* main event loop, also reads status text from stdin */
  1282. XSync(dpy, False);
  1283. xfd = ConnectionNumber(dpy);
  1284. readin = True;
  1285. offset = 0;
  1286. len = sizeof stext - 1;
  1287. buf[len] = stext[len] = '\0'; /* 0-terminator is never touched */
  1288. while(running) {
  1289. FD_ZERO(&rd);
  1290. if(readin)
  1291. FD_SET(STDIN_FILENO, &rd);
  1292. FD_SET(xfd, &rd);
  1293. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1294. if(errno == EINTR)
  1295. continue;
  1296. eprint("select failed\n");
  1297. }
  1298. if(FD_ISSET(STDIN_FILENO, &rd)) {
  1299. switch((r = read(STDIN_FILENO, buf + offset, len - offset))) {
  1300. case -1:
  1301. strncpy(stext, strerror(errno), len);
  1302. readin = False;
  1303. break;
  1304. case 0:
  1305. strncpy(stext, "EOF", 4);
  1306. readin = False;
  1307. break;
  1308. default:
  1309. for(p = buf + offset; r > 0; p++, r--, offset++)
  1310. if(*p == '\n' || *p == '\0') {
  1311. *p = '\0';
  1312. strncpy(stext, buf, len);
  1313. p += r - 1; /* p is buf + offset + r - 1 */
  1314. for(r = 0; *(p - r) && *(p - r) != '\n'; r++);
  1315. offset = r;
  1316. if(r)
  1317. memmove(buf, p - r + 1, r);
  1318. break;
  1319. }
  1320. break;
  1321. }
  1322. drawbar(selview);
  1323. }
  1324. while(XPending(dpy)) {
  1325. XNextEvent(dpy, &ev);
  1326. if(handler[ev.type])
  1327. (handler[ev.type])(&ev); /* call handler */
  1328. }
  1329. }
  1330. }
  1331. void
  1332. scan(void) {
  1333. unsigned int i, num;
  1334. Window *wins, d1, d2;
  1335. XWindowAttributes wa;
  1336. wins = NULL;
  1337. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1338. for(i = 0; i < num; i++) {
  1339. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1340. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1341. continue;
  1342. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1343. manage(wins[i], &wa);
  1344. }
  1345. for(i = 0; i < num; i++) { /* now the transients */
  1346. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1347. continue;
  1348. if(XGetTransientForHint(dpy, wins[i], &d1)
  1349. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1350. manage(wins[i], &wa);
  1351. }
  1352. }
  1353. if(wins)
  1354. XFree(wins);
  1355. }
  1356. void
  1357. setclientstate(Client *c, long state) {
  1358. long data[] = {state, None};
  1359. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1360. PropModeReplace, (unsigned char *)data, 2);
  1361. }
  1362. void
  1363. setlayout(const char *arg) {
  1364. unsigned int i;
  1365. View *v = selview;
  1366. if(!arg) {
  1367. v->layout++;
  1368. if(v->layout == &layouts[LENGTH(layouts)])
  1369. v->layout = &layouts[0];
  1370. }
  1371. else {
  1372. for(i = 0; i < LENGTH(layouts); i++)
  1373. if(!strcmp(arg, layouts[i].symbol))
  1374. break;
  1375. if(i == LENGTH(layouts))
  1376. return;
  1377. v->layout = &layouts[i];
  1378. }
  1379. if(sel)
  1380. arrange();
  1381. else
  1382. drawbar(v);
  1383. }
  1384. void
  1385. setmwfact(const char *arg) {
  1386. double delta;
  1387. View *v = selview;
  1388. if(!domwfact)
  1389. return;
  1390. /* arg handling, manipulate mwfact */
  1391. if(arg == NULL)
  1392. v->mwfact = MWFACT;
  1393. else if(sscanf(arg, "%lf", &delta) == 1) {
  1394. if(arg[0] == '+' || arg[0] == '-')
  1395. v->mwfact += delta;
  1396. else
  1397. v->mwfact = delta;
  1398. if(v->mwfact < 0.1)
  1399. v->mwfact = 0.1;
  1400. else if(v->mwfact > 0.9)
  1401. v->mwfact = 0.9;
  1402. }
  1403. arrange();
  1404. }
  1405. void
  1406. setup(void) {
  1407. unsigned int i;
  1408. View *v;
  1409. XSetWindowAttributes wa;
  1410. XineramaScreenInfo *info = NULL;
  1411. /* init atoms */
  1412. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1413. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1414. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1415. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1416. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1417. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1418. /* init cursors */
  1419. wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1420. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1421. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1422. if((isxinerama = XineramaIsActive(dpy)))
  1423. info = XineramaQueryScreens(dpy, &nviews);
  1424. #if defined(AIM_XINERAMA)
  1425. isxinerama = True;
  1426. nviews = 2; /* aim Xinerama */
  1427. #endif
  1428. selview = views = emallocz(nviews * sizeof(View));
  1429. screen = DefaultScreen(dpy);
  1430. root = RootWindow(dpy, screen);
  1431. /* init appearance */
  1432. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  1433. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  1434. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  1435. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  1436. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  1437. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  1438. initfont(FONT);
  1439. dc.h = bh = dc.font.height + 2;
  1440. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1441. dc.gc = XCreateGC(dpy, root, 0, 0);
  1442. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1443. if(!dc.font.set)
  1444. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1445. for(blw = i = 0; i < LENGTH(layouts); i++) {
  1446. i = textw(layouts[i].symbol);
  1447. if(i > blw)
  1448. blw = i;
  1449. }
  1450. seltags = emallocz(sizeof initags);
  1451. prevtags = emallocz(sizeof initags);
  1452. memcpy(seltags, initags, sizeof initags);
  1453. memcpy(prevtags, initags, sizeof initags);
  1454. for(i = 0; i < nviews; i++) {
  1455. /* init geometry */
  1456. v = &views[i];
  1457. v->id = i + 1;
  1458. if(nviews != 1 && isxinerama) {
  1459. #if defined(AIM_XINERAMA)
  1460. v->w = DisplayWidth(dpy, screen) / 2;
  1461. v->x = (i == 0) ? 0 : v->w;
  1462. v->y = 0;
  1463. v->h = DisplayHeight(dpy, screen);
  1464. #else
  1465. v->x = info[i].x_org;
  1466. v->y = info[i].y_org;
  1467. v->w = info[i].width;
  1468. v->h = info[i].height;
  1469. #endif
  1470. }
  1471. else {
  1472. v->x = 0;
  1473. v->y = 0;
  1474. v->w = DisplayWidth(dpy, screen);
  1475. v->h = DisplayHeight(dpy, screen);
  1476. }
  1477. /* init layouts */
  1478. v->mwfact = MWFACT;
  1479. v->layout = &layouts[0];
  1480. // TODO: bpos per screen?
  1481. bpos = BARPOS;
  1482. wa.override_redirect = 1;
  1483. wa.background_pixmap = ParentRelative;
  1484. wa.event_mask = ButtonPressMask|ExposureMask;
  1485. /* init bars */
  1486. v->barwin = XCreateWindow(dpy, root, v->x, v->y, v->w, bh, 0,
  1487. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  1488. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1489. XDefineCursor(dpy, v->barwin, cursor[CurNormal]);
  1490. updatebarpos(v);
  1491. XMapRaised(dpy, v->barwin);
  1492. strcpy(stext, "dwm-"VERSION);
  1493. /* EWMH support per view */
  1494. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1495. PropModeReplace, (unsigned char *) netatom, NetLast);
  1496. /* select for events */
  1497. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
  1498. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask;
  1499. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1500. XSelectInput(dpy, root, wa.event_mask);
  1501. drawbar(v);
  1502. }
  1503. if(info)
  1504. XFree(info);
  1505. /* grab keys */
  1506. grabkeys();
  1507. /* init tags */
  1508. compileregs();
  1509. selview = &views[0];
  1510. }
  1511. void
  1512. spawn(const char *arg) {
  1513. static char *shell = NULL;
  1514. if(!shell && !(shell = getenv("SHELL")))
  1515. shell = "/bin/sh";
  1516. if(!arg)
  1517. return;
  1518. /* The double-fork construct avoids zombie processes and keeps the code
  1519. * clean from stupid signal handlers. */
  1520. if(fork() == 0) {
  1521. if(fork() == 0) {
  1522. if(dpy)
  1523. close(ConnectionNumber(dpy));
  1524. setsid();
  1525. execl(shell, shell, "-c", arg, (char *)NULL);
  1526. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
  1527. perror(" failed");
  1528. }
  1529. exit(0);
  1530. }
  1531. wait(0);
  1532. }
  1533. void
  1534. tag(const char *arg) {
  1535. unsigned int i;
  1536. if(!sel)
  1537. return;
  1538. for(i = 0; i < LENGTH(tags); i++)
  1539. sel->tags[i] = (NULL == arg) ? selview->id : 0;
  1540. sel->tags[idxoftag(arg)] = selview->id;
  1541. arrange();
  1542. }
  1543. unsigned int
  1544. textnw(const char *text, unsigned int len) {
  1545. XRectangle r;
  1546. if(dc.font.set) {
  1547. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1548. return r.width;
  1549. }
  1550. return XTextWidth(dc.font.xfont, text, len);
  1551. }
  1552. unsigned int
  1553. textw(const char *text) {
  1554. return textnw(text, strlen(text)) + dc.font.height;
  1555. }
  1556. void
  1557. tile(View *v) {
  1558. unsigned int i, n, nx, ny, nw, nh, mw, th;
  1559. Client *c, *mc;
  1560. domwfact = dozoom = True;
  1561. nx = v->wax;
  1562. ny = v->way;
  1563. nw = 0;
  1564. for(n = 0, c = nexttiled(clients, v); c; c = nexttiled(c->next, v))
  1565. n++;
  1566. /* window geoms */
  1567. mw = (n == 1) ? v->waw : v->mwfact * v->waw;
  1568. th = (n > 1) ? v->wah / (n - 1) : 0;
  1569. if(n > 1 && th < bh)
  1570. th = v->wah;
  1571. for(i = 0, c = mc = nexttiled(clients, v); c; c = nexttiled(c->next, v)) {
  1572. if(i == 0) { /* master */
  1573. nx = v->wax;
  1574. ny = v->way;
  1575. nw = mw - 2 * c->border;
  1576. nh = v->wah - 2 * c->border;
  1577. }
  1578. else { /* tile window */
  1579. if(i == 1) {
  1580. ny = v->way;
  1581. nx += mc->w + 2 * mc->border;
  1582. nw = v->waw - mw - 2 * c->border;
  1583. }
  1584. if(i + 1 == n) /* remainder */
  1585. nh = (v->way + v->wah) - ny - 2 * c->border;
  1586. else
  1587. nh = th - 2 * c->border;
  1588. }
  1589. resize(c, nx, ny, nw, nh, RESIZEHINTS);
  1590. if((RESIZEHINTS) && ((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw)))
  1591. /* client doesn't accept size constraints */
  1592. resize(c, nx, ny, nw, nh, False);
  1593. if(n > 1 && th != v->wah)
  1594. ny = c->y + c->h + 2 * c->border;
  1595. i++;
  1596. }
  1597. }
  1598. void
  1599. togglebar(const char *arg) {
  1600. if(bpos == BarOff)
  1601. bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
  1602. else
  1603. bpos = BarOff;
  1604. updatebarpos(selview);
  1605. arrange();
  1606. }
  1607. void
  1608. togglefloating(const char *arg) {
  1609. if(!sel)
  1610. return;
  1611. sel->isfloating = !sel->isfloating;
  1612. if(sel->isfloating)
  1613. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1614. arrange();
  1615. }
  1616. void
  1617. toggletag(const char *arg) {
  1618. unsigned int i, j;
  1619. if(!sel)
  1620. return;
  1621. i = idxoftag(arg);
  1622. sel->tags[i] = !sel->tags[i];
  1623. for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
  1624. if(j == LENGTH(tags))
  1625. sel->tags[i] = selview->id; /* at least one tag must be enabled */
  1626. arrange();
  1627. }
  1628. void
  1629. toggleview(const char *arg) {
  1630. unsigned int i, j;
  1631. i = idxoftag(arg);
  1632. seltags[i] = !seltags[i];
  1633. for(j = 0; j < LENGTH(tags) && !seltags[j]; j++);
  1634. if(j == LENGTH(tags))
  1635. seltags[i] = selview->id; /* at least one tag must be viewed */
  1636. arrange();
  1637. }
  1638. void
  1639. unban(Client *c) {
  1640. if(!c->isbanned)
  1641. return;
  1642. XMoveWindow(dpy, c->win, c->x, c->y);
  1643. c->isbanned = False;
  1644. }
  1645. void
  1646. unmanage(Client *c) {
  1647. XWindowChanges wc;
  1648. wc.border_width = c->oldborder;
  1649. /* The server grab construct avoids race conditions. */
  1650. XGrabServer(dpy);
  1651. XSetErrorHandler(xerrordummy);
  1652. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1653. detach(c);
  1654. detachstack(c);
  1655. if(sel == c)
  1656. focus(NULL);
  1657. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1658. setclientstate(c, WithdrawnState);
  1659. free(c->tags);
  1660. free(c);
  1661. XSync(dpy, False);
  1662. XSetErrorHandler(xerror);
  1663. XUngrabServer(dpy);
  1664. arrange();
  1665. }
  1666. void
  1667. unmapnotify(XEvent *e) {
  1668. Client *c;
  1669. XUnmapEvent *ev = &e->xunmap;
  1670. if((c = getclient(ev->window)))
  1671. unmanage(c);
  1672. }
  1673. void
  1674. updatebarpos(View *v) {
  1675. XEvent ev;
  1676. v->wax = v->x;
  1677. v->way = v->y;
  1678. v->wah = v->h;
  1679. v->waw = v->w;
  1680. switch(bpos) {
  1681. default:
  1682. v->wah -= bh;
  1683. v->way += bh;
  1684. XMoveWindow(dpy, v->barwin, v->x, v->y);
  1685. break;
  1686. case BarBot:
  1687. v->wah -= bh;
  1688. XMoveWindow(dpy, v->barwin, v->x, v->y + v->wah);
  1689. break;
  1690. case BarOff:
  1691. XMoveWindow(dpy, v->barwin, v->x, v->y - bh);
  1692. break;
  1693. }
  1694. XSync(dpy, False);
  1695. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1696. }
  1697. void
  1698. updatesizehints(Client *c) {
  1699. long msize;
  1700. XSizeHints size;
  1701. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  1702. size.flags = PSize;
  1703. c->flags = size.flags;
  1704. if(c->flags & PBaseSize) {
  1705. c->basew = size.base_width;
  1706. c->baseh = size.base_height;
  1707. }
  1708. else if(c->flags & PMinSize) {
  1709. c->basew = size.min_width;
  1710. c->baseh = size.min_height;
  1711. }
  1712. else
  1713. c->basew = c->baseh = 0;
  1714. if(c->flags & PResizeInc) {
  1715. c->incw = size.width_inc;
  1716. c->inch = size.height_inc;
  1717. }
  1718. else
  1719. c->incw = c->inch = 0;
  1720. if(c->flags & PMaxSize) {
  1721. c->maxw = size.max_width;
  1722. c->maxh = size.max_height;
  1723. }
  1724. else
  1725. c->maxw = c->maxh = 0;
  1726. if(c->flags & PMinSize) {
  1727. c->minw = size.min_width;
  1728. c->minh = size.min_height;
  1729. }
  1730. else if(c->flags & PBaseSize) {
  1731. c->minw = size.base_width;
  1732. c->minh = size.base_height;
  1733. }
  1734. else
  1735. c->minw = c->minh = 0;
  1736. if(c->flags & PAspect) {
  1737. c->minax = size.min_aspect.x;
  1738. c->maxax = size.max_aspect.x;
  1739. c->minay = size.min_aspect.y;
  1740. c->maxay = size.max_aspect.y;
  1741. }
  1742. else
  1743. c->minax = c->maxax = c->minay = c->maxay = 0;
  1744. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1745. && c->maxw == c->minw && c->maxh == c->minh);
  1746. }
  1747. void
  1748. updatetitle(Client *c) {
  1749. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1750. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1751. }
  1752. void
  1753. updatewmhints(Client *c) {
  1754. XWMHints *wmh;
  1755. if((wmh = XGetWMHints(dpy, c->win))) {
  1756. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1757. XFree(wmh);
  1758. }
  1759. }
  1760. /* There's no way to check accesses to destroyed windows, thus those cases are
  1761. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1762. * default error handler, which may call exit. */
  1763. int
  1764. xerror(Display *dpy, XErrorEvent *ee) {
  1765. if(ee->error_code == BadWindow
  1766. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1767. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1768. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1769. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1770. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1771. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1772. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1773. return 0;
  1774. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1775. ee->request_code, ee->error_code);
  1776. return xerrorxlib(dpy, ee); /* may call exit */
  1777. }
  1778. int
  1779. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1780. return 0;
  1781. }
  1782. /* Startup Error handler to check if another window manager
  1783. * is already running. */
  1784. int
  1785. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1786. otherwm = True;
  1787. return -1;
  1788. }
  1789. void
  1790. view(const char *arg) {
  1791. unsigned int i;
  1792. int tmp[LENGTH(tags)];
  1793. for(i = 0; i < LENGTH(tags); i++)
  1794. tmp[i] = (NULL == arg) ? selview->id : 0;
  1795. tmp[idxoftag(arg)] = selview->id;
  1796. if(memcmp(seltags, tmp, sizeof initags) != 0) {
  1797. memcpy(prevtags, seltags, sizeof initags);
  1798. memcpy(seltags, tmp, sizeof initags);
  1799. arrange();
  1800. }
  1801. }
  1802. void
  1803. viewprevtag(const char *arg) {
  1804. static Bool tmp[LENGTH(tags)];
  1805. memcpy(tmp, seltags, sizeof initags);
  1806. memcpy(seltags, prevtags, sizeof initags);
  1807. memcpy(prevtags, tmp, sizeof initags);
  1808. arrange();
  1809. }
  1810. void
  1811. zoom(const char *arg) {
  1812. Client *c = sel;
  1813. if(!sel || !dozoom || sel->isfloating)
  1814. return;
  1815. if(c == nexttiled(clients, getview(c)))
  1816. if(!(c = nexttiled(c->next, getview(c))))
  1817. return;
  1818. detach(c);
  1819. attach(c);
  1820. focus(c);
  1821. arrange();
  1822. }
  1823. void
  1824. selectview(const char *arg) {
  1825. int i;
  1826. View *v;
  1827. if(!arg)
  1828. return;
  1829. if(arg)
  1830. i = atoi(arg);
  1831. v = &views[i % nviews];
  1832. XWarpPointer(dpy, None, root, 0, 0, 0, 0, v->wax+v->waw/2, v->way+v->wah/2);
  1833. focus(NULL);
  1834. }
  1835. int
  1836. main(int argc, char *argv[]) {
  1837. if(argc == 2 && !strcmp("-v", argv[1]))
  1838. eprint("dwm-"VERSION", © 2006-2008 dwm engineers, see LICENSE for details\n");
  1839. else if(argc != 1)
  1840. eprint("usage: dwm [-v]\n");
  1841. setlocale(LC_CTYPE, "");
  1842. if(!(dpy = XOpenDisplay(0)))
  1843. eprint("dwm: cannot open display\n");
  1844. checkotherwm();
  1845. setup();
  1846. scan();
  1847. run();
  1848. cleanup();
  1849. XCloseDisplay(dpy);
  1850. return 0;
  1851. }