diff options
Diffstat (limited to '')
| -rw-r--r-- | draw.c | 60 |
1 files changed, 50 insertions, 10 deletions
| @@ -1,8 +1,10 @@ | |||
| 1 | /* See LICENSE file for copyright and license details. */ | 1 | /* See LICENSE file for copyright and license details. */ |
| 2 | #include <stdio.h> | ||
| 2 | #include <stdlib.h> | 3 | #include <stdlib.h> |
| 3 | #include <X11/Xlib.h> | 4 | #include <X11/Xlib.h> |
| 4 | 5 | ||
| 5 | #include "draw.h" | 6 | #include "draw.h" |
| 7 | #include "util.h" | ||
| 6 | 8 | ||
| 7 | Draw * | 9 | Draw * |
| 8 | draw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h) { | 10 | draw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h) { |
| @@ -36,32 +38,70 @@ draw_free(Draw *draw) { | |||
| 36 | } | 38 | } |
| 37 | 39 | ||
| 38 | Fnt * | 40 | Fnt * |
| 39 | font_create(const char *fontname) { | 41 | draw_font_create(Draw *draw, const char *fontname) { |
| 40 | Fnt *font = (Fnt *)calloc(1, sizeof(Fnt)); | 42 | Fnt *font; |
| 41 | /* TODO: allocate actual font */ | 43 | char *def, **missing; |
| 44 | int n; | ||
| 45 | |||
| 46 | if(!draw) | ||
| 47 | return NULL; | ||
| 48 | font = (Fnt *)calloc(1, sizeof(Fnt)); | ||
| 49 | font->set = XCreateFontSet(draw->dpy, fontname, &missing, &n, &def); | ||
| 50 | if(missing) { | ||
| 51 | while(n--) | ||
| 52 | fprintf(stderr, "draw: missing fontset: %s\n", missing[n]); | ||
| 53 | XFreeStringList(missing); | ||
| 54 | } | ||
| 55 | if(font->set) { | ||
| 56 | XFontStruct **xfonts; | ||
| 57 | char **font_names; | ||
| 58 | |||
| 59 | XExtentsOfFontSet(font->set); | ||
| 60 | n = XFontsOfFontSet(font->set, &xfonts, &font_names); | ||
| 61 | while(n--) { | ||
| 62 | font->ascent = MAX(font->ascent, (*xfonts)->ascent); | ||
| 63 | font->descent = MAX(font->descent,(*xfonts)->descent); | ||
| 64 | xfonts++; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | else { | ||
| 68 | if(!(font->xfont = XLoadQueryFont(draw->dpy, fontname)) | ||
| 69 | && !(font->xfont = XLoadQueryFont(draw->dpy, "fixed"))) | ||
| 70 | die("error, cannot load font: '%s'\n", fontname); | ||
| 71 | font->ascent = font->xfont->ascent; | ||
| 72 | font->descent = font->xfont->descent; | ||
| 73 | } | ||
| 74 | font->h = font->ascent + font->descent; | ||
| 42 | return font; | 75 | return font; |
| 43 | } | 76 | } |
| 44 | 77 | ||
| 45 | void | 78 | void |
| 46 | font_free(Fnt *font) { | 79 | draw_font_free(Draw *draw, Fnt *font) { |
| 47 | if(!font) | 80 | if(!draw || !font) |
| 48 | return; | 81 | return; |
| 49 | /* TODO: deallocate any font resources */ | 82 | if(font->set) |
| 83 | XFreeFontSet(draw->dpy, font->set); | ||
| 84 | else | ||
| 85 | XFreeFont(draw->dpy, font->xfont); | ||
| 50 | free(font); | 86 | free(font); |
| 51 | } | 87 | } |
| 52 | 88 | ||
| 53 | Col * | 89 | Col * |
| 54 | col_create(const char *colname) { | 90 | draw_col_create(Draw *draw, const char *colname) { |
| 55 | Col *col = (Col *)calloc(1, sizeof(Col)); | 91 | Col *col = (Col *)calloc(1, sizeof(Col)); |
| 56 | /* TODO: allocate color */ | 92 | Colormap cmap = DefaultColormap(draw->dpy, draw->screen); |
| 93 | XColor color; | ||
| 94 | |||
| 95 | if(!XAllocNamedColor(draw->dpy, cmap, colname, &color, &color)) | ||
| 96 | die("error, cannot allocate color '%s'\n", colname); | ||
| 97 | col->rgb = color.pixel; | ||
| 57 | return col; | 98 | return col; |
| 58 | } | 99 | } |
| 59 | 100 | ||
| 60 | void | 101 | void |
| 61 | col_free(Col *col) { | 102 | draw_col_free(Draw *draw, Col *col) { |
| 62 | if(!col) | 103 | if(!col) |
| 63 | return; | 104 | return; |
| 64 | /* TODO: deallocate any color resource */ | ||
| 65 | free(col); | 105 | free(col); |
| 66 | } | 106 | } |
| 67 | 107 | ||
