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.

2042 lines
49 KiB

17 years ago
17 years ago
17 years ago
17 years ago
  1. /* See LICENSE file for copyright and license details.
  2. *
  3. * dynamic window manager is designed like any other X client as well. It is
  4. * driven through handling X events. In contrast to other X clients, a window
  5. * manager selects for SubstructureRedirectMask on the root window, to receive
  6. * events about window (dis-)appearance. Only one X connection at a time is
  7. * allowed to select for this event mask.
  8. *
  9. * Calls to fetch an X event from the event queue are blocking. Due reading
  10. * status text from standard input, a select()-driven main loop has been
  11. * implemented which selects for reads on the X connection and STDIN_FILENO to
  12. * handle all data smoothly. The event handlers of dwm are organized in an
  13. * array which is accessed whenever a new event has been fetched. This allows
  14. * event dispatching in O(1) time.
  15. *
  16. * Each child of the root window is called a client, except windows which have
  17. * set the override_redirect flag. Clients are organized in a global
  18. * doubly-linked client list, the focus history is remembered through a global
  19. * stack list. Each client contains an array of Bools of the same size as the
  20. * global tags array to indicate the tags of a client.
  21. *
  22. * Keys and tagging rules are organized as arrays and defined in config.h.
  23. *
  24. * To understand everything else, start reading main().
  25. */
  26. #include <errno.h>
  27. #include <locale.h>
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <sys/select.h>
  34. #include <sys/types.h>
  35. #include <sys/wait.h>
  36. #include <regex.h>
  37. #include <X11/cursorfont.h>
  38. #include <X11/keysym.h>
  39. #include <X11/Xatom.h>
  40. #include <X11/Xlib.h>
  41. #include <X11/Xproto.h>
  42. #include <X11/Xutil.h>
  43. //#ifdef XINERAMA
  44. #include <X11/extensions/Xinerama.h>
  45. //#endif
  46. /* macros */
  47. #define BUTTONMASK (ButtonPressMask | ButtonReleaseMask)
  48. #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
  49. #define LENGTH(x) (sizeof x / sizeof x[0])
  50. #define MAXTAGLEN 16
  51. #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
  52. /* enums */
  53. enum { BarTop, BarBot, BarOff }; /* bar position */
  54. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  55. enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
  56. enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
  57. enum { WMProtocols, WMDelete, WMName, WMState, WMLast };/* default atoms */
  58. /* typedefs */
  59. typedef struct Client Client;
  60. struct Client {
  61. char name[256];
  62. int x, y, w, h;
  63. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  64. int minax, maxax, minay, maxay;
  65. long flags;
  66. unsigned int border, oldborder;
  67. Bool isbanned, isfixed, isfloating;
  68. Bool *tags;
  69. Client *next;
  70. Client *prev;
  71. Client *snext;
  72. Window win;
  73. int monitor;
  74. };
  75. typedef struct {
  76. int x, y, w, h;
  77. unsigned long norm[ColLast];
  78. unsigned long sel[ColLast];
  79. Drawable drawable;
  80. GC gc;
  81. struct {
  82. int ascent;
  83. int descent;
  84. int height;
  85. XFontSet set;
  86. XFontStruct *xfont;
  87. } font;
  88. } DC; /* draw context */
  89. typedef struct {
  90. unsigned long mod;
  91. KeySym keysym;
  92. void (*func)(const char *arg);
  93. const char *arg;
  94. } Key;
  95. typedef struct {
  96. const char *symbol;
  97. void (*arrange)(void);
  98. } Layout;
  99. typedef struct {
  100. const char *prop;
  101. const char *tags;
  102. Bool isfloating;
  103. int monitor;
  104. } Rule;
  105. typedef struct {
  106. regex_t *propregex;
  107. regex_t *tagregex;
  108. } Regs;
  109. typedef struct {
  110. int id;
  111. Window barwin;
  112. //TODO: Window root;
  113. //TODO: int screen;
  114. int sx, sy, sw, sh, wax, way, wah, waw;
  115. DC dc;
  116. Bool *seltags;
  117. Bool *prevtags;
  118. Layout *layout;
  119. double mwfact;
  120. } Monitor;
  121. /* function declarations */
  122. void applyrules(Client *c);
  123. void arrange(void);
  124. void attach(Client *c);
  125. void attachstack(Client *c);
  126. void ban(Client *c);
  127. void buttonpress(XEvent *e);
  128. void checkotherwm(void);
  129. void cleanup(void);
  130. void compileregs(void);
  131. void configure(Client *c);
  132. void configurenotify(XEvent *e);
  133. void configurerequest(XEvent *e);
  134. void destroynotify(XEvent *e);
  135. void detach(Client *c);
  136. void detachstack(Client *c);
  137. void drawbar(void);
  138. void drawsquare(Monitor *, Bool filled, Bool empty, unsigned long col[ColLast]);
  139. void drawtext(Monitor *, const char *text, unsigned long col[ColLast]);
  140. void *emallocz(unsigned int size);
  141. void enternotify(XEvent *e);
  142. void eprint(const char *errstr, ...);
  143. void expose(XEvent *e);
  144. void floating(void); /* default floating layout */
  145. void focus(Client *c);
  146. void focusin(XEvent *e);
  147. void focusnext(const char *arg);
  148. void focusprev(const char *arg);
  149. Client *getclient(Window w);
  150. unsigned long getcolor(const char *colstr);
  151. long getstate(Window w);
  152. Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  153. void grabbuttons(Client *c, Bool focused);
  154. void grabkeys(void);
  155. unsigned int idxoftag(const char *tag);
  156. void initfont(Monitor*, const char *fontstr);
  157. Bool isoccupied(Monitor *m, unsigned int t);
  158. Bool isprotodel(Client *c);
  159. Bool isvisible(Client *c, Monitor *m);
  160. void keypress(XEvent *e);
  161. void killclient(const char *arg);
  162. void leavenotify(XEvent *e);
  163. void manage(Window w, XWindowAttributes *wa);
  164. void mappingnotify(XEvent *e);
  165. void maprequest(XEvent *e);
  166. void movemouse(Client *c);
  167. Client *nexttiled(Client *c, Monitor *m);
  168. void propertynotify(XEvent *e);
  169. void quit(const char *arg);
  170. void reapply(const char *arg);
  171. void resize(Client *c, int x, int y, int w, int h, Bool sizehints);
  172. void resizemouse(Client *c);
  173. void restack(void);
  174. void run(void);
  175. void scan(void);
  176. void setclientstate(Client *c, long state);
  177. void setlayout(const char *arg);
  178. void setmwfact(const char *arg);
  179. void setup(void);
  180. void spawn(const char *arg);
  181. void tag(const char *arg);
  182. unsigned int textnw(Monitor*, const char *text, unsigned int len);
  183. unsigned int textw(Monitor*, const char *text);
  184. void tile(void);
  185. void togglebar(const char *arg);
  186. void togglefloating(const char *arg);
  187. void toggletag(const char *arg);
  188. void toggleview(const char *arg);
  189. void unban(Client *c);
  190. void unmanage(Client *c);
  191. void unmapnotify(XEvent *e);
  192. void updatebarpos(Monitor *s);
  193. void updatesizehints(Client *c);
  194. void updatetitle(Client *c);
  195. void view(const char *arg);
  196. void viewprevtag(const char *arg); /* views previous selected tags */
  197. int xerror(Display *dpy, XErrorEvent *ee);
  198. int xerrordummy(Display *dsply, XErrorEvent *ee);
  199. int xerrorstart(Display *dsply, XErrorEvent *ee);
  200. void zoom(const char *arg);
  201. int monitorat(int, int);
  202. void movetomonitor(const char *arg);
  203. void selectmonitor(const char *arg);
  204. /* variables */
  205. char stext[256];
  206. int mcount, screen;
  207. //double mwfact;
  208. //int screen, sx, sy, sw, sh, wax, way, waw, wah;
  209. int (*xerrorxlib)(Display *, XErrorEvent *);
  210. unsigned int bh, bpos;
  211. unsigned int blw = 0;
  212. unsigned int numlockmask = 0;
  213. void (*handler[LASTEvent]) (XEvent *) = {
  214. [ButtonPress] = buttonpress,
  215. [ConfigureRequest] = configurerequest,
  216. [ConfigureNotify] = configurenotify,
  217. [DestroyNotify] = destroynotify,
  218. [EnterNotify] = enternotify,
  219. [Expose] = expose,
  220. [FocusIn] = focusin,
  221. [KeyPress] = keypress,
  222. [LeaveNotify] = leavenotify,
  223. [MappingNotify] = mappingnotify,
  224. [MapRequest] = maprequest,
  225. [PropertyNotify] = propertynotify,
  226. [UnmapNotify] = unmapnotify
  227. };
  228. Atom wmatom[WMLast], netatom[NetLast];
  229. Bool domwfact = True;
  230. Bool dozoom = True;
  231. Bool otherwm, readin;
  232. Bool running = True;
  233. //Bool selscreen = True;
  234. Client *clients = NULL;
  235. Client *sel = NULL;
  236. Client *stack = NULL;
  237. Cursor cursor[CurLast];
  238. Display *dpy;
  239. DC dc = {0};
  240. Window root;
  241. //Layout *layout = NULL;
  242. //Window barwin, root;
  243. Regs *regs = NULL;
  244. Monitor *monitors;
  245. int selmonitor = 0;
  246. /* configuration, allows nested code to access above variables */
  247. #include "config.h"
  248. //Bool prevtags[LENGTH(tags)];
  249. /* function implementations */
  250. void
  251. applyrules(Client *c) {
  252. static char buf[512];
  253. unsigned int i, j;
  254. regmatch_t tmp;
  255. Bool matched_tag = False;
  256. Bool matched_monitor = False;
  257. XClassHint ch = { 0 };
  258. /* rule matching */
  259. XGetClassHint(dpy, c->win, &ch);
  260. snprintf(buf, sizeof buf, "%s:%s:%s",
  261. ch.res_class ? ch.res_class : "",
  262. ch.res_name ? ch.res_name : "", c->name);
  263. for(i = 0; i < LENGTH(rules); i++)
  264. if(regs[i].propregex && !regexec(regs[i].propregex, buf, 1, &tmp, 0)) {
  265. if (rules[i].monitor >= 0 && rules[i].monitor < mcount) {
  266. matched_monitor = True;
  267. c->monitor = rules[i].monitor;
  268. }
  269. c->isfloating = rules[i].isfloating;
  270. for(j = 0; regs[i].tagregex && j < LENGTH(tags); j++) {
  271. if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) {
  272. matched_tag = True;
  273. c->tags[j] = True;
  274. }
  275. }
  276. }
  277. if(ch.res_class)
  278. XFree(ch.res_class);
  279. if(ch.res_name)
  280. XFree(ch.res_name);
  281. if(!matched_tag)
  282. memcpy(c->tags, monitors[monitorat(-1, -1)].seltags, sizeof initags);
  283. if (!matched_monitor)
  284. c->monitor = monitorat(-1, -1);
  285. }
  286. void
  287. arrange(void) {
  288. Client *c;
  289. for(c = clients; c; c = c->next)
  290. if(isvisible(c, &monitors[c->monitor]))
  291. unban(c);
  292. else
  293. ban(c);
  294. monitors[selmonitor].layout->arrange();
  295. focus(NULL);
  296. restack();
  297. }
  298. void
  299. attach(Client *c) {
  300. if(clients)
  301. clients->prev = c;
  302. c->next = clients;
  303. clients = c;
  304. }
  305. void
  306. attachstack(Client *c) {
  307. c->snext = stack;
  308. stack = c;
  309. }
  310. void
  311. ban(Client *c) {
  312. if(c->isbanned)
  313. return;
  314. XMoveWindow(dpy, c->win, c->x + 3 * monitors[c->monitor].sw, c->y);
  315. c->isbanned = True;
  316. }
  317. void
  318. buttonpress(XEvent *e) {
  319. unsigned int i, x;
  320. Client *c;
  321. XButtonPressedEvent *ev = &e->xbutton;
  322. Monitor s = monitors[monitorat(-1, -1)];
  323. if(ev->window == s.barwin) {
  324. x = 0;
  325. for(i = 0; i < LENGTH(tags); i++) {
  326. x += textw(&s, tags[i]);
  327. if(ev->x < x) {
  328. if(ev->button == Button1) {
  329. if(ev->state & MODKEY)
  330. tag(tags[i]);
  331. else
  332. view(tags[i]);
  333. }
  334. else if(ev->button == Button3) {
  335. if(ev->state & MODKEY)
  336. toggletag(tags[i]);
  337. else
  338. toggleview(tags[i]);
  339. }
  340. return;
  341. }
  342. }
  343. if((ev->x < x + blw) && ev->button == Button1)
  344. setlayout(NULL);
  345. }
  346. else if((c = getclient(ev->window))) {
  347. focus(c);
  348. if(CLEANMASK(ev->state) != MODKEY)
  349. return;
  350. if(ev->button == Button1) {
  351. if((s.layout->arrange == floating) || c->isfloating)
  352. restack();
  353. else
  354. togglefloating(NULL);
  355. movemouse(c);
  356. }
  357. else if(ev->button == Button2) {
  358. if((floating != s.layout->arrange) && c->isfloating)
  359. togglefloating(NULL);
  360. else
  361. zoom(NULL);
  362. }
  363. else if(ev->button == Button3 && !c->isfixed) {
  364. if((floating == s.layout->arrange) || c->isfloating)
  365. restack();
  366. else
  367. togglefloating(NULL);
  368. resizemouse(c);
  369. }
  370. }
  371. }
  372. void
  373. checkotherwm(void) {
  374. otherwm = False;
  375. XSetErrorHandler(xerrorstart);
  376. /* this causes an error if some other window manager is running */
  377. XSelectInput(dpy, root, SubstructureRedirectMask);
  378. XSync(dpy, False);
  379. if(otherwm)
  380. eprint("dwm: another window manager is already running\n");
  381. XSync(dpy, False);
  382. XSetErrorHandler(NULL);
  383. xerrorxlib = XSetErrorHandler(xerror);
  384. XSync(dpy, False);
  385. }
  386. void
  387. cleanup(void) {
  388. unsigned int i;
  389. close(STDIN_FILENO);
  390. while(stack) {
  391. unban(stack);
  392. unmanage(stack);
  393. }
  394. for(i = 0; i < mcount; i++) {
  395. Monitor *m = &monitors[i];
  396. if(m->dc.font.set)
  397. XFreeFontSet(dpy, m->dc.font.set);
  398. else
  399. XFreeFont(dpy, m->dc.font.xfont);
  400. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  401. XFreePixmap(dpy, m->dc.drawable);
  402. XFreeGC(dpy, m->dc.gc);
  403. XDestroyWindow(dpy, m->barwin);
  404. XFreeCursor(dpy, cursor[CurNormal]);
  405. XFreeCursor(dpy, cursor[CurResize]);
  406. XFreeCursor(dpy, cursor[CurMove]);
  407. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  408. XSync(dpy, False);
  409. }
  410. }
  411. void
  412. compileregs(void) {
  413. unsigned int i;
  414. regex_t *reg;
  415. if(regs)
  416. return;
  417. regs = emallocz(LENGTH(rules) * sizeof(Regs));
  418. for(i = 0; i < LENGTH(rules); i++) {
  419. if(rules[i].prop) {
  420. reg = emallocz(sizeof(regex_t));
  421. if(regcomp(reg, rules[i].prop, REG_EXTENDED))
  422. free(reg);
  423. else
  424. regs[i].propregex = reg;
  425. }
  426. if(rules[i].tags) {
  427. reg = emallocz(sizeof(regex_t));
  428. if(regcomp(reg, rules[i].tags, REG_EXTENDED))
  429. free(reg);
  430. else
  431. regs[i].tagregex = reg;
  432. }
  433. }
  434. }
  435. void
  436. configure(Client *c) {
  437. XConfigureEvent ce;
  438. ce.type = ConfigureNotify;
  439. ce.display = dpy;
  440. ce.event = c->win;
  441. ce.window = c->win;
  442. ce.x = c->x;
  443. ce.y = c->y;
  444. ce.width = c->w;
  445. ce.height = c->h;
  446. ce.border_width = c->border;
  447. ce.above = None;
  448. ce.override_redirect = False;
  449. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  450. }
  451. void
  452. configurenotify(XEvent *e) {
  453. XConfigureEvent *ev = &e->xconfigure;
  454. Monitor *m = &monitors[selmonitor];
  455. if(ev->window == root && (ev->width != m->sw || ev->height != m->sh)) {
  456. m->sw = ev->width;
  457. m->sh = ev->height;
  458. XFreePixmap(dpy, dc.drawable);
  459. dc.drawable = XCreatePixmap(dpy, root, m->sw, bh, DefaultDepth(dpy, screen));
  460. XResizeWindow(dpy, m->barwin, m->sw, bh);
  461. updatebarpos(m);
  462. arrange();
  463. }
  464. }
  465. void
  466. configurerequest(XEvent *e) {
  467. Client *c;
  468. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  469. XWindowChanges wc;
  470. if((c = getclient(ev->window))) {
  471. Monitor *m = &monitors[c->monitor];
  472. if(ev->value_mask & CWBorderWidth)
  473. c->border = ev->border_width;
  474. if(c->isfixed || c->isfloating || (floating == m->layout->arrange)) {
  475. if(ev->value_mask & CWX)
  476. c->x = m->sx+ev->x;
  477. if(ev->value_mask & CWY)
  478. c->y = m->sy+ev->y;
  479. if(ev->value_mask & CWWidth)
  480. c->w = ev->width;
  481. if(ev->value_mask & CWHeight)
  482. c->h = ev->height;
  483. if((c->x - m->sx + c->w) > m->sw && c->isfloating)
  484. c->x = m->sx + (m->sw / 2 - c->w / 2); /* center in x direction */
  485. if((c->y - m->sy + c->h) > m->sh && c->isfloating)
  486. c->y = m->sy + (m->sh / 2 - c->h / 2); /* center in y direction */
  487. if((ev->value_mask & (CWX | CWY))
  488. && !(ev->value_mask & (CWWidth | CWHeight)))
  489. configure(c);
  490. if(isvisible(c, &monitors[monitorat(-1,-1)]))
  491. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  492. }
  493. else
  494. configure(c);
  495. }
  496. else {
  497. wc.x = ev->x;
  498. wc.y = ev->y;
  499. wc.width = ev->width;
  500. wc.height = ev->height;
  501. wc.border_width = ev->border_width;
  502. wc.sibling = ev->above;
  503. wc.stack_mode = ev->detail;
  504. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  505. }
  506. XSync(dpy, False);
  507. }
  508. void
  509. destroynotify(XEvent *e) {
  510. Client *c;
  511. XDestroyWindowEvent *ev = &e->xdestroywindow;
  512. if((c = getclient(ev->window)))
  513. unmanage(c);
  514. }
  515. void
  516. detach(Client *c) {
  517. if(c->prev)
  518. c->prev->next = c->next;
  519. if(c->next)
  520. c->next->prev = c->prev;
  521. if(c == clients)
  522. clients = c->next;
  523. c->next = c->prev = NULL;
  524. }
  525. void
  526. detachstack(Client *c) {
  527. Client **tc;
  528. for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
  529. *tc = c->snext;
  530. }
  531. void
  532. drawbar(void) {
  533. int i, x, s;
  534. for(s = 0; s < mcount; ++s) {
  535. Monitor *m = &monitors[s];
  536. m->dc.x = 0;
  537. for(i = 0; i < LENGTH(tags); i++) {
  538. m->dc.w = textw(m, tags[i]);
  539. if(m->seltags[i]) {
  540. drawtext(m, tags[i], m->dc.sel);
  541. drawsquare(m, sel && sel->tags[i] && sel->monitor == m->id, isoccupied(m, i), m->dc.sel);
  542. }
  543. else {
  544. drawtext(m, tags[i], m->dc.norm);
  545. drawsquare(m, sel && sel->tags[i] && sel->monitor == m->id, isoccupied(m, i), m->dc.norm);
  546. }
  547. m->dc.x += m->dc.w;
  548. }
  549. m->dc.w = blw;
  550. drawtext(m, m->layout->symbol, m->dc.norm);
  551. x = m->dc.x + m->dc.w;
  552. m->dc.w = textw(m, stext);
  553. m->dc.x = m->sw - m->dc.w;
  554. if(m->dc.x < x) {
  555. m->dc.x = x;
  556. m->dc.w = m->sw - x;
  557. }
  558. drawtext(m, stext, m->dc.norm);
  559. if((m->dc.w = m->dc.x - x) > bh) {
  560. m->dc.x = x;
  561. if(sel && sel->monitor == m->id) {
  562. drawtext(m, sel->name, m->dc.sel);
  563. drawsquare(m, False, sel->isfloating, m->dc.sel);
  564. }
  565. else
  566. drawtext(m, NULL, m->dc.norm);
  567. }
  568. XCopyArea(dpy, m->dc.drawable, m->barwin, m->dc.gc, 0, 0, m->sw, bh, 0, 0);
  569. XSync(dpy, False);
  570. }
  571. }
  572. void
  573. drawsquare(Monitor *m, Bool filled, Bool empty, unsigned long col[ColLast]) {
  574. int x;
  575. XGCValues gcv;
  576. XRectangle r = { m->dc.x, m->dc.y, m->dc.w, m->dc.h };
  577. gcv.foreground = col[ColFG];
  578. XChangeGC(dpy, m->dc.gc, GCForeground, &gcv);
  579. x = (m->dc.font.ascent + m->dc.font.descent + 2) / 4;
  580. r.x = m->dc.x + 1;
  581. r.y = m->dc.y + 1;
  582. if(filled) {
  583. r.width = r.height = x + 1;
  584. XFillRectangles(dpy, m->dc.drawable, m->dc.gc, &r, 1);
  585. }
  586. else if(empty) {
  587. r.width = r.height = x;
  588. XDrawRectangles(dpy, m->dc.drawable, m->dc.gc, &r, 1);
  589. }
  590. }
  591. void
  592. drawtext(Monitor *m, const char *text, unsigned long col[ColLast]) {
  593. int x, y, w, h;
  594. static char buf[256];
  595. unsigned int len, olen;
  596. XRectangle r = { m->dc.x, m->dc.y, m->dc.w, m->dc.h };
  597. XSetForeground(dpy, m->dc.gc, col[ColBG]);
  598. XFillRectangles(dpy, m->dc.drawable, m->dc.gc, &r, 1);
  599. if(!text)
  600. return;
  601. w = 0;
  602. olen = len = strlen(text);
  603. if(len >= sizeof buf)
  604. len = sizeof buf - 1;
  605. memcpy(buf, text, len);
  606. buf[len] = 0;
  607. h = m->dc.font.ascent + m->dc.font.descent;
  608. y = m->dc.y + (m->dc.h / 2) - (h / 2) + m->dc.font.ascent;
  609. x = m->dc.x + (h / 2);
  610. /* shorten text if necessary */
  611. while(len && (w = textnw(m, buf, len)) > m->dc.w - h)
  612. buf[--len] = 0;
  613. if(len < olen) {
  614. if(len > 1)
  615. buf[len - 1] = '.';
  616. if(len > 2)
  617. buf[len - 2] = '.';
  618. if(len > 3)
  619. buf[len - 3] = '.';
  620. }
  621. if(w > m->dc.w)
  622. return; /* too long */
  623. XSetForeground(dpy, m->dc.gc, col[ColFG]);
  624. if(m->dc.font.set)
  625. XmbDrawString(dpy, m->dc.drawable, m->dc.font.set, m->dc.gc, x, y, buf, len);
  626. else
  627. XDrawString(dpy, m->dc.drawable, m->dc.gc, x, y, buf, len);
  628. }
  629. void *
  630. emallocz(unsigned int size) {
  631. void *res = calloc(1, size);
  632. if(!res)
  633. eprint("fatal: could not malloc() %u bytes\n", size);
  634. return res;
  635. }
  636. void
  637. enternotify(XEvent *e) {
  638. Client *c;
  639. XCrossingEvent *ev = &e->xcrossing;
  640. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  641. return;
  642. if((c = getclient(ev->window)))
  643. focus(c);
  644. else if(ev->window == root) {
  645. selmonitor = True;
  646. focus(NULL);
  647. }
  648. }
  649. void
  650. eprint(const char *errstr, ...) {
  651. va_list ap;
  652. va_start(ap, errstr);
  653. vfprintf(stderr, errstr, ap);
  654. va_end(ap);
  655. exit(EXIT_FAILURE);
  656. }
  657. void
  658. expose(XEvent *e) {
  659. XExposeEvent *ev = &e->xexpose;
  660. if(ev->count == 0) {
  661. if(ev->window == monitors[selmonitor].barwin)
  662. drawbar();
  663. }
  664. }
  665. void
  666. floating(void) { /* default floating layout */
  667. Client *c;
  668. domwfact = dozoom = False;
  669. for(c = clients; c; c = c->next)
  670. if(isvisible(c, &monitors[selmonitor]))
  671. resize(c, c->x, c->y, c->w, c->h, True);
  672. }
  673. void
  674. focus(Client *c) {
  675. Monitor *m = &monitors[monitorat(-1, -1)];
  676. if(!c || (c && !isvisible(c, m)))
  677. for(c = stack; c && !isvisible(c, m); c = c->snext);
  678. if(sel && sel != c) {
  679. grabbuttons(sel, False);
  680. XSetWindowBorder(dpy, sel->win, monitors[sel->monitor].dc.norm[ColBorder]);
  681. }
  682. if(c) {
  683. detachstack(c);
  684. attachstack(c);
  685. grabbuttons(c, True);
  686. }
  687. sel = c;
  688. drawbar();
  689. if(c) {
  690. XSetWindowBorder(dpy, c->win, monitors[c->monitor].dc.sel[ColBorder]);
  691. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  692. selmonitor = monitorat(c->x, c->y);
  693. }
  694. else {
  695. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  696. selmonitor = monitorat(-1, -1);
  697. }
  698. }
  699. void
  700. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  701. XFocusChangeEvent *ev = &e->xfocus;
  702. if(sel && ev->window != sel->win)
  703. XSetInputFocus(dpy, sel->win, RevertToPointerRoot, CurrentTime);
  704. }
  705. void
  706. focusnext(const char *arg) {
  707. Client *c;
  708. Monitor *m = &monitors[selmonitor];
  709. if(!sel)
  710. return;
  711. for(c = sel->next; c && !isvisible(c, m); c = c->next);
  712. if(!c)
  713. for(c = clients; c && !isvisible(c, m); c = c->next);
  714. if(c) {
  715. focus(c);
  716. restack();
  717. }
  718. }
  719. void
  720. focusprev(const char *arg) {
  721. Client *c;
  722. Monitor *m = &monitors[selmonitor];
  723. if(!sel)
  724. return;
  725. for(c = sel->prev; c && !isvisible(c, m); c = c->prev);
  726. if(!c) {
  727. for(c = clients; c && c->next; c = c->next);
  728. for(; c && !isvisible(c, m); c = c->prev);
  729. }
  730. if(c) {
  731. focus(c);
  732. restack();
  733. }
  734. }
  735. Client *
  736. getclient(Window w) {
  737. Client *c;
  738. for(c = clients; c && c->win != w; c = c->next);
  739. return c;
  740. }
  741. unsigned long
  742. getcolor(const char *colstr) {
  743. Colormap cmap = DefaultColormap(dpy, screen);
  744. XColor color;
  745. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  746. eprint("error, cannot allocate color '%s'\n", colstr);
  747. return color.pixel;
  748. }
  749. long
  750. getstate(Window w) {
  751. int format, status;
  752. long result = -1;
  753. unsigned char *p = NULL;
  754. unsigned long n, extra;
  755. Atom real;
  756. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  757. &real, &format, &n, &extra, (unsigned char **)&p);
  758. if(status != Success)
  759. return -1;
  760. if(n != 0)
  761. result = *p;
  762. XFree(p);
  763. return result;
  764. }
  765. Bool
  766. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  767. char **list = NULL;
  768. int n;
  769. XTextProperty name;
  770. if(!text || size == 0)
  771. return False;
  772. text[0] = '\0';
  773. XGetTextProperty(dpy, w, &name, atom);
  774. if(!name.nitems)
  775. return False;
  776. if(name.encoding == XA_STRING)
  777. strncpy(text, (char *)name.value, size - 1);
  778. else {
  779. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  780. && n > 0 && *list) {
  781. strncpy(text, *list, size - 1);
  782. XFreeStringList(list);
  783. }
  784. }
  785. text[size - 1] = '\0';
  786. XFree(name.value);
  787. return True;
  788. }
  789. void
  790. grabbuttons(Client *c, Bool focused) {
  791. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  792. if(focused) {
  793. XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  794. GrabModeAsync, GrabModeSync, None, None);
  795. XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
  796. GrabModeAsync, GrabModeSync, None, None);
  797. XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  798. GrabModeAsync, GrabModeSync, None, None);
  799. XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  800. GrabModeAsync, GrabModeSync, None, None);
  801. XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  802. GrabModeAsync, GrabModeSync, None, None);
  803. XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
  804. GrabModeAsync, GrabModeSync, None, None);
  805. XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  806. GrabModeAsync, GrabModeSync, None, None);
  807. XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  808. GrabModeAsync, GrabModeSync, None, None);
  809. XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  810. GrabModeAsync, GrabModeSync, None, None);
  811. XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
  812. GrabModeAsync, GrabModeSync, None, None);
  813. XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  814. GrabModeAsync, GrabModeSync, None, None);
  815. XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  816. GrabModeAsync, GrabModeSync, None, None);
  817. }
  818. else
  819. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
  820. GrabModeAsync, GrabModeSync, None, None);
  821. }
  822. void
  823. grabkeys(void) {
  824. unsigned int i;
  825. KeyCode code;
  826. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  827. for(i = 0; i < LENGTH(keys); i++) {
  828. code = XKeysymToKeycode(dpy, keys[i].keysym);
  829. XGrabKey(dpy, code, keys[i].mod, root, True,
  830. GrabModeAsync, GrabModeAsync);
  831. XGrabKey(dpy, code, keys[i].mod | LockMask, root, True,
  832. GrabModeAsync, GrabModeAsync);
  833. XGrabKey(dpy, code, keys[i].mod | numlockmask, root, True,
  834. GrabModeAsync, GrabModeAsync);
  835. XGrabKey(dpy, code, keys[i].mod | numlockmask | LockMask, root, True,
  836. GrabModeAsync, GrabModeAsync);
  837. }
  838. }
  839. unsigned int
  840. idxoftag(const char *tag) {
  841. unsigned int i;
  842. for(i = 0; (i < LENGTH(tags)) && (tags[i] != tag); i++);
  843. return (i < LENGTH(tags)) ? i : 0;
  844. }
  845. void
  846. initfont(Monitor *m, const char *fontstr) {
  847. char *def, **missing;
  848. int i, n;
  849. missing = NULL;
  850. if(m->dc.font.set)
  851. XFreeFontSet(dpy, m->dc.font.set);
  852. m->dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  853. if(missing) {
  854. while(n--)
  855. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  856. XFreeStringList(missing);
  857. }
  858. if(m->dc.font.set) {
  859. XFontSetExtents *font_extents;
  860. XFontStruct **xfonts;
  861. char **font_names;
  862. m->dc.font.ascent = m->dc.font.descent = 0;
  863. font_extents = XExtentsOfFontSet(m->dc.font.set);
  864. n = XFontsOfFontSet(m->dc.font.set, &xfonts, &font_names);
  865. for(i = 0, m->dc.font.ascent = 0, m->dc.font.descent = 0; i < n; i++) {
  866. if(m->dc.font.ascent < (*xfonts)->ascent)
  867. m->dc.font.ascent = (*xfonts)->ascent;
  868. if(m->dc.font.descent < (*xfonts)->descent)
  869. m->dc.font.descent = (*xfonts)->descent;
  870. xfonts++;
  871. }
  872. }
  873. else {
  874. if(m->dc.font.xfont)
  875. XFreeFont(dpy, m->dc.font.xfont);
  876. m->dc.font.xfont = NULL;
  877. if(!(m->dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  878. && !(m->dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  879. eprint("error, cannot load font: '%s'\n", fontstr);
  880. m->dc.font.ascent = m->dc.font.xfont->ascent;
  881. m->dc.font.descent = m->dc.font.xfont->descent;
  882. }
  883. m->dc.font.height = m->dc.font.ascent + m->dc.font.descent;
  884. }
  885. Bool
  886. isoccupied(Monitor *m, unsigned int t) {
  887. Client *c;
  888. for(c = clients; c; c = c->next)
  889. if(c->tags[t] && c->monitor == m->id)
  890. return True;
  891. return False;
  892. }
  893. Bool
  894. isprotodel(Client *c) {
  895. int i, n;
  896. Atom *protocols;
  897. Bool ret = False;
  898. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  899. for(i = 0; !ret && i < n; i++)
  900. if(protocols[i] == wmatom[WMDelete])
  901. ret = True;
  902. XFree(protocols);
  903. }
  904. return ret;
  905. }
  906. Bool
  907. isvisible(Client *c, Monitor *m) {
  908. unsigned int i;
  909. for(i = 0; i < LENGTH(tags); i++)
  910. if(c->tags[i] && monitors[c->monitor].seltags[i] && m->id == c->monitor)
  911. return True;
  912. return False;
  913. }
  914. void
  915. keypress(XEvent *e) {
  916. unsigned int i;
  917. KeySym keysym;
  918. XKeyEvent *ev;
  919. ev = &e->xkey;
  920. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  921. for(i = 0; i < LENGTH(keys); i++)
  922. if(keysym == keys[i].keysym
  923. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state))
  924. {
  925. if(keys[i].func)
  926. keys[i].func(keys[i].arg);
  927. }
  928. }
  929. void
  930. killclient(const char *arg) {
  931. XEvent ev;
  932. if(!sel)
  933. return;
  934. if(isprotodel(sel)) {
  935. ev.type = ClientMessage;
  936. ev.xclient.window = sel->win;
  937. ev.xclient.message_type = wmatom[WMProtocols];
  938. ev.xclient.format = 32;
  939. ev.xclient.data.l[0] = wmatom[WMDelete];
  940. ev.xclient.data.l[1] = CurrentTime;
  941. XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
  942. }
  943. else
  944. XKillClient(dpy, sel->win);
  945. }
  946. void
  947. leavenotify(XEvent *e) {
  948. XCrossingEvent *ev = &e->xcrossing;
  949. if((ev->window == root) && !ev->same_screen) {
  950. selmonitor = False;
  951. focus(NULL);
  952. }
  953. }
  954. void
  955. manage(Window w, XWindowAttributes *wa) {
  956. Client *c, *t = NULL;
  957. Window trans;
  958. Status rettrans;
  959. XWindowChanges wc;
  960. c = emallocz(sizeof(Client));
  961. c->tags = emallocz(sizeof initags);
  962. c->win = w;
  963. applyrules(c);
  964. Monitor *m = &monitors[c->monitor];
  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, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1039. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1040. return;
  1041. XQueryPointer(dpy, 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 = scr.id;
  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, 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. Client *c;
  1218. XEvent ev;
  1219. XWindowChanges wc;
  1220. int s;
  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(s = 0; s < mcount; s++) {
  1234. for(c = nexttiled(clients, &monitors[s]); c; c = nexttiled(c->next, &monitors[s])) {
  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, num;
  1306. Window *wins, d1, d2;
  1307. XWindowAttributes wa;
  1308. wins = NULL;
  1309. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1310. for(i = 0; i < num; i++) {
  1311. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1312. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1313. continue;
  1314. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1315. manage(wins[i], &wa);
  1316. }
  1317. for(i = 0; i < num; i++) { /* now the transients */
  1318. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1319. continue;
  1320. if(XGetTransientForHint(dpy, wins[i], &d1)
  1321. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1322. manage(wins[i], &wa);
  1323. }
  1324. }
  1325. if(wins)
  1326. XFree(wins);
  1327. }
  1328. void
  1329. setclientstate(Client *c, long state) {
  1330. long data[] = {state, None};
  1331. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1332. PropModeReplace, (unsigned char *)data, 2);
  1333. }
  1334. void
  1335. setlayout(const char *arg) {
  1336. unsigned int i;
  1337. Monitor *m = &monitors[monitorat(-1, -1)];
  1338. if(!arg) {
  1339. m->layout++;
  1340. if(m->layout == &layouts[LENGTH(layouts)])
  1341. m->layout = &layouts[0];
  1342. }
  1343. else {
  1344. for(i = 0; i < LENGTH(layouts); i++)
  1345. if(!strcmp(arg, layouts[i].symbol))
  1346. break;
  1347. if(i == LENGTH(layouts))
  1348. return;
  1349. m->layout = &layouts[i];
  1350. }
  1351. if(sel)
  1352. arrange();
  1353. else
  1354. drawbar();
  1355. }
  1356. void
  1357. setmwfact(const char *arg) {
  1358. double delta;
  1359. Monitor *m = &monitors[monitorat(-1, -1)];
  1360. if(!domwfact)
  1361. return;
  1362. /* arg handling, manipulate mwfact */
  1363. if(arg == NULL)
  1364. m->mwfact = MWFACT;
  1365. else if(sscanf(arg, "%lf", &delta) == 1) {
  1366. if(arg[0] == '+' || arg[0] == '-')
  1367. m->mwfact += delta;
  1368. else
  1369. m->mwfact = delta;
  1370. if(m->mwfact < 0.1)
  1371. m->mwfact = 0.1;
  1372. else if(m->mwfact > 0.9)
  1373. m->mwfact = 0.9;
  1374. }
  1375. arrange();
  1376. }
  1377. void
  1378. setup(void) {
  1379. unsigned int i, j, k;
  1380. XModifierKeymap *modmap;
  1381. XSetWindowAttributes wa;
  1382. int s = 1;
  1383. GC g;
  1384. XineramaScreenInfo *info = NULL;
  1385. /* init atoms */
  1386. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1387. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1388. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1389. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1390. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1391. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1392. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1393. PropModeReplace, (unsigned char *) netatom, NetLast);
  1394. /* init cursors */
  1395. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1396. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1397. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1398. /* init modifier map */
  1399. modmap = XGetModifierMapping(dpy);
  1400. for(i = 0; i < 8; i++)
  1401. for(j = 0; j < modmap->max_keypermod; j++) {
  1402. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1403. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1404. numlockmask = (1 << i);
  1405. }
  1406. XFreeModifiermap(modmap);
  1407. /* select for events */
  1408. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  1409. | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
  1410. wa.cursor = cursor[CurNormal];
  1411. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  1412. XSelectInput(dpy, root, wa.event_mask);
  1413. /* grab keys */
  1414. grabkeys();
  1415. /* init tags */
  1416. compileregs();
  1417. if (XineramaIsActive(dpy)) {
  1418. info = XineramaQueryScreens(dpy, &s);
  1419. }
  1420. monitors = emallocz(s*sizeof(Monitor));
  1421. mcount = s;
  1422. for(i = 0; i < s; i++) {
  1423. /* init geometry */
  1424. if (mcount != 1) {
  1425. monitors[i].sx = info[i].x_org;
  1426. monitors[i].sy = info[i].y_org;
  1427. monitors[i].sw = info[i].width;
  1428. monitors[i].sh = info[i].height;
  1429. }
  1430. else {
  1431. monitors[i].sx = 0;
  1432. monitors[i].sy = 0;
  1433. monitors[i].sw = DisplayWidth(dpy, screen);
  1434. monitors[i].sh = DisplayHeight(dpy, screen);
  1435. }
  1436. monitors[i].id = i;
  1437. monitors[i].seltags = emallocz(sizeof initags);
  1438. monitors[i].prevtags = emallocz(sizeof initags);
  1439. memcpy(monitors[i].seltags, initags, sizeof initags);
  1440. memcpy(monitors[i].prevtags, initags, sizeof initags);
  1441. /* init appearance */
  1442. monitors[i].dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  1443. monitors[i].dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  1444. monitors[i].dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  1445. monitors[i].dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  1446. monitors[i].dc.sel[ColBG] = getcolor(SELBGCOLOR);
  1447. monitors[i].dc.sel[ColFG] = getcolor(SELFGCOLOR);
  1448. initfont(&(monitors[i]), FONT);
  1449. monitors[i].dc.h = bh = monitors[i].dc.font.height + 2;
  1450. /* init layouts */
  1451. monitors[i].mwfact = MWFACT;
  1452. monitors[i].layout = &layouts[0];
  1453. for(blw = k = 0; k < LENGTH(layouts); k++) {
  1454. j = textw(&monitors[i], layouts[k].symbol);
  1455. if(j > blw)
  1456. blw = j;
  1457. }
  1458. bpos = BARPOS;
  1459. wa.override_redirect = 1;
  1460. wa.background_pixmap = ParentRelative;
  1461. wa.event_mask = ButtonPressMask | ExposureMask;
  1462. /* init bars */
  1463. monitors[i].barwin = XCreateWindow(dpy, root, monitors[i].sx, monitors[i].sy, monitors[i].sw, bh, 0,
  1464. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  1465. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  1466. XDefineCursor(dpy, monitors[i].barwin, cursor[CurNormal]);
  1467. updatebarpos(&monitors[i]);
  1468. XMapRaised(dpy, monitors[i].barwin);
  1469. strcpy(stext, "dwm-"VERSION);
  1470. monitors[i].dc.drawable = XCreatePixmap(dpy, root, monitors[i].sw, bh, DefaultDepth(dpy, screen));
  1471. g = XCreateGC(dpy, root, 0, 0);
  1472. monitors[i].dc.gc = XCreateGC(dpy, root, 0, 0);
  1473. XSetLineAttributes(dpy, monitors[i].dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1474. if(!monitors[i].dc.font.set)
  1475. XSetFont(dpy, monitors[i].dc.gc, monitors[i].dc.font.xfont->fid);
  1476. }
  1477. }
  1478. void
  1479. spawn(const char *arg) {
  1480. static char *shell = NULL;
  1481. if(!shell && !(shell = getenv("SHELL")))
  1482. shell = "/bin/sh";
  1483. if(!arg)
  1484. return;
  1485. /* The double-fork construct avoids zombie processes and keeps the code
  1486. * clean from stupid signal handlers. */
  1487. if(fork() == 0) {
  1488. if(fork() == 0) {
  1489. if(dpy)
  1490. close(ConnectionNumber(dpy));
  1491. setsid();
  1492. execl(shell, shell, "-c", arg, (char *)NULL);
  1493. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
  1494. perror(" failed");
  1495. }
  1496. exit(0);
  1497. }
  1498. wait(0);
  1499. }
  1500. void
  1501. tag(const char *arg) {
  1502. unsigned int i;
  1503. if(!sel)
  1504. return;
  1505. for(i = 0; i < LENGTH(tags); i++)
  1506. sel->tags[i] = (NULL == arg);
  1507. sel->tags[idxoftag(arg)] = True;
  1508. arrange();
  1509. }
  1510. unsigned int
  1511. textnw(Monitor *m, const char *text, unsigned int len) {
  1512. XRectangle r;
  1513. if(m->dc.font.set) {
  1514. XmbTextExtents(m->dc.font.set, text, len, NULL, &r);
  1515. return r.width;
  1516. }
  1517. return XTextWidth(m->dc.font.xfont, text, len);
  1518. }
  1519. unsigned int
  1520. textw(Monitor *m, const char *text) {
  1521. return textnw(m, text, strlen(text)) + m->dc.font.height;
  1522. }
  1523. void
  1524. tile(void) {
  1525. unsigned int i, n, nx, ny, nw, nh, mw, th;
  1526. int s;
  1527. Client *c, *mc;
  1528. domwfact = dozoom = True;
  1529. nw = 0; /* gcc stupidity requires this */
  1530. for (s = 0; s < mcount; s++) {
  1531. Monitor *m = &monitors[s];
  1532. for(n = 0, c = nexttiled(clients, m); c; c = nexttiled(c->next, m))
  1533. n++;
  1534. for(i = 0, c = mc = nexttiled(clients, m); c; c = nexttiled(c->next, m)) {
  1535. /* window geoms */
  1536. mw = (n == 1) ? m->waw : m->mwfact * m->waw;
  1537. th = (n > 1) ? m->wah / (n - 1) : 0;
  1538. if(n > 1 && th < bh)
  1539. th = m->wah;
  1540. if(i == 0) { /* master */
  1541. nx = m->wax;
  1542. ny = m->way;
  1543. nw = mw - 2 * c->border;
  1544. nh = m->wah - 2 * c->border;
  1545. }
  1546. else { /* tile window */
  1547. if(i == 1) {
  1548. ny = m->way;
  1549. nx += mc->w + 2 * mc->border;
  1550. nw = m->waw - mw - 2 * c->border;
  1551. }
  1552. if(i + 1 == n) /* remainder */
  1553. nh = (m->way + m->wah) - ny - 2 * c->border;
  1554. else
  1555. nh = th - 2 * c->border;
  1556. }
  1557. resize(c, nx, ny, nw, nh, RESIZEHINTS);
  1558. if((RESIZEHINTS) && ((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw)))
  1559. /* client doesn't accept size constraints */
  1560. resize(c, nx, ny, nw, nh, False);
  1561. if(n > 1 && th != m->wah)
  1562. ny = c->y + c->h + 2 * c->border;
  1563. i++;
  1564. }
  1565. }
  1566. }
  1567. void
  1568. togglebar(const char *arg) {
  1569. if(bpos == BarOff)
  1570. bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
  1571. else
  1572. bpos = BarOff;
  1573. updatebarpos(&monitors[monitorat(-1,-1)]);
  1574. arrange();
  1575. }
  1576. void
  1577. togglefloating(const char *arg) {
  1578. if(!sel)
  1579. return;
  1580. sel->isfloating = !sel->isfloating;
  1581. if(sel->isfloating)
  1582. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1583. arrange();
  1584. }
  1585. void
  1586. toggletag(const char *arg) {
  1587. unsigned int i, j;
  1588. if(!sel)
  1589. return;
  1590. i = idxoftag(arg);
  1591. sel->tags[i] = !sel->tags[i];
  1592. for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
  1593. if(j == LENGTH(tags))
  1594. sel->tags[i] = True; /* at least one tag must be enabled */
  1595. arrange();
  1596. }
  1597. void
  1598. toggleview(const char *arg) {
  1599. unsigned int i, j;
  1600. Monitor *m = &monitors[monitorat(-1, -1)];
  1601. i = idxoftag(arg);
  1602. m->seltags[i] = !m->seltags[i];
  1603. for(j = 0; j < LENGTH(tags) && !m->seltags[j]; j++);
  1604. if(j == LENGTH(tags))
  1605. m->seltags[i] = True; /* at least one tag must be viewed */
  1606. arrange();
  1607. }
  1608. void
  1609. unban(Client *c) {
  1610. if(!c->isbanned)
  1611. return;
  1612. XMoveWindow(dpy, c->win, c->x, c->y);
  1613. c->isbanned = False;
  1614. }
  1615. void
  1616. unmanage(Client *c) {
  1617. XWindowChanges wc;
  1618. wc.border_width = c->oldborder;
  1619. /* The server grab construct avoids race conditions. */
  1620. XGrabServer(dpy);
  1621. XSetErrorHandler(xerrordummy);
  1622. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1623. detach(c);
  1624. detachstack(c);
  1625. if(sel == c)
  1626. focus(NULL);
  1627. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1628. setclientstate(c, WithdrawnState);
  1629. free(c->tags);
  1630. free(c);
  1631. XSync(dpy, False);
  1632. XSetErrorHandler(xerror);
  1633. XUngrabServer(dpy);
  1634. arrange();
  1635. }
  1636. void
  1637. unmapnotify(XEvent *e) {
  1638. Client *c;
  1639. XUnmapEvent *ev = &e->xunmap;
  1640. if((c = getclient(ev->window)))
  1641. unmanage(c);
  1642. }
  1643. void
  1644. updatebarpos(Monitor *s) {
  1645. XEvent ev;
  1646. s->wax = s->sx;
  1647. s->way = s->sy;
  1648. s->wah = s->sh;
  1649. s->waw = s->sw;
  1650. switch(bpos) {
  1651. default:
  1652. s->wah -= bh;
  1653. s->way += bh;
  1654. XMoveWindow(dpy, s->barwin, s->sx, s->sy);
  1655. break;
  1656. case BarBot:
  1657. s->wah -= bh;
  1658. XMoveWindow(dpy, s->barwin, s->sx, s->sy + s->wah);
  1659. break;
  1660. case BarOff:
  1661. XMoveWindow(dpy, s->barwin, s->sx, s->sy - bh);
  1662. break;
  1663. }
  1664. XSync(dpy, False);
  1665. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1666. }
  1667. void
  1668. updatesizehints(Client *c) {
  1669. long msize;
  1670. XSizeHints size;
  1671. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  1672. size.flags = PSize;
  1673. c->flags = size.flags;
  1674. if(c->flags & PBaseSize) {
  1675. c->basew = size.base_width;
  1676. c->baseh = size.base_height;
  1677. }
  1678. else if(c->flags & PMinSize) {
  1679. c->basew = size.min_width;
  1680. c->baseh = size.min_height;
  1681. }
  1682. else
  1683. c->basew = c->baseh = 0;
  1684. if(c->flags & PResizeInc) {
  1685. c->incw = size.width_inc;
  1686. c->inch = size.height_inc;
  1687. }
  1688. else
  1689. c->incw = c->inch = 0;
  1690. if(c->flags & PMaxSize) {
  1691. c->maxw = size.max_width;
  1692. c->maxh = size.max_height;
  1693. }
  1694. else
  1695. c->maxw = c->maxh = 0;
  1696. if(c->flags & PMinSize) {
  1697. c->minw = size.min_width;
  1698. c->minh = size.min_height;
  1699. }
  1700. else if(c->flags & PBaseSize) {
  1701. c->minw = size.base_width;
  1702. c->minh = size.base_height;
  1703. }
  1704. else
  1705. c->minw = c->minh = 0;
  1706. if(c->flags & PAspect) {
  1707. c->minax = size.min_aspect.x;
  1708. c->maxax = size.max_aspect.x;
  1709. c->minay = size.min_aspect.y;
  1710. c->maxay = size.max_aspect.y;
  1711. }
  1712. else
  1713. c->minax = c->maxax = c->minay = c->maxay = 0;
  1714. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1715. && c->maxw == c->minw && c->maxh == c->minh);
  1716. }
  1717. void
  1718. updatetitle(Client *c) {
  1719. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1720. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1721. }
  1722. /* There's no way to check accesses to destroyed windows, thus those cases are
  1723. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1724. * default error handler, which may call exit. */
  1725. int
  1726. xerror(Display *dpy, XErrorEvent *ee) {
  1727. if(ee->error_code == BadWindow
  1728. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1729. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1730. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1731. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1732. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1733. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1734. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1735. return 0;
  1736. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1737. ee->request_code, ee->error_code);
  1738. return xerrorxlib(dpy, ee); /* may call exit */
  1739. }
  1740. int
  1741. xerrordummy(Display *dsply, XErrorEvent *ee) {
  1742. return 0;
  1743. }
  1744. /* Startup Error handler to check if another window manager
  1745. * is already running. */
  1746. int
  1747. xerrorstart(Display *dsply, XErrorEvent *ee) {
  1748. otherwm = True;
  1749. return -1;
  1750. }
  1751. void
  1752. view(const char *arg) {
  1753. unsigned int i;
  1754. Monitor *m = &monitors[monitorat(-1, -1)];
  1755. memcpy(m->prevtags, m->seltags, sizeof initags);
  1756. for(i = 0; i < LENGTH(tags); i++)
  1757. m->seltags[i] = (NULL == arg);
  1758. m->seltags[idxoftag(arg)] = True;
  1759. arrange();
  1760. }
  1761. void
  1762. viewprevtag(const char *arg) {
  1763. static Bool tmp[LENGTH(tags)];
  1764. Monitor *m = &monitors[monitorat(-1, -1)];
  1765. memcpy(tmp, m->seltags, sizeof initags);
  1766. memcpy(m->seltags, m->prevtags, sizeof initags);
  1767. memcpy(m->prevtags, tmp, sizeof initags);
  1768. arrange();
  1769. }
  1770. void
  1771. zoom(const char *arg) {
  1772. Client *c;
  1773. if(!sel || !dozoom || sel->isfloating)
  1774. return;
  1775. if((c = sel) == nexttiled(clients, &monitors[c->monitor]))
  1776. if(!(c = nexttiled(c->next, &monitors[c->monitor])))
  1777. return;
  1778. detach(c);
  1779. attach(c);
  1780. focus(c);
  1781. arrange();
  1782. }
  1783. int
  1784. monitorat(int x, int y) {
  1785. int i;
  1786. if(!XineramaIsActive(dpy))
  1787. return 0;
  1788. if (x < 0 || y < 0) {
  1789. Window win;
  1790. unsigned int mask;
  1791. XQueryPointer(dpy, root, &win, &win, &x, &y, &i, &i, &mask);
  1792. }
  1793. for(i = 0; i < mcount; i++)
  1794. if((x < 0 || (x >= monitors[i].sx && x < monitors[i].sx + monitors[i].sw))
  1795. && (y < 0 || (y >= monitors[i].sy && y < monitors[i].sy + monitors[i].sh)))
  1796. {
  1797. return i;
  1798. }
  1799. return 0;
  1800. }
  1801. void
  1802. movetomonitor(const char *arg) {
  1803. if (sel) {
  1804. sel->monitor = arg ? atoi(arg) : (sel->monitor+1) % mcount;
  1805. memcpy(sel->tags, monitors[sel->monitor].seltags, sizeof initags);
  1806. resize(sel, monitors[sel->monitor].wax, monitors[sel->monitor].way, sel->w, sel->h, True);
  1807. arrange();
  1808. }
  1809. }
  1810. void
  1811. selectmonitor(const char *arg) {
  1812. Monitor *m = &monitors[arg ? atoi(arg) : (monitorat(-1, -1)+1) % mcount];
  1813. XWarpPointer(dpy, None, root, 0, 0, 0, 0, m->wax+m->waw/2, m->way+m->wah/2);
  1814. focus(NULL);
  1815. }
  1816. int
  1817. main(int argc, char *argv[]) {
  1818. if(argc == 2 && !strcmp("-v", argv[1]))
  1819. eprint("dwm-"VERSION", © 2006-2007 Anselm R. Garbe, Sander van Dijk, "
  1820. "Jukka Salmi, Premysl Hruby, Szabolcs Nagy\n");
  1821. else if(argc != 1)
  1822. eprint("usage: dwm [-v]\n");
  1823. setlocale(LC_CTYPE, "");
  1824. if(!(dpy = XOpenDisplay(0)))
  1825. eprint("dwm: cannot open display\n");
  1826. screen = DefaultScreen(dpy);
  1827. root = RootWindow(dpy, screen);
  1828. checkotherwm();
  1829. setup();
  1830. drawbar();
  1831. scan();
  1832. run();
  1833. cleanup();
  1834. XCloseDisplay(dpy);
  1835. return 0;
  1836. }