continued with draw.c abstraction, also started util.{h,c} implementation, that will be used by draw.c as well
f21d46ea
6 file(s) · +80 −29
| 3 | 3 | ||
| 4 | 4 | include config.mk |
|
| 5 | 5 | ||
| 6 | - | SRC = draw.c dwm.c |
|
| 6 | + | SRC = util.c draw.c dwm.c |
|
| 7 | 7 | OBJ = ${SRC:.c=.o} |
|
| 8 | 8 | ||
| 9 | 9 | all: options dwm |
| 1 | 1 | /* See LICENSE file for copyright and license details. */ |
|
| 2 | + | #include <stdio.h> |
|
| 2 | 3 | #include <stdlib.h> |
|
| 3 | 4 | #include <X11/Xlib.h> |
|
| 4 | 5 | ||
| 5 | 6 | #include "draw.h" |
|
| 7 | + | #include "util.h" |
|
| 6 | 8 | ||
| 7 | 9 | Draw * |
|
| 8 | 10 | draw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h) { |
|
| 36 | 38 | } |
|
| 37 | 39 | ||
| 38 | 40 | Fnt * |
|
| 39 | - | font_create(const char *fontname) { |
|
| 40 | - | Fnt *font = (Fnt *)calloc(1, sizeof(Fnt)); |
|
| 41 | - | /* TODO: allocate actual font */ |
|
| 41 | + | draw_font_create(Draw *draw, const char *fontname) { |
|
| 42 | + | Fnt *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 | 75 | return font; |
|
| 43 | 76 | } |
|
| 44 | 77 | ||
| 45 | 78 | void |
|
| 46 | - | font_free(Fnt *font) { |
|
| 47 | - | if(!font) |
|
| 79 | + | draw_font_free(Draw *draw, Fnt *font) { |
|
| 80 | + | if(!draw || !font) |
|
| 48 | 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 | 86 | free(font); |
|
| 51 | 87 | } |
|
| 52 | 88 | ||
| 53 | 89 | Col * |
|
| 54 | - | col_create(const char *colname) { |
|
| 90 | + | draw_col_create(Draw *draw, const char *colname) { |
|
| 55 | 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 | 98 | return col; |
|
| 58 | 99 | } |
|
| 59 | 100 | ||
| 60 | 101 | void |
|
| 61 | - | col_free(Col *col) { |
|
| 102 | + | draw_col_free(Draw *draw, Col *col) { |
|
| 62 | 103 | if(!col) |
|
| 63 | 104 | return; |
|
| 64 | - | /* TODO: deallocate any color resource */ |
|
| 65 | 105 | free(col); |
|
| 66 | 106 | } |
|
| 67 | 107 | ||
| 8 | 8 | struct _XFont { |
|
| 9 | 9 | int ascent; |
|
| 10 | 10 | int descent; |
|
| 11 | - | unsigned int h, w; |
|
| 11 | + | unsigned int h; |
|
| 12 | 12 | XFontSet set; |
|
| 13 | 13 | XFontStruct *xfont; |
|
| 14 | 14 | }; |
|
| 45 | 45 | void draw_free(Draw *draw); |
|
| 46 | 46 | ||
| 47 | 47 | /* Fnt abstraction */ |
|
| 48 | - | Fnt *font_create(const char *fontname); |
|
| 49 | - | void font_free(Fnt *font); |
|
| 48 | + | Fnt *draw_font_create(Draw *draw, const char *fontname); |
|
| 49 | + | void draw_font_free(Draw *draw, Fnt *font); |
|
| 50 | 50 | ||
| 51 | 51 | /* Colour abstraction */ |
|
| 52 | - | Col *col_create(const char *colname); |
|
| 53 | - | void col_free(Col *col); |
|
| 52 | + | Col *draw_col_create(Draw *draw, const char *colname); |
|
| 53 | + | void draw_col_free(Draw *draw, Col *col); |
|
| 54 | 54 | ||
| 55 | 55 | /* Drawing context manipulation */ |
|
| 56 | 56 | void draw_setfont(Draw *draw, Fnt *font); |
|
| 41 | 41 | #endif /* XINERAMA */ |
|
| 42 | 42 | ||
| 43 | 43 | #include "draw.h" |
|
| 44 | + | #include "util.h" |
|
| 44 | 45 | ||
| 45 | 46 | /* macros */ |
|
| 46 | 47 | #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask) |
|
| 49 | 50 | * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy))) |
|
| 50 | 51 | #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags])) |
|
| 51 | 52 | #define LENGTH(X) (sizeof X / sizeof X[0]) |
|
| 52 | - | #define MAX(A, B) ((A) > (B) ? (A) : (B)) |
|
| 53 | - | #define MIN(A, B) ((A) < (B) ? (A) : (B)) |
|
| 54 | 53 | #define MOUSEMASK (BUTTONMASK|PointerMotionMask) |
|
| 55 | 54 | #define WIDTH(X) ((X)->w + 2 * (X)->bw) |
|
| 56 | 55 | #define HEIGHT(X) ((X)->h + 2 * (X)->bw) |
|
| 176 | 175 | static void destroynotify(XEvent *e); |
|
| 177 | 176 | static void detach(Client *c); |
|
| 178 | 177 | static void detachstack(Client *c); |
|
| 179 | - | static void die(const char *errstr, ...); |
|
| 180 | 178 | static Monitor *dirtomon(int dir); |
|
| 181 | 179 | static void drawbar(Monitor *m); |
|
| 182 | 180 | static void drawbars(void); |
|
| 693 | 691 | for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext); |
|
| 694 | 692 | c->mon->sel = t; |
|
| 695 | 693 | } |
|
| 696 | - | } |
|
| 697 | - | ||
| 698 | - | void |
|
| 699 | - | die(const char *errstr, ...) { |
|
| 700 | - | va_list ap; |
|
| 701 | - | ||
| 702 | - | va_start(ap, errstr); |
|
| 703 | - | vfprintf(stderr, errstr, ap); |
|
| 704 | - | va_end(ap); |
|
| 705 | - | exit(EXIT_FAILURE); |
|
| 706 | 694 | } |
|
| 707 | 695 | ||
| 708 | 696 | Monitor * |
|
| 1 | + | /* See LICENSE file for copyright and license details. */ |
|
| 2 | + | #include <stdarg.h> |
|
| 3 | + | #include <stdio.h> |
|
| 4 | + | #include <stdlib.h> |
|
| 5 | + | ||
| 6 | + | #include "util.h" |
|
| 7 | + | ||
| 8 | + | void |
|
| 9 | + | die(const char *errstr, ...) { |
|
| 10 | + | va_list ap; |
|
| 11 | + | ||
| 12 | + | va_start(ap, errstr); |
|
| 13 | + | vfprintf(stderr, errstr, ap); |
|
| 14 | + | va_end(ap); |
|
| 15 | + | exit(EXIT_FAILURE); |
|
| 16 | + | } |
|
| 17 | + |
| 1 | + | /* See LICENSE file for copyright and license details. */ |
|
| 2 | + | ||
| 3 | + | #define MAX(A, B) ((A) > (B) ? (A) : (B)) |
|
| 4 | + | #define MIN(A, B) ((A) < (B) ? (A) : (B)) |
|
| 5 | + | ||
| 6 | + | void die(const char *errstr, ...); |