From 9d312db24f977d42795890ece1fe78925273b7fc Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sun, 27 Nov 2016 20:06:08 -0500 Subject: [PATCH] Updated xpick.c to use the new x11 API. --- Makefile | 5 +++-- inc/x11.h | 1 + libx/x11.c | 15 +++++++++++++++ xpick.c | 46 ++++++++++++++-------------------------------- 4 files changed, 33 insertions(+), 34 deletions(-) diff --git a/Makefile b/Makefile index e8e6a50..12b1afb 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ TEST_OBJS = \ include config.mk -all: xedit +all: xedit xpick clean: $(RM) *.o lib*/*.o test/*.o *.a xpick xedit unittests @@ -29,7 +29,8 @@ test: unittests xedit: xedit.o libx.a libedit.a $(LD) -o $@ $^ $(LDFLAGS) -#xpick: xpick.o libx.a libedit.a +xpick: xpick.o libx.a libedit.a + $(LD) -o $@ $^ $(LDFLAGS) libedit.a: $(LIBEDIT_OBJS) $(AR) $(ARFLAGS) $@ $^ diff --git a/inc/x11.h b/inc/x11.h index aed8fee..a3ee126 100644 --- a/inc/x11.h +++ b/inc/x11.h @@ -124,3 +124,4 @@ void x11_draw_utf8(XFont font, int fg, int bg, int x, int y, char* str); void x11_font_getglyph(XFont font, XGlyphSpec* spec, uint32_t rune); size_t x11_font_getglyphs(XGlyphSpec* specs, const XGlyph* glyphs, int len, XFont font, int x, int y); void x11_draw_glyphs(int fg, int bg, XGlyphSpec* glyphs, size_t nglyphs); +void x11_draw_utf8(XFont font, int fg, int bg, int x, int y, char* str); diff --git a/libx/x11.c b/libx/x11.c index ec77fc0..d700c7b 100644 --- a/libx/x11.c +++ b/libx/x11.c @@ -375,6 +375,21 @@ void x11_draw_glyphs(int fg, int bg, XGlyphSpec* specs, size_t nspecs) { XftColorFree(X.display, X.visual, X.colormap, &fgc); } +void x11_draw_utf8(XFont fnt, int fg, int bg, int x, int y, char* str) { + struct XFont* font = fnt; + static XftGlyphFontSpec specs[256]; + size_t nspecs = 0; + while (*str && nspecs < 256) { + x11_font_getglyph(font, (XGlyphSpec*)&(specs[nspecs]), *str); + specs[nspecs].x = x; + specs[nspecs].y = y; + x += font->base.width; + nspecs++; + str++; + } + x11_draw_glyphs(fg, bg, (XGlyphSpec*)specs, nspecs); +} + //void x11_warp_mouse(int x, int y) { // XWarpPointer(X.display, X.window, X.window, 0, 0, X.width, X.height, x, y); //} diff --git a/xpick.c b/xpick.c index 72e5866..d77ec44 100644 --- a/xpick.c +++ b/xpick.c @@ -21,30 +21,12 @@ static unsigned Pos = 0; static Buf Query; static vec_t Choices = {0}; static size_t ChoiceIdx = 0; -static XFont Fonts; +static XFont Font; static XConfig Config = { .redraw = redraw, .handle_key = keyboard_input, .handle_mouse = mouse_input, - .palette = { - /* ARGB color values */ - 0xff002b36, - 0xff073642, - 0xff586e75, - 0xff657b83, - 0xff839496, - 0xff93a1a1, - 0xffeee8d5, - 0xfffdf6e3, - 0xffb58900, - 0xffcb4b16, - 0xffdc322f, - 0xffd33682, - 0xff6c71c4, - 0xff268bd2, - 0xff2aa198, - 0xff859900 - } + .palette = COLOR_PALETTE }; static char* rdline(FILE* fin) { @@ -138,10 +120,10 @@ static void score(void) { vec_sort(&Choices, by_score); } -static void draw_runes(unsigned x, unsigned y, int fg, int bg, XGlyph* glyphs, size_t rlen) { - XftGlyphFontSpec specs[rlen]; +static void draw_runes(size_t x, size_t y, int fg, int bg, UGlyph* glyphs, size_t rlen) { + XGlyphSpec specs[rlen]; while (rlen) { - size_t nspecs = x11_font_getglyphs(specs, glyphs, rlen, &Fonts, x, y); + size_t nspecs = x11_font_getglyphs(specs, (XGlyph*)glyphs, rlen, Font, x, y); x11_draw_glyphs(fg, bg, specs, nspecs); rlen -= nspecs; } @@ -150,26 +132,26 @@ static void draw_runes(unsigned x, unsigned y, int fg, int bg, XGlyph* glyphs, s static void redraw(int width, int height) { /* draw the background colors */ x11_draw_rect(CLR_BASE03, 0, 0, width, height); - x11_draw_rect(CLR_BASE02, 0, 0, width, Fonts.base.height); - x11_draw_rect(CLR_BASE01, 0, Fonts.base.height, width, 1); + x11_draw_rect(CLR_BASE02, 0, 0, width, x11_font_height(Font)); + x11_draw_rect(CLR_BASE01, 0, x11_font_height(Font), width, 1); /* create the array for the query glyphs */ - int rows = height / Fonts.base.height - 1; - int cols = width / Fonts.base.width; + int rows = height / x11_font_height(Font) - 1; + int cols = width / x11_font_width(Font); XGlyph glyphs[cols], *text = glyphs; /* draw the query */ unsigned start = 0, end = buf_end(&Query); while (start < end && start < cols) (text++)->rune = buf_get(&Query, start++); - draw_runes(0, 0, CLR_BASE3, CLR_BASE03, glyphs, text - glyphs); + draw_runes(0, 0, CLR_BASE3, CLR_BASE03, (UGlyph*)glyphs, text - glyphs); /* Draw the choices */ size_t off = (ChoiceIdx >= rows ? (ChoiceIdx-rows+1) : 0); for (size_t i = 0; i < vec_size(&Choices) && i < rows; i++) { Choice* choice = vec_at(&Choices, i+off); if (i+off == ChoiceIdx) { - x11_draw_rect(CLR_BASE1, 0, ((i+1) * Fonts.base.height)+Fonts.base.descent, width, Fonts.base.height); - x11_draw_utf8(&Fonts, CLR_BASE03, CLR_BASE1, 0, (i+2) * Fonts.base.height, choice->string); + x11_draw_rect(CLR_BASE1, 0, ((i+1) * x11_font_height(Font))+x11_font_descent(Font), width, x11_font_height(Font)); + x11_draw_utf8(Font, CLR_BASE03, CLR_BASE1, 0, (i+2) * x11_font_height(Font), choice->string); } else { - x11_draw_utf8(&Fonts, CLR_BASE1, CLR_BASE03, 0, (i+2) * Fonts.base.height, choice->string); + x11_draw_utf8(Font, CLR_BASE1, CLR_BASE03, 0, (i+2) * x11_font_height(Font), choice->string); } } } @@ -219,7 +201,7 @@ int main(int argc, char** argv) { x11_init(&Config); x11_dialog("pick", Width, Height); x11_show(); - x11_font_load(&Fonts, FONTNAME); + Font = x11_font_load(FONTNAME); x11_loop(); /* print out the choice */ if (vec_size(&Choices) && ChoiceIdx != SIZE_MAX) { -- 2.51.0