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.

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