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

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