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.

2032 lines
49 KiB

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