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.

258 lines
5.8 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. eprint("error, cannot load font: '%s'\n", fontstr);
  67. dc.font.ascent = dc.font.xfont->ascent;
  68. dc.font.descent = dc.font.xfont->descent;
  69. }
  70. dc.font.height = dc.font.ascent + dc.font.descent;
  71. }
  72. static Bool
  73. isoccupied(unsigned int t) {
  74. Client *c;
  75. for(c = clients; c; c = c->next)
  76. if(c->tags[t])
  77. return True;
  78. return False;
  79. }
  80. static unsigned int
  81. textnw(const char *text, unsigned int len) {
  82. XRectangle r;
  83. if(dc.font.set) {
  84. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  85. return r.width;
  86. }
  87. return XTextWidth(dc.font.xfont, text, len);
  88. }
  89. static void
  90. drawtext(const char *text, unsigned long col[ColLast]) {
  91. int x, y, w, h;
  92. static char buf[256];
  93. unsigned int len, olen;
  94. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  95. XSetForeground(dpy, dc.gc, col[ColBG]);
  96. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  97. if(!text)
  98. return;
  99. w = 0;
  100. olen = len = strlen(text);
  101. if(len >= sizeof buf)
  102. len = sizeof buf - 1;
  103. memcpy(buf, text, len);
  104. buf[len] = 0;
  105. h = dc.font.ascent + dc.font.descent;
  106. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  107. x = dc.x + (h / 2);
  108. /* shorten text if necessary */
  109. while(len && (w = textnw(buf, len)) > dc.w - h)
  110. buf[--len] = 0;
  111. if(len < olen) {
  112. if(len > 1)
  113. buf[len - 1] = '.';
  114. if(len > 2)
  115. buf[len - 2] = '.';
  116. if(len > 3)
  117. buf[len - 3] = '.';
  118. }
  119. if(w > dc.w)
  120. return; /* too long */
  121. XSetForeground(dpy, dc.gc, col[ColFG]);
  122. if(dc.font.set)
  123. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  124. else
  125. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  126. }
  127. /* extern */
  128. unsigned int bh;
  129. unsigned int bpos = BARPOS;
  130. DC dc = {0};
  131. Window barwin;
  132. void
  133. drawbar(void) {
  134. int i, x;
  135. dc.x = dc.y = 0;
  136. for(i = 0; i < ntags; i++) {
  137. dc.w = textw(tags[i]);
  138. if(seltags[i]) {
  139. drawtext(tags[i], dc.sel);
  140. drawsquare(sel && sel->tags[i], isoccupied(i), dc.sel);
  141. }
  142. else {
  143. drawtext(tags[i], dc.norm);
  144. drawsquare(sel && sel->tags[i], isoccupied(i), dc.norm);
  145. }
  146. dc.x += dc.w;
  147. }
  148. dc.w = blw;
  149. drawtext(getsymbol(), dc.norm);
  150. x = dc.x + dc.w;
  151. dc.w = textw(stext);
  152. dc.x = sw - dc.w;
  153. if(dc.x < x) {
  154. dc.x = x;
  155. dc.w = sw - x;
  156. }
  157. drawtext(stext, dc.norm);
  158. if((dc.w = dc.x - x) > bh) {
  159. dc.x = x;
  160. if(sel) {
  161. drawtext(sel->name, dc.sel);
  162. drawsquare(sel->ismax, sel->isfloating, dc.sel);
  163. }
  164. else
  165. drawtext(NULL, dc.norm);
  166. }
  167. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
  168. XSync(dpy, False);
  169. }
  170. void
  171. initbar(void) {
  172. XSetWindowAttributes wa;
  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. wa.override_redirect = 1;
  182. wa.background_pixmap = ParentRelative;
  183. wa.event_mask = ButtonPressMask | ExposureMask;
  184. barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
  185. DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  186. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  187. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  188. updatebarpos();
  189. XMapRaised(dpy, barwin);
  190. strcpy(stext, "dwm-"VERSION);
  191. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  192. dc.gc = XCreateGC(dpy, root, 0, 0);
  193. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  194. if(!dc.font.set)
  195. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  196. }
  197. unsigned int
  198. textw(const char *text) {
  199. return textnw(text, strlen(text)) + dc.font.height;
  200. }
  201. void
  202. togglebar(const char *arg) {
  203. if(bpos == BarOff)
  204. bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
  205. else
  206. bpos = BarOff;
  207. updatebarpos();
  208. arrange();
  209. }
  210. void
  211. updatebarpos(void) {
  212. XEvent ev;
  213. wax = sx;
  214. way = sy;
  215. wah = sh;
  216. waw = sw;
  217. switch(bpos) {
  218. default:
  219. wah -= bh;
  220. way += bh;
  221. XMoveWindow(dpy, barwin, sx, sy);
  222. break;
  223. case BarBot:
  224. wah -= bh;
  225. XMoveWindow(dpy, barwin, sx, sy + wah);
  226. break;
  227. case BarOff:
  228. XMoveWindow(dpy, barwin, sx, sy - bh);
  229. break;
  230. }
  231. XSync(dpy, False);
  232. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  233. }