Configuration file for DWM on MacBook Air
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.

2036 lines
48 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, Monitor *m);
  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, Monitor *m);
  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(int, int);
  200. void movetomonitor(const char *arg);
  201. void selectmonitor(const char *arg);
  202. /* variables */
  203. char stext[256];
  204. int mcount = 1;
  205. //double mwfact;
  206. int (*xerrorxlib)(Display *, XErrorEvent *);
  207. unsigned int bh, bpos;
  208. unsigned int blw = 0;
  209. unsigned int numlockmask = 0;
  210. void (*handler[LASTEvent]) (XEvent *) = {
  211. [ButtonPress] = buttonpress,
  212. [ConfigureRequest] = configurerequest,
  213. [ConfigureNotify] = configurenotify,
  214. [DestroyNotify] = destroynotify,
  215. [EnterNotify] = enternotify,
  216. [Expose] = expose,
  217. [FocusIn] = focusin,
  218. [KeyPress] = keypress,
  219. [MappingNotify] = mappingnotify,
  220. [MapRequest] = maprequest,
  221. [PropertyNotify] = propertynotify,
  222. [UnmapNotify] = unmapnotify
  223. };
  224. Atom wmatom[WMLast], netatom[NetLast];
  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(-1, -1)].seltags, sizeof initags);
  275. if (!matched_monitor)
  276. c->monitor = monitorat(-1, -1);
  277. }
  278. void
  279. arrange(void) {
  280. Client *c;
  281. for(c = clients; c; c = c->next)
  282. if(isvisible(c, &monitors[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(-1, -1)];
  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, &monitors[monitorat(-1,-1)]))
  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. unsigned int i;
  631. Client *c;
  632. XCrossingEvent *ev = &e->xcrossing;
  633. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  634. return;
  635. if((c = getclient(ev->window)))
  636. focus(c);
  637. else {
  638. for(i = 0; i < mcount; i++)
  639. if(ev->window == monitors[i].root) {
  640. selmonitor = i;
  641. focus(NULL);
  642. break;
  643. }
  644. }
  645. }
  646. void
  647. eprint(const char *errstr, ...) {
  648. va_list ap;
  649. va_start(ap, errstr);
  650. vfprintf(stderr, errstr, ap);
  651. va_end(ap);
  652. exit(EXIT_FAILURE);
  653. }
  654. void
  655. expose(XEvent *e) {
  656. XExposeEvent *ev = &e->xexpose;
  657. if(ev->count == 0) {
  658. if(ev->window == monitors[selmonitor].barwin)
  659. drawbar();
  660. }
  661. }
  662. void
  663. floating(void) { /* default floating layout */
  664. Client *c;
  665. domwfact = dozoom = False;
  666. for(c = clients; c; c = c->next)
  667. if(isvisible(c, &monitors[selmonitor]))
  668. resize(c, c->x, c->y, c->w, c->h, True);
  669. }
  670. void
  671. focus(Client *c) {
  672. Monitor *m = &monitors[c ? c->monitor : selmonitor];
  673. if(!c || (c && !isvisible(c, m)))
  674. for(c = stack; c && !isvisible(c, m); c = c->snext);
  675. if(sel && sel != c) {
  676. grabbuttons(sel, False);
  677. XSetWindowBorder(dpy, sel->win, monitors[sel->monitor].dc.norm[ColBorder]);
  678. }
  679. if(c) {
  680. detachstack(c);
  681. attachstack(c);
  682. grabbuttons(c, True);
  683. }
  684. sel = c;
  685. drawbar();
  686. if(c) {
  687. XSetWindowBorder(dpy, c->win, m->dc.sel[ColBorder]);
  688. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  689. selmonitor = c->monitor;
  690. }
  691. else {
  692. XSetInputFocus(dpy, m->root, RevertToPointerRoot, CurrentTime);
  693. }
  694. }
  695. void
  696. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  697. XFocusChangeEvent *ev = &e->xfocus;
  698. if(sel && ev->window != sel->win)
  699. XSetInputFocus(dpy, sel->win, RevertToPointerRoot, CurrentTime);
  700. }
  701. void
  702. focusnext(const char *arg) {
  703. Client *c;
  704. Monitor *m = &monitors[selmonitor];
  705. if(!sel)
  706. return;
  707. for(c = sel->next; c && !isvisible(c, m); c = c->next);
  708. if(!c)
  709. for(c = clients; c && !isvisible(c, m); c = c->next);
  710. if(c) {
  711. focus(c);
  712. restack();
  713. }
  714. }
  715. void
  716. focusprev(const char *arg) {
  717. Client *c;
  718. Monitor *m = &monitors[selmonitor];
  719. if(!sel)
  720. return;
  721. for(c = sel->prev; c && !isvisible(c, m); c = c->prev);
  722. if(!c) {
  723. for(c = clients; c && c->next; c = c->next);
  724. for(; c && !isvisible(c, m); c = c->prev);
  725. }
  726. if(c) {
  727. focus(c);
  728. restack();
  729. }
  730. }
  731. Client *
  732. getclient(Window w) {
  733. Client *c;
  734. for(c = clients; c && c->win != w; c = c->next);
  735. return c;
  736. }
  737. unsigned long
  738. getcolor(const char *colstr, int screen) {
  739. Colormap cmap = DefaultColormap(dpy, screen);
  740. XColor color;
  741. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  742. eprint("error, cannot allocate color '%s'\n", colstr);
  743. return color.pixel;
  744. }
  745. long
  746. getstate(Window w) {
  747. int format, status;
  748. long result = -1;
  749. unsigned char *p = NULL;
  750. unsigned long n, extra;
  751. Atom real;
  752. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  753. &real, &format, &n, &extra, (unsigned char **)&p);
  754. if(status != Success)
  755. return -1;
  756. if(n != 0)
  757. result = *p;
  758. XFree(p);
  759. return result;
  760. }
  761. Bool
  762. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  763. char **list = NULL;
  764. int n;
  765. XTextProperty name;
  766. if(!text || size == 0)
  767. return False;
  768. text[0] = '\0';
  769. XGetTextProperty(dpy, w, &name, atom);
  770. if(!name.nitems)
  771. return False;
  772. if(name.encoding == XA_STRING)
  773. strncpy(text, (char *)name.value, size - 1);
  774. else {
  775. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  776. && n > 0 && *list) {
  777. strncpy(text, *list, size - 1);
  778. XFreeStringList(list);
  779. }
  780. }
  781. text[size - 1] = '\0';
  782. XFree(name.value);
  783. return True;
  784. }
  785. void
  786. grabbuttons(Client *c, Bool focused) {
  787. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  788. if(focused) {
  789. XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  790. GrabModeAsync, GrabModeSync, None, None);
  791. XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
  792. GrabModeAsync, GrabModeSync, None, None);
  793. XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  794. GrabModeAsync, GrabModeSync, None, None);
  795. XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  796. GrabModeAsync, GrabModeSync, None, None);
  797. XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  798. GrabModeAsync, GrabModeSync, None, None);
  799. XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
  800. GrabModeAsync, GrabModeSync, None, None);
  801. XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  802. GrabModeAsync, GrabModeSync, None, None);
  803. XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  804. GrabModeAsync, GrabModeSync, None, None);
  805. XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  806. GrabModeAsync, GrabModeSync, None, None);
  807. XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
  808. GrabModeAsync, GrabModeSync, None, None);
  809. XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  810. GrabModeAsync, GrabModeSync, None, None);
  811. XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  812. GrabModeAsync, GrabModeSync, None, None);
  813. }
  814. else
  815. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
  816. GrabModeAsync, GrabModeSync, None, None);
  817. }
  818. void
  819. grabkeys(void) {
  820. unsigned int i, j;
  821. KeyCode code;
  822. XModifierKeymap *modmap;
  823. /* init modifier map */
  824. modmap = XGetModifierMapping(dpy);
  825. for(i = 0; i < 8; i++)
  826. for(j = 0; j < modmap->max_keypermod; j++) {
  827. if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
  828. numlockmask = (1 << i);
  829. }
  830. XFreeModifiermap(modmap);
  831. for(i = 0; i < mcount; i++) {
  832. Monitor *m = &monitors[i];
  833. XUngrabKey(dpy, AnyKey, AnyModifier, m->root);
  834. for(j = 0; j < LENGTH(keys); j++) {
  835. code = XKeysymToKeycode(dpy, keys[j].keysym);
  836. XGrabKey(dpy, code, keys[j].mod, m->root, True,
  837. GrabModeAsync, GrabModeAsync);
  838. XGrabKey(dpy, code, keys[j].mod | LockMask, m->root, True,
  839. GrabModeAsync, GrabModeAsync);
  840. XGrabKey(dpy, code, keys[j].mod | numlockmask, m->root, True,
  841. GrabModeAsync, GrabModeAsync);
  842. XGrabKey(dpy, code, keys[j].mod | numlockmask | LockMask, m->root, True,
  843. GrabModeAsync, GrabModeAsync);
  844. }
  845. }
  846. }
  847. unsigned int
  848. idxoftag(const char *tag) {
  849. unsigned int i;
  850. for(i = 0; (i < LENGTH(tags)) && (tags[i] != tag); i++);
  851. return (i < LENGTH(tags)) ? i : 0;
  852. }
  853. void
  854. initfont(Monitor *m, const char *fontstr) {
  855. char *def, **missing;
  856. int i, n;
  857. missing = NULL;
  858. if(m->dc.font.set)
  859. XFreeFontSet(dpy, m->dc.font.set);
  860. m->dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  861. if(missing) {
  862. while(n--)
  863. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  864. XFreeStringList(missing);
  865. }
  866. if(m->dc.font.set) {
  867. XFontSetExtents *font_extents;
  868. XFontStruct **xfonts;
  869. char **font_names;
  870. m->dc.font.ascent = m->dc.font.descent = 0;
  871. font_extents = XExtentsOfFontSet(m->dc.font.set);
  872. n = XFontsOfFontSet(m->dc.font.set, &xfonts, &font_names);
  873. for(i = 0, m->dc.font.ascent = 0, m->dc.font.descent = 0; i < n; i++) {
  874. if(m->dc.font.ascent < (*xfonts)->ascent)
  875. m->dc.font.ascent = (*xfonts)->ascent;
  876. if(m->dc.font.descent < (*xfonts)->descent)
  877. m->dc.font.descent = (*xfonts)->descent;
  878. xfonts++;
  879. }
  880. }
  881. else {
  882. if(m->dc.font.xfont)
  883. XFreeFont(dpy, m->dc.font.xfont);
  884. m->dc.font.xfont = NULL;
  885. if(!(m->dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  886. && !(m->dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  887. eprint("error, cannot load font: '%s'\n", fontstr);
  888. m->dc.font.ascent = m->dc.font.xfont->ascent;
  889. m->dc.font.descent = m->dc.font.xfont->descent;
  890. }
  891. m->dc.font.height = m->dc.font.ascent + m->dc.font.descent;
  892. }
  893. Bool
  894. isoccupied(Monitor *m, unsigned int t) {
  895. Client *c;
  896. for(c = clients; c; c = c->next)
  897. if(c->tags[t] && c->monitor == selmonitor)
  898. return True;
  899. return False;
  900. }
  901. Bool
  902. isprotodel(Client *c) {
  903. int i, n;
  904. Atom *protocols;
  905. Bool ret = False;
  906. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  907. for(i = 0; !ret && i < n; i++)
  908. if(protocols[i] == wmatom[WMDelete])
  909. ret = True;
  910. XFree(protocols);
  911. }
  912. return ret;
  913. }
  914. Bool
  915. isvisible(Client *c, Monitor *m) {
  916. unsigned int i;
  917. for(i = 0; i < LENGTH(tags); i++)
  918. if(c->tags[i] && monitors[c->monitor].seltags[i] && c->monitor == selmonitor)
  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. Monitor *m = &monitors[selmonitor];
  958. Status rettrans;
  959. Window trans;
  960. XWindowChanges wc;
  961. c = emallocz(sizeof(Client));
  962. c->tags = emallocz(sizeof initags);
  963. c->win = w;
  964. applyrules(c);
  965. c->x = wa->x + m->sx;
  966. c->y = wa->y + m->sy;
  967. c->w = wa->width;
  968. c->h = wa->height;
  969. c->oldborder = wa->border_width;
  970. if (monitorat(c->x, c->y) != c->monitor) {
  971. c->x = m->sx;
  972. c->y = m->sy;
  973. }
  974. if(c->w == m->sw && c->h == m->sh) {
  975. c->x = m->sx;
  976. c->y = m->sy;
  977. c->border = wa->border_width;
  978. }
  979. else {
  980. if(c->x + c->w + 2 * c->border > m->wax + m->waw)
  981. c->x = m->wax + m->waw - c->w - 2 * c->border;
  982. if(c->y + c->h + 2 * c->border > m->way + m->wah)
  983. c->y = m->way + m->wah - c->h - 2 * c->border;
  984. if(c->x < m->wax)
  985. c->x = m->wax;
  986. if(c->y < m->way)
  987. c->y = m->way;
  988. c->border = BORDERPX;
  989. }
  990. wc.border_width = c->border;
  991. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  992. XSetWindowBorder(dpy, w, m->dc.norm[ColBorder]);
  993. configure(c); /* propagates border_width, if size doesn't change */
  994. updatesizehints(c);
  995. XSelectInput(dpy, w, EnterWindowMask | FocusChangeMask | PropertyChangeMask | StructureNotifyMask);
  996. grabbuttons(c, False);
  997. updatetitle(c);
  998. if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
  999. for(t = clients; t && t->win != trans; t = t->next);
  1000. if(t)
  1001. memcpy(c->tags, t->tags, sizeof initags);
  1002. if(!c->isfloating)
  1003. c->isfloating = (rettrans == Success) || c->isfixed;
  1004. attach(c);
  1005. attachstack(c);
  1006. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
  1007. ban(c);
  1008. XMapWindow(dpy, c->win);
  1009. setclientstate(c, NormalState);
  1010. arrange();
  1011. }
  1012. void
  1013. mappingnotify(XEvent *e) {
  1014. XMappingEvent *ev = &e->xmapping;
  1015. XRefreshKeyboardMapping(ev);
  1016. if(ev->request == MappingKeyboard)
  1017. grabkeys();
  1018. }
  1019. void
  1020. maprequest(XEvent *e) {
  1021. static XWindowAttributes wa;
  1022. XMapRequestEvent *ev = &e->xmaprequest;
  1023. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1024. return;
  1025. if(wa.override_redirect)
  1026. return;
  1027. if(!getclient(ev->window))
  1028. manage(ev->window, &wa);
  1029. }
  1030. void
  1031. movemouse(Client *c) {
  1032. int x1, y1, ocx, ocy, di, nx, ny;
  1033. unsigned int dui;
  1034. Window dummy;
  1035. XEvent ev;
  1036. ocx = nx = c->x;
  1037. ocy = ny = c->y;
  1038. if(XGrabPointer(dpy, monitors[selmonitor].root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1039. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1040. return;
  1041. XQueryPointer(dpy, monitors[selmonitor].root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  1042. for(;;) {
  1043. XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
  1044. switch (ev.type) {
  1045. case ButtonRelease:
  1046. XUngrabPointer(dpy, CurrentTime);
  1047. return;
  1048. case ConfigureRequest:
  1049. case Expose:
  1050. case MapRequest:
  1051. handler[ev.type](&ev);
  1052. break;
  1053. case MotionNotify:
  1054. XSync(dpy, False);
  1055. nx = ocx + (ev.xmotion.x - x1);
  1056. ny = ocy + (ev.xmotion.y - y1);
  1057. Monitor *m = &monitors[monitorat(nx, ny)];
  1058. if(abs(m->wax - nx) < SNAP)
  1059. nx = m->wax;
  1060. else if(abs((m->wax + m->waw) - (nx + c->w + 2 * c->border)) < SNAP)
  1061. nx = m->wax + m->waw - c->w - 2 * c->border;
  1062. if(abs(m->way - ny) < SNAP)
  1063. ny = m->way;
  1064. else if(abs((m->way + m->wah) - (ny + c->h + 2 * c->border)) < SNAP)
  1065. ny = m->way + m->wah - c->h - 2 * c->border;
  1066. resize(c, nx, ny, c->w, c->h, False);
  1067. memcpy(c->tags, monitors[monitorat(nx, ny)].seltags, sizeof initags);
  1068. break;
  1069. }
  1070. }
  1071. }
  1072. Client *
  1073. nexttiled(Client *c, Monitor *m) {
  1074. for(; c && (c->isfloating || !isvisible(c, m)); 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. }
  1096. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1097. updatetitle(c);
  1098. if(c == sel)
  1099. drawbar();
  1100. }
  1101. }
  1102. }
  1103. void
  1104. quit(const char *arg) {
  1105. readin = running = False;
  1106. }
  1107. void
  1108. reapply(const char *arg) {
  1109. static Bool zerotags[LENGTH(tags)] = { 0 };
  1110. Client *c;
  1111. for(c = clients; c; c = c->next) {
  1112. memcpy(c->tags, zerotags, sizeof zerotags);
  1113. applyrules(c);
  1114. }
  1115. arrange();
  1116. }
  1117. void
  1118. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  1119. XWindowChanges wc;
  1120. Monitor scr = monitors[monitorat(x, y)];
  1121. c->monitor = monitorat(x, y);
  1122. if(sizehints) {
  1123. /* set minimum possible */
  1124. if (w < 1)
  1125. w = 1;
  1126. if (h < 1)
  1127. h = 1;
  1128. /* temporarily remove base dimensions */
  1129. w -= c->basew;
  1130. h -= c->baseh;
  1131. /* adjust for aspect limits */
  1132. if (c->minay > 0 && c->maxay > 0 && c->minax > 0 && c->maxax > 0) {
  1133. if (w * c->maxay > h * c->maxax)
  1134. w = h * c->maxax / c->maxay;
  1135. else if (w * c->minay < h * c->minax)
  1136. h = w * c->minay / c->minax;
  1137. }
  1138. /* adjust for increment value */
  1139. if(c->incw)
  1140. w -= w % c->incw;
  1141. if(c->inch)
  1142. h -= h % c->inch;
  1143. /* restore base dimensions */
  1144. w += c->basew;
  1145. h += c->baseh;
  1146. if(c->minw > 0 && w < c->minw)
  1147. w = c->minw;
  1148. if(c->minh > 0 && h < c->minh)
  1149. h = c->minh;
  1150. if(c->maxw > 0 && w > c->maxw)
  1151. w = c->maxw;
  1152. if(c->maxh > 0 && h > c->maxh)
  1153. h = c->maxh;
  1154. }
  1155. if(w <= 0 || h <= 0)
  1156. return;
  1157. /* TODO: offscreen appearance fixes */
  1158. /*
  1159. if(x > scr.sw)
  1160. x = scr.sw - w - 2 * c->border;
  1161. if(y > scr.sh)
  1162. y = scr.sh - h - 2 * c->border;
  1163. if(x + w + 2 * c->border < scr.sx)
  1164. x = scr.sx;
  1165. if(y + h + 2 * c->border < scr.sy)
  1166. y = scr.sy;
  1167. */
  1168. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  1169. c->x = wc.x = x;
  1170. c->y = wc.y = y;
  1171. c->w = wc.width = w;
  1172. c->h = wc.height = h;
  1173. wc.border_width = c->border;
  1174. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  1175. configure(c);
  1176. XSync(dpy, False);
  1177. }
  1178. }
  1179. void
  1180. resizemouse(Client *c) {
  1181. int ocx, ocy;
  1182. int nw, nh;
  1183. XEvent ev;
  1184. ocx = c->x;
  1185. ocy = c->y;
  1186. if(XGrabPointer(dpy, monitors[selmonitor].root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1187. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1188. return;
  1189. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
  1190. for(;;) {
  1191. XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
  1192. switch(ev.type) {
  1193. case ButtonRelease:
  1194. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
  1195. c->w + c->border - 1, c->h + c->border - 1);
  1196. XUngrabPointer(dpy, CurrentTime);
  1197. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1198. return;
  1199. case ConfigureRequest:
  1200. case Expose:
  1201. case MapRequest:
  1202. handler[ev.type](&ev);
  1203. break;
  1204. case MotionNotify:
  1205. XSync(dpy, False);
  1206. if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
  1207. nw = 1;
  1208. if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
  1209. nh = 1;
  1210. resize(c, c->x, c->y, nw, nh, True);
  1211. break;
  1212. }
  1213. }
  1214. }
  1215. void
  1216. restack(void) {
  1217. unsigned int i;
  1218. Client *c;
  1219. XEvent ev;
  1220. XWindowChanges wc;
  1221. drawbar();
  1222. if(!sel)
  1223. return;
  1224. if(sel->isfloating || (monitors[selmonitor].layout->arrange == floating))
  1225. XRaiseWindow(dpy, sel->win);
  1226. if(monitors[selmonitor].layout->arrange != floating) {
  1227. wc.stack_mode = Below;
  1228. wc.sibling = monitors[selmonitor].barwin;
  1229. if(!sel->isfloating) {
  1230. XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc);
  1231. wc.sibling = sel->win;
  1232. }
  1233. for(i = 0; i < mcount; i++) {
  1234. for(c = nexttiled(clients, &monitors[i]); c; c = nexttiled(c->next, &monitors[i])) {
  1235. if(c == sel)
  1236. continue;
  1237. XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
  1238. wc.sibling = c->win;
  1239. }
  1240. }
  1241. }
  1242. XSync(dpy, False);
  1243. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1244. }
  1245. void
  1246. run(void) {
  1247. char *p;
  1248. char buf[sizeof stext];
  1249. fd_set rd;
  1250. int r, xfd;
  1251. unsigned int len, offset;
  1252. XEvent ev;
  1253. /* main event loop, also reads status text from stdin */
  1254. XSync(dpy, False);
  1255. xfd = ConnectionNumber(dpy);
  1256. readin = True;
  1257. offset = 0;
  1258. len = sizeof stext - 1;
  1259. buf[len] = stext[len] = '\0'; /* 0-terminator is never touched */
  1260. while(running) {
  1261. FD_ZERO(&rd);
  1262. if(readin)
  1263. FD_SET(STDIN_FILENO, &rd);
  1264. FD_SET(xfd, &rd);
  1265. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1266. if(errno == EINTR)
  1267. continue;
  1268. eprint("select failed\n");
  1269. }
  1270. if(FD_ISSET(STDIN_FILENO, &rd)) {
  1271. switch((r = read(STDIN_FILENO, buf + offset, len - offset))) {
  1272. case -1:
  1273. strncpy(stext, strerror(errno), len);
  1274. readin = False;
  1275. break;
  1276. case 0:
  1277. strncpy(stext, "EOF", 4);
  1278. readin = False;
  1279. break;
  1280. default:
  1281. for(p = buf + offset; r > 0; p++, r--, offset++)
  1282. if(*p == '\n' || *p == '\0') {
  1283. *p = '\0';
  1284. strncpy(stext, buf, len);
  1285. p += r - 1; /* p is buf + offset + r - 1 */
  1286. for(r = 0; *(p - r) && *(p - r) != '\n'; r++);
  1287. offset = r;
  1288. if(r)
  1289. memmove(buf, p - r + 1, r);
  1290. break;
  1291. }
  1292. break;
  1293. }
  1294. drawbar();
  1295. }
  1296. while(XPending(dpy)) {
  1297. XNextEvent(dpy, &ev);
  1298. if(handler[ev.type])
  1299. (handler[ev.type])(&ev); /* call handler */
  1300. }
  1301. }
  1302. }
  1303. void
  1304. scan(void) {
  1305. unsigned int i, j, num;
  1306. Window *wins, d1, d2;
  1307. XWindowAttributes wa;
  1308. for(i = 0; i < mcount; i++) {
  1309. Monitor *m = &monitors[i];
  1310. wins = NULL;
  1311. if(XQueryTree(dpy, m->root, &d1, &d2, &wins, &num)) {
  1312. for(j = 0; j < num; j++) {
  1313. if(!XGetWindowAttributes(dpy, wins[j], &wa)
  1314. || wa.override_redirect || XGetTransientForHint(dpy, wins[j], &d1))
  1315. continue;
  1316. if(wa.map_state == IsViewable || getstate(wins[j]) == IconicState)
  1317. manage(wins[j], &wa);
  1318. }
  1319. for(j = 0; j < num; j++) { /* now the transients */
  1320. if(!XGetWindowAttributes(dpy, wins[j], &wa))
  1321. continue;
  1322. if(XGetTransientForHint(dpy, wins[j], &d1)
  1323. && (wa.map_state == IsViewable || getstate(wins[j]) == IconicState))
  1324. manage(wins[j], &wa);
  1325. }
  1326. }
  1327. if(wins)
  1328. XFree(wins);
  1329. }
  1330. }
  1331. void
  1332. setclientstate(Client *c, long state) {
  1333. long data[] = {state, None};
  1334. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1335. PropModeReplace, (unsigned char *)data, 2);
  1336. }
  1337. void
  1338. setlayout(const char *arg) {
  1339. unsigned int i;
  1340. Monitor *m = &monitors[monitorat(-1, -1)];
  1341. if(!arg) {
  1342. m->layout++;
  1343. if(m->layout == &layouts[LENGTH(layouts)])
  1344. m->layout = &layouts[0];
  1345. }
  1346. else {
  1347. for(i = 0; i < LENGTH(layouts); i++)
  1348. if(!strcmp(arg, layouts[i].symbol))
  1349. break;
  1350. if(i == LENGTH(layouts))
  1351. return;
  1352. m->layout = &layouts[i];
  1353. }
  1354. if(sel)
  1355. arrange();
  1356. else
  1357. drawbar();
  1358. }
  1359. void
  1360. setmwfact(const char *arg) {
  1361. double delta;
  1362. Monitor *m = &monitors[monitorat(-1, -1)];
  1363. if(!domwfact)
  1364. return;
  1365. /* arg handling, manipulate mwfact */
  1366. if(arg == NULL)
  1367. m->mwfact = MWFACT;
  1368. else if(sscanf(arg, "%lf", &delta) == 1) {
  1369. if(arg[0] == '+' || arg[0] == '-')
  1370. m->mwfact += delta;
  1371. else
  1372. m->mwfact = delta;
  1373. if(m->mwfact < 0.1)
  1374. m->mwfact = 0.1;
  1375. else if(m->mwfact > 0.9)
  1376. m->mwfact = 0.9;
  1377. }
  1378. arrange();
  1379. }
  1380. void
  1381. setup(void) {
  1382. unsigned int i, j, k;
  1383. Monitor *m;
  1384. XSetWindowAttributes wa;
  1385. XineramaScreenInfo *info = NULL;
  1386. /* init atoms */
  1387. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1388. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1389. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1390. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1391. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1392. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1393. /* init cursors */
  1394. wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1395. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1396. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1397. // init screens/monitors first
  1398. if (XineramaIsActive(dpy)) {
  1399. info = XineramaQueryScreens(dpy, &mcount);
  1400. }
  1401. mcount = 1;
  1402. monitors = emallocz(mcount * sizeof(Monitor));
  1403. for(i = 0; i < mcount; i++) {
  1404. /* init geometry */
  1405. m = &monitors[i];
  1406. m->screen = i;
  1407. m->root = RootWindow(dpy, i);
  1408. if (mcount != 1) { // TODO: Xinerama
  1409. m->sx = info[i].x_org;
  1410. m->sy = info[i].y_org;
  1411. m->sw = info[i].width;
  1412. m->sh = info[i].height;
  1413. }
  1414. else {
  1415. m->sx = 0;
  1416. m->sy = 0;
  1417. m->sw = DisplayWidth(dpy, m->screen);
  1418. m->sh = DisplayHeight(dpy, m->screen);
  1419. }
  1420. m->seltags = emallocz(sizeof initags);
  1421. m->prevtags = emallocz(sizeof initags);
  1422. memcpy(m->seltags, initags, sizeof initags);
  1423. memcpy(m->prevtags, initags, sizeof initags);
  1424. /* init appearance */
  1425. m->dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR, i);
  1426. m->dc.norm[ColBG] = getcolor(NORMBGCOLOR, i);
  1427. m->dc.norm[ColFG] = getcolor(NORMFGCOLOR, i);
  1428. m->dc.sel[ColBorder] = getcolor(SELBORDERCOLOR, i);
  1429. m->dc.sel[ColBG] = getcolor(SELBGCOLOR, i);
  1430. m->dc.sel[ColFG] = getcolor(SELFGCOLOR, i);
  1431. initfont(m, FONT);
  1432. m->dc.h = bh = m->dc.font.height + 2;
  1433. /* init layouts */
  1434. m->mwfact = MWFACT;
  1435. m->layout = &layouts[0];
  1436. for(blw = k = 0; k < LENGTH(layouts); k++) {
  1437. j = textw(m, layouts[k].symbol);
  1438. if(j > blw)
  1439. blw = j;
  1440. }
  1441. // TODO: bpos per screen?
  1442. bpos = BARPOS;
  1443. wa.override_redirect = 1;
  1444. wa.background_pixmap = ParentRelative;
  1445. wa.event_mask = ButtonPressMask | ExposureMask;
  1446. /* init bars */
  1447. m->barwin = XCreateWindow(dpy, m->root, m->sx, m->sy, m->sw, bh, 0,
  1448. DefaultDepth(dpy, m->screen), CopyFromParent, DefaultVisual(dpy, m->screen),
  1449. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  1450. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1451. updatebarpos(m);
  1452. XMapRaised(dpy, m->barwin);
  1453. strcpy(stext, "dwm-"VERSION);
  1454. m->dc.drawable = XCreatePixmap(dpy, m->root, m->sw, bh, DefaultDepth(dpy, m->screen));
  1455. m->dc.gc = XCreateGC(dpy, m->root, 0, 0);
  1456. XSetLineAttributes(dpy, m->dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1457. if(!m->dc.font.set)
  1458. XSetFont(dpy, m->dc.gc, m->dc.font.xfont->fid);
  1459. /* EWMH support per monitor */
  1460. XChangeProperty(dpy, m->root, netatom[NetSupported], XA_ATOM, 32,
  1461. PropModeReplace, (unsigned char *) netatom, NetLast);
  1462. /* select for events */
  1463. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  1464. | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
  1465. XChangeWindowAttributes(dpy, m->root, CWEventMask | CWCursor, &wa);
  1466. XSelectInput(dpy, m->root, wa.event_mask);
  1467. }
  1468. /* grab keys */
  1469. grabkeys();
  1470. /* init tags */
  1471. compileregs();
  1472. }
  1473. void
  1474. spawn(const char *arg) {
  1475. static char *shell = NULL;
  1476. if(!shell && !(shell = getenv("SHELL")))
  1477. shell = "/bin/sh";
  1478. if(!arg)
  1479. return;
  1480. /* The double-fork construct avoids zombie processes and keeps the code
  1481. * clean from stupid signal handlers. */
  1482. if(fork() == 0) {
  1483. if(fork() == 0) {
  1484. if(dpy)
  1485. close(ConnectionNumber(dpy));
  1486. setsid();
  1487. execl(shell, shell, "-c", arg, (char *)NULL);
  1488. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
  1489. perror(" failed");
  1490. }
  1491. exit(0);
  1492. }
  1493. wait(0);
  1494. }
  1495. void
  1496. tag(const char *arg) {
  1497. unsigned int i;
  1498. if(!sel)
  1499. return;
  1500. for(i = 0; i < LENGTH(tags); i++)
  1501. sel->tags[i] = (NULL == arg);
  1502. sel->tags[idxoftag(arg)] = True;
  1503. arrange();
  1504. }
  1505. unsigned int
  1506. textnw(Monitor *m, const char *text, unsigned int len) {
  1507. XRectangle r;
  1508. if(m->dc.font.set) {
  1509. XmbTextExtents(m->dc.font.set, text, len, NULL, &r);
  1510. return r.width;
  1511. }
  1512. return XTextWidth(m->dc.font.xfont, text, len);
  1513. }
  1514. unsigned int
  1515. textw(Monitor *m, const char *text) {
  1516. return textnw(m, text, strlen(text)) + m->dc.font.height;
  1517. }
  1518. void
  1519. tile(void) {
  1520. unsigned int i, j, n, nx, ny, nw, nh, mw, th;
  1521. Client *c, *mc;
  1522. domwfact = dozoom = True;
  1523. nw = 0; /* gcc stupidity requires this */
  1524. for (i = 0; i < mcount; i++) {
  1525. Monitor *m = &monitors[i];
  1526. for(n = 0, c = nexttiled(clients, m); c; c = nexttiled(c->next, m))
  1527. n++;
  1528. for(j = 0, c = mc = nexttiled(clients, m); c; c = nexttiled(c->next, m)) {
  1529. /* window geoms */
  1530. mw = (n == 1) ? m->waw : m->mwfact * m->waw;
  1531. th = (n > 1) ? m->wah / (n - 1) : 0;
  1532. if(n > 1 && th < bh)
  1533. th = m->wah;
  1534. if(j == 0) { /* master */
  1535. nx = m->wax;
  1536. ny = m->way;
  1537. nw = mw - 2 * c->border;
  1538. nh = m->wah - 2 * c->border;
  1539. }
  1540. else { /* tile window */
  1541. if(j == 1) {
  1542. ny = m->way;
  1543. nx += mc->w + 2 * mc->border;
  1544. nw = m->waw - mw - 2 * c->border;
  1545. }
  1546. if(j + 1 == n) /* remainder */
  1547. nh = (m->way + m->wah) - ny - 2 * c->border;
  1548. else
  1549. nh = th - 2 * c->border;
  1550. }
  1551. resize(c, nx, ny, nw, nh, RESIZEHINTS);
  1552. if((RESIZEHINTS) && ((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw)))
  1553. /* client doesn't accept size constraints */
  1554. resize(c, nx, ny, nw, nh, False);
  1555. if(n > 1 && th != m->wah)
  1556. ny = c->y + c->h + 2 * c->border;
  1557. j++;
  1558. }
  1559. }
  1560. }
  1561. void
  1562. togglebar(const char *arg) {
  1563. if(bpos == BarOff)
  1564. bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
  1565. else
  1566. bpos = BarOff;
  1567. updatebarpos(&monitors[monitorat(-1,-1)]);
  1568. arrange();
  1569. }
  1570. void
  1571. togglefloating(const char *arg) {
  1572. if(!sel)
  1573. return;
  1574. sel->isfloating = !sel->isfloating;
  1575. if(sel->isfloating)
  1576. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1577. arrange();
  1578. }
  1579. void
  1580. toggletag(const char *arg) {
  1581. unsigned int i, j;
  1582. if(!sel)
  1583. return;
  1584. i = idxoftag(arg);
  1585. sel->tags[i] = !sel->tags[i];
  1586. for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
  1587. if(j == LENGTH(tags))
  1588. sel->tags[i] = True; /* at least one tag must be enabled */
  1589. arrange();
  1590. }
  1591. void
  1592. toggleview(const char *arg) {
  1593. unsigned int i, j;
  1594. Monitor *m = &monitors[monitorat(-1, -1)];
  1595. i = idxoftag(arg);
  1596. m->seltags[i] = !m->seltags[i];
  1597. for(j = 0; j < LENGTH(tags) && !m->seltags[j]; j++);
  1598. if(j == LENGTH(tags))
  1599. m->seltags[i] = True; /* at least one tag must be viewed */
  1600. arrange();
  1601. }
  1602. void
  1603. unban(Client *c) {
  1604. if(!c->isbanned)
  1605. return;
  1606. XMoveWindow(dpy, c->win, c->x, c->y);
  1607. c->isbanned = False;
  1608. }
  1609. void
  1610. unmanage(Client *c) {
  1611. XWindowChanges wc;
  1612. wc.border_width = c->oldborder;
  1613. /* The server grab construct avoids race conditions. */
  1614. XGrabServer(dpy);
  1615. XSetErrorHandler(xerrordummy);
  1616. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1617. detach(c);
  1618. detachstack(c);
  1619. if(sel == c)
  1620. focus(NULL);
  1621. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1622. setclientstate(c, WithdrawnState);
  1623. free(c->tags);
  1624. free(c);
  1625. XSync(dpy, False);
  1626. XSetErrorHandler(xerror);
  1627. XUngrabServer(dpy);
  1628. arrange();
  1629. }
  1630. void
  1631. unmapnotify(XEvent *e) {
  1632. Client *c;
  1633. XUnmapEvent *ev = &e->xunmap;
  1634. if((c = getclient(ev->window)))
  1635. unmanage(c);
  1636. }
  1637. void
  1638. updatebarpos(Monitor *m) {
  1639. XEvent ev;
  1640. m->wax = m->sx;
  1641. m->way = m->sy;
  1642. m->wah = m->sh;
  1643. m->waw = m->sw;
  1644. switch(bpos) {
  1645. default:
  1646. m->wah -= bh;
  1647. m->way += bh;
  1648. XMoveWindow(dpy, m->barwin, m->sx, m->sy);
  1649. break;
  1650. case BarBot:
  1651. m->wah -= bh;
  1652. XMoveWindow(dpy, m->barwin, m->sx, m->sy + m->wah);
  1653. break;
  1654. case BarOff:
  1655. XMoveWindow(dpy, m->barwin, m->sx, m->sy - bh);
  1656. break;
  1657. }
  1658. XSync(dpy, False);
  1659. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1660. }
  1661. void
  1662. updatesizehints(Client *c) {
  1663. long msize;
  1664. XSizeHints size;
  1665. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  1666. size.flags = PSize;
  1667. c->flags = size.flags;
  1668. if(c->flags & PBaseSize) {
  1669. c->basew = size.base_width;
  1670. c->baseh = size.base_height;
  1671. }
  1672. else if(c->flags & PMinSize) {
  1673. c->basew = size.min_width;
  1674. c->baseh = size.min_height;
  1675. }
  1676. else
  1677. c->basew = c->baseh = 0;
  1678. if(c->flags & PResizeInc) {
  1679. c->incw = size.width_inc;
  1680. c->inch = size.height_inc;
  1681. }
  1682. else
  1683. c->incw = c->inch = 0;
  1684. if(c->flags & PMaxSize) {
  1685. c->maxw = size.max_width;
  1686. c->maxh = size.max_height;
  1687. }
  1688. else
  1689. c->maxw = c->maxh = 0;
  1690. if(c->flags & PMinSize) {
  1691. c->minw = size.min_width;
  1692. c->minh = size.min_height;
  1693. }
  1694. else if(c->flags & PBaseSize) {
  1695. c->minw = size.base_width;
  1696. c->minh = size.base_height;
  1697. }
  1698. else
  1699. c->minw = c->minh = 0;
  1700. if(c->flags & PAspect) {
  1701. c->minax = size.min_aspect.x;
  1702. c->maxax = size.max_aspect.x;
  1703. c->minay = size.min_aspect.y;
  1704. c->maxay = size.max_aspect.y;
  1705. }
  1706. else
  1707. c->minax = c->maxax = c->minay = c->maxay = 0;
  1708. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1709. && c->maxw == c->minw && c->maxh == c->minh);
  1710. }
  1711. void
  1712. updatetitle(Client *c) {
  1713. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1714. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1715. }
  1716. /* There's no way to check accesses to destroyed windows, thus those cases are
  1717. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1718. * default error handler, which may call exit. */
  1719. int
  1720. xerror(Display *dpy, XErrorEvent *ee) {
  1721. if(ee->error_code == BadWindow
  1722. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1723. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1724. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1725. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1726. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1727. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1728. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1729. return 0;
  1730. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1731. ee->request_code, ee->error_code);
  1732. return xerrorxlib(dpy, ee); /* may call exit */
  1733. }
  1734. int
  1735. xerrordummy(Display *dsply, XErrorEvent *ee) {
  1736. return 0;
  1737. }
  1738. /* Startup Error handler to check if another window manager
  1739. * is already running. */
  1740. int
  1741. xerrorstart(Display *dsply, XErrorEvent *ee) {
  1742. otherwm = True;
  1743. return -1;
  1744. }
  1745. void
  1746. view(const char *arg) {
  1747. unsigned int i;
  1748. Monitor *m = &monitors[monitorat(-1, -1)];
  1749. memcpy(m->prevtags, m->seltags, sizeof initags);
  1750. for(i = 0; i < LENGTH(tags); i++)
  1751. m->seltags[i] = (NULL == arg);
  1752. m->seltags[idxoftag(arg)] = True;
  1753. arrange();
  1754. }
  1755. void
  1756. viewprevtag(const char *arg) {
  1757. static Bool tmp[LENGTH(tags)];
  1758. Monitor *m = &monitors[monitorat(-1, -1)];
  1759. memcpy(tmp, m->seltags, sizeof initags);
  1760. memcpy(m->seltags, m->prevtags, sizeof initags);
  1761. memcpy(m->prevtags, tmp, sizeof initags);
  1762. arrange();
  1763. }
  1764. void
  1765. zoom(const char *arg) {
  1766. Client *c;
  1767. if(!sel || !dozoom || sel->isfloating)
  1768. return;
  1769. if((c = sel) == nexttiled(clients, &monitors[c->monitor]))
  1770. if(!(c = nexttiled(c->next, &monitors[c->monitor])))
  1771. return;
  1772. detach(c);
  1773. attach(c);
  1774. focus(c);
  1775. arrange();
  1776. }
  1777. int
  1778. monitorat(int x, int y) {
  1779. int i;
  1780. return 0;
  1781. if(!XineramaIsActive(dpy))
  1782. return 0;
  1783. if (x < 0 || y < 0) {
  1784. Window win;
  1785. unsigned int mask;
  1786. XQueryPointer(dpy, DefaultRootWindow(dpy), &win, &win, &x, &y, &i, &i, &mask);
  1787. }
  1788. for(i = 0; i < mcount; i++) {
  1789. Monitor *m = &monitors[i];
  1790. if((x < 0 || (x >= m->sx && x < m->sx + m->sw))
  1791. && (y < 0 || (y >= m->sy && y < m->sy + m->sh)))
  1792. {
  1793. return i;
  1794. }
  1795. }
  1796. return 0;
  1797. }
  1798. void
  1799. movetomonitor(const char *arg) {
  1800. if (sel) {
  1801. sel->monitor = arg ? atoi(arg) : (sel->monitor+1) % mcount;
  1802. memcpy(sel->tags, monitors[sel->monitor].seltags, sizeof initags);
  1803. resize(sel, monitors[sel->monitor].wax, monitors[sel->monitor].way, sel->w, sel->h, True);
  1804. arrange();
  1805. }
  1806. }
  1807. void
  1808. selectmonitor(const char *arg) {
  1809. Monitor *m = &monitors[arg ? atoi(arg) : (monitorat(-1, -1)+1) % mcount];
  1810. XWarpPointer(dpy, None, m->root, 0, 0, 0, 0, m->wax+m->waw/2, m->way+m->wah/2);
  1811. focus(NULL);
  1812. }
  1813. int
  1814. main(int argc, char *argv[]) {
  1815. if(argc == 2 && !strcmp("-v", argv[1]))
  1816. eprint("dwm-"VERSION", © 2006-2007 Anselm R. Garbe, Sander van Dijk, "
  1817. "Jukka Salmi, Premysl Hruby, Szabolcs Nagy, Christof Musik\n");
  1818. else if(argc != 1)
  1819. eprint("usage: dwm [-v]\n");
  1820. setlocale(LC_CTYPE, "");
  1821. if(!(dpy = XOpenDisplay(0)))
  1822. eprint("dwm: cannot open display\n");
  1823. checkotherwm();
  1824. setup();
  1825. drawbar();
  1826. scan();
  1827. run();
  1828. cleanup();
  1829. XCloseDisplay(dpy);
  1830. return 0;
  1831. }