Configuration file for DWM on MacBook Air
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2057 lines
49 KiB

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