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.

1820 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
16 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
16 years ago
17 years ago
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(x) (x->tags & tagset[mon[x->mon].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, DisplayWidth(dpy, screen), 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(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.w = m->ww - x;
  584. drawtext(NULL, dc.norm, False);
  585. }
  586. XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
  587. XSync(dpy, False);
  588. }
  589. void
  590. drawbars() {
  591. unsigned int i;
  592. for(i = 0; i < nmons; i++)
  593. drawbar(&mon[i]);
  594. }
  595. void
  596. drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  597. int x;
  598. XGCValues gcv;
  599. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  600. gcv.foreground = col[invert ? ColBG : ColFG];
  601. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  602. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  603. r.x = dc.x + 1;
  604. r.y = dc.y + 1;
  605. if(filled) {
  606. r.width = r.height = x + 1;
  607. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  608. }
  609. else if(empty) {
  610. r.width = r.height = x;
  611. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  612. }
  613. }
  614. void
  615. drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
  616. char buf[256];
  617. int i, x, y, h, len, olen;
  618. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  619. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  620. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  621. if(!text)
  622. return;
  623. olen = strlen(text);
  624. h = dc.font.ascent + dc.font.descent;
  625. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  626. x = dc.x + (h / 2);
  627. /* shorten text if necessary */
  628. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  629. if(!len)
  630. return;
  631. memcpy(buf, text, len);
  632. if(len < olen)
  633. for(i = len; i && i > len - 3; buf[--i] = '.');
  634. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  635. if(dc.font.set)
  636. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  637. else
  638. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  639. }
  640. void
  641. enternotify(XEvent *e) {
  642. Client *c;
  643. XCrossingEvent *ev = &e->xcrossing;
  644. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  645. return;
  646. if((c = getclient(ev->window)))
  647. focus(c);
  648. else
  649. focus(NULL);
  650. }
  651. void
  652. expose(XEvent *e) {
  653. unsigned int i;
  654. XExposeEvent *ev = &e->xexpose;
  655. for(i = 0; i < nmons; i++)
  656. if(ev->count == 0 && (ev->window == mon[i].barwin)) {
  657. drawbar(&mon[i]);
  658. break;
  659. }
  660. }
  661. void
  662. focus(Client *c) {
  663. if(!c || !ISVISIBLE(c))
  664. for(c = stack; c && !ISVISIBLE(c); c = c->snext);
  665. if(sel && sel != c) {
  666. grabbuttons(sel, False);
  667. XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
  668. }
  669. if(c) {
  670. if(c->isurgent)
  671. clearurgent(c);
  672. detachstack(c);
  673. attachstack(c);
  674. grabbuttons(c, True);
  675. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  676. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  677. }
  678. else
  679. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  680. sel = c;
  681. if(c)
  682. selmon = &mon[c->mon];
  683. drawbars();
  684. }
  685. void
  686. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  687. XFocusChangeEvent *ev = &e->xfocus;
  688. if(sel && ev->window != sel->win)
  689. XSetInputFocus(dpy, sel->win, RevertToPointerRoot, CurrentTime);
  690. }
  691. #ifdef XINERAMA
  692. void
  693. focusmon(const Arg *arg) {
  694. if(arg->ui >= nmons)
  695. return;
  696. selmon = &mon[arg->ui];
  697. focus(NULL);
  698. drawbars();
  699. }
  700. #endif /* XINERAMA */
  701. void
  702. focusstack(const Arg *arg) {
  703. Client *c = NULL, *i;
  704. if(!sel)
  705. return;
  706. if(arg->i > 0) {
  707. for(c = sel->next; c && !ISVISIBLE(c); c = c->next);
  708. if(!c)
  709. for(c = clients; c && !ISVISIBLE(c); c = c->next);
  710. }
  711. else {
  712. for(i = clients; i != sel; i = i->next)
  713. if(ISVISIBLE(i))
  714. c = i;
  715. if(!c)
  716. for(; i; i = i->next)
  717. if(ISVISIBLE(i))
  718. c = i;
  719. }
  720. if(c) {
  721. focus(c);
  722. restack(selmon);
  723. }
  724. }
  725. Client *
  726. getclient(Window w) {
  727. Client *c;
  728. for(c = clients; c && c->win != w; c = c->next);
  729. return c;
  730. }
  731. unsigned long
  732. getcolor(const char *colstr) {
  733. Colormap cmap = DefaultColormap(dpy, screen);
  734. XColor color;
  735. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  736. die("error, cannot allocate color '%s'\n", colstr);
  737. return color.pixel;
  738. }
  739. long
  740. getstate(Window w) {
  741. int format, status;
  742. long result = -1;
  743. unsigned char *p = NULL;
  744. unsigned long n, extra;
  745. Atom real;
  746. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  747. &real, &format, &n, &extra, (unsigned char **)&p);
  748. if(status != Success)
  749. return -1;
  750. if(n != 0)
  751. result = *p;
  752. XFree(p);
  753. return result;
  754. }
  755. Bool
  756. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  757. char **list = NULL;
  758. int n;
  759. XTextProperty name;
  760. if(!text || size == 0)
  761. return False;
  762. text[0] = '\0';
  763. XGetTextProperty(dpy, w, &name, atom);
  764. if(!name.nitems)
  765. return False;
  766. if(name.encoding == XA_STRING)
  767. strncpy(text, (char *)name.value, size - 1);
  768. else {
  769. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  770. && n > 0 && *list) {
  771. strncpy(text, *list, size - 1);
  772. XFreeStringList(list);
  773. }
  774. }
  775. text[size - 1] = '\0';
  776. XFree(name.value);
  777. return True;
  778. }
  779. void
  780. grabbuttons(Client *c, Bool focused) {
  781. updatenumlockmask();
  782. {
  783. unsigned int i, j;
  784. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  785. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  786. if(focused) {
  787. for(i = 0; i < LENGTH(buttons); i++)
  788. if(buttons[i].click == ClkClientWin)
  789. for(j = 0; j < LENGTH(modifiers); j++)
  790. XGrabButton(dpy, buttons[i].button,
  791. buttons[i].mask | modifiers[j],
  792. c->win, False, BUTTONMASK,
  793. GrabModeAsync, GrabModeSync, None, None);
  794. } else
  795. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  796. BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  797. }
  798. }
  799. void
  800. grabkeys(void) {
  801. updatenumlockmask();
  802. { /* grab keys */
  803. unsigned int i, j;
  804. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  805. KeyCode code;
  806. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  807. for(i = 0; i < LENGTH(keys); i++) {
  808. if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  809. for(j = 0; j < LENGTH(modifiers); j++)
  810. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  811. True, GrabModeAsync, GrabModeAsync);
  812. }
  813. }
  814. }
  815. void
  816. initfont(const char *fontstr) {
  817. char *def, **missing;
  818. int i, n;
  819. missing = NULL;
  820. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  821. if(missing) {
  822. while(n--)
  823. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  824. XFreeStringList(missing);
  825. }
  826. if(dc.font.set) {
  827. XFontSetExtents *font_extents;
  828. XFontStruct **xfonts;
  829. char **font_names;
  830. dc.font.ascent = dc.font.descent = 0;
  831. font_extents = XExtentsOfFontSet(dc.font.set);
  832. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  833. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  834. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  835. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  836. xfonts++;
  837. }
  838. }
  839. else {
  840. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  841. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  842. die("error, cannot load font: '%s'\n", fontstr);
  843. dc.font.ascent = dc.font.xfont->ascent;
  844. dc.font.descent = dc.font.xfont->descent;
  845. }
  846. dc.font.height = dc.font.ascent + dc.font.descent;
  847. }
  848. Bool
  849. isprotodel(Client *c) {
  850. int i, n;
  851. Atom *protocols;
  852. Bool ret = False;
  853. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  854. for(i = 0; !ret && i < n; i++)
  855. if(protocols[i] == wmatom[WMDelete])
  856. ret = True;
  857. XFree(protocols);
  858. }
  859. return ret;
  860. }
  861. void
  862. keypress(XEvent *e) {
  863. unsigned int i;
  864. KeySym keysym;
  865. XKeyEvent *ev;
  866. ev = &e->xkey;
  867. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  868. for(i = 0; i < LENGTH(keys); i++)
  869. if(keysym == keys[i].keysym
  870. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  871. && keys[i].func)
  872. keys[i].func(&(keys[i].arg));
  873. }
  874. void
  875. killclient(const Arg *arg) {
  876. XEvent ev;
  877. if(!sel)
  878. return;
  879. if(isprotodel(sel)) {
  880. ev.type = ClientMessage;
  881. ev.xclient.window = sel->win;
  882. ev.xclient.message_type = wmatom[WMProtocols];
  883. ev.xclient.format = 32;
  884. ev.xclient.data.l[0] = wmatom[WMDelete];
  885. ev.xclient.data.l[1] = CurrentTime;
  886. XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
  887. }
  888. else
  889. XKillClient(dpy, sel->win);
  890. }
  891. void
  892. manage(Window w, XWindowAttributes *wa) {
  893. static Client cz;
  894. Client *c, *t = NULL;
  895. Window trans = None;
  896. XWindowChanges wc;
  897. if(!(c = malloc(sizeof(Client))))
  898. die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  899. *c = cz;
  900. c->win = w;
  901. for(c->mon = 0; selmon != &mon[c->mon]; c->mon++);
  902. /* geometry */
  903. c->x = wa->x;
  904. c->y = wa->y;
  905. c->w = wa->width;
  906. c->h = wa->height;
  907. c->oldbw = wa->border_width;
  908. if(c->w == sw && c->h == sh) {
  909. c->x = sx;
  910. c->y = sy;
  911. c->bw = 0;
  912. }
  913. else {
  914. if(c->x + WIDTH(c) > sx + sw)
  915. c->x = sx + sw - WIDTH(c);
  916. if(c->y + HEIGHT(c) > sy + sh)
  917. c->y = sy + sh - HEIGHT(c);
  918. c->x = MAX(c->x, sx);
  919. /* only fix client y-offset, if the client center might cover the bar */
  920. c->y = MAX(c->y, ((selmon->by == 0) && (c->x + (c->w / 2) >= selmon->wx)
  921. && (c->x + (c->w / 2) < selmon->wx + selmon->ww)) ? bh : sy);
  922. c->bw = borderpx;
  923. }
  924. wc.border_width = c->bw;
  925. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  926. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  927. configure(c); /* propagates border_width, if size doesn't change */
  928. updatesizehints(c);
  929. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  930. grabbuttons(c, False);
  931. updatetitle(c);
  932. if(XGetTransientForHint(dpy, w, &trans))
  933. t = getclient(trans);
  934. if(t)
  935. c->tags = t->tags;
  936. else
  937. applyrules(c);
  938. if(!c->isfloating)
  939. c->isfloating = trans != None || c->isfixed;
  940. if(c->isfloating)
  941. XRaiseWindow(dpy, c->win);
  942. attach(c);
  943. attachstack(c);
  944. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  945. XMapWindow(dpy, c->win);
  946. setclientstate(c, NormalState);
  947. arrange();
  948. }
  949. void
  950. mappingnotify(XEvent *e) {
  951. XMappingEvent *ev = &e->xmapping;
  952. XRefreshKeyboardMapping(ev);
  953. if(ev->request == MappingKeyboard)
  954. grabkeys();
  955. }
  956. void
  957. maprequest(XEvent *e) {
  958. static XWindowAttributes wa;
  959. XMapRequestEvent *ev = &e->xmaprequest;
  960. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  961. return;
  962. if(wa.override_redirect)
  963. return;
  964. if(!getclient(ev->window))
  965. manage(ev->window, &wa);
  966. }
  967. void
  968. monocle(Monitor *m) {
  969. Client *c;
  970. for(c = nexttiled(m, clients); c; c = nexttiled(m, c->next))
  971. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw);
  972. }
  973. void
  974. movemouse(const Arg *arg) {
  975. int x, y, ocx, ocy, di, nx, ny;
  976. unsigned int dui;
  977. Client *c;
  978. Window dummy;
  979. XEvent ev;
  980. if(!(c = sel))
  981. return;
  982. restack(selmon);
  983. ocx = c->x;
  984. ocy = c->y;
  985. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  986. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  987. return;
  988. XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui);
  989. do {
  990. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  991. switch (ev.type) {
  992. case ConfigureRequest:
  993. case Expose:
  994. case MapRequest:
  995. handler[ev.type](&ev);
  996. break;
  997. case MotionNotify:
  998. nx = ocx + (ev.xmotion.x - x);
  999. ny = ocy + (ev.xmotion.y - y);
  1000. if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1001. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1002. if(abs(selmon->wx - nx) < snap)
  1003. nx = selmon->wx;
  1004. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1005. nx = selmon->wx + selmon->ww - WIDTH(c);
  1006. if(abs(selmon->wy - ny) < snap)
  1007. ny = selmon->wy;
  1008. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1009. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1010. if(!c->isfloating && lt[selmon->sellt]->arrange
  1011. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1012. togglefloating(NULL);
  1013. }
  1014. if(!lt[selmon->sellt]->arrange || c->isfloating)
  1015. resize(c, nx, ny, c->w, c->h);
  1016. break;
  1017. }
  1018. }
  1019. while(ev.type != ButtonRelease);
  1020. XUngrabPointer(dpy, CurrentTime);
  1021. }
  1022. Client *
  1023. nexttiled(Monitor *m, Client *c) {
  1024. // TODO: m handling
  1025. for(; c && (c->isfloating || m != &mon[c->mon] || !ISVISIBLE(c)); c = c->next);
  1026. return c;
  1027. }
  1028. void
  1029. propertynotify(XEvent *e) {
  1030. Client *c;
  1031. Window trans;
  1032. XPropertyEvent *ev = &e->xproperty;
  1033. if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1034. updatestatus();
  1035. else if(ev->state == PropertyDelete)
  1036. return; /* ignore */
  1037. else if((c = getclient(ev->window))) {
  1038. switch (ev->atom) {
  1039. default: break;
  1040. case XA_WM_TRANSIENT_FOR:
  1041. XGetTransientForHint(dpy, c->win, &trans);
  1042. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  1043. arrange();
  1044. break;
  1045. case XA_WM_NORMAL_HINTS:
  1046. updatesizehints(c);
  1047. break;
  1048. case XA_WM_HINTS:
  1049. updatewmhints(c);
  1050. drawbars();
  1051. break;
  1052. }
  1053. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1054. updatetitle(c);
  1055. if(c == sel)
  1056. drawbars();
  1057. }
  1058. }
  1059. }
  1060. void
  1061. quit(const Arg *arg) {
  1062. running = False;
  1063. }
  1064. void
  1065. resize(Client *c, int x, int y, int w, int h) {
  1066. XWindowChanges wc;
  1067. if(applysizehints(c, &x, &y, &w, &h)) {
  1068. c->x = wc.x = x;
  1069. c->y = wc.y = y;
  1070. c->w = wc.width = w;
  1071. c->h = wc.height = h;
  1072. wc.border_width = c->bw;
  1073. XConfigureWindow(dpy, c->win,
  1074. CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1075. configure(c);
  1076. XSync(dpy, False);
  1077. }
  1078. }
  1079. void
  1080. resizemouse(const Arg *arg) {
  1081. int ocx, ocy;
  1082. int nw, nh;
  1083. Client *c;
  1084. XEvent ev;
  1085. if(!(c = sel))
  1086. return;
  1087. restack(selmon);
  1088. ocx = c->x;
  1089. ocy = c->y;
  1090. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1091. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1092. return;
  1093. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1094. do {
  1095. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1096. switch(ev.type) {
  1097. case ConfigureRequest:
  1098. case Expose:
  1099. case MapRequest:
  1100. handler[ev.type](&ev);
  1101. break;
  1102. case MotionNotify:
  1103. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1104. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1105. if(snap && nw >= selmon->wx && nw <= selmon->wx + selmon->ww
  1106. && nh >= selmon->wy && nh <= selmon->wy + selmon->wh) {
  1107. if(!c->isfloating && lt[selmon->sellt]->arrange
  1108. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1109. togglefloating(NULL);
  1110. }
  1111. if(!lt[selmon->sellt]->arrange || c->isfloating)
  1112. resize(c, c->x, c->y, nw, nh);
  1113. break;
  1114. }
  1115. }
  1116. while(ev.type != ButtonRelease);
  1117. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1118. XUngrabPointer(dpy, CurrentTime);
  1119. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1120. }
  1121. void
  1122. restack(Monitor *m) {
  1123. Client *c;
  1124. XEvent ev;
  1125. XWindowChanges wc;
  1126. drawbars();
  1127. if(!sel)
  1128. return;
  1129. if(m == selmon && (sel->isfloating || !lt[m->sellt]->arrange))
  1130. XRaiseWindow(dpy, sel->win);
  1131. if(lt[m->sellt]->arrange) {
  1132. wc.stack_mode = Below;
  1133. wc.sibling = m->barwin;
  1134. for(c = stack; c; c = c->snext)
  1135. if(!c->isfloating && m == &mon[c->mon] && ISVISIBLE(c)) {
  1136. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1137. wc.sibling = c->win;
  1138. }
  1139. }
  1140. XSync(dpy, False);
  1141. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1142. }
  1143. void
  1144. run(void) {
  1145. XEvent ev;
  1146. /* main event loop */
  1147. XSync(dpy, False);
  1148. while(running && !XNextEvent(dpy, &ev)) {
  1149. if(handler[ev.type])
  1150. (handler[ev.type])(&ev); /* call handler */
  1151. }
  1152. }
  1153. void
  1154. scan(void) {
  1155. unsigned int i, num;
  1156. Window d1, d2, *wins = NULL;
  1157. XWindowAttributes wa;
  1158. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1159. for(i = 0; i < num; i++) {
  1160. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1161. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1162. continue;
  1163. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1164. manage(wins[i], &wa);
  1165. }
  1166. for(i = 0; i < num; i++) { /* now the transients */
  1167. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1168. continue;
  1169. if(XGetTransientForHint(dpy, wins[i], &d1)
  1170. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1171. manage(wins[i], &wa);
  1172. }
  1173. if(wins)
  1174. XFree(wins);
  1175. }
  1176. }
  1177. void
  1178. setclientstate(Client *c, long state) {
  1179. long data[] = {state, None};
  1180. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1181. PropModeReplace, (unsigned char *)data, 2);
  1182. }
  1183. void
  1184. setlayout(const Arg *arg) {
  1185. if(!arg || !arg->v || arg->v != lt[selmon->sellt])
  1186. selmon->sellt ^= 1;
  1187. if(arg && arg->v)
  1188. lt[selmon->sellt] = (Layout *)arg->v;
  1189. if(sel)
  1190. arrange();
  1191. else
  1192. drawbars();
  1193. }
  1194. /* arg > 1.0 will set mfact absolutly */
  1195. void
  1196. setmfact(const Arg *arg) {
  1197. float f;
  1198. if(!arg || !lt[selmon->sellt]->arrange)
  1199. return;
  1200. f = arg->f < 1.0 ? arg->f + mfact : arg->f - 1.0;
  1201. if(f < 0.1 || f > 0.9)
  1202. return;
  1203. mfact = f;
  1204. arrange();
  1205. }
  1206. void
  1207. setup(void) {
  1208. unsigned int i;
  1209. int w;
  1210. XSetWindowAttributes wa;
  1211. /* init screen */
  1212. screen = DefaultScreen(dpy);
  1213. root = RootWindow(dpy, screen);
  1214. initfont(font);
  1215. sx = 0;
  1216. sy = 0;
  1217. sw = DisplayWidth(dpy, screen);
  1218. sh = DisplayHeight(dpy, screen);
  1219. bh = dc.h = dc.font.height + 2;
  1220. lt[0] = &layouts[0];
  1221. lt[1] = &layouts[1 % LENGTH(layouts)];
  1222. updategeom();
  1223. /* init atoms */
  1224. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1225. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1226. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1227. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1228. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1229. /* init cursors */
  1230. wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1231. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1232. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1233. /* init appearance */
  1234. dc.norm[ColBorder] = getcolor(normbordercolor);
  1235. dc.norm[ColBG] = getcolor(normbgcolor);
  1236. dc.norm[ColFG] = getcolor(normfgcolor);
  1237. dc.sel[ColBorder] = getcolor(selbordercolor);
  1238. dc.sel[ColBG] = getcolor(selbgcolor);
  1239. dc.sel[ColFG] = getcolor(selfgcolor);
  1240. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1241. dc.gc = XCreateGC(dpy, root, 0, NULL);
  1242. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1243. if(!dc.font.set)
  1244. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1245. /* init bars */
  1246. wa.override_redirect = True;
  1247. wa.background_pixmap = ParentRelative;
  1248. wa.event_mask = ButtonPressMask|ExposureMask;
  1249. for(blw = i = 0; LENGTH(layouts) > 1 && i < LENGTH(layouts); i++) {
  1250. w = TEXTW(layouts[i].symbol);
  1251. blw = MAX(blw, w);
  1252. }
  1253. for(i = 0; i < nmons; i++) {
  1254. mon[i].barwin = XCreateWindow(dpy, root, mon[i].wx, mon[i].by, mon[i].ww, bh, 0, DefaultDepth(dpy, screen),
  1255. CopyFromParent, DefaultVisual(dpy, screen),
  1256. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1257. XDefineCursor(dpy, mon[i].barwin, cursor[CurNormal]);
  1258. XMapRaised(dpy, mon[i].barwin);
  1259. }
  1260. updatestatus();
  1261. /* EWMH support per view */
  1262. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1263. PropModeReplace, (unsigned char *) netatom, NetLast);
  1264. /* select for events */
  1265. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
  1266. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
  1267. |PropertyChangeMask;
  1268. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1269. XSelectInput(dpy, root, wa.event_mask);
  1270. grabkeys();
  1271. }
  1272. void
  1273. showhide(Client *c) {
  1274. if(!c)
  1275. return;
  1276. if(ISVISIBLE(c)) { /* show clients top down */
  1277. XMoveWindow(dpy, c->win, c->x, c->y);
  1278. if(!lt[selmon->sellt]->arrange || c->isfloating)
  1279. resize(c, c->x, c->y, c->w, c->h);
  1280. showhide(c->snext);
  1281. }
  1282. else { /* hide clients bottom up */
  1283. showhide(c->snext);
  1284. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  1285. }
  1286. }
  1287. void
  1288. sigchld(int signal) {
  1289. while(0 < waitpid(-1, NULL, WNOHANG));
  1290. }
  1291. void
  1292. spawn(const Arg *arg) {
  1293. signal(SIGCHLD, sigchld);
  1294. if(fork() == 0) {
  1295. if(dpy)
  1296. close(ConnectionNumber(dpy));
  1297. setsid();
  1298. execvp(((char **)arg->v)[0], (char **)arg->v);
  1299. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1300. perror(" failed");
  1301. exit(0);
  1302. }
  1303. }
  1304. void
  1305. tag(const Arg *arg) {
  1306. if(sel && arg->ui & TAGMASK) {
  1307. sel->tags = arg->ui & TAGMASK;
  1308. arrange();
  1309. }
  1310. }
  1311. #ifdef XINERAMA
  1312. void
  1313. tagmon(const Arg *arg) {
  1314. if(!sel || arg->ui >= nmons)
  1315. return;
  1316. sel->mon = arg->ui;
  1317. arrange();
  1318. }
  1319. #endif /* XINERAMA */
  1320. int
  1321. textnw(const char *text, unsigned int len) {
  1322. XRectangle r;
  1323. if(dc.font.set) {
  1324. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1325. return r.width;
  1326. }
  1327. return XTextWidth(dc.font.xfont, text, len);
  1328. }
  1329. void
  1330. tile(Monitor *m) {
  1331. int x, y, h, w, mw;
  1332. unsigned int i, n;
  1333. Client *c;
  1334. for(n = 0, c = nexttiled(m, clients); c; c = nexttiled(m, c->next), n++);
  1335. if(n == 0)
  1336. return;
  1337. /* master */
  1338. c = nexttiled(m, clients);
  1339. mw = mfact * m->ww;
  1340. resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw);
  1341. if(--n == 0)
  1342. return;
  1343. /* tile stack */
  1344. x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
  1345. y = m->wy;
  1346. w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
  1347. h = m->wh / n;
  1348. if(h < bh)
  1349. h = m->wh;
  1350. for(i = 0, c = nexttiled(m, c->next); c; c = nexttiled(m, c->next), i++) {
  1351. resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
  1352. ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw));
  1353. if(h != m->wh)
  1354. y = c->y + HEIGHT(c);
  1355. }
  1356. }
  1357. void
  1358. togglebar(const Arg *arg) {
  1359. selmon->showbar = !selmon->showbar;
  1360. updategeom();
  1361. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1362. arrange();
  1363. }
  1364. void
  1365. togglefloating(const Arg *arg) {
  1366. if(!sel)
  1367. return;
  1368. sel->isfloating = !sel->isfloating || sel->isfixed;
  1369. if(sel->isfloating)
  1370. resize(sel, sel->x, sel->y, sel->w, sel->h);
  1371. arrange();
  1372. }
  1373. void
  1374. toggletag(const Arg *arg) {
  1375. unsigned int mask;
  1376. if(!sel)
  1377. return;
  1378. mask = sel->tags ^ (arg->ui & TAGMASK);
  1379. if(mask) {
  1380. sel->tags = mask;
  1381. arrange();
  1382. }
  1383. }
  1384. void
  1385. toggleview(const Arg *arg) {
  1386. unsigned int mask = tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1387. if(mask) {
  1388. tagset[selmon->seltags] = mask;
  1389. arrange();
  1390. }
  1391. }
  1392. void
  1393. unmanage(Client *c) {
  1394. XWindowChanges wc;
  1395. wc.border_width = c->oldbw;
  1396. /* The server grab construct avoids race conditions. */
  1397. XGrabServer(dpy);
  1398. XSetErrorHandler(xerrordummy);
  1399. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1400. detach(c);
  1401. detachstack(c);
  1402. if(sel == c)
  1403. focus(NULL);
  1404. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1405. setclientstate(c, WithdrawnState);
  1406. free(c);
  1407. XSync(dpy, False);
  1408. XSetErrorHandler(xerror);
  1409. XUngrabServer(dpy);
  1410. arrange();
  1411. }
  1412. void
  1413. unmapnotify(XEvent *e) {
  1414. Client *c;
  1415. XUnmapEvent *ev = &e->xunmap;
  1416. if((c = getclient(ev->window)))
  1417. unmanage(c);
  1418. }
  1419. void
  1420. updategeom(void) {
  1421. #ifdef XINERAMA
  1422. int di, x, y, n;
  1423. unsigned int dui, i = 0;
  1424. Bool pquery;
  1425. Client *c;
  1426. Window dummy;
  1427. XineramaScreenInfo *info = NULL;
  1428. /* window area geometry */
  1429. if(XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
  1430. nmons = (unsigned int)n;
  1431. for(c = clients; c; c = c->next)
  1432. if(c->mon >= nmons)
  1433. c->mon = nmons - 1;
  1434. if(!(mon = (Monitor *)realloc(mon, sizeof(Monitor) * nmons)))
  1435. die("fatal: could not realloc() %u bytes\n", sizeof(Monitor) * nmons);
  1436. pquery = XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui);
  1437. for(i = 0; i < nmons; i++) {
  1438. /* TODO: consider re-using XineramaScreenInfo */
  1439. mon[i].symbol[0] = '[';
  1440. mon[i].symbol[1] = '0' + info[i].screen_number;
  1441. mon[i].symbol[2] = ']';
  1442. mon[i].symbol[3] = 0;
  1443. mon[i].showbar = showbar;
  1444. mon[i].topbar = topbar;
  1445. mon[i].wx = info[i].x_org;
  1446. mon[i].wy = mon[i].showbar && mon[i].topbar ? info[i].y_org + bh : info[i].y_org;
  1447. mon[i].ww = info[i].width;
  1448. mon[i].wh = mon[i].showbar ? info[i].height - bh : info[i].height;
  1449. mon[i].seltags = 0;
  1450. mon[i].sellt = 0;
  1451. if(mon[i].showbar)
  1452. mon[i].by = mon[i].topbar ? info[i].y_org : mon[i].wy + mon[i].wh;
  1453. else
  1454. mon[i].by = -bh;
  1455. if(pquery && INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
  1456. selmon = &mon[i];
  1457. }
  1458. XFree(info);
  1459. }
  1460. else
  1461. #endif /* XINERAMA */
  1462. {
  1463. nmons = 1;
  1464. if(!(mon = (Monitor *)realloc(mon, sizeof(Monitor))))
  1465. die("fatal: could not realloc() %u bytes\n", sizeof(Monitor));
  1466. selmon = &mon[0];
  1467. mon[0].symbol[0] = '[';
  1468. mon[0].symbol[1] = '0';
  1469. mon[0].symbol[2] = ']';
  1470. mon[0].symbol[3] = 0;
  1471. mon[0].showbar = showbar;
  1472. mon[0].topbar = topbar;
  1473. mon[0].wx = sx;
  1474. mon[0].wy = mon[0].showbar && mon[0].topbar ? sy + bh : sy;
  1475. mon[0].ww = sw;
  1476. mon[0].wh = mon[0].showbar ? sh - bh : sh;
  1477. mon[0].seltags = 0;
  1478. mon[0].sellt = 0;
  1479. if(mon[0].showbar)
  1480. mon[0].by = mon[0].topbar ? sy : mon[0].wy + mon[0].wh;
  1481. else
  1482. mon[0].by = -bh;
  1483. }
  1484. }
  1485. void
  1486. updatenumlockmask(void) {
  1487. unsigned int i, j;
  1488. XModifierKeymap *modmap;
  1489. numlockmask = 0;
  1490. modmap = XGetModifierMapping(dpy);
  1491. for(i = 0; i < 8; i++)
  1492. for(j = 0; j < modmap->max_keypermod; j++)
  1493. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1494. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1495. numlockmask = (1 << i);
  1496. XFreeModifiermap(modmap);
  1497. }
  1498. void
  1499. updatesizehints(Client *c) {
  1500. long msize;
  1501. XSizeHints size;
  1502. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1503. /* size is uninitialized, ensure that size.flags aren't used */
  1504. size.flags = PSize;
  1505. if(size.flags & PBaseSize) {
  1506. c->basew = size.base_width;
  1507. c->baseh = size.base_height;
  1508. }
  1509. else if(size.flags & PMinSize) {
  1510. c->basew = size.min_width;
  1511. c->baseh = size.min_height;
  1512. }
  1513. else
  1514. c->basew = c->baseh = 0;
  1515. if(size.flags & PResizeInc) {
  1516. c->incw = size.width_inc;
  1517. c->inch = size.height_inc;
  1518. }
  1519. else
  1520. c->incw = c->inch = 0;
  1521. if(size.flags & PMaxSize) {
  1522. c->maxw = size.max_width;
  1523. c->maxh = size.max_height;
  1524. }
  1525. else
  1526. c->maxw = c->maxh = 0;
  1527. if(size.flags & PMinSize) {
  1528. c->minw = size.min_width;
  1529. c->minh = size.min_height;
  1530. }
  1531. else if(size.flags & PBaseSize) {
  1532. c->minw = size.base_width;
  1533. c->minh = size.base_height;
  1534. }
  1535. else
  1536. c->minw = c->minh = 0;
  1537. if(size.flags & PAspect) {
  1538. c->mina = (float)size.min_aspect.y / (float)size.min_aspect.x;
  1539. c->maxa = (float)size.max_aspect.x / (float)size.max_aspect.y;
  1540. }
  1541. else
  1542. c->maxa = c->mina = 0.0;
  1543. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1544. && c->maxw == c->minw && c->maxh == c->minh);
  1545. }
  1546. void
  1547. updatetitle(Client *c) {
  1548. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1549. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1550. }
  1551. void
  1552. updatestatus() {
  1553. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1554. strcpy(stext, "dwm-"VERSION);
  1555. drawbar(selmon);
  1556. }
  1557. void
  1558. updatewmhints(Client *c) {
  1559. XWMHints *wmh;
  1560. if((wmh = XGetWMHints(dpy, c->win))) {
  1561. if(c == sel && wmh->flags & XUrgencyHint) {
  1562. wmh->flags &= ~XUrgencyHint;
  1563. XSetWMHints(dpy, c->win, wmh);
  1564. }
  1565. else
  1566. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1567. XFree(wmh);
  1568. }
  1569. }
  1570. void
  1571. view(const Arg *arg) {
  1572. if((arg->ui & TAGMASK) == tagset[selmon->seltags])
  1573. return;
  1574. selmon->seltags ^= 1; /* toggle sel tagset */
  1575. if(arg->ui & TAGMASK)
  1576. tagset[selmon->seltags] = arg->ui & TAGMASK;
  1577. arrange();
  1578. }
  1579. /* There's no way to check accesses to destroyed windows, thus those cases are
  1580. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1581. * default error handler, which may call exit. */
  1582. int
  1583. xerror(Display *dpy, XErrorEvent *ee) {
  1584. if(ee->error_code == BadWindow
  1585. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1586. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1587. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1588. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1589. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1590. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1591. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1592. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1593. return 0;
  1594. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1595. ee->request_code, ee->error_code);
  1596. return xerrorxlib(dpy, ee); /* may call exit */
  1597. }
  1598. int
  1599. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1600. return 0;
  1601. }
  1602. /* Startup Error handler to check if another window manager
  1603. * is already running. */
  1604. int
  1605. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1606. otherwm = True;
  1607. return -1;
  1608. }
  1609. void
  1610. zoom(const Arg *arg) {
  1611. Client *c = sel;
  1612. if(!lt[selmon->sellt]->arrange || lt[selmon->sellt]->arrange == monocle || (sel && sel->isfloating))
  1613. return;
  1614. if(c == nexttiled(selmon, clients))
  1615. if(!c || !(c = nexttiled(selmon, c->next)))
  1616. return;
  1617. detach(c);
  1618. attach(c);
  1619. focus(c);
  1620. arrange();
  1621. }
  1622. int
  1623. main(int argc, char *argv[]) {
  1624. if(argc == 2 && !strcmp("-v", argv[1]))
  1625. die("dwm-"VERSION", © 2006-2009 dwm engineers, see LICENSE for details\n");
  1626. else if(argc != 1)
  1627. die("usage: dwm [-v]\n");
  1628. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1629. fputs("warning: no locale support\n", stderr);
  1630. if(!(dpy = XOpenDisplay(NULL)))
  1631. die("dwm: cannot open display\n");
  1632. checkotherwm();
  1633. setup();
  1634. scan();
  1635. run();
  1636. cleanup();
  1637. XCloseDisplay(dpy);
  1638. return 0;
  1639. }