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.

263 lines
5.9 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include "dwm.h"
  3. #include <string.h>
  4. #include <stdio.h>
  5. /* static */
  6. static void
  7. drawsquare(Bool filled, Bool empty, unsigned long col[ColLast]) {
  8. int x;
  9. XGCValues gcv;
  10. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  11. gcv.foreground = col[ColFG];
  12. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  13. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  14. r.x = dc.x + 1;
  15. r.y = dc.y + 1;
  16. if(filled) {
  17. r.width = r.height = x + 1;
  18. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  19. }
  20. else if(empty) {
  21. r.width = r.height = x;
  22. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  23. }
  24. }
  25. static unsigned long
  26. initcolor(const char *colstr) {
  27. Colormap cmap = DefaultColormap(dpy, screen);
  28. XColor color;
  29. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  30. eprint("error, cannot allocate color '%s'\n", colstr);
  31. return color.pixel;
  32. }
  33. static void
  34. initfont(const char *fontstr) {
  35. char *def, **missing;
  36. int i, n;
  37. missing = NULL;
  38. if(dc.font.set)
  39. XFreeFontSet(dpy, dc.font.set);
  40. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  41. if(missing) {
  42. while(n--)
  43. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  44. XFreeStringList(missing);
  45. }
  46. if(dc.font.set) {
  47. XFontSetExtents *font_extents;
  48. XFontStruct **xfonts;
  49. char **font_names;
  50. dc.font.ascent = dc.font.descent = 0;
  51. font_extents = XExtentsOfFontSet(dc.font.set);
  52. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  53. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  54. if(dc.font.ascent < (*xfonts)->ascent)
  55. dc.font.ascent = (*xfonts)->ascent;
  56. if(dc.font.descent < (*xfonts)->descent)
  57. dc.font.descent = (*xfonts)->descent;
  58. xfonts++;
  59. }
  60. }
  61. else {
  62. if(dc.font.xfont)
  63. XFreeFont(dpy, dc.font.xfont);
  64. dc.font.xfont = NULL;
  65. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  66. || !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  67. eprint("error, cannot load font: '%s'\n", fontstr);
  68. dc.font.ascent = dc.font.xfont->ascent;
  69. dc.font.descent = dc.font.xfont->descent;
  70. }
  71. dc.font.height = dc.font.ascent + dc.font.descent;
  72. }
  73. static Bool
  74. isoccupied(unsigned int t) {
  75. Client *c;
  76. for(c = clients; c; c = c->next)
  77. if(c->tags[t])
  78. return True;
  79. return False;
  80. }
  81. static unsigned int
  82. textnw(const char *text, unsigned int len) {
  83. XRectangle r;
  84. if(dc.font.set) {
  85. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  86. return r.width;
  87. }
  88. return XTextWidth(dc.font.xfont, text, len);
  89. }
  90. static void
  91. drawtext(const char *text, unsigned long col[ColLast]) {
  92. int x, y, w, h;
  93. static char buf[256];
  94. unsigned int len, olen;
  95. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  96. XSetForeground(dpy, dc.gc, col[ColBG]);
  97. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  98. if(!text)
  99. return;
  100. w = 0;
  101. olen = len = strlen(text);
  102. if(len >= sizeof buf)
  103. len = sizeof buf - 1;
  104. memcpy(buf, text, len);
  105. buf[len] = 0;
  106. h = dc.font.ascent + dc.font.descent;
  107. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  108. x = dc.x + (h / 2);
  109. /* shorten text if necessary */
  110. while(len && (w = textnw(buf, len)) > dc.w - h)
  111. buf[--len] = 0;
  112. if(len < olen) {
  113. if(len > 1)
  114. buf[len - 1] = '.';
  115. if(len > 2)
  116. buf[len - 2] = '.';
  117. if(len > 3)
  118. buf[len - 3] = '.';
  119. }
  120. if(w > dc.w)
  121. return; /* too long */
  122. XSetForeground(dpy, dc.gc, col[ColFG]);
  123. if(dc.font.set)
  124. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  125. else
  126. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  127. }
  128. /* extern */
  129. unsigned int bh;
  130. unsigned int bpos = BARPOS;
  131. DC dc = {0};
  132. Window barwin;
  133. void
  134. drawbar(void) {
  135. int i, x;
  136. dc.x = dc.y = 0;
  137. for(i = 0; i < ntags; i++) {
  138. dc.w = textw(tags[i]);
  139. if(seltags[i]) {
  140. drawtext(tags[i], dc.sel);
  141. drawsquare(sel && sel->tags[i], isoccupied(i), dc.sel);
  142. }
  143. else {
  144. drawtext(tags[i], dc.norm);
  145. drawsquare(sel && sel->tags[i], isoccupied(i), dc.norm);
  146. }
  147. dc.x += dc.w;
  148. }
  149. dc.w = blw;
  150. drawtext(getsymbol(), dc.norm);
  151. x = dc.x + dc.w;
  152. dc.w = textw(stext);
  153. dc.x = sw - dc.w;
  154. if(dc.x < x) {
  155. dc.x = x;
  156. dc.w = sw - x;
  157. }
  158. drawtext(stext, dc.norm);
  159. if((dc.w = dc.x - x) > bh) {
  160. dc.x = x;
  161. if(sel) {
  162. drawtext(sel->name, dc.sel);
  163. drawsquare(sel->ismax, sel->isfloating, dc.sel);
  164. }
  165. else
  166. drawtext(NULL, dc.norm);
  167. }
  168. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
  169. XSync(dpy, False);
  170. }
  171. void
  172. initstyle(void) {
  173. dc.norm[ColBorder] = initcolor(NORMBORDERCOLOR);
  174. dc.norm[ColBG] = initcolor(NORMBGCOLOR);
  175. dc.norm[ColFG] = initcolor(NORMFGCOLOR);
  176. dc.sel[ColBorder] = initcolor(SELBORDERCOLOR);
  177. dc.sel[ColBG] = initcolor(SELBGCOLOR);
  178. dc.sel[ColFG] = initcolor(SELFGCOLOR);
  179. initfont(FONT);
  180. dc.h = bh = dc.font.height + 2;
  181. }
  182. void
  183. initbar(void) {
  184. XSetWindowAttributes wa;
  185. wa.override_redirect = 1;
  186. wa.background_pixmap = ParentRelative;
  187. wa.event_mask = ButtonPressMask | ExposureMask;
  188. barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
  189. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  190. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  191. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  192. updatebarpos();
  193. XMapRaised(dpy, barwin);
  194. strcpy(stext, "dwm-"VERSION);
  195. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  196. dc.gc = XCreateGC(dpy, root, 0, 0);
  197. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  198. if(!dc.font.set)
  199. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  200. }
  201. unsigned int
  202. textw(const char *text) {
  203. return textnw(text, strlen(text)) + dc.font.height;
  204. }
  205. void
  206. togglebar(const char *arg) {
  207. if(bpos == BarOff)
  208. bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
  209. else
  210. bpos = BarOff;
  211. updatebarpos();
  212. arrange();
  213. }
  214. void
  215. updatebarpos(void) {
  216. XEvent ev;
  217. wax = sx;
  218. way = sy;
  219. wah = sh;
  220. waw = sw;
  221. switch(bpos) {
  222. default:
  223. wah -= bh;
  224. way += bh;
  225. XMoveWindow(dpy, barwin, sx, sy);
  226. break;
  227. case BarBot:
  228. wah -= bh;
  229. XMoveWindow(dpy, barwin, sx, sy + wah);
  230. break;
  231. case BarOff:
  232. XMoveWindow(dpy, barwin, sx, sy - bh);
  233. break;
  234. }
  235. XSync(dpy, False);
  236. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  237. }