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.

2068 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 barwin;
  112. int sx, sy, sw, sh, wax, way, wah, waw;
  113. DC dc;
  114. Bool *seltags;
  115. Bool *prevtags;
  116. Layout *layout;
  117. double mwfact;
  118. } Monitor;
  119. /* function declarations */
  120. void applyrules(Client *c);
  121. void arrange(void);
  122. void attach(Client *c);
  123. void attachstack(Client *c);
  124. void ban(Client *c);
  125. void buttonpress(XEvent *e);
  126. void checkotherwm(void);
  127. void cleanup(void);
  128. void compileregs(void);
  129. void configure(Client *c);
  130. void configurenotify(XEvent *e);
  131. void configurerequest(XEvent *e);
  132. void destroynotify(XEvent *e);
  133. void detach(Client *c);
  134. void detachstack(Client *c);
  135. void drawbar(void);
  136. void drawsquare(Monitor *, Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
  137. void drawtext(Monitor *, const char *text, unsigned long col[ColLast], Bool invert);
  138. void *emallocz(unsigned int size);
  139. void enternotify(XEvent *e);
  140. void eprint(const char *errstr, ...);
  141. void expose(XEvent *e);
  142. void floating(void); /* default floating layout */
  143. void focus(Client *c);
  144. void focusin(XEvent *e);
  145. void focusnext(const char *arg);
  146. void focusprev(const char *arg);
  147. Client *getclient(Window w);
  148. unsigned long getcolor(const char *colstr, int screen);
  149. long getstate(Window w);
  150. Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  151. void grabbuttons(Client *c, Bool focused);
  152. void grabkeys(void);
  153. unsigned int idxoftag(const char *tag);
  154. void initfont(Monitor*, const char *fontstr);
  155. Bool isoccupied(unsigned int monitor, unsigned int t);
  156. Bool isprotodel(Client *c);
  157. Bool isurgent(unsigned int monitor, unsigned int t);
  158. Bool isvisible(Client *c, int monitor);
  159. void keypress(XEvent *e);
  160. void killclient(const char *arg);
  161. void manage(Window w, XWindowAttributes *wa);
  162. void mappingnotify(XEvent *e);
  163. void maprequest(XEvent *e);
  164. void movemouse(Client *c);
  165. Client *nexttiled(Client *c, int monitor);
  166. void propertynotify(XEvent *e);
  167. void quit(const char *arg);
  168. void reapply(const char *arg);
  169. void resize(Client *c, int x, int y, int w, int h, Bool sizehints);
  170. void resizemouse(Client *c);
  171. void restack(void);
  172. void run(void);
  173. void scan(void);
  174. void setclientstate(Client *c, long state);
  175. void setlayout(const char *arg);
  176. void setmwfact(const char *arg);
  177. void setup(void);
  178. void spawn(const char *arg);
  179. void tag(const char *arg);
  180. unsigned int textnw(Monitor*, const char *text, unsigned int len);
  181. unsigned int textw(Monitor*, const char *text);
  182. void tile(void);
  183. void togglebar(const char *arg);
  184. void togglefloating(const char *arg);
  185. void toggletag(const char *arg);
  186. void toggleview(const char *arg);
  187. void unban(Client *c);
  188. void unmanage(Client *c);
  189. void unmapnotify(XEvent *e);
  190. void updatebarpos(Monitor *m);
  191. void updatesizehints(Client *c);
  192. void updatetitle(Client *c);
  193. void updatewmhints(Client *c);
  194. void view(const char *arg);
  195. void viewprevtag(const char *arg); /* views previous selected tags */
  196. int xerror(Display *dpy, XErrorEvent *ee);
  197. int xerrordummy(Display *dsply, XErrorEvent *ee);
  198. int xerrorstart(Display *dsply, XErrorEvent *ee);
  199. void zoom(const char *arg);
  200. int monitorat(void);
  201. void movetomonitor(const char *arg);
  202. void selectmonitor(const char *arg);
  203. /* variables */
  204. char stext[256];
  205. int mcount = 1;
  206. int selmonitor = 0;
  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. Window root;
  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, 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 == 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, 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 != 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, 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. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  837. for(i = 0; i < LENGTH(keys); i++) {
  838. code = XKeysymToKeycode(dpy, keys[i].keysym);
  839. XGrabKey(dpy, code, keys[i].mod, root, True,
  840. GrabModeAsync, GrabModeAsync);
  841. XGrabKey(dpy, code, keys[i].mod | LockMask, root, True,
  842. GrabModeAsync, GrabModeAsync);
  843. XGrabKey(dpy, code, keys[i].mod | numlockmask, root, True,
  844. GrabModeAsync, GrabModeAsync);
  845. XGrabKey(dpy, code, keys[i].mod | numlockmask | LockMask, root, True,
  846. GrabModeAsync, GrabModeAsync);
  847. }
  848. }
  849. unsigned int
  850. idxoftag(const char *tag) {
  851. unsigned int i;
  852. for(i = 0; (i < LENGTH(tags)) && (tags[i] != tag); i++);
  853. return (i < LENGTH(tags)) ? i : 0;
  854. }
  855. void
  856. initfont(Monitor *m, const char *fontstr) {
  857. char *def, **missing;
  858. int i, n;
  859. missing = NULL;
  860. if(m->dc.font.set)
  861. XFreeFontSet(dpy, m->dc.font.set);
  862. m->dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  863. if(missing) {
  864. while(n--)
  865. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  866. XFreeStringList(missing);
  867. }
  868. if(m->dc.font.set) {
  869. XFontSetExtents *font_extents;
  870. XFontStruct **xfonts;
  871. char **font_names;
  872. m->dc.font.ascent = m->dc.font.descent = 0;
  873. font_extents = XExtentsOfFontSet(m->dc.font.set);
  874. n = XFontsOfFontSet(m->dc.font.set, &xfonts, &font_names);
  875. for(i = 0, m->dc.font.ascent = 0, m->dc.font.descent = 0; i < n; i++) {
  876. if(m->dc.font.ascent < (*xfonts)->ascent)
  877. m->dc.font.ascent = (*xfonts)->ascent;
  878. if(m->dc.font.descent < (*xfonts)->descent)
  879. m->dc.font.descent = (*xfonts)->descent;
  880. xfonts++;
  881. }
  882. }
  883. else {
  884. if(m->dc.font.xfont)
  885. XFreeFont(dpy, m->dc.font.xfont);
  886. m->dc.font.xfont = NULL;
  887. if(!(m->dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  888. && !(m->dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  889. eprint("error, cannot load font: '%s'\n", fontstr);
  890. m->dc.font.ascent = m->dc.font.xfont->ascent;
  891. m->dc.font.descent = m->dc.font.xfont->descent;
  892. }
  893. m->dc.font.height = m->dc.font.ascent + m->dc.font.descent;
  894. }
  895. Bool
  896. isoccupied(unsigned int monitor, unsigned int t) {
  897. Client *c;
  898. for(c = clients; c; c = c->next)
  899. if(c->tags[t] && c->monitor == monitor)
  900. return True;
  901. return False;
  902. }
  903. Bool
  904. isprotodel(Client *c) {
  905. int i, n;
  906. Atom *protocols;
  907. Bool ret = False;
  908. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  909. for(i = 0; !ret && i < n; i++)
  910. if(protocols[i] == wmatom[WMDelete])
  911. ret = True;
  912. XFree(protocols);
  913. }
  914. return ret;
  915. }
  916. Bool
  917. isurgent(unsigned int monitor, unsigned int t) {
  918. Client *c;
  919. for(c = clients; c; c = c->next)
  920. if(c->monitor == monitor && c->isurgent && c->tags[t])
  921. return True;
  922. return False;
  923. }
  924. Bool
  925. isvisible(Client *c, int monitor) {
  926. unsigned int i;
  927. if(c->monitor != monitor)
  928. return False;
  929. for(i = 0; i < LENGTH(tags); i++)
  930. if(c->tags[i] && monitors[c->monitor].seltags[i])
  931. return True;
  932. return False;
  933. }
  934. void
  935. keypress(XEvent *e) {
  936. unsigned int i;
  937. KeySym keysym;
  938. XKeyEvent *ev;
  939. ev = &e->xkey;
  940. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  941. for(i = 0; i < LENGTH(keys); i++)
  942. if(keysym == keys[i].keysym
  943. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state))
  944. {
  945. if(keys[i].func)
  946. keys[i].func(keys[i].arg);
  947. }
  948. }
  949. void
  950. killclient(const char *arg) {
  951. XEvent ev;
  952. if(!sel)
  953. return;
  954. if(isprotodel(sel)) {
  955. ev.type = ClientMessage;
  956. ev.xclient.window = sel->win;
  957. ev.xclient.message_type = wmatom[WMProtocols];
  958. ev.xclient.format = 32;
  959. ev.xclient.data.l[0] = wmatom[WMDelete];
  960. ev.xclient.data.l[1] = CurrentTime;
  961. XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
  962. }
  963. else
  964. XKillClient(dpy, sel->win);
  965. }
  966. void
  967. manage(Window w, XWindowAttributes *wa) {
  968. Client *c, *t = NULL;
  969. Monitor *m;
  970. Status rettrans;
  971. Window trans;
  972. XWindowChanges wc;
  973. c = emallocz(sizeof(Client));
  974. c->tags = emallocz(sizeof initags);
  975. c->win = w;
  976. applyrules(c);
  977. m = &monitors[c->monitor];
  978. c->x = wa->x + m->sx;
  979. c->y = wa->y + m->sy;
  980. c->w = wa->width;
  981. c->h = wa->height;
  982. c->oldborder = wa->border_width;
  983. if(c->w == m->sw && c->h == m->sh) {
  984. c->x = m->sx;
  985. c->y = m->sy;
  986. c->border = wa->border_width;
  987. }
  988. else {
  989. if(c->x + c->w + 2 * c->border > m->wax + m->waw)
  990. c->x = m->wax + m->waw - c->w - 2 * c->border;
  991. if(c->y + c->h + 2 * c->border > m->way + m->wah)
  992. c->y = m->way + m->wah - c->h - 2 * c->border;
  993. if(c->x < m->wax)
  994. c->x = m->wax;
  995. if(c->y < m->way)
  996. c->y = m->way;
  997. c->border = BORDERPX;
  998. }
  999. wc.border_width = c->border;
  1000. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  1001. XSetWindowBorder(dpy, w, m->dc.norm[ColBorder]);
  1002. configure(c); /* propagates border_width, if size doesn't change */
  1003. updatesizehints(c);
  1004. XSelectInput(dpy, w, EnterWindowMask | FocusChangeMask | PropertyChangeMask | StructureNotifyMask);
  1005. grabbuttons(c, False);
  1006. updatetitle(c);
  1007. if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
  1008. for(t = clients; t && t->win != trans; t = t->next);
  1009. if(t)
  1010. memcpy(c->tags, t->tags, sizeof initags);
  1011. if(!c->isfloating)
  1012. c->isfloating = (rettrans == Success) || c->isfixed;
  1013. attach(c);
  1014. attachstack(c);
  1015. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
  1016. ban(c);
  1017. XMapWindow(dpy, c->win);
  1018. setclientstate(c, NormalState);
  1019. arrange();
  1020. }
  1021. void
  1022. mappingnotify(XEvent *e) {
  1023. XMappingEvent *ev = &e->xmapping;
  1024. XRefreshKeyboardMapping(ev);
  1025. if(ev->request == MappingKeyboard)
  1026. grabkeys();
  1027. }
  1028. void
  1029. maprequest(XEvent *e) {
  1030. static XWindowAttributes wa;
  1031. XMapRequestEvent *ev = &e->xmaprequest;
  1032. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1033. return;
  1034. if(wa.override_redirect)
  1035. return;
  1036. if(!getclient(ev->window))
  1037. manage(ev->window, &wa);
  1038. }
  1039. int
  1040. monitorat() {
  1041. int i, x, y;
  1042. Window win;
  1043. unsigned int mask;
  1044. XQueryPointer(dpy, root, &win, &win, &x, &y, &i, &i, &mask);
  1045. for(i = 0; i < mcount; i++) {
  1046. if((x >= monitors[i].sx && x < monitors[i].sx + monitors[i].sw)
  1047. && (y >= monitors[i].sy && y < monitors[i].sy + monitors[i].sh)) {
  1048. return i;
  1049. }
  1050. }
  1051. return 0;
  1052. }
  1053. void
  1054. movemouse(Client *c) {
  1055. int x1, y1, ocx, ocy, di, nx, ny;
  1056. unsigned int dui;
  1057. Window dummy;
  1058. XEvent ev;
  1059. ocx = nx = c->x;
  1060. ocy = ny = c->y;
  1061. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1062. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1063. return;
  1064. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  1065. for(;;) {
  1066. XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
  1067. switch (ev.type) {
  1068. case ButtonRelease:
  1069. XUngrabPointer(dpy, CurrentTime);
  1070. return;
  1071. case ConfigureRequest:
  1072. case Expose:
  1073. case MapRequest:
  1074. handler[ev.type](&ev);
  1075. break;
  1076. case MotionNotify:
  1077. XSync(dpy, False);
  1078. nx = ocx + (ev.xmotion.x - x1);
  1079. ny = ocy + (ev.xmotion.y - y1);
  1080. Monitor *m = &monitors[monitorat()];
  1081. if(abs(m->wax - nx) < SNAP)
  1082. nx = m->wax;
  1083. else if(abs((m->wax + m->waw) - (nx + c->w + 2 * c->border)) < SNAP)
  1084. nx = m->wax + m->waw - c->w - 2 * c->border;
  1085. if(abs(m->way - ny) < SNAP)
  1086. ny = m->way;
  1087. else if(abs((m->way + m->wah) - (ny + c->h + 2 * c->border)) < SNAP)
  1088. ny = m->way + m->wah - c->h - 2 * c->border;
  1089. if((monitors[selmonitor].layout->arrange != floating) && (abs(nx - c->x) > SNAP || abs(ny - c->y) > SNAP))
  1090. togglefloating(NULL);
  1091. if((monitors[selmonitor].layout->arrange == floating) || c->isfloating)
  1092. resize(c, nx, ny, c->w, c->h, False);
  1093. memcpy(c->tags, monitors[monitorat()].seltags, sizeof initags);
  1094. break;
  1095. }
  1096. }
  1097. }
  1098. Client *
  1099. nexttiled(Client *c, int monitor) {
  1100. for(; c && (c->isfloating || !isvisible(c, monitor)); c = c->next);
  1101. return c;
  1102. }
  1103. void
  1104. propertynotify(XEvent *e) {
  1105. Client *c;
  1106. Window trans;
  1107. XPropertyEvent *ev = &e->xproperty;
  1108. if(ev->state == PropertyDelete)
  1109. return; /* ignore */
  1110. if((c = getclient(ev->window))) {
  1111. switch (ev->atom) {
  1112. default: break;
  1113. case XA_WM_TRANSIENT_FOR:
  1114. XGetTransientForHint(dpy, c->win, &trans);
  1115. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  1116. arrange();
  1117. break;
  1118. case XA_WM_NORMAL_HINTS:
  1119. updatesizehints(c);
  1120. break;
  1121. case XA_WM_HINTS:
  1122. updatewmhints(c);
  1123. drawbar();
  1124. break;
  1125. }
  1126. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1127. updatetitle(c);
  1128. if(c == sel)
  1129. drawbar();
  1130. }
  1131. }
  1132. }
  1133. void
  1134. quit(const char *arg) {
  1135. readin = running = False;
  1136. }
  1137. void
  1138. reapply(const char *arg) {
  1139. static Bool zerotags[LENGTH(tags)] = { 0 };
  1140. Client *c;
  1141. for(c = clients; c; c = c->next) {
  1142. memcpy(c->tags, zerotags, sizeof zerotags);
  1143. applyrules(c);
  1144. }
  1145. arrange();
  1146. }
  1147. void
  1148. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  1149. XWindowChanges wc;
  1150. //Monitor scr = monitors[monitorat()];
  1151. // c->monitor = monitorat();
  1152. if(sizehints) {
  1153. /* set minimum possible */
  1154. if (w < 1)
  1155. w = 1;
  1156. if (h < 1)
  1157. h = 1;
  1158. /* temporarily remove base dimensions */
  1159. w -= c->basew;
  1160. h -= c->baseh;
  1161. /* adjust for aspect limits */
  1162. if (c->minay > 0 && c->maxay > 0 && c->minax > 0 && c->maxax > 0) {
  1163. if (w * c->maxay > h * c->maxax)
  1164. w = h * c->maxax / c->maxay;
  1165. else if (w * c->minay < h * c->minax)
  1166. h = w * c->minay / c->minax;
  1167. }
  1168. /* adjust for increment value */
  1169. if(c->incw)
  1170. w -= w % c->incw;
  1171. if(c->inch)
  1172. h -= h % c->inch;
  1173. /* restore base dimensions */
  1174. w += c->basew;
  1175. h += c->baseh;
  1176. if(c->minw > 0 && w < c->minw)
  1177. w = c->minw;
  1178. if(c->minh > 0 && h < c->minh)
  1179. h = c->minh;
  1180. if(c->maxw > 0 && w > c->maxw)
  1181. w = c->maxw;
  1182. if(c->maxh > 0 && h > c->maxh)
  1183. h = c->maxh;
  1184. }
  1185. if(w <= 0 || h <= 0)
  1186. return;
  1187. /* TODO: offscreen appearance fixes */
  1188. /*
  1189. if(x > scr.sw)
  1190. x = scr.sw - w - 2 * c->border;
  1191. if(y > scr.sh)
  1192. y = scr.sh - h - 2 * c->border;
  1193. if(x + w + 2 * c->border < scr.sx)
  1194. x = scr.sx;
  1195. if(y + h + 2 * c->border < scr.sy)
  1196. y = scr.sy;
  1197. */
  1198. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  1199. c->x = wc.x = x;
  1200. c->y = wc.y = y;
  1201. c->w = wc.width = w;
  1202. c->h = wc.height = h;
  1203. wc.border_width = c->border;
  1204. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  1205. configure(c);
  1206. XSync(dpy, False);
  1207. }
  1208. }
  1209. void
  1210. resizemouse(Client *c) {
  1211. int ocx, ocy;
  1212. int nw, nh;
  1213. XEvent ev;
  1214. ocx = c->x;
  1215. ocy = c->y;
  1216. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1217. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1218. return;
  1219. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
  1220. for(;;) {
  1221. XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
  1222. switch(ev.type) {
  1223. case ButtonRelease:
  1224. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
  1225. c->w + c->border - 1, c->h + c->border - 1);
  1226. XUngrabPointer(dpy, CurrentTime);
  1227. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1228. return;
  1229. case ConfigureRequest:
  1230. case Expose:
  1231. case MapRequest:
  1232. handler[ev.type](&ev);
  1233. break;
  1234. case MotionNotify:
  1235. XSync(dpy, False);
  1236. if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
  1237. nw = 1;
  1238. if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
  1239. nh = 1;
  1240. if((monitors[selmonitor].layout->arrange != floating) && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP))
  1241. togglefloating(NULL);
  1242. if((monitors[selmonitor].layout->arrange == floating) || c->isfloating)
  1243. resize(c, c->x, c->y, nw, nh, True);
  1244. break;
  1245. }
  1246. }
  1247. }
  1248. void
  1249. restack(void) {
  1250. unsigned int i;
  1251. Client *c;
  1252. XEvent ev;
  1253. XWindowChanges wc;
  1254. drawbar();
  1255. if(!sel)
  1256. return;
  1257. if(sel->isfloating || (monitors[selmonitor].layout->arrange == floating))
  1258. XRaiseWindow(dpy, sel->win);
  1259. if(monitors[selmonitor].layout->arrange != floating) {
  1260. wc.stack_mode = Below;
  1261. wc.sibling = monitors[selmonitor].barwin;
  1262. if(!sel->isfloating) {
  1263. XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc);
  1264. wc.sibling = sel->win;
  1265. }
  1266. for(i = 0; i < mcount; i++) {
  1267. for(c = nexttiled(clients, i); c; c = nexttiled(c->next, i)) {
  1268. if(c == sel)
  1269. continue;
  1270. XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
  1271. wc.sibling = c->win;
  1272. }
  1273. }
  1274. }
  1275. XSync(dpy, False);
  1276. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1277. }
  1278. void
  1279. run(void) {
  1280. char *p;
  1281. char buf[sizeof stext];
  1282. fd_set rd;
  1283. int r, xfd;
  1284. unsigned int len, offset;
  1285. XEvent ev;
  1286. /* main event loop, also reads status text from stdin */
  1287. XSync(dpy, False);
  1288. xfd = ConnectionNumber(dpy);
  1289. readin = True;
  1290. offset = 0;
  1291. len = sizeof stext - 1;
  1292. buf[len] = stext[len] = '\0'; /* 0-terminator is never touched */
  1293. while(running) {
  1294. FD_ZERO(&rd);
  1295. if(readin)
  1296. FD_SET(STDIN_FILENO, &rd);
  1297. FD_SET(xfd, &rd);
  1298. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1299. if(errno == EINTR)
  1300. continue;
  1301. eprint("select failed\n");
  1302. }
  1303. if(FD_ISSET(STDIN_FILENO, &rd)) {
  1304. switch((r = read(STDIN_FILENO, buf + offset, len - offset))) {
  1305. case -1:
  1306. strncpy(stext, strerror(errno), len);
  1307. readin = False;
  1308. break;
  1309. case 0:
  1310. strncpy(stext, "EOF", 4);
  1311. readin = False;
  1312. break;
  1313. default:
  1314. for(p = buf + offset; r > 0; p++, r--, offset++)
  1315. if(*p == '\n' || *p == '\0') {
  1316. *p = '\0';
  1317. strncpy(stext, buf, len);
  1318. p += r - 1; /* p is buf + offset + r - 1 */
  1319. for(r = 0; *(p - r) && *(p - r) != '\n'; r++);
  1320. offset = r;
  1321. if(r)
  1322. memmove(buf, p - r + 1, r);
  1323. break;
  1324. }
  1325. break;
  1326. }
  1327. drawbar();
  1328. }
  1329. while(XPending(dpy)) {
  1330. XNextEvent(dpy, &ev);
  1331. if(handler[ev.type])
  1332. (handler[ev.type])(&ev); /* call handler */
  1333. }
  1334. }
  1335. }
  1336. void
  1337. scan(void) {
  1338. unsigned int i, num;
  1339. Window *wins, d1, d2;
  1340. XWindowAttributes wa;
  1341. wins = NULL;
  1342. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1343. for(i = 0; i < num; i++) {
  1344. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1345. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1346. continue;
  1347. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1348. manage(wins[i], &wa);
  1349. }
  1350. for(i = 0; i < num; i++) { /* now the transients */
  1351. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1352. continue;
  1353. if(XGetTransientForHint(dpy, wins[i], &d1)
  1354. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1355. manage(wins[i], &wa);
  1356. }
  1357. }
  1358. if(wins)
  1359. XFree(wins);
  1360. }
  1361. void
  1362. setclientstate(Client *c, long state) {
  1363. long data[] = {state, None};
  1364. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1365. PropModeReplace, (unsigned char *)data, 2);
  1366. }
  1367. void
  1368. setlayout(const char *arg) {
  1369. unsigned int i;
  1370. Monitor *m = &monitors[monitorat()];
  1371. if(!arg) {
  1372. m->layout++;
  1373. if(m->layout == &layouts[LENGTH(layouts)])
  1374. m->layout = &layouts[0];
  1375. }
  1376. else {
  1377. for(i = 0; i < LENGTH(layouts); i++)
  1378. if(!strcmp(arg, layouts[i].symbol))
  1379. break;
  1380. if(i == LENGTH(layouts))
  1381. return;
  1382. m->layout = &layouts[i];
  1383. }
  1384. if(sel)
  1385. arrange();
  1386. else
  1387. drawbar();
  1388. }
  1389. void
  1390. setmwfact(const char *arg) {
  1391. double delta;
  1392. Monitor *m = &monitors[monitorat()];
  1393. if(!domwfact)
  1394. return;
  1395. /* arg handling, manipulate mwfact */
  1396. if(arg == NULL)
  1397. m->mwfact = MWFACT;
  1398. else if(sscanf(arg, "%lf", &delta) == 1) {
  1399. if(arg[0] == '+' || arg[0] == '-')
  1400. m->mwfact += delta;
  1401. else
  1402. m->mwfact = delta;
  1403. if(m->mwfact < 0.1)
  1404. m->mwfact = 0.1;
  1405. else if(m->mwfact > 0.9)
  1406. m->mwfact = 0.9;
  1407. }
  1408. arrange();
  1409. }
  1410. void
  1411. setup(void) {
  1412. unsigned int i, j, k;
  1413. Monitor *m;
  1414. XSetWindowAttributes wa;
  1415. XineramaScreenInfo *info = NULL;
  1416. /* init atoms */
  1417. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1418. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1419. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1420. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1421. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1422. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1423. /* init cursors */
  1424. wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1425. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1426. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1427. // init screens/monitors first
  1428. mcount = 1;
  1429. if((isxinerama = XineramaIsActive(dpy)))
  1430. info = XineramaQueryScreens(dpy, &mcount);
  1431. monitors = emallocz(mcount * sizeof(Monitor));
  1432. root = DefaultRootWindow(dpy);
  1433. for(i = 0; i < mcount; i++) {
  1434. /* init geometry */
  1435. m = &monitors[i];
  1436. m->screen = isxinerama ? 0 : i;
  1437. if (mcount != 1 && isxinerama) {
  1438. m->sx = info[i].x_org;
  1439. m->sy = info[i].y_org;
  1440. m->sw = info[i].width;
  1441. m->sh = info[i].height;
  1442. fprintf(stderr, "monitor[%d]: %d,%d,%d,%d\n", i, m->sx, m->sy, m->sw, m->sh);
  1443. }
  1444. else {
  1445. m->sx = 0;
  1446. m->sy = 0;
  1447. m->sw = DisplayWidth(dpy, m->screen);
  1448. m->sh = DisplayHeight(dpy, m->screen);
  1449. }
  1450. m->seltags = emallocz(sizeof initags);
  1451. m->prevtags = emallocz(sizeof initags);
  1452. memcpy(m->seltags, initags, sizeof initags);
  1453. memcpy(m->prevtags, initags, sizeof initags);
  1454. /* init appearance */
  1455. m->dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR, m->screen);
  1456. m->dc.norm[ColBG] = getcolor(NORMBGCOLOR, m->screen);
  1457. m->dc.norm[ColFG] = getcolor(NORMFGCOLOR, m->screen);
  1458. m->dc.sel[ColBorder] = getcolor(SELBORDERCOLOR, m->screen);
  1459. m->dc.sel[ColBG] = getcolor(SELBGCOLOR, m->screen);
  1460. m->dc.sel[ColFG] = getcolor(SELFGCOLOR, m->screen);
  1461. initfont(m, FONT);
  1462. m->dc.h = bh = m->dc.font.height + 2;
  1463. /* init layouts */
  1464. m->mwfact = MWFACT;
  1465. m->layout = &layouts[0];
  1466. for(blw = k = 0; k < LENGTH(layouts); k++) {
  1467. j = textw(m, layouts[k].symbol);
  1468. if(j > blw)
  1469. blw = j;
  1470. }
  1471. // TODO: bpos per screen?
  1472. bpos = BARPOS;
  1473. wa.override_redirect = 1;
  1474. wa.background_pixmap = ParentRelative;
  1475. wa.event_mask = ButtonPressMask | ExposureMask;
  1476. /* init bars */
  1477. m->barwin = XCreateWindow(dpy, root, m->sx, m->sy, m->sw, bh, 0,
  1478. DefaultDepth(dpy, m->screen), CopyFromParent, DefaultVisual(dpy, m->screen),
  1479. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  1480. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1481. updatebarpos(m);
  1482. XMapRaised(dpy, m->barwin);
  1483. strcpy(stext, "dwm-"VERSION);
  1484. m->dc.drawable = XCreatePixmap(dpy, root, m->sw, bh, DefaultDepth(dpy, m->screen));
  1485. m->dc.gc = XCreateGC(dpy, root, 0, 0);
  1486. XSetLineAttributes(dpy, m->dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1487. if(!m->dc.font.set)
  1488. XSetFont(dpy, m->dc.gc, m->dc.font.xfont->fid);
  1489. /* EWMH support per monitor */
  1490. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1491. PropModeReplace, (unsigned char *) netatom, NetLast);
  1492. /* select for events */
  1493. wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  1494. | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
  1495. XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  1496. XSelectInput(dpy, root, wa.event_mask);
  1497. }
  1498. if(info)
  1499. XFree(info);
  1500. /* grab keys */
  1501. grabkeys();
  1502. /* init tags */
  1503. compileregs();
  1504. selmonitor = monitorat();
  1505. fprintf(stderr, "selmonitor == %d\n", selmonitor);
  1506. }
  1507. void
  1508. spawn(const char *arg) {
  1509. static char *shell = NULL;
  1510. if(!shell && !(shell = getenv("SHELL")))
  1511. shell = "/bin/sh";
  1512. if(!arg)
  1513. return;
  1514. /* The double-fork construct avoids zombie processes and keeps the code
  1515. * clean from stupid signal handlers. */
  1516. if(fork() == 0) {
  1517. if(fork() == 0) {
  1518. if(dpy)
  1519. close(ConnectionNumber(dpy));
  1520. setsid();
  1521. execl(shell, shell, "-c", arg, (char *)NULL);
  1522. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
  1523. perror(" failed");
  1524. }
  1525. exit(0);
  1526. }
  1527. wait(0);
  1528. }
  1529. void
  1530. tag(const char *arg) {
  1531. unsigned int i;
  1532. if(!sel)
  1533. return;
  1534. for(i = 0; i < LENGTH(tags); i++)
  1535. sel->tags[i] = (NULL == arg);
  1536. sel->tags[idxoftag(arg)] = True;
  1537. arrange();
  1538. }
  1539. unsigned int
  1540. textnw(Monitor *m, const char *text, unsigned int len) {
  1541. XRectangle r;
  1542. if(m->dc.font.set) {
  1543. XmbTextExtents(m->dc.font.set, text, len, NULL, &r);
  1544. return r.width;
  1545. }
  1546. return XTextWidth(m->dc.font.xfont, text, len);
  1547. }
  1548. unsigned int
  1549. textw(Monitor *m, const char *text) {
  1550. return textnw(m, text, strlen(text)) + m->dc.font.height;
  1551. }
  1552. void
  1553. tile(void) {
  1554. unsigned int i, j, n, nx, ny, nw, nh, mw, th;
  1555. Client *c, *mc;
  1556. domwfact = dozoom = True;
  1557. nx = ny = nw = 0; /* gcc stupidity requires this */
  1558. for (i = 0; i < mcount; i++) {
  1559. Monitor *m = &monitors[i];
  1560. for(n = 0, c = nexttiled(clients, i); c; c = nexttiled(c->next, i))
  1561. n++;
  1562. /* window geoms */
  1563. mw = (n == 1) ? m->waw : m->mwfact * m->waw;
  1564. th = (n > 1) ? m->wah / (n - 1) : 0;
  1565. if(n > 1 && th < bh)
  1566. th = m->wah;
  1567. for(j = 0, c = mc = nexttiled(clients, i); c; c = nexttiled(c->next, i)) {
  1568. if(j == 0) { /* master */
  1569. nx = m->wax;
  1570. ny = m->way;
  1571. nw = mw - 2 * c->border;
  1572. nh = m->wah - 2 * c->border;
  1573. }
  1574. else { /* tile window */
  1575. if(j == 1) {
  1576. ny = m->way;
  1577. nx += mc->w + 2 * mc->border;
  1578. nw = m->waw - mw - 2 * c->border;
  1579. }
  1580. if(j + 1 == n) /* remainder */
  1581. nh = (m->way + m->wah) - ny - 2 * c->border;
  1582. else
  1583. nh = th - 2 * c->border;
  1584. }
  1585. fprintf(stderr, "tile(%d, %d, %d, %d)\n", nx, ny, nw, nh);
  1586. resize(c, nx, ny, nw, nh, RESIZEHINTS);
  1587. if((RESIZEHINTS) && ((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw)))
  1588. /* client doesn't accept size constraints */
  1589. resize(c, nx, ny, nw, nh, False);
  1590. if(n > 1 && th != m->wah)
  1591. ny = c->y + c->h + 2 * c->border;
  1592. j++;
  1593. }
  1594. }
  1595. fprintf(stderr, "done\n");
  1596. }
  1597. void
  1598. togglebar(const char *arg) {
  1599. if(bpos == BarOff)
  1600. bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
  1601. else
  1602. bpos = BarOff;
  1603. updatebarpos(&monitors[monitorat()]);
  1604. arrange();
  1605. }
  1606. void
  1607. togglefloating(const char *arg) {
  1608. if(!sel)
  1609. return;
  1610. sel->isfloating = !sel->isfloating;
  1611. if(sel->isfloating)
  1612. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1613. arrange();
  1614. }
  1615. void
  1616. toggletag(const char *arg) {
  1617. unsigned int i, j;
  1618. if(!sel)
  1619. return;
  1620. i = idxoftag(arg);
  1621. sel->tags[i] = !sel->tags[i];
  1622. for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
  1623. if(j == LENGTH(tags))
  1624. sel->tags[i] = True; /* at least one tag must be enabled */
  1625. arrange();
  1626. }
  1627. void
  1628. toggleview(const char *arg) {
  1629. unsigned int i, j;
  1630. Monitor *m = &monitors[monitorat()];
  1631. i = idxoftag(arg);
  1632. m->seltags[i] = !m->seltags[i];
  1633. for(j = 0; j < LENGTH(tags) && !m->seltags[j]; j++);
  1634. if(j == LENGTH(tags))
  1635. m->seltags[i] = True; /* at least one tag must be viewed */
  1636. arrange();
  1637. }
  1638. void
  1639. unban(Client *c) {
  1640. if(!c->isbanned)
  1641. return;
  1642. XMoveWindow(dpy, c->win, c->x, c->y);
  1643. c->isbanned = False;
  1644. }
  1645. void
  1646. unmanage(Client *c) {
  1647. XWindowChanges wc;
  1648. wc.border_width = c->oldborder;
  1649. /* The server grab construct avoids race conditions. */
  1650. XGrabServer(dpy);
  1651. XSetErrorHandler(xerrordummy);
  1652. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1653. detach(c);
  1654. detachstack(c);
  1655. if(sel == c)
  1656. focus(NULL);
  1657. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1658. setclientstate(c, WithdrawnState);
  1659. free(c->tags);
  1660. free(c);
  1661. XSync(dpy, False);
  1662. XSetErrorHandler(xerror);
  1663. XUngrabServer(dpy);
  1664. arrange();
  1665. }
  1666. void
  1667. unmapnotify(XEvent *e) {
  1668. Client *c;
  1669. XUnmapEvent *ev = &e->xunmap;
  1670. if((c = getclient(ev->window)))
  1671. unmanage(c);
  1672. }
  1673. void
  1674. updatebarpos(Monitor *m) {
  1675. XEvent ev;
  1676. m->wax = m->sx;
  1677. m->way = m->sy;
  1678. m->wah = m->sh;
  1679. m->waw = m->sw;
  1680. switch(bpos) {
  1681. default:
  1682. m->wah -= bh;
  1683. m->way += bh;
  1684. XMoveWindow(dpy, m->barwin, m->sx, m->sy);
  1685. break;
  1686. case BarBot:
  1687. m->wah -= bh;
  1688. XMoveWindow(dpy, m->barwin, m->sx, m->sy + m->wah);
  1689. break;
  1690. case BarOff:
  1691. XMoveWindow(dpy, m->barwin, m->sx, m->sy - bh);
  1692. break;
  1693. }
  1694. XSync(dpy, False);
  1695. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1696. }
  1697. void
  1698. updatesizehints(Client *c) {
  1699. long msize;
  1700. XSizeHints size;
  1701. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  1702. size.flags = PSize;
  1703. c->flags = size.flags;
  1704. if(c->flags & PBaseSize) {
  1705. c->basew = size.base_width;
  1706. c->baseh = size.base_height;
  1707. }
  1708. else if(c->flags & PMinSize) {
  1709. c->basew = size.min_width;
  1710. c->baseh = size.min_height;
  1711. }
  1712. else
  1713. c->basew = c->baseh = 0;
  1714. if(c->flags & PResizeInc) {
  1715. c->incw = size.width_inc;
  1716. c->inch = size.height_inc;
  1717. }
  1718. else
  1719. c->incw = c->inch = 0;
  1720. if(c->flags & PMaxSize) {
  1721. c->maxw = size.max_width;
  1722. c->maxh = size.max_height;
  1723. }
  1724. else
  1725. c->maxw = c->maxh = 0;
  1726. if(c->flags & PMinSize) {
  1727. c->minw = size.min_width;
  1728. c->minh = size.min_height;
  1729. }
  1730. else if(c->flags & PBaseSize) {
  1731. c->minw = size.base_width;
  1732. c->minh = size.base_height;
  1733. }
  1734. else
  1735. c->minw = c->minh = 0;
  1736. if(c->flags & PAspect) {
  1737. c->minax = size.min_aspect.x;
  1738. c->maxax = size.max_aspect.x;
  1739. c->minay = size.min_aspect.y;
  1740. c->maxay = size.max_aspect.y;
  1741. }
  1742. else
  1743. c->minax = c->maxax = c->minay = c->maxay = 0;
  1744. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1745. && c->maxw == c->minw && c->maxh == c->minh);
  1746. }
  1747. void
  1748. updatetitle(Client *c) {
  1749. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1750. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1751. }
  1752. void
  1753. updatewmhints(Client *c) {
  1754. XWMHints *wmh;
  1755. if((wmh = XGetWMHints(dpy, c->win))) {
  1756. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1757. XFree(wmh);
  1758. }
  1759. }
  1760. /* There's no way to check accesses to destroyed windows, thus those cases are
  1761. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1762. * default error handler, which may call exit. */
  1763. int
  1764. xerror(Display *dpy, XErrorEvent *ee) {
  1765. if(ee->error_code == BadWindow
  1766. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1767. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1768. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1769. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1770. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1771. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1772. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1773. return 0;
  1774. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1775. ee->request_code, ee->error_code);
  1776. return xerrorxlib(dpy, ee); /* may call exit */
  1777. }
  1778. int
  1779. xerrordummy(Display *dsply, XErrorEvent *ee) {
  1780. return 0;
  1781. }
  1782. /* Startup Error handler to check if another window manager
  1783. * is already running. */
  1784. int
  1785. xerrorstart(Display *dsply, XErrorEvent *ee) {
  1786. otherwm = True;
  1787. return -1;
  1788. }
  1789. void
  1790. view(const char *arg) {
  1791. unsigned int i;
  1792. Bool tmp[LENGTH(tags)];
  1793. Monitor *m = &monitors[monitorat()];
  1794. for(i = 0; i < LENGTH(tags); i++)
  1795. tmp[i] = (NULL == arg);
  1796. tmp[idxoftag(arg)] = True;
  1797. if(memcmp(m->seltags, tmp, sizeof initags) != 0) {
  1798. memcpy(m->prevtags, m->seltags, sizeof initags);
  1799. memcpy(m->seltags, tmp, sizeof initags);
  1800. arrange();
  1801. }
  1802. }
  1803. void
  1804. viewprevtag(const char *arg) {
  1805. static Bool tmp[LENGTH(tags)];
  1806. Monitor *m = &monitors[monitorat()];
  1807. memcpy(tmp, m->seltags, sizeof initags);
  1808. memcpy(m->seltags, m->prevtags, sizeof initags);
  1809. memcpy(m->prevtags, tmp, sizeof initags);
  1810. arrange();
  1811. }
  1812. void
  1813. zoom(const char *arg) {
  1814. Client *c;
  1815. if(!sel || !dozoom || sel->isfloating)
  1816. return;
  1817. if((c = sel) == nexttiled(clients, c->monitor))
  1818. if(!(c = nexttiled(c->next, c->monitor)))
  1819. return;
  1820. detach(c);
  1821. attach(c);
  1822. focus(c);
  1823. arrange();
  1824. }
  1825. void
  1826. movetomonitor(const char *arg) {
  1827. if (sel) {
  1828. sel->monitor = arg ? atoi(arg) : (sel->monitor+1) % mcount;
  1829. memcpy(sel->tags, monitors[sel->monitor].seltags, sizeof initags);
  1830. resize(sel, monitors[sel->monitor].wax, monitors[sel->monitor].way, sel->w, sel->h, True);
  1831. arrange();
  1832. }
  1833. }
  1834. void
  1835. selectmonitor(const char *arg) {
  1836. Monitor *m = &monitors[arg ? atoi(arg) : (monitorat()+1) % mcount];
  1837. XWarpPointer(dpy, None, root, 0, 0, 0, 0, m->wax+m->waw/2, m->way+m->wah/2);
  1838. focus(NULL);
  1839. }
  1840. int
  1841. main(int argc, char *argv[]) {
  1842. if(argc == 2 && !strcmp("-v", argv[1]))
  1843. eprint("dwm-"VERSION", © 2006-2007 Anselm R. Garbe, Sander van Dijk, "
  1844. "Jukka Salmi, Premysl Hruby, Szabolcs Nagy, Christof Musik\n");
  1845. else if(argc != 1)
  1846. eprint("usage: dwm [-v]\n");
  1847. setlocale(LC_CTYPE, "");
  1848. if(!(dpy = XOpenDisplay(0)))
  1849. eprint("dwm: cannot open display\n");
  1850. checkotherwm();
  1851. setup();
  1852. drawbar();
  1853. scan();
  1854. run();
  1855. cleanup();
  1856. XCloseDisplay(dpy);
  1857. return 0;
  1858. }