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

2050 lines
49 KiB

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