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.

2058 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. if((m->layout->arrange == floating) || c->isfloating)
  346. restack();
  347. else
  348. togglefloating(NULL);
  349. movemouse(c);
  350. }
  351. else if(ev->button == Button2) {
  352. if((floating != m->layout->arrange) && c->isfloating)
  353. togglefloating(NULL);
  354. else
  355. zoom(NULL);
  356. }
  357. else if(ev->button == Button3 && !c->isfixed) {
  358. if((floating == m->layout->arrange) || c->isfloating)
  359. restack();
  360. else
  361. togglefloating(NULL);
  362. resizemouse(c);
  363. }
  364. }
  365. }
  366. void
  367. checkotherwm(void) {
  368. otherwm = False;
  369. XSetErrorHandler(xerrorstart);
  370. /* this causes an error if some other window manager is running */
  371. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  372. XSync(dpy, False);
  373. if(otherwm)
  374. eprint("dwm: another window manager is already running\n");
  375. XSync(dpy, False);
  376. XSetErrorHandler(NULL);
  377. xerrorxlib = XSetErrorHandler(xerror);
  378. XSync(dpy, False);
  379. }
  380. void
  381. cleanup(void) {
  382. unsigned int i;
  383. close(STDIN_FILENO);
  384. while(stack) {
  385. unban(stack);
  386. unmanage(stack);
  387. }
  388. for(i = 0; i < mcount; i++) {
  389. Monitor *m = &monitors[i];
  390. if(m->dc.font.set)
  391. XFreeFontSet(dpy, m->dc.font.set);
  392. else
  393. XFreeFont(dpy, m->dc.font.xfont);
  394. XUngrabKey(dpy, AnyKey, AnyModifier, m->root);
  395. XFreePixmap(dpy, m->dc.drawable);
  396. XFreeGC(dpy, m->dc.gc);
  397. XDestroyWindow(dpy, m->barwin);
  398. XFreeCursor(dpy, cursor[CurNormal]);
  399. XFreeCursor(dpy, cursor[CurResize]);
  400. XFreeCursor(dpy, cursor[CurMove]);
  401. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  402. XSync(dpy, False);
  403. }
  404. }
  405. void
  406. compileregs(void) {
  407. unsigned int i;
  408. regex_t *reg;
  409. if(regs)
  410. return;
  411. regs = emallocz(LENGTH(rules) * sizeof(Regs));
  412. for(i = 0; i < LENGTH(rules); i++) {
  413. if(rules[i].prop) {
  414. reg = emallocz(sizeof(regex_t));
  415. if(regcomp(reg, rules[i].prop, REG_EXTENDED))
  416. free(reg);
  417. else
  418. regs[i].propregex = reg;
  419. }
  420. if(rules[i].tags) {
  421. reg = emallocz(sizeof(regex_t));
  422. if(regcomp(reg, rules[i].tags, REG_EXTENDED))
  423. free(reg);
  424. else
  425. regs[i].tagregex = reg;
  426. }
  427. }
  428. }
  429. void
  430. configure(Client *c) {
  431. XConfigureEvent ce;
  432. ce.type = ConfigureNotify;
  433. ce.display = dpy;
  434. ce.event = c->win;
  435. ce.window = c->win;
  436. ce.x = c->x;
  437. ce.y = c->y;
  438. ce.width = c->w;
  439. ce.height = c->h;
  440. ce.border_width = c->border;
  441. ce.above = None;
  442. ce.override_redirect = False;
  443. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  444. }
  445. void
  446. configurenotify(XEvent *e) {
  447. XConfigureEvent *ev = &e->xconfigure;
  448. Monitor *m = &monitors[selmonitor];
  449. if(ev->window == m->root && (ev->width != m->sw || ev->height != m->sh)) {
  450. m->sw = ev->width;
  451. m->sh = ev->height;
  452. XFreePixmap(dpy, dc.drawable);
  453. dc.drawable = XCreatePixmap(dpy, m->root, m->sw, bh, DefaultDepth(dpy, m->screen));
  454. XResizeWindow(dpy, m->barwin, m->sw, bh);
  455. updatebarpos(m);
  456. arrange();
  457. }
  458. }
  459. void
  460. configurerequest(XEvent *e) {
  461. Client *c;
  462. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  463. XWindowChanges wc;
  464. if((c = getclient(ev->window))) {
  465. Monitor *m = &monitors[c->monitor];
  466. if(ev->value_mask & CWBorderWidth)
  467. c->border = ev->border_width;
  468. if(c->isfixed || c->isfloating || (floating == m->layout->arrange)) {
  469. if(ev->value_mask & CWX)
  470. c->x = m->sx+ev->x;
  471. if(ev->value_mask & CWY)
  472. c->y = m->sy+ev->y;
  473. if(ev->value_mask & CWWidth)
  474. c->w = ev->width;
  475. if(ev->value_mask & CWHeight)
  476. c->h = ev->height;
  477. if((c->x - m->sx + c->w) > m->sw && c->isfloating)
  478. c->x = m->sx + (m->sw / 2 - c->w / 2); /* center in x direction */
  479. if((c->y - m->sy + c->h) > m->sh && c->isfloating)
  480. c->y = m->sy + (m->sh / 2 - c->h / 2); /* center in y direction */
  481. if((ev->value_mask & (CWX | CWY))
  482. && !(ev->value_mask & (CWWidth | CWHeight)))
  483. configure(c);
  484. if(isvisible(c, monitorat()))
  485. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  486. }
  487. else
  488. configure(c);
  489. }
  490. else {
  491. wc.x = ev->x;
  492. wc.y = ev->y;
  493. wc.width = ev->width;
  494. wc.height = ev->height;
  495. wc.border_width = ev->border_width;
  496. wc.sibling = ev->above;
  497. wc.stack_mode = ev->detail;
  498. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  499. }
  500. XSync(dpy, False);
  501. }
  502. void
  503. destroynotify(XEvent *e) {
  504. Client *c;
  505. XDestroyWindowEvent *ev = &e->xdestroywindow;
  506. if((c = getclient(ev->window)))
  507. unmanage(c);
  508. }
  509. void
  510. detach(Client *c) {
  511. if(c->prev)
  512. c->prev->next = c->next;
  513. if(c->next)
  514. c->next->prev = c->prev;
  515. if(c == clients)
  516. clients = c->next;
  517. c->next = c->prev = NULL;
  518. }
  519. void
  520. detachstack(Client *c) {
  521. Client **tc;
  522. for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
  523. *tc = c->snext;
  524. }
  525. void
  526. drawbar(void) {
  527. int i, j, x;
  528. for(i = 0; i < mcount; i++) {
  529. Monitor *m = &monitors[i];
  530. m->dc.x = 0;
  531. for(j = 0; j < LENGTH(tags); j++) {
  532. m->dc.w = textw(m, tags[j]);
  533. if(m->seltags[j]) {
  534. drawtext(m, tags[j], m->dc.sel, isurgent(i, j));
  535. drawsquare(m, sel && sel->tags[j] && sel->monitor == selmonitor, isoccupied(m, j), m->dc.sel);
  536. }
  537. else {
  538. drawtext(m, tags[j], m->dc.norm, isurgent(i, j));
  539. drawsquare(m, sel && sel->tags[j] && sel->monitor == selmonitor, isoccupied(m, j), m->dc.norm);
  540. }
  541. m->dc.x += m->dc.w;
  542. }
  543. m->dc.w = blw;
  544. drawtext(m, m->layout->symbol, m->dc.norm, False);
  545. x = m->dc.x + m->dc.w;
  546. m->dc.w = textw(m, stext);
  547. m->dc.x = m->sw - m->dc.w;
  548. if(m->dc.x < x) {
  549. m->dc.x = x;
  550. m->dc.w = m->sw - x;
  551. }
  552. drawtext(m, stext, m->dc.norm, False);
  553. if((m->dc.w = m->dc.x - x) > bh) {
  554. m->dc.x = x;
  555. if(sel && sel->monitor == selmonitor) {
  556. drawtext(m, sel->name, m->dc.sel, False);
  557. drawsquare(m, False, sel->isfloating, m->dc.sel);
  558. }
  559. else
  560. drawtext(m, NULL, m->dc.norm, False);
  561. }
  562. XCopyArea(dpy, m->dc.drawable, m->barwin, m->dc.gc, 0, 0, m->sw, bh, 0, 0);
  563. XSync(dpy, False);
  564. }
  565. }
  566. void
  567. drawsquare(Monitor *m, Bool filled, Bool empty, unsigned long col[ColLast]) {
  568. int x;
  569. XGCValues gcv;
  570. XRectangle r = { m->dc.x, m->dc.y, m->dc.w, m->dc.h };
  571. gcv.foreground = col[ColFG];
  572. XChangeGC(dpy, m->dc.gc, GCForeground, &gcv);
  573. x = (m->dc.font.ascent + m->dc.font.descent + 2) / 4;
  574. r.x = m->dc.x + 1;
  575. r.y = m->dc.y + 1;
  576. if(filled) {
  577. r.width = r.height = x + 1;
  578. XFillRectangles(dpy, m->dc.drawable, m->dc.gc, &r, 1);
  579. }
  580. else if(empty) {
  581. r.width = r.height = x;
  582. XDrawRectangles(dpy, m->dc.drawable, m->dc.gc, &r, 1);
  583. }
  584. }
  585. void
  586. drawtext(Monitor *m, const char *text, unsigned long col[ColLast], Bool invert) {
  587. int x, y, w, h;
  588. static char buf[256];
  589. unsigned int len, olen;
  590. XRectangle r = { m->dc.x, m->dc.y, m->dc.w, m->dc.h };
  591. XSetForeground(dpy, m->dc.gc, col[invert ? ColFG : ColBG]);
  592. XFillRectangles(dpy, m->dc.drawable, m->dc.gc, &r, 1);
  593. if(!text)
  594. return;
  595. w = 0;
  596. olen = len = strlen(text);
  597. if(len >= sizeof buf)
  598. len = sizeof buf - 1;
  599. memcpy(buf, text, len);
  600. buf[len] = 0;
  601. h = m->dc.font.ascent + m->dc.font.descent;
  602. y = m->dc.y + (m->dc.h / 2) - (h / 2) + m->dc.font.ascent;
  603. x = m->dc.x + (h / 2);
  604. /* shorten text if necessary */
  605. while(len && (w = textnw(m, buf, len)) > m->dc.w - h)
  606. buf[--len] = 0;
  607. if(len < olen) {
  608. if(len > 1)
  609. buf[len - 1] = '.';
  610. if(len > 2)
  611. buf[len - 2] = '.';
  612. if(len > 3)
  613. buf[len - 3] = '.';
  614. }
  615. if(w > m->dc.w)
  616. return; /* too long */
  617. XSetForeground(dpy, m->dc.gc, col[invert ? ColBG : ColFG]);
  618. if(m->dc.font.set)
  619. XmbDrawString(dpy, m->dc.drawable, m->dc.font.set, m->dc.gc, x, y, buf, len);
  620. else
  621. XDrawString(dpy, m->dc.drawable, m->dc.gc, x, y, buf, len);
  622. }
  623. void *
  624. emallocz(unsigned int size) {
  625. void *res = calloc(1, size);
  626. if(!res)
  627. eprint("fatal: could not malloc() %u bytes\n", size);
  628. return res;
  629. }
  630. void
  631. enternotify(XEvent *e) {
  632. Client *c;
  633. XCrossingEvent *ev = &e->xcrossing;
  634. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior);
  635. //return;
  636. if((c = getclient(ev->window)))
  637. focus(c);
  638. else {
  639. selmonitor = monitorat();
  640. fprintf(stderr, "updating selmonitor %d\n", selmonitor);
  641. focus(NULL);
  642. }
  643. }
  644. void
  645. eprint(const char *errstr, ...) {
  646. va_list ap;
  647. va_start(ap, errstr);
  648. vfprintf(stderr, errstr, ap);
  649. va_end(ap);
  650. exit(EXIT_FAILURE);
  651. }
  652. void
  653. expose(XEvent *e) {
  654. XExposeEvent *ev = &e->xexpose;
  655. if(ev->count == 0) {
  656. if(ev->window == monitors[selmonitor].barwin)
  657. drawbar();
  658. }
  659. }
  660. void
  661. floating(void) { /* default floating layout */
  662. Client *c;
  663. domwfact = dozoom = False;
  664. for(c = clients; c; c = c->next)
  665. if(isvisible(c, selmonitor))
  666. resize(c, c->x, c->y, c->w, c->h, True);
  667. }
  668. void
  669. focus(Client *c) {
  670. Monitor *m;
  671. if(c)
  672. selmonitor = c->monitor;
  673. m = &monitors[selmonitor];
  674. if(!c || (c && !isvisible(c, selmonitor)))
  675. for(c = stack; c && !isvisible(c, c->monitor); c = c->snext);
  676. if(sel && sel != c) {
  677. grabbuttons(sel, False);
  678. XSetWindowBorder(dpy, sel->win, monitors[sel->monitor].dc.norm[ColBorder]);
  679. }
  680. if(c) {
  681. detachstack(c);
  682. attachstack(c);
  683. grabbuttons(c, True);
  684. }
  685. sel = c;
  686. drawbar();
  687. if(c) {
  688. XSetWindowBorder(dpy, c->win, m->dc.sel[ColBorder]);
  689. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  690. selmonitor = c->monitor;
  691. }
  692. else {
  693. XSetInputFocus(dpy, m->root, RevertToPointerRoot, CurrentTime);
  694. }
  695. }
  696. void
  697. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  698. XFocusChangeEvent *ev = &e->xfocus;
  699. if(sel && ev->window != sel->win)
  700. XSetInputFocus(dpy, sel->win, RevertToPointerRoot, CurrentTime);
  701. }
  702. void
  703. focusnext(const char *arg) {
  704. Client *c;
  705. if(!sel)
  706. return;
  707. for(c = sel->next; c && !isvisible(c, selmonitor); c = c->next);
  708. if(!c)
  709. for(c = clients; c && !isvisible(c, selmonitor); c = c->next);
  710. if(c) {
  711. focus(c);
  712. restack();
  713. }
  714. }
  715. void
  716. focusprev(const char *arg) {
  717. Client *c;
  718. if(!sel)
  719. return;
  720. for(c = sel->prev; c && !isvisible(c, selmonitor); c = c->prev);
  721. if(!c) {
  722. for(c = clients; c && c->next; c = c->next);
  723. for(; c && !isvisible(c, selmonitor); c = c->prev);
  724. }
  725. if(c) {
  726. focus(c);
  727. restack();
  728. }
  729. }
  730. Client *
  731. getclient(Window w) {
  732. Client *c;
  733. for(c = clients; c && c->win != w; c = c->next);
  734. return c;
  735. }
  736. unsigned long
  737. getcolor(const char *colstr, int screen) {
  738. Colormap cmap = DefaultColormap(dpy, screen);
  739. XColor color;
  740. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  741. eprint("error, cannot allocate color '%s'\n", colstr);
  742. return color.pixel;
  743. }
  744. long
  745. getstate(Window w) {
  746. int format, status;
  747. long result = -1;
  748. unsigned char *p = NULL;
  749. unsigned long n, extra;
  750. Atom real;
  751. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  752. &real, &format, &n, &extra, (unsigned char **)&p);
  753. if(status != Success)
  754. return -1;
  755. if(n != 0)
  756. result = *p;
  757. XFree(p);
  758. return result;
  759. }
  760. Bool
  761. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  762. char **list = NULL;
  763. int n;
  764. XTextProperty name;
  765. if(!text || size == 0)
  766. return False;
  767. text[0] = '\0';
  768. XGetTextProperty(dpy, w, &name, atom);
  769. if(!name.nitems)
  770. return False;
  771. if(name.encoding == XA_STRING)
  772. strncpy(text, (char *)name.value, size - 1);
  773. else {
  774. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  775. && n > 0 && *list) {
  776. strncpy(text, *list, size - 1);
  777. XFreeStringList(list);
  778. }
  779. }
  780. text[size - 1] = '\0';
  781. XFree(name.value);
  782. return True;
  783. }
  784. void
  785. grabbuttons(Client *c, Bool focused) {
  786. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  787. if(focused) {
  788. XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  789. GrabModeAsync, GrabModeSync, None, None);
  790. XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
  791. GrabModeAsync, GrabModeSync, None, None);
  792. XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  793. GrabModeAsync, GrabModeSync, None, None);
  794. XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  795. GrabModeAsync, GrabModeSync, None, None);
  796. XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  797. GrabModeAsync, GrabModeSync, None, None);
  798. XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
  799. GrabModeAsync, GrabModeSync, None, None);
  800. XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  801. GrabModeAsync, GrabModeSync, None, None);
  802. XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  803. GrabModeAsync, GrabModeSync, None, None);
  804. XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  805. GrabModeAsync, GrabModeSync, None, None);
  806. XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
  807. GrabModeAsync, GrabModeSync, None, None);
  808. XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  809. GrabModeAsync, GrabModeSync, None, None);
  810. XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  811. GrabModeAsync, GrabModeSync, None, None);
  812. }
  813. else
  814. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
  815. GrabModeAsync, GrabModeSync, None, None);
  816. }
  817. void
  818. grabkeys(void) {
  819. unsigned int i, j;
  820. KeyCode code;
  821. XModifierKeymap *modmap;
  822. /* init modifier map */
  823. modmap = XGetModifierMapping(dpy);
  824. for(i = 0; i < 8; i++)
  825. for(j = 0; j < modmap->max_keypermod; j++) {
  826. if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
  827. numlockmask = (1 << i);
  828. }
  829. XFreeModifiermap(modmap);
  830. for(i = 0; i < mcount; i++) {
  831. Monitor *m = &monitors[i];
  832. XUngrabKey(dpy, AnyKey, AnyModifier, m->root);
  833. for(j = 0; j < LENGTH(keys); j++) {
  834. code = XKeysymToKeycode(dpy, keys[j].keysym);
  835. XGrabKey(dpy, code, keys[j].mod, m->root, True,
  836. GrabModeAsync, GrabModeAsync);
  837. XGrabKey(dpy, code, keys[j].mod | LockMask, m->root, True,
  838. GrabModeAsync, GrabModeAsync);
  839. XGrabKey(dpy, code, keys[j].mod | numlockmask, m->root, True,
  840. GrabModeAsync, GrabModeAsync);
  841. XGrabKey(dpy, code, keys[j].mod | numlockmask | LockMask, m->root, True,
  842. GrabModeAsync, GrabModeAsync);
  843. }
  844. }
  845. }
  846. unsigned int
  847. idxoftag(const char *tag) {
  848. unsigned int i;
  849. for(i = 0; (i < LENGTH(tags)) && (tags[i] != tag); i++);
  850. return (i < LENGTH(tags)) ? i : 0;
  851. }
  852. void
  853. initfont(Monitor *m, const char *fontstr) {
  854. char *def, **missing;
  855. int i, n;
  856. missing = NULL;
  857. if(m->dc.font.set)
  858. XFreeFontSet(dpy, m->dc.font.set);
  859. m->dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  860. if(missing) {
  861. while(n--)
  862. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  863. XFreeStringList(missing);
  864. }
  865. if(m->dc.font.set) {
  866. XFontSetExtents *font_extents;
  867. XFontStruct **xfonts;
  868. char **font_names;
  869. m->dc.font.ascent = m->dc.font.descent = 0;
  870. font_extents = XExtentsOfFontSet(m->dc.font.set);
  871. n = XFontsOfFontSet(m->dc.font.set, &xfonts, &font_names);
  872. for(i = 0, m->dc.font.ascent = 0, m->dc.font.descent = 0; i < n; i++) {
  873. if(m->dc.font.ascent < (*xfonts)->ascent)
  874. m->dc.font.ascent = (*xfonts)->ascent;
  875. if(m->dc.font.descent < (*xfonts)->descent)
  876. m->dc.font.descent = (*xfonts)->descent;
  877. xfonts++;
  878. }
  879. }
  880. else {
  881. if(m->dc.font.xfont)
  882. XFreeFont(dpy, m->dc.font.xfont);
  883. m->dc.font.xfont = NULL;
  884. if(!(m->dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  885. && !(m->dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  886. eprint("error, cannot load font: '%s'\n", fontstr);
  887. m->dc.font.ascent = m->dc.font.xfont->ascent;
  888. m->dc.font.descent = m->dc.font.xfont->descent;
  889. }
  890. m->dc.font.height = m->dc.font.ascent + m->dc.font.descent;
  891. }
  892. Bool
  893. isoccupied(Monitor *m, unsigned int t) {
  894. Client *c;
  895. for(c = clients; c; c = c->next)
  896. if(c->tags[t] && c->monitor == selmonitor)
  897. return True;
  898. return False;
  899. }
  900. Bool
  901. isprotodel(Client *c) {
  902. int i, n;
  903. Atom *protocols;
  904. Bool ret = False;
  905. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  906. for(i = 0; !ret && i < n; i++)
  907. if(protocols[i] == wmatom[WMDelete])
  908. ret = True;
  909. XFree(protocols);
  910. }
  911. return ret;
  912. }
  913. Bool
  914. isurgent(int monitor, unsigned int t) {
  915. Client *c;
  916. for(c = clients; c; c = c->next)
  917. if(c->monitor == monitor && c->isurgent && c->tags[t])
  918. return True;
  919. return False;
  920. }
  921. Bool
  922. isvisible(Client *c, int monitor) {
  923. unsigned int i;
  924. for(i = 0; i < LENGTH(tags); i++)
  925. if(c->tags[i] && monitors[c->monitor].seltags[i] && c->monitor == monitor)
  926. return True;
  927. return False;
  928. }
  929. void
  930. keypress(XEvent *e) {
  931. unsigned int i;
  932. KeySym keysym;
  933. XKeyEvent *ev;
  934. ev = &e->xkey;
  935. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  936. for(i = 0; i < LENGTH(keys); i++)
  937. if(keysym == keys[i].keysym
  938. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state))
  939. {
  940. if(keys[i].func)
  941. keys[i].func(keys[i].arg);
  942. }
  943. }
  944. void
  945. killclient(const char *arg) {
  946. XEvent ev;
  947. if(!sel)
  948. return;
  949. if(isprotodel(sel)) {
  950. ev.type = ClientMessage;
  951. ev.xclient.window = sel->win;
  952. ev.xclient.message_type = wmatom[WMProtocols];
  953. ev.xclient.format = 32;
  954. ev.xclient.data.l[0] = wmatom[WMDelete];
  955. ev.xclient.data.l[1] = CurrentTime;
  956. XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
  957. }
  958. else
  959. XKillClient(dpy, sel->win);
  960. }
  961. void
  962. manage(Window w, XWindowAttributes *wa) {
  963. Client *c, *t = NULL;
  964. Monitor *m;
  965. Status rettrans;
  966. Window trans;
  967. XWindowChanges wc;
  968. c = emallocz(sizeof(Client));
  969. c->tags = emallocz(sizeof initags);
  970. c->win = w;
  971. applyrules(c);
  972. m = &monitors[c->monitor];
  973. c->x = wa->x + m->sx;
  974. c->y = wa->y + m->sy;
  975. c->w = wa->width;
  976. c->h = wa->height;
  977. c->oldborder = wa->border_width;
  978. if(c->w == m->sw && c->h == m->sh) {
  979. c->x = m->sx;
  980. c->y = m->sy;
  981. c->border = wa->border_width;
  982. }
  983. else {
  984. if(c->x + c->w + 2 * c->border > m->wax + m->waw)
  985. c->x = m->wax + m->waw - c->w - 2 * c->border;
  986. if(c->y + c->h + 2 * c->border > m->way + m->wah)
  987. c->y = m->way + m->wah - c->h - 2 * c->border;
  988. if(c->x < m->wax)
  989. c->x = m->wax;
  990. if(c->y < m->way)
  991. c->y = m->way;
  992. c->border = BORDERPX;
  993. }
  994. wc.border_width = c->border;
  995. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  996. XSetWindowBorder(dpy, w, m->dc.norm[ColBorder]);
  997. configure(c); /* propagates border_width, if size doesn't change */
  998. updatesizehints(c);
  999. XSelectInput(dpy, w, EnterWindowMask | FocusChangeMask | PropertyChangeMask | StructureNotifyMask);
  1000. grabbuttons(c, False);
  1001. updatetitle(c);
  1002. if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
  1003. for(t = clients; t && t->win != trans; t = t->next);
  1004. if(t)
  1005. memcpy(c->tags, t->tags, sizeof initags);
  1006. if(!c->isfloating)
  1007. c->isfloating = (rettrans == Success) || c->isfixed;
  1008. attach(c);
  1009. attachstack(c);
  1010. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
  1011. ban(c);
  1012. XMapWindow(dpy, c->win);
  1013. setclientstate(c, NormalState);
  1014. arrange();
  1015. }
  1016. void
  1017. mappingnotify(XEvent *e) {
  1018. XMappingEvent *ev = &e->xmapping;
  1019. XRefreshKeyboardMapping(ev);
  1020. if(ev->request == MappingKeyboard)
  1021. grabkeys();
  1022. }
  1023. void
  1024. maprequest(XEvent *e) {
  1025. static XWindowAttributes wa;
  1026. XMapRequestEvent *ev = &e->xmaprequest;
  1027. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1028. return;
  1029. if(wa.override_redirect)
  1030. return;
  1031. if(!getclient(ev->window))
  1032. manage(ev->window, &wa);
  1033. }
  1034. int
  1035. monitorat() {
  1036. int i, x, y;
  1037. Window win;
  1038. unsigned int mask;
  1039. XQueryPointer(dpy, monitors[selmonitor].root, &win, &win, &x, &y, &i, &i, &mask);
  1040. for(i = 0; i < mcount; i++) {
  1041. fprintf(stderr, "checking monitor[%d]: %d %d %d %d\n", i, monitors[i].sx, monitors[i].sy, monitors[i].sw, monitors[i].sh);
  1042. if((x >= monitors[i].sx && x < monitors[i].sx + monitors[i].sw)
  1043. && (y >= monitors[i].sy && y < monitors[i].sy + monitors[i].sh)) {
  1044. fprintf(stderr, "%d,%d -> %d\n", x, y, i);
  1045. return i;
  1046. }
  1047. }
  1048. fprintf(stderr, "?,? -> 0\n");
  1049. return 0;
  1050. }
  1051. void
  1052. movemouse(Client *c) {
  1053. int x1, y1, ocx, ocy, di, nx, ny;
  1054. unsigned int dui;
  1055. Window dummy;
  1056. XEvent ev;
  1057. ocx = nx = c->x;
  1058. ocy = ny = c->y;
  1059. if(XGrabPointer(dpy, monitors[selmonitor].root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1060. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1061. return;
  1062. XQueryPointer(dpy, monitors[selmonitor].root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  1063. for(;;) {
  1064. XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
  1065. switch (ev.type) {
  1066. case ButtonRelease:
  1067. XUngrabPointer(dpy, CurrentTime);
  1068. return;
  1069. case ConfigureRequest:
  1070. case Expose:
  1071. case MapRequest:
  1072. handler[ev.type](&ev);
  1073. break;
  1074. case MotionNotify:
  1075. XSync(dpy, False);
  1076. nx = ocx + (ev.xmotion.x - x1);
  1077. ny = ocy + (ev.xmotion.y - y1);
  1078. Monitor *m = &monitors[monitorat()];
  1079. if(abs(m->wax - nx) < SNAP)
  1080. nx = m->wax;
  1081. else if(abs((m->wax + m->waw) - (nx + c->w + 2 * c->border)) < SNAP)
  1082. nx = m->wax + m->waw - c->w - 2 * c->border;
  1083. if(abs(m->way - ny) < SNAP)
  1084. ny = m->way;
  1085. else if(abs((m->way + m->wah) - (ny + c->h + 2 * c->border)) < SNAP)
  1086. ny = m->way + m->wah - c->h - 2 * c->border;
  1087. resize(c, nx, ny, c->w, c->h, False);
  1088. memcpy(c->tags, monitors[monitorat()].seltags, sizeof initags);
  1089. break;
  1090. }
  1091. }
  1092. }
  1093. Client *
  1094. nexttiled(Client *c, int monitor) {
  1095. for(; c && (c->isfloating || !isvisible(c, monitor)); c = c->next);
  1096. return c;
  1097. }
  1098. void
  1099. propertynotify(XEvent *e) {
  1100. Client *c;
  1101. Window trans;
  1102. XPropertyEvent *ev = &e->xproperty;
  1103. if(ev->state == PropertyDelete)
  1104. return; /* ignore */
  1105. if((c = getclient(ev->window))) {
  1106. switch (ev->atom) {
  1107. default: break;
  1108. case XA_WM_TRANSIENT_FOR:
  1109. XGetTransientForHint(dpy, c->win, &trans);
  1110. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  1111. arrange();
  1112. break;
  1113. case XA_WM_NORMAL_HINTS:
  1114. updatesizehints(c);
  1115. break;
  1116. case XA_WM_HINTS:
  1117. updatewmhints(c);
  1118. drawbar();
  1119. break;
  1120. }
  1121. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1122. updatetitle(c);
  1123. if(c == sel)
  1124. drawbar();
  1125. }
  1126. }
  1127. }
  1128. void
  1129. quit(const char *arg) {
  1130. readin = running = False;
  1131. }
  1132. void
  1133. reapply(const char *arg) {
  1134. static Bool zerotags[LENGTH(tags)] = { 0 };
  1135. Client *c;
  1136. for(c = clients; c; c = c->next) {
  1137. memcpy(c->tags, zerotags, sizeof zerotags);
  1138. applyrules(c);
  1139. }
  1140. arrange();
  1141. }
  1142. void
  1143. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  1144. XWindowChanges wc;
  1145. //Monitor scr = monitors[monitorat()];
  1146. // c->monitor = monitorat();
  1147. if(sizehints) {
  1148. /* set minimum possible */
  1149. if (w < 1)
  1150. w = 1;
  1151. if (h < 1)
  1152. h = 1;
  1153. /* temporarily remove base dimensions */
  1154. w -= c->basew;
  1155. h -= c->baseh;
  1156. /* adjust for aspect limits */
  1157. if (c->minay > 0 && c->maxay > 0 && c->minax > 0 && c->maxax > 0) {
  1158. if (w * c->maxay > h * c->maxax)
  1159. w = h * c->maxax / c->maxay;
  1160. else if (w * c->minay < h * c->minax)
  1161. h = w * c->minay / c->minax;
  1162. }
  1163. /* adjust for increment value */
  1164. if(c->incw)
  1165. w -= w % c->incw;
  1166. if(c->inch)
  1167. h -= h % c->inch;
  1168. /* restore base dimensions */
  1169. w += c->basew;
  1170. h += c->baseh;
  1171. if(c->minw > 0 && w < c->minw)
  1172. w = c->minw;
  1173. if(c->minh > 0 && h < c->minh)
  1174. h = c->minh;
  1175. if(c->maxw > 0 && w > c->maxw)
  1176. w = c->maxw;
  1177. if(c->maxh > 0 && h > c->maxh)
  1178. h = c->maxh;
  1179. }
  1180. if(w <= 0 || h <= 0)
  1181. return;
  1182. /* TODO: offscreen appearance fixes */
  1183. /*
  1184. if(x > scr.sw)
  1185. x = scr.sw - w - 2 * c->border;
  1186. if(y > scr.sh)
  1187. y = scr.sh - h - 2 * c->border;
  1188. if(x + w + 2 * c->border < scr.sx)
  1189. x = scr.sx;
  1190. if(y + h + 2 * c->border < scr.sy)
  1191. y = scr.sy;
  1192. */
  1193. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  1194. c->x = wc.x = x;
  1195. c->y = wc.y = y;
  1196. c->w = wc.width = w;
  1197. c->h = wc.height = h;
  1198. wc.border_width = c->border;
  1199. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  1200. configure(c);
  1201. XSync(dpy, False);
  1202. }
  1203. }
  1204. void
  1205. resizemouse(Client *c) {
  1206. int ocx, ocy;
  1207. int nw, nh;
  1208. XEvent ev;
  1209. ocx = c->x;
  1210. ocy = c->y;
  1211. if(XGrabPointer(dpy, monitors[selmonitor].root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1212. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1213. return;
  1214. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
  1215. for(;;) {
  1216. XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
  1217. switch(ev.type) {
  1218. case ButtonRelease:
  1219. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
  1220. c->w + c->border - 1, c->h + c->border - 1);
  1221. XUngrabPointer(dpy, CurrentTime);
  1222. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1223. return;
  1224. case ConfigureRequest:
  1225. case Expose:
  1226. case MapRequest:
  1227. handler[ev.type](&ev);
  1228. break;
  1229. case MotionNotify:
  1230. XSync(dpy, False);
  1231. if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
  1232. nw = 1;
  1233. if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
  1234. nh = 1;
  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. Monitor *m = &monitors[monitorat()];
  1788. memcpy(m->prevtags, m->seltags, sizeof initags);
  1789. for(i = 0; i < LENGTH(tags); i++)
  1790. m->seltags[i] = (NULL == arg);
  1791. m->seltags[idxoftag(arg)] = True;
  1792. arrange();
  1793. }
  1794. void
  1795. viewprevtag(const char *arg) {
  1796. static Bool tmp[LENGTH(tags)];
  1797. Monitor *m = &monitors[monitorat()];
  1798. memcpy(tmp, m->seltags, sizeof initags);
  1799. memcpy(m->seltags, m->prevtags, sizeof initags);
  1800. memcpy(m->prevtags, tmp, sizeof initags);
  1801. arrange();
  1802. }
  1803. void
  1804. zoom(const char *arg) {
  1805. Client *c;
  1806. if(!sel || !dozoom || sel->isfloating)
  1807. return;
  1808. if((c = sel) == nexttiled(clients, c->monitor))
  1809. if(!(c = nexttiled(c->next, c->monitor)))
  1810. return;
  1811. detach(c);
  1812. attach(c);
  1813. focus(c);
  1814. arrange();
  1815. }
  1816. void
  1817. movetomonitor(const char *arg) {
  1818. if (sel) {
  1819. sel->monitor = arg ? atoi(arg) : (sel->monitor+1) % mcount;
  1820. memcpy(sel->tags, monitors[sel->monitor].seltags, sizeof initags);
  1821. resize(sel, monitors[sel->monitor].wax, monitors[sel->monitor].way, sel->w, sel->h, True);
  1822. arrange();
  1823. }
  1824. }
  1825. void
  1826. selectmonitor(const char *arg) {
  1827. Monitor *m = &monitors[arg ? atoi(arg) : (monitorat()+1) % mcount];
  1828. XWarpPointer(dpy, None, m->root, 0, 0, 0, 0, m->wax+m->waw/2, m->way+m->wah/2);
  1829. focus(NULL);
  1830. }
  1831. int
  1832. main(int argc, char *argv[]) {
  1833. if(argc == 2 && !strcmp("-v", argv[1]))
  1834. eprint("dwm-"VERSION", © 2006-2007 Anselm R. Garbe, Sander van Dijk, "
  1835. "Jukka Salmi, Premysl Hruby, Szabolcs Nagy, Christof Musik\n");
  1836. else if(argc != 1)
  1837. eprint("usage: dwm [-v]\n");
  1838. setlocale(LC_CTYPE, "");
  1839. if(!(dpy = XOpenDisplay(0)))
  1840. eprint("dwm: cannot open display\n");
  1841. checkotherwm();
  1842. setup();
  1843. drawbar();
  1844. scan();
  1845. run();
  1846. cleanup();
  1847. XCloseDisplay(dpy);
  1848. return 0;
  1849. }