My dmenu build
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.
 
 
 
 
 
 

197 lines
4.7 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include <locale.h>
  3. #include <stdarg.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <X11/Xlib.h>
  8. #include "draw.h"
  9. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  10. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  11. #define FG(dc, col) ((col)[(dc)->invert ? ColBG : ColFG])
  12. #define BG(dc, col) ((col)[(dc)->invert ? ColFG : ColBG])
  13. #define DEFFONT "fixed"
  14. static Bool loadfont(DC *dc, const char *fontstr);
  15. void
  16. drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, Bool fill, unsigned long color) {
  17. XRectangle r = { dc->x + x, dc->y + y, w, h };
  18. if(!fill) {
  19. r.width -= 1;
  20. r.height -= 1;
  21. }
  22. XSetForeground(dc->dpy, dc->gc, color);
  23. (fill ? XFillRectangles : XDrawRectangles)(dc->dpy, dc->canvas, dc->gc, &r, 1);
  24. }
  25. void
  26. drawtext(DC *dc, const char *text, unsigned long col[ColLast]) {
  27. char buf[256];
  28. size_t n, mn;
  29. /* shorten text if necessary */
  30. n = strlen(text);
  31. for(mn = MIN(n, sizeof buf); textnw(dc, text, mn) > dc->w - dc->font.height/2; mn--)
  32. if(mn == 0)
  33. return;
  34. memcpy(buf, text, mn);
  35. if(mn < n)
  36. for(n = MAX(mn-3, 0); n < mn; buf[n++] = '.');
  37. drawrect(dc, 0, 0, dc->w, dc->h, True, BG(dc, col));
  38. drawtextn(dc, buf, mn, col);
  39. }
  40. void
  41. drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]) {
  42. int x, y;
  43. x = dc->x + dc->font.height/2;
  44. y = dc->y + dc->font.ascent+1;
  45. XSetForeground(dc->dpy, dc->gc, FG(dc, col));
  46. if(dc->font.set)
  47. XmbDrawString(dc->dpy, dc->canvas, dc->font.set, dc->gc, x, y, text, n);
  48. else {
  49. XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid);
  50. XDrawString(dc->dpy, dc->canvas, dc->gc, x, y, text, n);
  51. }
  52. }
  53. void
  54. eprintf(const char *fmt, ...) {
  55. va_list ap;
  56. fprintf(stderr, "%s: ", progname);
  57. va_start(ap, fmt);
  58. vfprintf(stderr, fmt, ap);
  59. va_end(ap);
  60. exit(EXIT_FAILURE);
  61. }
  62. void
  63. freedc(DC *dc) {
  64. if(dc->font.set)
  65. XFreeFontSet(dc->dpy, dc->font.set);
  66. if(dc->font.xfont)
  67. XFreeFont(dc->dpy, dc->font.xfont);
  68. if(dc->canvas)
  69. XFreePixmap(dc->dpy, dc->canvas);
  70. XFreeGC(dc->dpy, dc->gc);
  71. XCloseDisplay(dc->dpy);
  72. free(dc);
  73. }
  74. unsigned long
  75. getcolor(DC *dc, const char *colstr) {
  76. Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy));
  77. XColor color;
  78. if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color))
  79. eprintf("cannot allocate color '%s'\n", colstr);
  80. return color.pixel;
  81. }
  82. DC *
  83. initdc(void) {
  84. DC *dc;
  85. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  86. weprintf("no locale support\n");
  87. if(!(dc = malloc(sizeof *dc)))
  88. eprintf("cannot malloc %u bytes\n", sizeof *dc);
  89. if(!(dc->dpy = XOpenDisplay(NULL)))
  90. eprintf("cannot open display\n");
  91. dc->gc = XCreateGC(dc->dpy, DefaultRootWindow(dc->dpy), 0, NULL);
  92. XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter);
  93. dc->font.xfont = NULL;
  94. dc->font.set = NULL;
  95. dc->canvas = None;
  96. return dc;
  97. }
  98. void
  99. initfont(DC *dc, const char *fontstr) {
  100. if(!loadfont(dc, fontstr ? fontstr : DEFFONT)) {
  101. if(fontstr != NULL)
  102. weprintf("cannot load font '%s'\n", fontstr);
  103. if(fontstr == NULL || !loadfont(dc, DEFFONT))
  104. eprintf("cannot load font '%s'\n", DEFFONT);
  105. }
  106. dc->font.height = dc->font.ascent + dc->font.descent;
  107. }
  108. Bool
  109. loadfont(DC *dc, const char *fontstr) {
  110. char *def, **missing;
  111. int i, n;
  112. if(!*fontstr)
  113. return False;
  114. if((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) {
  115. char **names;
  116. XFontStruct **xfonts;
  117. n = XFontsOfFontSet(dc->font.set, &xfonts, &names);
  118. for(i = dc->font.ascent = dc->font.descent = 0; i < n; i++) {
  119. dc->font.ascent = MAX(dc->font.ascent, xfonts[i]->ascent);
  120. dc->font.descent = MAX(dc->font.descent, xfonts[i]->descent);
  121. }
  122. }
  123. else if((dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))) {
  124. dc->font.ascent = dc->font.xfont->ascent;
  125. dc->font.descent = dc->font.xfont->descent;
  126. }
  127. if(missing)
  128. XFreeStringList(missing);
  129. return (dc->font.set || dc->font.xfont);
  130. }
  131. void
  132. mapdc(DC *dc, Window win, unsigned int w, unsigned int h) {
  133. XCopyArea(dc->dpy, dc->canvas, win, dc->gc, 0, 0, w, h, 0, 0);
  134. }
  135. void
  136. resizedc(DC *dc, unsigned int w, unsigned int h) {
  137. if(dc->canvas)
  138. XFreePixmap(dc->dpy, dc->canvas);
  139. dc->canvas = XCreatePixmap(dc->dpy, DefaultRootWindow(dc->dpy), w, h,
  140. DefaultDepth(dc->dpy, DefaultScreen(dc->dpy)));
  141. dc->x = dc->y = 0;
  142. dc->w = w;
  143. dc->h = h;
  144. dc->invert = False;
  145. }
  146. int
  147. textnw(DC *dc, const char *text, size_t len) {
  148. if(dc->font.set) {
  149. XRectangle r;
  150. XmbTextExtents(dc->font.set, text, len, NULL, &r);
  151. return r.width;
  152. }
  153. return XTextWidth(dc->font.xfont, text, len);
  154. }
  155. int
  156. textw(DC *dc, const char *text) {
  157. return textnw(dc, text, strlen(text)) + dc->font.height;
  158. }
  159. void
  160. weprintf(const char *fmt, ...) {
  161. va_list ap;
  162. fprintf(stderr, "%s: warning: ", progname);
  163. va_start(ap, fmt);
  164. vfprintf(stderr, fmt, ap);
  165. va_end(ap);
  166. }