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.

2055 lines
47 KiB

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