diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | draw.c | 60 | ||||
| -rw-r--r-- | draw.h | 10 | ||||
| -rw-r--r-- | dwm.c | 14 | ||||
| -rw-r--r-- | util.c | 17 | ||||
| -rw-r--r-- | util.h | 6 |
6 files changed, 80 insertions, 29 deletions
| @@ -3,7 +3,7 @@ | |||
| 3 | 3 | ||
| 4 | include config.mk | 4 | include config.mk |
| 5 | 5 | ||
| 6 | SRC = draw.c dwm.c | 6 | SRC = util.c draw.c dwm.c |
| 7 | OBJ = ${SRC:.c=.o} | 7 | OBJ = ${SRC:.c=.o} |
| 8 | 8 | ||
| 9 | all: options dwm | 9 | all: options dwm |
| @@ -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 | ||
| @@ -8,7 +8,7 @@ typedef struct _XCol Col; | |||
| 8 | struct _XFont { | 8 | struct _XFont { |
| 9 | int ascent; | 9 | int ascent; |
| 10 | int descent; | 10 | int descent; |
| 11 | unsigned int h, w; | 11 | unsigned int h; |
| 12 | XFontSet set; | 12 | XFontSet set; |
| 13 | XFontStruct *xfont; | 13 | XFontStruct *xfont; |
| 14 | }; | 14 | }; |
| @@ -45,12 +45,12 @@ void draw_resize(Draw *draw, unsigned int w, unsigned int h); | |||
| 45 | void draw_free(Draw *draw); | 45 | void draw_free(Draw *draw); |
| 46 | 46 | ||
| 47 | /* Fnt abstraction */ | 47 | /* Fnt abstraction */ |
| 48 | Fnt *font_create(const char *fontname); | 48 | Fnt *draw_font_create(Draw *draw, const char *fontname); |
| 49 | void font_free(Fnt *font); | 49 | void draw_font_free(Draw *draw, Fnt *font); |
| 50 | 50 | ||
| 51 | /* Colour abstraction */ | 51 | /* Colour abstraction */ |
| 52 | Col *col_create(const char *colname); | 52 | Col *draw_col_create(Draw *draw, const char *colname); |
| 53 | void col_free(Col *col); | 53 | void draw_col_free(Draw *draw, Col *col); |
| 54 | 54 | ||
| 55 | /* Drawing context manipulation */ | 55 | /* Drawing context manipulation */ |
| 56 | void draw_setfont(Draw *draw, Fnt *font); | 56 | void draw_setfont(Draw *draw, Fnt *font); |
| @@ -41,6 +41,7 @@ | |||
| 41 | #endif /* XINERAMA */ | 41 | #endif /* XINERAMA */ |
| 42 | 42 | ||
| 43 | #include "draw.h" | 43 | #include "draw.h" |
| 44 | #include "util.h" | ||
| 44 | 45 | ||
| 45 | /* macros */ | 46 | /* macros */ |
| 46 | #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask) | 47 | #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask) |
| @@ -49,8 +50,6 @@ | |||
| 49 | * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy))) | 50 | * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy))) |
| 50 | #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags])) | 51 | #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags])) |
| 51 | #define LENGTH(X) (sizeof X / sizeof X[0]) | 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 | #define MOUSEMASK (BUTTONMASK|PointerMotionMask) | 53 | #define MOUSEMASK (BUTTONMASK|PointerMotionMask) |
| 55 | #define WIDTH(X) ((X)->w + 2 * (X)->bw) | 54 | #define WIDTH(X) ((X)->w + 2 * (X)->bw) |
| 56 | #define HEIGHT(X) ((X)->h + 2 * (X)->bw) | 55 | #define HEIGHT(X) ((X)->h + 2 * (X)->bw) |
| @@ -176,7 +175,6 @@ static Monitor *createmon(void); | |||
| 176 | static void destroynotify(XEvent *e); | 175 | static void destroynotify(XEvent *e); |
| 177 | static void detach(Client *c); | 176 | static void detach(Client *c); |
| 178 | static void detachstack(Client *c); | 177 | static void detachstack(Client *c); |
| 179 | static void die(const char *errstr, ...); | ||
| 180 | static Monitor *dirtomon(int dir); | 178 | static Monitor *dirtomon(int dir); |
| 181 | static void drawbar(Monitor *m); | 179 | static void drawbar(Monitor *m); |
| 182 | static void drawbars(void); | 180 | static void drawbars(void); |
| @@ -695,16 +693,6 @@ detachstack(Client *c) { | |||
| 695 | } | 693 | } |
| 696 | } | 694 | } |
| 697 | 695 | ||
| 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 | } | ||
| 707 | |||
| 708 | Monitor * | 696 | Monitor * |
| 709 | dirtomon(int dir) { | 697 | dirtomon(int dir) { |
| 710 | Monitor *m = NULL; | 698 | Monitor *m = NULL; |
| @@ -0,0 +1,17 @@ | |||
| 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 | |||
| @@ -0,0 +1,6 @@ | |||
| 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, ...); | ||
