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.

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