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.
 
 
 
 
 
 

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