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

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