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

1842 lines
45 KiB

17 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
16 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 & M->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. float mfact;
  112. int by, btx; /* bar geometry */
  113. int wx, wy, ww, wh; /* window area */
  114. unsigned int seltags;
  115. unsigned int sellt;
  116. unsigned int tagset[2];
  117. Bool showbar;
  118. Bool topbar;
  119. Window barwin;
  120. } Monitor;
  121. typedef struct {
  122. const char *symbol;
  123. void (*arrange)(Monitor *);
  124. } Layout;
  125. typedef struct {
  126. const char *class;
  127. const char *instance;
  128. const char *title;
  129. unsigned int tags;
  130. Bool isfloating;
  131. } Rule;
  132. /* function declarations */
  133. static void applyrules(Client *c);
  134. static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h);
  135. static void arrange(void);
  136. static void attach(Client *c);
  137. static void attachstack(Client *c);
  138. static void buttonpress(XEvent *e);
  139. static void checkotherwm(void);
  140. static void cleanup(void);
  141. static void clearurgent(Client *c);
  142. static void configure(Client *c);
  143. static void configurenotify(XEvent *e);
  144. static void configurerequest(XEvent *e);
  145. static void destroynotify(XEvent *e);
  146. static void detach(Client *c);
  147. static void detachstack(Client *c);
  148. static void die(const char *errstr, ...);
  149. static void drawbar(Monitor *m);
  150. static void drawbars();
  151. static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
  152. static void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
  153. static void enternotify(XEvent *e);
  154. static void expose(XEvent *e);
  155. static void focus(Client *c);
  156. static void focusin(XEvent *e);
  157. static void focusstack(const Arg *arg);
  158. static Client *getclient(Window w);
  159. static unsigned long getcolor(const char *colstr);
  160. static long getstate(Window w);
  161. static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  162. static void grabbuttons(Client *c, Bool focused);
  163. static void grabkeys(void);
  164. static void initfont(const char *fontstr);
  165. static Bool isprotodel(Client *c);
  166. static void keypress(XEvent *e);
  167. static void killclient(const Arg *arg);
  168. static void manage(Window w, XWindowAttributes *wa);
  169. static void mappingnotify(XEvent *e);
  170. static void maprequest(XEvent *e);
  171. static void monocle(Monitor *m);
  172. static void movemouse(const Arg *arg);
  173. static Client *nexttiled(Monitor *m, Client *c);
  174. static void propertynotify(XEvent *e);
  175. static void quit(const Arg *arg);
  176. static void resize(Client *c, int x, int y, int w, int h);
  177. static void resizemouse(const Arg *arg);
  178. static void restack(Monitor *m);
  179. static void run(void);
  180. static void scan(void);
  181. static void setclientstate(Client *c, long state);
  182. static void setlayout(const Arg *arg);
  183. static void setmfact(const Arg *arg);
  184. static void setup(void);
  185. static void showhide(Client *c);
  186. static void sigchld(int signal);
  187. static void spawn(const Arg *arg);
  188. static void tag(const Arg *arg);
  189. static int textnw(const char *text, unsigned int len);
  190. static void tile(Monitor *);
  191. static void togglebar(const Arg *arg);
  192. static void togglefloating(const Arg *arg);
  193. static void toggletag(const Arg *arg);
  194. static void toggleview(const Arg *arg);
  195. static void unmanage(Client *c);
  196. static void unmapnotify(XEvent *e);
  197. static void updategeom(void);
  198. static void updatenumlockmask(void);
  199. static void updatesizehints(Client *c);
  200. static void updatestatus(void);
  201. static void updatetitle(Client *c);
  202. static void updatewmhints(Client *c);
  203. static void view(const Arg *arg);
  204. static int xerror(Display *dpy, XErrorEvent *ee);
  205. static int xerrordummy(Display *dpy, XErrorEvent *ee);
  206. static int xerrorstart(Display *dpy, XErrorEvent *ee);
  207. static void zoom(const Arg *arg);
  208. #ifdef XINERAMA
  209. static void focusmon(const Arg *arg);
  210. static void tagmon(const Arg *arg);
  211. #endif /* XINERAMA */
  212. /* variables */
  213. static char stext[256];
  214. static int screen;
  215. static int sx, sy, sw, sh; /* X display screen geometry x, y, width, height */
  216. static int bh, blw = 0; /* bar geometry */
  217. static int (*xerrorxlib)(Display *, XErrorEvent *);
  218. static unsigned int numlockmask = 0;
  219. static void (*handler[LASTEvent]) (XEvent *) = {
  220. [ButtonPress] = buttonpress,
  221. [ConfigureRequest] = configurerequest,
  222. [ConfigureNotify] = configurenotify,
  223. [DestroyNotify] = destroynotify,
  224. [EnterNotify] = enternotify,
  225. [Expose] = expose,
  226. [FocusIn] = focusin,
  227. [KeyPress] = keypress,
  228. [MappingNotify] = mappingnotify,
  229. [MapRequest] = maprequest,
  230. [PropertyNotify] = propertynotify,
  231. [UnmapNotify] = unmapnotify
  232. };
  233. static Atom wmatom[WMLast], netatom[NetLast];
  234. static Bool otherwm;
  235. static Bool running = True;
  236. static Client *clients = NULL;
  237. static Client *sel = NULL;
  238. static Client *stack = NULL;
  239. static Cursor cursor[CurLast];
  240. static Display *dpy;
  241. static DC dc;
  242. static Layout *lt[] = { NULL, NULL };
  243. static Monitor *mon = NULL, *selmon = NULL;
  244. static unsigned int nmons = 0;
  245. static Window root;
  246. /* configuration, allows nested code to access above variables */
  247. #include "config.h"
  248. /* compile-time check if all tags fit into an unsigned int bit array. */
  249. struct NumTags { char limitexceeded[sizeof(unsigned int) * 8 < LENGTH(tags) ? -1 : 1]; };
  250. /* function implementations */
  251. void
  252. applyrules(Client *c) {
  253. unsigned int i;
  254. Rule *r;
  255. XClassHint ch = { 0 };
  256. /* rule matching */
  257. c->isfloating = c->tags = 0;
  258. if(XGetClassHint(dpy, c->win, &ch)) {
  259. for(i = 0; i < LENGTH(rules); i++) {
  260. r = &rules[i];
  261. if((!r->title || strstr(c->name, r->title))
  262. && (!r->class || (ch.res_class && strstr(ch.res_class, r->class)))
  263. && (!r->instance || (ch.res_name && strstr(ch.res_name, r->instance)))) {
  264. c->isfloating = r->isfloating;
  265. c->tags |= r->tags;
  266. }
  267. }
  268. if(ch.res_class)
  269. XFree(ch.res_class);
  270. if(ch.res_name)
  271. XFree(ch.res_name);
  272. }
  273. c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : mon[c->mon].tagset[mon[c->mon].seltags];
  274. }
  275. Bool
  276. applysizehints(Client *c, int *x, int *y, int *w, int *h) {
  277. Bool baseismin;
  278. /* set minimum possible */
  279. *w = MAX(1, *w);
  280. *h = MAX(1, *h);
  281. if(*x > sx + sw)
  282. *x = sw - WIDTH(c);
  283. if(*y > sy + sh)
  284. *y = sh - HEIGHT(c);
  285. if(*x + *w + 2 * c->bw < sx)
  286. *x = sx;
  287. if(*y + *h + 2 * c->bw < sy)
  288. *y = sy;
  289. if(*h < bh)
  290. *h = bh;
  291. if(*w < bh)
  292. *w = bh;
  293. if(resizehints || c->isfloating) {
  294. /* see last two sentences in ICCCM 4.1.2.3 */
  295. baseismin = c->basew == c->minw && c->baseh == c->minh;
  296. if(!baseismin) { /* temporarily remove base dimensions */
  297. *w -= c->basew;
  298. *h -= c->baseh;
  299. }
  300. /* adjust for aspect limits */
  301. if(c->mina > 0 && c->maxa > 0) {
  302. if(c->maxa < (float)*w / *h)
  303. *w = *h * c->maxa;
  304. else if(c->mina < (float)*h / *w)
  305. *h = *w * c->mina;
  306. }
  307. if(baseismin) { /* increment calculation requires this */
  308. *w -= c->basew;
  309. *h -= c->baseh;
  310. }
  311. /* adjust for increment value */
  312. if(c->incw)
  313. *w -= *w % c->incw;
  314. if(c->inch)
  315. *h -= *h % c->inch;
  316. /* restore base dimensions */
  317. *w += c->basew;
  318. *h += c->baseh;
  319. *w = MAX(*w, c->minw);
  320. *h = MAX(*h, c->minh);
  321. if(c->maxw)
  322. *w = MIN(*w, c->maxw);
  323. if(c->maxh)
  324. *h = MIN(*h, c->maxh);
  325. }
  326. return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
  327. }
  328. void
  329. arrange(void) {
  330. unsigned int i;
  331. showhide(stack);
  332. focus(NULL);
  333. for(i = 0; i < nmons; i++) {
  334. if(lt[mon[i].sellt]->arrange)
  335. lt[mon[i].sellt]->arrange(&mon[i]);
  336. restack(&mon[i]);
  337. }
  338. }
  339. void
  340. attach(Client *c) {
  341. c->next = clients;
  342. clients = c;
  343. }
  344. void
  345. attachstack(Client *c) {
  346. c->snext = stack;
  347. stack = c;
  348. }
  349. void
  350. buttonpress(XEvent *e) {
  351. unsigned int i, x, click;
  352. Arg arg = {0};
  353. Client *c;
  354. XButtonPressedEvent *ev = &e->xbutton;
  355. click = ClkRootWin;
  356. if(ev->window == selmon->barwin) {
  357. i = 0;
  358. x = selmon->btx;
  359. do
  360. x += TEXTW(tags[i]);
  361. while(ev->x >= x && ++i < LENGTH(tags));
  362. if(i < LENGTH(tags)) {
  363. click = ClkTagBar;
  364. arg.ui = 1 << i;
  365. }
  366. else if(ev->x < x + blw)
  367. click = ClkLtSymbol;
  368. else if(ev->x > selmon->wx + selmon->ww - TEXTW(stext))
  369. click = ClkStatusText;
  370. else
  371. click = ClkWinTitle;
  372. }
  373. else if((c = getclient(ev->window))) {
  374. focus(c);
  375. click = ClkClientWin;
  376. }
  377. for(i = 0; i < LENGTH(buttons); i++)
  378. if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
  379. && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
  380. buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
  381. }
  382. void
  383. checkotherwm(void) {
  384. otherwm = False;
  385. xerrorxlib = XSetErrorHandler(xerrorstart);
  386. /* this causes an error if some other window manager is running */
  387. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  388. XSync(dpy, False);
  389. if(otherwm)
  390. die("dwm: another window manager is already running\n");
  391. XSetErrorHandler(xerror);
  392. XSync(dpy, False);
  393. }
  394. void
  395. cleanup(void) {
  396. unsigned int i;
  397. Arg a = {.ui = ~0};
  398. Layout foo = { "", NULL };
  399. view(&a);
  400. lt[selmon->sellt] = &foo;
  401. while(stack)
  402. unmanage(stack);
  403. if(dc.font.set)
  404. XFreeFontSet(dpy, dc.font.set);
  405. else
  406. XFreeFont(dpy, dc.font.xfont);
  407. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  408. XFreePixmap(dpy, dc.drawable);
  409. XFreeGC(dpy, dc.gc);
  410. XFreeCursor(dpy, cursor[CurNormal]);
  411. XFreeCursor(dpy, cursor[CurResize]);
  412. XFreeCursor(dpy, cursor[CurMove]);
  413. for(i = 0; i < nmons; i++)
  414. XDestroyWindow(dpy, mon[i].barwin);
  415. free(mon);
  416. XSync(dpy, False);
  417. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  418. }
  419. void
  420. clearurgent(Client *c) {
  421. XWMHints *wmh;
  422. c->isurgent = False;
  423. if(!(wmh = XGetWMHints(dpy, c->win)))
  424. return;
  425. wmh->flags &= ~XUrgencyHint;
  426. XSetWMHints(dpy, c->win, wmh);
  427. XFree(wmh);
  428. }
  429. void
  430. configure(Client *c) {
  431. XConfigureEvent ce;
  432. ce.type = ConfigureNotify;
  433. ce.display = dpy;
  434. ce.event = c->win;
  435. ce.window = c->win;
  436. ce.x = c->x;
  437. ce.y = c->y;
  438. ce.width = c->w;
  439. ce.height = c->h;
  440. ce.border_width = c->bw;
  441. ce.above = None;
  442. ce.override_redirect = False;
  443. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  444. }
  445. void
  446. configurenotify(XEvent *e) {
  447. unsigned int i;
  448. XConfigureEvent *ev = &e->xconfigure;
  449. if(ev->window == root && (ev->width != sw || ev->height != sh)) {
  450. sw = ev->width;
  451. sh = ev->height;
  452. updategeom();
  453. if(dc.drawable != 0)
  454. XFreePixmap(dpy, dc.drawable);
  455. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  456. for(i = 0; i < nmons; i++)
  457. XMoveResizeWindow(dpy, mon[i].barwin, mon[i].wx, mon[i].by, mon[i].ww, bh);
  458. arrange();
  459. }
  460. }
  461. void
  462. configurerequest(XEvent *e) {
  463. Client *c;
  464. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  465. XWindowChanges wc;
  466. if((c = getclient(ev->window))) {
  467. if(ev->value_mask & CWBorderWidth)
  468. c->bw = ev->border_width;
  469. else if(c->isfloating || !lt[selmon->sellt]->arrange) {
  470. if(ev->value_mask & CWX)
  471. c->x = sx + ev->x;
  472. if(ev->value_mask & CWY)
  473. c->y = sy + ev->y;
  474. if(ev->value_mask & CWWidth)
  475. c->w = ev->width;
  476. if(ev->value_mask & CWHeight)
  477. c->h = ev->height;
  478. if((c->x - sx + c->w) > sw && c->isfloating)
  479. c->x = sx + (sw / 2 - c->w / 2); /* center in x direction */
  480. if((c->y - sy + c->h) > sh && c->isfloating)
  481. c->y = sy + (sh / 2 - c->h / 2); /* center in y direction */
  482. if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
  483. configure(c);
  484. if(ISVISIBLE((&mon[c->mon]), c))
  485. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  486. }
  487. else
  488. configure(c);
  489. }
  490. else {
  491. wc.x = ev->x;
  492. wc.y = ev->y;
  493. wc.width = ev->width;
  494. wc.height = ev->height;
  495. wc.border_width = ev->border_width;
  496. wc.sibling = ev->above;
  497. wc.stack_mode = ev->detail;
  498. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  499. }
  500. XSync(dpy, False);
  501. }
  502. void
  503. destroynotify(XEvent *e) {
  504. Client *c;
  505. XDestroyWindowEvent *ev = &e->xdestroywindow;
  506. if((c = getclient(ev->window)))
  507. unmanage(c);
  508. }
  509. void
  510. detach(Client *c) {
  511. Client **tc;
  512. for(tc = &clients; *tc && *tc != c; tc = &(*tc)->next);
  513. *tc = c->next;
  514. }
  515. void
  516. detachstack(Client *c) {
  517. Client **tc;
  518. for(tc = &stack; *tc && *tc != c; tc = &(*tc)->snext);
  519. *tc = c->snext;
  520. }
  521. void
  522. die(const char *errstr, ...) {
  523. va_list ap;
  524. va_start(ap, errstr);
  525. vfprintf(stderr, errstr, ap);
  526. va_end(ap);
  527. exit(EXIT_FAILURE);
  528. }
  529. void
  530. drawbar(Monitor *m) {
  531. int x;
  532. unsigned int i, occ = 0, urg = 0;
  533. unsigned long *col;
  534. Client *c;
  535. for(c = clients; c; c = c->next) {
  536. if(m == &mon[c->mon]) {
  537. occ |= c->tags;
  538. if(c->isurgent)
  539. urg |= c->tags;
  540. }
  541. }
  542. dc.x = 0;
  543. #ifdef XINERAMA
  544. {
  545. dc.w = TEXTW(m->symbol);
  546. drawtext(m->symbol, selmon == m ? dc.sel : dc.norm, False);
  547. dc.x += dc.w;
  548. }
  549. #endif /* XINERAMA */
  550. m->btx = dc.x;
  551. for(i = 0; i < LENGTH(tags); i++) {
  552. dc.w = TEXTW(tags[i]);
  553. col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
  554. drawtext(tags[i], col, urg & 1 << i);
  555. drawsquare(m == selmon && sel && sel->tags & 1 << i,
  556. occ & 1 << i, urg & 1 << i, col);
  557. dc.x += dc.w;
  558. }
  559. if(blw > 0) {
  560. dc.w = blw;
  561. drawtext(lt[m->sellt]->symbol, dc.norm, False);
  562. x = dc.x + dc.w;
  563. }
  564. else
  565. x = dc.x;
  566. if(m == selmon) {
  567. dc.w = TEXTW(stext);
  568. dc.x = m->ww - dc.w;
  569. if(dc.x < x) {
  570. dc.x = x;
  571. dc.w = m->ww - x;
  572. }
  573. drawtext(stext, dc.norm, False);
  574. if((dc.w = dc.x - x) > bh) {
  575. dc.x = x;
  576. if(sel) {
  577. drawtext(sel->name, dc.sel, False);
  578. drawsquare(sel->isfixed, sel->isfloating, False, dc.sel);
  579. }
  580. else
  581. drawtext(NULL, dc.norm, False);
  582. }
  583. }
  584. else {
  585. dc.x = x;
  586. dc.w = m->ww - x;
  587. drawtext(NULL, dc.norm, False);
  588. }
  589. XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
  590. XSync(dpy, False);
  591. }
  592. void
  593. drawbars() {
  594. unsigned int i;
  595. for(i = 0; i < nmons; i++)
  596. drawbar(&mon[i]);
  597. }
  598. void
  599. drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  600. int x;
  601. XGCValues gcv;
  602. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  603. gcv.foreground = col[invert ? ColBG : ColFG];
  604. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  605. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  606. r.x = dc.x + 1;
  607. r.y = dc.y + 1;
  608. if(filled) {
  609. r.width = r.height = x + 1;
  610. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  611. }
  612. else if(empty) {
  613. r.width = r.height = x;
  614. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  615. }
  616. }
  617. void
  618. drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
  619. char buf[256];
  620. int i, x, y, h, len, olen;
  621. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  622. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  623. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  624. if(!text)
  625. return;
  626. olen = strlen(text);
  627. h = dc.font.ascent + dc.font.descent;
  628. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  629. x = dc.x + (h / 2);
  630. /* shorten text if necessary */
  631. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  632. if(!len)
  633. return;
  634. memcpy(buf, text, len);
  635. if(len < olen)
  636. for(i = len; i && i > len - 3; buf[--i] = '.');
  637. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  638. if(dc.font.set)
  639. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  640. else
  641. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  642. }
  643. void
  644. enternotify(XEvent *e) {
  645. Client *c;
  646. XCrossingEvent *ev = &e->xcrossing;
  647. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  648. return;
  649. if((c = getclient(ev->window)))
  650. focus(c);
  651. else
  652. focus(NULL);
  653. }
  654. void
  655. expose(XEvent *e) {
  656. unsigned int i;
  657. XExposeEvent *ev = &e->xexpose;
  658. if(ev->count == 0)
  659. for(i = 0; i < nmons; i++)
  660. if(ev->window == mon[i].barwin) {
  661. drawbar(&mon[i]);
  662. break;
  663. }
  664. }
  665. void
  666. focus(Client *c) {
  667. if(!c || !ISVISIBLE((&mon[c->mon]), c))
  668. for(c = stack; c && !ISVISIBLE(selmon, c); c = c->snext);
  669. if(sel && sel != c) {
  670. grabbuttons(sel, False);
  671. XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
  672. }
  673. if(c) {
  674. if(c->isurgent)
  675. clearurgent(c);
  676. detachstack(c);
  677. attachstack(c);
  678. grabbuttons(c, True);
  679. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  680. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  681. }
  682. else
  683. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  684. sel = c;
  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 + selmon->mfact : arg->f - 1.0;
  1203. if(f < 0.1 || f > 0.9)
  1204. return;
  1205. selmon->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[mon[c->mon].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 = m->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 = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1389. if(mask) {
  1390. selmon->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 n;
  1425. unsigned int i = 0;
  1426. Client *c;
  1427. XineramaScreenInfo *info = NULL;
  1428. /* window area geometry */
  1429. if(XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
  1430. if(n != nmons) {
  1431. for(c = clients; c; c = c->next)
  1432. if(c->mon >= n)
  1433. c->mon = n - 1;
  1434. if(!(mon = (Monitor *)realloc(mon, sizeof(Monitor) * n)))
  1435. die("fatal: could not realloc() %u bytes\n", sizeof(Monitor) * nmons);
  1436. selmon = NULL;
  1437. }
  1438. for(i = 0; i < n ; i++) {
  1439. /* TODO: consider re-using XineramaScreenInfo */
  1440. mon[i].symbol[0] = '[';
  1441. mon[i].symbol[1] = '0' + info[i].screen_number;
  1442. mon[i].symbol[2] = ']';
  1443. mon[i].symbol[3] = 0;
  1444. if(!selmon) { /* not initialised yet */
  1445. mon[i].mfact = mfact;
  1446. mon[i].showbar = showbar;
  1447. mon[i].topbar = topbar;
  1448. mon[i].tagset[0] = mon[i].tagset[1] = 1;
  1449. }
  1450. mon[i].wx = info[i].x_org;
  1451. mon[i].wy = mon[i].showbar && mon[i].topbar ? info[i].y_org + bh : info[i].y_org;
  1452. mon[i].ww = info[i].width;
  1453. mon[i].wh = mon[i].showbar ? info[i].height - bh : info[i].height;
  1454. mon[i].seltags = 0;
  1455. mon[i].sellt = 0;
  1456. if(mon[i].showbar)
  1457. mon[i].by = mon[i].topbar ? info[i].y_org : mon[i].wy + mon[i].wh;
  1458. else
  1459. mon[i].by = -bh;
  1460. }
  1461. nmons = (unsigned int)n;
  1462. if(!selmon) {
  1463. selmon = &mon[0];
  1464. int di, x, y;
  1465. unsigned int dui;
  1466. Window dummy;
  1467. if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui))
  1468. for(i = 0; i < nmons; i++)
  1469. if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height)) {
  1470. selmon = &mon[i];
  1471. break;
  1472. }
  1473. }
  1474. XFree(info);
  1475. }
  1476. else
  1477. #endif /* XINERAMA */
  1478. {
  1479. if(!mon) {
  1480. nmons = 1;
  1481. if(!(mon = (Monitor *)malloc(sizeof(Monitor))))
  1482. die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
  1483. }
  1484. if(!selmon) {
  1485. mon[0].symbol[0] = '[';
  1486. mon[0].symbol[1] = '0';
  1487. mon[0].symbol[2] = ']';
  1488. mon[0].symbol[3] = 0;
  1489. mon[0].mfact = mfact;
  1490. mon[0].showbar = showbar;
  1491. mon[0].topbar = topbar;
  1492. mon[0].tagset[0] = mon[0].tagset[1] = 1;
  1493. }
  1494. mon[0].wx = sx;
  1495. mon[0].wy = mon[0].showbar && mon[0].topbar ? sy + bh : sy;
  1496. mon[0].ww = sw;
  1497. mon[0].wh = mon[0].showbar ? sh - bh : sh;
  1498. mon[0].seltags = 0;
  1499. mon[0].sellt = 0;
  1500. if(mon[0].showbar)
  1501. mon[0].by = mon[0].topbar ? sy : mon[0].wy + mon[0].wh;
  1502. else
  1503. mon[0].by = -bh;
  1504. selmon = &mon[0];
  1505. }
  1506. }
  1507. void
  1508. updatenumlockmask(void) {
  1509. unsigned int i, j;
  1510. XModifierKeymap *modmap;
  1511. numlockmask = 0;
  1512. modmap = XGetModifierMapping(dpy);
  1513. for(i = 0; i < 8; i++)
  1514. for(j = 0; j < modmap->max_keypermod; j++)
  1515. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1516. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1517. numlockmask = (1 << i);
  1518. XFreeModifiermap(modmap);
  1519. }
  1520. void
  1521. updatesizehints(Client *c) {
  1522. long msize;
  1523. XSizeHints size;
  1524. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1525. /* size is uninitialized, ensure that size.flags aren't used */
  1526. size.flags = PSize;
  1527. if(size.flags & PBaseSize) {
  1528. c->basew = size.base_width;
  1529. c->baseh = size.base_height;
  1530. }
  1531. else if(size.flags & PMinSize) {
  1532. c->basew = size.min_width;
  1533. c->baseh = size.min_height;
  1534. }
  1535. else
  1536. c->basew = c->baseh = 0;
  1537. if(size.flags & PResizeInc) {
  1538. c->incw = size.width_inc;
  1539. c->inch = size.height_inc;
  1540. }
  1541. else
  1542. c->incw = c->inch = 0;
  1543. if(size.flags & PMaxSize) {
  1544. c->maxw = size.max_width;
  1545. c->maxh = size.max_height;
  1546. }
  1547. else
  1548. c->maxw = c->maxh = 0;
  1549. if(size.flags & PMinSize) {
  1550. c->minw = size.min_width;
  1551. c->minh = size.min_height;
  1552. }
  1553. else if(size.flags & PBaseSize) {
  1554. c->minw = size.base_width;
  1555. c->minh = size.base_height;
  1556. }
  1557. else
  1558. c->minw = c->minh = 0;
  1559. if(size.flags & PAspect) {
  1560. c->mina = (float)size.min_aspect.y / (float)size.min_aspect.x;
  1561. c->maxa = (float)size.max_aspect.x / (float)size.max_aspect.y;
  1562. }
  1563. else
  1564. c->maxa = c->mina = 0.0;
  1565. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1566. && c->maxw == c->minw && c->maxh == c->minh);
  1567. }
  1568. void
  1569. updatetitle(Client *c) {
  1570. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1571. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1572. }
  1573. void
  1574. updatestatus() {
  1575. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1576. strcpy(stext, "dwm-"VERSION);
  1577. drawbar(selmon);
  1578. }
  1579. void
  1580. updatewmhints(Client *c) {
  1581. XWMHints *wmh;
  1582. if((wmh = XGetWMHints(dpy, c->win))) {
  1583. if(c == sel && wmh->flags & XUrgencyHint) {
  1584. wmh->flags &= ~XUrgencyHint;
  1585. XSetWMHints(dpy, c->win, wmh);
  1586. }
  1587. else
  1588. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1589. XFree(wmh);
  1590. }
  1591. }
  1592. void
  1593. view(const Arg *arg) {
  1594. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1595. return;
  1596. selmon->seltags ^= 1; /* toggle sel tagset */
  1597. if(arg->ui & TAGMASK)
  1598. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1599. arrange();
  1600. }
  1601. /* There's no way to check accesses to destroyed windows, thus those cases are
  1602. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1603. * default error handler, which may call exit. */
  1604. int
  1605. xerror(Display *dpy, XErrorEvent *ee) {
  1606. if(ee->error_code == BadWindow
  1607. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1608. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1609. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1610. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1611. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1612. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1613. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1614. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1615. return 0;
  1616. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1617. ee->request_code, ee->error_code);
  1618. return xerrorxlib(dpy, ee); /* may call exit */
  1619. }
  1620. int
  1621. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1622. return 0;
  1623. }
  1624. /* Startup Error handler to check if another window manager
  1625. * is already running. */
  1626. int
  1627. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1628. otherwm = True;
  1629. return -1;
  1630. }
  1631. void
  1632. zoom(const Arg *arg) {
  1633. Client *c = sel;
  1634. if(!lt[selmon->sellt]->arrange || lt[selmon->sellt]->arrange == monocle || (sel && sel->isfloating))
  1635. return;
  1636. if(c == nexttiled(selmon, clients))
  1637. if(!c || !(c = nexttiled(selmon, c->next)))
  1638. return;
  1639. detach(c);
  1640. attach(c);
  1641. focus(c);
  1642. arrange();
  1643. }
  1644. int
  1645. main(int argc, char *argv[]) {
  1646. if(argc == 2 && !strcmp("-v", argv[1]))
  1647. die("dwm-"VERSION", © 2006-2009 dwm engineers, see LICENSE for details\n");
  1648. else if(argc != 1)
  1649. die("usage: dwm [-v]\n");
  1650. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1651. fputs("warning: no locale support\n", stderr);
  1652. if(!(dpy = XOpenDisplay(NULL)))
  1653. die("dwm: cannot open display\n");
  1654. checkotherwm();
  1655. setup();
  1656. scan();
  1657. run();
  1658. cleanup();
  1659. XCloseDisplay(dpy);
  1660. return 0;
  1661. }