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.

1956 lines
46 KiB

17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
16 years ago
17 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
16 years ago
17 years ago
  1. //#define XINULATOR /* debug, simulates dual head */
  2. /* See LICENSE file for copyright and license details.
  3. *
  4. * dynamic window manager is designed like any other X client as well. It is
  5. * driven through handling X events. In contrast to other X clients, a window
  6. * manager selects for SubstructureRedirectMask on the root window, to receive
  7. * events about window (dis-)appearance. Only one X connection at a time is
  8. * allowed to select for this event mask.
  9. *
  10. * The event handlers of dwm are organized in an array which is accessed
  11. * whenever a new event has been fetched. This allows event dispatching
  12. * in O(1) time.
  13. *
  14. * Each child of the root window is called a client, except windows which have
  15. * set the override_redirect flag. Clients are organized in a global
  16. * linked client list, the focus history is remembered through a global
  17. * stack list. Each client contains a bit array to indicate the tags of a
  18. * client.
  19. *
  20. * Keys and tagging rules are organized as arrays and defined in config.h.
  21. *
  22. * To understand everything else, start reading main().
  23. */
  24. #include <errno.h>
  25. #include <locale.h>
  26. #include <stdarg.h>
  27. #include <signal.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <sys/types.h>
  33. #include <sys/wait.h>
  34. #include <X11/cursorfont.h>
  35. #include <X11/keysym.h>
  36. #include <X11/Xatom.h>
  37. #include <X11/Xlib.h>
  38. #include <X11/Xproto.h>
  39. #include <X11/Xutil.h>
  40. #ifdef XINERAMA
  41. #include <X11/extensions/Xinerama.h>
  42. #endif /* XINERAMA */
  43. /* macros */
  44. #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
  45. #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
  46. #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
  47. #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
  48. #define LENGTH(X) (sizeof X / sizeof X[0])
  49. #define MAX(A, B) ((A) > (B) ? (A) : (B))
  50. #define MIN(A, B) ((A) < (B) ? (A) : (B))
  51. #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
  52. #define WIDTH(X) ((X)->w + 2 * (X)->bw)
  53. #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
  54. #define TAGMASK ((int)((1LL << LENGTH(tags)) - 1))
  55. #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
  56. /* enums */
  57. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  58. enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
  59. enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
  60. enum { WMProtocols, WMDelete, WMState, WMLast }; /* default atoms */
  61. enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
  62. ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
  63. typedef union {
  64. int i;
  65. unsigned int ui;
  66. float f;
  67. void *v;
  68. } Arg;
  69. typedef struct {
  70. unsigned int click;
  71. unsigned int mask;
  72. unsigned int button;
  73. void (*func)(const Arg *arg);
  74. const Arg arg;
  75. } Button;
  76. typedef struct Monitor Monitor;
  77. typedef struct Client Client;
  78. struct Client {
  79. char name[256];
  80. float mina, maxa;
  81. int x, y, w, h;
  82. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  83. int bw, oldbw;
  84. unsigned int tags;
  85. Bool isfixed, isfloating, isurgent;
  86. Client *next;
  87. Client *snext;
  88. Monitor *mon;
  89. Window win;
  90. };
  91. typedef struct {
  92. int x, y, w, h;
  93. unsigned long norm[ColLast];
  94. unsigned long sel[ColLast];
  95. Drawable drawable;
  96. GC gc;
  97. struct {
  98. int ascent;
  99. int descent;
  100. int height;
  101. XFontSet set;
  102. XFontStruct *xfont;
  103. } font;
  104. } DC; /* draw context */
  105. typedef struct {
  106. unsigned int mod;
  107. KeySym keysym;
  108. void (*func)(const Arg *);
  109. const Arg arg;
  110. } Key;
  111. typedef struct {
  112. const char *symbol;
  113. void (*arrange)(Monitor *);
  114. } Layout;
  115. struct Monitor {
  116. int screen_number;
  117. float mfact;
  118. int by, btx; /* bar geometry */
  119. int my, mh; /* vertical screen size*/
  120. int wx, wy, ww, wh; /* window area */
  121. unsigned int seltags;
  122. unsigned int sellt;
  123. unsigned int tagset[2];
  124. Bool showbar;
  125. Bool topbar;
  126. Client *clients;
  127. Client *sel;
  128. Client *stack;
  129. Monitor *next;
  130. Window barwin;
  131. };
  132. typedef struct {
  133. const char *class;
  134. const char *instance;
  135. const char *title;
  136. unsigned int tags;
  137. Bool isfloating;
  138. } Rule;
  139. /* function declarations */
  140. static void applyrules(Client *c);
  141. static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h);
  142. static void arrange(void);
  143. static void attach(Client *c);
  144. static void attachstack(Client *c);
  145. static void buttonpress(XEvent *e);
  146. static void checkotherwm(void);
  147. static void cleanup(void);
  148. static void cleanupmons(void);
  149. static void clearurgent(Client *c);
  150. static void configure(Client *c);
  151. static void configurenotify(XEvent *e);
  152. static void configurerequest(XEvent *e);
  153. static void destroynotify(XEvent *e);
  154. static void detach(Client *c);
  155. static void detachstack(Client *c);
  156. static void die(const char *errstr, ...);
  157. static void drawbar(Monitor *m);
  158. static void drawbars();
  159. static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
  160. static void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
  161. static void enternotify(XEvent *e);
  162. static void expose(XEvent *e);
  163. static void focus(Client *c);
  164. static void focusin(XEvent *e);
  165. static void focusstack(const Arg *arg);
  166. static Client *getclient(Window w);
  167. static unsigned long getcolor(const char *colstr);
  168. static long getstate(Window w);
  169. static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  170. static void grabbuttons(Client *c, Bool focused);
  171. static void grabkeys(void);
  172. static void initfont(const char *fontstr);
  173. static Bool isprotodel(Client *c);
  174. static void keypress(XEvent *e);
  175. static void killclient(const Arg *arg);
  176. static void manage(Window w, XWindowAttributes *wa);
  177. static void mappingnotify(XEvent *e);
  178. static void maprequest(XEvent *e);
  179. static void monocle(Monitor *m);
  180. static void movemouse(const Arg *arg);
  181. static Client *nexttiled(Client *c);
  182. static void propertynotify(XEvent *e);
  183. static void quit(const Arg *arg);
  184. static void resize(Client *c, int x, int y, int w, int h);
  185. static void resizemouse(const Arg *arg);
  186. static void restack(Monitor *m);
  187. static void run(void);
  188. static void scan(void);
  189. static void setclientstate(Client *c, long state);
  190. static void setlayout(const Arg *arg);
  191. static void setmfact(const Arg *arg);
  192. static void setup(void);
  193. static void showhide(Client *c);
  194. static void sigchld(int signal);
  195. static void spawn(const Arg *arg);
  196. static void tag(const Arg *arg);
  197. static int textnw(const char *text, unsigned int len);
  198. static void tile(Monitor *);
  199. static void togglebar(const Arg *arg);
  200. static void togglefloating(const Arg *arg);
  201. static void toggletag(const Arg *arg);
  202. static void toggleview(const Arg *arg);
  203. static void unfocus(Client *c);
  204. static void unmanage(Client *c);
  205. static void unmapnotify(XEvent *e);
  206. static void updategeom(void);
  207. static void updatebarpos(Monitor *m);
  208. static void updatebars(void);
  209. static void updatenumlockmask(void);
  210. static void updatesizehints(Client *c);
  211. static void updatestatus(void);
  212. static void updatetitle(Client *c);
  213. static void updatewmhints(Client *c);
  214. static void view(const Arg *arg);
  215. static int xerror(Display *dpy, XErrorEvent *ee);
  216. static int xerrordummy(Display *dpy, XErrorEvent *ee);
  217. static int xerrorstart(Display *dpy, XErrorEvent *ee);
  218. static void zoom(const Arg *arg);
  219. #ifdef XINERAMA
  220. static void focusmon(const Arg *arg);
  221. static void tagmon(const Arg *arg);
  222. #endif /* XINERAMA */
  223. /* variables */
  224. static char stext[256];
  225. static int screen;
  226. static int sx, sy, sw, sh; /* X display screen geometry x, y, width, height */
  227. static int bh, blw = 0; /* bar geometry */
  228. static int (*xerrorxlib)(Display *, XErrorEvent *);
  229. static unsigned int numlockmask = 0;
  230. static void (*handler[LASTEvent]) (XEvent *) = {
  231. [ButtonPress] = buttonpress,
  232. [ConfigureRequest] = configurerequest,
  233. [ConfigureNotify] = configurenotify,
  234. [DestroyNotify] = destroynotify,
  235. [EnterNotify] = enternotify,
  236. [Expose] = expose,
  237. [FocusIn] = focusin,
  238. [KeyPress] = keypress,
  239. [MappingNotify] = mappingnotify,
  240. [MapRequest] = maprequest,
  241. [PropertyNotify] = propertynotify,
  242. [UnmapNotify] = unmapnotify
  243. };
  244. static Atom wmatom[WMLast], netatom[NetLast];
  245. static Bool otherwm;
  246. static Bool running = True;
  247. static Cursor cursor[CurLast];
  248. static Display *dpy;
  249. static DC dc;
  250. static Layout *lt[] = { NULL, NULL };
  251. static Monitor *mons = NULL, *selmon = NULL;
  252. static Window root;
  253. /* configuration, allows nested code to access above variables */
  254. #include "config.h"
  255. /* compile-time check if all tags fit into an unsigned int bit array. */
  256. struct NumTags { char limitexceeded[sizeof(unsigned int) * 8 < LENGTH(tags) ? -1 : 1]; };
  257. /* function implementations */
  258. void
  259. applyrules(Client *c) {
  260. unsigned int i;
  261. Rule *r;
  262. XClassHint ch = { 0 };
  263. /* rule matching */
  264. c->isfloating = c->tags = 0;
  265. if(XGetClassHint(dpy, c->win, &ch)) {
  266. for(i = 0; i < LENGTH(rules); i++) {
  267. r = &rules[i];
  268. if((!r->title || strstr(c->name, r->title))
  269. && (!r->class || (ch.res_class && strstr(ch.res_class, r->class)))
  270. && (!r->instance || (ch.res_name && strstr(ch.res_name, r->instance)))) {
  271. c->isfloating = r->isfloating;
  272. c->tags |= r->tags;
  273. }
  274. }
  275. if(ch.res_class)
  276. XFree(ch.res_class);
  277. if(ch.res_name)
  278. XFree(ch.res_name);
  279. }
  280. c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
  281. }
  282. Bool
  283. applysizehints(Client *c, int *x, int *y, int *w, int *h) {
  284. Bool baseismin;
  285. /* set minimum possible */
  286. *w = MAX(1, *w);
  287. *h = MAX(1, *h);
  288. if(*x > sx + sw)
  289. *x = sw - WIDTH(c);
  290. if(*y > sy + sh)
  291. *y = sh - HEIGHT(c);
  292. if(*x + *w + 2 * c->bw < sx)
  293. *x = sx;
  294. if(*y + *h + 2 * c->bw < sy)
  295. *y = sy;
  296. if(*h < bh)
  297. *h = bh;
  298. if(*w < bh)
  299. *w = bh;
  300. if(resizehints || c->isfloating) {
  301. /* see last two sentences in ICCCM 4.1.2.3 */
  302. baseismin = c->basew == c->minw && c->baseh == c->minh;
  303. if(!baseismin) { /* temporarily remove base dimensions */
  304. *w -= c->basew;
  305. *h -= c->baseh;
  306. }
  307. /* adjust for aspect limits */
  308. if(c->mina > 0 && c->maxa > 0) {
  309. if(c->maxa < (float)*w / *h)
  310. *w = *h * c->maxa;
  311. else if(c->mina < (float)*h / *w)
  312. *h = *w * c->mina;
  313. }
  314. if(baseismin) { /* increment calculation requires this */
  315. *w -= c->basew;
  316. *h -= c->baseh;
  317. }
  318. /* adjust for increment value */
  319. if(c->incw)
  320. *w -= *w % c->incw;
  321. if(c->inch)
  322. *h -= *h % c->inch;
  323. /* restore base dimensions */
  324. *w += c->basew;
  325. *h += c->baseh;
  326. *w = MAX(*w, c->minw);
  327. *h = MAX(*h, c->minh);
  328. if(c->maxw)
  329. *w = MIN(*w, c->maxw);
  330. if(c->maxh)
  331. *h = MIN(*h, c->maxh);
  332. }
  333. return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
  334. }
  335. void
  336. arrange(void) {
  337. Monitor *m;
  338. /* optimise two loops into one, check focus(NULL) */
  339. for(m = mons; m; m = m->next)
  340. showhide(m->stack);
  341. focus(NULL);
  342. for(m = mons; m; m = m->next) {
  343. if(lt[m->sellt]->arrange)
  344. lt[m->sellt]->arrange(m);
  345. restack(m);
  346. }
  347. }
  348. void
  349. attach(Client *c) {
  350. c->next = c->mon->clients;
  351. c->mon->clients = c;
  352. }
  353. void
  354. attachstack(Client *c) {
  355. c->snext = c->mon->stack;
  356. c->mon->stack = c;
  357. }
  358. void
  359. buttonpress(XEvent *e) {
  360. unsigned int i, x, click;
  361. Arg arg = {0};
  362. Client *c;
  363. Monitor *m;
  364. XButtonPressedEvent *ev = &e->xbutton;
  365. click = ClkRootWin;
  366. /* focus monitor if necessary */
  367. for(m = mons; m; m = m->next)
  368. if(ev->window == m->barwin) {
  369. if(m != selmon) {
  370. unfocus(selmon->stack);
  371. selmon = m;
  372. focus(NULL);
  373. }
  374. break;
  375. }
  376. if(ev->window == selmon->barwin && ev->x >= selmon->btx) {
  377. i = 0;
  378. x = selmon->btx;
  379. do
  380. x += TEXTW(tags[i]);
  381. while(ev->x >= x && ++i < LENGTH(tags));
  382. if(i < LENGTH(tags)) {
  383. click = ClkTagBar;
  384. arg.ui = 1 << i;
  385. }
  386. else if(ev->x < x + blw)
  387. click = ClkLtSymbol;
  388. else if(ev->x > selmon->wx + selmon->ww - TEXTW(stext))
  389. click = ClkStatusText;
  390. else
  391. click = ClkWinTitle;
  392. }
  393. else if((c = getclient(ev->window))) {
  394. focus(c);
  395. click = ClkClientWin;
  396. }
  397. for(i = 0; i < LENGTH(buttons); i++)
  398. if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
  399. && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
  400. buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
  401. }
  402. void
  403. checkotherwm(void) {
  404. otherwm = False;
  405. xerrorxlib = XSetErrorHandler(xerrorstart);
  406. /* this causes an error if some other window manager is running */
  407. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  408. XSync(dpy, False);
  409. if(otherwm)
  410. die("dwm: another window manager is already running\n");
  411. XSetErrorHandler(xerror);
  412. XSync(dpy, False);
  413. }
  414. void
  415. cleanup(void) {
  416. Arg a = {.ui = ~0};
  417. Layout foo = { "", NULL };
  418. Monitor *m;
  419. view(&a);
  420. lt[selmon->sellt] = &foo;
  421. /* TODO: consider simplifying cleanup code of the stack, perhaps do that in cleanmons() ? */
  422. for(m = mons; m; m = m->next)
  423. while(m->stack)
  424. unmanage(m->stack);
  425. if(dc.font.set)
  426. XFreeFontSet(dpy, dc.font.set);
  427. else
  428. XFreeFont(dpy, dc.font.xfont);
  429. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  430. XFreePixmap(dpy, dc.drawable);
  431. XFreeGC(dpy, dc.gc);
  432. XFreeCursor(dpy, cursor[CurNormal]);
  433. XFreeCursor(dpy, cursor[CurResize]);
  434. XFreeCursor(dpy, cursor[CurMove]);
  435. cleanupmons();
  436. XSync(dpy, False);
  437. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  438. }
  439. void
  440. cleanupmons(void) {
  441. Monitor *m;
  442. while(mons) {
  443. m = mons->next;
  444. XUnmapWindow(dpy, mons->barwin);
  445. XDestroyWindow(dpy, mons->barwin);
  446. free(mons);
  447. mons = m;
  448. }
  449. }
  450. void
  451. clearurgent(Client *c) {
  452. XWMHints *wmh;
  453. c->isurgent = False;
  454. if(!(wmh = XGetWMHints(dpy, c->win)))
  455. return;
  456. wmh->flags &= ~XUrgencyHint;
  457. XSetWMHints(dpy, c->win, wmh);
  458. XFree(wmh);
  459. }
  460. void
  461. configure(Client *c) {
  462. XConfigureEvent ce;
  463. ce.type = ConfigureNotify;
  464. ce.display = dpy;
  465. ce.event = c->win;
  466. ce.window = c->win;
  467. ce.x = c->x;
  468. ce.y = c->y;
  469. ce.width = c->w;
  470. ce.height = c->h;
  471. ce.border_width = c->bw;
  472. ce.above = None;
  473. ce.override_redirect = False;
  474. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  475. }
  476. void
  477. configurenotify(XEvent *e) {
  478. Monitor *m;
  479. XConfigureEvent *ev = &e->xconfigure;
  480. if(ev->window == root && (ev->width != sw || ev->height != sh)) {
  481. sw = ev->width;
  482. sh = ev->height;
  483. updategeom();
  484. if(dc.drawable != 0)
  485. XFreePixmap(dpy, dc.drawable);
  486. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  487. updatebars();
  488. for(m = mons; m; m = m->next)
  489. XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
  490. arrange();
  491. }
  492. }
  493. void
  494. configurerequest(XEvent *e) {
  495. Client *c;
  496. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  497. XWindowChanges wc;
  498. if((c = getclient(ev->window))) {
  499. if(ev->value_mask & CWBorderWidth)
  500. c->bw = ev->border_width;
  501. else if(c->isfloating || !lt[selmon->sellt]->arrange) {
  502. if(ev->value_mask & CWX)
  503. c->x = sx + ev->x;
  504. if(ev->value_mask & CWY)
  505. c->y = sy + ev->y;
  506. if(ev->value_mask & CWWidth)
  507. c->w = ev->width;
  508. if(ev->value_mask & CWHeight)
  509. c->h = ev->height;
  510. if((c->x - sx + c->w) > sw && c->isfloating)
  511. c->x = sx + (sw / 2 - c->w / 2); /* center in x direction */
  512. if((c->y - sy + c->h) > sh && c->isfloating)
  513. c->y = sy + (sh / 2 - c->h / 2); /* center in y direction */
  514. if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
  515. configure(c);
  516. if(ISVISIBLE(c))
  517. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  518. }
  519. else
  520. configure(c);
  521. }
  522. else {
  523. wc.x = ev->x;
  524. wc.y = ev->y;
  525. wc.width = ev->width;
  526. wc.height = ev->height;
  527. wc.border_width = ev->border_width;
  528. wc.sibling = ev->above;
  529. wc.stack_mode = ev->detail;
  530. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  531. }
  532. XSync(dpy, False);
  533. }
  534. void
  535. destroynotify(XEvent *e) {
  536. Client *c;
  537. XDestroyWindowEvent *ev = &e->xdestroywindow;
  538. if((c = getclient(ev->window)))
  539. unmanage(c);
  540. }
  541. void
  542. detach(Client *c) {
  543. Client **tc;
  544. for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
  545. *tc = c->next;
  546. }
  547. void
  548. detachstack(Client *c) {
  549. Client **tc;
  550. for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
  551. *tc = c->snext;
  552. }
  553. void
  554. die(const char *errstr, ...) {
  555. va_list ap;
  556. va_start(ap, errstr);
  557. vfprintf(stderr, errstr, ap);
  558. va_end(ap);
  559. exit(EXIT_FAILURE);
  560. }
  561. void
  562. drawbar(Monitor *m) {
  563. int x;
  564. unsigned int i, occ = 0, urg = 0;
  565. unsigned long *col;
  566. Client *c;
  567. for(c = m->clients; c; c = c->next) {
  568. occ |= c->tags;
  569. if(c->isurgent)
  570. urg |= c->tags;
  571. }
  572. dc.x = 0;
  573. #ifdef XINERAMA
  574. {
  575. char buf[2];
  576. buf[0] = m->screen_number + '0';
  577. buf[1] = '\0';
  578. dc.w = TEXTW(buf);
  579. drawtext(buf, selmon == m ? dc.sel : dc.norm, True);
  580. dc.x += dc.w;
  581. }
  582. #endif /* XINERAMA */
  583. m->btx = dc.x;
  584. for(i = 0; i < LENGTH(tags); i++) {
  585. dc.w = TEXTW(tags[i]);
  586. col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
  587. drawtext(tags[i], col, urg & 1 << i);
  588. drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
  589. occ & 1 << i, urg & 1 << i, col);
  590. dc.x += dc.w;
  591. }
  592. if(blw > 0) {
  593. dc.w = blw;
  594. drawtext(lt[m->sellt]->symbol, dc.norm, False);
  595. x = dc.x + dc.w;
  596. }
  597. else
  598. x = dc.x;
  599. if(m == selmon) { /* status is only drawn on selected monitor */
  600. dc.w = TEXTW(stext);
  601. dc.x = m->ww - dc.w;
  602. if(dc.x < x) {
  603. dc.x = x;
  604. dc.w = m->ww - x;
  605. }
  606. drawtext(stext, dc.norm, False);
  607. }
  608. else {
  609. dc.x = m->ww;
  610. }
  611. if((dc.w = dc.x - x) > bh) {
  612. dc.x = x;
  613. if(m->sel) {
  614. col = m == selmon ? dc.sel : dc.norm;
  615. drawtext(m->sel->name, col, False);
  616. drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
  617. }
  618. else
  619. drawtext(NULL, dc.norm, False);
  620. }
  621. XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
  622. XSync(dpy, False);
  623. }
  624. void
  625. drawbars() {
  626. Monitor *m;
  627. for(m = mons; m; m = m->next)
  628. drawbar(m);
  629. }
  630. void
  631. drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  632. int x;
  633. XGCValues gcv;
  634. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  635. gcv.foreground = col[invert ? ColBG : ColFG];
  636. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  637. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  638. r.x = dc.x + 1;
  639. r.y = dc.y + 1;
  640. if(filled) {
  641. r.width = r.height = x + 1;
  642. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  643. }
  644. else if(empty) {
  645. r.width = r.height = x;
  646. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  647. }
  648. }
  649. void
  650. drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
  651. char buf[256];
  652. int i, x, y, h, len, olen;
  653. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  654. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  655. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  656. if(!text)
  657. return;
  658. olen = strlen(text);
  659. h = dc.font.ascent + dc.font.descent;
  660. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  661. x = dc.x + (h / 2);
  662. /* shorten text if necessary */
  663. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  664. if(!len)
  665. return;
  666. memcpy(buf, text, len);
  667. if(len < olen)
  668. for(i = len; i && i > len - 3; buf[--i] = '.');
  669. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  670. if(dc.font.set)
  671. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  672. else
  673. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  674. }
  675. void
  676. enternotify(XEvent *e) {
  677. Client *c;
  678. XCrossingEvent *ev = &e->xcrossing;
  679. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  680. return;
  681. if((c = getclient(ev->window)))
  682. focus(c);
  683. else
  684. focus(NULL);
  685. }
  686. void
  687. expose(XEvent *e) {
  688. Monitor *m;
  689. XExposeEvent *ev = &e->xexpose;
  690. if(ev->count == 0)
  691. for(m = mons; m; m = m->next)
  692. if(ev->window == m->barwin) {
  693. drawbar(m);
  694. break;
  695. }
  696. }
  697. void
  698. focus(Client *c) {
  699. if(!c || !ISVISIBLE(c))
  700. for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
  701. if(selmon->sel)
  702. unfocus(selmon->sel);
  703. if(c) {
  704. if(c->mon != selmon)
  705. selmon = c->mon;
  706. if(c->isurgent)
  707. clearurgent(c);
  708. detachstack(c);
  709. attachstack(c);
  710. grabbuttons(c, True);
  711. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  712. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  713. }
  714. else
  715. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  716. selmon->sel = c;
  717. drawbars();
  718. }
  719. void
  720. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  721. XFocusChangeEvent *ev = &e->xfocus;
  722. if(selmon->sel && ev->window != selmon->sel->win)
  723. XSetInputFocus(dpy, selmon->sel->win, RevertToPointerRoot, CurrentTime);
  724. }
  725. #ifdef XINERAMA
  726. void
  727. focusmon(const Arg *arg) {
  728. unsigned int i;
  729. Monitor *m;
  730. for(i = 0, m = mons; m; m = m->next, i++)
  731. if(i == arg->ui) {
  732. if(m->stack)
  733. focus(m->stack);
  734. else {
  735. unfocus(selmon->stack);
  736. selmon = m;
  737. focus(NULL);
  738. }
  739. drawbars();
  740. break;
  741. }
  742. }
  743. #endif /* XINERAMA */
  744. void
  745. focusstack(const Arg *arg) {
  746. Client *c = NULL, *i;
  747. if(!selmon->sel)
  748. return;
  749. if(arg->i > 0) {
  750. for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
  751. if(!c)
  752. for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
  753. }
  754. else {
  755. for(i = selmon->clients; i != selmon->sel; i = i->next)
  756. if(ISVISIBLE(i))
  757. c = i;
  758. if(!c)
  759. for(; i; i = i->next)
  760. if(ISVISIBLE(i))
  761. c = i;
  762. }
  763. if(c) {
  764. focus(c);
  765. restack(selmon);
  766. }
  767. }
  768. Client *
  769. getclient(Window w) {
  770. Client *c;
  771. Monitor *m;
  772. for(m = mons; m; m = m->next)
  773. for(c = m->clients; c; c = c->next)
  774. if(c->win == w)
  775. return c;
  776. return NULL;
  777. }
  778. unsigned long
  779. getcolor(const char *colstr) {
  780. Colormap cmap = DefaultColormap(dpy, screen);
  781. XColor color;
  782. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  783. die("error, cannot allocate color '%s'\n", colstr);
  784. return color.pixel;
  785. }
  786. long
  787. getstate(Window w) {
  788. int format, status;
  789. long result = -1;
  790. unsigned char *p = NULL;
  791. unsigned long n, extra;
  792. Atom real;
  793. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  794. &real, &format, &n, &extra, (unsigned char **)&p);
  795. if(status != Success)
  796. return -1;
  797. if(n != 0)
  798. result = *p;
  799. XFree(p);
  800. return result;
  801. }
  802. Bool
  803. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  804. char **list = NULL;
  805. int n;
  806. XTextProperty name;
  807. if(!text || size == 0)
  808. return False;
  809. text[0] = '\0';
  810. XGetTextProperty(dpy, w, &name, atom);
  811. if(!name.nitems)
  812. return False;
  813. if(name.encoding == XA_STRING)
  814. strncpy(text, (char *)name.value, size - 1);
  815. else {
  816. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  817. && n > 0 && *list) {
  818. strncpy(text, *list, size - 1);
  819. XFreeStringList(list);
  820. }
  821. }
  822. text[size - 1] = '\0';
  823. XFree(name.value);
  824. return True;
  825. }
  826. void
  827. grabbuttons(Client *c, Bool focused) {
  828. updatenumlockmask();
  829. {
  830. unsigned int i, j;
  831. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  832. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  833. if(focused) {
  834. for(i = 0; i < LENGTH(buttons); i++)
  835. if(buttons[i].click == ClkClientWin)
  836. for(j = 0; j < LENGTH(modifiers); j++)
  837. XGrabButton(dpy, buttons[i].button,
  838. buttons[i].mask | modifiers[j],
  839. c->win, False, BUTTONMASK,
  840. GrabModeAsync, GrabModeSync, None, None);
  841. } else
  842. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  843. BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  844. }
  845. }
  846. void
  847. grabkeys(void) {
  848. updatenumlockmask();
  849. { /* grab keys */
  850. unsigned int i, j;
  851. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  852. KeyCode code;
  853. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  854. for(i = 0; i < LENGTH(keys); i++) {
  855. if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  856. for(j = 0; j < LENGTH(modifiers); j++)
  857. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  858. True, GrabModeAsync, GrabModeAsync);
  859. }
  860. }
  861. }
  862. void
  863. initfont(const char *fontstr) {
  864. char *def, **missing;
  865. int i, n;
  866. missing = NULL;
  867. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  868. if(missing) {
  869. while(n--)
  870. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  871. XFreeStringList(missing);
  872. }
  873. if(dc.font.set) {
  874. XFontSetExtents *font_extents;
  875. XFontStruct **xfonts;
  876. char **font_names;
  877. dc.font.ascent = dc.font.descent = 0;
  878. font_extents = XExtentsOfFontSet(dc.font.set);
  879. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  880. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  881. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  882. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  883. xfonts++;
  884. }
  885. }
  886. else {
  887. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  888. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  889. die("error, cannot load font: '%s'\n", fontstr);
  890. dc.font.ascent = dc.font.xfont->ascent;
  891. dc.font.descent = dc.font.xfont->descent;
  892. }
  893. dc.font.height = dc.font.ascent + dc.font.descent;
  894. }
  895. Bool
  896. isprotodel(Client *c) {
  897. int i, n;
  898. Atom *protocols;
  899. Bool ret = False;
  900. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  901. for(i = 0; !ret && i < n; i++)
  902. if(protocols[i] == wmatom[WMDelete])
  903. ret = True;
  904. XFree(protocols);
  905. }
  906. return ret;
  907. }
  908. void
  909. keypress(XEvent *e) {
  910. unsigned int i;
  911. KeySym keysym;
  912. XKeyEvent *ev;
  913. ev = &e->xkey;
  914. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  915. for(i = 0; i < LENGTH(keys); i++)
  916. if(keysym == keys[i].keysym
  917. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  918. && keys[i].func)
  919. keys[i].func(&(keys[i].arg));
  920. }
  921. void
  922. killclient(const Arg *arg) {
  923. XEvent ev;
  924. if(!selmon->sel)
  925. return;
  926. if(isprotodel(selmon->sel)) {
  927. ev.type = ClientMessage;
  928. ev.xclient.window = selmon->sel->win;
  929. ev.xclient.message_type = wmatom[WMProtocols];
  930. ev.xclient.format = 32;
  931. ev.xclient.data.l[0] = wmatom[WMDelete];
  932. ev.xclient.data.l[1] = CurrentTime;
  933. XSendEvent(dpy, selmon->sel->win, False, NoEventMask, &ev);
  934. }
  935. else
  936. XKillClient(dpy, selmon->sel->win);
  937. }
  938. void
  939. manage(Window w, XWindowAttributes *wa) {
  940. static Client cz;
  941. Client *c, *t = NULL;
  942. Window trans = None;
  943. XWindowChanges wc;
  944. if(!(c = malloc(sizeof(Client))))
  945. die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  946. *c = cz;
  947. c->win = w;
  948. c->mon = selmon;
  949. /* geometry */
  950. c->x = wa->x;
  951. c->y = wa->y;
  952. c->w = wa->width;
  953. c->h = wa->height;
  954. c->oldbw = wa->border_width;
  955. if(c->w == sw && c->h == sh) {
  956. c->x = sx;
  957. c->y = sy;
  958. c->bw = 0;
  959. }
  960. else {
  961. if(c->x + WIDTH(c) > sx + sw)
  962. c->x = sx + sw - WIDTH(c);
  963. if(c->y + HEIGHT(c) > sy + sh)
  964. c->y = sy + sh - HEIGHT(c);
  965. c->x = MAX(c->x, sx);
  966. /* only fix client y-offset, if the client center might cover the bar */
  967. c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
  968. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : sy);
  969. c->bw = borderpx;
  970. }
  971. wc.border_width = c->bw;
  972. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  973. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  974. configure(c); /* propagates border_width, if size doesn't change */
  975. updatesizehints(c);
  976. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  977. grabbuttons(c, False);
  978. updatetitle(c);
  979. if(XGetTransientForHint(dpy, w, &trans))
  980. t = getclient(trans);
  981. if(t)
  982. c->tags = t->tags;
  983. else
  984. applyrules(c);
  985. if(!c->isfloating)
  986. c->isfloating = trans != None || c->isfixed;
  987. if(c->isfloating)
  988. XRaiseWindow(dpy, c->win);
  989. attach(c);
  990. attachstack(c);
  991. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  992. XMapWindow(dpy, c->win);
  993. setclientstate(c, NormalState);
  994. arrange();
  995. }
  996. void
  997. mappingnotify(XEvent *e) {
  998. XMappingEvent *ev = &e->xmapping;
  999. XRefreshKeyboardMapping(ev);
  1000. if(ev->request == MappingKeyboard)
  1001. grabkeys();
  1002. }
  1003. void
  1004. maprequest(XEvent *e) {
  1005. static XWindowAttributes wa;
  1006. XMapRequestEvent *ev = &e->xmaprequest;
  1007. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1008. return;
  1009. if(wa.override_redirect)
  1010. return;
  1011. if(!getclient(ev->window))
  1012. manage(ev->window, &wa);
  1013. }
  1014. void
  1015. monocle(Monitor *m) {
  1016. Client *c;
  1017. for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1018. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw);
  1019. }
  1020. void
  1021. movemouse(const Arg *arg) {
  1022. int x, y, ocx, ocy, di, nx, ny;
  1023. unsigned int dui;
  1024. Client *c;
  1025. Window dummy;
  1026. XEvent ev;
  1027. if(!(c = selmon->sel))
  1028. return;
  1029. restack(selmon);
  1030. ocx = c->x;
  1031. ocy = c->y;
  1032. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1033. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1034. return;
  1035. XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui);
  1036. do {
  1037. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1038. switch (ev.type) {
  1039. case ConfigureRequest:
  1040. case Expose:
  1041. case MapRequest:
  1042. handler[ev.type](&ev);
  1043. break;
  1044. case MotionNotify:
  1045. nx = ocx + (ev.xmotion.x - x);
  1046. ny = ocy + (ev.xmotion.y - y);
  1047. if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1048. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1049. if(abs(selmon->wx - nx) < snap)
  1050. nx = selmon->wx;
  1051. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1052. nx = selmon->wx + selmon->ww - WIDTH(c);
  1053. if(abs(selmon->wy - ny) < snap)
  1054. ny = selmon->wy;
  1055. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1056. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1057. if(!c->isfloating && lt[selmon->sellt]->arrange
  1058. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1059. togglefloating(NULL);
  1060. }
  1061. if(!lt[selmon->sellt]->arrange || c->isfloating)
  1062. resize(c, nx, ny, c->w, c->h);
  1063. break;
  1064. }
  1065. }
  1066. while(ev.type != ButtonRelease);
  1067. XUngrabPointer(dpy, CurrentTime);
  1068. }
  1069. Client *
  1070. nexttiled(Client *c) {
  1071. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1072. return c;
  1073. }
  1074. void
  1075. propertynotify(XEvent *e) {
  1076. Client *c;
  1077. Window trans;
  1078. XPropertyEvent *ev = &e->xproperty;
  1079. if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1080. updatestatus();
  1081. else if(ev->state == PropertyDelete)
  1082. return; /* ignore */
  1083. else if((c = getclient(ev->window))) {
  1084. switch (ev->atom) {
  1085. default: break;
  1086. case XA_WM_TRANSIENT_FOR:
  1087. XGetTransientForHint(dpy, c->win, &trans);
  1088. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  1089. arrange();
  1090. break;
  1091. case XA_WM_NORMAL_HINTS:
  1092. updatesizehints(c);
  1093. break;
  1094. case XA_WM_HINTS:
  1095. updatewmhints(c);
  1096. drawbars();
  1097. break;
  1098. }
  1099. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1100. updatetitle(c);
  1101. if(c == selmon->sel)
  1102. drawbars();
  1103. }
  1104. }
  1105. }
  1106. void
  1107. quit(const Arg *arg) {
  1108. running = False;
  1109. }
  1110. void
  1111. resize(Client *c, int x, int y, int w, int h) {
  1112. XWindowChanges wc;
  1113. if(applysizehints(c, &x, &y, &w, &h)) {
  1114. c->x = wc.x = x;
  1115. c->y = wc.y = y;
  1116. c->w = wc.width = w;
  1117. c->h = wc.height = h;
  1118. wc.border_width = c->bw;
  1119. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1120. configure(c);
  1121. XSync(dpy, False);
  1122. }
  1123. }
  1124. void
  1125. resizemouse(const Arg *arg) {
  1126. int ocx, ocy;
  1127. int nw, nh;
  1128. Client *c;
  1129. XEvent ev;
  1130. if(!(c = selmon->sel))
  1131. return;
  1132. restack(selmon);
  1133. ocx = c->x;
  1134. ocy = c->y;
  1135. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1136. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1137. return;
  1138. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1139. do {
  1140. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1141. switch(ev.type) {
  1142. case ConfigureRequest:
  1143. case Expose:
  1144. case MapRequest:
  1145. handler[ev.type](&ev);
  1146. break;
  1147. case MotionNotify:
  1148. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1149. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1150. if(snap && nw >= selmon->wx && nw <= selmon->wx + selmon->ww
  1151. && nh >= selmon->wy && nh <= selmon->wy + selmon->wh) {
  1152. if(!c->isfloating && lt[selmon->sellt]->arrange
  1153. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1154. togglefloating(NULL);
  1155. }
  1156. if(!lt[selmon->sellt]->arrange || c->isfloating)
  1157. resize(c, c->x, c->y, nw, nh);
  1158. break;
  1159. }
  1160. }
  1161. while(ev.type != ButtonRelease);
  1162. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1163. XUngrabPointer(dpy, CurrentTime);
  1164. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1165. }
  1166. void
  1167. restack(Monitor *m) {
  1168. Client *c;
  1169. XEvent ev;
  1170. XWindowChanges wc;
  1171. drawbars();
  1172. if(!m->sel)
  1173. return;
  1174. if(m->sel->isfloating || !lt[m->sellt]->arrange)
  1175. XRaiseWindow(dpy, m->sel->win);
  1176. if(lt[m->sellt]->arrange) {
  1177. wc.stack_mode = Below;
  1178. wc.sibling = m->barwin;
  1179. for(c = m->stack; c; c = c->snext)
  1180. if(!c->isfloating && ISVISIBLE(c)) {
  1181. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1182. wc.sibling = c->win;
  1183. }
  1184. }
  1185. XSync(dpy, False);
  1186. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1187. }
  1188. void
  1189. run(void) {
  1190. XEvent ev;
  1191. /* main event loop */
  1192. XSync(dpy, False);
  1193. while(running && !XNextEvent(dpy, &ev)) {
  1194. if(handler[ev.type])
  1195. (handler[ev.type])(&ev); /* call handler */
  1196. }
  1197. }
  1198. void
  1199. scan(void) {
  1200. unsigned int i, num;
  1201. Window d1, d2, *wins = NULL;
  1202. XWindowAttributes wa;
  1203. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1204. for(i = 0; i < num; i++) {
  1205. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1206. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1207. continue;
  1208. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1209. manage(wins[i], &wa);
  1210. }
  1211. for(i = 0; i < num; i++) { /* now the transients */
  1212. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1213. continue;
  1214. if(XGetTransientForHint(dpy, wins[i], &d1)
  1215. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1216. manage(wins[i], &wa);
  1217. }
  1218. if(wins)
  1219. XFree(wins);
  1220. }
  1221. }
  1222. void
  1223. setclientstate(Client *c, long state) {
  1224. long data[] = {state, None};
  1225. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1226. PropModeReplace, (unsigned char *)data, 2);
  1227. }
  1228. void
  1229. setlayout(const Arg *arg) {
  1230. if(!arg || !arg->v || arg->v != lt[selmon->sellt])
  1231. selmon->sellt ^= 1;
  1232. if(arg && arg->v)
  1233. lt[selmon->sellt] = (Layout *)arg->v;
  1234. if(selmon->sel)
  1235. arrange();
  1236. else
  1237. drawbars();
  1238. }
  1239. /* arg > 1.0 will set mfact absolutly */
  1240. void
  1241. setmfact(const Arg *arg) {
  1242. float f;
  1243. if(!arg || !lt[selmon->sellt]->arrange)
  1244. return;
  1245. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1246. if(f < 0.1 || f > 0.9)
  1247. return;
  1248. selmon->mfact = f;
  1249. arrange();
  1250. }
  1251. void
  1252. setup(void) {
  1253. unsigned int i;
  1254. int w;
  1255. XSetWindowAttributes wa;
  1256. /* init screen */
  1257. screen = DefaultScreen(dpy);
  1258. root = RootWindow(dpy, screen);
  1259. initfont(font);
  1260. sx = 0;
  1261. sy = 0;
  1262. sw = DisplayWidth(dpy, screen);
  1263. sh = DisplayHeight(dpy, screen);
  1264. bh = dc.h = dc.font.height + 2;
  1265. lt[0] = &layouts[0];
  1266. lt[1] = &layouts[1 % LENGTH(layouts)];
  1267. updategeom();
  1268. /* init atoms */
  1269. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1270. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1271. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1272. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1273. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1274. /* init cursors */
  1275. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1276. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1277. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1278. /* init appearance */
  1279. dc.norm[ColBorder] = getcolor(normbordercolor);
  1280. dc.norm[ColBG] = getcolor(normbgcolor);
  1281. dc.norm[ColFG] = getcolor(normfgcolor);
  1282. dc.sel[ColBorder] = getcolor(selbordercolor);
  1283. dc.sel[ColBG] = getcolor(selbgcolor);
  1284. dc.sel[ColFG] = getcolor(selfgcolor);
  1285. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1286. dc.gc = XCreateGC(dpy, root, 0, NULL);
  1287. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1288. if(!dc.font.set)
  1289. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1290. /* init bars */
  1291. for(blw = i = 0; LENGTH(layouts) > 1 && i < LENGTH(layouts); i++) {
  1292. w = TEXTW(layouts[i].symbol);
  1293. blw = MAX(blw, w);
  1294. }
  1295. updatebars();
  1296. updatestatus();
  1297. /* EWMH support per view */
  1298. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1299. PropModeReplace, (unsigned char *) netatom, NetLast);
  1300. /* select for events */
  1301. wa.cursor = cursor[CurNormal];
  1302. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
  1303. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
  1304. |PropertyChangeMask;
  1305. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1306. XSelectInput(dpy, root, wa.event_mask);
  1307. grabkeys();
  1308. }
  1309. void
  1310. showhide(Client *c) {
  1311. if(!c)
  1312. return;
  1313. if(ISVISIBLE(c)) { /* show clients top down */
  1314. XMoveWindow(dpy, c->win, c->x, c->y);
  1315. if(!lt[c->mon->sellt]->arrange || c->isfloating)
  1316. resize(c, c->x, c->y, c->w, c->h);
  1317. showhide(c->snext);
  1318. }
  1319. else { /* hide clients bottom up */
  1320. showhide(c->snext);
  1321. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  1322. }
  1323. }
  1324. void
  1325. sigchld(int signal) {
  1326. while(0 < waitpid(-1, NULL, WNOHANG));
  1327. }
  1328. void
  1329. spawn(const Arg *arg) {
  1330. signal(SIGCHLD, sigchld);
  1331. if(fork() == 0) {
  1332. if(dpy)
  1333. close(ConnectionNumber(dpy));
  1334. setsid();
  1335. execvp(((char **)arg->v)[0], (char **)arg->v);
  1336. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1337. perror(" failed");
  1338. exit(0);
  1339. }
  1340. }
  1341. void
  1342. tag(const Arg *arg) {
  1343. if(selmon->sel && arg->ui & TAGMASK) {
  1344. selmon->sel->tags = arg->ui & TAGMASK;
  1345. arrange();
  1346. }
  1347. }
  1348. #ifdef XINERAMA
  1349. void
  1350. tagmon(const Arg *arg) {
  1351. unsigned int i;
  1352. Client *c;
  1353. Monitor *m;
  1354. if(!(c = selmon->sel))
  1355. return;
  1356. for(i = 0, m = mons; m; m = m->next, i++)
  1357. if(i == arg->ui) {
  1358. detach(c);
  1359. detachstack(c);
  1360. c->mon = m;
  1361. c->tags = m->seltags; /* assign tags of target monitor */
  1362. attach(c);
  1363. attachstack(c);
  1364. m->sel = c;
  1365. for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
  1366. selmon->sel = c;
  1367. arrange();
  1368. break;
  1369. }
  1370. }
  1371. #endif /* XINERAMA */
  1372. int
  1373. textnw(const char *text, unsigned int len) {
  1374. XRectangle r;
  1375. if(dc.font.set) {
  1376. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1377. return r.width;
  1378. }
  1379. return XTextWidth(dc.font.xfont, text, len);
  1380. }
  1381. void
  1382. tile(Monitor *m) {
  1383. int x, y, h, w, mw;
  1384. unsigned int i, n;
  1385. Client *c;
  1386. for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  1387. if(n == 0)
  1388. return;
  1389. /* master */
  1390. c = nexttiled(m->clients);
  1391. mw = m->mfact * m->ww;
  1392. resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw);
  1393. if(--n == 0)
  1394. return;
  1395. /* tile stack */
  1396. x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
  1397. y = m->wy;
  1398. w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
  1399. h = m->wh / n;
  1400. if(h < bh)
  1401. h = m->wh;
  1402. for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1403. resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
  1404. ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw));
  1405. if(h != m->wh)
  1406. y = c->y + HEIGHT(c);
  1407. }
  1408. }
  1409. void
  1410. togglebar(const Arg *arg) {
  1411. selmon->showbar = !selmon->showbar;
  1412. updatebarpos(selmon);
  1413. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1414. arrange();
  1415. }
  1416. void
  1417. togglefloating(const Arg *arg) {
  1418. if(!selmon->sel)
  1419. return;
  1420. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1421. if(selmon->sel->isfloating)
  1422. resize(selmon->sel, selmon->sel->x, selmon->sel->y, selmon->sel->w, selmon->sel->h);
  1423. arrange();
  1424. }
  1425. void
  1426. toggletag(const Arg *arg) {
  1427. unsigned int mask;
  1428. if(!selmon->sel)
  1429. return;
  1430. mask = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1431. if(mask) {
  1432. selmon->sel->tags = mask;
  1433. arrange();
  1434. }
  1435. }
  1436. void
  1437. toggleview(const Arg *arg) {
  1438. unsigned int mask = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1439. if(mask) {
  1440. selmon->tagset[selmon->seltags] = mask;
  1441. arrange();
  1442. }
  1443. }
  1444. void
  1445. unfocus(Client *c) {
  1446. if(!c)
  1447. return;
  1448. grabbuttons(c, False);
  1449. XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
  1450. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1451. }
  1452. void
  1453. unmanage(Client *c) {
  1454. XWindowChanges wc;
  1455. wc.border_width = c->oldbw;
  1456. /* The server grab construct avoids race conditions. */
  1457. XGrabServer(dpy);
  1458. XSetErrorHandler(xerrordummy);
  1459. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1460. detach(c);
  1461. detachstack(c);
  1462. if(c->mon->sel == c) {
  1463. /* TODO: consider separate the next code into a function or into detachstack? */
  1464. Client *tc;
  1465. for(tc = c->mon->stack; tc && !ISVISIBLE(tc); tc = tc->snext);
  1466. c->mon->sel = tc;
  1467. focus(NULL);
  1468. }
  1469. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1470. setclientstate(c, WithdrawnState);
  1471. free(c);
  1472. XSync(dpy, False);
  1473. XSetErrorHandler(xerror);
  1474. XUngrabServer(dpy);
  1475. arrange();
  1476. }
  1477. void
  1478. unmapnotify(XEvent *e) {
  1479. Client *c;
  1480. XUnmapEvent *ev = &e->xunmap;
  1481. if((c = getclient(ev->window)))
  1482. unmanage(c);
  1483. }
  1484. void
  1485. updatebars(void) {
  1486. Monitor *m;
  1487. XSetWindowAttributes wa;
  1488. wa.override_redirect = True;
  1489. wa.background_pixmap = ParentRelative;
  1490. wa.event_mask = ButtonPressMask|ExposureMask;
  1491. for(m = mons; m; m = m->next) {
  1492. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  1493. CopyFromParent, DefaultVisual(dpy, screen),
  1494. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1495. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1496. XMapRaised(dpy, m->barwin);
  1497. }
  1498. }
  1499. void
  1500. updatebarpos(Monitor *m) {
  1501. m->wy = m->my;
  1502. m->wh = m->mh;
  1503. if(m->showbar) {
  1504. m->wh -= bh;
  1505. m->by = m->topbar ? m->wy : m->wy + m->wh;
  1506. m->wy = m->topbar ? m->wy + bh : m->wy;
  1507. }
  1508. else
  1509. m->by = -bh;
  1510. }
  1511. void
  1512. updategeom(void) {
  1513. int i, di, n = 1, x, y;
  1514. unsigned int dui;
  1515. Client *c;
  1516. Monitor *newmons = NULL, *m, *tm;
  1517. Window dummy;
  1518. #ifdef XINULATOR
  1519. n = 2;
  1520. #elif defined(XINERAMA)
  1521. XineramaScreenInfo *info = NULL;
  1522. if(XineramaIsActive(dpy))
  1523. info = XineramaQueryScreens(dpy, &n);
  1524. #endif
  1525. /* allocate monitor(s) for the new geometry setup */
  1526. for(i = 0; i < n; i++) {
  1527. m = (Monitor *)malloc(sizeof(Monitor));
  1528. m->next = newmons;
  1529. newmons = m;
  1530. }
  1531. /* initialise monitor(s) */
  1532. #ifdef XINULATOR
  1533. if(1) {
  1534. m = newmons;
  1535. m->screen_number = 0;
  1536. m->wx = sx;
  1537. m->my = m->wy = sy;
  1538. m->ww = sw;
  1539. m->mh = m->wh = sh / 2;
  1540. m = newmons->next;
  1541. m->screen_number = 1;
  1542. m->wx = sx;
  1543. m->my = m->wy = sy + sh / 2;
  1544. m->ww = sw;
  1545. m->mh = m->wh = sh / 2;
  1546. }
  1547. else
  1548. #elif defined(XINERAMA)
  1549. if(XineramaIsActive(dpy)) {
  1550. for(i = 0, m = newmons; m; m = m->next, i++) {
  1551. m->screen_number = info[i].screen_number;
  1552. m->wx = info[i].x_org;
  1553. m->my = m->wy = info[i].y_org;
  1554. m->ww = info[i].width;
  1555. m->mh = m->wh = info[i].height;
  1556. }
  1557. XFree(info);
  1558. }
  1559. else
  1560. #endif
  1561. /* default monitor setup */
  1562. {
  1563. m->screen_number = 0;
  1564. m->wx = sx;
  1565. m->my = m->wy = sy;
  1566. m->ww = sw;
  1567. m->mh = m->wh = sh;
  1568. }
  1569. /* bar geometry setup */
  1570. for(m = newmons; m; m = m->next) {
  1571. /* TODO: consider removing the following values from config.h */
  1572. m->clients = NULL;
  1573. m->sel = NULL;
  1574. m->stack = NULL;
  1575. m->seltags = 0;
  1576. m->sellt = 0;
  1577. m->tagset[0] = m->tagset[1] = 1;
  1578. m->mfact = mfact;
  1579. m->showbar = showbar;
  1580. m->topbar = topbar;
  1581. updatebarpos(m);
  1582. }
  1583. /* reassign left over clients of disappeared monitors */
  1584. for(tm = mons; tm; tm = tm->next)
  1585. while(tm->clients) {
  1586. c = tm->clients;
  1587. tm->clients = c->next;
  1588. detachstack(c);
  1589. c->mon = newmons;
  1590. attach(c);
  1591. attachstack(c);
  1592. }
  1593. /* select focused monitor */
  1594. selmon = newmons;
  1595. if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui))
  1596. for(m = newmons; m; m = m->next)
  1597. if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh)) {
  1598. selmon = m;
  1599. break;
  1600. }
  1601. /* final assignment of new monitors */
  1602. cleanupmons();
  1603. mons = newmons;
  1604. }
  1605. void
  1606. updatenumlockmask(void) {
  1607. unsigned int i, j;
  1608. XModifierKeymap *modmap;
  1609. numlockmask = 0;
  1610. modmap = XGetModifierMapping(dpy);
  1611. for(i = 0; i < 8; i++)
  1612. for(j = 0; j < modmap->max_keypermod; j++)
  1613. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1614. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1615. numlockmask = (1 << i);
  1616. XFreeModifiermap(modmap);
  1617. }
  1618. void
  1619. updatesizehints(Client *c) {
  1620. long msize;
  1621. XSizeHints size;
  1622. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1623. /* size is uninitialized, ensure that size.flags aren't used */
  1624. size.flags = PSize;
  1625. if(size.flags & PBaseSize) {
  1626. c->basew = size.base_width;
  1627. c->baseh = size.base_height;
  1628. }
  1629. else if(size.flags & PMinSize) {
  1630. c->basew = size.min_width;
  1631. c->baseh = size.min_height;
  1632. }
  1633. else
  1634. c->basew = c->baseh = 0;
  1635. if(size.flags & PResizeInc) {
  1636. c->incw = size.width_inc;
  1637. c->inch = size.height_inc;
  1638. }
  1639. else
  1640. c->incw = c->inch = 0;
  1641. if(size.flags & PMaxSize) {
  1642. c->maxw = size.max_width;
  1643. c->maxh = size.max_height;
  1644. }
  1645. else
  1646. c->maxw = c->maxh = 0;
  1647. if(size.flags & PMinSize) {
  1648. c->minw = size.min_width;
  1649. c->minh = size.min_height;
  1650. }
  1651. else if(size.flags & PBaseSize) {
  1652. c->minw = size.base_width;
  1653. c->minh = size.base_height;
  1654. }
  1655. else
  1656. c->minw = c->minh = 0;
  1657. if(size.flags & PAspect) {
  1658. c->mina = (float)size.min_aspect.y / (float)size.min_aspect.x;
  1659. c->maxa = (float)size.max_aspect.x / (float)size.max_aspect.y;
  1660. }
  1661. else
  1662. c->maxa = c->mina = 0.0;
  1663. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1664. && c->maxw == c->minw && c->maxh == c->minh);
  1665. }
  1666. void
  1667. updatetitle(Client *c) {
  1668. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1669. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1670. }
  1671. void
  1672. updatestatus() {
  1673. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1674. strcpy(stext, "dwm-"VERSION);
  1675. drawbar(selmon);
  1676. }
  1677. void
  1678. updatewmhints(Client *c) {
  1679. XWMHints *wmh;
  1680. if((wmh = XGetWMHints(dpy, c->win))) {
  1681. if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  1682. wmh->flags &= ~XUrgencyHint;
  1683. XSetWMHints(dpy, c->win, wmh);
  1684. }
  1685. else
  1686. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1687. XFree(wmh);
  1688. }
  1689. }
  1690. void
  1691. view(const Arg *arg) {
  1692. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1693. return;
  1694. selmon->seltags ^= 1; /* toggle sel tagset */
  1695. if(arg->ui & TAGMASK)
  1696. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1697. arrange();
  1698. }
  1699. /* There's no way to check accesses to destroyed windows, thus those cases are
  1700. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1701. * default error handler, which may call exit. */
  1702. int
  1703. xerror(Display *dpy, XErrorEvent *ee) {
  1704. if(ee->error_code == BadWindow
  1705. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1706. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1707. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1708. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1709. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1710. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1711. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1712. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1713. return 0;
  1714. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1715. ee->request_code, ee->error_code);
  1716. return xerrorxlib(dpy, ee); /* may call exit */
  1717. }
  1718. int
  1719. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1720. return 0;
  1721. }
  1722. /* Startup Error handler to check if another window manager
  1723. * is already running. */
  1724. int
  1725. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1726. otherwm = True;
  1727. return -1;
  1728. }
  1729. void
  1730. zoom(const Arg *arg) {
  1731. Client *c = selmon->sel;
  1732. if(!lt[selmon->sellt]->arrange || lt[selmon->sellt]->arrange == monocle || (selmon->sel && selmon->sel->isfloating))
  1733. return;
  1734. if(c == nexttiled(selmon->clients))
  1735. if(!c || !(c = nexttiled(c->next)))
  1736. return;
  1737. detach(c);
  1738. attach(c);
  1739. focus(c);
  1740. arrange();
  1741. }
  1742. int
  1743. main(int argc, char *argv[]) {
  1744. if(argc == 2 && !strcmp("-v", argv[1]))
  1745. die("dwm-"VERSION", © 2006-2009 dwm engineers, see LICENSE for details\n");
  1746. else if(argc != 1)
  1747. die("usage: dwm [-v]\n");
  1748. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1749. fputs("warning: no locale support\n", stderr);
  1750. if(!(dpy = XOpenDisplay(NULL)))
  1751. die("dwm: cannot open display\n");
  1752. checkotherwm();
  1753. setup();
  1754. scan();
  1755. run();
  1756. cleanup();
  1757. XCloseDisplay(dpy);
  1758. return 0;
  1759. }